concat_compute.h 2.5 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
// 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.
#pragma once

#include <Eigen/Core>
#include <vector>
#include "lite/core/kernel.h"
#include "lite/core/op_registry.h"
#include "lite/core/types.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace x86 {

27 28 29 30 31 32 33 34
inline int count(int start_axis, int end_axis, const lite::DDim& dim) {
  int count = 1;
  for (int i = start_axis; i < end_axis; ++i) {
    count *= dim[i];
  }
  return count;
}

Y
Yan Chunwei 已提交
35 36 37 38 39 40 41
template <typename T>
class ConcatCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
 public:
  using param_t = operators::ConcatParam;

  void Run() override {
    auto& param = *param_.get_mutable<param_t>();
42 43 44 45 46
    if (param.x.size() == 1) {
      param.output->ShareDataWith(*param.x[0]);
      return;
    }

Y
Yan Chunwei 已提交
47
    int64_t axis = static_cast<int64_t>(param.axis);
48 49 50 51 52
    auto* axis_tensor = param.axis_tensor;
    if (axis_tensor != nullptr) {
      auto* axis_tensor_data = axis_tensor->data<int>();
      axis = static_cast<int64_t>(axis_tensor_data[0]);
    }
Y
Yan Chunwei 已提交
53

54 55 56 57
    const auto& x_dims = param.x[0]->dims();
    auto* out = param.output;
    T* output_data = param.output->template mutable_data<T>();

58 59 60 61 62
    int offset_concat_axis = 0;
    int num_concat = count(0, axis, x_dims);
    int concat_input_size = count(axis + 1, x_dims.size(), x_dims);
    const int top_concat_axis = out->dims()[axis];
    for (size_t i = 0; i < param.x.size(); ++i) {
63
      const T* bottom_data = param.x[i]->data<T>();
64 65 66 67 68 69 70
      const int64_t bottom_concat_axis = param.x[i]->dims()[axis];
      for (int n = 0; n < num_concat; ++n) {
        std::memcpy(
            output_data +
                (n * top_concat_axis + offset_concat_axis) * concat_input_size,
            bottom_data + n * bottom_concat_axis * concat_input_size,
            (bottom_concat_axis * concat_input_size) * sizeof(T));
Y
Yan Chunwei 已提交
71
      }
72
      offset_concat_axis += bottom_concat_axis;
Y
Yan Chunwei 已提交
73 74 75 76 77 78 79 80 81
    }
  }
  virtual ~ConcatCompute() = default;
};

}  // namespace x86
}  // namespace kernels
}  // namespace lite
}  // namespace paddle