Skip to content

Fix skipping the check for nvidia-smi - #1084

Merged
leofang merged 1 commit into
NVIDIA:mainfrom
leofang:fix_err
Oct 4, 2025
Merged

Fix skipping the check for nvidia-smi#1084
leofang merged 1 commit into
NVIDIA:mainfrom
leofang:fix_err

Conversation

@leofang

Copy link
Copy Markdown
Member

Description

Found during local debugging with Andy.

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@leofangleofang added this to the cuda.core beta 7 milestone Oct 3, 2025
@leofang
leofang requested a review from Andy-JostOctober 3, 2025 20:16
@leofangleofang self-assigned this Oct 3, 2025
@leofangleofang added bug Something isn't working P0 High priority - Must do! cuda.core Everything related to the cuda.core module labels Oct 3, 2025
@copy-pr-bot

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@leofang

Copy link
Copy Markdown
MemberAuthor

/ok to test 4e19418

if m:
return m.group(1).split(".")[0]
except FileNotFoundError:
except (FileNotFoundError, subprocess.CalledProcessError):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: use shutil.which, since CalledProcessError can be literally any error that comes from running the command.

Not blocking the review though!

@github-actions

This comment has been minimized.

@kkraus14

Copy link
Copy Markdown
Collaborator

Using nvidia-smi isn't the right answer here regardless. It's 100% valid to build cuda.core with CUDA 12.x libraries on a machine with a CUDA 13+ driver.

@cpcloud

Copy link
Copy Markdown
Contributor

In theory, one could also not have nvidia-smi available and that shouldn't matter.

@rwgk

rwgk commented Oct 3, 2025

Copy link
Copy Markdown
Contributor

Purely as a bug fix this PR seems fine to me.

But bigger picture:

For building we don't actually need a GPU, so the driver version and nvidia-smi don't meaningfully matter (what @kkraus14 and @cpcloud said already).

We already have a hard requirement that CUDA_HOME (or CUDA_PATH) are set:

@functools.cache
defget_cuda_paths():
CUDA_PATH=os.environ.get("CUDA_PATH", os.environ.get("CUDA_HOME", None))
ifnotCUDA_PATH:
raiseRuntimeError("Environment variable CUDA_PATH or CUDA_HOME is not set")
CUDA_PATH=CUDA_PATH.split(os.pathsep)
print("CUDA paths:", CUDA_PATH)
returnCUDA_PATH

That defines conclusively what CUDA version the build is for.

We are also sure that we need the headers for the build. So in the given context this should always work:

$ grep '#define\s\s*CUDA_VERSION' $CUDA_HOME/include/cuda.h
#define CUDA_VERSION 13000

Maybe a better fix is to integrate something like this?

from __future__ importannotationsimportrefrompathlibimportPathdefget_cuda_version_macro(cuda_home: str|Path) ->int|None:
""" Given CUDA_HOME, try to extract the CUDA_VERSION macro from include/cuda.h. Example line in cuda.h: #define CUDA_VERSION 13000 Returns the integer (e.g. 13000) or None if not found / on error. """try:
cuda_h=Path(cuda_home) /"include"/"cuda.h"ifnotcuda_h.is_file():
returnNonetext=cuda_h.read_text(encoding="utf-8", errors="ignore")
m=re.search(r"^\s*#define\s+CUDA_VERSION\s+(\d+)", text, re.MULTILINE)
ifm:
returnint(m.group(1))
exceptException:
passreturnNone

@leofang

leofang commented Oct 3, 2025

Copy link
Copy Markdown
MemberAuthor

