Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 57 additions & 32 deletions cuda_bindings_12/cuda/bindings/nvfatbin.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: Apache-2.0
#
# This code was automatically generated across versions from 12.4.1 to 13.5.0. Do not modify it directly.
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=5017cbd422315ed05d8c8d89fcf7ee2efa806dbccab0e7cc58a646a037f8053a
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=93a3aa883e727ee9fb62ffe1decd27a9c8f56eb75312e1c3c8ab5457a880c10c

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Maintainability & Code Quality · Quick win

important: Record the generation inputs and command in the PR.

The PR description identifies a generator update, but it does not identify the generator revision, the toolkit inputs for each root, or the regeneration command. A seal verifies file content, not how the file was generated. Add those details so reviewers can reproduce the artifacts. As per coding guidelines, “record the generator revision, toolkit inputs, and command in the pull request.” (github.com)

Source: Coding guidelines



# <<<< PREAMBLE CONTENT >>>>
Expand All @@ -17,35 +17,53 @@ from libc.stdint cimport intptr_t

from cuda.bindings._internal._fast_enum import FastEnum as _cyb_FastEnum

cdef intptr_t _cyb_get_buffer_pointer(buf, Py_ssize_t size, readonly=True) except?-1:
cdef intptr_t ptr
cdef int flags = _cyb_cpython.PyBUF_ANY_CONTIGUOUS
if not readonly:
flags |= _cyb_cpython.PyBUF_WRITABLE
cdef int status = -1
cdef _cyb_cpython.Py_buffer view
if buf is None:
ptr = 0
elif isinstance(buf, int):
ptr = <intptr_t>buf
else:
cdef class _cyb_BufferGuard:
"""Holds an export of a buffer for as long as it lives.

`buf` is `None` (NULL), a Python `int` (an address, which is not held), or a contiguous buffer.
With `wrap_errors`, any failure to get the buffer (or its size, if `size` is not -1) is a `ValueError`.
"""
cdef _cyb_cpython.Py_buffer _view
cdef bint _held
cdef void* ptr
cdef Py_ssize_t nbytes

def __cinit__(self, buf, Py_ssize_t size, bint readonly, bint wrap_errors):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

Keep oversized explicit counts on the ValueError path in the buffer guard.

_BufferGuard.__cinit__ declares Py_ssize_t size, but the public wrappers pass a size_t. For a count above PY_SSIZE_T_MAX, Cython can raise OverflowError during argument conversion. This happens before the wrap_errors handler runs. The merge-base helper raised ValueError for the same input, so these calls now raise a different exception type.

Declare the guard's count as size_t. Range-check it inside the guarded body so that an oversized count raises ValueError, as a size mismatch does.

cdef int flags = _cyb_cpython.PyBUF_ANY_CONTIGUOUS
self._held = False
self.ptr = NULL
self.nbytes = 0
if buf is None:
return
if isinstance(buf, int):
self.ptr = <void*><intptr_t>buf
return
if not readonly:
flags |= _cyb_cpython.PyBUF_WRITABLE
adj = "writable " if not readonly else ""
try:
status = _cyb_cpython.PyObject_GetBuffer(buf, &view, flags)
_cyb_cpython.PyObject_GetBuffer(buf, &self._view, flags)
self._held = True
if size != -1:
assert view.len == size
assert self._view.len == size

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Major · Stability & Availability · Quick win

important: Keep the buffer length check active under optimized Python.

Cython omits this check when assertions are disabled. A buffer with a mismatched byte count then reaches the native call with the caller's original size. If the count exceeds the export, the native call can read past it. The old helper had the same gap. Replace assert with an explicit mismatch check in the generator and regenerate both roots. Preserve ValueError for mismatches and allow matching exports to proceed. This applies to the equivalent guards in nvjitlink.pyx and nvvm.pyx. (raw.githubusercontent.com)

