fc_compute_test.cc 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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 "paddle/fluid/lite/kernels/arm/fc_compute.h"
#include <gtest/gtest.h>
T
tensor-tang 已提交
17 18 19 20 21
#include <algorithm>
#include <iostream>
#include <memory>
#include <random>
#include <utility>
22
#include <vector>
T
tensor-tang 已提交
23
#include "paddle/fluid/lite/arm/math/funcs.h"
24 25 26 27 28 29 30
#include "paddle/fluid/lite/core/op_registry.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace arm {

T
tensor-tang 已提交
31 32 33 34 35 36 37 38 39 40 41
template <typename T>
void FillData(T* a, const int n, const T lower = static_cast<T>(-2.f),
              const T upper = static_cast<T>(2.f)) {
  static unsigned int seed = 100;
  std::mt19937 rng(seed++);
  std::uniform_real_distribution<double> uniform_dist(0, 1);
  for (int i = 0; i < n; ++i) {
    a[i] = static_cast<T>(uniform_dist(rng) * (upper - lower) + lower);
  }
}

T
tensor-tang 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55
TEST(fc_arm, retrive_op) {
  auto fc =
      KernelRegistry::Global().Create<TARGET(kARM), PRECISION(kFloat)>("fc");
  ASSERT_FALSE(fc.empty());
  ASSERT_TRUE(fc.front());
}

TEST(fc_arm, init) {
  FcCompute fc;
  ASSERT_EQ(fc.precision(), PRECISION(kFloat));
  ASSERT_EQ(fc.target(), TARGET(kARM));
}

TEST(fc_arm, compare_test) {
T
tensor-tang 已提交
56 57 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
  using T = float;

  for (int m : {1, 2, 3, 4}) {
    for (int n : {1, 2, 3, 4}) {
      for (int k : {1, 2, 3, 4}) {
        for (bool with_bias : {true, false}) {
          VLOG(3) << "m: " << m << ", n: " << n << ", k: " << k
                  << (with_bias ? ", with bias" : "");
          lite::Tensor x, w, b, out, ref;

          x.Resize({m, k});
          w.Resize({k, n});
          b.Resize({1, n});
          out.Resize({m, n});
          ref.Resize({m, n});

          auto* x_data = x.mutable_data<T>();
          auto* w_data = w.mutable_data<T>();
          auto* b_data = with_bias ? b.mutable_data<T>() : nullptr;

          auto* out_data = out.mutable_data<T>();
          auto* ref_data = ref.mutable_data<T>();

          FillData<T>(x_data, x.dims().production());
          FillData<T>(w_data, w.dims().production());
          FillData<T>(out_data, out.dims().production(), 0, 0);
          FillData<T>(ref_data, ref.dims().production(), 0, 0);

          if (with_bias) {
            FillData<T>(b_data, b.dims().production());
          }

          FcCompute fc;
          operators::FcParam param;

          param.input = &x;
          param.w = &w;
          param.bias = with_bias ? &b : nullptr;
          param.output = &out;
          param.in_num_col_dims = 1;
          param.in_mat_dims = x.dims();

          DeviceInfo::Init();
          std::unique_ptr<KernelContext> ctx(new KernelContext);
          ctx->As<ARMContext>();
          fc.SetParam(param);
          fc.SetContext(std::move(ctx));
          fc.PrepareForRun();
          fc.Run();

          lite::arm::math::fc_compute_eigen(x_data, m, k, w_data, k, n, b_data,
                                            ref_data);
          for (int i = 0; i < out.dims().production(); i++) {
            EXPECT_NEAR(out_data[i], ref_data[i], 1e-3);
          }
        }
      }
    }
T
tensor-tang 已提交
114
  }
115 116
}

T
tensor-tang 已提交
117
TEST(fc_arm, num_col_dims) {
T
tensor-tang 已提交
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
  using T = float;

  for (bool with_bias : {true, false}) {
    lite::Tensor x, w, b, out, ref;

    x.Resize({1, 2, 3});
    w.Resize({3, 4});
    b.Resize({1, 4});
    out.Resize({2, 4});
    ref.Resize({2, 4});

    auto* x_data = x.mutable_data<float>();
    auto* w_data = w.mutable_data<float>();
    auto* b_data = with_bias ? b.mutable_data<T>() : nullptr;

    auto* out_data = out.mutable_data<T>();
    auto* ref_data = ref.mutable_data<T>();

    FillData<T>(x_data, x.dims().production());
    FillData<T>(w_data, w.dims().production());
    FillData<T>(out_data, out.dims().production(), 0, 0);
    FillData<T>(ref_data, ref.dims().production(), 0, 0);
    if (with_bias) {
      FillData<T>(b_data, b.dims().production());
    }
    FcCompute fc;
    operators::FcParam param;
    param.input = &x;
    param.w = &w;
    param.bias = with_bias ? &b : nullptr;
    param.output = &out;
    param.in_num_col_dims = 2;
    param.in_mat_dims = x.dims();

    std::unique_ptr<KernelContext> ctx(new KernelContext);
    ctx->As<ARMContext>();
    DeviceInfo::Init();

    fc.SetParam(param);
    fc.SetContext(std::move(ctx));
    fc.PrepareForRun();
    fc.Run();

    lite::arm::math::fc_compute_eigen(x_data, 2, 3, w_data, 3, 4, b_data,
                                      ref_data);
    for (int i = 0; i < out.dims().production(); i++) {
      EXPECT_NEAR(out_data[i], ref_data[i], 1e-3);
    }
T
tensor-tang 已提交
166
  }
167 168 169 170 171 172 173 174
}

}  // namespace arm
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

USE_LITE_KERNEL(fc, kARM, kFloat, kNCHW, def);