Skip to content

[BugFix] Fix attention mask for multimodal models#7841

Open
TBD1 wants to merge 1 commit into
PaddlePaddle:developfrom
TBD1:develop
Open

[BugFix] Fix attention mask for multimodal models#7841
TBD1 wants to merge 1 commit into
PaddlePaddle:developfrom
TBD1:develop

Conversation

@TBD1
Copy link
Copy Markdown
Collaborator

@TBD1 TBD1 commented May 18, 2026

Motivation

修复多模+MTP场景attention mask计算问题

Modifications

Usage or Command

Accuracy Tests

Checklist

  • 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.
  • 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.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented May 18, 2026

Thanks for your contribution!

Copy link
Copy Markdown

@PaddlePaddle-bot PaddlePaddle-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Paddle-CI-Agent | pr_review | 2026-05-18 11:22:48

📋 Review 摘要

PR 概述:修复多模态 + MTP 场景下 attention_mask_offset 为空时的 attention mask 计算错误,增加 None 分支回退到线性偏移量。
变更范围fastdeploy/spec_decode/mtp.pyinsert_tasks_v1
影响面 Tag[Speculative Decoding]

问题

级别 文件 概述
🔴 Bug mtp.py(行 539-542,diff 外) XPU 路径未同步 None 保护,attention_mask_offset 缺失时仍会 KeyError
❓ 疑问 mtp.py:525 multimodal_inputsOptional[dict]inputs 可能为 Noneinputs.get() 将抛 AttributeError
📝 PR 规范 ## Modifications 段落为空

🔴 Bug:XPU 路径未同步 None 保护(mtp.py 行 539-542)

现象:本次 PR 在 GPU 路径新增了对 attention_mask_offsetNone/缺失的处理(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_offsetNone 或字段不存在时,主块已通过 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 时抛 AttributeError

multimodal_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 处理后再合入。

@PaddlePaddle-bot
Copy link
Copy Markdown

PaddlePaddle-bot commented May 18, 2026

🤖 Paddle-CI-Agent | ci_status_monitor | 2026-05-19 04:50:20

CI报告基于以下代码生成(30分钟更新一次):


1 任务总览

❌ 有 2 个 Required 任务失败,需优先处理后方可合并。

总执行(rerun次数) 总任务 ✅ 通过 ❌ 失败 ⏳ 运行中 ⏸️ 等待中 跳过
42(0) 42 37 5 0 0 0

2 任务状态汇总

2.1 Required 任务 : 8/10 通过

必选任务阻塞合并,失败需优先处理。

状态 任务 耗时 根因 修复建议 日志 重跑
Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage 1h23m PR问题:mtp.py 新增代码覆盖率0%,未达80%阈值 为 mtp.py L503-511 新增单元测试或申请豁免 Job -
Approval 10s PR问题:修改 spec_decode,需指定 RD 审批,尚未获得 请联系 freeliuzc 或 Deleter-D 完成 Code Review Job -
其余 8 个必选任务通过 - - - - -

2.2 可选任务 — 29/32 通过

可选任务不阻塞合并,失败仅供参考。

状态 任务 耗时 日志 重跑
Run iluvatar Tests / run_iluvatar_cases 1m19s Job -
Check PR Template 15s Job -
CI_HPU 1h8m Job -
其余 29 个可选任务通过 - - -

3 失败详情(仅 Required)

Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage — 代码覆盖率不足(置信度: 高)

Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage

  • 状态: ❌ 失败
  • 错误类型: 覆盖率不足
  • 置信度: 高
  • 根因摘要: mtp.py L503-511 新增代码覆盖率0%,未达80%阈值
  • 分析器: ci_analyze_unittest_fastdeploy

根因详情:
PR 修改了 fastdeploy/spec_decode/mtp.py,共变更 9 行,其中第 503、504、505、507、511 行新增代码没有对应的单元测试覆盖,实际 diff 覆盖率为 0%,远低于 80% 门禁阈值(exit code 9)。注意:单元测试本身全部通过(TEST_EXIT_CODE=0),仅覆盖率不达标。

关键日志:

COVERAGE_EXIT_CODE: 9
"fastdeploy/spec_decode/mtp.py": {
  "percent_covered": 0.0,
  "violation_lines": [503, 504, 505, 507, 511],
  "total_percent_covered": 0,
  "total_num_violations": 5
}
##[error]Process completed with exit code 9.

修复建议:

  1. 在相关测试文件中为 fastdeploy/spec_decode/mtp.py L503, L504, L505, L507, L511 的新增逻辑添加单元测试,使 diff 覆盖率达到 80% 以上
  2. 若这些行属于难以覆盖的边界逻辑,可在 PR 描述中申请覆盖率豁免

修复建议摘要: 为 mtp.py L503-511 新增单元测试或申请豁免

关联变更: fastdeploy/spec_decode/mtp.py L503, L504, L505, L507, L511(新增代码 0% 覆盖)
链接: 查看日志

Approval — 审批缺失(置信度: 高)

Approval

  • 状态: ❌ 失败
  • 错误类型: 审批缺失
  • 置信度: 高
  • 根因摘要: PR 修改了 fastdeploy/spec_decode,需指定 RD 审批,尚未获得
  • 分析器: ci_analyze_infra

关键日志:

0. You must have one FastDeploy RD (freeliuzc(liuzichang01), Deleter-D(wangyanpeng04)) approval
   for modifing [fastdeploy/spec_decode, custom_ops/gpu_ops/speculate_decoding].
There are 1 approved errors.
##[error]Process completed with exit code 6.

根因详情:
PR 修改了受代码所有者保护的目录 fastdeploy/spec_decode,根据仓库规则,需至少一名指定 FastDeploy RD(freeliuzc 或 Deleter-D)通过 Code Review 并审批。当前尚未收到任何合格审批,脚本以 exit code 6 退出。

修复建议:

  1. 请联系 freeliuzc (liuzichang01)Deleter-D (wangyanpeng04) 对本 PR 进行 Code Review 并完成审批(GitHub Review Approve)

修复建议摘要: 请联系 freeliuzc 或 Deleter-D 完成审批

链接: 查看日志

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 18, 2026

Codecov Report

❌ Patch coverage is 60.00000% with 2 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@d70f33d). Learn more about missing BASE report.

Files with missing lines Patch % Lines
fastdeploy/spec_decode/mtp.py 60.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #7841   +/-   ##
==========================================
  Coverage           ?   63.27%           
==========================================
  Files              ?      462           
  Lines              ?    64280           
  Branches           ?     9852           
==========================================
  Hits               ?    40672           
  Misses             ?    20843           
  Partials           ?     2765           
Flag Coverage Δ
GPU 72.38% <60.00%> (?)
XPU 7.12% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants