PART 4 • DEEP & GENERATIVE AI • LEVEL 21

Transformers & Large Language Models

Calculate self-attention from first principles, understand encoder and decoder architectures, trace autoregressive generation and evaluate how foundation models are adapted and deployed responsibly.

⏱️ 420–520 min🎯 Beginner → Interview Ready🧪 2 Computational Labs💼 Foundation-Model Focus
attentionfindsusefulcontextHEAD 1HEAD 2HEAD 3CONTEXTUAL STATE
ATTENTION CONTRACTcompare tokens • normalize relevance • mix value evidenceAttention(Q,K,V) = softmax(QKᵀ/√dₖ)V

By the End of This Level, You Can

01Explain why transformers replace recurrent bottlenecks with parallel attention.
02Calculate queries, keys, values, scaled scores, masks and softmax weights.
03Explain multi-head attention, residual paths, normalization and feed-forward blocks.
04Compare encoder, decoder and encoder–decoder model families.
05Trace greedy, Top-K and nucleus next-token generation.
06Select prompting, fine-tuning, PEFT, evaluation and responsible deployment strategies.

Six Building Blocks of a Transformer

A transformer repeatedly builds context-aware token states and converts them into task outputs or next-token probabilities.

TOKEN STATEMeaning at one position

Token and position information represented by a dense vector.

QUERYWhat this token seeks

A learned projection used to compare the current token with all legal keys.

KEYWhat a token offers

A learned projection that determines how relevant a source position is.

VALUEEvidence to retrieve

The information mixed according to normalized attention weights.

MASKWhat may be visible

Padding and causal masks enforce the legal information boundary.

LOGITSScores before probability

Final hidden states are projected to vocabulary scores for prediction.

Transformers Remove the Recurrent Information Bottleneck

Every token can directly compare itself with other permitted positions instead of waiting for a hidden state to carry the entire past.

RNNSequential recurrence

Processes one step after another. Earlier evidence must survive repeated hidden-state updates, limiting parallel training and making long paths difficult.

SELF-ATTENTIONDirect token interaction

Builds pairwise relevance scores so a token can retrieve useful evidence from distant positions in one layer.

TRANSFORMERAttention plus transformation

Combines multi-head attention, position-wise feed-forward networks, residual paths and normalization into a scalable block.

DETAILED EXPLANATION

A recurrent network represents history through a state that is updated step by step. A transformer instead creates a contextual representation for each position by comparing its query with the keys of visible positions and mixing their values. This shortens the path between related tokens and allows all training positions to be processed in parallel. The cost is a dense attention matrix: ordinary self-attention requires memory and computation proportional to the square of sequence length. Transformers therefore improve parallelism and contextual access, but do not make long context free.

WORKED INTUITION

In “The student submitted the project because it was complete,” the token “it” can directly attend to “project” instead of relying on many recurrent updates.

AI / PLACEMENT CONNECTION

Explain both benefits and cost: parallel training and short dependency paths versus quadratic attention and large memory requirements.

COMMON MISCONCEPTION

Attention weights show what information is mixed inside a layer; they are not automatically a complete causal explanation of the model’s decision.

Embeddings and Positions Create the Initial Token States

Attention alone has no built-in understanding of token order, so positional information must enter the sequence representation.

1Tokenize

Split text into vocabulary units and assign IDs.

2Embed

Look up a learned vector for every token ID.

3Position

Add or apply absolute, relative or rotary position information.

4Mask

Block padding or future tokens before softmax.

5Transform

Refine contextual states through stacked blocks.

DETAILED EXPLANATION

Token embeddings identify learned lexical units, while position mechanisms distinguish “student teaches model” from “model teaches student.” Original transformers add sinusoidal position vectors. Learned absolute embeddings store one vector per position. Relative biases represent distance between query and key positions. Rotary position embeddings rotate query and key features so their dot product contains relative-position information. Padding masks prevent artificial batch padding from receiving attention, and causal masks ensure decoder position t can see only positions at or before t.

