Skip to content

Commit 6aaf4e1

Browse files
committed
Use cimport where possible
1 parent dc56154 commit 6aaf4e1

8 files changed

Lines changed: 24 additions & 33 deletions

File tree

cuda_core/cuda/core/system/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
__all__.extend(
2626
[
2727
"initialize",
28+
"get_nvml_version",
2829
"Device",
2930
"DeviceArchitecture",
3031
"UninitializedError",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
cpdef initialize()
6+
cpdef bint is_initialized()
7+
cpdef validate()

cuda_core/cuda/core/system/_nvml_context.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ _NVML_OWNER_PID = 0
2828
_lock = threading.Lock()
2929

3030

31-
def initialize() -> None:
31+
cpdef initialize():
3232
"""Idempotent (per-process) initialization of Nvidia Management Library (NVML).
3333
3434
Notes
@@ -62,7 +62,7 @@ def initialize() -> None:
6262
raise RuntimeError(f"Unhandled initialisation state ({_NVML_STATE=}, {_NVML_OWNER_PID=})")
6363

6464

65-
def is_initialized() -> bool:
65+
cpdef bint is_initialized():
6666
"""
6767
Check whether the NVML context is initialized on this process.
6868
@@ -74,7 +74,7 @@ def is_initialized() -> bool:
7474
return _NVML_STATE == _NVMLState.INITIALIZED and os.getpid() == _NVML_OWNER_PID
7575

7676

77-
def validate() -> None:
77+
cpdef validate():
7878
"""
7979
Validate NVML state.
8080

cuda_core/cuda/core/system/device.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ from typing import Iterable
1010

1111
from cuda.bindings import _nvml as nvml
1212

13-
from ._nvml_context import validate
14-
from .utils import unpack_bitmask
13+
from ._nvml_context cimport validate
14+
from .utils cimport unpack_bitmask
1515

1616

1717
class DeviceArchitecture:

cuda_core/cuda/core/system/system.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ HAS_WORKING_NVML = _BINDINGS_VERSION >= (13, 1, 2) or (_BINDINGS_VERSION[0] == 1
1515

1616
if HAS_WORKING_NVML:
1717
from cuda.bindings import _nvml as nvml
18-
from ._nvml_context import validate
18+
from ._nvml_context cimport validate
1919
else:
2020
from cuda.core._utils.cuda_utils import driver, handle_return, runtime
2121

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
6+
from cpython cimport array
7+
8+
9+
cpdef list[int] unpack_bitmask(x: list[int] | array.array)

cuda_core/cuda/core/system/utils.pyx

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,6 @@ from cpython cimport array
66
from libc.stdint cimport uint64_t
77

88

9-
cpdef str format_bytes(uint64_t x):
10-
"""Return formatted string in B, KiB, MiB, GiB or TiB"""
11-
if x < 1024:
12-
return f"{x} B"
13-
elif x < 1024**2:
14-
return f"{x / 1024:.2f} KiB"
15-
elif x < 1024**3:
16-
return f"{x / 1024**2:.2f} MiB"
17-
elif x < 1024**4:
18-
return f"{x / 1024**3:.2f} GiB"
19-
else:
20-
return f"{x / 1024**4:.2f} TiB"
21-
22-
239
cpdef list[int] unpack_bitmask(x: list[int] | array.array):
2410
"""
2511
Unpack a list of integers containing bitmasks.

cuda_core/tests/system/test_system_utils.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import pytest
6-
from cuda.core.system.utils import format_bytes, unpack_bitmask
7-
8-
9-
def test_format_bytes():
10-
assert format_bytes(0) == "0 B"
11-
assert format_bytes(1) == "1 B"
12-
assert format_bytes(1023) == "1023 B"
13-
assert format_bytes(1024) == "1.00 KiB"
14-
assert format_bytes(1024**2) == "1.00 MiB"
15-
assert format_bytes(1024**3) == "1.00 GiB"
16-
assert format_bytes(1024**4) == "1.00 TiB"
17-
assert format_bytes(1024**5) == "1024.00 TiB"
18-
assert format_bytes(1024**6) == "1048576.00 TiB"
6+
from cuda.core.system.utils import unpack_bitmask
197

208

219
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)