logical_op_xpu.h 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/* 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 <algorithm>
#include <string>
#include <vector>

#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 <XpuLogicalType xpu_type, typename T>
class BinaryLogicalOpXPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
45 46 47
    auto* x = context.Input<phi::DenseTensor>("X");
    auto* y = context.Input<phi::DenseTensor>("Y");
    auto* out = context.Output<phi::DenseTensor>("Out");
48
    bool* out_ptr = out->mutable_data<bool>(context.GetPlace());
49 50 51 52
    const T* x_ptr = x->data<T>();
    const T* y_ptr = y->data<T>();
    auto& dev_ctx =
        context.template device_context<paddle::platform::XPUDeviceContext>();
53 54
    phi::DenseTensor broadcast_x;
    phi::DenseTensor broadcast_y;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    bool need_broad_cast = false;
    if (x->numel() != out->numel()) {
      // x need broadcast
      T* broadcast_x_ptr =
          broadcast_x.mutable_data<T>(context.GetPlace(), out->numel());
      auto& out_dim = out->dims();
      auto& x_dim = x->dims();
      int dims = out_dim.size();
      std::vector<int> bcast_xdims;
      std::vector<int> 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]);
      }

77 78 79 80 81 82 83 84
      int ret =
          xpu::broadcast<int8_t>(dev_ctx.x_context(),
                                 reinterpret_cast<const int8_t*> x_ptr,
                                 reinterpret_cast<int8_t*> broadcast_x_ptr,
                                 bcast_xdims,
                                 bcast_ydims);
      PADDLE_ENFORCE_EQ(ret,
                        XPU_SUCCESS,
85 86
                        platform::errors::External(
                            "XPU broadcast kernel return wrong value[%d %s]",
87 88
                            ret,
                            XPUAPIErrorMsg[ret]));
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      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<T>(context.GetPlace(), out->numel());
      auto& out_dim = out->dims();
      auto& y_dim = y->dims();
      int dims = out_dim.size();
      std::vector<int> bcast_xdims;
      std::vector<int> 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]);
      }

113 114 115 116 117 118 119 120
      int ret =
          xpu::broadcast<int8_t>(dev_ctx.x_context(),
                                 reinterpret_cast<const int8_t*> y_ptr,
                                 reinterpret_cast<int8_t*> broadcast_y_ptr,
                                 bcast_xdims,
                                 bcast_ydims);
      PADDLE_ENFORCE_EQ(ret,
                        XPU_SUCCESS,
121 122
                        platform::errors::External(
                            "XPU broadcast kernel return wrong value[%d %s]",
123 124
                            ret,
                            XPUAPIErrorMsg[ret]));
125 126 127 128 129 130 131 132
      y_ptr = (const T*)broadcast_y_ptr;
      need_broad_cast = true;
    }

    // logical kernel
    int ret = XPU_SUCCESS;
    switch (xpu_type) {
      case XpuLogicalType::XPU_OR:
133 134
        ret = xpu::logical_or<bool>(
            dev_ctx.x_context(), x_ptr, y_ptr, out_ptr, out->numel());
135 136
        break;
      case XpuLogicalType::XPU_AND:
137 138
        ret = xpu::logical_and<bool>(
            dev_ctx.x_context(), x_ptr, y_ptr, out_ptr, out->numel());
139 140 141 142 143 144
      default:
        LOG(ERROR) << "xpu not support logical xpu type = "
                   << XpuLogicalType2Str(xpu_type);
        break;
    }
    PADDLE_ENFORCE_EQ(
145 146
        ret,
        XPU_SUCCESS,
147 148
        platform::errors::External("XPU API return wrong value[%d %s] in "
                                   "op_name[%s].",
149 150
                                   ret,
                                   XPUAPIErrorMsg[ret],
151 152 153 154 155 156 157 158 159 160 161 162
                                   XpuLogicalType2Str(xpu_type)));

    if (need_broad_cast && dev_ctx.x_context()->xpu_stream != nullptr) {
      xpu_wait();
    }
  }
};

template <typename T>
class UnaryLogicalOpXPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
163 164
    auto* x = context.Input<phi::DenseTensor>("X");
    auto* out = context.Output<phi::DenseTensor>("Out");
165 166 167
    if (x->numel() == 0) {
      return;
    }
168
    out->mutable_data<bool>(context.GetPlace());
169 170
    auto& dev_ctx =
        context.template device_context<paddle::platform::XPUDeviceContext>();
171 172
    int ret = xpu::logical_not<bool>(
        dev_ctx.x_context(), x->data<T>(), out->data<T>(), x->numel());
173
    PADDLE_ENFORCE_EQ(
174 175 176 177
        ret,
        XPU_SUCCESS,
        platform::errors::External(
            "XPU API return wrong value[%d %s].", ret, XPUAPIErrorMsg[ret]));
178 179 180 181 182 183
  }
};

}  // namespace operators
}  // namespace paddle
#endif