Distributed training

From j4flmao/agent-skills · View interactive version

Distributed training is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about multi-GPU training, DDP, FSDP, ZeRO, DeepSpeed, torchrun, tensor/pipeline parallel, all-reduce or NCCL slowness. Produces a parallelism plan with per-GPU memory math, launch command and config. Not for fabric tuning (use rdma-roce) or experiment logging (use ml-experimen

How to use this skill

Skill file

Distributed Training

Purpose

Train models that are too slow or too large for one GPU by splitting data, optimizer state, parameters, or layers across devices. The right strategy follows from a memory budget and the interconnect topology; communication cost, not FLOPs, usually decides scaling efficiency.

Agent Protocol

Trigger

"distributed training", "multi-gpu", "multi-node", "DDP", "FSDP", "ZeRO", "DeepSpeed", "Megatron", "all-reduce", "tensor parallel", "pipeline parallel", "torchrun", "NCCL".

Input Context

Output Artifact

Parallelism plan + launch command + config. No file unless requested.

Response Format

Memory/GPU: params {GB} + grads {GB} + optimizer {GB} + activations {GB}
Strategy: {DDP|FSDP/ZeRO-n|TP=x PP=y DP=z}
Launch: {torchrun/deepspeed command}
Watch: {MFU, step time, comm/compute overlap}

Completion Criteria

Memory Model

Mixed-precision Adam needs ~16 bytes/parameter before activations: 2 (BF16 weights) + 2 (BF16 grads) + 12 (FP32 master weights, momentum, variance). A 7B model therefore needs ~112 GB of model state, more than one 80 GB GPU.

| Scheme | Model-state bytes/param per GPU (N data-parallel ranks) |

|--------|-----------------------------------------------------|

| DDP | 16 |

| ZeRO-1 (shard optimizer) | 4 + 12/N |

| ZeRO-2 (+ shard gradients) | 2 + 14/N |

| ZeRO-3 / FSDP full shard (+ shard params) | 16/N |

Activations scale with micro-batch x sequence x hidden x layers; reduce with activation checkpointing (recompute) and smaller micro-batches.

Choosing a Strategy

Model state fits on one GPU?
├── Yes → DDP (simplest, fastest per-byte)
└── No → FSDP / ZeRO-3 within and across nodes
          Still too large or too slow (layer > GPU, poor overlap)?
          └── Add tensor parallel (TP) inside a node (needs NVLink)
              + pipeline parallel (PP) across nodes
              + context/sequence parallel for very long sequences

Rule of thumb: TP traffic is per-layer and latency-sensitive, so keep TP within an NVLink domain; DP/FSDP traffic tolerates the slower inter-node fabric.

Collectives

PyTorch DDP

import os
import torch
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.data import DataLoader, DistributedSampler

def main() -> None:
    dist.init_process_group(backend="nccl")
    local_rank = int(os.environ["LOCAL_RANK"])
    torch.cuda.set_device(local_rank)

    model = build_model().cuda(local_rank)          # build_model: user-defined
    model = DDP(model, device_ids=[local_rank])
    sampler = DistributedSampler(dataset, shuffle=True)  # dataset: user-defined
    loader = DataLoader(dataset, batch_size=8, sampler=sampler, num_workers=4, pin_memory=True)
    opt = torch.optim.AdamW(model.parameters(), lr=3e-4)

    for epoch in range(10):
        sampler.set_epoch(epoch)                     # different shuffle each epoch
        for batch in loader:
            with torch.autocast("cuda", dtype=torch.bfloat16):
                loss = model(**batch).loss
            loss.backward()
            opt.step()
            opt.zero_grad(set_to_none=True)
    dist.destroy_process_group()

if __name__ == "__main__":
    main()
# single node, 8 GPUs
torchrun --standalone --nproc_per_node=8 train.py
# multi node (run on every node)
torchrun --nnodes=4 --nproc_per_node=8 --rdzv_backend=c10d --rdzv_endpoint=$HEAD:29500 train.py

PyTorch FSDP2 (`fully_shard`)

from torch.distributed.fsdp import fully_shard, MixedPrecisionPolicy

mp = MixedPrecisionPolicy(param_dtype=torch.bfloat16, reduce_dtype=torch.float32)
for block in model.layers:            # shard each transformer block separately
    fully_shard(block, mp_policy=mp)
fully_shard(model, mp_policy=mp)      # root module last

Wrap per transformer block so only one block's full params are materialized at a time. Save with torch.distributed.checkpoint (DCP), not torch.save(model.state_dict()) on rank 0.

DeepSpeed ZeRO Config

{
  "train_micro_batch_size_per_gpu": 4,
  "gradient_accumulation_steps": 8,
  "bf16": { "enabled": true },
  "zero_optimization": {
    "stage": 3,
    "overlap_comm": true,
    "contiguous_gradients": true,
    "stage3_gather_16bit_weights_on_model_save": true
  },
  "gradient_clipping": 1.0
}

offload_optimizer / offload_param (CPU or NVMe) trade large slowdowns for capacity; use only when GPUs are insufficient.

Diagnostics

export NCCL_DEBUG=INFO          # prints chosen transport (NVLink/P2P, IB, Socket) and algorithms
export TORCH_NCCL_ASYNC_ERROR_HANDLING=1

Anti-Patterns

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.