WORKED INTUITION

Without positions, permuting the same token embeddings would produce the same set of pairwise content comparisons. Position information breaks that symmetry.

AI / PLACEMENT CONNECTION

When discussing context length, distinguish the tokenizer limit, positional method, trained length distribution, attention memory and serving budget.

COMMON MISCONCEPTION

A larger advertised context window does not guarantee that a model uses evidence equally well at every location or distance.

Scaled Dot-Product Attention Retrieves Relevant Evidence

Queries and keys calculate relevance; softmax converts scores into weights; values carry the retrieved information.

PROJECTQ=XWQ, K=XWK, V=XWV

Learn different views for seeking, matching and carrying information.

SCORES=QKᵀ/√dₖ

Compare every legal query–key pair and control dot-product scale.

NORMALIZEA=softmax(S+M)

Apply the mask, then create a probability distribution per query.

MIXH=AV

Build each contextual output as a weighted sum of value vectors.

DETAILED EXPLANATION

For a sequence matrix X, learned matrices WQ, WK and WV create queries, keys and values. The dot product qᵢ·kⱼ measures compatibility between destination position i and source position j. Dividing by √dₖ prevents large-dimensional dot products from driving softmax into saturated, extremely sharp distributions. A mask adds a very negative value to forbidden scores before softmax. Each attention row then sums to one, and multiplying by V forms a context vector using the same weights.

WORKED CALCULATION

If scaled scores are [1, 0, −1], subtract the maximum and exponentiate: [1, e⁻¹, e⁻²]. After division by their sum, the weights are approximately [0.665, 0.245, 0.090].

AI / PLACEMENT CONNECTION

Always state tensor shapes: Q,K,V ∈ ℝⁿˣᵈ; QKᵀ ∈ ℝⁿˣⁿ; softmax operates across each key row.

COMMON MISCONCEPTION

The scaling factor uses the key/query head dimension dₖ, not the full sequence length and not necessarily the total model width.

PREMIUM COMPUTATIONAL VISUALIZER

🧠 Self-Attention Matrix Laboratory

Project token states into queries, keys and values; calculate every scaled score, apply a legal visibility mask, normalize each row and mix the value evidence.

CodeBhavya • Compare, Normalize, Retrieve
PHASEReady
QUERYcontext
LEGAL KEYS
STRONGEST KEY
TOP WEIGHT

Multi-Head Attention Learns Several Relationship Subspaces

Independent heads can specialize in different compatibility patterns before their outputs are concatenated and projected.

HEAD 1Local syntax

May emphasize nearby modifier, subject or dependency evidence.

HEAD 2Long-distance reference

May connect pronouns, entities or repeated concepts across positions.

HEAD 3Task pattern

May organize punctuation, delimiters, positions or semantic roles.

OUTPUTConcatenate + project

Head outputs are joined and mapped back to the model width.

DETAILED EXPLANATION

The model width is divided across h heads, so each head usually uses dₖ=dmodel/h features. Every head owns separate query, key and value projections, calculates its own attention matrix and returns a context representation. Concatenating all head results restores the combined width, and an output matrix mixes their evidence. A transformer block then adds a residual connection and normalizes, applies the same feed-forward network independently at every position, and uses another residual-plus-normalization path.

BLOCK SHAPE

X → attention → add & normalize → feed-forward → add & normalize. Modern variants often apply normalization before each sublayer.

FEED-FORWARD ROLE

Attention exchanges information across positions; the MLP transforms features within each position using shared parameters.

COMMON MISCONCEPTION

More heads do not automatically mean more useful diversity. Redundant or inactive heads can occur, and head dimension becomes smaller when width is fixed.

Encoder and Decoder Families Enforce Different Information Flows

The architecture and training objective decide which tokens are visible and which tasks are natural.

ENCODER-ONLYall tokens ↔ all tokensBERT-style understanding

