Sn-CodeExplainer-0.5B

A 494M parameter code explanation model fine-tuned from Qwen/Qwen2.5-Coder-0.5B. Given a function or code snippet, it provides a clear, high-level summary of what the code does.

What it does: Explains code — purpose, key logic, and edge cases. What it doesn't do: Write, fix, refactor, or review code.

Supported Languages

Python, JavaScript, TypeScript, Java, C, C++, C#, Go, Rust, PHP

Usage

from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "SnurfyAI/Sn-CodeExplainer-0.5B"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16).to("cuda")

messages = [
    {"role": "system", "content": "You are a code explanation assistant. Given a piece of code, explain what it does clearly and concisely."},
    {"role": "user", "content": """Can you explain this Python function?

def fibonacci(n):
    if n <= 1:
        return n
    a, b = 0, 1
    for _ in range(2, n + 1):
        a, b = b, a + b
    return b"""},
]

prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")

with torch.no_grad():
    output = model.generate(**inputs, max_new_tokens=512, temperature=0.3, do_sample=True, top_p=0.9)

response = tokenizer.decode(output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
print(response)

Example output:

This function computes the nth Fibonacci number iteratively. It handles base cases (0 and 1) directly, then uses two variables to track consecutive Fibonacci values, updating them in a loop until reaching the target index.

Chat Template

This model uses the ChatML format:

<|im_start|>system
You are a code explanation assistant. Given a piece of code, explain what it does clearly and concisely.<|im_end|>
<|im_start|>user
{code and question here}<|im_end|>
<|im_start|>assistant

The chat template is baked into the tokenizer — apply_chat_template() handles formatting automatically.

Training Details

Parameter Value
Base model Qwen/Qwen2.5-Coder-0.5B (494M params)
Method Full fine-tune (SFT)
Training data ~15k synthetic examples
Data format 1-2 turn ChatML conversations
Data source DeepSeek V3.2 via OpenRouter
Epochs 2
Effective batch size 32 (batch 4 x grad_accum 8)
Learning rate 2e-5 (cosine schedule)
Warmup steps 10
Max sequence length 2048
Precision bfloat16
Optimizer AdamW
Hardware NVIDIA RTX 5090 (32GB)

Training Data

The training data covers:

  • 10 programming languages (Python, JavaScript, TypeScript, Java, C, C++, C#, Go, Rust, PHP)
  • 15 code domains (algorithms, data structures, string manipulation, file I/O, API handlers, database operations, error handling, math, utilities, OOP, concurrency, recursion, iterators, configuration, data transformation)
  • 3 complexity levels (simple 5-15 lines, moderate 15-30 lines, complex 30-50 lines)
  • 3 explanation detail levels (brief, moderate, detailed)
  • ~2% polite refusal examples (redirecting off-topic requests back to code explanation)

Data Generation Cost

~$3.30 for 15k examples via OpenRouter (DeepSeek V3.2).

Limitations

  • Synthetic training data — may inherit factual errors or biases from the generator model
  • English only — explanations are always in English regardless of the code language
  • Small model — 494M parameters limits the depth and nuance of explanations, especially for complex algorithms
  • Code length — best with functions under ~50 lines; longer code may exceed the 2048 token context
  • Not a code generator — explicitly trained to explain, not to write or modify code

Citations

Base Model

@article{hui2024qwen2,
  title={Qwen2. 5-Coder Technical Report},
  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},
  journal={arXiv preprint arXiv:2409.12186},
  year={2024}
}

@article{qwen2,
  title={Qwen2 Technical Report},
  author={An Yang and Baosong Yang and Binyuan Hui and Bo Zheng and Bowen Yu and Chang Zhou and Chengpeng Li and Chengyuan Li and Dayiheng Liu and Fei Huang and Guanting Dong and Haoran Wei and Huan Lin and Jialong Tang and Jialin Wang and Jian Yang and Jianhong Tu and Jianwei Zhang and Jianxin Ma and Jin Xu and Jingren Zhou and Jinze Bai and Jinzheng He and Junyang Lin and Kai Dang and Keming Lu and Keqin Chen and Kexin Yang and Mei Li and Mingfeng Xue and Na Ni and Pei Zhang and Peng Wang and Ru Peng and Rui Men and Ruize Gao and Runji Lin and Shijie Wang and Shuai Bai and Sinan Tan and Tianhang Zhu and Tianhao Li and Tianyu Liu and Wenbin Ge and Xiaodong Deng and Xiaohuan Zhou and Xingzhang Ren and Xinyu Zhang and Xipin Wei and Xuancheng Ren and Yang Fan and Yang Yao and Yichang Zhang and Yu Wan and Yunfei Chu and Yuqiong Liu and Zeyu Cui and Zhenru Zhang and Zhihao Fan},
  journal={arXiv preprint arXiv:2407.10671},
  year={2024}
}

Training Framework

@software{vonwerra2020trl,
  title   = {{TRL: Transformers Reinforcement Learning}},
  author  = {von Werra, Leandro and Belkada, Younes and Tunstall, Lewis and Beeching, Edward and Thrush, Tristan and Lambert, Nathan and Huang, Shengyi and Rasul, Kashif and Gallouédec, Quentin},
  license = {Apache-2.0},
  url     = {https://github.com/huggingface/trl},
  year    = {2020}
}

License

Apache-2.0 (same as the base model)

Downloads last month
21
Safetensors
Model size
0.5B params
Tensor type
BF16
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for SnurfyAI/Sn-CodeExplainer-0.5B

Finetuned
(24)
this model
Quantizations
1 model

Dataset used to train SnurfyAI/Sn-CodeExplainer-0.5B

Papers for SnurfyAI/Sn-CodeExplainer-0.5B