except Exception as e:
adj = "writable " if not readonly else ""
if wrap_errors:
raise ValueError(
"buf must be None, a Python int representing the pointer "
f"address to a valid buffer, or a contiguous {adj}"
f"buffer (a ctypes object, NumPy array, bytes, ...), of size {size}"
) from e
if not isinstance(e, BufferError):
raise
raise ValueError(
"buf must be None, a Python int representing the pointer "
f"address to a valid buffer, or a contiguous {adj}"
f"buffer (a ctypes object, NumPy array, bytes, ...), of size {size}"
f"buf must be a contiguous {adj}buffer (a ctypes object, NumPy array, bytes, ...)"
) from e
else:
ptr = <intptr_t>view.buf
finally:
if status == 0:
_cyb_cpython.PyBuffer_Release(&view)
return ptr
self.ptr = self._view.buf
self.nbytes = self._view.len

def __dealloc__(self):
if self._held:
_cyb_cpython.PyBuffer_Release(&self._view)


# <<<< END OF PREAMBLE CONTENT >>>>
Expand Down Expand Up @@ -192,7 +210,8 @@ cpdef add_ptx(intptr_t handle, code, size_t size, arch, identifier, options_cmd_

.. seealso:: `nvFatbinAddPTX`
"""
cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True)
cdef _cyb_BufferGuard _code_guard_ = _cyb_BufferGuard(code, size, True, True)
cdef void* _code_ = _code_guard_.ptr
if not isinstance(arch, str):
raise TypeError("arch must be a Python str")
cdef bytes _temp_arch_ = (<str>arch).encode()
Expand Down Expand Up @@ -224,7 +243,8 @@ cpdef add_cubin(intptr_t handle, code, size_t size, arch, identifier):

.. seealso:: `nvFatbinAddCubin`
"""
cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True)
cdef _cyb_BufferGuard _code_guard_ = _cyb_BufferGuard(code, size, True, True)
cdef void* _code_ = _code_guard_.ptr
if not isinstance(arch, str):
raise TypeError("arch must be a Python str")
cdef bytes _temp_arch_ = (<str>arch).encode()
Expand Down Expand Up @@ -253,7 +273,8 @@ cpdef add_ltoir(intptr_t handle, code, size_t size, arch, identifier, options_cm

.. seealso:: `nvFatbinAddLTOIR`
"""
cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True)
cdef _cyb_BufferGuard _code_guard_ = _cyb_BufferGuard(code, size, True, True)
cdef void* _code_ = _code_guard_.ptr
if not isinstance(arch, str):
raise TypeError("arch must be a Python str")
cdef bytes _temp_arch_ = (<str>arch).encode()
Expand Down Expand Up @@ -298,7 +319,8 @@ cpdef get(intptr_t handle, buffer):

.. seealso:: `nvFatbinGet`
"""
cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, -1, readonly=False)
cdef _cyb_BufferGuard _buffer_guard_ = _cyb_BufferGuard(buffer, -1, False, True)
cdef void* _buffer_ = _buffer_guard_.ptr
with nogil:
__status__ = nvFatbinGet(<Handle>handle, <void*>_buffer_)
check_status(__status__)
Expand Down Expand Up @@ -334,7 +356,8 @@ cpdef add_index(intptr_t handle, code, size_t size, identifier):

