pool_op.cc 4.6 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
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 45 46 47 48 49 50
  // Get input and output vars and op attributes
  auto x_name = op_info->Input("X").front();
  auto x_type = kernel->GetInputDeclType("X");
  CHECK(x_type->precision() == PRECISION(kFloat));
  CHECK(x_type->layout() == DATALAYOUT(kNCHW));
  auto x = scope->FindMutableTensor(x_name);
  auto x_dims = x->dims();
  auto out_name = op_info->Output("Out").front();
  auto out_type = kernel->GetOutputDeclType("Out");
  CHECK(out_type->precision() == PRECISION(kFloat));
  CHECK(out_type->layout() == DATALAYOUT(kNCHW));
  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
51 52 53
  std::shared_ptr<Node> x_node = nullptr;
  if (graph->Has(x_name)) {
    x_node = graph->Get(x_name);
54
  } else {
55
    x_node = graph->Add(x_name, *x);
56
  }
Z
zhupengyang 已提交
57

58
  // pool mode
Z
zhupengyang 已提交
59
  int mode = 0;
Y
Yan Chunwei 已提交
60
  if (pooling_type == "max") {
Z
zhupengyang 已提交
61
    mode = 0;
Y
Yan Chunwei 已提交
62
  } else if (pooling_type == "avg") {
Z
zhupengyang 已提交
63
    mode = 1;
Y
Yan Chunwei 已提交
64
    CHECK(op_info->GetAttr<bool>("exclusive"))
65
        << "[NPU] exclusive must be true in HiAI DDK";
Y
Yan Chunwei 已提交
66
  } else {
67 68
    LOG(WARNING) << "[NPU] Unsupported pooling type: " << pooling_type;
    return FAILED;
Y
Yan Chunwei 已提交
69
  }
Z
zhupengyang 已提交
70

71
  // pad mode
Z
zhupengyang 已提交
72 73 74 75 76 77 78 79 80 81
  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 已提交
82

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

105
  // ceil mode
Z
zhupengyang 已提交
106
  int ceil_mode = 0;
Y
Yan Chunwei 已提交
107
  if (op_info->HasAttr("ceil_mode")) {
Z
zhupengyang 已提交
108
    ceil_mode = op_info->GetAttr<bool>("ceil_mode") ? 1 : 0;
Y
Yan Chunwei 已提交
109
  }
110 111

  // Pooling node
112 113 114 115 116 117 118 119
  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()));
  pool_op->set_attr_pad(ge::AttrValue::LIST_INT{
120
      paddings[0], paddings[1], paddings[2], paddings[3]});
121
  pool_op->set_attr_stride(
122
      ge::AttrValue::LIST_INT(strides.begin(), strides.end()));
123 124
  pool_op->set_attr_ceil_mode(ceil_mode);
  // pool_op->set_attr_data_mode(data_mode);
125
  return REBUILD_WHEN_SHAPE_CHANGED;
Y
Yan Chunwei 已提交
126 127 128
}

}  // namespace npu
129
}  // namespace subgraph
Y
Yan Chunwei 已提交
130 131 132
}  // namespace lite
}  // namespace paddle

133 134
REGISTER_SUBGRAPH_BRIDGE(pool2d,
                         kNPU,
135
                         paddle::lite::subgraph::npu::PoolConverter);