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

39
  auto out_name = op_info->Output("Out").front();
Y
Yan Chunwei 已提交
40

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

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

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

100
  return REBUILD_WHEN_SHAPE_CHANGED;
Y
Yan Chunwei 已提交
101 102 103
}

}  // namespace npu
104
}  // namespace subgraph
Y
Yan Chunwei 已提交
105 106 107
}  // namespace lite
}  // namespace paddle

108 109
REGISTER_SUBGRAPH_BRIDGE(reshape,
                         kNPU,
110
                         paddle::lite::subgraph::npu::ReshapeConverter);
111 112
REGISTER_SUBGRAPH_BRIDGE(reshape2,
                         kNPU,
113
                         paddle::lite::subgraph::npu::ReshapeConverter);