-
Notifications
You must be signed in to change notification settings - Fork 236
Initial import of cuda.core.system
#1393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mdboom
wants to merge
19
commits into
NVIDIA:main
Choose a base branch
from
mdboom:cuda.core.system
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,104
−169
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0cff9c7
First pass at cuda.core.system
mdboom 71ffb27
Fix fallback to non-working NVML
mdboom 5cb0264
Precommit fixes
mdboom 599a7da
Move exceptions related to NVML to their own module.
mdboom aa4752d
Merge remote-tracking branch 'upstream/main' into cuda.core.system
mdboom fc97709
Update version check
mdboom 2647e8b
Add verbose logging for doc build
mdboom 7097c23
Try to get doc failure info
mdboom a323200
Try to get more info
mdboom dc56154
No implicit init, and fix docs build
mdboom 6aaf4e1
Use cimport where possible
mdboom dcfee0b
Move some tests
mdboom 1951748
Improve docs
mdboom cfad2b3
Fix cyclical imports
mdboom 29f5f07
Merge remote-tracking branch 'upstream/main' into cuda.core.system
mdboom 4350787
Remove more cimports
mdboom e2bc6e5
Fix exposure of unpack_bitmask
mdboom ef4579f
Fix tests that use cuda.core.system
mdboom 615314d
Merge remote-tracking branch 'upstream/main' into cuda.core.system
mdboom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # ruff: noqa: F403, F405 | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "get_driver_version", | ||
| "get_driver_version_full", | ||
| "get_gpu_driver_version", | ||
| "get_num_devices", | ||
| "get_process_name", | ||
| "HAS_WORKING_NVML", | ||
| ] | ||
|
|
||
|
|
||
| from .system import * | ||
|
|
||
| if HAS_WORKING_NVML: | ||
| from ._nvml_context import initialize | ||
| from .device import Device, DeviceArchitecture | ||
| from .exceptions import * | ||
|
|
||
| __all__.extend( | ||
| [ | ||
| "initialize", | ||
| "get_nvml_version", | ||
| "Device", | ||
| "DeviceArchitecture", | ||
| "UninitializedError", | ||
| "InvalidArgumentError", | ||
| "NotSupportedError", | ||
| "NoPermissionError", | ||
| "AlreadyInitializedError", | ||
| "NotFoundError", | ||
| "InsufficientSizeError", | ||
| "InsufficientPowerError", | ||
| "DriverNotLoadedError", | ||
| "TimeoutError", | ||
| "IrqIssueError", | ||
| "LibraryNotFoundError", | ||
| "FunctionNotFoundError", | ||
| "CorruptedInforomError", | ||
| "GpuIsLostError", | ||
| "ResetRequiredError", | ||
| "OperatingSystemError", | ||
| "LibRmVersionMismatchError", | ||
| "InUseError", | ||
| "MemoryError", | ||
| "NoDataError", | ||
| "VgpuEccNotSupportedError", | ||
| "InsufficientResourcesError", | ||
| "FreqNotSupportedError", | ||
| "ArgumentVersionMismatchError", | ||
| "DeprecatedError", | ||
| "NotReadyError", | ||
| "GpuNotFoundError", | ||
| "InvalidStateError", | ||
| "ResetTypeNotSupportedError", | ||
| "UnknownError", | ||
| ] | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import os | ||
| import threading | ||
|
|
||
| from cuda.bindings import _nvml as nvml | ||
|
|
||
| from . import exceptions | ||
|
|
||
|
|
||
| ctypedef enum _NVMLState: | ||
| UNINITIALIZED = 0 | ||
| INITIALIZED = 1 | ||
| DISABLED_LIBRARY_NOT_FOUND = 2 | ||
|
|
||
|
|
||
| # Initialisation must occur per-process, so an initialised state is a | ||
| # (state, pid) pair | ||
| _NVML_STATE = _NVMLState.UNINITIALIZED | ||
| # """Current initialization state""" | ||
|
|
||
| _NVML_OWNER_PID = 0 | ||
| # """PID of process that successfully called pynvml.nvmlInit""" | ||
|
|
||
|
|
||
| _lock = threading.Lock() | ||
|
|
||
|
|
||
| cpdef initialize(): | ||
| """ | ||
| Initializes Nvidia Management Library (NVML), ensuring it only happens once per process. | ||
| """ | ||
| global _NVML_STATE, _NVML_OWNER_PID | ||
|
|
||
| with _lock: | ||
| if _NVML_STATE == _NVMLState.DISABLED_LIBRARY_NOT_FOUND or ( | ||
| _NVML_STATE == _NVMLState.INITIALIZED and os.getpid() == _NVML_OWNER_PID | ||
| ): | ||
| return | ||
| elif ( | ||
| _NVML_STATE == _NVMLState.INITIALIZED and os.getpid() != _NVML_OWNER_PID | ||
| ) or _NVML_STATE == _NVMLState.UNINITIALIZED: | ||
| try: | ||
| nvml.init_v2() | ||
| except ( | ||
| exceptions.LibraryNotFoundError, | ||
| exceptions.DriverNotLoadedError, | ||
| exceptions.UnknownError, | ||
| ): | ||
| _NVML_STATE = _NVMLState.DISABLED_LIBRARY_NOT_FOUND | ||
| return | ||
|
|
||
| # initialization was successful | ||
| _NVML_STATE = _NVMLState.INITIALIZED | ||
| _NVML_OWNER_PID = os.getpid() | ||
| else: | ||
| raise RuntimeError(f"Unhandled initialisation state ({_NVML_STATE=}, {_NVML_OWNER_PID=})") | ||
|
|
||
|
|
||
| cpdef bint is_initialized(): | ||
| """ | ||
| Check whether the NVML context is initialized on this process. | ||
|
|
||
| Returns | ||
| ------- | ||
| result: bool | ||
| Whether the NVML context is initialized on this process. | ||
| """ | ||
| return _NVML_STATE == _NVMLState.INITIALIZED and os.getpid() == _NVML_OWNER_PID | ||
|
|
||
|
|
||
| cpdef validate(): | ||
| """ | ||
| Validate NVML state. | ||
|
|
||
| Validate that NVML is initialized, functional and that the system has at | ||
| least one GPU available. | ||
|
|
||
| Raises | ||
| ------ | ||
| nvml.UninitializedError | ||
| If NVML hasn't been initialized. | ||
| nvml.LibraryNotFoundError | ||
| If the NVML library could not be found. | ||
| nvml.GpuNotFoundError | ||
| If no GPUs are available. | ||
| """ | ||
| if _NVML_STATE == _NVMLState.DISABLED_LIBRARY_NOT_FOUND: | ||
| raise exceptions.LibraryNotFoundError() | ||
| elif not is_initialized(): | ||
| raise exceptions.UninitializedError() | ||
| elif nvml.device_get_count_v2() == 0: | ||
| raise exceptions.GpuNotFoundError() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI,
cuda.coresupports anycuda-bindings/cuda-python12.x and 13.x, many of which do not have the NVML bindings available. So, we need a version guard here before importing anything that would expect the bindings to exist, and raise an exception in such cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good reminder. I guess that precludes
cimport'ing anything fromcuda.bindings._nvml, since_nvmlis a moving target. Will just take that out for now...