ddim.cc 5.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
L
liaogang 已提交
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. */

Y
Yi Wang 已提交
15 16
#include "paddle/fluid/framework/ddim.h"
#include "paddle/fluid/platform/enforce.h"
F
fengjiayi 已提交
17

18 19
namespace paddle {
namespace framework {
F
fengjiayi 已提交
20

S
sneaxiy 已提交
21 22 23 24 25
template <typename T>
struct DDimAssignFunctor {
  static_assert(std::is_integral<T>::value, "T must be integral type");
  using result_type = void;
  explicit DDimAssignFunctor(const T* in) : in_(in) {}
F
fengjiayi 已提交
26

S
sneaxiy 已提交
27 28 29 30 31 32 33
  template <int D>
  inline void operator()(Dim<D>& dim) {  // NOLINT
    UnrollAssign<D>::Run(in_, dim.data());
  }

  const T* in_;
};
F
fengjiayi 已提交
34

S
sneaxiy 已提交
35 36
DDim::DDim(const int* d, int n) : rank_(n) {
  this->apply_visitor(DDimAssignFunctor<int>(d));
F
fengjiayi 已提交
37 38
}

S
sneaxiy 已提交
39 40
DDim::DDim(const int64_t* d, int n) : rank_(n) {
  this->apply_visitor(DDimAssignFunctor<int64_t>(d));
F
fengjiayi 已提交
41 42
}

S
sneaxiy 已提交
43 44 45 46 47 48
template <int N>
Dim<N> make_dim(const int64_t* d) {
  Dim<N> ret;
  for (int i = 0; i < N; ++i) ret[i] = d[i];
  return ret;
}
F
fengjiayi 已提交
49

Q
qijun 已提交
50
DDim make_ddim(std::initializer_list<int64_t> dims) {
S
sneaxiy 已提交
51
  return DDim(dims.begin(), dims.size());
F
fengjiayi 已提交
52 53
}

Q
qijun 已提交
54
DDim make_ddim(const std::vector<int64_t>& dims) {
S
sneaxiy 已提交
55
  return DDim(dims.data(), dims.size());
F
fengjiayi 已提交
56 57
}

Y
Yu Yang 已提交
58
DDim make_ddim(const std::vector<int>& dims) {
S
sneaxiy 已提交
59
  return DDim(dims.data(), dims.size());
Y
Yu Yang 已提交
60 61
}

S
sneaxiy 已提交
62 63
struct DDimEqualityVisitor {
  explicit DDimEqualityVisitor(const int64_t* d) : d_(d) {}
F
fengjiayi 已提交
64 65

