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

Q
qijun 已提交
21
DDim make_ddim(std::initializer_list<int64_t> dims) {
S
sneaxiy 已提交
22
  return DDim(dims.begin(), dims.size());
F
fengjiayi 已提交
23 24
}

Q
qijun 已提交
25
DDim make_ddim(const std::vector<int64_t>& dims) {
S
sneaxiy 已提交
26
  return DDim(dims.data(), dims.size());
F
fengjiayi 已提交
27 28
}

Y
Yu Yang 已提交
29
DDim make_ddim(const std::vector<int>& dims) {
S
sneaxiy 已提交
30
  return DDim(dims.data(), dims.size());
Y
Yu Yang 已提交
31 32
}

S
sneaxiy 已提交
33 34
struct DDimEqualityVisitor {
  explicit DDimEqualityVisitor(const int64_t* d) : d_(d) {}
F
fengjiayi 已提交
35 36

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

S
sneaxiy 已提交
41
  const int64_t* d_;
F
fengjiayi 已提交
42 43
};

S
sneaxiy 已提交
44
bool DDim::operator==(const DDim& d) const {
S
sneaxiy 已提交
45 46
  return size() == d.size() &&
         this->apply_visitor(DDimEqualityVisitor(d.Get()));
F
fengjiayi 已提交
47 48
}

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

Q
qijun 已提交
51
std::vector<int64_t> vectorize(const DDim& ddim) {
S
sneaxiy 已提交
52
  std::vector<int64_t> result(DDim::kMaxRank);
S
sneaxiy 已提交
53
  dynamic_dim_assign(ddim.Get(), result.data(), ddim.size());
S
sneaxiy 已提交
54
  result.resize(ddim.size());
F
fengjiayi 已提交
55 56 57
  return result;
}

C
chengduoZH 已提交
58 59 60
// NOTE: framework::vectorize converts to type int64_t
//       which does not fit cudnn inputs.
std::vector<int> vectorize2int(const DDim& ddim) {
S
sneaxiy 已提交
61
  std::vector<int> result(DDim::kMaxRank);
S
sneaxiy 已提交
62
  dynamic_dim_assign(ddim.Get(), result.data(), ddim.size());
S
sneaxiy 已提交
63
  result.resize(ddim.size());
C
chengduoZH 已提交
64 65 66
  return result;
}

S
sneaxiy 已提交
67
struct ProductVisitor {
F
fengjiayi 已提交
68
  template <int D>
S
sneaxiy 已提交
69
  inline int64_t operator()(const Dim<D>& dim) {
F
fengjiayi 已提交
70
    return product(dim);
F
fengjiayi 已提交
71
  }
F
fengjiayi 已提交
72 73
};

Q
qijun 已提交
74
int64_t product(const DDim& ddim) {
S
sneaxiy 已提交
75
  return ddim.apply_visitor(ProductVisitor());
F
fengjiayi 已提交
76 77
}

H
Hongyu Liu 已提交
78 79 80 81 82 83 84 85 86 87
bool contain_unknown_dim(const DDim& ddim) {
  for (int i = 0; i < ddim.size(); ++i) {
    if (ddim[i] < 0) {
      return true;
    }
  }

  return false;
}

88
DDim slice_ddim(const DDim& dim, int begin, int end) {
S
sneaxiy 已提交
89 90 91 92 93
  PADDLE_ENFORCE(begin >= 0 && end <= dim.size(),
                 "[begin(%d), end(%d)) must be inside [0, %d) in ddim slice.",
                 begin, end, dim.size());
  // Constructor of DDim would check whether end - begin is valid
  return DDim(dim.Get() + begin, end - begin);
S
sneaxiy 已提交
94
}
F
fengjiayi 已提交
95

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

S
sneaxiy 已提交
98
struct DDimPrinter {
F
fengjiayi 已提交
99
  std::ostream& os;
L
liaogang 已提交
100
  explicit DDimPrinter(std::ostream& os_) : os(os_) {}
F
fengjiayi 已提交
101

S
sneaxiy 已提交
102 103
  template <int D>
  void operator()(const Dim<D>& t) {
F
fengjiayi 已提交
104 105 106 107
    os << t;
  }
};

108
std::ostream& operator<<(std::ostream& os, const DDim& ddim) {
S
sneaxiy 已提交
109
  ddim.apply_visitor(DDimPrinter(os));
F
fengjiayi 已提交
110 111 112
  return os;
}

F
fengjiayi 已提交
113
DDim flatten_to_2d(const DDim& src, int num_col_dims) {
S
sneaxiy 已提交
114 115
  return DDim({product(slice_ddim(src, 0, num_col_dims)),
               product(slice_ddim(src, num_col_dims, src.size()))});
116 117
}

S
sneaxiy 已提交
118
DDim flatten_to_1d(const DDim& src) { return DDim({product(src)}); }
F
fengjiayi 已提交
119

W
wanghaoshuang 已提交
120
DDim stride(const DDim& ddim) {
S
sneaxiy 已提交
121 122
  DDim strides;
  strides.rank_ = ddim.size();
W
wanghaoshuang 已提交
123 124 125 126
  strides[ddim.size() - 1] = 1;
  for (int i = ddim.size() - 2; i >= 0; --i) {
    strides[i] = strides[i + 1] * ddim[i + 1];
  }
S
sneaxiy 已提交
127
  return strides;
W
wanghaoshuang 已提交
128
}
Y
Yancey1989 已提交
129

S
sneaxiy 已提交
130
DDim stride_numel(const DDim& ddim) {
S
sneaxiy 已提交
131 132
  DDim strides;
  strides.rank_ = ddim.size();
Y
Yancey1989 已提交
133 134 135 136
  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 已提交
137
  return strides;
Y
Yancey1989 已提交
138 139
}

140 141
}  // namespace framework
}  // namespace paddle