pytorch-fsdp2
यह क्या करती है
यह स्किल बताती है कि PyTorch के FSDP2 शार्डिंग को ट्रेनिंग स्क्रिप्ट में कैसे जोड़ें, ताकि एक GPU के लिए बहुत बड़ा मॉडल कई GPU पर ट्रेन हो सके। यह बड़े मॉडल पर काम करने वाले मशीन लर्निंग इंजीनियरों के लिए है।
कब इस्तेमाल करें
- ✓जब मॉडल एक GPU की मेमोरी में नहीं समाता।
- ✓जब आप ट्रेनिंग को कई GPU पर बाँटना चाहते हैं, ऐसी शार्डिंग के साथ जिसे जाँचना आसान हो।
- ✓जब आपको ऐसे चेकपॉइंट चाहिए जो बँटे हुए मॉडल के साथ काम करें।
Skill का लिंक
https://github.com/Orchestra-Research/AI-Research-SKILLs/blob/main/08-distributed-training/pytorch-fsdp2/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“क्या आप मेरी ट्रेनिंग स्क्रिप्ट में FSDP2 जोड़ने में मदद कर सकते हैं, ताकि मेरा बड़ा मॉडल दो GPU पर ट्रेन हो सके?”
हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।
यह कैसे काम करती है
एजेंट डिस्ट्रिब्यूटेड एनवायरनमेंट सेट करता है, मॉडल पर शार्डिंग लागू करता है, और जहाँ ज़रूरत हो वहाँ mixed precision, offloading और चेकपॉइंट सेटिंग्स जोड़ता है।
तकनीकी सामग्री (मूल SKILL.md)
लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।
Skill: Use PyTorch FSDP2 (fully_shard) correctly in a training script
This skill teaches a coding agent how to add PyTorch FSDP2 to a training loop with correct initialization, sharding, mixed precision/offload configuration, and checkpointing.
FSDP2 in PyTorch is exposed primarily via
torch.distributed.fsdp.fully_shardand theFSDPModulemethods it adds in-place to modules. See:references/pytorch_fully_shard_api.md,references/pytorch_fsdp2_tutorial.md.
When to use this skill
Use FSDP2 when:
- Your model doesn’t fit on one GPU (parameters + gradients + optimizer state).
- You want an eager-mode sharding approach that is DTensor-based per-parameter sharding (more inspectable, simpler sharded state dicts) than FSDP1.
- You may later compose DP with Tensor Parallel using DeviceMesh.
Avoid (or be careful) if:
- You need strict backwards-compatible checkpoints across PyTorch versions (DCP warns against this).
- You’re forced onto older PyTorch versions without the FSDP2 stack.
Alternatives (when FSDP2 is not the best fit)
- DistributedDataParallel (DDP): Use the standard data-parallel wrapper when you want classic distributed data parallel training.
- FullyShardedDataParallel (FSDP1): Use the original FSDP wrapper for parameter sharding across data-parallel workers.
Reference: references/pytorch_ddp_notes.md, references/pytorch_fsdp1_api.md.
Contract the agent must follow
- Launch with
torchrunand set the CUDA device per process (usually viaLOCAL_RANK). - Apply
fully_shard()bottom-up, i.e., shard submodules (e.g., Transformer blocks) before the root module. - Call
model(input), notmodel.forward(input), so the FSDP2 hooks run (unless you explicitlyunshard()or register the forward method). - Create the optimizer after sharding and make sure it is built on the DTensor parameters (post-
fully_shard). - Checkpoint using Distributed Checkpoint (DCP) or the distributed-state-dict helpers, not naïve
torch.save(model.state_dict())unless you deliberately gather to full tensors.
(Each of these rules is directly described in the official API docs/tutorial; see references.)
Step-by-step procedure
0) Version & environment sanity
- Prefer a recent stable PyTorch where the docs show FSDP2 and DCP updated recently.
- Use
torchrun --nproc_per_node <gpus_per_node> ...and ensureRANK,WORLD_SIZE,LOCAL_RANKare visible.
Reference: references/pytorch_fsdp2_tutorial.md (launch commands and setup), references/pytorch_fully_shard_api.md (user contract).
1) Initialize distributed and set device
Minimal, correct pattern:
dist.init_process_group(backend="nccl")torch.cuda.set_device(int(os.environ["LOCAL_RANK"]))- Optionally create a
DeviceMeshto describe the data-parallel group(s)
Reference: references/pytorch_device_mesh_tutorial.md (why DeviceMesh exists & how it manages process groups).
2) Build model on meta device (recommended for very large models)
For big models, initialize on meta, apply sharding, then materialize weights on GPU:
with torch.device("meta"): model = ...- apply
fully_shard(...)on submodules, thenfully_shard(model) model.to_empty(device="cuda")model.reset_parameters()(or your init routine)
Reference: references/pytorch_fsdp2_tutorial.md (migration guide shows this flow explicitly).
3) Apply fully_shard() bottom-up (wrapping policy = “apply where needed”)
Do not only call fully_shard on the topmost module.
Recommended sharding pattern for transformer-like models:
- iterate modules,
if isinstance(m, TransformerBlock): fully_shard(m, ...) - then
fully_shard(model, ...)
Why:
fully_shardforms “parameter groups” for collective efficiency and excludes params already grouped by earlier calls. Bottom-up gives better overlap and lower peak memory.
Reference: references/pytorch_fully_shard_api.md (bottom-up requirement and why).
4) Configure reshard_after_forward for memory/perf trade-offs
Default behavior:
NonemeansTruefor non-root modules andFalsefor root modules (good default).
Heuristics:
- If you’re memory-bound: keep defaults or force
Trueon many blocks. - If you’re throughput-bound and can afford memory: consider keeping unsharded params longer (root often
False). - Advanced: use an
intto reshard to a smaller mesh after forward (e.g., intra-n
अक्सर पूछे जाने वाले सवाल
शार्डिंग का क्या मतलब है?
इसका मतलब है मॉडल के weights, gradients और optimizer state को कई GPU में बाँटना, ताकि हर GPU में उसका सिर्फ़ एक हिस्सा रहे।
FSDP2 कब इस्तेमाल न करें?
जब आपको कई PyTorch संस्करणों में काम करने वाले चेकपॉइंट चाहिए, या जब आप FSDP2 स्टैक के बिना पुराने PyTorch संस्करण पर अटके हों, तब स्किल इससे चेताती है।
विकल्प क्या हैं?
DDP के साथ मानक डेटा पैरेलल ट्रेनिंग, या सरल सेटअप के लिए मूल FSDP।
जानकारी
लेखक का मूल विवरण
Adds PyTorch FSDP2 (fully_shard) to training scripts with correct init, sharding, mixed precision/offload config, and distributed checkpointing. Use when models exceed single-GPU memory or when you need DTensor-based sharding with DeviceMesh.
लोकप्रियता
- GitHub स्टार
- 14k
- रैंकिंग में स्थान
- #11161
- लाइसेंस
- MIT
- अपडेट की गई
- 10/10/2026
कमेंट
- …
इस repository की और skills
autoresearch
14kयह आपके एजेंट को साहित्य समीक्षा से प्रयोग और लेखन तक पूरे AI रिसर्च प्रोजेक्ट में मार्गदर्शन देती है, परीक्षण और समीक्षा के दोहराने वाले चक्र के साथ।
prompt-guard
14kयह आपके एजेंट को Meta के छोटे classifier से टेक्स्ट को AI ऐप तक पहुँचने से पहले prompt injection और jailbreak कोशिशों के लिए जाँचने देती है।
ml-training-recipes
14kयह आपके एजेंट को न्यूरल नेटवर्क के लिए परखे हुए ट्रेनिंग तरीके देती है, जिनमें मॉडल चुनाव, optimizers, learning rates और loss spikes ठीक करना शामिल है।
experiment-tracking-swanlab
14kयह आपके एजेंट को SwanLab सेट करने में मदद करती है, ताकि मशीन लर्निंग प्रयोग स्थानीय रूप से या अपने सर्वर पर रिकॉर्ड, तुलना और चार्ट हो सकें।
मिलती-जुलती skills
pytorch-patterns
280kआपके एजेंट को machine learning models के लिए PyTorch इस्तेमाल करने में मदद करता है। कुशल model training के best practices लागू करता है और नतीजों को दोहराने लायक रखता है।
evm-token-decimals
280kयह आपके एजेंट को अलग-अलग blockchain networks पर decimal numbers की गलतियों से बचने में मदद करता है। यह transactions और calculations में token values सही रखने का ध्यान रखता है।
ito-inference
280kआपके एजेंट को AI models की उपलब्धता जाँचने देता है। यह compute request को तैयार model service से जोड़ता है।