Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cross compile for windows #3220

Open
wants to merge 56 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
458a4d1
skip run_shape_analysis
lanluo-nvidia Oct 6, 2024
2f408f9
test
lanluo-nvidia Oct 6, 2024
1c5e86c
test
lanluo-nvidia Oct 6, 2024
ba487dc
test
lanluo-nvidia Oct 6, 2024
99d2274
Merge branch 'main' into lluo/save_remove_inputs
lanluo-nvidia Oct 6, 2024
2b43480
test
lanluo-nvidia Oct 6, 2024
92105ce
cross compile for windows initial check in
lanluo-nvidia Oct 7, 2024
dd24bdf
test
lanluo-nvidia Oct 7, 2024
e6c9fa4
add testcase
lanluo-nvidia Oct 7, 2024
9909eca
add test case
lanluo-nvidia Oct 8, 2024
312a79e
add test case
lanluo-nvidia Oct 8, 2024
b4e02e1
Merge branch 'main' into lluo/save_remove_inputs
lanluo-nvidia Oct 11, 2024
3d94f8b
test
lanluo-nvidia Oct 13, 2024
f43669a
Merge branch 'lluo/save_remove_inputs' into lluo/cross_compilation_fo…
lanluo-nvidia Oct 13, 2024
f4d0d27
test
lanluo-nvidia Oct 14, 2024
2a43ca1
add more logs
lanluo-nvidia Oct 14, 2024
76ddd66
test
lanluo-nvidia Oct 15, 2024
28ba6cc
Merge branch 'main' into lluo/save_remove_inputs
lanluo-nvidia Oct 15, 2024
b89cbe0
resolve comments
lanluo-nvidia Oct 15, 2024
079c4be
test
lanluo-nvidia Oct 15, 2024
90f1a60
clean up
lanluo-nvidia Oct 16, 2024
c38dc5b
test
lanluo-nvidia Oct 16, 2024
2843d37
Merge branch 'main' into lluo/save_remove_inputs
lanluo-nvidia Oct 16, 2024
b06a41b
Merge branch 'lluo/save_remove_inputs' into lluo/cross_compilation_fo…
lanluo-nvidia Oct 16, 2024
3eb48d7
test
lanluo-nvidia Oct 16, 2024
fedc5c2
Merge branch 'lluo/save_remove_inputs' into lluo/cross_compilation_fo…
lanluo-nvidia Oct 16, 2024
50eb0d8
replace dummy inference
lanluo-nvidia Oct 20, 2024
95ed602
test
lanluo-nvidia Oct 20, 2024
120f30d
test
lanluo-nvidia Oct 21, 2024
424cbf7
add run_test_with_dynamic_shape change
lanluo-nvidia Oct 21, 2024
2fc9cef
Merge branch 'main' into lluo/save_remove_inputs
lanluo-nvidia Oct 21, 2024
ef54cfc
split the PR, add dummy inference for converter test
lanluo-nvidia Oct 21, 2024
14f5d61
test
lanluo-nvidia Oct 22, 2024
7563959
test
lanluo-nvidia Oct 22, 2024
77355f0
test
lanluo-nvidia Oct 22, 2024
13361fd
add linear lowering meta val
lanluo-nvidia Oct 22, 2024
f0a9fef
add linear_lowering change
lanluo-nvidia Oct 23, 2024
cff64a4
test
lanluo-nvidia Oct 23, 2024
933abac
test
lanluo-nvidia Oct 23, 2024
8417684
resolve comments
lanluo-nvidia Oct 25, 2024
8676f88
test
lanluo-nvidia Oct 25, 2024
785d0b1
change solution: use no_op_placeholder during save and replace it wit…
lanluo-nvidia Oct 26, 2024
b85cb74
Merge branch 'lluo/save_remove_inputs' into lluo/cross_compilation_fo…
lanluo-nvidia Oct 26, 2024
5d594b1
test
lanluo-nvidia Oct 26, 2024
076f47a
resolve comments
lanluo-nvidia Oct 29, 2024
8250179
Merge branch 'main' into lluo/save_remove_inputs
lanluo-nvidia Oct 29, 2024
96e93e4
resolve comments
lanluo-nvidia Oct 29, 2024
7d055bf
Merge branch 'lluo/save_remove_inputs' into lluo/cross_compilation_fo…
lanluo-nvidia Oct 30, 2024
6e214d6
Merge branch 'main' into lluo/cross_compilation_for_windows
lanluo-nvidia Oct 30, 2024
cf0e0ae
resolve comments
lanluo-nvidia Oct 30, 2024
fb7fd1d
resolve comments
lanluo-nvidia Oct 31, 2024
8fc89dc
resolve comments
lanluo-nvidia Oct 31, 2024
63b3376
Merge branch 'main' into lluo/cross_compilation_for_windows
lanluo-nvidia Nov 1, 2024
1a1ba41
merge latest main
lanluo-nvidia Nov 1, 2024
96bc256
fix linting
lanluo-nvidia Nov 1, 2024
eebe1bc
test
lanluo-nvidia Nov 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
resolve comments
  • Loading branch information
