ddim.h 6.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
qijun 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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 已提交
15 16 17 18 19
#pragma once

#include <initializer_list>
#include <stdexcept>
#include <vector>
Y
Yi Wang 已提交
20
#include "paddle/fluid/framework/dim.h"
F
fengjiayi 已提交
21

22 23
namespace paddle {
namespace framework {
F
fengjiayi 已提交
24

S
sneaxiy 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
template <typename T1, typename T2>
inline void dynamic_dim_assign(const T1* in, T2* out, int n) {
#define STATIC_DIM_ASSIGN_CASE(rank)          \
  case rank:                                  \
    static_dim_assign<rank, T1, T2>(in, out); \
    return
  switch (n) {
    STATIC_DIM_ASSIGN_CASE(0);
    STATIC_DIM_ASSIGN_CASE(1);
    STATIC_DIM_ASSIGN_CASE(2);
    STATIC_DIM_ASSIGN_CASE(3);
    STATIC_DIM_ASSIGN_CASE(4);
    STATIC_DIM_ASSIGN_CASE(5);
    STATIC_DIM_ASSIGN_CASE(6);
    STATIC_DIM_ASSIGN_CASE(7);
    STATIC_DIM_ASSIGN_CASE(8);
    STATIC_DIM_ASSIGN_CASE(9);
    default:
      PADDLE_THROW("Invalid rank %d", n);
  }
#undef STATIC_DIM_ASSIGN_CASE
}

F
fengjiayi 已提交
48 49 50 51 52
/**
 * \brief A dynamically sized dimension.
 *
 * The number of dimensions must be between [1, 9].
 */
S
sneaxiy 已提交
53 54 55
class DDim {
 public:
  constexpr static int kMaxRank = 9;
F
fengjiayi 已提交
56

S
sneaxiy 已提交
57 58
  DDim() : rank_(1) { dim_[0] = 0; }

S
sneaxiy 已提交
59 60 61 62 63 64 65
  DDim(const int* d, int n) : rank_(n) {
    dynamic_dim_assign(d, dim_.GetMutable(), n);
  }

  DDim(const int64_t* d, int n) : rank_(n) {
    dynamic_dim_assign(d, dim_.GetMutable(), n);
  }
F
fengjiayi 已提交
66 67

  template <int D>
S
sneaxiy 已提交
68 69 70
  /*implicit*/ DDim(const Dim<D>& in) : rank_(D) {  // NOLINT
    UnsafeCast<D>() = in;
  }
F
fengjiayi 已提交
71

S
sneaxiy 已提交
72 73
  /*implicit*/ DDim(std::initializer_list<int64_t> init_list)
      : DDim(init_list.begin(), init_list.size()) {}
F
fengjiayi 已提交
74 75

  template <int D>
S
sneaxiy 已提交
76 77 78
  inline DDim& operator=(const Dim<D>& in) {
    rank_ = D;
    UnsafeCast<D>() = in;
F
fengjiayi 已提交
79 80 81
    return *this;
  }

S
sneaxiy 已提交
82
  inline int64_t& operator[](int idx) { return dim_[idx]; }
F
fengjiayi 已提交
83

S
sneaxiy 已提交
84 85 86 87 88
  inline int64_t operator[](int idx) const { return dim_[idx]; }

  inline int64_t& at(int idx) {
    PADDLE_ENFORCE(idx >= 0 && idx < rank_);
    return dim_[idx];
F
fengjiayi 已提交
89 90
  }

S
sneaxiy 已提交
91 92 93
  inline int64_t at(int idx) const {
    PADDLE_ENFORCE(idx >= 0 && idx < rank_);
    return dim_[idx];
F
fengjiayi 已提交
94 95
  }

S
sneaxiy 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108
  template <typename Visitor>
  typename std::result_of<Visitor(Dim<0>&)>::type apply_visitor(
      Visitor&& visitor);

  template <typename Visitor>
  typename std::result_of<Visitor(const Dim<0>&)>::type apply_visitor(
      Visitor&& visitor) const;

  bool operator==(const DDim& d) const;

  bool operator!=(const DDim& d) const;

  DDim operator+(const DDim& d) const;
F
fengjiayi 已提交
109

S
sneaxiy 已提交
110
  DDim operator*(const DDim& d) const;
F
fengjiayi 已提交
111

S
sneaxiy 已提交
112
  inline const int64_t* Get() const { return dim_.Get(); }
F
fengjiayi 已提交
113

S
sneaxiy 已提交
114
  inline int64_t* GetMutable() { return dim_.GetMutable(); }
S
sneaxiy 已提交
115

S
sneaxiy 已提交
116
  inline int size() const { return rank_; }
S
sneaxiy 已提交
117 118 119 120 121 122

 private:
  template <int M>
  inline Dim<M>& UnsafeCast() {
    return const_cast<Dim<M>&>(const_cast<const DDim*>(this)->UnsafeCast<M>());
  }
123

S
sneaxiy 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136
  template <int M>
  inline const Dim<M>& UnsafeCast() const {
    static_assert(M >= 0 && M <= kMaxRank, "Invalid rank");
    auto* p = static_cast<const void*>(&dim_);
    return *reinterpret_cast<const Dim<M>*>(p);
  }

