8000 Make python_agnostic cpp extension tests standalone · pytorch/pytorch@b72c752 · GitHub
[go: up one dir, main page]

Skip to content

Commit b72c752

Browse files
committed
Make python_agnostic cpp extension tests standalone
ghstack-source-id: a2a474a Pull Request resolved: #153274
1 parent ab61a87 commit b72c752

File tree

2 files changed

+65
-32
lines changed

2 files changed

+65
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Owner(s): ["module: cpp"]
2+
3+
import os
4+
import shutil
5+
import subprocess
6+
import sys
7+
import unittest
8+
from pathlib import Path
9+
10+
from torch.testing._internal.common_device_type import (
11+
instantiate_device_type_tests,
12+
onlyCUDA,
13+
)
14+
from torch.testing._internal.common_utils import IS_LINUX, run_tests, shell, TestCase
15+
16+
17+
class TestPythonAgnostic(TestCase):
18+
@classmethod
19+
def setUpClass(cls):
20+
# Wipe the dist dir if it exists
21+
cls.extension_root = Path(__file__).parent.parent
22+
cls.dist_dir = os.path.join(cls.extension_root, "dist")
23+
if os.path.exists(cls.dist_dir):
24+
shutil.rmtree(cls.dist_dir)
25+
26+
# Build the wheel
27+
wheel_cmd = [sys.executable, "setup.py", "bdist_wheel"]
28+
return_code = shell(wheel_cmd, cwd=cls.extension_root, env=os.environ)
29+
if return_code != 0:
30+
raise RuntimeError("python_agnostic bdist_wheel failed to build")
31+
32+
@onlyCUDA
33+
@unittest.skipIf(not IS_LINUX, "test requires linux tools ldd and nm")
34+
def test_extension_is_python_agnostic(self, device):
35+
# For this test, run_test.py will call `python setup.py bdist_wheel` in the
36+
# cpp_extensions/python_agnostic_extension folder, where the extension and
37+
# setup calls specify py_limited_api to `True`. To approximate that the
38+
# extension is indeed python agnostic, we test
39+
# a. The extension wheel name contains "cp39-abi3", meaning the wheel
40+
# should be runnable for any Python 3 version after and including 3.9
41+
# b. The produced shared library does not have libtorch_python.so as a
42+
# dependency from the output of "ldd _C.so"
43+
# c. The .so does not need any python related symbols. We approximate
44+
# this by running "nm -u _C.so" and grepping that nothing starts with "Py"
45+
46+
matches = list(Path(self.dist_dir).glob("*.whl"))
47+
self.assertEqual(len(matches), 1, msg=str(matches))
48+
whl_file = matches[0]
49+
self.assertRegex(str(whl_file), r".*python_agnostic-0\.0-cp39-abi3-.*\.whl")
50+
51+
build_dir = os.path.join(self.extension_root, "build")
52+
matches = list(Path(build_dir).glob("**/*.so"))
53+
self.assertEqual(len(matches), 1, msg=str(matches))
54+
so_file = matches[0]
55+
lddtree = subprocess.check_output(["ldd", so_file]).decode("utf-8")
56+
self.assertFalse("torch_python" in lddtree)
57+
58+
missing_symbols = subprocess.check_output(["nm", "-u", so_file]).decode("utf-8")
59+
self.assertFalse("Py" in missing_symbols)
60+
61+
62+
instantiate_device_type_tests(TestPythonAgnostic, globals())
63+
64+
if __name__ == "__main__":
65+
run_tests()

test/test_cpp_extensions_aot.py

-32
Original file line numberDiff line numberDiff line change
@@ -183,38 +183,6 @@ def test_cuda_dlink_libs(self):
183183
test = cuda_dlink.add(a, b)
184184
self.assertEqual(test, ref)
185185

186-
@unittest.skipIf(not TEST_CUDA, "python_agnostic is a CUDA extension + needs CUDA")
187-
@unittest.skipIf(not common.IS_LINUX, "test requires linux tools ldd and nm")
188-
def test_python_agnostic(self):
189-
# For this test, run_test.py will call `python setup.py bdist_wheel` in the
190-
# cpp_extensions/python_agnostic_extension folder, where the extension and
191-
# setup calls specify py_limited_api to `True`. To approximate that the
192-
# extension is indeed python agnostic, we test
193-
# a. The extension wheel name contains "cp39-abi3", meaning the wheel
194-
# should be runnable for any Python 3 version after and including 3.9
195-
# b. The produced shared library does not have libtorch_python.so as a
196-
# dependency from the output of "ldd _C.so"
197-
# c. The .so does not need any python related symbols. We approximate
198-
# this by running "nm -u _C.so" and grepping that nothing starts with "Py"
199-
200-
dist_root = os.path.join("cpp_extensions", "python_agnostic_extension", "dist")
201-
matches = list(Path(dist_root).glob("*.whl"))
202-
self.assertEqual(len(matches), 1, msg=str(matches))
203-
whl_file = matches[0]
204-
self.assertRegex(str(whl_file), r".*python_agnostic-0\.0-cp39-abi3-.*\.whl")
205-
206-
build_root = os.path.join(
207-
"cpp_extensions", "python_agnostic_extension", "build"
208-
)
209-
matches = list(Path(build_root).glob("**/*.so"))
210-
self.assertEqual(len(matches), 1, msg=str(matches))
211-
so_file = matches[0]
212-
lddtree = subprocess.check_output(["ldd", so_file]).decode("utf-8")
213-
self.assertFalse("torch_python" in lddtree)
214-
215-
missing_symbols = subprocess.check_output(["nm", "-u", so_file]).decode("utf-8")
216-
self.assertFalse("Py" in missing_symbols)
217-
218186

219187
@torch.testing._internal.common_utils.markDynamoStrictTest
220188
class TestPybindTypeCasters(common.TestCase):

0 commit comments

Comments
 (0)
0