Skip to content

Commit 05ad158

Browse files
committed
initial commit
1 parent c042137 commit 05ad158

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

paddle/fluid/operators/dropout_op.cu

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License. */
1818
#include <thrust/random.h>
1919
#include <thrust/transform.h>
2020
#include "paddle/fluid/operators/dropout_op.h"
21+
#include "paddle/fluid/platform/float16.h"
2122

2223
namespace paddle {
2324
namespace operators {
@@ -51,7 +52,7 @@ class GPUDropoutKernel : public framework::OpKernel<T> {
5152
auto* x = context.Input<Tensor>("X");
5253
auto* y = context.Output<Tensor>("Out");
5354
y->mutable_data<T>(context.GetPlace());
54-
AttrType dropout_prob = context.Attr<AttrType>("dropout_prob");
55+
AttrType dropout_prob = context.Attr<AttrType>("dropout_prob"));
5556

5657
auto X = EigenMatrix<T>::Reshape(*x, 1);
5758
auto Y = EigenMatrix<T>::Reshape(*y, 1);
@@ -74,7 +75,7 @@ class GPUDropoutKernel : public framework::OpKernel<T> {
7475
context.cuda_device_context().stream()>>>(
7576
size, seed, dropout_prob, x_data, mask_data, y_data);
7677
} else {
77-
Y.device(place) = X * (1.0f - dropout_prob);
78+
Y.device(place) = X * static_cast<T>(1.0f - dropout_prob);
7879
}
7980
}
8081
};
@@ -83,9 +84,9 @@ class GPUDropoutKernel : public framework::OpKernel<T> {
8384
} // namespace paddle
8485

8586
namespace ops = paddle::operators;
87+
namespace plat = paddle::platform;
8688
REGISTER_OP_CUDA_KERNEL(
87-
dropout,
88-
ops::GPUDropoutKernel<paddle::platform::CUDADeviceContext, float, float>);
89-
REGISTER_OP_CUDA_KERNEL(
90-
dropout_grad,
91-
ops::DropoutGradKernel<paddle::platform::CUDADeviceContext, float>);
89+
dropout, ops::GPUDropoutKernel<plat::CUDADeviceContext, float, float>,
90+
ops::GPUDropoutKernel<plat::CUDADeviceContext, plat::float16, float>);
91+
REGISTER_OP_CUDA_KERNEL(dropout_grad,
92+
ops::DropoutGradKernel<plat::CUDADeviceContext, float>);

python/paddle/fluid/tests/unittests/test_dropout_op.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,31 @@ def test_check_output(self):
8282
self.check_output()
8383

8484

85+
class TestFP16DropoutOp1(OpTest):
86+
def setUp(self):
87+
x = np.random.random((32, 64)).astype("float16")
88+
self.op_type = "dropout"
89+
self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
90+
self.attrs = {'dropout_prob': 0.35, 'fix_seed': True, 'is_test': True}
91+
self.outputs = {'Out': x * (1.0 - self.attrs['dropout_prob'])}
92+
93+
def test_check_output(self):
94+
if core.is_compiled_with_cuda() and core.op_support_gpu("dropout"):
95+
self.check_output_with_place(core.CUDAPlace(0), atol=1e-3)
96+
97+
98+
class TestFP16DropoutOp2(OpTest):
99+
def setUp(self):
100+
x = np.random.random((32, 64, 3)).astype("float16")
101+
self.op_type = "dropout"
102+
self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
103+
self.attrs = {'dropout_prob': 0.75, 'is_test': True}
104+
self.outputs = {'Out': x * (1.0 - self.attrs['dropout_prob'])}
105+
106+
def test_check_output(self):
107+
if core.is_compiled_with_cuda() and core.op_support_gpu("dropout"):
108+
self.check_output_with_place(core.CUDAPlace(0), atol=1e-3)
109+
110+
85111
if __name__ == '__main__':
86112
unittest.main()

0 commit comments

Comments
 (0)