"""Turn the streaming export into the tract-friendly, int8 and int4 files in this repo. 1) python export_onnx_streaming.py model.tzf out 16 128 static (batch fixed to 1) 2) python make_tract_model.py out/encoder.onnx . onnxruntime's BASIC (pure-ONNX) optimizer folds the shape arithmetic (Range/Shape chains) that tract's inference rejects.""" import sys,onnxruntime as ort from pathlib import Path from onnxruntime.quantization import quantize_dynamic,QuantType from onnxruntime.quantization.matmul_nbits_quantizer import MatMulNBitsQuantizer,DefaultWeightOnlyQuantConfig from onnxruntime.quantization.quant_utils import load_model_with_shape_infer src,out=sys.argv[1],Path(sys.argv[2]);out.mkdir(exist_ok=True) so=ort.SessionOptions();so.graph_optimization_level=ort.GraphOptimizationLevel.ORT_ENABLE_BASIC;so.optimized_model_filepath=str(out/'encoder.onnx') ort.InferenceSession(src,so) quantize_dynamic(model_input=str(out/'encoder.onnx'),model_output=str(out/'encoder.int8.onnx'),op_types_to_quantize=['MatMul'],weight_type=QuantType.QInt8) q=MatMulNBitsQuantizer(load_model_with_shape_infer(out/'encoder.onnx'),algo_config=DefaultWeightOnlyQuantConfig(block_size=32,is_symmetric=True,accuracy_level=4)) q.process();q.model.save_model_to_file(str(out/'encoder.int4.onnx'),use_external_data_format=False);print('ok')