KBench / tools /factory /gate_probe.sh
ZMC2019's picture
Reorganise: group 313 tasks into 17 families under tasks/, generators under tools/ (part 3)
a4a97ba verified
Raw
History Blame Contribute Delete
4.81 kB
#!/bin/bash
# Empirical test that each correctness gate actually DISCRIMINATES.
#
# The grader compares by relative Frobenius error against TOL, so a submission that returns the
# reference plus noise of a known relative magnitude lands at a known relerr. Two runs per task:
#
# noise = 0.5 x TOL -> MUST pass (a gate that fails here rejects correct kernels)
# noise = 2.0 x TOL -> MUST fail (a gate that passes here accepts wrong kernels)
#
# Integer/bool outputs are compared exactly and are deliberately left unperturbed; a task whose
# outputs are ALL integer is reported as NOISE-IMMUNE rather than silently "passing".
#
# KNOWN LIMITATIONS -- a flag from this probe is not automatically a task defect. Verified cases where
# the probe, not the task, is wrong:
# * SCALAR outputs (a loss). Multiplicative noise gives relerr = |randn|*S, which exceeds 2S about
# 5% of the time, so across several graded shapes it reads as a false TOO TIGHT. Handled: tensors
# of <= 4 elements are perturbed by exactly S.
# * fp8 outputs. e4m3 eps is 0.125, so a 1% injected perturbation is 8x BELOW one ULP: most elements
# round straight back and the ones near a boundary flip a whole 12.5% code. Measured relerr is set
# by the flip FRACTION, not the injected magnitude, so fp8-output tasks report TOO TIGHT spuriously.
# * tol far below the output dtype's epsilon (e.g. 1e-4 on a bf16 output, eps 7.8e-3). The injected
# noise rounds away entirely at 2x tol, giving a false NO DISCRIMINATION; the 100x probe separates
# this from a genuinely dead gate.
# * ROWWISE comparators, if the noise is scaled by the global norm rather than multiplicatively.
set -u
LANE=/home/zhuominc/MLE-Bench/mle_tasks/kernel-generation/kernels
GPU=${GPU:-1}
for T in $(cat "$1"); do
D=$LANE/$T
V=$D/tests/verify_env.py
[ -f "$V" ] || { echo "$T :: NO GRADER"; continue; }
TOL=$(grep -oP '(?<=^TOL = )[0-9.eE+-]+' $V | head -1)
FN=$(grep -oP '(?<=fn = m\.)\w+' $V | head -1)
MOD=$(grep -oP '(?<=^COPY )\S+\.py(?= /app/)' $D/environment/Dockerfile | tail -1)
if [ -z "${TOL:-}" ] || [ -z "${FN:-}" ] || [ -z "${MOD:-}" ]; then
echo "$T :: UNPARSEABLE (tol=${TOL:-?} fn=${FN:-?} mod=${MOD:-?})"; continue
fi
case "$TOL" in 0|0.0) echo "$T :: TOL=0 exact-only, skipped"; continue;; esac
SH=/tmp/gp.$$.$T.py
cat > $SH <<EOF
import os, sys, torch
sys.path.insert(0, "/app")
import reference as _R
_f = getattr(_R, "$FN")
S = float(os.environ.get("GP_NOISE", "0"))
_SEEN = {"float": 0, "exact": 0}
def _pert(t):
# MULTIPLICATIVE noise: relative error is ~S both globally AND per row. Scaling by the global
# norm instead would dump disproportionate error into low-norm rows and spuriously fail
# rowwise comparators (ROW_PASS), which is not what we are testing.
if not torch.is_tensor(t) or not t.is_floating_point():
_SEEN["exact"] += 1
return t
_SEEN["float"] += 1
f = t.float()
if f.numel() <= 4:
# A SCALAR output (a loss) has relerr = |randn|*S under multiplicative noise, which exceeds
# 2*S about 5% of the time -- across several graded shapes that reads as a false "too tight".
# Perturb it by exactly S instead, so the injected relative error is deterministic.
return (f * (1.0 + S)).to(t.dtype)
return (f + f * torch.randn_like(f) * S).to(t.dtype)
def $FN(*a, **k):
o = _f(*a, **k)
r = type(o)(_pert(x) for x in o) if isinstance(o, (tuple, list)) else _pert(o)
if _SEEN["float"] == 0 and _SEEN["exact"]:
sys.stderr.write("GP_ALL_EXACT\n")
return r
EOF
run() {
docker run --rm --gpus device=$GPU -e GP_NOISE=$1 \
-v $D/tests:/tests:ro -v $D/environment/reference.py:/app/reference.py:ro \
-v $SH:/app/$MOD:ro mle-gp-$T bash -c 'bash /tests/test.sh 2>/dev/null | tail -1' 2>/dev/null \
| grep -oP '(?<=correct: )[01]\.[0-9]' | head -1
}
docker build -q -t mle-gp-$T $D/environment >/dev/null 2>&1 || { echo "$T :: BUILD FAILED"; rm -f $SH; continue; }
LO=$(python3 -c "print($TOL*0.5)")
HI=$(python3 -c "print($TOL*2.0)")
A=$(run $LO); B=$(run $HI)
VERDICT="ok"; C=""
[ "${A:-x}" != "1.0" ] && VERDICT="TOO TIGHT (0.5xtol rejected)"
if [ "${B:-x}" != "0.0" ]; then
# Either the outputs are all integer (compared exactly, so float noise is a no-op) or the gate
# genuinely does not bite. A 100x probe separates the two.
C=$(run $(python3 -c "print($TOL*100)"))
if [ "${C:-x}" = "1.0" ]; then VERDICT="NOISE-IMMUNE (exact/integer outputs; probe N/A)"
else VERDICT="NO DISCRIMINATION (2xtol accepted, 100x rejected)"; fi
fi
printf "%-38s tol=%-8s lo=%-4s hi=%-4s %s\n" "$T" "$TOL" "${A:-?}" "${B:-?}" "$VERDICT"
docker rmi -f mle-gp-$T >/dev/null 2>&1
rm -f $SH
done
echo "GATEPROBE_DONE_$GPU"