claude-code-pilot 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -97
- package/bin/install.js +13 -13
- package/manifest.json +1 -1
- package/package.json +1 -1
- package/src/agents/doc-updater.md +1 -1
- package/src/agents/gan-evaluator.md +209 -0
- package/src/agents/gan-generator.md +131 -0
- package/src/agents/gan-planner.md +99 -0
- package/src/agents/harness-optimizer.md +35 -0
- package/src/agents/loop-operator.md +36 -0
- package/src/agents/opensource-forker.md +198 -0
- package/src/agents/opensource-packager.md +249 -0
- package/src/agents/opensource-sanitizer.md +188 -0
- package/src/agents/performance-optimizer.md +446 -0
- package/src/available-rules/README.md +1 -1
- package/src/commands/{aside.md → ccp/aside.md} +14 -13
- package/src/commands/{build-fix.md → ccp/build-fix.md} +5 -0
- package/src/commands/{checkpoint.md → ccp/checkpoint.md} +12 -7
- package/src/commands/{code-review.md → ccp/code-review.md} +5 -0
- package/src/commands/{context-budget.md → ccp/context-budget.md} +2 -1
- package/src/commands/{cpp-build.md → ccp/cpp-build.md} +6 -5
- package/src/commands/{cpp-review.md → ccp/cpp-review.md} +7 -6
- package/src/commands/{cpp-test.md → ccp/cpp-test.md} +6 -5
- package/src/commands/ccp/docs-update.md +48 -0
- package/src/commands/{docs.md → ccp/docs.md} +4 -3
- package/src/commands/{e2e.md → ccp/e2e.md} +7 -6
- package/src/commands/{eval.md → ccp/eval.md} +10 -5
- package/src/commands/{evolve.md → ccp/evolve.md} +3 -3
- package/src/commands/{go-build.md → ccp/go-build.md} +6 -5
- package/src/commands/{go-review.md → ccp/go-review.md} +7 -6
- package/src/commands/{go-test.md → ccp/go-test.md} +6 -5
- package/src/commands/{gradle-build.md → ccp/gradle-build.md} +1 -0
- package/src/commands/{harness-audit.md → ccp/harness-audit.md} +6 -1
- package/src/commands/{kotlin-build.md → ccp/kotlin-build.md} +6 -5
- package/src/commands/{kotlin-review.md → ccp/kotlin-review.md} +7 -6
- package/src/commands/{kotlin-test.md → ccp/kotlin-test.md} +6 -5
- package/src/commands/{learn.md → ccp/learn.md} +7 -2
- package/src/commands/{model-route.md → ccp/model-route.md} +6 -1
- package/src/commands/{orchestrate.md → ccp/orchestrate.md} +4 -3
- package/src/commands/{plan.md → ccp/plan.md} +6 -5
- package/src/commands/ccp/profile-user.md +46 -0
- package/src/commands/{prompt-optimize.md → ccp/prompt-optimize.md} +3 -2
- package/src/commands/{prune.md → ccp/prune.md} +4 -4
- package/src/commands/{python-review.md → ccp/python-review.md} +7 -6
- package/src/commands/{quality-gate.md → ccp/quality-gate.md} +6 -1
- package/src/commands/{refactor-clean.md → ccp/refactor-clean.md} +5 -0
- package/src/commands/{resume-session.md → ccp/resume-session.md} +9 -8
- package/src/commands/ccp/review.md +37 -0
- package/src/commands/{rules-distill.md → ccp/rules-distill.md} +2 -1
- package/src/commands/{rust-build.md → ccp/rust-build.md} +6 -5
- package/src/commands/{rust-review.md → ccp/rust-review.md} +7 -6
- package/src/commands/{rust-test.md → ccp/rust-test.md} +6 -5
- package/src/commands/{save-session.md → ccp/save-session.md} +2 -1
- package/src/commands/ccp/secure-phase.md +35 -0
- package/src/commands/{sessions.md → ccp/sessions.md} +29 -24
- package/src/commands/{setup-pm.md → ccp/setup-pm.md} +1 -0
- package/src/commands/{setup-refresh.md → ccp/setup-refresh.md} +4 -3
- package/src/commands/{setup.md → ccp/setup.md} +24 -23
- package/src/commands/{skill-create.md → ccp/skill-create.md} +8 -8
- package/src/commands/{skill-health.md → ccp/skill-health.md} +5 -5
- package/src/commands/{tdd.md → ccp/tdd.md} +9 -8
- package/src/commands/{test-coverage.md → ccp/test-coverage.md} +5 -0
- package/src/commands/{tool-guide.md → ccp/tool-guide.md} +2 -1
- package/src/commands/{update-codemaps.md → ccp/update-codemaps.md} +5 -0
- package/src/commands/{update-docs.md → ccp/update-docs.md} +5 -0
- package/src/commands/{verify.md → ccp/verify.md} +5 -0
- package/src/commands/ccp/workstreams.md +68 -0
- package/src/examples/CLAUDE.md +4 -4
- package/src/examples/django-api-CLAUDE.md +5 -5
- package/src/examples/go-microservice-CLAUDE.md +6 -6
- package/src/examples/rust-api-CLAUDE.md +4 -4
- package/src/examples/saas-nextjs-CLAUDE.md +8 -8
- package/src/hooks/session-start.js +1 -1
- package/src/pilot/references/mcp-servers.json +1 -1
- package/src/pilot/workflows/docs-update.md +1165 -0
- package/src/pilot/workflows/help.md +48 -56
- package/src/pilot/workflows/profile-user.md +452 -0
- package/src/pilot/workflows/review.md +244 -0
- package/src/pilot/workflows/secure-phase.md +164 -0
- package/src/rules/common/code-review.md +124 -0
- package/src/rules/zh/README.md +108 -0
- package/src/rules/zh/agents.md +50 -0
- package/src/rules/zh/code-review.md +124 -0
- package/src/rules/zh/coding-style.md +48 -0
- package/src/rules/zh/development-workflow.md +44 -0
- package/src/rules/zh/git-workflow.md +24 -0
- package/src/rules/zh/hooks.md +30 -0
- package/src/rules/zh/patterns.md +31 -0
- package/src/rules/zh/performance.md +55 -0
- package/src/rules/zh/security.md +29 -0
- package/src/rules/zh/testing.md +29 -0
- package/src/skills/autonomous-agent-harness/SKILL.md +267 -0
- package/src/skills/autonomous-loops/SKILL.md +610 -0
- package/src/skills/bun-runtime/SKILL.md +84 -0
- package/src/skills/content-hash-cache-pattern/SKILL.md +161 -0
- package/src/skills/context-budget/SKILL.md +3 -3
- package/src/skills/continuous-learning-v2/SKILL.md +4 -4
- package/src/skills/continuous-learning-v2/agents/observer.md +1 -1
- package/src/skills/cost-aware-llm-pipeline/SKILL.md +183 -0
- package/src/skills/design-system/SKILL.md +82 -0
- package/src/skills/eval-harness/SKILL.md +270 -0
- package/src/skills/flutter-dart-code-review/SKILL.md +435 -0
- package/src/skills/gan-style-harness/SKILL.md +278 -0
- package/src/skills/git-workflow/SKILL.md +715 -0
- package/src/skills/hexagonal-architecture/SKILL.md +276 -0
- package/src/skills/iterative-retrieval/SKILL.md +211 -0
- package/src/skills/laravel-plugin-discovery/SKILL.md +229 -0
- package/src/skills/nextjs-turbopack/SKILL.md +44 -0
- package/src/skills/nuxt4-patterns/SKILL.md +100 -0
- package/src/skills/opensource-pipeline/SKILL.md +255 -0
- package/src/skills/perl-security/SKILL.md +503 -0
- package/src/skills/project-flow-ops/SKILL.md +111 -0
- package/src/skills/project-guidelines-example/SKILL.md +349 -0
- package/src/skills/prompt-optimizer/SKILL.md +38 -38
- package/src/skills/pytorch-patterns/SKILL.md +396 -0
- package/src/skills/regex-vs-llm-structured-text/SKILL.md +220 -0
- package/src/skills/repo-scan/SKILL.md +78 -0
- package/src/skills/rules-distill/SKILL.md +264 -0
- package/src/skills/rules-distill/scripts/scan-rules.sh +58 -0
- package/src/skills/rules-distill/scripts/scan-skills.sh +129 -0
- package/src/skills/swift-concurrency-6-2/SKILL.md +216 -0
- package/src/skills/token-budget-advisor/SKILL.md +133 -0
- package/src/skills/verification-loop/SKILL.md +1 -1
- package/src/skills/workspace-surface-audit/SKILL.md +125 -0
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: pytorch-patterns
|
|
3
|
+
description: PyTorch deep learning patterns and best practices for building robust, efficient, and reproducible training pipelines, model architectures, and data loading.
|
|
4
|
+
origin: ECC
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# PyTorch Development Patterns
|
|
8
|
+
|
|
9
|
+
Idiomatic PyTorch patterns and best practices for building robust, efficient, and reproducible deep learning applications.
|
|
10
|
+
|
|
11
|
+
## When to Activate
|
|
12
|
+
|
|
13
|
+
- Writing new PyTorch models or training scripts
|
|
14
|
+
- Reviewing deep learning code
|
|
15
|
+
- Debugging training loops or data pipelines
|
|
16
|
+
- Optimizing GPU memory usage or training speed
|
|
17
|
+
- Setting up reproducible experiments
|
|
18
|
+
|
|
19
|
+
## Core Principles
|
|
20
|
+
|
|
21
|
+
### 1. Device-Agnostic Code
|
|
22
|
+
|
|
23
|
+
Always write code that works on both CPU and GPU without hardcoding devices.
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
# Good: Device-agnostic
|
|
27
|
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
28
|
+
model = MyModel().to(device)
|
|
29
|
+
data = data.to(device)
|
|
30
|
+
|
|
31
|
+
# Bad: Hardcoded device
|
|
32
|
+
model = MyModel().cuda() # Crashes if no GPU
|
|
33
|
+
data = data.cuda()
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 2. Reproducibility First
|
|
37
|
+
|
|
38
|
+
Set all random seeds for reproducible results.
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
# Good: Full reproducibility setup
|
|
42
|
+
def set_seed(seed: int = 42) -> None:
|
|
43
|
+
torch.manual_seed(seed)
|
|
44
|
+
torch.cuda.manual_seed_all(seed)
|
|
45
|
+
np.random.seed(seed)
|
|
46
|
+
random.seed(seed)
|
|
47
|
+
torch.backends.cudnn.deterministic = True
|
|
48
|
+
torch.backends.cudnn.benchmark = False
|
|
49
|
+
|
|
50
|
+
# Bad: No seed control
|
|
51
|
+
model = MyModel() # Different weights every run
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 3. Explicit Shape Management
|
|
55
|
+
|
|
56
|
+
Always document and verify tensor shapes.
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
# Good: Shape-annotated forward pass
|
|
60
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
61
|
+
# x: (batch_size, channels, height, width)
|
|
62
|
+
x = self.conv1(x) # -> (batch_size, 32, H, W)
|
|
63
|
+
x = self.pool(x) # -> (batch_size, 32, H//2, W//2)
|
|
64
|
+
x = x.view(x.size(0), -1) # -> (batch_size, 32*H//2*W//2)
|
|
65
|
+
return self.fc(x) # -> (batch_size, num_classes)
|
|
66
|
+
|
|
67
|
+
# Bad: No shape tracking
|
|
68
|
+
def forward(self, x):
|
|
69
|
+
x = self.conv1(x)
|
|
70
|
+
x = self.pool(x)
|
|
71
|
+
x = x.view(x.size(0), -1) # What size is this?
|
|
72
|
+
return self.fc(x) # Will this even work?
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Model Architecture Patterns
|
|
76
|
+
|
|
77
|
+
### Clean nn.Module Structure
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
# Good: Well-organized module
|
|
81
|
+
class ImageClassifier(nn.Module):
|
|
82
|
+
def __init__(self, num_classes: int, dropout: float = 0.5) -> None:
|
|
83
|
+
super().__init__()
|
|
84
|
+
self.features = nn.Sequential(
|
|
85
|
+
nn.Conv2d(3, 64, kernel_size=3, padding=1),
|
|
86
|
+
nn.BatchNorm2d(64),
|
|
87
|
+
nn.ReLU(inplace=True),
|
|
88
|
+
nn.MaxPool2d(2),
|
|
89
|
+
)
|
|
90
|
+
self.classifier = nn.Sequential(
|
|
91
|
+
nn.Dropout(dropout),
|
|
92
|
+
nn.Linear(64 * 16 * 16, num_classes),
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
96
|
+
x = self.features(x)
|
|
97
|
+
x = x.view(x.size(0), -1)
|
|
98
|
+
return self.classifier(x)
|
|
99
|
+
|
|
100
|
+
# Bad: Everything in forward
|
|
101
|
+
class ImageClassifier(nn.Module):
|
|
102
|
+
def __init__(self):
|
|
103
|
+
super().__init__()
|
|
104
|
+
|
|
105
|
+
def forward(self, x):
|
|
106
|
+
x = F.conv2d(x, weight=self.make_weight()) # Creates weight each call!
|
|
107
|
+
return x
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Proper Weight Initialization
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
# Good: Explicit initialization
|
|
114
|
+
def _init_weights(self, module: nn.Module) -> None:
|
|
115
|
+
if isinstance(module, nn.Linear):
|
|
116
|
+
nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu")
|
|
117
|
+
if module.bias is not None:
|
|
118
|
+
nn.init.zeros_(module.bias)
|
|
119
|
+
elif isinstance(module, nn.Conv2d):
|
|
120
|
+
nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu")
|
|
121
|
+
elif isinstance(module, nn.BatchNorm2d):
|
|
122
|
+
nn.init.ones_(module.weight)
|
|
123
|
+
nn.init.zeros_(module.bias)
|
|
124
|
+
|
|
125
|
+
model = MyModel()
|
|
126
|
+
model.apply(model._init_weights)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Training Loop Patterns
|
|
130
|
+
|
|
131
|
+
### Standard Training Loop
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
# Good: Complete training loop with best practices
|
|
135
|
+
def train_one_epoch(
|
|
136
|
+
model: nn.Module,
|
|
137
|
+
dataloader: DataLoader,
|
|
138
|
+
optimizer: torch.optim.Optimizer,
|
|
139
|
+
criterion: nn.Module,
|
|
140
|
+
device: torch.device,
|
|
141
|
+
scaler: torch.amp.GradScaler | None = None,
|
|
142
|
+
) -> float:
|
|
143
|
+
model.train() # Always set train mode
|
|
144
|
+
total_loss = 0.0
|
|
145
|
+
|
|
146
|
+
for batch_idx, (data, target) in enumerate(dataloader):
|
|
147
|
+
data, target = data.to(device), target.to(device)
|
|
148
|
+
|
|
149
|
+
optimizer.zero_grad(set_to_none=True) # More efficient than zero_grad()
|
|
150
|
+
|
|
151
|
+
# Mixed precision training
|
|
152
|
+
with torch.amp.autocast("cuda", enabled=scaler is not None):
|
|
153
|
+
output = model(data)
|
|
154
|
+
loss = criterion(output, target)
|
|
155
|
+
|
|
156
|
+
if scaler is not None:
|
|
157
|
+
scaler.scale(loss).backward()
|
|
158
|
+
scaler.unscale_(optimizer)
|
|
159
|
+
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
|
|
160
|
+
scaler.step(optimizer)
|
|
161
|
+
scaler.update()
|
|
162
|
+
else:
|
|
163
|
+
loss.backward()
|
|
164
|
+
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
|
|
165
|
+
optimizer.step()
|
|
166
|
+
|
|
167
|
+
total_loss += loss.item()
|
|
168
|
+
|
|
169
|
+
return total_loss / len(dataloader)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Validation Loop
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
# Good: Proper evaluation
|
|
176
|
+
@torch.no_grad() # More efficient than wrapping in torch.no_grad() block
|
|
177
|
+
def evaluate(
|
|
178
|
+
model: nn.Module,
|
|
179
|
+
dataloader: DataLoader,
|
|
180
|
+
criterion: nn.Module,
|
|
181
|
+
device: torch.device,
|
|
182
|
+
) -> tuple[float, float]:
|
|
183
|
+
model.eval() # Always set eval mode — disables dropout, uses running BN stats
|
|
184
|
+
total_loss = 0.0
|
|
185
|
+
correct = 0
|
|
186
|
+
total = 0
|
|
187
|
+
|
|
188
|
+
for data, target in dataloader:
|
|
189
|
+
data, target = data.to(device), target.to(device)
|
|
190
|
+
output = model(data)
|
|
191
|
+
total_loss += criterion(output, target).item()
|
|
192
|
+
correct += (output.argmax(1) == target).sum().item()
|
|
193
|
+
total += target.size(0)
|
|
194
|
+
|
|
195
|
+
return total_loss / len(dataloader), correct / total
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Data Pipeline Patterns
|
|
199
|
+
|
|
200
|
+
### Custom Dataset
|
|
201
|
+
|
|
202
|
+
```python
|
|
203
|
+
# Good: Clean Dataset with type hints
|
|
204
|
+
class ImageDataset(Dataset):
|
|
205
|
+
def __init__(
|
|
206
|
+
self,
|
|
207
|
+
image_dir: str,
|
|
208
|
+
labels: dict[str, int],
|
|
209
|
+
transform: transforms.Compose | None = None,
|
|
210
|
+
) -> None:
|
|
211
|
+
self.image_paths = list(Path(image_dir).glob("*.jpg"))
|
|
212
|
+
self.labels = labels
|
|
213
|
+
self.transform = transform
|
|
214
|
+
|
|
215
|
+
def __len__(self) -> int:
|
|
216
|
+
return len(self.image_paths)
|
|
217
|
+
|
|
218
|
+
def __getitem__(self, idx: int) -> tuple[torch.Tensor, int]:
|
|
219
|
+
img = Image.open(self.image_paths[idx]).convert("RGB")
|
|
220
|
+
label = self.labels[self.image_paths[idx].stem]
|
|
221
|
+
|
|
222
|
+
if self.transform:
|
|
223
|
+
img = self.transform(img)
|
|
224
|
+
|
|
225
|
+
return img, label
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Efficient DataLoader Configuration
|
|
229
|
+
|
|
230
|
+
```python
|
|
231
|
+
# Good: Optimized DataLoader
|
|
232
|
+
dataloader = DataLoader(
|
|
233
|
+
dataset,
|
|
234
|
+
batch_size=32,
|
|
235
|
+
shuffle=True, # Shuffle for training
|
|
236
|
+
num_workers=4, # Parallel data loading
|
|
237
|
+
pin_memory=True, # Faster CPU->GPU transfer
|
|
238
|
+
persistent_workers=True, # Keep workers alive between epochs
|
|
239
|
+
drop_last=True, # Consistent batch sizes for BatchNorm
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
# Bad: Slow defaults
|
|
243
|
+
dataloader = DataLoader(dataset, batch_size=32) # num_workers=0, no pin_memory
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Custom Collate for Variable-Length Data
|
|
247
|
+
|
|
248
|
+
```python
|
|
249
|
+
# Good: Pad sequences in collate_fn
|
|
250
|
+
def collate_fn(batch: list[tuple[torch.Tensor, int]]) -> tuple[torch.Tensor, torch.Tensor]:
|
|
251
|
+
sequences, labels = zip(*batch)
|
|
252
|
+
# Pad to max length in batch
|
|
253
|
+
padded = nn.utils.rnn.pad_sequence(sequences, batch_first=True, padding_value=0)
|
|
254
|
+
return padded, torch.tensor(labels)
|
|
255
|
+
|
|
256
|
+
dataloader = DataLoader(dataset, batch_size=32, collate_fn=collate_fn)
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## Checkpointing Patterns
|
|
260
|
+
|
|
261
|
+
### Save and Load Checkpoints
|
|
262
|
+
|
|
263
|
+
```python
|
|
264
|
+
# Good: Complete checkpoint with all training state
|
|
265
|
+
def save_checkpoint(
|
|
266
|
+
model: nn.Module,
|
|
267
|
+
optimizer: torch.optim.Optimizer,
|
|
268
|
+
epoch: int,
|
|
269
|
+
loss: float,
|
|
270
|
+
path: str,
|
|
271
|
+
) -> None:
|
|
272
|
+
torch.save({
|
|
273
|
+
"epoch": epoch,
|
|
274
|
+
"model_state_dict": model.state_dict(),
|
|
275
|
+
"optimizer_state_dict": optimizer.state_dict(),
|
|
276
|
+
"loss": loss,
|
|
277
|
+
}, path)
|
|
278
|
+
|
|
279
|
+
def load_checkpoint(
|
|
280
|
+
path: str,
|
|
281
|
+
model: nn.Module,
|
|
282
|
+
optimizer: torch.optim.Optimizer | None = None,
|
|
283
|
+
) -> dict:
|
|
284
|
+
checkpoint = torch.load(path, map_location="cpu", weights_only=True)
|
|
285
|
+
model.load_state_dict(checkpoint["model_state_dict"])
|
|
286
|
+
if optimizer:
|
|
287
|
+
optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
|
|
288
|
+
return checkpoint
|
|
289
|
+
|
|
290
|
+
# Bad: Only saving model weights (can't resume training)
|
|
291
|
+
torch.save(model.state_dict(), "model.pt")
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## Performance Optimization
|
|
295
|
+
|
|
296
|
+
### Mixed Precision Training
|
|
297
|
+
|
|
298
|
+
```python
|
|
299
|
+
# Good: AMP with GradScaler
|
|
300
|
+
scaler = torch.amp.GradScaler("cuda")
|
|
301
|
+
for data, target in dataloader:
|
|
302
|
+
with torch.amp.autocast("cuda"):
|
|
303
|
+
output = model(data)
|
|
304
|
+
loss = criterion(output, target)
|
|
305
|
+
scaler.scale(loss).backward()
|
|
306
|
+
scaler.step(optimizer)
|
|
307
|
+
scaler.update()
|
|
308
|
+
optimizer.zero_grad(set_to_none=True)
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### Gradient Checkpointing for Large Models
|
|
312
|
+
|
|
313
|
+
```python
|
|
314
|
+
# Good: Trade compute for memory
|
|
315
|
+
from torch.utils.checkpoint import checkpoint
|
|
316
|
+
|
|
317
|
+
class LargeModel(nn.Module):
|
|
318
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
319
|
+
# Recompute activations during backward to save memory
|
|
320
|
+
x = checkpoint(self.block1, x, use_reentrant=False)
|
|
321
|
+
x = checkpoint(self.block2, x, use_reentrant=False)
|
|
322
|
+
return self.head(x)
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### torch.compile for Speed
|
|
326
|
+
|
|
327
|
+
```python
|
|
328
|
+
# Good: Compile the model for faster execution (PyTorch 2.0+)
|
|
329
|
+
model = MyModel().to(device)
|
|
330
|
+
model = torch.compile(model, mode="reduce-overhead")
|
|
331
|
+
|
|
332
|
+
# Modes: "default" (safe), "reduce-overhead" (faster), "max-autotune" (fastest)
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
## Quick Reference: PyTorch Idioms
|
|
336
|
+
|
|
337
|
+
| Idiom | Description |
|
|
338
|
+
|-------|-------------|
|
|
339
|
+
| `model.train()` / `model.eval()` | Always set mode before train/eval |
|
|
340
|
+
| `torch.no_grad()` | Disable gradients for inference |
|
|
341
|
+
| `optimizer.zero_grad(set_to_none=True)` | More efficient gradient clearing |
|
|
342
|
+
| `.to(device)` | Device-agnostic tensor/model placement |
|
|
343
|
+
| `torch.amp.autocast` | Mixed precision for 2x speed |
|
|
344
|
+
| `pin_memory=True` | Faster CPU→GPU data transfer |
|
|
345
|
+
| `torch.compile` | JIT compilation for speed (2.0+) |
|
|
346
|
+
| `weights_only=True` | Secure model loading |
|
|
347
|
+
| `torch.manual_seed` | Reproducible experiments |
|
|
348
|
+
| `gradient_checkpointing` | Trade compute for memory |
|
|
349
|
+
|
|
350
|
+
## Anti-Patterns to Avoid
|
|
351
|
+
|
|
352
|
+
```python
|
|
353
|
+
# Bad: Forgetting model.eval() during validation
|
|
354
|
+
model.train()
|
|
355
|
+
with torch.no_grad():
|
|
356
|
+
output = model(val_data) # Dropout still active! BatchNorm uses batch stats!
|
|
357
|
+
|
|
358
|
+
# Good: Always set eval mode
|
|
359
|
+
model.eval()
|
|
360
|
+
with torch.no_grad():
|
|
361
|
+
output = model(val_data)
|
|
362
|
+
|
|
363
|
+
# Bad: In-place operations breaking autograd
|
|
364
|
+
x = F.relu(x, inplace=True) # Can break gradient computation
|
|
365
|
+
x += residual # In-place add breaks autograd graph
|
|
366
|
+
|
|
367
|
+
# Good: Out-of-place operations
|
|
368
|
+
x = F.relu(x)
|
|
369
|
+
x = x + residual
|
|
370
|
+
|
|
371
|
+
# Bad: Moving data to GPU inside the training loop repeatedly
|
|
372
|
+
for data, target in dataloader:
|
|
373
|
+
model = model.cuda() # Moves model EVERY iteration!
|
|
374
|
+
|
|
375
|
+
# Good: Move model once before the loop
|
|
376
|
+
model = model.to(device)
|
|
377
|
+
for data, target in dataloader:
|
|
378
|
+
data, target = data.to(device), target.to(device)
|
|
379
|
+
|
|
380
|
+
# Bad: Using .item() before backward
|
|
381
|
+
loss = criterion(output, target).item() # Detaches from graph!
|
|
382
|
+
loss.backward() # Error: can't backprop through .item()
|
|
383
|
+
|
|
384
|
+
# Good: Call .item() only for logging
|
|
385
|
+
loss = criterion(output, target)
|
|
386
|
+
loss.backward()
|
|
387
|
+
print(f"Loss: {loss.item():.4f}") # .item() after backward is fine
|
|
388
|
+
|
|
389
|
+
# Bad: Not using torch.save properly
|
|
390
|
+
torch.save(model, "model.pt") # Saves entire model (fragile, not portable)
|
|
391
|
+
|
|
392
|
+
# Good: Save state_dict
|
|
393
|
+
torch.save(model.state_dict(), "model.pt")
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
__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()`.
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: regex-vs-llm-structured-text
|
|
3
|
+
description: Decision framework for choosing between regex and LLM when parsing structured text — start with regex, add LLM only for low-confidence edge cases.
|
|
4
|
+
origin: ECC
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Regex vs LLM for Structured Text Parsing
|
|
8
|
+
|
|
9
|
+
A practical decision framework for parsing structured text (quizzes, forms, invoices, documents). The key insight: regex handles 95-98% of cases cheaply and deterministically. Reserve expensive LLM calls for the remaining edge cases.
|
|
10
|
+
|
|
11
|
+
## When to Activate
|
|
12
|
+
|
|
13
|
+
- Parsing structured text with repeating patterns (questions, forms, tables)
|
|
14
|
+
- Deciding between regex and LLM for text extraction
|
|
15
|
+
- Building hybrid pipelines that combine both approaches
|
|
16
|
+
- Optimizing cost/accuracy tradeoffs in text processing
|
|
17
|
+
|
|
18
|
+
## Decision Framework
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
Is the text format consistent and repeating?
|
|
22
|
+
├── Yes (>90% follows a pattern) → Start with Regex
|
|
23
|
+
│ ├── Regex handles 95%+ → Done, no LLM needed
|
|
24
|
+
│ └── Regex handles <95% → Add LLM for edge cases only
|
|
25
|
+
└── No (free-form, highly variable) → Use LLM directly
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Architecture Pattern
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
Source Text
|
|
32
|
+
│
|
|
33
|
+
▼
|
|
34
|
+
[Regex Parser] ─── Extracts structure (95-98% accuracy)
|
|
35
|
+
│
|
|
36
|
+
▼
|
|
37
|
+
[Text Cleaner] ─── Removes noise (markers, page numbers, artifacts)
|
|
38
|
+
│
|
|
39
|
+
▼
|
|
40
|
+
[Confidence Scorer] ─── Flags low-confidence extractions
|
|
41
|
+
│
|
|
42
|
+
├── High confidence (≥0.95) → Direct output
|
|
43
|
+
│
|
|
44
|
+
└── Low confidence (<0.95) → [LLM Validator] → Output
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Implementation
|
|
48
|
+
|
|
49
|
+
### 1. Regex Parser (Handles the Majority)
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
import re
|
|
53
|
+
from dataclasses import dataclass
|
|
54
|
+
|
|
55
|
+
@dataclass(frozen=True)
|
|
56
|
+
class ParsedItem:
|
|
57
|
+
id: str
|
|
58
|
+
text: str
|
|
59
|
+
choices: tuple[str, ...]
|
|
60
|
+
answer: str
|
|
61
|
+
confidence: float = 1.0
|
|
62
|
+
|
|
63
|
+
def parse_structured_text(content: str) -> list[ParsedItem]:
|
|
64
|
+
"""Parse structured text using regex patterns."""
|
|
65
|
+
pattern = re.compile(
|
|
66
|
+
r"(?P<id>\d+)\.\s*(?P<text>.+?)\n"
|
|
67
|
+
r"(?P<choices>(?:[A-D]\..+?\n)+)"
|
|
68
|
+
r"Answer:\s*(?P<answer>[A-D])",
|
|
69
|
+
re.MULTILINE | re.DOTALL,
|
|
70
|
+
)
|
|
71
|
+
items = []
|
|
72
|
+
for match in pattern.finditer(content):
|
|
73
|
+
choices = tuple(
|
|
74
|
+
c.strip() for c in re.findall(r"[A-D]\.\s*(.+)", match.group("choices"))
|
|
75
|
+
)
|
|
76
|
+
items.append(ParsedItem(
|
|
77
|
+
id=match.group("id"),
|
|
78
|
+
text=match.group("text").strip(),
|
|
79
|
+
choices=choices,
|
|
80
|
+
answer=match.group("answer"),
|
|
81
|
+
))
|
|
82
|
+
return items
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 2. Confidence Scoring
|
|
86
|
+
|
|
87
|
+
Flag items that may need LLM review:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
@dataclass(frozen=True)
|
|
91
|
+
class ConfidenceFlag:
|
|
92
|
+
item_id: str
|
|
93
|
+
score: float
|
|
94
|
+
reasons: tuple[str, ...]
|
|
95
|
+
|
|
96
|
+
def score_confidence(item: ParsedItem) -> ConfidenceFlag:
|
|
97
|
+
"""Score extraction confidence and flag issues."""
|
|
98
|
+
reasons = []
|
|
99
|
+
score = 1.0
|
|
100
|
+
|
|
101
|
+
if len(item.choices) < 3:
|
|
102
|
+
reasons.append("few_choices")
|
|
103
|
+
score -= 0.3
|
|
104
|
+
|
|
105
|
+
if not item.answer:
|
|
106
|
+
reasons.append("missing_answer")
|
|
107
|
+
score -= 0.5
|
|
108
|
+
|
|
109
|
+
if len(item.text) < 10:
|
|
110
|
+
reasons.append("short_text")
|
|
111
|
+
score -= 0.2
|
|
112
|
+
|
|
113
|
+
return ConfidenceFlag(
|
|
114
|
+
item_id=item.id,
|
|
115
|
+
score=max(0.0, score),
|
|
116
|
+
reasons=tuple(reasons),
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
def identify_low_confidence(
|
|
120
|
+
items: list[ParsedItem],
|
|
121
|
+
threshold: float = 0.95,
|
|
122
|
+
) -> list[ConfidenceFlag]:
|
|
123
|
+
"""Return items below confidence threshold."""
|
|
124
|
+
flags = [score_confidence(item) for item in items]
|
|
125
|
+
return [f for f in flags if f.score < threshold]
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### 3. LLM Validator (Edge Cases Only)
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
def validate_with_llm(
|
|
132
|
+
item: ParsedItem,
|
|
133
|
+
original_text: str,
|
|
134
|
+
client,
|
|
135
|
+
) -> ParsedItem:
|
|
136
|
+
"""Use LLM to fix low-confidence extractions."""
|
|
137
|
+
response = client.messages.create(
|
|
138
|
+
model="claude-haiku-4-5-20251001", # Cheapest model for validation
|
|
139
|
+
max_tokens=500,
|
|
140
|
+
messages=[{
|
|
141
|
+
"role": "user",
|
|
142
|
+
"content": (
|
|
143
|
+
f"Extract the question, choices, and answer from this text.\n\n"
|
|
144
|
+
f"Text: {original_text}\n\n"
|
|
145
|
+
f"Current extraction: {item}\n\n"
|
|
146
|
+
f"Return corrected JSON if needed, or 'CORRECT' if accurate."
|
|
147
|
+
),
|
|
148
|
+
}],
|
|
149
|
+
)
|
|
150
|
+
# Parse LLM response and return corrected item...
|
|
151
|
+
return corrected_item
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### 4. Hybrid Pipeline
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
def process_document(
|
|
158
|
+
content: str,
|
|
159
|
+
*,
|
|
160
|
+
llm_client=None,
|
|
161
|
+
confidence_threshold: float = 0.95,
|
|
162
|
+
) -> list[ParsedItem]:
|
|
163
|
+
"""Full pipeline: regex -> confidence check -> LLM for edge cases."""
|
|
164
|
+
# Step 1: Regex extraction (handles 95-98%)
|
|
165
|
+
items = parse_structured_text(content)
|
|
166
|
+
|
|
167
|
+
# Step 2: Confidence scoring
|
|
168
|
+
low_confidence = identify_low_confidence(items, confidence_threshold)
|
|
169
|
+
|
|
170
|
+
if not low_confidence or llm_client is None:
|
|
171
|
+
return items
|
|
172
|
+
|
|
173
|
+
# Step 3: LLM validation (only for flagged items)
|
|
174
|
+
low_conf_ids = {f.item_id for f in low_confidence}
|
|
175
|
+
result = []
|
|
176
|
+
for item in items:
|
|
177
|
+
if item.id in low_conf_ids:
|
|
178
|
+
result.append(validate_with_llm(item, content, llm_client))
|
|
179
|
+
else:
|
|
180
|
+
result.append(item)
|
|
181
|
+
|
|
182
|
+
return result
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Real-World Metrics
|
|
186
|
+
|
|
187
|
+
From a production quiz parsing pipeline (410 items):
|
|
188
|
+
|
|
189
|
+
| Metric | Value |
|
|
190
|
+
|--------|-------|
|
|
191
|
+
| Regex success rate | 98.0% |
|
|
192
|
+
| Low confidence items | 8 (2.0%) |
|
|
193
|
+
| LLM calls needed | ~5 |
|
|
194
|
+
| Cost savings vs all-LLM | ~95% |
|
|
195
|
+
| Test coverage | 93% |
|
|
196
|
+
|
|
197
|
+
## Best Practices
|
|
198
|
+
|
|
199
|
+
- **Start with regex** — even imperfect regex gives you a baseline to improve
|
|
200
|
+
- **Use confidence scoring** to programmatically identify what needs LLM help
|
|
201
|
+
- **Use the cheapest LLM** for validation (Haiku-class models are sufficient)
|
|
202
|
+
- **Never mutate** parsed items — return new instances from cleaning/validation steps
|
|
203
|
+
- **TDD works well** for parsers — write tests for known patterns first, then edge cases
|
|
204
|
+
- **Log metrics** (regex success rate, LLM call count) to track pipeline health
|
|
205
|
+
|
|
206
|
+
## Anti-Patterns to Avoid
|
|
207
|
+
|
|
208
|
+
- Sending all text to an LLM when regex handles 95%+ of cases (expensive and slow)
|
|
209
|
+
- Using regex for free-form, highly variable text (LLM is better here)
|
|
210
|
+
- Skipping confidence scoring and hoping regex "just works"
|
|
211
|
+
- Mutating parsed objects during cleaning/validation steps
|
|
212
|
+
- Not testing edge cases (malformed input, missing fields, encoding issues)
|
|
213
|
+
|
|
214
|
+
## When to Use
|
|
215
|
+
|
|
216
|
+
- Quiz/exam question parsing
|
|
217
|
+
- Form data extraction
|
|
218
|
+
- Invoice/receipt processing
|
|
219
|
+
- Document structure parsing (headers, sections, tables)
|
|
220
|
+
- Any structured text with repeating patterns where cost matters
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: repo-scan
|
|
3
|
+
description: Cross-stack source code asset audit — classifies every file, detects embedded third-party libraries, and delivers actionable four-level verdicts per module with interactive HTML reports.
|
|
4
|
+
origin: community
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# repo-scan
|
|
8
|
+
|
|
9
|
+
> Every ecosystem has its own dependency manager, but no tool looks across C++, Android, iOS, and Web to tell you: how much code is actually yours, what's third-party, and what's dead weight.
|
|
10
|
+
|
|
11
|
+
## When to Use
|
|
12
|
+
|
|
13
|
+
- Taking over a large legacy codebase and need a structural overview
|
|
14
|
+
- Before major refactoring — identify what's core, what's duplicate, what's dead
|
|
15
|
+
- Auditing third-party dependencies embedded directly in source (not declared in package managers)
|
|
16
|
+
- Preparing architecture decision records for monorepo reorganization
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
# Fetch only the pinned commit for reproducibility
|
|
22
|
+
mkdir -p ~/.claude/skills/repo-scan
|
|
23
|
+
git init repo-scan
|
|
24
|
+
cd repo-scan
|
|
25
|
+
git remote add origin https://github.com/haibindev/repo-scan.git
|
|
26
|
+
git fetch --depth 1 origin 2742664
|
|
27
|
+
git checkout --detach FETCH_HEAD
|
|
28
|
+
cp -r . ~/.claude/skills/repo-scan
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
> Review the source before installing any agent skill.
|
|
32
|
+
|
|
33
|
+
## Core Capabilities
|
|
34
|
+
|
|
35
|
+
| Capability | Description |
|
|
36
|
+
|---|---|
|
|
37
|
+
| **Cross-stack scanning** | C/C++, Java/Android, iOS (OC/Swift), Web (TS/JS/Vue) in one pass |
|
|
38
|
+
| **File classification** | Every file tagged as project code, third-party, or build artifact |
|
|
39
|
+
| **Library detection** | 50+ known libraries (FFmpeg, Boost, OpenSSL…) with version extraction |
|
|
40
|
+
| **Four-level verdicts** | Core Asset / Extract & Merge / Rebuild / Deprecate |
|
|
41
|
+
| **HTML reports** | Interactive dark-theme pages with drill-down navigation |
|
|
42
|
+
| **Monorepo support** | Hierarchical scanning with summary + sub-project reports |
|
|
43
|
+
|
|
44
|
+
## Analysis Depth Levels
|
|
45
|
+
|
|
46
|
+
| Level | Files Read | Use Case |
|
|
47
|
+
|---|---|---|
|
|
48
|
+
| `fast` | 1-2 per module | Quick inventory of huge directories |
|
|
49
|
+
| `standard` | 2-5 per module | Default audit with full dependency + architecture checks |
|
|
50
|
+
| `deep` | 5-10 per module | Adds thread safety, memory management, API consistency |
|
|
51
|
+
| `full` | All files | Pre-merge comprehensive review |
|
|
52
|
+
|
|
53
|
+
## How It Works
|
|
54
|
+
|
|
55
|
+
1. **Classify the repo surface**: enumerate files, then tag each as project code, embedded third-party code, or build artifact.
|
|
56
|
+
2. **Detect embedded libraries**: inspect directory names, headers, license files, and version markers to identify bundled dependencies and likely versions.
|
|
57
|
+
3. **Score each module**: group files by module or subsystem, then assign one of the four verdicts based on ownership, duplication, and maintenance cost.
|
|
58
|
+
4. **Highlight structural risks**: call out dead-weight artifacts, duplicated wrappers, outdated vendored code, and modules that should be extracted, rebuilt, or deprecated.
|
|
59
|
+
5. **Produce the report**: return a concise summary plus the interactive HTML output with per-module drill-down so the audit can be reviewed asynchronously.
|
|
60
|
+
|
|
61
|
+
## Examples
|
|
62
|
+
|
|
63
|
+
On a 50,000-file C++ monorepo:
|
|
64
|
+
- Found FFmpeg 2.x (2015 vintage) still in production
|
|
65
|
+
- Discovered the same SDK wrapper duplicated 3 times
|
|
66
|
+
- Identified 636 MB of committed Debug/ipch/obj build artifacts
|
|
67
|
+
- Classified: 3 MB project code vs 596 MB third-party
|
|
68
|
+
|
|
69
|
+
## Best Practices
|
|
70
|
+
|
|
71
|
+
- Start with `standard` depth for first-time audits
|
|
72
|
+
- Use `fast` for monorepos with 100+ modules to get a quick inventory
|
|
73
|
+
- Run `deep` incrementally on modules flagged for refactoring
|
|
74
|
+
- Review the cross-module analysis for duplicate detection across sub-projects
|
|
75
|
+
|
|
76
|
+
## Links
|
|
77
|
+
|
|
78
|
+
- [GitHub Repository](https://github.com/haibindev/repo-scan)
|