Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cuda_pathfinder/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Windows.
- unknown name -> `DynamicLibUnknownError`
- known but unsupported on this OS -> `DynamicLibNotAvailableError`
- known/supported but not found/loadable -> `DynamicLibNotFoundError`
- **Spell `found_via` labels once**: package code uses the constants in
`cuda/pathfinder/_utils/found_via.py` and never types the label strings.
The labels are public and the Linux loader branches on them. Tests type
the strings on purpose: that pins the public values.
- **Do not hardcode host assumptions**: avoid baking in machine-local paths,
shell-specific quoting, or environment assumptions.
- **Prefer focused abstractions**: if a change is platform-specific, route it
Expand Down
16 changes: 11 additions & 5 deletions cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
from typing import TYPE_CHECKING, cast

from cuda.pathfinder._dynamic_libs.load_dl_common import LoadedDL
from cuda.pathfinder._utils.found_via import (
FOUND_VIA_CONDA,
FOUND_VIA_SITE_PACKAGES,
FOUND_VIA_SYSTEM_SEARCH,
FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE,
)

if TYPE_CHECKING:
from cuda.pathfinder._dynamic_libs.lib_descriptor import LibDescriptor
Expand Down Expand Up @@ -142,7 +148,7 @@ def check_if_already_loaded_from_elsewhere(desc: LibDescriptor) -> LoadedDL | No
abs_path_for_dynamic_library(desc.name, handle),
True,
handle._handle,
"was-already-loaded-from-elsewhere",
FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE,
)
return None

Expand Down Expand Up @@ -178,17 +184,17 @@ def load_with_system_search(desc: LibDescriptor) -> LoadedDL | None:
else:
abs_path = abs_path_for_dynamic_library(desc.name, handle)
assert abs_path
return LoadedDL(abs_path, False, handle._handle, "system-search")
return LoadedDL(abs_path, False, handle._handle, FOUND_VIA_SYSTEM_SEARCH)
return None


def _preload_nvrtc_builtins(found_path: str, found_via: str | None) -> None:
# Let Conda packaging provide sibling lookup through RPATH.
if found_via == "conda":
if found_via == FOUND_VIA_CONDA:
return
real_path = os.path.realpath(found_path)
version = os.path.basename(real_path).removeprefix("libnvrtc.so.").split(".")
if found_via == "site-packages":
if found_via == FOUND_VIA_SITE_PACKAGES:
# Retain only the historical 13.0 wheel workaround.
if version[0] != "13":
return
Expand All @@ -204,7 +210,7 @@ def _preload_nvrtc_builtins(found_path: str, found_via: str | None) -> None:
major, minor = version[0], version[1]
sibling = os.path.join(os.path.dirname(real_path), f"libnvrtc-builtins.so.{major}.{minor}")
if not os.path.isfile(sibling):
if found_via != "site-packages":
if found_via != FOUND_VIA_SITE_PACKAGES:
warnings.warn(f"NVRTC builtins sibling not found: {sibling}", RuntimeWarning, stacklevel=2)
return
# If preloading fails, NVRTC's own builtins lookup may still succeed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import TYPE_CHECKING

from cuda.pathfinder._dynamic_libs.load_dl_common import LoadedDL
from cuda.pathfinder._utils.found_via import FOUND_VIA_SYSTEM_SEARCH, FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE

if TYPE_CHECKING:
from cuda.pathfinder._dynamic_libs.lib_descriptor import LibDescriptor
Expand Down Expand Up @@ -129,7 +130,9 @@ def check_if_already_loaded_from_elsewhere(desc: LibDescriptor) -> LoadedDL | No
# Match load_with_abs_path(): lazy component DLLs need the directory
# of the module that is actually loaded, regardless of how it arrived.
add_dll_directory(abs_path)
return LoadedDL(abs_path, True, ctypes_handle_to_unsigned_int(handle), "was-already-loaded-from-elsewhere")
return LoadedDL(
abs_path, True, ctypes_handle_to_unsigned_int(handle), FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE
)
return None


Expand All @@ -153,7 +156,7 @@ def load_with_system_search(desc: LibDescriptor) -> LoadedDL | None:
handle = kernel32.LoadLibraryExW(dll_name, None, 0)
if handle:
abs_path = abs_path_for_dynamic_library(desc.name, handle)
return LoadedDL(abs_path, False, ctypes_handle_to_unsigned_int(handle), "system-search")
return LoadedDL(abs_path, False, ctypes_handle_to_unsigned_int(handle), FOUND_VIA_SYSTEM_SEARCH)

return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError
from cuda.pathfinder._dynamic_libs.supported_nvidia_libs import is_suppressed_dll_file
from cuda.pathfinder._utils.find_sub_dirs import find_sub_dirs_all_sitepackages
from cuda.pathfinder._utils.found_via import FOUND_VIA_SITE_PACKAGES
from cuda.pathfinder._utils.platform_aware import IS_WINDOWS
from cuda.pathfinder._utils.windows_arch import windows_pe_matches_arch, windows_python_arch

Expand Down Expand Up @@ -177,7 +178,7 @@ def find_lib_in_site_packages(ctx: SearchContext) -> FindResult | NotFound:
for abs_dir in find_sub_dirs_all_sitepackages(sub_dir):
lib_path = _find_lib_file(ctx, abs_dir)
if lib_path is not None:
return FindResult(lib_path, "site-packages")
return FindResult(lib_path, FOUND_VIA_SITE_PACKAGES)
sub_dirs_searched.append(sub_dir)
missed = NotFound()
for sub_dir in sub_dirs_searched:
Expand Down
14 changes: 10 additions & 4 deletions cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
find_lib_in_site_packages,
)
from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home
from cuda.pathfinder._utils.found_via import (
FOUND_VIA_CONDA,
FOUND_VIA_CUDA_PATH,
FOUND_VIA_PROGRAM_FILES,
FOUND_VIA_SYSTEM_CTK_ROOT,
)
from cuda.pathfinder._utils.path_sort import numeric_aware_path_sort_key

from .._utils.resolve_path import resolve_path
Expand Down Expand Up @@ -123,7 +129,7 @@ def derive_ctk_root(resolved_lib_path: str) -> str | None:

def find_via_ctk_root(ctx: SearchContext, ctk_root: str) -> FindResult | NotFound:
"""Find a library under a previously derived CTK root."""
return _find_under_anchor_root(ctx, ctk_root, "system-ctk-root")
return _find_under_anchor_root(ctx, ctk_root, FOUND_VIA_SYSTEM_CTK_ROOT)


# ---------------------------------------------------------------------------
Expand All @@ -144,7 +150,7 @@ def find_in_conda(ctx: SearchContext) -> FindResult | NotFound:
if not conda_prefix:
return NotFound()
anchor = ctx.conda_anchor_point(conda_prefix)
return _find_under_anchor_root(ctx, anchor, "conda")
return _find_under_anchor_root(ctx, anchor, FOUND_VIA_CONDA)


def find_in_install_root_env_vars(ctx: SearchContext) -> FindResult | NotFound:
Expand Down Expand Up @@ -176,7 +182,7 @@ def find_in_cuda_path(ctx: SearchContext) -> FindResult | NotFound:
cuda_home = get_cuda_path_or_home()
if cuda_home is None:
return NotFound()
return _find_under_anchor_root(ctx, cuda_home, "CUDA_PATH")
return _find_under_anchor_root(ctx, cuda_home, FOUND_VIA_CUDA_PATH)


