pad2d_op.cc 3.0 KB
Newer Older
Z
zhupengyang 已提交
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.

15
#include "lite/backends/npu/builder.h"
Z
zhupengyang 已提交
16
#include "lite/kernels/npu/bridges/registry.h"
Z
zhupengyang 已提交
17 18 19

namespace paddle {
namespace lite {
Z
zhupengyang 已提交
20
namespace kernels {
Z
zhupengyang 已提交
21
namespace npu {
Z
zhupengyang 已提交
22
namespace bridges {
Z
zhupengyang 已提交
23 24 25 26 27 28

node_map_type Pad2dConverter(const std::shared_ptr<lite::OpLite> pad2d_op,
                             const node_map_type& inputs_map) {
  auto scope = pad2d_op->scope();
  auto op_info = pad2d_op->op_info();
  auto op_type = op_info->Type();
29
  auto unique_op_type = lite::npu::UniqueName(op_type);
30
  LOG(INFO) << "[NPU] Converting " + op_type + "...";
Z
zhupengyang 已提交
31 32 33 34 35

  std::shared_ptr<ge::op::Pad> pad2d_node =
      std::make_shared<ge::op::Pad>(unique_op_type);
  auto x_var_name = op_info->Input("X").front();
  pad2d_node->set_input_x(*inputs_map.at(x_var_name));
36 37
  lite::npu::OpList::Global().add(inputs_map.at(x_var_name));
  lite::npu::OpList::Global().add(pad2d_node);
Z
zhupengyang 已提交
38 39 40 41 42

  auto mode = op_info->GetAttr<std::string>("mode");
  if (mode == "constant") {
    pad2d_node->set_attr_mode(0);
  } else if (mode == "reflect") {
43
    LOG(FATAL) << "[NPU] pad mode " << mode << " isn't supported in HiAI DDK";
Z
zhupengyang 已提交
44 45
    pad2d_node->set_attr_mode(1);
  } else {
46
    LOG(FATAL) << "[NPU] pad mode " << mode << " isn't supported in HiAI DDK";
Z
zhupengyang 已提交
47 48 49 50 51 52 53 54 55
  }

  auto x_dims = scope->FindTensor(x_var_name)->dims();
  auto padding = op_info->GetAttr<std::vector<int>>("paddings");
  CHECK_EQ(padding.size(), 4);
  int xds = x_dims.size();
  padding.insert(padding.begin(), xds * 2 - 4, 0);
  auto npu_padding =
      std::make_shared<ge::op::Const>(unique_op_type + "/padding");
56 57
  npu_padding->set_attr_value(
      lite::npu::CreateTensorAndFillData<int>(padding, {xds, 2}));
Z
zhupengyang 已提交
58
  pad2d_node->set_input_padding(*npu_padding);
59
  lite::npu::OpList::Global().add(npu_padding);
Z
zhupengyang 已提交
60 61 62 63 64

  if (mode == "constant") {
    auto pad_value = op_info->GetAttr<float>("pad_value");
    auto npu_pad_value =
        std::make_shared<ge::op::Const>(unique_op_type + "/pad_value");
65 66
    npu_pad_value->set_attr_value(
        lite::npu::CreateTensorAndFillData<float>({pad_value}));
Z
zhupengyang 已提交
67
    pad2d_node->set_input_constant_values(*npu_pad_value);
68
    lite::npu::OpList::Global().add(npu_pad_value);
Z
zhupengyang 已提交
69 70 71 72 73 74 75 76 77

    pad2d_node->set_attr_T(0);  // type of pad_value:  0:float  3:int32
  }

  node_map_type outputs_map;
  outputs_map[op_info->Output("Out").front()] = pad2d_node;
  return outputs_map;
}

Z
zhupengyang 已提交
78
}  // namespace bridges
Z
zhupengyang 已提交
79
}  // namespace npu
Z
zhupengyang 已提交
80
}  // namespace kernels
Z
zhupengyang 已提交
81 82 83
}  // namespace lite
}  // namespace paddle

Z
zhupengyang 已提交
84
REGISTER_NPU_BRIDGE(pad2d, paddle::lite::kernels::npu::bridges::Pad2dConverter);