Knowledge Distillation Customization
Knowledge Distillation Customization
Learn how to train a smaller student model to mimic a larger teacher model using knowledge distillation (KD).
About
Knowledge Distillation transfers knowledge from a large teacher model to a smaller student model. During training, the student learns to match the teacher’s output probability distribution, producing a compact model that retains much of the teacher’s capability.
What you can achieve with KD:
- Compress models: Distill a 3B model into a 1B model for faster inference and lower deployment costs
- Reduce latency: Deploy a smaller model that responds faster while preserving quality
- Lower resource requirements: Serve a distilled model on fewer GPUs
KD vs SFT: Understanding the Trade-offs
Key Parameters
Workflow Overview
This tutorial follows a complete distillation pipeline:
- Fine-tune the teacher (SFT on the task dataset) so it learns the domain
- Establish a baseline by deploying the base student model and measuring ROUGE scores
- Distill into the student using the fine-tuned teacher’s soft targets
- Evaluate the distilled student and compare ROUGE scores against the baseline
When to choose KD:
- You have a high-quality large model and want a smaller, faster version
- Deployment latency or cost is a constraint
- The teacher and student share the same vocabulary (e.g., both are Llama models)
When to choose SFT instead: Refer to the Full SFT tutorial when you want to train a model directly on labeled data without a teacher.
Prerequisites
Before starting this tutorial, ensure you have:
- Completed the Quickstart to install and deploy NeMo Platform locally
- Installed the Python SDK (PyPI wrapper:
pip install "nemo-platform[all]"; source checkout: runmake bootstrapfrom the repository root) - Installed evaluation dependencies:
Quick Start
1. Initialize SDK
The SDK needs to know your NeMo Platform server URL. By default, http://localhost:8080 is used in accordance with the Quickstart guide. If NeMo Platform is running at a custom location, you can override the URL by setting the NMP_BASE_URL environment variable:
2. Prepare Dataset
Knowledge distillation uses the same dataset formats as SFT. We use the SQuAD dataset for both teacher training and distillation so that the teacher first learns the task, then transfers that knowledge to the student.
We also hold out a small test split for ROUGE evaluation at the end.
3. Secrets Setup
In this tutorial we use two Llama 3.2 Instruct models from HuggingFace:
- Teacher: meta-llama/Llama-3.2-3B-Instruct (3B parameters)
- Student: meta-llama/Llama-3.2-1B-Instruct (1B parameters)
Both models share the same tokenizer/vocabulary (required for knowledge distillation) and include a chat template for deployment with /chat/completions.
HuggingFace Authentication:
- For gated models (Llama, Gemma), you must provide a HuggingFace token via the
token_secretparameter - Get your token from HuggingFace Settings (requires Read access)
- Accept the model’s terms on the HuggingFace model page before using it:
4. Create Model FileSets and Model Entities
Knowledge distillation requires two model entities:
- Student model — the smaller model that will be trained (meta-llama/Llama-3.2-1B-Instruct)
- Teacher model — the larger model that provides soft targets (meta-llama/Llama-3.2-3B-Instruct)
Using the Instruct variants ensures the output model includes a chat template, which is required for the /chat/completions inference endpoint.
Phase 1: Fine-Tune the Teacher
5. Train Teacher with Full SFT
For best distillation results, fine-tune the teacher on the same dataset that will be used for distillation. This ensures the teacher has learned the task-specific knowledge that the student will inherit.
We train the 3B Instruct model with Full SFT on the SQuAD dataset.
Phase 2: Establish Baseline (Base Student)
6. Deploy the Base Student Model
Before distillation, deploy the base student model (1B Instruct, without any fine-tuning) to establish a baseline ROUGE score. After distillation, we compare the distilled student against this baseline to measure improvement.
7. Generate Baseline Predictions on Test Set
8. Delete Baseline Deployment
Delete the baseline student deployment to free GPU resources for the distillation training job and subsequent distilled model deployment.
Phase 3: Distill into Student
9. Create Knowledge Distillation Job
Now create a distillation job that trains the 1B student using the fine-tuned 3B teacher’s output distribution. The model field specifies the student, and teacher_model references the trained teacher model entity from Phase 1.
GPU Requirements:
KD requires loading both student and teacher models, so plan GPU memory accordingly:
- 1B student + 3B teacher: 1 GPU (24GB+ VRAM each)
- 3B student + 8B teacher: 4 GPUs
- 8B student + 70B teacher: 8+ GPUs
Use teacher_precision="bf16" (default) to reduce teacher memory footprint.
10. Track Distillation Progress
Phase 4: Evaluate the Distilled Student Model
11. Deploy the Distilled Student Model
The output model has the same architecture as the 1B student—only its weights have been updated via distillation. It requires just 1 GPU to deploy.
12. Generate Student Predictions on Test Set
13. Compute ROUGE Scores
Compare the base student (before distillation) and the distilled student against the ground-truth reference completions using ROUGE metrics.
Interpreting ROUGE Scores:
What to expect:
- The base student (1B, no training) provides a lower bound since it has not seen the task data
- The distilled student (1B, KD) should significantly outperform the base student, demonstrating the knowledge transferred from the 3B teacher
- If the distilled student scores are not much higher than the baseline, try increasing
distillation_temperature, adjustingdistillation_ratio, or training for more epochs
Hyperparameters
For detailed information on all available hyperparameters, recommended values, and tuning guidance, refer to the Hyperparameter Reference.
Troubleshooting
Job fails during model download:
- Verify authentication secrets are configured (refer to Managing Secrets)
- For gated HuggingFace models (Llama, Gemma), accept the license on the model page
- Check both
model(student) andteacher_modelURNs are correct - Ensure both model entities exist:
client.models.retrieve(name=..., workspace="default")
Job fails with OOM (Out of Memory) error:
KD loads both models, so OOM is more likely than with SFT:
- First try: Use
teacher_precision="bf16"to reduce teacher memory - Still OOM: Reduce
micro_batch_sizeto 1 - Still OOM: Reduce
global_batch_sizeandmax_seq_length - Last resort: Increase
num_gpus_per_node
No chat template / /chat/completions fails:
- Use Instruct model variants (e.g.,
Llama-3.2-1B-Instruct) instead of base models (Llama-3.2-1B). Base models do not include a chat template in their tokenizer, so the output model will also lack one.
Distilled model quality is poor:
- Increase
distillation_temperature(try 2.0–5.0) to transfer more nuanced knowledge - Adjust
distillation_ratio—if dataset labels are high-quality, lower the ratio; if the teacher is strong, raise it - Increase
epochsormax_stepsfor more training - Verify teacher and student share the same vocabulary
Vocabulary mismatch error:
- Teacher and student must use the same tokenizer. Use models from the same family (e.g., Llama 3.2 1B Instruct + Llama 3.2 3B Instruct)
Deployment fails:
- Verify output model exists:
client.models.retrieve(name=DISTILLED_STUDENT_NAME, workspace="default") - Check deployment logs:
client.inference.deployments.get_logs(name=deployment.name, workspace="default") - The distilled model has the same size as the student, so GPU requirements match the student model
Next Steps
- Monitor training metrics in detail
- Evaluate your fine-tuned model using the Evaluator service
- Learn about LoRA customization for resource-efficient fine-tuning
- Learn about Full SFT for direct supervised fine-tuning