concat.cc 2.0 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

15
#include "lite/backends/arm/math/concat.h"
Y
Yan Chunwei 已提交
16 17 18
#include <algorithm>
#include <limits>
#include <memory>
19
#include "lite/backends/arm/math/funcs.h"
Y
Yan Chunwei 已提交
20 21 22 23 24 25 26 27 28

namespace paddle {
namespace lite {
namespace arm {
namespace math {

void concat_func(const std::vector<lite::Tensor *> &input,
                 const int axis,
                 lite::Tensor *output) {
29 30
  int64_t concat_input_size = 1;
  int64_t num_cancats = 1;
Y
Yan Chunwei 已提交
31
  auto dim_0 = input[0]->dims();
32 33 34
  size_t num = input.size();
  for (int i = axis + 1; i < dim_0.size(); i++) {
    concat_input_size *= dim_0[i];
Y
Yan Chunwei 已提交
35
  }
36 37
  for (int i = 0; i < axis; i++) {
    num_cancats *= dim_0[i];
Y
Yan Chunwei 已提交
38
  }
39 40 41 42 43 44 45 46 47 48 49 50 51 52
  float *dst_ptr = output->mutable_data<float>();
  const int out_concat_axis = output->dims()[axis];
  int64_t offset_concat_axis = 0;
  int64_t out_sum = out_concat_axis * concat_input_size;
  for (int n = 0; n < num; n++) {
    auto dims = input[n]->dims();
    const float *src_ptr = input[n]->data<float>();
    int64_t in_concat_axis = dims[axis];
    float *dout_ptr = dst_ptr + offset_concat_axis * concat_input_size;
    int64_t in_sum = in_concat_axis * concat_input_size;
    for (int i = 0; i < num_cancats; i++) {
      std::memcpy(dout_ptr, src_ptr, sizeof(float) * in_sum);
      dout_ptr += out_sum;
      src_ptr += in_sum;
Y
Yan Chunwei 已提交
53
    }
54
    offset_concat_axis += in_concat_axis;
Y
Yan Chunwei 已提交
55 56 57 58 59 60 61
  }
}

}  // namespace math
}  // namespace arm
}  // namespace lite
}  // namespace paddle