Skip to content
Open
Changes from all commits
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
61 changes: 33 additions & 28 deletions backends/arm/quantizer/quantization_annotator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Copyright 2024-2026 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
Expand Down Expand Up @@ -81,6 +81,27 @@
self.quant_output: Optional[_QuantProperty] = None


def _conv_linear_quant_inputs(
input_act_qspec: QuantizationSpecBase | None,
weight_qspec: QuantizationSpecBase | None,
bias_qspec: QuantizationSpecBase | None,
) -> List[_QuantProperty]:
"""Build conv/linear input quant properties, tolerating partial quant.

Activation-only PTQ recipes leave ``weight_qspec`` (and the bias derived
from it) None; omit those QuantProperties so weight/bias stay fp32 instead of
asserting a non-None weight spec. Weight-only and full configs keep the
weight (and bias) quantized exactly as before.
"""
quant_inputs: List[_QuantProperty] = [_QuantProperty(0, input_act_qspec)]

Check failure on line 96 in backends/arm/quantizer/quantization_annotator.py

View workflow job for this annotation

GitHub Actions / lintrunner-mypy

MYPY arg-type

Argument 2 to "_QuantProperty" has incompatible type "QuantizationSpecBase | None"; expected "QuantizationSpecBase | list[QuantizationSpecBase]" To disable, use ` # type: ignore[arg-type]`
if weight_qspec is not None:
quant_inputs.append(_QuantProperty(1, weight_qspec, mark_annotated=True))
quant_inputs.append(
_QuantProperty(2, bias_qspec, optional=True, mark_annotated=True)

Check failure on line 100 in backends/arm/quantizer/quantization_annotator.py

View workflow job for this annotation

GitHub Actions / lintrunner-mypy

MYPY arg-type

Argument 2 to "_QuantProperty" has incompatible type "QuantizationSpecBase | None"; expected "QuantizationSpecBase | list[QuantizationSpecBase]" To disable, use ` # type: ignore[arg-type]`
)
return quant_inputs


class _QParams(NamedTuple):
scale: float
zero_point: int
Expand Down Expand Up @@ -716,12 +737,9 @@
filter_fn=any_or_hardtanh_min_zero,
):
if node.target in _conv_ops:
conv_weight_qspec = ensure_type(QuantizationSpec, weight_qspec) # For MyPy
quant_properties.quant_inputs = [
_QuantProperty(0, input_act_qspec),
_QuantProperty(1, conv_weight_qspec, mark_annotated=True),
_QuantProperty(2, bias_qspec, optional=True, mark_annotated=True),
]
quant_properties.quant_inputs = _conv_linear_quant_inputs(
input_act_qspec, weight_qspec, bias_qspec
)
elif node.target in (
torch.ops.aten.relu.default,
torch.ops.aten.relu_.default,
Expand All @@ -738,12 +756,9 @@
],
):
if node.target in _conv_ops:
conv_weight_qspec = ensure_type(QuantizationSpec, weight_qspec) # For MyPy
quant_properties.quant_inputs = [
_QuantProperty(0, input_act_qspec),
_QuantProperty(1, conv_weight_qspec, mark_annotated=True),
_QuantProperty(2, bias_qspec, optional=True, mark_annotated=True),
]
quant_properties.quant_inputs = _conv_linear_quant_inputs(
input_act_qspec, weight_qspec, bias_qspec
)
elif node.target in {torch.ops.aten.batch_norm.default}:
quant_properties.quant_output = _QuantProperty(0, output_act_qspec)
elif not is_symmetric and _match_pattern(
Expand All @@ -766,28 +781,18 @@
*_conv_ops,
torch.ops.aten.linear.default,
):
conv_or_linear_weight_qspec = ensure_type(
QuantizationSpec, weight_qspec
) # For MyPy
quant_properties.quant_inputs = [
_QuantProperty(0, input_act_qspec),
_QuantProperty(1, conv_or_linear_weight_qspec, mark_annotated=True),
_QuantProperty(2, bias_qspec, optional=True, mark_annotated=True),
]
quant_properties.quant_inputs = _conv_linear_quant_inputs(
input_act_qspec, weight_qspec, bias_qspec
)
else:
quant_properties.quant_output = _QuantProperty(0, output_act_qspec)
elif node.target in (
*_conv_ops,
torch.ops.aten.linear.default,
):
conv_or_linear_weight_qspec = ensure_type(
QuantizationSpec, weight_qspec
) # For MyPy
quant_properties.quant_inputs = [
_QuantProperty(0, input_act_qspec),
_QuantProperty(1, conv_or_linear_weight_qspec, mark_annotated=True),
_QuantProperty(2, bias_qspec, optional=True, mark_annotated=True),
]
quant_properties.quant_inputs = _conv_linear_quant_inputs(
input_act_qspec, weight_qspec, bias_qspec
)
quant_properties.quant_output = _QuantProperty(0, output_act_qspec)
elif node.target in (
torch.ops.aten.add.Tensor,
Expand Down
Loading