slice.h 4.4 KB
Newer Older
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
// 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

#include "paddle/phi/core/ddim.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
#include "paddle/phi/kernels/funcs/eigen/eigen_function.h"

// TODO(paddle-dev): Remove this file when we can call related Kernel directly

namespace phi {
namespace funcs {

template <typename Context, typename T, size_t D>
void EigenSliceWrapper(const Context& dev_ctx,
                       const DenseTensor* in,
                       const std::vector<int>& start,
                       const std::vector<int>& end,
                       DenseTensor* out) {
  // Slice by call Eigen Tensor Function `.slice()`
  size_t rank = in->dims().size();
  PADDLE_ENFORCE_EQ(start.size(),
                    rank,
                    errors::InvalidArgument(
                        "EigenSliceWrapper function start "
                        "argument must have the same length as input rank."));
  PADDLE_ENFORCE_EQ(end.size(),
                    rank,
                    errors::InvalidArgument(
                        "EigenSliceWrapper function end "
                        "argument must have the same length as input rank."));
  auto eigen_place_ptr = dev_ctx.eigen_device();
  auto eigen_place = *eigen_place_ptr;
  auto out_t = phi::EigenTensor<T, D>::From(*out, out->dims());
  auto in_t = phi::EigenTensor<T, D>::From(*in, in->dims());
  Eigen::DSizes<int, D> offsets_32bit, extents_32bit;
  for (size_t i = 0; i < D; i++) {
    offsets_32bit[i] = start[i];
    extents_32bit[i] = end[i];
  }
  EigenSlice<std::decay_t<decltype(eigen_place)>, T, D>::Eval(
      eigen_place,
      phi::To32BitIndex(out_t),
      phi::To32BitIndex(in_t),
      offsets_32bit,
      extents_32bit);
}

#define SLICE_RANK_CASE(N)                                                \
  case N: {                                                               \
    EigenSliceWrapper<Context, T, N>(dev_ctx, &x, offset, extends, &ret); \
    break;                                                                \
  }

template <typename T, typename Context>
DenseTensor Slice(const Context& dev_ctx,
                  const DenseTensor& x,
                  std::vector<int> axes,
                  std::vector<int> starts,
                  std::vector<int> ends) {
  DenseTensor ret;
  std::vector<int> new_axes = axes;
  std::vector<int> out_shape = phi::vectorize<int>(x.dims());
  size_t rank = out_shape.size();
  PADDLE_ENFORCE_EQ(
      axes.size(),
      starts.size(),
      errors::InvalidArgument("Slice Operator Argument Invalided"));
  PADDLE_ENFORCE_EQ(
      ends.size(),
      starts.size(),
      errors::InvalidArgument("Slice Operator Argument Invalided"));
  for (unsigned int i = 0; i < axes.size(); ++i) {
    int axis = axes[i];
    if (axis < 0) axis = rank + axis;
    new_axes[i] = axis;  // change negative to positive
    int st = starts[i];
    int ed = ends[i];
    PADDLE_ENFORCE_GT(
        ed,
        st,
        errors::InvalidArgument("C++ Slice Operation Not Support End < Start"));
    out_shape[axis] = ed - st;
  }
  std::vector<int> offset(rank), extends(rank);
  for (size_t i = 0; i < rank; ++i) {
    offset[i] = 0;
    extends[i] = x.dims()[i];
  }
  for (size_t i = 0; i < new_axes.size(); ++i) {
    offset[new_axes[i]] = starts[i];
    extends[new_axes[i]] = ends[i] - starts[i];
  }
  ret.Resize(phi::make_ddim(out_shape));
  dev_ctx.template Alloc<T>(&ret);
  switch (rank) {
    SLICE_RANK_CASE(1);
    SLICE_RANK_CASE(2);
    SLICE_RANK_CASE(3);
    SLICE_RANK_CASE(4);
    SLICE_RANK_CASE(5);
    SLICE_RANK_CASE(6);
    default: {
      PADDLE_THROW(
          errors::InvalidArgument("Invalid Rank number, "
                                  "currently only support rank between 2~6"));
    }
  }
  return ret;
}

}  // namespace funcs
}  // namespace phi