From cb242afe1b3ede4439c19dbd54e30ef8cfef1269 Mon Sep 17 00:00:00 2001 From: PyTorch MergeBot Date: Tue, 21 Apr 2026 00:09:01 +0000 Subject: [PATCH] Revert "Make Generator an Opaque Object (#179661)" This reverts commit 18357d94831c15a30dd6f490f44546ecc3c0ef2d. Reverted https://github.com/pytorch/pytorch/pull/179661 on behalf of https://github.com/yushangdi due to breaks pr_time_benchmarks ([comment](https://github.com/pytorch/pytorch/pull/179661#issuecomment-4285042651)) --- test/dynamo/test_after_aot.py | 2 + test/lazy/test_generator.py | 3 +- test/test_opaque_obj_v2.py | 84 ------------------------ torch/_dynamo/debug_utils.py | 5 +- torch/_dynamo/variables/builder.py | 12 ---- torch/_dynamo/variables/script_object.py | 7 +- torch/_opaque_base.py | 11 ++-- torch/_prims/rng_prims.py | 12 ---- torch/csrc/Generator.cpp | 33 ++-------- torch/csrc/Generator.h | 1 - torch/csrc/Module.cpp | 6 +- 11 files changed, 21 insertions(+), 155 deletions(-) diff --git a/test/dynamo/test_after_aot.py b/test/dynamo/test_after_aot.py index 7faf0165285d..67f0d34acb4d 100644 --- a/test/dynamo/test_after_aot.py +++ b/test/dynamo/test_after_aot.py @@ -135,6 +135,7 @@ reader.tensor(buf0, (3, 4, 5, 6), (120, 1, 24, 4), is_leaf=True) # x""", @unittest.skipIf(not TEST_CUDA, "requires CUDA") def test_dump_generator(self): + torch.cuda.init() gen = torch.cuda.default_generators[0].clone_state() writer = InputWriter(None) writer.generator("fwd_rng_state_0", gen) @@ -151,6 +152,7 @@ reader.tensor(buf0, (3, 4, 5, 6), (120, 1, 24, 4), is_leaf=True) # x""", @unittest.skipIf(not TEST_CUDA, "requires CUDA") def test_graphsafe_rng_repro(self): """save_graph_repro should emit reader.generator() for Generator args.""" + torch.cuda.init() gen = torch.cuda.default_generators[0].clone_state() def f(x): diff --git a/test/lazy/test_generator.py b/test/lazy/test_generator.py index 2a27ec328d2a..36cf8c52df5d 100644 --- a/test/lazy/test_generator.py +++ b/test/lazy/test_generator.py @@ -3,7 +3,7 @@ import torch import torch._lazy.metrics as metrics import torch._lazy.ts_backend -from torch.testing._internal.common_utils import run_tests, TestCase +from torch.testing._internal.common_utils import run_tests, skipIfTorchDynamo, TestCase torch._lazy.ts_backend.init() @@ -47,6 +47,7 @@ class LazyGeneratorTest(TestCase): if not torch.allclose(cpu_t2, lazy_t2.to("cpu")): raise AssertionError(f"Expected {cpu_t2}, got {lazy_t2.to('cpu')}") + @skipIfTorchDynamo("Torch Dynamo does not support torch.Generator type") def test_generator_causes_multiple_compiles(self): """ Test that inserting generators with different seed caused recompile diff --git a/test/test_opaque_obj_v2.py b/test/test_opaque_obj_v2.py index ef9eba4c674e..608c1787f090 100644 --- a/test/test_opaque_obj_v2.py +++ b/test/test_opaque_obj_v2.py @@ -3977,89 +3977,5 @@ class GraphModule(torch.nn.Module): instantiate_parametrized_tests(TestOpaqueObject) -@unittest.skipIf(not torch.cuda.is_available(), "requires CUDA") -class TestOpaqueGenerator(TestCase): - def test_make_fx_with_generator(self): - """make_fx should trace through Generator inputs as opaque values.""" - from torch._prims.rng_prims import graphsafe_run_with_rng_state - - class M(torch.nn.Module): - def forward(self, q, k, v, rng_state): - out = graphsafe_run_with_rng_state( - torch.ops.aten._scaled_dot_product_efficient_attention.default, - q, - k, - v, - None, - True, - 0.1, - True, - rng_state=rng_state, - ) - return out[0] - - q = torch.randn(2, 8, 64, 32, device="cuda", dtype=torch.float16) - k = torch.randn(2, 8, 64, 32, device="cuda", dtype=torch.float16) - v = torch.randn(2, 8, 64, 32, device="cuda", dtype=torch.float16) - gen = torch.cuda.default_generators[0].clone_state() - - gm = make_fx(M(), tracing_mode="real")(q, k, v, gen) - - # The last placeholder (generator) should be used by graphsafe_run_with_rng_state - placeholders = [n for n in gm.graph.nodes if n.op == "placeholder"] - gen_placeholder = placeholders[-1] - self.assertEqual(len(gen_placeholder.users), 1) - user = next(iter(gen_placeholder.users)) - self.assertIs(user.target, graphsafe_run_with_rng_state) - - # Verify the traced graph produces the same result as eager. - # Use dropout_p=0.0 so the result is deterministic. - class M0(torch.nn.Module): - def forward(self, q, k, v, rng_state): - out = graphsafe_run_with_rng_state( - torch.ops.aten._scaled_dot_product_efficient_attention.default, - q, - k, - v, - None, - False, - 0.0, - True, - rng_state=rng_state, - ) - return out[0] - - gen1 = torch.cuda.default_generators[0].clone_state() - gen2 = torch.cuda.default_generators[0].clone_state() - gm0 = make_fx(M0(), tracing_mode="real")(q, k, v, gen1) - expected = M0()(q, k, v, gen2) - gen3 = torch.cuda.default_generators[0].clone_state() - actual = gm0(q, k, v, gen3) - self.assertEqual(actual, expected) - - def test_make_fx_randn_with_generator(self): - """make_fx should trace torch.randn with a Generator input.""" - - def fn(a, generator): - return torch.randn([20, 20], generator=generator, device=a.device) - - gen = torch.Generator("cuda") - gm = make_fx(fn, tracing_mode="real")(torch.randn(4, device="cuda"), gen) - - # Generator is baked in as a get_attr constant (not a placeholder input) - # because torch.randn passes it directly to C++ without going through - # proxy dispatch. The generator placeholder has 0 users. - self.assertExpectedInline( - normalize_gm(gm.print_readable(False)), - """\ -class fn(torch.nn.Module): - def forward(self, a_1: "f32[4]", generator_1): - _opaque_obj0 = self._opaque_obj0 - randn: "f32[20, 20]" = torch.ops.aten.randn.generator([20, 20], generator = _opaque_obj0, device = device(type='cuda', index=0), pin_memory = False); _opaque_obj0 = None - return randn -""", - ) - - if __name__ == "__main__": run_tests() diff --git a/torch/_dynamo/debug_utils.py b/torch/_dynamo/debug_utils.py index 7b8496a97ba5..e43f45a138e2 100644 --- a/torch/_dynamo/debug_utils.py +++ b/torch/_dynamo/debug_utils.py @@ -683,9 +683,8 @@ class InputReader: def unsupported(self, name: str) -> None: self.args.append(None) - def generator(self, device_type: str, device_index: int) -> Any: - device = torch.device(device_type, device_index) - gen = torch.Generator(device=device) + def generator(self, device_type: str, device_index: int) -> torch._C.Generator: + gen = torch.cuda.default_generators[device_index].clone_state() self.args.append(gen) return gen diff --git a/torch/_dynamo/variables/builder.py b/torch/_dynamo/variables/builder.py index f516cdc38b73..4235a16b68f8 100644 --- a/torch/_dynamo/variables/builder.py +++ b/torch/_dynamo/variables/builder.py @@ -1562,18 +1562,6 @@ class VariableBuilder: value, source=self.source, ) - elif type(value) is torch._C.Generator: - # Generator is registered as an opaque reference type for make_fx - # tracing, but in dynamo we handle it as a regular object so that - # trace_rules-based graph breaks (e.g. initial_seed, manual_seed) - # work gracefully — allowing dynamo to compile code before and - # after the generator call. TorchScriptObjectVariable's var_getattr - # and call_method are decorated with @_raise_hard_error_if_graph_break, - # which turns any graph break into a hard error that falls back to - # eager for the entire function. Generator methods intentionally - # graph-break (they mutate/read RNG state), so they need the - # UserDefinedObjectVariable path which supports graceful graph breaks. - return self.wrap_user_defined(value) elif TorchScriptObjectVariable.is_matching_cls(type(value)): from ..source import ( FlattenScriptObjectSource, diff --git a/torch/_dynamo/variables/script_object.py b/torch/_dynamo/variables/script_object.py index 5cccd43ab0fb..e0d4f6cb45d1 100644 --- a/torch/_dynamo/variables/script_object.py +++ b/torch/_dynamo/variables/script_object.py @@ -167,11 +167,8 @@ class OpaqueObjectClassVariable(UserDefinedVariable): # disallow creating reference-type opaque objects in the middle of the # program if is_opaque_reference_type(self.value): - # Skip __init__ to prevent dynamo from tracing it during resume. - # C extension types (e.g. torch._C.Generator) have wrapper_descriptor - # __init__ without __code__, so guard the skip_code call. - if hasattr(self.value.__init__, "__code__"): - skip_code(self.value.__init__.__code__) + # Skip __init__ to prevent dynamo from tracing it during resume + skip_code(self.value.__init__.__code__) unimplemented( gb_type="An opaque object was created in the middle of the program.", diff --git a/torch/_opaque_base.py b/torch/_opaque_base.py index d52a75720562..4df74be088da 100644 --- a/torch/_opaque_base.py +++ b/torch/_opaque_base.py @@ -12,14 +12,11 @@ class OpaqueBaseMeta(type): if super().__instancecheck__(instance): return True - from torch._library.fake_class_registry import FakeScriptObject + if hasattr(instance, "real_obj"): + from torch._library.fake_class_registry import FakeScriptObject - # Check FakeScriptObject before hasattr to avoid triggering custom - # __getattr__ on arbitrary user objects (e.g. dict-like objects that - # raise KeyError on unknown attributes). FakeScriptObject has - # well-defined attribute access so hasattr is safe on it. - if isinstance(instance, FakeScriptObject) and hasattr(instance, "real_obj"): - return super().__instancecheck__(instance.real_obj) + if isinstance(instance, FakeScriptObject): + return super().__instancecheck__(instance.real_obj) return False diff --git a/torch/_prims/rng_prims.py b/torch/_prims/rng_prims.py index 525607c842c1..4c3e686c82a1 100644 --- a/torch/_prims/rng_prims.py +++ b/torch/_prims/rng_prims.py @@ -394,18 +394,6 @@ def register_graphsafe_run_with_rng_state_op(): graphsafe_run_with_rng_state = register_graphsafe_run_with_rng_state_op() -torch._library.opaque_object.register_opaque_type( - torch._C.Generator, - typ="reference", - guard_fn=lambda gen: [gen.device], - members={ - "device": torch._library.opaque_object.MemberType.USE_REAL, - "__eq__": torch._library.opaque_object.MemberType.USE_REAL, - "__ne__": torch._library.opaque_object.MemberType.USE_REAL, - }, -) - - def register_run_dtensor_rng_op(): """ Register a higher-order operator for DTensor distributed random operations. diff --git a/torch/csrc/Generator.cpp b/torch/csrc/Generator.cpp index 160e95c9f579..800854ce3b65 100644 --- a/torch/csrc/Generator.cpp +++ b/torch/csrc/Generator.cpp @@ -24,16 +24,14 @@ PyObject* THPGenerator_initDefaultGenerator(const at::Generator& cdata) { auto type = reinterpret_cast(THPGeneratorClass); auto self = THPObjectPtr{type->tp_alloc(type, 0)}; if (!self) - throw python_error(); // @allow-raw-throw + throw python_error(); auto self_ = reinterpret_cast(self.get()); self_->cdata = cdata; - self_->weakreflist = nullptr; return self.release(); } static void THPGenerator_dealloc(PyObject* _self) { auto self = reinterpret_cast(_self); - PyObject_ClearWeakRefs(_self); if (self->cdata.defined()) { self->cdata.set_pyobj(nullptr); self->cdata.~Generator(); @@ -118,7 +116,7 @@ static uint64_t unpack_uint64(PyObject* pyobj) { unsigned_obj = *(reinterpret_cast(&obj)); } else { // If any other type of exception happened, rethrow it - throw; // @allow-raw-throw + throw; } } return unsigned_obj; @@ -237,7 +235,7 @@ static PyObject* THPGenerator_reduce(PyObject* _self, PyObject* noargs) { auto ret = THPObjectPtr{PyTuple_New(3)}; if (!ret) - throw python_error(); // @allow-raw-throw + throw python_error(); py::object torch_module = py::module::import("torch"); py::object torch_generator = torch_module.attr("Generator"); @@ -245,14 +243,14 @@ static PyObject* THPGenerator_reduce(PyObject* _self, PyObject* noargs) { auto args = THPObjectPtr{PyTuple_New(1)}; if (!args) - throw python_error(); // @allow-raw-throw + throw python_error(); PyTuple_SET_ITEM(args.get(), 0, THPGenerator_get_device(self, nullptr)); PyTuple_SET_ITEM(ret.get(), 1, args.release()); auto state = THPObjectPtr{PyTuple_New(3)}; if (!state) - throw python_error(); // @allow-raw-throw + throw python_error(); c10::DeviceType device_type = gen.device().type(); PyTuple_SET_ITEM(state.get(), 0, THPGenerator_initialSeed(_self, nullptr)); @@ -339,7 +337,7 @@ static PyTypeObject THPGeneratorType = { nullptr, /* tp_traverse */ nullptr, /* tp_clear */ nullptr, /* tp_richcompare */ - offsetof(THPGenerator, weakreflist), /* tp_weaklistoffset */ + 0, /* tp_weaklistoffset */ nullptr, /* tp_iter */ nullptr, /* tp_iternext */ THPGenerator_methods, /* tp_methods */ @@ -356,28 +354,9 @@ static PyTypeObject THPGeneratorType = { }; 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). - 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.release()); - THPGeneratorClass = reinterpret_cast(&THPGeneratorType); if (PyType_Ready(&THPGeneratorType) < 0) return false; - // PyType_Ready inherits __module__ from the metaclass (OpaqueBaseMeta lives - // in torch._opaque_base). Override it so pickle can find the class at - // torch._C.Generator. - auto module_name = THPObjectPtr(PyUnicode_FromString("torch._C")); - if (!module_name) - return false; - if (PyDict_SetItemString( - THPGeneratorType.tp_dict, "__module__", module_name) < 0) - return false; Py_INCREF(&THPGeneratorType); PyModule_AddObject( module, "Generator", reinterpret_cast(&THPGeneratorType)); diff --git a/torch/csrc/Generator.h b/torch/csrc/Generator.h index f0f2e5c2cbb0..b5f72cb47b76 100644 --- a/torch/csrc/Generator.h +++ b/torch/csrc/Generator.h @@ -8,7 +8,6 @@ struct THPGenerator { PyObject_HEAD at::Generator cdata; - PyObject* weakreflist; }; // Creates a new Python object wrapping the default at::Generator. The reference diff --git a/torch/csrc/Module.cpp b/torch/csrc/Module.cpp index 28f61755eecc..c61ec1711927 100644 --- a/torch/csrc/Module.cpp +++ b/torch/csrc/Module.cpp @@ -241,7 +241,7 @@ static PyObject* THPModule_initExtension( auto module = THPObjectPtr(PyImport_ImportModule("torch")); if (!module) - throw python_error(); // @allow-raw-throw + throw python_error(); THPStorage_postInit(module); THPAutograd_initFunctions(); @@ -473,7 +473,7 @@ static PyObject* THPModule_addDocStr(PyObject* _unused, PyObject* args) { m->d_getset->name); } m->d_getset->doc = doc_str; - } else if (PyType_Check(obj)) { + } else if (Py_TYPE(obj) == &PyType_Type) { PyTypeObject* t = reinterpret_cast(obj); if (t->tp_doc) { return PyErr_Format( @@ -682,7 +682,7 @@ static PyObject* THPModule_torchDeviceToDLDevice( auto tuple = PyTuple_New(2); if (!tuple) { - throw python_error(); // @allow-raw-throw + throw python_error(); } PyTuple_SET_ITEM(tuple, 0, THPUtils_packInt64(dl_device.device_type));