Bidirectional representations support classification, retrieval and token labelling.

DECODER-ONLYpast → currentGPT-style generation

Causal masking supports next-token prediction and autoregressive completion.

ENCODER–DECODERsource → targetT5-style transformation

An encoder reads the source; a causal decoder attends to source and generated target tokens.

PREFIX / HYBRIDtask-defined visibilityControlled context

Special masks combine bidirectional prefixes with autoregressive outputs.

DETAILED EXPLANATION

Encoder-only models use bidirectional self-attention because a complete input is available before prediction. Decoder-only models use causal self-attention: token t cannot inspect future ground-truth tokens during training, matching left-to-right generation. Encoder–decoder models separate source understanding from target generation and add cross-attention from decoder queries to encoder keys and values. Model family should follow the information boundary and task, not brand popularity.

WORKED DECISION

For low-latency intent classification, an encoder may be smaller and more natural. For open-ended response generation, a causal decoder is appropriate.

AI / PLACEMENT CONNECTION

Compare BERT and GPT using visibility mask, objective, output behavior and common downstream use—not only parameter count.

COMMON MISCONCEPTION

A decoder-only model can perform classification through prompting or fine-tuning, but capability does not imply the best cost, latency or reliability choice.

Large Language Models Learn a Conditional Token Distribution

Pretraining compresses broad statistical regularities into parameters; inference repeatedly converts logits into one selected token.

NEXT-TOKEN OBJECTIVE−Σ log P(xₜ|x<t)

Teacher forcing trains each causal position to predict its observed successor.

LOGITSz = hWᵀ

The final contextual state is projected to one score per vocabulary token.

TEMPERATUREsoftmax(z/T)

Lower T sharpens differences; higher T spreads probability mass.

DECODINGgreedy / Top-K / Top-P

A policy filters or selects from the next-token distribution.

DETAILED EXPLANATION

During causal pretraining, one shifted sequence supplies many training targets. At inference, the model receives a prompt, produces logits for the next position, applies decoding controls, chooses one token, appends it and repeats. Greedy decoding always chooses the maximum. Top-K retains K candidates. Nucleus sampling retains the smallest probability-ranked set whose cumulative mass reaches p. Temperature rescales logits before softmax; it does not add knowledge. Generation ends at a stop token, stop sequence or length limit.

WORKED INTUITION

If logits are [3,2,1], T=0.5 makes them [6,4,2] and the distribution sharper; T=2 makes them [1.5,1,0.5] and less concentrated.

AI / PLACEMENT CONNECTION

Explain decoding as a probability-policy layer separate from the model parameters and prompt context.

COMMON MISCONCEPTION

Lower temperature reduces sampling diversity but cannot guarantee factuality, safety or consistency when the underlying distribution is wrong.

PREMIUM COMPUTATIONAL VISUALIZER

⚡ Next-Token Generation & Decoding Workbench

Inspect candidate logits, rescale them, apply greedy, Top-K or nucleus filtering, calculate probabilities and generate a deterministic token sequence step by step.

CodeBhavya • Score, Filter, Generate
PHASEReady
STEP0
CANDIDATES
SELECTED
PROBABILITY

Adaptation Changes Behavior Without Always Retraining Everything

Choose the smallest reliable intervention that supplies the missing instruction, domain evidence or task behavior.

PROMPTINGNo parameter update

Provide instructions, examples, constraints and context at inference time. Fast to change but consumes context and may be sensitive to wording.

FULL FINE-TUNINGUpdate all parameters

Offers maximum flexibility but requires large training memory, careful regularization and deployment of a distinct model version.

PEFT / LoRATrain small adapters

Learn low-rank updates while keeping the base model frozen, reducing trainable parameters and storage.

PREFERENCE ALIGNMENTShape response choices

Use ranked or preferred responses through supervised and preference-based objectives such as DPO or reward-guided training.

