Skip to content

[BUG]: system.Device.bar1_memory_info.reserved raises AttributeError on None #3467

Open

Description

@1AbdullahButt

Is this a duplicate?

Type of Bug

Runtime Error

Component

cuda.core

Describe the bug

BAR1MemoryInfo subclasses MemoryInfo but declares its own cdef object _memory_info. The subclass methods use the new slot. The reserved property it inherits from MemoryInfo reads the base class's slot, which is never set. So Device.bar1_memory_info.reserved exists (it's in dir(), and isinstance(bar1, MemoryInfo) is true) but raises an internal AttributeError on None:

@property
def reserved(self) -> int:
"""
Device memory (in bytes) reserved for system use (driver or firmware)
"""
return self._memory_info.reserved
cdef class BAR1MemoryInfo(MemoryInfo):
"""
BAR1 Memory allocation information for a device.
"""
cdef object _memory_info
def __init__(self, memory_info: nvml.BAR1Memory):
self._memory_info = memory_info

Code that handles a MemoryInfo generically (for example, logging total/used/free/reserved for both memory_info and bar1_memory_info) crashes on the BAR1 object.

How to Reproduce

from cuda.core import system

dev = system.Device(index=0)
bar1 = dev.bar1_memory_info
print(type(bar1).__mro__)
print("total:", bar1.total, "free:", bar1.free, "used:", bar1.used)
print("reserved:", bar1.reserved)

Output:

(<class 'cuda.core.system._device.BAR1MemoryInfo'>, <class 'cuda.core.system._device.MemoryInfo'>, <class 'object'>)
total: 8589934592 free: 8589934592 used: 0
Traceback (most recent call last):
  File "repro.py", line 7, in <module>
    print("reserved:", bar1.reserved)
                       ^^^^^^^^^^^^^
  File "cuda/core/system/_memory.pxi", line 41, in cuda.core.system._device.MemoryInfo.reserved.__get__
AttributeError: 'NoneType' object has no attribute 'reserved'

Expected behavior

BAR1MemoryInfo shouldn't expose a reserved attribute that can only crash. nvmlBAR1Memory_t has no reserved field, so either BAR1MemoryInfo stops inheriting from MemoryInfo, or it overrides reserved with a clear NotSupportedError/AttributeError. Either way, the shadowed _memory_info slot should go.

Operating System

Linux container (python:3.12-slim) on Windows 11 / WSL2 with the NVIDIA container runtime. The bug is in the Cython class layout, so the OS shouldn't matter.

nvidia-smi output

| NVIDIA-SMI 610.59                 KMD Version: 610.91        CUDA UMD Version: 13.3     |
|   0  NVIDIA GeForce RTX 3050 ...    On  |   00000000:01:00.0 Off |                  N/A |

Python 3.12.16. Packages:

cuda-bindings     13.5.0
cuda-core         1.3.0
cuda-pathfinder   1.9.0
cuda-toolkit      13.5.0
cupy-cuda13x      14.2.0
numpy             2.5.4
nvidia-cuda-nvrtc 13.5.37
nvidia-cufile     1.20.0.107
nvidia-nvfatbin   13.5.37
nvidia-nvjitlink  13.5.37
nvidia-nvvm       13.5.37

The file is unchanged between cuda-core-v1.3.0 and current main (ba7dcd0).

Happy to send a fix if that helps.

Activity

  1. lxcxjxhx commented on Nov 18, 2026

    @lxcxjxhx

    Reproduced on a 4060 Laptop (sm_89), cuda-core from wheels, Windows:

    mro: ['BAR1MemoryInfo', 'MemoryInfo', 'object']
    total: 8589934592 free: 30015488 used: 8559919104
    reserved RAISES: AttributeError 'NoneType' object has no attribute 'reserved'
    

    Same failure, and there is a second cause behind the one in the report that changes which fix is right.

    The slot shadowing is real. BAR1MemoryInfo re-declares cdef object _memory_info in _memory.pxi, so Cython gives the subclass its own field; __init__ writes the subclass field, the inherited reserved reads the base field, which is unset. total, free and used are shadowed in the subclass and work.

    But the base field would not help. nvml.BAR1Memory has no reserved at all:

    BAR1Memory fields: ['bar1_free', 'bar1_total', 'bar1_used', 'from_buffer', 'from_data', 'from_ptr', 'ptr']
    

    compared with Memory_v2, which does. So removing the duplicate cdef fixes the shadowing and still leaves reserved raising — with a different message (BAR1Memory has no attribute reserved) instead of a NoneType one. The isinstance(bar1, MemoryInfo) claim is also wrong in a way the report only half-states: BAR1MemoryInfo genuinely is a MemoryInfo by Cython type, but the thing it wraps is not a MemoryInfo-shaped struct.

    Two options, and I would take the first:

    1. Keep the inheritance, give BAR1 an honest reserved. Drop the duplicate cdef (so all four readers use one field) and override reserved to return 0, documented as "BAR1 has no driver- or firmware-reserved region". Generic code that logs total/used/free/reserved then works unchanged, which is what the report asks for, and 0 is the truthful value rather than a placeholder.
    2. Break the inheritance. BAR1MemoryInfo stops subclassing MemoryInfo; isinstance becomes false and generic code has to branch. Correct but a breaking change for anything that already relies on the current (lying) type relationship.

    One thing I cannot settle from the outside: whether option 1's 0 should instead be None, to signal "not applicable" the way NVML_VALUE_NOT_AVAILABLE is handled in #3468. That is a decision for whoever owns the API.

    Happy to open a PR for whichever you prefer — say the word and I will send it, given #3480 is in your queue.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNeeds the team's attention

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions