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
44 changes: 23 additions & 21 deletions cuda_pathfinder/cuda/pathfinder/_static_libs/find_bitcode_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import functools
import os
from dataclasses import dataclass
from pathlib import Path
from typing import NoReturn, TypedDict

from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home
Expand Down Expand Up @@ -35,7 +36,7 @@ class _BitcodeLibInfo(TypedDict):
_SUPPORTED_BITCODE_LIBS_INFO: dict[str, _BitcodeLibInfo] = {
"device": {
"filename": "libdevice.10.bc",
"rel_path": os.path.join("nvvm", "libdevice"),
"rel_path": "nvvm/libdevice",
"site_packages_dirs": (
"nvidia/cu13/nvvm/libdevice",
"nvidia/cuda_nvcc/nvvm/libdevice",
Expand Down Expand Up @@ -64,14 +65,14 @@ class _BitcodeLibInfo(TypedDict):
)


def _no_such_file_in_dir(dir_path: str, filename: str, error_messages: list[str], attachments: list[str]) -> None:
error_messages.append(f"No such file: {os.path.join(dir_path, filename)}")
if os.path.isdir(dir_path):
attachments.append(f' listdir("{dir_path}"):')
for node in sorted(os.listdir(dir_path)):
def _no_such_file_in_dir(directory: Path, filename: str, error_messages: list[str], attachments: list[str]) -> None:
error_messages.append(f"No such file: {directory / filename}")
if directory.is_dir():
attachments.append(f' listdir("{directory}"):')
for node in sorted(node_path.name for node_path in directory.iterdir()):
attachments.append(f" {node}")
else:
attachments.append(f' Directory does not exist: "{dir_path}"')
attachments.append(f' Directory does not exist: "{directory}"')


class _FindBitcodeLib:
Expand All @@ -86,38 +87,39 @@ def __init__(self, name: str) -> None:
self.error_messages: list[str] = []
self.attachments: list[str] = []

def try_site_packages(self) -> str | None:
def try_site_packages(self) -> Path | None:
for rel_dir in self.site_packages_dirs:
sub_dir = tuple(rel_dir.split("/"))
for abs_dir in find_sub_dirs_all_sitepackages(sub_dir):
file_path = os.path.join(abs_dir, self.filename)
if os.path.isfile(file_path):
file_path = Path(abs_dir, self.filename)
if file_path.is_file():
return file_path
return None

def try_with_conda_prefix(self) -> str | None:
def try_with_conda_prefix(self) -> Path | None:
conda_prefix = os.environ.get("CONDA_PREFIX")
if not conda_prefix:
return None

anchor = os.path.join(conda_prefix, "Library") if IS_WINDOWS else conda_prefix
file_path = os.path.join(anchor, self.rel_path, self.filename)
if os.path.isfile(file_path):
anchor = Path(conda_prefix, "Library") if IS_WINDOWS else Path(conda_prefix)
file_path = anchor / self.rel_path / self.filename
if file_path.is_file():
return file_path
return None

def try_with_cuda_home(self) -> str | None:
def try_with_cuda_home(self) -> Path | None:
cuda_home = get_cuda_path_or_home()
if cuda_home is None:
self.error_messages.append("CUDA_HOME/CUDA_PATH not set")
return None

file_path = os.path.join(cuda_home, self.rel_path, self.filename)
if os.path.isfile(file_path):
anchor = Path(cuda_home)
file_path = anchor / self.rel_path / self.filename
if file_path.is_file():
return file_path

_no_such_file_in_dir(
os.path.join(cuda_home, self.rel_path),
anchor / self.rel_path,
self.filename,
self.error_messages,
self.attachments,
Expand All @@ -143,7 +145,7 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib:
if abs_path is not None:
return LocatedBitcodeLib(
name=name,
abs_path=abs_path,
abs_path=str(abs_path),
filename=finder.filename,
found_via="site-packages",
)
Expand All @@ -152,7 +154,7 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib:
if abs_path is not None:
return LocatedBitcodeLib(
name=name,
abs_path=abs_path,
abs_path=str(abs_path),
filename=finder.filename,
found_via="conda",
)
Expand All @@ -161,7 +163,7 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib:
if abs_path is not None:
return LocatedBitcodeLib(
name=name,
abs_path=abs_path,
abs_path=str(abs_path),
filename=finder.filename,
found_via="CUDA_PATH",
)
Expand Down
46 changes: 24 additions & 22 deletions cuda_pathfinder/cuda/pathfinder/_static_libs/find_static_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import functools
import os
from dataclasses import dataclass
from pathlib import Path
from typing import NoReturn, TypedDict

from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home
Expand Down Expand Up @@ -35,8 +36,8 @@ class _StaticLibInfo(TypedDict):
_SUPPORTED_STATIC_LIBS_INFO: dict[str, _StaticLibInfo] = {
"cudadevrt": {
"filename": "cudadevrt.lib" if IS_WINDOWS else "libcudadevrt.a",
"ctk_rel_paths": (os.path.join("lib", "x64"),) if IS_WINDOWS else ("lib64", "lib"),
"conda_rel_paths": ((os.path.join("lib", "x64"), "lib") if IS_WINDOWS else ("lib",)),
"ctk_rel_paths": ("lib/x64",) if IS_WINDOWS else ("lib64", "lib"),
"conda_rel_paths": (("lib/x64", "lib") if IS_WINDOWS else ("lib",)),
"site_packages_dirs": (
("nvidia/cu13/lib/x64", "nvidia/cuda_runtime/lib/x64")
if IS_WINDOWS
Expand All @@ -48,14 +49,14 @@ class _StaticLibInfo(TypedDict):
SUPPORTED_STATIC_LIBS: tuple[str, ...] = tuple(sorted(_SUPPORTED_STATIC_LIBS_INFO.keys()))


def _no_such_file_in_dir(dir_path: str, filename: str, error_messages: list[str], attachments: list[str]) -> None:
error_messages.append(f"No such file: {os.path.join(dir_path, filename)}")
if os.path.isdir(dir_path):
attachments.append(f' listdir("{dir_path}"):')
for node in sorted(os.listdir(dir_path)):
def _no_such_file_in_dir(directory: Path, filename: str, error_messages: list[str], attachments: list[str]) -> None:
error_messages.append(f"No such file: {directory / filename}")
if directory.is_dir():
attachments.append(f' listdir("{directory}"):')
for node in sorted(node_path.name for node_path in directory.iterdir()):
attachments.append(f" {node}")
else:
attachments.append(f' Directory does not exist: "{dir_path}"')
attachments.append(f' Directory does not exist: "{directory}"')


class _FindStaticLib:
Expand All @@ -71,40 +72,41 @@ def __init__(self, name: str) -> None:
self.error_messages: list[str] = []
self.attachments: list[str] = []

def try_site_packages(self) -> str | None:
def try_site_packages(self) -> Path | None:
for rel_dir in self.site_packages_dirs:
sub_dir = tuple(rel_dir.split("/"))
for abs_dir in find_sub_dirs_all_sitepackages(sub_dir):
file_path = os.path.join(abs_dir, self.filename)
if os.path.isfile(file_path):
file_path = Path(abs_dir, self.filename)
if file_path.is_file():
return file_path
return None

def try_with_conda_prefix(self) -> str | None:
def try_with_conda_prefix(self) -> Path | None:
conda_prefix = os.environ.get("CONDA_PREFIX")
if not conda_prefix:
return None

anchor = os.path.join(conda_prefix, "Library") if IS_WINDOWS else conda_prefix
anchor = Path(conda_prefix, "Library") if IS_WINDOWS else Path(conda_prefix)
for rel_path in self.conda_rel_paths:
file_path = os.path.join(anchor, rel_path, self.filename)
if os.path.isfile(file_path):
file_path = anchor / rel_path / self.filename
if file_path.is_file():
return file_path
return None

def try_with_cuda_home(self) -> str | None:
def try_with_cuda_home(self) -> Path | None:
cuda_home = get_cuda_path_or_home()
if cuda_home is None:
self.error_messages.append("CUDA_HOME/CUDA_PATH not set")
return None

anchor = Path(cuda_home)
for rel_path in self.ctk_rel_paths:
file_path = os.path.join(cuda_home, rel_path, self.filename)
if os.path.isfile(file_path):
file_path = anchor / rel_path / self.filename
if file_path.is_file():
return file_path

_no_such_file_in_dir(
os.path.join(cuda_home, self.ctk_rel_paths[0]),
anchor / self.ctk_rel_paths[0],
self.filename,
self.error_messages,
self.attachments,
Expand All @@ -130,7 +132,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
if abs_path is not None:
return LocatedStaticLib(
name=name,
abs_path=abs_path,
abs_path=str(abs_path),
filename=finder.filename,
found_via="site-packages",
)
Expand All @@ -139,7 +141,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
if abs_path is not None:
return LocatedStaticLib(
name=name,
abs_path=abs_path,
abs_path=str(abs_path),
filename=finder.filename,
found_via="conda",
)
Expand All @@ -148,7 +150,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
if abs_path is not None:
return LocatedStaticLib(
name=name,
abs_path=abs_path,
abs_path=str(abs_path),
filename=finder.filename,
found_via="CUDA_PATH",
)
Expand Down
Loading