Skip to content

fix(cuda.core): declare cdef attrs for system event wrappers and decode packed gpu_id - #2606

Open
rluo8 wants to merge 8 commits into
NVIDIA:mainfrom
rluo8:fix/system-event-missing-cdef-attrs
Open

fix(cuda.core): declare cdef attrs for system event wrappers and decode packed gpu_id#2606
rluo8 wants to merge 8 commits into
NVIDIA:mainfrom
rluo8:fix/system-event-missing-cdef-attrs

Conversation

@rluo8

@rluo8 rluo8 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Description

Two issues were found when adding coverage tests (internal bug 6595639):

  1. Missing cdef attributes on SystemEvent, SystemEvents, EventData, and DeviceAttributes. These are Cython cdef class types (no __dict__), so undeclared self._event_data / self._attributes assignments raise AttributeError. RegisteredSystemEvents.wait() therefore crashes as soon as a real bind/unbind event arrives.

  2. SystemEvent.device type mismatch. NVML's packed int gpu_id (domain[31:16] | bus[15:8] | device[7:0]), which requires an NVML-style PCI bus ID string (NVML_DEVICE_PCI_BUS_ID_FMT). Decode at the call site via _pci_bus_id_from_gpu_id.

Adds regression tests for construction, packing decode, and end-to-end SystemEvent.device resolution against live pci_info.

Verification:

  • B10: new regression tests 8 passed; live unbind of 0000:c1:00.0
    yields SystemEvents with gpu_id=0xC10000000000:C1:00.0, and
    post-rebind SystemEvent.device resolves correctly
  • A100 MIG: DeviceAttributes cdef fix verified
  • System Event register/wait remains Linux-only (Windows/WSL/WOA skip)

@github-actions github-actions Bot added the cuda.core Everything related to the cuda.core module label Aug 12, 2026
@rluo8 rluo8 self-assigned this Aug 12, 2026
@rluo8
rluo8 requested a review from mdboom August 12, 2026 03:05
@rluo8

rluo8 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Hi @mdboom , this issue is about system event. Could you please help review it?
Thanks!

@github-actions

Copy link
Copy Markdown

@rwgk rwgk added this to the cuda.core 1.2.0 milestone Aug 12, 2026
@rwgk rwgk added the P0 High priority - Must do! label Aug 12, 2026

@mdboom mdboom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the root issue this is trying to solve?

This is making these objects mutable and they definitely shouldn't be -- their intention is to return values from the NVML API, not to set things on them.

The gpu_id / pci_id bridge in _system_events.pyx added here makes sense, though.

@rluo8

rluo8 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

Hi @mdboom , thanks for the review. I understand your concern. The root issue is that constructing SystemEvent / SystemEvents / EventData / DeviceAttributes throws exception: these are Cython cdef class types with no dict, so undeclared self._event_data / self._attributes in init raise AttributeError.

This is the reproduce script:

from cuda.bindings import nvml
from cuda.core.system._system_events import SystemEvents

ed = nvml.SystemEventData_v1(1)
ed.event_type = nvml.SystemEventType.GPU_DRIVER_UNBIND
ed.gpu_id = 0xC100
SystemEvents(ed) 

Output:

   SystemEvents(ed)  # main → AttributeError: no '_event_data' / no __dict__
    ~~~~~~~~~~~~^^^^
  File "cuda/core/system/_system_events.pyx", line 60, in cuda.core.system._system_events.SystemEvents.__init__
AttributeError: 'cuda.core.system._system_events.SystemEvents' object has no attribute '_event_data' and no __dict__ for setting new attributes

After the fix, it doesn't make the public API mutable, the cdef field is not visible from Python.
Test script:

from cuda.bindings import nvml
from cuda.core.system._system_events import SystemEvent

ed = nvml.SystemEventData_v1(1)
ed.event_type = nvml.SystemEventType.GPU_DRIVER_BIND
ed.gpu_id = 0xC100
event = SystemEvent(ed)
print("constructed OK:", event.event_type, hex(event.gpu_id))

try:
    event.event_type = "BIND"
except Exception as err:
    print(repr(err))

try:
    print(event._event_data)
except Exception as err:
    print(repr(err))

Output:

constructed OK: bind 0xc100
AttributeError("attribute 'event_type' of '...SystemEvent' objects is not writable")
AttributeError("'...SystemEvent' object has no attribute '_event_data'")

This looks similar to Cython’s guidance about cdef class AttributeErrors : https://docs.cython.org/en/stable/src/userguide/troubleshooting.html
AttributeErrors can also happen when writing into a cdef class, commonly in init:

cdef class Company:
    def __init__(self, staff):
        self.staff = staff  # AttributeError!

Unlike a regular class, cdef class has a fixed list of attributes that you can write to and you need to declare them explicitly. For example:

cdef class Company:
   cdef list staff
   # ...

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.core Everything related to the cuda.core module P0 High priority - Must do!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants