Skip to content

Commit f1bf283

Browse files
committed
feat: Add functionality to only benchmark FX
- Add support for FX in YAML config specification path of `perf_run.py` - Add support for only specifying one of: a PyTorch or TorchScript model - Improve input model validation to throw errors earlier - Remove deprecated option from torchvision model
1 parent 81f2dab commit f1bf283

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

tools/perf/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ There are two sample configuration files added.
6666

6767
| Name | Supported Values | Description |
6868
| ----------------- | ------------------------------------ | ------------------------------------------------------------ |
69-
| backend | all, torch, torch_tensorrt, tensorrt | Supported backends for inference. |
69+
| backend | all, torch, torch_tensorrt, tensorrt, fx2trt | Supported backends for inference. |
7070
| input | - | Input binding names. Expected to list shapes of each input bindings |
7171
| model | - | Configure the model filename and name |
72+
| model_torch | - | Name of torch model file and name (used for fx2trt) (optional) |
7273
| filename | - | Model file name to load from disk. |
7374
| name | - | Model name |
7475
| runtime | - | Runtime configurations |
@@ -83,6 +84,7 @@ backend:
8384
- torch
8485
- torch_tensorrt
8586
- tensorrt
87+
- fx2trt
8688
input:
8789
input0:
8890
- 3
@@ -92,6 +94,9 @@ input:
9294
model:
9395
filename: model.plan
9496
name: vgg16
97+
model_torch:
98+
filename: model_torch.pt
99+
name: vgg16
95100
runtime:
96101
device: 0
97102
precision:
@@ -108,8 +113,9 @@ Note:
108113

109114
Here are the list of `CompileSpec` options that can be provided directly to compile the pytorch module
110115

111-
* `--backends` : Comma separated string of backends. Eg: torch,torch_tensorrt, tensorrt or fx2trt
116+
* `--backends` : Comma separated string of backends. Eg: torch, torch_tensorrt, tensorrt or fx2trt
112117
* `--model` : Name of the model file (Can be a torchscript module or a tensorrt engine (ending in `.plan` extension)). If the backend is `fx2trt`, the input should be a Pytorch module (instead of a torchscript module) and the options for model are (`vgg16` | `resnet50` | `efficientnet_b0`)
118+
* `--model_torch` : Name of the PyTorch model file (optional, only necessary if fx2trt is a chosen backend)
113119
* `--inputs` : List of input shapes & dtypes. Eg: (1, 3, 224, 224)@fp32 for Resnet or (1, 128)@int32;(1, 128)@int32 for BERT
114120
* `--batch_size` : Batch size
115121
* `--precision` : Comma separated list of precisions to build TensorRT engine Eg: fp32,fp16
@@ -122,9 +128,10 @@ Eg:
122128

123129
```
124130
python perf_run.py --model ${MODELS_DIR}/vgg16_scripted.jit.pt \
131+
--model_torch ${MODELS_DIR}/vgg16_torch.pt \
125132
--precision fp32,fp16 --inputs="(1, 3, 224, 224)@fp32" \
126133
--batch_size 1 \
127-
--backends torch,torch_tensorrt,tensorrt \
134+
--backends torch,torch_tensorrt,tensorrt,fx2trt \
128135
--report "vgg_perf_bs1.txt"
129136
```
130137

tools/perf/hub.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626

