Skip to content

[0-size Tensor No.2、3、13、14、15、17、30、36、37、68、189、211、217、219、224、228、245、254、258、260、264、265、272、273、288、291、295、305、327、331、332、333、334、336、337、340] Add 0-size Tensor support for a lot of activation function APIs. - #72821

Merged
wanghuancoder merged 13 commits into
PaddlePaddle:developfrom
DanielSun11:fix_0-size_activation_func
May 28, 2025

Conversation

@DanielSun11

@DanielSun11 DanielSun11 commented May 20, 2025 •

Copy link
Copy Markdown
Contributor

PR Category

Execute Infrastructure

PR Types

Improvements

Description

修复了一个比较共性的问题,涉及到如下API:
acos acosh asin asinh atan atanh ceil cos cosh floor rsqrt sin sinh sqrt square tan tanh relu sigmoid silu softsign TanhShrink log_sigmoid
这类api的kernel通常使用宏来快速定义,因此只需要修复对应的宏即可。具体的修改方法(以CPU为例)如下:

 #define DEFINE_CPU_ACTIVATION_KERNEL(name, functor_class)               \
  template <typename T, typename Context>                               \
  void name##Kernel(                                                    \
      const Context& dev_ctx, const DenseTensor& x, DenseTensor* out) { \
    funcs::functor_class<T> functor;                                    \
    if (out && out->numel() == 0) {                                     \
      dev_ctx.template Alloc<T>(out);                                   \
      return;                                                           \
    }                                                                   \
    ActivationImpl<T, T, Context, funcs::functor_class<T>>(             \
        dev_ctx, x, out, functor);                                      \
  }

添加了

    if (out && out->numel() == 0) {                                     \
      dev_ctx.template Alloc<T>(out);                                   \
      return;                                                           \
    }  

除此之外,这类api较为常见,因此需要在gpu xpu sparse对应的kernel中一并修改。修改方法类似。

由于test_activation_op.py 里面代码太长而且有较多遗留问题,因此新增单测文件: Paddle/test/legacy_test/test_activation_op_zero_size.py

Pcard-67164

@paddle-bot

paddle-bot Bot commented May 20, 2025

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.

@DanielSun11 DanielSun11 changed the title [0-size Tensor No.xxx] Add 0-size Tensor support for a lot of activation function API. [0-size Tensor No.2、3、13、14、15、17、30、36、37、68、189、211、217、219、224、228、245、254、258、260、264、265、272、273、288、291、295、305、327、331、332、333、334、336、337、340] Add 0-size Tensor support for a lot of activation function APIs. May 20, 2025
@DanielSun11 DanielSun11 changed the title [0-size Tensor No.2、3、13、14、15、17、30、36、37、68、189、211、217、219、224、228、245、254、258、260、264、265、272、273、288、291、295、305、327、331、332、333、334、336、337、340] Add 0-size Tensor support for a lot of activation function APIs. [WIP][0-size Tensor No.2、3、13、14、15、17、30、36、37、68、189、211、217、219、224、228、245、254、258、260、264、265、272、273、288、291、295、305、327、331、332、333、334、336、337、340] Add 0-size Tensor support for a lot of activation function APIs. May 21, 2025
@co63oc

co63oc commented May 21, 2025

Copy link
Copy Markdown
Contributor

宏中调用了 ActivationGradImpl,修改ActivationGradImpl更通用
image

@DanielSun11

Copy link
Copy Markdown
Contributor Author

宏中调用了 ActivationGradImpl,修改ActivationGradImpl更通用 image

好主意!👍

@DanielSun11 DanielSun11 changed the title [WIP][0-size Tensor No.2、3、13、14、15、17、30、36、37、68、189、211、217、219、224、228、245、254、258、260、264、265、272、273、288、291、295、305、327、331、332、333、334、336、337、340] Add 0-size Tensor support for a lot of activation function APIs. [0-size Tensor No.2、3、13、14、15、17、30、36、37、68、189、211、217、219、224、228、245、254、258、260、264、265、272、273、288、291、295、305、327、331、332、333、334、336、337、340] Add 0-size Tensor support for a lot of activation function APIs. May 26, 2025

@wanghuancoder wanghuancoder 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.

LGTM

@DanielSun11

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

3 similar comments
@DanielSun11

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@DanielSun11

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@DanielSun11

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@DanielSun11

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@DanielSun11

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@DanielSun11

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@DanielSun11

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

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