transpose_op.cc 5.7 KB
Newer Older
Y
Yan Chunwei 已提交
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
// 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/transpose_op.h"
#include "lite/core/op_registry.h"

namespace paddle {
namespace lite {
namespace operators {

// Transpose
bool TransposeOp::CheckShape() const {
  CHECK_OR_FALSE(param_.x);
  CHECK_OR_FALSE(param_.output);
  auto x_dims = param_.x->dims();
  auto x_rank = x_dims.size();
  std::vector<int> axis = param_.axis;
  size_t axis_size = axis.size();
  // "The input tensor's rank(%d) should be equal to the axis's size(%d)",
  // x_rank, axis_size
  CHECK_OR_FALSE(x_rank == axis_size);

  std::vector<int> count(axis_size, 0);
  for (size_t i = 0; i < axis_size; i++) {
    // Each element of Attribute axis should be a unique value
    // range from 0 to (dims - 1),
    // where the dims is the axis's size
    CHECK_OR_FALSE(axis[i] < static_cast<int>(axis_size) &&
                   ++count[axis[i]] == 1);
  }
  return true;
}

45
bool TransposeOp::InferShapeImpl() const {
Y
Yan Chunwei 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  CHECK_OR_FALSE(param_.x);
  CHECK_OR_FALSE(param_.output);
  auto x_dims = param_.x->dims();
  auto x_rank = x_dims.size();
  std::vector<int> axis = param_.axis;
  size_t axis_size = axis.size();
  // "The input tensor's rank(%d) should be equal to the axis's size(%d)",
  // x_rank, axis_size
  CHECK_OR_FALSE(x_rank == axis_size);

  std::vector<int> count(axis_size, 0);
  for (size_t i = 0; i < axis_size; i++) {
    // Each element of Attribute axis should be a unique value
    // range from 0 to (dims - 1),
    // where the dims is the axis's size
    CHECK_OR_FALSE(axis[i] < static_cast<int>(axis_size) &&
                   ++count[axis[i]] == 1);
  }
  lite::DDim out_dims(x_dims);
  for (size_t i = 0; i < axis_size; i++) {
    out_dims[i] = x_dims[axis[i]];
  }
  param_.output->Resize(out_dims);
  return true;
}

bool TransposeOp::AttachImpl(const cpp::OpDesc &op_desc, lite::Scope *scope) {
  auto x = op_desc.Input("X").front();
  auto out = op_desc.Output("Out").front();

  CHECK(scope->FindVar(x));
  CHECK(scope->FindVar(out));
  param_.x = GetVar<lite::Tensor>(scope, x);
  param_.output = GetMutableVar<lite::Tensor>(scope, out);

  param_.axis = op_desc.GetAttr<std::vector<int>>("axis");
  if (op_desc.HasAttr("use_mkldnn")) {
    param_.use_mkldnn = op_desc.GetAttr<bool>("use_mkldnn");
  }
  if (op_desc.HasAttr("data_format")) {
    param_.data_format = op_desc.GetAttr<std::string>("data_format");
  }
  return true;
}

// Transpose2
bool Transpose2Op::CheckShape() const {
  CHECK_OR_FALSE(param_.x);
  CHECK_OR_FALSE(param_.output);
  auto x_dims = param_.x->dims();
  auto x_rank = x_dims.size();
  std::vector<int> axis = param_.axis;
  size_t axis_size = axis.size();
  // "The input tensor's rank(%d) should be equal to the axis's size(%d)",
  // x_rank, axis_size
  CHECK_OR_FALSE(x_rank == axis_size);

  std::vector<int> count(axis_size, 0);
  for (size_t i = 0; i < axis_size; i++) {
    // Each element of Attribute axis should be a unique value
    // range from 0 to (dims - 1),
    // where the dims is the axis's size
    CHECK_OR_FALSE(axis[i] < static_cast<int>(axis_size) &&
                   ++count[axis[i]] == 1);
  }
  return true;
}

114
bool Transpose2Op::InferShapeImpl() const {
Y
Yan Chunwei 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  CHECK_OR_FALSE(param_.x);
  CHECK_OR_FALSE(param_.output);
  auto x_dims = param_.x->dims();
  auto x_rank = x_dims.size();
  std::vector<int> axis = param_.axis;
  size_t axis_size = axis.size();
  // "The input tensor's rank(%d) should be equal to the axis's size(%d)",
  // x_rank, axis_size
  CHECK_OR_FALSE(x_rank == axis_size);

  std::vector<int> count(axis_size, 0);
  for (size_t i = 0; i < axis_size; i++) {
    // Each element of Attribute axis should be a unique value
    // range from 0 to (dims - 1),
    // where the dims is the axis's size
    CHECK_OR_FALSE(axis[i] < static_cast<int>(axis_size) &&
                   ++count[axis[i]] == 1);
  }
  lite::DDim out_dims(x_dims);
  for (size_t i = 0; i < axis_size; i++) {
    out_dims[i] = x_dims[axis[i]];
  }
  param_.output->Resize(out_dims);
138 139 140 141 142 143 144 145 146

  std::vector<DDim::value_type> xshape_dims(x_dims.size() + 1, 0);
  for (size_t i = 0; i < x_dims.size(); i++) {
    xshape_dims[i + 1] = x_dims[i];
  }
  param_.xshape->Resize(xshape_dims);
  auto xshape_lod = param_.xshape->mutable_lod();
  *xshape_lod = param_.x->lod();

Y
Yan Chunwei 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  return true;
}

bool Transpose2Op::AttachImpl(const cpp::OpDesc &op_desc, lite::Scope *scope) {
  auto x = op_desc.Input("X").front();
  auto out = op_desc.Output("Out").front();

  CHECK(scope->FindVar(x));
  CHECK(scope->FindVar(out));
  param_.x = GetVar<lite::Tensor>(scope, x);
  param_.output = GetMutableVar<lite::Tensor>(scope, out);

  param_.axis = op_desc.GetAttr<std::vector<int>>("axis");
  if (op_desc.HasAttr("use_mkldnn")) {
    param_.use_mkldnn = op_desc.GetAttr<bool>("use_mkldnn");
  }
  if (op_desc.HasAttr("data_format")) {
    param_.data_format = op_desc.GetAttr<std::string>("data_format");
  }
166 167 168 169
  if (op_desc.HasOutput("XShape")) {
    auto xshape_var = scope->FindVar(op_desc.Output("XShape").front());
    param_.xshape = xshape_var->GetMutable<lite::Tensor>();
  }
Y
Yan Chunwei 已提交
170 171 172 173 174 175 176 177 178
  return true;
}

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

REGISTER_LITE_OP(transpose, paddle::lite::operators::TransposeOp);
REGISTER_LITE_OP(transpose2, paddle::lite::operators::Transpose2Op);