fc_compute_test.cc 5.6 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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 "lite/api/paddle_use_kernels.h"
#include "lite/api/paddle_use_ops.h"
#include "lite/core/arena/framework.h"
19 20
#include "lite/tests/utils/fill_data.h"
#include "lite/tests/utils/naive_math_impl.h"
Y
Yan Chunwei 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

namespace paddle {
namespace lite {

void fill_bias_fc(float* out, const float* bias, int num, int channel) {
  int remain = channel;
  for (int j = 0; j < num; ++j) {
    const float* ptr_bias = bias;
    float* ptr_out = out + j * channel;
    for (int i = 0; i < remain; ++i) {
      *(ptr_out++) += *(ptr_bias++);
    }
  }
}

DDim compute_out_dim(const DDim& dim_in, const DDim& wdim, int in_num_col_dim) {
  std::vector<int64_t> out_dim;
  out_dim.resize(in_num_col_dim + 1);
  auto in_mat_dims = dim_in.Flatten2D(in_num_col_dim);
  for (int i = 0; i < in_num_col_dim; ++i) {
    out_dim[i] = dim_in[i];
  }
  out_dim[in_num_col_dim] = wdim[1];
  return DDim(out_dim);
}

class FcOPTest : public arena::TestCase {
 protected:
  // common attributes for this op.
  std::string input_ = "x";
  std::string weight_ = "w";
  std::string bias_ = "b";
  std::string out_ = "out";
  DDim dims_{{1, 128}};
  DDim wdims_{{128, 4}};
  DDim bdims_{{4}};
57
  int in_num_col_dims_{1};
Y
Yan Chunwei 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

 public:
  FcOPTest(const Place& place,
           const std::string& alias,
           DDim dim_in,
           DDim dim_w,
           DDim dim_b,
           int in_num_col_dims)
      : TestCase(place, alias),
        dims_(std::move(dim_in)),
        wdims_(std::move(dim_w)),
        bdims_(dim_b),
        in_num_col_dims_(in_num_col_dims) {}

  void RunBaseline(Scope* scope) override {
    auto x = scope->FindTensor(input_);
    auto w = scope->FindTensor(weight_);
    auto b = scope->FindTensor(bias_);
    bool flag_bias = b;
    auto out = scope->NewTensor(out_);
    CHECK(out);
    DDim out_dim = compute_out_dim(x->dims(), w->dims(), in_num_col_dims_);
    out->Resize(out_dim);

    LOG(INFO) << "out dims: " << out_dim;

    auto x_data = x->data<float>();
    auto w_data = w->data<float>();
    const float* b_data = nullptr;
    if (flag_bias) {
      b_data = b->data<float>();
    }
    auto out_data = out->mutable_data<float>();

    int m = x->dims().count(0, in_num_col_dims_);
    CHECK_EQ(wdims_[0], x->dims().count(in_num_col_dims_, x->dims().size()));
    int k = wdims_[0];
    int n = wdims_[1];

    LOG(INFO) << "m: " << m << ", n: " << n << ", k: " << k;

    if (m == 1) {
      basic_gemv(n,
                 k,
                 w_data,
                 x_data,
                 b_data,
                 out_data,
                 1.f,
                 0.f,
                 true,
                 flag_bias,
                 false);
    } else {
      basic_gemm(false,
                 false,
                 m,
                 n,
                 k,
                 1.f,
                 x_data,
                 k,
                 w_data,
                 n,
                 0.f,
                 out_data,
                 n,
                 b_data,
                 false,
                 false);
      if (flag_bias) {
        fill_bias_fc(out_data, b_data, m, n);
      }
    }
  }

  void PrepareOpDesc(cpp::OpDesc* op_desc) {
    op_desc->SetType("fc");
    op_desc->SetInput("Input", {input_});
    op_desc->SetInput("W", {weight_});
    if (bdims_.production() > 0) {
      op_desc->SetInput("Bias", {bias_});
    }
    op_desc->SetOutput("Out", {out_});
    op_desc->SetAttr<int>("in_num_col_dims", in_num_col_dims_);
  }

  void PrepareData() override {
    std::vector<float> din(dims_.production());
    fill_data_rand(din.data(), -1.f, 1.f, dims_.production());

    std::vector<float> win(wdims_.production());
    fill_data_rand(win.data(), -1.f, 1.f, wdims_.production());

    bool flag_bias = bdims_.production() > 0;
    std::vector<float> bin(bdims_.production());
    fill_data_rand(bin.data(), -1.f, 1.f, bdims_.production());

    SetCommonTensor(input_, dims_, din.data());
    SetCommonTensor(weight_, wdims_, win.data());
    if (flag_bias) {
      SetCommonTensor(bias_, bdims_, bin.data());
    }
  }
};

void test_fc(Place place) {
  for (auto& m : {1, 3, 16}) {
    for (auto& n : {1, 4, 16, 128, 256, 1024}) {
      for (auto& k : {1, 16, 128, 1024}) {
        for (auto& bflag : {false, true}) {
          DDim dim_in{{m, k}};
          DDim wdim{{k, n}};
          DDim bdim{{bflag ? n : 0}};
          std::unique_ptr<arena::TestCase> tester(
              new FcOPTest(place, "def", dim_in, wdim, bdim, 1));
174
#ifdef LITE_WITH_ARM
Y
Yan Chunwei 已提交
175
          auto& ctx = tester->context()->As<ARMContext>();
176
          ctx.SetRunMode(lite_api::LITE_POWER_HIGH, 1);
Y
Yan Chunwei 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
#endif
          arena::Arena arena(std::move(tester), place, 6e-5);
          if (!arena.TestPrecision()) {
            LOG(ERROR) << "run m: " << m << ", n: " << n << ", k: " << k
                       << ", bias: " << (bflag ? "true" : "false") << " failed";
            return;
          }
        }
      }
    }
  }
}

TEST(FcOP, precision) {
#ifdef LITE_WITH_X86
  Place place(TARGET(kX86));
#endif
#ifdef LITE_WITH_ARM
  Place place(TARGET(kARM));
  test_fc(place);
#endif
}

}  // namespace lite
}  // namespace paddle