M5-038M5: NLP & Large Language ModelsAlignment & RLHFHard
⚡Flashcard Drill➔Mastery:
On-Policy Sampling vs. Off-Policy Data in RLHF
📐 Mathematical Definition
⚡ One-Sentence Core Answer
RLHF requires on-policy sampling so that policy updates correspond strictly to the current distribution of generated tokens, while off-policy buffer reuse introduces severe distribution shifts and advantage estimation bias.
📌 Key Takeaways
- •On-policy requirement: policy gradient theorems require expectations taken over trajectories generated by the current policy:
- •Off-policy drift: if rollouts are collected from an older policy and reused for multiple PPO epochs, importance sampling weights explode or vanish, destabilizing updates
- •Engineering tension: on-policy sampling requires continuous, expensive generation rollouts during training, making RLHF to slower than offline supervised learning
📐 Mathematical Derivations
Mathematical Mechanism: 1. Importance Sampling Correction: When evaluating policy using trajectories sampled from behavior policy (off-policy): In language models where , the cumulative importance weight is a product of hundreds of ratios: If drifts slightly from , suffers from exponential variance explosion: either collapsing to zero for almost all samples or exploding to infinity on a single outlier sample, destroying gradient updates. 2. PPO Off-Policy Epoch Horizon: PPO allows mild off-policy reuse by collecting a batch with , training for 1-4 gradient epochs with clipping (), and immediately discarding the batch to collect fresh on-policy samples.
🏭 Production Trade-offs
Deep Dive & Engineering Trade-offs: ① Rollout Freshness vs GPU Throughput: To maximize throughput, engineers want to train on a generated batch for many epochs. In LLMs, training for epochs on the same rollout batch leads to immediate policy collapse due to importance weight divergence. Batches must be refreshed constantly. ② Decoupled Asynchronous Rollouts: Frameworks like Ray-RLHF run dedicated inference engines (vLLM) that continuously generate rollouts into a shared queue while trainer nodes pull the freshest rollouts, overlapping generation and training without falling behind on-policy freshness. ③ Contrast with DPO (Offline Off-Policy): DPO is fully offline: it trains on static preference datasets without any on-policy generation rollouts. However, offline DPO suffers from distribution shift because the student never learns to correct its own generation errors (addressed by Online DPO). ④ Experience Replay Buffers in RLHF: Traditional RL experience replay buffers (storing samples across thousands of past steps) fail completely in LLM RLHF due to the exponential length product . Replay buffers are strictly restricted to 1 step. ⑤ Interview Strategy: Write down the cumulative sequence importance sampling formula , explain why variance explodes exponentially with sequence length , and justify why PPO restricts batch reuse to 1-2 epochs.
⚠️ Common Interview Pitfalls
- ✕Attempting to reuse rollout data across dozens of PPO training epochs to save compute (causes immediate divergence)
- ✕Using a traditional deep Q-learning experience replay buffer for full autoregressive LLM trajectories
- ✕Assuming DPO is on-policy (standard DPO is completely offline and off-policy)
🎯 Interviewer Follow-up Inquiries
- ?Why does the product of token-level importance weights make multi-epoch off-policy training practically impossible in LLMs?
- ?How does Online / Iterative DPO bridge the gap between offline simplicity and on-policy freshness?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
解释 RLHF 的 on-policy 采样与 off-policy 数据。
PPO 需 on-policy 采样(当前策略生成)保证重要性采样正确;RM 训练常用 off-policy 数据;迭代式 RLHF 混合两者。
核心要点
- 数学机理: on-policy vs off-policy 的含义 ——on-policy 指'用 当前策略 生成的数据'更新策略;off-policy 指'用 其他策略 (如旧策略、人类、其他模型)生成的数据'。 为什么 PPO 需 on-policy ——PPO 的目标含重要性采样比 r=π_θ/π_old,其 无偏性 要求样本来自 π_old(采样时的策略);若样本来自很旧的策略,则 r 的方差会爆炸(因为 π_θ 与采样策略差异大)。故 PPO 每轮采样后只能做 有限次 更新(受 clip 限制),然后必须 重新采样 (这就是'on-policy、采样昂贵'的来源)。 RM 训练可用 off-policy ——奖励模型是 监督学习 (拟合偏好标签),不依赖重要性采样,故可用 任意来源 的偏好数据:人类标注的(来自多种模型/人工)、历史策略的、其他模型的;这使 RM 训练更省(可离线、可复用数据)。 但 on-policy 数据对 RM 更有效 ——因为 RM 需在'策略会访问的区域'可靠;若 RM 只用'旧策略/人类'的数据训练,而新策略探索到新区域,RM 在那里不可靠(导致过优化)。故 iterative/online RLHF 用 最新策略的输出 做偏好标注来更新 RM(近似 on-policy),使 RM 的可靠区域'跟随'策略。 三者的组合 ——(a) PPO :严格 on-policy 采样(每轮重新生成);(b) RM 初始训练 :off-policy(用现有偏好数据);(c) RM 迭代更新 :on-policy(用最新策略输出 + 人工/AI 标注)。 成本权衡 ——on-policy 更有效但更贵(需实时采样 + 实时标注);off-policy 便宜但可能失配。实践常'off-policy 冷启动 + on-policy 迭代'。
- 深度剖析与工程权衡:① 'on-policy 采样昂贵'是 PPO 的主要成本 ——每轮都要用当前策略自回归生成(慢)、且生成的数据用一次就废(受 clip 限制);故 RLHF 的吞吐远低于 SFT。这是 DPO(用固定偏好数据,无采样)更经济的原因。② 'RLAIF 让 on-policy 变便宜' ——用 AI(而非人工)标注新策略的输出,使 on-policy RM 更新的成本大幅下降;这是 iterative RLHF / online DPO 能规模化的关键。③ 与'分布偏移'的关系 ——off-policy 数据与当前策略的分布差异越大,RM 的可靠性越差(因为训练分布与使用分布不匹配);这与监督学习的'训练-测试分布一致'原则同理。④ DPO 的 off-policy 本质 ——DPO 用 固定的偏好数据集 训练,且其推导假设'偏好数据来自参考模型';若数据来自其他模型(如人类写的),则 DPO 的假设被违反,效果下降(这是 DPO 的局限之一,催生了 online DPO)。⑤ '数据效率'的对比 ——on-policy 数据的'每样本价值'更高(因为它直接反映当前策略的弱点),但获取成本高;off-policy 数据便宜但可能浪费在'已学会的'区域。⑥ 面试要点 ——被问'为什么 PPO 要 on-policy',应给出' 重要性采样比的无偏性要求样本来自采样时的策略 ',并说明' RM 可 off-policy 但 on-policy 数据更有效 → 迭代式 RLHF ';能指出'DPO 用固定数据是其局限、online DPO 修正它'是深度理解的标志。
- PPO 的 ratio 需样本来自采样时的策略(on-policy)
- RM 可用任意策略的数据(off-policy,更省)
- 迭代式 RLHF:用最新策略的数据更新 RM(近似 on-policy)
常见陷阱
- 用很旧的策略数据做 PPO(重要性采样方差爆炸)
- RM 只用 off-policy 数据(分布外不可靠)