Revert "[User-streams] Make torch.Event weakref compatible (#164522)"

This reverts commit cde81e92b9.

Reverted https://github.com/pytorch/pytorch/pull/164522 on behalf of https://github.com/atalman due to Breaks periodic: test/dynamo/test_streams.py::TestStreams::test_stream_weakref [GH job link](https://github.com/pytorch/pytorch/actions/runs/18909552619/job/53979171605) [HUD commit link](cde81e92b9) ([comment](https://github.com/pytorch/pytorch/pull/164522#issuecomment-3462450571))
This commit is contained in:
PyTorch MergeBot 2025-10-29 16:03:03 +00:00
parent eae701cad0
commit 5cdbcb5233
4 changed files with 23 additions and 12 deletions

View File

@ -234,6 +234,27 @@ class InPlaceCompilationTests(TestCase):
with self.assertRaises(IndexError): with self.assertRaises(IndexError):
fn(torch.randn(10), 99) fn(torch.randn(10), 99)
def test_list_bad_weakref(self):
import weakref
a = torch.Event()
with self.assertRaises(TypeError):
weakref.ref(a)
@torch.compile(backend="eager")
class Mod(torch.nn.Module):
def __init__(self, event):
super().__init__()
self.event = event
def forward(self, x):
return x * int(self.event.query())
e = torch.Event()
m = Mod(e)
a = torch.randn(10)
self.assertEqual(m(a), a)
# The private variants of the below functions are extensively tested # The private variants of the below functions are extensively tested
# So as long as the signatures match we're good # So as long as the signatures match we're good

View File

@ -20,10 +20,6 @@ class TestStreams(torch._dynamo.test_case.TestCase):
s = torch.Stream() s = torch.Stream()
weakref.ref(s) weakref.ref(s)
def test_event_weakref(self):
e = torch.Event()
weakref.ref(e)
@requires_cuda @requires_cuda
def test_run_opcheck(self): def test_run_opcheck(self):
from torch._dynamo.variables.streams import fork_stream, join_stream from torch._dynamo.variables.streams import fork_stream, join_stream

View File

@ -49,7 +49,6 @@ static PyObject* THPEvent_pynew(
} }
THPEvent* self = reinterpret_cast<THPEvent*>(ptr.get()); THPEvent* self = reinterpret_cast<THPEvent*>(ptr.get());
self->weakreflist = nullptr;
// TODO: blocking and interprocess are not supported yet. To support them, the // TODO: blocking and interprocess are not supported yet. To support them, the
// flag system of c10::Event needs to be refactored. C10::Event should also // flag system of c10::Event needs to be refactored. C10::Event should also
@ -74,7 +73,6 @@ PyObject* THPEvent_new(c10::DeviceType device_type, c10::EventFlag flag) {
auto self = THPObjectPtr{type->tp_alloc(type, 0)}; auto self = THPObjectPtr{type->tp_alloc(type, 0)};
TORCH_CHECK(self, "Failed to allocate memory for Event"); TORCH_CHECK(self, "Failed to allocate memory for Event");
auto self_ = reinterpret_cast<THPEvent*>(self.get()); auto self_ = reinterpret_cast<THPEvent*>(self.get());
self_->weakreflist = nullptr;
new (&self_->event) c10::Event(device_type, flag); new (&self_->event) c10::Event(device_type, flag);
return self.release(); return self.release();
} }
@ -84,7 +82,6 @@ static void THPEvent_dealloc(THPEvent* self) {
pybind11::gil_scoped_release no_gil{}; pybind11::gil_scoped_release no_gil{};
self->event.~Event(); self->event.~Event();
} }
PyObject_ClearWeakRefs((PyObject*)self);
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
} }
@ -285,8 +282,7 @@ static PyMethodDef THPEvent_methods[] = {
{"synchronize", THPEvent_synchronize, METH_NOARGS, nullptr}, {"synchronize", THPEvent_synchronize, METH_NOARGS, nullptr},
{"ipc_handle", THPEvent_ipc_handle, METH_NOARGS, nullptr}, {"ipc_handle", THPEvent_ipc_handle, METH_NOARGS, nullptr},
{nullptr}}; {nullptr}};
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
PyTypeObject THPEventType = { PyTypeObject THPEventType = {
PyVarObject_HEAD_INIT(nullptr, 0) PyVarObject_HEAD_INIT(nullptr, 0)
"torch.Event", /* tp_name */ "torch.Event", /* tp_name */
@ -312,7 +308,7 @@ PyTypeObject THPEventType = {
nullptr, /* tp_traverse */ nullptr, /* tp_traverse */
nullptr, /* tp_clear */ nullptr, /* tp_clear */
nullptr, /* tp_richcompare */ nullptr, /* tp_richcompare */
offsetof(THPEvent, weakreflist), /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
nullptr, /* tp_iter */ nullptr, /* tp_iter */
nullptr, /* tp_iternext */ nullptr, /* tp_iternext */
THPEvent_methods, /* tp_methods */ THPEvent_methods, /* tp_methods */
@ -327,7 +323,6 @@ PyTypeObject THPEventType = {
nullptr, /* tp_alloc */ nullptr, /* tp_alloc */
THPEvent_pynew, /* tp_new */ THPEvent_pynew, /* tp_new */
}; };
#pragma GCC diagnostic pop
void THPEvent_init(PyObject* module) { void THPEvent_init(PyObject* module) {
THPEventClass = &THPEventType; THPEventClass = &THPEventType;

View File

@ -7,7 +7,6 @@
struct TORCH_API THPEvent { struct TORCH_API THPEvent {
PyObject_HEAD PyObject_HEAD
c10::Event event; c10::Event event;
PyObject* weakreflist;
}; };
TORCH_API extern PyTypeObject* THPEventClass; TORCH_API extern PyTypeObject* THPEventClass;
TORCH_API extern PyTypeObject THPEventType; TORCH_API extern PyTypeObject THPEventType;