onnxruntime/onnxruntime/python
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
..
backend Bump linter versions (#18341) 2023-11-08 13:04:40 -08:00
datasets
providers/tvm
tools [QNN EP] Support per-channel quantized weights (#20154) 2024-04-16 08:45:35 -07:00
torch_cpp_extensions remove const cast for DLManagedTensor (#20015) 2024-03-22 10:39:19 -07:00
training
__init__.py
_ld_preload.py
_pybind_state.py.in
exported_symbols.lst
numpy_helper.h
onnxruntime_collect_build_info.py
onnxruntime_inference_collection.py Bump ruff linter to 0.2.1 (#19471) 2024-02-08 16:08:27 -08:00
onnxruntime_pybind.h
onnxruntime_pybind_exceptions.cc
onnxruntime_pybind_exceptions.h
onnxruntime_pybind_iobinding.cc Remove internal enforce for IO binding inputs (#18266) 2023-11-03 16:12:32 -07:00
onnxruntime_pybind_mlvalue.cc io_binding to handle optional input of sequence type_proto (#19273) 2024-01-30 10:25:14 -08:00
onnxruntime_pybind_mlvalue.h [DML EP] Complete python IO binding implementation (#17344) 2023-09-13 07:26:35 -07:00
onnxruntime_pybind_module.cc Fix warning C4003 in ORT python binding code (#18612) 2023-11-30 08:07:47 -08:00
onnxruntime_pybind_ortvalue.cc [DML EP] Complete python IO binding implementation (#17344) 2023-09-13 07:26:35 -07:00
onnxruntime_pybind_quant.cc Block-wise 4b quantization matmul operator change (#18172) 2023-11-03 15:29:43 -07:00
onnxruntime_pybind_schema.cc [DML EP] Add dynamic graph compilation (#17876) 2023-10-25 19:56:16 -07:00
onnxruntime_pybind_sparse_tensor.cc
onnxruntime_pybind_state.cc Ort openvino npu 1.17 master (#19966) 2024-03-21 18:44:00 -07:00
onnxruntime_pybind_state.h
onnxruntime_pybind_state_common.cc
onnxruntime_pybind_state_common.h Ort openvino npu 1.17 master (#19966) 2024-03-21 18:44:00 -07:00
onnxruntime_validation.py fix: "UserWarning: Unsupported Windows version (11). ONNX Runtime sup… (#19845) 2024-03-15 12:41:44 -07:00
pybind.def
version_script.lds
version_script_expose_onnx_protobuf.lds