Skip to content

Commit ee96758

Browse files
rparolinclaude
andcommitted
chore(cuda.core): satisfy pre-commit hooks
- ruff auto-applied: * Drop unused `_managed_memory_ops` test import (no longer needed after the legacy-bindings monkeypatch test was deleted) * Drop "Location" string-quoted forward refs in _managed_location.py (file already uses `from __future__ import annotations`) * Reformat string concatenations and add blank-line-after-import spacing - cython-lint auto-applied: * Drop unused libc.stdint cimport of `uintptr_t` * Drop unused `Location` Python import (only used in docstrings) * Drop unused `n` local in `discard()` * Move `cpython.mem cimport` of PyMem_Free / PyMem_Malloc inside the `IF CUDA_CORE_BUILD_MAJOR >= 13:` block where the symbols are actually used; cython-lint cannot see across compile-time branches. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b4d9cbf commit ee96758

3 files changed

Lines changed: 49 additions & 25 deletions

File tree

cuda_core/cuda/core/_memory/_managed_location.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ def __post_init__(self) -> None:
3535
raise ValueError(f"{self.kind} location must have id=None")
3636

3737
@classmethod
38-
def device(cls, device_id: int) -> "Location":
38+
def device(cls, device_id: int) -> Location:
3939
return cls(kind="device", id=device_id)
4040

4141
@classmethod
42-
def host(cls) -> "Location":
42+
def host(cls) -> Location:
4343
return cls(kind="host", id=None)
4444

4545
@classmethod
46-
def host_numa(cls, numa_id: int) -> "Location":
46+
def host_numa(cls, numa_id: int) -> Location:
4747
return cls(kind="host_numa", id=numa_id)
4848

4949
@classmethod
50-
def host_numa_current(cls) -> "Location":
50+
def host_numa_current(cls) -> Location:
5151
return cls(kind="host_numa_current", id=None)
5252

5353

@@ -71,9 +71,5 @@ def _coerce_location(value, *, allow_none: bool = False) -> Location | None:
7171
return Location.host()
7272
if value >= 0:
7373
return Location.device(value)
74-
raise ValueError(
75-
f"device ordinal must be >= 0 (or -1 for host), got {value}"
76-
)
77-
raise TypeError(
78-
f"location must be a Location, Device, int, or None; got {type(value).__name__}"
79-
)
74+
raise ValueError(f"device ordinal must be >= 0 (or -1 for host), got {value}")
75+
raise TypeError(f"location must be a Location, Device, int, or None; got {type(value).__name__}")

cuda_core/cuda/core/_memory/_managed_memory_ops.pyx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
from __future__ import annotations
66

7-
from cpython.mem cimport PyMem_Free, PyMem_Malloc
8-
from libc.stdint cimport uintptr_t
7+
IF CUDA_CORE_BUILD_MAJOR >= 13:
8+
from cpython.mem cimport PyMem_Free, PyMem_Malloc
99

1010
from cuda.bindings cimport cydriver
1111
from cuda.core._memory._buffer cimport Buffer
@@ -14,7 +14,7 @@ from cuda.core._stream cimport Stream, Stream_accept
1414
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
1515

1616
from cuda.core._utils.cuda_utils import driver
17-
from cuda.core._memory._managed_location import Location, _coerce_location
17+
from cuda.core._memory._managed_location import _coerce_location
1818

1919

2020
cdef dict _MANAGED_ADVICE_ALIASES = {
@@ -182,7 +182,6 @@ def discard(
182182
f"discard options must be None (reserved); got {type(options).__name__}"
183183
)
184184
cdef tuple bufs = _coerce_buffer_targets(targets, "discard")
185-
cdef Py_ssize_t n = len(bufs)
186185
cdef Stream s = Stream_accept(stream)
187186

188187
cdef Buffer buf

cuda_core/tests/test_memory.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
system as ccx_system,
4545
)
4646
from cuda.core._dlpack import DLDeviceType
47-
from cuda.core._memory import IPCBufferDescriptor, _managed_memory_ops
47+
from cuda.core._memory import IPCBufferDescriptor
4848
from cuda.core._utils.cuda_utils import CUDAError, handle_return
4949
from cuda.core.utils import StridedMemoryView
5050

