Macalinao StudioLearnAll enriched lessons ↗
College path · 7 sectionsSwitch to high school ↗

College / LONGER LESSON 07

Transformer families: follow the visibility rules

Draw encoder, decoder and cross-attention connections before choosing an architecture.

Preparation: Tables, sequences and basic attention.

Plan 20–35 minutes with the activities; this is a study estimate, not a timed requirement. Both paths have complete narration. Your notes and progress are kept separately for each level.

Section 01 / 07

Architecture and objective work together

College path. Draw encoder, decoder and cross-attention connections before choosing an architecture.

Preparation: Tables, sequences and basic attention. Work through one section at a time. Predict the experiment result before changing its controls, then explain what the example leaves out.

An architecture defines computation and allowed connections. A training objective defines what that computation is rewarded for predicting. Neither name alone fully specifies a model. Distinguish encoder-only, decoder-only and encoder-decoder layouts from masked-token, causal and sequence-to-sequence objectives. Variants may combine these choices.

Worked step 1. Write four input positions.

Worked step 2. Draw the permitted connections.

Worked step 3. Then separately write the prediction target.

Try it yourself. Does decoder-only specify a particular dataset?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. No. It describes a layout and usual causal pattern, not the content or quality of the training corpus.

Section 02 / 07

An encoder can compare both directions

Bidirectional self-attention can use earlier and later visible input positions. Padding positions should still be excluded where appropriate; bidirectional does not mean every stored slot is meaningful. For four valid tokens, the unrestricted self-attention score matrix is four by four. A padding mask removes invalid key positions, with API-specific mask polarity.

Worked step 1. Four valid tokens permit 16 query-key pairs.

Worked step 2. If one key is padding, four queries have 12 valid pairs.

Worked step 3. Handle padded output rows separately.

Try it yourself. Can a masked word’s true token be left visible?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. Not in a masked-token exercise that intends to hide it. The corruption scheme must remove the answer from its input position.

Section 03 / 07

A decoder hides the future

Causal attention prevents an earlier position from reading a later input position. Shifted targets let each visible prefix predict its next token without leaking the answer. An inclusive lower-triangular mask permits current and previous input positions. Training targets are shifted, so seeing the current input is not seeing its next-token target.

causal pairs = n(n+1)/2

Worked step 1. For four positions, allowed counts are 1,2,3,4.

Worked step 2. Total allowed pairs are 10.

Worked step 3. Future pairs number 6.

Try it yourself. Why is the diagonal normally allowed?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. The current input token belongs to the prefix. Its prediction target is the following token, not the same unshifted label.

Section 04 / 07

Two sequences can meet

An encoder-decoder model reads a source sequence and generates a target sequence. Decoder cross-attention queries come from target states; keys and values come from source states. With target length three and source length five, cross-attention scores have shape three by five per head. Causal target self-attention remains a separate operation.

Worked step 1. Draw three target rows and five source columns.

Worked step 2. There are 15 potential cross pairs.

Worked step 3. The output still has three query positions.

Try it yourself. Must source and target lengths match?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. No. Cross-attention permits different lengths, provided the projected feature dimensions are compatible.

Section 05 / 07

Test masks, not just shapes

A tensor can have the correct shape while exposing forbidden information. Change a future input and verify that earlier causal outputs do not change in a controlled evaluation. Disable dropout and hold other inputs fixed for the test. Also test padding and fully masked rows; all negative-infinity scores can make softmax undefined.

Worked step 1. Run a causal fixture twice.

Worked step 2. Change only the fourth token.

Worked step 3. Compare outputs at positions one through three.

Try it yourself. Should earlier causal outputs change?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. Not through that future token in this controlled forward pass. If they do, inspect masking or another unintended dependency.

Section 06 / 07

Choose by the actual task

Classification, retrieval, translation and generation place different demands on the system. Compare complete training and evaluation setups rather than treating one architecture family as universally superior. Account for latency, available supervision, source conditioning, context length and serving constraints. Decoder-only systems can perform many tasks; architectural labels are not exclusive capability boundaries.

Worked step 1. Specify the input and required output.

Worked step 2. List what each output may observe.

Worked step 3. Choose and evaluate a compatible layout.

Try it yourself. Can a decoder-only model summarize?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. Yes. It can condition on a source in its prefix. That differs from a separate encoder-decoder cross-attention layout.

Section 07 / 07

Mastery and transfer

Bring the pieces together. First answer the checkpoint without the worked solutions. Then explain the mechanism in your own words, using one numerical example from this lesson.

Three target queries and five source keys produce… Option 1: Five output query positions. Option 2: A 3 by 5 score matrix. Option 3: A required 5 by 5 score matrix.

Pause here and choose an answer before continuing. The checkpoint explanation follows.

Reveal worked solution

The correct choice is option 2: A 3 by 5 score matrix. Cross-attention scores follow query length by key length.

Transfer task. Change one assumption in the worked example. State your prediction, calculate or test it in Workbench, then explain whether the result supports your prediction.

Reveal worked solution

Self-check rubric. Name the changed assumption, keep the other inputs fixed, show at least one intermediate calculation, and distinguish what the toy example guarantees from what would require a trained model or a real measurement.

MODERN MODEL CASE STUDY

DeepSeek V4.1 Flash: where this idea goes next

Compare the causal encoder with the familiar bidirectional encoder. Follow the worked examples, then evaluate the reported gains and their limits.

Open the college case study →

Added October 21, 2026. This optional extension has its own complete local narration.

Sources and scope

Original worked examples and teaching text, grounded in these primary references. Numerical fixtures explain mechanisms; model quality and hardware performance require separate measurements. Both levels share key examples, with different explanations and checkpoints.

Reset this college path?

This removes only this path’s notes, progress and experiments from this browser.