dim.h 8.0 KB
Newer Older
1
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13
//
// 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.
F
fengjiayi 已提交
14 15 16 17 18
#pragma once

#include <iostream>
#include <sstream>
#include <stdexcept>
S
sneaxiy 已提交
19
#include <string>
F
fengjiayi 已提交
20 21
#include <type_traits>

S
sneaxiy 已提交
22
#include "paddle/fluid/framework/array.h"
Y
Yi Wang 已提交
23
#include "paddle/fluid/platform/assert.h"
S
sneaxiy 已提交
24
#include "paddle/fluid/platform/enforce.h"
Y
Yi Wang 已提交
25
#include "paddle/fluid/platform/hostdevice.h"
F
fengjiayi 已提交
26

27 28
namespace paddle {
namespace framework {
F
fengjiayi 已提交
29 30

// Statically sized, statically indexed dimension
S
sneaxiy 已提交
31 32 33 34
template <int N>
class Dim : public Array<int64_t, N> {
 public:
  static_assert(N >= 0, "N must be not less than 0");
F
fengjiayi 已提交
35

S
sneaxiy 已提交
36 37
  static constexpr int kRank = N;
  using BaseClass = Array<int64_t, N>;
F
fengjiayi 已提交
38

S
sneaxiy 已提交
39 40 41 42
  inline Dim(int64_t head, const Dim<N - 1>& tail) {
    (*this)[0] = head;
    new (this->GetMutable() + 1) Dim<N - 1>(tail);
  }
F
fengjiayi 已提交
43

S
sneaxiy 已提交
44 45 46
  template <typename... Args>
  HOSTDEVICE explicit Dim(int64_t head, Args... args)
      : BaseClass(head, args...) {}
F
fengjiayi 已提交
47 48 49

  /** Construct a Dim from a linear index and size.  Uses Fortran order
   * indexing. */
S
sneaxiy 已提交
50
  HOSTDEVICE Dim(int64_t idx, const Dim<N>& size);
F
fengjiayi 已提交
51 52

  /** Construct a Dim with each dimension set to the given index */
S
sneaxiy 已提交
53
  HOSTDEVICE explicit Dim(int64_t idx) { this->Fill(idx); }
F
fengjiayi 已提交
54

S
sneaxiy 已提交
55
  HOSTDEVICE Dim() = default;
F
fengjiayi 已提交
56

S
sneaxiy 已提交
57
  HOSTDEVICE int64_t* data() { return this->GetMutable(); }
F
fengjiayi 已提交
58

S
sneaxiy 已提交
59
  HOSTDEVICE const int64_t* data() const { return this->Get(); }
F
fengjiayi 已提交
60 61 62 63

