conv_transpose_op.cc 6.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"
18
#include "lite/operators/conv_op.h"
Y
Yan Chunwei 已提交
19 20 21

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

25
int ConvTransposeConverter(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();
Y
Yan Chunwei 已提交
30
  auto op_type = op_info->Type();
31 32
  auto scope = op->scope();
  VLOG(3) << "[NPU] Converting " << op_type << "... ";
Y
Yan Chunwei 已提交
33

34
  // Get input, output and op attributes
35 36 37 38
  auto input_name = op_info->Input("Input").front();
  auto input = scope->FindMutableTensor(input_name);
  auto input_dims = input->dims();
  CHECK_EQ(input_dims.size(), 4);
39

40 41 42 43
  auto filter_name = op_info->Input("Filter").front();
  auto filter = scope->FindMutableTensor(filter_name);
  auto filter_dims = filter->dims();
  CHECK_EQ(filter_dims.size(), 4);
44

45
  auto output_name = op_info->Output("Output").front();
46

Y
Yan Chunwei 已提交
47
  auto strides = op_info->GetAttr<std::vector<int>>("strides");
48
  CHECK_EQ(strides.size(), 2L);
Y
Yan Chunwei 已提交
49
  auto groups = op_info->GetAttr<int>("groups");
50 51 52 53 54
  if (groups > 1) {
    LOG(WARNING) << "[NPU] only support groups == 1";
    return FAILED;
  }

55 56
  auto fuse_relu =
      op_info->HasAttr("fuse_relu") && op_info->GetAttr<bool>("fuse_relu");
57 58 59 60 61 62 63
  std::vector<int> output_size;
  if (op_info->HasAttr("output_size")) {
    output_size = op_info->GetAttr<std::vector<int>>("output_size");
  }

  auto paddings = op_info->GetAttr<std::vector<int>>("paddings");
  auto dilations = op_info->GetAttr<std::vector<int>>("dilations");
H
HappyAngel 已提交
64
  CHECK_EQ(dilations.size(), 2L);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  std::string padding_algorithm =
      op_info->HasAttr("padding_algorithm")
          ? op_info->GetAttr<std::string>("padding_algorithm")
          : "";
  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)
      << "[NPU] Paddings size should be the same or twice as the input size.";
  operators::UpdatePaddingAndDilation(&paddings,
                                      &dilations,
                                      strides,
                                      padding_algorithm,
                                      input_dims,
                                      filter_dims);
  if (paddings[0] != paddings[1] || paddings[2] != paddings[3]) {
    LOG(WARNING) << "[NPU] only support \"pad_top == pad_bottom && pad_left == "
                    "pad_right\" .";
    return FAILED;
  }
Y
Yan Chunwei 已提交
88

89
  // Input node
90 91 92
  std::shared_ptr<Node> input_node = nullptr;
  if (graph->Has(input_name)) {
    input_node = graph->Get(input_name);
93
  } else {
94
    input_node = graph->Add(input_name, *input);
95 96 97
  }

  // Create input sizes node to describe the dimensions of input tensor
98
  std::vector<int32_t> input_sizes;
99 100
  input_sizes.push_back(input_dims[0]);
  input_sizes.push_back(filter_dims[1] * groups);
Y
Yan Chunwei 已提交
101
  for (int i = 0; i < strides.size(); i++) {
102
    int kernel_ext = dilations[i] * (filter_dims[i + 2] - 1) + 1;
103 104
    int output_size = (input_dims[i + 2] - 1) * strides[i] + kernel_ext -
                      paddings[i * 2] - paddings[i * 2 + 1];
105
    input_sizes.push_back(output_size);
Y
Yan Chunwei 已提交
106
  }
107 108 109 110 111 112 113 114
  if (!output_size.empty()) {
    CHECK_EQ(output_size.size(), 2L);
    if (output_size[0] != input_sizes[2] || output_size[1] != input_sizes[3]) {
      LOG(WARNING) << "[NPU] not support output_size: " << output_size[0]
                   << ", " << output_size[1];
      return FAILED;
    }
  }
115
  auto input_sizes_node = graph->Add(output_name + "/input_sizes", input_sizes);
Y
Yan Chunwei 已提交
116

117
  // Filter node
118
  auto filter_node = graph->Add(filter_name, *filter);
Y
Yan Chunwei 已提交
119

120
  // Deconv node
121 122 123 124 125
  auto conv_transpose_node = graph->Add<ge::op::Deconvolution>(output_name);
  auto conv_transpose_op = conv_transpose_node->data<ge::op::Deconvolution>();
  conv_transpose_op->set_input_input_sizes(*input_sizes_node->data());
  conv_transpose_op->set_input_filter(*filter_node->data());
  conv_transpose_op->set_input_x(*input_node->data());
126
  // Set attributes
127 128 129 130 131 132 133
  conv_transpose_op->set_attr_format(0);  // NCHW
  // "SAME" is different from paddle
  if (padding_algorithm == "VALID") {
    conv_transpose_op->set_attr_pad_mode(5);
  } else {
    conv_transpose_op->set_attr_pad_mode(0);  // NOTSET
  }
134 135
  conv_transpose_op->set_attr_group(groups);
  conv_transpose_op->set_attr_pad(ge::AttrValue::LIST_INT(
136
      {paddings[0], paddings[1], paddings[2], paddings[3]}));
137
  conv_transpose_op->set_attr_dilation(
Y
Yan Chunwei 已提交
138
      ge::AttrValue::LIST_INT({dilations[0], dilations[1]}));
139
  conv_transpose_op->set_attr_stride(
Y
Yan Chunwei 已提交
140
      ge::AttrValue::LIST_INT({strides[0], strides[1]}));
141
  conv_transpose_op->set_attr_kernel(
142
      ge::AttrValue::LIST_INT({filter_dims[2], filter_dims[3]}));
Y
Yan Chunwei 已提交
143

144 145
  // Append add node to add bias if exists bias
  if (HasInputArg(op_info, scope, "Bias")) {
146
    std::shared_ptr<Node> bias_node = nullptr;
147
    auto bias_name = op_info->Input("Bias").front();
148 149 150 151 152 153 154 155
    if (graph->Has(bias_name)) {
      bias_node = graph->Get(bias_name);
    } else {
      auto bias = scope->FindMutableTensor(bias_name);
      auto channel_size = bias->dims().production();
      CHECK_EQ(channel_size, filter_dims[1] * groups);
      bias_node = graph->Add(bias_name, *bias, {1, channel_size, 1, 1});
    }
156
    // Append add node to add bias node
157 158 159 160 161
    auto add_node = graph->Add<ge::op::Add>(output_name);
    auto add_op = add_node->data<ge::op::Add>();
    add_op->set_input_x1(*conv_transpose_node->data());
    add_op->set_input_x2(*bias_node->data());
    conv_transpose_node = add_node;
Y
Yan Chunwei 已提交
162 163 164
  }

  if (fuse_relu) {
165
    // Append relu node if fuse_relu is true
166 167 168 169
    auto relu_node = graph->Add<ge::op::Activation>(output_name);
    auto relu_op = relu_node->data<ge::op::Activation>();
    relu_op->set_input_x(*conv_transpose_node->data());
    relu_op->set_attr_mode(CvtActMode("relu"));
Y
Yan Chunwei 已提交
170
  }
171
  return REBUILD_WHEN_SHAPE_CHANGED;
Y
Yan Chunwei 已提交
172 173 174
}

}  // namespace npu
175
}  // namespace subgraph
Y
Yan Chunwei 已提交
176 177 178
}  // namespace lite
}  // namespace paddle

179 180
REGISTER_SUBGRAPH_BRIDGE(conv2d_transpose,
                         kNPU,
181
                         paddle::lite::subgraph::npu::ConvTransposeConverter);