.. seealso:: `nvFatbinAddIndex`
"""
cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True)
cdef _cyb_BufferGuard _code_guard_ = _cyb_BufferGuard(code, size, True, True)
cdef void* _code_ = _code_guard_.ptr
if not isinstance(identifier, str):
raise TypeError("identifier must be a Python str")
cdef bytes _temp_identifier_ = (<str>identifier).encode()
Expand All @@ -354,7 +377,8 @@ cpdef add_reloc(intptr_t handle, code, size_t size):

.. seealso:: `nvFatbinAddReloc`
"""
cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True)
cdef _cyb_BufferGuard _code_guard_ = _cyb_BufferGuard(code, size, True, True)
cdef void* _code_ = _code_guard_.ptr
with nogil:
__status__ = nvFatbinAddReloc(<Handle>handle, <const void*>_code_, size)
check_status(__status__)
Expand All @@ -373,7 +397,8 @@ cpdef add_tile_ir(intptr_t handle, code, size_t size, identifier, options_cmd_li

.. seealso:: `nvFatbinAddTileIR`
"""
cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True)
cdef _cyb_BufferGuard _code_guard_ = _cyb_BufferGuard(code, size, True, True)
cdef void* _code_ = _code_guard_.ptr
if not isinstance(identifier, str):
raise TypeError("identifier must be a Python str")
cdef bytes _temp_identifier_ = (<str>identifier).encode()
Expand Down
86 changes: 55 additions & 31 deletions cuda_bindings_12/cuda/bindings/nvjitlink.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: Apache-2.0
#
# This code was automatically generated across versions from 12.0.1 to 13.5.0. Do not modify it directly.
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=9fa17f14f752f454a8a5050362f6a420cc053eb063de1bdb72b49f2ee76b0658
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=f45b69c67c66d7bd6050b1db926a709e9328fccdbb5365bbe35072ed41f2dbfe


# <<<< PREAMBLE CONTENT >>>>
Expand All @@ -20,35 +20,53 @@ from libc.stdint cimport (

from cuda.bindings._internal._fast_enum import FastEnum as _cyb_FastEnum

cdef intptr_t _cyb_get_buffer_pointer(buf, Py_ssize_t size, readonly=True) except?-1:
cdef intptr_t ptr
cdef int flags = _cyb_cpython.PyBUF_ANY_CONTIGUOUS
if not readonly:
flags |= _cyb_cpython.PyBUF_WRITABLE
cdef int status = -1
cdef _cyb_cpython.Py_buffer view
if buf is None:
ptr = 0
elif isinstance(buf, int):
ptr = <intptr_t>buf
else:
cdef class _cyb_BufferGuard:
"""Holds an export of a buffer for as long as it lives.

`buf` is `None` (NULL), a Python `int` (an address, which is not held), or a contiguous buffer.
With `wrap_errors`, any failure to get the buffer (or its size, if `size` is not -1) is a `ValueError`.
"""
cdef _cyb_cpython.Py_buffer _view
cdef bint _held
cdef void* ptr
cdef Py_ssize_t nbytes

def __cinit__(self, buf, Py_ssize_t size, bint readonly, bint wrap_errors):
cdef int flags = _cyb_cpython.PyBUF_ANY_CONTIGUOUS
self._held = False
self.ptr = NULL
self.nbytes = 0
if buf is None:
return
if isinstance(buf, int):
self.ptr = <void*><intptr_t>buf
return
if not readonly:
flags |= _cyb_cpython.PyBUF_WRITABLE
adj = "writable " if not readonly else ""
try:
status = _cyb_cpython.PyObject_GetBuffer(buf, &view, flags)
_cyb_cpython.PyObject_GetBuffer(buf, &self._view, flags)
self._held = True
if size != -1:
assert view.len == size
assert self._view.len == size
except Exception as e:
adj = "writable " if not readonly else ""
if wrap_errors:
raise ValueError(
"buf must be None, a Python int representing the pointer "
f"address to a valid buffer, or a contiguous {adj}"
f"buffer (a ctypes object, NumPy array, bytes, ...), of size {size}"
) from e
if not isinstance(e, BufferError):
raise
raise ValueError(
"buf must be None, a Python int representing the pointer "
f"address to a valid buffer, or a contiguous {adj}"
f"buffer (a ctypes object, NumPy array, bytes, ...), of size {size}"
f"buf must be a contiguous {adj}buffer (a ctypes object, NumPy array, bytes, ...)"
) from e
else:
ptr = <intptr_t>view.buf
finally:
if status == 0:
_cyb_cpython.PyBuffer_Release(&view)
return ptr
self.ptr = self._view.buf
self.nbytes = self._view.len

def __dealloc__(self):
if self._held:
_cyb_cpython.PyBuffer_Release(&self._view)


# <<<< END OF PREAMBLE CONTENT >>>>
Expand Down Expand Up @@ -193,7 +211,8 @@ cpdef add_data(intptr_t handle, int input_type, data, size_t size, name):

.. seealso:: `nvJitLinkAddData`
"""
cdef void* _data_ = <void *>_cyb_get_buffer_pointer(data, size, readonly=True)
cdef _cyb_BufferGuard _data_guard_ = _cyb_BufferGuard(data, size, True, True)
cdef void* _data_ = _data_guard_.ptr
if not isinstance(name, str):
raise TypeError("name must be a Python str")
cdef bytes _temp_name_ = (<str>name).encode()
Expand Down Expand Up @@ -267,7 +286,8 @@ cpdef get_linked_cubin(intptr_t handle, cubin):

.. seealso:: `nvJitLinkGetLinkedCubin`
"""
cdef void* _cubin_ = <void *>_cyb_get_buffer_pointer(cubin, -1, readonly=False)
cdef _cyb_BufferGuard _cubin_guard_ = _cyb_BufferGuard(cubin, -1, False, True)
cdef void* _cubin_ = _cubin_guard_.ptr
with nogil:
__status__ = nvJitLinkGetLinkedCubin(<Handle>handle, <void*>_cubin_)
check_status(__status__)
Expand Down Expand Up @@ -305,7 +325,8 @@ cpdef get_linked_ptx(intptr_t handle, ptx):

.. seealso:: `nvJitLinkGetLinkedPtx`
"""
cdef void* _ptx_ = <void *>_cyb_get_buffer_pointer(ptx, -1, readonly=False)
cdef _cyb_BufferGuard _ptx_guard_ = _cyb_BufferGuard(ptx, -1, False, True)
cdef void* _ptx_ = _ptx_guard_.ptr
with nogil:
__status__ = nvJitLinkGetLinkedPtx(<Handle>handle, <char*>_ptx_)
check_status(__status__)
Expand Down Expand Up @@ -341,7 +362,8 @@ cpdef get_error_log(intptr_t handle, log):

.. seealso:: `nvJitLinkGetErrorLog`
"""
cdef void* _log_ = <void *>_cyb_get_buffer_pointer(log, -1, readonly=False)
cdef _cyb_BufferGuard _log_guard_ = _cyb_BufferGuard(log, -1, False, True)
cdef void* _log_ = _log_guard_.ptr
with nogil:
__status__ = nvJitLinkGetErrorLog(<Handle>handle, <char*>_log_)
check_status(__status__)
Expand Down Expand Up @@ -377,7 +399,8 @@ cpdef get_info_log(intptr_t handle, log):

.. seealso:: `nvJitLinkGetInfoLog`
"""
cdef void* _log_ = <void *>_cyb_get_buffer_pointer(log, -1, readonly=False)
cdef _cyb_BufferGuard _log_guard_ = _cyb_BufferGuard(log, -1, False, True)
cdef void* _log_ = _log_guard_.ptr
with nogil:
__status__ = nvJitLinkGetInfoLog(<Handle>handle, <char*>_log_)
check_status(__status__)
Expand Down Expand Up @@ -436,7 +459,8 @@ cpdef get_linked_ltoir(intptr_t handle, ltoir):

.. seealso:: `nvJitLinkGetLinkedLTOIR`
"""
cdef void* _ltoir_ = <void *>_cyb_get_buffer_pointer(ltoir, -1, readonly=False)
cdef _cyb_BufferGuard _ltoir_guard_ = _cyb_BufferGuard(ltoir, -1, False, True)
cdef void* _ltoir_ = _ltoir_guard_.ptr
with nogil:
__status__ = nvJitLinkGetLinkedLTOIR(<Handle>handle, <void*>_ltoir_)
check_status(__status__)
Expand Down
3 changes: 2 additions & 1 deletion cuda_bindings_12/cuda/bindings/nvml.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: Apache-2.0
#
# This code was automatically generated across versions from 12.9.1 to 13.5.0. Do not modify it directly.
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=1a1a93143a45c1317fad32c5836bb153412cd49748d57bac998f8a9fc00e1bef
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=aaac7896390181ec86b43c291b7d46aa9ba2cb67603d9ce02a7b38856f9b9002



Expand Down Expand Up @@ -352,6 +352,7 @@ cpdef device_modify_drain_state(pci_info, int new_state)
cpdef int device_query_drain_state(pci_info) except? -1
cpdef device_remove_gpu_v2(pci_info, int gpu_state, int link_state)
cpdef device_discover_gpus(pci_info)
cpdef device_clear_field_values(intptr_t device, values)
cpdef int device_get_virtualization_mode(intptr_t device) except? -1
cpdef int device_get_host_vgpu_mode(intptr_t device) except? -1
cpdef device_set_virtualization_mode(intptr_t device, int virtual_mode)
Expand Down
Loading
Loading