ddim.h 5.5 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 25 26 27 28 29

/**
 * \brief A dynamically sized dimension.
 *
 * The number of dimensions must be between [1, 9].
 */
S
sneaxiy 已提交
30 31 32
class DDim {
 public:
  constexpr static int kMaxRank = 9;
F
fengjiayi 已提交
33

S
sneaxiy 已提交
34 35 36 37
  DDim() : rank_(1) { dim_[0] = 0; }

  DDim(const int* d, int n);
  DDim(const int64_t* d, int n);
F
fengjiayi 已提交
38 39

  template <int D>
S
sneaxiy 已提交
40 41 42
  /*implicit*/ DDim(const Dim<D>& in) : rank_(D) {  // NOLINT
    UnsafeCast<D>() = in;
  }
F
fengjiayi 已提交
43

S
sneaxiy 已提交
44 45
  /*implicit*/ DDim(std::initializer_list<int64_t> init_list)
      : DDim(init_list.begin(), init_list.size()) {}
F
fengjiayi 已提交
46 47

  template <int D>
S
sneaxiy 已提交
48 49 50
  inline DDim& operator=(const Dim<D>& in) {
    rank_ = D;
    UnsafeCast<D>() = in;
F
fengjiayi 已提交
51 52 53
    return *this;
  }

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

S
sneaxiy 已提交
56 57 58 59 60
  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 已提交
61 62
  }

S
sneaxiy 已提交
63 64 65
  inline int64_t at(int idx) const {
    PADDLE_ENFORCE(idx >= 0 && idx < rank_);
    return dim_[idx];
F
fengjiayi 已提交
66 67
  }

S
sneaxiy 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80
  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 已提交
81

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

S
sneaxiy 已提交
84 85 86
  // Make DDim act like std::vector<int64_t>
  using iterator = int64_t*;
  using const_iterator = const int64_t*;
F
fengjiayi 已提交
87

S
sneaxiy 已提交
88 89
  int64_t* data() { return dim_.data(); }
  const int64_t* data() const { return dim_.data(); }
F
fengjiayi 已提交
90

S
sneaxiy 已提交
91 92 93 94 95 96 97 98 99 100 101 102
  iterator begin() { return data(); }
  const_iterator begin() const { return data(); }
  iterator end() { return data() + rank_; }
  const_iterator end() const { return data() + rank_; }

  int size() const { return rank_; }

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

S
sneaxiy 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116
  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 已提交
117 118
};

S
sneaxiy 已提交
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 150 151 152 153 154 155 156 157 158 159 160 161
#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 已提交
162
/**
Q
qijun 已提交
163
 * \brief Make a DDim from std::vector<int64_t>
F
fengjiayi 已提交
164 165 166
 *
 * \param dims An vector of ints. Must be sized between [1, 9]
 */
Q
qijun 已提交
167
DDim make_ddim(const std::vector<int64_t>& dims);
F
fengjiayi 已提交
168

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

F
fengjiayi 已提交
171 172 173 174 175 176
/**
 * \brief Make a DDim from an initializer list
 *
 * \param dims An initializer list of ints. Must be sized between [1, 9]
 *
 */
Q
qijun 已提交
177
DDim make_ddim(std::initializer_list<int64_t> dims);
F
fengjiayi 已提交
178

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

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

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

F
fengjiayi 已提交
187 188 189 190 191 192 193
/**
 * \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}
 */
194 195
DDim slice_ddim(const DDim& dim, int begin, int end);

F
fengjiayi 已提交
196 197 198 199 200 201 202 203
/**
 * \brief What is the length of this dimension?
 *
 * \param Dynamic dimension to inspect
 */

int arity(const DDim& ddim);

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

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

F
fengjiayi 已提交
210 211
DDim flatten_to_1d(const DDim& src);

W
wanghaoshuang 已提交
212
DDim stride(const DDim& ddim);
Y
Yancey1989 已提交
213 214

DDim stride_numel(const DDim& ddim);
215 216
}  // namespace framework
}  // namespace paddle