/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once #ifdef PADDLE_WITH_XPU #include #include #include #include #include #include "paddle/phi/backends/xpu/xpu_context.h" #include "paddle/phi/common/place.h" #include "paddle/phi/core/dense_tensor.h" #include "xpu/refactor/math.h" namespace phi { template void XPUElementwise(const XPUContext& dev_ctx, const DenseTensor& x, const DenseTensor& y, int axis, DenseTensor* z, std::function&, const std::vector&)> func) { dev_ctx.template Alloc(z); auto x_dims = x.dims(); auto y_dims = y.dims(); int max_dim = std::max(x_dims.size(), y_dims.size()); axis = (axis == -1 ? std::abs(x_dims.size() - y_dims.size()) : axis); PADDLE_ENFORCE_GE( axis, 0, errors::InvalidArgument( "Axis should be great than or equal to 0, but received axis is %d.", axis)); PADDLE_ENFORCE_LT(axis, max_dim, errors::InvalidArgument( "Axis should be less than %d, but received axis is %d.", max_dim, axis)); std::vector x_dims_vec(max_dim, 1); std::vector y_dims_vec(max_dim, 1); if (x_dims.size() == max_dim) { for (int i = 0; i < max_dim; i++) { x_dims_vec[i] = x_dims[i]; } } else { for (int i = 0; i < x_dims.size(); i++) { x_dims_vec[i + axis] = x_dims[i]; } } if (y_dims.size() == max_dim) { for (int i = 0; i < max_dim; i++) { y_dims_vec[i] = y_dims[i]; } } else { for (int i = 0; i < y_dims.size(); i++) { y_dims_vec[i + axis] = y_dims[i]; } } const T* x_data = x.data(); const T* y_data = y.data(); T* z_data = z->data(); int ret = xpu::SUCCESS; ret = func(dev_ctx.x_context(), reinterpret_cast(x_data), reinterpret_cast(y_data), reinterpret_cast(z_data), x_dims_vec, y_dims_vec); PADDLE_ENFORCE_EQ( ret, xpu::SUCCESS, errors::External( "XPU kernel Elementwise occur error in XPUElementwise error code ", ret, XPUAPIErrorMsg[ret])); } template void XPUElementwiseGrad(const XPUContext& dev_ctx, const DenseTensor& x, const DenseTensor& y, const DenseTensor& dz, int axis, DenseTensor* dx, DenseTensor* dy, std::function&, const std::vector&)> func, bool use_x_y_data) { auto* z = &dz; const DDim& x_dims = x.dims(); const DDim& y_dims = y.dims(); int max_dim = std::max(x_dims.size(), y_dims.size()); axis = (axis == -1 ? std::abs(x_dims.size() - y_dims.size()) : axis); PADDLE_ENFORCE_GE( axis, 0, errors::InvalidArgument( "Axis should be great than or equal to 0, but received axis is %d.", axis)); PADDLE_ENFORCE_LT(axis, max_dim, errors::InvalidArgument( "Axis should be less than %d, but received axis is %d.", max_dim, axis)); std::vector x_dims_vec(max_dim, 1); std::vector y_dims_vec(max_dim, 1); if (x_dims.size() == max_dim) { for (int i = 0; i < max_dim; i++) { x_dims_vec[i] = x_dims[i]; } } else { for (int i = 0; i < x_dims.size(); i++) { x_dims_vec[i + axis] = x_dims[i]; } } if (y_dims.size() == max_dim) { for (int i = 0; i < max_dim; i++) { y_dims_vec[i] = y_dims[i]; } } else { for (int i = 0; i < y_dims.size(); i++) { y_dims_vec[i + axis] = y_dims[i]; } } const T* x_data = use_x_y_data ? x.data() : z->data(); const T* y_data = use_x_y_data ? y.data() : z->data(); const T* z_data = z->data(); const T* dz_data = dz.data(); T* dx_data = nullptr; T* dy_data = nullptr; if (dx) { dx_data = dev_ctx.template Alloc(dx); } if (dy) { dy_data = dev_ctx.template Alloc(dy); } int ret = func(dev_ctx.x_context(), reinterpret_cast(x_data), reinterpret_cast(y_data), reinterpret_cast(z_data), reinterpret_cast(dz_data), reinterpret_cast(dy_data), reinterpret_cast(dx_data), x_dims_vec, y_dims_vec); PADDLE_ENFORCE_EQ( ret, xpu::SUCCESS, errors::External( "XPU kernel Elementwise occur error in XPUElementwise error code ", ret, XPUAPIErrorMsg[ret])); } } // namespace phi #endif