Skip to content

Commit d56a83d

Browse files
marialyucopybara-github
authored andcommitted
Remove scale constraint for SUM int8
PiperOrigin-RevId: 754059965
1 parent 5d8df8d commit d56a83d

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

ai_edge_quantizer/algorithms/uniform_quantize/common_quantize.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,21 @@ def materialize_sum(
416416
tensor_name_to_qsv: dict[str, Any],
417417
) -> list[qtyping.TensorTransformationParams]:
418418
"""Materialize tensors in tfl.sum."""
419+
# For 8 bits the reference kernel calls a function without input/output
420+
# constraints. For all others it calls a function that enforces input/output
421+
# scale/zero point checks. See:
422+
# https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/kernels/reduce.cc#L909
423+
activation_config = op_info.op_quant_config.activation_tensor_config
424+
if activation_config is not None and activation_config.num_bits == 8:
425+
constraint = _OpQuantConstraint.NO_CONSTRAIN
426+
else:
427+
constraint = _OpQuantConstraint.SAME_AS_INPUT_SCALE
419428
return common_utils.materialize_standard_op(
420429
op_info,
421430
graph_info,
422431
tensor_name_to_qsv,
423432
get_tensor_quant_params_fn,
424-
constraint=_OpQuantConstraint.SAME_AS_INPUT_SCALE,
433+
constraint=constraint,
425434
inputs_to_ignore=[1], # Axis index does not need to be quantized.
426435
)
427436

ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/sum_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_materialize_sum_succeeds(
102102
self._op_test_info,
103103
common_quantize.materialize_sum,
104104
get_tensor_quant_params_func,
105-
same_input_output_params=True,
105+
same_input_output_params=num_bits != 8, # No constraint for 8 bits.
106106
inputs_to_ignore=[1], # Ignore axis tensor.
107107
)
108108

0 commit comments

Comments
 (0)