Introducing π€ Accelerate
π€ Accelerate lets PyTorch users keep their own training loops while running the same script on CPU, single or multi-GPU, multi-node, TPU, and with mixed precision, by adding about five lines: an Accelerator object, accelerator.prepare() for model/optimizer/dataloader, and accelerator.backward(). prepare() wraps the model in the right container such as DistributedDataParallel, wraps the optimizer for mixed precision, and shards any sampler's indices per process using synchronized RNGs instead of requiring DistributedSampler. Distributed evaluation uses accelerator.gather plus truncation of padded batches. An `accelerate config` / `accelerate launch` CLI replaces torch.distributed.launch arguments and can target SageMaker.
Related posts
- From PyTorch DDP to Accelerate to Trainer, mastery of distributed training with ease
Hugging Face mediumAdeptConverting a basic PyTorch MNIST training script to multi-GPU Distributed Data Parallel training is shown at three levels of abstraction. The first is native torch.distributed with process-group setup, a DDP wrapper and the torchrun launcher. The second is Hugging Face Accelerate's Accelerator.prepare, which keeps the loop nearly unchanged, runs on single GPU, multi-GPU or TPU, and shards batches through custom samplers so only one copy of the dataset sits in memory. The third is the Transformers Trainer, which needs only a custom collate function and compute_loss override, and any of these can be launched from Jupyter via notebook_launcher.
- Accelerate 1.0.0
Hugging Face mediumAdeptHugging Face has published the first release candidates for Accelerate 1.0.0. The library has grown from a thin multi-GPU/TPU training-loop wrapper into the base layer for transformers, diffusers, peft and trl, covering six accelerator types, a launch CLI and big-model inference via device_map="auto". Before 1.0 the team added FP8 support through MS-AMP and TransformerEngine, multi-model DeepSpeed orchestration, torch.compile for big model inference, torch.distributed.pipelining and StatefulDataLoader. Migration notes list the breaking deprecations, such as moving dataloader flags into DataLoaderConfiguration, using AutocastKwargs and renaming is_tpu_available to is_torch_xla_available.
- Accelerate Large Model Training using DeepSpeed
Hugging Face mediumAdeptHugging 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.
- 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.
- How π€ Accelerate runs very large models thanks to PyTorch
Hugging Face mediumAdeptAccelerate loads and runs models too large for a single GPU or for CPU RAM by changing PyTorch's usual load path. It creates an empty model on the meta device, computes a device map that fills GPUs first, then CPU RAM, then disk, and loads sharded checkpoints one shard at a time so peak RAM is bounded by the largest shard (7.19GB for BLOOM instead of 352GB). Module hooks then move inputs and offloaded weights onto the right device at forward time. Practical details include no_split_module_classes for residual blocks, device_map strategies (auto, balanced_low_0, sequential), dtype effects on placement, and offload_folder/offload_state_dict for fitting OPT-13B on Colab.