conv_transpose_op.cc 5.1 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 ConvTransposeConverter(void* ctx, OpLite* op) {
  CHECK(ctx != nullptr);
  CHECK(op != nullptr);
  auto graph = static_cast<Graph*>(ctx);
  auto op_info = op->op_info();
Y
Yan Chunwei 已提交
29
  auto op_type = op_info->Type();
30 31
  auto scope = op->scope();
  VLOG(3) << "[NPU] Converting " << op_type << "... ";
Y
Yan Chunwei 已提交
32

33
  // Get input, output and op attributes
Y
Yan Chunwei 已提交
34
  auto input_var_name = op_info->Input("Input").front();
35
  auto input = scope->FindVar(input_var_name)->GetMutable<Tensor>();
Y
Yan Chunwei 已提交
36
  auto input_shape = input->dims().Vectorize();
37
  auto output_var_name = op_info->Output("Output").front();
Y
Yan Chunwei 已提交
38
  auto filter_var_name = op_info->Input("Filter").front();
39
  auto filter = scope->FindVar(filter_var_name)->GetMutable<Tensor>();
Y
Yan Chunwei 已提交
40 41 42 43 44 45 46 47
  auto filter_shape = filter->dims().Vectorize();
  CHECK_EQ(input_shape.size(), 4);
  CHECK_EQ(filter_shape.size(), 4);
  auto strides = op_info->GetAttr<std::vector<int>>("strides");
  auto paddings = op_info->GetAttr<std::vector<int>>("paddings");
  auto groups = op_info->GetAttr<int>("groups");
  auto dilations = op_info->GetAttr<std::vector<int>>("dilations");
  auto fuse_relu = op_info->GetAttr<bool>("fuse_relu");
H
HappyAngel 已提交
48 49
  CHECK_EQ(strides.size(), 2L);
  CHECK_EQ(dilations.size(), 2L);
Y
Yan Chunwei 已提交
50

51 52 53 54 55 56 57
  if (paddings.size() == 2L) {
    for (size_t i = 0; i < 2L; ++i) {
      int copy_pad = *(paddings.begin() + 2 * i);
      paddings.insert(paddings.begin() + 2 * i + 1, copy_pad);
    }
  }
  CHECK_EQ(paddings.size(), 4L)
58
      << "[NPU] Paddings size should be the same or twice as the input size.";
59

60
  // Create deconv node
Y
Yan Chunwei 已提交
61
  auto conv_transpose_node =
62
      graph->AddNode<ge::op::Deconvolution>(output_var_name);
63

64 65 66 67
  // Create input sizes node to describe the dimensions of input tensor
  std::vector<int32_t> input_sizes;
  input_sizes.push_back(input_shape[0]);
  input_sizes.push_back(filter_shape[1] * groups);
Y
Yan Chunwei 已提交
68 69 70 71
  for (int i = 0; i < strides.size(); i++) {
    int kernel_ext = dilations[i] * (filter_shape[i + 2] - 1) + 1;
    int output_size =
        (input_shape[i + 2] - 1) * strides[i] + kernel_ext - 2 * paddings[i];
72
    input_sizes.push_back(output_size);
Y
Yan Chunwei 已提交
73 74
  }
  auto input_sizes_const_node =
75
      graph->AddNode(output_var_name + "/input_sizes", input_sizes);
Y
Yan Chunwei 已提交
76 77
  conv_transpose_node->set_input_input_sizes(*input_sizes_const_node);

78 79
  // Create filter node
  auto filter_const_node = graph->AddNode(filter_var_name, *filter);
Y
Yan Chunwei 已提交
80 81
  conv_transpose_node->set_input_filter(*filter_const_node);

82 83
  // Set input node
  conv_transpose_node->set_input_x(*graph->GetNode(input_var_name));
Y
Yan Chunwei 已提交
84

85
  // Set attributes
Y
Yan Chunwei 已提交
86 87 88 89
  conv_transpose_node->set_attr_format(0);    // NCHW
  conv_transpose_node->set_attr_pad_mode(0);  // NOTSET
  conv_transpose_node->set_attr_group(groups);
  conv_transpose_node->set_attr_pad(ge::AttrValue::LIST_INT(
90
      {paddings[0], paddings[1], paddings[2], paddings[3]}));
Y
Yan Chunwei 已提交
91 92 93 94 95 96 97
  conv_transpose_node->set_attr_dilation(
      ge::AttrValue::LIST_INT({dilations[0], dilations[1]}));
  conv_transpose_node->set_attr_stride(
      ge::AttrValue::LIST_INT({strides[0], strides[1]}));
  conv_transpose_node->set_attr_kernel(
      ge::AttrValue::LIST_INT({filter_shape[2], filter_shape[3]}));

98
  // Append add node to add bias if exists bias
Y
Yan Chunwei 已提交
99
  std::shared_ptr<ge::Operator> output_node = conv_transpose_node;
100 101
  if (HasInputArg(op_info, scope, "Bias")) {
    // Create bias node
Y
Yan Chunwei 已提交
102
    auto bias_var_name = op_info->Input("Bias").front();
103 104
    CHECK(!graph->HasNode(bias_var_name));
    auto* bias = scope->FindVar(bias_var_name)->GetMutable<Tensor>();
Y
Yan Chunwei 已提交
105 106
    auto channel_size = bias->dims().production();
    CHECK_EQ(channel_size, filter_shape[1] * groups);
107 108 109 110
    auto bias_const_node =
        graph->AddNode(bias_var_name, *bias, {1, channel_size, 1, 1});
    // Append add node to add bias node
    auto add_node = graph->AddNode<ge::op::Add>(output_var_name);
Y
Yan Chunwei 已提交
111 112 113 114 115 116
    add_node->set_input_x1(*conv_transpose_node);
    add_node->set_input_x2(*bias_const_node);
    output_node = add_node;
  }

  if (fuse_relu) {
117 118
    // Append relu node if fuse_relu is true
    auto relu_node = graph->AddNode<ge::op::Activation>(output_var_name);
Y
Yan Chunwei 已提交
119
    relu_node->set_input_x(*output_node);
120
    relu_node->set_attr_mode(CvtActMode("relu"));
Y
Yan Chunwei 已提交
121
  }
122
  return REBUILD_WHEN_SHAPE_CHANGED;
Y
Yan Chunwei 已提交
123 124 125
}

}  // namespace npu
126
}  // namespace subgraph
Y
Yan Chunwei 已提交
127 128 129
}  // namespace lite
}  // namespace paddle

130 131 132
REGISTER_SUBGRAPH_BRIDGE(NPU,
                         conv2d_transpose,
                         paddle::lite::subgraph::npu::ConvTransposeConverter);