  friend DDim slice_ddim(const DDim& dim, int begin, int end);
  friend DDim stride(const DDim& ddim);
  friend DDim stride_numel(const DDim& ddim);

  Dim<kMaxRank> dim_;
  int rank_;
F
fengjiayi 已提交
137 138
};

S
sneaxiy 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
#define PADDLE_VISIT_DDIM(rank) \
  case rank:                    \
    return visitor(UnsafeCast<rank>())

template <typename Visitor>
typename std::result_of<Visitor(Dim<0>&)>::type DDim::apply_visitor(
    Visitor&& visitor) {
  switch (rank_) {
    PADDLE_VISIT_DDIM(0);
    PADDLE_VISIT_DDIM(1);
    PADDLE_VISIT_DDIM(2);
    PADDLE_VISIT_DDIM(3);
    PADDLE_VISIT_DDIM(4);
    PADDLE_VISIT_DDIM(5);
    PADDLE_VISIT_DDIM(6);
    PADDLE_VISIT_DDIM(7);
    PADDLE_VISIT_DDIM(8);
    PADDLE_VISIT_DDIM(9);
    default:
      PADDLE_THROW("Invalid rank %d", rank_);
  }
}

template <typename Visitor>
typename std::result_of<Visitor(const Dim<0>&)>::type DDim::apply_visitor(
    Visitor&& visitor) const {
  switch (rank_) {
    PADDLE_VISIT_DDIM(0);
    PADDLE_VISIT_DDIM(1);
    PADDLE_VISIT_DDIM(2);
    PADDLE_VISIT_DDIM(3);
    PADDLE_VISIT_DDIM(4);
    PADDLE_VISIT_DDIM(5);
    PADDLE_VISIT_DDIM(6);
    PADDLE_VISIT_DDIM(7);
    PADDLE_VISIT_DDIM(8);
    PADDLE_VISIT_DDIM(9);
    default:
      PADDLE_THROW("Invalid rank %d", rank_);
  }
}
#undef PADDLE_VISIT_DDIM

F
fengjiayi 已提交
182
/**
Q
qijun 已提交
183
 * \brief Make a DDim from std::vector<int64_t>
F
fengjiayi 已提交
184 185 186
 *
 * \param dims An vector of ints. Must be sized between [1, 9]
 */
Q
qijun 已提交
187
DDim make_ddim(const std::vector<int64_t>& dims);
F
fengjiayi 已提交
188

Y
Yu Yang 已提交
189 190
DDim make_ddim(const std::vector<int>& dims);

F
fengjiayi 已提交
191 192 193 194 195 196
/**
 * \brief Make a DDim from an initializer list
 *
 * \param dims An initializer list of ints. Must be sized between [1, 9]
 *
 */
Q
qijun 已提交
197
DDim make_ddim(std::initializer_list<int64_t> dims);
F
fengjiayi 已提交
198

Q
qijun 已提交
199
int64_t get(const DDim& dim, int idx);
S
sneaxiy 已提交
200
void set(DDim& dim, int idx, int val);  // NOLINT
F
fengjiayi 已提交
201

Q
qijun 已提交
202
std::vector<int64_t> vectorize(const DDim& ddim);
C
chengduoZH 已提交
203
std::vector<int> vectorize2int(const DDim& ddim);
F
fengjiayi 已提交
204

Q
qijun 已提交
205
int64_t product(const DDim& ddim);
F
fengjiayi 已提交
206

F
fengjiayi 已提交
207 208 209 210 211 212 213
/**
 * \brief Slice a ddim
 *
 * Slice dim with [begin, end).
 * e.g.  DDim d = make_ddim({1,2,3,4,5});
 *       slice_ddim(d, 1, 3); ====> {2,3}
 */
214 215
DDim slice_ddim(const DDim& dim, int begin, int end);

F
fengjiayi 已提交
216 217 218 219 220 221 222 223
/**
 * \brief What is the length of this dimension?
 *
 * \param Dynamic dimension to inspect
 */

int arity(const DDim& ddim);

224
std::ostream& operator<<(std::ostream&, const DDim&);
F
fengjiayi 已提交
225

F
fengjiayi 已提交
226
// Reshape a tensor to a matrix. The matrix's first dimension(column length)
F
test  
fengjiayi 已提交
227
// will be the product of tensor's first `num_col_dims` dimensions.
F
fengjiayi 已提交
228
DDim flatten_to_2d(const DDim& src, int num_col_dims);
229

F
fengjiayi 已提交
230 231
DDim flatten_to_1d(const DDim& src);

W
wanghaoshuang 已提交
232
DDim stride(const DDim& ddim);
Y
Yancey1989 已提交
233 234

DDim stride_numel(const DDim& ddim);
235 236
}  // namespace framework
}  // namespace paddle