Views
No views yet
hkust-nlp/drkernel-14b is a Qwen3-14B-based model specialized for GPU kernel generation and optimization (especially Triton) in the DR.Kernel framework.Qwen3ForCausalLM14,768,307,200 (from model.safetensors.index.json)ModelNew kernel implementations from PyTorch reference tasksconfig.json:num_hidden_layers: 40hidden_size: 5120intermediate_size: 17408num_attention_heads: 40num_key_value_heads: 8max_position_embeddings: 32768vocab_size: 151936transformers_version: 4.56.0model.safetensors.index.json:total_parameters: 14,768,307,200total_size: 29,536,614,400 bytesmodel-00001-of-00007.safetensors ... model-00007-of-00007.safetensorshkust-nlp/drkernel-coldstart-8khkust-nlp/drkernel-rl-datahkust-nlp/drkernel-validation-data (KernelBench Level 2 validation split)drkernel/kernel/scripts/sft/14b-coldstart.shdrkernel/kernel/scripts/rl/14b_trloo_mrs_pr_prs.shModel, get_inputs, get_init_inputs)ModelNew1import textwrap
2import torch
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5model_id = "hkust-nlp/drkernel-14b"
6tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
7model = AutoModelForCausalLM.from_pretrained(
8 model_id,
9 torch_dtype=torch.bfloat16,
10 device_map="auto",
11 trust_remote_code=True,
12)
13
14ref_code = textwrap.dedent(
15 """
16 import torch
17 import torch.nn as nn
18
19 class Model(nn.Module):
20 def __init__(self):
21 super().__init__()
22
23 def forward(self, x):
24 x = torch.abs(x)
25 x = x - 1.0
26 return x
27
28 def get_inputs():
29 return [torch.randn(64, 128)]
30
31 def get_init_inputs():
32 return []
33 """
34).strip()
35
36example_ref_code = textwrap.dedent(
37 """
38 import torch
39 import torch.nn as nn
40 import torch.nn.functional as F
41
42 class Model(nn.Module):
43 def __init__(self) -> None:
44 super().__init__()
45
46 def forward(self, a, b):
47 return a + b
48
49 def get_inputs():
50 # randomly generate input tensors based on the model architecture
51 a = torch.randn(1, 128).cuda()
52 b = torch.randn(1, 128).cuda()
53 return [a, b]
54
55 def get_init_inputs():
56 # randomly generate tensors required for initialization based on the model architecture
57 return []
58 """
59).strip()
60
61example_kernel_code = textwrap.dedent(
62 '''
63 import torch
64 import torch.nn as nn
65 import torch.nn.functional as F
66 import triton
67 import triton.language as tl
68
69 @triton.jit
70 def add_kernel(
71 x_ptr, # Pointer to first input
72 y_ptr, # Pointer to second input
73 out_ptr, # Pointer to output
74 n_elements, # Total number of elements in input/output
75 BLOCK_SIZE: tl.constexpr,
76 ):
77 # Each program handles a contiguous block of data of size BLOCK_SIZE
78 block_start = tl.program_id(0) * BLOCK_SIZE
79 # Create a range of offsets [0..BLOCK_SIZE-1]
80 offsets = block_start + tl.arange(0, BLOCK_SIZE)
81 # Mask to ensure we don't go out of bounds
82 mask = offsets < n_elements
83 # Load input values
84 x = tl.load(x_ptr + offsets, mask=mask, other=0.0)
85 y = tl.load(y_ptr + offsets, mask=mask, other=0.0)
86 # Perform the elementwise addition
87 out = x + y
88 # Store the result
89 tl.store(out_ptr + offsets, out, mask=mask)
90
91 def triton_add(x: torch.Tensor, y: torch.Tensor):
92 """
93 This function wraps the Triton kernel call. It:
94 1. Ensures the inputs are contiguous on GPU.
95 2. Calculates the grid (blocks) needed.
96 3. Launches the Triton kernel.
97 """
98 assert x.is_cuda and y.is_cuda, "Tensors must be on CUDA."
99 x = x.contiguous()
100 y = y.contiguous()
101
102 # Prepare output tensor
103 out = torch.empty_like(x)
104
105 # Number of elements in the tensor
106 n_elements = x.numel()
107 BLOCK_SIZE = 128 # Tunable parameter for block size
108
109 # Determine the number of blocks needed
110 grid = lambda meta: ((n_elements + meta["BLOCK_SIZE"] - 1) // meta["BLOCK_SIZE"],)
111
112 # Launch the Triton kernel
113 add_kernel[grid](x, y, out, n_elements, BLOCK_SIZE=BLOCK_SIZE)
114 return out
115
116 class ModelNew(nn.Module):
117 def __init__(self) -> None:
118 super().__init__()
119
120 def forward(self, a, b):
121 # Instead of "return a + b", call our Triton-based addition
122 return triton_add(a, b)
123 '''
124).strip()
125
126prompt_template = textwrap.dedent(
127 """\
128 You write custom Triton kernels to replace the pytorch operators in the given architecture to get speedups.
129
130 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.
131
132 Here's an example to show you the syntax of inline embedding custom Triton kernels in torch: The example given architecture is:
133
134 ```python
135 {example_ref_code}
136 ```
137
138 The example new arch with custom Triton kernels looks like this:
139
140 ```python
141 {example_kernel_code}
142 ```
143
144 You are given the following architecture:
145 ```python
146 {ref_code}
147 ```
148
149 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.
150 """
151).strip()
152
153prompt = prompt_template.format(
154 example_ref_code=example_ref_code,
155 example_kernel_code=example_kernel_code,
156 ref_code=ref_code,
157)
158messages = [{"role": "user", "content": prompt}]
159
160inputs = tokenizer.apply_chat_template(
161 messages,
162 add_generation_prompt=True,
163 return_tensors="pt",
164).to(model.device)
165
166with torch.no_grad():
167 outputs = model.generate(
168 inputs,
169 max_new_tokens=2048,
170 do_sample=True,
171 temperature=1.0,
172 top_p=1.0,
173 )
174
175# Only print newly generated tokens
176print(tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=False))drkernel/kernel/scripts/eval/drkernel-14b-maxturns3.shdrkernel/kernel/scripts/eval/grading_common.sh for custom evaluation runshkust-nlp/drkernel-validation-data (KernelBench Level 2 validation tasks)drkernel/README.md1@article{liuetal2026,
2 title={Dr.Kernel: Reinforcement Learning Done Right for Triton Kernel Generations},
3 author={Wei Liu, Jiawei Xu, Yingru Li, Longtao Zheng, Tianjian Li, Qian Liu, Junxian He},
4 journal={arXiv:2602.05885},
5 year={2026}
6}