DETAILED EXPLANATION

Prompting is appropriate when the model already has the required capability and the missing piece is instruction or temporary context. Retrieval supplies external evidence without encoding it permanently in weights. Supervised fine-tuning teaches examples of desired input–output behavior. LoRA represents a weight update as a product of two smaller matrices, reducing the number of trainable parameters. Preference tuning changes relative response likelihoods, but no adaptation method replaces task-specific evaluation, data governance or safety controls.

LORA INTUITION

Instead of training a d×d update, learn B∈ℝᵈˣʳ and A∈ℝʳˣᵈ with small rank r, then use W′=W+(α/r)BA.

AI / PLACEMENT CONNECTION

Answer “prompt or fine-tune?” by comparing knowledge freshness, behavior gap, data volume, privacy, cost, latency and evaluation evidence.

COMMON MISCONCEPTION

Fine-tuning is not a dependable database for frequently changing facts; retrieval is usually easier to refresh and cite.

LLM Quality Requires Task, System and Human Evaluation

Perplexity and benchmark scores cannot alone measure grounding, instruction following, operational reliability or harm.

TASK QUALITY

Measure exact match, F1, ranking quality, groundedness, completeness or rubric scores appropriate to the output.

GENERATION QUALITY

Use human or model-assisted rubrics with calibration; inspect factual claims, omissions, style and refusal behavior.

SYSTEM QUALITY

Track time to first token, tokens per second, context usage, cost, availability and tool or retrieval failures.

SAFETY

Test prompt injection, data leakage, harmful instructions, bias, over-refusal and high-impact domain boundaries.

ROBUSTNESS

Evaluate paraphrases, long context, noisy inputs, multilingual text, adversarial wording and distribution shift.

MONITORING

Log privacy-safe traces, version prompts and models, sample failures, capture feedback and maintain rollback paths.

DETAILED EXPLANATION

Perplexity summarizes average next-token surprise on a corpus, but a lower value may not improve a specific product decision. Closed-ended tasks can use deterministic labels and exact metrics. Open-ended outputs need carefully defined rubrics, blinded human review and checks for evaluator bias. A production system must evaluate the complete chain—including retrieval, prompt construction, model, tools, decoding and post-processing—because a correct base model can still fail through stale evidence, truncated context or unsafe orchestration.

HALLUCINATION DIAGNOSIS

Separate unsupported claims caused by missing context, conflicting evidence, prompt ambiguity, model behavior and decoding.

EFFICIENCY

Quantization reduces numerical precision; distillation trains a smaller student; KV caching reuses earlier attention states during decoding.

COMMON MISCONCEPTION

A fluent response is not evidence of truth. Language-model confidence and verbal certainty are not calibrated factual probabilities.

Transformer Logic Before Libraries

Use these compact procedures for coding, calculation and interviews.

SELF-ATTENTION
  1. Project input states into Q, K and V.
  2. Calculate every query–key dot product.
  3. Divide scores by √dₖ.
  4. Add padding or causal mask values.
  5. Apply row-wise softmax and multiply by V.
CAUSAL GENERATION
  1. Tokenize the prompt and build legal positions.
  2. Run the decoder and take the last-position logits.
  3. Apply temperature and candidate filtering.
  4. Select or sample one token.
  5. Append it, reuse KV cache and repeat.
LORA ADAPTATION
  1. Freeze the selected base-model matrix W.
  2. Create trainable low-rank matrices A and B.
  3. Compute W′x = Wx + (α/r)BAx.
  4. Backpropagate only through adapter parameters.
  5. Save, evaluate and version the adapter.
LLM EVALUATION
  1. Define task, users, risks and success rubric.
  2. Create representative and adversarial examples.
  3. Freeze model, prompt, tools and decoding settings.
  4. Measure quality, safety, latency and cost.
  5. Review failures, set gates and monitor production.
PROGRAM TRACING • TRUE NESTED-LOOP EXECUTION

