onnxruntime
8fcb725d - feat(quantization): add opset-21 block_size attribute to QDQ (#28522)

Commit
35 days ago
feat(quantization): add opset-21 block_size attribute to QDQ (#28522) ## Summary - Adds a `BlockSize` extra option to `quantize_static` / `StaticQuantConfig` that emits opset-21 `block_size` attribute on QuantizeLinear / DequantizeLinear nodes. - Weight initializers are quantized per-block along axis 0; scale and zero-point are 2-D `[n_blocks, n_channels]` tensors matching the ONNX spec. - Models below opset 21 are auto-upgraded; per-tensor and per-channel paths are unchanged. ## Motivation ONNX opset 21 added a `block_size` attribute to Q/DQ enabling sub-channel (blocked) quantization. The Python quantization tool had no way to produce this attribute, leaving users unable to export blocked-quantized models through ORT's static QDQ pipeline. Fixes #20981 ## Changes - `onnxruntime/python/tools/quantization/quant_utils.py`: new `compute_scale_zp_blocked` helper and a `block_size` branch in `quantize_onnx_initializer`. `update_opset_version` now bumps to opset 21 when `block_size > 0` (ordered before the float8 branch so a combined float8 + blocked model still reaches 21). - `onnxruntime/python/tools/quantization/qdq_quantizer.py`: thread `block_size` through `_create_q_node` / `_create_dq_node` / `_create_qdq_nodes` and through `_add_qdq_nodes_for_initializer`; permit 2-D scale/zp initializers when blocked. - `onnxruntime/python/tools/quantization/quantize.py`: surfaces the `BlockSize` extra option to `StaticQuantConfig`. - `onnxruntime/python/tools/quantization/base_quantizer.py`: extends `QuantizationParams` to carry `block_size`. ## Test Plan - New `onnxruntime/test/python/quantization/test_qdq_block_size.py` covers: - `block_size` attribute is emitted on Q/DQ nodes. - Scale and zero-point initializers have the expected 2-D `[n_blocks, ...]` shape. - Targeting opset < 21 with `BlockSize` triggers the auto-upgrade path. - `BlockSize = 0` (default) leaves the per-channel graph unchanged. - Regression: existing `test_qdq.py` suite (21 tests, 26 subtests) passes unchanged. - `lintrunner -a` is clean on the changed files.
Author
Parents
Loading