2022-08-01 00:47:44 +00:00
|
|
|
import os
|
2022-08-01 10:31:48 +00:00
|
|
|
import pytest
|
2022-08-02 02:22:41 +00:00
|
|
|
import bitsandbytes as bnb
|
|
|
|
|
|
|
|
from typing import List, NamedTuple
|
2022-07-28 04:16:04 +00:00
|
|
|
|
2022-08-01 16:32:47 +00:00
|
|
|
from bitsandbytes.cuda_setup import (
|
|
|
|
CUDA_RUNTIME_LIB,
|
|
|
|
evaluate_cuda_setup,
|
2022-08-03 04:26:50 +00:00
|
|
|
determine_cuda_runtime_lib_path,
|
|
|
|
extract_candidate_paths,
|
2022-08-01 16:32:47 +00:00
|
|
|
)
|
2022-07-28 04:16:04 +00:00
|
|
|
|
2022-08-03 04:26:50 +00:00
|
|
|
"""
|
|
|
|
'LD_LIBRARY_PATH': ':/mnt/D/titus/local/cuda-11.1/lib64/'
|
|
|
|
'CONDA_EXE': '/mnt/D/titus/miniconda/bin/conda'
|
|
|
|
'LESSCLOSE': '/usr/bin/lesspipe %s %s'
|
|
|
|
'OLDPWD': '/mnt/D/titus/src'
|
|
|
|
'CONDA_PREFIX': '/mnt/D/titus/miniconda/envs/8-bit'
|
|
|
|
'SSH_AUTH_SOCK': '/mnt/D/titus/.ssh/ssh-agent.tim-uw.sock'
|
|
|
|
'CONDA_PREFIX_1': '/mnt/D/titus/miniconda'
|
|
|
|
'PWD': '/mnt/D/titus/src/8-bit'
|
|
|
|
'HOME': '/mnt/D/titus'
|
|
|
|
'CONDA_PYTHON_EXE': '/mnt/D/titus/miniconda/bin/python'
|
|
|
|
'CUDA_HOME': '/mnt/D/titus/local/cuda-11.1/'
|
|
|
|
'TMUX': '/tmp/tmux-1007/default,59286,1'
|
|
|
|
'XDG_DATA_DIRS': '/usr/local/share:/usr/share:/var/lib/snapd/desktop'
|
|
|
|
'SSH_TTY': '/dev/pts/0'
|
|
|
|
'MAIL': '/var/mail/titus'
|
|
|
|
'SHELL': '/bin/bash'
|
|
|
|
'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1007/bus'
|
|
|
|
'XDG_RUNTIME_DIR': '/run/user/1007'
|
|
|
|
'PATH': '/mnt/D/titus/miniconda/envs/8-bit/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/mnt/D/titus/local/cuda-11.1/bin'
|
|
|
|
'LESSOPEN': '| /usr/bin/lesspipe %s'
|
|
|
|
'_': '/mnt/D/titus/miniconda/envs/8-bit/bin/python'
|
|
|
|
# any that include 'CONDA' that are not 'CONDA_PREFIX'
|
|
|
|
|
|
|
|
# we search for
|
|
|
|
'CUDA_HOME': '/mnt/D/titus/local/cuda-11.1/'
|
|
|
|
"""
|
|
|
|
|
2022-07-28 04:16:04 +00:00
|
|
|
|
2022-08-01 10:31:48 +00:00
|
|
|
class InputAndExpectedOutput(NamedTuple):
|
|
|
|
input: str
|
|
|
|
output: str
|
2022-07-28 04:16:04 +00:00
|
|
|
|
2022-08-01 10:31:48 +00:00
|
|
|
|
|
|
|
HAPPY_PATH__LD_LIB_TEST_PATHS: List[InputAndExpectedOutput] = [
|
2022-08-01 16:32:47 +00:00
|
|
|
(
|
|
|
|
f"some/other/dir:dir/with/{CUDA_RUNTIME_LIB}",
|
|
|
|
f"dir/with/{CUDA_RUNTIME_LIB}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
f":some/other/dir:dir/with/{CUDA_RUNTIME_LIB}",
|
|
|
|
f"dir/with/{CUDA_RUNTIME_LIB}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
f"some/other/dir:dir/with/{CUDA_RUNTIME_LIB}:",
|
|
|
|
f"dir/with/{CUDA_RUNTIME_LIB}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
f"some/other/dir::dir/with/{CUDA_RUNTIME_LIB}",
|
|
|
|
f"dir/with/{CUDA_RUNTIME_LIB}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
f"dir/with/{CUDA_RUNTIME_LIB}:some/other/dir",
|
|
|
|
f"dir/with/{CUDA_RUNTIME_LIB}",
|
|
|
|
),
|
2022-08-01 10:31:48 +00:00
|
|
|
(
|
|
|
|
f"dir/with/{CUDA_RUNTIME_LIB}:other/dir/libcuda.so",
|
|
|
|
f"dir/with/{CUDA_RUNTIME_LIB}",
|
|
|
|
),
|
2022-07-28 04:16:04 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-08-01 10:31:48 +00:00
|
|
|
@pytest.fixture(params=HAPPY_PATH__LD_LIB_TEST_PATHS)
|
|
|
|
def happy_path_path_string(tmpdir, request):
|
2022-08-03 04:26:50 +00:00
|
|
|
for path in extract_candidate_paths(request.param):
|
2022-08-01 10:31:48 +00:00
|
|
|
test_dir.mkdir()
|
|
|
|
if CUDA_RUNTIME_LIB in path:
|
|
|
|
(test_input / CUDA_RUNTIME_LIB).touch()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("test_input, expected", HAPPY_PATH__LD_LIB_TEST_PATHS)
|
2022-08-03 04:26:50 +00:00
|
|
|
def test_determine_cuda_runtime_lib_path__happy_path(
|
2022-08-01 10:31:48 +00:00
|
|
|
tmp_path, test_input: str, expected: str
|
2022-07-28 04:16:04 +00:00
|
|
|
):
|
2022-08-03 04:26:50 +00:00
|
|
|
for path in extract_candidate_paths(test_input):
|
2022-08-01 10:31:48 +00:00
|
|
|
path.mkdir()
|
|
|
|
(path / CUDA_RUNTIME_LIB).touch()
|
2022-08-03 04:26:50 +00:00
|
|
|
assert determine_cuda_runtime_lib_path(test_input) == expected
|
2022-07-28 04:16:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
UNHAPPY_PATH__LD_LIB_TEST_PATHS = [
|
|
|
|
f"a/b/c/{CUDA_RUNTIME_LIB}:d/e/f/{CUDA_RUNTIME_LIB}",
|
|
|
|
f"a/b/c/{CUDA_RUNTIME_LIB}:d/e/f/{CUDA_RUNTIME_LIB}:g/h/j/{CUDA_RUNTIME_LIB}",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("test_input", UNHAPPY_PATH__LD_LIB_TEST_PATHS)
|
2022-08-03 04:26:50 +00:00
|
|
|
def test_determine_cuda_runtime_lib_path__unhappy_path(tmp_path, test_input: str):
|
2022-07-28 04:16:04 +00:00
|
|
|
test_input = tmp_path / test_input
|
|
|
|
(test_input / CUDA_RUNTIME_LIB).touch()
|
|
|
|
with pytest.raises(FileNotFoundError) as err_info:
|
2022-08-03 04:26:50 +00:00
|
|
|
determine_cuda_runtime_lib_path(test_input)
|
2022-08-01 10:31:48 +00:00
|
|
|
assert all(match in err_info for match in {"duplicate", CUDA_RUNTIME_LIB})
|
2022-07-28 04:16:04 +00:00
|
|
|
|
|
|
|
|
2022-08-03 04:26:50 +00:00
|
|
|
def test_determine_cuda_runtime_lib_path__non_existent_dir(capsys, tmp_path):
|
2022-08-01 10:31:48 +00:00
|
|
|
existent_dir = tmp_path / "a/b"
|
2022-07-28 04:16:04 +00:00
|
|
|
existent_dir.mkdir()
|
2022-08-01 10:31:48 +00:00
|
|
|
non_existent_dir = tmp_path / "c/d" # non-existent dir
|
2022-07-28 04:16:04 +00:00
|
|
|
test_input = ":".join([str(existent_dir), str(non_existent_dir)])
|
|
|
|
|
2022-08-03 04:26:50 +00:00
|
|
|
determine_cuda_runtime_lib_path(test_input)
|
2022-07-28 04:16:04 +00:00
|
|
|
std_err = capsys.readouterr().err
|
|
|
|
|
2022-08-01 10:31:48 +00:00
|
|
|
assert all(match in std_err for match in {"WARNING", "non-existent"})
|
|
|
|
|
2022-08-01 00:47:44 +00:00
|
|
|
|
|
|
|
def test_full_system():
|
|
|
|
## this only tests the cuda version and not compute capability
|
2022-08-02 02:43:09 +00:00
|
|
|
|
|
|
|
# if CONDA_PREFIX exists, it has priority before all other env variables
|
|
|
|
# but it does not contain the library directly, so we need to look at the a sub-folder
|
2022-08-03 04:26:50 +00:00
|
|
|
version = ""
|
|
|
|
if "CONDA_PREFIX" in os.environ:
|
|
|
|
ls_output, err = bnb.utils.execute_and_return(
|
|
|
|
f'ls -l {os.environ["CONDA_PREFIX"]}/lib/libcudart.so'
|
|
|
|
)
|
|
|
|
major, minor, revision = (
|
|
|
|
ls_output.split(" ")[-1].replace("libcudart.so.", "").split(".")
|
|
|
|
)
|
|
|
|
version = float(f"{major}.{minor}")
|
|
|
|
|
|
|
|
if version == "" and "LD_LIBRARY_PATH":
|
2022-08-02 02:22:41 +00:00
|
|
|
ld_path = os.environ["LD_LIBRARY_PATH"]
|
|
|
|
paths = ld_path.split(":")
|
|
|
|
version = ""
|
|
|
|
for p in paths:
|
|
|
|
if "cuda" in p:
|
|
|
|
idx = p.rfind("cuda-")
|
|
|
|
version = p[idx + 5 : idx + 5 + 4].replace("/", "")
|
|
|
|
version = float(version)
|
|
|
|
break
|
|
|
|
|
|
|
|
assert version > 0
|
2022-08-01 00:47:44 +00:00
|
|
|
binary_name = evaluate_cuda_setup()
|
2022-08-01 10:31:48 +00:00
|
|
|
binary_name = binary_name.replace("libbitsandbytes_cuda", "")
|
|
|
|
assert binary_name.startswith(str(version).replace(".", ""))
|