dropout_compute_test.cc 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright (c) 2019 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 <gtest/gtest.h>
#include <cmath>
#include <string>
#include "lite/api/paddle_use_kernels.h"
#include "lite/api/paddle_use_ops.h"
#include "lite/core/arena/framework.h"
21
#include "lite/tests/utils/fill_data.h"
22 23 24 25 26 27 28 29

namespace paddle {
namespace lite {

class DropoutComputeTester : public arena::TestCase {
 protected:
  // common attributes for this op.
  std::string type_ = "dropout";
30 31
  std::string x_ = "x";
  std::string out_ = "out";
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  std::string mask_ = "mask";
  DDim dims_{{1}};
  float dropout_prob_ = 0.5;
  bool fix_seed_ = true;
  int seed_ = 1;
  std::string dropout_implementation_ = "downgrade_in_infer";

 public:
  DropoutComputeTester(const Place& place,
                       const std::string& alias,
                       DDim dims,
                       float dropout_prob,
                       bool fix_seed,
                       int seed,
                       std::string dropout_implementation)
      : TestCase(place, alias),
        dims_(dims),
        dropout_prob_(dropout_prob),
        fix_seed_(fix_seed),
        seed_(seed),
        dropout_implementation_(dropout_implementation) {}

  void RunBaseline(Scope* scope) override {
55
    auto* out = scope->NewTensor(out_);
56 57 58 59
    CHECK(out);
    out->Resize(dims_);
    auto* output_data = out->mutable_data<float>();

60
    auto* x = scope->FindTensor(x_);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    const auto* x_data = x->data<float>();

    if (dropout_implementation_ == "downgrade_in_infer") {
      float rate = 1 - dropout_prob_;
      for (int64_t i = 0; i < dims_.production(); i++) {
        output_data[i] = x_data[i] * rate;
      }
    } else if (dropout_implementation_ == "upscale_in_train") {
      memcpy(output_data, x_data, sizeof(float) * dims_.production());
    } else {
      LOG(FATAL) << "unsupported dropout_implementation: "
                 << dropout_implementation_;
    }
  }

  void PrepareOpDesc(cpp::OpDesc* op_desc) {
    op_desc->SetType(type_);
78 79
    op_desc->SetInput("X", {x_});
    op_desc->SetOutput("Out", {out_});
80 81 82 83 84 85 86 87
    op_desc->SetOutput("Mask", {mask_});
    op_desc->SetAttr("dropout_prob", dropout_prob_);
    op_desc->SetAttr("fix_seed", fix_seed_);
    op_desc->SetAttr("seed", seed_);
    op_desc->SetAttr("dropout_implementation", dropout_implementation_);
  }

  void PrepareData() override {
88 89 90
    std::vector<float> x(dims_.production());
    fill_data_rand(x.data(), -1.f, 1.f, dims_.production());
    SetCommonTensor(x_, dims_, x.data());
91 92 93 94 95 96 97 98 99 100 101 102 103
  }
};

TEST(Dropout, precision) {
  LOG(INFO) << "test dropout op";
  float abs_error = 2e-5;
  Place place;
#if defined(LITE_WITH_XPU)
  place = TARGET(kXPU);
#else
  return;
#endif

104 105 106
  for (auto dims : std::vector<std::vector<int64_t>>{
           {3}, {3, 4}, {3, 4, 5}, {1, 2, 3, 4}, {2, 3, 4, 5}}) {
    for (auto dropout_prob : {0., 0.5, 1.}) {
107 108 109 110 111
      for (auto dropout_implementation :
           {"downgrade_in_infer", "upscale_in_train"}) {
        std::unique_ptr<arena::TestCase> tester(
            new DropoutComputeTester(place,
                                     "def",
112
                                     DDim(dims),
113 114 115 116 117 118 119 120 121 122 123 124 125
                                     dropout_prob,
                                     true,
                                     1,
                                     dropout_implementation));
        arena::Arena arena(std::move(tester), place, abs_error);
        arena.TestPrecision({"mask"});
      }
    }
  }
}

}  // namespace lite
}  // namespace paddle