2727
# Key models selected for benchmarking with their respective paths
2828
BENCHMARK_MODELS = {
29-
"vgg16": {"model": models.vgg16(pretrained=True), "path": ["script", "pytorch"]},
29+
"vgg16": {
30+
"model": models.vgg16(weights=models.VGG16_Weights.DEFAULT),
31+
"path": ["script", "pytorch"],
32+
},
3033
"resnet50": {
3134
"model": models.resnet50(weights=None),
3235
"path": ["script", "pytorch"],

tools/perf/perf_run.py

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ def run(
292292
print("int8 precision expects calibration cache file for inference")
293293
return False
294294

295+
if (model is None) and (backend != "fx2trt"):
296+
warnings.warn(
297+
f"Requested backend {backend} without specifying a TorchScript Model, "
298+
+ "skipping this backend"
299+
)
300+
continue
301+
295302
if backend == "all":
296303
run_torch(model, input_tensors, params, precision, batch_size)
297304
run_torch_tensorrt(
@@ -371,9 +378,14 @@ def recordStats(backend, timings, precision, batch_size=1, compile_time_ms=None)
371378
results.append(stats)
372379

373380

374-
def load_model(params):
381+
def load_ts_model(params):
375382
model = None
376383
is_trt_engine = False
384+
385+
# No TorchScript Model Specified
386+
if len(params.get("model", "")) == 0:
387+
return None, None, is_trt_engine
388+
377389
# Load torch model traced/scripted
378390
model_file = params.get("model").get("filename")
379391
try:
@@ -393,6 +405,26 @@ def load_model(params):
393405
return model, model_name, is_trt_engine
394406

395407

408+
def load_torch_model(params):
409+
model = None
410+
411+
# No Torch Model Specified
412+
if len(params.get("model_torch", "")) == 0:
413+
return None, None
414+
415+
# Load torch model
416+
model_file = params.get("model_torch").get("filename")
417+
try:
418+
model_name = params.get("model_torch").get("name")
419+
except:
420+
model_name = model_file
421+
422+
print("Loading Torch model: ", model_file)
423+
model = torch.load(model_file).cuda()
424+
425+
return model, model_name
426+
427+
396428
if __name__ == "__main__":
397429
arg_parser = argparse.ArgumentParser(
398430
description="Run inference on a model with random input values"
@@ -408,7 +440,9 @@ def load_model(params):
408440
type=str,
409441
help="Comma separated string of backends. Eg: torch,torch_tensorrt,fx2trt,tensorrt",
410442
)
411-
arg_parser.add_argument("--model", type=str, help="Name of torchscript model file")
443+
arg_parser.add_argument(
444+
"--model", type=str, default="", help="Name of torchscript model file"
445+
)
412446
arg_parser.add_argument(
413447
"--model_torch",
414448
type=str,
@@ -458,7 +492,16 @@ def load_model(params):
458492
parser = ConfigParser(args.config)
459493
# Load YAML params
460494
params = parser.read_config()
461-
model, model_name, is_trt_engine = load_model(params)
495+
model, model_name, is_trt_engine = load_ts_model(params)
496+
model_torch, model_name_torch = load_torch_model(params)
497+
498+
# If neither model type was provided
499+
if (model is None) and (model_torch is None):
500+
raise ValueError(
501+
"No valid models specified. Please provide a torchscript model file or model name "
502+
+ "(among the following options vgg16|resnet50|efficientnet_b0|vit) "
503+
+ "or provide a torch model file"
504+
)
462505

463506
# Default device is set to 0. Configurable using yaml config file.
464507
torch.cuda.set_device(params.get("runtime").get("device", 0))
@@ -489,7 +532,10 @@ def load_model(params):
489532

490533
if not is_trt_engine and (precision == "fp16" or precision == "half"):
491534
# If model is TensorRT serialized engine then model.half will report failure
492-
model = model.half()
535+
if model is not None:
536+
model = model.half()
537+
if model_torch is not None:
538+
model_torch = model_torch.half()
493539

494540
backends = params.get("backend")
495541
# Run inference
@@ -502,6 +548,7 @@ def load_model(params):
502548
truncate_long_and_double,
503549
batch_size,
504550
is_trt_engine,
551+
model_torch,
505552
)
506553
else:
507554
params = vars(args)
@@ -511,23 +558,27 @@ def load_model(params):
511558
model_name_torch = params["model_torch"]
512559
model_torch = None
513560

514-
# Load TorchScript model
561+
# Load TorchScript model, if provided
515562
if os.path.exists(model_name):
516563
print("Loading user provided torchscript model: ", model_name)
517564
model = torch.jit.load(model_name).cuda().eval()
518565
elif model_name in BENCHMARK_MODELS:
519566
print("Loading torchscript model from BENCHMARK_MODELS for: ", model_name)
520567
model = BENCHMARK_MODELS[model_name]["model"].eval().cuda()
521-
else:
522-
raise ValueError(
523-
"Invalid model name. Please provide a torchscript model file or model name (among the following options vgg16|resnet50|efficientnet_b0|vit)"
524-
)
525568

526569
# Load PyTorch Model, if provided
527570
if len(model_name_torch) > 0 and os.path.exists(model_name_torch):
528571
print("Loading user provided torch model: ", model_name_torch)
529572
model_torch = torch.load(model_name_torch).eval().cuda()
530573

574+
# If neither model type was provided
575+
if (model is None) and (model_torch is None):
576+
raise ValueError(
577+
"No valid models specified. Please provide a torchscript model file or model name "
578+
+ "(among the following options vgg16|resnet50|efficientnet_b0|vit) "
579+
+ "or provide a torch model file"
580+
)
581+
531582
backends = parse_backends(params["backends"])
532583
truncate_long_and_double = params["truncate"]
533584
batch_size = params["batch_size"]

tools/perf/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import timm
66

77
BENCHMARK_MODELS = {
8-
"vgg16": {"model": models.vgg16(pretrained=True), "path": ["script", "pytorch"]},
8+
"vgg16": {
9+
"model": models.vgg16(weights=models.VGG16_Weights.DEFAULT),
10+
"path": ["script", "pytorch"],
11+
},
912
"resnet50": {
1013
"model": models.resnet50(weights=None),
1114
"path": ["script", "pytorch"],

0 commit comments

Comments
 (0)