compare_all_op.cu 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

W
wawltor 已提交
15 16
#include <thrust/fill.h>
#include "paddle/fluid/operators/controlflow/compare_all_op.h"
17
#include "paddle/fluid/operators/elementwise/elementwise_op_impl.cu.h"
18
#include "paddle/fluid/operators/reduce_ops/cub_reduce.h"
19

20 21 22
namespace ops = paddle::operators;
namespace plat = paddle::platform;

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
namespace paddle {
namespace operators {

template <typename T>
struct IdentityFunctor {
  HOSTDEVICE explicit inline IdentityFunctor() {}
  HOSTDEVICE inline T operator()(const T& x) const { return x; }
};

struct BitwiseAdd {
  // Bitwise add operator, returns <tt>a + b</tt>
  template <typename T>
  __host__ __device__ __forceinline__ T operator()(const T& a,
                                                   const T& b) const {
    return a & b;
  }
};
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
template <typename T, typename Enable = void>
struct CudaEqualReduceFunctor {
  using ELEM_TYPE = T;
  HOSTDEVICE bool operator()(const T args[]) const {
    return (args[0] == args[1]);
  }
};

template <typename T>
struct CudaEqualReduceFunctor<
    T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
  using ELEM_TYPE = T;
  HOSTDEVICE bool operator()(const T args[]) const {
    return fabs(static_cast<double>(args[0] - args[1])) < 1e-8;
  }
};

58 59 60 61 62 63 64 65 66 67 68
template <typename DeviceContext, typename Functor>
class CompareReduceOpKernel
    : public framework::OpKernel<typename Functor::ELEM_TYPE> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    using T = typename Functor::ELEM_TYPE;
    using Tensor = framework::Tensor;

    auto* x = context.Input<Tensor>("X");
    auto* y = context.Input<Tensor>("Y");
    auto* z = context.Output<Tensor>("Out");
69
    bool* z_data = z->mutable_data<bool>(context.GetPlace());
70
    Tensor tmp;
W
wawltor 已提交
71

72
    if (x->dims() != y->dims()) {
W
wawltor 已提交
73 74 75 76
      thrust::device_ptr<bool> z_dev_ptr(z_data);
      thrust::fill(z_dev_ptr, z_dev_ptr + 1, false);
      return;
    } else {
77 78 79 80 81 82 83 84
      tmp.mutable_data<bool>(x->dims(), context.GetPlace());
      const auto& cuda_ctx =
          context.template device_context<platform::CUDADeviceContext>();
      std::vector<const framework::Tensor*> ins = {x, y};
      std::vector<framework::Tensor*> outs = {&tmp};
      LaunchSameDimsElementwiseCudaKernel<ElementwiseType::kBinary, T, bool>(
          cuda_ctx, ins, &outs, Functor());

W
wawltor 已提交
85 86 87 88 89 90 91 92 93
      // Reduce by 'bitwise and' operator
      std::vector<int> reduce_dims;
      reduce_dims.resize(tmp.dims().size());
      for (int i = 0; i < reduce_dims.size(); ++i) reduce_dims[i] = i;
      auto stream = context.cuda_device_context().stream();
      TensorReduce<bool, bool, BitwiseAdd, IdentityFunctor<bool>>(
          tmp, z, reduce_dims, true, BitwiseAdd(), IdentityFunctor<bool>(),
          stream);
    }
94 95 96 97 98 99
  }
};

}  // namespace operators
}  // namespace paddle

100 101 102 103 104 105 106 107 108 109 110 111
#define REGISTER_COMPARE_REDUCE_CUDA_KERNEL(op_type, functor)                  \
  REGISTER_OP_CUDA_KERNEL(                                                     \
      op_type,                                                                 \
      ops::CompareReduceOpKernel<plat::CUDADeviceContext, ops::functor<bool>>, \
      ops::CompareReduceOpKernel<plat::CUDADeviceContext, ops::functor<int>>,  \
      ops::CompareReduceOpKernel<plat::CUDADeviceContext,                      \
                                 ops::functor<int64_t>>,                       \
      ops::CompareReduceOpKernel<plat::CUDADeviceContext,                      \
                                 ops::functor<float>>,                         \
      ops::CompareReduceOpKernel<plat::CUDADeviceContext,                      \
                                 ops::functor<double>>);

112
REGISTER_COMPARE_REDUCE_CUDA_KERNEL(equal_all, CudaEqualReduceFunctor)
113
#undef REGISTER_COMPARE_REDUCE_CUDA_KERNEL