Skip to content

Commit 08d6c41

Browse files
committed
minor fix: Small changes
1 parent 075a028 commit 08d6c41

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

py/torch_tensorrt/dynamo/test/test_dynamo_backend.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import torch
22
import timm
33
import pytest
4+
import unittest
45

56
import torch_tensorrt as torchtrt
67
import torchvision.models as models
@@ -12,6 +13,8 @@
1213
cosine_similarity,
1314
)
1415

16+
assertions = unittest.TestCase()
17+
1518

1619
@pytest.mark.unit
1720
def test_resnet18(ir):
@@ -32,9 +35,9 @@ def test_resnet18(ir):
3235

3336
trt_mod = torchtrt.compile(model, **compile_spec)
3437
cos_sim = cosine_similarity(model(input), trt_mod(input))
35-
assert (
38+
assertions.assertTrue(
3639
cos_sim > COSINE_THRESHOLD,
37-
f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
40+
msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
3841
)
3942

4043
# Clean up model env
@@ -63,9 +66,9 @@ def test_mobilenet_v2(ir):
6366

6467
trt_mod = torchtrt.compile(model, **compile_spec)
6568
cos_sim = cosine_similarity(model(input), trt_mod(input))
66-
assert (
69+
assertions.assertTrue(
6770
cos_sim > COSINE_THRESHOLD,
68-
f"Mobilenet v2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
71+
msg=f"Mobilenet v2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
6972
)
7073

7174
# Clean up model env
@@ -94,9 +97,9 @@ def test_efficientnet_b0(ir):
9497

9598
trt_mod = torchtrt.compile(model, **compile_spec)
9699
cos_sim = cosine_similarity(model(input), trt_mod(input))
97-
assert (
100+
assertions.assertTrue(
98101
cos_sim > COSINE_THRESHOLD,
99-
f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
102+
msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
100103
)
101104

102105
# Clean up model env
@@ -138,9 +141,9 @@ def test_bert_base_uncased(ir):
138141
for key in model_outputs.keys():
139142
out, trt_out = model_outputs[key], trt_model_outputs[key]
140143
cos_sim = cosine_similarity(out, trt_out)
141-
assert (
144+
assertions.assertTrue(
142145
cos_sim > COSINE_THRESHOLD,
143-
f"HF BERT base-uncased TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
146+
msg=f"HF BERT base-uncased TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
144147
)
145148

146149
# Clean up model env
@@ -169,9 +172,9 @@ def test_resnet18_half(ir):
169172

170173
trt_mod = torchtrt.compile(model, **compile_spec)
171174
cos_sim = cosine_similarity(model(input), trt_mod(input))
172-
assert (
175+
assertions.assertTrue(
173176
cos_sim > COSINE_THRESHOLD,
174-
f"Resnet18 Half TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
177+
msg=f"Resnet18 Half TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
175178
)
176179

177180
# Clean up model env

py/torch_tensorrt/fx/converters/acc_ops_converters.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -159,36 +159,32 @@ def acc_ops_conv_transposend(
159159
input_val.shape[1] != -1
160160
), "Channel dim can't be dynamic for transpose convolution."
161161

162-
if not isinstance(kwargs["bias"], TRTTensor):
163-
if kwargs["bias"] is not None and not isinstance(kwargs["bias"], torch.Tensor):
164-
raise RuntimeError(
165-
f"linear {name} has bias of type {type(kwargs['bias'])}, Expect Optional[Tensor]"
166-
)
167-
bias = to_numpy(kwargs["bias"]) # type: ignore[arg-type]
168-
else:
169-
bias = kwargs["bias"]
162+
# for now we'll assume bias is constant Tensor or None,
163+
# and bias being ITensor is not supported in TensorRT api
164+
# right now
165+
if kwargs["bias"] is not None and not isinstance(kwargs["bias"], torch.Tensor):
166+
raise RuntimeError(
167+
f"ConvTranspose {name} has bias of type {type(kwargs['bias'])}, Expect Optional[Tensor]"
168+
)
169+
bias = to_numpy(kwargs["bias"]) # type: ignore[arg-type]
170170

171171
if network.has_explicit_precision or isinstance(kwargs["weight"], TRTTensor):
172172
weight = get_trt_tensor(network, kwargs["weight"], f"{name}_weight")
173173
weight_shape = tuple(kwargs["weight"].shape) # type: ignore[union-attr]
174174
# will need to use uninitialized weight and set it later to support
175175
# ITensor weights
176+
dummy_weight = trt.Weights()
176177

177178
# nn.ConvTranspose2d/3d weight size is (in_channels, out_channels/groups, kernel_0, kernel_1, [kernel_2])
178179
layer = network.add_deconvolution_nd(
179180
input=input_val,
180181
num_output_maps=weight.shape[1] * kwargs["groups"],
181182
kernel_shape=weight.shape[2:],
182-
kernel=trt.Weights(),
183-
bias=trt.Weights() if isinstance(bias, TRTTensor) else bias,
183+
kernel=dummy_weight,
184+
bias=bias,
184185
)
185186

186187
layer.set_input(1, weight)
187-
188-
# If the bias is a TRTTensor, set it as an input of the layer
189-
if isinstance(bias, TRTTensor):
190-
bias = get_trt_tensor(network, bias, f"{name}_bias")
191-
layer.set_input(2, bias)
192188
else:
193189
if not isinstance(kwargs["weight"], torch.Tensor):
194190
raise RuntimeError(

py/torch_tensorrt/fx/converters/impl/convolution.py

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numpy as np
2-
from typing import Any, Callable, Optional, Sequence, Union
2+
from typing import Any, Optional, Sequence, Union
33

44
# @manual=//deeplearning/trt/python:py_tensorrt
55
import tensorrt as trt
@@ -62,21 +62,8 @@ def convNd(
6262
# Transform the bias constant into a Numpy array
6363
bias = to_numpy(bias)
6464

65-
# Prepend new dimension (unsqueeze) if the convolution is 1d
66-
if is_conv1d:
67-
bias = np.expand_dims(bias, 0)
68-
6965
elif isinstance(bias, TRTTensor):
7066
bias = get_trt_tensor(network, bias, f"{name}_bias")
71-
# Prepend new dimension (unsqueeze) if the convolution is 1d
72-
if is_conv1d:
73-
kwargs = {
74-
"input": bias,
75-
"dim": 0,
76-
}
77-
bias = acc_ops_unsqueeze(
78-
network, target, tuple(), kwargs, name + "_unsqueeze_bias"
79-
)
8067

8168
elif bias is not None:
8269
raise RuntimeError(

0 commit comments

Comments
 (0)