"""Named performance baselines, distinct from the FP32 correctness oracles.""" from functools import lru_cache import torch import torch.nn.functional as F from torch.nn.attention import SDPBackend, sdpa_kernel from .extended_baselines import ( dense_gemm, w8a16_gemm, rowwise_fp8, residual_rmsnorm, rope, paged_gqa, ragged_gqa, grouped_gemm, moe_combine, NAMES as EXTENDED_NAMES, ) from .sparse_baselines import block_sparse_gqa, sparse_mla, NAMES as SPARSE_NAMES NAMES = { "gqa_attention": "torch.sdpa/FLASH_ATTENTION/GQA", "fp8_gemm": "torch._scaled_mm/cuBLASLt/fast_accum=False", "fused_mlp": "torch.compile/max-autotune-no-cudagraphs/fullgraph", } NAMES.update(EXTENDED_NAMES) NAMES.update(SPARSE_NAMES) def resolve(case): """Baseline selection is frozen in the suite, never a candidate-side fallback.""" task = case["task"] profile = case.get("baseline_profile", "legacy") if profile == "h100-main-v081": from importlib import import_module from .kernel_v081 import REFERENCES return getattr(import_module(f".baseline_v081.{task}", __package__), task), REFERENCES[task]["baseline"] if profile == "h100-main-v06": from importlib import import_module from .main_h100 import REPLACEMENTS module, name, _tier = REPLACEMENTS[task] return getattr(import_module(f".baseline_v06.{module}", __package__), task), name if profile == "h100-expansion-v05": from .expansion import experts from .expansion.catalog import SPECS return getattr(experts, task), SPECS[task]["baseline"] if profile == "legacy": return globals()[task], NAMES[task] if task == "sparse_mla" and profile == "flashmla-sm90-ba89a346": from .h100_baselines import sparse_mla, NAME return sparse_mla, NAME raise ValueError(f"Unsupported baseline profile {profile!r} for {task!r}") def gqa_attention(q, k, v, causal): # Refuse a silent fallback to the much slower math attention backend. with sdpa_kernel(SDPBackend.FLASH_ATTENTION): return F.scaled_dot_product_attention(q, k, v, is_causal=causal, enable_gqa=True) def fp8_gemm(a, b, scale_a, scale_b): return torch._scaled_mm(a, b, scale_a, scale_b, out_dtype=torch.bfloat16, use_fast_accum=False) def eager_mlp(x, w_up, w_down): gate, up = (x @ w_up).float().chunk(2, dim=-1) return (F.silu(gate) * up).to(x.dtype) @ w_down @lru_cache(maxsize=1) def compiled_mlp(): return torch.compile(eager_mlp, fullgraph=True, mode="max-autotune-no-cudagraphs") def fused_mlp(x, w_up, w_down): return compiled_mlp()(x, w_up, w_down)