mul_op.cc 4.3 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
// Note: all of the input weight vars should be handled in this converter
25
int MulConverter(void* ctx, OpLite* op, KernelBase* kernel) {
26 27 28 29
  CHECK(ctx != nullptr);
  CHECK(op != nullptr);
  auto graph = static_cast<Graph*>(ctx);
  auto op_info = op->op_info();
30
  auto op_type = op_info->Type();
31 32
  auto scope = op->scope();
  VLOG(3) << "[NPU] Converting " + op_type + "...";
33

34 35
  // Get input and output vars and op attributes
  auto x_name = op_info->Input("X").front();
36
  auto x = scope->FindTensor(x_name);
37
  auto x_dims = x->dims();
38

39
  auto y_name = op_info->Input("Y").front();
40
  auto y = scope->FindTensor(y_name);
41
  auto y_dims = y->dims();
42

43
  auto out_name = op_info->Output("Out").front();
44 45 46 47 48 49 50
  auto out = scope->FindTensor(out_name);
  auto out_dims = out->dims();
  if (out_dims.size() > 4) {
    LOG(WARNING) << "[NPU] not supported above 4-D.";
    return FAILED;
  }

Y
Yan Chunwei 已提交
51 52
  int x_num_col_dims = op_info->GetAttr<int>("x_num_col_dims");
  int y_num_col_dims = op_info->GetAttr<int>("y_num_col_dims");
53 54 55 56 57
  int m = x_dims.Slice(0, x_num_col_dims).production();
  int k = x_dims.Slice(x_num_col_dims, x_dims.size()).production();
  CHECK_EQ(k, y_dims.Slice(0, y_num_col_dims).production())
      << "[NPU] columns of X must be equal with rows of Y";
  int n = y_dims.Slice(y_num_col_dims, y_dims.size()).production();
58
  VLOG(3) << "m:" << m << ",n:" << n << ",k:" << k;
59 60
  VLOG(3) << "x_name:" << x_name << ", is data: " << graph->Has(x_name);
  VLOG(3) << "y_name:" << y_name << ", is data: " << graph->Has(y_name);
61

62
  // X node which supports persistable and non-persistable tensor, and
63
  // reshape to (m, k)
64 65 66
  std::shared_ptr<Node> x_node = nullptr;
  if (graph->Has(x_name)) {
    x_node = graph->Get(x_name);
67 68 69 70 71 72 73 74
    if (x_dims.size() != 2) {
      auto reshaped_x_node = graph->Add<ge::op::Reshape>(x_name + "/reshape");
      auto reshaped_x_op = reshaped_x_node->data<ge::op::Reshape>();
      reshaped_x_op->set_input_tensor(*x_node->data());
      reshaped_x_op->set_attr_shape({m, k});
      reshaped_x_op->set_attr_axis(0);
      x_node = reshaped_x_node;
    }
Y
Yan Chunwei 已提交
75
  } else {
76
    x_node = graph->Add(x_name, *x, {m, k});
Y
Yan Chunwei 已提交
77
  }
78 79

  // Y node which only supports persistable tensor, and reshape to
80
  // (k,n)
81 82 83
  std::shared_ptr<Node> y_node = nullptr;
  if (graph->Has(y_name)) {
    y_node = graph->Get(y_name);
84 85 86 87 88 89 90 91
    if (y_dims.size() != 2) {
      auto reshaped_y_node = graph->Add<ge::op::Reshape>(y_name + "/reshape");
      auto reshaped_y_op = reshaped_y_node->data<ge::op::Reshape>();
      reshaped_y_op->set_input_tensor(*y_node->data());
      reshaped_y_op->set_attr_shape({k, n});
      reshaped_y_op->set_attr_axis(0);
      y_node = reshaped_y_node;
    }
Y
Yan Chunwei 已提交
92
  } else {
93
    y_node = graph->Add(y_name, *y, {k, n});
Y
Yan Chunwei 已提交
94
  }
95 96

  // Matmul node
97 98 99 100
  auto mul_node = graph->Add<ge::op::MatMul>(out_name);
  auto mul_op = mul_node->data<ge::op::MatMul>();
  mul_op->set_input_x1(*x_node->data());
  mul_op->set_input_x2(*y_node->data());
101 102 103 104 105 106 107 108 109 110 111

  if (out_dims.size() != 2) {
    auto reshaped_out_node = graph->Add<ge::op::Reshape>(out_name);
    auto reshaped_out_op = reshaped_out_node->data<ge::op::Reshape>();
    reshaped_out_op->set_input_tensor(*mul_node->data());
    auto out_shape = out_dims.Vectorize();
    reshaped_out_op->set_attr_shape(
        ge::AttrValue::LIST_INT(out_shape.begin(), out_shape.end()));
    reshaped_out_op->set_attr_axis(0);
  }

112
  return REBUILD_WHEN_SHAPE_CHANGED;
Y
Yan Chunwei 已提交
113 114 115
}

}  // namespace npu
116
}  // namespace subgraph
Y
Yan Chunwei 已提交
117 118 119
}  // namespace lite
}  // namespace paddle

120
REGISTER_SUBGRAPH_BRIDGE(mul, kNPU, paddle::lite::subgraph::npu::MulConverter);