split_kernel.h 4.0 KB
Newer Older
C
chentianyu03 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2022 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

17
#include "paddle/phi/common/int_array.h"
18
#include "paddle/phi/common/scalar.h"
19
#include "paddle/phi/core/dense_tensor.h"
20
#include "paddle/phi/infermeta/unary.h"
C
chentianyu03 已提交
21

22
namespace phi {
C
chentianyu03 已提交
23 24 25 26

template <typename T, typename Context>
void SplitKernel(const Context& dev_ctx,
                 const DenseTensor& x,
C
Charles-hit 已提交
27
                 const IntArray& sections,
C
chentianyu03 已提交
28 29 30
                 const Scalar& axis,
                 std::vector<DenseTensor*> out);

C
Charles-hit 已提交
31 32 33 34 35 36 37
template <typename T, typename Context>
void SplitWithNumKernel(const Context& dev_ctx,
                        const DenseTensor& x,
                        int num,
                        const Scalar& axis,
                        std::vector<DenseTensor*> out);

W
wanghuancoder 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51
template <typename Context>
void SplitStridedKernel(const Context& dev_ctx,
                        const DenseTensor& x,
                        const IntArray& sections,
                        const Scalar& axis,
                        std::vector<DenseTensor*> out);

template <typename Context>
void SplitWithNumStridedKernel(const Context& dev_ctx,
                               const DenseTensor& x,
                               int num,
                               const Scalar& axis,
                               std::vector<DenseTensor*> out);

C
chentianyu03 已提交
52
template <typename T, typename Context>
53 54 55 56 57 58
void Split(const Context& dev_ctx,
           const DenseTensor& x,
           const IntArray& sections,
           const Scalar& axis,
           std::vector<DenseTensor>* result) {
  size_t out_number = sections.GetData().size();
C
Charles-hit 已提交
59 60 61 62 63

  std::vector<MetaTensor> out_meta;
  std::vector<MetaTensor*> out_meta_ptr;
  out_meta.reserve(out_number);
  out_meta_ptr.reserve(out_number);
64
  result->resize(out_number);
C
Charles-hit 已提交
65 66

  for (size_t i = 0; i < out_number; ++i) {
67
    out_meta.emplace_back(&result->at(i));
C
Charles-hit 已提交
68
    out_meta_ptr.push_back(&out_meta.back());
C
chentianyu03 已提交
69
  }
C
Charles-hit 已提交
70 71 72 73
  SplitInferMeta(x, sections, axis, out_meta_ptr);
  std::vector<DenseTensor*> outs;
  outs.reserve(out_meta.size());
  for (size_t i = 0; i < out_meta.size(); ++i) {
74
    outs.push_back(&result->at(i));
C
Charles-hit 已提交
75 76 77
  }

  SplitKernel<T, Context>(dev_ctx, x, sections, axis, outs);
78 79 80 81 82 83 84 85 86 87 88 89
}

template <typename T, typename Context>
std::vector<DenseTensor> Split(const Context& dev_ctx,
                               const DenseTensor& x,
                               const IntArray& sections,
                               const Scalar& axis) {
  size_t out_number = sections.GetData().size();
  std::vector<DenseTensor> result(out_number);

  Split(dev_ctx, x, sections, axis, &result);

C
Charles-hit 已提交
90 91 92 93 94 95 96 97 98
  return result;
}

template <typename T, typename Context>
std::vector<DenseTensor> SplitWithNum(const Context& dev_ctx,
                                      const DenseTensor& x,
                                      int num,
                                      const Scalar& axis) {
  size_t out_number = num;
C
chentianyu03 已提交
99 100

  std::vector<MetaTensor> out_meta;
101
  std::vector<MetaTensor*> out_meta_ptr;
C
chentianyu03 已提交
102
  out_meta.reserve(out_number);
103
  out_meta_ptr.reserve(out_number);
C
Charles-hit 已提交
104
  std::vector<DenseTensor> result(out_number);
C
chentianyu03 已提交
105 106

  for (size_t i = 0; i < out_number; ++i) {
C
Charles-hit 已提交
107
    out_meta.emplace_back(&result[i]);
108
    out_meta_ptr.push_back(&out_meta.back());
C
chentianyu03 已提交
109
  }
C
Charles-hit 已提交
110
  SplitWithNumInferMeta(x, num, axis, out_meta_ptr);
C
chentianyu03 已提交
111 112 113 114 115 116 117

  std::vector<DenseTensor*> outs;
  outs.reserve(out_meta.size());
  for (size_t i = 0; i < out_meta.size(); ++i) {
    outs.push_back(&result[i]);
  }

C
Charles-hit 已提交
118
  SplitWithNumKernel<T, Context>(dev_ctx, x, num, axis, outs);
C
chentianyu03 已提交
119 120 121 122

  return result;
}

123
}  // namespace phi