onnxruntime/onnxruntime/test
Adrian Lizarraga f644ff9fc0
[QNN EP] Support per-channel quantized weights (#20154)
### Description
- Adds general support for per-channel quantized weights to QNN EP (HTP
backend).
- Add QNN EP unit tests for per-channel Conv
- Update quantization tool to allow selecting which ops are quantized
per-channel (and which axis) via tensor-level overrides. Currently,
setting `per_channel=True` assumes all Convs, MatMuls, Gemms,
InstanceNormalization, and LayerNormalization ops should be quantized
per-channel using some assumed default axis.

#### Creating QDQ per-channel Conv model example
```python
from onnxruntime.quantization import CalibrationDataReader, QuantType, quantize
from onnxruntime.quantization.execution_providers.qnn import get_qnn_qdq_config, qnn_preprocess_model

class DataReader(CalibrationDataReader):
    # TODO: See ONNX Runtime QNN docs for example of a data reader
    # https://onnxruntime.ai/docs/execution-providers/QNN-ExecutionProvider.html#generating-a-quantized-model-x64
    pass

if __name__ == "__main__":
    input_model_path = "model.onnx"
    my_data_reader = DataReader(model_to_quantize)

    # Pre-process the original float32 model.
    preproc_model_path = "model.preproc.onnx"
    model_changed = qnn_preprocess_model(input_model_path, preproc_model_path)
    model_to_quantize = preproc_model_path if model_changed else input_model_path

    # RELEVANT TO THIS PR:
    # Make sure Conv's weight input is quantized to int8/symmetric/per-channel with axis == 0.
    # The presence of the 'axis' key indicates that this is a per-channel quantized weight.
    init_overrides = {'weight': [{'axis': 0, 'quant_type': QuantType.QInt8, 'symmetric': True}]}

    qnn_config = get_qnn_qdq_config(model_to_quantize,
                                    my_data_reader,
                                    init_overrides=init_overrides,
                                    activation_type=QuantType.QUInt16, # uint16 activations
                                    weight_type=QuantType.QUInt8)      # uint8 weights by default

    quantize(model_to_quantize, "model.qdq.onnx", qnn_config)
```

float32 model:
<img width="683" alt="image"
src="https://github.com/microsoft/onnxruntime/assets/19691973/ca650e49-1ad0-47d8-8c46-17fbc224ca39">

QDQ model (per-channel Conv weight):
<img width="748" alt="image"
src="https://github.com/microsoft/onnxruntime/assets/19691973/6bd469f2-968b-4d11-9526-09b3e71f98e7">

### Motivation and Context
Support more models, especially models with int4 quantized weights.
2024-04-16 08:45:35 -07:00
..
common Fix build errors from date/date.h C++20 compatibility (#20139) 2024-04-02 22:10:25 -07:00
contrib_ops add QMoE (#20108) 2024-03-29 10:24:19 -07:00
cuda_host Enable CUDA EP unit testing on Windows (#20039) 2024-03-27 13:32:36 -07:00
custom_op_registration
debug_node_inputs_outputs
framework Switch a portion of CI/packaging jobs to MacOS12 (#19908) 2024-03-19 14:54:58 -07:00
fuzzing
global_thread_pools Replace T4 to A10 in Linux GPU workflow (#19205) 2024-01-23 10:49:24 -08:00
ir Make session configuration options available to kernels via OpKernelInfo (#18897) 2024-01-13 10:02:43 +10:00
logging_apis Remove two tests from test_logging_apis.cc (#19100) 2024-01-12 09:26:28 -08:00
mlas [MLAS][AArch64] SQ4BitGemm CompInt8 multi-block implementation (#19826) 2024-03-14 13:05:42 -07:00
onnx Integration with ONNX 1.16.0 (#19745) 2024-04-12 09:46:49 -07:00
opaque_api
optimizer [QNN EP] Support per-channel quantized weights (#20154) 2024-04-16 08:45:35 -07:00
perftest Ort openvino npu 1.17 master (#19966) 2024-03-21 18:44:00 -07:00
platform Support xcframework for mac catalyst builds. (#19534) 2024-03-20 10:55:19 -07:00
proto
providers [QNN EP] Support per-channel quantized weights (#20154) 2024-04-16 08:45:35 -07:00
python [QNN EP] Support per-channel quantized weights (#20154) 2024-04-16 08:45:35 -07:00
quantization Disable CPU EP's allocator's arena when address sanitizer is enabled (#19485) 2024-02-12 09:39:49 -08:00
shared_lib Add new SessionOptions config entry to disable specific transformers and rules (#20135) 2024-04-02 16:33:05 -07:00
testdata Integration with ONNX 1.16.0 (#19745) 2024-04-12 09:46:49 -07:00
unittest_main Don't reduce warning level for CUDA build on Windows (#19663) 2024-03-06 15:03:55 +10:00
util Fix build errors from date/date.h C++20 compatibility (#20139) 2024-04-02 22:10:25 -07:00
wasm Bump follow-redirects from 1.15.4 to 1.15.6 in /onnxruntime/test/wasm (#19950) 2024-03-16 18:54:06 -07:00
win_getopt
xctest
run_benchmark.py Add onnxruntime/test/run_benchmark.py helper script. (#19234) 2024-04-05 07:02:01 -07:00
run_benchmark.readme.md Add onnxruntime/test/run_benchmark.py helper script. (#19234) 2024-04-05 07:02:01 -07:00