  template <int D>
S
sneaxiy 已提交
66 67
  inline bool operator()(const Dim<D>& self) const {
    return UnrollCompare<D>::Run(self.data(), d_);
F
fengjiayi 已提交
68 69
  }

S
sneaxiy 已提交
70
  const int64_t* d_;
F
fengjiayi 已提交
71 72
};

S
sneaxiy 已提交
73 74
bool DDim::operator==(const DDim& d) const {
  return rank_ == d.rank_ && this->apply_visitor(DDimEqualityVisitor(d.data()));
F
fengjiayi 已提交
75 76
}

S
sneaxiy 已提交
77
bool DDim::operator!=(const DDim& d) const { return !(*this == d); }
F
fengjiayi 已提交
78

S
sneaxiy 已提交
79 80 81
struct DDimPlusVisitor {
  explicit DDimPlusVisitor(const int64_t* d1, const int64_t* d2)
      : d1_(d1), d2_(d2) {}
F
fengjiayi 已提交
82

S
sneaxiy 已提交
83 84 85
  template <int D>
  inline void operator()(Dim<D>& self) const {
    UnrollAdd<D>::Run(d1_, d2_, self.data());
F
fengjiayi 已提交
86 87
  }

S
sneaxiy 已提交
88 89 90
  const int64_t* d1_;
  const int64_t* d2_;
};
F
fengjiayi 已提交
91

S
sneaxiy 已提交
92 93 94 95 96 97
DDim DDim::operator+(const DDim& d) const {
  PADDLE_ENFORCE(rank_ == d.rank_);
  DDim ret;
  ret.rank_ = rank_;
  ret.apply_visitor(DDimPlusVisitor(data(), d.data()));
  return ret;
F
fengjiayi 已提交
98 99
}

S
sneaxiy 已提交
100 101 102
struct DDimMulVisitor {
  explicit DDimMulVisitor(const int64_t* d1, const int64_t* d2)
      : d1_(d1), d2_(d2) {}
F
fengjiayi 已提交
103

S
sneaxiy 已提交
104 105 106
  template <int D>
  inline void operator()(Dim<D>& self) const {
    UnrollMul<D>::Run(d1_, d2_, self.data());
F
fengjiayi 已提交
107 108
  }

S
sneaxiy 已提交
109 110 111 112 113 114 115 116 117 118
  const int64_t* d1_;
  const int64_t* d2_;
};

DDim DDim::operator*(const DDim& d) const {
  PADDLE_ENFORCE(rank_ == d.rank_);
  DDim ret;
  ret.rank_ = rank_;
  ret.apply_visitor(DDimMulVisitor(data(), d.data()));
  return ret;
F
fengjiayi 已提交
119 120
}

Q
qijun 已提交
121
int64_t get(const DDim& ddim, int idx) { return ddim[idx]; }
F
fengjiayi 已提交
122

S
sneaxiy 已提交
123
void set(DDim& ddim, int idx, int value) { ddim[idx] = value; }  // NOLINT
F
fengjiayi 已提交
124

Q
qijun 已提交
125
std::vector<int64_t> vectorize(const DDim& ddim) {
S
sneaxiy 已提交
126 127 128 129 130
  std::vector<int64_t> result(DDim::kMaxRank);
  for (int i = 0; i < ddim.size(); ++i) {
    result[i] = ddim[i];
  }
  result.resize(ddim.size());
F
fengjiayi 已提交
131 132 133
  return result;
}

C
chengduoZH 已提交
134 135 136
// NOTE: framework::vectorize converts to type int64_t
//       which does not fit cudnn inputs.
std::vector<int> vectorize2int(const DDim& ddim) {
S
sneaxiy 已提交
137 138 139 140 141
  std::vector<int> result(DDim::kMaxRank);
  for (int i = 0; i < ddim.size(); ++i) {
    result[i] = ddim[i];
  }
  result.resize(ddim.size());
C
chengduoZH 已提交
142 143 144
  return result;
}

S
sneaxiy 已提交
145
struct ProductVisitor {
F
fengjiayi 已提交
146
  template <int D>
Q
qijun 已提交
147
  int64_t operator()(const Dim<D>& dim) {
F
fengjiayi 已提交
148
    return product(dim);
F
fengjiayi 已提交
149
  }
F
fengjiayi 已提交
150 151
};

Q
qijun 已提交
152
int64_t product(const DDim& ddim) {
S
sneaxiy 已提交
153
  return ddim.apply_visitor(ProductVisitor());
F
fengjiayi 已提交
154 155
}

156
DDim slice_ddim(const DDim& dim, int begin, int end) {
S
sneaxiy 已提交
157 158 159 160 161 162 163 164
  PADDLE_ENFORCE(begin < end,
                 "Begin index must be less than end index in ddim slice.");
  PADDLE_ENFORCE(begin >= 0,
                 "Begin index can't be less than zero in ddim slice.");
  DDim ret;
  ret.rank_ = end - begin;
  for (int i = 0; i < ret.rank_; ++i) {
    ret[i] = dim[i + begin];
F
fengjiayi 已提交
165
  }
S
sneaxiy 已提交
166 167
  return ret;
}
F
fengjiayi 已提交
168

S
sneaxiy 已提交
169
int arity(const DDim& d) { return d.size(); }
F
fengjiayi 已提交
170

L
liaogang 已提交
171
/// \cond HIDDEN
F
fengjiayi 已提交
172

S
sneaxiy 已提交
173
struct DDimPrinter {
F
fengjiayi 已提交
174
  std::ostream& os;
L
liaogang 已提交
175
  explicit DDimPrinter(std::ostream& os_) : os(os_) {}
F
fengjiayi 已提交
176 177 178 179 180 181 182

  template <typename T>
  void operator()(const T& t) {
    os << t;
  }
};

L
liaogang 已提交
183
/// \endcond
F
fengjiayi 已提交
184

185
std::ostream& operator<<(std::ostream& os, const DDim& ddim) {
S
sneaxiy 已提交
186
  ddim.apply_visitor(DDimPrinter(os));
F
fengjiayi 已提交
187 188 189
  return os;
}

F
fengjiayi 已提交
190
DDim flatten_to_2d(const DDim& src, int num_col_dims) {
191
  int rank = src.size();
F
Fix bug  
fengjiayi 已提交
192 193
  return make_ddim({product(slice_ddim(src, 0, num_col_dims)),
                    product(slice_ddim(src, num_col_dims, rank))});
194 195
}

F
Fix bug  
fengjiayi 已提交
196
DDim flatten_to_1d(const DDim& src) { return make_ddim({product(src)}); }
F
fengjiayi 已提交
197

W
wanghaoshuang 已提交
198
DDim stride(const DDim& ddim) {
S
sneaxiy 已提交
199 200
  DDim strides;
  strides.rank_ = ddim.size();
W
wanghaoshuang 已提交
201 202 203 204
  strides[ddim.size() - 1] = 1;
  for (int i = ddim.size() - 2; i >= 0; --i) {
    strides[i] = strides[i + 1] * ddim[i + 1];
  }
S
sneaxiy 已提交
205
  return strides;
W
wanghaoshuang 已提交
206
}
Y
Yancey1989 已提交
207 208

DDim stride_numel(const framework::DDim& ddim) {
S
sneaxiy 已提交
209 210
  DDim strides;
  strides.rank_ = ddim.size();
Y
Yancey1989 已提交
211 212 213 214
  strides[ddim.size() - 1] = ddim[ddim.size() - 1];
  for (int i = ddim.size() - 2; i >= 0; --i) {
    strides[i] = strides[i + 1] * ddim[i];
  }
S
sneaxiy 已提交
215
  return strides;
Y
Yancey1989 已提交
216 217
}

218 219
}  // namespace framework
}  // namespace paddle