2022-02-20 23:26:19 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
import os
|
|
|
|
|
import pathlib
|
|
|
|
|
|
2022-04-26 16:35:16 +00:00
|
|
|
import onnx
|
|
|
|
|
|
2022-02-20 23:26:19 +00:00
|
|
|
|
|
|
|
|
def optimize_qdq_model():
|
2022-04-26 16:35:16 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
os.path.basename(__file__),
|
2023-03-30 22:39:43 +00:00
|
|
|
description="Update a QDQ format ONNX model to ensure optimal performance when executed using ONNX Runtime.",
|
2022-04-26 16:35:16 +00:00
|
|
|
)
|
2022-02-20 23:26:19 +00:00
|
|
|
|
2022-04-26 16:35:16 +00:00
|
|
|
parser.add_argument("input_model", type=pathlib.Path, help="Provide path to ONNX model to update.")
|
|
|
|
|
parser.add_argument("output_model", type=pathlib.Path, help="Provide path to write updated ONNX model to.")
|
2022-02-20 23:26:19 +00:00
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
model = onnx.load(str(args.input_model.resolve(strict=True)))
|
|
|
|
|
|
2023-03-30 22:39:43 +00:00
|
|
|
# run QDQ model optimizations here
|
|
|
|
|
|
|
|
|
|
# Originally, the fixing up of DQ nodes with multiple consumers was implemented as one such optimization.
|
|
|
|
|
# That was moved to an ORT graph transformer.
|
|
|
|
|
print("As of ORT 1.15, the fixing up of DQ nodes with multiple consumers is done by an ORT graph transformer.")
|
|
|
|
|
|
|
|
|
|
# There are no optimizations being run currently but we expect that there may be in the future.
|
2022-02-20 23:26:19 +00:00
|
|
|
|
|
|
|
|
onnx.save(model, str(args.output_model.resolve()))
|
|
|
|
|
|
|
|
|
|
|
2022-04-26 16:35:16 +00:00
|
|
|
if __name__ == "__main__":
|
2022-02-20 23:26:19 +00:00
|
|
|
optimize_qdq_model()
|