dropout_op_test.cc 3.2 KB
Newer Older
G
gongweibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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 <unistd.h>
D
dzhwinter 已提交
16
#include <iostream>
Y
Yi Wang 已提交
17

G
gongweibao 已提交
18
#include <string>
Y
Yi Wang 已提交
19 20
#include <thread>  // NOLINT
#include <vector>
G
gongweibao 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#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);

D
dzhwinter 已提交
36 37
static paddle::framework::DDim dims = {10, 10};

Y
Yi Wang 已提交
38
void Compare(f::Scope* scope, const p::DeviceContext& ctx) {
G
gongweibao 已提交
39
  // init
Y
Yi Wang 已提交
40
  auto var = scope->Var("X");
G
gongweibao 已提交
41
  auto tensor = var->GetMutable<f::LoDTensor>();
D
dzhwinter 已提交
42
  tensor->Resize(dims);
G
gongweibao 已提交
43 44

  std::vector<float> init;
D
dzhwinter 已提交
45
  for (int64_t i = 0; i < f::product(dims); ++i) {
G
gongweibao 已提交
46 47 48 49 50 51
    init.push_back(1.0);
  }

  TensorFromVector(init, ctx, tensor);

  auto place = ctx.GetPlace();
Y
Yi Wang 已提交
52
  auto out_var = scope->Var("Out");
G
gongweibao 已提交
53
  auto out_tensor = out_var->GetMutable<f::LoDTensor>();
D
dzhwinter 已提交
54
  out_tensor->Resize(dims);
G
gongweibao 已提交
55 56
  out_tensor->mutable_data<float>(place);  // allocate

Y
Yi Wang 已提交
57
  auto mask_var = scope->Var("Mask");
G
gongweibao 已提交
58
  auto mask_tensor = mask_var->GetMutable<f::LoDTensor>();
D
dzhwinter 已提交
59
  mask_tensor->Resize(dims);
G
gongweibao 已提交
60 61 62 63 64
  mask_tensor->mutable_data<float>(place);  // allocate

  // run
  f::AttributeMap attrs;
  float dropout_prob = 0.5;
D
dzhwinter 已提交
65 66
  attrs.insert({"is_test", false});
  attrs.insert({"fix_seed", true});
G
gongweibao 已提交
67 68 69 70 71
  attrs.insert({"seed", 3});
  attrs.insert({"dropout_prob", dropout_prob});
  auto dropout_op = f::OpRegistry::CreateOp(
      "dropout", {{"X", {"X"}}}, {{"Out", {"Out"}}, {"Mask", {"Mask"}}}, attrs);

Y
Yi Wang 已提交
72
  dropout_op->Run(*scope, place);
G
gongweibao 已提交
73 74 75

  std::vector<float> out_vec;
  TensorToVector(*out_tensor, ctx, &out_vec);
D
dzhwinter 已提交
76
  ctx.Wait();
G
gongweibao 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

  std::vector<float> 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++) {
    EXPECT_EQ(out_vec[i], std_out[i]);
  }
}

TEST(Dropout, CPUDense) {
  f::Scope scope;
  p::CPUPlace place;
  p::CPUDeviceContext ctx(place);
D
dzhwinter 已提交
95
  Compare(&scope, ctx);
G
gongweibao 已提交
96 97
}

D
dzhwinter 已提交
98 99 100 101 102
// TODO(wyi, dzhwinter): Due to
// https://github.com/PaddlePaddle/Paddle/issues/9507, I temporarily
// disable this test to remove the prevention of the merge of
// unrelated PRs.
/*
G
gongweibao 已提交
103 104 105 106
TEST(Dropout, GPUDense) {
  f::Scope scope;
  p::CUDAPlace place;
  p::CUDADeviceContext ctx(place);
D
dzhwinter 已提交
107
  Compare(&scope, ctx);
G
gongweibao 已提交
108
}
Y
Yi Wang 已提交
109
*/