Skip to content

Arm backend: Avoid subtraction error on boolean tensors in error diff logging #11249

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

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
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
24 changes: 16 additions & 8 deletions backends/arm/test/tester/analyze_output_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ def print_error_diffs(
output_str += f"BATCH {n}\n"
result_batch = result[n, :, :, :]
reference_batch = reference[n, :, :, :]

if reference_batch.dtype == torch.bool or result_batch.dtype == torch.bool:
mismatches = (reference_batch != result_batch).sum().item()
total = reference_batch.numel()
output_str += f"(BOOLEAN tensor) {mismatches} / {total} elements differ ({mismatches / total:.2%})\n"
continue

is_close = torch.allclose(result_batch, reference_batch, rtol, atol)
if is_close:
output_str += ".\n"
Expand All @@ -180,14 +187,15 @@ def print_error_diffs(
output_str += _print_elements(
result[n, :, :, :], reference[n, :, :, :], C, H, W, rtol, atol
)

reference_range = torch.max(reference) - torch.min(reference)
diff = torch.abs(reference - result).flatten()
diff = diff[diff.nonzero()]
if not len(diff) == 0:
diff_percent = diff / reference_range
output_str += "\nMEAN MEDIAN MAX MIN (error as % of reference output range)\n"
output_str += f"{torch.mean(diff_percent):<8.2%} {torch.median(diff_percent):<8.2%} {torch.max(diff_percent):<8.2%} {torch.min(diff_percent):<8.2%}\n"
# Only compute numeric error metrics if tensor is not boolean
if reference.dtype != torch.bool and result.dtype != torch.bool:
reference_range = torch.max(reference) - torch.min(reference)
diff = torch.abs(reference - result).flatten()
diff = diff[diff.nonzero()]
if not len(diff) == 0:
diff_percent = diff / reference_range
output_str += "\nMEAN MEDIAN MAX MIN (error as % of reference output range)\n"
output_str += f"{torch.mean(diff_percent):<8.2%} {torch.median(diff_percent):<8.2%} {torch.max(diff_percent):<8.2%} {torch.min(diff_percent):<8.2%}\n"

# Over-engineer separators to match output width
lines = output_str.split("\n")
Expand Down
43 changes: 25 additions & 18 deletions backends/xnnpack/test/tester/tester.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# Copyright 2024-2025 Arm Limited and/or its affiliates.
# All rights reserved.
# Copyright 2024-2025 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -714,23 +714,30 @@ def _assert_outputs_equal(model_output, ref_output, atol=1e-03, rtol=1e-03):
assert (
ref.shape == model.shape
), f"Output {i} shape {model.shape} does not match reference output shape {ref.shape}"
assert torch.allclose(
model,
ref,
atol=atol,
rtol=rtol,
), (
f"Output {i} does not match reference output.\n"
f"\tGiven atol: {atol}, rtol: {rtol}.\n"
f"\tOutput tensor shape: {model.shape}, dtype: {model.dtype}\n"
f"\tDifference: max: {torch.max(model-ref)}, abs: {torch.max(torch.abs(model-ref))}, mean abs error: {torch.mean(torch.abs(model-ref))}.\n"
f"\t-- Model vs. Reference --\n"
f"\t Numel: {model.numel()}, {ref.numel()}\n"
f"\tMedian: {model.median()}, {ref.median()}\n"
f"\t Mean: {model.mean()}, {ref.mean()}\n"
f"\t Max: {model.max()}, {ref.max()}\n"
f"\t Min: {model.min()}, {ref.min()}\n"
)
if model.dtype == torch.bool:
assert torch.equal(model, ref), (
f"Output {i} (bool tensor) does not match reference output.\n"
f"\tShape: {model.shape}\n"
f"\tMismatched count: {(model != ref).sum().item()} / {model.numel()}\n"
)
else:
assert torch.allclose(
model,
ref,
atol=atol,
rtol=rtol,
), (
f"Output {i} does not match reference output.\n"
f"\tGiven atol: {atol}, rtol: {rtol}.\n"
f"\tOutput tensor shape: {model.shape}, dtype: {model.dtype}\n"
f"\tDifference: max: {torch.max(model-ref)}, abs: {torch.max(torch.abs(model-ref))}, mean abs error: {torch.mean(torch.abs(model-ref))}.\n"
f"\t-- Model vs. Reference --\n"
f"\t Numel: {model.numel()}, {ref.numel()}\n"
f"\tMedian: {model.median()}, {ref.median()}\n"
f"\t Mean: {model.mean()}, {ref.mean()}\n"
f"\t Max: {model.max()}, {ref.max()}\n"
f"\t Min: {model.min()}, {ref.min()}\n"
)

@staticmethod
def _compare_outputs(
Expand Down
Loading