KernelBench-M / rules /mined_rules.py
Elfsong's picture
KernelBench-M artifact: rules, substrates, witnesses, pipeline, summaries
1276a5c verified
Raw
History Blame Contribute Delete
7.55 kB
"""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"),
]