Skip to content

Conversation

@DrRyanHuang
Copy link
Contributor

@DrRyanHuang DrRyanHuang commented Nov 11, 2025

PR Category

Execute Infrastructure

PR Types

Bug fixes

Description

运行 GPT OSS 20B 出现:

----------------------
Error Message Summary:
----------------------
InvalidArgumentError: The operands' types of the node [Add] don't match. 
Received types: float32 and bfloat16
  [Hint: Expected a.type() == b.type(), but received a.type():float32 != b.type():bfloat16.] 
(at /workspace/Paddle/paddle/cinn/ir/ir.cc:138)

打出IR图:

    (%544) = "cinn_op.fusion" [id:12442] () -> tensor<-1x32xf32> {
		...
        (%548) = "pd_op.add" [id:12441] (%543, %547) {stop_gradient:[false]} : (tensor<-1x32xf32>, tensor<-1x-1xf16>) -> tensor<-1x32xf32> ......
        () = "cf.yield" [id:12443] (%548) {} : (tensor<-1x32xf32>) ->  {  }
    } { ...... }

是这里的 pd_op.add 出现了问题,本PR添加 AddCastToElementwiseAddPass 在 add op 之前插入 cast op,保证类型一致

--

这个 PR 给 add 的两个输入都添加了 cast ,如下:

if (x_dtype != y_dtype) {
pir::Type output_dtype = GetOutputDtype(x_dtype, y_dtype);
auto cast_op0 = rewriter.Build<paddle::dialect::CastOp>(
op->operand_source(0),
::paddle::dialect::TransToPhiDataType(output_dtype));
auto cast_op1 = rewriter.Build<paddle::dialect::CastOp>(
op->operand_source(1),
::paddle::dialect::TransToPhiDataType(output_dtype));
op->operand(0).set_source(cast_op0->result(0));
op->operand(1).set_source(cast_op1->result(0));
return true;
}

不过不必担心 CreateAddBroadcastToElementwisePassApplyGroupOpPass 中的第一个Pass
之后其中的不必要的 cast 将会被其他 pass 消除

void ApplyGroupOpPass(::pir::Program* program,
const std::function<std::shared_ptr<pir::PassManager>()>&
CreatePassManager) {
std::shared_ptr<pir::PassManager> pass_manager = CreatePassManager();
pass_manager->AddPass(
cinn::dialect::ir::CreateAddBroadcastToElementwisePass());

这个 Pass 处于 CINN 前端:

@paddle-bot
Copy link

paddle-bot bot commented Nov 11, 2025

你的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.

@DrRyanHuang DrRyanHuang changed the title add cast before addop by adding pass [SOT] Add castOp before addOp by AddCastToElementwiseAddPass Nov 11, 2025
@DrRyanHuang DrRyanHuang changed the title [SOT] Add castOp before addOp by AddCastToElementwiseAddPass [CINN] Add castOp before addOp by AddCastToElementwiseAddPass Nov 11, 2025
@DrRyanHuang DrRyanHuang force-pushed the add_cast_before_addOp branch from abbf572 to 7662bfe Compare November 11, 2025 13:47
@DrRyanHuang DrRyanHuang force-pushed the add_cast_before_addOp branch from e86727f to c57b4c1 Compare November 12, 2025 03:40
@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 89.58333% with 5 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@99a2725). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...tor/transforms/add_cast_to_elementwise_add_pass.cc 89.36% 5 Missing ⚠️

❌ Your patch status has failed because the patch coverage (89.58%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #76359   +/-   ##
==========================================
  Coverage           ?   89.58%           
==========================================
  Files              ?        2           
  Lines              ?       48           
  Branches           ?        0           
==========================================
  Hits               ?       43           
  Misses             ?        5           
  Partials           ?        0           

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

@DrRyanHuang
Copy link
Contributor Author

DrRyanHuang commented Nov 12, 2025

TODO: 后续本PR内容需要与此处的💩代码一起删除
PS: 请查看 history ,💩这个 emoji 不是我写的,是 @SigureMo 加的
PSS: 啥 history 呀,我不到啊

if (x_dtype != y_dtype) {
// TODO(Xi Zhao): we got special case for add now, should remove it in future.
#ifdef PADDLE_WITH_CUDA
if ((op_name == "add" || op_name == "add_") &&
x_dtype == DataType::FLOAT32 &&
(y_dtype == phi::DataType::BFLOAT16 ||
y_dtype == phi::DataType::FLOAT16)) {
return false;
}
#elif defined(PADDLE_WITH_XPU)
if ((op_name == "add" || op_name == "add_") &&
x_dtype == DataType::FLOAT32 &&
(y_dtype == phi::DataType::BFLOAT16 ||
y_dtype == phi::DataType::FLOAT16)) {
return false;
}
#endif

@DrRyanHuang DrRyanHuang merged commit cfd897f into PaddlePaddle:develop Nov 12, 2025
89 of 92 checks passed
@DrRyanHuang DrRyanHuang deleted the add_cast_before_addOp branch November 12, 2025 14:24
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.

4 participants