11
11
import torchvision .models as models
12
12
from torch import nn
13
13
from torch_tensorrt .dynamo ._compiler import convert_module_to_trt_engine
14
- from torch_tensorrt .dynamo .utils import COSINE_THRESHOLD , cosine_similarity
14
+ from torch_tensorrt .dynamo .utils import (
15
+ COSINE_THRESHOLD ,
16
+ cosine_similarity ,
17
+ prepare_inputs ,
18
+ )
15
19
16
20
assertions = unittest .TestCase ()
17
21
18
22
23
+ # @pytest.mark.unit
24
+ # def test_custom_model():
25
+ # class net(nn.Module):
26
+ # def __init__(self):
27
+ # super().__init__()
28
+ # self.conv1 = nn.Conv2d(3, 12, 3, padding=1)
29
+ # self.bn = nn.BatchNorm2d(12)
30
+ # self.conv2 = nn.Conv2d(12, 12, 3, padding=1)
31
+ # self.fc1 = nn.Linear(12 * 56 * 56, 10)
32
+
33
+ # def forward(self, x, b=5, c=None, d=None):
34
+ # x = self.conv1(x)
35
+ # x = F.relu(x)
36
+ # x = self.bn(x)
37
+ # x = F.max_pool2d(x, (2, 2))
38
+ # x = self.conv2(x)
39
+ # x = F.relu(x)
40
+ # x = F.max_pool2d(x, (2, 2))
41
+ # x = torch.flatten(x, 1)
42
+ # x = x + b
43
+ # if c is not None:
44
+ # x = x * c
45
+ # if d is not None:
46
+ # x = x - d["value"]
47
+ # return self.fc1(x)
48
+
49
+ # model = net().eval().to("cuda")
50
+ # args = [torch.rand((1, 3, 224, 224)).to("cuda")]
51
+ # kwargs = {
52
+ # "b": torch.tensor(6).to("cuda"),
53
+ # "d": {"value": torch.tensor(8).to("cuda")},
54
+ # }
55
+
56
+ # compile_spec = {
57
+ # "inputs": args,
58
+ # "kwarg_inputs": kwargs,
59
+ # "device": torchtrt.Device("cuda:0"),
60
+ # "enabled_precisions": {torch.float},
61
+ # "pass_through_build_failures": True,
62
+ # "optimization_level": 1,
63
+ # "min_block_size": 1,
64
+ # "ir": "dynamo",
65
+ # }
66
+
67
+ # exp_program = torch.export.export(model, args=tuple(args), kwargs=kwargs)
68
+ # trt_gm = torchtrt.dynamo.compile(exp_program, **compile_spec)
69
+ # cos_sim = cosine_similarity(model(*args, **kwargs), trt_gm(*args, **kwargs)[0])
70
+ # assertions.assertTrue(
71
+ # cos_sim > COSINE_THRESHOLD,
72
+ # msg=f"CustomKwargs Module TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
73
+ # )
74
+
75
+ # # Save the module
76
+ # trt_ep_path = os.path.join(tempfile.gettempdir(), "compiled.ep")
77
+ # torchtrt.save(trt_gm, trt_ep_path, inputs=args, kwargs_inputs=kwargs)
78
+ # # Clean up model env
79
+ # torch._dynamo.reset()
80
+
81
+
82
+ # @pytest.mark.unit
83
+ # def test_custom_model_with_dynamo_trace():
84
+ # class net(nn.Module):
85
+ # def __init__(self):
86
+ # super().__init__()
87
+ # self.conv1 = nn.Conv2d(3, 12, 3, padding=1)
88
+ # self.bn = nn.BatchNorm2d(12)
89
+ # self.conv2 = nn.Conv2d(12, 12, 3, padding=1)
90
+ # self.fc1 = nn.Linear(12 * 56 * 56, 10)
91
+
92
+ # def forward(self, x, b=5, c=None, d=None):
93
+ # x = self.conv1(x)
94
+ # x = F.relu(x)
95
+ # x = self.bn(x)
96
+ # x = F.max_pool2d(x, (2, 2))
97
+ # x = self.conv2(x)
98
+ # x = F.relu(x)
99
+ # x = F.max_pool2d(x, (2, 2))
100
+ # x = torch.flatten(x, 1)
101
+ # x = x + b
102
+ # if c is not None:
103
+ # x = x * c
104
+ # if d is not None:
105
+ # x = x - d["value"]
106
+ # return self.fc1(x)
107
+
108
+ # model = net().eval().to("cuda")
109
+ # args = [torch.rand((1, 3, 224, 224)).to("cuda")]
110
+ # kwargs = {
111
+ # "b": torch.tensor(6).to("cuda"),
112
+ # "d": {"value": torch.tensor(8).to("cuda")},
113
+ # }
114
+
115
+ # compile_spec = {
116
+ # "inputs": prepare_inputs(args),
117
+ # "kwarg_inputs": prepare_inputs(kwargs),
118
+ # "device": torchtrt.Device("cuda:0"),
119
+ # "enabled_precisions": {torch.float},
120
+ # "pass_through_build_failures": True,
121
+ # "optimization_level": 1,
122
+ # "min_block_size": 1,
123
+ # "ir": "dynamo",
124
+ # }
125
+
126
+ # exp_program = torchtrt.dynamo.trace(model, **compile_spec)
127
+ # trt_gm = torchtrt.dynamo.compile(exp_program, **compile_spec)
128
+ # cos_sim = cosine_similarity(model(*args, **kwargs), trt_gm(*args, **kwargs)[0])
129
+ # assertions.assertTrue(
130
+ # cos_sim > COSINE_THRESHOLD,
131
+ # msg=f"CustomKwargs Module TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
132
+ # )
133
+
134
+ # # Save the module
135
+ # trt_ep_path = os.path.join(tempfile.gettempdir(), "compiled.ep")
136
+ # torchtrt.save(trt_gm, trt_ep_path, inputs=args, kwargs_inputs=kwargs)
137
+ # # Clean up model env
138
+ # torch._dynamo.reset()
139
+
140
+
19
141
@pytest .mark .unit
20
- def test_custom_model ():
142
+ def test_custom_model_with_dynamo_trace_dynamic ():
21
143
class net (nn .Module ):
22
144
def __init__ (self ):
23
145
super ().__init__ ()
@@ -50,8 +172,17 @@ def forward(self, x, b=5, c=None, d=None):
50
172
}
51
173
52
174
compile_spec = {
53
- "inputs" : args ,
54
- "kwarg_inputs" : kwargs ,
175
+ # "arg_inputs": prepare_inputs(args),
176
+ "inputs" : [
177
+ torchtrt .Input (
178
+ min_shape = (1 , 3 , 224 , 224 ),
179
+ opt_shape = (4 , 3 , 224 , 224 ),
180
+ max_shape = (8 , 3 , 224 , 224 ),
181
+ dtype = torch .float32 ,
182
+ name = "x" ,
183
+ )
184
+ ],
185
+ "kwarg_inputs" : prepare_inputs (kwargs ),
55
186
"device" : torchtrt .Device ("cuda:0" ),
56
187
"enabled_precisions" : {torch .float },
57
188
"pass_through_build_failures" : True ,
@@ -60,7 +191,88 @@ def forward(self, x, b=5, c=None, d=None):
60
191
"ir" : "dynamo" ,
61
192
}
62
193
63
- exp_program = torch .export .export (model , args = tuple (args ), kwargs = kwargs )
194
+ exp_program = torchtrt .dynamo .trace (model , ** compile_spec )
195
+ trt_gm = torchtrt .dynamo .compile (exp_program , ** compile_spec )
196
+ cos_sim = cosine_similarity (model (* args , ** kwargs ), trt_gm (* args , ** kwargs )[0 ])
197
+ assertions .assertTrue (
198
+ cos_sim > COSINE_THRESHOLD ,
199
+ msg = f"CustomKwargs Module TRT outputs don't match with the original model. Cosine sim score: { cos_sim } Threshold: { COSINE_THRESHOLD } " ,
200
+ )
201
+
202
+ # Save the module
203
+ trt_ep_path = os .path .join (tempfile .gettempdir (), "compiled.ep" )
204
+ torchtrt .save (trt_gm , trt_ep_path , inputs = args , kwargs_inputs = kwargs )
205
+ # Clean up model env
206
+ torch ._dynamo .reset ()
207
+
208
+
209
+ @pytest .mark .unit
210
+ def test_custom_model_with_dynamo_trace_dynamic_complex ():
211
+ ir = "dynamo"
212
+
213
+ class net (nn .Module ):
214
+ def __init__ (self ):
215
+ super ().__init__ ()
216
+ self .conv1 = nn .Conv2d (3 , 12 , 3 , padding = 1 )
217
+ self .bn = nn .BatchNorm2d (12 )
218
+ self .conv2 = nn .Conv2d (12 , 12 , 3 , padding = 1 )
219
+ self .fc1 = nn .Linear (12 * 56 * 56 , 10 )
220
+
221
+ def forward (self , x , b = None , c = None , d = None , e = []):
222
+ x = self .conv1 (x )
223
+ x = F .relu (x )
224
+ x = self .bn (x )
225
+ x = F .max_pool2d (x , (2 , 2 ))
226
+ x = self .conv2 (x )
227
+ x = F .relu (x )
228
+ x = F .max_pool2d (x , (2 , 2 ))
229
+ x = torch .flatten (x , 1 )
230
+ x = x @ b
231
+ if c is not None :
232
+ x = x * c
233
+ if d is not None :
234
+ x = x - d ["value" ]
235
+ for n in e :
236
+ x += n
237
+ return x
238
+
239
+ model = net ().eval ().to ("cuda" )
240
+ args = [torch .rand ((1 , 3 , 224 , 224 )).to ("cuda" )]
241
+ kwargs = {
242
+ "b" : torch .rand ((37632 , 10 )).to ("cuda" ),
243
+ "d" : {"value" : torch .tensor (8 ).to ("cuda" )},
244
+ "e" : [torch .tensor (8 ).to ("cuda" ), torch .tensor (10 ).to ("cuda" )],
245
+ }
246
+ model (* args , ** kwargs )
247
+ kwarg_torchtrt_input = prepare_inputs (kwargs )
248
+ kwarg_torchtrt_input ["b" ] = torchtrt .Input (
249
+ min_shape = (37632 , 1 ),
250
+ opt_shape = (37632 , 5 ),
251
+ max_shape = (37632 , 10 ),
252
+ dtype = torch .float32 ,
253
+ name = "b" ,
254
+ )
255
+ compile_spec = {
256
+ # "arg_inputs": prepare_inputs(args),
257
+ "inputs" : [
258
+ torchtrt .Input (
259
+ min_shape = (1 , 3 , 224 , 224 ),
260
+ opt_shape = (4 , 3 , 224 , 224 ),
261
+ max_shape = (8 , 3 , 224 , 224 ),
262
+ dtype = torch .float32 ,
263
+ name = "x" ,
264
+ ),
265
+ ],
266
+ "kwarg_inputs" : kwarg_torchtrt_input ,
267
+ "device" : torchtrt .Device ("cuda:0" ),
268
+ "enabled_precisions" : {torch .float },
269
+ "pass_through_build_failures" : True ,
270
+ "optimization_level" : 1 ,
271
+ "min_block_size" : 1 ,
272
+ "ir" : "dynamo" ,
273
+ }
274
+
275
+ exp_program = torchtrt .dynamo .trace (model , ** compile_spec )
64
276
trt_gm = torchtrt .dynamo .compile (exp_program , ** compile_spec )
65
277
cos_sim = cosine_similarity (model (* args , ** kwargs ), trt_gm (* args , ** kwargs )[0 ])
66
278
assertions .assertTrue (
0 commit comments