Skip to content

Commit 22876ca

Browse files
kyunggeunleequic-kyunggeu
authored andcommitted
Make export_int32_bias default to True if encoding_version >= 2.0.0
Signed-off-by: Kyunggeun Lee <quic_kyunggeu@quicinc.com> Co-authored-by: Kyunggeun Lee <quic_kyunggeu@quicinc.com>
1 parent 8485300 commit 22876ca

4 files changed

Lines changed: 48 additions & 15 deletions

File tree

TrainingExtensions/onnx/src/python/aimet_onnx/quantsim.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,8 @@ def _get_weight_and_bias(
18281828
export_model (bool, optional):
18291829
If True, then ONNX model is exported. When False, only encodings are exported.
18301830
export_int32_bias (bool, optional):
1831-
If true, generate and export int32 bias encoding on the fly (default: `True`)
1831+
If true, generate and export int32 bias encoding on the fly.
1832+
Default: `True` if encoding version is 2.0.0 or higher, otherwise `False`.
18321833
encoding_version (str, optional):
18331834
Version of the encoding format to use. (default: {quantsim.encoding_version})
18341835
Supported versions are: {sorted(list(quantsim.VALID_ENCODING_VERSIONS))}
@@ -1848,7 +1849,7 @@ def export(
18481849
filename_prefix: str,
18491850
export_model: bool = True,
18501851
*,
1851-
export_int32_bias: bool = False,
1852+
export_int32_bias: Optional[bool] = None,
18521853
encoding_version: Optional[str] = None,
18531854
force_activation_as: Literal["unsigned"]
18541855
| Literal["signed"]
@@ -1870,6 +1871,11 @@ def export(
18701871
)
18711872
warnings.warn(msg, DeprecationWarning, stacklevel=2)
18721873

1874+
if export_int32_bias is None:
1875+
export_int32_bias = version.parse(encoding_version) >= version.parse(
1876+
"2.0.0"
1877+
)
1878+
18731879
with (
18741880
self._concretize_int32_bias_quantizers()
18751881
if export_int32_bias

TrainingExtensions/onnx/test/python/test_quantsim.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -577,23 +577,26 @@ def test_export_model_2_0_0(self, tmp_path: pathlib.Path, activation_type):
577577
"version": aimet_onnx.__version__,
578578
}
579579
encodings = encodings["encodings"]
580-
assert (
581-
sorted(e["name"] for e in encodings)
582-
== [
580+
encoding_keys = set(e["name"] for e in encodings)
581+
582+
if activation_type == aimet_onnx.int8:
583+
assert encoding_keys == {
583584
"4",
584585
"5",
585586
"6",
587+
"conv_b",
586588
"conv_w",
589+
"fc_b",
587590
"fc_w",
588591
"input",
589592
"output",
590-
]
591-
if activation_type == aimet_onnx.int8
592-
else [
593+
}
594+
else:
595+
assert encoding_keys == {
593596
"conv_w",
594597
"fc_w",
595-
]
596-
)
598+
}
599+
597600
# Exported encoding can contain more entry than qc_quantize_op_dict since
598601
# some grid-preserving op's input/output encodings are auto-generated
599602
assert set(e["name"] for e in encodings) >= {
@@ -604,7 +607,13 @@ def test_export_model_2_0_0(self, tmp_path: pathlib.Path, activation_type):
604607

605608
# Cross-check with onnx QDQ.
606609
expected_encodings = _remove_onnx_qdq_nodes(sim.to_onnx_qdq())
607-
assert len(encodings) == len(expected_encodings)
610+
expected_encoding_keys = set(e["name"] for e in expected_encodings)
611+
612+
if activation_type == aimet_onnx.int8:
613+
assert encoding_keys == expected_encoding_keys | {"conv_b", "fc_b"}
614+
encodings = [e for e in encodings if e["name"] in expected_encoding_keys]
615+
else:
616+
assert encoding_keys == expected_encoding_keys
608617

609618
encodings = sorted(encodings, key=lambda e: e["name"])
610619
expected_encodings = sorted(expected_encodings, key=lambda e: e["name"])
@@ -1620,7 +1629,13 @@ def callback(session, args):
16201629
for name, q in sim.qc_quantize_op_dict.items()
16211630
if q in reconfigured_quantizers
16221631
]
1623-
assert len(mismatched_encodings) == len(reconfigured_tensors)
1632+
1633+
if encoding_version in ("0.6.1", "1.0.0"):
1634+
assert len(mismatched_encodings) == len(reconfigured_tensors)
1635+
else:
1636+
# mismatched_encodings contains 2 extra entries for int32 bias encodings
1637+
assert len(mismatched_encodings) == len(reconfigured_tensors) + 2
1638+
16241639
assert np.allclose(
16251640
out2,
16261641
out3,

TrainingExtensions/onnx/test/python/test_torch_to_onnx.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ def test_torch_to_onnx_zero_point_shift(tmp_dir, encoding_version):
177177
onnx_export_path = os.path.join(tmp_dir, "onnx_export")
178178
onnx_encoding_path = os.path.join(onnx_export_path, "model.encodings")
179179
os.makedirs(onnx_export_path, exist_ok=True)
180-
onnx_sim.export(onnx_export_path, "model", encoding_version=encoding_version)
180+
onnx_sim.export(
181+
onnx_export_path,
182+
"model",
183+
encoding_version=encoding_version,
184+
export_int32_bias=False,
185+
)
181186

182187
with open(onnx_encoding_path, "r") as f:
183188
onnx_encodings = json.load(f)

TrainingExtensions/torch/src/python/aimet_torch/v2/quantsim/quantsim.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import json
3030
import contextlib
3131
import os
32+
from packaging import version
3233
from aimet_torch.v2.nn.true_quant import QuantizedGRU, QuantizedLSTM, QuantizedRNN
3334
import torch
3435
import onnx
@@ -1064,7 +1065,8 @@ def __init__(self, sim: QuantizationSimModel):
10641065
Version of the encoding format to use. (default: {quantsim.encoding_version})
10651066
Supported versions are: {sorted(list(quantsim.VALID_ENCODING_VERSIONS))}
10661067
export_int32_bias (bool, optional):
1067-
If true, generate and export int32 bias encoding on the fly (default: `True`).
1068+
If true, generate and export int32 bias encoding on the fly.
1069+
Default: `True` if encoding version is 2.0.0 or higher, otherwise `False`.
10681070
force_activation_as (str, optional):
10691071
Force representing quantized activations as signed or unsigned integers.
10701072
This argument is only applicable for encoding version 2.0.0.
@@ -1093,7 +1095,7 @@ def export(
10931095
f: Union[str, io.BytesIO],
10941096
*,
10951097
encoding_version: Optional[str] = None,
1096-
export_int32_bias: bool = True,
1098+
export_int32_bias: Optional[bool] = None,
10971099
force_activation_as: Literal["unsigned"]
10981100
| Literal["signed"]
10991101
| None = "unsigned",
@@ -1107,6 +1109,11 @@ def export(
11071109
f"Supported versions are: {quantsim.VALID_ENCODING_VERSIONS}"
11081110
)
11091111

1112+
if export_int32_bias is None:
1113+
export_int32_bias = version.parse(encoding_version) >= version.parse(
1114+
"2.0.0"
1115+
)
1116+
11101117
from aimet_torch.onnx import (
11111118
_temporarily_convert_activation_to,
11121119
_check_unsupported_args,

0 commit comments

Comments
 (0)