Skip to content
BriskStack Study
Sign in
Deep Learning & Transformer Architectures

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).
Next sectionFrom word vectors to attentionabout 1 min

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).

Next sectionInside the transformer (Needs access)
Sign in to keep reading. It is free, and your place is kept. Sign in and continue

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.

The unit is done when you have answered the check and marked the required sources.

  1. 1 Check yourself

    Do the check
  2. 2 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 0 to 1.2 on 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.

More for this unit

22 readings

Readings no section cites. They do not count toward the unit.

Show
The transformer architecture7 readings
  • youtube.comvideoGo deeperabout 3 h 31 min

    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.

  • youtube.comvideoGo deeperabout 13 min

    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.

  • youtube.comvideoGo deeperabout 22 min

    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.

  • youtube.comvideoGo deeperabout 27 min

    Builds visual intuition for attention and for how large language models generate text. Watch it before the chapter if you prefer pictures to prose.

  • youtube.comvideoGo deeperabout 1 h 56 min

    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.

  • poloclub.github.iotoolGo deeper

    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.

  • youtube.comvideoGo deeper

    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.

Transformer families (encoder, decoder, encoder-decoder)2 readings
  • jalammar.github.iopaperGo deeperabout 30 min

    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.

  • arxiv.orgpaperGo deeper

    Introduces deep bidirectional pre-training, fine-tuned with one added output layer. Read the introduction to see what the encoder gives up and gains.

NLP lineage (word2vec, RNNs, transformers)4 readings
  • youtube.comvideoGo deeperabout 1 h 30 min

    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.

  • youtube.comvideoGo deeper

    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.

  • towardsdatascience.compaperGo deeperabout 25 min

    A visual walkthrough of the whole evolution, from RNN to LSTM to attention to transformer. Read it for the full sequence in one place.

  • baeldung.compaperGo deeperabout 15 min

    A concise technical comparison of the architectures, with diagrams. Read it when you want a short summary of the differences.

Tokenization and security3 readings
  • platform.openai.comtoolGo deeper

    An interactive tool that shows how text maps to tokens. Use it to watch payloads split, which is the core of understanding token smuggling.

  • huggingface.copaperGo deeperabout 30 min

    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.

  • youtube.comvideoGo deeperabout 2 h

    A deep dive into BPE tokenization, built from scratch. Watch it if you want to implement a tokenizer yourself.

RLHF and fine-tuning2 readings
  • youtube.comvideoGo deeperabout 18 min

    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.

  • sebastianraschka.comreadingGo deeper

    πŸ“– : 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.

LoRA and PEFT (fine-tuning as attack surface)2 readings
  • huggingface.copaperGo deeperabout 1 h

    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.

  • youtube.comvideoGo deeperabout 20 min

    A clear technical walkthrough of low-rank adaptation with the math intuition. Watch it after the LoRA conceptual guide.

Temperature and sampling2 readings
  • docs.cohere.compaperGo deeperabout 10 min

    Explains temperature, where 0 is deterministic and higher values are more random. It is the determinism knob that red teams must report, so read it before the sampling section.

  • huggingface.copaperGo deeper

    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.

This unit's library holds 1 more reading for readers with access.