Agent skill
pytorch-distributed
Distributed training strategies including DistributedDataParallel (DDP) and Fully Sharded Data Parallel (FSDP). Covers multi-node setup, checkpointing, and process management using torchrun. (ddp, fsdp, distributeddataparallel, torchrun, nccl, rank, process-group)
Install this agent skill to your Project
npx add-skill https://github.com/cuba6112/skillfactory/tree/main/skills/pytorch-distributed
SKILL.md
Overview
PyTorch Distributed enables training models across multiple GPUs and nodes. DistributedDataParallel (DDP) is the standard for multi-process data parallelism, while Fully Sharded Data Parallel (FSDP) shards model state to allow training models too large for a single GPU.
When to Use
Use DDP for general multi-GPU training on a single or multiple nodes. Use FSDP when model parameters, gradients, and optimizer states exceed the memory of a single GPU.
Decision Tree
- Does your model fit on one GPU?
- YES: Use
DistributedDataParallel(DDP). - NO: Use
Fully Sharded Data Parallel(FSDP).
- YES: Use
- Are you launching the job?
- USE:
torchrunto handle environmental setup and fault recovery.
- USE:
- Are you saving a checkpoint?
- DO: Only save on
rank == 0to avoid file corruption and redundant I/O.
- DO: Only save on
Workflows
-
Setting Up a DDP Training Job
- Initialize the process group using
dist.init_process_group()with appropriate backend (e.g., 'nccl'). - Set the current device for the process using
torch.cuda.set_device(rank). - Wrap the model with
DistributedDataParallel. - Wrap the dataset with a
DistributedSamplerto ensure unique data shards per process. - Clean up the process group using
dist.destroy_process_group()after training.
- Initialize the process group using
-
Checkpointing in Distributed Environments
- Check if the current process is rank 0 (
dist.get_rank() == 0). - Only rank 0 saves the model state dict to disk.
- Call
dist.barrier()to ensure all other processes wait until the file is written. - All processes load the checkpoint using
torch.load(..., map_location=...). - Resume training or perform evaluation.
- Check if the current process is rank 0 (
-
Launching with torchrun
- Refactor training code to read
LOCAL_RANKandRANKfrom environment variables. - Remove manual
mp.spawn()logic and usedist.init_process_group(backend='nccl')without rank/world_size args. - Execute the script via
torchrun --nproc_per_node=G script.py. torchrunhandles process spawning, master address setup, and fault recovery.
- Refactor training code to read
Non-Obvious Insights
- Multi-Process vs Multi-Thread: DDP is multi-process, whereas
DataParallelis single-process multi-threaded. DDP is significantly faster because it avoids Python's Global Interpreter Lock (GIL) contention. - Mapping Locations: The
map_locationargument intorch.loadis mandatory in DDP to prevent multiple processes from attempting to load tensors into the same GPU (usually rank 0), which would cause an Out of Memory (OOM) error. - Synchronization Points: In DDP, the constructor, forward pass, and backward pass act as distributed synchronization points where processes communicate gradients.
Evidence
- "GPU devices cannot be shared across DDP processes (i.e. one GPU for one DDP process)." (https://pytorch.org/tutorials/intermediate/ddp_tutorial.html)
- "In DDP, the constructor, the forward pass, and the backward pass are distributed synchronization points." (https://pytorch.org/tutorials/intermediate/ddp_tutorial.html)
Scripts
scripts/pytorch-distributed_tool.py: Boilerplate for atorchrun-compatible DDP script.scripts/pytorch-distributed_tool.js: Node.js wrapper to launchtorchruncommands.
Dependencies
- torch
- nccl (for GPU communication)
- gloo (for CPU-based distributed testing)
References
- PyTorch Distributed Reference
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
prompt-engineering
Comprehensive prompt engineering techniques for Claude models. Use this skill when crafting, optimizing, or debugging prompts for Claude API, Claude Code, or any Claude-powered application. Covers system prompts, role prompting, multishot examples, chain of thought, XML structuring, long context handling, extended thinking, prompt chaining, Claude 4.x-specific best practices, and agentic orchestration including subagents, agent loops, skills, MCP integration, and multi-agent workflows.
adk-rag-agent
Build RAG (Retrieval-Augmented Generation) agents with Google ADK and Vertex AI RAG Engine. Use when implementing document Q&A, knowledge base search, or citation-backed responses. Covers VertexAiRagRetrieval tool, corpus setup, and citation formatting.
headless-cli-agents
Build agentic systems using Claude CLI in headless mode or the Claude Agent SDK. Use when building automation pipelines, CI/CD integrations, multi-agent orchestration, or programmatic Claude interactions. Covers CLI flags (-p, --output-format), session management (--resume, --continue), Python SDK (claude-agent-sdk), custom tools, and agent loop patterns.
notion-knowledge-capture
Capture conversations and decisions into structured Notion pages; use when turning chats/notes into wiki entries, how-tos, decisions, or FAQs with proper linking.
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).
gh-fix-ci
Inspect GitHub PR checks with gh, pull failing GitHub Actions logs, summarize failure context, then create a fix plan and implement after user approval. Use when a user asks to debug or fix failing PR CI/CD checks on GitHub Actions and wants a plan + code changes; for external checks (e.g., Buildkite), only report the details URL and mark them out of scope.
Didn't find tool you were looking for?