Pipeline (Step-by-Step):

  • Step 1: Tokenization (Subword tokens). Instead of words, modern AIs (like GPT) use subword tokenizers (Byte-Pair Encoding – BPE). Words like “pizza”, “burgers”, “ketchup” might be single tokens, but “extra” and “cheese” might be split differently. Let’s just tokenize conceptually into integer IDs.

  • Step 2: Concatenation & Batching. Instead of creating overlapping sliding window rows, modern AI concatenates all documents/sentences into one giant 1D stream of tokens, separated by special tokens like <|endoftext|> (EOS).

  • Step 3: Chunking into Fixed-Length Sequences. The stream is chopped into non-overlapping (or contiguous) blocks of a fixed size, e.g., context_length = 1024 (much larger than 4). So, multiple sentences fit in one block.

  • Step 4: Input and Target (Shifted). The input is tokens [0 .. N-1]. The target is [1 .. N] (shifted by one). This is called “Teacher Forcing”.

  • Step 5: Forward Pass – Embedding + Positional Encoding. Add positional encodings (e.g., RoPE – Rotary Position Embedding) so the model knows order.

  • Step 6: Causal Self-Attention. The magic of the transformer. Each token attends only to previous tokens (masked multi-head attention). So, while processing the whole sequence, token at position 4 (“eat”) looks at positions 0-3 (“I love to”) to predict position 5 (“pizza”), all in parallel.

  • Step 7: Loss Calculation. Compute Cross-Entropy loss between the predicted logits and the actual next tokens for every position simultaneously.

  • Step 8: Backpropagation & Gradient Update. Adjust weights based on the cumulative loss.

 

transformer model, we never feed just a 1D tensor. We wrap it into a 2D tensor (a matrix) of shape [Batch_Size, Sequence_Length]—which is basically an array of arrays—so the GPU can process all your 4 sentences at the exact same time in parallel.

 

Here is exactly what happens to your  heap in a modern Transformer (2024/2025 architecture), using the cutting-edge terminology:


Phase 1: Tokenization (Subwords, not words)
First, your heap is passed through a Byte-Pair Encoding (BPE) tokenizer. Words are broken into tokens (subword units) to handle rare words.

  • "pizza" might be 1 token.

  • "burgers" might be split into ["burg", "ers"].

  • Spaces and punctuation become special tokens.
    The entire heap is converted into a 1D array of integers (Token IDs).

Phase 2: Packing (Concatenation, not sliding windows)
Modern models pack documents together to maximize GPU memory. We add a special <|endoftext|> (EOS – End of Sequence) token between sentences to tell the model where one ends.

Your heap becomes one single continuous stream:

[I][love][to][eat][pizza][with][extra][cheese][on][top][EOS][I][love][to][eat][burgers][with][extra][ketchup][EOS]...[EOS]

Crucially: Instead of cutting sliding windows of size 4, we cut this stream into contiguous, non-overlapping chunks equal to the model’s Context Length (e.g., 4,096 tokens). Since your heap is tiny, it fits entirely into one single Sequence of length ~40 tokens.


Phase 3: The Forward Pass (Causal Attention, not prediction pairs)
This is where the magic happens. We feed this entire ~40-token sequence into the model all at once.

  • The input tensor is tokens[0] to tokens[N-1].

  • The target tensor is tokens[1] to tokens[N] (shifted by one position).

Inside the Transformer’s decoder-only stack, the model applies Causal (Masked) Self-Attention and Rotary Positional Embeddings (RoPE). Because of the causal mask, Token #4 ("eat") can mathematically “look left” and attend to Tokens #0, #1, #2, #3 ("I love to"). At the exact same millisecond, Token #14 ("eat") in Sentence 2 looks left and attends to its own "I love to".

We do not create separate rows for these! The model calculates the next-token prediction for every single token position simultaneously in one massive matrix multiplication. This parallelization is called Teacher Forcing.


