Repository navigation
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/179661
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ❌ 10 New Failures, 179 Pending, 2 Unrelated FailuresAs of commit f533214 with merge base f11ab64 ( NEW FAILURES - The following jobs have failed:
FLAKY - The following job failed but was likely due to flakiness present on trunk:
BROKEN TRUNK - The following job failed but was present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
| auto& gen = (reinterpret_cast<THPGenerator*>(_self))->cdata; | ||
| auto device = gen.device(); | ||
| auto repr = fmt::format( | ||
| "torch.cuda.default_generators[{}].clone_state()", device.index()); |
There was a problem hiding this comment.
it seems like generators can support any device. wouldnt this hardcode cuda?
### Summary Replaces the `OpaqueGenerator` Python wrapper from #179658 by making `torch._C.Generator` itself a proper opaque value type. This eliminates the wrapper class entirely. C++ changes to `THPGenerator`: - Set `OpaqueBaseMeta` as the metaclass (same pattern as `ProcessGroup`) - Add weakref support (`tp_weaklistoffset` + `weakreflist` field) - Add `tp_richcompare` (identity-based `__eq__`/`__ne__`) - Add `tp_hash` (identity-based) - Add `__fx_repr__` method for FX codegen Also fixes `THPModule_addDocStr` to use `PyType_Check(obj)` instead of `Py_TYPE(obj) == &PyType_Type`, since Generator's metaclass is now `OpaqueBaseMeta` rather than `type`. With these changes, `torch._C.Generator` is registered directly via `register_opaque_type(torch._C.Generator, typ="value")` and flows through `make_fx`, FX codegen, and the repro infrastructure without any wrapper. Authored with Claude. ### Test plan - `python test/dynamo/test_after_aot.py` (all 6 tests) - `python test/inductor/test_torchinductor.py -k test_randn_generator_cuda` - `python test/test_opaque_obj_v2.py TestOpaqueGenerator` (make_fx + correctness) - `python agent_space/repro.py` (end-to-end repro) cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo Lucaskabela azahed98 [ghstack-poisoned]
| value, | ||
| source=self.source, | ||
| ) | ||
| elif isinstance(value, torch._C.Generator): |
There was a problem hiding this comment.
what are the issues with going through the TorchScriptObjectVariable? or you just want to leave this as a TODO for later? If so can you leave a comment?
There was a problem hiding this comment.
The TorchScriptObjectVariable path assumes the object can be converted to a FakeScriptObject via
maybe_to_fake_obj (line 1613 in builder.py), which doesn't work for Generator yet, and cause a segfault. Probably because MemberType.USE_REAL and MemberType.INLINED are not properly setup. For now I don't need it to work with dynamo, so I just error out. I can add a TODO.
There was a problem hiding this comment.
tried to fix that, but then there is another issue:
fake tensor execution can't use a
FakeScriptObject as a real generator — ops like torch.randn(generator=...) actually invoke the
generator during tracing. Fixing that would require teaching the fake tensor layer to pass
through the real generator (from FakeScriptObject.real_obj) when it sees an opaque Generator,
which is a larger follow-up.
### Summary Replaces the `OpaqueGenerator` Python wrapper from #179658 by making `torch._C.Generator` itself a proper opaque value type. This eliminates the wrapper class entirely. C++ changes to `THPGenerator`: - Set `OpaqueBaseMeta` as the metaclass (same pattern as `ProcessGroup`) - Add weakref support (`tp_weaklistoffset` + `weakreflist` field) - Add `tp_richcompare` (identity-based `__eq__`/`__ne__`) - Add `tp_hash` (identity-based) - Add `__fx_repr__` method for FX codegen Also fixes `THPModule_addDocStr` to use `PyType_Check(obj)` instead of `Py_TYPE(obj) == &PyType_Type`, since Generator's metaclass is now `OpaqueBaseMeta` rather than `type`. With these changes, `torch._C.Generator` is registered directly via `register_opaque_type(torch._C.Generator, typ="value")` and flows through `make_fx`, FX codegen, and the repro infrastructure without any wrapper. Authored with Claude. ### Test plan - `python test/dynamo/test_after_aot.py` (all 6 tests) - `python test/inductor/test_torchinductor.py -k test_randn_generator_cuda` - `python test/test_opaque_obj_v2.py TestOpaqueGenerator` (make_fx + correctness) - ` python test/test_torch.py -k test_pickle_generator` (generator can be pickled) - `python test/dynamo/test_dynamic_shapes.py -k test_user_getattr1_dynamic_shapes` - `python agent_space/repro.py` (end-to-end repro) cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo Lucaskabela azahed98 [ghstack-poisoned]
colesbury
left a comment
There was a problem hiding this comment.
A few drive-by comments
| PyObject* module_name = PyUnicode_FromString("torch._C"); | ||
| if (!module_name) | ||
| return false; | ||
| PyDict_SetItemString(THPGeneratorType.tp_dict, "__module__", module_name); |
There was a problem hiding this comment.
Should probably check the return value of PyDict_SetItemString for errors here.
There was a problem hiding this comment.
Changed to
if (PyDict_SetItemString(
THPGeneratorType.tp_dict, "__module__", module_name) < 0)
return false;
|
|
||
| // force ATen to initialize because it handles | ||
| // setting up TH Errors so that they throw C++ exceptions | ||
| // setting up TH Errors so that they throw C++ exceptions // @allow-raw-throw |
There was a problem hiding this comment.
this one is actually a false positive, removed.
I do see the following error when the other raw-throw are not marked, but if I mark the other two, this one does not need to be marked.
Error (RAWTHROW) raw throw statement
Do not use raw `throw` in C++ code. Use TORCH_CHECK, TORCH_CHECK_WITH,
C10_THROW_ERROR, or other error-reporting macros instead. See c10/
util/Exception.h for available macros. If this is a re-throw (throw;),
restructure the code to avoid try/catch/throw or add the file to the
RAWTHROW exclude list in .lintrunner.toml with justification.
2382 | ASSERT_TRUE(set_module_attr("has_spectral", has_spectral));
2383 |
2384 | // force ATen to initialize because it handles
>>> 2385 | // setting up TH Errors so that they throw C++ exceptions
2386 | at::init();
2387 |
2388 | // Automatically translate errors thrown from pybind11 functions
| bool THPGenerator_init(PyObject* module) { | ||
| // Set OpaqueBaseMeta as the metaclass so Generator can be registered as an | ||
| // opaque type for FX tracing (same pattern as ProcessGroup). | ||
| PyObject* opaque_module = PyImport_ImportModule("torch._opaque_base"); |
There was a problem hiding this comment.
This leaks on errors/exceptions below. You can avoid it with THPObjectPtr or similar.
There was a problem hiding this comment.
Changed to below:
auto opaque_module =
THPObjectPtr(PyImport_ImportModule("torch._opaque_base"));
TORCH_CHECK(opaque_module, "Failed to import torch._opaque_base");
auto opaque_base_meta =
THPObjectPtr(PyObject_GetAttrString(opaque_module, "OpaqueBaseMeta"));
TORCH_CHECK(opaque_base_meta, "Failed to get OpaqueBaseMeta");
Py_SET_TYPE(&THPGeneratorType, (PyTypeObject*)opaque_base_meta.get());
// Prevent THPObjectPtr from decref-ing; the ref is now owned by ob_type.
opaque_base_meta.release();
### Summary Replaces the `OpaqueGenerator` Python wrapper from #179658 by making `torch._C.Generator` itself a proper opaque value type. This eliminates the wrapper class entirely. C++ changes to `THPGenerator`: - Set `OpaqueBaseMeta` as the metaclass (same pattern as `ProcessGroup`) - Add weakref support (`tp_weaklistoffset` + `weakreflist` field) - Add `tp_richcompare` (identity-based `__eq__`/`__ne__`) - Add `tp_hash` (identity-based) - Add `__fx_repr__` method for FX codegen Also fixes `THPModule_addDocStr` to use `PyType_Check(obj)` instead of `Py_TYPE(obj) == &PyType_Type`, since Generator's metaclass is now `OpaqueBaseMeta` rather than `type`. With these changes, `torch._C.Generator` is registered directly via `register_opaque_type(torch._C.Generator, typ="value")` and flows through `make_fx`, FX codegen, and the repro infrastructure without any wrapper. Authored with Claude. ### Test plan - `python test/dynamo/test_after_aot.py` (all 6 tests) - `python test/inductor/test_torchinductor.py -k test_randn_generator_cuda` - `python test/test_opaque_obj_v2.py TestOpaqueGenerator` (make_fx + correctness) - ` python test/test_torch.py -k test_pickle_generator` (generator can be pickled) - `python test/dynamo/test_dynamic_shapes.py -k test_user_getattr1_dynamic_shapes` - `python agent_space/repro.py` (end-to-end repro) cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo Lucaskabela azahed98 [ghstack-poisoned]
### Summary Replaces the `OpaqueGenerator` Python wrapper from #179658 by making `torch._C.Generator` itself a proper opaque value type. This eliminates the wrapper class entirely. C++ changes to `THPGenerator`: - Set `OpaqueBaseMeta` as the metaclass (same pattern as `ProcessGroup`) - Add weakref support (`tp_weaklistoffset` + `weakreflist` field) - Add `tp_richcompare` (identity-based `__eq__`/`__ne__`) - Add `tp_hash` (identity-based) - Add `__fx_repr__` method for FX codegen Also fixes `THPModule_addDocStr` to use `PyType_Check(obj)` instead of `Py_TYPE(obj) == &PyType_Type`, since Generator's metaclass is now `OpaqueBaseMeta` rather than `type`. With these changes, `torch._C.Generator` is registered directly via `register_opaque_type(torch._C.Generator, typ="value")` and flows through `make_fx`, FX codegen, and the repro infrastructure without any wrapper. Authored with Claude. ### Test plan - `python test/dynamo/test_after_aot.py` (all 6 tests) - `python test/inductor/test_torchinductor.py -k test_randn_generator_cuda` - `python test/test_opaque_obj_v2.py TestOpaqueGenerator` (make_fx + correctness) - ` python test/test_torch.py -k test_pickle_generator` (generator can be pickled) - `python test/dynamo/test_dynamic_shapes.py -k test_user_getattr1_dynamic_shapes` - `python agent_space/repro.py` (end-to-end repro) cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo Lucaskabela azahed98 [ghstack-poisoned]
| Py_SET_TYPE(&THPGeneratorType, (PyTypeObject*)opaque_base_meta.get()); | ||
| // Prevent THPObjectPtr from decref-ing; the ref is now owned by ob_type. | ||
| opaque_base_meta.release(); |
There was a problem hiding this comment.
I think it's usually good practice to combine "release()" with the thing that takes ownership, i.e.:
Py_SET_TYPE(&THPGeneratorType, (PyTypeObject*)opaque_base_meta.release());
There was a problem hiding this comment.
Other than the above minor comment, all the Python C API stuff looks good to me.
### Summary Replaces the `OpaqueGenerator` Python wrapper from #179658 by making `torch._C.Generator` itself a proper opaque reference type. This eliminates the wrapper class entirely. C++ changes to `THPGenerator`: - Set `OpaqueBaseMeta` as the metaclass (same pattern as `ProcessGroup`) - Add weakref support (`tp_weaklistoffset` + `weakreflist` field) - Add `tp_richcompare` (identity-based `__eq__`/`__ne__`) - Add `tp_hash` (identity-based) - Add `__fx_repr__` method for FX codegen Also fixes `THPModule_addDocStr` to use `PyType_Check(obj)` instead of `Py_TYPE(obj) == &PyType_Type`, since Generator's metaclass is now `OpaqueBaseMeta` rather than `type`. With these changes, `torch._C.Generator` is registered directly via `register_opaque_type(torch._C.Generator, typ="value")` and flows through `make_fx`, FX codegen, and the repro infrastructure without any wrapper. Authored with Claude. ### Test plan - `python test/dynamo/test_after_aot.py` (all 6 tests) - `python test/inductor/test_torchinductor.py -k test_randn_generator_cuda` - `python test/test_opaque_obj_v2.py TestOpaqueGenerator` (make_fx + correctness) - ` python test/test_torch.py -k test_pickle_generator` (generator can be pickled) - `python test/dynamo/test_dynamic_shapes.py -k test_user_getattr1_dynamic_shapes` - `python agent_space/repro.py` (end-to-end repro) cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo Lucaskabela azahed98 [ghstack-poisoned]
|
Hi @yushangdi please test windows wheel builds on this PR we suspect it broke these earlier today when it landed. Errors can be seen here: #181160 Claude explanation: |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 7 checks: pull / linux-jammy-py3.14-clang18 / test (dynamo_wrapped, 1, 3, lf.linux.2xlarge), pull / linux-jammy-py3.14t-clang18 / test (dynamo_wrapped, 1, 3, lf.linux.4xlarge), pull / linux-jammy-py3.10-clang18 / test (dynamo_wrapped, 1, 3, lf.linux.2xlarge), pull / linux-jammy-py3.10-clang18 / test (dynamo_wrapped, 2, 3, lf.linux.2xlarge), pull / linux-jammy-py3.10-clang18 / test (dynamo_wrapped, 3, 3, lf.linux.2xlarge), trunk / linux-jammy-rocm-py3.10-mi355 / test (default, 5, 6, linux.rocm.gpu.gfx950.1, unstable), linux-aarch64-binary-manywheel / manywheel-py3_13t-cuda-aarch64-13_0-build / build Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 1 jobs have failed, first few of them are: trunk / linux-jammy-rocm-py3.10-mi355 / test (distributed, 1, 3, linux.rocm.gpu.gfx950.2) Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 8 checks: pull / linux-jammy-py3.14-clang18 / test (dynamo_wrapped, 1, 3, lf.linux.2xlarge), pull / linux-jammy-py3.14t-clang18 / test (dynamo_wrapped, 1, 3, lf.linux.4xlarge), pull / linux-jammy-py3.10-clang18 / test (dynamo_wrapped, 1, 3, lf.linux.2xlarge), pull / linux-jammy-py3.10-clang18 / test (dynamo_wrapped, 2, 3, lf.linux.2xlarge), pull / linux-jammy-py3.10-clang18 / test (dynamo_wrapped, 3, 3, lf.linux.2xlarge), trunk / linux-jammy-rocm-py3.10-mi355 / test (distributed, 1, 3, linux.rocm.gpu.gfx950.2), trunk / linux-jammy-rocm-py3.10-mi355 / test (default, 5, 6, linux.rocm.gpu.gfx950.1, unstable), linux-aarch64-binary-manywheel / manywheel-py3_13t-cuda-aarch64-13_0-build / build Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
@pytorchmergebot revert -c "sorry need to revert again, not sure why windows builds where not executed" |
|
❌ 🤖 pytorchbot command failed: Try |
|
@pytorchmergebot revert -c nosignal -m "sorry need to revert again, not sure why windows builds where not executed" |
|
@pytorchbot successfully started a revert job. Check the current status here. |
|
@yushangdi your PR has been successfully reverted. |
|
@pytorchmergebot rebase -b main |
|
@pytorchbot started a rebase job onto refs/remotes/origin/main. Check the current status here |
|
Rebase failed due to |
|
@pytorchbot merge -f "reland, reverted because another PR caused windows build fail and we got confused" |
Merge startedYour change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
note: windows build is failing after the revert of this PR as well: https://github.com/pytorch/pytorch/actions/runs/24897314506/job/72926901529?pr=181274 |
Stack from ghstack (oldest at bottom):
Summary
Replaces the
OpaqueGeneratorPython wrapper from #179658 by makingtorch._C.Generatoritself a proper opaque reference type. This eliminates thewrapper class entirely.
C++ changes to
THPGenerator:OpaqueBaseMetaas the metaclass (same pattern asProcessGroup)tp_weaklistoffset+weakreflistfield)tp_richcompare(identity-based__eq__/__ne__)tp_hash(identity-based)__fx_repr__method for FX codegenAlso fixes
THPModule_addDocStrto usePyType_Check(obj)instead ofPy_TYPE(obj) == &PyType_Type, since Generator's metaclass is nowOpaqueBaseMetarather thantype.With these changes,
torch._C.Generatoris registered directly viaregister_opaque_type(torch._C.Generator, typ="value")and flows throughmake_fx, FX codegen, and the repro infrastructure without any wrapper.Authored with Claude.
Test plan
python test/dynamo/test_after_aot.py(all 6 tests)python test/inductor/test_torchinductor.py -k test_randn_generator_cudapython test/test_opaque_obj_v2.py TestOpaqueGenerator(make_fx + correctness)python test/test_torch.py -k test_pickle_generator(generator can be pickled)python test/dynamo/test_dynamic_shapes.py -k test_user_getattr1_dynamic_shapespython agent_space/repro.py(end-to-end repro)python test/test_fx.py -k test_impure_random(Generator is a C type, it has init without code)python test/dynamo/test_misc.py -k test_torch_generator_initial_seedcc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo @Lucaskabela @azahed98