fc_op_test.cc 4.6 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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 "lite/operators/fc_op.h"
#include <gtest/gtest.h>
#include "lite/core/op_registry.h"
Z
zhupengyang 已提交
18 19
#include "lite/kernels/npu/bridges/registry.h"
#include "lite/kernels/npu/bridges/test_helper.h"
Y
Yan Chunwei 已提交
20 21 22

namespace paddle {
namespace lite {
Z
zhupengyang 已提交
23
namespace kernels {
Y
Yan Chunwei 已提交
24
namespace npu {
Z
zhupengyang 已提交
25
namespace bridges {
Y
Yan Chunwei 已提交
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70

void fc_ref(const std::shared_ptr<operators::FcOpLite> op) {
  Scope* scope = op->scope();
  const OpInfo* op_info = op->op_info();
  auto input =
      scope->FindVar(op_info->Input("Input").front())->GetMutable<Tensor>();
  auto w = scope->FindVar(op_info->Input("W").front())->GetMutable<Tensor>();
  auto out =
      scope->FindVar(op_info->Output("Out").front())->GetMutable<Tensor>();
  int32_t in_num_col_dims = op_info->GetAttr<int32_t>("in_num_col_dims");
  Tensor* bias = nullptr;
  float* bias_data = nullptr;
  if (op_info->HasInput("Bias")) {
    auto bias_var_names = op_info->Input("Bias");
    if (bias_var_names.size() > 0) {
      auto bias_var_name = bias_var_names.front();
      bias = scope->FindVar(bias_var_name)->GetMutable<lite::Tensor>();
      bias_data = bias->mutable_data<float>();
    }
  }
  auto input_data = input->data<float>();
  auto w_data = w->mutable_data<float>();
  auto out_data = out->mutable_data<float>();
  auto in_mat_dims = input->dims().Flatten2D(in_num_col_dims);
  int out_num_classes = w->dims()[1];
  const int M = in_mat_dims[0];
  const int K = in_mat_dims[1];
  const int N = out_num_classes;
  for (int m = 0; m < M; ++m) {
    for (int n = 0; n < N; ++n) {
      out_data[m * N + n] = 0;
      for (int k = 0; k < K; ++k) {
        out_data[m * N + n] += input_data[m * K + k] * w_data[k * N + n];
      }
    }
  }
  if (bias_data != nullptr) {
    for (int m = 0; m < M; ++m) {
      for (int n = 0; n < N; ++n) {
        out_data[m * N + n] += bias_data[n];
      }
    }
  }
}

71
void test_fc(const std::vector<int64_t>& input_shape,
Y
Yan Chunwei 已提交
72 73 74 75 76
             const std::vector<int64_t>& w_shape,
             int in_num_col_dims,
             bool has_bias) {
  CHECK_EQ(w_shape.size(), 2UL);

Z
zhupengyang 已提交
77
  const auto& bridges = lite::kernels::npu::bridges::Factory::Instance();
Y
Yan Chunwei 已提交
78 79 80 81
  const auto& supported_lists = bridges.AllFunctions();
  CHECK(bridges.HasType("fc"));

  Scope scope;
82
  std::string input_var_name("Input");
Y
Yan Chunwei 已提交
83 84 85 86
  std::string w_var_name("W");
  std::string bias_var_name("Bias");
  std::string out_var_name("Out");
  std::string out_ref_var_name("out_ref");
87
  auto* input = scope.Var(input_var_name)->GetMutable<Tensor>();
Y
Yan Chunwei 已提交
88 89 90
  auto* w = scope.Var(w_var_name)->GetMutable<Tensor>();
  auto* out = scope.Var(out_var_name)->GetMutable<Tensor>();
  auto* out_ref = scope.Var(out_ref_var_name)->GetMutable<Tensor>();
91
  input->Resize(input_shape);
Y
Yan Chunwei 已提交
92 93
  w->Resize(w_shape);

94
  FillTensor<float, int>(input);
Y
Yan Chunwei 已提交
95 96 97 98 99
  FillTensor<float, int>(w);

  // create fc op
  cpp::OpDesc fc_op_desc;
  fc_op_desc.SetType("fc");
100
  fc_op_desc.SetInput("Input", {input_var_name});
Y
Yan Chunwei 已提交
101 102 103 104 105 106 107 108 109 110 111
  fc_op_desc.SetInput("W", {w_var_name});
  fc_op_desc.SetOutput("Out", {out_var_name});
  fc_op_desc.SetAttr("in_num_col_dims", static_cast<int>(in_num_col_dims));
  if (has_bias) {
    auto* bias = scope.Var(bias_var_name)->GetMutable<Tensor>();
    bias->Resize({w_shape[1]});
    FillTensor<float, int>(bias);
    fc_op_desc.SetInput("Bias", {bias_var_name});
  }

  auto fc_op = CreateOp<operators::FcOpLite>(fc_op_desc, &scope);
112
  LauchOp(fc_op, {input_var_name}, {out_var_name});
Y
Yan Chunwei 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  out_ref->CopyDataFrom(*out);

  // compare results
  fc_ref(fc_op);
  auto* out_data = out->mutable_data<float>();
  auto* out_ref_data = out_ref->mutable_data<float>();
  for (int i = 0; i < out->dims().production(); i++) {
    EXPECT_NEAR(out_data[i], out_ref_data[i], 1e-5);
  }
}

TEST(NPUBridges, fc) {
  for (bool use_bias : {true, false}) {
    test_fc({1, 8, 8, 1}, {8, 4}, 2, use_bias);
    test_fc({1, 5, 5, 1}, {5, 7}, 2, use_bias);
    test_fc({1, 4, 1, 1}, {4, 8}, 1, use_bias);
129
    test_fc({1, 1024, 1, 1}, {1024, 1000}, 1, use_bias);
Y
Yan Chunwei 已提交
130 131 132
  }
}

Z
zhupengyang 已提交
133
}  // namespace bridges
Y
Yan Chunwei 已提交
134
}  // namespace npu
Z
zhupengyang 已提交
135
}  // namespace kernels
Y
Yan Chunwei 已提交
136 137 138 139 140
}  // namespace lite
}  // namespace paddle

USE_LITE_OP(fc);
USE_NPU_BRIDGE(fc);