ddim.cc 7.7 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

L
liaogang 已提交
21
/// @cond HIDDEN
F
fengjiayi 已提交
22 23

template <int i>
Q
qijun 已提交
24
Dim<i> make_dim(const int64_t* d) {
F
fengjiayi 已提交
25 26 27 28
  return Dim<i>(*d, make_dim<i - 1>(d + 1));
}

template <>
X
xuwei06 已提交
29 30
Dim<0> make_dim<0>(const int64_t* d) {
  return Dim<0>(*d);
F
fengjiayi 已提交
31 32
}

Q
qijun 已提交
33
void make_ddim(DDim& ddim, const int64_t* dims, int n) {
F
fengjiayi 已提交
34
  switch (n) {
X
xuwei06 已提交
35 36 37
    case 0:
      ddim = make_dim<0>(dims);
      break;
F
fengjiayi 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    case 1:
      ddim = make_dim<1>(dims);
      break;
    case 2:
      ddim = make_dim<2>(dims);
      break;
    case 3:
      ddim = make_dim<3>(dims);
      break;
    case 4:
      ddim = make_dim<4>(dims);
      break;
    case 5:
      ddim = make_dim<5>(dims);
      break;
    case 6:
      ddim = make_dim<6>(dims);
      break;
    case 7:
      ddim = make_dim<7>(dims);
      break;
    case 8:
      ddim = make_dim<8>(dims);
      break;
    case 9:
      ddim = make_dim<9>(dims);
      break;
    default:
Y
Yang Yang(Tony) 已提交
66
      PADDLE_THROW("Dynamic dimensions must have between [1, 9] dimensions.");
F
fengjiayi 已提交
67 68 69
  }
}

L
liaogang 已提交
70
/// @endcond
F
fengjiayi 已提交
71

Q
qijun 已提交
72
DDim make_ddim(std::initializer_list<int64_t> dims) {
F
fengjiayi 已提交
73 74 75 76 77
  DDim result(make_dim(0));
  make_ddim(result, dims.begin(), dims.size());
  return result;
}

Q
qijun 已提交
78
DDim make_ddim(const std::vector<int64_t>& dims) {
F
fengjiayi 已提交
79 80 81 82 83
  DDim result(make_dim(0));
  make_ddim(result, &dims[0], dims.size());
  return result;
}

Y
Yu Yang 已提交
84 85 86 87 88 89 90
DDim make_ddim(const std::vector<int>& dims) {
  std::vector<int64_t> res(dims.size());
  std::transform(dims.begin(), dims.end(), res.begin(),
                 [](int d) { return static_cast<int64_t>(d); });
  return make_ddim(res);
}

L
liaogang 已提交
91
/// @cond HIDDEN
F
fengjiayi 已提交
92
// XXX For some reason, putting this in an anonymous namespace causes errors
Q
qijun 已提交
93
class DynamicMutableIndexer : public boost::static_visitor<int64_t&> {
94
 public:
L
liaogang 已提交
95
  explicit DynamicMutableIndexer(int idx) : idx_(idx) {}
F
fengjiayi 已提交
96 97

  template <int D>
Q
qijun 已提交
98
  int64_t& operator()(Dim<D>& dim) const {
F
fengjiayi 已提交
99 100 101
    return dim[idx_];
  }

102
 private:
F
fengjiayi 已提交
103 104 105
  int idx_;
};

Q
qijun 已提交
106
class DynamicConstIndexer : public boost::static_visitor<int64_t> {
107
 public:
L
liaogang 已提交
108
  explicit DynamicConstIndexer(int idx) : idx_(idx) {}
F
fengjiayi 已提交
109 110

  template <int D>
Q
qijun 已提交
111
  int64_t operator()(const Dim<D>& dim) const {
F
fengjiayi 已提交
112 113 114
    return dim[idx_];
  }

115
 private:
F
fengjiayi 已提交
116 117 118
  int idx_;
};

L
liaogang 已提交
119
/// @endcond
F
fengjiayi 已提交
120

Q
qijun 已提交
121
int64_t& DDim::operator[](int idx) {
F
fengjiayi 已提交
122 123 124
  return boost::apply_visitor(DynamicMutableIndexer(idx), var);
}