@@ -1389,6 +1389,7 @@ def test_managed_memory_operation_validation(init_cuda):
13891389
with pytest.raises(ValueError, match="location is required"):
13901390
managed_memory.prefetch(buffer, stream=stream)
13911391
from cuda.core.managed_memory import Location
1392+
13921393
with pytest.raises(ValueError, match="does not support location_type='host_numa'"):
13931394
managed_memory.advise(buffer, "set_accessed_by", Location.host_numa(_INVALID_HOST_DEVICE_ORDINAL))
13941395

@@ -1875,42 +1876,50 @@ def test_memory_resource_alloc_zero_bytes(init_cuda, memory_resource_factory):
18751876
class TestLocation:
18761877
def test_device_constructor(self):
18771878
from cuda.core.managed_memory import Location
1879+
18781880
loc = Location.device(0)
18791881
assert loc.kind == "device"
18801882
assert loc.id == 0
18811883

18821884
def test_host_constructor(self):
18831885
from cuda.core.managed_memory import Location
1886+
18841887
loc = Location.host()
18851888
assert loc.kind == "host"
18861889
assert loc.id is None
18871890

18881891
def test_host_numa_constructor(self):
18891892
from cuda.core.managed_memory import Location
1893+
18901894
loc = Location.host_numa(3)
18911895
assert loc.kind == "host_numa"
18921896
assert loc.id == 3
18931897

18941898
def test_host_numa_current_constructor(self):
18951899
from cuda.core.managed_memory import Location
1900+
18961901
loc = Location.host_numa_current()
18971902
assert loc.kind == "host_numa_current"
18981903
assert loc.id is None
18991904

19001905
def test_frozen(self):
19011906
import dataclasses
1907+
19021908
from cuda.core.managed_memory import Location
1909+
19031910
loc = Location.device(0)
19041911
with pytest.raises(dataclasses.FrozenInstanceError):
19051912
loc.id = 1
19061913

19071914
def test_invalid_device_id(self):
19081915
from cuda.core.managed_memory import Location
1916+
19091917
with pytest.raises(ValueError, match="device id must be >= 0"):
19101918
Location.device(-1)
19111919

19121920
def test_invalid_kind(self):
19131921
from cuda.core.managed_memory import Location
1922+
19141923
with pytest.raises(ValueError, match="kind must be one of"):
19151924
Location(kind="not_a_kind", id=None)
19161925

@@ -1919,49 +1928,58 @@ class TestLocationCoerce:
19191928
def test_passthrough(self):
19201929
from cuda.core._memory._managed_location import _coerce_location
19211930
from cuda.core.managed_memory import Location
1931+
19221932
loc = Location.device(0)
19231933
assert _coerce_location(loc) is loc
19241934

19251935
def test_int_device(self):
19261936
from cuda.core._memory._managed_location import _coerce_location
1937+
19271938
assert _coerce_location(0).kind == "device"
19281939
assert _coerce_location(0).id == 0
19291940

19301941
def test_int_minus_one_is_host(self):
19311942
from cuda.core._memory._managed_location import _coerce_location
1943+
19321944
assert _coerce_location(-1).kind == "host"
19331945

19341946
def test_device_object(self, init_cuda):
19351947
from cuda.core import Device
19361948
from cuda.core._memory._managed_location import _coerce_location
1949+
19371950
dev = Device()
19381951
loc = _coerce_location(dev)
19391952
assert loc.kind == "device"
19401953
assert loc.id == dev.device_id
19411954

19421955
def test_none_when_disallowed(self):
19431956
from cuda.core._memory._managed_location import _coerce_location
1957+
19441958
with pytest.raises(ValueError, match="location is required"):
19451959
_coerce_location(None, allow_none=False)
19461960

19471961
def test_none_when_allowed(self):
19481962
from cuda.core._memory._managed_location import _coerce_location
1963+
19491964
assert _coerce_location(None, allow_none=True) is None
19501965

19511966
def test_bad_int(self):
19521967
from cuda.core._memory._managed_location import _coerce_location
1968+
19531969
with pytest.raises(ValueError, match="device ordinal"):
19541970
_coerce_location(-2)
19551971

