logical_kernel.cpp 3.4 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 48 49 50 51 52 53 54 55 56 57 58
/* Copyright (c) 2018 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. */

#include "operators/kernel/logical_kernel.h"

namespace paddle_mobile {
namespace operators {

template <typename T>
struct LogicalAndFunctor {
  bool operator()(const T& a, const T& b) const { return a && b; }
};

template <typename T>
struct LogicalOrFunctor {
  bool operator()(const T& a, const T& b) const { return a || b; }
};

template <typename T>
struct LogicalNotFunctor {
  bool operator()(const T& a) const { return !a; }
};

template <typename T>
struct LogicalXorFunctor {
  bool operator()(const T& a, const T& b) const {
    return (a || b) && !(a && b);
  }
};

template <typename T, typename Functor>
void UnaryLogicalCompute(const Tensor* inputX, Tensor* output) {
  Functor func;
  std::transform(inputX->data<T>(), inputX->data<T>() + inputX->numel(),
                 output->data<T>(), func);
}

template <typename T, typename Functor>
void BinaryLogicalCompute(const Tensor* inputX, const Tensor* inputY,
                          Tensor* output) {
  Functor func;
  std::transform(inputX->data<T>(), inputX->data<T>() + inputX->numel(),
                 inputY->data<T>(), output->data<T>(), func);
}

#ifdef LOGICAL_AND_OP
template <>
Z
zhaojiaying01 已提交
59
bool LogicalAndKernel<CPU, float>::Init(LogicalBinaryParam<CPU>* param) {
60 61 62 63
  return true;
}

template <>
Z
zhaojiaying01 已提交
64 65
void LogicalAndKernel<CPU, float>::Compute(
    const LogicalBinaryParam<CPU>& param) {
66 67 68 69 70 71 72 73 74 75
  auto* inputX = param.InputX();
  auto* inputY = param.InputY();
  auto* out = param.Out();
  out->mutable_data<bool>();
  BinaryLogicalCompute<bool, LogicalAndFunctor<bool>>(inputX, inputY, out);
}
#endif

#ifdef LOGICAL_OR_OP
template <>
Z
zhaojiaying01 已提交
76
bool LogicalOrKernel<CPU, float>::Init(LogicalBinaryParam<CPU>* param) {
77 78 79 80
  return true;
}

template <>
Z
zhaojiaying01 已提交
81 82
void LogicalOrKernel<CPU, float>::Compute(
    const LogicalBinaryParam<CPU>& param) {
83 84 85 86 87 88 89 90 91 92
  auto* inputX = param.InputX();
  auto* inputY = param.InputY();
  auto* out = param.Out();
  out->mutable_data<bool>();
  BinaryLogicalCompute<bool, LogicalOrFunctor<bool>>(inputX, inputY, out);
}
#endif

#ifdef LOGICAL_NOT_OP
template <>
Z
zhaojiaying01 已提交
93
bool LogicalNotKernel<CPU, float>::Init(LogicalUnaryParam<CPU>* param) {
94 95 96 97
  return true;
}

template <>
Z
zhaojiaying01 已提交
98 99
void LogicalNotKernel<CPU, float>::Compute(
    const LogicalUnaryParam<CPU>& param) {
100 101 102 103 104 105 106 107 108
  auto* inputX = param.InputX();
  auto* out = param.Out();
  out->mutable_data<bool>();
  UnaryLogicalCompute<bool, LogicalNotFunctor<bool>>(inputX, out);
}
#endif

#ifdef LOGICAL_XOR_OP
template <>
Z
zhaojiaying01 已提交
109
bool LogicalXorKernel<CPU, float>::Init(LogicalBinaryParam<CPU>* param) {
110 111 112 113
  return true;
}

template <>
Z
zhaojiaying01 已提交
114 115
void LogicalXorKernel<CPU, float>::Compute(
    const LogicalBinaryParam<CPU>& param) {
116 117 118 119 120 121 122 123 124 125
  auto* inputX = param.InputX();
  auto* inputY = param.InputY();
  auto* out = param.Out();
  out->mutable_data<bool>();
  BinaryLogicalCompute<bool, LogicalXorFunctor<bool>>(inputX, inputY, out);
}
#endif

}  // namespace operators
}  // namespace paddle_mobile