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 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
  auto reshape_node = graph->Add<ge::op::Reshape>(
      out_name, x_node->precision(), x_node->layout());
54 55
  auto reshape_op = reshape_node->data<ge::op::Reshape>();
  reshape_op->set_input_tensor(*x_node->data());
Y
Yan Chunwei 已提交
56

57 58 59 60 61
  // 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")) {
62 63 64 65
    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));
66 67 68
    std::shared_ptr<Node> actual_shape_node = nullptr;
    if (graph->Has(actual_shape_name)) {
      actual_shape_node = graph->Get(actual_shape_name);
69 70
    } else {
      auto actual_shape = scope->FindMutableTensor(actual_shape_name);
Y
Yan Chunwei 已提交
71 72 73 74 75
      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());
76
      auto out_shape = lite::operators::ValidateShape(shape, x_dims);
Y
Yan Chunwei 已提交
77
      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_shape = lite::operators::ValidateShape(shape, x_dims);
Y
Yan Chunwei 已提交
91
    if (out_shape.size() > 4) {
92 93 94
      LOG(WARNING) << "[NPU] HiAI DDK only supports less than 4 dimensions, "
                      "but shape has "
                   << out_shape.size();
95
      return FAILED;
Y
Yan Chunwei 已提交
96
    }
97
    reshape_op->set_attr_shape(
Y
Yan Chunwei 已提交
98 99 100
        ge::AttrValue::LIST_INT(out_shape.begin(), out_shape.end()));
  }

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

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

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