Skip to content

Conversation

@Enigmatisms
Copy link
Contributor

@Enigmatisms Enigmatisms commented Aug 5, 2025

PR Category

CINN

PR Types

Bug fixes

Description

修复了 与arange dynamic shape 支持 PR:

所进一步暴露的问题。问题的表现形式如下:

在一些非常复杂的 op fusion 过程中,generate shape 会生成新的 shape symbol,这些 symbol 会被直接记录在 cinn_op.generate_shape 的 attribute(output_dim_exprs)中。而记录的 output_dim_exprs 其中的 symbol 在后续应该被替换为实际的 symbol,但 CINN 中并没有这么做,比如:

Expr: Mul(SS0, SS1, 4)

SS0 / SS1 这些是在 Lowering 过程中产生的,但实际代表的是实际参与 shape 计算的 symbol(实际存在的 symbol):

Mul(S41, S42, 4)

本 PR 利用 GetTensor 时向 Tensor Value 内部填入的 ShapeOrData 的 Data 数据,替换了 tensor->operation 中较为复杂和隐晦的 operation attribute(以及一系列的 attribute from/to DimExpr&Expr)操作。简化了实现,并且可以获得正确的 symbol 信息。

Pcard-89620

@paddle-bot
Copy link

paddle-bot bot commented Aug 5, 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.

@zyfncg zyfncg requested a review from Copilot August 5, 2025 03:09
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug in CINN where cinn_op.generate_shape operations produce new symbolic shapes that aren't properly replaced with actual symbols during lowering. The issue manifests in complex op fusion scenarios where temporary symbols (SS0, SS1) are created but not replaced with the real symbols (S41, S42), causing LLVM compilation failures.

Key changes:

  • Added ReplaceGenerateShapeAttribute function to replace temporary symbols with actual ones
  • Added detection for groups containing both cinn_op.arange and cinn_op.generate_shape operations
  • Implemented attribute replacement logic in the lowering process
Comments suppressed due to low confidence (2)

paddle/cinn/hlir/framework/pir/op_lowering_impl.cc:524

  • The parameter name 'unsafe_op' is misleading. Consider renaming it to 'op' or 'generate_shape_op' since the function specifically handles generate_shape operations.
 ::pir::Operation* unsafe_op) { 

paddle/cinn/hlir/framework/pir/op_lowering_impl.cc:605

  • The variable name 'unsafe_op_' with trailing underscore is inconsistent with typical naming conventions. Consider using 'mutable_op' or simply 'op_mut' to better indicate its purpose.
 auto unsafe_op_ = const_cast<::pir::Operation*>(op); 
// 'output_dim_exprs' attribute is used almost nowhere but symbolic arange, it
// will be fine to use a unsafe const_cast. It will actually be better, if we
// can trace verify whether this `cinn_op.generate_shape` is the input to the
// arange op.
Copy link

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

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

The comment explanation is unclear and contains grammatical issues. Consider rewriting: 'The operation parameter is made mutable via const_cast to modify the output_dim_exprs attribute. This is safe because this attribute is primarily used for symbolic arange operations.'

Suggested change
// arange op.
// The unsafe_op parameter is made mutable via const_cast to allow modification
// of the 'output_dim_exprs' attribute. This is considered safe because this
// attribute is primarily used for symbolic arange operations. For improved
// safety, it would be better to verify that 'cinn_op.generate_shape' is used
// as input to an arange operation before applying this modification.
Copilot uses AI. Check for mistakes.
"The size of dim expr vector should not be empty."));
// TODO(heqianyue): more secure impl is to check whether the old DimExpr has
// the same structure as the updated one. We can use a binary tree comparison
// algorithm. Yet normally we will be fine without checking.
Copy link

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

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

The TODO comment contains grammatical errors. Consider: 'TODO(heqianyue): A more secure implementation would check whether the old DimExpr has the same structure as the updated one. We could use a binary tree comparison algorithm, though normally we should be fine without this check.'

Suggested change
// algorithm. Yet normally we will be fine without checking.
// TODO(heqianyue): A more secure implementation would check whether the old DimExpr
// has the same structure as the updated one. We could use a binary tree comparison
// algorithm, though normally we should be fine without this check.
Copilot uses AI. Check for mistakes.
@Enigmatisms Enigmatisms closed this Aug 5, 2025
@Enigmatisms Enigmatisms changed the title [CINN] Fixed generate_shape output_dim_exprs producing new symbolic shape [CINN] Fixed dynamic arange symbolic values extraction. Aug 5, 2025
@Enigmatisms Enigmatisms reopened this Aug 5, 2025
@Enigmatisms
Copy link
Contributor Author

/re-run all-failed

@Enigmatisms
Copy link
Contributor Author

/re-run Model-Benchmark

@Enigmatisms
Copy link
Contributor Author

/re-run all-failed

@zyfncg zyfncg merged commit c3eb447 into PaddlePaddle:develop Aug 6, 2025
85 of 88 checks passed
Enigmatisms added a commit to Enigmatisms/Paddle that referenced this pull request Aug 6, 2025
…#74412) * [CINN] Fix cinn_op.generate_op attribute storing useless dim_expr * [CINN] Removed unnecessary VLOGs * [CINN] Simplify dynamic arange logic and fix bugs.
Enigmatisms added a commit to Enigmatisms/Paddle that referenced this pull request Aug 6, 2025
…#74412) * [CINN] Fix cinn_op.generate_op attribute storing useless dim_expr * [CINN] Removed unnecessary VLOGs * [CINN] Simplify dynamic arange logic and fix bugs.
phlrain pushed a commit that referenced this pull request Aug 7, 2025
…p bugs (#74437) * [CINN] Robustify min/max type matching (#74316) * [CINN] Fixed dynamic arange symbolic values extraction. (#74412) * [CINN] Fix cinn_op.generate_op attribute storing useless dim_expr * [CINN] Removed unnecessary VLOGs * [CINN] Simplify dynamic arange logic and fix bugs.
@Enigmatisms Enigmatisms deleted the fix_generate_shape branch August 29, 2025 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants