reshape_op.cc 4.3 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
int ReshapeConverter(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();
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 35 36 37
  // Get input and output vars and op attributes
  auto x_name = op_info->Input("X").front();
  auto x_type = kernel->GetInputDeclType("X");
  auto x = scope->FindMutableTensor(x_name);
Y
Yan Chunwei 已提交
38
  auto x_dims = x->dims();
39

40 41
  auto out_name = op_info->Output("Out").front();
  auto out_type = kernel->GetOutputDeclType("Out");
Y
Yan Chunwei 已提交
42

43
  // X node
44 45 46
  std::shared_ptr<Node> x_node = nullptr;
  if (graph->Has(x_name)) {
    x_node = graph->Get(x_name);
47
  } else {
48
    x_node = graph->Add(x_name, *x);
49 50 51
  }

  // Reshape node
52 53 54
  auto reshape_node = graph->Add<ge::op::Reshape>(out_name);
  auto reshape_op = reshape_node->data<ge::op::Reshape>();
  reshape_op->set_input_tensor(*x_node->data());
Y
Yan Chunwei 已提交
55

56 57 58 59 60
  // 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")) {
61 62 63 64
    auto actual_shape_name = op_info->Input("Shape").front();
    // auto actual_shape_type = kernel->GetInputDeclType("Shape");
    // CHECK(actual_shape_type->precision() == PRECISION(kInt32));
    // CHECK(actual_shape_type->layout() == DATALAYOUT(kNCHW));
65 66 67
    std::shared_ptr<Node> actual_shape_node = nullptr;
    if (graph->Has(actual_shape_name)) {
      actual_shape_node = graph->Get(actual_shape_name);
68 69
    } else {
      auto actual_shape = scope->FindMutableTensor(actual_shape_name);
Y
Yan Chunwei 已提交
70 71 72 73 74
      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());
75
      auto out_dims = lite::operators::ValidateShape(shape, x_dims);
Y
Yan Chunwei 已提交
76 77
      auto out_shape = out_dims.Vectorize();
      if (out_shape.size() > 4) {
78 79 80
        LOG(WARNING) << "[NPU] HiAI DDK only supports less than 4 dimensions, "
                        "but Shape has "
                     << out_shape.size();
81
        return FAILED;
Y
Yan Chunwei 已提交
82
      }
83 84 85
      actual_shape_node =
          graph->Add(actual_shape_name,
                     std::vector<int>(out_shape.begin(), out_shape.end()));
Y
Yan Chunwei 已提交
86
    }
87
    reshape_op->set_input_w(*actual_shape_node->data());
Y
Yan Chunwei 已提交
88 89
  } else {
    auto shape = op_info->GetAttr<std::vector<int>>("shape");
90
    auto out_dims = lite::operators::ValidateShape(shape, x_dims);
Y
Yan Chunwei 已提交
91 92
    auto out_shape = out_dims.Vectorize();
    if (out_shape.size() > 4) {
93 94 95
      LOG(WARNING) << "[NPU] HiAI DDK only supports less than 4 dimensions, "
                      "but shape has "
                   << out_shape.size();
96
      return FAILED;
Y
Yan Chunwei 已提交
97
    }
98
    reshape_op->set_attr_shape(
Y
Yan Chunwei 已提交
99 100 101
        ge::AttrValue::LIST_INT(out_shape.begin(), out_shape.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
REGISTER_SUBGRAPH_BRIDGE(reshape,
                         kNPU,
112
                         paddle::lite::subgraph::npu::ReshapeConverter);
113 114
REGISTER_SUBGRAPH_BRIDGE(reshape2,
                         kNPU,
115
                         paddle::lite::subgraph::npu::ReshapeConverter);