Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix static quantization for QDQ and Percentile distribution #17649

Merged
merged 11 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions onnxruntime/python/tools/quantization/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


class TensorData:
_allowed = frozenset(["avg", "std", "lowest", "highest", "hist", "hist_edges"])
_allowed = frozenset(["avg", "std", "lowest", "highest", "hist", "hist_edges", "bins"])

def __init__(self, **kwargs):
for k, v in kwargs.items():
Expand Down Expand Up @@ -55,7 +55,7 @@ def __init__(self, calibration_method, data: Dict[str, Union[TensorData, Tuple]]
self.data[k] = TensorData(lowest=v[0], highest=v[1])
continue
if len(v) == 4:
self.data[k] = TensorData(lowest=v[0], highest=v[1], histogram=v[2], bins=v[3])
self.data[k] = TensorData(lowest=v[0], highest=v[1], hist=v[2], bins=v[3])
continue
raise TypeError(f"Unexpected tuple for {k:r}, it has {len(v)} elements: {v}.")
if not isinstance(v, TensorData):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import unittest
import os
import random
import tempfile
import numpy as np
from onnxruntime import InferenceSession
from onnxruntime.quantization.calibrate import CalibrationDataReader
from onnxruntime.quantization import QuantFormat, QuantType, quantize_static
from onnxruntime.quantization.calibrate import CalibrationMethod


class FakeResnetCalibrationDataReader(CalibrationDataReader):
def __init__(self, batch_size: int = 16):
super().__init__()
self.dataset = [
(np.random.rand(1, 3, 32, 32).astype(np.float32), random.randint(0, 9)) for _ in range(batch_size)
]
self.iterator = iter(self.dataset)

def get_next(self) -> dict:
try:
return {"input": next(self.iterator)[0]}
except Exception:
return None


class TestStaticQuantizationResNet(unittest.TestCase):
def test_quantize_static_resnet(self):
folder = os.path.join(os.path.dirname(__file__), "..", "..", "testdata")
model = os.path.join(folder, "resnet_first_nodes.onnx")
if not os.path.exists(model):
raise FileNotFoundError(f"Unable to find {model!r} in testdata.")

kwargs = {
"activation_type": QuantType.QUInt8,
"calibrate_method": CalibrationMethod.Percentile,
"extra_options": {
"ActivationSymmetric": False,
"EnableSubgraph": False,
"ForceQuantizeNoInputCheck": False,
"MatMulConstBOnly": False,
"WeightSymmetric": True,
"extra.Sigmoid.nnapi": False,
},
"nodes_to_exclude": None,
"nodes_to_quantize": None,
"op_types_to_quantize": None,
"per_channel": True,
"quant_format": QuantFormat.QDQ,
"reduce_range": False,
"weight_type": QuantType.QUInt8,
}

dataloader = FakeResnetCalibrationDataReader(16)

with tempfile.TemporaryDirectory(prefix="test_calibration.") as temp:
qdq_file = os.path.join(temp, "preprocessed-small-qdq.onnx")
quantize_static(
model_input=model,
model_output=qdq_file,
calibration_data_reader=dataloader,
use_external_data_format=False,
**kwargs,
)

sess = InferenceSession(qdq_file, providers=["CPUExecutionProvider"])
shape = (1, 3, 32, 32)
size = np.prod(shape)
dummy = (np.arange(size) / float(size)).astype(np.float32).reshape(shape)
got = sess.run(None, {"input": dummy})
print(got[0])


if __name__ == "__main__":
unittest.main(verbosity=2)
Binary file added onnxruntime/test/testdata/resnet_first_nodes.onnx
Binary file not shown.