/* 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/fluid/framework/tensor.h" #include "paddle/fluid/platform/place.h" #include "paddle/phi/kernels/xpu/elementwise.h" #include "xpu/refactor/math.h" namespace paddle { namespace operators { template void XPUElementwise(const framework::ExecutionContext& ctx, std::function&, const std::vector&)> func) { auto x_var = ctx.InputVar("X"); PADDLE_ENFORCE_NE( x_var, nullptr, platform::errors::InvalidArgument("Cannot get input Variable X")); PADDLE_ENFORCE_EQ( x_var->IsType(), true, platform::errors::InvalidArgument("XPU only support phi::DenseTensor, " "Input(X) is not phi::DenseTensor")); auto x = x_var->Get(); auto* y = ctx.Input("Y"); auto* z = ctx.Output("Out"); int axis = ctx.Attr("axis"); auto& dev_ctx = ctx.template device_context(); phi::XPUElementwise(dev_ctx, x, *y, axis, z, func); } template void XPUElementwiseGrad(const framework::ExecutionContext& ctx, std::function&, const std::vector&)> func, bool use_x_y_data) { auto* x = ctx.Input("X"); auto* y = ctx.Input("Y"); auto* dz = ctx.Input(framework::GradVarName("Out")); auto* dx = ctx.Output(framework::GradVarName("X")); auto* dy = ctx.Output(framework::GradVarName("Y")); int axis = ctx.Attr("axis"); auto& dev_ctx = ctx.template device_context(); phi::XPUElementwiseGrad( dev_ctx, *x, *y, *dz, axis, dx, dy, func, use_x_y_data); } } // namespace operators } // namespace paddle #endif