logical_op_xpu.h 6.0 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 45 46 47
/* 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 {
    auto* x = context.Input<framework::Tensor>("X");
    auto* y = context.Input<framework::Tensor>("Y");
    auto* out = context.Output<framework::Tensor>("Out");
48
    bool* out_ptr = out->mutable_data<bool>(context.GetPlace());
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    const T* x_ptr = x->data<T>();
    const T* y_ptr = y->data<T>();
    auto& dev_ctx =
        context.template device_context<paddle::platform::XPUDeviceContext>();
    framework::Tensor broadcast_x;
    framework::Tensor broadcast_y;
    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]);
      }

      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,
                        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<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]);
      }

      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,
                        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<bool>(dev_ctx.x_context(), x_ptr, y_ptr, out_ptr,
                                    out->numel());
        break;
      case XpuLogicalType::XPU_AND:
        ret = xpu::logical_and<bool>(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 <typename T>
class UnaryLogicalOpXPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* x = context.Input<framework::Tensor>("X");
    auto* out = context.Output<framework::Tensor>("Out");
    if (x->numel() == 0) {
      return;
    }
156
    out->mutable_data<bool>(context.GetPlace());
157 158 159 160 161 162 163 164 165 166 167 168 169 170
    auto& dev_ctx =
        context.template device_context<paddle::platform::XPUDeviceContext>();
    int ret = xpu::logical_not<bool>(dev_ctx.x_context(), x->data<T>(),
                                     out->data<T>(), 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