def find_in_program_files_roots(ctx: SearchContext) -> FindResult | NotFound:
Expand All @@ -186,7 +192,7 @@ def find_in_program_files_roots(ctx: SearchContext) -> FindResult | NotFound:
for root in sorted(glob.glob(root_glob), key=numeric_aware_path_sort_key, reverse=True):
if not os.path.isdir(root):
continue
result = _find_under_anchor_root(ctx, str(resolve_path(root)), "ProgramFiles")
result = _find_under_anchor_root(ctx, str(resolve_path(root)), FOUND_VIA_PROGRAM_FILES)
if result.found:
return result
missed += result
Expand Down
17 changes: 12 additions & 5 deletions cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
from cuda.pathfinder._utils.ctk_root_canary import CTK_ROOT_CANARY_ANCHOR_LIBNAMES
from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home
from cuda.pathfinder._utils.find_sub_dirs import find_sub_dirs_all_sitepackages
from cuda.pathfinder._utils.found_via import (
FOUND_VIA_CONDA,
FOUND_VIA_CUDA_PATH,
FOUND_VIA_SITE_PACKAGES,
FOUND_VIA_SUPPORTED_INSTALL_DIR,
FOUND_VIA_SYSTEM_CTK_ROOT,
)
from cuda.pathfinder._utils.path_sort import numeric_aware_path_sort_key

from .._utils.resolve_path import resolve_path
Expand Down Expand Up @@ -87,7 +94,7 @@ def find_in_site_packages(desc: HeaderDescriptor) -> LocatedHeaderDir | None:
hdr_dir: str # help mypy
for hdr_dir in find_sub_dirs_all_sitepackages(tuple(sub_dir.split("/"))):
if _joined_isfile(hdr_dir, desc.header_basename):
return LocatedHeaderDir(abs_path=hdr_dir, found_via="site-packages")
return LocatedHeaderDir(abs_path=hdr_dir, found_via=FOUND_VIA_SITE_PACKAGES)
return None


Expand All @@ -101,7 +108,7 @@ def find_in_conda(desc: HeaderDescriptor) -> LocatedHeaderDir | None:
return None
found_header_path = _locate_in_anchor_layout(desc, anchor_point)
if found_header_path:
return LocatedHeaderDir(abs_path=found_header_path, found_via="conda")
return LocatedHeaderDir(abs_path=found_header_path, found_via=FOUND_VIA_CONDA)
return None


Expand All @@ -124,7 +131,7 @@ def find_in_cuda_path(desc: HeaderDescriptor) -> LocatedHeaderDir | None:
return None
result = _locate_in_anchor_layout(desc, cuda_home)
if result is not None:
return LocatedHeaderDir(abs_path=result, found_via="CUDA_PATH")
return LocatedHeaderDir(abs_path=result, found_via=FOUND_VIA_CUDA_PATH)
return None


Expand All @@ -146,7 +153,7 @@ def find_via_ctk_root_canary(desc: HeaderDescriptor) -> LocatedHeaderDir | None:
return None
result = _locate_in_anchor_layout(desc, ctk_root)
if result is not None:
return LocatedHeaderDir(abs_path=result, found_via="system-ctk-root")
return LocatedHeaderDir(abs_path=result, found_via=FOUND_VIA_SYSTEM_CTK_ROOT)
return None


Expand All @@ -155,7 +162,7 @@ def find_in_system_install_dirs(desc: HeaderDescriptor) -> LocatedHeaderDir | No
for pattern in system_install_dir_patterns(desc):
for hdr_dir in sorted(glob.glob(pattern), key=numeric_aware_path_sort_key, reverse=True):
if _joined_isfile(hdr_dir, desc.header_basename):
return LocatedHeaderDir(abs_path=hdr_dir, found_via="supported_install_dir")
return LocatedHeaderDir(abs_path=hdr_dir, found_via=FOUND_VIA_SUPPORTED_INSTALL_DIR)
return None


