Install any skill in seconds. Free to start, no credit card required.
Get Started Free →PyTorch deep learning patterns and best practices for building robust, efficient, and reproducible training pipelines, model architectures, and data loading.
.claude/skills/loulanyue-pytorch-patterns/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 118% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 114% | 0% |
| case-13 | ✗→✓ | ▲ Improved | 89% | 0% |
| case-15 | ✓→✓ | = Same ✓ | 103% | 0% |
| case-16 | ✓→✓ | = Same ✓ | 146% | 0% |
Idiomatic PyTorch patterns and best practices for building robust, efficient, and reproducible deep learning applications.
Always write code that works on both CPU and GPU without hardcoding devices.
python# 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()
Set all random seeds for reproducible results.
python# 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
Always document and verify tensor shapes.
python# 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?
python# 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
python# 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)
python# 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() else: loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) optimizer.step() total_loss += loss.item() return total_loss / len(dataloader)
python# Good: Proper evaluation @torch.no_grad() # More efficient than wrapping in torch.no_grad() block def evaluate( model: nn.Module, dataloader: DataLoader, criterion: nn.Module, device: torch.device, ) -> tuple[float, float]: model.eval() # Always set eval mode — disables dropout, uses running BN stats total_loss = 0.0 correct = 0 total = 0 for data, target in dataloader: data, target = data.to(device), target.to(device) output = model(data) total_loss += criterion(output, target).item() correct += (output.argmax(1) == target).sum().item() total += target.size(0) return total_loss / len(dataloader), correct / total
python# Good: Clean Dataset with type hints class ImageDataset(Dataset): def __init__( self, image_dir: str, labels: dict[str, int], transform: transforms.Compose | None = None, ) -> None: self.image_paths = list(Path(image_dir).glob("*.jpg")) self.labels = labels self.transform = transform def __len__(self) -> int: return len(self.image_paths) def __getitem__(self, idx: int) -> tuple[torch.Tensor, int]: img = Image.open(self.image_paths[idx]).convert("RGB") label = self.labels[self.image_paths[idx].stem] if self.transform: img = self.transform(img) return img, label
python# Good: Optimized DataLoader dataloader = DataLoader( dataset, batch_size=32, shuffle=True, # Shuffle for training num_workers=4, # Parallel data loading pin_memory=True, # Faster CPU->GPU transfer persistent_workers=True, # Keep workers alive between epochs drop_last=True, # Consistent batch sizes for BatchNorm ) # Bad: Slow defaults dataloader = DataLoader(dataset, batch_size=32) # num_workers=0, no pin_memory
python# Good: Pad sequences in collate_fn def collate_fn(batch: list[tuple[torch.Tensor, int]]) -> tuple[torch.Tensor, torch.Tensor]: sequences, labels = zip(*batch) # Pad to max length in batch padded = nn.utils.rnn.pad_sequence(sequences, batch_first=True, padding_value=0) return padded, torch.tensor(labels) dataloader = DataLoader(dataset, batch_size=32, collate_fn=collate_fn)
python# Good: Complete checkpoint with all training state def save_checkpoint( model: nn.Module, optimizer: torch.optim.Optimizer, epoch: int, loss: float, path: str, ) -> None: torch.save({ "epoch": epoch, "model_state_dict": model.state_dict(), "optimizer_state_dict": optimizer.state_dict(), "loss": loss, }, path) def load_checkpoint( path: str, model: nn.Module, optimizer: torch.optim.Optimizer | None = None, ) -> dict: checkpoint = torch.load(path, map_location="cpu", weights_only=True) model.load_state_dict(checkpoint["model_state_dict"]) if optimizer: optimizer.load_state_dict(checkpoint["optimizer_state_dict"]) return checkpoint # Bad: Only saving model weights (can't resume training) torch.save(model.state_dict(), "model.pt")
python# Good: AMP with GradScaler scaler = torch.amp.GradScaler("cuda") for data, target in dataloader: with torch.amp.autocast("cuda"): output = model(data) loss = criterion(output, target) scaler.scale(loss).backward() scaler.step(optimizer) scaler.update() optimizer.zero_grad(set_to_none=True)
python# Good: Trade compute for memory from torch.utils.checkpoint import checkpoint class LargeModel(nn.Module): def forward(self, x: torch.Tensor) -> torch.Tensor: # Recompute activations during backward to save memory x = checkpoint(self.block1, x, use_reentrant=False) x = checkpoint(self.block2, x, use_reentrant=False) return self.head(x)
python# Good: Compile the model for faster execution (PyTorch 2.0+) model = MyModel().to(device) model = torch.compile(model, mode="reduce-overhead") # Modes: "default" (safe), "reduce-overhead" (faster), "max-autotune" (fastest)
| Idiom | Description | |-------|-------------| | model.train() / model.eval() | Always set mode before train/eval | | torch.no_grad() | Disable gradients for inference | | optimizer.zero_grad(set_to_none=True) | More efficient gradient clearing | | .to(device) | Device-agnostic tensor/model placement | | torch.amp.autocast | Mixed precision for 2x speed | | pin_memory=True | Faster CPU→GPU data transfer | | torch.compile | JIT compilation for speed (2.0+) | | weights_only=True | Secure model loading | | torch.manual_seed | Reproducible experiments | | gradient_checkpointing | Trade compute for memory |
python# Bad: Forgetting model.eval() during validation model.train() with torch.no_grad(): output = model(val_data) # Dropout still active! BatchNorm uses batch stats! # Good: Always set eval mode model.eval() with torch.no_grad(): output = model(val_data) # Bad: In-place operations breaking autograd x = F.relu(x, inplace=True) # Can break gradient computation x += residual # In-place add breaks autograd graph # Good: Out-of-place operations x = F.relu(x) x = x + residual # Bad: Moving data to GPU inside the training loop repeatedly for data, target in dataloader: model = model.cuda() # Moves model EVERY iteration! # Good: Move model once before the loop model = model.to(device) for data, target in dataloader: data, target = data.to(device), target.to(device) # Bad: Using .item() before backward loss = criterion(output, target).item() # Detaches from graph! loss.backward() # Error: can't backprop through .item() # Good: Call .item() only for logging loss = criterion(output, target) loss.backward() print(f"Loss: {loss.item():.4f}") # .item() after backward is fine # Bad: Not using torch.save properly torch.save(model, "model.pt") # Saves entire model (fragile, not portable) # Good: Save state_dict torch.save(model.state_dict(), "model.pt")
__Remember__: PyTorch code should be device-agnostic, reproducible, and memory-conscious. When in doubt, profile with torch.profiler and check GPU memory with torch.cuda.memory_summary().
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-15 | pass→pass | 16,502 | 14,721 | -11% | 1 | 1 | 0% | 3,021 | 6,124 | +103% | 0 | 0 | — |
case-16 | pass→pass | 11,229 | 9,507 | -15% | 1 | 1 | 0% | 2,095 | 5,146 | +146% | 0 | 0 | — |
case-01 | fail→pass | 12,061 | 9,677 | -20% | 1 | 1 | 0% | 2,426 | 5,279 | +118% | 0 | 0 | — |
case-02 | pass→pass | 11,434 | 10,277 | -10% | 1 | 1 | 0% | 2,341 | 5,466 | +133% | 0 | 0 | — |
case-03 | fail→pass | 15,855 | 16,014 | +1% | 1 | 1 | 0% | 3,184 | 6,803 | +114% | 0 | 0 | — |
case-04 | pass→pass | 12,829 | 11,556 | -10% | 1 | 1 | 0% | 2,275 | 5,427 | +139% | 0 | 0 | — |
case-05 | pass→pass | 15,861 | 14,079 | -11% | 1 | 1 | 0% | 2,731 | 6,103 | +123% | 0 | 0 | — |
case-06 | pass→pass | 13,606 | 9,976 | -27% | 1 | 1 | 0% | 2,370 | 5,137 | +117% | 0 | 0 | — |
case-07 | fail→fail | 4,597 | 3,467 | -25% | 1 | 1 | 0% | 730 | 3,956 | +442% | 0 | 0 | — |
case-08 | pass→pass | 4,403 | 3,908 | -11% | 1 | 1 | 0% | 694 | 4,018 | +479% | 0 | 0 | — |
case-09 | pass→pass | 13,154 | 12,194 | -7% | 1 | 1 | 0% | 2,281 | 5,615 | +146% | 0 | 0 | — |
case-10 | pass→pass | 13,837 | 11,161 | -19% | 1 | 1 | 0% | 2,579 | 5,464 | +112% | 0 | 0 | — |
case-11 | pass→pass | 12,726 | 8,380 | -34% | 1 | 1 | 0% | 2,455 | 4,964 | +102% | 0 | 0 | — |
case-12 | pass→pass | 14,321 | 10,517 | -27% | 1 | 1 | 0% | 2,787 | 5,455 | +96% | 0 | 0 | — |
case-13 | fail→pass | 17,973 | 12,250 | -32% | 1 | 1 | 0% | 3,083 | 5,836 | +89% | 0 | 0 | — |
case-14 | pass→pass | 6,900 | 5,759 | -17% | 1 | 1 | 0% | 1,269 | 4,407 | +247% | 0 | 0 | — |
case-17 | pass→pass | 10,943 | 10,186 | -7% | 1 | 1 | 0% | 1,854 | 5,085 | +174% | 0 | 0 | — |
case-18 | pass→pass | 7,401 | 5,681 | -23% | 1 | 1 | 0% | 1,202 | 4,364 | +263% | 0 | 0 | — |
case-19 | pass→pass | 15,130 | 9,090 | -40% | 1 | 1 | 0% | 2,874 | 5,055 | +76% | 0 | 0 | — |
case-20 | pass→pass | 6,929 | 7,023 | +1% | 1 | 1 | 0% | 1,283 | 4,765 | +271% | 0 | 0 | — |
case-21 | pass→pass | 11,391 | 7,156 | -37% | 1 | 1 | 0% | 2,182 | 4,837 | +122% | 0 | 0 | — |
case-22 | pass→pass | 10,434 | 7,708 | -26% | 1 | 1 | 0% | 2,115 | 4,944 | +134% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +14 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.