File size: 9,078 Bytes
5df54a2
338c91e
 
d1f1ebf
338c91e
 
 
5df54a2
338c91e
 
 
 
 
 
 
 
5df54a2
d1f1ebf
 
 
 
 
 
 
 
 
 
 
75d1863
 
d1f1ebf
 
 
75d1863
d1f1ebf
 
 
 
 
 
 
 
 
75d1863
d1f1ebf
75d1863
 
 
 
d1f1ebf
75d1863
 
 
 
d1f1ebf
 
 
 
75d1863
d1f1ebf
 
 
75d1863
d1f1ebf
 
75d1863
d1f1ebf
 
 
 
75d1863
d1f1ebf
75d1863
 
 
 
 
 
 
 
 
d1f1ebf
 
 
 
75d1863
 
 
 
 
 
 
 
 
 
 
d1f1ebf
 
75d1863
 
 
 
 
 
 
 
 
 
 
d1f1ebf
 
 
 
 
75d1863
 
d1f1ebf
 
75d1863
 
 
d1f1ebf
 
75d1863
 
d1f1ebf
 
75d1863
 
 
 
 
 
 
 
 
 
 
 
d1f1ebf
 
 
 
 
 
 
 
75d1863
d1f1ebf
75d1863
 
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
144
145
146
147
148
149
150
151
152
---
license: [apache-2.0, bsd-3-clause, mit]
language: [en, code]
pretty_name: Kernel-Opt RL (vLLM / SGLang / FlashInfer)
size_categories: [10K<n<100K]
task_categories: [text-generation]
tags: [cuda, triton, gpu-kernels, kernel-optimization, reinforcement-learning, autotuning, vllm, sglang, flashinfer]
configs:
  - config_name: tuned_configs
    data_files:
      - {split: train, path: tuned_configs/train-*}
      - {split: test, path: tuned_configs/test-*}
  - config_name: kernel_commits
    data_files:
      - {split: train, path: kernel_commits/train-*}
      - {split: test, path: kernel_commits/test-*}
---

# Kernel-Opt RL

