Skip to content

chore: Add cherry picks #2797

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

Merged
merged 24 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
815751b
fix: FakeTensors appearing in `get_attr` calls (#2669)
gs-olive Mar 20, 2024
5930e96
feat: support adaptive_avg_pool1d dynamo converter (#2614)
zewenli98 Mar 21, 2024
e150913
fix: Add cmake missing source file ref for core_lowering.passes (#2672)
Arktische Mar 22, 2024
4bb60f3
Add support for `aten.pixel_unshuffle` dynamo converter (#2696)
HolyWu Apr 5, 2024
4314fbc
feat: support aten.atan2 converter (#2689)
chohk88 Apr 12, 2024
a9a6272
feat: support aten.index_select converter (#2710)
chohk88 Apr 12, 2024
f5b7b31
feat: support aten.isnan converter (#2711)
chohk88 Apr 12, 2024
264905d
feat: support adaptive avg pool 2d and 3d dynamo converters (#2632)
zewenli98 Apr 12, 2024
fc29af4
feat: support aten.expm1 converter (#2714)
chohk88 Apr 12, 2024
4abec39
fix: Add dependencies to Docker container for `apt` versioning TRT (#…
gs-olive Apr 12, 2024
25a3b28
fix: param bug in `test_binary_ops_aten` (#2733)
zewenli98 Apr 16, 2024
822e63c
aten::empty_like (#2654)
apbose Apr 16, 2024
dee74c4
empty_permute decomposition (#2698)
apbose Apr 17, 2024
77c4b96
Removing grid lowering (#2686)
apbose Apr 17, 2024
2f8937f
chore(deps): bump transformers from 4.33.2 to 4.36.0 in /tools/perf (…
dependabot[bot] Apr 19, 2024
6ea06d9
Fix upsample converter not properly registered (#2683)
HolyWu Apr 19, 2024
79f7f38
feat: TS Add converter support for aten::grid_sampler (#2717)
mfeliz-cruise Apr 19, 2024
164b352
chore: fix is_nan_test
peri044 Apr 26, 2024
bec91fb
Fix minor grammatical corrections (#2779)
aakashapoorv Apr 26, 2024
08f1636
fix: convert_module_to_trt_engine (#2728)
zewenli98 Apr 25, 2024
4fd5e11
chore: rebase
peri044 Apr 29, 2024
ea4d580
"empty_like" decomposition test correction (#2784)
apbose Apr 26, 2024
744abeb
fix: refactor layer norm converter with INormalization Layer (#2755)
zewenli98 Apr 26, 2024
9c6597f
chore: rebase
peri044 Apr 29, 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
111 changes: 20 additions & 91 deletions py/torch_tensorrt/dynamo/conversion/impl/normalization/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from torch_tensorrt.dynamo.conversion._ConversionContext import ConversionContext
from torch_tensorrt.dynamo.conversion.converter_utils import (
cast_trt_tensor,
get_axes_for_reduce_op,
get_positive_dim,
get_trt_tensor,
to_numpy,
Expand Down Expand Up @@ -105,102 +106,30 @@ def layer_norm(
cudnn_enable: bool,
return_mean_rstd: bool,
) -> Union[TRTTensor, Tuple[TRTTensor, torch.Tensor, torch.Tensor]]:
if weight is None:
weight = to_numpy(1.0)

if bias is None:
bias = to_numpy(0.0)

shape = weight.shape
gamma = to_numpy(weight).reshape(shape)
beta = to_numpy(bias).reshape(shape)

dims = list(range(len(input.shape) - len(shape), len(input.shape)))

# E[x]
mean_expected_trt = impl.reduce.mean(
ctx, target, source_ir, f"{name}_mean_expected", input, dims, True
)

# X-E[x]
sub_trt = impl.elementwise.sub(
ctx,
target,
source_ir,
f"{name}_sub",
input,
mean_expected_trt,
)

# Variance = mean(pow(x_sub_mean, 2))
pow_trt = get_trt_tensor(ctx, 2, f"{name}_power", np.float32)
pow_var = impl.elementwise.pow(
ctx,
target,
source_ir,
f"{name}_pow_var",
sub_trt,
pow_trt,
)
mean_trt = impl.reduce.mean(
ctx, target, source_ir, f"{name}_mean", pow_var, dims, True
)

# sqrt((var + eps))
eps_trt = get_trt_tensor(ctx, eps, f"{name}_eps", np.float32)
add_trt = impl.elementwise.add(
ctx,
target,
source_ir,
f"{name}_add",
mean_trt,
eps_trt,
)
sqrt_trt = impl.unary.sqrt(
ctx,
target,
source_ir,
f"{name}_sqrt",
add_trt,
)

# (X - E[X]) / sqrt((var + eps))
div_trt = impl.elementwise.div(
ctx,
target,
source_ir,
f"{name}_div",
sub_trt,
sqrt_trt,
)

gamma_trt = get_trt_tensor(ctx, weight, f"{name}_gamma")
beta_trt = get_trt_tensor(ctx, bias, f"{name}_beta")

# y * gamma + beta
scaled_y = impl.elementwise.mul(
ctx,
target,
source_ir,
f"{name}_mul_gamma",
div_trt,
gamma_trt,
)
dims = list(range(len(input.shape) - len(normalized_shape), len(input.shape)))
axes = get_axes_for_reduce_op(dims)

weight = get_trt_tensor(ctx, weight, f"{name}_weight")
bias = get_trt_tensor(ctx, bias, f"{name}_bias")
if tuple(input.shape) != tuple(weight.shape):
weight = impl.slice.expand(
ctx, target, source_ir, f"{name}_expand_weight", weight, input.shape
)
if tuple(input.shape) != tuple(bias.shape):
bias = impl.slice.expand(
ctx, target, source_ir, f"{name}_expand_bias", bias, input.shape
)

output = impl.elementwise.add(
ctx,
target,
source_ir,
f"{name}_add_beta",
scaled_y,
beta_trt,
)
layer_norm = ctx.net.add_normalization(input, weight, bias, axes)
layer_norm.epsilon = eps
layer_norm.compute_precision = input.dtype
set_layer_name(layer_norm, target, f"{name}_layer_norm", source_ir)

if return_mean_rstd:
# return fake mean and rstd for now
return output, None, None
return layer_norm.get_output(0), None, None

return output
return layer_norm.get_output(0)


def native_group_norm(
Expand Down
72 changes: 61 additions & 11 deletions tests/py/dynamo/conversion/test_layer_norm_aten.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,75 @@
import torch
from parameterized import parameterized
from torch.testing._internal.common_utils import run_tests
from torch_tensorrt import Input

from .harness import DispatchTestCase


class TestLayerNormConverter(DispatchTestCase):
def test_layer_norm(self):
@parameterized.expand(
[
(
(5, 3, 2, 4),
[
4,
],
),
((5, 3, 2, 4), [2, 4]),
((5, 3, 2, 4), [3, 2, 4]),
((5, 3, 2, 4), [5, 3, 2, 4]),
]
)
def test_layer_norm(self, input_shape, normalized_shape, eps=1e-05):
class LayerNorm(torch.nn.Module):
def forward(self, x):
return torch.ops.aten.layer_norm.default(
x,
torch.tensor([3, 224, 224]),
torch.ones((3, 224, 224)),
torch.zeros((3, 224, 224)),
1e-05,
True,
normalized_shape,
torch.randn(normalized_shape),
torch.randn(normalized_shape),
eps,
)

inputs = [torch.randn(1, 3, 224, 224)]
inputs = [torch.randn(input_shape)]
self.run_test(
LayerNorm(),
inputs,
)


class TestNativeLayerNormConverter(DispatchTestCase):
def test_layer_norm(self):
@parameterized.expand(
[
(
(5, 3, 2, 4),
[
4,
],
),
((5, 3, 2, 4), [2, 4]),
((5, 3, 2, 4), [3, 2, 4]),
((5, 3, 2, 4), [5, 3, 2, 4]),
]
)
def test_layer_norm(self, input_shape, normalized_shape, eps=1e-05):
class LayerNorm(torch.nn.Module):
def forward(self, x):
return torch.ops.aten.native_layer_norm.default(
x,
normalized_shape,
torch.randn(normalized_shape),
torch.randn(normalized_shape),
eps,
)[0]

inputs = [torch.randn(input_shape)]
self.run_test(
LayerNorm(),
inputs,
)

def test_layernorm_with_dynamic_shape(self):
class LayerNorm(torch.nn.Module):
def forward(self, x):
return torch.ops.aten.native_layer_norm.default(
Expand All @@ -37,10 +80,17 @@ def forward(self, x):
1e-05,
)[0]

inputs = [torch.randn(1, 3, 224, 224)]
self.run_test(
input_specs = [
Input(
shape=(-1, 3, 224, 224),
dtype=torch.float32,
shape_ranges=[((1, 3, 224, 224), (5, 3, 224, 224), (10, 3, 224, 224))],
),
]

self.run_test_with_dynamic_shape(
LayerNorm(),
inputs,
input_specs,
)


Expand Down
15 changes: 7 additions & 8 deletions tests/py/dynamo/lowering/test_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,13 @@ def forward(self, x):
optimized_model_results = optimized_model(*inputs).detach().cpu()
torch_model_results = fx_graph(*inputs).detach().cpu()

max_diff = float(
torch.max(torch.abs(optimized_model_results - torch_model_results))
)
self.assertAlmostEqual(
max_diff,
0,
DECIMALS_OF_AGREEMENT,
f"Select_scatter TRT outputs don't match with the original model.",
optimized_model_results_shape = optimized_model_results.size()
torch_model_results_shape = torch_model_results.size()

self.assertEquals(
optimized_model_results_shape,
torch_model_results_shape,
f"The optimized model results shape and torch model results shape should be equal in empty_like",
)


Expand Down
Loading