mul_compute_test.cc 4.0 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

T
tensor-tang 已提交
15
#include "paddle/fluid/lite/kernels/arm/mul_compute.h"
T
tensor-tang 已提交
16
#include <gtest/gtest.h>
T
tensor-tang 已提交
17 18
#include <memory>
#include <utility>
T
tensor-tang 已提交
19 20 21 22 23 24 25 26 27
#include <vector>
#include "paddle/fluid/lite/arm/math/funcs.h"
#include "paddle/fluid/lite/core/op_registry.h"

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

T
tensor-tang 已提交
28 29 30 31 32
TEST(mul_arm, retrive_op) {
  auto mul =
      KernelRegistry::Global().Create<TARGET(kARM), PRECISION(kFloat)>("mul");
  ASSERT_FALSE(mul.empty());
  ASSERT_TRUE(mul.front());
T
tensor-tang 已提交
33 34
}

T
tensor-tang 已提交
35 36 37 38
TEST(mul_arm, init) {
  FcCompute mul;
  ASSERT_EQ(mul.precision(), PRECISION(kFloat));
  ASSERT_EQ(mul.target(), TARGET(kARM));
T
tensor-tang 已提交
39 40
}

T
tensor-tang 已提交
41
TEST(mul_arm, compare_test) {
T
tensor-tang 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  lite::Tensor x, w, b, out, ref;
  constexpr int batch_size = 2;
  x.Resize({batch_size, 3});
  w.Resize({3, 4});
  b.Resize({1, 4});
  out.Resize({batch_size, 4});
  ref.Resize({batch_size, 4});

  auto x_data = x.mutable_data<float>();
  auto w_data = w.mutable_data<float>();
  auto b_data = b.mutable_data<float>();
  auto out_data = out.mutable_data<float>();
  auto ref_data = ref.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().product(); i++) {
    x_data[i] = static_cast<float>(i);
  }
  for (int64_t i = 0; i < w.dims().product(); i++) {
    w_data[i] = static_cast<float>(i);
  }
  for (int64_t i = 0; i < b.dims().product(); i++) {
    b_data[i] = static_cast<float>(i);
  }

  lite::arm::math::fc_compute_eigen(x_data, batch_size, 3,  //
                                    w_data, 3, 4,           //
                                    b_data, ref_data);

T
tensor-tang 已提交
70 71
  // mul compute kernel
  FcCompute mul;
T
tensor-tang 已提交
72 73 74 75 76 77 78 79 80 81 82 83
  operators::FcParam param;

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

  DeviceInfo::Init();
  std::unique_ptr<KernelContext> ctx(new KernelContext);
  ctx->As<ARMContext>();
T
tensor-tang 已提交
84 85 86
  mul.SetParam(param);
  mul.SetContext(std::move(ctx));
  mul.Run();
T
tensor-tang 已提交
87 88 89 90 91 92 93 94 95 96 97

  VLOG(3) << "output vs ref";
  for (int i = 0; i < out.dims().product(); i++) {
    VLOG(3) << out_data[i] << " vs " << ref_data[i];
  }

  for (int i = 0; i < out.dims().product(); ++i) {
    EXPECT_NEAR(out_data[i], ref_data[i], 1e-5);
  }
}

T
tensor-tang 已提交
98 99
TEST(mul_arm, num_col_dims) {
  FcCompute mul;
T
tensor-tang 已提交
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
  operators::FcParam param;

  lite::Tensor x;
  lite::Tensor w;
  lite::Tensor bias;
  lite::Tensor output;

  x.Resize({1, 2, 3});
  w.Resize({3, 4});
  bias.Resize({1, 4});
  output.Resize({2, 4});

  auto* x_data = x.mutable_data<float>();
  auto* w_data = w.mutable_data<float>();
  auto* bias_data = bias.mutable_data<float>();
  auto* output_data = output.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().product(); i++) {
    x_data[i] = static_cast<float>(i);
  }
  for (int64_t i = 0; i < w.dims().product(); i++) {
    w_data[i] = static_cast<float>(i);
  }
  for (int64_t i = 0; i < bias.dims().product(); i++) {
    bias_data[i] = static_cast<float>(i);
  }
  for (int64_t i = 0; i < output.dims().product(); i++) {
    output_data[i] = static_cast<float>(i);
  }

  param.in_num_col_dims = 2;
  param.input = &x;
  param.w = &w;
  param.bias = &bias;
  param.output = &output;
  param.in_mat_dims = x.dims();

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

T
tensor-tang 已提交
141 142 143
  mul.SetParam(param);
  mul.SetContext(std::move(ctx));
  mul.Run();
T
tensor-tang 已提交
144 145 146 147 148 149 150
}

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

T
tensor-tang 已提交
151
USE_LITE_KERNEL(mul, kARM, kFloat, kNCHW, def);