19561972
def test_bad_type(self):
19571973
from cuda.core._memory._managed_location import _coerce_location
1974+
19581975
with pytest.raises(TypeError, match="Location, Device, int, or None"):
19591976
_coerce_location("device")
19601977

19611978

19621979
class TestPrefetch:
19631980
def test_single_with_location_host(self, init_cuda):
19641981
from cuda.core.managed_memory import Location, prefetch
1982+
19651983
device = Device()
19661984
skip_if_managed_memory_unsupported(device)
19671985
device.set_current()
@@ -1980,6 +1998,7 @@ def test_single_with_location_host(self, init_cuda):
19801998

19811999
def test_batched_same_location(self, init_cuda):
19822000
from cuda.core.managed_memory import Location, prefetch
2001+
19832002
device = Device()
19842003
skip_if_managed_memory_unsupported(device)
19852004
if not hasattr(driver, "cuMemPrefetchBatchAsync"):
@@ -2002,6 +2021,7 @@ def test_batched_same_location(self, init_cuda):
20022021

20032022
def test_batched_per_buffer_location(self, init_cuda):
20042023
from cuda.core.managed_memory import Location, prefetch
2024+
20052025
device = Device()
20062026
skip_if_managed_memory_unsupported(device)
20072027
if not hasattr(driver, "cuMemPrefetchBatchAsync"):
@@ -2029,6 +2049,7 @@ def test_batched_per_buffer_location(self, init_cuda):
20292049

20302050
def test_length_mismatch(self, init_cuda):
20312051
from cuda.core.managed_memory import Location, prefetch
2052+
20322053
device = Device()
20332054
skip_if_managed_memory_unsupported(device)
20342055
device.set_current()
@@ -2043,6 +2064,7 @@ def test_length_mismatch(self, init_cuda):
20432064

20442065
def test_rejects_non_managed(self, init_cuda):
20452066
from cuda.core.managed_memory import Location, prefetch
2067+
20462068
device = Device()
20472069
device.set_current()
20482070
buf = DummyDeviceMemoryResource(device).allocate(_MANAGED_TEST_ALLOCATION_SIZE)
@@ -2053,6 +2075,7 @@ def test_rejects_non_managed(self, init_cuda):
20532075

20542076
def test_location_required(self, init_cuda):
20552077
from cuda.core.managed_memory import prefetch
2078+
20562079
device = Device()
20572080
skip_if_managed_memory_unsupported(device)
20582081
device.set_current()
@@ -2065,6 +2088,7 @@ def test_location_required(self, init_cuda):
20652088

20662089
def test_options_must_be_none(self, init_cuda):
20672090
from cuda.core.managed_memory import Location, prefetch
2091+
20682092
device = Device()
20692093
skip_if_managed_memory_unsupported(device)
20702094
device.set_current()
@@ -2079,6 +2103,7 @@ def test_options_must_be_none(self, init_cuda):
20792103
class TestDiscard:
20802104
def test_single_buffer(self, init_cuda):
20812105
from cuda.core.managed_memory import Location, discard, prefetch
2106+
20822107
device = Device()
20832108
skip_if_managed_memory_unsupported(device)
20842109
if not hasattr(driver, "cuMemDiscardBatchAsync"):
@@ -2095,6 +2120,7 @@ def test_single_buffer(self, init_cuda):
20952120

20962121
def test_batched(self, init_cuda):
20972122
from cuda.core.managed_memory import Location, discard, prefetch
2123+
20982124
device = Device()
20992125
skip_if_managed_memory_unsupported(device)
21002126
if not hasattr(driver, "cuMemDiscardBatchAsync"):
@@ -2112,6 +2138,7 @@ def test_batched(self, init_cuda):
21122138

21132139
def test_rejects_non_managed(self, init_cuda):
21142140
from cuda.core.managed_memory import discard
2141+
21152142
device = Device()
21162143
device.set_current()
21172144
buf = DummyDeviceMemoryResource(device).allocate(_MANAGED_TEST_ALLOCATION_SIZE)
@@ -2122,6 +2149,7 @@ def test_rejects_non_managed(self, init_cuda):
21222149

