Views
No views yet
nn.Module code into high-performance Triton kernels.torch.compile. This fine-tuning enables the model to understand the patterns of PyTorch operations and translate them into efficient, fused GPU kernels written in the Triton language.KernelBench framework.Qwen/Qwen2.5-Coder-3BGPUMODE/KernelBookGPUMODE/KernelBook dataset (18,162 examples), showcasing strong learning and convergence.0.092298.34%5818.25 seconds (approx. 1 hour 37 minutes)learning_rate: 2e-4per_device_train_batch_size: 1gradient_accumulation_steps: 8 (effective batch size of 8)max_seq_length: 4096optimizer: adamw_torch_fusedprecision: bfloat16pip install torch transformers peft acceleratenn.Module.1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4# The repository ID of this model on the Hugging Face Hub
5model_id = "TEEN-D/Qwen2.5-Coder-3B-KernelBook-Finetuned"
6
7print("Loading model and tokenizer...")
8model = AutoModelForCausalLM.from_pretrained(
9 model_id,
10 torch_dtype=torch.bfloat16,
11 device_map="auto",
12 trust_remote_code=True
13)
14tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
15print("Model loaded successfully.")
16
17# --- 1. Define your PyTorch code ---
18pytorch_code = """
19import torch
20import torch.nn as nn
21
22class SumAggregator(nn.Module):
23 def __init__(self):
24 super(SumAggregator, self).__init__()
25
26 def forward(self, neighbor):
27 return torch.sum(neighbor, dim=1)
28"""
29
30# --- 2. Format the prompt as used during training ---
31prompt = f"""### INSTRUCTION
32Generate the Triton code for the following Python code.
33
34### PYTHON CODE:
35{pytorch_code}
36
37### TRITON CODE:
38"""
39
40# --- 3. Generate the Triton kernel ---
41inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
42outputs = model.generate(
43 **inputs,
44 max_new_tokens=2048,
45 do_sample=False, # Use greedy decoding for reproducibility
46 pad_token_id=tokenizer.eos_token_id
47)
48
49full_output = tokenizer.decode(outputs, skip_special_tokens=True)
50
51# --- 4. Extract and print only the Triton code ---
52try:
53 triton_code = full_output.split("### TRITON CODE:").strip()
54 print("\n--- Generated Triton Code ---")
55 print(triton_code)
56except IndexError:
57 print("Could not parse the output. Full generated text:")
58 print(full_output)
59GPUMODE/KernelBook dataset.torch.compile.nn.Module classes, generated Triton code with torch.compile, and enriched the data with metadata.torch==2.5.0).Qwen2.5-Coder is a series of code-specific large language models. The 3B model has the following characteristics:1@software{kernelbook2025,
2 title={KernelBook},
3 author={Paliskara, Sahan and Saroufim, Mark},
4 year={2025},
5 month={5},
6 url={https://huggingface.co/datasets/GPUMODE/KernelBook},
7}1@article{hui2024qwen2,
2 title={Qwen2. 5-Coder Technical Report},
3 author={Hui, Binyuan and Yang, Jian and Cui, Zeyu and Yang, Jiaxi and Liu, Dayiheng and Zhang, Lei and Liu, Tianyu and Zhang, Jiajun and Yu, Bowen and Dang, Kai and others},
4 journal={arXiv preprint arXiv:2409.12186},
5 year={2024}
6}