Configures Claude Code hooks and Codex hooks.json/notify. Use when adding PreToolUse guards, Stop hooks, managed hooks, format-on-save, preflight, audits, or worktree/budget hooks.
日本語の概要は準備中です。原文の説明を表示しています。
Builds a GPT and BPE tokenizer from scratch. Use when implementing autograd, attention, a nanoGPT loop, muP hyperparameter transfer, WSD annealing, loss spikes, or mid-training.
インストールする前に、エージェントに与えられる指示の中身を確認できます。
Domain: building a transformer/GPT and a BPE tokenizer from first principles — the from-first-principles training-layer competency. Does NOT cover applications-layer fine-tuning, RLHF, or inference optimization; those belong to sibling skills.
Canonical teachers: Karpathy "Neural Networks: Zero to Hero" (micrograd → makemore → "Let's build GPT" → "Let's build the GPT Tokenizer" → "Let's reproduce GPT-2"), Karpathy nanochat (full-stack from-scratch successor to nanoGPT), Raschka "Build a Large Language Model From Scratch", nanoGPT, minbpe, "Attention Is All You Need".
GPT-2 is the pedagogical spine here — the right thing to build first. The modern from-scratch baseline then swaps four components onto that spine (RoPE, RMSNorm, SwiGLU, GQA) and runs attention through FlashAttention/SDPA; see Modern Architecture Deltas.
Activate when the user asks about:
nn.MultiheadAttention using the same weights, mask, dropout setting and a numerical tolerance.torch.autocast('cuda', dtype=torch.bfloat16) when supported, gradient accumulation, cosine or WSD LR, checkpoint (weights and data position). Verify on your model with python3 scripts/check_loop.py --model yourmodule:build (a zero-argument factory returning a fresh model): it checks step-0 loss ≈ ln(V), that accumulated micro-batch gradients equal the large-batch gradient, and that changing future tokens never changes earlier logits. Without --model it only runs a built-in demo. Then take the throughput wins: torch.set_float32_matmul_precision('high') for TF32, pad vocab_size 50257 → 50304, and track MFU rather than tokens/sec. See Pretraining Loop; parameter counts, FLOPs per token and run times for GPT-2 124M are in its reference card.wpe, RMSNorm for LayerNorm, SwiGLU for the GELU-MLP, GQA, and F.scaled_dot_product_attention; optionally train with Muon. See Modern Architecture Deltas.Build GPT-2 first to understand the mechanics, then apply the deltas — the pre-norm residual skeleton is unchanged; you swap sublayers, not the architecture.
| GPT-2 (2019) | Modern baseline | Why |
|---|---|---|
Learned absolute pos embed (wpe) | RoPE (rotary, in attention) | Relative position without a learned position table; test quality beyond the trained context |
| LayerNorm | RMSNorm | Cheaper, no centering/bias, stable at depth |
| GELU-MLP (4×) | SwiGLU (~8/3×) | Gated FFN improves quality per param |
| MHA (KV heads = query heads) | GQA (fewer KV heads) | Shrinks KV cache for inference |
| Hand-rolled softmax attention | F.scaled_dot_product_attention | PyTorch selects an eligible fused kernel on supported CUDA inputs; check the selected backend before claiming its memory or speed gains |
| AdamW for all params | Muon (2D matrices) + AdamW (embed/head/norms) | Newton-Schulz orthogonalized updates; per-step speedup that shrinks with scale (~2× reported at Moonlight scale, ~1.1× at 1.2B in a controlled benchmark) |
| No q/k normalization | QK-Norm (RMSNorm on q/k before attention) | Bounds attention-logit growth — a stability default in new dense/MoE recipes, not just a speedrun trick (cf. Kimi K2's MuonClip QK-Clip) |
Frontier reference: the modded-nanoGPT speedrun stacks Muon, QK-Norm, ReLU², logit softcap, and embedding-skip connections to reach GPT-2-grade FineWeb validation loss far faster than the original nanoGPT on 8×H100. For the full from-scratch pipeline (tokenizer → pretrain → SFT → RL → serve), Karpathy's nanochat is the successor to nanoGPT. Lookup step: both records move; read the repo README for the current target loss, hardware and time before quoting one, and quote it with the date you read it.
| Component | Key Detail | Common Mistake |
|---|---|---|
| Autograd | Value.backward() accumulates += into .grad, not = | Forgetting to zero grads before .backward() |
| Embedding | nn.Embedding(vocab_size, n_embd) — random init, learned | Confusing token embed with positional embed shape |
| Causal mask | torch.tril(torch.ones(T,T)) before softmax; fill -inf or torch.finfo(dtype).min, not 0 | Using 0 fill — attention leaks future tokens |
| Attention math | softmax(QK^T / sqrt(d_k)) * V | Forgetting /sqrt(d_k) — variance explodes |
| LayerNorm placement | Pre-norm (before attention/FFN) in GPT-2; original paper was post-norm | Post-norm makes deep stacks hard to train |
| FFN expansion | 4x hidden dim, GELU activation | Using ReLU — slight quality difference, matters at scale |
| Weight tying | LM head shares the embedding matrix (head.weight = wte.weight) | Forgetting tying adds a second V×d matrix: +38.6M params (+31%) at GPT-2 124M, not 2× |
| Init scaling | std=0.02 for most; residual projections: std=0.02/sqrt(2*n_layer) | Flat 0.02 everywhere — residual stream variance grows |
| Gradient accumulation | accumulate N micro-batches, divide loss by N, step once | Forgetting to divide loss — effective LR N× too large |
| bf16 autocast | torch.autocast('cuda', dtype=torch.bfloat16) | Using fp16 without loss scaling — NaN on older GPUs |
| BPE merges | greedy highest-frequency pair; merge in-place, repeat | Not updating pair counts after each merge — wrong vocab |
| LR schedule | cosine: linear warmup (GPT-3 used ≈375M tokens; nanoGPT's 715 steps × 524,288 tokens matches it), then cosine decay to ~10% of peak. WSD (trapezoidal) when the token budget is not fixed up front | Skipping warmup — loss spike at start; stating warmup as a % of the run, which changes meaning with run length |
| Temperature | logits / temperature before softmax; T<1 sharpens (more deterministic), T>1 flattens (more random) | Applying temperature after softmax — has no effect on the distribution |
| Top-k sampling | zero out all logits except the top-k before softmax; draw from the remaining distribution | Top-k=1 is greedy decoding; top-k=vocab_size is pure sampling |
| KV-cache | at inference, cache K and V tensors for all past positions; on each new token only compute Q/K/V for the single new position and append to cache | Re-computing all K/V at each generation step; the cache removes the redundant projection work (O(T²) → O(T) for K/V), not the attention itself — scoring is still O(T) per step, so total generation stays O(T²) |
Prove the tokenizer, data loader, masking, loss, optimizer order, checkpoint restore, and sample generation on a tiny run before reserving large compute. Then run a fixed-budget pilot that records effective tokens, loss by source slice, gradient and activation health, throughput, utilization, and restart equivalence. Scale only when the loss curve and downstream probes improve as expected, the input pipeline is not the bottleneck, and a costed stop rule is written. Successful allocation or falling training loss alone does not justify the next scale.
Zero-grad placement: call optimizer.zero_grad() before the forward pass (or set_to_none=True for speed), not after .step().
Post-norm vs pre-norm: original "Attention Is All You Need" uses post-norm; GPT-2 and nanoGPT use pre-norm. Pre-norm trains more stably at depth.
Causal mask fill value: use float('-inf') or torch.finfo(dtype).min, not a hard-coded -1e9. In fp32, -1e9 does not leak (future weights are exactly 0), but in fp16 masked_fill(-1e9) raises an overflow error. Guard fully masked rows (padding): with -inf they turn into NaN, with finfo.min into a uniform row.
Gradient accumulation scaling: divide the loss by the accumulation steps inside the micro-batch loop, not outside.
Weight tying in state_dict: when saving checkpoints, the LM head weight is the same tensor as the embedding weight — loading requires care to avoid double-counting params.
BPE encode-decode round-trip: bytes, not characters — always encode text as UTF-8 bytes first before running BPE.
DataLoader seeding: fix random seeds for reproducibility across runs; DataLoader worker seeds need explicit worker_init_fn.
torch.compile interaction: torch.compile + gradient checkpointing can conflict in some PyTorch versions — test before enabling both. A compiled model also prefixes state_dict keys with _orig_mod., which breaks checkpoint loading into an uncompiled model.
DDP gradient sync in the micro-loop: require_backward_grad_sync is reset to True by DDP on every forward, so it must be re-assigned per micro-step (or use model.no_sync()). Setting it once outside the loop either all-reduces every micro-step or never syncs at all — both silent.
Resuming without the data position: restoring weights and optimizer but restarting the loader re-trains on seen shards with no error.
Verify PyTorch API details (autocast dtype names, torch.compile flags, DataLoader args) against current PyTorch docs before recommending.
Verify current nanoGPT and minbpe repo states (file structure, hyperparameters) against the GitHub repos — they are actively maintained.
attn_weights.sum(dim=-1) is all-ones (no causal leak check).torch.nn. equivalent before stacking.ln(V) loss; check this at step 0.betas=(0.9, 0.999) — GPT-3 used betas=(0.9, 0.95) for stability at scale, and nanoGPT follows it (the GPT-2 paper does not report betas).nn.MultiheadAttention using matched weights, masks and dropout, within a numerical tolerance.ln(vocab_size) by >10%, stop and debug — don't train through bad initialization.MonarchLinear and BlockLowRankLinear sketches; when it beats or composes with pruning/quantizationscripts/check_loop.py --model module:factory — runs three checks on your model: step-0 loss ≈ ln(V), accumulated micro-batch gradient ≡ large-batch gradient, and no future leak (earlier logits unchanged when later tokens change). The factory takes no arguments and returns a fresh nn.Module whose forward returns logits or (logits, ...). Without --model it runs a built-in demo that says nothing about your code. Requires PyTorch (CPU is fine). Exit 0 pass, 1 a check failed, 2 usage/import error. Tests: python3 scripts/test_check_loop.py.When prior decisions or pitfalls are relevant, consult learnings.consolidated.md if present; use learnings.md only for needed history or as the available fallback. Otherwise skip both.
After applying it, if you encountered a pattern worth remembering, a mistake worth preventing, or a domain fact that surprised you, append one dated bullet to learnings.md via agents-skills-feedback-loop/scripts/append_learning.py. Do not modify SKILL.md itself.
まだレビューはありません。使ってみた感想をお寄せください。
概要と使いどころ
Configures Claude Code hooks and Codex hooks.json/notify. Use when adding PreToolUse guards, Stop hooks, managed hooks, format-on-save, preflight, audits, or worktree/budget hooks.
日本語の概要は準備中です。原文の説明を表示しています。
Configures and hardens Claude Code and Codex MCP servers. Use when connecting databases, APIs, SaaS, building servers, or serving a clearance-filtered knowledge base.
日本語の概要は準備中です。原文の説明を表示しています。
Owns instruction files: AGENTS.md, CLAUDE.md, personal and repo rules. Use when writing, pruning, auditing them, sharing rules across Claude and Codex, or fixing ignored rules.
日本語の概要は準備中です。原文の説明を表示しています。
Creates and audits agent skills: SKILL.md, references, scripts, runtime metadata. Use when writing, validating, or security-reviewing a skill, or fixing truncated skill listings.
日本語の概要は準備中です。原文の説明を表示しています。
Adds per-skill learnings loops for dated patterns, mistakes, and domain facts. Use when wiring skill memory, consolidation, or drift audits.
日本語の概要は準備中です。原文の説明を表示しています。
Chooses subagent, team, workflow, or debate and launches it on Claude Code or Codex. Use when delegating, running agent review boards, or installing shared agents.
日本語の概要は準備中です。原文の説明を表示しています。