Text Generation
Transformers
Safetensors
qwen3
triton
kernel-generation
reinforcement-learning
code
conversational
text-generation-inference
PeterV09 commited on
Commit
990fe42
·
verified ·
1 Parent(s): 668218b

upload ckpt

Browse files
Files changed (1) hide show
  1. README.md +298 -0
README.md ADDED
@@ -0,0 +1,298 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ pipeline_tag: text-generation
4
+ base_model: Qwen/Qwen3-8B-Base
5
+ tags:
6
+ - qwen3
7
+ - triton
8
+ - kernel-generation
9
+ - reinforcement-learning
10
+ - code
11
+ datasets:
12
+ - hkust-nlp/drkernel-coldstart-8k
13
+ - hkust-nlp/drkernel-rl-data
14
+ - hkust-nlp/drkernel-validation-data
15
+ ---
16
+
17
+ # DR.Kernel-8B
18
+
19
+ [![Model](https://img.shields.io/badge/🤗%20Model-hkust--nlp/drkernel--8b-yellow)](https://huggingface.co/hkust-nlp/drkernel-8b)
20
+ [![Paper](https://img.shields.io/badge/arXiv-2602.05885-b31b1b)](https://arxiv.org/abs/2602.05885)
21
+
22
+ `hkust-nlp/drkernel-8b` is a Qwen3-8B-based model specialized for GPU kernel generation and optimization (especially Triton) in the DR.Kernel framework.
23
+
24
+ It is trained for iterative optimization with execution feedback from KernelGYM, rather than single-shot code generation only.
25
+
26
+ ## Model Summary
27
+
28
+ - Model type: `Qwen3ForCausalLM`
29
+ - Parameter count: `8,190,735,360` (from `model.safetensors.index.json`)
30
+ - Weight dtype: BF16
31
+ - Base model family: Qwen3-8B
32
+ - Main capability: generate and iteratively refine optimized `ModelNew` kernel implementations from PyTorch reference tasks
33
+
34
+
35
+ ## Training Recipe (DR.Kernel)
36
+
37
+ The 8B model follows the same two-stage DR.Kernel pipeline:
38
+
39
+ 1. Cold-start SFT
40
+ - Dataset: `hkust-nlp/drkernel-coldstart-8k`
41
+ - Multi-turn trajectory warm-up for kernel generation/refinement
42
+ 2. Multi-turn RL
43
+ - Train dataset: `hkust-nlp/drkernel-rl-data`
44
+ - Validation dataset: `hkust-nlp/drkernel-validation-data` (KernelBench Level 2 validation split)
45
+ - Core methods: TRLOO + MRS + PR + PRS
46
+ - Execution environment: KernelGYM with compilation/correctness/performance/profiling feedback
47
+
48
+ Related training scripts in this repo:
49
+
50
+ - `drkernel/kernel/scripts/sft/8b-coldstart.sh`
51
+ - `drkernel/kernel/scripts/rl/8b_trloo_mrs_pr_prs.sh`
52
+
53
+ ## Intended Use
54
+
55
+ - Kernel generation research and benchmarking
56
+ - Triton kernel optimization with iterative feedback
57
+ - Multi-turn agentic code refinement under execution-based reward
58
+
59
+ ## Not Intended Use
60
+
61
+ - Safety-critical production deployment without additional verification
62
+ - General-purpose coding assistant use where kernel-evaluation feedback is unavailable
63
+
64
+ ## Prompting Format
65
+
66
+ The model is trained with kernel-optimization prompts that:
67
+
68
+ - Provide a PyTorch reference architecture (`Model`, `get_inputs`, `get_init_inputs`)
69
+ - Require returning an optimized `ModelNew`
70
+ - In multi-turn settings, append server feedback and request iterative improvement
71
+
72
+ For best behavior, keep the same task style as DR.Kernel datasets and use chat-format messages.
73
+
74
+ ## Quick Start (Transformers)
75
+
76
+ Use the same fixed 1-shot first-turn prompt template as DR.Kernel data (recommended):
77
+
78
+ ````python
79
+ import textwrap
80
+ import torch
81
+ from transformers import AutoModelForCausalLM, AutoTokenizer
82
+
83
+ model_id = "hkust-nlp/drkernel-8b"
84
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
85
+ model = AutoModelForCausalLM.from_pretrained(
86
+ model_id,
87
+ torch_dtype=torch.bfloat16,
88
+ device_map="auto",
89
+ trust_remote_code=True,
90
+ )
91
+
92
+ ref_code = textwrap.dedent(
93
+ """
94
+ import torch
95
+ import torch.nn as nn
96
+
97
+ class Model(nn.Module):
98
+ def __init__(self):
99
+ super().__init__()
100
+
101
+ def forward(self, x):
102
+ x = torch.abs(x)
103
+ x = x - 1.0
104
+ return x
105
+
106
+ def get_inputs():
107
+ return [torch.randn(64, 128)]
108
+
109
+ def get_init_inputs():
110
+ return []
111
+ """
112
+ ).strip()
113
+
114
+ example_ref_code = textwrap.dedent(
115
+ """
116
+ import torch
117
+ import torch.nn as nn
118
+ import torch.nn.functional as F
119
+
120
+ class Model(nn.Module):
121
+ def __init__(self) -> None:
122
+ super().__init__()
123
+
124
+ def forward(self, a, b):
125
+ return a + b
126
+
127
+ def get_inputs():
128
+ # randomly generate input tensors based on the model architecture
129
+ a = torch.randn(1, 128).cuda()
130
+ b = torch.randn(1, 128).cuda()
131
+ return [a, b]
132
+
133
+ def get_init_inputs():
134
+ # randomly generate tensors required for initialization based on the model architecture
135
+ return []
136
+ """
137
+ ).strip()
138
+
139
+ example_kernel_code = textwrap.dedent(
140
+ '''
141
+ import torch
142
+ import torch.nn as nn
143
+ import torch.nn.functional as F
144
+ import triton
145
+ import triton.language as tl
146
+
147
+ @triton.jit
148
+ def add_kernel(
149
+ x_ptr, # Pointer to first input
150
+ y_ptr, # Pointer to second input
151
+ out_ptr, # Pointer to output
152
+ n_elements, # Total number of elements in input/output
153
+ BLOCK_SIZE: tl.constexpr,
154
+ ):
155
+ # Each program handles a contiguous block of data of size BLOCK_SIZE
156
+ block_start = tl.program_id(0) * BLOCK_SIZE
157
+ # Create a range of offsets [0..BLOCK_SIZE-1]
158
+ offsets = block_start + tl.arange(0, BLOCK_SIZE)
159
+ # Mask to ensure we don't go out of bounds
160
+ mask = offsets < n_elements
161
+ # Load input values
162
+ x = tl.load(x_ptr + offsets, mask=mask, other=0.0)
163
+ y = tl.load(y_ptr + offsets, mask=mask, other=0.0)
164
+ # Perform the elementwise addition
165
+ out = x + y
166
+ # Store the result
167
+ tl.store(out_ptr + offsets, out, mask=mask)
168
+
169
+ def triton_add(x: torch.Tensor, y: torch.Tensor):
170
+ """
171
+ This function wraps the Triton kernel call. It:
172
+ 1. Ensures the inputs are contiguous on GPU.
173
+ 2. Calculates the grid (blocks) needed.
174
+ 3. Launches the Triton kernel.
175
+ """
176
+ assert x.is_cuda and y.is_cuda, "Tensors must be on CUDA."
177
+ x = x.contiguous()
178
+ y = y.contiguous()
179
+
180
+ # Prepare output tensor
181
+ out = torch.empty_like(x)
182
+
183
+ # Number of elements in the tensor
184
+ n_elements = x.numel()
185
+ BLOCK_SIZE = 128 # Tunable parameter for block size
186
+
187
+ # Determine the number of blocks needed
188
+ grid = lambda meta: ((n_elements + meta["BLOCK_SIZE"] - 1) // meta["BLOCK_SIZE"],)
189
+
190
+ # Launch the Triton kernel
191
+ add_kernel[grid](x, y, out, n_elements, BLOCK_SIZE=BLOCK_SIZE)
192
+ return out
193
+
194
+ class ModelNew(nn.Module):
195
+ def __init__(self) -> None:
196
+ super().__init__()
197
+
198
+ def forward(self, a, b):
199
+ # Instead of "return a + b", call our Triton-based addition
200
+ return triton_add(a, b)
201
+ '''
202
+ ).strip()
203
+
204
+ prompt_template = textwrap.dedent(
205
+ """\
206
+ You write custom Triton kernels to replace the pytorch operators in the given architecture to get speedups.
207
+
208
+ You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom Triton kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination.
209
+
210
+ Here's an example to show you the syntax of inline embedding custom Triton kernels in torch: The example given architecture is:
211
+
212
+ ```python
213
+ {example_ref_code}
214
+ ```
215
+
216
+ The example new arch with custom Triton kernels looks like this:
217
+
218
+ ```python
219
+ {example_kernel_code}
220
+ ```
221
+
222
+ You are given the following architecture:
223
+ ```python
224
+ {ref_code}
225
+ ```
226
+
227
+ Optimize the architecture named Model with custom Triton operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Let's think step by step.
228
+ """
229
+ ).strip()
230
+
231
+ prompt = prompt_template.format(
232
+ example_ref_code=example_ref_code,
233
+ example_kernel_code=example_kernel_code,
234
+ ref_code=ref_code,
235
+ )
236
+ messages = [{"role": "user", "content": prompt}]
237
+
238
+ inputs = tokenizer.apply_chat_template(
239
+ messages,
240
+ add_generation_prompt=True,
241
+ return_tensors="pt",
242
+ ).to(model.device)
243
+
244
+ with torch.no_grad():
245
+ outputs = model.generate(
246
+ inputs,
247
+ max_new_tokens=2048,
248
+ do_sample=True,
249
+ temperature=1.0,
250
+ top_p=1.0,
251
+ )
252
+
253
+ # Only print newly generated tokens
254
+ print(tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=False))
255
+ ````
256
+
257
+ ## Evaluation
258
+
259
+ Use KernelGYM-based evaluation scripts in this repo:
260
+
261
+ - `drkernel/kernel/scripts/eval/drkernel-14b-maxturns3.sh` (set model path to `hkust-nlp/drkernel-8b`)
262
+ - `drkernel/kernel/scripts/eval/grading_common.sh` for custom evaluation runs
263
+
264
+ Validation data:
265
+
266
+ - `hkust-nlp/drkernel-validation-data` (KernelBench Level 2 validation tasks)
267
+
268
+ ## Data and Attribution
269
+
270
+ - Query/task source includes:
271
+ - [ByteDance-Seed/cudaLLM-data](https://huggingface.co/datasets/ByteDance-Seed/cudaLLM-data)
272
+ - SFT cold-start trajectories:
273
+ - [hkust-nlp/drkernel-coldstart-8k](https://huggingface.co/datasets/hkust-nlp/drkernel-coldstart-8k)
274
+ - RL train data:
275
+ - [hkust-nlp/drkernel-rl-data](https://huggingface.co/datasets/hkust-nlp/drkernel-rl-data)
276
+ - Validation/eval data:
277
+ - [hkust-nlp/drkernel-validation-data](https://huggingface.co/datasets/hkust-nlp/drkernel-validation-data)
278
+ - Benchmark source:
279
+ - [KernelBench](https://github.com/ScalingIntelligence/KernelBench)
280
+
281
+ Please acknowledge original dataset/benchmark authors when using this model.
282
+
283
+ ## Related Resources
284
+
285
+ - Paper: [Dr.Kernel: Reinforcement Learning Done Right for Triton Kernel Generations](https://arxiv.org/abs/2602.05885)
286
+ - Codebase: [KernelGYM](https://github.com/hkust-nlp/KernelGYM)
287
+ - Training docs: `drkernel/README.md`
288
+
289
+ ## Citation
290
+
291
+ ```bibtex
292
+ @article{liuetal2026,
293
+ title={Dr.Kernel: Reinforcement Learning Done Right for Triton Kernel Generations},
294
+ author={Wei Liu, Jiawei Xu, Yingru Li, Longtao Zheng, Tianjian Li, Qian Liu, Junxian He},
295
+ journal={arXiv:2602.05885},
296
+ year={2026}
297
+ }
298
+ ```