[BugFix] Fix attention mask for multimodal models#7841
Conversation
|
|
|
Thanks for your contribution! |
PaddlePaddle-bot
left a comment
There was a problem hiding this comment.
🤖 Paddle-CI-Agent | pr_review |
2026-05-18 11:22:48
📋 Review 摘要
PR 概述:修复多模态 + MTP 场景下 attention_mask_offset 为空时的 attention mask 计算错误,增加 None 分支回退到线性偏移量。
变更范围:fastdeploy/spec_decode/mtp.py(insert_tasks_v1)
影响面 Tag:[Speculative Decoding]
问题
| 级别 | 文件 | 概述 |
|---|---|---|
| 🔴 Bug | mtp.py(行 539-542,diff 外) |
XPU 路径未同步 None 保护,attention_mask_offset 缺失时仍会 KeyError |
| ❓ 疑问 | mtp.py:525 |
multimodal_inputs 为 Optional[dict],inputs 可能为 None,inputs.get() 将抛 AttributeError |
| 📝 PR 规范 | — | ## Modifications 段落为空 |
🔴 Bug:XPU 路径未同步 None 保护(mtp.py 行 539-542)
现象:本次 PR 在 GPU 路径新增了对 attention_mask_offset 为 None/缺失的处理(np.arange 回退),但紧随其后的 XPU 专用分支未做同步修改:
# 行 539-542(未变更,diff 外)
if current_platform.is_xpu():
self.model_inputs["attn_mask_offsets_decoder"][idx : idx + 1] = (
inputs["attention_mask_offset"][prefill_end_index - 1] + 1 # ← 仍直接下标访问
)影响:在 XPU 平台上,当 attention_mask_offset 为 None 或字段不存在时,主块已通过 arange 回退正常执行,但代码继续执行到 XPU 分支,此处 inputs["attention_mask_offset"] 将抛出 KeyError(若字段不存在)或 TypeError(若值为 None)。本次修复在 XPU 上是不完整的。
建议修复:
if current_platform.is_xpu():
if inputs is not None and inputs.get("attention_mask_offset") is not None:
decoder_offset = inputs["attention_mask_offset"][prefill_end_index - 1] + 1
else:
decoder_offset = prefill_end_index # 与 arange 回退语义一致
self.model_inputs["attn_mask_offsets_decoder"][idx : idx + 1] = decoder_offset❓ 疑问:inputs 本身为 None 时的防御(mtp.py 行 525-527)
inputs = request.multimodal_inputs # Optional[dict],可能为 None
attn_offset_len = prefill_end_index - prefill_start_index
if inputs.get("attention_mask_offset", None) is None: # ← inputs 为 None 时抛 AttributeErrormultimodal_inputs 类型声明为 Optional[dict],若某路径未设置该字段(如纯文本 fallback 或框架内部路由),inputs.get() 会抛 AttributeError。原始代码同样存在此隐患,建议趁此次修改一并加固:
inputs = request.multimodal_inputs or {}📝 PR 规范检查
标题 [BugFix] Fix attention mask for multimodal models 格式合规,Tag 符合官方列表。
## Modifications 段落为空(仅有 HTML 注释占位符),建议按以下模板补全:
PR 描述建议(可直接复制):
## Motivation
修复多模态 + MTP 场景下,当 `multimodal_inputs` 中 `attention_mask_offset` 字段缺失或为 None 时,attention mask 偏移量计算崩溃的问题。
## Modifications
- `fastdeploy/spec_decode/mtp.py`:在 `insert_tasks_v1` 的 `use_attn_mask_offset` 分支中,新增对 `attention_mask_offset` 字段的空值保护;当字段缺失或为 None 时,回退使用 `np.arange(prefill_start_index, prefill_end_index)` 作为线性默认偏移量。
## Usage or Command
N/A
## Accuracy Tests
N/A
## Checklist
- [x] Add at least a tag in the PR title.
- Tag list: [`[FDConfig]`,`[APIServer]`,`[Engine]`, `[Scheduler]`, `[PD Disaggregation]`, `[Executor]`, `[Graph Optimization]`, `[Speculative Decoding]`, `[RL]`, `[Models]`, `[Quantization]`, `[Loader]`, `[OP]`, `[KVCache]`, `[DataProcessor]`, `[BugFix]`, `[Docs]`, `[CI]`, `[Optimization]`, `[Feature]`, `[Benchmark]`, `[Others]`, `[XPU]`, `[HPU]`, `[GCU]`, `[DCU]`, `[Iluvatar]`, `[Metax]`]
- You can add new tags based on the PR content, but the semantics must be clear.
- [x] Format your code, run `pre-commit` before commit.
- [ ] Add unit tests. Please write the reason in this PR if no unit tests.
- [ ] Provide accuracy results.
- [ ] If the current PR is submitting to the `release` branch, make sure the PR has been submitted to the `develop` branch, then cherry-pick it to the `release` branch with the `[Cherry-Pick]` PR tag.总体评价
GPU 路径的 None 回退逻辑清晰正确,但 XPU 专用分支(行 539-542)未同步保护,在 XPU 平台上仍会因 attention_mask_offset 缺失而崩溃,属修复不完整;建议补充 XPU 分支的 None 处理后再合入。
CI报告基于以下代码生成(30分钟更新一次): 1 任务总览❌ 有 2 个 Required 任务失败,需优先处理后方可合并。
2 任务状态汇总2.1 Required 任务 : 8/10 通过
2.2 可选任务 — 29/32 通过
3 失败详情(仅 Required)Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage — 代码覆盖率不足(置信度: 高)Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage
根因详情: 关键日志: 修复建议:
修复建议摘要: 为 关联变更: Approval — 审批缺失(置信度: 高)Approval
关键日志: 根因详情: 修复建议:
修复建议摘要: 请联系 freeliuzc 或 Deleter-D 完成审批 链接: 查看日志 |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #7841 +/- ##
==========================================
Coverage ? 63.27%
==========================================
Files ? 462
Lines ? 64280
Branches ? 9852
==========================================
Hits ? 40672
Misses ? 20843
Partials ? 2765
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Motivation
修复多模+MTP场景attention mask计算问题
Modifications
Usage or Command
Accuracy Tests
Checklist
[FDConfig],[APIServer],[Engine],[Scheduler],[PD Disaggregation],[Executor],[Graph Optimization],[Speculative Decoding],[RL],[Models],[Quantization],[Loader],[OP],[KVCache],[DataProcessor],[BugFix],[Docs],[CI],[Optimization],[Feature],[Benchmark],[Others],[XPU],[HPU],[GCU],[DCU],[Iluvatar],[Metax]]pre-commitbefore commit.releasebranch, make sure the PR has been submitted to thedevelopbranch, then cherry-pick it to thereleasebranch with the[Cherry-Pick]PR tag.