Skip to content

[Optimization] merge matmul and add#6986

Merged
qingqing01 merged 11 commits intoPaddlePaddle:developfrom
BingooYang:linear_opt
Apr 3, 2026
Merged

[Optimization] merge matmul and add#6986
qingqing01 merged 11 commits intoPaddlePaddle:developfrom
BingooYang:linear_opt

Conversation

@BingooYang
Copy link
Copy Markdown
Contributor

@BingooYang BingooYang commented Mar 24, 2026

Motivation

性能优化

Modifications

将UnquantizedLinearMethod中的matmul和add用linear替换。
带bias情况基本上有加速,不带bias情况小shape下性能有下降(主要是python层if等调度开销,linear内部实现也是matmul)。
810cbda4d14af8f770c231d04dbe1090

Usage or Command

Accuracy Tests

精度保持一致

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.
  • [ x ] Add unit tests. Please write the reason in this PR if no unit tests.
  • [ x ] Provide accuracy results.
  • [ x ] 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.

@paddle-bot
Copy link
Copy Markdown

paddle-bot bot commented Mar 24, 2026

Thanks for your contribution!

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@98f3fc9). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #6986   +/-   ##
==========================================
  Coverage           ?   73.99%           
==========================================
  Files              ?      376           
  Lines              ?    53406           
  Branches           ?     8470           
==========================================
  Hits               ?    39518           
  Misses             ?    11138           
  Partials           ?     2750           
Flag Coverage Δ
GPU 73.99% <100.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.

Comment thread fastdeploy/model_executor/layers/linear.py Outdated
@BingooYang
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

2 similar comments
@BingooYang
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@BingooYang
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

Comment thread tests/e2e/4cards_cases/test_GLM_45_AIR_mtp_tp4.py Outdated
yuanlehome
yuanlehome previously approved these changes Apr 3, 2026
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.

🤖 AI Code Review | 2026-04-03 17:42 CST

📋 Review 摘要

PR 概述:将 UnquantizedLinearMethod.apply() 中的 paddle.matmul + paddle.add 优化为 paddle.nn.functional.linear,减少算子调用开销。

变更范围model_executor/layers/linear.py(核心优化)、测试 baseline 路径更新

影响面 TagOP Optimization

问题

未发现阻塞性问题。

✅ 代码分析

检查项 结果
PR 标题规范 ✅ 包含 [Optimization] Tag
PR 描述完整性 ✅ Motivation/Modifications 已填写,附带性能对比图
代码逻辑正确性 ✅ PaddlePaddle 的 F.linear(x, weight, bias) 计算公式为 x @ weight + bias,与原 matmul + add 实现等价
新增 assert 检查 ✅ 验证 bias shape 与 weight 最后一维匹配,有助于提前发现配置错误
测试覆盖 ✅ baseline 已更新(0402 → 0403),精度验证通过

总体评价

代码变更逻辑正确,性能优化合理。带 bias 情况使用 F.linear 可减少 Python 层调度开销;不带 bias 情况保持使用 matmul 避免小 shape 下的性能损失,符合 PR 描述中的性能分析结论。

@qingqing01 qingqing01 merged commit 2068656 into PaddlePaddle:develop Apr 3, 2026
36 of 37 checks passed
zoooo0820 pushed a commit that referenced this pull request Apr 9, 2026
* replace matmul+add to linear

* modify baseline
zoooo0820 pushed a commit that referenced this pull request Apr 9, 2026
* merge matmul and add

* modify format

* using paddle.nn.functional.linear

* using _C_ops.linear

* using paddle.nn.functional.linear

* add FLAGS_use_legacy_linear env var in test case

* fix format

* add assert and remove env

* modify format

* using matmul for no bias

* modify accurate baseline
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.

6 participants