Self-Attention, Subword Tokenization and Transformer Families

MetaCyberGuru Academy

AdvancedEstimated learning effort: 85 minutesFree, no sign-up requiredPublished by Muhammad AzharCourse version: August 2026

Back to Transformers, Fine-Tuning and NLP Tasks

A transformer does not read a sentence as one object. It turns text into subword IDs, builds contextual vectors and uses attention masks to control which positions may exchange information.

Learning objective

By the end, you can calculate a small attention result, explain why token counts differ from word counts, and choose an encoder, decoder or encoder-decoder family for a stated NLP task.

From text to contextual vectors

A tokenizer first maps text to subword units. A familiar word may become one token, while a name, spelling variation or Urdu-English code-switched phrase may split into several. This matters because model limits and usage costs are measured in tokens. Always inspect the tokenizer used by the exact checkpoint rather than estimating from spaces.

For one attention head, the model projects each input vector into a query, key and value. It scores every allowed query-key pair, divides by the square root of the key width, applies the mask, then normalizes with softmax. The weighted sum of value vectors is the contextual output. Multi-head attention repeats this operation with different learned projections and combines the results.

Transformer family selection
FamilyAttention patternTypical fit
Encoder-onlyBidirectional over visible inputClassification, NER and retrieval embeddings
Decoder-onlyCausal, each position sees earlier positionsGeneration and conversational completion
Encoder-decoderInput encoding plus causal output decodingTranslation, summarization and sequence transformation

Context windows are an engineering limit

A larger context window does not guarantee that every supplied fact will influence the answer equally. Long inputs increase memory and latency, and important evidence can be diluted. Count tokens, reserve room for output, place trusted instructions separately from untrusted documents, and test answer quality by evidence position. For long corpora, retrieval and structured summaries are usually more controllable than sending everything on every request.

Calculate one attention head

Save this as attention_demo.py. The matrices are deliberately tiny so you can inspect the scores rather than trusting a framework.

python -m pip install numpy
python attention_demo.py
# attention_demo.py
import numpy as np

tokens = ["refund", "request", "today"]
q = np.array([[1.0, 0.0], [0.8, 0.2], [0.0, 1.0]])
k = np.array([[1.0, 0.0], [0.5, 0.5], [0.0, 1.0]])
v = np.array([[1.0, 0.0], [0.0, 2.0], [3.0, 1.0]])

scores = q @ k.T / np.sqrt(k.shape[1])
weights = np.exp(scores - scores.max(axis=1, keepdims=True))
weights = weights / weights.sum(axis=1, keepdims=True)
context = weights @ v

for token, row, vector in zip(tokens, weights, context):
    print(token, "weights", np.round(row, 3), "context", np.round(vector, 3))

Expected output

refund weights [0.456 0.32  0.225] context [1.129 0.864]
request weights [0.406 0.328 0.266] context [1.203 0.922]
today weights [0.225 0.32  0.456] context [1.591 1.095]

The exact rows sum to one. The last query gives the largest weight to the last key because their directions align most strongly. A real model learns the projections, adds positional information and runs many layers. This script proves the calculation only, not linguistic quality.

Debug the representation before the model

  • Unexpected truncation: print token IDs, token count, special tokens and the truncation side.
  • Padding changes predictions: confirm the attention mask marks padding as unavailable.
  • Decoder leaks future text: inspect the causal mask rather than assuming the architecture created it.
  • Names fragment badly: compare tokenizer coverage on the target language and preserve the checkpoint revision.

Practice: build a tokenizer and context audit

Choose one checkpoint from each architecture family. Tokenize ten English sentences, ten Urdu sentences and ten Roman Urdu code-switched sentences. Record median tokens per sentence, truncation rate and one surprising split. Then explain which family you would use for ticket classification, translation and answer generation.

Challenge: move the same relevant sentence to the beginning, middle and end of a long input. Compare the output and document whether position changes the result.

Knowledge check

1. Why divide attention scores by the square root of key width?
Check the answer

Answer: The second option. Scaling stabilizes the magnitude before softmax.

2. Which family naturally fits translation?
Check the answer

Answer: The third option, because it encodes a source sequence and decodes a target sequence.

3. What should you inspect before claiming text fits the context window?
Check the answer

Answer: The first option.

Authoritative references

Save your place

Completion is stored only in this browser on this device.

Share this page

Share this page with the people who will use it next.

X Facebook LinkedIn WhatsApp Email

Discussion

No comments yet. Add the first useful question or observation.