ddim.cc 5.0 KB
Newer Older
L
liaogang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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

15
#include "paddle/framework/ddim.h"
F
fengjiayi 已提交
16

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

L
liaogang 已提交
20
/// @cond HIDDEN
F
fengjiayi 已提交
21 22 23 24 25 26 27 28 29 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 64 65 66

template <int i>
Dim<i> make_dim(const int* d) {
  return Dim<i>(*d, make_dim<i - 1>(d + 1));
}

template <>
Dim<1> make_dim<1>(const int* d) {
  return Dim<1>(*d);
}

void make_ddim(DDim& ddim, const int* dims, int n) {
  switch (n) {
    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:
      throw std::invalid_argument(
          "Dynamic dimensions must have between [1, 9] dimensions.");
  }
}

L
liaogang 已提交
67
/// @endcond
F
fengjiayi 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80

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

DDim make_ddim(const std::vector<int>& dims) {
  DDim result(make_dim(0));
  make_ddim(result, &dims[0], dims.size());
  return result;
}

L
liaogang 已提交
81
/// @cond HIDDEN
F
fengjiayi 已提交
82 83
// XXX For some reason, putting this in an anonymous namespace causes errors
class DynamicMutableIndexer : public boost::static_visitor<int&> {
84
 public:
L
liaogang 已提交
85
  explicit DynamicMutableIndexer(int idx) : idx_(idx) {}
F
fengjiayi 已提交
86 87 88 89 90 91

  template <int D>
  int& operator()(Dim<D>& dim) const {
    return dim[idx_];
  }

92
 private:
F
fengjiayi 已提交
93 94 95 96
  int idx_;
};

class DynamicConstIndexer : public boost::static_visitor<int> {
97
 public:
L
liaogang 已提交
98
  explicit DynamicConstIndexer(int idx) : idx_(idx) {}
F
fengjiayi 已提交
99 100 101 102 103 104

  template <int D>
  int operator()(const Dim<D>& dim) const {
    return dim[idx_];
  }

105
 private:
F
fengjiayi 已提交
106 107 108
  int idx_;
};

L
liaogang 已提交
109
/// @endcond
F
fengjiayi 已提交
110 111 112 113 114 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

int& DDim::operator[](int idx) {
  return boost::apply_visitor(DynamicMutableIndexer(idx), var);
}

int DDim::operator[](int idx) const {
  return boost::apply_visitor(DynamicConstIndexer(idx), var);
}

bool DDim::operator==(DDim d) const {
  if (var.which() != d.getVar().which()) {
    return false;
  } else {
    std::vector<int> v1 = vectorize(*this);
    std::vector<int> 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<int> v1 = vectorize(*this);
  std::vector<int> v2 = vectorize(d);

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

  std::vector<int> 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);
}

int get(const DDim& ddim, int idx) { return ddim[idx]; }

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

L
liaogang 已提交
172
/// @cond HIDDEN
F
fengjiayi 已提交
173 174 175
struct VectorizeVisitor : public boost::static_visitor<> {
  std::vector<int>& vector;

L
liaogang 已提交
176
  explicit VectorizeVisitor(std::vector<int>& v) : vector(v) {}
F
fengjiayi 已提交
177 178 179 180 181 182 183 184 185

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

  void operator()(const Dim<1>& t) { vector.push_back(t.head); }
};
L
liaogang 已提交
186
/// @endcond
F
fengjiayi 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

std::vector<int> vectorize(const DDim& ddim) {
  std::vector<int> result;
  VectorizeVisitor visitor(result);
  boost::apply_visitor(visitor, ddim);
  return result;
}

ssize_t product(const DDim& ddim) {
  ssize_t result = 1;
  std::vector<int> v = vectorize(ddim);
  for (auto i : v) {
    result *= i;
  }
  return result;
}

L
liaogang 已提交
204
/// \cond HIDDEN
F
fengjiayi 已提交
205 206 207 208 209 210 211 212

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

L
liaogang 已提交
213
/// \endcond
F
fengjiayi 已提交
214 215 216

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

L
liaogang 已提交
217
/// \cond HIDDEN
F
fengjiayi 已提交
218 219 220

struct DDimPrinter : boost::static_visitor<void> {
  std::ostream& os;
L
liaogang 已提交
221
  explicit DDimPrinter(std::ostream& os_) : os(os_) {}
F
fengjiayi 已提交
222 223 224 225 226 227 228

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

L
liaogang 已提交
229
/// \endcond
F
fengjiayi 已提交
230

231
std::ostream& operator<<(std::ostream& os, const DDim& ddim) {
F
fengjiayi 已提交
232 233 234 235 236
  DDimPrinter printer(os);
  boost::apply_visitor(printer, ddim);
  return os;
}

237 238
}  // namespace framework
}  // namespace paddle