|
| 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() |
0 commit comments