From e202d1911e8ce4bf0d0fedfc84105acf1acbb4b0 Mon Sep 17 00:00:00 2001 From: apbose Date: Mon, 21 Apr 2025 20:58:20 -0700 Subject: [PATCH 1/7] cross compile guard for windows --- py/torch_tensorrt/_features.py | 30 +++++++++++++++++-- py/torch_tensorrt/_utils.py | 28 +++++++++++++++++ .../test_003_cross_compile_for_windows.py | 7 +++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/py/torch_tensorrt/_features.py b/py/torch_tensorrt/_features.py index bee0c3dbf0..9603a0a14c 100644 --- a/py/torch_tensorrt/_features.py +++ b/py/torch_tensorrt/_features.py @@ -3,7 +3,10 @@ from collections import namedtuple from typing import Any, Callable, Dict, List, Optional, Type, TypeVar -from torch_tensorrt._utils import sanitized_torch_version +from torch_tensorrt._utils import ( + check_cross_compile_trt_win_lib, + sanitized_torch_version, +) from packaging import version @@ -15,6 +18,7 @@ "dynamo_frontend", "fx_frontend", "refit", + "windows_cross_compile", ], ) @@ -38,9 +42,15 @@ _DYNAMO_FE_AVAIL = version.parse(sanitized_torch_version()) >= version.parse("2.1.dev") _FX_FE_AVAIL = True _REFIT_AVAIL = True +_WINDOWS_CROSS_COMPILE = check_cross_compile_trt_win_lib ENABLED_FEATURES = FeatureSet( - _TS_FE_AVAIL, _TORCHTRT_RT_AVAIL, _DYNAMO_FE_AVAIL, _FX_FE_AVAIL, _REFIT_AVAIL + _TS_FE_AVAIL, + _TORCHTRT_RT_AVAIL, + _DYNAMO_FE_AVAIL, + _FX_FE_AVAIL, + _REFIT_AVAIL, + _WINDOWS_CROSS_COMPILE, ) @@ -80,6 +90,22 @@ def not_implemented(*args: List[Any], **kwargs: Dict[str, Any]) -> Any: return wrapper +def needs_cross_compile(f: Callable[..., Any]) -> Callable[..., Any]: + def wrapper(*args: List[Any], **kwargs: Dict[str, Any]) -> Any: + if ENABLED_FEATURES.windows_cross_compile: + return f(*args, **kwargs) + else: + + def not_implemented(*args: List[Any], **kwargs: Dict[str, Any]) -> Any: + raise NotImplementedError( + "Windows cross compilation feature is not available" + ) + + return not_implemented(*args, **kwargs) + + return wrapper + + T = TypeVar("T") diff --git a/py/torch_tensorrt/_utils.py b/py/torch_tensorrt/_utils.py index 3d5f98b5e5..09c1fba994 100644 --- a/py/torch_tensorrt/_utils.py +++ b/py/torch_tensorrt/_utils.py @@ -1,6 +1,10 @@ +import ctypes +import platform +import sys from typing import Any import torch +from torch_tensorrt import _find_lib def sanitized_torch_version() -> Any: @@ -9,3 +13,27 @@ def sanitized_torch_version() -> Any: if ".nv" not in torch.__version__ else torch.__version__.split(".nv")[0] ) + + +def check_cross_compile_trt_win_lib(lib_name): + if sys.platform.startswith("linux"): + LINUX_PATHS = ["/usr/local/cuda-12.8/lib64", "/usr/lib", "/usr/lib64"] + + if platform.uname().processor == "x86_64": + LINUX_PATHS += [ + "/usr/lib/x86_64-linux-gnu", + ] + elif platform.uname().processor == "aarch64": + LINUX_PATHS += ["/usr/lib/aarch64-linux-gnu"] + + LINUX_LIBS = [ + f"libnvinfer_builder_resource_win.so.*", + ] + + for lib in LINUX_LIBS: + try: + ctypes.CDLL(_find_lib(lib, LINUX_PATHS)) + return True + except: + continue + return False diff --git a/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py b/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py index 2816edf7d4..f347c04c96 100644 --- a/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py +++ b/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py @@ -8,6 +8,7 @@ import torch_tensorrt from torch.testing._internal.common_utils import TestCase from torch_tensorrt.dynamo.utils import get_model_device +from torch_tensorrt._utils import check_cross_compile_trt_win_lib from ..testing_utilities import DECIMALS_OF_AGREEMENT @@ -17,6 +18,9 @@ class TestCrossCompileSaveForWindows(TestCase): platform.system() != "Linux" or platform.architecture()[0] != "64bit", "Cross compile for windows can only be enabled on linux x86-64 platform", ) + @unittest.skipIf( + not (check_cross_compile_trt_win_lib()), + ) @pytest.mark.unit def test_cross_compile_for_windows(self): class Add(torch.nn.Module): @@ -41,6 +45,9 @@ def forward(self, a, b): platform.system() != "Linux" or platform.architecture()[0] != "64bit", "Cross compile for windows can only be enabled on linux x86-64 platform", ) + @unittest.skipIf( + not (check_cross_compile_trt_win_lib()), + ) @pytest.mark.unit def test_dynamo_cross_compile_for_windows(self): class Add(torch.nn.Module): From 9584d1f0a9d0ec673f4440112017942905609491 Mon Sep 17 00:00:00 2001 From: apbose Date: Tue, 22 Apr 2025 11:13:32 -0700 Subject: [PATCH 2/7] adding decorator for cross compile flag --- py/torch_tensorrt/_compile.py | 4 +++- py/torch_tensorrt/dynamo/_compiler.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/py/torch_tensorrt/_compile.py b/py/torch_tensorrt/_compile.py index 7ba704274e..e4e3710675 100644 --- a/py/torch_tensorrt/_compile.py +++ b/py/torch_tensorrt/_compile.py @@ -9,7 +9,7 @@ import torch import torch.fx from torch_tensorrt._enums import dtype -from torch_tensorrt._features import ENABLED_FEATURES +from torch_tensorrt._features import ENABLED_FEATURES, needs_cross_compile from torch_tensorrt._Input import Input from torch_tensorrt.dynamo import _defaults from torch_tensorrt.dynamo.runtime._CudaGraphsTorchTensorRTModule import ( @@ -301,6 +301,7 @@ def compile( raise RuntimeError("Module is an unknown format or the ir requested is unknown") +@needs_cross_compile def cross_compile_for_windows( module: torch.nn.Module, file_path: str, @@ -525,6 +526,7 @@ def convert_method_to_trt_engine( raise RuntimeError("Module is an unknown format or the ir requested is unknown") +@needs_cross_compile def load_cross_compiled_exported_program(file_path: str = "") -> Any: """ Load an ExportedProgram file in Windows which was previously cross compiled in Linux diff --git a/py/torch_tensorrt/dynamo/_compiler.py b/py/torch_tensorrt/dynamo/_compiler.py index 831ce37305..0bcfb0fcf2 100644 --- a/py/torch_tensorrt/dynamo/_compiler.py +++ b/py/torch_tensorrt/dynamo/_compiler.py @@ -11,6 +11,7 @@ from torch.fx.node import Target from torch_tensorrt._Device import Device from torch_tensorrt._enums import EngineCapability, dtype +from torch_tensorrt._features import needs_cross_compile from torch_tensorrt._Input import Input from torch_tensorrt.dynamo import _defaults, partitioning from torch_tensorrt.dynamo._DryRunTracker import ( @@ -50,6 +51,7 @@ logger = logging.getLogger(__name__) +@needs_cross_compile def cross_compile_for_windows( exported_program: ExportedProgram, inputs: Optional[Sequence[Sequence[Any]]] = None, @@ -1223,6 +1225,7 @@ def convert_exported_program_to_serialized_trt_engine( return serialized_engine +@needs_cross_compile def save_cross_compiled_exported_program( gm: torch.fx.GraphModule, file_path: str, @@ -1244,6 +1247,7 @@ def save_cross_compiled_exported_program( logger.debug(f"successfully saved the module for windows at {file_path}") +@needs_cross_compile def load_cross_compiled_exported_program(file_path: str = "") -> Any: """ Load an ExportedProgram file in Windows which was previously cross compiled in Linux From 0f045b1c36e7b7416fdbb129250ee7752bc33baa Mon Sep 17 00:00:00 2001 From: apbose Date: Tue, 22 Apr 2025 12:08:06 -0700 Subject: [PATCH 3/7] correcting CI failures --- py/torch_tensorrt/_features.py | 5 +++++ py/torch_tensorrt/_utils.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/py/torch_tensorrt/_features.py b/py/torch_tensorrt/_features.py index 9603a0a14c..bcffc1e9bb 100644 --- a/py/torch_tensorrt/_features.py +++ b/py/torch_tensorrt/_features.py @@ -41,8 +41,13 @@ _TORCHTRT_RT_AVAIL = _TS_FE_AVAIL or os.path.isfile(linked_file_runtime_full_path) _DYNAMO_FE_AVAIL = version.parse(sanitized_torch_version()) >= version.parse("2.1.dev") _FX_FE_AVAIL = True +<<<<<<< HEAD _REFIT_AVAIL = True _WINDOWS_CROSS_COMPILE = check_cross_compile_trt_win_lib +======= +_REFIT_AVAIL = version.parse(sys.version.split()[0]) < version.parse("3.13") +_WINDOWS_CROSS_COMPILE = check_cross_compile_trt_win_lib() +>>>>>>> 0e180c5f1 (correcting CI failures) ENABLED_FEATURES = FeatureSet( _TS_FE_AVAIL, diff --git a/py/torch_tensorrt/_utils.py b/py/torch_tensorrt/_utils.py index 09c1fba994..57ca43e43a 100644 --- a/py/torch_tensorrt/_utils.py +++ b/py/torch_tensorrt/_utils.py @@ -15,7 +15,7 @@ def sanitized_torch_version() -> Any: ) -def check_cross_compile_trt_win_lib(lib_name): +def check_cross_compile_trt_win_lib(): if sys.platform.startswith("linux"): LINUX_PATHS = ["/usr/local/cuda-12.8/lib64", "/usr/lib", "/usr/lib64"] From 09c81b585c7f8774272ecc243904830dabdc76b6 Mon Sep 17 00:00:00 2001 From: apbose Date: Fri, 25 Apr 2025 11:44:24 -0700 Subject: [PATCH 4/7] CI error for not stating reason in unittest skip --- py/torch_tensorrt/_features.py | 5 ----- .../py/dynamo/runtime/test_003_cross_compile_for_windows.py | 6 +++++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/py/torch_tensorrt/_features.py b/py/torch_tensorrt/_features.py index bcffc1e9bb..3beccec6af 100644 --- a/py/torch_tensorrt/_features.py +++ b/py/torch_tensorrt/_features.py @@ -41,13 +41,8 @@ _TORCHTRT_RT_AVAIL = _TS_FE_AVAIL or os.path.isfile(linked_file_runtime_full_path) _DYNAMO_FE_AVAIL = version.parse(sanitized_torch_version()) >= version.parse("2.1.dev") _FX_FE_AVAIL = True -<<<<<<< HEAD _REFIT_AVAIL = True -_WINDOWS_CROSS_COMPILE = check_cross_compile_trt_win_lib -======= -_REFIT_AVAIL = version.parse(sys.version.split()[0]) < version.parse("3.13") _WINDOWS_CROSS_COMPILE = check_cross_compile_trt_win_lib() ->>>>>>> 0e180c5f1 (correcting CI failures) ENABLED_FEATURES = FeatureSet( _TS_FE_AVAIL, diff --git a/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py b/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py index f347c04c96..a721da1baa 100644 --- a/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py +++ b/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py @@ -20,6 +20,7 @@ class TestCrossCompileSaveForWindows(TestCase): ) @unittest.skipIf( not (check_cross_compile_trt_win_lib()), + "TRT windows lib for cross compile not found", ) @pytest.mark.unit def test_cross_compile_for_windows(self): @@ -46,7 +47,10 @@ def forward(self, a, b): "Cross compile for windows can only be enabled on linux x86-64 platform", ) @unittest.skipIf( - not (check_cross_compile_trt_win_lib()), + not ( + check_cross_compile_trt_win_lib(), + "TRT windows lib for cross compile not found", + ), ) @pytest.mark.unit def test_dynamo_cross_compile_for_windows(self): From b43198a17390ac420f7cb5e117c75f73a69de4d8 Mon Sep 17 00:00:00 2001 From: apbose Date: Thu, 8 May 2025 10:26:05 -0700 Subject: [PATCH 5/7] Use dllist on linux to check cross compile feature --- py/requirements.txt | 3 ++- py/torch_tensorrt/_utils.py | 26 +++++-------------- pyproject.toml | 2 ++ .../test_003_cross_compile_for_windows.py | 10 ++++--- 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/py/requirements.txt b/py/requirements.txt index dbb342dae5..00cb832331 100644 --- a/py/requirements.txt +++ b/py/requirements.txt @@ -5,4 +5,5 @@ pybind11==2.6.2 torch>=2.8.0.dev,<2.9.0 torchvision>=0.22.0.dev,<0.23.0 --extra-index-url https://pypi.ngc.nvidia.com -pyyaml \ No newline at end of file +pyyaml +dllist \ No newline at end of file diff --git a/py/torch_tensorrt/_utils.py b/py/torch_tensorrt/_utils.py index 57ca43e43a..2ef6eac17c 100644 --- a/py/torch_tensorrt/_utils.py +++ b/py/torch_tensorrt/_utils.py @@ -4,7 +4,6 @@ from typing import Any import torch -from torch_tensorrt import _find_lib def sanitized_torch_version() -> Any: @@ -16,24 +15,13 @@ def sanitized_torch_version() -> Any: def check_cross_compile_trt_win_lib(): - if sys.platform.startswith("linux"): - LINUX_PATHS = ["/usr/local/cuda-12.8/lib64", "/usr/lib", "/usr/lib64"] - - if platform.uname().processor == "x86_64": - LINUX_PATHS += [ - "/usr/lib/x86_64-linux-gnu", - ] - elif platform.uname().processor == "aarch64": - LINUX_PATHS += ["/usr/lib/aarch64-linux-gnu"] + # cross compile feature is only available on linux + # build engine on linux and run on windows + import dllist - LINUX_LIBS = [ - f"libnvinfer_builder_resource_win.so.*", - ] - - for lib in LINUX_LIBS: - try: - ctypes.CDLL(_find_lib(lib, LINUX_PATHS)) + if sys.platform.startswith("linux"): + loaded_libs = dllist.dllist() + target_lib = "libnvinfer_builder_resource_win.so.*" + if target_lib in loaded_libs: return True - except: - continue return False diff --git a/pyproject.toml b/pyproject.toml index 3bb857e3e0..70e36a3175 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ requires = [ "pybind11==2.6.2", "numpy", "sympy", + "dllist", ] build-backend = "setuptools.build_meta" @@ -63,6 +64,7 @@ dependencies = [ "packaging>=23", "numpy", "typing-extensions>=4.7.0", + "dllist", ] dynamic = ["version"] diff --git a/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py b/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py index a721da1baa..d37ddb6871 100644 --- a/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py +++ b/tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py @@ -47,10 +47,8 @@ def forward(self, a, b): "Cross compile for windows can only be enabled on linux x86-64 platform", ) @unittest.skipIf( - not ( - check_cross_compile_trt_win_lib(), - "TRT windows lib for cross compile not found", - ), + not (check_cross_compile_trt_win_lib()), + "TRT windows lib for cross compile not found", ) @pytest.mark.unit def test_dynamo_cross_compile_for_windows(self): @@ -80,6 +78,10 @@ def forward(self, a, b): platform.system() != "Linux" or platform.architecture()[0] != "64bit", "Cross compile for windows can only be enabled on linux x86-64 platform", ) + @unittest.skipIf( + not (check_cross_compile_trt_win_lib()), + "TRT windows lib for cross compile not found", + ) @pytest.mark.unit def test_dynamo_cross_compile_for_windows_cpu_offload(self): class Add(torch.nn.Module): From a6a67d802ca8881ca9467c3ca853b413396f35e3 Mon Sep 17 00:00:00 2001 From: apbose Date: Fri, 9 May 2025 16:27:57 -0700 Subject: [PATCH 6/7] using torchTRT enum platform --- py/torch_tensorrt/_compile.py | 1 - py/torch_tensorrt/_utils.py | 10 +++++----- py/torch_tensorrt/dynamo/_compiler.py | 1 - 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/py/torch_tensorrt/_compile.py b/py/torch_tensorrt/_compile.py index e4e3710675..6dbf52de86 100644 --- a/py/torch_tensorrt/_compile.py +++ b/py/torch_tensorrt/_compile.py @@ -526,7 +526,6 @@ def convert_method_to_trt_engine( raise RuntimeError("Module is an unknown format or the ir requested is unknown") -@needs_cross_compile def load_cross_compiled_exported_program(file_path: str = "") -> Any: """ Load an ExportedProgram file in Windows which was previously cross compiled in Linux diff --git a/py/torch_tensorrt/_utils.py b/py/torch_tensorrt/_utils.py index 2ef6eac17c..135740a536 100644 --- a/py/torch_tensorrt/_utils.py +++ b/py/torch_tensorrt/_utils.py @@ -1,9 +1,7 @@ -import ctypes -import platform -import sys from typing import Any import torch +from torch_tensorrt._enums import Platform def sanitized_torch_version() -> Any: @@ -14,12 +12,14 @@ def sanitized_torch_version() -> Any: ) -def check_cross_compile_trt_win_lib(): +def check_cross_compile_trt_win_lib() -> bool: # cross compile feature is only available on linux # build engine on linux and run on windows import dllist - if sys.platform.startswith("linux"): + platform = Platform.current_platform() + platform = str(platform).lower() + if platform.startswith("linux"): loaded_libs = dllist.dllist() target_lib = "libnvinfer_builder_resource_win.so.*" if target_lib in loaded_libs: diff --git a/py/torch_tensorrt/dynamo/_compiler.py b/py/torch_tensorrt/dynamo/_compiler.py index 0bcfb0fcf2..c109e3fa3c 100644 --- a/py/torch_tensorrt/dynamo/_compiler.py +++ b/py/torch_tensorrt/dynamo/_compiler.py @@ -1247,7 +1247,6 @@ def save_cross_compiled_exported_program( logger.debug(f"successfully saved the module for windows at {file_path}") -@needs_cross_compile def load_cross_compiled_exported_program(file_path: str = "") -> Any: """ Load an ExportedProgram file in Windows which was previously cross compiled in Linux From 5826aac47eed8b02e06c22943ea0be085d7a72ff Mon Sep 17 00:00:00 2001 From: apbose Date: Wed, 14 May 2025 18:17:35 -0700 Subject: [PATCH 7/7] undoing the platform change since its leading to circular imports --- py/torch_tensorrt/_utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/py/torch_tensorrt/_utils.py b/py/torch_tensorrt/_utils.py index 135740a536..9875ae7bbc 100644 --- a/py/torch_tensorrt/_utils.py +++ b/py/torch_tensorrt/_utils.py @@ -1,7 +1,8 @@ from typing import Any +import sys +import platform import torch -from torch_tensorrt._enums import Platform def sanitized_torch_version() -> Any: @@ -17,9 +18,7 @@ def check_cross_compile_trt_win_lib() -> bool: # build engine on linux and run on windows import dllist - platform = Platform.current_platform() - platform = str(platform).lower() - if platform.startswith("linux"): + if sys.platform.startswith("linux"): loaded_libs = dllist.dllist() target_lib = "libnvinfer_builder_resource_win.so.*" if target_lib in loaded_libs: