How answerdotai/fsdp_qlora works
Purpose and Entry Points
fsdp_qlora is an alpha CLI training system for fine-tuning Hugging Face LLMs with PyTorch Fully Sharded Data Parallel (FSDP), including full tuning, LoRA/DoRA adapters, and 4-bit bitsandbytes or HQQ quantization. The primary entry point is train.py; launch wrappers target local, benchmark, and SLURM/multi-node execution: train.sh, fsdp_multi_node.sh, and benchmarking/large_gpu_benchmarking.sh. hf_train.py is a separate TRL SFTTrainer comparison path.
Distributed Training Orchestration
train.py owns argument-driven selection of model, dataset, precision, train type, checkpointing, CPU offload, logging, and world size. It initializes distributed GPU workers and applies FSDP sharding/wrapping around supported Transformer layers. FSDP is the central distributed boundary: model states, gradients, and optimizer work are partitioned across CUDA ranks, with optional CPU offload and synchronized module state.
Paths: train.py, fsdp_multi_node.sh, train.sh.
Model Loading and Quantized Initialization
For QLoRA/HQQ modes, the system bypasses ordinary Transformers pretrained loading limitations with custom low-memory loading: weights are loaded iteratively, quantized on GPU, then returned to CPU or meta-device while FSDP initialization synchronizes quantization state. This avoids materializing a full quantized model on one GPU before sharding. Bitsandbytes supplies NF4 Linear4bit; HQQ is an optional alternative with custom CUDA kernels.
Paths: train.py, nbs/02-qlora-memeff-loading.ipynb, train_hqq_bench.sh.
Adapter and Fine-Tuning Strategies
Training mode dispatch distinguishes full parameter updates from PEFT LoRA, custom LoRA, custom DoRA, bitsandbytes QLoRA/DoRA, HQQ LoRA/DoRA, and Llama-Pro variants. Custom adapter implementations are repo-owned and are the extension boundary for injected trainable layers over frozen/quantized base weights.
Paths: scripts/lora.py, scripts/dora.py, train.py.
Llama-Pro Model Expansion
Llama-Pro support is a preprocessing stage rather than a training runtime feature: it expands a base model’s blocks and writes derived weights, which train.py later receives through --llama_pro_path for BnB or HQQ training modes.
Paths: scripts/block_expansion.py, train.py, tests/test_block_expansion.py.
Training Data, Execution, and Persistence Flow
The main flow is CLI options → distributed/model setup → dataset tokenization and batched training → forward/backward/optimizer under selected precision and FSDP → metrics logging and optional adapter-state save. Saved custom quantized modes contain trainable LoRA weights only; inference must reload and quantize the base model separately. Conversion toward standard Hugging Face artifacts is documented in a notebook.
Paths: train.py, Converting the State Dict.ipynb, profiling_utils.py.
Runtime and External Dependencies
The runtime is Python with PyTorch 2.2+ on CUDA, FSDP, Transformers, and Hugging Face model/dataset access (including authenticated Llama access). Optional integrations are PEFT, bitsandbytes, HQQ/CUDA kernels, Weights & Biases, TRL (hf_train.py), and SLURM. Native FlashAttention 2 is available through recent PyTorch CUDA builds.
Paths: hf_train.py, fsdp_multi_node.sh, profile.sh.
Main entry points: train.py Training CLI · 02-qlora-memeff-loading.ipynb Memory-efficient quantization · train.sh Local launcher · fsdp_multi_node.sh Multi-node launcher
Components 16
Launch & orchestration
- Local launcher
train.sh - Multi-node launcher
fsdp_multi_node.sh - Large GPU benchmark
benchmarking/large_gpu_benchmarking.sh - Training CLI · Selects model, data, precision, training mode, and checkpoint behavior.
train.py
Distributed training
- CUDA rank setup
train.py - FSDP boundary · Shards model, gradients, and optimizer state across ranks.
train.py - Model loading
train.py - Memory-efficient quantization · Iteratively quantizes weights without materializing a full model on one GPU.
nbs/02-qlora-memeff-loading.ipynb - Tokenization & training loop · Batches data and runs forward, backward, and optimizer steps.
train.py - Metrics & profiling
profiling_utils.py - Adapter state save · Custom quantized modes save trainable LoRA weights only.
Converting the State Dict.ipynb
Training extensions
- Fine-tuning mode dispatch · Chooses full tuning, PEFT, custom, BnB, HQQ, or Llama-Pro modes.
train.py - Custom LoRA · Repo-owned trainable layers over frozen base weights.
scripts/lora.py - Custom DoRA
scripts/dora.py - Llama-Pro expansion · Expands model blocks and writes derived weights before training.
scripts/block_expansion.py - TRL comparison path
hf_train.py
Connections 18
- Local launcher → Training CLI: launches
- Multi-node launcher → Training CLI: launches
- Large GPU benchmark → Training CLI: launches
- Training CLI → CUDA rank setup: initializes
- CUDA rank setup → FSDP boundary: forms ranks
- Training CLI → Model loading: configures
- Model loading → Memory-efficient quantization: for QLoRA/HQQ
- Memory-efficient quantization → FSDP boundary: synchronizes state
- Training CLI → Fine-tuning mode dispatch: selects mode
- Fine-tuning mode dispatch → Custom LoRA: custom LoRA
- Fine-tuning mode dispatch → Custom DoRA: custom DoRA
- Llama-Pro expansion → Model loading: derived model path
- Model loading → FSDP boundary: wrapped and sharded
- Fine-tuning mode dispatch → Tokenization & training loop: defines trainable layers
- FSDP boundary → Tokenization & training loop: distributed execution
- Tokenization & training loop → Metrics & profiling: emits metrics
- Tokenization & training loop → Adapter state save: saves adapter state
- TRL comparison path → Tokenization & training loop: alternative SFT path