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)

Stars 0
Forks 0

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

  1. Does your model fit on one GPU?
    • YES: Use DistributedDataParallel (DDP).
    • NO: Use Fully Sharded Data Parallel (FSDP).
  2. Are you launching the job?
    • USE: torchrun to handle environmental setup and fault recovery.
  3. Are you saving a checkpoint?
    • DO: Only save on rank == 0 to avoid file corruption and redundant I/O.

Workflows

  1. Setting Up a DDP Training Job

    1. Initialize the process group using dist.init_process_group() with appropriate backend (e.g., 'nccl').
    2. Set the current device for the process using torch.cuda.set_device(rank).
    3. Wrap the model with DistributedDataParallel.
    4. Wrap the dataset with a DistributedSampler to ensure unique data shards per process.
    5. Clean up the process group using dist.destroy_process_group() after training.
  2. Checkpointing in Distributed Environments

    1. Check if the current process is rank 0 (dist.get_rank() == 0).
    2. Only rank 0 saves the model state dict to disk.
    3. Call dist.barrier() to ensure all other processes wait until the file is written.
    4. All processes load the checkpoint using torch.load(..., map_location=...).
    5. Resume training or perform evaluation.
  3. Launching with torchrun

    1. Refactor training code to read LOCAL_RANK and RANK from environment variables.
    2. Remove manual mp.spawn() logic and use dist.init_process_group(backend='nccl') without rank/world_size args.
    3. Execute the script via torchrun --nproc_per_node=G script.py.
    4. torchrun handles process spawning, master address setup, and fault recovery.

Non-Obvious Insights

  • Multi-Process vs Multi-Thread: DDP is multi-process, whereas DataParallel is single-process multi-threaded. DDP is significantly faster because it avoids Python's Global Interpreter Lock (GIL) contention.
  • Mapping Locations: The map_location argument in torch.load is 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

Scripts

  • scripts/pytorch-distributed_tool.py: Boilerplate for a torchrun-compatible DDP script.
  • scripts/pytorch-distributed_tool.js: Node.js wrapper to launch torchrun commands.

Dependencies

  • torch
  • nccl (for GPU communication)
  • gloo (for CPU-based distributed testing)

References

  • PyTorch Distributed Reference

Expand your agent's capabilities with these related and highly-rated skills.

cuba6112/skillfactory

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.

0 0
Explore
cuba6112/skillfactory

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.

0 0
Explore
cuba6112/skillfactory

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.

0 0
Explore
cuba6112/skillfactory

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.

0 0
Explore
cuba6112/skillfactory

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

0 0
Explore
cuba6112/skillfactory

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.

0 0
Explore

Didn't find tool you were looking for?

Be as detailed as possible for better results