Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Guide to Transformer architectures for NLP and computer vision
.claude/skills/brycewang-stanford-transformer-architecture-guide/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-17 | ✗→✓ | ▲ Improved | 160% | 0% |
| case-07 | ✓→✗ | ▼ Worse | 88% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 560% | 0% |
| case-04 | ✓→✓ | = Same ✓ | 167% | 0% |
| case-05 | ✓→✓ | = Same ✓ | 141% | 0% |
Understand, implement, and adapt Transformer architectures for NLP, computer vision, and multimodal research, from the original attention mechanism to modern variants.
The Transformer (Vaswani et al., 2017, "Attention Is All You Need") replaced recurrence and convolution with self-attention as the primary sequence modeling mechanism.
| Component | Function | Key Parameters | |-----------|----------|---------------| | Multi-Head Self-Attention | Computes attention weights across all positions | d_model, n_heads, d_k, d_v | | Feed-Forward Network | Position-wise nonlinear transformation | d_model, d_ff | | Positional Encoding | Injects sequence order information | Sinusoidal or learned | | Layer Normalization | Stabilizes training | Pre-norm or post-norm | | Residual Connections | Enables gradient flow in deep networks | Add before or after norm |
pythonimport torch import torch.nn as nn import torch.nn.functional as F import math class MultiHeadAttention(nn.Module): def __init__(self, d_model=512, n_heads=8): super().__init__() self.d_model = d_model self.n_heads = n_heads self.d_k = d_model // n_heads self.W_q = nn.Linear(d_model, d_model) self.W_k = nn.Linear(d_model, d_model) self.W_v = nn.Linear(d_model, d_model) self.W_o = nn.Linear(d_model, d_model) def forward(self, Q, K, V, mask=None): batch_size = Q.size(0) # Linear projections and reshape for multi-head Q = self.W_q(Q).view(batch_size, -1, self.n_heads, self.d_k).transpose(1, 2) K = self.W_k(K).view(batch_size, -1, self.n_heads, self.d_k).transpose(1, 2) V = self.W_v(V).view(batch_size, -1, self.n_heads, self.d_k).transpose(1, 2) # Scaled dot-product attention scores = torch.matmul(Q, K.transpose(-2, -1)) / math.sqrt(self.d_k) if mask is not None: scores = scores.masked_fill(mask == 0, -1e9) attn_weights = F.softmax(scores, dim=-1) context = torch.matmul(attn_weights, V) # Concatenate heads and project context = context.transpose(1, 2).contiguous().view(batch_size, -1, self.d_model) return self.W_o(context)
pythonclass TransformerBlock(nn.Module): def __init__(self, d_model=512, n_heads=8, d_ff=2048, dropout=0.1): super().__init__() self.attention = MultiHeadAttention(d_model, n_heads) self.norm1 = nn.LayerNorm(d_model) self.norm2 = nn.LayerNorm(d_model) self.ffn = nn.Sequential( nn.Linear(d_model, d_ff), nn.GELU(), nn.Dropout(dropout), nn.Linear(d_ff, d_model), nn.Dropout(dropout) ) self.dropout = nn.Dropout(dropout) def forward(self, x, mask=None): # Pre-norm architecture (GPT-style) attn_out = self.attention(self.norm1(x), self.norm1(x), self.norm1(x), mask) x = x + self.dropout(attn_out) ffn_out = self.ffn(self.norm2(x)) x = x + ffn_out return x
| Architecture | Type | Key Innovation | Representative Model | |-------------|------|---------------|---------------------| | Encoder-only | Bidirectional | Masked language modeling | BERT, RoBERTa | | Decoder-only | Autoregressive | Causal language modeling | GPT, LLaMA, Claude | | Encoder-Decoder | Seq2seq | Cross-attention between encoder and decoder | T5, BART, mBART |
python# BERT-style masked language modeling from transformers import BertTokenizer, BertForMaskedLM tokenizer = BertTokenizer.from_pretrained("bert-base-uncased") model = BertForMaskedLM.from_pretrained("bert-base-uncased") text = "The Transformer architecture has [MASK] natural language processing." inputs = tokenizer(text, return_tensors="pt") outputs = model(**inputs) # Get predictions for [MASK] mask_idx = (inputs.input_ids == tokenizer.mask_token_id).nonzero(as_tuple=True)[1] logits = outputs.logits[0, mask_idx] top_tokens = logits.topk(5).indices[0] print([tokenizer.decode(t) for t in top_tokens])
python# GPT-style autoregressive generation from transformers import GPT2LMHeadModel, GPT2Tokenizer tokenizer = GPT2Tokenizer.from_pretrained("gpt2") model = GPT2LMHeadModel.from_pretrained("gpt2") prompt = "The key innovation of the Transformer is" inputs = tokenizer(prompt, return_tensors="pt") outputs = model.generate( **inputs, max_new_tokens=50, temperature=0.7, top_p=0.9, do_sample=True ) print(tokenizer.decode(outputs[0], skip_special_tokens=True))
The Vision Transformer (Dosovitskiy et al., 2021) applies the Transformer to image classification:
pythonclass VisionTransformer(nn.Module): def __init__(self, img_size=224, patch_size=16, in_channels=3, d_model=768, n_heads=12, n_layers=12, n_classes=1000): super().__init__() self.patch_size = patch_size n_patches = (img_size // patch_size) ** 2 # Patch embedding: split image into patches and project self.patch_embed = nn.Conv2d(in_channels, d_model, kernel_size=patch_size, stride=patch_size) # Learnable [CLS] token and position embeddings self.cls_token = nn.Parameter(torch.zeros(1, 1, d_model)) self.pos_embed = nn.Parameter(torch.zeros(1, n_patches + 1, d_model)) # Transformer blocks self.blocks = nn.ModuleList([ TransformerBlock(d_model, n_heads) for _ in range(n_layers) ]) self.norm = nn.LayerNorm(d_model) self.head = nn.Linear(d_model, n_classes) def forward(self, x): B = x.size(0) # Patchify and flatten x = self.patch_embed(x).flatten(2).transpose(1, 2) # (B, n_patches, d_model) # Prepend CLS token cls = self.cls_token.expand(B, -1, -1) x = torch.cat([cls, x], dim=1) x = x + self.pos_embed # Transformer blocks for block in self.blocks: x = block(x) # Classification from CLS token x = self.norm(x[:, 0]) return self.head(x)
| Method | Complexity | Key Idea | Reference | |--------|-----------|----------|-----------| | Standard attention | O(n^2) | Full pairwise attention | Vaswani et al., 2017 | | Linear attention | O(n) | Kernel approximation of softmax | Katharopoulos et al., 2020 | | Flash Attention | O(n^2) time, O(n) memory | IO-aware tiled computation | Dao et al., 2022 | | Sparse attention | O(n sqrt(n)) | Fixed or learned sparse patterns | Child et al., 2019 | | Sliding window | O(n w) | Local attention window | Beltagy et al., 2020 (Longformer) | | Multi-query attention | O(n^2) but faster | Shared K/V across heads | Shazeer, 2019 | | Grouped-query attention | O(n^2) but faster | Groups of heads share K/V | Ainslie et al., 2023 |
Kaplan et al. (2020) and Hoffmann et al. (2022, "Chinchilla") established scaling laws:
Performance (loss) scales as a power law with:
- Model parameters (N): L ~ N^(-0.076)
- Dataset size (D): L ~ D^(-0.095)
- Compute budget (C): L ~ C^(-0.050)
Chinchilla optimal scaling:
- For compute budget C, allocate equally to model size and data
- Optimal tokens ~ 20 * parameters
- Example: 70B parameter model needs ~1.4T training tokens| Resource | Description | |----------|-------------| | Hugging Face Transformers | Pre-trained models and fine-tuning framework | | Papers With Code | Benchmarks, SOTA tracking, and code links | | The Illustrated Transformer (Jay Alammar) | Visual explanations of attention | | Andrej Karpathy's nanoGPT | Minimal GPT implementation for education | | EleutherAI | Open-source LLM research community | | MLCommons | Standardized ML benchmarks (MLPerf) |
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→fail | 30,328 | 27,298 | -10% | 1 | 1 | 0% | 7,024 | 7,821 | +11% | 0 | 0 | — |
case-02 | fail→fail | 23,672 | 17,398 | -27% | 1 | 1 | 0% | 3,973 | 6,295 | +58% | 0 | 0 | — |
case-03 | pass→pass | 3,308 | 3,065 | -7% | 1 | 1 | 0% | 456 | 3,010 | +560% | 0 | 0 | — |
case-04 | pass→pass | 7,016 | 4,337 | -38% | 1 | 1 | 0% | 1,189 | 3,180 | +167% | 0 | 0 | — |
case-05 | pass→pass | 9,549 | 10,603 | +11% | 1 | 1 | 0% | 1,715 | 4,130 | +141% | 0 | 0 | — |
case-06 | pass→pass | 3,217 | 3,199 | -1% | 1 | 1 | 0% | 543 | 2,984 | +450% | 0 | 0 | — |
case-07 | pass→fail | 13,381 | 12,800 | -4% | 1 | 1 | 0% | 2,426 | 4,551 | +88% | 0 | 0 | — |
case-08 | fail→fail | 10,924 | 10,387 | -5% | 1 | 1 | 0% | 2,003 | 4,313 | +115% | 0 | 0 | — |
case-09 | pass→pass | 5,902 | 4,527 | -23% | 1 | 1 | 0% | 992 | 3,267 | +229% | 0 | 0 | — |
case-10 | pass→pass | 7,210 | 7,812 | +8% | 1 | 1 | 0% | 1,216 | 3,788 | +212% | 0 | 0 | — |
case-11 | pass→pass | 17,141 | 20,487 | +20% | 1 | 1 | 0% | 3,114 | 6,186 | +99% | 0 | 0 | — |
case-12 | pass→pass | 8,498 | 4,179 | -51% | 1 | 1 | 0% | 1,161 | 3,125 | +169% | 0 | 0 | — |
case-13 | pass→pass | 7,882 | 5,673 | -28% | 1 | 1 | 0% | 1,193 | 3,482 | +192% | 0 | 0 | — |
case-14 | pass→pass | 14,632 | 15,942 | +9% | 1 | 1 | 0% | 2,580 | 4,996 | +94% | 0 | 0 | — |
case-15 | pass→pass | 11,177 | 18,829 | +68% | 1 | 1 | 0% | 1,962 | 5,400 | +175% | 0 | 0 | — |
case-16 | pass→pass | 5,770 | 10,236 | +77% | 1 | 1 | 0% | 884 | 4,287 | +385% | 0 | 0 | — |
case-17 | fail→pass | 9,092 | 9,580 | +5% | 1 | 1 | 0% | 1,682 | 4,377 | +160% | 0 | 0 | — |
case-18 | pass→pass | 5,771 | 2,035 | -65% | 1 | 1 | 0% | 828 | 2,743 | +231% | 0 | 0 | — |
case-19 | pass→pass | 8,523 | 8,187 | -4% | 1 | 1 | 0% | 1,367 | 3,910 | +186% | 0 | 0 | — |
case-20 | pass→pass | 18,224 | 20,425 | +12% | 1 | 1 | 0% | 3,278 | 6,017 | +84% | 0 | 0 | — |
case-21 | pass→pass | 8,632 | 8,167 | -5% | 1 | 1 | 0% | 1,650 | 4,054 | +146% | 0 | 0 | — |
case-22 | pass→pass | 11,415 | 18,009 | +58% | 1 | 1 | 0% | 2,132 | 5,174 | +143% | 0 | 0 | — |
case-23 | pass→pass | 12,276 | 13,228 | +8% | 1 | 1 | 0% | 2,182 | 5,141 | +136% | 0 | 0 | — |
case-24 | pass→pass | 17,092 | 18,960 | +11% | 1 | 1 | 0% | 3,384 | 5,191 | +53% | 0 | 0 | — |
case-25 | pass→pass | 14,543 | 15,239 | +5% | 1 | 1 | 0% | 2,624 | 5,387 | +105% | 0 | 0 | — |
case-26 | pass→pass | 16,189 | 19,197 | +19% | 1 | 1 | 0% | 3,259 | 5,996 | +84% | 0 | 0 | — |
case-27 | pass→pass | 17,743 | 21,346 | +20% | 1 | 1 | 0% | 3,067 | 6,782 | +121% | 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. 27 cases were attempted. The headline lift of 0 percentage points is the difference between those two pass rates over the 27 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
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.