bernoulli_op.cu 3.2 KB
Newer Older
L
Leo Chen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2020 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 <thrust/execution_policy.h>
#include <thrust/random.h>
#include <thrust/transform.h>

19
#include "paddle/fluid/framework/generator.h"
L
Leo Chen 已提交
20 21 22 23 24 25 26 27 28 29 30
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/operators/bernoulli_op.h"
#include "paddle/fluid/platform/transform.h"

namespace paddle {
namespace operators {
// it can be consistent with cpu when CUDAGenerator is provided.
template <typename T>
struct BernoulliCudaFunctor {
  unsigned int seed_;
31 32 33 34
  unsigned int offset_;
  __host__ __device__ BernoulliCudaFunctor(unsigned int seed,
                                           unsigned int offset)
      : seed_(seed), offset_(offset) {}
L
Leo Chen 已提交
35 36

  __host__ __device__ T operator()(const unsigned int n, const T p) const {
37 38 39 40
    // NOTE(zhiqiu): currently, PADDLE_ENFORCE in cuda kernel may print several
    // lines of error messages if, and it should be refined.
    PADDLE_ENFORCE(p >= 0.0 && p <= 1.0,
                   "The probability should be >=0 and <= 1, but got %f", p);
L
Leo Chen 已提交
41 42 43
    thrust::minstd_rand rng;
    rng.seed(seed_);
    thrust::uniform_real_distribution<T> dist(0.0, 1.0);
44
    rng.discard(n + offset_);
L
Leo Chen 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58
    return static_cast<T>(dist(rng) < p);
  }
};

template <typename T>
class BernoulliOpKernel<platform::CUDADeviceContext, T>
    : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    const auto x = ctx.Input<framework::Tensor>("X");
    auto out = ctx.Output<framework::Tensor>("Out");
    auto* in_data = x->data<T>();
    auto* out_data = out->mutable_data<T>(ctx.GetPlace());
    int64_t size = x->numel();
59 60 61 62 63 64

    int device_id =
        BOOST_GET_CONST(platform::CUDAPlace, ctx.GetPlace()).GetDeviceId();
    auto gen_cuda = framework::GetDefaultCUDAGenerator(device_id);
    auto seed_offset = gen_cuda->IncrementOffset(1);
    int gen_offset = size * seed_offset.second;
L
Leo Chen 已提交
65
    platform::Transform<platform::CUDADeviceContext> trans;
66
    thrust::counting_iterator<unsigned int> index_sequence_begin(0);
L
Leo Chen 已提交
67 68
    auto* context =
        static_cast<const platform::CUDADeviceContext*>(&ctx.device_context());
69

L
Leo Chen 已提交
70
    trans(*context, index_sequence_begin, index_sequence_begin + size, in_data,
71 72 73
          out_data,
          BernoulliCudaFunctor<T>(static_cast<unsigned int>(seed_offset.first),
                                  static_cast<unsigned int>(gen_offset)));
L
Leo Chen 已提交
74 75 76 77 78 79 80 81 82 83 84 85
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;

REGISTER_OP_CUDA_KERNEL(
    bernoulli, ops::BernoulliOpKernel<plat::CUDADeviceContext, float>,
    ops::BernoulliOpKernel<plat::CUDADeviceContext, double>);