pool_op.cc 3.9 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/kernels/npu/bridges/graph.h"
Z
zhupengyang 已提交
17
#include "lite/kernels/npu/bridges/registry.h"
18
#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 26 27 28 29
int PoolConverter(void* ctx, OpLite* op) {
  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

  auto x_var_name = op_info->Input("X").front();
Z
zhupengyang 已提交
35
  auto x = scope->FindTensor(x_var_name);
36 37 38
  auto out_var_name = op_info->Output("Out").front();
  auto pool_node = graph->AddNode<ge::op::Pooling>(out_var_name);
  pool_node->set_input_x(*graph->GetNode(x_var_name));
Z
zhupengyang 已提交
39 40

  int mode = 0;
Y
Yan Chunwei 已提交
41 42
  auto pooling_type = op_info->GetAttr<std::string>("pooling_type");
  if (pooling_type == "max") {
Z
zhupengyang 已提交
43
    mode = 0;
Y
Yan Chunwei 已提交
44
  } else if (pooling_type == "avg") {
Z
zhupengyang 已提交
45
    mode = 1;
Y
Yan Chunwei 已提交
46
    CHECK(op_info->GetAttr<bool>("exclusive"))
47
        << "[NPU] exclusive must be true in HiAI DDK";
Y
Yan Chunwei 已提交
48
  } else {
49 50
    LOG(WARNING) << "[NPU] Unsupported pooling type: " << pooling_type;
    return FAILED;
Y
Yan Chunwei 已提交
51
  }
Z
zhupengyang 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  pool_node->set_attr_mode(mode);

  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;
  }
  pool_node->set_attr_pad_mode(pad_mode);

  bool global_pooling = op_info->GetAttr<bool>("global_pooling");
  pool_node->set_attr_global_pooling(global_pooling);

Y
Yan Chunwei 已提交
69
  auto ksize = op_info->GetAttr<std::vector<int>>("ksize");
70 71
  pool_node->set_attr_window(
      ge::AttrValue::LIST_INT(ksize.begin(), ksize.end()));
Y
Yan Chunwei 已提交
72

Z
zhupengyang 已提交
73 74 75 76 77 78 79 80
  auto paddings = op_info->GetAttr<std::vector<int>>("paddings");
  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)
81
      << "[NPU] Paddings size should be the same or twice as the inputs size.";
Z
zhupengyang 已提交
82 83 84
  bool adaptive = false;
  if (op_info->HasAttr("adaptive")) {
    adaptive = op_info->GetAttr<bool>("adaptive");
85
  }
Y
Yan Chunwei 已提交
86
  auto strides = op_info->GetAttr<std::vector<int>>("strides");
87 88 89 90 91 92 93 94 95 96 97
  lite::operators::UpdatePadding(&paddings,
                                 global_pooling,
                                 adaptive,
                                 padding_algorithm,
                                 x->dims(),
                                 strides,
                                 ksize);
  pool_node->set_attr_pad(ge::AttrValue::LIST_INT{
      paddings[0], paddings[1], paddings[2], paddings[3]});
  pool_node->set_attr_stride(
      ge::AttrValue::LIST_INT(strides.begin(), strides.end()));
Z
zhupengyang 已提交
98 99

  int ceil_mode = 0;
Y
Yan Chunwei 已提交
100
  if (op_info->HasAttr("ceil_mode")) {
Z
zhupengyang 已提交
101
    ceil_mode = op_info->GetAttr<bool>("ceil_mode") ? 1 : 0;
Y
Yan Chunwei 已提交
102
  }
Z
zhupengyang 已提交
103
  pool_node->set_attr_ceil_mode(ceil_mode);
104 105
  // pool_node->set_attr_data_mode(data_mode);
  return REBUILD_WHEN_SHAPE_CHANGED;
Y
Yan Chunwei 已提交
106 107 108
}

}  // namespace npu
109
}  // namespace subgraph
Y
Yan Chunwei 已提交
110 111 112
}  // namespace lite
}  // namespace paddle

113 114 115
REGISTER_SUBGRAPH_BRIDGE(NPU,
                         pool2d,
                         paddle::lite::subgraph::npu::PoolConverter);