Real-world GPU kernel optimization data mined from production LLM inference engines
([vLLM](https://github.com/vllm-project/vllm), [SGLang](https://github.com/sgl-project/sglang),
[FlashInfer](https://github.com/flashinfer-ai/flashinfer)). It's built for RL: given a base kernel and a
concrete deployment (model shape, GPU, dtype, batch size), choose the optimization.

Inference engines ship one generic kernel and specialize it per model and per GPU. Most of that
specialization is not new code. It is **autotuned launch parameters** checked in as JSON
(`E=8,N=14336,device_name=NVIDIA_H100_80GB_HBM3.json`), plus a stream of **perf commits** that rewrite
kernels for specific models and hardware. This dataset turns both into RL tasks, with the **benchmark
gains reported in each PR description** extracted as structured measurements.

## Subsets

### `tuned_configs`: kernel + deployment → best launch config

| | |
|---|---|
| rows | 19,287 (17,561 train / 1,726 test) |
| tuned config files | 1,138 |
| GPUs | 48 (NVIDIA A100 → B300/GB300, AMD MI300X/MI325X/MI355X, Intel Arc, …) |
| kernels | `fused_moe_kernel` (vLLM + SGLang), `fused_moe_kernel_gptq_awq`, `_w8a8_triton_block_scaled_mm`, `_w8a8_block_fp8_matmul`, `_w8a8_block_int8_matmul`, `_selective_scan_update_kernel` |
| dtypes | bf16/fp16, fp8_w8a8 (per-tensor + 128×128 block), int8_w8a8, int8_w8a16, int4_w4a16 |
| M (tokens) | 1 … 262,144 |
| documented gains | 148 tuning PRs traced, 66 with benchmark tables; `speedup_vs_default` on 399 rows from 46 config files (median 1.30×, p10 1.03×, p90 1.98×) |

One row is one (config file, batch size M) entry. `prompt` contains the Triton kernel source, the
hardware, the problem shape and the MoE models whose shape matches under TP/EP (`candidate_models`,
resolved against 32 MoE model configs on the Hub). It asks for a JSON config. `best_config` is the
autotuned answer.

Gain fields: `config_pr_url` is the newest PR that added or changed the config file. `config_pr_gains`
holds all measurements extracted from that PR. `speedup_vs_default` is the documented speedup of the
tuned config over vLLM/SGLang's default heuristic at this exact M. It is set only when the attribution
is unambiguous: the GPU and shape match, and multiple matching rows agree within 5%.

**Split:** grouped by config filename (hash), so a given (shape, GPU, dtype) never appears in both
train and test, whatever the repo, Triton version or M.

### `kernel_commits`: optimize a kernel file the way the real perf PR did

| | |
|---|---|
| rows | 1,517 file-level tasks (1,365 train / 152 test) from 850 PRs |
| languages | CUDA (`.cu/.cuh`), C++, Triton, CuTe DSL |
| date range | 2023-04 → 2026-10 |
| documented gains | 529 PRs with benchmark numbers → 913 rows; `kernel_speedup` on 596 rows (median 1.33×, p10 1.02×, p90 4.25×), `e2e_speedup` on 556 rows |

Selected from non-merge commits that touch kernel directories and carry a performance signal in the
title (`[Perf]`, optimize, speedup, faster, tune, throughput, latency, …), excluding build, CI, docs,
tests and reverts. Only modified kernel files are kept: ≤10 per commit, ≤120 KB per file, ≤800 changed
lines.

`prompt` gives the goal (the PR title, which plays the role of the issue in SWE-RL), the target hardware
and models, and the full `before_code`. It asks for **SEARCH/REPLACE edits**. The PR body is withheld
from the prompt because it usually describes the fix. The reference answer is `after_code` / `diff`.
Prompt lengths: median ~21K chars (~6K tokens), p90 ~74K chars. Filter on `len(prompt)` for short-context
RL.

Other fields: `models_mentioned`, `hardware_mentioned`, `speedup_claims` (regex over the commit
message), `pr_body`, `gains` (structured measurements from the PR), `kernel_speedup` / `e2e_speedup`
(median documented speedup at kernel and end-to-end scope), `license`.

**Split:** temporal. The newest ~10% of rows are test (2026-10-18 onward), the code least likely to be in
a model's pretraining data.

### `gains` / `config_pr_gains` format

A JSON list of measurements extracted from the PR description:
`{scope: "kernel"|"e2e", hardware, model, shape, batch_size, metric, unit, higher_is_better, baseline,
optimized, stated_speedup, speedup}`. `speedup` is computed in code from baseline/optimized and
normalized so that > 1 means the PR is better. When a PR states only a ratio, `stated_speedup` is used.
Extraction was done by `Qwen3-235B-A22B-Instruct-2507`, with `gpt-oss-120b` as a fallback for very long
tables, using an extract-only prompt (no computed numbers). Audit: in 98.8% of extracted
baseline/optimized pairs, both values appear verbatim in the PR text. The remainder were mostly averages
of repeated runs. Extraction failed for 5 PRs (malformed model output), which have empty `gains`.

## Using it for RL

Rewards live in [`code/reward.py`](code/reward.py):

- **`tuned_configs`**: `config_reward` is deliberately a stub, because the partial-credit scheme is a design choice. Compare the parsed JSON config to `best_config` as an offline proxy (exact match is
  sparse, so use partial credit or log-distance on the power-of-two tile sizes). `speedup_vs_default`
  tells you how much a config actually matters for that row and can weight the reward. On hardware,
  launch `kernel_source` with the predicted config and reward `t_best / t_pred`. The tuned config is
  only the best within the tuner's search space, so it can be beaten.
- **`kernel_commits`**: `patch_reward(completion, before_code, after_code)` follows SWE-RL. It returns −1 if
  the edits are malformed or don't apply, and otherwise the `difflib` similarity between the changed lines
  and the real commit's. Validated on this data: the oracle edit scores 1.0 on 98.7% of rows and ≥ 0.95 on
  all but one. For an execution reward, run through an environment such as
  [KernelGYM](https://github.com/hkust-nlp/KernelGYM). Watch for reward hacking (timer exploits, cached
  outputs; see the [CUDA-L1](https://github.com/ornith-ai/CUDA-L1) notes).

```python
from datasets import load_dataset
ds = load_dataset("guychuk/kernel-opt-rl", "kernel_commits", split="train")
ds = ds.filter(lambda r: len(r["prompt"]) < 32_000 and r["kernel_speedup"])  # short, with documented gains
```

Both subsets have a plain-string `prompt` column. TRL's `GRPOTrainer` forwards the other columns
(`best_config`, `before_code`, `after_code`, …) to reward functions as keyword arguments.

## Limitations

- Tuned configs record only the *winner*, not the full latency table, so offline config rewards are a
  proxy. `speedup_vs_default` covers only rows whose PR documented that exact M.
- `candidate_models` comes from shape matching (`E == experts`, `N == intermediate / TP`, or
  `E == experts / EP`). Shapes are shared across models, e.g. DeepSeek-V3, Kimi-K2 and MiMo-V2-Flash.
- PR gains are self-reported by the authors, on their hardware and benchmark setup, and are not reproduced.
- Snapshots are from each repo's HEAD at build time (see `commit`). Rebuild for fresher data.

## Licensing

All code comes from vLLM, SGLang and FlashInfer (all Apache-2.0). Some vendored files carry their own
licenses. Each row has a `license` field taken from the file header (Apache-2.0: 1,481 commit rows and all
config rows; BSD-3-Clause / BSD: 35; MIT: 1). Files under non-open-source licenses (the NVIDIA TensorRT
Source Code License Agreement and NVIDIA proprietary headers) are **excluded**. License and copyright
headers in the code are kept verbatim. The upstream `LICENSE` and `NOTICE` files are in
[`licenses/`](licenses/), as Apache-2.0 §4 requires. The compilation itself is released under
Apache-2.0. PR descriptions are quoted from public GitHub PRs and linked via `pr_url`.

## Related datasets

[KernelBench](https://huggingface.co/datasets/ScalingIntelligence/KernelBench) (eval; keep held out),
[KernelBook](https://huggingface.co/datasets/GPUMODE/KernelBook) (PyTorch→Triton pairs),
[Nemotron-SFT-CUDA-v1](https://huggingface.co/datasets/nvidia/Nemotron-SFT-CUDA-v1) (CUDA SFT with tests),
[CUDA-L1](https://github.com/ornith-ai/CUDA-L1) (RL-optimized KernelBench solutions per GPU).

## Reproduce

The build code is in [`code/`](code/). `uv run build.py --push <repo_id>` clones the three repos as blobless clones, mines configs and commits,
fetches PR descriptions with `gh`, extracts gains via HF Inference Providers (cached), and pushes.