  HOST std::string to_string() const;
};

S
sneaxiy 已提交
64 65 66 67 68 69 70 71 72 73
namespace detail {
template <int kStart, int kEnd, bool kStop>
struct FortranOrderIndexingConstructorFunctor {
  HOSTDEVICE inline static void Run(const int64_t* in, int64_t* idx,
                                    int64_t* out) {
    out[kStart] = (*idx) % in[kStart];
    (*idx) /= in[kStart];
    FortranOrderIndexingConstructorFunctor<kStart + 1, kEnd,
                                           kStart + 1 == kEnd>::Run(in, idx,
                                                                    out);
F
fengjiayi 已提交
74 75 76
  }
};

S
sneaxiy 已提交
77 78 79 80
template <int kStart, int kEnd>
struct FortranOrderIndexingConstructorFunctor<kStart, kEnd, true> {
  HOSTDEVICE inline static void Run(const int64_t* in, int64_t* idx,
                                    int64_t* out) {}
F
fengjiayi 已提交
81
};
S
sneaxiy 已提交
82
}  // namespace detail
F
fengjiayi 已提交
83

S
sneaxiy 已提交
84 85 86 87
template <int N>
HOSTDEVICE Dim<N>::Dim(int64_t idx, const Dim<N>& size) {
  detail::FortranOrderIndexingConstructorFunctor<0, N, N == 0>::Run(
      size.Get(), &idx, this->GetMutable());
F
fengjiayi 已提交
88 89
}

S
sneaxiy 已提交
90 91 92
template <int idx, int N>
HOSTDEVICE inline int64_t get(const Dim<N>& dim) {
  return dim[idx];
F
fengjiayi 已提交
93 94
}

S
sneaxiy 已提交
95 96 97
template <int idx, int N>
HOSTDEVICE inline int64_t& get(Dim<N>& dim) {  // NOLINT
  return dim[idx];
F
fengjiayi 已提交
98 99
}

S
sneaxiy 已提交
100 101 102
template <int N>
HOSTDEVICE inline int64_t get(const Dim<N>& dim, int idx) {
  return dim[idx];
F
fengjiayi 已提交
103 104
}

S
sneaxiy 已提交
105 106 107
template <int N>
HOSTDEVICE inline int64_t& get(Dim<N>& dim, int idx) {  // NOLINT
  return dim[idx];
F
fengjiayi 已提交
108 109 110
}

// Dot product of two dims
S
sneaxiy 已提交
111 112 113
template <int N>
HOSTDEVICE inline int64_t linearize(const Dim<N>& a, const Dim<N>& b) {
  return UnrollProduct<N>::Run(a.Get(), b.Get());
F
fengjiayi 已提交
114 115 116
}

// Product of a Dim
S
sneaxiy 已提交
117 118 119
template <int N>
HOSTDEVICE inline int64_t product(const Dim<N>& a) {
  return UnrollProduct<N>::Run(a.Get());
F
fengjiayi 已提交
120 121 122
}

// Is 0 <= idx_i < size_i for all i?
S
sneaxiy 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
namespace detail {
template <int kStart, int kEnd, bool kStop>
struct ContainedFunctor {
  HOSTDEVICE static inline bool Run(const int64_t* idx, const int64_t* size) {
    return (idx[kStart] >= 0 && idx[kStart] < size[kStart]) &&
           ContainedFunctor<kStart + 1, kEnd, kStart + 1 == kEnd>::Run(idx,
                                                                       size);
  }
};

template <int kStart, int kEnd>
struct ContainedFunctor<kStart, kEnd, true> {
  HOSTDEVICE static constexpr inline bool Run(const int64_t* idx,
                                              const int64_t* size) {
    return true;
  }
};
}  // namespace detail
F
fengjiayi 已提交
141

S
sneaxiy 已提交
142 143 144
template <int N>
HOSTDEVICE inline bool contained(const Dim<N>& idx, const Dim<N>& size) {
  return detail::ContainedFunctor<0, N, N == 0>::Run(idx.Get(), size.Get());
F
fengjiayi 已提交
145 146 147 148 149
}

/**
 * \brief Compute exclusive prefix-multiply of a Dim.
 */
S
sneaxiy 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
namespace detail {
template <int kStart, int kEnd, bool kStop>
struct ExPrefixMulFunctor {
  HOSTDEVICE static inline void Run(const int64_t* in, int64_t* out) {
    kStart == 0 ? out[kStart] = 1 : out[kStart] =
                                        out[kStart - 1] * in[kStart - 1];
    detail::ExPrefixMulFunctor<kStart + 1, kEnd, kStart + 1 == kEnd>::Run(in,
                                                                          out);
  }
};

template <int kStart, int kEnd>
struct ExPrefixMulFunctor<kStart, kEnd, true> {
  HOSTDEVICE static inline void Run(const int64_t* in, int64_t* out) {}
};
}  // namespace detail
F
fengjiayi 已提交
166

S
sneaxiy 已提交
167 168 169 170 171
template <int N>
HOSTDEVICE inline Dim<N> ex_prefix_mul(const Dim<N>& src) {
  Dim<N> ret;
  detail::ExPrefixMulFunctor<0, N, N == 0>::Run(src.Get(), ret.GetMutable());
  return ret;
F
fengjiayi 已提交
172 173 174 175 176
}

/**
 * Add two dimensions together
 */
S
sneaxiy 已提交
177 178 179 180 181
template <int N>
HOSTDEVICE inline Dim<N> dim_plus(const Dim<N>& a, const Dim<N>& b) {
  Dim<N> ret;
  UnrollAdd<N>::Run(a.Get(), b.Get(), ret.GetMutable());
  return ret;
F
fengjiayi 已提交
182 183
}

S
sneaxiy 已提交
184 185
template <int N>
HOSTDEVICE inline Dim<N> operator+(const Dim<N>& lhs, const Dim<N>& rhs) {
F
fengjiayi 已提交
186 187 188 189 190 191
  return dim_plus(lhs, rhs);
}

/**
 * Multiply two dimensions together
 */
S
sneaxiy 已提交
192 193 194 195 196
template <int N>
HOSTDEVICE inline Dim<N> dim_mult(const Dim<N>& a, const Dim<N>& b) {
  Dim<N> ret;
  UnrollMul<N>::Run(a.Get(), b.Get(), ret.GetMutable());
  return ret;
F
fengjiayi 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
}

template <int i>
HOSTDEVICE Dim<i> operator*(const Dim<i>& lhs, const Dim<i>& rhs) {
  return dim_mult(lhs, rhs);
}

/**
 * \brief Normalize strides to ensure any dimension with extent 1
 * has stride 0.
 *
 * \param size Dim object containing the size of an array
 * \param stride Dim object containing stride of an array
 * \return Dim object the same size as \p size with normalized strides
 *
 */
S
sneaxiy 已提交
213 214 215 216 217 218 219 220 221 222
namespace detail {
template <int kStart, int kEnd, bool kStop>
struct NormalizeStridesFunctor {
  HOSTDEVICE static void Run(const int64_t* size, const int64_t* stride,
                             int64_t* ret) {
    ret[kStart] = (size[kStart] == 1 ? 0 : stride[kStart]);
    NormalizeStridesFunctor<kStart + 1, kEnd, kStart + 1 == kEnd>::Run(
        size, stride, ret);
  }
};
F
fengjiayi 已提交
223

S
sneaxiy 已提交
224 225 226 227 228 229
template <int kStart, int kEnd>
struct NormalizeStridesFunctor<kStart, kEnd, true> {
  HOSTDEVICE static void Run(const int64_t* size, const int64_t* stride,
                             int64_t* ret) {}
};
}  // namespace detail
F
fengjiayi 已提交
230

S
sneaxiy 已提交
231 232 233 234 235 236
template <int N>
HOSTDEVICE Dim<N> normalize_strides(const Dim<N>& size, const Dim<N>& stride) {
  Dim<N> ret;
  detail::NormalizeStridesFunctor<0, N, N == 0>::Run(size.Get(), stride.Get(),
                                                     ret.GetMutable());
  return ret;
F
fengjiayi 已提交
237 238 239 240 241 242 243 244 245 246
}

/**
 * Helper function to create a Dim
 *
 * \param idxes The type of Dim constructed depends on the number of params
 *
 */

template <typename... Args>
S
sneaxiy 已提交
247
HOSTDEVICE inline Dim<sizeof...(Args)> make_dim(Args... idxes) {
F
fengjiayi 已提交
248 249 250 251
  return Dim<sizeof...(Args)>(idxes...);
}

// Allows us to output a Dim
S
sneaxiy 已提交
252 253 254 255 256 257
template <int N>
inline std::ostream& operator<<(std::ostream& os, const Dim<N>& d) {
  os << d[0];
  for (int i = 1; i < N; ++i) {
    os << ", " << d[i];
  }
F
fengjiayi 已提交
258 259 260
  return os;
}

X
xuwei06 已提交
261 262 263 264
inline std::ostream& operator<<(std::ostream& os, const Dim<0>& d) {
  return os;
}

S
sneaxiy 已提交
265 266
template <int N>
HOST std::string Dim<N>::to_string() const {
F
fengjiayi 已提交
267 268 269 270 271
  std::stringstream stream;
  stream << *this;
  return stream.str();
}

S
sneaxiy 已提交
272 273 274
template <int N>
HOSTDEVICE Dim<N> linear_to_dimension(int linear_index, const Dim<N>& extents) {
  Dim<N> result;
F
fengjiayi 已提交
275

S
sneaxiy 已提交
276
  for (int i = 0; i < N - 1; ++i) {
F
fengjiayi 已提交
277 278 279 280
    result[i] = linear_index % extents[i];
    linear_index /= extents[i];
  }

S
sneaxiy 已提交
281
  result[N - 1] = linear_index;
F
fengjiayi 已提交
282 283 284 285

  return result;
}

286 287
}  // namespace framework
}  // namespace paddle