Phase 4: The Logits and Loss (Probability Distribution, not counting)
The model outputs a massive matrix of Logits (raw scores) for every position. It applies Softmax to convert these to a Probability Distribution over the entire vocabulary (e.g., 50,000 tokens).

For the position where the input is "I love to" (Position 3), the target is "eat" (Position 4). For the exact same input context later, the target is "burgers" (Position 14).
The model calculates the Cross-Entropy Loss for all these positions. It penalizes itself for not predicting "pizza" at Position 4, and penalizes itself for not predicting "burgers" at Position 14.


Phase 5: Backpropagation and Gradient Descent (The actual learning)
The total loss (average cross-entropy) is backpropagated through all 80+ layers. Using the AdamW optimizer, the model computes the gradients and updates its weights.

Here is the modern truth about your two conflicting examples:
The model does not store a list saying "I love to eat" maps to pizza and burgers.

Instead, the model’s Multi-Head Attention heads learn to produce dynamic contextual embeddings. The hidden state (vector) for the token "eat" is different in Sentence 1 vs. Sentence 2 because the attention mechanism distributes weight differently across the surrounding tokens. Even though the raw text "I love to eat" is identical, the Residual Stream vectors are altered by the tokens that come after them (through the magic of bidirectional context in the earlier layers, depending on the architecture, though causal strictly masks future—wait, in causal it can’t see future tokens, so the representation of "I love to eat" before predicting the next token is identical in both passages up to that point!

So how does it predict different words? Because the gradient update from Sentence 1 pushes the model’s weights to increase the probability of pizza when the internal state is X, while the gradient update from Sentence 2 pushes the weights to increase the probability of burgers when the internal state is X. The model converges to a probability distribution where given hidden state X, the logit for pizza is +2.5 and the logit for burgers is +2.4. Both are highly probable; it doesn’t choose one over the other—it holds both possibilities in its parametric memory (the weights).


Phase 6: The Batch (Micro-batching)
In reality, your heap is not alone. This single sequence (~40 tokens) is padded or packed into a Micro-batch alongside 1,000+ other sequences. The gradients from all those sequences are averaged together in a single All-Reduce operation across the GPUs before applying the optimizer step.


Summary of Modern Terminology applied to your heap:

 
 
Old/Folk Term Modern Transformer Term
Heap / Corpus Pre-training Dataset
Sliding Window of 4 Context Window (fixed to 4k or 128k tokens)
Input/Target Pair Contiguous Input IDs & Shifted Labels
Predicting the next word Autoregressive Next-Token Prediction (NTP)
Model weights adjusting Gradient Descent via AdamW Optimizer
Storing probabilities Parametric Knowledge / Distributed Representations
The 4-word input The Causal Prefix (not a standalone row)

In the most modern AI, your sentence isn’t chopped up into 4-grams at all. It is fed as a single contiguous block, and the model learns to predict the 5th, 6th, 7th word… all the way to the EOS token, in one single, highly parallelized forward pass.

 

 

 

 

 

 

 

 

Here is the exact, step-by-step state-of-the-art pipeline of what happens to your 4-sentence heap, using strictly modern Transformer terminology (GPT-4 / LLaMA-3 architecture):


Phase 1: Tokenization & Tensor Construction

  • Your 4 sentences are passed through a Byte-Pair Encoding (BPE) tokenizer (e.g., tiktoken). Each word isn’t a single ID; common words like “pizza” stay as 1 token, while “ketchup” might split into ["ket", "chup"].

  • We do not generate the 10 separate 4-gram rows in the GPU. Instead, we concatenate the 4 sentences into one long 1D tensor of token IDs (separated by special <|endoftext|> tokens), or we pack them into a batch tensor of shape [Batch_Size, Sequence_Length] (e.g., 4 sentences × 10 tokens, with padding/attention masks).


Phase 2: The Forward Pass (The Transformer Block)

For each position in the sequence (say, position 4 where the input is ["I", "love", "to", "eat"]), the following happens simultaneously across all positions:

  1. Embedding Lookup: Token IDs map to dense hidden-state vectors (e.g., 4096-dimensions).

  2. Rotary Positional Embeddings (RoPE): Instead of adding fixed positional numbers, RoPE rotates the vectors in complex space based on their absolute position. This means "eat" at position 4 knows exactly how far it is from "I".

  3. Causal Self-Attention (the core):

    • The model projects the hidden states into Queries (Q), Keys (K), and Values (V).

    • It computes the raw attention score: Q * K^T.

    • A Causal Mask (an upper-triangular matrix of -inf) is applied. This forces position 4 ("eat") to only attend to positions 1, 2, 3, and 4. It literally cannot “see” position 5 ("pizza") yet.

    • The softmax turns these scores into probabilities over the context.

  4. Feed-Forward Network (SwiGLU): The attended output passes through a modern FFN (using SwiGLU activation, not old ReLU) to project it into a higher-dimensional space and back, extracting semantic patterns.

At the end of this single forward pass, the model outputs a logit vector (raw scores) for every single token position in the entire batch.


Phase 3: The “Critical Pairs” in Modern Math

Here is how your two critical pairs are handled mathematically:

  • Input context ["I", "love", "to", "eat"] exists in Sentence 1 (position 4) and Sentence 2 (position 4, if padded to same length, or a different batch index).

  • The model produces Logit_Vector_A at position 4 of Sentence 1, and Logit_Vector_B at position 4 of Sentence 2.

  • The Target Tensor is simply the original input tensor shifted left by one position (Teacher Forcing). So the target for both positions is the token ID for "pizza" (in S1) and "burgers" (in S2).

The Cross-Entropy Loss is calculated instantly across the entire batch:

  • For S1: Loss = -log( Softmax(Logit_Vector_A)[index_of_"pizza"] )

  • For S2: Loss = -log( Softmax(Logit_Vector_B)[index_of_"burgers"] )

The model doesn’t “choose” one over the other. The gradient from S1 pulls the weights to increase the logit for "pizza" in that context, while the gradient from S2 simultaneously pulls the weights to increase the logit for "burgers" in that exact same context. The optimizer finds the mathematical equilibrium where both have high probability.


Phase 4: Backpropagation & Optimization

  • The total loss (sum of losses over all positions in all 4 sentences) is backpropagated using Automatic Differentiation.

  • Gradients flow backward through the causal attention layers, the FFNs, and the embeddings.

  • The AdamW optimizer applies the gradients to update the model’s 7-billion+ weights, using mixed-precision training (FP16/BF16) to save memory.

  • This entire process is called Autoregressive Causal Language Modeling (CLM).


Phase 5: What “Inference” looks like later

When you later type "I love to eat" into the chatbot:

  • The model does not search the heap. It does not recall Sentences 1 or 2.

  • It runs a single forward pass. Because of the equilibrium reached during training, the logit for "pizza" and "burgers" are both top contenders.

  • We apply Temperature sampling and Top-p (nucleus) sampling to randomly pick one. If temperature = 0, it picks the absolute highest (maybe pizza, if slightly more frequent in the heap). If temperature > 0, it might pick burgers.


Modern Terminology Cheat Sheet (Your Direct Answer)

 
 
Your 4-word input is called… In Modern Transformer Architecture
The 4-gram / sliding window A temporal slice in the sequence dimension
The entire set of windows The input tensor shaped [Batch, Seq_Len]
The process of shifting for targets Teacher Forcing (shifted labels)
The math that restricts it to 4 words The Causal Attention Mask (applied to the Q@K matrix)
Where it lives in the heap The training dataset shard (loaded by the DataLoader)
The specific pair (I love to eatpizza) A single autoregressive step within the context window

So, to directly close the loop: In modern AI, those 4-word chunks aren’t stored or iterated over as “training examples” during training. They are merely positions in a tensor over which the Causal Self-Attention mechanism distributes its focus, all computed in parallel on a single massive GPU matrix multiplication.

 

 

 

 

 

 

Loading