Skip to content

Commit 5cdbcb5

Browse files
Revert "[User-streams] Make torch.Event weakref compatible (pytorch#164522)"
This reverts commit cde81e9. Reverted pytorch#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](https://hud.pytorch.org/pytorch/pytorch/commit/cde81e92b95eee9af2879c9c75f7b03699ca72ad) ([comment](pytorch#164522 (comment)))
1 parent eae701c commit 5cdbcb5

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

test/dynamo/test_compile.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,27 @@ def fn(x, y):
234234
with self.assertRaises(IndexError):
235235
fn(torch.randn(10), 99)
236236

237+
def test_list_bad_weakref(self):
238+
import weakref
239+
240+
a = torch.Event()
241+
with self.assertRaises(TypeError):
242+
weakref.ref(a)
243+
244+
@torch.compile(backend="eager")
245+
class Mod(torch.nn.Module):
246+
def __init__(self, event):
247+
super().__init__()
248+
self.event = event
249+
250+
def forward(self, x):
251+
return x * int(self.event.query())
252+
253+
e = torch.Event()
254+
m = Mod(e)
255+
a = torch.randn(10)
256+
self.assertEqual(m(a), a)
257+
237258

238259
# The private variants of the below functions are extensively tested
239260
# So as long as the signatures match we're good

test/dynamo/test_streams.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ def test_stream_weakref(self):
2020
s = torch.Stream()
2121
weakref.ref(s)
2222

23-
def test_event_weakref(self):
24-
e = torch.Event()
25-
weakref.ref(e)
26-
2723
@requires_cuda
2824
def test_run_opcheck(self):
2925
from torch._dynamo.variables.streams import fork_stream, join_stream

torch/csrc/Event.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ static PyObject* THPEvent_pynew(
4949
}
5050

5151
THPEvent* self = reinterpret_cast<THPEvent*>(ptr.get());
52-
self->weakreflist = nullptr;
5352

5453
// TODO: blocking and interprocess are not supported yet. To support them, the
5554
// 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) {
7473
auto self = THPObjectPtr{type->tp_alloc(type, 0)};
7574
TORCH_CHECK(self, "Failed to allocate memory for Event");
7675
auto self_ = reinterpret_cast<THPEvent*>(self.get());
77-
self_->weakreflist = nullptr;
7876
new (&self_->event) c10::Event(device_type, flag);
7977
return self.release();
8078
}
@@ -84,7 +82,6 @@ static void THPEvent_dealloc(THPEvent* self) {
8482
pybind11::gil_scoped_release no_gil{};
8583
self->event.~Event();
8684
}
87-
PyObject_ClearWeakRefs((PyObject*)self);
8885
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
8986
}
9087

@@ -285,8 +282,7 @@ static PyMethodDef THPEvent_methods[] = {
285282
{"synchronize", THPEvent_synchronize, METH_NOARGS, nullptr},
286283
{"ipc_handle", THPEvent_ipc_handle, METH_NOARGS, nullptr},
287284
{nullptr}};
288-
#pragma GCC diagnostic push
289-
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
285+
290286
PyTypeObject THPEventType = {
291287
PyVarObject_HEAD_INIT(nullptr, 0)
292288
"torch.Event", /* tp_name */
@@ -312,7 +308,7 @@ PyTypeObject THPEventType = {
312308
nullptr, /* tp_traverse */
313309
nullptr, /* tp_clear */
314310
nullptr, /* tp_richcompare */
315-
offsetof(THPEvent, weakreflist), /* tp_weaklistoffset */
311+
0, /* tp_weaklistoffset */
316312
nullptr, /* tp_iter */
317313
nullptr, /* tp_iternext */
318314
THPEvent_methods, /* tp_methods */
@@ -327,7 +323,6 @@ PyTypeObject THPEventType = {
327323
nullptr, /* tp_alloc */
328324
THPEvent_pynew, /* tp_new */
329325
};
330-
#pragma GCC diagnostic pop
331326

332327
void THPEvent_init(PyObject* module) {
333328
THPEventClass = &THPEventType;

torch/csrc/Event.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
struct TORCH_API THPEvent {
88
PyObject_HEAD
99
c10::Event event;
10-
PyObject* weakreflist;
1110
};
1211
TORCH_API extern PyTypeObject* THPEventClass;
1312
TORCH_API extern PyTypeObject THPEventType;

0 commit comments

Comments
 (0)