lanluo-nvidia committed Oct 31, 2024
commit 8fc89dc31e15c6feca2a2fe5746b0fa409c92553
27 changes: 4 additions & 23 deletions py/torch_tensorrt/_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from torch_tensorrt._features import ENABLED_FEATURES
from torch_tensorrt._Input import Input
from torch_tensorrt.dynamo import _defaults
from torch_tensorrt.dynamo._exporter import replace_execute_engine_no_op_node
from torch_tensorrt.fx import InputTensorSpec
from torch_tensorrt.fx.lower import compile as fx_compile
from torch_tensorrt.fx.utils import LowerPrecision
Expand All @@ -34,6 +33,9 @@
from torch_tensorrt.dynamo._compiler import (
cross_compile_for_windows as dynamo_cross_compile_for_windows,
)
from torch_tensorrt.dynamo._compiler import (
load_cross_compiled_exported_program as dynamo_load_cross_compiled_exported_program,
)
from torch_tensorrt.dynamo._compiler import (
save_cross_compiled_exported_program as dynamo_save_cross_compiled_exported_program,
)
Expand Down Expand Up @@ -530,28 +532,7 @@ def load_cross_compiled_exported_program(file_path: str = "") -> Any:
Raises:
ValueError: If the api is not called in windows or there is no file or the file is a valid ExportedProgram file
"""
if not file_path:
raise ValueError("File path cannot be empty. Please provide a valid file path")

if platform.system() != "Windows" or platform.machine() != "AMD64":
raise ValueError(
"cross runtime compiled model for windows can only be loaded in Windows system"
)

try:
logger.debug(f"Loading the provided file {file_path} using torch.export.load()")
# TODO: think about how to handle the torch.jit.load route?
exp_program = torch.export.load(file_path)
except Exception as e:
logger.info(
f"Loading the provided file {file_path} via torch.export.load() failed with the following error: {e}",
exc_info=True,
)
raise ValueError(
f"cross_load the file {file_path} doesn't correspond to a valid ExportedProgram. Please verify the file path."
)

return replace_execute_engine_no_op_node(exp_program)
return dynamo_load_cross_compiled_exported_program(file_path)


def load(file_path: str = "") -> Any:
Expand Down
37 changes: 35 additions & 2 deletions py/torch_tensorrt/dynamo/_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
parse_non_trt_nodes,
)
from torch_tensorrt.dynamo._engine_cache import BaseEngineCache, DiskEngineCache
from torch_tensorrt.dynamo._exporter import replace_execute_engine_no_op_node
from torch_tensorrt.dynamo.conversion import (
CompilationSettings,
UnsupportedOperatorException,
Expand Down Expand Up @@ -83,8 +84,6 @@ def cross_compile_for_windows(
dryrun: bool = _defaults.DRYRUN,
hardware_compatible: bool = _defaults.HARDWARE_COMPATIBLE,
timing_cache_path: str = _defaults.TIMING_CACHE_PATH,
engine_cache_dir: str = _defaults.ENGINE_CACHE_DIR,
engine_cache_size: int = _defaults.ENGINE_CACHE_SIZE,
use_explicit_typing: bool = _defaults.USE_EXPLICIT_TYPING,
use_fp32_acc: bool = _defaults.USE_FP32_ACC,
enable_weight_streaming: bool = _defaults.ENABLE_WEIGHT_STREAMING,
Expand Down Expand Up @@ -993,3 +992,37 @@ def save_cross_compiled_exported_program(
exp_program = export(gm, cross_compile_flag=True)
torch.export.save(exp_program, file_path)
logger.debug(f"successfully saved the module for windows at {file_path}")


def load_cross_compiled_exported_program(file_path: str = "") -> Any:
"""
Load an ExportedProgram file in Windows which was previously cross compiled in Linux

