Skip to content

Commit f38eb76

Browse files
committed
chore: Add dynamo support
Signed-off-by: Dheeraj Peri <[email protected]>
1 parent e1c7abf commit f38eb76

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

tools/perf/perf_run.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ def run(
274274
truncate_long_and_double=False,
275275
batch_size=1,
276276
is_trt_engine=False,
277+
use_dynamo=False,
277278
model_torch=None,
278279
):
279280
for backend in backends:
@@ -306,7 +307,7 @@ def run(
306307
)
307308
continue
308309

309-
if backend == "all":
310+
if backend == "all" and not use_dynamo:
310311
run_torch(model, input_tensors, params, precision, batch_size)
311312
run_torch_tensorrt(
312313
model,
@@ -327,7 +328,7 @@ def run(
327328
)
328329
run_fx2trt(model_torch, input_tensors, params, precision, batch_size)
329330

330-
elif backend == "torchscript":
331+
elif backend == "torchscript" and not use_dynamo:
331332
run_torch(model, input_tensors, params, precision, batch_size)
332333
run_torch_tensorrt(
333334
model,
@@ -347,10 +348,10 @@ def run(
347348
batch_size,
348349
)
349350

350-
elif backend == "torch":
351+
elif backend == "torch" and not use_dynamo:
351352
run_torch(model, input_tensors, params, precision, batch_size)
352353

353-
elif backend == "torch_tensorrt":
354+
elif backend == "torch_tensorrt" and not use_dynamo:
354355
run_torch_tensorrt(
355356
model,
356357
input_tensors,
@@ -499,6 +500,17 @@ def load_torch_model(params):
499500
action="store_true",
500501
help="Boolean flag to determine if the user provided model is a TRT engine or not",
501502
)
503+
arg_parser.add_argument(
504+
"--dynamo",
505+
action="store_true",
506+
help="Boolean flag to determine if the user provided model should be compiled with torch._dynamo",
507+
)
508+
arg_parser.add_argument(
509+
"--dynamo_backend",
510+
type=str,
511+
default="inductor",
512+
help="List of backends to use in Torchdynamo. Select options: inductor|fx2trt",
513+
)
502514
arg_parser.add_argument(
503515
"--report",
504516
type=str,
@@ -579,6 +591,8 @@ def load_torch_model(params):
579591

580592
model_name_torch = params["model_torch"]
581593
model_torch = None
594+
use_dynamo = params["dynamo"]
595+
dynamo_backend = params["dynamo_backend"]
582596

583597
# Load TorchScript model, if provided
584598
if os.path.exists(model_name):
@@ -601,6 +615,12 @@ def load_torch_model(params):
601615
+ "or provide a torch model file"
602616
)
603617

618+
if use_dynamo and (model_torch is None):
619+
raise ValueError("No Pytorch model (nn.Module) is provided for torchdynamo compilation. Please provide a pytorch model")
620+
621+
if use_dynamo and model_torch:
622+
model_torch = torch.compile(model_torch, "default", dynamic=False, fullgraph=False, backend=dynamo_backend)
623+
604624
backends = parse_backends(params["backends"])
605625
truncate_long_and_double = params["truncate"]
606626
batch_size = params["batch_size"]
@@ -623,6 +643,7 @@ def load_torch_model(params):
623643
truncate_long_and_double,
624644
batch_size,
625645
is_trt_engine,
646+
use_dynamo,
626647
model_torch=model_torch,
627648
)
628649

0 commit comments

Comments
 (0)