pytorch-patterns
यह क्या करती है
यह agent PyTorch library का इस्तेमाल करके AI के लिए कोड लिखने में मदद करता है। यह सुनिश्चित करता है कि आपका कोड व्यवस्थित हो, अलग-अलग computers पर चले (शक्तिशाली graphics card वाले या उसके बिना), और नतीजे दोहराए जा सकें।
कब इस्तेमाल करें
- ✓PyTorch से नया AI model शुरू करते समय।
- ✓AI कोड को सर्वोत्तम तरीकों के अनुसार है या नहीं, यह जाँचने के लिए उसका रिव्यू करते समय।
- ✓जब आपको AI tests के नतीजे हर बार बिल्कुल एक जैसे चाहिए हों।
Skill का लिंक
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/pytorch-patterns/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“मैं कैसे सुनिश्चित करूँ कि मेरा PyTorch कोड CPU और GPU दोनों पर चले और नतीजे हमेशा एक जैसे रहें?”
हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।
यह कैसे काम करती है
यह जाँचता है कि कोड इस बात पर निर्भर है या नहीं कि वह कहाँ चलेगा (CPU या GPU), और क्या नतीजों को दोहराने वाली settings लगी हैं। यह model के हिस्सों और data के dimensions को व्यवस्थित करने में भी मदद करता है।
तकनीकी सामग्री (मूल SKILL.md)
लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।
PyTorch Development Patterns
Idiomatic PyTorch patterns and best practices for building robust, efficient, and reproducible deep learning applications.
When to Activate
- Writing new PyTorch models or training scripts
- Reviewing deep learning code
- Debugging training loops or data pipelines
- Optimizing GPU memory usage or training speed
- Setting up reproducible experiments
Core Principles
1. Device-Agnostic Code
Always write code that works on both CPU and GPU without hardcoding devices.
# Good: Device-agnostic
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = MyModel().to(device)
data = data.to(device)
# Bad: Hardcoded device
model = MyModel().cuda() # Crashes if no GPU
data = data.cuda()
2. Reproducibility First
Set all random seeds for reproducible results.
# Good: Full reproducibility setup
def set_seed(seed: int = 42) -> None:
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Bad: No seed control
model = MyModel() # Different weights every run
3. Explicit Shape Management
Always document and verify tensor shapes.
# Good: Shape-annotated forward pass
def forward(self, x: torch.Tensor) -> torch.Tensor:
# x: (batch_size, channels, height, width)
x = self.conv1(x) # -> (batch_size, 32, H, W)
x = self.pool(x) # -> (batch_size, 32, H//2, W//2)
x = x.view(x.size(0), -1) # -> (batch_size, 32*H//2*W//2)
return self.fc(x) # -> (batch_size, num_classes)
# Bad: No shape tracking
def forward(self, x):
x = self.conv1(x)
x = self.pool(x)
x = x.view(x.size(0), -1) # What size is this?
return self.fc(x) # Will this even work?
Model Architecture Patterns
Clean nn.Module Structure
# Good: Well-organized module
class ImageClassifier(nn.Module):
def __init__(self, num_classes: int, dropout: float = 0.5) -> None:
super().__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
)
self.classifier = nn.Sequential(
nn.Dropout(dropout),
nn.Linear(64 * 16 * 16, num_classes),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.features(x)
x = x.view(x.size(0), -1)
return self.classifier(x)
# Bad: Everything in forward
class ImageClassifier(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
x = F.conv2d(x, weight=self.make_weight()) # Creates weight each call!
return x
Proper Weight Initialization
# Good: Explicit initialization
def _init_weights(self, module: nn.Module) -> None:
if isinstance(module, nn.Linear):
nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu")
if module.bias is not None:
nn.init.zeros_(module.bias)
elif isinstance(module, nn.Conv2d):
nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu")
elif isinstance(module, nn.BatchNorm2d):
nn.init.ones_(module.weight)
nn.init.zeros_(module.bias)
model = MyModel()
model.apply(model._init_weights)
Training Loop Patterns
Standard Training Loop
# Good: Complete training loop with best practices
def train_one_epoch(
model: nn.Module,
dataloader: DataLoader,
optimizer: torch.optim.Optimizer,
criterion: nn.Module,
device: torch.device,
scaler: torch.amp.GradScaler | None = None,
) -> float:
model.train() # Always set train mode
total_loss = 0.0
for batch_idx, (data, target) in enumerate(dataloader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad(set_to_none=True) # More efficient than zero_grad()
# Mixed precision training
with torch.amp.autocast("cuda", enabled=scaler is not None):
output = model(data)
loss = criterion(output, target)
if scaler is not None:
scaler.scale(loss).backward()
scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
scaler.step(optimizer)
scaler.update()
…
अक्सर पूछे जाने वाले सवाल
कोड का CPU और GPU दोनों पर चलना क्यों ज़रूरी है?
इससे आप अलग-अलग computers पर models को train कर सकते हैं, यहाँ तक कि उन computers पर भी जिनमें शक्तिशाली graphics card (GPU) नहीं है। इससे कोड ज़्यादा लचीला बनता है।
'reproducible results' का क्या मतलब है?
इसका मतलब है कि एक ही data के साथ एक ही कोड चलाने पर हर बार बिल्कुल एक जैसे नतीजे मिलें। यह testing और models की तुलना के लिए बहुत ज़रूरी है।
agent कोड को व्यवस्थित करने में कैसे मदद करता है?
यह model के अलग-अलग हिस्सों (जैसे layers और functions) और training प्रक्रिया के लिए व्यवस्था के तरीके सुझाता है, जिससे कोड पढ़ना और संभालना आसान होता है।
जानकारी
लेखक का मूल विवरण
PyTorch deep learning patterns and best practices for building robust, efficient, and reproducible training pipelines, model architectures, and data loading. Use when writing or reviewing PyTorch training loops, model architectures, or data loading, or when a run will not reproduce.
repo: affaan-m/ECC
लोकप्रियता
- GitHub स्टार
- 280k
- रैंकिंग में स्थान
- #230
- लाइसेंस
- MIT
- अपडेट की गई
- 15/10/2026
कमेंट
- …
इस repository की और skills
accessibility
280kयह आपके एजेंट को ऐसे इंटरफ़ेस बनाने में मदद करता है जिन्हें हर कोई इस्तेमाल कर सके। यह पक्का करता है कि वेबसाइट और ऐप दिव्यांग लोगों के लिए भी अच्छे से काम करें, जैसे नेत्रहीन या जिन्हें चलने-फिरने में दिक्कत होती है।
agent-architecture-audit
280kयह आपके एजेंट को जाँचने देता है कि सब कुछ सही से चल रहा है। यह AI सिस्टम में समस्याएँ ढूँढकर ठीक करता है, ताकि वे उम्मीद के मुताबिक काम करें।
agent-eval
280kयह आपके एजेंट को अलग-अलग AI coding tools की तुलना करने में मदद करता है। यह मापता है कि किसी खास काम के लिए कौन सा टूल ज़्यादा तेज़, सस्ता और भरोसेमंद है।
agent-harness-construction
280kयह आपके एजेंट को काम कैसे करना है, यह बेहतर समझने में मदद करता है। यह उन tools और instructions को बेहतर बनाता है जिनका एजेंट इस्तेमाल करता है, ताकि उसकी सफलता की संभावना बढ़े।
मिलती-जुलती skills
accelerate
260kआपके एजेंट को Hugging Face Accelerate से कुछ ही अतिरिक्त लाइनों में PyTorch ट्रेनिंग स्क्रिप्ट को कई GPUs पर चलाने में मदद करती है।
evaluating-llms-harness
260kआपके एजेंट को MMLU, GSM8K और HellaSwag जैसे मानक शैक्षणिक टेस्ट पर भाषा मॉडल की जाँच करने और नतीजों की तुलना करने में मदद करती है।
flash-attention
260kआपके एजेंट को transformer मॉडल का attention चरण तेज़ करने और मेमोरी कम करने में मदद करती है, खासकर लंबे इनपुट पर, ट्रेनिंग या inference में।