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

switch from fx.symbolic_trace to dynamo_trace for converter test part-1 #3261

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 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
b4e02e1
Merge branch 'main' into lluo/save_remove_inputs
lanluo-nvidia Oct 11, 2024
3d94f8b
test
lanluo-nvidia Oct 13, 2024
28ba6cc
Merge branch 'main' into lluo/save_remove_inputs
lanluo-nvidia Oct 15, 2024
b89cbe0
resolve comments
lanluo-nvidia Oct 15, 2024
2843d37
Merge branch 'main' into lluo/save_remove_inputs
lanluo-nvidia Oct 16, 2024
3eb48d7
test
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
891e963
enable converter non dynamic shape tests to use dynamo tracer
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
814262f
Merge branch 'lluo/save_remove_inputs' into lluo/switch_to_dynamo_trace
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
d8e52bf
test
lanluo-nvidia Oct 27, 2024
4d46235
Merge branch 'lluo/save_remove_inputs' into lluo/switch_to_dynamo_trace
lanluo-nvidia Oct 27, 2024
8b3842a
test
lanluo-nvidia Oct 27, 2024
7ddf56f
test
lanluo-nvidia Oct 27, 2024
39e0a49
test
lanluo-nvidia Oct 27, 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
7a9659f
Merge branch 'lluo/save_remove_inputs' into lluo/switch_to_dynamo_trace
lanluo-nvidia Oct 29, 2024
cb656bb
Merge branch 'main' into lluo/switch_to_dynamo_trace
lanluo-nvidia Oct 29, 2024
c023714
resolve comments
lanluo-nvidia Oct 30, 2024
594ca28
resolve comments
lanluo-nvidia Oct 30, 2024
56d034b
resolve comments
lanluo-nvidia Oct 31, 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
test
  • Loading branch information
lanluo-nvidia committed Oct 27, 2024
commit 7ddf56f27b9886c91b3c1c4de91d4b864283fc47
46 changes: 23 additions & 23 deletions tests/py/dynamo/conversion/test_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,26 @@ def forward(self, query, key, value):
enable_passes=True,
)

@unittest.skip("need to change to custom dynamic shapes")
@parameterized.expand(
[
# (
# "4d-2d",
# (4, 2, 16, 32),
# (6, 3, 32, 64),
# (32, 8, 64, 128),
# (4, 32),
# (4, 64),
# (16, 128),
# ),
# (
# "4d-3d",
# (2, 2, 2, 2),
# (3, 3, 3, 4),
# (3, 4, 4, 5),
# (2, 3, 2),
# (3, 3, 4),
# (4, 5, 5),
# ),
(
"4d-2d",
(4, 2, 16, 32),
(6, 3, 32, 64),
(32, 8, 64, 128),
(4, 32),
(4, 64),
(16, 128),
),
(
"4d-3d",
(2, 2, 2, 2),
(3, 3, 3, 4),
(3, 4, 4, 5),
(2, 3, 2),
(3, 3, 4),
(4, 5, 5),
),
(
"4d-4d",
(4, 2, 12, 4),
Expand All @@ -66,6 +65,7 @@ def forward(self, query, key, value):
),
]
)
@unittest.skip("need to change to custom dynamic shapes")
def test_sdpa_no_causal_dynamic_shape_with_scale(
self,
_,
Expand Down Expand Up @@ -146,7 +146,6 @@ def forward(self, query, key, value):
enable_passes=True,
)

@unittest.skip("need to change to custom dynamic shapes")
@parameterized.expand(
[
(
Expand All @@ -169,6 +168,7 @@ def forward(self, query, key, value):
),
]
)
@unittest.skip("need to change to custom dynamic shapes")
def test_sdpa_no_causal_no_scale_dynamic_shape(
self,
_,
Expand Down Expand Up @@ -217,7 +217,6 @@ def forward(self, query, key, value):

self.run_test_with_dynamic_shape(SDPA(), inputs)

@unittest.skip("need to change to custom dynamic shapes")
@parameterized.expand(
[
(
Expand Down Expand Up @@ -252,6 +251,7 @@ def forward(self, query, key, value):
),
]
)
@unittest.skip("need to change to custom dynamic shapes")
def test_sdpa_causal_dynamic_shape(
self,
_,
Expand Down Expand Up @@ -295,11 +295,11 @@ def forward(self, query, key, value):

self.run_test_with_dynamic_shape(SDPA(), inputs)

# it is already added in the integration test
@parameterized.expand([((32, 8, 128, 64), (32, 8, 128, 64))])
# it is already added in the test_composite_aten_op.py as integration test
@unittest.skip(
"skip torch.nn.functional.scaled_dot_product_attention converter test"
)
@parameterized.expand([((32, 8, 128, 64), (32, 8, 128, 64))])
def test_sdpa_causal(self, query_shape, key_shape):
class SDPA(nn.Module):
def forward(self, query, key, value):
Expand Down
194 changes: 194 additions & 0 deletions tests/py/dynamo/conversion/test_composite_aten_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import unittest

import pytest
import torch
import torch.nn as nn
import torch_tensorrt as torchtrt
from parameterized import parameterized
from torch.export import Dim
from torch_tensorrt import Input
from torch_tensorrt.dynamo.utils import COSINE_THRESHOLD, cosine_similarity

assertions = unittest.TestCase()


@pytest.mark.unit
@parameterized.expand(
[
((5,), (5,)),
(
(
2,
3,
),
(
2,
3,
),
),
]
)
def test_atan2_out_static_shape(input_shape, out_shape):
class atan2(torch.nn.Module):
def forward(self, lhs_val, rhs_val, out):
return torch.ops.aten.atan2.out(lhs_val, rhs_val, out=out)

model = atan2().eval().cuda()
inputs = (
torch.randn(input_shape).cuda(),
torch.randn(input_shape).cuda(),
torch.randn(out_shape).cuda(),
)
compile_spec = {
"inputs": inputs,
"ir": "dynamo",
"min_block_size": 1,
}

trt_model = torchtrt.compile(model, **compile_spec)
py_outputs = model(*inputs)
trt_outputs = trt_model(*inputs)
cos_sim = cosine_similarity(py_outputs, trt_outputs)
assertions.assertTrue(
cos_sim > COSINE_THRESHOLD,
msg=f"test_atan2_out_static_shape model TRT outputs don't match with the pytorch model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
)


@pytest.mark.unit
@parameterized.expand(
[
(
(
1,
2,
),
(2, 3),
(2, 4),
),
]
)
def test_atan2_out_dynamic_shape(min_shape, opt_shape, max_shape):
class atan2(torch.nn.Module):
def forward(self, lhs_val, rhs_val, out):
return torch.ops.aten.atan2.out(lhs_val, rhs_val, out=out)

model = atan2().eval().cuda()
input_spec = [
Input(
min_shape=min_shape,
opt_shape=opt_shape,
max_shape=max_shape,
),
Input(
min_shape=min_shape,
opt_shape=opt_shape,
max_shape=max_shape,
),
Input(
min_shape=min_shape,
opt_shape=opt_shape,
max_shape=max_shape,
),
]

compile_spec = {
"inputs": input_spec,
"ir": "dynamo",
"min_block_size": 1,
}

trt_model = torchtrt.compile(model, **compile_spec)
inputs = (
torch.randn(max_shape).cuda(),
torch.randn(max_shape).cuda(),
torch.randn(max_shape).cuda(),
)
py_outputs = model(*inputs)
trt_outputs = trt_model(*inputs)
cos_sim = cosine_similarity(py_outputs, trt_outputs)
assertions.assertTrue(
cos_sim > COSINE_THRESHOLD,
msg=f"test_atan2_out_dynamic_shape model TRT outputs don't match with the pytorch model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
)


@parameterized.expand(
[
((32, 8, 128, 64), (32, 8, 128, 64), True, None),
((32, 32, 128, 64), (32, 8, 128, 64), True, 0.1),
]
)
def test_sdpa_static_shape(query_shape, key_shape, is_causal, scale):
class SDPA(nn.Module):
def forward(self, query, key, value):
return torch.nn.functional.scaled_dot_product_attention(
query, key, value, None, 0.0, is_causal=is_causal, scale=scale
)

model = SDPA().eval().cuda()

query = torch.randn(query_shape, dtype=torch.float16).cuda()
key = torch.randn(key_shape, dtype=torch.float16).cuda()
value = torch.randn(key_shape, dtype=torch.float16).cuda()
inputs = (query, key, value)
compile_spec = {
"inputs": inputs,
"ir": "dynamo",
"min_block_size": 1,
}

trt_model = torchtrt.compile(model, **compile_spec)
py_outputs = model(*inputs)
trt_outputs = trt_model(*inputs)
cos_sim = cosine_similarity(py_outputs, trt_outputs)
assertions.assertTrue(
cos_sim > COSINE_THRESHOLD,
msg=f"test_sdpa_static_shape model TRT outputs don't match with the pytorch model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
)


@parameterized.expand(
[
(True, None),
(True, 0.1),
(False, None),
]
)
def test_sdpa_dynamic_shape(is_causal, scale):
class SDPA(nn.Module):
def forward(self, query, key, value):
return torch.nn.functional.scaled_dot_product_attention(
query, key, value, None, 0.0, is_causal=is_causal, scale=scale
)

model = SDPA().eval().cuda()

# N: batch_size
dyn_N = Dim("dyn_N", min=2, max=4)

# query tensor shape (N, ..., Hq, L, E)
query = torch.randn((3, 3, 4, 64), dtype=torch.float16).cuda()
# key tensor shape (N,...,H, S, E)
key = torch.randn((3, 3, 4, 64), dtype=torch.float16).cuda()
# value tensor shape (N, ..., H, S, Ev)
value = torch.randn((3, 3, 4, 64), dtype=torch.float16).cuda()

dynamic_shapes = {"query": {0: dyn_N}, "key": {0: dyn_N}, "value": {0: dyn_N}}
inputs = (query, key, value)

exp_program = torch.export.export(model, inputs, dynamic_shapes=dynamic_shapes)

compile_spec = {
"inputs": inputs,
"ir": "dynamo",
"min_block_size": 1,
}
trt_model = torchtrt.dynamo.compile(exp_program, **compile_spec)
py_outputs = model(*inputs)
trt_outputs = trt_model(*inputs)
cos_sim = cosine_similarity(py_outputs, trt_outputs)
assertions.assertTrue(
cos_sim > COSINE_THRESHOLD,
msg=f"test_sdpa_dynamic_shape model TRT outputs don't match with the pytorch model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
)
Loading