Skip to content

Commit 3f7a79b

Browse files
committed
Merge remote-tracking branch 'upstream/main' into dching/add-compute-sanitizer-to-ci
2 parents 2bfd402 + 2aca306 commit 3f7a79b

File tree

13 files changed

+209
-55
lines changed

13 files changed

+209
-55
lines changed

.bandit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[bandit]
2+
skips = B101,B311

.github/workflows/test-wheel-windows.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ jobs:
5555
5656
if ('${{ inputs.local-ctk }}' -eq '1') {
5757
if ($TEST_CUDA_MAJOR -eq '12') {
58-
$MINI_CTK_DEPS = '["nvcc", "nvrtc", "nvjitlink"]'
58+
$MINI_CTK_DEPS = '["nvcc", "nvrtc", "nvjitlink", "thrust"]'
5959
} else {
60-
$MINI_CTK_DEPS = '["nvcc", "nvrtc"]'
60+
$MINI_CTK_DEPS = '["nvcc", "nvrtc", "thrust"]'
6161
}
6262
}
6363

.pre-commit-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ repos:
2020
rev: 8ff25e07e487f143571cc305e56dd0253c60bc7b #v1.8.3
2121
hooks:
2222
- id: bandit
23+
args:
24+
- --ini
25+
- .bandit
2326

2427
default_language_version:
2528
python: python3

cuda_bindings/tests/test_cuda.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,8 @@ def test_get_error_name_and_string():
652652

653653
@pytest.mark.skipif(not callableBinary("nvidia-smi"), reason="Binary existance needed")
654654
def test_device_get_name():
655-
import subprocess
655+
# TODO: Refactor this test once we have nvml bindings to avoid the use of subprocess
656+
import subprocess # nosec B404
656657

657658
(err,) = cuda.cuInit(0)
658659
assert err == cuda.CUresult.CUDA_SUCCESS
@@ -661,12 +662,12 @@ def test_device_get_name():
661662
err, ctx = cuda.cuCtxCreate(0, device)
662663
assert err == cuda.CUresult.CUDA_SUCCESS
663664

