From 287291c232815ce9e04fb4e05d62d065c6c08a0a Mon Sep 17 00:00:00 2001 From: fzyzcjy <5236035+fzyzcjy@users.noreply.github.com> Date: Thu, 9 Jul 2026 20:16:18 +0800 Subject: [PATCH] Fix rel_diff being nan for bitwise-identical tensors (#30655) --- .../comparator/tensor_comparator/comparator.py | 4 +++- .../tensor_comparator/test_comparator.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/python/sglang/srt/debug_utils/comparator/tensor_comparator/comparator.py b/python/sglang/srt/debug_utils/comparator/tensor_comparator/comparator.py index 447f2c186..ec9fc40b2 100644 --- a/python/sglang/srt/debug_utils/comparator/tensor_comparator/comparator.py +++ b/python/sglang/srt/debug_utils/comparator/tensor_comparator/comparator.py @@ -165,8 +165,10 @@ def compute_diff( raw_abs_diff = (x_target - x_baseline).abs() max_diff_coord = argmax_coord(raw_abs_diff) - rel_diff = calc_rel_diff(x_target, x_baseline).item() max_abs_diff = raw_abs_diff.max().item() + rel_diff = ( + 0.0 if max_abs_diff == 0.0 else calc_rel_diff(x_target, x_baseline).item() + ) mean_abs_diff = raw_abs_diff.mean().item() include_quantiles: bool = raw_abs_diff.numel() < QUANTILE_NUMEL_THRESHOLD diff --git a/test/registered/debug_utils/comparator/tensor_comparator/test_comparator.py b/test/registered/debug_utils/comparator/tensor_comparator/test_comparator.py index a3c4bdcad..e9e8083b8 100644 --- a/test/registered/debug_utils/comparator/tensor_comparator/test_comparator.py +++ b/test/registered/debug_utils/comparator/tensor_comparator/test_comparator.py @@ -458,6 +458,20 @@ class TestComputeDiffPredicate: is False ) + def test_bitwise_predicate(self) -> None: + """'rel <= 0' passes only for bitwise-identical tensors.""" + ident = torch.randn(5, 5) + assert ( + compute_diff( + x_baseline=ident, x_target=ident.clone(), predicate="rel <= 0" + ).passed + is True + ) + x, y = self._near_zero_pair() + assert ( + compute_diff(x_baseline=x, x_target=y, predicate="rel <= 0").passed is False + ) + def test_predicate_recorded_for_empty_tensor(self) -> None: """Empty tensors short-circuit to passed=True and still record the predicate.""" empty = torch.empty(0)