Skip to content

Commit 226cc79

Browse files
committed
fix: Reorganize, add tests
1 parent 6d2e01a commit 226cc79

File tree

18 files changed

+481
-270
lines changed

18 files changed

+481
-270
lines changed

.circleci/config.yml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,23 @@ commands:
707707
- store_artifacts:
708708
path: /tmp/testlogs
709709

710+
test-dynamo-torch_compile:
711+
description: "Test the Dynamo torch_compile path"
712+
steps:
713+
- run:
714+
name: Run Dynamo torch_compile E2E tests
715+
command: |
716+
cd py/torch_tensorrt/dynamo/
717+
pushd test/
718+
pip3 install timm
719+
pip3 install transformers
720+
pytest --junitxml=/tmp/artifacts/test_results/dynamo/test_results.xml --ir torch_compile
721+
popd
722+
- store_test_results:
723+
path: /tmp/artifacts
724+
- store_artifacts:
725+
path: /tmp/testlogs
726+
710727
# Define a job to be invoked later in a workflow.
711728
# See: https://circleci.com/docs/2.0/configuration-reference/#jobs
712729
jobs:
@@ -883,6 +900,68 @@ jobs:
883900
- dump-test-env
884901
- test-fx-no-aten
885902

903+
test-py-dynamo-x86_64-linux:
904+
parameters:
905+
torch-build:
906+
type: string
907+
torch-build-index:
908+
type: string
909+
trt-version-long:
910+
type: string
911+
machine:
912+
image: ubuntu-2004-cuda-11.4:202110-01
913+
resource_class: gpu.nvidia.large
914+
steps:
915+
- checkout
916+
- attach_workspace:
917+
at: /tmp/dist/
918+
- install-torch-from-index:
919+
torch-build: << parameters.torch-build >>
920+
torch-build-index: << parameters.torch-build-index >>
921+
- create-py-env:
922+
trt-version-long: << parameters.trt-version-long >>
923+
- install-cudnn
924+
# - run:
925+
# name: "Set LD_LIBRARY_PATH path to include the installed CUDNN"
926+
# command: export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/:$LD_LIBRARY_PATH
927+
- run:
928+
name: "Install torch-tensorrt"
929+
command: pip3 install --pre /tmp/dist/x86_64-linux/*cp39-cp39*.whl
930+
# We install torch after torch-trt because pip automatically enforces the version constraint otherwise
931+
- dump-test-env
932+
- test-dynamo-torch_compile
933+
934+
test-py-dynamo-x86_64-linux-no-aten:
935+
parameters:
936+
torch-build:
937+
type: string
938+
torch-build-index:
939+
type: string
940+
trt-version-long:
941+
type: string
942+
machine:
943+
image: ubuntu-2004-cuda-11.4:202110-01
944+
resource_class: gpu.nvidia.large
945+
steps:
946+
- checkout
947+
- attach_workspace:
948+
at: /tmp/dist/
949+
- install-torch-from-index:
950+
torch-build: << parameters.torch-build >>
951+
torch-build-index: << parameters.torch-build-index >>
952+
- create-py-env:
953+
trt-version-long: << parameters.trt-version-long >>
954+
- install-cudnn
955+
# - run:
956+
# name: "Set LD_LIBRARY_PATH path to include the installed CUDNN"
957+
# command: export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/:$LD_LIBRARY_PATH
958+
- run:
959+
name: "Install torch-tensorrt"
960+
command: pip3 install --pre /tmp/dist/x86_64-linux/*cp39-cp39*.whl
961+
# We install torch after torch-trt because pip automatically enforces the version constraint otherwise
962+
- dump-test-env
963+
- test-dynamo-torch_compile
964+
886965
package-x86_64-linux:
887966
parameters:
888967
enabled:
@@ -1261,6 +1340,13 @@ workflows:
12611340
requires:
12621341
- build-x86_64-linux
12631342

1343+
- test-py-dynamo-x86_64-linux:
1344+
torch-build: << pipeline.parameters.torch-build >>
1345+
torch-build-index: << pipeline.parameters.torch-build-index >>
1346+
trt-version-long: << pipeline.parameters.trt-version-long >>
1347+
requires:
1348+
- build-x86_64-linux
1349+
12641350
- build-x86_64-linux:
12651351
name: build-x86_64-linux-legacy
12661352
torch-build: << pipeline.parameters.torch-build-legacy >>
@@ -1291,6 +1377,12 @@ workflows:
12911377
requires:
12921378
- build-x86_64-linux-legacy
12931379

1380+
- test-py-dynamo-x86_64-linux-no-aten:
1381+
torch-build: << pipeline.parameters.torch-build-legacy >>
1382+
torch-build-index: << pipeline.parameters.torch-build-index-legacy >>
1383+
trt-version-long: << pipeline.parameters.trt-version-long >>
1384+
requires:
1385+
- build-x86_64-linux-legacy
12941386
release:
12951387
when: << pipeline.parameters.enable-packaging >>
12961388
jobs:
@@ -1328,6 +1420,13 @@ workflows:
13281420
requires:
13291421
- package-x86_64-linux
13301422

1423+
- test-py-dynamo-x86_64-linux:
1424+
torch-build: << pipeline.parameters.torch-build >>
1425+
torch-build-index: << pipeline.parameters.torch-build-index >>
1426+
trt-version-long: << pipeline.parameters.trt-version-long >>
1427+
requires:
1428+
- package-x86_64-linux
1429+
13311430
on-push:
13321431
jobs:
13331432
- build-x86_64-linux:
@@ -1357,6 +1456,13 @@ workflows:
13571456
requires:
13581457
- build-x86_64-linux
13591458

1459+
- test-py-dynamo-x86_64-linux:
1460+
torch-build: << pipeline.parameters.torch-build >>
1461+
torch-build-index: << pipeline.parameters.torch-build-index >>
1462+
trt-version-long: << pipeline.parameters.trt-version-long >>
1463+
requires:
1464+
- build-x86_64-linux
1465+
13601466
- build-x86_64-linux-cmake:
13611467
torch-build: << pipeline.parameters.torch-build >>
13621468
torch-build-index: << pipeline.parameters.torch-build-index >>

py/torch_tensorrt/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ def _find_lib(name, paths):
9393
from torch_tensorrt._TRTModuleNext import TRTModuleNext
9494

9595
from torch_tensorrt import fx
96+
from torch_tensorrt import dynamo
97+
from torch_tensorrt.dynamo import torch_compile
9698

9799

98100
def _register_with_torch():

py/torch_tensorrt/_compile.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class _IRType(Enum):
1515

1616
ts = 0
1717
fx = 1
18+
torch_compile = 2
19+
fx_ts_compat_compile = 3
1820

1921

2022
class _ModuleType(Enum):
@@ -45,11 +47,17 @@ def _get_target_ir(module_type: _ModuleType, ir: str) -> _IRType:
4547

4648
ir_targets_torchscript = any([ir == opt for opt in ["torchscript", "ts"]])
4749
ir_targets_fx = ir == "fx"
50+
ir_targets_torch_compile = ir == "torch_compile"
51+
ir_targets_fx_ts_compat_compile = ir == "fx_ts_compat_compile"
4852

4953
if module_is_tsable and ir_targets_torchscript:
5054
return _IRType.ts
5155
elif module_is_fxable and ir_targets_fx:
5256
return _IRType.fx
57+
elif module_is_fxable and ir_targets_torch_compile:
58+
return _IRType.torch_compile
59+
elif module_is_fxable and ir_targets_fx_ts_compat_compile:
60+
return _IRType.fx_ts_compat_compile
5361
else:
5462
if ir == "default":
5563
# Options are listed in order of preference
@@ -148,6 +156,10 @@ def compile(
148156
dynamic_batch=False,
149157
**kwargs,
150158
)
159+
elif target_ir == _IRType.torch_compile:
160+
return torch_tensorrt.dynamo.torch_compile(
161+
module, inputs=inputs, enabled_precisions=enabled_precisions, **kwargs
162+
)
151163
else:
152164
raise RuntimeError("Module is an unknown format or the ir requested is unknown")
153165

py/torch_tensorrt/dynamo/__init__.py

Lines changed: 1 addition & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1 @@
1-
import torch
2-
import logging
3-
import collections.abc
4-
import torch_tensorrt
5-
from functools import partial
6-
7-
from typing import Any
8-
from torch_tensorrt import EngineCapability, Device
9-
from torch_tensorrt.fx.utils import LowerPrecision
10-
11-
from torch_tensorrt.dynamo._settings import CompilationSettings
12-
from torch_tensorrt.dynamo.utils import prepare_inputs, prepare_device
13-
from torch_tensorrt.dynamo.backends import tensorrt_backend
14-
from torch_tensorrt.dynamo._defaults import (
15-
PRECISION,
16-
DEBUG,
17-
MAX_WORKSPACE_SIZE,
18-
MAX_NUM_TRT_ENGINES,
19-
)
20-
21-
22-
logger = logging.getLogger(__name__)
23-
24-
25-
def compile(
26-
gm: torch.nn.Module,
27-
inputs: Any,
28-
*,
29-
device=Device._current_device(),
30-
disable_tf32=False,
31-
sparse_weights=False,
32-
enabled_precisions=set(),
33-
refit=False,
34-
debug=DEBUG,
35-
capability=EngineCapability.default,
36-
num_avg_timing_iters=1,
37-
workspace_size=MAX_WORKSPACE_SIZE,
38-
dla_sram_size=1048576,
39-
dla_local_dram_size=1073741824,
40-
dla_global_dram_size=536870912,
41-
calibrator=None,
42-
truncate_long_and_double=False,
43-
require_full_compilation=False,
44-
min_block_size=3,
45-
torch_executed_ops=[],
46-
torch_executed_modules=[],
47-
**kwargs,
48-
):
49-
50-
logger.warn(
51-
"The Dynamo backend is an experimental feature, for which only the "
52-
+ "following arguments are supported: "
53-
+ "{enabled_precisions, debug, workspace_size, max_num_trt_engines}"
54-
)
55-
56-
if not isinstance(inputs, collections.abc.Sequence):
57-
inputs = [inputs]
58-
59-
inputs = prepare_inputs(inputs, prepare_device(device))
60-
61-
if (
62-
torch.float16 in enabled_precisions
63-
or torch_tensorrt.dtype.half in enabled_precisions
64-
):
65-
lower_precision = LowerPrecision.FP16
66-
elif (
67-
torch.float32 in enabled_precisions
68-
or torch_tensorrt.dtype.float in enabled_precisions
69-
):
70-
lower_precision = LowerPrecision.FP32
71-
elif len(enabled_precisions) == 0:
72-
logger.info(f"No precision specified, defaulting to {PRECISION}")
73-
lower_precision = PRECISION
74-
else:
75-
raise ValueError(
76-
f"Precision {enabled_precisions} not supported in the Dynamo Path"
77-
)
78-
79-
custom_backend = create_backend(
80-
precision=lower_precision,
81-
debug=debug,
82-
workspace_size=workspace_size,
83-
**kwargs,
84-
)
85-
86-
model = torch.compile(gm, backend=custom_backend)
87-
88-
# Ensure compilation occurs by calling the function with provided inputs
89-
model(*inputs)
90-
91-
return model
92-
93-
94-
from torch_tensorrt.fx.utils import LowerPrecision
95-
96-
logger = logging.getLogger(__name__)
97-
98-
99-
def create_backend(
100-
precision: LowerPrecision = PRECISION,
101-
debug: bool = DEBUG,
102-
workspace_size: int = MAX_WORKSPACE_SIZE,
103-
max_num_trt_engines: int = MAX_NUM_TRT_ENGINES,
104-
**kwargs,
105-
):
106-
"""Create torch.compile backend given specified arguments
107-
108-
Args:
109-
precision:
110-
debug: Whether to print out verbose debugging information
111-
workspace_size: Maximum workspace TRT is allowed to use for the module
112-
precision: Model Layer precision
113-
Returns:
114-
Backend for torch.compile
115-
"""
116-
settings = CompilationSettings(
117-
debug=debug,
118-
precision=precision,
119-
workspace_size=workspace_size,
120-
max_num_trt_engines=max_num_trt_engines,
121-
)
122-
123-
return partial(
124-
tensorrt_backend,
125-
settings=settings,
126-
)
1+
from .torch_compile import compile as torch_compile

py/torch_tensorrt/dynamo/lowering/__init__.py

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
4+
def pytest_addoption(parser):
5+
parser.addoption(
6+
"--ir",
7+
metavar="Internal Representation",
8+
nargs=1,
9+
type=str,
10+
required=True,
11+
help="IR to compile with",
12+
choices=["torch_compile", "fx_ts_compat_compile"],
13+
)
14+
15+
16+
@pytest.fixture
17+
def ir(request):
18+
return request.config.getoption("--ir")[0]

0 commit comments

Comments
 (0)