reshape_op.cc 4.5 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#include "lite/operators/reshape_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 ReshapeConverter(void* ctx, OpLite* op) {
  CHECK(ctx != nullptr);
  CHECK(op != nullptr);
  auto graph = static_cast<Graph*>(ctx);
  auto op_info = op->op_info();
Y
Yan Chunwei 已提交
30
  auto op_type = op_info->Type();
31 32
  auto scope = op->scope();
  VLOG(3) << "[NPU] Converting " + op_type + "...";
Y
Yan Chunwei 已提交
33

34
  // Get input, output and op attributes
Y
Yan Chunwei 已提交
35
  auto x_var_name = op_info->Input("X").front();
36 37
  auto out_var_name = op_info->Output("Out").front();
  auto x = scope->FindVar(x_var_name)->GetMutable<Tensor>();
Y
Yan Chunwei 已提交
38 39
  auto x_dims = x->dims();

40 41 42
  // Create reshape node and set input node from inputs_map
  auto reshape_node = graph->AddNode<ge::op::Reshape>(out_var_name);
  reshape_node->set_input_tensor(*graph->GetNode(x_var_name));
Y
Yan Chunwei 已提交
43

44 45 46 47 48
  // Read shape from "ShapeTensor"(input), or "Shape"(input), or "shape"(attr)
  if (HasInputArg(op_info, scope, "ShapeTensor")) {
    LOG(WARNING) << "[NPU] not support \"Shape\" from more than one Tensor.";
    return FAILED;
  } else if (HasInputArg(op_info, scope, "Shape")) {
Y
Yan Chunwei 已提交
49
    auto actual_shape_var_name = op_info->Input("Shape").front();
50
    if (!graph->HasNode(actual_shape_var_name)) {
Y
Yan Chunwei 已提交
51
      auto actual_shape =
52
          scope->FindVar(actual_shape_var_name)->GetMutable<Tensor>();
Y
Yan Chunwei 已提交
53 54 55 56 57
      auto actual_shape_dims = actual_shape->dims();
      auto actual_shape_data = actual_shape->mutable_data<int>();
      auto shape =
          std::vector<int>(actual_shape_data,
                           actual_shape_data + actual_shape_dims.production());
58
      auto out_dims = lite::operators::ValidateShape(shape, x_dims);
Y
Yan Chunwei 已提交
59 60
      auto out_shape = out_dims.Vectorize();
      if (out_shape.size() > 4) {
61 62 63
        LOG(WARNING) << "[NPU] HiAI DDK only supports less than 4 dimensions, "
                        "but Shape has "
                     << out_shape.size();
Y
Yan Chunwei 已提交
64 65
      }
      auto actual_shape_const_node =
66 67
          graph->AddNode(actual_shape_var_name,
                         std::vector<int>(out_shape.begin(), out_shape.end()));
Y
Yan Chunwei 已提交
68 69
      reshape_node->set_input_w(*actual_shape_const_node);
    } else {
70
      reshape_node->set_input_w(*graph->GetNode(actual_shape_var_name));
Y
Yan Chunwei 已提交
71 72 73
    }
  } else {
    auto shape = op_info->GetAttr<std::vector<int>>("shape");
74
    auto out_dims = lite::operators::ValidateShape(shape, x_dims);
Y
Yan Chunwei 已提交
75 76
    auto out_shape = out_dims.Vectorize();
    if (out_shape.size() > 4) {
77 78 79
      LOG(WARNING) << "[NPU] HiAI DDK only supports less than 4 dimensions, "
                      "but shape has "
                   << out_shape.size();
Y
Yan Chunwei 已提交
80 81 82 83 84 85
    }
    reshape_node->set_attr_shape(
        ge::AttrValue::LIST_INT(out_shape.begin(), out_shape.end()));
  }

  if (op_type == "reshape2") {
86
    // Append an extra reshape node to calc XShape
Y
Yan Chunwei 已提交
87 88 89 90 91
    std::vector<int64_t> xshape_dims(x_dims.size() + 1, 1);
    for (size_t i = 0; i < x_dims.size(); i++) {
      xshape_dims[i + 1] = x_dims[i];
    }
    if (xshape_dims.size() > 4) {
92 93 94
      LOG(WARNING) << "[NPU] HiAI DDK only supports less than 4 dimensions, "
                      "but XShape has "
                   << xshape_dims.size();
Y
Yan Chunwei 已提交
95
    }
96 97 98
    auto xshape_var_name = op_info->Output("XShape").front();
    auto xshape_node = graph->AddNode<ge::op::Reshape>(xshape_var_name);
    xshape_node->set_input_tensor(*graph->GetNode(x_var_name));
Y
Yan Chunwei 已提交
99 100 101
    xshape_node->set_attr_shape(
        ge::AttrValue::LIST_INT(xshape_dims.begin(), xshape_dims.end()));
  }
102
  return REBUILD_WHEN_SHAPE_CHANGED;
Y
Yan Chunwei 已提交
103 104 105
}

}  // namespace npu
106
}  // namespace subgraph
Y
Yan Chunwei 已提交
107 108 109
}  // namespace lite
}  // namespace paddle

110 111 112 113 114 115
REGISTER_SUBGRAPH_BRIDGE(NPU,
                         reshape,
                         paddle::lite::subgraph::npu::ReshapeConverter);
REGISTER_SUBGRAPH_BRIDGE(NPU,
                         reshape2,
                         paddle::lite::subgraph::npu::ReshapeConverter);