Repository navigation
Forward reduce_range and nodes_to_quantize in the static QDQ config path - #2755
Open
Shreya Boyane (Sonica-B) wants to merge 1 commit into
Open
Shreya Boyane (Sonica-B) wants to merge 1 commit into
Shreya Boyane (Sonica-B) wants to merge 1 commit into
Conversation
With quant_format="QDQ" and prepare_qdq_config=True (both defaults), OnnxStaticQuantization builds its run config from get_qdq_config (or get_qnn_qdq_config for the QNN EP) and uses the returned StaticQuantConfig in place of the pass config. reduce_range was never passed to get_qdq_config, and neither helper accepts nodes_to_quantize, so both settings were silently dropped: weights were quantized to the full 8-bit range and every node of the selected op types was quantized. Pass reduce_range to get_qdq_config, warn when it is set for the QNN EP (get_qnn_qdq_config has no such parameter), and copy nodes_to_quantize onto the final run config.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your changes
OnnxStaticQuantizationsilently ignoresreduce_rangeandnodes_to_quantizeon its default path (quant_format="QDQ",prepare_qdq_config=True, non-QNN EP, onnxruntime >= 1.21).On that path,
get_static_run_configcalls onnxruntime'sget_qdq_config(...)and then uses the members of the returnedStaticQuantConfigas thequantize_staticarguments. Two user settings get lost on the way:reduce_rangeis a parameter ofget_qdq_config, but Olive never passes it, soStaticQuantConfigkeeps its defaultreduce_range=False.get_qdq_confignorget_qnn_qdq_confighas anodes_to_quantizeparameter, soStaticQuantConfig.nodes_to_quantizestaysNoneand every node of the selected op types is quantized.With
prepare_qdq_config=Falseboth settings work, so the pass does mean to support them. The pass-configuration docs example (docs/source/how-to/configure-workflows/pass-configuration.md) searches overreduce_rangeforOnnxQuantization; on the default path both search points currently produce the same model.flowchart LR subgraph Before A1[pass config<br/>reduce_range, nodes_to_quantize] --> B1["get_qdq_config(...)<br/>no reduce_range passed"] B1 --> C1["StaticQuantConfig<br/>reduce_range=False<br/>nodes_to_quantize=None"] C1 --> D1[quantize_static] end subgraph After A2[pass config<br/>reduce_range, nodes_to_quantize] --> B2["get_qdq_config(..., reduce_range)"] B2 --> C2[StaticQuantConfig members] A2 -- "nodes_to_quantize copied" --> C2 C2 --> D2[quantize_static] endChanges in
olive/passes/onnx/quantization.py:reduce_rangetoget_qdq_config;reduce_rangeis set, sinceget_qnn_qdq_confighas no such parameter (QNN behavior is otherwise unchanged);nodes_to_quantizeonto the final run config, for both the QNN and non-QNN helpers.Reproduction: a two-MatMul ONNX model (
x -> MatMul(W1) -> Relu -> MatMul(W2)),OnnxStaticQuantizationwithprecision=int8,weight_symmetric=True, everything else default. The script prints the largest absolute value in each quantized weight (127 is the full symmetric int8 range, 64 is the 7-bitreduce_rangerange).Before (main @ 1fce2a6):
After:
Tests (
test/passes/onnx/test_quantization.py):test_nodes_and_opsgains anodes_to_quantizecase for both QNN and non-QNN and checks thatnodes_to_quantizereachesquantize_static;test_static_quantization_applies_reduce_range_when_prepare_qdq_configruns real static quantization and checks that the quantized weights span 127 withoutreduce_rangeand 64 with it.On
mainthe three new cases fail (assert set() == {'node_linear'}twice,assert 127 == 64); with the fix the file passes (16 passed). Validated on Windows 11 with CPU torch (Python 3.11.9, onnxruntime 1.31.0, onnx 1.20.1, torch 2.14.1+cpu);test_add_metadata.py,test_split_model.py,test_quantize_embedding.py,test/engine/test_engine.pyandtest/cli/test_cli.pyalso pass there. CI will confirm on the other platforms. The QNN EP itself was not run; the QNN path is covered by the mockedtest_nodes_and_ops[True-*]cases.Checklist before requesting a review
lintrunner -aRelease note:
OnnxStaticQuantization/OnnxQuantizationnow applyreduce_rangeandnodes_to_quantizewhenprepare_qdq_config=True(the default for QDQ). Before this change both were ignored on that path. Configs that setreduce_range: truewill now produce 7-bit weights, as documented.(Optional) Issue link
None; found while reading the static quantization path.