Summarize candidate-vs-reference drift for benchmark/test output.
(
candidate: torch.Tensor,
reference: torch.Tensor,
mask: torch.Tensor | None = None,
)
| 107 | |
| 108 | |
| 109 | def summarize_kernel_drift( |
| 110 | candidate: torch.Tensor, |
| 111 | reference: torch.Tensor, |
| 112 | mask: torch.Tensor | None = None, |
| 113 | ) -> dict[str, Any]: |
| 114 | """Summarize candidate-vs-reference drift for benchmark/test output.""" |
| 115 | |
| 116 | if candidate.shape != reference.shape: |
| 117 | raise ValueError( |
| 118 | f"candidate shape {tuple(candidate.shape)} must match reference shape " |
| 119 | f"{tuple(reference.shape)}" |
| 120 | ) |
| 121 | |
| 122 | diff = (candidate.float() - reference.float()).abs() |
| 123 | if mask is not None: |
| 124 | active = _bool_mask(mask, device=diff.device) |
| 125 | active_diff = diff[active] |
| 126 | active_count = int(active.sum().item()) |
| 127 | else: |
| 128 | active_diff = diff.reshape(-1) |
| 129 | active_count = int(diff.numel()) |
| 130 | |
| 131 | if active_count == 0: |
| 132 | max_abs = 0.0 |
| 133 | mean_abs = 0.0 |
| 134 | else: |
| 135 | max_abs = float(active_diff.max().item()) |
| 136 | mean_abs = float(active_diff.mean().item()) |
| 137 | |
| 138 | return { |
| 139 | "max_abs_error": max_abs, |
| 140 | "mean_abs_error": mean_abs, |
| 141 | "active_count": active_count, |
| 142 | } |