Skip to content

Commit fefd1f9

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

File tree

18 files changed

+481
-270
lines changed

18 files changed

+481
-270
lines changed

.circleci/config.yml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,22 @@ 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+
pip install timm
719+
pytest --junitxml=/tmp/artifacts/test_results/dynamo/test_results.xml --ir torch_compile
720+
popd
721+
- store_test_results:
722+
path: /tmp/artifacts
723+
- store_artifacts:
724+
path: /tmp/testlogs
725+
710726
# Define a job to be invoked later in a workflow.
711727
# See: https://circleci.com/docs/2.0/configuration-reference/#jobs
712728
jobs:
@@ -883,6 +899,68 @@ jobs:
883899
- dump-test-env
884900
- test-fx-no-aten
885901

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

1342+
- test-py-dynamo-x86_64-linux:
1343+
torch-build: << pipeline.parameters.torch-build >>
1344+
torch-build-index: << pipeline.parameters.torch-build-index >>
1345+
trt-version-long: << pipeline.parameters.trt-version-long >>
1346+
requires:
1347+
- build-x86_64-linux
1348+
12641349
- build-x86_64-linux:
12651350
name: build-x86_64-linux-legacy
12661351
torch-build: << pipeline.parameters.torch-build-legacy >>
@@ -1291,6 +1376,12 @@ workflows:
12911376
requires:
12921377
- build-x86_64-linux-legacy
12931378

1379+
- test-py-dynamo-x86_64-linux-no-aten:
1380+
torch-build: << pipeline.parameters.torch-build-legacy >>
1381+
torch-build-index: << pipeline.parameters.torch-build-index-legacy >>
1382+
trt-version-long: << pipeline.parameters.trt-version-long >>
1383+
requires:
1384+
- build-x86_64-linux-legacy
12941385
release:
12951386
when: << pipeline.parameters.enable-packaging >>
12961387
jobs:
@@ -1328,6 +1419,13 @@ workflows:
13281419
requires:
13291420
- package-x86_64-linux
13301421

1422+
- test-py-dynamo-x86_64-linux:
1423+
torch-build: << pipeline.parameters.torch-build >>
1424+
torch-build-index: << pipeline.parameters.torch-build-index >>
1425+
trt-version-long: << pipeline.parameters.trt-version-long >>
1426+
requires:
1427+
- package-x86_64-linux
1428+
13311429
on-push:
13321430
jobs:
13331431
- build-x86_64-linux:
@@ -1357,6 +1455,13 @@ workflows:
13571455
requires:
13581456
- build-x86_64-linux
13591457

1458+
- test-py-dynamo-x86_64-linux:
1459+
torch-build: << pipeline.parameters.torch-build >>
1460+
torch-build-index: << pipeline.parameters.torch-build-index >>
1461+
trt-version-long: << pipeline.parameters.trt-version-long >>
1462+
requires:
1463+
- build-x86_64-linux
1464+
13601465
- build-x86_64-linux-cmake:
13611466
torch-build: << pipeline.parameters.torch-build >>
13621467
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)