File size: 7,554 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""Additional mutation rules mined from the batch-2 and accepted corpora.

These rules favor faults hidden by aligned, positive, modest-sized inputs:
activation/clamp identities, removed numerical safeguards, tail-only omissions,
premature shared-memory reads, and plausible one-site stride/bound mistakes.
Patterns are intentionally narrow so each independent substitution remains
valid CUDA and avoids the gross arithmetic mutations already in ``mutator``.
"""

from mutator import Rule


MINED_RULES = [
    # Identity substitutions: positive inputs often make these wrappers inert.
    Rule("relu-fmax-zero-remove", "semantic",
         r"fmaxf\((\w+),\s*0\.0f\)", r"\1",
         "remove a simple ReLU wrapper; differs only for negative values"),
    Rule("relu-fmax-zero-first-remove", "semantic",
         r"fmaxf\(0\.0f,\s*(\w+)\)", r"\1",
         "remove commuted ReLU wrapper; positive inputs conceal the fault"),
    Rule("upper-unit-clamp-remove", "semantic",
         r"fminf\((\w+),\s*1\.0f\)", r"\1",
         "drop a unit upper clamp, exposed by values above one"),
    Rule("upper-unit-clamp-first-remove", "semantic",
         r"fminf\(1\.0f,\s*(\w+)\)", r"\1",
         "drop a commuted unit upper clamp"),
    Rule("lower-negunit-clamp-remove", "semantic",
         r"fmaxf\((\w+),\s*-1\.0f\)", r"\1",
         "drop a lower saturation bound"),
    Rule("nested-unit-clamp-drop-upper", "semantic",
         r"fminf\((fmaxf\([^,()]+,\s*[-\w.]+\)),\s*1\.0f\)", r"\1",
         "retain the lower clamp but remove the upper saturation"),
    Rule("fabs-simple-remove", "semantic",
         r"fabsf\((\w+(?:\[\w+\])?)\)", r"\1",
         "absolute value is an identity on the original positive corpus"),

    # Numerical stability and compensation faults.
    Rule("rsqrt-epsilon-remove", "precision",
         r"rsqrtf\(([^()\n;+]{1,80})\s*\+\s*eps\)", r"rsqrtf(\1)",
         "remove the variance epsilon safeguard"),
    Rule("sqrt-epsilon-remove", "precision",
         r"sqrtf\(([^()\n;+]{1,80})\s*\+\s*eps\)", r"sqrtf(\1)",
         "remove epsilon under a square root"),
    Rule("literal-epsilon-remove", "precision",
         r"(\w+(?:\[[^]\n]+\])?)\s*\+\s*1e-0?[5-8]f", r"\1",
         "drop a literal small stabilizer from a simple expression"),
    Rule("softmax-max-subtraction-remove", "precision",
         r"expf\((\w+(?:\[[^]\n]+\])?)\s*-\s*(\w*(?:max|best)\w*)\)",
         r"expf(\1)",
         "remove max subtraction, causing overflow only at large magnitude"),
    Rule("softmax-max-subtraction-fast-remove", "precision",
         r"__expf\((\w+(?:\[[^]\n]+\])?)\s*-\s*(\w*(?:max|best)\w*)\)",
         r"__expf(\1)",
         "remove stabilization from a fast exponential"),
    Rule("welford-second-delta-reuse", "precision",
         r"float\s+delta2\s*=\s*(\w+)\s*-\s*(\w+);", r"float delta2 = delta;",
         "replace Welford's corrected second delta with the first delta"),
    Rule("variance-unbiased-count", "precision",
         r"m2s\[0\]\s*/\s*\(float\)counts\[0\]",
         r"m2s[0] / (float)(counts[0] - 1)",
         "use sample rather than population variance"),
    Rule("kahan-compensation-drop", "precision",
         r"(\w+)\s*=\s*(\w+)\s*-\s*(\w*(?:comp|correction)\w*)\s*;",
         r"\1 = \2;",
         "remove a simple Kahan compensation subtraction"),

    # Tail and last-iteration faults that aligned dimensions can conceal.
    Rule("last-row-reduction-skip", "boundary",
         r"for \(int (\w+) = 0; \1 < (\w+); \1\+\+\)",
         r"for (int \1 = 0; \1 + 1 < \2; \1++)",
         "skip only the final item of a simple reduction loop"),
    Rule("last-row-reduction-skip-start1", "boundary",
         r"for \(int (\w+) = 1; \1 < (\w+); \1\+\+\)",
         r"for (int \1 = 1; \1 + 1 < \2; \1++)",
         "omit the last item while retaining a separately seeded first item"),
    Rule("strided-tail-drop", "boundary",
         r"(\w+)\s*<\s*(\w+);\s*\1\s*\+=\s*blockDim\.x",
         r"\1 + blockDim.x < \2; \1 += blockDim.x",
         "drop the final strided chunk of a reduction or output pass"),
    Rule("tile-last-iteration-skip", "boundary",
         r"for \(int (\w+) = 0; \1 < (\w*[Tt]iles\w*); \1\+\+\)",
         r"for (int \1 = 0; \1 + 1 < \2; \1++)",
         "omit only the final matrix tile"),
    Rule("literal-loop-last-skip", "boundary",
         r"for \(int (\w+) = 0; \1 < (\d+); \1\+\+\)",
         r"for (int \1 = 0; \1 + 1 < \2; \1++)",
         "omit the last tap of a fixed-size pooling or convolution loop"),
    Rule("tail-guard-tighten", "boundary",
         r"if \((\w+) < (\w+)\) \{", r"if (\1 + 1 < \2) {",
         "reject exactly the final valid scalar in a guarded tail"),

    # Shared-memory staging and reduction faults.
    Rule("shared-denom-use-local", "sync",
         r"float denom = sdata\[0\] / dim;", r"float denom = local_sum / dim;",
         "consume the per-thread value instead of the reduced shared value"),
    Rule("shared-sqrt-use-local", "sync",
         r"float denom = sqrtf\(sdata\[0\]\);", r"float denom = sqrtf(local_sum);",
         "read the local accumulator in place of the synchronized reduction"),
    Rule("reduction-barrier-move-before-store", "sync",
         r"(sdata\[tid\]\s*=\s*[^;]+;)\s*\n\s*__syncthreads\(\);",
         r"__syncthreads();\n    \1",
         "move the barrier before the shared-memory publication"),
    Rule("welford-count-merge-early", "sync",
         r"counts\[tid\]\s*=\s*combined_count;", r"counts[tid + width] = combined_count;",
         "publish a merged count to the partner lane instead of the consumer"),
    Rule("reduction-partner-half-offset", "indexing",
         r"sdata\[tid \+ s\]", r"sdata[tid + (s >> 1)]",
         "read the wrong partner within one tree-reduction stage"),

    # One-site dimension, stride, and flattening mistakes.
    Rule("row-store-stride-k", "indexing",
         r"C\[\(long long\)row \* N \+ col\]", r"C[(long long)row * K + col]",
         "use the reduction dimension as one output row stride"),
    Rule("a-row-stride-n", "indexing",
         r"A\[\(long long\)row \* K \+ a_k\]", r"A[(long long)row * N + a_k]",
         "use N rather than K for one matrix-A row stride"),
    Rule("b-row-stride-k", "indexing",
         r"B\[\(long long\)b_k \* N \+ col\]", r"B[(long long)b_k * K + col]",
         "use K rather than N for one matrix-B row stride"),
    Rule("matrix-row-bound-use-n", "boundary",
         r"row < M", r"row < N",
         "guard matrix rows with the column dimension"),
    Rule("matrix-col-bound-use-m", "boundary",
         r"col < N", r"col < M",
         "guard matrix columns with the row dimension"),
    Rule("height-bound-use-width", "boundary",
         r"input_y < input_height", r"input_y < input_width",
         "use width as the vertical edge bound"),
    Rule("width-bound-use-height", "boundary",
         r"input_x < input_width", r"input_x < input_height",
         "use height as the horizontal edge bound"),
    Rule("flatten-height-use-width", "indexing",
         r"\* input_height \+ input_y", r"* input_width + input_y",
         "substitute width for one height stride in a flattened index"),
    Rule("output-position-height-first", "indexing",
         r"position % output_width", r"position % output_height",
         "decode a flattened x coordinate with the wrong extent"),
    Rule("grid-x-use-block-y", "indexing",
         r"blockIdx\.x \* blockDim\.x \+ threadIdx\.x",
         r"blockIdx.x * blockDim.y + threadIdx.x",
         "use the other block extent in a linear global index"),
]