Skip to content

Commit

Permalink
Use convert_attribute=True by default for onnx.save (#1738)
Browse files Browse the repository at this point in the history
* use convert_attribute=True by default for onnx.save

* leave some constants in the model.onnx
  • Loading branch information
fxmarty authored Mar 1, 2024
1 parent 27deaf6 commit ebb63f8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
6 changes: 5 additions & 1 deletion optimum/exporters/onnx/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@ def fix_dynamic_axes(
dims = onnx_model.graph.output[output_idx].type.tensor_type.shape.dim
dims[dim_idx].dim_value = outputs[output_idx].shape[dim_idx]

onnx.save(onnx_model, model_path.as_posix())
onnx.save(
onnx_model,
model_path.as_posix(),
convert_attribute=True,
)
del onnx_model
gc.collect()

Expand Down
9 changes: 7 additions & 2 deletions optimum/exporters/onnx/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,8 @@ def remap(value):
save_as_external_data=True,
all_tensors_to_one_file=True,
location=output.name + "_data",
size_threshold=1024 if not FORCE_ONNX_EXTERNAL_DATA else 0,
size_threshold=1024 if not FORCE_ONNX_EXTERNAL_DATA else 100,
convert_attribute=True,
)

# delete previous external data
Expand Down Expand Up @@ -688,7 +689,11 @@ def export_tensorflow(

with config.patch_model_for_export(model):
onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature, opset=opset)
onnx.save(onnx_model, output.as_posix())
onnx.save(
onnx_model,
output.as_posix(),
convert_attribute=True,
)

return input_names, output_names

Expand Down
7 changes: 6 additions & 1 deletion optimum/onnx/graph_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ def check_and_save_model(model: onnx.ModelProto, save_path: Optional[Union[str,
# The new model may be below the maximum protobuf size, overwritting a model that was larger. Hence this os.remove.
os.remove(external_path)

onnx.save(model, save_path)
onnx.save(
model,
save_path,
convert_attribute=True,
)
elif save_path is not None:
# path/to/model.onnx
save_path = Path(save_path).as_posix()
Expand All @@ -177,6 +181,7 @@ def check_and_save_model(model: onnx.ModelProto, save_path: Optional[Union[str,
save_as_external_data=True,
all_tensors_to_one_file=True,
location=external_file_name,
convert_attribute=True,
)
try:
onnx.checker.check_model(save_path)
Expand Down
1 change: 1 addition & 0 deletions optimum/onnxruntime/modeling_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ def _from_pretrained(
all_tensors_to_one_file=True,
location=model_cache_path.name + "_data",
size_threshold=0,
convert_attribute=True,
)
del onnx_model

Expand Down
1 change: 1 addition & 0 deletions tests/onnx/test_onnx_graph_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def test_to_int32(self):
save_as_external_data=True,
all_tensors_to_one_file=True,
location=Path(save_path).name + "_data",
convert_attribute=True,
)

onnx.checker.check_model(save_path)
Expand Down

0 comments on commit ebb63f8

Please sign in to comment.