DistilQwen2.5: Industrial Practices of Training Distilled Open Lightweight Language Models
Paper • 2504.15027 • Published
Distill Llama-3.1-70B-Instruct (teacher) into Phi-3.5-mini-instruct (3.8B student) for IT networking & security Q&A using SFT distillation.
Two-phase pipeline:
Based on the Magpie SFT distillation recipe (arXiv 2406.08464, ICLR 2025).
The model is gated. Go to https://huggingface.co/meta-llama/Llama-3.1-70B-Instruct and request access.
huggingface-cli login
pip install vllm transformers datasets huggingface_hub
python generate_teacher_data.py
This will:
./generated_data.jsonl# More questions per topic
N_PER_TOPIC=30 python generate_teacher_data.py
# Upload to HF Hub (requires huggingface-cli login)
UPLOAD_TO_HUB=1 DATASET_REPO=youruser/your-dataset-name python generate_teacher_data.py
# Use full bf16 model instead of AWQ (needs ~140GB VRAM, e.g. 2x A100 80GB)
TEACHER_MODEL=meta-llama/Llama-3.1-70B-Instruct TENSOR_PARALLEL=2 QUANTIZATION=none python generate_teacher_data.py
Each line in generated_data.jsonl:
{
"messages": [
{"role": "user", "content": "How does ARP cache poisoning work?"},
{"role": "assistant", "content": "ARP cache poisoning is an attack..."}
],
"topic": "How ARP works and ARP cache poisoning"
}
pip install torch transformers trl accelerate datasets
python train_sft.py
This will:
./generated_data.jsonl (or a HF Hub dataset)./phi35-distilled/final/# Use a HF Hub dataset instead of local file
DATASET_PATH=youruser/your-dataset python train_sft.py
# Upload model to HF Hub
UPLOAD_TO_HUB=1 MODEL_REPO=youruser/your-model-name python train_sft.py
# Enable Trackio monitoring (live loss dashboard)
USE_TRACKIO=1 python train_sft.py
# Adjust batch size for smaller GPUs
# Edit per_device_train_batch_size in train_sft.py
# For 16GB GPUs: per_device_train_batch_size=1, gradient_accumulation_steps=16
| Parameter | Value | Rationale |
|---|---|---|
| Learning rate | 1e-5 | Lower than default 2e-5 for instruct→instruct (DistilQwen recipe) |
| Epochs | 2 | Standard for SFT distillation |
| Effective batch size | 16 | per_device=4 × grad_accum=4 (A100 80GB) |
| Max sequence length | 4096 | Covers most IT/security Q&A |
| Packing | True | Efficient training |
| Completion-only loss | True | Loss only on teacher responses |
| Precision | bf16 | Standard for modern GPUs |
| Gradient checkpointing | True | Memory efficient |
After training, test with:
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained(
"./phi35-distilled/final",
trust_remote_code=True,
torch_dtype="auto",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("./phi35-distilled/final", trust_remote_code=True)
messages = [
{"role": "user", "content": "Explain how a TCP three-way handshake works."}
]
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.6, do_sample=True)
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
print(response)
This model repository was generated by ML Intern, an agent for machine learning research and development on the Hugging Face Hub.
from transformers import AutoModelForCausalLM, AutoTokenizer
model_id = 'eduard76/it-security-distill'
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)
For non-causal architectures, replace AutoModelForCausalLM with the appropriate AutoModel class.