Fine-tuning Llama 2 70B using PyTorch FSDP
Full fine-tuning of Llama 2 70B across two nodes of 8×A100 80GB GPUs with PyTorch FSDP, Hugging Face Accelerate, Transformers and TRL, addressing three obstacles. CPU RAM exhaustion (~2TB if every rank loads the model) is avoided by loading weights only on rank 0 via the meta device and broadcasting with sync_module_states. Slow, NCCL-timeout-prone checkpointing is fixed by saving intermediate checkpoints as SHARDED_STATE_DICT and switching to FULL_STATE_DICT only for the final save. Flash Attention V2 (kernel fusion, tiling, online softmax) plus gradient checkpointing cut VRAM and time; the run finished in ~13.5 hours, with a paged AdamW optimizer needed on a single node.
Related posts
- Fine-tuning 20B LLMs with RLHF on a 24GB consumer GPU
Hugging Face hardMasterCombining trl with peft and bitsandbytes makes RLHF fine-tuning of a 20B-parameter LLM feasible on a single 24GB consumer GPU. The approach loads the active model in 8-bit using LLM.int8(), trains only small LoRA adapters to shrink optimizer state, and obtains reference logits for PPO from the same model by disabling the adapters, so no second model copy is needed. Memory rules of thumb are 4/2/1 GB per billion parameters in fp32/fp16/int8, plus 8 bytes per parameter for AdamW. A gpt-neox-20b model was fine-tuned on IMDB, its adapters were merged, and it was then RL-tuned to generate positive reviews. The main cost is speed, since adapters make forward and backward passes roughly twice as slow.
- Accelerate Large Model Training using PyTorch Fully Sharded Data Parallel
Hugging Face hardMasterHugging Face Accelerate exposes PyTorch FullyShardedDataParallel (FSDP), which shards parameters, gradients and optimizer states across GPUs (ZeRO stage 2/3) with optional CPU offload, and does so without changes to training code. On 2x24GB Titan RTX GPUs, FSDP gives 2-3x larger batch sizes than DDP for GPT-2 Large. It also trains GPT-2 XL (1.5B), which DDP cannot fit even at batch size 1, and with CPU offload it can do so on a single GPU. DDP with FP16 remained fastest in wall-clock time. Practical caveats: tune the auto-wrap min_num_params, prepare the model before creating the optimizer because parameter flattening loses parameter groups, and note that mixed precision was not yet supported.
- No GPU left behind: Unlocking Efficiency with Co-located vLLM in TRL
Hugging Face hardMasterTRL v0.18.0 can run vLLM co-located with the training job on the same GPUs. vLLM's external_launcher executor puts it inside the torch.distributed process group, replacing the separate HTTP server that left training and inference GPUs idle in turn during GRPO's frequent generation steps. Benchmarks show up to 1.43× throughput for a 1.5B model with larger batches and up to 1.73× for a 7B model with higher tensor parallelism, though extra TP hurts small models. Qwen2.5-Math-72B trained ~1.26× faster on fewer GPUs with matching rewards and Math500 scores, using vLLM level-2 sleep to free memory and DeepSpeed ZeRO-3 with CPU optimizer offload. The write-up also covers debugging a TP seed-determinism regression, a buffer-restore bug after sleep and a remaining shutdown segfault.
- Make LLM Fine-tuning 2x faster with Unsloth and 🤗 TRL
Hugging Face mediumAdeptUnsloth speeds up QLoRA fine-tuning of Llama and Mistral-family models by rewriting PyTorch modules as Triton kernels with manually derived backpropagation. Because no approximations are made, it claims 0% accuracy loss relative to standard QLoRA. Across 59 benchmark runs on A100 and T4 GPUs, it ran 1.5–3.9x faster than Hugging Face transformers and used up to 74% less VRAM. Code examples show loading pre-quantized 4-bit models with FastLanguageModel, attaching LoRA adapters, and training with TRL's SFTTrainer or DPOTrainer.
- (LoRA) Fine-Tuning FLUX.1-dev on Consumer Hardware
Hugging Face hardMasterFLUX.1-dev can be LoRA fine-tuned on a single consumer GPU by combining QLoRA (4-bit NF4 base transformer via bitsandbytes), 8-bit AdamW, gradient checkpointing, cached VAE latents and precomputed CLIP/T5 text embeddings, so that only the quantized transformer plus adapters sit in GPU memory. On an RTX 4090, a rank-4 LoRA at 512x768 peaked around 9 GB VRAM, compared with 26 GB for BF16 LoRA and an estimated ~120 GB for full fine-tuning, and 700 steps took about 41 minutes to learn Alphonse Mucha's style. The guide includes the diffusers training script settings, an embedding precompute script, FP8 training with torchao for faster runs on compatible hardware, and options for loading or merging the trained adapters at inference.