How Neural Networks & Large Language Models Actually Work
Strip away the hype: from 13,000 numbers in a matrix to self-attention, next-token probability distributions, and GPU memory bottlenecks.
A neural network is not a mysterious digital brain — it is a giant mathematical function made of billions of adjustable dials (weights). A Large Language Model (Transformer) converts text into high-dimensional direction vectors, uses 'Attention' so words dynamically pass context to one another in parallel, and outputs a probability score for every possible next token.
- 1.Tokens & Embeddings: Models don't read letters; they split text into ~50,000–100,000 sub-word tokens and map each token to a vector of thousands of numbers where geometric direction encodes meaning.
- 2.Self-Attention (Query · Key · Value): Every token broadcasts a Query ('what context am I looking for?') and a Key ('what do I represent?'); their dot product routes exact context from relevant earlier words in a single step.
- 3.Feed-Forward MLP Layers (~2/3 of parameters): While Attention routes context between tokens, dense Multi-Layer Perceptron blocks store factual associations and nonlinear transformations.
- 4.Training vs. Inference Bottleneck: Training adjusts weights via backpropagation (calculus chain rule) to minimize prediction error; live generation is bottlenecked by moving Key-Value (KV) caches across GPU memory — solved by breakthroughs like FlashAttention and Multi-Head Latent Attention (MLA).
FIRST BIT — Build the Silicon Logic Underneath Every GPU
First-Principles Mechanism (Step by Step)
1. From Words to Geometry (Embeddings)
Before any reasoning happens, input text is broken into tokens (roughly 3/4 of an English word on average). Each token ID looks up a row in an embedding matrix — a list of e.g. 4,096 floating-point numbers. During training, words that play similar roles migrate to nearby directions: the vector shift from 'Paris' to 'France' mirrors the shift from 'Tokyo' to 'Japan'.
2. Multi-Head Self-Attention (Context Routing)
A static embedding for the word 'mole' cannot know whether a sentence discusses chemistry, a garden animal, or espionage. In an Attention head, matrices project each token into a Query (Q), Key (K), and Value (V). Computing softmax(Q·Kᵀ / √d) produces an attention map telling 'mole' how strongly to absorb the Value vector of 'atoms' or 'burrow'. Running dozens of heads in parallel tracks grammar, coreference, and logic simultaneously.
3. Deep MLP Layers & Residual Streams
After Attention mixes context across positions, each token passes through a Feed-Forward Network (linear expansion -> nonlinear activation like SwiGLU -> linear projection back to the residual stream). Think of the residual stream as a shared conveyor belt: 60 to 120 layers sequentially read from it, refine hypotheses, and add sharp features before the final unembedding matrix scores every token in the vocabulary.
4. Backpropagation & Modern Memory Engineering
Nobody hand-codes grammar rules. Over trillions of tokens, backpropagation computes the exact partial derivative of prediction loss with respect to every single weight. At deployment scale, arithmetic is so fast on Tensor Cores that waiting on HBM memory dominates latency — which is why tiling SRAM reads (FlashAttention) and compressing Key-Value caches (Multi-Head Latent Attention) unlocked 10x longer, faster reasoning.
Common Myths vs. Verified Reality (Audited by SkepticJudge)
In a Transformer, what is the primary job of the Query-Key dot product inside Self-Attention?
Verified Primary Lectures & Sources (90+ Brain-Score)
Browse All 10,534 Sources →Attention in transformers, step-by-step | Deep Learning Chapter 6
- • Self-attention maps high-dimensional token embeddings into Query and Key vectors whose dot products score contextual alignment.
- • Softmax-normalized attention weights blend factored Value down- and up-projection vectors to shift token embeddings toward fuller meaning.
How might LLMs store facts | Deep Learning Chapter 7
- • Transformer MLP blocks process each token vector independently through two learned matrix projections and a non-linear clip.
- • Rows of the up-projection matrix act like soft AND-gates that fire when input vectors encode combined entity features.
How FlashAttention Accelerates Generative AI Revolution
- • Standard self-attention bottlenecks on slow GPU HBM bandwidth by repeatedly reading and writing the quadratic N×N attention matrix.
- • Online Softmax tracks running block maximums with a rescaling recurrence, fusing two-pass normalization into a single streaming loop.
How DeepSeek Rewrote the Transformer [MLA]
- • Standard multi-head attention stores distinct key and value matrices per head, creating a massive KV-cache memory bottleneck.
- • Multi-Head Latent Attention projects keys and values into a shared low-rank latent space rather than dropping head diversity.
Ein neuronales Netz ist kein magisches Bewusstsein, sondern eine riesige mathematische Funktion aus Milliarden drehbarer Regler (Gewichte). Ein Sprachmodell (Transformer) übersetzt Textteile in Richtungs-Vektoren, verknüpft deren Bedeutung parallel über den ‚Attention‘-Mechanismus und berechnet für jedes mögliche nächste Wortfragment eine exakte Wahrscheinlichkeit.
- 1.Tokens & Vektoren: Sprachmodelle lesen keine Buchstaben, sondern zerlegen Text in rund 50.000–100.000 Wortbausteine (Tokens) und ordnen jedem einen Zahlenvektor zu, dessen Richtung im Raum Bedeutung codiert.
- 2.Self-Attention (Query · Key · Value): Jedes Token stellt eine Suchanfrage (Query) an die Schlüssel (Keys) aller vorherigen Wörter — so erkennt das Modell sofort, ob ‚Bank‘ ein Geldinstitut oder eine Parkbank meint.
- 3.MLP-Schichten (ca. 2/3 aller Gewichte): Während Attention Informationen zwischen Wörtern austauscht, speichern die dichten Feed-Forward-Netze Faktenmuster und logische Zusammenhänge.
- 4.Training vs. Speicher-Wand: Beim Training verschiebt der Gradientenabstieg Milliarden Gewichte minimal in Richtung kleinerer Vorhersagefehler; beim Antworten bremst vor allem der Speichertransfer (KV-Cache), wofür Techniken wie FlashAttention und MLA entwickelt wurden.
FIRST BIT — Baue die Silizium-Logik unter jeder KI-GPU selbst
Wie es Schritt für Schritt wirklich funktioniert
1. Von Wörtern zu Geometrie (Embeddings)
Bevor gerechnet wird, zerlegt ein Tokenizer den Satz in Wortstücke. Jedes Token erhält einen Vektor aus z. B. 4.096 Kommazahlen. Im Laufe des Trainings ordnen sich verwandte Begriffe geometrisch an: Der Richtungsvektor von ‚Berlin‘ nach ‚Deutschland‘ entspricht nahezu exakt dem Vektor von ‚Paris‘ nach ‚Frankreich‘.
2. Multi-Head Self-Attention (Kontext-Verknüpfung)
Das isolierte Wort ‚Schloss‘ weiß noch nicht, ob es um ein Gebäude oder ein Türschloss geht. Im Attention-Kopf erzeugt jedes Token drei Vektoren: Query (Suchfrage), Key (Merkmal) und Value (Inhalt). Das Skalarprodukt aus Query und Key misst in Millisekunden, welche Wörter zusammengehören, und mischt den passenden Kontext direkt in den Vektor ein.
3. Tiefe MLP-Netze & der Residual-Strom
Nach dem Kontext-Austausch durchläuft jedes Token ein mehrschichtiges Perzeptron (MLP mit nichtlinearer Aktivierung wie SwiGLU). Man kann sich den Prozess wie ein Fließband vorstellen: Über 60 bis 120 Schichten hinweg lesen Attention- und MLP-Blöcke Zwischenergebnisse aus, schärfen Zusammenhänge und berechnen am Ende für jedes Vokabel-Token einen Logit-Wert.
4. Backpropagation & moderne Speicher-Architektur
Kein Mensch programmiert Grammatikregeln von Hand: Beim Training auf Billionen Wörtern berechnet Backpropagation (Kettenregel der Analysis) für jedes Gewicht, in welche Richtung der Vorhersagefehler sinkt. Im Live-Betrieb rechnen moderne GPUs schneller, als Daten aus dem Speicher nachfließen — erst Innovationen wie FlashAttention (SRAM-Tiling) und komprimierte KV-Caches machten lange Kontexte bezahlbar.
Häufige Irrtümer vs. geprüfte Realität (SkepticJudge)
Welche Hauptaufgabe hat das Query-Key-Skalarprodukt im Self-Attention-Mechanismus?
Geprüfte Primär-Vorträge im DeepScroll-Index
Alle 10.534 Quellen →Attention in transformers, step-by-step | Deep Learning Chapter 6
- • Self-Attention projiziert Token-Embeddings in kleinere Query- und Key-Vektoren, deren Skalarprodukte kontextuelle Bezüge präzise messen.
- • Per Softmax normierte Gewichte kombinieren faktorisierte Value-Projektionen, um die Bedeutung jedes Tokens im Satzkontext zu verfeinern.
How might LLMs store facts | Deep Learning Chapter 7
- • MLP-Blöcke im Transformer verarbeiten jeden Token-Vektor parallel durch zwei Matrixprodukte und eine Nichtlinearität.
- • Zeilen der Aufwärtsprojektion wirken wie logische UND-Gatter, die bei kombinierten Begriffsmerkmalen im Vektor feuern.
How FlashAttention Accelerates Generative AI Revolution
- • Klassische Self-Attention wird durch langsame GPU-HBM-Speicherzugriffe beim Lesen und Schreiben der N×N-Aufmerksamkeitsmatrix gebremst.
- • Online Softmax nutzt laufende Teilmaxima und Neuskalierung, um die Normalisierung in einem einzigen Streaming-Durchlauf zu vereinen.
How DeepSeek Rewrote the Transformer [MLA]
- • Klassische Multi-Head-Attention speichert eigene Key- und Value-Matrizen pro Kopf und erzeugt enorme KV-Cache-Engpässe.
- • Multi-Head Latent Attention komprimiert Keys und Values in einen gemeinsamen niedrigdimensionalen latenten Vektorraum.
Connected AI-Wiki Articles
Built on 1 laptop + specialist AI agents + learner donations to keep knowledge 100% ad-free forever.