ddim.cc 7.7 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.

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. */

#include "ddim.h"

namespace paddle_mobile {
namespace framework {

/// @cond HIDDEN

L
liuruilong 已提交
22
template <int i> Dim<i> make_dim(const int64_t *d) {
朔-望's avatar
朔-望 已提交
23 24 25
  return Dim<i>(*d, make_dim<i - 1>(d + 1));
}

L
liuruilong 已提交
26
template <> Dim<0> make_dim<0>(const int64_t *d) { return Dim<0>(*d); }
朔-望's avatar
朔-望 已提交
27

L
liuruilong 已提交
28
void make_ddim(DDim &ddim, const int64_t *dims, int n) {
朔-望's avatar
朔-望 已提交
29
  switch (n) {
L
liuruilong 已提交
30 31 32 33 34 35 36 37 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
  case 0:
    ddim = make_dim<0>(dims);
    break;
  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:
    //      std::cout << "Dynamic dimensions must have between [1, 9]
    //      dimensions.";
    break;
朔-望's avatar
朔-望 已提交
64 65 66 67 68 69 70 71 72 73 74
  }
}

/// @endcond

DDim make_ddim(std::initializer_list<int64_t> dims) {
  DDim result(make_dim(0));
  make_ddim(result, dims.begin(), dims.size());
  return result;
}

L
liuruilong 已提交
75
DDim make_ddim(const std::vector<int64_t> &dims) {
朔-望's avatar
朔-望 已提交
76 77 78 79 80
  DDim result(make_dim(0));
  make_ddim(result, &dims[0], dims.size());
  return result;
}

L
liuruilong 已提交
81
DDim make_ddim(const std::vector<int> &dims) {
朔-望's avatar
朔-望 已提交
82 83 84 85 86 87 88 89
  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);
}

/// @cond HIDDEN
// XXX For some reason, putting this in an anonymous namespace causes errors
L
liuruilong 已提交
90 91
struct DynamicMutableIndexer : Vistor<int64_t &> {
public:
朔-望's avatar
朔-望 已提交
92 93
  explicit DynamicMutableIndexer(int idx) : idx_(idx) {}

L
liuruilong 已提交
94
  template <int D> int64_t &operator()(Dim<D> &dim) const { return dim[idx_]; }
朔-望's avatar
朔-望 已提交
95

L
liuruilong 已提交
96
private:
朔-望's avatar
朔-望 已提交
97 98 99 100
  int idx_;
};

struct DynamicConstIndexer : public Vistor<int64_t> {
L
liuruilong 已提交
101
public:
朔-望's avatar
朔-望 已提交
102 103
  explicit DynamicConstIndexer(int idx) : idx_(idx) {}

L
liuruilong 已提交
104
  template <int D> int64_t operator()(const Dim<D> &dim) const {
朔-望's avatar
朔-望 已提交
105 106 107
    return dim[idx_];
  }

L
liuruilong 已提交
108
private:
朔-望's avatar
朔-望 已提交
109 110 111 112 113
  int idx_;
};

/// @endcond

L
liuruilong 已提交
114
int64_t &DDim::operator[](int idx) {
朔-望's avatar
朔-望 已提交
115 116 117 118 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 162 163 164 165 166 167 168 169 170 171 172
  return DDim::ApplyVistor(DynamicMutableIndexer(idx), *this);
}

int64_t DDim::operator[](int idx) const {
  return DDim::ApplyVistor(DynamicConstIndexer(idx), *this);
}

int DDim::size() const { return arity(*this); }

bool DDim::operator==(DDim d) const {
  //  if (var.which() != d.getVar().which()) {
  //    return false;
  //  } else {
  std::vector<int64_t> v1 = vectorize(*this);
  std::vector<int64_t> v2 = vectorize(d);

  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 {
  std::vector<int64_t> v1 = vectorize(*this);
  std::vector<int64_t> v2 = vectorize(d);

  std::vector<int64_t> v3;

  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 {
  std::vector<int64_t> v1 = vectorize(*this);
  std::vector<int64_t> v2 = vectorize(d);

  std::vector<int64_t> v3;

  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);
}

L
liuruilong 已提交
173
int64_t get(const DDim &ddim, int idx) { return ddim[idx]; }
朔-望's avatar
朔-望 已提交
174

L
liuruilong 已提交
175
void set(DDim &ddim, int idx, int value) { ddim[idx] = value; }
朔-望's avatar
朔-望 已提交
176 177 178

/// @cond HIDDEN
struct VectorizeVisitor : Vistor<void> {
L
liuruilong 已提交
179
  std::vector<int64_t> &vector;
朔-望's avatar
朔-望 已提交
180

L
liuruilong 已提交
181
  explicit VectorizeVisitor(std::vector<int64_t> &v) : vector(v) {}
朔-望's avatar
朔-望 已提交
182

L
liuruilong 已提交
183
  template <typename T> void operator()(const T &t) {
朔-望's avatar
朔-望 已提交
184 185 186 187
    vector.push_back(t.head);
    this->operator()(t.tail);
  }

L
liuruilong 已提交
188
  void operator()(const Dim<0> &t) {}
朔-望's avatar
朔-望 已提交
189 190 191
};
/// @endcond

L
liuruilong 已提交
192
std::vector<int64_t> vectorize(const DDim &ddim) {
朔-望's avatar
朔-望 已提交
193 194 195 196 197 198 199 200
  std::vector<int64_t> result;
  VectorizeVisitor visitor(result);
  DDim::ApplyVistor(visitor, ddim);
  return result;
}

// NOTE: framework::vectorize converts to type int64_t
//       which does not fit cudnn inputs.
L
liuruilong 已提交
201
std::vector<int> vectorize2int(const DDim &ddim) {
朔-望's avatar
朔-望 已提交
202 203 204 205 206 207
  std::vector<int64_t> temp = vectorize(ddim);
  std::vector<int> result(temp.begin(), temp.end());
  return result;
}

struct ProductVisitor : Vistor<int64_t> {
L
liuruilong 已提交
208
  template <int D> int64_t operator()(const Dim<D> &dim) {
朔-望's avatar
朔-望 已提交
209 210 211 212
    return product(dim);
  }
};

L
liuruilong 已提交
213
int64_t product(const DDim &ddim) {
朔-望's avatar
朔-望 已提交
214 215 216 217 218
  ProductVisitor visitor;
  return DDim::ApplyVistor(visitor, ddim);
}

struct SliceVectorizeVisitor : Vistor<void> {
L
liuruilong 已提交
219
  std::vector<int64_t> &vector;
朔-望's avatar
朔-望 已提交
220 221 222
  int begin;
  int end;

L
liuruilong 已提交
223
  SliceVectorizeVisitor(std::vector<int64_t> &v, int b, int e)
朔-望's avatar
朔-望 已提交
224 225 226 227 228 229 230 231
      : 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.");
  }

L
liuruilong 已提交
232
  template <int S> void operator()(const Dim<S> &dim) {
朔-望's avatar
朔-望 已提交
233 234 235 236 237 238 239 240 241 242 243
    if (begin == 0) {
      vector.push_back(dim.head);
    } else {
      --begin;
    }
    --end;
    if (end > 0) {
      this->operator()(dim.tail);
    }
  }

L
liuruilong 已提交
244
  void operator()(const Dim<0> &dim) {
朔-望's avatar
朔-望 已提交
245 246 247 248
    //    PADDLE_ENFORCE(end == 0, "End index in ddim slice is out of bound.");
  }
};

L
liuruilong 已提交
249
DDim slice_ddim(const DDim &ddim, int begin, int end) {
朔-望's avatar
朔-望 已提交
250 251 252 253 254 255 256 257 258 259 260 261
  std::vector<int64_t> vec;
  vec.reserve(end - begin);
  SliceVectorizeVisitor visitor(vec, begin, end);
  //  boost::apply_visitor(visitor, dim);
  DDim::ApplyVistor(visitor, ddim);
  //  visitor(ddim.var.Get<Dim<4>>());
  return make_ddim(vec);
}

/// \cond HIDDEN

struct ArityVisitor : Vistor<int> {
L
liuruilong 已提交
262
  template <int D> int operator()(Dim<D>) const { return D; }
朔-望's avatar
朔-望 已提交
263 264 265 266
};

/// \endcond

L
liuruilong 已提交
267
int arity(const DDim &d) {
朔-望's avatar
朔-望 已提交
268 269 270 271 272 273 274 275 276
  ArityVisitor arityVisitor = ArityVisitor();
  return DDim::ApplyVistor(arityVisitor, d);
  //  return arityVisitor(d.var.Get<Dim<4>>());
  //  return boost::apply_visitor(ArityVisitor(), d); }
}
/// \cond HIDDEN

/// \endcond

L
liuruilong 已提交
277 278
struct OSVistor : Vistor<std::ostream &> {
  OSVistor(std::ostream &os) : os_(os) {}
朔-望's avatar
朔-望 已提交
279

L
liuruilong 已提交
280
  template <int D> std::ostream &operator()(Dim<D> dim) const {
朔-望's avatar
朔-望 已提交
281 282 283
    return os_ << dim;
  }

L
liuruilong 已提交
284 285
private:
  std::ostream &os_;
朔-望's avatar
朔-望 已提交
286 287
};

L
liuruilong 已提交
288
std::ostream &operator<<(std::ostream &os, const DDim &ddim) {
朔-望's avatar
朔-望 已提交
289 290 291 292 293 294 295 296 297
  auto vistor = OSVistor(os);
  DDim::ApplyVistor(vistor, ddim);
  return os;
}

DDim::DDim(std::initializer_list<int64_t> init_list) {
  *this = make_ddim(init_list);
}

L
liuruilong 已提交
298
DDim flatten_to_2d(const DDim &src, int num_col_dims) {
朔-望's avatar
朔-望 已提交
299 300 301 302 303
  int rank = src.size();
  return make_ddim({product(slice_ddim(src, 0, num_col_dims)),
                    product(slice_ddim(src, num_col_dims, rank))});
}

L
liuruilong 已提交
304
DDim flatten_to_1d(const DDim &src) { return make_ddim({product(src)}); }
朔-望's avatar
朔-望 已提交
305

L
liuruilong 已提交
306
DDim stride(const DDim &ddim) {
朔-望's avatar
朔-望 已提交
307 308 309 310 311 312 313 314
  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);
}

L
liuruilong 已提交
315
DDim stride_numel(const framework::DDim &ddim) {
朔-望's avatar
朔-望 已提交
316 317 318 319 320 321 322 323
  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);
}

L
liuruilong 已提交
324 325
} // namespace framework
} // namespace paddle_mobile