@@ -114,7 +114,7 @@ def custom_aten_add(input_x, input_y, alpha: float = 1.0):
114
114
{ onnx_registry .is_registered_op (namespace = 'aten' , op_name = 'add' , overload = 'Tensor' )} "
115
115
)
116
116
export_options = torch .onnx .ExportOptions (onnx_registry = onnx_registry )
117
- export_output = torch .onnx .dynamo_export (
117
+ onnx_program = torch .onnx .dynamo_export (
118
118
aten_add_model , input_add_x , input_add_y , export_options = export_options
119
119
)
120
120
@@ -125,14 +125,14 @@ def custom_aten_add(input_x, input_y, alpha: float = 1.0):
125
125
#
126
126
127
127
# graph node domain is the custom domain we registered
128
- assert export_output .model_proto .graph .node [0 ].domain == "custom.aten"
129
- assert len (export_output .model_proto .graph .node ) == 1
128
+ assert onnx_program .model_proto .graph .node [0 ].domain == "custom.aten"
129
+ assert len (onnx_program .model_proto .graph .node ) == 1
130
130
# graph node name is the function name
131
- assert export_output .model_proto .graph .node [0 ].op_type == "custom_aten_add"
131
+ assert onnx_program .model_proto .graph .node [0 ].op_type == "custom_aten_add"
132
132
# function node domain is empty because we use standard ONNX operators
133
- assert export_output .model_proto .functions [0 ].node [3 ].domain == ""
133
+ assert onnx_program .model_proto .functions [0 ].node [3 ].domain == ""
134
134
# function node name is the standard ONNX operator name
135
- assert export_output .model_proto .functions [0 ].node [3 ].op_type == "Add"
135
+ assert onnx_program .model_proto .functions [0 ].node [3 ].op_type == "Add"
136
136
137
137
138
138
######################################################################
@@ -155,20 +155,20 @@ def custom_aten_add(input_x, input_y, alpha: float = 1.0):
155
155
156
156
157
157
# Use ONNX Runtime to run the model, and compare the results with PyTorch
158
- export_output .save ("./custom_add_model.onnx" )
158
+ onnx_program .save ("./custom_add_model.onnx" )
159
159
ort_session = onnxruntime .InferenceSession (
160
160
"./custom_add_model.onnx" , providers = ['CPUExecutionProvider' ]
161
161
)
162
162
163
163
def to_numpy (tensor ):
164
164
return tensor .detach ().cpu ().numpy () if tensor .requires_grad else tensor .cpu ().numpy ()
165
165
166
- onnx_input = export_output .adapt_torch_inputs_to_onnx (input_add_x , input_add_y )
166
+ onnx_input = onnx_program .adapt_torch_inputs_to_onnx (input_add_x , input_add_y )
167
167
onnxruntime_input = {k .name : to_numpy (v ) for k , v in zip (ort_session .get_inputs (), onnx_input )}
168
168
onnxruntime_outputs = ort_session .run (None , onnxruntime_input )
169
169
170
170
torch_outputs = aten_add_model (input_add_x , input_add_y )
171
- torch_outputs = export_output .adapt_torch_outputs_to_onnx (torch_outputs )
171
+ torch_outputs = onnx_program .adapt_torch_outputs_to_onnx (torch_outputs )
172
172
173
173
assert len (torch_outputs ) == len (onnxruntime_outputs )
174
174
for torch_output , onnxruntime_output in zip (torch_outputs , onnxruntime_outputs ):
@@ -225,7 +225,7 @@ def custom_aten_gelu(input_x, approximate: str = "none"):
225
225
aten_gelu_model = CustomGelu ()
226
226
input_gelu_x = torch .randn (3 , 3 )
227
227
228
- export_output = torch .onnx .dynamo_export (
228
+ onnx_program = torch .onnx .dynamo_export (
229
229
aten_gelu_model , input_gelu_x , export_options = export_options
230
230
)
231
231
@@ -238,13 +238,13 @@ def custom_aten_gelu(input_x, approximate: str = "none"):
238
238
#
239
239
240
240
# graph node domain is the custom domain we registered
241
- assert export_output .model_proto .graph .node [0 ].domain == "com.microsoft"
241
+ assert onnx_program .model_proto .graph .node [0 ].domain == "com.microsoft"
242
242
# graph node name is the function name
243
- assert export_output .model_proto .graph .node [0 ].op_type == "custom_aten_gelu"
243
+ assert onnx_program .model_proto .graph .node [0 ].op_type == "custom_aten_gelu"
244
244
# function node domain is the custom domain we registered
245
- assert export_output .model_proto .functions [0 ].node [0 ].domain == "com.microsoft"
245
+ assert onnx_program .model_proto .functions [0 ].node [0 ].domain == "com.microsoft"
246
246
# function node name is the node name used in the function
247
- assert export_output .model_proto .functions [0 ].node [0 ].op_type == "Gelu"
247
+ assert onnx_program .model_proto .functions [0 ].node [0 ].op_type == "Gelu"
248
248
249
249
250
250
######################################################################
@@ -263,20 +263,20 @@ def custom_aten_gelu(input_x, approximate: str = "none"):
263
263
# and compare the results with PyTorch.
264
264
#
265
265
266
- export_output .save ("./custom_gelu_model.onnx" )
266
+ onnx_program .save ("./custom_gelu_model.onnx" )
267
267
ort_session = onnxruntime .InferenceSession (
268
268
"./custom_gelu_model.onnx" , providers = ['CPUExecutionProvider' ]
269
269
)
270
270
271
271
def to_numpy (tensor ):
272
272
return tensor .detach ().cpu ().numpy () if tensor .requires_grad else tensor .cpu ().numpy ()
273
273
274
- onnx_input = export_output .adapt_torch_inputs_to_onnx (input_gelu_x )
274
+ onnx_input = onnx_program .adapt_torch_inputs_to_onnx (input_gelu_x )
275
275
onnxruntime_input = {k .name : to_numpy (v ) for k , v in zip (ort_session .get_inputs (), onnx_input )}
276
276
onnxruntime_outputs = ort_session .run (None , onnxruntime_input )
277
277
278
278
torch_outputs = aten_gelu_model (input_gelu_x )
279
- torch_outputs = export_output .adapt_torch_outputs_to_onnx (torch_outputs )
279
+ torch_outputs = onnx_program .adapt_torch_outputs_to_onnx (torch_outputs )
280
280
281
281
assert len (torch_outputs ) == len (onnxruntime_outputs )
282
282
for torch_output , onnxruntime_output in zip (torch_outputs , onnxruntime_outputs ):
@@ -365,24 +365,24 @@ def custom_addandround(input_x):
365
365
)
366
366
367
367
export_options = torch .onnx .ExportOptions (onnx_registry = onnx_registry )
368
- export_output = torch .onnx .dynamo_export (
368
+ onnx_program = torch .onnx .dynamo_export (
369
369
custom_addandround_model , input_addandround_x , export_options = export_options
370
370
)
371
- export_output .save ("./custom_addandround_model.onnx" )
371
+ onnx_program .save ("./custom_addandround_model.onnx" )
372
372
373
373
374
374
######################################################################
375
- # The ``export_output `` exposes the exported model as protobuf through ``export_output .model_proto``.
375
+ # The ``onnx_program `` exposes the exported model as protobuf through ``onnx_program .model_proto``.
376
376
# The graph has one graph nodes for ``custom_addandround``, and inside ``custom_addandround``,
377
377
# there are two function nodes, one for each operator.
378
378
#
379
379
380
- assert export_output .model_proto .graph .node [0 ].domain == "test.customop"
381
- assert export_output .model_proto .graph .node [0 ].op_type == "custom_addandround"
382
- assert export_output .model_proto .functions [0 ].node [0 ].domain == "test.customop"
383
- assert export_output .model_proto .functions [0 ].node [0 ].op_type == "CustomOpOne"
384
- assert export_output .model_proto .functions [0 ].node [1 ].domain == "test.customop"
385
- assert export_output .model_proto .functions [0 ].node [1 ].op_type == "CustomOpTwo"
380
+ assert onnx_program .model_proto .graph .node [0 ].domain == "test.customop"
381
+ assert onnx_program .model_proto .graph .node [0 ].op_type == "custom_addandround"
382
+ assert onnx_program .model_proto .functions [0 ].node [0 ].domain == "test.customop"
383
+ assert onnx_program .model_proto .functions [0 ].node [0 ].op_type == "CustomOpOne"
384
+ assert onnx_program .model_proto .functions [0 ].node [1 ].domain == "test.customop"
385
+ assert onnx_program .model_proto .functions [0 ].node [1 ].op_type == "CustomOpTwo"
386
386
387
387
388
388
######################################################################
@@ -432,12 +432,12 @@ def custom_addandround(input_x):
432
432
# def to_numpy(tensor):
433
433
# return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
434
434
#
435
- # onnx_input = export_output .adapt_torch_inputs_to_onnx(input_addandround_x)
435
+ # onnx_input = onnx_program .adapt_torch_inputs_to_onnx(input_addandround_x)
436
436
# onnxruntime_input = {k.name: to_numpy(v) for k, v in zip(ort_session.get_inputs(), onnx_input)}
437
437
# onnxruntime_outputs = ort_session.run(None, onnxruntime_input)
438
438
#
439
439
# torch_outputs = custom_addandround_model(input_addandround_x)
440
- # torch_outputs = export_output .adapt_torch_outputs_to_onnx(torch_outputs)
440
+ # torch_outputs = onnx_program .adapt_torch_outputs_to_onnx(torch_outputs)
441
441
#
442
442
# assert len(torch_outputs) == len(onnxruntime_outputs)
443
443
# for torch_output, onnxruntime_output in zip(torch_outputs, onnxruntime_outputs):
0 commit comments