pool_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.

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

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

25
int PoolConverter(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 + "...";
Y
Yan Chunwei 已提交
33

34 35 36 37 38 39 40 41 42 43 44
  // Get input and output vars and op attributes
  auto x_name = op_info->Input("X").front();
  auto x = scope->FindMutableTensor(x_name);
  auto x_dims = x->dims();
  auto out_name = op_info->Output("Out").front();
  auto pooling_type = op_info->GetAttr<std::string>("pooling_type");
  auto global_pooling = op_info->GetAttr<bool>("global_pooling");
  auto ksize = op_info->GetAttr<std::vector<int>>("ksize");
  auto paddings = op_info->GetAttr<std::vector<int>>("paddings");

  // X node
45 46 47
  std::shared_ptr<Node> x_node = nullptr;
  if (graph->Has(x_name)) {
    x_node = graph->Get(x_name);
48
  } else {
49
    x_node = graph->Add(x_name, *x);
50
  }
Z
zhupengyang 已提交
51

52
  // pool mode
Z
zhupengyang 已提交
53
  int mode = 0;
Y
Yan Chunwei 已提交
54
  if (pooling_type == "max") {
Z
zhupengyang 已提交
55
    mode = 0;
Y
Yan Chunwei 已提交
56
  } else if (pooling_type == "avg") {
Z
zhupengyang 已提交
57
    mode = 1;
58 59 60 61
    if (!op_info->GetAttr<bool>("exclusive")) {
      LOG(WARNING) << "[NPU] Only exclusive=true is supported for the pooling "
                      "type 'avg' by HiAI DDK";
    }
Y
Yan Chunwei 已提交
62
  } else {
63 64
    LOG(WARNING) << "[NPU] Unsupported pooling type: " << pooling_type;
    return FAILED;
Y
Yan Chunwei 已提交
65
  }
Z
zhupengyang 已提交
66

67
  // pad mode
Z
zhupengyang 已提交
68 69 70 71 72 73 74 75 76 77
  int pad_mode = 0;
  std::string padding_algorithm("");
  if (op_info->HasAttr("padding_algorithm")) {
    padding_algorithm = op_info->GetAttr<std::string>("padding_algorithm");
  }
  if (padding_algorithm == "SAME") {
    pad_mode = 6;
  } else if (padding_algorithm == "VALID") {
    pad_mode = 5;
  }
Y
Yan Chunwei 已提交
78

79
  // paddings and strides
Z
zhupengyang 已提交
80 81 82 83 84 85 86
  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)
87
      << "[NPU] Paddings size should be the same or twice as the inputs size.";
Z
zhupengyang 已提交
88 89 90
  bool adaptive = false;
  if (op_info->HasAttr("adaptive")) {
    adaptive = op_info->GetAttr<bool>("adaptive");
91
  }
Y
Yan Chunwei 已提交
92
  auto strides = op_info->GetAttr<std::vector<int>>("strides");
93 94 95 96 97 98 99
  lite::operators::UpdatePadding(&paddings,
                                 global_pooling,
                                 adaptive,
                                 padding_algorithm,
                                 x->dims(),
                                 strides,
                                 ksize);
Z
zhupengyang 已提交
100

101
  // ceil mode
102 103
  bool ceil_mode =
      op_info->HasAttr("ceil_mode") && op_info->GetAttr<bool>("ceil_mode");
104 105

  // Pooling node
106 107 108 109 110 111 112
  auto pool_node = graph->Add<ge::op::Pooling>(out_name);
  auto pool_op = pool_node->data<ge::op::Pooling>();
  pool_op->set_input_x(*x_node->data());
  pool_op->set_attr_mode(mode);
  pool_op->set_attr_pad_mode(pad_mode);
  pool_op->set_attr_global_pooling(global_pooling);
  pool_op->set_attr_window(ge::AttrValue::LIST_INT(ksize.begin(), ksize.end()));
113 114
  pool_op->set_attr_pad(
      ge::AttrValue::LIST_INT(paddings.begin(), paddings.end()));
115
  pool_op->set_attr_stride(
116
      ge::AttrValue::LIST_INT(strides.begin(), strides.end()));
117 118 119 120
  if (ceil_mode) {
    pool_op->set_attr_ceil_mode(1);
    pool_op->set_attr_data_mode(0);
  }
121
  return REBUILD_WHEN_SHAPE_CHANGED;
Y
Yan Chunwei 已提交
122 123 124
}

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

129 130
REGISTER_SUBGRAPH_BRIDGE(pool2d,
                         kNPU,
131
                         paddle::lite::subgraph::npu::PoolConverter);