Trace Self-Attention from First Principles

Follow every query, key, dot-product feature, scaled score, softmax denominator and value-weighted context update. The cursor returns through all loops exactly as Python executes.

💻 Transformer & LLM Challenges

Attempt each program independently. Workspaces, hints and model programs remain collapsed initially.

0 / 5Solved independently0 / 500Best score

Test Your Transformer Reasoning

Select one answer per question. Results show your choice, the correct answer and a clear explanation.

Not checked yet

Diagnose LLM Systems Like an AI Engineer

Identify whether failure comes from data, context, model, decoding, tools or the evaluation contract before changing architecture.

WRONG FACT?

Check whether required evidence exists, was retrieved, fits the context window and is actually supported by the output.

INSTRUCTION MISSED?

Inspect prompt priority, conflicts, truncation, examples, output schema and fine-tuning distribution.

OUTPUT UNSTABLE?

Freeze model version and prompt; inspect temperature, Top-P, seeds, tool results and asynchronous context.

TOO SLOW?

Separate retrieval, prefill and decoding latency; inspect token count, model size, batching, caching and quantization.

UNSAFE RESPONSE?

Test the complete system boundary with threat models, access control, filtering, red teaming and human escalation.

COST TOO HIGH?

Reduce unnecessary context and output, route simple tasks, cache safe results and compare smaller task-specific models.

CodeBhavya interview pattern:Define user task → Specify information boundary → Choose architecture → Trace tokens and context → Separate model from decoding → Establish baseline → Evaluate quality and safety → Inspect latency and cost → Add monitoring and fallback → Connect the metric to user impact.

🎤 Transformers & LLMs — Interview Questions

Answer aloud before selecting Show Answer for each detailed explanation.

A Transformer Is a Repeated Evidence-Routing System

1Encode

Create token and position states.

2Compare

Score every legal query–key pair.

3Retrieve

Mix values using attention weights.

4Transform

Apply residual and MLP blocks.

5Decide

Predict, generate and verify.

Large language models become understandable when you separate tokenization, positional information, attention, learned parameters, decoding policy, external evidence and evaluation.

Eight Practical Transformer Habits

01

Write tensor shapes beside every Q, K, V and attention operation.

02

Apply masks before softmax and verify that forbidden probabilities become zero.

03

Separate prompt tokens, generated tokens, context limit and billing units.

04

Build deterministic evaluation before changing temperature or sampling.

05

Use retrieval for fresh evidence and adaptation for repeatable behavior.

06

Evaluate the whole system, not only the base model or one benchmark.

07

Version the model, tokenizer, prompt, tools, decoding and safety policy together.

08

Keep a smaller baseline and a safe fallback for cost or availability failures.

Strengthen Transformer Reasoning

Calculate intermediate values and defend each architecture, adaptation and evaluation choice.

  1. 01

    Write the shapes of Q, K, V, QKᵀ and AV for n=8, dmodel=512 and h=8.

  2. 02

    Calculate softmax weights for scaled scores [2,1,0].

  3. 03

    Apply a causal mask to a 4×4 attention matrix.

  4. 04

    Explain why √dₖ scaling stabilizes softmax.

  5. 05

    Compare absolute, relative and rotary position methods.

  6. 06

    Trace one multi-head attention block with two heads.

  7. 07

    Compare pre-norm and post-norm transformer blocks.

  8. 08

    Distinguish encoder-only, decoder-only and encoder–decoder use cases.

  9. 09

    Calculate temperature-scaled probabilities from three logits.

  10. 10

    Apply Top-K and Top-P filtering to one token distribution.

  11. 11

    Explain how KV caching reduces repeated decoding work.

  12. 12

    Calculate trainable LoRA parameters for given d and rank r.

  13. 13

    Choose prompting, retrieval or fine-tuning for four business scenarios.

  14. 14

    Design an evaluation set for a multilingual student-support assistant.