KernelBench-M / SPEC.md
Elfsong's picture
KernelBench-M artifact: rules, substrates, witnesses, pipeline, summaries
1276a5c verified
|
Raw
History Blame Contribute Delete
3.39 kB

Substrate candidate spec (for automated kernel authors)

A substrate is a CORRECT CUDA implementation of one KernelBench level1 problem, used as the mutation target. It is NOT the correctness oracle (the official PyTorch reference is). Every candidate must pass an automated gate comparing it against the official reference on multiple input suites before admission.

Deliverable

One Python file: substrates/candidate_<name>.py (snake_case short name). It must be import-safe (no GPU work at import time) and define exactly:

def register(K, torch, ctypes):
    K.PROBLEMS["<name>"] = dict(
        kb_file="<original KernelBench filename>",
        cuda=CUDA_SOURCE,          # device-only source, see rules below
        kernel_name="<kernel symbol>",
        ref=<callable>,            # torch reference, MUST match the KB Model's
                                   # forward() semantics exactly (params from
                                   # get_init_inputs() defaults)
        launch=<callable>,         # (inputs, torch) -> (out_tensor, grid3,
                                   # block3, [(ctypes_type, value), ...])
        suites=<callable>,         # () -> [(name, n_trials, builder)]
        probe=<callable>,          # (torch) -> [(name, [small cpu tensors])]
    )

CUDA source rules

  • Device code ONLY: __global__ kernel(s) + #include <cuda_fp16.h> (and <math_constants.h> if needed). NO torch headers, NO host wrapper. It is compiled with NVRTC; the launch config lives in the Python launch.
  • fp32 in/out. Use long long for any index that can exceed 2^31.
  • One thread-block pattern from the proven set when applicable: elementwise 1D grid; block-per-row with 256-thread shared-memory reduction; 32x32 shared-memory tiles for matmul; column-thread + row-loop for dim-1 reductions. Look at ../problems_batch2.py for working examples of all four patterns, including suites/probe conventions.
  • Structure the source so mutation sites are visible: keep bounds guards as single-line if (...) { ... } or if (...) return;, use ceil-division (x + K - 1) / K, label repeated __syncthreads(); with distinct trailing comments (e.g. // sync-after-load).

Suites rules

  • T0_original: EXACTLY the shapes/distribution of the problem's get_inputs() (5 trials, seed 1000 + t).
  • Plus 2-3 targeted suites: signed values, misaligned shapes (dims not divisible by 32/256), and a structure-specific stressor (spike/large values) where meaningful. Seed every builder deterministically.
  • CRITICAL: no suite may false-kill a correct implementation. Avoid large-magnitude SIGNED values feeding long accumulations (catastrophic cancellation exceeds the 1e-2 tolerance between two legitimate fp32 implementations); use positive-only values for large-magnitude suites.

Probe rules

Tiny CPU tensors (<1 MB), 2 entries: one aligned T0-like, one misaligned.

Semantics rules

  • Read the KernelBench problem file CAREFULLY: match forward() exactly (e.g. L1Norm divides by mean(|x|), NOT sum; reductions may or may not keepdim; losses reduce to a scalar with 'mean').
  • Scalar outputs: return a 0-dim or shape-(1,) tensor from launch's out and make ref produce the matching shape.
  • Integer-output ops (argmax/argmin): out dtype int64 in launch, ref returns the indices tensor; comparison is exact.