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

L
liuruilong 已提交
15 16
#ifdef CONCAT_OP

E
eclipsess 已提交
17 18 19 20 21 22 23
#include "concat_op.h"

namespace paddle_mobile {
namespace operators {

template <typename Dtype, typename T>
void ConcatOp<Dtype, T>::InferShape() const {
L
liuruilong 已提交
24
  auto inputs = this->param_.Inputs();
25
  const size_t n = inputs.size();
E
eclipsess 已提交
26

27 28 29 30 31
  std::vector<DDim> inputs_dims;
  inputs_dims.reserve(n);
  for (int i = 0; i < n; i++) {
    inputs_dims.push_back(inputs[i]->dims());
  }
E
eclipsess 已提交
32

L
liuruilong 已提交
33
  auto axis = static_cast<size_t>(this->param_.Axis());
E
eclipsess 已提交
34

35 36 37 38
  if (n == 1) {
    DLOG << "Warning: concat op have only one input, "
            "may waste memory";
  }
E
eclipsess 已提交
39

40 41 42 43 44 45 46 47 48 49
  /// add all dim[axis] and check other dims if equal.
  auto out_dims = inputs_dims[0];
  int in_zero_dims_size = out_dims.size();
  for (size_t i = 1; i < n; i++) {
    for (size_t j = 0; j < in_zero_dims_size; j++) {
      if (j == axis) {
        out_dims[axis] += inputs_dims[i][j];
      } else {
        assert(out_dims[j] == inputs_dims[i][j]);
      }
E
eclipsess 已提交
50
    }
51
  }
E
eclipsess 已提交
52

53 54 55
  if (out_dims[axis] < 0) {
    out_dims[axis] = -1;
  }
E
eclipsess 已提交
56

L
liuruilong 已提交
57
  this->param_.Out()->Resize(out_dims);
E
eclipsess 已提交
58 59 60
}
template class ConcatOp<CPU, float>;

朔-望's avatar
朔-望 已提交
61 62
}  // namespace operators
}  // namespace paddle_mobile
E
eclipsess 已提交
63 64

namespace ops = paddle_mobile::operators;
L
for  
liuruilong 已提交
65 66 67 68 69 70 71 72
#ifdef PADDLE_MOBILE_CPU
USE_OP_CPU(concat);
REGISTER_OPERATOR_CPU(concat, ops::ConcatOp);
#endif
#ifdef PADDLE_MOBILE_MALI_GPU
#endif
#ifdef PADDLE_MOBILE_FPGA
#endif
L
liuruilong 已提交
73 74

#endif