Expand Down
7 changes: 4 additions & 3 deletions cuda_pathfinder/cuda/pathfinder/_static_libs/locate_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home
from cuda.pathfinder._utils.find_sub_dirs import find_sub_dirs_all_sitepackages
from cuda.pathfinder._utils.found_via import FOUND_VIA_CONDA, FOUND_VIA_CUDA_PATH, FOUND_VIA_SITE_PACKAGES
from cuda.pathfinder._utils.platform_aware import IS_WINDOWS

from .._utils.resolve_path import resolve_path
Expand Down Expand Up @@ -41,22 +42,22 @@ def locate_file(
for abs_dir in find_sub_dirs_all_sitepackages(tuple(rel_dir.split("/"))):
abs_path = _resolved_if_file(abs_dir, filename)
if abs_path is not None:
return abs_path, "site-packages"
return abs_path, FOUND_VIA_SITE_PACKAGES

conda_prefix = os.environ.get("CONDA_PREFIX")
if conda_prefix:
anchor = Path(conda_prefix, "Library") if IS_WINDOWS else Path(conda_prefix)
for rel_path in conda_rel_paths:
abs_path = _resolved_if_file(anchor / rel_path, filename)
if abs_path is not None:
return abs_path, "conda"
return abs_path, FOUND_VIA_CONDA

cuda_home = get_cuda_path_or_home()
if cuda_home is not None:
for rel_path in ctk_rel_paths:
abs_path = _resolved_if_file(Path(cuda_home, rel_path), filename)
if abs_path is not None:
return abs_path, "CUDA_PATH"
return abs_path, FOUND_VIA_CUDA_PATH

raise not_found_error(_not_found_message(filename, cuda_home, ctk_rel_paths))

Expand Down
26 changes: 26 additions & 0 deletions cuda_pathfinder/cuda/pathfinder/_utils/found_via.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""The ``found_via`` labels: where a search found what it was looking for.

These strings are public. They appear in ``LoadedDL.found_via``,
``LocatedHeaderDir.found_via``, ``LocatedStaticLib.found_via`` and
``LocatedBitcodeLib.found_via``, and the Linux loader branches on two of them.
Spell them here and nowhere else.

A product root found through a product-specific environment variable is
labeled with that variable's name (for example ``"CUDNN_PATH"``), so it has no
constant here.
"""

from typing import Final

FOUND_VIA_SITE_PACKAGES: Final = "site-packages"
FOUND_VIA_CONDA: Final = "conda"
FOUND_VIA_SYSTEM_SEARCH: Final = "system-search"
#: Covers both the ``CUDA_PATH`` and the ``CUDA_HOME`` environment variable.
FOUND_VIA_CUDA_PATH: Final = "CUDA_PATH"
FOUND_VIA_PROGRAM_FILES: Final = "ProgramFiles"
FOUND_VIA_SYSTEM_CTK_ROOT: Final = "system-ctk-root"
FOUND_VIA_SUPPORTED_INSTALL_DIR: Final = "supported_install_dir"
FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE: Final = "was-already-loaded-from-elsewhere"
19 changes: 19 additions & 0 deletions cuda_pathfinder/tests/test_utils_found_via.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

import pytest

from cuda.pathfinder._utils import found_via


@pytest.mark.agent_authored(model="claude-fable-5-2")
def test_found_via_labels_keep_their_public_values():
# Callers compare ``found_via`` against these exact strings.
assert found_via.FOUND_VIA_SITE_PACKAGES == "site-packages"
assert found_via.FOUND_VIA_CONDA == "conda"
assert found_via.FOUND_VIA_SYSTEM_SEARCH == "system-search"
assert found_via.FOUND_VIA_CUDA_PATH == "CUDA_PATH"
assert found_via.FOUND_VIA_PROGRAM_FILES == "ProgramFiles"
assert found_via.FOUND_VIA_SYSTEM_CTK_ROOT == "system-ctk-root"
assert found_via.FOUND_VIA_SUPPORTED_INSTALL_DIR == "supported_install_dir"
assert found_via.FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE == "was-already-loaded-from-elsewhere"
Loading