Q
qijun 已提交
125
int64_t DDim::operator[](int idx) const {
F
fengjiayi 已提交
126 127 128
  return boost::apply_visitor(DynamicConstIndexer(idx), var);
}

129
int DDim::size() const { return arity(*this); }
F
fengjiayi 已提交
130

F
fengjiayi 已提交
131 132 133 134
bool DDim::operator==(DDim d) const {
  if (var.which() != d.getVar().which()) {
    return false;
  } else {
Q
qijun 已提交
135 136
    std::vector<int64_t> v1 = vectorize(*this);
    std::vector<int64_t> v2 = vectorize(d);
F
fengjiayi 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150

    for (unsigned int i = 0; i < v1.size(); i++) {
      if (v1[i] != v2[i]) {
        return false;
      }
    }

    return true;
  }
}

bool DDim::operator!=(DDim d) const { return !(*this == d); }

DDim DDim::operator+(DDim d) const {
Q
qijun 已提交
151 152
  std::vector<int64_t> v1 = vectorize(*this);
  std::vector<int64_t> v2 = vectorize(d);
F
fengjiayi 已提交
153

Q
qijun 已提交
154
  std::vector<int64_t> v3;
F
fengjiayi 已提交
155 156 157 158 159 160 161 162 163 164 165

  assert(v1.size() == v2.size());

  for (unsigned int i = 0; i < v1.size(); i++) {
    v3.push_back(v1[i] + v2[i]);
  }

  return make_ddim(v3);
}

DDim DDim::operator*(DDim d) const {
Q
qijun 已提交
166 167
  std::vector<int64_t> v1 = vectorize(*this);
  std::vector<int64_t> v2 = vectorize(d);
F
fengjiayi 已提交
168

Q
qijun 已提交
169
  std::vector<int64_t> v3;
F
fengjiayi 已提交
170 171 172 173 174 175 176 177 178 179

  assert(v1.size() == v2.size());

  for (unsigned int i = 0; i < v1.size(); i++) {
    v3.push_back(v1[i] * v2[i]);
  }

  return make_ddim(v3);
}

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

void set(DDim& ddim, int idx, int value) { ddim[idx] = value; }

L
liaogang 已提交
184
/// @cond HIDDEN
F
fengjiayi 已提交
185
struct VectorizeVisitor : public boost::static_visitor<> {
Q
qijun 已提交
186
  std::vector<int64_t>& vector;
F
fengjiayi 已提交
187

Q
qijun 已提交
188
  explicit VectorizeVisitor(std::vector<int64_t>& v) : vector(v) {}
F
fengjiayi 已提交
189 190 191 192 193 194 195

  template <typename T>
  void operator()(const T& t) {
    vector.push_back(t.head);
    this->operator()(t.tail);
  }

X
xuwei06 已提交
196
  void operator()(const Dim<0>& t) {}
F
fengjiayi 已提交
197
};
L
liaogang 已提交
198
/// @endcond
F
fengjiayi 已提交
199

Q
qijun 已提交
200 201
std::vector<int64_t> vectorize(const DDim& ddim) {
  std::vector<int64_t> result;
F
fengjiayi 已提交
202 203 204 205 206
  VectorizeVisitor visitor(result);
  boost::apply_visitor(visitor, ddim);
  return result;
}

C
chengduoZH 已提交
207 208 209 210 211 212 213 214
// NOTE: framework::vectorize converts to type int64_t
//       which does not fit cudnn inputs.
std::vector<int> vectorize2int(const DDim& ddim) {
  std::vector<int64_t> temp = vectorize(ddim);
  std::vector<int> result(temp.begin(), temp.end());
  return result;
}

Q
qijun 已提交
215
struct ProductVisitor : public boost::static_visitor<int64_t> {
F
fengjiayi 已提交
216
  template <int D>
Q
qijun 已提交
217
  int64_t operator()(const Dim<D>& dim) {
F
fengjiayi 已提交
218
    return product(dim);
F
fengjiayi 已提交
219
  }
F
fengjiayi 已提交
220 221
};

Q
qijun 已提交
222
int64_t product(const DDim& ddim) {
F
fengjiayi 已提交
223 224
  ProductVisitor visitor;
  return boost::apply_visitor(visitor, ddim);
F
fengjiayi 已提交
225 226
}

227
struct SliceVectorizeVisitor : public boost::static_visitor<> {
Q
qijun 已提交
228
  std::vector<int64_t>& vector;
229 230 231
  int begin;
  int end;

Q
qijun 已提交
232
  SliceVectorizeVisitor(std::vector<int64_t>& v, int b, int e)
233 234 235 236 237
      : vector(v), begin(b), end(e) {
    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.");
F
fengjiayi 已提交
238
  }
239 240 241 242 243 244 245 246 247 248 249 250 251 252

  template <int S>
  void operator()(const Dim<S>& dim) {
    if (begin == 0) {
      vector.push_back(dim.head);
    } else {
      --begin;
    }
    --end;
    if (end > 0) {
      this->operator()(dim.tail);
    }
  }

X
xuwei06 已提交
253 254
  void operator()(const Dim<0>& dim) {
    PADDLE_ENFORCE(end == 0, "End index in ddim slice is out of bound.");
255 256 257 258
  }
};

DDim slice_ddim(const DDim& dim, int begin, int end) {
Q
qijun 已提交
259
  std::vector<int64_t> vec;
260 261 262 263
  vec.reserve(end - begin);
  SliceVectorizeVisitor visitor(vec, begin, end);
  boost::apply_visitor(visitor, dim);
  return make_ddim(vec);
F
fengjiayi 已提交
264 265
}

L
liaogang 已提交
266
/// \cond HIDDEN
F
fengjiayi 已提交
267 268 269 270 271 272 273 274

struct ArityVisitor : boost::static_visitor<int> {
  template <int D>
  int operator()(Dim<D>) const {
    return D;
  }
};

L
liaogang 已提交
275
/// \endcond
F
fengjiayi 已提交
276 277 278

int arity(const DDim& d) { return boost::apply_visitor(ArityVisitor(), d); }

L
liaogang 已提交
279
/// \cond HIDDEN
F
fengjiayi 已提交
280 281 282

struct DDimPrinter : boost::static_visitor<void> {
  std::ostream& os;
L
liaogang 已提交
283
  explicit DDimPrinter(std::ostream& os_) : os(os_) {}
F
fengjiayi 已提交
284 285 286 287 288 289 290

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

L
liaogang 已提交
291
/// \endcond
F
fengjiayi 已提交
292

293
std::ostream& operator<<(std::ostream& os, const DDim& ddim) {
F
fengjiayi 已提交
294 295 296 297 298
  DDimPrinter printer(os);
  boost::apply_visitor(printer, ddim);
  return os;
}

Q
qijun 已提交
299
DDim::DDim(std::initializer_list<int64_t> init_list) {
300 301
  *this = make_ddim(init_list);
}
302

F
fengjiayi 已提交
303
DDim flatten_to_2d(const DDim& src, int num_col_dims) {
304
  int rank = src.size();
F
Fix bug  
fengjiayi 已提交
305 306
  return make_ddim({product(slice_ddim(src, 0, num_col_dims)),
                    product(slice_ddim(src, num_col_dims, rank))});
307 308
}

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

W
wanghaoshuang 已提交
311 312 313 314 315 316 317 318
DDim stride(const DDim& ddim) {
  std::vector<int64_t> strides(ddim.size());
  strides[ddim.size() - 1] = 1;
  for (int i = ddim.size() - 2; i >= 0; --i) {
    strides[i] = strides[i + 1] * ddim[i + 1];
  }
  return framework::make_ddim(strides);
}
Y
Yancey1989 已提交
319 320 321 322 323 324 325 326 327 328

DDim stride_numel(const framework::DDim& ddim) {
  std::vector<int64_t> strides(ddim.size());
  strides[ddim.size() - 1] = ddim[ddim.size() - 1];
  for (int i = ddim.size() - 2; i >= 0; --i) {
    strides[i] = strides[i + 1] * ddim[i];
  }
  return framework::make_ddim(strides);
}

329 330
}  // namespace framework
}  // namespace paddle