Skip to content

Commit 626a431

Browse files
authored
Merge branch 'main' into subtests2
2 parents 0d71cf4 + 94e3a76 commit 626a431

75 files changed

Lines changed: 41463 additions & 30969 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci-pixi-source-test.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ defaults:
5252
shell: bash --noprofile --norc -xeuo pipefail {0}
5353

5454
env:
55-
PIXI_VERSION: "v0.66.0" # keep in sync with the version developers run locally
55+
# keep in sync with the version developers run locally. Must be >=0.71.0:
56+
# older pixi re-ran the editable source build on every `pixi run`, recompiling
57+
# all Cython extensions (#2138). The fix (content-addressed source-build cache,
58+
# prefix-dev/pixi#6285 + #6123) also bumps the pixi.lock format to v7.
59+
PIXI_VERSION: "v0.73.0"
5660

5761
jobs:
5862
# ── PR guard: CPU-only build + import + placement smoke ──

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ CUDA Python is the home for accessing NVIDIA’s CUDA platform from Python. It c
55
* [cuda.core](https://nvidia.github.io/cuda-python/cuda-core/latest): Pythonic access to CUDA Runtime and other core functionality
66
* [cuda.bindings](https://nvidia.github.io/cuda-python/cuda-bindings/latest): Low-level Python bindings to CUDA C APIs
77
* [cuda.pathfinder](https://nvidia.github.io/cuda-python/cuda-pathfinder/latest): Utilities for locating CUDA components installed in the user's Python environment
8-
* [cuda.coop](https://nvidia.github.io/cccl/unstable/python/coop.html): A Python module providing CCCL's reusable block-wide and warp-wide *device* primitives for use within Numba CUDA kernels
98
* [cuda.compute](https://nvidia.github.io/cccl/unstable/python/compute/index.html): A Python module for easy access to CCCL's highly efficient and customizable parallel algorithms, like `sort`, `scan`, `reduce`, `transform`, etc. that are callable on the *host*
109
* [numba-cuda-mlir](https://nvidia.github.io/numba-cuda-mlir/): An evolution of Numba CUDA that improves upon its technical foundation and performance to provide the future of CUDA Python JIT compilation. It currently supports developing CUDA **SIMT** kernels in Python, providing Python bindings for accelerated device libraries, and serving as a compiler for user-defined functions in accelerated libraries.
1110
* [numba.cuda](https://nvidia.github.io/numba-cuda/): A Python DSL that exposes CUDA **SIMT** programming model and compiles a restricted subset of Python code into CUDA kernels and device functions

benchmarks/cuda_bindings/pixi.lock

Lines changed: 1286 additions & 621 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmarks/cuda_core/pixi.lock

Lines changed: 1052 additions & 287 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cuda_bindings/AGENTS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ subpackage in the `cuda-python` monorepo.
4242
- **Examples**: example coverage is pytest-based under `examples/`.
4343
- **Benchmarks**: run with `pytest --benchmark-only benchmarks/` when needed.
4444

45+
The `legacy_tests` subdirectory tests the old pre-v2 APIs of `driver`, `runtime`
46+
and `nvrtc`. These test files should not be added to, only updated when
47+
necessary to fix test failures. The canonical set of tests are those outside of
48+
the `legacy_tests` subdirectory.
49+
4550
## Build and environment notes
4651

4752
- `CUDA_HOME` or `CUDA_PATH` must point to a valid CUDA Toolkit for source

cuda_bindings/build_hooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def _cleanup_dst_files():
196196

197197
# Build extension list
198198
extensions = []
199-
cuda_bindings_files = glob.glob("cuda/bindings/*.pyx")
199+
cuda_bindings_files = glob.glob("cuda/bindings/*.pyx") + glob.glob("cuda/bindings/_v2/*.pyx")
200200
if sys.platform == "win32":
201201
cuda_bindings_files = [f for f in cuda_bindings_files if "cufile" not in f]
202202

cuda_bindings/cuda/bindings/_example_helpers/common.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
from cuda import pathfinder
1111
from cuda.bindings import driver as cuda
12-
from cuda.bindings import nvrtc
1312
from cuda.bindings import runtime as cudart
13+
from cuda.bindings._v2 import nvrtc
1414

1515
from .helper_cuda import check_cuda_errors
1616

@@ -44,7 +44,7 @@ def __init__(self, code, dev_id):
4444
requirement_not_met(f'pathfinder.find_nvidia_header_directory("{libname}") returned None')
4545
include_dirs.append(hdr_dir)
4646

47-
prog = check_cuda_errors(nvrtc.nvrtcCreateProgram(str.encode(code), b"sourceCode.cu", 0, None, None))
47+
prog = nvrtc.create_program(str.encode(code), b"sourceCode.cu")
4848

4949
# Initialize CUDA
5050
check_cuda_errors(cudart.cudaFree(0))
@@ -55,7 +55,7 @@ def __init__(self, code, dev_id):
5555
minor = check_cuda_errors(
5656
cudart.cudaDeviceGetAttribute(cudart.cudaDeviceAttr.cudaDevAttrComputeCapabilityMinor, dev_id)
5757
)
58-
_, nvrtc_minor = check_cuda_errors(nvrtc.nvrtcVersion())
58+
_, nvrtc_minor = nvrtc.version()
5959
use_cubin = nvrtc_minor >= 1
6060
prefix = "sm" if use_cubin else "compute"
6161
arch_arg = bytes(f"--gpu-architecture={prefix}_{major}{minor}", "ascii")
@@ -70,25 +70,19 @@ def __init__(self, code, dev_id):
7070
opts.append(f"--include-path={inc_dir}".encode())
7171

7272
try:
73-
check_cuda_errors(nvrtc.nvrtcCompileProgram(prog, len(opts), opts))
74-
except RuntimeError as err:
75-
log_size = check_cuda_errors(nvrtc.nvrtcGetProgramLogSize(prog))
76-
log = b" " * log_size
77-
check_cuda_errors(nvrtc.nvrtcGetProgramLog(prog, log))
73+
nvrtc.compile_program(prog, opts)
74+
except nvrtc.NvrtcError as err:
75+
log = nvrtc.get_program_log(prog)
7876
import sys
7977

8078
print(log.decode(), file=sys.stderr) # noqa: T201
8179
print(err, file=sys.stderr) # noqa: T201
8280
sys.exit(1)
8381

8482
if use_cubin:
85-
data_size = check_cuda_errors(nvrtc.nvrtcGetCUBINSize(prog))
86-
data = b" " * data_size
87-
check_cuda_errors(nvrtc.nvrtcGetCUBIN(prog, data))
83+
data = nvrtc.get_cubin(prog)
8884
else:
89-
data_size = check_cuda_errors(nvrtc.nvrtcGetPTXSize(prog))
90-
data = b" " * data_size
91-
check_cuda_errors(nvrtc.nvrtcGetPTX(prog, data))
85+
data = nvrtc.get_ptx(prog)
9286

9387
self.module = check_cuda_errors(cuda.cuModuleLoadData(np.char.array(data)))
9488

cuda_bindings/cuda/bindings/_lib/param_packer.h

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@
77
#include <functional>
88
#include <stdexcept>
99
#include <string>
10+
#include <climits>
11+
#include <cstdint>
12+
13+
// PyLong_AsInt entered the public/stable CPython API in 3.13. cuda.bindings
14+
// supports Python 3.10+, so provide a file-local backport for older builds.
15+
// This is a copy of the CPython implementation; it is `static` (unlike the
16+
// original) because this header is compiled into every extension module that
17+
// includes it, mirroring the other helpers below.
18+
#if PY_VERSION_HEX < 0x030D0000
19+
static int
20+
PyLong_AsInt(PyObject *obj)
21+
{
22+
int overflow;
23+
long result = PyLong_AsLongAndOverflow(obj, &overflow);
24+
if (overflow || result > INT_MAX || result < INT_MIN) {
25+
PyErr_SetString(PyExc_OverflowError,
26+
"Python int too large to convert to C int");
27+
return -1;
28+
}
29+
return (int)result;
30+
}
31+
#endif
1032

1133
static PyObject* ctypes_module = nullptr;
1234

@@ -69,7 +91,13 @@ static void populate_feeders(PyTypeObject* target_t, PyTypeObject* source_t)
6991
{
7092
m_feeders[{target_t,source_t}] = [](void* ptr, PyObject* value) -> int
7193
{
72-
*((int*)ptr) = (int)PyLong_AsLong(value);
94+
// PyLong_AsInt range-checks against the 32-bit int slot and raises
95+
// OverflowError itself, so an out-of-range value is rejected rather
96+
// than silently truncated.
97+
int v = PyLong_AsInt(value);
98+
if (v == -1 && PyErr_Occurred())
99+
return -1;
100+
*((int*)ptr) = v;
73101
return sizeof(int);
74102
};
75103
return;
@@ -89,7 +117,23 @@ static void populate_feeders(PyTypeObject* target_t, PyTypeObject* source_t)
89117
{
90118
m_feeders[{target_t,source_t}] = [](void* ptr, PyObject* value) -> int
91119
{
92-
*((int8_t*)ptr) = (int8_t)PyLong_AsLong(value);
120+
// c_byte is an 8-bit slot with no dedicated CPython converter, so
121+
// range-check explicitly against INT8_MIN/INT8_MAX. AsLongAndOverflow's
122+
// `overflow` only flags values outside `long` (64-bit on LP64), so a
123+
// value in that range would be silently truncated by (int8_t)v without
124+
// the explicit bounds check. When overflow!=0, v is the -1 sentinel
125+
// (not the real value), so that case must be caught before trusting v.
126+
int overflow = 0;
127+
long v = PyLong_AsLongAndOverflow(value, &overflow);
128+
if (overflow == 0 && v == -1 && PyErr_Occurred())
129+
return -1; // non-overflow conversion error; exception already set
130+
if (overflow != 0 || v < INT8_MIN || v > INT8_MAX)
131+
{
132+
PyErr_SetString(PyExc_OverflowError,
133+
"Python int is out of range for a c_byte (8-bit) kernel argument");
134+
return -1;
135+
}
136+
*((int8_t*)ptr) = (int8_t)v;
93137
return sizeof(int8_t);
94138
};
95139
return;

cuda_bindings/cuda/bindings/_lib/param_packer.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
# Include "param_packer.h" so its contents get compiled into every
55
# Cython extension module that depends on param_packer.pxd.
66
cdef extern from "param_packer.h":
7-
int feed(void* ptr, object o, object ct)
7+
int feed(void* ptr, object o, object ct) except? -1

cuda_bindings/cuda/bindings/_lib/utils.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ cdef class _HelperCUcoredumpSettings:
162162
cdef cydriver.CUcoredumpSettings_enum _attrib
163163
cdef bint _is_getter
164164
cdef size_t _size
165+
cdef object _references # keeps caller bytes alive so _charstar stays valid
165166

166167
# Return values
167168
cdef bint _bool

0 commit comments

Comments
 (0)