21232150
def test_options_must_be_none(self, init_cuda):
21242151
from cuda.core.managed_memory import discard
2152+
21252153
device = Device()
21262154
skip_if_managed_memory_unsupported(device)
21272155
device.set_current()
@@ -2136,6 +2164,7 @@ def test_options_must_be_none(self, init_cuda):
21362164
class TestDiscardPrefetch:
21372165
def test_single_buffer(self, init_cuda):
21382166
from cuda.core.managed_memory import Location, discard_prefetch, prefetch
2167+
21392168
device = Device()
21402169
skip_if_managed_memory_unsupported(device)
21412170
if not hasattr(driver, "cuMemDiscardAndPrefetchBatchAsync"):
@@ -2159,6 +2188,7 @@ def test_single_buffer(self, init_cuda):
21592188

21602189
def test_batched_same_location(self, init_cuda):
21612190
from cuda.core.managed_memory import Location, discard_prefetch, prefetch
2191+
21622192
device = Device()
21632193
skip_if_managed_memory_unsupported(device)
21642194
if not hasattr(driver, "cuMemDiscardAndPrefetchBatchAsync"):
@@ -2181,6 +2211,7 @@ def test_batched_same_location(self, init_cuda):
21812211

21822212
def test_length_mismatch(self, init_cuda):
21832213
from cuda.core.managed_memory import Location, discard_prefetch
2214+
21842215
device = Device()
21852216
skip_if_managed_memory_unsupported(device)
21862217
device.set_current()
@@ -2194,6 +2225,7 @@ def test_length_mismatch(self, init_cuda):
21942225

21952226
def test_rejects_non_managed(self, init_cuda):
21962227
from cuda.core.managed_memory import Location, discard_prefetch
2228+
21972229
device = Device()
21982230
device.set_current()
21992231
buf = DummyDeviceMemoryResource(device).allocate(_MANAGED_TEST_ALLOCATION_SIZE)
@@ -2205,14 +2237,12 @@ def test_rejects_non_managed(self, init_cuda):
22052237

22062238
class TestAdvise:
22072239
def test_batched_same_advice(self, init_cuda):
2208-
from cuda.core.managed_memory import advise, Location
2240+
from cuda.core.managed_memory import advise
2241+
22092242
device = Device()
22102243
_skip_if_managed_location_ops_unsupported(device)
22112244
device.set_current()
2212-
bufs = [
2213-
DummyUnifiedMemoryResource(device).allocate(_MANAGED_TEST_ALLOCATION_SIZE)
2214-
for _ in range(2)
2215-
]
2245+
bufs = [DummyUnifiedMemoryResource(device).allocate(_MANAGED_TEST_ALLOCATION_SIZE) for _ in range(2)]
22162246
advise(bufs, "set_read_mostly")
22172247
for buf in bufs:
22182248
assert (
@@ -2225,14 +2255,12 @@ def test_batched_same_advice(self, init_cuda):
22252255
buf.close()
22262256

22272257
def test_batched_per_buffer_location(self, init_cuda):
2228-
from cuda.core.managed_memory import advise, Location
2258+
from cuda.core.managed_memory import Location, advise
2259+
22292260
device = Device()
22302261
_skip_if_managed_location_ops_unsupported(device)
22312262
device.set_current()
2232-
bufs = [
2233-
DummyUnifiedMemoryResource(device).allocate(_MANAGED_TEST_ALLOCATION_SIZE)
2234-
for _ in range(2)
2235-
]
2263+
bufs = [DummyUnifiedMemoryResource(device).allocate(_MANAGED_TEST_ALLOCATION_SIZE) for _ in range(2)]
22362264
advise(
22372265
bufs,
22382266
"set_preferred_location",
@@ -2243,6 +2271,7 @@ def test_batched_per_buffer_location(self, init_cuda):
22432271

22442272
def test_options_must_be_none(self, init_cuda):
22452273
from cuda.core.managed_memory import advise
2274+
22462275
device = Device()
22472276
_skip_if_managed_allocation_unsupported(device)
22482277
device.set_current()

0 commit comments

Comments
 (0)