-
Notifications
You must be signed in to change notification settings - Fork 234
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
4
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,479
−133
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
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 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 * | ||
|
|
||
| initialize() | ||
|
|
||
| __all__.extend( | ||
| [ | ||
| "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,93 @@ | ||
| # 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() | ||
|
|
||
|
|
||
| def initialize() -> None: | ||
| """Idempotent (per-process) initialization of NVUtil's NVML | ||
|
|
||
| Notes | ||
| ----- | ||
|
|
||
| Modifies global variables _NVML_STATE and _NVML_OWNER_PID""" | ||
| 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=})") | ||
|
|
||
|
|
||
| def is_initialized() -> bool: | ||
| """ | ||
| 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 | ||
|
|
||
|
|
||
| def validate() -> None: | ||
| """ | ||
| Validate NVML state. | ||
|
|
||
| Validate that NVML is functional and that the system has at least one GPU available. | ||
|
|
||
| Raises | ||
| ------ | ||
| 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("The underlying NVML library was not found") | ||
| elif nvml.device_get_count_v2() == 0: | ||
| raise exceptions.GpuNotFoundError("No GPUs available") |
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...