Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 12 min read How-to hardMaster

Accelerate Large Model Training using PyTorch Fully Sharded Data Parallel

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. 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.

Assumes: PyTorch training loopsDistributed Data ParallelGPU memory constraints

Related posts

  • Fully Sharded Data Parallel: faster AI training with fewer GPUs

    Meta Engineering hardMaster

    Fully Sharded Data Parallel (FSDP) shards model parameters, gradients and optimizer states across data-parallel GPU workers. It does this by decomposing DDP's all-reduce into reduce-scatter and all-gather steps, gathering each layer's full weights just in time for the forward and backward passes and then discarding them. It produces results identical to PyTorch DDP, overlaps communication with computation, can offload optimizer state to CPU, and is meant to scale to trillions of parameters. It is available through FairScale, fairseq, VISSL and PyTorch Lightning, with caveats around nested wrapping, consistent weight initialization, optimizer parameter groups, mixed precision and checkpointing.

  • Accelerate Large Model Training using DeepSpeed

    Hugging Face mediumAdept

    Hugging Face Accelerate can enable DeepSpeed ZeRO data parallelism, which shards optimizer states, gradients and parameters across GPUs and optionally offloads them to CPU, with no or minimal code changes. On two 24GB Titan RTX GPUs, ZeRO Stage-2 allowed 5x larger batches than DDP for DeBERTa-XL (900M) and gave about a 3.5x training speedup with no accuracy loss. BlenderBot-400M fine-tuning saw 2x batch size and a ~1.44x training speedup. ZeRO Stage-3 with CPU offload made GPT-2 XL (1.5B) trainable at batch size 16 where DDP ran out of memory even at batch size 1. The post includes the accelerate config, DeepSpeed JSON configs, and the DummyOptim/DummyScheduler changes needed when the optimizer and scheduler are defined in the DeepSpeed config.

  • From DeepSpeed to FSDP and Back Again with Hugging Face Accelerate

    Hugging Face hardMaster

    DeepSpeed ZeRO-3 and PyTorch FSDP gave different training results on Mistral-7B loaded in bfloat16 because DeepSpeed silently upcasts flattened parameters and optimizer state to fp32, while FSDP keeps them in the loaded dtype. That made the same learning rate converge on one backend and stall on the other. Hugging Face Accelerate 0.30.0 adds automatic fp32 upcasting for FSDP when mixed precision is enabled, so FSDP can run either in a DeepSpeed-like mixed-precision mode or in a low-precision mode for memory-constrained setups. In that aligned mode, throughput on four A100s with Granite 7B is roughly equal: about 3159 vs 3095 tokens/sec/GPU and an MFU of 0.41 vs 0.40.

  • Fine-tuning Llama 2 70B using PyTorch FSDP

    Hugging Face hardMaster

    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.

  • Accelerate ND-Parallel: A guide to Efficient Multi-GPU Training

    Hugging Face hardMaster

    Accelerate's ParallelismConfig, also exposed through Axolotl config fields, lets you compose data parallelism, FSDP, tensor parallelism and context parallelism in one training setup. The guide explains how each strategy trades memory for communication. FSDP shards weights, gradients and optimizer states and gathers each layer when needed. Tensor parallelism splits linear layers column-wise then row-wise so only one all-reduce is required, which makes it best kept within a single NVLink node. Context parallelism shards the sequence and uses RingAttention so very long contexts fit in memory. It then covers hybrid combinations such as HSDP, FSDP+TP and FSDP+CP, with guidance on matching each parallelism degree to intra-node versus inter-node bandwidth.

The quest for software engineering knowledge.

Look:
Layout: