Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

πŸ“– ⏱️ ~40 min read 🎯 Intermediate

Large Language Model (LLM) Foundations

πŸ“ Before You Continue: You should first read the Decoder-Only architecture and self-attention in 6.2. This section focuses on "how LLMs are trained," laying the groundwork for later migrating this pipeline to recommendation.

The evolution from Transformer to LLM is not just growth in parameter scale β€” more importantly, it is the systematization of the training paradigm. Modern LLMs (GPT-3/4, LLaMA, etc.) developed a complete "pretrainingβ€”instruction tuningβ€”preference alignment" three-stage training pipeline, so models can generate fluent text while also understanding instructions and following human intent.

But applying LLMs to recommendation is not a matter of "plugging in" off-the-shelf language models β€” you must understand the modeling principles and adapt/optimize for recommendation scenarios. This section systematically introduces the basic LLM modeling pipeline, focusing on the technical links most relevant to generative recommendation.

After reading this chapter, you will be able to:

  • Explain the goals and losses of each LLM stage (pretraining / instruction tuning / preference alignment)
  • Distinguish the pipeline differences between RLHF (with reward model and PPO) and DPO
  • Explain what Scaling Laws and emergent abilities imply for generative recommendation
  • Map the three-stage paradigm to recommendation scenarios and identify challenges specific to it, such as item tokenization
  • Complete 4 tiered practice problems to consolidate the LLMβ†’recommendation knowledge chain

6.3.0 Overview of the Three-Stage LLM Paradigm

Current mainstream LLMs follow the "pretrainingβ€”instruction tuningβ€”preference alignment" three-stage paradigm, first systematized in InstructGPT and widely adopted by GPT-4, Claude, and LLaMA. The three stages have progressive goals and together form a complete capability-building system.

LLM three-stage post-training pipeline: SFT β†’ RM β†’ PPO

  • Step 1 (SFT): collect human demonstration data for supervised fine-tuning, so the model initially learns to follow instructions.
  • Step 2 (RM): collect comparison data to train a reward model that automatically evaluates output quality.
  • Step 3 (PPO): with the reward model as feedback, use reinforcement learning to continually optimize the generation policy, with a KL divergence constraint to prevent drifting too far from the reference model.

🧠 Mental Model: From "Autocomplete Writer" to "Assistant"

A pretrained LLM is just a "text continuation engine" β€” give it an opening and it naturally keeps writing, without knowing "what you want it to do." Instruction tuning is like onboarding training (teaching it to understand task instructions); preference alignment is like values calibration (teaching it what a better answer is). Only after all three steps does it turn from a "completion tool" into a "reliable assistant."


6.3.1 Pretraining and Instruction Tuning

Pretraining: the Foundation of Language Ability

Pre-training is the first stage and the most compute-intensive. The goal is to learn general language representation and generation ability on large-scale unlabeled text, relying entirely on self-supervised learning (the data itself provides the signal; no manual annotation needed).

Training objective: causal language modeling (CLM), also known as Next Token Prediction:

where . By maximizing this likelihood, the model masters the statistical regularities of language, grammar, semantics, and even commonsense reasoning.

When model scale and data scale reach a certain level, Scaling Laws emerge: performance keeps improving with parameter count, data volume, and compute, and Emergent Abilities such as zero-shot/few-shot learning may even appear.

Analysis: Most modern LLMs use Decoder-Only architectures (GPT/LLaMA) β€” clean, efficient, and well-suited to large-scale training. Parameters range from billions to trillions (GPT-3 175B, PaLM 540B, LLaMA-2 7B–70B, GPT-4 estimated over 1T). Pretraining needs thousands to tens of thousands of GPUs/TPUs for weeks to months at extreme cost β€” so most teams fine-tune directly on open pretrained models (LLaMA, Mistral).

Instruction Tuning: Following Instructions

A pretrained model only "completes text" β€” it does not "understand and execute instructions." Instruction Tuning, also called Supervised Fine-Tuning (SFT), addresses "making the model understand task instructions and generate accordingly."

The core is constructing "instructionβ€”inputβ€”output" triples, for example:

Instruction: Summarize the main content of the following passage.
Input: [a passage about the history of artificial intelligence]
Output: [Artificial intelligence started in the 1950s ... and has gone through several stages of development ...]

Training objective: conditional language modeling loss, computed only on the output portion:

where is the conditioning information (instruction + input) and is the target output. Key point: the loss is computed only on output tokens β€” instruction and input do not participate in gradient updates. Either full fine-tuning or parameter-efficient methods (e.g., LoRA) can be used. SFT models significantly outperform pure pretrained models on zero-shot/few-shot tasks β€” they have learned the meta-ability of "understanding instructions."


6.3.2 Preference Alignment and From LLM to Recommendation

Preference Alignment: RLHF and DPO

Even after instruction tuning, LLM outputs can still be insufficiently helpful, hallucinated, or unsafe. The root cause is that SFT only learns "how humans would answer," without optimizing "which answer is better." Preference Alignment makes outputs better match human values and preferences.

RLHF (Reinforcement Learning from Human Feedback) proceeds in three steps:

  1. Collect preference data: for the same prompt, the model generates multiple outputs; human annotators rank them, yielding preference pairs ( chosen, rejected).
  2. Train the reward model (RM):

  1. Policy optimization (PPO): maximize the reward while constraining deviation from the reference model with KL divergence:

RLHF/PPO pipeline: preference data β†’ reward model β†’ policy optimization

DPO (Direct Preference Optimization) is more concise: its core idea is that "the reward model can be represented implicitly by the policy model itself" β€” no explicit RM training and no reinforcement learning:

DPO training resembles supervised learning β€” simple and stable, often matching or exceeding RLHF, and widely adopted recently.

Mapping the Three-Stage Paradigm to Recommendation

The LLM's three stages provide a complete capability framework for recommendation, but each stage needs repositioning:

Mapping the LLM three stages to generative recommendation

LLM StageRecommendation Adaptation DirectionCore Challenge
PretrainingUser behavior sequence pretraining, multimodal content pretrainingHow to represent items? How to balance language ability and recommendation ability?
Instruction tuningInstructionalizing recommendation tasks, multi-task joint trainingHow to design recommendation instructions? How to handle ID-based items?
Preference alignmentImplicit feedback alignment, business metric optimizationHow to construct preference data? How to balance multiple objectives?
  • Pretraining: the core is "letting the model master both language understanding and recommendation modeling." Overemphasizing language neglects collaborative signals; over-focusing on behavior weakens semantics β€” a balance is needed: for content items (news/video), language ability matters more; for collaboration-rich domains (e-commerce/music), behavior modeling matters more.
  • Instruction tuning: the difficulty is that items exist as IDs, which are completely alien symbols to a language model. These IDs must be "translated" into semantic representations the model understands β€” this is exactly the core of item tokenization, the key bridge connecting traditional recommendation data and generative models (see Section 6.4).
  • Preference alignment: recommendation feedback is mostly implicit (clicks, watch time, skips), and objectives are often multi-dimensional (CTR, retention, ecosystem health). Constructing effective preference signals from implicit feedback and balancing multiple metrics is subtler than in LLMs.

Challenges Specific to Recommendation Scenarios

Beyond adapting the three stages, generative recommendation must face four families of challenges rarely seen in the LLM domain:

  1. Item tokenization: natural language tokens carry semantics by construction; recommendation item IDs are abstract numbers, meaningless to the model. How to inject semantics and characterize similarity between IDs? β€” the core topic of Section 6.4.
  2. Collaborative signal fusion: "users who bought A also buy B" cannot be obtained from textual descriptions; careful design is needed to inject collaborative signals into generative architectures.
  3. Cold start: new items/new users lack interactions; generative models can leverage LLM semantic understanding to build capability quickly from content features, but the model must be trained to adaptively switch β€” "collaboration when interactions exist, content when they don't."
  4. Real-time constraints: online services often must respond within tens of milliseconds; autoregressive token-by-token generation can take hundreds of milliseconds. Inference optimizations (quantization, KV Cache, speculative decoding) and system-level innovations (hybrid architectures, offline-online combination, caching) are needed.

πŸ’‘ Key Insight: Generative recommendation is not "wrapping a language model around recommendation" β€” it is reconceptualizing recommendation as a sequence generation problem and deeply adapting to recommendation's unique characteristics. It borrows the successful LLM paradigm while creatively solving recommendation-specific challenges β€” this chain of knowledge is the foundation for later chapters (Scaling architectures, end-to-end generation, thinking recommenders, diffusion models).


⚠️ Common Mistakes in 6.3

#MistakeExampleWhy It's WrongFix
1Computing the SFT loss on all tokensInstructions also get gradientsSFT computes the loss only on outputs; input/instruction are conditionsLoss applies only to
2Assuming RLHF needs no reference modelJust maximize the reward directlyThe model learns to "game" the reward model; quality degradesAdd a KL constraint toward
3Confusing RLHF and DPO complexity"Both need a reward model"DPO represents the reward implicitly; no explicit RM/RL neededDPO training resembles supervised learning
4Applying the LLM vocabulary to items directly"Encode products with an off-the-shelf tokenizer"Item IDs are alien symbols to an LLMItem tokenization is required (see Section 6.4)
5Ignoring multi-objective preference alignment in recommendation"CTR as the reward is enough"Implicit feedback + multiple objectives need careful constructionHandle multi-objectives and implicit signals explicitly

Chapter Summary

πŸ“Œ Key Takeaways

ConceptKey PointsWhy It Matters
Pretraining CLMThe foundation of general generation ability; Scaling Law emergence
Instruction tuning SFTConditional language modeling; loss only on outputsFrom "completion" to "following instructions"
RLHFRM + PPO + KL constraintValue alignment, but a complex pipeline
DPOImplicit reward; resembles supervised trainingSimple and stable; the recent mainstream
Recommendation mappingThree stages β†’ behavior pretraining / task instructionalization / implicit alignmentEach stage needs repositioning
Four challengesTokenization / collaboration / cold start / real-timeDetermines whether research can reach production

❓ FAQ

Q1: Why is DPO simpler than RLHF yet often more effective?

A: DPO merges "training a reward model + reinforcement learning" into one step β€” the reward is represented implicitly by the ratio of policy to reference model, training looks like ordinary supervised learning, and it avoids RL's instability and the extra RM.

Q2: Why is preference alignment harder in recommendation?

A: LLMs have explicit human preference rankings; recommendation feedback is mostly implicit behavior (clicks/skips), objectives are multi-dimensional and often conflict, so constructing the "what is better" signal is subtler.

Q3: What do Scaling Laws mean for recommendation?

A: Like LLMs, generative recommendation models keep improving with parameters/data/compute β€” which supports the "stacking is scaling" claim of [6.2] and the later Scaling chapters.

πŸ”— Connections to Later Chapters

  • The Decoder-Only architecture of 6.2 (architectural foundations) is exactly the main architecture for LLM pretraining.
  • 6.4 (Codebook Quantization) solves the "item tokenization" bridge problem raised repeatedly in this section.
  • 8.x (End-to-end Generation) implements the three-stage paradigm in recommendation training pipelines.
  • 9.x (Thinking Recommenders) deepens the combination of preference alignment and reasoning-style generation.

Practice Problems

Work through all problems in order β€” they get progressively harder. Each has a complete solution you can reveal after trying it yourself.


Problem 6.3.1 β€” Scope of the SFT Loss 🟒 Easy

An instruction tuning sample: instruction "Translate into English", input "Bonjour le monde", output "Hello world". If the output is tokenized into 2 tokens, which tokens should the training loss cover? Do the instruction and input tokens participate in gradient updates?

πŸ’‘ Solution (click to reveal)

Answer: The loss covers only the output tokens Hello and world (2 tokens), computing at each position. The instruction "Translate into English" and the input "Bonjour le monde" serve as conditions and do not participate in gradient updates β€” the model learns only "given instruction + input, how to generate the correct output."

Key points:

  • Conditional language modeling: conditions are fixed; the loss applies only to outputs.
  • This is the key difference between SFT and pretraining CLM.

Problem 6.3.2 β€” Reward Model Loss 🟒 Easy

For a preference pair , the reward model gives . Compute the RM loss term and explain what it encourages.

πŸ’‘ Solution (click to reveal)

Approach: Substitute values.

; ; loss term .

Answer: This small loss (near 0) indicates the reward model already scores higher. The RM loss overall encourages "giving higher reward scores to better outputs," enabling the RM to automatically evaluate the quality of any output.

Key points:

  • compresses score differences into a probability.
  • The RM learns "relative better/worse," not absolute scores.

Problem 6.3.3 β€” RLHF vs. DPO 🟑 Medium

Briefly describe the differences between RLHF and DPO on three aspects: "whether an explicit reward model is needed," "whether reinforcement learning is used," and "training stability."

πŸ’‘ Solution (click to reveal)

Answer:

DimensionRLHFDPO
Explicit reward modelNeeded (train an RM separately)Not needed (reward represented implicitly by the policy/reference ratio)
RL usedUses PPO reinforcement learningNo β€” training resembles supervised learning
Training stabilityLower (RL is unstable, easy to game the RM)Higher (no RL, no separate RM)

Key points:

  • DPO replaces RM + RL with the reference model .
  • DPO has recently been favored for being simple, stable, and comparably effective.

πŸ† Challenge: Designing Preference Alignment for Recommendation

A music app wants to optimize recommendations with preference alignment but has only implicit signals (play completion rate, favorites, skips). In about 150 words, explain: how would you construct preference pairs from implicit behavior? Which business objectives must be balanced (list at least 2)? And state the essential difference from LLMs' explicit rankings.

πŸ’‘ Hint

Construction: generate multiple candidate sequences for the same user and context, and define quality via implicit signals β€” e.g., with high completion rate and a favorite; with many skips / low completion. Objectives to balance: user retention, content ecosystem health (diversity/long tail). Essential difference: LLMs have explicit human rankings, while recommendation infers preferences from behavioral proxies β€” noisier, with often-conflicting multi-objectives requiring weighting, not a plain "good/bad binary classification."