[Easy] The event_id of torch.cuda.Event and torch.xpu.Event always is 0 (#151226)

Although torch.cuda.Event and torch.xpu.Event have cuda_event and sycl_event fields respectively, the event_id exposed from the base class torch.Event is always 0, which can confuse users.

The memory of torch.Event is not useful to torch.cuda.Event and torch.xpu.Event, but we still need to inherit from torch.Event because CPython will check it.

Repro with cuda:
```
>>> import torch
>>> event = torch.cuda.Event()
>>> event.cuda_event
0
>>> event.event_id
0
>>> event.record()
>>> event.cuda_event
127982096
>>> event.event_id
0
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/151226
Approved by: https://github.com/albanD, https://github.com/guangyey
ghstack dependencies: #151404, #151221, #151411
This commit is contained in:
FFFrog 2025-04-23 14:26:14 +08:00 committed by PyTorch MergeBot
parent 2ce9d2e9aa
commit 580913290c
4 changed files with 20 additions and 0 deletions

View File

@ -962,6 +962,15 @@ class TestCuda(TestCase):
self.assertTrue(event.query())
self.assertGreater(start_event.elapsed_time(event), 0)
event = torch.cuda.Event(enable_timing=True)
self.assertEqual(event.cuda_event, 0)
self.assertEqual(event.event_id, 0)
event.record()
self.assertNotEqual(event.cuda_event, 0)
self.assertNotEqual(event.event_id, 0)
self.assertEqual(event.cuda_event, event.event_id)
def test_events_elapsedtime(self):
event1 = torch.cuda.Event(enable_timing=False)
event2 = torch.cuda.Event(enable_timing=False)

View File

@ -259,6 +259,15 @@ if __name__ == "__main__":
):
start_event.elapsed_time(end_event)
event = torch.xpu.Event(enable_timing=True)
self.assertEqual(event.sycl_event, 0)
self.assertEqual(event.event_id, 0)
event.record()
self.assertNotEqual(event.sycl_event, 0)
self.assertNotEqual(event.event_id, 0)
self.assertEqual(event.sycl_event, event.event_id)
def test_generic_stream_event(self):
stream = torch.Stream("xpu")
self.assertEqual(stream.device_index, torch.xpu.current_device())

View File

@ -183,6 +183,7 @@ static PyObject* THCPEvent_ipc_handle(PyObject* _self, PyObject* noargs) {
static struct PyGetSetDef THCPEvent_properties[] = {
{"device", (getter)THCPEvent_get_device, nullptr, nullptr, nullptr},
{"cuda_event", (getter)THCPEvent_get_cuda_event, nullptr, nullptr, nullptr},
{"event_id", (getter)THCPEvent_get_cuda_event, nullptr, nullptr, nullptr},
{nullptr}};
// NOLINTNEXTLINE(*c-arrays*, *global-variables)

View File

@ -115,6 +115,7 @@ static PyObject* THXPEvent_synchronize(PyObject* _self, PyObject* noargs) {
static struct PyGetSetDef THXPEvent_properties[] = {
{"device", (getter)THXPEvent_get_device, nullptr, nullptr, nullptr},
{"sycl_event", (getter)THXPEvent_get_sycl_event, nullptr, nullptr, nullptr},
{"event_id", (getter)THXPEvent_get_sycl_event, nullptr, nullptr, nullptr},
{nullptr}};
// NOLINTNEXTLINE(*c-arrays*, *global-variables)