Distributed FSDP training with LoRA fine-tuning for the GLM-4.5-Air Mixture-of-Experts model (106B parameters) on 8x Nvidia H200 GPUs.
- ✅ Stable training (87GB/141GB per GPU)
- ✅ CPU offload checkpointing (saves in ~3 min)
- ✅ Checkpoint resume verified
- ✅ Batch size 4 (4x throughput vs initial)
cd "/workspace/MoE Training/fsdp"
source /workspace/MoE\ Training/moe_env/bin/activate
PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True \
NCCL_TIMEOUT=7200 \
accelerate launch --config_file accelerate_config.yaml \
train_fsdp_lora.py --config config.yamlPYTORCH_CUDA_ALLOC_CONF=expandable_segments:True \
NCCL_TIMEOUT=7200 \
accelerate launch --config_file accelerate_config.yaml \
train_fsdp_lora.py --config config.yaml \
--resume_from_checkpoint glm_output/checkpoint-10| Parameter | Value | Notes |
|---|---|---|
| GPUs | 8x H200 (141GB each) | 87GB used per GPU |
| Batch Size | 4 per GPU | 32 global batch |
| Sequence Length | 1024 tokens | Reduced from 4096 |
| LoRA Rank | 16 | 32M trainable params |
| FSDP Policy | SIZE_BASED_WRAP | 500M param threshold |
| Checkpoint Freq | Every 10 steps | ~3 min save time |
- Training Speed: ~0.07 it/s (7 iterations/min)
- Memory Usage: 87GB/141GB per GPU (62% utilization)
- Checkpoint Size: 210GB (sharded) + 121MB (LoRA adapters)
- Stability: No OOM, no NCCL timeouts, no FSDP errors
train_fsdp_lora.py: Main training script with CPU offload checkpointingconfig.yaml: Training hyperparameters and data settingsaccelerate_config.yaml: FSDP and distributed configurationDOCUMENTATION.md: Complete technical documentation ⭐GLM_TRAINING_GUIDE.md: Original training guide
- Stable distributed training across 8 GPUs
- CPU offload checkpoint saving (no OOM/timeout)
- Checkpoint resume with full state restoration
- Gradient checkpointing for memory efficiency
- Expert usage logging for MoE layers
- JSONL training logs
- Gradient accumulation > 1 causes FSDP state error (kept at 1)
- Evaluation disabled (needs synchronization fixes)
- Flash Attention 2 not installed (using SDPA fallback)
- Sequence length limited to 1024 (memory constraints)
Model: GLM-4.5-Air MoE
- Total Parameters: 106.9B
- Trainable (LoRA): 31.6M (0.03%)
- Experts per Layer: 96
- Top-K Routing: 2
LoRA Configuration:
- Rank: 16
- Alpha: 32
- Target Modules:
query_key_value,dense - Dropout: 0.05
Problem: Standard checkpointing methods failed:
accelerator.get_state_dict(): 10+ min timeout (gathering 212GB)get_peft_state_dict(): GPU OOM (tries to clone on GPU)
Solution: Dual checkpoint strategy
- Sharded state via
accelerator.save_state()(fast, parallel) - LoRA adapters via CPU offload (avoids GPU OOM)
# Key code pattern
save_policy = FullStateDictConfig(offload_to_cpu=True, rank0_only=True)
with FSDP.state_dict_type(model, StateDictType.FULL_STATE_DICT, save_policy):
cpu_state_dict = model.state_dict() # Gathers to CPU RAM (1TB+)
# Filter and save LoRA params...Why it works: Uses abundant CPU RAM (1TB+) instead of limited GPU VRAM (141GB).
Critical: Use SIZE_BASED_WRAP with fsdp_min_num_params: 500000000
- ❌
TRANSFORMER_BASED_WRAP: Causes severe OOM - ✅
SIZE_BASED_WRAP: Balanced sharding across GPUs
Required for LoRA + gradient checkpointing:
model = get_peft_model(model, peft_config)
model.enable_input_require_grads() # Critical!
model = model.to(torch.bfloat16)- Check FSDP policy is
SIZE_BASED_WRAP - Verify
block_size: 1024in config.yaml - Confirm
batch_size: 4or lower - Ensure
use_cache: falsein model config
- Set
NCCL_TIMEOUT=7200environment variable - Verify
fsdp_ddp_timeout: 7200in accelerate_config.yaml - Check network connectivity between GPUs
- Ensure using latest
train_fsdp_lora.pywith CPU offload - Verify
fsdp_state_dict_type: SHARDED_STATE_DICT - Check
accelerator.wait_for_everyone()calls present
📖 For complete details, see DOCUMENTATION.md which includes:
- All problems encountered and solutions
- Detailed observations and findings
- Performance analysis
- Configuration explanations
- Best practices and lessons learned
pip install torch transformers accelerate peft datasets pyyamlSee requirements.txt for specific versions.
# Watch training logs
tail -f training_output_new.log
# Monitor GPU usage
watch -n 1 nvidia-smi
# Check checkpoints
ls -lh glm_output/checkpoint-*
du -sh glm_output/checkpoint-*from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
# Load base model
model = AutoModelForCausalLM.from_pretrained(
"THUDM/glm-4-9b-chat",
torch_dtype=torch.bfloat16,
device_map="auto"
)
# Load LoRA adapters
model = PeftModel.from_pretrained(model, "glm_output/checkpoint-50")
tokenizer = AutoTokenizer.from_pretrained("glm_output/checkpoint-50")
# Inference
model.eval()
# ... use for generation- Python: 3.12.3
- PyTorch: 2.9.1+cu128
- Accelerate: 1.11.0
- CUDA: 12.8
- Hardware: 8x Nvidia H200 (141GB VRAM)
Same as GLM-4.5-Air model license.
- Implements CPU offload checkpointing strategy for FSDP+LoRA
- Based on HuggingFace Accelerate and PEFT libraries
- Optimized for Nvidia H200 GPUs
Last Updated: 2025-11-21
Status: Production Ready ✅