fc_op.cc 3.7 KB
Newer Older
Y
Yan Chunwei 已提交
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.

15
#include "lite/kernels/npu/bridges/graph.h"
Z
zhupengyang 已提交
16
#include "lite/kernels/npu/bridges/registry.h"
17
#include "lite/kernels/npu/bridges/utility.h"
Y
Yan Chunwei 已提交
18 19 20

namespace paddle {
namespace lite {
21
namespace subgraph {
Y
Yan Chunwei 已提交
22 23
namespace npu {

24 25 26 27 28
int FCConverter(void* ctx, OpLite* op) {
  CHECK(ctx != nullptr);
  CHECK(op != nullptr);
  auto graph = static_cast<Graph*>(ctx);
  auto op_info = op->op_info();
29
  auto op_type = op_info->Type();
30 31
  auto scope = op->scope();
  VLOG(3) << "[NPU] Converting " + op_type + "...";
Y
Yan Chunwei 已提交
32 33 34

  auto x_var_name = op_info->Input("Input").front();
  auto w_var_name = op_info->Input("W").front();
35
  auto out_var_name = op_info->Output("Out").front();
Y
Yan Chunwei 已提交
36 37

  int in_num_col_dims = op_info->GetAttr<int>("in_num_col_dims");
38 39
  auto x = scope->FindVar(x_var_name)->GetMutable<Tensor>();
  auto w = scope->FindVar(w_var_name)->GetMutable<Tensor>();
40 41
  auto x_dims = x->dims();
  auto w_dims = w->dims();
Y
Yan Chunwei 已提交
42 43 44 45 46 47 48

  CHECK_GE(x_dims.size(), 2UL);
  CHECK_EQ(w_dims.size(), 2UL);

  int m = x_dims.Slice(0, in_num_col_dims).production();
  int k = x_dims.Slice(in_num_col_dims, x_dims.size()).production();
  int n = w_dims[1];
49
  CHECK_EQ(k * n, w_dims.production());
50
  VLOG(3) << "[NPU] x dims: " << x_dims << " w dims: " << w_dims << " m: " << m
51
          << " k: " << k << " n: " << n;
Y
Yan Chunwei 已提交
52

53 54
  auto fc_node = graph->AddNode<ge::op::FullConnection>(out_var_name + "/fc");
  CHECK(!graph->HasNode(w_var_name));
Y
Yan Chunwei 已提交
55

56
  // Reshape x to (m, k, 1, 1)
57
  auto reshaped_x_node =
58 59
      graph->AddNode<ge::op::Reshape>(x_var_name + "/reshape");
  reshaped_x_node->set_input_tensor(*graph->GetNode(x_var_name));
60 61 62 63
  reshaped_x_node->set_attr_shape({m, k, 1, 1});
  reshaped_x_node->set_attr_axis(0);
  fc_node->set_input_x(*reshaped_x_node);

64
  // Create w const node, set its shape to (n, k, 1, 1) and fill with
65
  // the transposed w tensor
66 67 68
  Tensor transpose_w;
  transpose_w.Resize({n, k, 1, 1});
  auto transpose_w_data = transpose_w.mutable_data<float>();
69 70 71
  auto w_data = w->mutable_data<float>();
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n; j++) {
72
      transpose_w_data[j * k + i] = w_data[i * n + j];
73 74
    }
  }
75
  auto w_const_node = graph->AddNode(w_var_name, transpose_w);
76
  fc_node->set_input_w(*w_const_node);
Y
Yan Chunwei 已提交
77

78 79
  // Add bias node if bias tensor exists
  if (HasInputArg(op_info, scope, "Bias")) {
80 81 82
    auto bias_var_name = op_info->Input("Bias").front();
    auto bias = scope->FindVar(bias_var_name)->GetMutable<lite::Tensor>();
    auto bias_dims = bias->dims();
83
    CHECK(!graph->HasNode(bias_var_name));
84 85
    CHECK_EQ(bias_dims.production(), n);

86
    auto bias_const_node = graph->AddNode(bias_var_name, *bias, {1, n, 1, 1});
87
    fc_node->set_input_b(*bias_const_node);
Y
Yan Chunwei 已提交
88 89
  }

90 91
  // Reshape output of fc_node from (m, n, 1, 1) to (m, n)
  auto reshaped_fc_node = graph->AddNode<ge::op::Reshape>(out_var_name);
92 93 94
  reshaped_fc_node->set_input_tensor(*fc_node);
  reshaped_fc_node->set_attr_shape({m, n});
  reshaped_fc_node->set_attr_axis(0);
95
  return REBUILD_WHEN_SHAPE_CHANGED;
Y
Yan Chunwei 已提交
96 97 98
}

}  // namespace npu
99
}  // namespace subgraph
Y
Yan Chunwei 已提交
100 101 102
}  // namespace lite
}  // namespace paddle

103
REGISTER_SUBGRAPH_BRIDGE(NPU, fc, paddle::lite::subgraph::npu::FCConverter);