Skip to content

[Operator Mechanism] Replace VCOPY/SCAL with direct copy and ForRange scale in CPU/GPU kernels#79468

Open
feixi139 wants to merge 2 commits into
PaddlePaddle:developfrom
feixi139:Paddle_fix_VCOPY
Open

[Operator Mechanism] Replace VCOPY/SCAL with direct copy and ForRange scale in CPU/GPU kernels#79468
feixi139 wants to merge 2 commits into
PaddlePaddle:developfrom
feixi139:Paddle_fix_VCOPY

Conversation

@feixi139

Copy link
Copy Markdown
Contributor

PR Category

Operator Mechanism

PR Types

Bug fixes

Description

本 PR 移除了 phi::funcs::Blas 中的 VCOPY(向量拷贝)和 SCAL(原地标量乘)两个 BLAS API 的定义、声明及全部调用点,替换为更直接、精度更可控的实现方式,同时消除了对 MKLML/HML/cuBLAS/rocBLAS 底层 copy/scal 例程的依赖。

背景

VCOPY/SCAL 在 Blas<DeviceContext> 中只是对底层 BLAS 库(MKLML cblas_?copy/?scal、HML、cuBLAS cublasS/Dcopy/Sscal/Dscal、rocBLAS 对应例程)的简单封装,语义上完全等价于普通的内存拷贝和逐元素乘法。

保留这两个 API 增加了跨多个后端分支(MKLML/HML/plain-OpenBLAS/CUDA/HIP)的维护成本,且部分调用点(如 baddbmm_grad_kernel_impl.h)已经改用 ForRange + Functor 的方式以支持混合精度(MPType)计算,与 VCOPY/SCAL 的实现方式不一致。

改动内容

1. API 删除

删除 Blas<DeviceContext>::VCOPY/SCAL 声明与实现,以及:

  • CBlas<T>(CPU,MKLML/HML/plain 三个分支)中对应的底层特化。
  • CUBlas<T>(cuBLAS/rocBLAS)中对应的底层特化。
2. 内部依赖修复

Blas<CPUContext>::VADD 内部原先调用 VCOPY 做初始拷贝,改为直接使用 std::memcpy

3. 调用点替换
  • CPU 端 VCOPY → memcpy;GPU 端 VCOPY → memory_utils::Copy(stream-aware)。
  • 模板化/GPU 兼容的 SCAL 调用(如 sparse/gpu/addmm_grad_kernel.cu 中 4 处)→ ForRange + 自定义 ScaleFunctor。
  • CPU-only 遗留 kernel 中的 SCAL 调用(hsigmoid_loss_grad.hattention_lstm_kernel.cc)→ 逐元素 for 循环。
  • gelu_grad_kernel.cc 中绕过 Blas<Context> 直接调用的 CBlas<T>::SCAL(仅在 PADDLE_WITH_MKLML 分支下生效)→ 逐元素 for 循环,等价改写并合并了原先 +1 与 SCAL(0.5) 两步为单次表达式。
  • 其余涉及 VCOPY/SCAL 的文件(elementwise_grad.hselected_rows_functor.ccsequence_pooling.ccfusion_lstm_kernel.ccfusion_seqconv_eltadd_relu_kernel.ccsparse_weight_embedding_kernel.cclookup_table_kernel.cc)同步完成等价替换。

是否引起精度变化

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

未发现需要阻塞合入的问题。重点检查了 VCOPY/SCAL 删除后的 CPU/GPU 拷贝替换、ForRange scale 路径、addmm/baddbmm 混合精度处理以及剩余符号引用,未发现需要作者修改的行级问题。CI 仍有部分构建/测试在运行,合入前请以最终 CI 结果为准。

Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

PaddlePaddle-bot

This comment was marked as outdated.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
...i/kernels/selected_rows/cpu/lookup_table_kernel.cc 66.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #79468   +/-   ##
==========================================
  Coverage           ?   90.00%           
==========================================
  Files              ?        6           
  Lines              ?       20           
  Branches           ?        0           
==========================================
  Hits               ?       18           
  Misses             ?        2           
  Partials           ?        0           

☔ View full report in Codecov by Harness.
📢 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.

@paddle-bot paddle-bot Bot added the contributor External developers label Jul 13, 2026

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

已复查新提交。VADD fallback 现在在 y == z 时跳过自拷贝,别名安全问题已在当前代码中处理;同时检查了 fusion_lstm 的字节数变量调整,未发现新的阻塞问题。CI 仍有任务在运行,合入前请以最终 CI 结果为准。

Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

@PaddlePaddle-bot PaddlePaddle-bot left a comment

Copy link
Copy Markdown

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-07-14 14:34:54

📋 Review 摘要

PR 概述:移除 phi::funcs::Blas 的 VCOPY/SCAL 封装,并用 memcpymemory_utils::CopyForRange 替换 CPU/GPU kernel 调用。
变更范围paddle/phi/kernels/funcs/blas/、CPU/Fusion/Sparse/SelectedRows kernel、addmm/baddbmm grad。
影响面 TagOperator Mechanism Performance Optimization

问题

未发现阻塞性问题。PR 规范问题在下面章节报,不要在这里重复

📝 PR 规范检查

符合规范。

总体评价

本轮按风险优先审查了 BLAS API 删除、GPU/CPU copy/scale 替换、addmm/baddbmm 梯度 ForRange 替换和 PR 描述一致性,未发现需要阻塞合入的问题。建议在 CI 中重点覆盖 CPU/GPU addmm/baddbmm、sparse addmm grad 以及 sequence pooling 等被替换路径。

@paddle-bot

paddle-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

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

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants