Qwen2.5-7B (52% accuracy) + DeepSeek-V3 knowledge → Optimized 7B model (70% accuracy, structured format)
Course Overview
This tutorial demonstrates a systematic two-stage knowledge distillation pipeline: Stage 1 - SFT (Format Learning):- Generate training data with consistent output formatting
- Train student model to internalize structured response patterns
- Demonstrate format learning without explicit instructions
- Build reward system based on answer correctness
- Apply reinforcement learning to improve reasoning within learned format
- Show accuracy gains while maintaining consistent structure
- SFT: Excels at learning structural patterns and making them default behavior
- RFT: Excels at optimizing content quality through reward-based learning
- Together: Create models that are both well-formatted AND more accurate
Chapter 1: Environment Setup
Requirements:- Fireworks AI account with API access
- Basic familiarity with fine-tuning concepts
- Understanding of train/test splits for valid evaluation
API Configuration
- Fireworks SDK: Simplified interface for model deployment and fine-tuning
- Serverless Models: Pre-deployed models you can use immediately
- API Key: Authenticates your requests and tracks usage
Chapter 2: Dataset Preparation and Analysis
Why GSM8K?- Standard Benchmark: Widely used for evaluating mathematical reasoning
- Clear Evaluation: Numerical answers are easy to check for correctness
- Appropriate Difficulty: Challenging enough to demonstrate knowledge transfer
Load GSM8K Dataset
#### 18 format provides the ground truth answer we need for automated evaluation. We’ll extract this pattern to check model correctness.
Process Dataset for Training and Evaluation
Chapter 3: Model Setup
Deploy Your Student Model
Model Selection: We’re using Qwen2.5-7B as our student model because:- Right Size: Large enough to learn complex patterns, small enough to be efficient
- Strong Base: Pre-trained on diverse data including mathematical content
- Cost-Effective: Significantly cheaper to run than larger models
Testing Baseline Model Behavior
Chapter 4: Stage 1 - Supervised Fine-Tuning (SFT)
Generate Formatted Training Data with Teacher Model
Why Use a Teacher Model
The Knowledge Transfer Principle Rather than learning math reasoning from scratch, we’ll have a powerful model (DeepSeek-V3) solve problems step-by-step, then train our small model to mimic those high-quality solutions. Why DeepSeek-V3:- Strong mathematical reasoning (>90% accuracy on GSM8K)
- Clear step-by-step explanations that provide good learning examples
- Consistent output format when given proper instructions
- Cost-effective for generating training data (no deployment required)
- Available as serverless model on Fireworks AI platform
- Stage 1 (SFT): Use teacher responses as training targets to learn format patterns
- Stage 2 (RFT): Use the same problems with ground truth labels for reward-based learning
Defining Our Target Format
Why Structured Output?- Consistency: Every response follows the same pattern
- Parseability: Easy to extract answers programmatically
- Debugging: Clear separation of reasoning and results
- Production Ready: Reliable format for downstream applications
- Unique: Different from typical model outputs
Teaching the Teacher Model Our Format
Strategy: We’ll use a system prompt to teach our teacher model (DeepSeek-V3) to use our desired format, then capture those formatted responses as training data.Generating High-Quality Training Data
The Process:- Take problems from GSM8K training set
- Have teacher model solve them using our format
- Verify teacher got the right answer
- Create training examples from successful solutions
- Questions/Inputs: Your domain-specific problems
- No Perfect Answers: No ground truth responses
- Solution: Use a powerful teacher model to create accurate high-quality training data
Real-World Knowledge Distillation Use Cases
Common Scenarios Where You Need Teacher Models
1. Legal Document Analysis- Challenge: No ground truth for contract clause interpretation
- Teacher Solution: Use teacher models to generate expert-level legal analyses
- Challenge: No perfect code review comments for your codebase
- Teacher Solution: Use teacher models to generate code review insights
- Challenge: No ideal responses for company-specific questions
- Teacher Solution: Use teacher models for customer service responses
- Challenge: No labeled decisions for edge-case content
- Teacher Solution: Use teacher models to generate moderation reasoning and decisions
Popular Open Source Teacher Models
- Kimi K2: Great general purpose model, especially for agentic use cases.
- Qwen3 Coder 480B: Strong coding model, especially for one-off coding tasks.
- Qwen3 235B (instruct): Good general purpose model. Has strong world knowledge for tasks like classification.
- Qwen3 235B (thinking): Good reasoning model for agentic tasks and tasks that require multi-step planning.
- Open AI GPT OSS 120B: OpenAI’s open-weight model, with strong reasoning and tool use capabilities. Runs efficiently on single 80GB GPU and achieves near-parity with o4-mini on core reasoning benchmarks.
- DeepSeek V3: Powerful MoE model with 671B parameters (37B active) that rivals GPT-4o and Claude 3.5 Sonnet. Strong performance in math, coding, and reasoning tasks.
- DeepSeek R1: Open-source reasoning model that rivals OpenAI o1. Trained using pure reinforcement learning. Shows explicit chain-of-thought reasoning process and excels at complex mathematical and logical problems.
Uploading Training Data to Fireworks
SFT Training Configuration
Supervised Fine-Tuning Job:- Model:
Qwen2.5 7B - Dataset: dataset (Your uploaded dataset)
- Epochs: 5-8 (format learning needs repetition)
- Learning Rate: 1e-5
- Higher Learning Rate: Needed to override existing response patterns
- More Epochs: Format internalization requires repetition
- Larger Model: 3B+ has capacity to learn complex structural patterns
- No System Prompts in Training: Teaches default behavior, not instruction-following
Running the SFT Training Job
Deploying the Fine-Tuned Model
Chapter 5: Evaluating SFT Results
Testing Format Learning Success
The Critical Test: Can our fine-tuned model use the target format WITHOUT being explicitly told to do so?- More training examples (aim for 1000+)
- Higher learning rate (try 1e-4)
- More epochs (try 5-8)
- Verify training data format consistency
Understanding SFT’s Strengths and Limitations
Strengths demonstrated- Consistent output formatting
- No system prompts needed
- Internalized behavior patterns
- Accuracy may not improve dramatically
- Only mimics teacher, doesn’t generalize
- No feedback loop for corrections
Chapter 6: Stage 2 - Reinforcement Fine-Tuning (RFT)
Now that our model consistently uses the[WORK] and [RESULT] format automatically (without being told), we can apply RFT to improve the accuracy of answers within that structure.
Why Add Reinforcement Learning
Beyond Imitation: While SFT teaches the model to mimic the teacher’s style, RFT optimizes for correctness. The model learns to:- Prefer reasoning paths that lead to correct answers
- Self-correct when making mistakes
- Develop confidence in its mathematical reasoning
- Easy reward calculation from
[RESULT]tags - Maintains learned formatting while optimizing correctness
Creating the RFT Dataset
Strategy: Reuse the same problems our teacher model solved correctly during SFT generation, but format them for reinforcement learning.Understanding Reward Kit and Evaluators
What is Reward Kit?Setting Up RFT Training (Manual Dashboard Configuration)
Due to the complexity of reinforcement learning setup, we’ll use the Fireworks dashboard for the final configuration steps.Step 1: Upload the RFT Evaluator
- Navigate to Evaluators
-
Create New Evaluator
- Click “Create Evaluator”
- Evaluator Name:
kd-rft-evaluator
-
Configure Dataset
- Select “Select an existing dataset”
- Choose the
kd-rft-datasetyou uploaded earlier
-
Add Evaluator Code
- Choose “Start from scratch”
- Click “Next”
- In the code editor, delete any existing code
- Copy and paste the complete code from
kd-rft-evaluator.py
-
Save Evaluator
- Click “Save Evaluator”
- Your evaluator is now ready for RFT training
Step 2: Create RFT Training Job
-
Navigate to Fine-Tuning
- Go to the Fine-Tuning tab in the dashboard
- Click “Fine-Tune a Model”
- Select “Reinforcement” tab
-
Configure Training Job
Model Selection:
- Select your SFT-trained model
- Use
job.output_modelfrom your SFT job to obtain SFT model name (e.g.,accounts/your-account/models/kd-sft-model)
- Select
kd-rft-datasetfrom the dropdown
- Select
kd-rft-evaluator(the one you just created)
- Rollout Settings: Leave as default values
- Model Output Name:
- Option 1: Leave blank for auto-generated name
- Option 2: Enter custom name (e.g.,
kd-rft-model)
- Other Hyperparameters: Leave as defaults
-
Launch Training
- Review your configuration
- Click “Create Job”
- Important: Note the output model name for evaluation later
- Monitoring
- Track progress in the dashboard’s Fine Tuning section.
- Once the job status is
Completed, you can deploy your model.
Deploying the Fine-Tuned Model
Chapter 7: Evaluate Complete Knowledge Distillation Pipeline
Now that we’ve completed our knowledge distillation pipeline, it’s time to evaluate our results. But first, we need robust evaluation tools that can handle the complexity of comparing different models fairly. Why We Need Sophisticated Evaluation Tools The Challenge: We now have models that may respond in different formats:- Baseline model: Natural language, inconsistent formatting
- RFT model: Structured [WORK]/[RESULT] format
- Extract answers from any response format
- Normalize numbers (handle commas, decimals, currency)
- Track multiple metrics (accuracy, extraction success)
Test Model Performance
Actual Results Analysis
Course Summary and Key Takeaways
What We Demonstrated
1. SFT for Internalized Format Learning:- Training Strategy: Include format examples without system prompts in training data
- Testing Strategy: No system prompts needed - format is internalized
- Result: Model automatically uses
[WORK]/[RESULT]structure as default behavior - Key Insight: SFT teaches “how to respond” by making patterns the model’s natural behavior
- Foundation: Builds on SFT model
- Optimization: Reward-based learning improves content quality
- Result: Significantly improves reasoning accuracy
- Key Insight: RFT optimizes “what to respond with”
- Stage 1 (SFT): Establishes reliable, consistent response structure
- Stage 2 (RFT): Optimizes content quality within that structure
- Combined Result: Models that are both well-formatted AND accurate
Practical Applications
This knowledge distillation approach is valuable for:- API Integrations: Reliable output parsing + improved accuracy
- Structured Reasoning Tasks: Clear thinking process + better results
- Production Pipelines: Consistent format + higher quality content
- Evaluation Systems: Easy answer extraction + improved performance
- Cost Optimization: Small models with large model capabilities
Expected Resources
- Cost: ~Costs apply for API calls, deployments and training jobs
Conclusion
This tutorial demonstrated how to systematically apply knowledge distillation using Fireworks AI to create models that combine the structural reliability of supervised learning with the performance optimization of reinforcement learning. Key Success Factors:- Clear separation of concerns: SFT for structure, RFT for accuracy
- Consistent evaluation methodology: Test without system prompts to measure true learning
- Building on foundations: RFT builds on SFT rather than starting from scratch
- Quality training data: High teacher model accuracy and format consistency
- Defining appropriate outputs for your use case
- Generating high-quality teacher demonstrations
- Fine tuning
- Evaluating performance improvements