unsqueeze_op.cc 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
// 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/unsqueeze_op.h"
#include "lite/core/op_registry.h"
namespace paddle {
namespace lite {
namespace operators {

static DDim GetOutputShape(const std::vector<int> &unsqz_dims,
                           const DDim &in_dims) {
  int output_size = in_dims.size() + static_cast<int>(unsqz_dims.size());
  int cur_output_size = in_dims.size();
  std::vector<int64_t> output_shape(output_size, 0);

  // Validate Check: rank range.
  CHECK_LE(output_size, 6) << "The output tensor's rank should be less than 6.";

  for (int axis : unsqz_dims) {
    int cur = axis < 0 ? axis + cur_output_size + 1 : axis;
    // Validate Check: the axis bound
    CHECK((cur >= 0) && (cur <= cur_output_size))
        << "The unsqueeze dims must be within range of current rank.";
    // Move old axis, and insert new axis
    for (int i = cur_output_size; i >= cur; --i) {
      if (output_shape[i] == 1) {
        // Move axis
        output_shape[i + 1] = 1;
        output_shape[i] = 0;
      }
    }

    output_shape[cur] = 1;
    // Add the output size.
    cur_output_size++;
  }

  // Make output shape
  for (int in_idx = 0, out_idx = 0; out_idx < output_size; ++out_idx) {
    if (output_shape[out_idx] == 0) {
      output_shape[out_idx] = in_dims[in_idx++];
    }
  }

  return DDim(output_shape);
}

bool UnsqueezeOp::CheckShape() const {
  CHECK_OR_FALSE(param_.X);
  CHECK_OR_FALSE(param_.Out);
  return true;
}

bool UnsqueezeOp::InferShape() const {
66 67 68
  std::vector<int> final_axes;
  auto axes = param_.axes;
  auto *axes_tensor = param_.axes_tensor;
69
  auto axes_tensor_vct = param_.axes_tensor_vct;
70 71 72 73 74 75 76 77 78

  if (!axes.empty()) {
    final_axes = axes;
  } else if (axes_tensor != nullptr) {
    auto *axes_tensor_data = axes_tensor->data<int>();
    final_axes = std::vector<int>(axes_tensor_data,
                                  axes_tensor_data + axes_tensor->numel());
  } else if (!axes_tensor_vct.empty()) {
    for (int i = 0; i < axes_tensor_vct.size(); i++) {
79
      final_axes.push_back(axes_tensor_vct[i]->data<int>()[0]);
80 81 82 83 84
    }
  } else {
    LOG(FATAL) << "Input axis error";
  }

85
  DDim in_dims = param_.X->dims();
86
  DDim out_dim = GetOutputShape(final_axes, in_dims);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  param_.Out->Resize(out_dim);
  return true;
}

bool UnsqueezeOp::AttachImpl(const cpp::OpDesc &opdesc, lite::Scope *scope) {
  auto x_var = scope->FindVar(opdesc.Input("X").front());
  auto output_var = scope->FindVar(opdesc.Output("Out").front());
  CHECK(x_var);
  CHECK(output_var);
  param_.X = const_cast<lite::Tensor *>(&(x_var->Get<lite::Tensor>()));
  param_.Out = output_var->GetMutable<lite::Tensor>();

  if (opdesc.HasAttr("axes")) {
    param_.axes = opdesc.GetAttr<std::vector<int>>("axes");
  }
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

  if (opdesc.HasInput("AxesTensor") && opdesc.Input("AxesTensor").size() > 0) {
    auto var = scope->FindVar(opdesc.Input("AxesTensor").front());
    if (var != nullptr) {
      param_.axes_tensor = var->GetMutable<lite::Tensor>();
      VLOG(5) << "load AxesTensor";
    }
  }

  if (opdesc.HasInput("AxesTensorList") &&
      opdesc.Input("AxesTensorList").size() > 0) {
    auto args = opdesc.Input("AxesTensorList");
    for (auto arg : args) {
      auto *var = scope->FindVar(arg);
      if (var != nullptr) {
        param_.axes_tensor_vct.push_back(var->GetMutable<lite::Tensor>());
      }
    }
  }
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  CHECK(param_.X) << "Input(X) of UnsqueezeOp should not be null.";
  CHECK(param_.Out) << "Output(Out) of UnsqueezeOp should not be null.";
  return true;
}

bool Unsqueeze2Op::CheckShape() const {
  UnsqueezeOp::CheckShape();
  CHECK_OR_FALSE(param_.XShape);
  return true;
}

bool Unsqueeze2Op::InferShape() const {
  UnsqueezeOp::InferShape();
  auto x_dims = param_.X->dims();
  std::vector<DDim::value_type> xshape_dims(x_dims.size() + 1, 1);
  for (size_t i = 0; i < x_dims.size(); i++) {
    xshape_dims[i + 1] = x_dims[i];
  }
  param_.XShape->Resize(DDim(xshape_dims));
  return true;
}

bool Unsqueeze2Op::AttachImpl(const cpp::OpDesc &opdesc, lite::Scope *scope) {
  UnsqueezeOp::AttachImpl(opdesc, scope);
  auto xshape_var = scope->FindVar(opdesc.Output("XShape").front());
  CHECK(xshape_var);
  param_.XShape = xshape_var->GetMutable<lite::Tensor>();
  CHECK(param_.XShape) << "Output(XShape) of Unsqueeze2Op should not be null.";
  return true;
}

}  // namespace operators
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_OP(unsqueeze, paddle::lite::operators::UnsqueezeOp);
REGISTER_LITE_OP(unsqueeze2, paddle::lite::operators::Unsqueeze2Op);