Foundations, 3 of 5, about 4 h 5 min of reading
Deep Learning & Transformer Architectures
About 3 h 57 min left
Concept. A transformer is a next-token predictor built almost entirely from one operation, attention, and everything that makes it powerful also makes it exploitable. Attention weighs every input token against every other token. That is why an instruction buried in retrieved text or in a tool description gets the same consideration as your system prompt. This week you learn the architecture from the inside, so the attacks in the attack weeks read as consequences and not as surprises. Prompt injection is attention doing its job, token smuggling is tokenization doing its job, and weaponized model variants are fine-tuning doing its job.
π― Objectives
By the end of this unit you can:
- Trace the natural language processing (NLP) lineage from word2vec embeddings to recurrent neural networks (RNNs) and long short-term memory networks (LSTMs) to the transformer, and explain why self-attention replaced recurrence (Illustrated Word2Vec, Colah on LSTMs).
- Walk through the query, key and value mechanism, multi-head attention and positional encoding, and name the data and instruction conflation that attention creates (Illustrated Transformer).
- Explain how byte pair encoding (BPE) and byte-level BPE map text to tokens, and how token boundaries enable token smuggling past naive filters (HF Tokenizers).
- Reason about temperature, top-k and top-p, and why non-determinism matters for both testing and probing (Raschka).
- Explain how low-rank adaptation (LoRA) and parameter-efficient fine-tuning (PEFT) make weaponized model variants affordable, and cite the evidence that a handful of examples unaligns a safety-tuned model (Qi et al. 2023).
- Build a basic chatbot against a transformer API to see the inference loop from end to end.
- Tell the three transformer families apart (encoder-only, decoder-only and encoder-decoder) by their attention mask, name a model of each, and say which attack surface each one is in this course (Hugging Face).
- (Go deeper) Build and measure the control that answers each primitive: canonicalize text before a filter with NFKC and the UTS 39 skeleton, bound a failure rate from repeated samples, and admit adapters by hash with a safety regression gate, and say where each stops (UTS 39, Qi et al. 2023).
From word vectors to attention
Before transformers, meaning was already geometric. The word2vec model learns
to place each word at a point in a dense vector space. It does this by
predicting a word from its neighbors (continuous bag of words, CBOW) or its
neighbors from the word (skip-gram). Words that mean similar things end up
close together, and vector arithmetic captures relationships, as in the
famous king β man + woman β queen (Illustrated
Word2Vec). That embedding
space is more than a historical footnote. It is the same space that
retrieval-augmented generation (RAG) searches in RAG SystemsRAG security, 1 of 2, about 5.4 h, and the same space
that adversarial perturbations move through in RAG Poisoning & Knowledge CorruptionRAG security, 2 of 2, about 3.6 h.
The next problem was sequence. RNNs loop a hidden state forward token by token, but as the gap between related words grows they "become unable to learn to connect the information", which is the vanishing-gradient problem. LSTMs patched this with a cell-state "conveyor belt" gated by forget, input and output gates, which bought longer memory (Colah on LSTMs). Both stayed strictly sequential, because token n waits for token nβ1. That caps parallelism and still strains on long dependencies.
π The transformer's one big idea. Drop recurrence entirely. Because every token attends to every other token in parallel, training parallelizes far better, and a long-range dependency is one hop away instead of n hops (Attention Is All You Need).
Inside the transformer
Three families, three attack surfaces
Tokenization: where text becomes tokens
Sampling: temperature, top-k, top-p
Fine-tuning as attack surface
Go deeper: the defender's side of the three primitives
β Check yourself
π§ͺ Hands-on
Finish Deep Learning & Transformer Architectures
About 3 h 57 min left in this unit.
From memory first
Write the main points of this unit, in your own words.
What you set out to be able to do
Check each one against what you can do now.
- Trace the natural language processing (NLP) lineage from word2vec embeddings to recurrent neural networks (RNNs) and long short-term memory networks (LSTMs) to the transformer, and explain why self-attention replaced recurrence (Illustrated Word2Vec, Colah on LSTMs).
- Walk through the query, key and value mechanism, multi-head attention and positional encoding, and name the data and instruction conflation that attention creates (Illustrated Transformer).
- Explain how byte pair encoding (BPE) and byte-level BPE map text to tokens, and how token boundaries enable token smuggling past naive filters (HF Tokenizers).
- Reason about temperature, top-k and top-p, and why non-determinism matters for both testing and probing (Raschka).
- Explain how low-rank adaptation (LoRA) and parameter-efficient fine-tuning (PEFT) make weaponized model variants affordable, and cite the evidence that a handful of examples unaligns a safety-tuned model (Qi et al. 2023).
- Build a basic chatbot against a transformer API to see the inference loop from end to end.
- Tell the three transformer families apart (encoder-only, decoder-only and encoder-decoder) by their attention mask, name a model of each, and say which attack surface each one is in this course (Hugging Face).
- (Go deeper) Build and measure the control that answers each primitive: canonicalize text before a filter with NFKC and the UTS 39 skeleton, bound a failure rate from repeated samples, and admit adapters by hash with a safety regression gate, and say where each stops (UTS 39, Qi et al. 2023).
The unit is done when you have answered the check and marked the required sources.
1 Check yourself
Do the check2 Required sources
Go to the sources
Sources in this unit
- Jay Alammar: The Illustrated Transformer
- Vaswani et al.: "Attention Is All You Need" (2017)
- Jay Alammar: The Illustrated Word2Vec
- Hugging Face: Tokenizers Summary
- Qi et al.: "Fine-tuning Aligned Language Models Compromises Safety" (2023)
- Hugging Face: LoRA Conceptual Guide
- Andrej Karpathy: Deep Dive into LLMs like ChatGPT
- Gal Lahat: I Visualised Attention in Transformers
- Under The Hood: How Attention Mechanism Works in Transformer Architecture
- 3Blue1Brown: But what is a GPT? Visual intro to transformers
- Andrej Karpathy: Let's build GPT: from scratch, in code
- Transformer Explainer (Georgia Tech)
- Stanford CS25: Intro to Transformers w/ Andrej Karpathy
- Hugging Face LLM Course: How do Transformers work?
- Jay Alammar: The Illustrated GPT-2
- Devlin et al.: BERT (2018)
- Stanford CS224N: Word Vectors (Spring 2024)
- Stanford CS224N: Full Playlist (Spring 2024)
- Understanding LSTM Networks (Colah's Blog)
- Beautifully Illustrated: NLP Models from RNN to Transformer
- From RNNs to Transformers (Baeldung)
- OpenAI Tokenizer
- Hugging Face: BPE Tokenization (LLM Course Ch.6)
- Andrej Karpathy: Let's build the GPT Tokenizer
- StatQuest: RLHF Clearly Explained
- Sebastian Raschka: Build a Large Language Model (From Scratch)
- Hugging Face: PEFT Documentation
- Sebastian Raschka: LoRA and QLoRA: Efficient LLM Finetuning
- Cohere: LLM Parameters Demystified
- How to generate text: different decoding methods
- Sebastian Raschka: Temperature, Top-k, and Top-p FAQ
Checklist
It does not count toward done.
- Trace the NLP lineage from the word2vec embedding space to RNNs and LSTMs (vanishing gradients), and write one sentence on why self-attention replaced recurrence.
- Walk through the transformer (query/key/value self-attention, multi-head attention, positional encoding) and sketch one attention step on paper.
- Name the data and instruction conflation and write one sentence connecting it to prompt injection.
- Explain byte pair encoding (BPE) and byte-level BPE, and show in the OpenAI Tokenizer how a split word can slip past a substring filter.
- Explain temperature, top-k and top-p, and state why a single refusal at one sampling setting proves nothing, and what a red-team report must record.
- Explain the economics of LoRA and PEFT, and cite the result that about 10 examples unalign a safety-tuned model.
- π§ͺ Build a basic chatbot against a transformer API, log the tokenization of each turn, and sweep temperature from
0to1.2on the same prompt, noting where repeated runs stop giving the same output. - π§ Use the Transformer Explainer to watch attention and temperature live on GPT-2, and note which tokens each head attends to.
- Name the three transformer families by their attention mask, give one model each, and state the attack surface each is in this course. Give BERT (fill-mask) and GPT-2 (next token) the same sentence with a gap, and write down what context each could use.
Practise this in the lab, Make a filter read what the model will read.40 to 55 min estimate
Study notes
None yet
Sign in to take notes.
Your additions
None yet
More for this unit
22 readings
Readings no section cites. They do not count toward the unit.
The transformer architecture7 readings
- youtube.comvideo
A long technical talk on how large language models are built and how inference works, and the most-watched LLM explainer (6.2M views). Watch it when you want the whole pipeline in one sitting.
Sign in to mark it done, keep it for later and open its questions.
- youtube.comvideo
A visual explainer of attention with strong animations (250K views), which complements the math-heavy approach of 3Blue1Brown. Watch it if the Illustrated Transformer left attention abstract.
Sign in to mark it done, keep it for later and open its questions.
- youtube.comvideo
A walkthrough of query/key/value, multi-head attention and positional encoding with code-level intuition (106K views). Watch it when you want to see the same mechanism as code.
Sign in to mark it done, keep it for later and open its questions.
- youtube.comvideo
Builds visual intuition for attention and for how large language models generate text. Watch it before the chapter if you prefer pictures to prose.
Sign in to mark it done, keep it for later and open its questions.
- youtube.comvideo
Karpathy builds a transformer from scratch in PyTorch, so you see every component. Watch it when you want to understand the parts by writing them.
Sign in to mark it done, keep it for later and open its questions.
- poloclub.github.iotool
Runs a live GPT-2-small in the browser. Hover a token to see attention across all 12 heads, trace the path from embedding through query/key/value, the multilayer perceptron (MLP) and softmax, and drag a temperature slider on the output. It shows the three security surfaces of this week (token split, attention weights, sampling) in one view, so open it while you read the chapter.
Sign in to mark it done, keep it for later and open its questions.
- youtube.comvideo
The inaugural lecture of the "Transformers United" course, on the impact of transformers from language to vision to biology. Watch it for context on why the architecture spread.
Sign in to mark it done, keep it for later and open its questions.
Transformer families (encoder, decoder, encoder-decoder)2 readings
- jalammar.github.iopaper
Explains masked self-attention and auto-regression, contrasted with BERT's two-sided context, so it is the picture behind the attention mask. Read it after the families table.
Sign in to mark it done, keep it for later and open its questions.
- arxiv.orgpaper
Introduces deep bidirectional pre-training, fine-tuned with one added output layer. Read the introduction to see what the encoder gives up and gains.
Sign in to mark it done, keep it for later and open its questions.
NLP lineage (word2vec, RNNs, transformers)4 readings
- youtube.comvideo
Chris Manning's lecture on word2vec, GloVe and embedding spaces, in the latest publicly available edition. Watch it if the word-vector section of the chapter was too brief.
Sign in to mark it done, keep it for later and open its questions.
- youtube.comvideo
The complete course on natural language processing (NLP) with deep learning, from word vectors through transformers. Use it as a reference when you want more depth on any step of the lineage.
Sign in to mark it done, keep it for later and open its questions.
- towardsdatascience.compaper
A visual walkthrough of the whole evolution, from RNN to LSTM to attention to transformer. Read it for the full sequence in one place.
Sign in to mark it done, keep it for later and open its questions.
- baeldung.compaper
A concise technical comparison of the architectures, with diagrams. Read it when you want a short summary of the differences.
Sign in to mark it done, keep it for later and open its questions.
Tokenization and security3 readings
- platform.openai.comtool
An interactive tool that shows how text maps to tokens. Use it to watch payloads split, which is the core of understanding token smuggling.
Sign in to mark it done, keep it for later and open its questions.
- huggingface.copaper
A detailed BPE walkthrough with code, covering byte-level BPE (GPT-2) and the
<unk>and ambiguous-tokenization edge cases that evade filters. Read it when you want the mechanism behind token smuggling.Sign in to mark it done, keep it for later and open its questions.
- youtube.comvideo
A deep dive into BPE tokenization, built from scratch. Watch it if you want to implement a tokenizer yourself.
Sign in to mark it done, keep it for later and open its questions.
RLHF and fine-tuning2 readings
- youtube.comvideo
A clear explanation of reinforcement learning from human feedback (RLHF), the step that aligns models after pre-training. Watch it to see what the fine-tuning attacks undo.
Sign in to mark it done, keep it for later and open its questions.
- sebastianraschka.comreading
π : A 2024 book from Manning that bridges the math and the implementation, covering tokenization, transformers and fine-tuning from first principles in PyTorch. Use it as a long-term companion to the week.
Sign in to mark it done, keep it for later and open its questions.
LoRA and PEFT (fine-tuning as attack surface)2 readings
- huggingface.copaper
The official docs of the parameter-efficient fine-tuning (PEFT) library, which freezes the base model and trains a tiny parameter set. That is the economics that make weaponized variants feasible, so read it with the fine-tuning section.
Sign in to mark it done, keep it for later and open its questions.
- youtube.comvideo
A clear technical walkthrough of low-rank adaptation with the math intuition. Watch it after the LoRA conceptual guide.
Sign in to mark it done, keep it for later and open its questions.
Temperature and sampling2 readings
- docs.cohere.compaper
Explains temperature, where
0is deterministic and higher values are more random. It is the determinism knob that red teams must report, so read it before the sampling section.Sign in to mark it done, keep it for later and open its questions.
- huggingface.copaper
A Hugging Face blog post that covers greedy search, beam search, top-k and top-p (nucleus) sampling and their tradeoffs. Read it when you want the decoding methods side by side.
Sign in to mark it done, keep it for later and open its questions.
This unit's library holds 1 more reading for readers with access.