dropout_op.cu 3.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
X
Xinghai Sun 已提交
2

L
Luo Tao 已提交
3 4 5
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
X
Xinghai Sun 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
X
Xinghai Sun 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
14

P
phlrain 已提交
15
#include <string>
16 17 18

#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/dropout_impl.cu.h"
Y
Yi Wang 已提交
19
#include "paddle/fluid/operators/dropout_op.h"
K
Kexin Zhao 已提交
20
#include "paddle/fluid/platform/float16.h"
21

22 23 24 25 26 27
namespace paddle {
namespace operators {

// It seems that Eigen::Tensor::setRandom in GPU will SEGFAULT.
// Use std::random and thrust::random(thrust is a std library in CUDA) to
// implement uniform random.
K
Kexin Zhao 已提交
28
template <typename Place, typename T>
Y
Yu Yang 已提交
29
class GPUDropoutKernel : public framework::OpKernel<T> {
30 31 32
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* x = context.Input<Tensor>("X");
M
mapingshuo 已提交
33 34
    auto* seed =
        context.HasInput("Seed") ? context.Input<Tensor>("Seed") : nullptr;
35 36
    auto* y = context.Output<Tensor>("Out");
    y->mutable_data<T>(context.GetPlace());
K
Kexin Zhao 已提交
37
    float dropout_prob = context.Attr<float>("dropout_prob");
38

Z
Zeng Jinle 已提交
39
    auto& dropout_implementation =
P
phlrain 已提交
40
        context.Attr<std::string>("dropout_implementation");
Z
Zeng Jinle 已提交
41 42
    bool upscale_in_train = (dropout_implementation == "upscale_in_train");

43
    bool is_test = context.Attr<bool>("is_test");
44

45 46 47
    auto& dev_ctx = context.cuda_device_context();
    auto* mask = context.Output<Tensor>("Mask");
    mask->mutable_data<uint8_t>(context.GetPlace());
Z
Zhang Ting 已提交
48

49 50 51 52 53 54 55
    bool is_fix_seed = context.Attr<bool>("fix_seed");
    int seed_val = context.Attr<int>("seed");
    DropoutFwGPUKernelDriver<T>(dev_ctx, is_test, dropout_implementation,
                                dropout_prob, upscale_in_train, is_fix_seed,
                                seed_val, *x, seed, mask, y);
  }
};
Z
Zhang Ting 已提交
56

57 58 59 60 61 62 63 64 65 66 67 68
template <typename DeviceContext, typename T>
class GPUDropoutGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* grad_x = context.Output<Tensor>(framework::GradVarName("X"));
    auto* grad_y = context.Input<Tensor>(framework::GradVarName("Out"));
    auto* mask = context.Input<Tensor>("Mask");
    grad_x->mutable_data<T>(context.GetPlace());
    auto size = grad_x->numel();
    auto& dropout_implementation =
        context.Attr<std::string>("dropout_implementation");
    float dropout_prob = context.Attr<float>("dropout_prob");
69

70 71
    bool is_test = context.Attr<bool>("is_test");

72 73 74
    auto& dev_ctx =
        context.template device_context<platform::CUDADeviceContext>();
    DropoutGradGPUKernelDriver<T>(dev_ctx, dropout_implementation, dropout_prob,
75
                                  *grad_y, *mask, size, grad_x, is_test);
76 77 78 79 80 81
  }
};

}  // namespace operators
}  // namespace paddle

X
Xinghai Sun 已提交
82
namespace ops = paddle::operators;
K
Kexin Zhao 已提交
83
namespace plat = paddle::platform;
Q
QI JUN 已提交
84
REGISTER_OP_CUDA_KERNEL(
K
Kexin Zhao 已提交
85
    dropout, ops::GPUDropoutKernel<plat::CUDADeviceContext, float>,
P
phlrain 已提交
86 87 88
    ops::GPUDropoutKernel<plat::CUDADeviceContext, plat::float16>,
    ops::GPUDropoutKernel<plat::CUDADeviceContext, double>);
REGISTER_OP_CUDA_KERNEL(
89 90 91
    dropout_grad, ops::GPUDropoutGradKernel<plat::CUDADeviceContext, float>,
    ops::GPUDropoutGradKernel<plat::CUDADeviceContext, plat::float16>,
    ops::GPUDropoutGradKernel<plat::CUDADeviceContext, double>);