Model Architecture
Use this sub-skill when you need to construct, inspect, or debug the repository's
Transformer model classes rather than preprocess data, run training CLIs, or
perform checkpoint translation.
Use This For
- Building
Transformer, Encoder, Decoder, EncoderLayer, DecoderLayer,
MultiHeadAttention, PositionwiseFeedForward, ScaledDotProductAttention,
PositionalEncoding, or ScheduledOptim instances.
- Checking source/target mask shapes, boolean semantics, causal target masks,
device placement, attention tensor shapes, and flattened model logits.
- Choosing safe tiny-model settings for unit tests, especially weight-sharing
flags and
d_model == d_word_vec.
- Debugging architecture assertions, projection/embedding scaling, incompatible
shared vocabularies, and Noam-style warmup scheduling.
Route Elsewhere
- Dataset fields, vocab building, BPE, spaCy, or pickle schemas belong to the
data-preparation sub-skill.
- Training command-line options, logging, checkpoints, label smoothing, and loss
handling belong to the training sub-skill.
- Checkpoint loading,
Translator, beam search, and decoding workflows belong
to the translation sub-skill; this sub-skill only covers class-level model
shapes that those workflows rely on.
Primary References
- Start with references/api-reference.md for
constructor signatures, forward contracts, return shapes, and optimizer API.
- Use references/architecture-notes.md for
end-to-end tensor flow, mask broadcasting, tiny-model recipes, and weight
sharing rules.
- Use references/troubleshooting.md for common
assertion, mask, CUDA/device, flattened-output, sharing, and scheduler
failures.
Smoke Check
Run the bundled architecture check from any current directory by pointing it at
an explicit repository checkout:
python scripts/architecture_smoke_check.py --repo-root /path/to/attention-is-all-you-need-pytorch
Useful variants:
python scripts/architecture_smoke_check.py --repo-root /path/to/repo --device cpu
python scripts/architecture_smoke_check.py --repo-root /path/to/repo --device cuda
python scripts/architecture_smoke_check.py --repo-root /path/to/repo --skip-negative-checks
The script constructs a tiny deterministic Transformer with sharing disabled,
checks pad and causal masks, verifies multi-head attention broadcasting, probes
ScheduledOptim warmup/decay behavior, and optionally confirms that known-bad
architecture settings fail for the expected reasons.
Fast Operating Pattern
- Keep
d_model equal to d_word_vec; the model asserts this after parameter
initialization because residual connections require a common hidden width.
- If source and target vocabularies are not the same shared vocabulary with the
same token ids, set
emb_src_trg_weight_sharing=False. For tiny tests with
different vocab sizes, also set trg_emb_prj_weight_sharing=False unless you
specifically need target embedding/projection sharing.
- Use
scale_emb_or_prj only as one of emb, prj, or none; scaling is
applied only when target embedding/projection sharing is enabled.
- Treat masks as boolean keep-masks:
True means a key position is visible and
False means it is masked before softmax.
- Remember that
Transformer.forward(src_seq, trg_seq) returns flattened
logits with shape [batch * target_length, n_trg_vocab], not
[batch, target_length, n_trg_vocab].
1---2name: model-architecture-23description: "Instantiate and debug the attention-is-all-you-need-pytorch4 Transformer architecture, masks, attention layers, feed-forward blocks, weight5 sharing, tensor shapes, and scheduled optimizer."6disable-model-invocation: true7metadata:8 source: https://skillmd.com/skills/vectorspacelab/model-architecture-29 disco-role: operating10license: MIT11---1213# Model Architecture1415Use this sub-skill when you need to construct, inspect, or debug the repository's16Transformer model classes rather than preprocess data, run training CLIs, or17perform checkpoint translation.1819## Use This For2021- Building `Transformer`, `Encoder`, `Decoder`, `EncoderLayer`, `DecoderLayer`,22 `MultiHeadAttention`, `PositionwiseFeedForward`, `ScaledDotProductAttention`,23 `PositionalEncoding`, or `ScheduledOptim` instances.24- Checking source/target mask shapes, boolean semantics, causal target masks,25 device placement, attention tensor shapes, and flattened model logits.26- Choosing safe tiny-model settings for unit tests, especially weight-sharing27 flags and `d_model == d_word_vec`.28- Debugging architecture assertions, projection/embedding scaling, incompatible29 shared vocabularies, and Noam-style warmup scheduling.3031## Route Elsewhere3233- Dataset fields, vocab building, BPE, spaCy, or pickle schemas belong to the34 data-preparation sub-skill.35- Training command-line options, logging, checkpoints, label smoothing, and loss36 handling belong to the training sub-skill.37- Checkpoint loading, `Translator`, beam search, and decoding workflows belong38 to the translation sub-skill; this sub-skill only covers class-level model39 shapes that those workflows rely on.4041## Primary References4243- Start with [references/api-reference.md](references/api-reference.md) for44 constructor signatures, forward contracts, return shapes, and optimizer API.45- Use [references/architecture-notes.md](references/architecture-notes.md) for46 end-to-end tensor flow, mask broadcasting, tiny-model recipes, and weight47 sharing rules.48- Use [references/troubleshooting.md](references/troubleshooting.md) for common49 assertion, mask, CUDA/device, flattened-output, sharing, and scheduler50 failures.5152## Smoke Check5354Run the bundled architecture check from any current directory by pointing it at55an explicit repository checkout:5657```bash58python scripts/architecture_smoke_check.py --repo-root /path/to/attention-is-all-you-need-pytorch59```6061Useful variants:6263```bash64python scripts/architecture_smoke_check.py --repo-root /path/to/repo --device cpu65python scripts/architecture_smoke_check.py --repo-root /path/to/repo --device cuda66python scripts/architecture_smoke_check.py --repo-root /path/to/repo --skip-negative-checks67```6869The script constructs a tiny deterministic Transformer with sharing disabled,70checks pad and causal masks, verifies multi-head attention broadcasting, probes71`ScheduledOptim` warmup/decay behavior, and optionally confirms that known-bad72architecture settings fail for the expected reasons.7374## Fast Operating Pattern75761. Keep `d_model` equal to `d_word_vec`; the model asserts this after parameter77 initialization because residual connections require a common hidden width.782. If source and target vocabularies are not the same shared vocabulary with the79 same token ids, set `emb_src_trg_weight_sharing=False`. For tiny tests with80 different vocab sizes, also set `trg_emb_prj_weight_sharing=False` unless you81 specifically need target embedding/projection sharing.823. Use `scale_emb_or_prj` only as one of `emb`, `prj`, or `none`; scaling is83 applied only when target embedding/projection sharing is enabled.844. Treat masks as boolean keep-masks: `True` means a key position is visible and85 `False` means it is masked before softmax.865. Remember that `Transformer.forward(src_seq, trg_seq)` returns flattened87 logits with shape `[batch * target_length, n_trg_vocab]`, not88 `[batch, target_length, n_trg_vocab]`.