Yes switching to check the major version based on CUDA_VERSION from header would have been my last-minute task if I could have wrapped up today, but looks like I am still hunting down a naughty file descriptor with Andy. @rwgk if you want to rewrite this check, please feel free (either push to my branch or create a new PR, and I'll close this one!)

@leofang

leofang commented Oct 3, 2025

Copy link
Copy Markdown
MemberAuthor

Well let me take a step back. There is a reason that we want nvidia-smi to play a role. For local development, if the user does not already have cuda-bindings installed (thus triggering the latter checks) we want to ensure we build a cuda.core that uses cuda.bindings whose version is runnable on the user's driver. Now, we don't have a way to inject extra run-time dependencies through the build time info yet, so this is not fully doable, but at least we can think about how this can be approached and see the value of checking driver versions.

@rwgk

rwgk commented Oct 3, 2025

Copy link
Copy Markdown
Contributor

For local development, if the user does not already have cuda-bindings installed (thus triggering the latter checks) we want to ensure we build a cuda.core that uses cuda.bindings whose version is runnable on the user's driver.

I looked into that, too (before already), I'm attaching the POC implementation for Linux; I believe Windows will work similarly.

The reasoning behind it:

  • libcuda.so is installed with the driver.
  • It is needed at boot time, therefore we can count on it being found via the system dynamic library search.
  • ctypes-based Python code to call cuDriverGetVersion is almost trivial (attached POC, LLM-generated in a second or two).
  • If the driver is actually installed, we can rely on the Python code to work.
from __future__ importannotationsimportctypesimportosfromtypingimportOptionaldefcuda_driver_version() ->Optional[int]:
""" Linux-only. Try to load `libcuda.so` via standard dynamic library lookup and call `CUresult cuDriverGetVersion(int* driverVersion)`. Returns: int : driver version (e.g., 12040 for 12.4), if successful. None : on any failure (load error, missing symbol, non-success CUresult). """# CUDA_SUCCESS = 0CUDA_SUCCESS=0try:
# Use system search paths only; do not provide an absolute path.# Make symbols globally available to any dependent libraries.mode=os.RTLD_NOW|os.RTLD_GLOBALlib=ctypes.CDLL("libcuda.so", mode=mode)
exceptOSError:
returnNonetry:
cuDriverGetVersion=lib.cuDriverGetVersionexceptAttributeError:
# Symbol not found in the loaded library.returnNone# int cuDriverGetVersion(int* driverVersion);cuDriverGetVersion.restype=ctypes.c_int# CUresultcuDriverGetVersion.argtypes= [ctypes.POINTER(ctypes.c_int)]
out=ctypes.c_int(0)
try:
rc=cuDriverGetVersion(ctypes.byref(out))
exceptException:
returnNoneifrc!=CUDA_SUCCESS:
returnNonereturnint(out.value)
if__name__=="__main__":
print(cuda_driver_version())

@rwgk

rwgk commented Oct 3, 2025

Copy link
Copy Markdown
Contributor

For this PR, I'd say just merge, it's definitely an improvement, and it exists already.

I'll work on another PR to integrate the get_cuda_version_macro() code I posted before and we can continue the discussion there.

rwgk
rwgk approved these changes Oct 3, 2025
@leofang
leofang merged commit b09d7ed into NVIDIA:mainOct 4, 2025
74 checks passed
@leofang
leofang deleted the fix_err branch October 4, 2025 00:00
@github-actions

Copy link
Copy Markdown
Doc Preview CI
Preview removed because the pull request was closed or merged.

rwgk pushed a commit to rwgk/cuda-python that referenced this pull request Oct 4, 2025
leofang added a commit that referenced this pull request Oct 7, 2025
* _decide_nvjitlink_or_driver(): catch RuntimeError (bug fix), use importlib + ModuleNotFoundError (more selective than ImportError) and produce specific error messages
* Fix misunderstanding: RuntimeError is raised only from inner_nvjitlink._inspect_function_pointer()
* Better way of formatting warning messages.
* Change from importlib.import_module() to plain import (the latter does also raise ModuleNotFoundError)
* Enhance to warning messages, to make them actionable.
* Factor out _nvjitlink_has_version_symbol() for clarity and testability
This aids unit testing by allowing localized stubbing of the version-symbol
check, without needing to patch the full inner nvjitlink module.
* Add test_linker_warnings.py
As generated by ChatGPT 5, with minor manual tweaks.
* Fix "the the" oversight
* Replace "culink APIs" → "driver APIs" in warning message.
* Fix oversight: test_linker_warnings.py needs to be updated after commit 0948942
* fix skipping the check for nvidia-smi (#1084)
* rm cuda_core/tests/test_linker_warnings.py: see #1095
---------
Co-authored-by: Leo Fang <leof@nvidia.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugSomething isn't workingcuda.coreEverything related to the cuda.core moduleP0High priority - Must do!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@leofang@kkraus14@cpcloud@rwgk