/* Copyright (c) 2016 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 "paddle/fluid/framework/op_registry.h" #include "xpu/refactor/math.h" namespace paddle { namespace operators { typedef enum { XPU_OR, XPU_AND } XpuLogicalType; std::string XpuLogicalType2Str(XpuLogicalType ty) { switch (ty) { case XpuLogicalType::XPU_OR: return std::string("logical or"); case XpuLogicalType::XPU_AND: return std::string("logical and"); default: return std::string("unknown type"); } return std::string("unknown"); } template class BinaryLogicalOpXPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* x = context.Input("X"); auto* y = context.Input("Y"); auto* out = context.Output("Out"); bool* out_ptr = out->mutable_data(context.GetPlace()); const T* x_ptr = x->data(); const T* y_ptr = y->data(); auto& dev_ctx = context.template device_context(); phi::DenseTensor broadcast_x; phi::DenseTensor broadcast_y; bool need_broad_cast = false; if (x->numel() != out->numel()) { // x need broadcast T* broadcast_x_ptr = broadcast_x.mutable_data(context.GetPlace(), out->numel()); auto& out_dim = out->dims(); auto& x_dim = x->dims(); int dims = out_dim.size(); std::vector bcast_xdims; std::vector bcast_ydims; for (int i = 0; i < dims; ++i) { if (out_dim[i] == x_dim[i]) { bcast_xdims.push_back(x_dim[i]); bcast_ydims.push_back(x_dim[i]); continue; } bcast_xdims.push_back(1); bcast_xdims.push_back(x_dim[i]); bcast_ydims.push_back(out_dim[i] / x_dim[i]); bcast_ydims.push_back(x_dim[i]); } int ret = xpu::broadcast(dev_ctx.x_context(), reinterpret_cast x_ptr, reinterpret_cast broadcast_x_ptr, bcast_xdims, bcast_ydims); PADDLE_ENFORCE_EQ(ret, XPU_SUCCESS, platform::errors::External( "XPU broadcast kernel return wrong value[%d %s]", ret, XPUAPIErrorMsg[ret])); x_ptr = (const T*)broadcast_x_ptr; need_broad_cast = true; } if (y->numel() != out->numel()) { // y need broadcast T* broadcast_y_ptr = broadcast_y.mutable_data(context.GetPlace(), out->numel()); auto& out_dim = out->dims(); auto& y_dim = y->dims(); int dims = out_dim.size(); std::vector bcast_xdims; std::vector bcast_ydims; for (int i = 0; i < dims; ++i) { if (out_dim[i] == y_dim[i]) { bcast_xdims.push_back(y_dim[i]); bcast_ydims.push_back(y_dim[i]); continue; } bcast_xdims.push_back(1); bcast_xdims.push_back(y_dim[i]); bcast_ydims.push_back(out_dim[i] / y_dim[i]); bcast_ydims.push_back(y_dim[i]); } int ret = xpu::broadcast(dev_ctx.x_context(), reinterpret_cast y_ptr, reinterpret_cast broadcast_y_ptr, bcast_xdims, bcast_ydims); PADDLE_ENFORCE_EQ(ret, XPU_SUCCESS, platform::errors::External( "XPU broadcast kernel return wrong value[%d %s]", ret, XPUAPIErrorMsg[ret])); y_ptr = (const T*)broadcast_y_ptr; need_broad_cast = true; } // logical kernel int ret = XPU_SUCCESS; switch (xpu_type) { case XpuLogicalType::XPU_OR: ret = xpu::logical_or( dev_ctx.x_context(), x_ptr, y_ptr, out_ptr, out->numel()); break; case XpuLogicalType::XPU_AND: ret = xpu::logical_and( dev_ctx.x_context(), x_ptr, y_ptr, out_ptr, out->numel()); default: LOG(ERROR) << "xpu not support logical xpu type = " << XpuLogicalType2Str(xpu_type); break; } PADDLE_ENFORCE_EQ( ret, XPU_SUCCESS, platform::errors::External("XPU API return wrong value[%d %s] in " "op_name[%s].", ret, XPUAPIErrorMsg[ret], XpuLogicalType2Str(xpu_type))); if (need_broad_cast && dev_ctx.x_context()->xpu_stream != nullptr) { xpu_wait(); } } }; template class UnaryLogicalOpXPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* x = context.Input("X"); auto* out = context.Output("Out"); if (x->numel() == 0) { return; } out->mutable_data(context.GetPlace()); auto& dev_ctx = context.template device_context(); int ret = xpu::logical_not( dev_ctx.x_context(), x->data(), out->data(), x->numel()); PADDLE_ENFORCE_EQ( ret, XPU_SUCCESS, platform::errors::External( "XPU API return wrong value[%d %s].", ret, XPUAPIErrorMsg[ret])); } }; } // namespace operators } // namespace paddle #endif