A production-ready framework for phased fine-tuning of large language models using LoRA (Low-Rank Adaptation) with DeepSpeed optimization. Designed for multi-GPU training on models like Kwaipilot/KAT-Dev (32B parameters).
Python
1
2 commits
updated Dec 10, 2025
A production-ready framework for phased fine-tuning of large language models using LoRA (Low-Rank Adaptation) with DeepSpeed optimization. Designed for multi-GPU training on models like Kwaipilot/KAT-Dev (32B parameters).
This framework implements a curriculum learning approach where the model is trained sequentially across multiple phases, each building upon the previous:
| Phase | Name | Purpose | Default Epochs |
|---|---|---|---|
| 1 | Foundation | Core codebase understanding | 3 |
| 2 | Evolution | Code evolution patterns | 2 |
| 3 | PR Mastery | Pull request and review patterns | 2 |
git clone https://github.com/YOUR_USERNAME/phased-cpt-finetuning.git
cd phased-cpt-finetuning
# Create virtual environment
python -m venv venv
source venv/bin/activate
# Install PyTorch first (adjust for your CUDA version)
pip install torch>=2.1.0
# Install Flash Attention (requires torch to be installed)
pip install flash-attn --no-build-isolation
# Install remaining dependencies
pip install -r requirements.txt
Create JSONL files in the ./dataset directory with the following format:
{"training_content": "Your training text content here..."}
{"training_content": "Another training sample..."}
Required files:
dataset/phase1_foundation.jsonldataset/phase2_evolution.jsonldataset/phase3_pr_mastery.jsonlDataset Generation: For automated dataset creation from GitHub repositories, see the companion tool: Phased-CPT-DatasetGenerator
Edit config.yaml to customize:
# Model Configuration
model:
name: "Kwaipilot/KAT-Dev" # HuggingFace model ID
size: "32B"
# LoRA Configuration
training:
lora:
r: 128 # LoRA rank
alpha: 256 # LoRA alpha (typically 2*r)
dropout: 0.05
# Add your WandB API key
wandb:
api_key: "your-api-key-here" # Or set WANDB_API_KEY env var
project: "your-project-name"
# Using the shell script (recommended)
./train_phased.sh
# Or directly with DeepSpeed
deepspeed --num_gpus=8 train_phased.py
.
├── config.yaml # Main configuration file
├── train_phased.py # Phased training script
├── train_phased.sh # Training launcher with pre-flight checks
├── inference.py # Interactive inference & model comparison
├── requirements.txt # Python dependencies
├── deepspeed_configs/
│ ├── zero2.json # DeepSpeed ZeRO-2 config (shorter sequences)
│ └── zero3.json # DeepSpeed ZeRO-3 config (longer sequences)
└── dataset/ # Training data directory
├── phase1_foundation.jsonl
├── phase2_evolution.jsonl
└── phase3_pr_mastery.jsonl
| Parameter | Default | Description |
|---|---|---|
micro_batch_size | 1 | Per-GPU batch size |
gradient_accumulation_steps | 6 | Steps before optimizer update |
learning_rate | 1e-4 | Learning rate |
lr_scheduler | cosine | LR scheduler type |
warmup_ratio | 0.03 | Warmup proportion |
weight_decay | 0.1 | Weight decay |
max_grad_norm | 1.0 | Gradient clipping |
| Parameter | Default | Description |
|---|---|---|
r | 128 | LoRA rank (higher = more capacity) |
alpha | 256 | LoRA scaling factor |
dropout | 0.05 | LoRA dropout |
target_modules | q,k,v,o,gate,up,down_proj | Modules to apply LoRA |
| Parameter | Default | Description |
|---|---|---|
max_tokens | 32768 | Maximum sequence length |
overlap_tokens | 2048 | Overlap between chunks |
chunk_long_samples | true | Enable automatic chunking |
Compare base model vs fine-tuned outputs interactively:
python inference.py
Features:
/set temperature 0.7 # Adjust temperature
/set max_tokens 2048 # Adjust max generation length
/compare # Enable comparison mode
/quit # Exit
Training logs to:
outputs/<run_name>/tensorboardAfter training completes:
outputs/
└── phased-kat-dev-lora-YYYYMMDD_HHMMSS/
├── phase1_foundation/
│ └── checkpoint-xxx/
├── phase2_evolution/
│ └── checkpoint-xxx/
├── phase3_pr_mastery/
│ └── checkpoint-xxx/ # Final adapters
└── tensorboard/
micro_batch_size to 1zero3.json)max_tokenssample_packing: truenvidia-smitokenizer.eos_token is set correctlypip install -U deepspeedexport NCCL_DEBUG=INFOIf you use this framework, please cite:
@software{phased_cpt_finetuning,
title = {Phased Continual Pre-Training with LoRA},
year = {2024},
url = {https://github.com/YOUR_USERNAME/phased-cpt-finetuning}
}
MIT License - see LICENSE for details.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)Note: This codebase includes a critical fix for EOS token handling that ensures the model learns proper stopping behavior during training.
A production-ready framework for phased fine-tuning of large language models using LoRA (Low-Rank Adaptation) with DeepSpeed optimization. Designed for multi-GPU training on models like Kwaipilot/KAT-Dev (32B parameters).
Python
1
2 commits
updated Dec 10, 2025
A production-ready framework for phased fine-tuning of large language models using LoRA (Low-Rank Adaptation) with DeepSpeed optimization. Designed for multi-GPU training on models like Kwaipilot/KAT-Dev (32B parameters).
This framework implements a curriculum learning approach where the model is trained sequentially across multiple phases, each building upon the previous:
| Phase | Name | Purpose | Default Epochs |
|---|---|---|---|
| 1 | Foundation | Core codebase understanding | 3 |
| 2 | Evolution | Code evolution patterns | 2 |
| 3 | PR Mastery | Pull request and review patterns | 2 |
git clone https://github.com/YOUR_USERNAME/phased-cpt-finetuning.git
cd phased-cpt-finetuning
# Create virtual environment
python -m venv venv
source venv/bin/activate
# Install PyTorch first (adjust for your CUDA version)
pip install torch>=2.1.0
# Install Flash Attention (requires torch to be installed)
pip install flash-attn --no-build-isolation
# Install remaining dependencies
pip install -r requirements.txt
Create JSONL files in the ./dataset directory with the following format:
{"training_content": "Your training text content here..."}
{"training_content": "Another training sample..."}
Required files:
dataset/phase1_foundation.jsonldataset/phase2_evolution.jsonldataset/phase3_pr_mastery.jsonlDataset Generation: For automated dataset creation from GitHub repositories, see the companion tool: Phased-CPT-DatasetGenerator
Edit config.yaml to customize:
# Model Configuration
model:
name: "Kwaipilot/KAT-Dev" # HuggingFace model ID
size: "32B"
# LoRA Configuration
training:
lora:
r: 128 # LoRA rank
alpha: 256 # LoRA alpha (typically 2*r)
dropout: 0.05
# Add your WandB API key
wandb:
api_key: "your-api-key-here" # Or set WANDB_API_KEY env var
project: "your-project-name"
# Using the shell script (recommended)
./train_phased.sh
# Or directly with DeepSpeed
deepspeed --num_gpus=8 train_phased.py
.
├── config.yaml # Main configuration file
├── train_phased.py # Phased training script
├── train_phased.sh # Training launcher with pre-flight checks
├── inference.py # Interactive inference & model comparison
├── requirements.txt # Python dependencies
├── deepspeed_configs/
│ ├── zero2.json # DeepSpeed ZeRO-2 config (shorter sequences)
│ └── zero3.json # DeepSpeed ZeRO-3 config (longer sequences)
└── dataset/ # Training data directory
├── phase1_foundation.jsonl
├── phase2_evolution.jsonl
└── phase3_pr_mastery.jsonl
| Parameter | Default | Description |
|---|---|---|
micro_batch_size | 1 | Per-GPU batch size |
gradient_accumulation_steps | 6 | Steps before optimizer update |
learning_rate | 1e-4 | Learning rate |
lr_scheduler | cosine | LR scheduler type |
warmup_ratio | 0.03 | Warmup proportion |
weight_decay | 0.1 | Weight decay |
max_grad_norm | 1.0 | Gradient clipping |
| Parameter | Default | Description |
|---|---|---|
r | 128 | LoRA rank (higher = more capacity) |
alpha | 256 | LoRA scaling factor |
dropout | 0.05 | LoRA dropout |
target_modules | q,k,v,o,gate,up,down_proj | Modules to apply LoRA |
| Parameter | Default | Description |
|---|---|---|
max_tokens | 32768 | Maximum sequence length |
overlap_tokens | 2048 | Overlap between chunks |
chunk_long_samples | true | Enable automatic chunking |
Compare base model vs fine-tuned outputs interactively:
python inference.py
Features:
/set temperature 0.7 # Adjust temperature
/set max_tokens 2048 # Adjust max generation length
/compare # Enable comparison mode
/quit # Exit
Training logs to:
outputs/<run_name>/tensorboardAfter training completes:
outputs/
└── phased-kat-dev-lora-YYYYMMDD_HHMMSS/
├── phase1_foundation/
│ └── checkpoint-xxx/
├── phase2_evolution/
│ └── checkpoint-xxx/
├── phase3_pr_mastery/
│ └── checkpoint-xxx/ # Final adapters
└── tensorboard/
micro_batch_size to 1zero3.json)max_tokenssample_packing: truenvidia-smitokenizer.eos_token is set correctlypip install -U deepspeedexport NCCL_DEBUG=INFOIf you use this framework, please cite:
@software{phased_cpt_finetuning,
title = {Phased Continual Pre-Training with LoRA},
year = {2024},
url = {https://github.com/YOUR_USERNAME/phased-cpt-finetuning}
}
MIT License - see LICENSE for details.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)Note: This codebase includes a critical fix for EOS token handling that ensures the model learns proper stopping behavior during training.