-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[XPU] remove cast for elementwise_add(float, bf16/fp16) #64289
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,18 +37,44 @@ void AddKernel(const Context& dev_ctx, | |
| if (x.dtype() == phi::DataType::FLOAT32 && | ||
| (y.dtype() == phi::DataType::BFLOAT16 || | ||
| y.dtype() == phi::DataType::FLOAT16)) { | ||
| using Type = DataTypeToCppType<phi::DataType::FLOAT32>::type; | ||
| using XPUType = typename XPUTypeTrait<Type>::Type; | ||
| auto f = [](xpu::Context* ctx, | ||
| const XPUType* x, | ||
| const XPUType* y, | ||
| XPUType* z, | ||
| const std::vector<int>& xshape, | ||
| const std::vector<int>& yshape) { | ||
| return xpu::broadcast_add<XPUType>(ctx, x, y, z, xshape, yshape); | ||
| }; | ||
| auto casted_y = phi::Cast<T>(dev_ctx, y, phi::DataType::FLOAT32); | ||
| XPUElementwise<Type, XPUType>(dev_ctx, x, casted_y, -1, out, f); | ||
| auto dev_version = | ||
| phi::backends::xpu::get_xpu_version(dev_ctx.GetPlace().GetDeviceId()); | ||
| if (dev_version >= phi::backends::xpu::XPUVersion::XPU3 && | ||
| x.dims() == y.dims()) { | ||
| dev_ctx.template Alloc<float>(out); | ||
|
|
||
| const float* x_data = x.data<float>(); | ||
| float* z_data = out->data<float>(); | ||
|
|
||
| int ret = xpu::SUCCESS; | ||
| if (y.dtype() == phi::DataType::BFLOAT16) { | ||
| using YType = DataTypeToCppType<phi::DataType::BFLOAT16>::type; | ||
| using XPUYType = typename XPUTypeTrait<YType>::Type; | ||
| auto y_data = reinterpret_cast<const XPUYType*>(y.data<YType>()); | ||
| ret = xpu::add_mul_type<float, XPUYType, float>( | ||
| dev_ctx.x_context(), x_data, y_data, z_data, x.numel()); | ||
| } else { | ||
| using YType = DataTypeToCppType<phi::DataType::FLOAT16>::type; | ||
| using XPUYType = typename XPUTypeTrait<YType>::Type; | ||
| auto y_data = reinterpret_cast<const XPUYType*>(y.data<YType>()); | ||
| ret = xpu::add_mul_type<float, XPUYType, float>( | ||
| dev_ctx.x_context(), x_data, y_data, z_data, x.numel()); | ||
| } | ||
| PADDLE_ENFORCE_XDNN_SUCCESS(ret, "add_mul_type"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的报错信息支持common::errors::XX 类型提示么?如果支持的话,建议加一下报错类型
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好的, 我研究下
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 目前原本的XPUElementwise里面也是调用的 |
||
| } else { | ||
| using Type = DataTypeToCppType<phi::DataType::FLOAT32>::type; | ||
| using XPUType = typename XPUTypeTrait<Type>::Type; | ||
| auto f = [](xpu::Context* ctx, | ||
| const XPUType* x, | ||
| const XPUType* y, | ||
| XPUType* z, | ||
| const std::vector<int>& xshape, | ||
| const std::vector<int>& yshape) { | ||
| return xpu::broadcast_add<XPUType>(ctx, x, y, z, xshape, yshape); | ||
| }; | ||
| auto casted_y = phi::Cast<T>(dev_ctx, y, phi::DataType::FLOAT32); | ||
| XPUElementwise<Type, XPUType>(dev_ctx, x, casted_y, -1, out, f); | ||
| } | ||
| } else { | ||
| using XPUType = typename XPUTypeTrait<T>::Type; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Aurelius84 @zxcd 麻烦看看CUDA版本是否是漏了add_算子,这样XPU和CUDA可以合并,add_(fp32, bf16/fp16)最一开始的需求是来自于main_grad的inplace_add:https://github.com/PaddlePaddle/Paddle/pull/54415/files#diff-1c8fea91efa1fa5f00f4b766fb11723120b74ec26365724cca4be8a0792946d9R62