File size: 5,897 Bytes
1276a5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""Structure-specific subtle-fault rules.

Motivation: on elementwise / index-output kernels our generic rules degenerate
to gross arithmetic mutations (a*b -> a+b), which any input kills. Those
kernels DO admit realistic subtle faults — they just live in different
syntax. Each rule below models a fault an LLM-written kernel plausibly makes,
and each is invisible under the benchmark's default inputs (aligned shapes,
torch.rand[0,1)) while being killable by a deliberately constructed input.

Fault families:
  A. tie-breaking in max/min/argmax (ties have measure zero under continuous
     random input; only duplicate-valued inputs expose them)
  B. approximation-constant perturbation (relative error ~1e-3 hides under
     atol=1e-2 at small magnitude, grows with |x|)
  C. branch-cutoff shifts in piecewise activations (wrong only inside the
     moved interval)
  D. single-element tail loss in elementwise guards (needs misaligned or
     spiked-tail input)
"""
from mutator import Rule

STRUCTURAL_RULES = [
    # --- A. tie-breaking ---------------------------------------------------
    Rule("argmax-tie-last", "semantic",
         r"if \((\w+) > (\w+)\) \{",
         r"if (\1 >= \2) { ",
         "argmax keeps the LAST maximum instead of the first: differs only "
         "when duplicate values exist (measure zero under rand)"),
    Rule("argmin-tie-last", "semantic",
         r"if \((\w+) < (\w+)\) \{",
         r"if (\1 <= \2) { ",
         "argmin keeps the last minimum instead of the first"),
    Rule("argmax-tie-nan-form", "semantic",
         r"if \((\w+) > (\w+) \|\| \(isnan\(",
         r"if (\1 >= \2 || (isnan(",
         "argmax with NaN handling: ties now keep the LAST index; only "
         "duplicate-valued inputs differ (measure zero under rand)"),
    Rule("argmax-nan-policy-drop", "semantic",
         r" \|\| \(isnan\((\w+)\) && !isnan\((\w+)\)\)",
         r"",
         "drop the NaN-propagation branch: differs only on NaN inputs"),
    Rule("argmax-seed-from-second", "boundary",
         r"for \(int (\w+) = 1; \1 < (\w+); \1\+\+\)",
         r"for (int \1 = 2; \1 < \2; \1++)",
         "scan starts one element late: wrong only when the true extremum "
         "sits at that position"),
    Rule("reduce-fmax-tie-order", "semantic",
         r"(\w+) = fmaxf\(\1, ([^;]+)\);",
         r"\1 = (\2 > \1) ? \2 : \1;",
         "fmaxf replaced by a strict comparison: identical except for NaN "
         "operands, where fmaxf ignores NaN but > propagates the old value"),
    Rule("reduce-fmin-tie-order", "semantic",
         r"(\w+) = fminf\(\1, ([^;]+)\);",
         r"\1 = (\2 < \1) ? \2 : \1;",
         "fminf replaced by strict comparison: NaN semantics differ"),
    Rule("reduce-seed-first-element", "boundary",
         r"float (\w+) = -CUDART_INF_F;",
         r"float \1 = 0.0f;",
         "max-reduction seeded with 0 instead of -inf: wrong only when every "
         "value in the row is negative"),
    Rule("reduce-seed-first-element-min", "boundary",
         r"float (\w+) = CUDART_INF_F;",
         r"float \1 = 0.0f;",
         "min-reduction seeded with 0: wrong only for all-positive rows"),

    # --- B. approximation constants ---------------------------------------
    Rule("gelu-tanh-const-perturb", "precision",
         r"0\.044715f",
         r"0.044615f",
         "GELU tanh-approximation cubic coefficient off in the 4th digit"),
    Rule("gelu-sqrt2pi-perturb", "precision",
         r"0\.7978845608f?",
         r"0.7968845608",
         "sqrt(2/pi) constant mistyped: relative error ~1e-3"),
    Rule("invsqrt2-perturb", "precision",
         r"0\.70710678f",
         r"0.70701678f",
         "1/sqrt(2) mistyped in the erf-based GELU"),
    Rule("selu-alpha-perturb", "precision",
         r"1\.6732632423543772f",
         r"1.6723632423543772f",
         "SELU alpha constant digit transposition"),
    Rule("selu-scale-perturb", "precision",
         r"1\.0507009873554805f",
         r"1.0057009873554805f",
         "SELU scale constant digit transposition"),
    Rule("erf-to-tanh-approx", "precision",
         r"0\.5f \* (\w+) \* \(1\.0f \+ erff\((\w+) \* 0\.70710678f\)\)",
         r"0.5f * \1 * (1.0f + tanhf(0.7978845608f * (\2 + 0.044715f * \2 * \2 * \2)))",
         "exact erf GELU replaced by the tanh approximation (~1e-3 relative)"),

    # --- C. piecewise cutoffs ---------------------------------------------
    Rule("softplus-cutoff-shift", "precision",
         r"(\w+) > 20\.0f \?",
         r"\1 > 8.0f ?",
         "softplus linear-regime threshold moved: wrong inside [8,20]"),
    Rule("relu-threshold-shift", "semantic",
         r"(\w+) > 0\.0f \? \1 :",
         r"\1 > 1e-6f ? \1 :",
         "activation threshold nudged off zero: only tiny values differ"),
    Rule("hardsigmoid-slope-perturb", "precision",
         r"/ 6\.0f \+ 0\.5f",
         r"/ 6.0f + 0.4995f",
         "hardsigmoid offset off by 5e-4: hidden by atol at small outputs"),
    Rule("clamp-bound-perturb", "precision",
         r"fminf\(fmaxf\((\w+), -1\.0f\), 1\.0f\)",
         r"fminf(fmaxf(\1, -1.0f), 0.999f)",
         "saturation bound off by 1e-3: only saturated inputs differ"),

    # --- D. elementwise tail loss -----------------------------------------
    Rule("elementwise-guard-minus1", "boundary",
         r"if \((\w+) < (n|N|size|numel|total)\) \{",
         r"if (\1 < \2 - 1) { ",
         "last element of the flat range never written: needs a spiked or "
         "misaligned tail to expose"),
    Rule("elementwise-guard-plus1", "boundary",
         r"if \((\w+) < (n|N|size|numel|total)\) \{",
         r"if (\1 < \2 + 1) { ",
         "one lane past the end writes OOB (sanitizer-visible)"),
    Rule("grid-stride-step-double", "boundary",
         r"idx \+= (blockDim\.x \* gridDim\.x)",
         r"idx += 2 * \1",
         "grid-stride loop skips every other element"),
]