QianQu's picture
Upload folder using huggingface_hub
902d2bb verified
Raw History Blame Contribute Delete
4.94 kB
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: CC-BY-NC-4.0
"""Keep the declared precision: a static scan for math below the problem's declared dtypes."""
from __future__ import annotations
import re
from .. import harness
# The rule (as in the paper, after the benchmark paper's own rule):
# - fp32 problems: TF32, fp16 / bf16, FP8 / FP4 / int8 math, fp16 accumulation, and fp32
# emulation (3xTF32, BF16x9, Ozaki-style splits) are "reduced": such a kernel counts only
# once the robustness check says ROBUST (the harness already keeps the I/O dtypes);
# - bf16 / fp16 problems: going below the declared type (FP8 / FP4 / int8, fp16 accumulation)
# is a violation, whatever the evidence;
# - problems that declare quantized types, and the Quantization board, are exempt.
# Code is read without comments, docstrings or `#if 0` blocks. String literals stay: inline PTX
# and a Python solution's CUDA live in strings.
MARKERS = {
"tf32": r"kind::tf32|\.tf32\b|\btfloat32_t\b|_TF32\b|TF32TF32|allow_tf32\s*=\s*True"
r"|float32_matmul_precision\(\s*['\"](?:high|medium)"
r"|input_precision\s*=\s*['\"]tf32['\"]",
"fp16": r"kind::f16|\b__half2?\b|\bhalf2\b|\.f16(?:x2)?\b|torch\.(?:float16|half)\b|\.half\(\)"
r"|tl\.float16\b|\bCUDA_R_16F\b|__float2half|\bautocast\b",
"bf16": r"\b__nv_bfloat162?\b|\.bf16(?:x2)?\b|torch\.bfloat16\b|\.bfloat16\(\)"
r"|tl\.bfloat16\b|\bCUDA_R_16BF\b|\bbfloat16_t\b|__float2bfloat16",
"fp8": r"(?<![A-Za-z0-9])u?e[45]m[23](?![0-9])|kind::(?:mx)?f8|float8_|__nv_fp8|\bCUDA_R_8F",
"fp4": r"(?<![A-Za-z0-9])e2m1(?![0-9])|kind::mxf4|\bnvf4|__nv_fp4|float4_e2m1",
"int8": r"kind::i8|\.s8\.s8|__dp4a|\bCUDA_R_8I\b",
"half_accum": r"mma\.sync[^\n\"]*\.f16\.f16\.f16\.f16|\bCUBLAS_COMPUTE_16F\b"
r"|wmma::accumulator[^>;\n]*half",
"emulation": r"3xtf32|tf32x3|bf16x9|ozaki|OpMultiplyAddFastF32|EMULATE_SINGLE_PRECISION",
}
_MARKERS = [(k, re.compile(p, re.I if k == "emulation" else 0)) for k, p in MARKERS.items()]
REDUCED = {"fp32": frozenset({"tf32", "fp16", "bf16", "fp8", "fp4", "int8", "half_accum"}),
"half": frozenset({"fp8", "fp4", "int8", "half_accum"})}
# Lines that only talk about a dtype (a guard or a message) do not compute in it.
_TALK = re.compile(r"TORCH_CHECK|static_assert|\bassert\b|\braise\b|\bprint\(|\bprintf\(|\bthrow\b"
r"|dtype\s*(?:[!=]=|in\b|not\s+in\b)|scalar_type\(\)\s*[!=]=")
_VENDORED = re.compile(r"(?:^|/)(?:cutlass|cute|cub|thrust)/")
def declared_class(defn: dict) -> str:
"""'fp32', 'half' (any bf16 / fp16 tensor) or 'exempt' (quantized, or no float tensors)."""
dts = [str(t.get("dtype")) for part in ("inputs", "outputs")
for t in (defn.get(part) or {}).values() if isinstance(t, dict)]
if any(d.startswith(("float8", "float4")) or d == "int8" for d in dts):
return "exempt"
if any(d in ("float16", "bfloat16") for d in dts):
return "half"
return "fp32" if any(d in ("float32", "float64") for d in dts) else "exempt"
def scan(sol: dict) -> list:
"""[kind, path, line] for every marker found in the solution's code."""
found = []
for s in sol.get("sources") or []:
path, text = str(s.get("path") or ""), str(s.get("content") or "")
if _VENDORED.search(path):
continue
for i, line in enumerate(harness.code_only(path, text).split("\n"), 1):
if not line.strip() or _TALK.search(line):
continue
found.extend([k, path, i] for k, rx in _MARKERS if rx.search(line))
return found
def check(sol: dict, defn: dict, exempt: bool = False) -> dict:
"""{"class", "verdict": clean | reduced | violation | exempt, "findings"} for one solution."""
cls = "exempt" if exempt else declared_class(defn)
if cls == "exempt":
return {"class": cls, "verdict": "exempt", "findings": []}
found = scan(sol)
bad = {k for k, _, _ in found} & REDUCED[cls]
if cls == "fp32":
emul = any(k == "emulation" for k, _, _ in found)
verdict = "reduced" if bad or emul else "clean"
keep = bad | ({"emulation"} if emul else set())
else:
verdict = "violation" if bad else "clean"
keep = bad
return {"class": cls, "verdict": verdict, "findings": [f for f in found if f[0] in keep][:20]}
def counts(verdict: str | None, robustness: str | None, policy: str = "paper") -> bool | None:
"""True: the kernel counts; False: it never counts; None: pending until a ROBUST verdict."""
if policy == "off" or verdict in (None, "clean", "exempt"):
return True
if verdict == "violation" or policy == "declared": # "declared": no reduced precision at all
return False
if robustness == "ROBUST":
return True
return False if robustness == "NOT_ROBUST" else None