Skip to content

Commit c652736

Browse files
committed
Arm backend: Fix inf/-inf handling in comparison function
`compare_rel_frobenius_and_cosine_similarity` was not handling inf/-inf correctly for cases where zero points were non-zero. This patch addresses this issue by using qmin/qmax together with the zero point when translating inf/-inf in the floating-point reference before comparison. Signed-off-by: Martin Lindström <Martin.Lindstroem@arm.com> Change-Id: I498783bdf2065eae22c262a6179534682aa7c5ec
1 parent 170677f commit c652736

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

backends/arm/test/ops/test_masked_fill.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,7 @@ def test_masked_fill_scalar_tosa_FP(test_module):
106106
pipeline.run()
107107

108108

109-
@common.parametrize(
110-
"test_module",
111-
test_modules,
112-
xfails={
113-
"masked_fill_8_extreme_scalar_inf": "MLETORCH-1812 - Quantization inaccurate on inf-values in masked fill"
114-
},
115-
)
109+
@common.parametrize("test_module", test_modules)
116110
def test_masked_fill_scalar_tosa_INT(test_module):
117111
module, inputs = test_module()
118112
pipeline = TosaPipelineINT[input_t](

backends/arm/test/tester/analyze_output_utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ def compare_rel_frobenius_and_cosine_similarity(
350350
Cosine similarity test: The cosine similiarity of the flattened reference and test tensor. Closer to 1 is better.
351351
352352
If clean_reference is set to True the following is done to the reference :
353-
- NaN-values will be set to 0
354-
- Inf values will be set to max/min representable by the dtype * quantization scale
353+
- NaN-values will be set to 0.0
354+
- Inf values will be set to max/min representable by the (dtype - zp) * scale
355355
- Values lower than the scale will be set to 0.0
356356
If the reference is all zeros, the function returns without testing.
357357
@@ -374,10 +374,15 @@ def compare_rel_frobenius_and_cosine_similarity(
374374
if isinstance(scale, torch.Tensor)
375375
else float(scale)
376376
)
377-
dtype_info = torch.iinfo(quantization_parameters.dtype)
378377
assert quant_scale_for_guards is not None
379-
posinf_value = float(dtype_info.max) * quant_scale_for_guards
380-
neginf_value = float(dtype_info.min) * quant_scale_for_guards
378+
posinf_value = (
379+
float(quantization_parameters.qmax - quantization_parameters.zp)
380+
* quant_scale_for_guards
381+
)
382+
neginf_value = (
383+
float(quantization_parameters.qmin - quantization_parameters.zp)
384+
* quant_scale_for_guards
385+
)
381386
reference_output = reference_output.where(
382387
torch.abs(reference_output) >= scale, 0.0
383388
)

0 commit comments

Comments
 (0)