Arguments:
file_path (str): Path to file on the disk

Raises:
ValueError: If the api is not called in windows or there is no file or the file is a valid ExportedProgram file
"""
if not file_path:
raise ValueError("File path cannot be empty. Please provide a valid file path")

if platform.system() != "Windows" or platform.machine() != "AMD64":
raise ValueError(
"cross runtime compiled model for windows can only be loaded in Windows system"
)

try:
logger.debug(f"Loading the provided file {file_path} using torch.export.load()")
# TODO: think about how to handle the torch.jit.load route?
exp_program = torch.export.load(file_path)
except Exception as e:
logger.info(
f"Loading the provided file {file_path} via torch.export.load() failed with the following error: {e}",
exc_info=True,
)
raise ValueError(
f"cross_load the file {file_path} doesn't correspond to a valid ExportedProgram. Please verify the file path."
)

return replace_execute_engine_no_op_node(exp_program)
22 changes: 7 additions & 15 deletions py/torch_tensorrt/dynamo/_exporter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64
import copy
import operator
from typing import Any, Dict, Sequence, Tuple, cast, Optional
from typing import Any, Dict, Optional, Sequence, Tuple, cast

import torch
from torch._guards import detect_fake_mode
Expand Down Expand Up @@ -438,7 +438,11 @@ def inline_trt_modules_for_windows(gm: torch.fx.GraphModule) -> torch.fx.GraphMo
trt_module_node = trt_module_node[0]
assert trt_module_node.args

num_outputs = len(trt_module.output_shapes)
if "val" not in trt_module_node.meta:
raise ValueError(
f"trt_module_node: {trt_module_node.name} does not have the metadata which should be set during dynamo compile_module step."
)
num_outputs = len(trt_module_node.meta["val"])
# Insert a call_function node to perform inference on TRT engine
with gm.graph.inserting_before(trt_module_node):
engine_info = trt_module._pack_engine_info()
Expand All @@ -449,20 +453,8 @@ def inline_trt_modules_for_windows(gm: torch.fx.GraphModule) -> torch.fx.GraphMo
torch.ops.tensorrt.no_op_placeholder_for_execute_engine.default,
(trt_module_node.args, *engine_info),
)

trt_node.meta["val"] = []
assert num_outputs > 0
# Generate meta data for TRT node (a FakeTensor with corresponding output shape)
for idx in range(num_outputs):
trt_node.meta["val"].append(
cast(
FakeTensor,
torch.empty_strided(
tuple(trt_module.output_shapes[idx]),
tuple([1] * len(trt_module.output_shapes[idx])),
),
)
)
trt_node.meta["val"] = trt_module_node.meta["val"]

if num_outputs == 1:
# Insert getitem nodes as outputs (for export serialization to work)
Expand Down
12 changes: 10 additions & 2 deletions tests/py/dynamo/runtime/test_003_cross_compile_for_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ def forward(self, a, b):
model = Add().eval().cuda()
inputs = [torch.randn(2, 3).cuda(), torch.randn(2, 3).cuda()]
trt_ep_path = os.path.join(tempfile.gettempdir(), "trt.ep")
compile_spec = {
"inputs": inputs,
"min_block_size": 1,
}
try:
torch_tensorrt.cross_compile_for_windows(
model, file_path=trt_ep_path, inputs=inputs
model, file_path=trt_ep_path, **compile_spec
)
except Exception as e:
pytest.fail(f"unexpected exception raised: {e}")
Expand All @@ -47,9 +51,13 @@ def forward(self, a, b):
inputs = (torch.randn(2, 3).cuda(), torch.randn(2, 3).cuda())
trt_ep_path = os.path.join(tempfile.gettempdir(), "trt.ep")
exp_program = torch.export.export(model, inputs)
compile_spec = {
"inputs": inputs,
"min_block_size": 1,
}
try:
trt_gm = torch_tensorrt.dynamo.cross_compile_for_windows(
exp_program, inputs=inputs
exp_program, **compile_spec
)
torch_tensorrt.dynamo.save_cross_compiled_exported_program(
trt_gm, file_path=trt_ep_path
Expand Down
Loading