Skip to content

Operator task guide

How to debug PyTorch FSDP2 memory and distributed crashes

PyTorch FSDP2 replaces FSDP1 FlatParameter wrapping with per-parameter DTensor sharding via fully_shard(). Most FSDP2 OOMs happen when a model is materialized on CPU/GPU before sharding instead of using torch.device("meta") initialization, or when unsharded forward/backward peaks lack activation checkpointing.

For FSDP2 hangs or DTensor errors during backward or Distributed Checkpoint (DCP) saves, verify all ranks call fully_shard in identical module order, wrap sub-blocks before the root module, and enable TORCH_DISTRIBUTED_DEBUG=DETAIL alongside Flight Recorder.

Reviewed . Reference guidance is not a diagnosis of your workload.

Step-by-step method

  1. 1

    Initialize large models on meta device before applying fully_shard

    Avoid allocating full unsharded weights on every rank GPU at once. Construct on meta, apply fully_shard bottom-up on transformer blocks then root, and materialize or load sharded DCP weights afterward.

    python3 -c "import torch; print(torch.__version__, hasattr(torch.distributed.fsdp, 'fully_shard'))"
  2. 2

    Separate FSDP2 GPU VRAM OOM from CPU offload host OOM

    Inspect torch.cuda.memory_summary() for CUDA OOMs versus host dmesg/sacct MaxRSS if CPUOffloadPolicy killed the process silently with SIGKILL (-9).

    dmesg -T | grep -iE 'Killed process|Out of memory|memory cgroup'
    export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
  3. 3

    Debug DTensor shape mismatch or rank deadlock

    If ranks hang inside backward all-gather or reduce-scatter, check that no rank takes a conditional branch skipping a sharded submodule and enable TORCH_DISTRIBUTED_DEBUG=DETAIL.

    export TORCH_DISTRIBUTED_DEBUG=DETAIL
    export TORCH_NCCL_TRACE_BUFFER_SIZE=2000
    export TORCH_NCCL_DUMP_ON_TIMEOUT=1
  4. 4

    Verify activation checkpointing and reshard_after_forward tradeoffs

    Confirm reshard_after_forward=True on transformer blocks when VRAM is tight, and apply selective or full activation checkpointing on transformer layers rather than shrinking global batch size prematurely.

FSDP2 failure signal to fix

Signals, meanings and actions for how to debug pytorch fsdp2 memory and distributed crashes.
SignalWhat it meansNext action
CUDA OOM during model construction or from_pretrainedFull unsharded model loaded per rank before fully_shard.Initialize inside torch.device("meta") context, shard with fully_shard, then materialize/load.
CUDA OOM during forward/backward despite sharded weightsActivation memory or unsharded layer peak exceeded HBM.Enable activation checkpointing and verify reshard_after_forward=True.
DTensor mesh / shape mismatch across ranksInconsistent tensor parallel / FSDP device mesh or conditional forward.Verify uniform module calls across ranks and consistent DeviceMesh dimensions.
Worker killed with exit code -9 under CPUOffloadPolicyHost RAM / cgroup exhausted by offloaded parameters/optimizer.Check sacct MaxRSS and lower host memory pressure or pin_memory usage.

Evidence checklist

  • PyTorch version and DeviceMesh / fully_shard configuration
  • Model construction and checkpoint load order (meta vs CPU vs CUDA)
  • First traceback across all ranks plus TORCH_DISTRIBUTED_DEBUG=DETAIL
  • torch.cuda.memory_summary() and PYTORCH_CUDA_ALLOC_CONF
  • Host dmesg OOM events when using CPUOffloadPolicy

Common mistakes

Applying fully_shard only to the top-level root model

Sharding only the root forces FSDP2 to all-gather the entire model at once; shard each transformer block first, then the root.

Comparing FSDP2 memory without activation checkpointing

Weight sharding saves static parameter memory, not long-sequence activations.

Mixing FSDP1 FlatParameter hooks with FSDP2 DTensor APIs

Checkpoints and state_dict APIs differ between FSDP1 and FSDP2 (use torch.distributed.checkpoint).

Frequently asked questions

How does PyTorch FSDP2 differ from FSDP1 and DDP?

DDP replicates the entire model on every GPU. FSDP1 shards parameters into flattened 1D FlatParameters. FSDP2 uses per-parameter DTensors via fully_shard(), eliminating FlatParameter metadata complexity and simplifying mixed-precision and TP composability.

When should I choose FSDP2 vs DeepSpeed ZeRO-3?

FSDP2 integrates natively with PyTorch DTensor, torch.compile, and tensor parallelism. DeepSpeed ZeRO-3 offers mature NVMe/CPU offload pipelines (ZeRO-Infinity) and built-in pipeline/offload configs.

Why does FSDP2 still run out of CUDA memory during training?

The usual culprits are loading weights onto GPU before calling fully_shard (fix with meta-device init), large sequence activations without activation checkpointing, or holding reshard_after_forward=False on memory-bound runs.

How do I save checkpoints cleanly in FSDP2?

Use PyTorch Distributed Checkpoint (torch.distributed.checkpoint) with get_model_state_dict / get_optimizer_state_dict rather than naive torch.save on DTensor weights.

Live diagnosis, no signup

Upload your GPU log. Find what happened first.

Get the likely cause, supporting evidence, and next safe steps with the same cloud reasoning used by paid plans. A crash the engine recognizes is answered in seconds, then checked by an independent AI review that updates this page when it finishes. A crash it does not recognize waits for the AI's own answer, which can take a few minutes. No account, card, or installation. Logs are masked at Denpex API ingress and are not saved to account history.

Before uploading, review cloud data handling and local options.

training_logs.txt
No log to hand? Try one:

Upload your log file. Large files are processed in your browser. Only selected evidence is submitted when you run diagnosis.

3 free cloud diagnoses per day

Try Denpex for 30 days. FREE.

No credit card. No automatic subscription. See whether Denpex helps with an incident you already understand.

Start your free 30-day Team evaluation

Up to 50 diagnoses a day for 30 days. Verify your work email to activate an eligible company evaluation, then replay a resolved incident and compare the evidence and next steps with your team's confirmed cause.

Use your company email, for example alex@yourcompany.com.

Who qualifies?

Company email domains activate the free 30-day Team evaluation (up to 50 diagnoses a day) after an email and domain check, whatever mail provider you use. If we cannot verify your domain automatically, support reviews it: write to support@denpex.com. Free local diagnosis in the editor extension needs no account.

Using a personal email (Gmail, Outlook, Naver, QQ) or solo cluster? Create a free $0 account (no work email required) to save 7-day history and unlock AI fallback, or send a cluster crash log for a live walkthrough.

Apply the method to your incident

Use the three free diagnoses to review your error and relevant evidence. Keep reference guidance separate from the cause and recovery status of your own workload.

Diagnose your incident