/pytorch-opt-skills:pytorch-ddpfrom pytorch-opt-skills
Tune PyTorch data-parallel training (DistributedDataParallel, and FSDP / HSDP), single- or multi-node, with two calibration jobs and a set of formulas instead of a knob sweep: decide whether communication is the bottleneck at all, then pick the strategy, visible GPUs/NICs, gradient accumulation and micro-batch size, bucket_cap_mb or FSDP unit size and resharding, comm hooks or reduce dtype (bf16/fp16/noop/PowerSGD), gradient_as_bucket_view, batched_grad_copy, static_graph and NCCL settings from measurements. Use when multi-GPU or multi-node scaling efficiency is below what you expect, when choosing DDP or FSDP knobs on a new project or cluster, when gradient accumulation looks like it all-reduces every micro-batch (no_sync not working), when torch.compile + DDP/FSDP misbehaves, when NCCL bandwidth, NVLink or GPUDirect RDMA is suspect, when one slow rank stalls the job, or when deciding if a model has outgrown DDP (FSDP/HSDP/ZeRO).
Ships in pytorch-opt-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Core principle: predict first, then measure only what the prediction says can matter. Two calibration jobs supply every input the formulas need. The formulas say which change can buy more than ~1% of the step, and one validation job tests only those changes.
The order of payoff is the same on every cluster and for every strategy:
no_sync, FSDP set_requires_gradient_sync(False)). A silent break
multiplies the per-sync communication by ga.c_mb), and that can cost more than all of its communication.ga or b, lower precision on the wire) recover a large share
of it. Granularity (buckets, units) and NCCL env stay near ties in both
cases.Most tuning that does nothing was spent on item 5 while items 1-4 were broken, or while communication was already under 1% of the step.
Files:
formulas.md: the formulas for any data-parallel strategy, with
inputs, the decision each drives, and caveats. A strategy enters
through its collective inventory.references/ddp.md: DDP's collectives, its correctness gate (no_sync and the
compile traps), F3 bucket size, F14 tail, F7 comm hook, its knobs, and
when to leave it.references/fsdp.md: FSDP2 and HSDP: choosing the strategy, the collectives, the
correctness gate, what sharding does to the compute, unit size,
resharding, mixed precision, the working set, the knobs.scripts/collective_probe.py (under torchrun): Job B's probe. A size
sweep of all-reduce, all-gather and reduce-scatter on the world, node
or replica group, with the t_o/w fit. Standalone, so a project
needs no probe code: run it as one more step of Job B's allocation, with
the training arms' NCCL environment and visible GPUs.scripts/nccl_topo.py <nccl log dir>: the NVLink topology NCCL detected
for every communicator; flags GPUs with NVLink down and communicators
that saw less NVLink than the same GPUs elsewhere.scripts/ddp_trace.py rank*.json: any torch.profiler trace, one per
rank, from a loop that labels forward and backward per micro-batch
with record_function. Per micro-batch: collectives by type, group and
phase, transfer (min across ranks), exposed comm, forward and backward
compute; for DDP buckets, tail and overlap slowdown.sync_cost, c_mb, imbalance and stalls come from per-rank step timings
("Reading the numbers"), in whatever form the project's trainer logs them.
| step | GPU time | produces |
|---|---|---|
| 0. Preflight | none | strategy, correctness guard, visible GPUs, node placement, memory fit, first guess |
| 1. Job A: 1-GPU ceiling | 1-GPU arms inside Job B's allocation | throughput ceiling, compute and memory constants |
| 2. Job B: target shape | >= 2 nodes | every comm, rank and stall input |
| 3. Predict | none | the step ledger (F11) and the decision sheet |
| 4. Validate | one job, only if step 3 finds a change worth >= 1% | the changes that ship |
| 5. Production node count | only if needed | the scale-out check |
references/fsdp.md, "Choosing the strategy"): DDP
whenever it fits, since it communicates least and leaves the compute
alone; else HSDP, else FSDP.ddp_model.compile(), not
torch.compile(ddp_model)), so no helper can strip DDP off and break
no_sync (references/ddp.md). If the code keeps torch.compile(DDP(model)),
assert at train start and after every checkpoint save that no_sync
still resolves to DistributedDataParallel.no_sync. For FSDP, build the
optimizer after sharding and clip with the sharded API (references/fsdp.md).
With process groups beyond the world one (HSDP, tensor parallel),
create their communicators at startup, before any in-process NVML
sampler starts (gate 2).nvidia-smi topo -m,
ibstat, lspci -tv, or NCCL's topology dump). If a job uses fewer
GPUs than a node has, choose them so every fast NIC has a visible GPU
on its PCIe switch, and balance sockets where possible (F9). Note any
NICs that must not carry NCCL traffic (management, storage, slow
Ethernet).scontrol show topology). If the fabric may be oversubscribed, keep Job B on one
leaf (Slurm: --switches=1@<max wait>), and record the leaves of every
run. Keep a list of nodes with
known-bad GPUs and exclude them.6 P FLOPs per token at the expected
MFU, forward ~1/3 of it), it gives the regime:
a = 1 is already under
~1%, Job B is a check, not a search.(transfer - window) / step.
Plan B_sync and b (F2) and precision on the wire before anything
else.One GPU, no wrapper, production config, at two micro-batch sizes (b and
b/2, or 2b if it fits); read only steady-state steps (after compile
and autotune warmup). Run it as 1-GPU arms of Job B's allocation: no
extra queue wait, and the ceiling comes from the same node as the
multi-GPU arms. Run it apart only if the scheduler cannot split an
allocation, and skip it if a 1-GPU benchmark of the same model on the
same hardware exists. If the model does not fit on one GPU, see references/fsdp.md
("Process changes").
| read | feeds |
|---|---|
tok/s per GPU R, step time T_1 | F2, F11 |
accumulation micro-batch time, forward and backward, at both b | F11 (d_load), F13, F15 windows |
peak allocated memory, at both b | F12 |
Setup:
= 2 nodes, because the inter-node fabric is what the formulas describe.
ga, which must be >= 2 to measure sync_cost.NCCL_DEBUG=INFO,
NCCL_DEBUG_SUBSYS=INIT,ENV,NET,GRAPH,TUNING, one file per process
(NCCL_DEBUG_FILE=<dir>/%h.%p.log), and a topology dump
(NCCL_TOPO_DUMP_FILE).scripts/collective_probe.py does this if the
trainer has no probe of its own.Arms, run back to back in one allocation: the strategy file's arms
(references/ddp.md: base, optional noop; references/fsdp.md: base, a resharding
control). For a sharded strategy, add a DDP arm when DDP fits: it is the
reference that measures c_mb.
Gates, in order. If one fails, fix it and rerun Job B before reading anything else:
scripts/nccl_topo.py). NCCL detects the topology when a
communicator is created and keeps the paths for its whole life. A GPU
whose NVLinks are down, or read as down at that moment, sends the
communicator over PCIe (PHB/SYS paths, fewer channels, no NVLS):
several times slower, no error. A GPU low in every communicator is a
hardware fault (nvidia-smi nvlink -s): exclude its node. A
communicator low where others on the same GPUs are fine was created
while something in the same process polled NVML from a thread (a
telemetry or GPU-stats sampler): NCCL's link-state reads then fail
and count as links down. Sub-communicators (HSDP, tensor parallel)
are created lazily, at their first collective, so they are the usual
victims. Create every communicator at startup, before such threads
start (one small collective on each process group);NET/Socket means TCP through the host, which is a
fallback unless the cluster has no RDMA;w_k is at least ~0.7x
B_node / c_k (F9).Then read the inputs:
| read | from | feeds |
|---|---|---|
t_o,k, w_k per collective type (fit of each size sweep) | probe | F9, F0, DDP F3 |
t_k, w_eff,k, call sizes, per collective and phase | trace (min across ranks) | F2, F9 check, F15 |
| exposed comm per collective, overlap slowdown, DDP tail | trace | F15, F6, DDP F7/F14 |
| forward and backward compute per micro-batch | trace | F15 windows, c_mb |
sync_cost (gives a), loaded accumulation micro-batch (gives d_load, c_mb) | per-rank timings | F11, F2 |
| per-rank compute speed, slowest rank, imbalance wait | per-rank timings | F10 |
| stalled steps and their extra time | per-rank timings | F5 |
| algorithm, protocol and channels per message size | NCCL TUNING lines | fit range, F6 |
First fill in the step ledger (F11): the 1-GPU step, node load, the
strategy's per-micro-batch cost c_mb, sync_cost, imbalance and stalls.
Only sync_cost and the comm part of c_mb respond to comm knobs. If
they are under ~1% of the step, skip every comm-knob row below and work on
the largest other term. If c_mb is large but its comm is hidden, diff
the compute by kernel against the DDP or 1-GPU trace (F11).
Then find the regime (F15) of each collective: is its transfer shorter than the compute window it must hide behind (hidden), or longer (exposed)? The regime decides which rows below can pay.
Then go through the decision sheet: the rows below, plus the strategy
file's rows (references/ddp.md, references/fsdp.md). A default costs no arm.
| decision | formula | change when | default |
|---|---|---|---|
| strategy | F12, F11 | DDP does not fit, or a sharded strategy's c_mb exceeds its memory benefit | DDP if it fits |
| visible GPUs per node | F9 | a fast NIC has no visible GPU | the allocation's GPUs |
| drain a GPU or node | F10, gate 2 | one rank is > ~1% slower than the median on every step, or a GPU has no NVLink | keep |
| fix hosts before tuning | F5 | expected stall loss at the target W exceeds the comm share | - |
tokens per sync, B_sync = ga * b | F2, F4 | per-sync comm share > target at the target W | production ga |
micro-batch b | F13, F12, F2 (C_mb) | the fixed cost share is far from ~3%, per-micro-batch comm is exposed, or b exceeds b_max | production b |
| NCCL env (CTAs, algorithm, protocol) | F6, F0 | hidden regime, the overlap slowdown is large next to the comm cost, and the change does not stretch transfer | NCCL's choice |
In the exposed regime the order is: tokens per collective first (no
numerics change), then lower precision on the wire (check the loss curve).
Changing B_sync changes the global batch, and so the optimization: keep
the global batch fixed unless that trade-off is intended, and compare
throughput only at equal global batch.
Step 4 gets only the changes predicted to buy >= ~1% of the step, plus free changes with a clear mechanism (visible GPUs, draining a slow GPU). Smaller effects sit inside run-to-run noise and cannot be confirmed anyway.
Run in one allocation: base, one arm per change from step 3, and one arm
with all of them. Judge each arm by its trace (transfer, exposed comm and
overlap slowdown; F0) and by sync_cost and c_mb. Throughput from a short arm is
dominated by whichever steps happened to stall. Accept a change when its
direction matches the prediction and its size is within ~25%. Otherwise
record the miss against the formula before shipping anything.
If production runs on more nodes than Job B, predict before running:
W ranks;Run a job at the target size only if a prediction lands near a threshold, or the fabric may be blocking.
scripts/nccl_topo.py):
GPU-to-GPU paths of type NVL, NVLS channels where the platform has
NVLink SHARP, and the same channel count on every node.NCCL_IB_HCA or NCCL_SOCKET_IFNAME falls back silently. = in
NCCL_IB_HCA makes the match exact.NCCL_NET_GDR_LEVEL can force it. A/B that
change: across a CPU socket link it can be slower than staging.ENV lines confirm which NCCL_* overrides took effect.TUNING and per-size algorithm lines show the algorithm, protocol
and channel count NCCL actually chose. Do not read them off the
profiler's kernel name, which reflects a default specialization (the
dtype in the name is reliable).sync_cost is the cost of the sync: t_sync_mb - t_accum_mb per
rank (with backward timings: backward_sync - backward_nosync/(ga-1)),
then the min across ranks per step, then the median over steps. One
rank's reading includes waiting for its peers, and one stall wrecks a
mean.c_mb is the strategy's cost on every micro-batch: the
accumulation micro-batch time minus a same-allocation DDP arm's (or
minus the 1-GPU one times 1 + d_load).sync_cost and
c_mb are the cost. Tail (DDP): the end of the last bucket minus the end of the backward
compute it waited on.references/fsdp.md).B_sync and b.Only after the ledger says comm matters. A/B one setting at a time on the
same nodes, confirm it in the ENV and TUNING lines, and judge it with
F0. The usual pattern:
NCCL_MAX_CTAS, CTA policies) lower the
overlap slowdown but stretch transfer.A setting that lowers sync_cost by stretching transfer only moves the
cost into the margin you will need when scaling out.
c_k (F9), and it is what nccl-tests and many
probes print. Mixing them is a ~2x error on all-reduce.sync_cost and c_mb from the wall clock of clean steps.multi_tensor_apply serves the optimizer, gradient clipping and FSDP's
copies alike, and NCCL kernel names show a default specialization.recompile_limit (or TORCH_LOGS=recompiles).torch.cuda.get_device_properties(i).pci_bus_id), or it reads another
GPU.G with fewer GPUs per node in one allocation.#SBATCH lines after the first command are ignored without a
warning. Confirm --exclude and --dependency in scontrol show job.What SKILL.md sets; muted values are Claude Code's defaults
pytorch-opt-skills@mila-skills is listed by mila-iqia/skills — review the source before installing
npx claudepluginhub mila-iqia/skills --plugin pytorch-opt-skills5 skills in this plugin
/pytorch-compileDiagnose and fix torch.compile problems: graph breaks, recompilation, slow compile time, and compile-mode / runtime tradeoffs. Use when torch.compile is slow to compile, slower than eager, stalls for minutes on step 1 or on every new shape, or when you see graph break, "Recompiling function", guard failure, cache_size_limit / recompile_limit, dynamic shapes, mark_dynamic, tlparse, TORCH_TRACE, TORCH_LOGS=recompiles/graph_breaks, Dynamo, Inductor, Triton autotune, max-autotune, coordinate_descent_tuning, specialize_float, CUDA graphs / reduce-overhead, cudagraph_mark_step_begin, allow_in_graph, torch.compiler.disable, or "accessing tensor output of CUDAGraphs". Also for reusing compiled artifacts across jobs or nodes — megacache / Mega-Cache, save_cache_artifacts, load_cache_artifacts, cold vs warm compile, TORCHINDUCTOR_CACHE_DIR — for profiling a compiled model (kernels are fused and invisible to module hooks), for measurement hygiene when benchmarking compiled vs eager, and for the optimizer step outside the compiled model — fused=True, foreach, fused Adam / AdamW / SGD, compiling optimizer.step, LR scheduler recompiles./pytorch-dataloaderDiagnose and fix PyTorch input pipelines that starve the GPU: DataLoader wait, low or bursty GPU utilization, periodic or rare multi-second stalls, eval slower than train, slow time to first step, worker host-memory blowups. Use when tuning num_workers, prefetch_factor, pin_memory, non_blocking, persistent_workers or in_order; when next() wait may be a CUDA sync; when samples come from mmap, Arrow / HF datasets, LMDB, HDF5, .npy or shards on network storage (Lustre, BeeGFS, VAST/NFS; Mila and Alliance/DRAC clusters) or are staged to $SLURM_TMPDIR; when data outgrows page cache or local disk; for chunk shuffling or shuffle buffers; when scaling to more nodes or faster GPUs; or when one DDP rank waits on another's late batch.Other plugins' skills for the same job