-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add model quantization instructions to ONNX documentation (#2570)
- Loading branch information
1 parent
9960861
commit 9b1ba34
Showing
2 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import onnx | ||
from onnxruntime.quantization import quantize_dynamic, QuantType | ||
import argparse | ||
import os | ||
|
||
def quantize_model(onnx_model_path): | ||
base, ext = os.path.splitext(onnx_model_path) | ||
quantized_model_path = f"{base}-8bit{ext}" | ||
|
||
quantize_dynamic( | ||
model_input=onnx_model_path, | ||
model_output=quantized_model_path, | ||
weight_type=QuantType.QInt8, | ||
extra_options={'DefaultTensorType': onnx.TensorProto.FLOAT} | ||
) | ||
|
||
print(f"Quantized model saved to {quantized_model_path}") | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Quantize ONNX model to 8-bit") | ||
parser.add_argument("--model_path", type=str, required=True, help="Path to ONNX model") | ||
args = parser.parse_args() | ||
|
||
quantize_model(args.model_path) |