Skip to content

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

Hugging Face 9 min read How-to mediumAdept

From PyTorch DDP to Accelerate to Trainer, mastery of distributed training with ease

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

Related posts

  • Introducing 🤗 Accelerate

    Hugging Face mediumAdept

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

  • Accelerate 1.0.0

    Hugging Face mediumAdept

    Hugging 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 PyTorch Fully Sharded Data Parallel

    Hugging Face hardMaster

    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.

  • Accelerating PyTorch distributed fine-tuning with Intel technologies

    Hugging Face mediumAdept

    Fine-tuning transformer models can be distributed across a cluster of Intel Ice Lake Xeon CPU servers using the Intel Extension for PyTorch and the oneCCL communication backend, making CPU clusters a cost-effective option for shorter transfer-learning jobs. The walkthrough builds a 4-node EC2 c6i.16xlarge cluster, installs oneAPI toolkits, PyTorch 1.9, IPEX and torch-ccl, sets oneCCL environment variables, patches run_glue.py for rank handling, and launches jobs with mpirun. Fine-tuning on GLUE MRPC drops from 7m46s on one node to 4m39s on two (1.7x) and 2m36s on four (3x). The larger QQP task shows similar scaling, from 11h22m to 3h51m on four nodes (2.95x).

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

The quest for software engineering knowledge.

Look:
Layout: