conv_op.cc 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
// Copyright (c) 2020 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/conv_op.h"
#include "lite/kernels/huawei_ascend_npu/bridges/graph.h"
#include "lite/kernels/huawei_ascend_npu/bridges/utility.h"
#include "lite/kernels/npu/bridges/registry.h"

namespace paddle {
namespace lite {
namespace subgraph {
namespace huawei_ascend_npu {

int ConvConverter(void* ctx, OpLite* op, KernelBase* kernel) {
  CHECK(ctx != nullptr);
  CHECK(op != nullptr);
  auto graph = static_cast<Graph*>(ctx);
  auto op_info = op->op_info();
  auto op_type = op_info->Type();
  auto scope = op->scope();
  VLOG(3) << "[HUAWEI_ASCEND_NPU] Converting " << op_type << "... ";

  // Get input and output vars and op attributes
  auto input_name = op_info->Input("Input").front();
  auto input = scope->FindMutableTensor(input_name);
  auto input_dims = input->dims();

  auto filter_name = op_info->Input("Filter").front();
  auto filter = scope->FindMutableTensor(filter_name);
  auto filter_dims = filter->dims();

  auto output_name = op_info->Output("Output").front();
  auto output = scope->FindMutableTensor(output_name);
  auto output_dims = output->dims();

  auto bs = input_dims[0];
  auto ic = input_dims[1];
  auto oc = filter_dims[0];
  CHECK_EQ(input_dims.size(), 4L);
  CHECK_EQ(output_dims.size(), 4L);
  CHECK_EQ(filter_dims.size(), 4L);
  CHECK_EQ(output_dims[0], bs);
  CHECK_EQ(output_dims[1], oc);
  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");
  bool with_act =
      op_info->HasAttr("with_act") && op_info->GetAttr<bool>("with_act");
  std::string act_type =
      with_act ? op_info->GetAttr<std::string>("act_type") : "";
  float leaky_relu_alpha = act_type == "leaky_relu"
                               ? op_info->GetAttr<float>("leaky_relu_alpha")
                               : 0.f;
  CHECK_EQ(strides.size(), 2L);
  CHECK_EQ(dilations.size(), 2L);

  // Input node
  std::shared_ptr<Node> input_node = nullptr;
  if (graph->Has(input_name)) {
    input_node = graph->Get(input_name);
  } else {
    input_node = graph->Add(input_name, *input);
  }

  if (paddings.size() == 2L) {
    for (size_t i = 0; i < strides.size(); ++i) {
      int copy_pad = *(paddings.begin() + 2 * i);
      paddings.insert(paddings.begin() + 2 * i + 1, copy_pad);
    }
  }
  CHECK_EQ(paddings.size(), 4L)
      << "[HUAWEI_ASCEND_NPU] Paddings size should be "
         "the same or twice as the input size.";

  std::string padding_algorithm("");
  if (op_info->HasAttr("padding_algorithm")) {
    padding_algorithm = op_info->GetAttr<std::string>("padding_algorithm");
  }
  operators::UpdatePaddingAndDilation(&paddings,
                                      &dilations,
                                      strides,
                                      padding_algorithm,
                                      input_dims,
                                      filter_dims);

98
  // Check Restrictions: HxW(input) == HxW(filter) if output feature h*w = 1*1
99
  if (output_dims[2] == 1) {
100 101
    int input_h = input_dims[2] + paddings[0] + paddings[1];
    int filter_h = (filter_dims[2] - 1) * dilations[0] + 1;
102 103 104 105 106 107 108 109 110 111 112 113 114
    if (input_h != filter_h) {
      LOG(WARNING) << "[HUAWEI_ASCEND_NPU] Huawei Ascend NPU DDK restriction: "
                      "input height after padding should equal to filter "
                      "height after dilation if output height is 1. Input "
                      "height after padding is: "
                   << input_h
                   << ", filter height after dilation is: " << filter_h;
      return FAILED;
    }
  }
  // Check Restrictions: HxW(input) == HxW(filter) if output feature h*w = 1*1
  if (output_dims[3] == 1) {
    int input_w = input_dims[3] + paddings[2] + paddings[3];
115
    int filter_w = (filter_dims[3] - 1) * dilations[1] + 1;
116 117 118 119 120 121 122 123 124
    if (input_w != filter_w) {
      LOG(WARNING) << "[HUAWEI_ASCEND_NPU] Huawei Ascend NPU DDK restriction: "
                      "input width after padding should equal to filter width "
                      "after dilation if output width is 1. Input width after "
                      "padding is: "
                   << input_w
                   << ", filter width after dilation is: " << filter_w;
      return FAILED;
    }
125
  }
126
  // Check Restrictions: outChannel divide groups should equal to 0
127 128 129 130 131 132 133
  if (oc % groups != 0) {
    LOG(WARNING) << "[HUAWEI_ASCEND_NPU] Huawei Ascend NPU DDK restriction: "
                    "out channel divice groups should equal to 0. out channel "
                    "is: "
                 << oc << ", groups is: " << groups;
    return FAILED;
  }
134

135 136 137
  // Filter node
  std::shared_ptr<Node> filter_node = nullptr;

138 139
  // Check depthwise mode, and decide whether use DepthwiseConv2D Op
  bool use_depthwise_conv = false;
140
  bool is_depthwise_mode = (ic == groups && oc == groups);
141 142
  if (is_depthwise_mode && dilations[0] == 1 && dilations[1] == 1) {
    use_depthwise_conv = true;
143
    // Change filter shape {oc, ic/groups = 1, kh, kw} => { K=1, oc, kh, hw}
144 145
    filter_node = graph->Add(
        filter_name, *filter, {1L, oc, filter_dims[2], filter_dims[3]});
146
    LOG(WARNING) << "[HUAWEI_ASCEND_NPU] DepthwiseConv2D op is used.";
147 148
  } else {
    filter_node = graph->Add(filter_name, *filter);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  }

  // Add bias node if exists bias
  // Supports the bias nodes with the following dimensions
  // 0: {oc} => 1D tensor of foramt ND
  // 1: {1, oc, oh, ow}
  // 2: {n, oc, oh, ow}
  std::vector<int64_t> bias_shape;
  std::shared_ptr<Node> bias_node = nullptr;
  bool is_channel_bias = false;
  if (HasInputArg(op_info, scope, "Bias")) {
    auto bias_name = op_info->Input("Bias").front();
    if (graph->Has(bias_name)) {
      bias_node = graph->Get(bias_name);
    } else {
      auto bias = scope->FindMutableTensor(bias_name);
      auto bias_dims = bias->dims();
      auto bias_data_size = bias_dims.production();
      auto output_data_size = output_dims.production();
      if (bias_data_size == oc) {
        // 0: {oc}
        bias_shape = {oc};
        is_channel_bias = true;
      } else if (bias_data_size == output_data_size / bs) {
        // 1: {1, oc, oh, ow}
        bias_shape = {1, output_dims[1], output_dims[2], output_dims[3]};
      } else if (bias_data_size == output_data_size) {
        // 2: {n, oc, oh, ow}
        bias_shape = output_dims.Vectorize();
      } else {
        LOG(WARNING)
            << "[HUAWEI_ASCEND_NPU] Bias dimension " << bias_dims
            << " isn't supported in conv2d Op when output dimension is "
            << output_dims;
        return FAILED;
      }
      bias_node = graph->Add(bias_name, *bias, bias_shape);
    }
  }

  // Conv node
  std::shared_ptr<Node> conv_node = nullptr;
  if (use_depthwise_conv && is_depthwise_mode) {
    conv_node = graph->Add<ge::op::DepthwiseConv2D>(output_name);
    auto conv_op = conv_node->data<ge::op::DepthwiseConv2D>();
    conv_op->set_input_x(*input_node->data());
    conv_op->set_input_filter(*filter_node->data());
    conv_op->set_attr_strides(
        ge::Operator::OpListInt({1, 1, strides[0], strides[1]}));
    conv_op->set_attr_dilations({1, 1, dilations[0], dilations[1]});
    conv_op->set_attr_pads(
        {paddings[0], paddings[1], paddings[2], paddings[3]});
    conv_op->set_attr_data_format("NCHW");
    if (bias_node != nullptr && is_channel_bias) {
      conv_op->set_input_bias(*bias_node->data());
204
      INPUT_UPDATE(conv_op, bias, bias_node);
205
    }
206 207 208
    INPUT_UPDATE(conv_op, x, input_node);
    INPUT_UPDATE(conv_op, filter, filter_node);
    OUTPUT_UPDATE(conv_op, y, conv_node);
209 210 211 212 213 214
  } else {
    conv_node = graph->Add<ge::op::Conv2D>(output_name);
    auto conv_op = conv_node->data<ge::op::Conv2D>();
    conv_op->set_input_x(*input_node->data());
    conv_op->set_input_filter(*filter_node->data());
    conv_op->set_attr_strides(
215
        ge::Operator::OpListInt({1, 1, strides[0], strides[1]}));
216 217 218
    conv_op->set_attr_pads(ge::Operator::OpListInt(
        {paddings[0], paddings[1], paddings[2], paddings[3]}));
    conv_op->set_attr_dilations(
219
        ge::Operator::OpListInt({1, 1, dilations[0], dilations[1]}));
220 221 222 223
    conv_op->set_attr_groups(groups);
    conv_op->set_attr_data_format("NCHW");
    if (bias_node != nullptr && is_channel_bias) {
      conv_op->set_input_bias(*bias_node->data());
224
      INPUT_UPDATE(conv_op, bias, bias_node);
225
    }
226 227 228
    INPUT_UPDATE(conv_op, x, input_node);
    INPUT_UPDATE(conv_op, filter, filter_node);
    OUTPUT_UPDATE(conv_op, y, conv_node);
229 230 231 232 233 234 235
  }
  // append Add node to support bias
  if (bias_node != nullptr && !is_channel_bias) {
    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_node->data());
    add_op->set_input_x2(*bias_node->data());
236 237 238
    INPUT_UPDATE(add_op, x1, conv_node);
    INPUT_UPDATE(add_op, x2, bias_node);
    OUTPUT_UPDATE(add_op, y, add_node);
239 240 241 242 243 244 245 246 247 248
  }
  CHECK(conv_node);

  // ONLY support relu/leaky_relu now
  // to do (@qili93): add more act types
  if (!act_type.empty()) {
    if (act_type == "relu") {
      auto act_node = graph->Add<ge::op::Relu>(output_name);
      auto act_op = act_node->data<ge::op::Relu>();
      act_op->set_input_x(*conv_node->data());
249 250
      INPUT_UPDATE(act_op, x, conv_node);
      OUTPUT_UPDATE(act_op, y, act_node);
251 252 253 254 255
    } else if (act_type == "leaky_relu") {
      auto act_node = graph->Add<ge::op::LeakyRelu>(output_name);
      auto act_op = act_node->data<ge::op::LeakyRelu>();
      act_op->set_input_x(*conv_node->data());
      act_op->set_attr_negative_slope(leaky_relu_alpha);
256 257
      INPUT_UPDATE(act_op, x, conv_node);
      OUTPUT_UPDATE(act_op, y, act_node);
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    } else {
      LOG(WARNING) << "[HUAWEI_ASCEND_NPU] act type not supported: "
                   << act_type;
      return FAILED;
    }
  }

  return REBUILD_WHEN_SHAPE_CHANGED;
}

}  // namespace huawei_ascend_npu
}  // namespace subgraph
}  // namespace lite
}  // namespace paddle

REGISTER_SUBGRAPH_BRIDGE(
    conv2d,
    kHuaweiAscendNPU,
    paddle::lite::subgraph::huawei_ascend_npu::ConvConverter);
REGISTER_SUBGRAPH_BRIDGE(
    depthwise_conv2d,
    kHuaweiAscendNPU,
    paddle::lite::subgraph::huawei_ascend_npu::ConvConverter);