Skip to content

Commit 18b3187

Browse files
committed
fix tests
1 parent 3225ea1 commit 18b3187

2 files changed

Lines changed: 35 additions & 23 deletions

File tree

python/tvm_ffi/_optional_torch_c_dlpack.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,26 @@ def load_torch_c_dlpack_extension() -> Any:
6060

6161
# check whether a JIT shared library is built in cache
6262
cache_dir = Path(os.environ.get("TVM_FFI_CACHE_DIR", "~/.cache/tvm-ffi")).expanduser()
63-
addon_build_dir = cache_dir / "torch_c_dlpack_addon"
64-
lib_path = addon_build_dir / (
65-
"libtorch_c_dlpack_addon" + (".dll" if sys.platform == "win32" else ".so")
66-
)
63+
addon_output_dir = cache_dir
64+
major, minor = torch.__version__.split(".")[:2]
65+
device = "cpu" if not torch.cuda.is_available() else "cuda"
66+
suffix = ".dll" if sys.platform.startswith("win") else ".so"
67+
libname = f"libtorch_c_dlpack_addon_torch{major}{minor}-{device}{suffix}"
68+
lib_path = addon_output_dir / libname
6769
if not lib_path.exists():
6870
build_script_path = (
6971
Path(__file__).parent / "utils" / "_build_optional_torch_c_dlpack.py"
7072
)
71-
args = [sys.executable, str(build_script_path), "--build_dir", str(addon_build_dir)]
73+
args = [
74+
sys.executable,
75+
str(build_script_path),
76+
"--output-dir",
77+
str(cache_dir),
78+
"--libname",
79+
libname,
80+
]
7281
if torch.cuda.is_available():
73-
args.append("--build_with_cuda")
82+
args.append("--build-with-cuda")
7483
subprocess.run(
7584
args,
7685
check=True,

tests/python/test_optional_torch_c_dlpack.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@
3636
@pytest.mark.skipif(torch is None, reason="torch is not installed")
3737
def test_build_torch_c_dlpack_extension() -> None:
3838
build_script = Path(tvm_ffi.__file__).parent / "utils" / "_build_optional_torch_c_dlpack.py"
39-
subprocess.run(
40-
[sys.executable, str(build_script), "--build_dir", "./build_test_dir"], check=True
41-
)
39+
args = [
40+
sys.executable,
41+
str(build_script),
42+
"--output-dir",
43+
"./output-dir",
44+
"--libname",
45+
"libtorch_c_dlpack_addon_test.so",
46+
]
47+
if torch.cuda.is_available():
48+
args.append("--build-with-cuda")
49+
subprocess.run(args, check=True)
4250

43-
lib_path = str(
44-
Path(
45-
"./build_test_dir/libtorch_c_dlpack_addon.{}".format("dll" if IS_WINDOWS else "so")
46-
).resolve()
47-
)
51+
lib_path = str(Path("./output-dir/libtorch_c_dlpack_addon_test.so").resolve())
4852
assert Path(lib_path).exists()
4953

5054
lib = ctypes.CDLL(lib_path)
@@ -58,20 +62,19 @@ def test_build_torch_c_dlpack_extension() -> None:
5862
def test_parallel_build() -> None:
5963
build_script = Path(tvm_ffi.__file__).parent / "utils" / "_build_optional_torch_c_dlpack.py"
6064
num_processes = 4
61-
build_dir = "./build_test_dir_parallel"
65+
output_dir = "./output-dir-parallel"
66+
libname = "libtorch_c_dlpack_addon_test.so"
6267
processes = []
6368
for i in range(num_processes):
64-
p = subprocess.Popen([sys.executable, str(build_script), "--build_dir", build_dir])
65-
processes.append((p, build_dir))
69+
p = subprocess.Popen(
70+
[sys.executable, str(build_script), "--output-dir", output_dir, "--libname", libname]
71+
)
72+
processes.append((p, output_dir))
6673

67-
for p, build_dir in processes:
74+
for p, output_dir in processes:
6875
p.wait()
6976
assert p.returncode == 0
70-
lib_path = str(
71-
Path(
72-
"{}/libtorch_c_dlpack_addon.{}".format(build_dir, "dll" if IS_WINDOWS else "so")
73-
).resolve()
74-
)
77+
lib_path = str(Path(f"{output_dir}/{libname}").resolve())
7578
assert Path(lib_path).exists()
7679

7780

0 commit comments

Comments
 (0)