Skip to content

【BIT】square Tensor.square support 0-size #72385

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

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from

Conversation

mzj104
Copy link
Contributor

@mzj104 mzj104 commented Apr 21, 2025

PR Category

Execute Infrastructure

PR Types

New features

Description

square Tensor.square支持0-Size。

修改历程介绍如下:

这个api本身就已经支持0-size,故在kernal中加入了拦截,在op中添加了单测

前向修复:
a. 在Paddle代码中检索def isclose,发现isclose的核心实现调用的是_C_ops的square
b. 以_C_ops的square在paddle/phi/ops/yaml中检索,发现square的InferMeta函数使用到:
UnchangedInferMeta
c. 在代码中检索UnchangedInferMeta,并检查其dims(shape)的推导是否正确(是正确的)

void UnchangedInferMeta(const MetaTensor& x, MetaTensor* out) {
  out->share_meta(x);
}

d. 在paddle/phi/kernels中检索square,找全所有square的实现Kernel。发现共有1个涉及square的文件,为:

Paddle/paddle/phi/kernels/selected_rows/activation_kernel.cc

加入以下代码,实现0-size情况的拦截

  if (x.value().numel() == 0) {
    dev_ctx.template Alloc<T>(out->mutable_value());
    return;
  }

添加单测:

在/test_activation_op.py中添加0 size tensor输入的单测:

paddle.square(Tensor([0, 1, 1],"float32"), )

class TestSquare_ZeroSize(TestSquare):
    def init_shape(self):
        self.shape = [0, 1, 1]  # 0-size tensor

Copy link

paddle-bot bot commented Apr 21, 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.

@paddle-bot paddle-bot bot added the contributor External developers label Apr 21, 2025
@mzj104 mzj104 marked this pull request as ready for review April 21, 2025 06:29
@luotao1 luotao1 added the HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 label Apr 22, 2025
Copy link
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

DEFINE_CPU_ACTIVATION_KERNEL 对应的 ActivationImpl
DEFINE_GPU_ACTIVATION_KERNEL 对应的 ActivationGPUImpl
DEFINE_XPU_ACTIVATION_KERNEL 对应的 ActivationXPUImpl
paddle/phi/kernels/cpu/activation_grad_kernel.cc 几个宏对应的 ActivationGradImpl
以及对应GPU的 ActivationGradGPUImp,XPU的ActivationGradXPUImpl都应该修改。

修改后,相应的activation API应该同时加单测,同时复测。

Comment on lines +31 to +34
if (x.value().numel() == 0) {
dev_ctx.template Alloc<T>(out->mutable_value());
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

当把常规的activation修改后,这里应该就不用修改了。因为下面调用了常规的activation Kernel。

Copy link

paddle-ci-bot bot commented May 1, 2025

Sorry to inform you that f53db94's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants