transpose_op.cpp 1.9 KB
Newer Older
E
eclipsess 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 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. */

L
liuruilong 已提交
15 16
#ifdef TRANSPOSE_OP

E
eclipsess 已提交
17
#include <vector>
L
liuruilong 已提交
18 19 20

#include "common/enforce.h"
#include "operators/transpose_op.h"
E
eclipsess 已提交
21 22 23 24 25
namespace paddle_mobile {
namespace operators {

template <typename Dtype, typename T>
void TransposeOp<Dtype, T>::InferShape() const {
L
liuruilong 已提交
26 27
  auto input_x_dims = this->param_.InputX()->dims();
  auto axis = this->param_.Axis();
E
eclipsess 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

  size_t x_dims_size = input_x_dims.size();
  size_t axis_size = axis.size();

  PADDLE_MOBILE_ENFORCE((x_dims_size == axis_size),
                        "input_dims must "
                        "be equal to the axis_size. ")

  std::vector<int> count(axis_size, 0);
  for (size_t i = 0; i < axis_size; i++) {
    PADDLE_MOBILE_ENFORCE(
        axis[i] < static_cast<int>(axis_size) && ++count[axis[i]] == 1,
        "Each element of Attribute axis should be a unique value "
        "range from 0 to (dims - 1), "
        "where the dims is the axis's size");
  }
  framework::DDim out_dims(input_x_dims);
  for (size_t i = 0; i < axis_size; i++) {
    out_dims[i] = input_x_dims[axis[i]];
  }
L
liuruilong 已提交
48
  this->param_.Out()->Resize(out_dims);
E
eclipsess 已提交
49
}
L
liuruilong 已提交
50

E
eclipsess 已提交
51 52 53 54
}  // namespace operators
}  // namespace paddle_mobile

namespace ops = paddle_mobile::operators;
L
for  
liuruilong 已提交
55 56 57 58 59 60 61 62
#ifdef PADDLE_MOBILE_CPU
REGISTER_OPERATOR_CPU(transpose, ops::TransposeOp);
#endif
#ifdef PADDLE_MOBILE_MALI_GPU
#endif
#ifdef PADDLE_MOBILE_FPGA
#endif

L
liuruilong 已提交
63
#endif