---
title: "Fine-tuning Llama 2 70B using PyTorch FSDP"
description: "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."
url: "https://swe.quest/articles/fine-tuning-llama-2-70b-using-pytorch-fsdp-0e8dde34b4ad3fd0"
canonical: "https://huggingface.co/blog/ram-efficient-pytorch-fsdp"
source: "Hugging Face"
published: "2023-09-13"
difficulty: "hard"
kind: "How-to"
reading_minutes: 10
tags: ["model-training","llms","hardware","python"]
---

# Fine-tuning Llama 2 70B using PyTorch FSDP

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2023-09-13, 10 min read, How-to. Original: [https://huggingface.co/blog/ram-efficient-pytorch-fsdp](https://huggingface.co/blog/ram-efficient-pytorch-fsdp)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Hardware](https://swe.quest/tags/hardware.md) (Infrastructure)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- PyTorch distributed training ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- Transformer model architecture ([easy posts on System design](https://swe.quest/tags/system-design.md?difficulty=easy))
- Hugging Face Transformers
- SLURM job scheduling ([easy posts on Scheduling](https://swe.quest/tags/scheduling.md?difficulty=easy))

## Keywords

PyTorch FSDP, Llama 2, Hugging Face Accelerate, FlashAttention, SLURM, TRL, bitsandbytes

## Related posts

- [Fine-tuning 20B LLMs with RLHF on a 24GB consumer GPU](https://swe.quest/articles/fine-tuning-20b-llms-with-rlhf-on-a-24gb-consumer-gpu-7ad6c7bb6c14c3cf.md): Hugging Face · 2023-03-09 · Hard · 11 min read · Model training, LLMs, Machine learning, Python
  Combining trl with peft and bitsandbytes makes RLHF fine-tuning of a 20B-parameter LLM feasible on a single 24GB consumer GPU.
- [Accelerate Large Model Training using PyTorch Fully Sharded Data Parallel](https://swe.quest/articles/accelerate-large-model-training-using-pytorch-fully-sharded-data-parallel-513a65557fbde722.md): Hugging Face · 2022-05-02 · Hard · 12 min read · Model training, Machine learning, Python, Performance engineering
  Hugging 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.
- [No GPU left behind: Unlocking Efficiency with Co-located vLLM in TRL](https://swe.quest/articles/no-gpu-left-behind-unlocking-efficiency-with-co-located-vllm-in-trl-6f9360880cc9b7e0.md): Hugging Face · 2025-06-03 · Hard · 22 min read · Model training, Inference & serving, Performance engineering, LLMs
  TRL 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.
- [Make LLM Fine-tuning 2x faster with Unsloth and 🤗 TRL](https://swe.quest/articles/make-llm-fine-tuning-2x-faster-with-unsloth-and-trl-5db8c0b6a40cf74e.md): Hugging Face · 2024-01-10 · Medium · 5 min read · Model training, LLMs, Performance engineering, Python
  Unsloth speeds up QLoRA fine-tuning of Llama and Mistral-family models by rewriting PyTorch modules as Triton kernels with manually derived backpropagation.
- [(LoRA) Fine-Tuning FLUX.1-dev on Consumer Hardware](https://swe.quest/articles/lora-fine-tuning-flux-1-dev-on-consumer-hardware-310a6094e7391e33.md): Hugging Face · 2025-06-19 · Hard · 11 min read · Model training, Computer vision, Python
  FLUX.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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
