← Back to Multimodal Mind Map
👁️ MultimodalID: masked-cross-entropy

Masked Cross-Entropy Loss

Masked Cross-Entropy
🎯Core Definition
Masked Cross-Entropy (Masked CE) is the standard VLM training loss — sequence-level next-token prediction that computes loss only on assistant (model-answer) text tokens, masking gradients of user-instruction tokens and all vision patch tokens: L=−∑t∈assistantlog⁡pθ(yt∣x<t)\mathcal{L} = -\sum_{t \in \text{assistant}} \log p_\theta(y_t \mid x_{<t}) The input is x=[v1,…,vN,u1,…,um,a1,…,aL]x = [v_1, \dots, v_N, u_1, \dots, u_m, a_1, \dots, a_L] (vision patches → user instruction → assistant answer); causal attention sees all tokens, but only t∈assistantt \in \text{assistant} positions contribute loss — exactly the LLM SFT labels mechanism where non-supervised positions are set to ignore_index\text{ignore\_index} (e.g. -100). Why mask: (1) vision patch tokens have no "next token" supervision target (they come from the frozen encoder, they are not generation targets) — CE on them forces patch-sequence prediction, pointless and harmful to text learning; (2) user instructions are not content the model should generate — predicting them teaches the model to parrot the user; (3) compute efficiency — with high-resolution inputs, vision tokens dominate the sequence (336×336, patch 14 → 576 vision tokens, often 85–90% of the sequence), so masking cuts backward-computation roughly an order of magnitude. An optional 1/L1/L normalizes by supervised-token count.
💡Use Cases
the standard objective across all VLM stages (alignment/pretraining/SFT); interview follow-ups on how the multimodal loss is written and whether vision tokens are supervised; it also links to why hallucination correlates with missing vision gradients (see the multimodal-hallucination card).
⚡Key Problems Solved
naively computing CE over the whole sequence makes the model learn to predict image patches and echo user questions, wasting 85–90% of compute and degrading generation quality; Masked CE concentrates supervision on what the model should actually generate (assistant text), mirroring the ignore_index mechanism of LLM chat templates — the design that unifies VLM and LLM training objectives.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Masked Cross-Entropy Loss.
Use Cases
In production engineering, what are the primary use cases for Masked Cross-Entropy Loss and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Write the masked-CE formula; which tokens are masked and why?
2
What goes wrong if you compute CE on vision patch tokens?
3
With vision tokens at 85–90% of the sequence, how does masking affect compute (336×336→576 patches)?
4
How does masked CE correspond to the ignore_index=-100 mechanism of LLM SFT?
5
Why mask user instructions too? What would the model learn otherwise?
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Masked Cross-Entropy Loss"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardVLM 3-Stage PipelineNext Card →Multimodal DPO / RLHF-V

🔗 More Multimodal Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/Multimodal/card/masked-cross-entropy
中文速览与面试要点

Masked Cross-Entropy (Masked Cross-Entropy Loss)

定义: Masked Cross-Entropy (Masked CE) 是 VLM 的标准训练损失——序列级 next-token 预测, 但只在 assistant (模型回答) 文本 token 上计算损失, 屏蔽用户指令 token 与全部视觉 patch token 的梯度: L = - _t assistant p_ (y_t x_<t) 输入序列为 x = [v_1, , v_N, u_1, , u_m, a_1, , a_L] (视觉 patch → 用户指令 → assistant 回答), 因果注意力对全部 token 可见, 但只有 t assistant 位置产生损失——等价于 LLM SFT 的 labels 机制: 非监督位置 label = ignore\_index (如 -100)。 为什么屏蔽: (1) 视觉 patch token 没有“下一个 token”的监督目标 (它们由编码器静态提供, 不是生成目标), 对它们算 CE 会强迫模型预测 patch 序列, 既无意义又干扰文本学习; (2) 用户指令不是模型该生成的内容, 预测指令会鼓励模型“续写用户话术”; (3) 计算效率——高分辨率输入下视觉 token 占序列绝大部分 (336×336、patch 14 → 576 个视觉 token, 常占整条序列 85–90%), 屏蔽后反向传播量近似下降一个量级。归一化可加 1/L (按监督 token 数平均)。 使用场景: VLM 全阶段训练 (预对齐/预训练/SFT) 的标准目标; 面试追问“多模态训练的 loss 怎么写、视觉 token 有没有监督”; 也解释为什么幻觉与视觉梯度缺位相关 (见 multimodal-hallucination 卡)。 解决的问题: 朴素地“整条序列算 CE”会让模型学预测图像 patch 与复述用户问题, 浪费 85–90% 的计算且损伤生成质量; Masked CE 把监督集中到模型真正要生成的内容 (assistant 文本), 与 LLM chat 模板的 ignore_index 机制同源, 是 VLM 与 LLM 训练目标统一的关键设计。

核心要点

  • 写出 Masked CE 公式, 哪些 token 被屏蔽?为什么?
  • 对视觉 patch token 算 CE 会发生什么问题?
  • 视觉 token 占序列 85–90% 时, 屏蔽如何影响计算量 (336×336→576 patch)?
  • Masked CE 与 LLM SFT 的 ignore_index=-100 机制如何对应?
  • 为什么用户指令也要屏蔽?不屏蔽会学到什么?