| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import torch |
| 5 | |
| 6 | ToleranceSpec = dict[torch.dtype, dict[str, float]] |
| 7 | |
| 8 | # Default tolerances for comparing IR op implementations against native. |
| 9 | # These are intentionally conservative (permissive) to avoid false failures |
| 10 | # across different hardware and kernel implementations. Ops that need tighter |
| 11 | # or looser bounds should use override_tolerance. |
| 12 | DEFAULT_TOLERANCES: ToleranceSpec = { |
| 13 | # 52-bit mantissa; machine epsilon ~1.1e-16 |
| 14 | torch.float64: {"atol": 1e-8, "rtol": 1e-8}, |
| 15 | # 23-bit mantissa; machine epsilon ~1.2e-7. |
| 16 | # Values from PyTorch test_transformers.py reference defaults. |
| 17 | torch.float32: {"atol": 1e-5, "rtol": 1.3e-6}, |
| 18 | # 10-bit mantissa; machine epsilon ~9.8e-4. |
| 19 | # Standard tolerance used across vLLM kernel tests. |
| 20 | torch.float16: {"atol": 1e-3, "rtol": 1e-3}, |
| 21 | # 7-bit mantissa; machine epsilon ~7.8e-3. |
| 22 | # Wider rtol than float16 to account for the coarser mantissa. |
| 23 | torch.bfloat16: {"atol": 1e-3, "rtol": 1.6e-2}, |
| 24 | # 3-bit mantissa; machine epsilon ~6.25e-2. |
| 25 | # Derived from vLLM fp8 kernel tests (merge_attn_states, silu_mul_fp8). |
| 26 | torch.float8_e4m3fn: {"atol": 1e-1, "rtol": 1e-1}, |
| 27 | # 2-bit mantissa; machine epsilon ~1.25e-1. |
| 28 | # Wider than e4m3fn due to the smaller mantissa. |
| 29 | torch.float8_e5m2: {"atol": 2e-1, "rtol": 2e-1}, |
| 30 | # 1-bit mantissa; machine epsilon ~2.5e-1. Packed pair format (x2). |
| 31 | # Derived from vLLM fp4 tests (test_silu_mul_nvfp4_quant: atol=3e-1). |
| 32 | torch.float4_e2m1fn_x2: {"atol": 3e-1, "rtol": 3e-1}, |
| 33 | # Integer quantized; off-by-one from rounding is expected. |
| 34 | # rtol=0 because relative error is meaningless for small integers. |
| 35 | torch.int8: {"atol": 1, "rtol": 0}, |
| 36 | } |