slice_utils.h 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2021 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
16
#include <glog/logging.h>
H
hong 已提交
17
#include <paddle/phi/core/ddim.h>
18 19
#include <string>
#include <vector>
20 21 22
#include "paddle/phi/core/flags.h"

PHI_DECLARE_bool(set_to_1d);
23

H
hong 已提交
24 25 26
namespace phi {

namespace funcs {
27 28

template <typename T = int64_t>
H
hong 已提交
29
inline void CheckAndUpdateSliceAttrs(const DDim in_dims,
30 31 32 33 34 35 36
                                     const std::vector<T>& axes,
                                     std::vector<T>* starts,
                                     std::vector<T>* ends,
                                     std::vector<int64_t>* steps = nullptr,
                                     std::vector<T>* infer_flags = nullptr) {
  for (size_t i = 0; i < axes.size(); ++i) {
    T axis = axes[i];
37
    PADDLE_ENFORCE_LT(
H
hong 已提交
38 39 40
        axis,
        in_dims.size(),
        phi::errors::InvalidArgument(
41 42
            "The axis value should be less than the rank of input, "
            "but received axes[%d] = %d, rank of input is %d.",
H
hong 已提交
43 44 45
            i,
            axis,
            in_dims.size()));
46 47 48 49 50

    if (infer_flags != nullptr && (*infer_flags)[i] == -1) {
      continue;
    }

51 52 53 54 55
    T dim_value = in_dims[axis];

    if (dim_value > 0) {
      T step = steps == nullptr ? 1 : (*steps)[i];
      PADDLE_ENFORCE_NE(
H
hong 已提交
56 57 58 59
          step,
          0,
          phi::errors::InvalidArgument(
              "Step should not be 0, but received step = %d.", step));
60

61 62 63 64 65 66 67
      T start = (*starts)[i] < 0 ? ((*starts)[i] + dim_value) : (*starts)[i];
      start = std::max(start, static_cast<T>(0));

      T end =
          0 < step && (*ends)[i] < 0 ? ((*ends)[i] + dim_value) : (*ends)[i];
      end = std::min(end, dim_value);

68 69 70
      if (step > 0) {
        start = std::min(start, dim_value);
        end = std::max(end, static_cast<T>(0));
71
        PADDLE_ENFORCE_GE(
H
hong 已提交
72 73 74
            end,
            start,
            phi::errors::InvalidArgument(
75 76
                "When step > 0, end should be greater than start, but "
                "received end = %d, start = %d.",
H
hong 已提交
77 78
                end,
                start));
79 80 81 82 83
      } else {
        // NOTE(liym27): When step < 0, start should less and equal to
        // dim_value-1
        // "end is -1" means contain the 0-th element of this axis.
        start = std::min(start, dim_value - 1);
Z
zyfncg 已提交
84 85 86
        if (end < -1) {
          end += dim_value;
        }
87
        end = std::max(end, static_cast<T>(-1));
88
        PADDLE_ENFORCE_GE(
H
hong 已提交
89 90 91
            start,
            end,
            phi::errors::InvalidArgument(
92 93
                "When step < 0, start should be greater than end, but "
                "received start = %d, end = %d.",
H
hong 已提交
94 95
                start,
                end));
96 97 98 99
      }

      (*starts)[i] = start;
      (*ends)[i] = end;
100 101 102
    } else if (dim_value == 0) {
      (*starts)[i] = 0;
      (*ends)[i] = 0;
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
template <typename T = int64_t>
inline void UpdateSliceAttrs(const DDim in_dims,
                             const std::vector<T>& axes,
                             std::vector<T>* starts,
                             std::vector<T>* ends,
                             std::vector<int64_t>* steps = nullptr,
                             std::vector<T>* infer_flags = nullptr) {
  for (size_t i = 0; i < axes.size(); ++i) {
    T axis = axes[i];
    if (infer_flags != nullptr && (*infer_flags)[i] == -1) {
      continue;
    }
    T dim_value = in_dims[axis];
    if (dim_value > 0) {
      T step = steps == nullptr ? 1 : (*steps)[i];
      T start = (*starts)[i] < 0 ? ((*starts)[i] + dim_value) : (*starts)[i];
      start = std::max(start, static_cast<T>(0));
      T end =
          0 < step && (*ends)[i] < 0 ? ((*ends)[i] + dim_value) : (*ends)[i];
      end = std::min(end, dim_value);

      if (step > 0) {
        start = std::min(start, dim_value);
        end = std::max(end, static_cast<T>(0));
      } else {
        // NOTE: When step < 0, start should less and equal to
        // dim_value-1
        // "end is -1" means contain the 0-th element of this axis.
        start = std::min(start, dim_value - 1);
        if (end < -1) {
          end += dim_value;
        }
        end = std::max(end, static_cast<T>(-1));
      }
      (*starts)[i] = start;
      (*ends)[i] = end;
    } else if (dim_value == 0) {
      (*starts)[i] = 0;
      (*ends)[i] = 0;
    }
  }
}

150
template <typename T = int64_t>
H
hong 已提交
151 152 153 154 155 156 157
inline phi::DDim GetSliceDims(const phi::DDim in_dims,
                              const std::vector<T>& axes,
                              const std::vector<T>& starts,
                              const std::vector<T>& ends,
                              std::vector<T>* steps = nullptr,
                              std::vector<T>* infer_flags = nullptr) {
  phi::DDim slice_dims(in_dims);
158 159 160 161 162 163 164 165

  for (size_t i = 0; i < axes.size(); ++i) {
    T axis = axes[i];
    if (infer_flags != nullptr && (*infer_flags)[i] == -1) {
      slice_dims[axis] = -1;
      continue;
    }

166 167 168 169
    if (in_dims[axis] == -1) {
      continue;
    }

170 171 172 173 174 175 176 177 178 179 180 181 182 183
    T start = starts[i];
    T end = ends[i];
    T step = steps == nullptr ? 1 : (*steps)[i];

    if (step > 0) {
      slice_dims[axis] = (end - start + step - 1) / step;
    } else {
      slice_dims[axis] = (end - start + step + 1) / step;
    }
  }
  return slice_dims;
}

template <typename T = int64_t>
H
hong 已提交
184 185 186 187
inline DDim GetDecreasedDims(const DDim slice_dims,
                             const std::vector<T>& decrease_axes,
                             std::vector<T>* infer_flags = nullptr) {
  DDim decreased_dims(slice_dims);
188
  std::vector<uint8_t> decrease_flag(slice_dims.size(), 0);
189 190 191
  if (decrease_axes.size() > 0) {
    for (size_t i = 0; i < decrease_axes.size(); ++i) {
      T axis = decrease_axes[i];
192
      decrease_flag[axis] = 1;
193
      if (infer_flags && (*infer_flags)[i] != -1) {
H
hong 已提交
194 195 196
        PADDLE_ENFORCE_EQ(decreased_dims[axis],
                          1,
                          phi::errors::InvalidArgument(
197 198
                              "Decrease dim should be 1, but now received %d",
                              decreased_dims[axis]));
199 200 201 202 203
      }
    }

    std::vector<T> new_shape;
    for (int i = 0; i < decreased_dims.size(); ++i) {
204
      if (decrease_flag[i] == 0) {
205 206 207
        new_shape.push_back(decreased_dims[i]);
      }
    }
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    if (FLAGS_set_to_1d && new_shape.size() == 0) {
      // NOTE(zoooo0820): Hack procssing to 1-D, when axes decrease to 0-D in
      // slice. This will remove in release 2.6.
      VLOG(0)
          << "Warning:: In Tensor '__getitem__', if the number of scalar "
             "elements "
             "in the index is equal to the rank of the Tensor, the output "
             "should "
             "be 0-D. In order to be consistent with the behavior of previous "
             "versions, it will be processed to 1-D. But it is not correct and "
             "will be "
             "removed in release 2.6. "
             "If 1-D is still wanted, please modify the index element from "
             "scalar to slice "
             "(e.g. 'x[i]' => 'x[i:i+1]'). ";
      new_shape.push_back(1);
    }
225
    decreased_dims = phi::make_ddim(new_shape);
226 227 228 229
  }
  return decreased_dims;
}

H
hong 已提交
230 231
}  // namespace funcs
}  // namespace phi