dropout_op.cu 3.8 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"
20
#include "paddle/fluid/platform/bfloat16.h"
K
Kexin Zhao 已提交
21
#include "paddle/fluid/platform/float16.h"
22

23 24 25 26 27 28
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 已提交
29
template <typename Place, typename T>
Y
Yu Yang 已提交
30
class GPUDropoutKernel : public framework::OpKernel<T> {
31 32 33
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* x = context.Input<Tensor>("X");
M
mapingshuo 已提交
34 35
    auto* seed =
        context.HasInput("Seed") ? context.Input<Tensor>("Seed") : nullptr;
36 37
    auto* y = context.Output<Tensor>("Out");
    y->mutable_data<T>(context.GetPlace());
K
Kexin Zhao 已提交
38
    float dropout_prob = context.Attr<float>("dropout_prob");
39

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

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

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

50 51 52 53 54 55 56
    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 已提交
57

58 59 60 61 62 63 64 65 66 67 68 69
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");
70

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

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

}  // namespace operators
}  // namespace paddle

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