@@ -51,7 +51,7 @@ def print_predictions(scores: torch.Tensor):
51
51
print (f" { label_desc } ({ prob :.2f} )" )
52
52
53
53
54
- def export_timm_model (config : str , onnx_path : str ):
54
+ def export_timm_model (config : str , onnx_path : str , dynamo : bool = False ):
55
55
"""
56
56
Export a PyTorch model from timm to ONNX.
57
57
@@ -89,16 +89,13 @@ def export_timm_model(config: str, onnx_path: str):
89
89
print_predictions (output )
90
90
91
91
print (f"Exporting model to { onnx_path } " )
92
- torch .onnx .export (model , input_img , onnx_path )
92
+ torch .onnx .export (model , input_img , onnx_path , dynamo = dynamo )
93
93
94
94
# Test exported model with ONNX Runtime as a reference implementation.
95
95
#
96
96
# We test both with graph optimizations disabled and enabled, to show the
97
97
# impact of running the ONNX model "as is" vs. with the various fusions that
98
98
# ONNX Runtime does.
99
- #
100
- # RTen currently doesn't do any fusions, so the unoptimized performance
101
- # is a "fairer" comparison.
102
99
print (f"Testing model with ONNX Runtime (unoptimized)..." )
103
100
sess_options = ort .SessionOptions ()
104
101
sess_options .graph_optimization_level = ort .GraphOptimizationLevel .ORT_DISABLE_ALL
@@ -145,14 +142,20 @@ def main():
145
142
help = "Name of the model configuration or Hugging Face model URL or path" ,
146
143
)
147
144
parser .add_argument ("onnx_path" , nargs = "?" , help = "Path to ONNX file" )
145
+ parser .add_argument (
146
+ "-d" ,
147
+ "--dynamo" ,
148
+ action = "store_true" ,
149
+ help = "Use PyTorch's newer TorchDynamo-based ONNX exporter" ,
150
+ )
148
151
args = parser .parse_args ()
149
152
150
153
config_name = extract_config_name (args .model_config )
151
154
onnx_path = args .onnx_path
152
155
if onnx_path is None :
153
156
onnx_path = config_name + ".onnx"
154
157
155
- export_timm_model (config_name , onnx_path )
158
+ export_timm_model (config_name , onnx_path , dynamo = args . dynamo )
156
159
157
160
158
161
if __name__ == "__main__" :
0 commit comments