Download tools/quantize.py from KitsuMate/uni2005-onnx: direct link, hf CLI and curl.
- Browser
- Download file 898 Bytes
-
https://huggingface.co/KitsuMate/uni2005-onnx/resolve/main/tools/quantize.py
- Command line
-
hf download hf://KitsuMate/uni2005-onnx/tools/quantize.py
-
curl -L -o quantize.py https://huggingface.co/KitsuMate/uni2005-onnx/resolve/main/tools/quantize.py
898 Bytes
| """Rebuild the published dynamic 8-bit Uni2005 ONNX variants.""" | |
| from pathlib import Path | |
| import onnx | |
| from onnxconverter_common import float16 | |
| from onnxruntime.quantization import QuantType, quantize_dynamic | |
| ROOT = Path(__file__).resolve().parents[1] | |
| SOURCE = ROOT / "onnx" / "model_fp32.onnx" | |
| def quantize(output_name: str, weight_type: QuantType) -> None: | |
| quantize_dynamic( | |
| model_input=SOURCE, | |
| model_output=ROOT / "onnx" / output_name, | |
| op_types_to_quantize=["LSTM", "MatMul"], | |
| weight_type=weight_type, | |
| ) | |
| if __name__ == "__main__": | |
| fp32 = onnx.load(SOURCE) | |
| fp16 = float16.convert_float_to_float16( | |
| fp32, | |
| keep_io_types=True, | |
| node_block_list=["/Cast"], | |
| ) | |
| onnx.save(fp16, ROOT / "onnx" / "model_fp16.onnx") | |
| quantize("model_quantized.onnx", QuantType.QInt8) | |
| quantize("model_uint8.onnx", QuantType.QUInt8) | |