From 497b40b8c1f25c1178c627c4da9b813fba7427ac Mon Sep 17 00:00:00 2001 From: gongweibao Date: Mon, 26 Mar 2018 04:52:02 +0000 Subject: [PATCH 1/8] add drop test --- .../fluid/tests/unittests/test_dropout_op.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_dropout_op.py b/python/paddle/fluid/tests/unittests/test_dropout_op.py index eaa3435a86462..d6db1a00e4927 100644 --- a/python/paddle/fluid/tests/unittests/test_dropout_op.py +++ b/python/paddle/fluid/tests/unittests/test_dropout_op.py @@ -15,6 +15,7 @@ import unittest import numpy as np import paddle.fluid.core as core +import paddle.fluid as fluid from op_test import OpTest @@ -83,6 +84,39 @@ def test_check_output(self): self.check_output() +#Reference: https://github.com/PaddlePaddle/Paddle/issues/8654 +class TestDropoutOp6(unittest.TestCase): + def program(self): + x = fluid.layers.data( + name='x', + shape=[64, 32, 512], + dtype='float32', + append_batch_size=False) + out = fluid.layers.dropout(x, dropout_prob=0.1, is_test=False) + return out + + def LoopMaxMin(self, place): + exe = fluid.Executor(place) + out = self.program() + data_input = {} + in_tensor = fluid.LoDTensor() + in_tensor.set(np.ones([64, 32, 512], dtype="float32"), place) + data_input['x'] = in_tensor + for i in range(10): + out_ = exe.run(fluid.framework.default_main_program(), + feed=data_input, + fetch_list=[out])[0] + self.assertTrue(abs(np.max(out_) - 1.0) <= 0.000001) + self.assertTrue(abs(np.min(out_) - 0.0) <= 0.000001) + + def test_all(self): + place = fluid.CUDAPlace(0) + self.LoopMaxMin(place) + + place = fluid.CPUPlace() + self.LoopMaxMin(place) + + class TestFP16DropoutOp(OpTest): def setUp(self): self.op_type = "dropout" From ae933e483e4ec810226e75a096b6597a77729c9b Mon Sep 17 00:00:00 2001 From: gongweibao Date: Mon, 26 Mar 2018 04:55:10 +0000 Subject: [PATCH 2/8] modify --- python/paddle/fluid/tests/unittests/test_dropout_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_dropout_op.py b/python/paddle/fluid/tests/unittests/test_dropout_op.py index d6db1a00e4927..59c5a2e133cab 100644 --- a/python/paddle/fluid/tests/unittests/test_dropout_op.py +++ b/python/paddle/fluid/tests/unittests/test_dropout_op.py @@ -89,7 +89,7 @@ class TestDropoutOp6(unittest.TestCase): def program(self): x = fluid.layers.data( name='x', - shape=[64, 32, 512], + shape=[16, 32, 33], dtype='float32', append_batch_size=False) out = fluid.layers.dropout(x, dropout_prob=0.1, is_test=False) From ee2093945d48739b45151776a65e0d4acf993bed Mon Sep 17 00:00:00 2001 From: gongweibao Date: Mon, 26 Mar 2018 04:56:17 +0000 Subject: [PATCH 3/8] restore --- python/paddle/fluid/tests/unittests/test_dropout_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_dropout_op.py b/python/paddle/fluid/tests/unittests/test_dropout_op.py index 59c5a2e133cab..d6db1a00e4927 100644 --- a/python/paddle/fluid/tests/unittests/test_dropout_op.py +++ b/python/paddle/fluid/tests/unittests/test_dropout_op.py @@ -89,7 +89,7 @@ class TestDropoutOp6(unittest.TestCase): def program(self): x = fluid.layers.data( name='x', - shape=[16, 32, 33], + shape=[64, 32, 512], dtype='float32', append_batch_size=False) out = fluid.layers.dropout(x, dropout_prob=0.1, is_test=False) From daf67710de0e54281d72c0b4ec68d77e53c55f75 Mon Sep 17 00:00:00 2001 From: gongweibao Date: Mon, 26 Mar 2018 06:45:56 +0000 Subject: [PATCH 4/8] add isclose --- .../fluid/tests/unittests/test_dropout_op.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_dropout_op.py b/python/paddle/fluid/tests/unittests/test_dropout_op.py index d6db1a00e4927..af82a8720447d 100644 --- a/python/paddle/fluid/tests/unittests/test_dropout_op.py +++ b/python/paddle/fluid/tests/unittests/test_dropout_op.py @@ -84,6 +84,10 @@ def test_check_output(self): self.check_output() +def isclose(a, b, rel_tol=1e-09, abs_tol=0.0): + return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) + + #Reference: https://github.com/PaddlePaddle/Paddle/issues/8654 class TestDropoutOp6(unittest.TestCase): def program(self): @@ -103,11 +107,14 @@ def LoopMaxMin(self, place): in_tensor.set(np.ones([64, 32, 512], dtype="float32"), place) data_input['x'] = in_tensor for i in range(10): - out_ = exe.run(fluid.framework.default_main_program(), - feed=data_input, - fetch_list=[out])[0] - self.assertTrue(abs(np.max(out_) - 1.0) <= 0.000001) - self.assertTrue(abs(np.min(out_) - 0.0) <= 0.000001) + ret_out = exe.run(fluid.framework.default_main_program(), + feed=data_input, + fetch_list=[out])[0] + out_max = np.max(ret_out) + out_min = np.min(ret_out) + + self.assertTrue(isclose(out_max, 1.0)) + self.assertTrue(isclose(out_min, 0.0)) def test_all(self): place = fluid.CUDAPlace(0) From f5d8cee05853bec7bdc4d5920df9b261b9a9e092 Mon Sep 17 00:00:00 2001 From: gongweibao Date: Mon, 26 Mar 2018 12:11:49 +0000 Subject: [PATCH 5/8] add dropout_op_test --- paddle/fluid/operators/dropout_op_test.cc | 88 +++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 paddle/fluid/operators/dropout_op_test.cc diff --git a/paddle/fluid/operators/dropout_op_test.cc b/paddle/fluid/operators/dropout_op_test.cc new file mode 100644 index 0000000000000..df6d972eff3f3 --- /dev/null +++ b/paddle/fluid/operators/dropout_op_test.cc @@ -0,0 +1,88 @@ +/* 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. */ + +#include +#include +#include + +#include "gtest/gtest.h" +#include "paddle/fluid/framework/op_registry.h" +#include "paddle/fluid/framework/operator.h" +#include "paddle/fluid/framework/program_desc.h" +#include "paddle/fluid/operators/dropout_op.h" +#include "paddle/fluid/operators/math/math_function.h" +#include "paddle/fluid/string/printf.h" + +namespace f = paddle::framework; +namespace p = paddle::platform; +namespace m = paddle::operators::math; + +USE_OP(dropout); + +void InitTensorInScope(f::Scope &scope, p::CPUPlace &place) { + p::CPUDeviceContext ctx(place); + auto var = scope.Var("X"); + auto tensor = var->GetMutable(); + tensor->Resize({10, 10}); + float *expect = tensor->mutable_data(place); + for (int64_t i = 0; i < tensor->numel(); ++i) { + expect[i] = 1.0; + } + + auto out_var = scope.Var("Out"); + auto out_tensor = out_var->GetMutable(); + out_tensor->Resize({10, 10}); + out_tensor->mutable_data(place); // allocate + + auto mask_var = scope.Var("Mask"); + auto mask_tensor = mask_var->GetMutable(); + mask_tensor->Resize({10, 10}); + mask_tensor->mutable_data(place); // allocate +} + +TEST(Dropout, CPUDense) { + f::Scope scope; + p::CPUPlace place; + p::CPUDeviceContext ctx(place); + InitTensorInScope(scope, place); + + f::AttributeMap attrs; + float dropout_prob = 0.5; + attrs.insert({"fix_seed", 1}); + attrs.insert({"seed", 3}); + attrs.insert({"dropout_prob", dropout_prob}); + auto dropout_op = f::OpRegistry::CreateOp( + "dropout", {{"X", {"X"}}}, {{"Out", {"Out"}}, {"Mask", {"Mask"}}}, attrs); + + dropout_op->Run(scope, place); + + auto out_var = scope.Var("Out"); + auto out_tensor = out_var->GetMutable(); + std::vector out_vec; + TensorToVector(*out_tensor, &out_vec); + + std::vector std_out = { + 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, + 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, + 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, + 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1}; + + EXPECT_EQ(out_vec.size(), std_out.size()); + for (uint32_t i = 0; i < out_vec.size(); i++) { + std::cout << out_vec[i] << ","; + EXPECT_EQ(out_vec[i], std_out[i]); + } + std::cout << std::endl; +} From 7e75fb06f4bf60a2b42f1dc2e1e362d06868fdc9 Mon Sep 17 00:00:00 2001 From: gongweibao Date: Mon, 26 Mar 2018 12:46:24 +0000 Subject: [PATCH 6/8] add gpu support --- paddle/fluid/operators/CMakeLists.txt | 1 + paddle/fluid/operators/dropout_op.cu | 5 +-- paddle/fluid/operators/dropout_op_test.cc | 43 ++++++++++++------- .../fluid/tests/unittests/test_dropout_op.py | 40 ----------------- 4 files changed, 30 insertions(+), 59 deletions(-) diff --git a/paddle/fluid/operators/CMakeLists.txt b/paddle/fluid/operators/CMakeLists.txt index 9a11e1be7050a..8341170d6897d 100644 --- a/paddle/fluid/operators/CMakeLists.txt +++ b/paddle/fluid/operators/CMakeLists.txt @@ -264,3 +264,4 @@ cc_test(strided_memcpy_test SRCS strided_memcpy_test.cc DEPS tensor paddle_memor cc_test(save_load_op_test SRCS save_load_op_test.cc DEPS save_op load_op) cc_test(save_load_combine_op_test SRCS save_load_combine_op_test.cc DEPS save_combine_op load_combine_op) nv_test(nccl_op_test SRCS nccl_op_test.cu.cc DEPS nccl_op gpu_info device_context) +nv_test(dropout_op_test SRCS dropout_op_test.cc DEPS dropout_op tensor) diff --git a/paddle/fluid/operators/dropout_op.cu b/paddle/fluid/operators/dropout_op.cu index 94382739b5077..184c095e487a3 100644 --- a/paddle/fluid/operators/dropout_op.cu +++ b/paddle/fluid/operators/dropout_op.cu @@ -55,9 +55,6 @@ class GPUDropoutKernel : public framework::OpKernel { y->mutable_data(context.GetPlace()); float dropout_prob = context.Attr("dropout_prob"); - auto X = EigenMatrix::Reshape(*x, 1); - auto Y = EigenMatrix::Reshape(*y, 1); - auto& place = *context.template device_context().eigen_device(); if (!context.Attr("is_test")) { auto* mask = context.Output("Mask"); @@ -76,6 +73,8 @@ class GPUDropoutKernel : public framework::OpKernel { T><<>>( size, seed, dropout_prob, x_data, mask_data, y_data); } else { + auto X = EigenMatrix::Reshape(*x, 1); + auto Y = EigenMatrix::Reshape(*y, 1); Y.device(place) = X * static_cast(1.0f - dropout_prob); } } diff --git a/paddle/fluid/operators/dropout_op_test.cc b/paddle/fluid/operators/dropout_op_test.cc index df6d972eff3f3..8fb7e02e8bbcb 100644 --- a/paddle/fluid/operators/dropout_op_test.cc +++ b/paddle/fluid/operators/dropout_op_test.cc @@ -30,16 +30,20 @@ namespace m = paddle::operators::math; USE_OP(dropout); -void InitTensorInScope(f::Scope &scope, p::CPUPlace &place) { - p::CPUDeviceContext ctx(place); +void InitTensorInScope(f::Scope& scope, p::DeviceContext& ctx) { + // init auto var = scope.Var("X"); auto tensor = var->GetMutable(); tensor->Resize({10, 10}); - float *expect = tensor->mutable_data(place); - for (int64_t i = 0; i < tensor->numel(); ++i) { - expect[i] = 1.0; + + std::vector init; + for (int64_t i = 0; i < 10 * 10; ++i) { + init.push_back(1.0); } + TensorFromVector(init, ctx, tensor); + + auto place = ctx.GetPlace(); auto out_var = scope.Var("Out"); auto out_tensor = out_var->GetMutable(); out_tensor->Resize({10, 10}); @@ -49,14 +53,8 @@ void InitTensorInScope(f::Scope &scope, p::CPUPlace &place) { auto mask_tensor = mask_var->GetMutable(); mask_tensor->Resize({10, 10}); mask_tensor->mutable_data(place); // allocate -} - -TEST(Dropout, CPUDense) { - f::Scope scope; - p::CPUPlace place; - p::CPUDeviceContext ctx(place); - InitTensorInScope(scope, place); + // run f::AttributeMap attrs; float dropout_prob = 0.5; attrs.insert({"fix_seed", 1}); @@ -67,10 +65,10 @@ TEST(Dropout, CPUDense) { dropout_op->Run(scope, place); - auto out_var = scope.Var("Out"); - auto out_tensor = out_var->GetMutable(); + // auto out_var = scope.Var("Out"); + // auto out_tensor = out_var->GetMutable(); std::vector out_vec; - TensorToVector(*out_tensor, &out_vec); + TensorToVector(*out_tensor, ctx, &out_vec); std::vector std_out = { 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, @@ -81,8 +79,21 @@ TEST(Dropout, CPUDense) { EXPECT_EQ(out_vec.size(), std_out.size()); for (uint32_t i = 0; i < out_vec.size(); i++) { - std::cout << out_vec[i] << ","; EXPECT_EQ(out_vec[i], std_out[i]); } std::cout << std::endl; } + +TEST(Dropout, CPUDense) { + f::Scope scope; + p::CPUPlace place; + p::CPUDeviceContext ctx(place); + InitTensorInScope(scope, ctx); +} + +TEST(Dropout, GPUDense) { + f::Scope scope; + p::CUDAPlace place; + p::CUDADeviceContext ctx(place); + InitTensorInScope(scope, ctx); +} diff --git a/python/paddle/fluid/tests/unittests/test_dropout_op.py b/python/paddle/fluid/tests/unittests/test_dropout_op.py index af82a8720447d..2c301f9d37a41 100644 --- a/python/paddle/fluid/tests/unittests/test_dropout_op.py +++ b/python/paddle/fluid/tests/unittests/test_dropout_op.py @@ -84,46 +84,6 @@ def test_check_output(self): self.check_output() -def isclose(a, b, rel_tol=1e-09, abs_tol=0.0): - return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) - - -#Reference: https://github.com/PaddlePaddle/Paddle/issues/8654 -class TestDropoutOp6(unittest.TestCase): - def program(self): - x = fluid.layers.data( - name='x', - shape=[64, 32, 512], - dtype='float32', - append_batch_size=False) - out = fluid.layers.dropout(x, dropout_prob=0.1, is_test=False) - return out - - def LoopMaxMin(self, place): - exe = fluid.Executor(place) - out = self.program() - data_input = {} - in_tensor = fluid.LoDTensor() - in_tensor.set(np.ones([64, 32, 512], dtype="float32"), place) - data_input['x'] = in_tensor - for i in range(10): - ret_out = exe.run(fluid.framework.default_main_program(), - feed=data_input, - fetch_list=[out])[0] - out_max = np.max(ret_out) - out_min = np.min(ret_out) - - self.assertTrue(isclose(out_max, 1.0)) - self.assertTrue(isclose(out_min, 0.0)) - - def test_all(self): - place = fluid.CUDAPlace(0) - self.LoopMaxMin(place) - - place = fluid.CPUPlace() - self.LoopMaxMin(place) - - class TestFP16DropoutOp(OpTest): def setUp(self): self.op_type = "dropout" From bc22cba84e92e0c092271734ac404f3fa1f0d6e0 Mon Sep 17 00:00:00 2001 From: gongweibao Date: Mon, 26 Mar 2018 12:49:27 +0000 Subject: [PATCH 7/8] clean up --- paddle/fluid/operators/dropout_op_test.cc | 2 -- python/paddle/fluid/tests/unittests/test_dropout_op.py | 1 - 2 files changed, 3 deletions(-) diff --git a/paddle/fluid/operators/dropout_op_test.cc b/paddle/fluid/operators/dropout_op_test.cc index 8fb7e02e8bbcb..f83a016e6a5e0 100644 --- a/paddle/fluid/operators/dropout_op_test.cc +++ b/paddle/fluid/operators/dropout_op_test.cc @@ -65,8 +65,6 @@ void InitTensorInScope(f::Scope& scope, p::DeviceContext& ctx) { dropout_op->Run(scope, place); - // auto out_var = scope.Var("Out"); - // auto out_tensor = out_var->GetMutable(); std::vector out_vec; TensorToVector(*out_tensor, ctx, &out_vec); diff --git a/python/paddle/fluid/tests/unittests/test_dropout_op.py b/python/paddle/fluid/tests/unittests/test_dropout_op.py index 2c301f9d37a41..eaa3435a86462 100644 --- a/python/paddle/fluid/tests/unittests/test_dropout_op.py +++ b/python/paddle/fluid/tests/unittests/test_dropout_op.py @@ -15,7 +15,6 @@ import unittest import numpy as np import paddle.fluid.core as core -import paddle.fluid as fluid from op_test import OpTest From 2bf971332af6dffe7363db18a738225fb52f5524 Mon Sep 17 00:00:00 2001 From: gongweibao Date: Mon, 26 Mar 2018 12:56:06 +0000 Subject: [PATCH 8/8] clean up --- paddle/fluid/operators/dropout_op_test.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/operators/dropout_op_test.cc b/paddle/fluid/operators/dropout_op_test.cc index f83a016e6a5e0..db97ba4f64105 100644 --- a/paddle/fluid/operators/dropout_op_test.cc +++ b/paddle/fluid/operators/dropout_op_test.cc @@ -30,7 +30,7 @@ namespace m = paddle::operators::math; USE_OP(dropout); -void InitTensorInScope(f::Scope& scope, p::DeviceContext& ctx) { +void Compare(f::Scope& scope, p::DeviceContext& ctx) { // init auto var = scope.Var("X"); auto tensor = var->GetMutable(); @@ -79,19 +79,18 @@ void InitTensorInScope(f::Scope& scope, p::DeviceContext& ctx) { for (uint32_t i = 0; i < out_vec.size(); i++) { EXPECT_EQ(out_vec[i], std_out[i]); } - std::cout << std::endl; } TEST(Dropout, CPUDense) { f::Scope scope; p::CPUPlace place; p::CPUDeviceContext ctx(place); - InitTensorInScope(scope, ctx); + Compare(scope, ctx); } TEST(Dropout, GPUDense) { f::Scope scope; p::CUDAPlace place; p::CUDADeviceContext ctx(place); - InitTensorInScope(scope, ctx); + Compare(scope, ctx); }