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 57 58 59

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

S
sneaxiy 已提交
60 61 62 63 64 65 66 67 68 69
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 已提交
70 71 72
  }
};

S
sneaxiy 已提交
73 74 75 76
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 已提交
77
};
S
sneaxiy 已提交
78
}  // namespace detail
F
fengjiayi 已提交
79

S
sneaxiy 已提交
80 81 82 83
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 已提交
84 85
}

S
sneaxiy 已提交
86 87 88
template <int idx, int N>
HOSTDEVICE inline int64_t get(const Dim<N>& dim) {
  return dim[idx];
F
fengjiayi 已提交
89 90
}

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

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

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

// Dot product of two dims
S
sneaxiy 已提交
107 108 109
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 已提交
110 111 112
}

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

// Is 0 <= idx_i < size_i for all i?
S
sneaxiy 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
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 已提交
137

S
sneaxiy 已提交
138 139 140
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 已提交
141 142 143 144 145
}

/**
 * \brief Compute exclusive prefix-multiply of a Dim.
 */
S
sneaxiy 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
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 已提交
162

S
sneaxiy 已提交
163 164 165 166 167
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 已提交
168 169 170 171 172
}

/**
 * Add two dimensions together
 */
S
sneaxiy 已提交
173 174 175 176 177
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 已提交
178 179
}

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

/**
 * Multiply two dimensions together
 */
S
sneaxiy 已提交
188 189 190 191 192
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 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
}

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 已提交
209 210 211 212 213 214 215 216 217 218
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 已提交
219

S
sneaxiy 已提交
220 221 222 223 224 225
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 已提交
226

S
sneaxiy 已提交
227 228 229 230 231 232
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 已提交
233 234 235 236 237 238 239 240 241 242
}

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

template <typename... Args>
S
sneaxiy 已提交
243
HOSTDEVICE inline Dim<sizeof...(Args)> make_dim(Args... idxes) {
F
fengjiayi 已提交
244 245 246 247
  return Dim<sizeof...(Args)>(idxes...);
}

// Allows us to output a Dim
S
sneaxiy 已提交
248 249 250 251 252 253
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 已提交
254 255 256
  return os;
}

X
xuwei06 已提交
257 258 259 260
inline std::ostream& operator<<(std::ostream& os, const Dim<0>& d) {
  return os;
}

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

S
sneaxiy 已提交
268 269 270
template <int N>
HOSTDEVICE Dim<N> linear_to_dimension(int linear_index, const Dim<N>& extents) {
  Dim<N> result;
F
fengjiayi 已提交
271

S
sneaxiy 已提交
272
  for (int i = 0; i < N - 1; ++i) {
F
fengjiayi 已提交
273 274 275 276
    result[i] = linear_index % extents[i];
    linear_index /= extents[i];
  }

S
sneaxiy 已提交
277
  result[N - 1] = linear_index;
F
fengjiayi 已提交
278 279 280 281

  return result;
}

S
sneaxiy 已提交
282 283 284 285 286
template <int D, typename T1, typename T2>
inline void static_dim_assign(const T1* in, T2* out) {
  UnrollAssign<D>::Run(in, out);
}

287 288
}  // namespace framework
}  // namespace paddle