664-
p = subprocess.run(
665-
["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
666-
)
665+
p = subprocess.check_output(
666+
["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"], shell=False, stderr=subprocess.PIPE
667+
) # nosec B603, B607
667668

668669
delimiter = b"\r\n" if platform.system() == "Windows" else b"\n"
669-
expect = p.stdout.split(delimiter)
670+
expect = p.split(delimiter)
670671
size = 64
671672
_, got = cuda.cuDeviceGetName(size, device)
672673
got = got.split(b"\x00")[0]

cuda_core/cuda/core/experimental/_event.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
from dataclasses import dataclass
99
from typing import TYPE_CHECKING, Optional
1010

11-
from cuda.core.experimental._utils.cuda_utils import CUDAError, check_or_create_options, driver, handle_return
11+
from cuda.core.experimental._utils.cuda_utils import (
12+
CUDAError,
13+
check_or_create_options,
14+
driver,
15+
handle_return,
16+
)
17+
from cuda.core.experimental._utils.cuda_utils import (
18+
_check_driver_error as raise_if_driver_error,
19+
)
1220

1321
if TYPE_CHECKING:
1422
import cuda.bindings
@@ -117,13 +125,31 @@ def __rsub__(self, other):
117125

118126
def __sub__(self, other):
119127
# return self - other (in milliseconds)
128+
err, timing = driver.cuEventElapsedTime(other.handle, self.handle)
120129
try:
121-
timing = handle_return(driver.cuEventElapsedTime(other.handle, self.handle))
130+
raise_if_driver_error(err)
131+
return timing
122132
except CUDAError as e:
123-
raise RuntimeError(
124-
"Timing capability must be enabled in order to subtract two Events; timing is disabled by default."
125-
) from e
126-
return timing
133+
if err == driver.CUresult.CUDA_ERROR_INVALID_HANDLE:
134+
if self.is_timing_disabled or other.is_timing_disabled:
135+
explanation = (
136+
"Both Events must be created with timing enabled in order to subtract them; "
137+
"use EventOptions(enable_timing=True) when creating both events."
138+
)
139+
else:
140+
explanation = (
141+
"Both Events must be recorded before they can be subtracted; "
142+
"use Stream.record() to record both events to a stream."
143+
)
144+
elif err == driver.CUresult.CUDA_ERROR_NOT_READY:
145+
explanation = (
146+
"One or both events have not completed; "
147+
"use Event.sync(), Stream.sync(), or Device.sync() to wait for the events to complete "
148+
"before subtracting them."
149+
)
150+
else:
151+
raise e
152+
raise RuntimeError(explanation) from e
127153

128154
@property
129155
def is_timing_disabled(self) -> bool:
@@ -164,5 +190,11 @@ def is_done(self) -> bool:
164190

165191
@property
166192
def handle(self) -> cuda.bindings.driver.CUevent:
167-
"""Return the underlying CUevent object."""
193+
"""Return the underlying CUevent object.
194+
195+
.. caution::
196+
197+
This handle is a Python object. To get the memory address of the underlying C
198+
handle, call ``int(Event.handle)``.
199+
"""
168200
return self._mnff.handle

cuda_core/cuda/core/experimental/_linker.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ def handle(self) -> LinkerHandleT:
503503
.. note::
504504
505505
The type of the returned object depends on the backend.
506+
507+
.. caution::
508+
509+
This handle is a Python object. To get the memory address of the underlying C
510+
handle, call ``int(Linker.handle)``.
506511
"""
507512
return self._mnff.handle
508513

cuda_core/cuda/core/experimental/_memory.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import abc
88
import weakref
9-
from typing import Optional, Tuple, TypeVar
9+
from typing import Optional, Tuple, TypeVar, Union
1010

1111
from cuda.core.experimental._dlpack import DLDeviceType, make_py_capsule
1212
from cuda.core.experimental._stream import default_stream
@@ -18,6 +18,9 @@
1818
# TODO: define a memory property mixin class and make Buffer and
1919
# MemoryResource both inherit from it
2020

21+
DevicePointerT = Union[driver.CUdeviceptr, int, None]
22+
"""A type union of `Cudeviceptr`, `int` and `None` for hinting Buffer.handle."""
23+
2124

2225
class Buffer:
2326
"""Represent a handle to allocated memory.
@@ -81,8 +84,14 @@ def close(self, stream=None):
8184
self._mnff.close(stream)
8285

8386
@property
84-
def handle(self):
85-
"""Return the buffer handle object."""
87+
def handle(self) -> DevicePointerT:
88+
"""Return the buffer handle object.
89+
90+
.. caution::
91+
92+
This handle is a Python object. To get the memory address of the underlying C
93+
handle, call ``int(Buffer.handle)``.
94+
"""
8695
return self._mnff.ptr
8796

8897
@property

cuda_core/cuda/core/experimental/_module.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,5 +354,11 @@ def code(self) -> CodeTypeT:
354354
@property
355355
@precondition(_lazy_load_module)
356356
def handle(self):
357-
"""Return the underlying handle object."""
357+
"""Return the underlying handle object.
358+
359+
.. caution::
360+
361+
This handle is a Python object. To get the memory address of the underlying C
362+
handle, call ``int(ObjectCode.handle)``.
363+
"""
358364
return self._handle

cuda_core/cuda/core/experimental/_program.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,5 +524,10 @@ def handle(self) -> ProgramHandleT:
524524
.. note::
525525
526526
The type of the returned object depends on the backend.
527+
528+
.. caution::
529+
530+
This handle is a Python object. To get the memory address of the underlying C
531+
handle, call ``int(Program.handle)``.
527532
"""
528533
return self._mnff.handle

cuda_core/cuda/core/experimental/_stream.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ def __cuda_stream__(self) -> Tuple[int, int]:
189189

190190
@property
191191
def handle(self) -> cuda.bindings.driver.CUstream:
192-
"""Return the underlying ``CUstream`` object."""
192+
"""Return the underlying ``CUstream`` object.
193+
194+
.. caution::
195+
196+
This handle is a Python object. To get the memory address of the underlying C
197+
handle, call ``int(Stream.handle)``.
198+
"""
193199
return self._mnff.handle
194200

195201
@property

0 commit comments

Comments
 (0)