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
template <int D>
class Dim : public Array<int64_t, D> {
S
sneaxiy 已提交
33
 public:
S
sneaxiy 已提交
34
  static_assert(D >= 0, "D must be not less than 0");
F
fengjiayi 已提交
35

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

S
sneaxiy 已提交
39
  inline Dim(int64_t head, const Dim<D - 1>& tail) {
S
sneaxiy 已提交
40
    (*this)[0] = head;
S
sneaxiy 已提交
41
    new (this->GetMutable() + 1) Dim<D - 1>(tail);
S
sneaxiy 已提交
42
  }
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<D>& 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
template <int D>
HOSTDEVICE Dim<D>::Dim(int64_t idx, const Dim<D>& size) {
  detail::FortranOrderIndexingConstructorFunctor<0, D, D == 0>::Run(
S
sneaxiy 已提交
83
      size.Get(), &idx, this->GetMutable());
F
fengjiayi 已提交
84 85
}

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

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

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

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

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

// Product of a Dim
S
sneaxiy 已提交
113 114 115
template <int D>
HOSTDEVICE inline int64_t product(const Dim<D>& a) {
  return UnrollProduct<D>::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 D>
HOSTDEVICE inline bool contained(const Dim<D>& idx, const Dim<D>& size) {
  return detail::ContainedFunctor<0, D, D == 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
template <int D>
HOSTDEVICE inline Dim<D> ex_prefix_mul(const Dim<D>& src) {
  Dim<D> ret;
  detail::ExPrefixMulFunctor<0, D, D == 0>::Run(src.Get(), ret.GetMutable());
S
sneaxiy 已提交
167
  return ret;
F
fengjiayi 已提交
168 169 170 171 172
}

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

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

/**
 * Multiply two dimensions together
 */
S
sneaxiy 已提交
188 189 190 191
template <int D>
HOSTDEVICE inline Dim<D> dim_mult(const Dim<D>& a, const Dim<D>& b) {
  Dim<D> ret;
  UnrollMul<D>::Run(a.Get(), b.Get(), ret.GetMutable());
S
sneaxiy 已提交
192
  return ret;
F
fengjiayi 已提交
193 194
}

S
sneaxiy 已提交
195 196
template <int D>
HOSTDEVICE Dim<D> operator*(const Dim<D>& lhs, const Dim<D>& rhs) {
F
fengjiayi 已提交
197 198 199 200 201 202 203 204 205 206 207 208
  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
template <int D>
HOSTDEVICE Dim<D> normalize_strides(const Dim<D>& size, const Dim<D>& stride) {
  Dim<D> ret;
  detail::NormalizeStridesFunctor<0, D, D == 0>::Run(size.Get(), stride.Get(),
S
sneaxiy 已提交
231 232
                                                     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
template <int D>
inline std::ostream& operator<<(std::ostream& os, const Dim<D>& d) {
S
sneaxiy 已提交
250
  os << d[0];
S
sneaxiy 已提交
251
  for (int i = 1; i < D; ++i) {
S
sneaxiy 已提交
252 253
    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 D>
HOST std::string Dim<D>::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 D>
HOSTDEVICE Dim<D> linear_to_dimension(int linear_index, const Dim<D>& extents) {
  Dim<D> result;
F
fengjiayi 已提交
271

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

S
sneaxiy 已提交
277
  result[D - 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