pool_op.cc 4.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.

Z
zhupengyang 已提交
15
#include "lite/operators/pool_op.h"
16
#include "lite/backends/npu/builder.h"
Z
zhupengyang 已提交
17
#include "lite/kernels/npu/bridges/registry.h"
Y
Yan Chunwei 已提交
18 19 20

namespace paddle {
namespace lite {
Z
zhupengyang 已提交
21
namespace kernels {
Y
Yan Chunwei 已提交
22
namespace npu {
Z
zhupengyang 已提交
23
namespace bridges {
Y
Yan Chunwei 已提交
24 25 26

node_map_type PoolConverter(const std::shared_ptr<lite::OpLite> pool_op,
                            const node_map_type& inputs_map) {
27 28 29
  auto scope = pool_op->scope();
  auto op_info = pool_op->op_info();
  auto op_type = op_info->Type();
30
  auto unique_op_type = lite::npu::UniqueName(op_type);
31
  LOG(INFO) << "[NPU] Converting " + op_type + "...";
Y
Yan Chunwei 已提交
32

33 34
  std::shared_ptr<ge::op::Pooling> pool_node =
      std::make_shared<ge::op::Pooling>(unique_op_type);
Y
Yan Chunwei 已提交
35
  auto x_var_name = op_info->Input("X").front();
Z
zhupengyang 已提交
36 37 38 39 40 41
  auto x = scope->FindTensor(x_var_name);
  pool_node->set_input_x(*inputs_map.at(x_var_name));
  lite::npu::OpList::Global().add(inputs_map.at(x_var_name));
  lite::npu::OpList::Global().add(pool_node);

  int mode = 0;
Y
Yan Chunwei 已提交
42 43
  auto pooling_type = op_info->GetAttr<std::string>("pooling_type");
  if (pooling_type == "max") {
Z
zhupengyang 已提交
44
    mode = 0;
Y
Yan Chunwei 已提交
45
  } else if (pooling_type == "avg") {
Z
zhupengyang 已提交
46
    mode = 1;
Y
Yan Chunwei 已提交
47
    CHECK(op_info->GetAttr<bool>("exclusive"))
48
        << "[NPU] exclusive must be true in HiAI DDK";
Y
Yan Chunwei 已提交
49
  } else {
50
    LOG(FATAL) << "[NPU] Unsupported pooling type: " << pooling_type;
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");
Z
zhupengyang 已提交
70 71
  auto window = ge::AttrValue::LIST_INT(ksize.begin(), ksize.end());
  pool_node->set_attr_window(window);
Y
Yan Chunwei 已提交
72

Z
zhupengyang 已提交
73 74 75 76 77 78 79 80 81 82 83 84
  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)
      << "Paddings size should be the same or twice as the inputs size.";
  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");
Z
zhupengyang 已提交
87 88 89 90 91 92 93 94 95 96 97
  operators::UpdatePadding(&paddings,
                           global_pooling,
                           adaptive,
                           padding_algorithm,
                           x->dims(),
                           strides,
                           ksize);
  auto npu_pad = ge::AttrValue::LIST_INT{
      paddings[0], paddings[1], paddings[2], paddings[3]};
  pool_node->set_attr_pad(npu_pad);

Y
Yan Chunwei 已提交
98
  auto npu_stride = ge::AttrValue::LIST_INT(strides.begin(), strides.end());
Z
zhupengyang 已提交
99 100 101
  pool_node->set_attr_stride(npu_stride);

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

  node_map_type outputs_map;
109
  outputs_map[op_info->Output("Out").front()] = pool_node;
Y
Yan Chunwei 已提交
110 111 112
  return outputs_map;
}

Z
zhupengyang 已提交
113
}  // namespace bridges
Y
Yan Chunwei 已提交
114
}  // namespace npu
Z
zhupengyang 已提交
115
}  // namespace kernels
Y
Yan Chunwei 已提交
116 117 118
}  // namespace lite
}  // namespace paddle

Z
zhupengyang 已提交
119
REGISTER_NPU_BRIDGE(pool2d, paddle::lite::kernels::npu::bridges::PoolConverter);