Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Optimize paddle.where and paddle.where_ in eager mode #69556

Merged
merged 6 commits into from
Nov 28, 2024

Conversation

HydrogenSulfate
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate commented Nov 20, 2024

PR Category

Performance Optimization

PR Types

Improvements

Description

Pcard-75624

  1. paddle.wherepaddle.where_的原有实现通过多次调用基础二元运算符进行隐式广播,从而让cond, x,, y形状保持一致,优化之后使用无计算量的broadcast_shape计算广播后的形状,再使用broadcast_to进行广播,极大简化了代码逻辑,并且减少了不必要的前向和反向算子开销
  2. 由于减少了算子,重新适配了where_部分动态图单测,并且修复了个别静态图单测中,占位符形状和实际数据形状不一致的问题。

Note

考虑到broadcast_shape和broadcast_to暂时没有完全适配动态shape,因此本PR只针对动态图分支进行修改

Copy link

paddle-bot bot commented Nov 20, 2024

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

Comment on lines 777 to 791
broadcast_shape = paddle.broadcast_shape(x_shape, y_shape)
broadcast_shape = paddle.broadcast_shape(broadcast_shape, condition_shape)

broadcast_x = x
broadcast_y = y
broadcast_condition = condition

if condition_shape != broadcast_shape:
broadcast_condition = paddle.broadcast_to(
broadcast_condition, broadcast_shape
)
if x_shape != broadcast_shape:
broadcast_x = paddle.broadcast_to(broadcast_x, broadcast_shape)
if y_shape != broadcast_shape:
broadcast_y = paddle.broadcast_to(broadcast_y, broadcast_shape)
Copy link
Contributor

Choose a reason for hiding this comment

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

确认下broadcast_shape和broadcast_to是否支持动态shape, 不支持的话静态图下可能会有问题

Copy link
Contributor Author

@HydrogenSulfate HydrogenSulfate Nov 22, 2024

Choose a reason for hiding this comment

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

确认下broadcast_shape和broadcast_to是否支持动态shape, 不支持的话静态图下可能会有问题

看了下,修改前的代码用了add这个支持动态shape的算子进行了隐式broadcast,所以静态图支持动态shape,但是修改之后的方案依赖于broadcast_shape和broadcast_to,这两个算子至少有一个存在对-1的处理BUG,导致静态图下的运行时结果不太对,所以目前的临时解决方案是:新的代码逻辑放在dynamic_mode里面,pir仍然使用原本来的代码。这样不影响已有功能。

@HydrogenSulfate HydrogenSulfate changed the title optimize where [API] Optimize paddle.where and paddle.where_ in eager mode Nov 22, 2024
zyfncg
zyfncg previously approved these changes Nov 22, 2024
@HydrogenSulfate HydrogenSulfate merged commit 510b3ed into PaddlePaddle:develop Nov 28, 2024
28 checks passed
@HydrogenSulfate HydrogenSulfate deleted the fix_where3 branch November 28, 2024 07:39
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.

2 participants