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

S
sneaxiy 已提交
21 22 23
template <int N>
Dim<N> make_dim(const int64_t* d) {
  Dim<N> ret;
S
sneaxiy 已提交
24
  fix_dim_assign(d, ret.GetMutable());
S
sneaxiy 已提交
25 26
  return ret;
}
F
fengjiayi 已提交
27

Q
qijun 已提交
28
DDim make_ddim(std::initializer_list<int64_t> dims) {
S
sneaxiy 已提交
29
  return DDim(dims.begin(), dims.size());
F
fengjiayi 已提交
30 31
}

Q
qijun 已提交
32
DDim make_ddim(const std::vector<int64_t>& dims) {
S
sneaxiy 已提交
33
  return DDim(dims.data(), dims.size());
F
fengjiayi 已提交
34 35
}

Y
Yu Yang 已提交
36
DDim make_ddim(const std::vector<int>& dims) {
S
sneaxiy 已提交
37
  return DDim(dims.data(), dims.size());
Y
Yu Yang 已提交
38 39
}

S
sneaxiy 已提交
40 41
struct DDimEqualityVisitor {
  explicit DDimEqualityVisitor(const int64_t* d) : d_(d) {}
F
fengjiayi 已提交
42 43

  template <int D>
S
sneaxiy 已提交
44
  inline bool operator()(const Dim<D>& self) const {
S
sneaxiy 已提交
45
    return UnrollCompare<D>::Run(self.Get(), d_);
F
fengjiayi 已提交
46 47
  }

S
sneaxiy 已提交
48
  const int64_t* d_;
F
fengjiayi 已提交
49 50
};

S
sneaxiy 已提交
51
bool DDim::operator==(const DDim& d) const {
S
sneaxiy 已提交
52
  return rank_ == d.rank_ && this->apply_visitor(DDimEqualityVisitor(d.Get()));
F
fengjiayi 已提交
53 54
}

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

S
sneaxiy 已提交
57 58 59
struct DDimPlusVisitor {
  explicit DDimPlusVisitor(const int64_t* d1, const int64_t* d2)
      : d1_(d1), d2_(d2) {}
F
fengjiayi 已提交
60

S
sneaxiy 已提交
61 62
  template <int D>
  inline void operator()(Dim<D>& self) const {
S
sneaxiy 已提交
63
    UnrollAdd<D>::Run(d1_, d2_, self.GetMutable());
F
fengjiayi 已提交
64 65
  }

S
sneaxiy 已提交
66 67 68
  const int64_t* d1_;
  const int64_t* d2_;
};
F
fengjiayi 已提交
69

S
sneaxiy 已提交
70 71 72 73
DDim DDim::operator+(const DDim& d) const {
  PADDLE_ENFORCE(rank_ == d.rank_);
  DDim ret;
  ret.rank_ = rank_;
S
sneaxiy 已提交
74
  ret.apply_visitor(DDimPlusVisitor(Get(), d.Get()));
S
sneaxiy 已提交
75
  return ret;
F
fengjiayi 已提交
76 77
}

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

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

S
sneaxiy 已提交
87 88 89 90 91 92 93 94
  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_;
S
sneaxiy 已提交
95
  ret.apply_visitor(DDimMulVisitor(Get(), d.Get()));
S
sneaxiy 已提交
96
  return ret;
F
fengjiayi 已提交
97 98
}

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

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

Q
qijun 已提交
103
std::vector<int64_t> vectorize(const DDim& ddim) {
S
sneaxiy 已提交
104
  std::vector<int64_t> result(DDim::kMaxRank);
S
sneaxiy 已提交
105
  dynamic_dim_assign(ddim.Get(), result.data(), ddim.size());
S
sneaxiy 已提交
106
  result.resize(ddim.size());
F
fengjiayi 已提交
107 108 109
  return result;
}

C
chengduoZH 已提交
110 111 112
// NOTE: framework::vectorize converts to type int64_t
//       which does not fit cudnn inputs.
std::vector<int> vectorize2int(const DDim& ddim) {
S
sneaxiy 已提交
113
  std::vector<int> result(DDim::kMaxRank);
S
sneaxiy 已提交
114
  dynamic_dim_assign(ddim.Get(), result.data(), ddim.size());
S
sneaxiy 已提交
115
  result.resize(ddim.size());
C
chengduoZH 已提交
116 117 118
  return result;
}

S
sneaxiy 已提交
119
struct ProductVisitor {
F
fengjiayi 已提交
120
  template <int D>
Q
qijun 已提交
121
  int64_t operator()(const Dim<D>& dim) {
F
fengjiayi 已提交
122
    return product(dim);
F
fengjiayi 已提交
123
  }
F
fengjiayi 已提交
124 125
};

Q
qijun 已提交
126
int64_t product(const DDim& ddim) {
S
sneaxiy 已提交
127
  return ddim.apply_visitor(ProductVisitor());
F
fengjiayi 已提交
128 129
}

130
DDim slice_ddim(const DDim& dim, int begin, int end) {
S
sneaxiy 已提交
131 132 133 134
  PADDLE_ENFORCE(begin >= 0,
                 "Begin index can't be less than zero in ddim slice.");
  DDim ret;
  ret.rank_ = end - begin;
S
sneaxiy 已提交
135
  dynamic_dim_assign(dim.Get() + begin, ret.GetMutable(), ret.rank_);
S
sneaxiy 已提交
136 137
  return ret;
}
F
fengjiayi 已提交
138

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

L
liaogang 已提交
141
/// \cond HIDDEN
F
fengjiayi 已提交
142

S
sneaxiy 已提交
143
struct DDimPrinter {
F
fengjiayi 已提交
144
  std::ostream& os;
L
liaogang 已提交
145
  explicit DDimPrinter(std::ostream& os_) : os(os_) {}
F
fengjiayi 已提交
146 147 148 149 150 151 152

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

L
liaogang 已提交
153
/// \endcond
F
fengjiayi 已提交
154

155
std::ostream& operator<<(std::ostream& os, const DDim& ddim) {
S
sneaxiy 已提交
156
  ddim.apply_visitor(DDimPrinter(os));
F
fengjiayi 已提交
157 158 159
  return os;
}

F
fengjiayi 已提交
160
DDim flatten_to_2d(const DDim& src, int num_col_dims) {
161
  int rank = src.size();
F
Fix bug  
fengjiayi 已提交
162 163
  return make_ddim({product(slice_ddim(src, 0, num_col_dims)),
                    product(slice_ddim(src, num_col_dims, rank))});
164 165
}

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

W
wanghaoshuang 已提交
168
DDim stride(const DDim& ddim) {
S
sneaxiy 已提交
169 170
  DDim strides;
  strides.rank_ = ddim.size();
W
wanghaoshuang 已提交
171 172 173 174
  strides[ddim.size() - 1] = 1;
  for (int i = ddim.size() - 2; i >= 0; --i) {
    strides[i] = strides[i + 1] * ddim[i + 1];
  }
S
sneaxiy 已提交
175
  return strides;
W
wanghaoshuang 已提交
176
}
Y
Yancey1989 已提交
177 178

DDim stride_numel(const framework::DDim& ddim) {
S
sneaxiy 已提交
179 180
  DDim strides;
  strides.rank_ = ddim.size();
Y
Yancey1989 已提交
181 182 183 184
  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 已提交
185
  return strides;
Y
Yancey1989 已提交
186 187
}

188 189
}  // namespace framework
}  // namespace paddle