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|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
base_model: Qwen/Qwen2.5-Coder-3B
|
| 4 |
+
datasets:
|
| 5 |
+
- GPUMODE/KernelBook
|
| 6 |
+
tags:
|
| 7 |
+
- qwen2
|
| 8 |
+
- code-generation
|
| 9 |
+
- triton
|
| 10 |
+
- pytorch
|
| 11 |
+
- kernel-generation
|
| 12 |
+
- kernelbook
|
| 13 |
+
- lora
|
| 14 |
+
- finetune
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# Qwen2.5-Coder-3B-KernelBook: Fine-tuned for PyTorch to Triton Kernel Generation
|
| 18 |
+
|
| 19 |
+
This repository contains a fine-tuned version of the **[Qwen/Qwen2.5-Coder-3B](https://huggingface.co/Qwen/Qwen2.5-Coder-3B)** model, specialized for transpiling PyTorch `nn.Module` code into high-performance Triton kernels.
|
| 20 |
+
|
| 21 |
+
The model was trained on the **[GPUMODE/KernelBook](https://huggingface.co/datasets/GPUMODE/KernelBook)** dataset, which contains thousands of pairs of equivalent PyTorch and Triton code snippets generated by `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.
|
| 22 |
+
|
| 23 |
+
This model was fine-tuned as part of a demonstration of an end-to-end workflow: from dataset preparation and model training to benchmarking with the official `KernelBench` framework.
|
| 24 |
+
|
| 25 |
+
## Model Details
|
| 26 |
+
|
| 27 |
+
- **Base Model:** `Qwen/Qwen2.5-Coder-3B`
|
| 28 |
+
- **Fine-tuning Dataset:** `GPUMODE/KernelBook`
|
| 29 |
+
- **Method:** Low-Rank Adaptation (LoRA)
|
| 30 |
+
- **Framework:** PyTorch 2.5.0, Transformers, PEFT, TRL
|
| 31 |
+
|
| 32 |
+
### Training Summary
|
| 33 |
+
|
| 34 |
+
The model was trained for **1 full epoch** on the `GPUMODE/KernelBook` dataset (18,162 examples), showcasing strong learning and convergence.
|
| 35 |
+
|
| 36 |
+
- **Final Training Loss:** **`0.0922`**
|
| 37 |
+
- **Final Mean Token Accuracy:** **`98.34%`**
|
| 38 |
+
- **Training Runtime:** `5818.25 seconds` (approx. 1 hour 37 minutes)
|
| 39 |
+
- **Hardware:** 1x NVIDIA H100 80GB
|
| 40 |
+
|
| 41 |
+
**Key Training Hyperparameters:**
|
| 42 |
+
- `learning_rate`: 2e-4
|
| 43 |
+
- `per_device_train_batch_size`: 1
|
| 44 |
+
- `gradient_accumulation_steps`: 8 (effective batch size of 8)
|
| 45 |
+
- `max_seq_length`: 4096
|
| 46 |
+
- `optimizer`: adamw_torch_fused
|
| 47 |
+
- `precision`: bfloat16
|
| 48 |
+
|
| 49 |
+
For a detailed view of the training progress, you can visit the [Weights & Biases run page](https://wandb.ai/tarunreddi-university-at-buffalo/huggingface/runs/ew21hn3w).
|
| 50 |
+
|
| 51 |
+
## How to Use
|
| 52 |
+
|
| 53 |
+
This model is designed to be used for code generation in a structured prompt format. You should provide the PyTorch code and ask for the Triton code in return.
|
| 54 |
+
|
| 55 |
+
### Installation
|
| 56 |
+
|
| 57 |
+
First, make sure you have the necessary libraries installed:
|
| 58 |
+
|
| 59 |
+
```bash
|
| 60 |
+
pip install torch transformers peft accelerate
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
### Example Usage
|
| 64 |
+
|
| 65 |
+
Here is a Python snippet demonstrating how to generate a Triton kernel from a PyTorch `nn.Module`.
|
| 66 |
+
|
| 67 |
+
```python
|
| 68 |
+
import torch
|
| 69 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 70 |
+
|
| 71 |
+
# The repository ID of this model on the Hugging Face Hub
|
| 72 |
+
model_id = "TEEN-D/Qwen2.5-Coder-3B-KernelBook-Finetuned"
|
| 73 |
+
|
| 74 |
+
print("Loading model and tokenizer...")
|
| 75 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 76 |
+
model_id,
|
| 77 |
+
torch_dtype=torch.bfloat16,
|
| 78 |
+
device_map="auto",
|
| 79 |
+
trust_remote_code=True
|
| 80 |
+
)
|
| 81 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 82 |
+
print("Model loaded successfully.")
|
| 83 |
+
|
| 84 |
+
# --- 1. Define your PyTorch code ---
|
| 85 |
+
pytorch_code = """
|
| 86 |
+
import torch
|
| 87 |
+
import torch.nn as nn
|
| 88 |
+
|
| 89 |
+
class SumAggregator(nn.Module):
|
| 90 |
+
def __init__(self):
|
| 91 |
+
super(SumAggregator, self).__init__()
|
| 92 |
+
|
| 93 |
+
def forward(self, neighbor):
|
| 94 |
+
return torch.sum(neighbor, dim=1)
|
| 95 |
+
"""
|
| 96 |
+
|
| 97 |
+
# --- 2. Format the prompt as used during training ---
|
| 98 |
+
prompt = f"""### INSTRUCTION
|
| 99 |
+
Generate the Triton code for the following Python code.
|
| 100 |
+
|
| 101 |
+
### PYTHON CODE:
|
| 102 |
+
{pytorch_code}
|
| 103 |
+
|
| 104 |
+
### TRITON CODE:
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
# --- 3. Generate the Triton kernel ---
|
| 108 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 109 |
+
outputs = model.generate(
|
| 110 |
+
**inputs,
|
| 111 |
+
max_new_tokens=2048,
|
| 112 |
+
do_sample=False, # Use greedy decoding for reproducibility
|
| 113 |
+
pad_token_id=tokenizer.eos_token_id
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
full_output = tokenizer.decode(outputs, skip_special_tokens=True)
|
| 117 |
+
|
| 118 |
+
# --- 4. Extract and print only the Triton code ---
|
| 119 |
+
try:
|
| 120 |
+
triton_code = full_output.split("### TRITON CODE:").strip()
|
| 121 |
+
print("\n--- Generated Triton Code ---")
|
| 122 |
+
print(triton_code)
|
| 123 |
+
except IndexError:
|
| 124 |
+
print("Could not parse the output. Full generated text:")
|
| 125 |
+
print(full_output)
|
| 126 |
+
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
## Fine-tuning Dataset: GPUMODE/KernelBook
|
| 130 |
+
|
| 131 |
+
This model's capabilities are a direct result of the high-quality `GPUMODE/KernelBook` dataset.
|
| 132 |
+
|
| 133 |
+
- **Content:** The dataset contains 18,162 pairs of PyTorch programs and their equivalent Triton kernels, as generated by `torch.compile`.
|
| 134 |
+
- **Creation Process:** The authors collected PyTorch repositories, extracted `nn.Module` classes, generated Triton code with `torch.compile`, and enriched the data with metadata.
|
| 135 |
+
- **Recommended Usage:** For best results when using or evaluating the generated Triton code, it is recommended to use the same PyTorch version the dataset was created with (`torch==2.5.0`).
|
| 136 |
+
|
| 137 |
+
## Base Model: Qwen2.5-Coder-3B
|
| 138 |
+
|
| 139 |
+
`Qwen2.5-Coder` is a series of code-specific large language models. The 3B model has the following characteristics:
|
| 140 |
+
- **Parameters:** 3.09B
|
| 141 |
+
- **Context Length:** 32,768 tokens
|
| 142 |
+
- **Architecture:** Transformer with RoPE, SwiGLU, RMSNorm.
|
| 143 |
+
|
| 144 |
+
## Citation
|
| 145 |
+
|
| 146 |
+
If you use this model or the dataset in your work, please cite the original authors.
|
| 147 |
+
|
| 148 |
+
**To cite the dataset:**
|
| 149 |
+
```bibtex
|
| 150 |
+
@software{kernelbook2025,
|
| 151 |
+
title={KernelBook},
|
| 152 |
+
author={Paliskara, Sahan and Saroufim, Mark},
|
| 153 |
+
year={2025},
|
| 154 |
+
month={5},
|
| 155 |
+
url={https://huggingface.co/datasets/GPUMODE/KernelBook},
|
| 156 |
+
}
|
| 157 |
+
```
|
| 158 |
+
|
| 159 |
+
**To cite the base model:**
|
| 160 |
+
```bibtex
|
| 161 |
+
@article{hui2024qwen2,
|
| 162 |
+
title={Qwen2. 5-Coder Technical Report},
|
| 163 |
+
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},
|
| 164 |
+
journal={arXiv preprint arXiv:2409.12186},
|
| 165 |
+
year={2024}
|
| 166 |
+
}
|
| 167 |
+
```
|