ddim.h 3.3 KB
Newer Older
Q
qijun 已提交
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. */

F
fengjiayi 已提交
15 16 17 18 19
#pragma once

#include <initializer_list>
#include <stdexcept>
#include <vector>
Y
Yi Wang 已提交
20 21 22
#include "paddle/fluid/framework/dim.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/variant.h"
F
fengjiayi 已提交
23

24 25
namespace paddle {
namespace framework {
F
fengjiayi 已提交
26 27 28 29 30 31 32

/**
 * \brief A dynamically sized dimension.
 *
 * The number of dimensions must be between [1, 9].
 */
struct DDim {
L
liaogang 已提交
33 34 35
  typedef boost::variant<Dim<1>, Dim<2>, Dim<3>, Dim<4>, Dim<5>, Dim<6>, Dim<7>,
                         Dim<8>, Dim<9>>
      DDimVar;
F
fengjiayi 已提交
36 37 38 39 40
  DDimVar var;

  DDim() : var(Dim<1>()) {}

  template <int D>
L
liaogang 已提交
41
  explicit DDim(const Dim<D>& in) : var(in) {}
F
fengjiayi 已提交
42

Q
qijun 已提交
43
  /*implicit*/ DDim(std::initializer_list<int64_t> init_list);
F
fengjiayi 已提交
44 45 46 47 48 49 50

  template <int D>
  DDim& operator=(const Dim<D>& in) {
    var = in;
    return *this;
  }

Q
qijun 已提交
51 52
  int64_t& operator[](int idx);
  int64_t operator[](int idx) const;
F
fengjiayi 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

  template <typename Visitor>
  typename Visitor::result_type apply_visitor(Visitor& visitor) {
    return var.apply_visitor(visitor);
  }

  template <typename Visitor>
  typename Visitor::result_type apply_visitor(Visitor& visitor) const {
    return var.apply_visitor(visitor);
  }

  DDimVar getVar() { return var; }

  bool operator==(DDim d) const;

  bool operator!=(DDim d) const;

  DDim operator+(DDim d) const;

  DDim operator*(DDim d) const;
73

74
  int size() const;
F
fengjiayi 已提交
75 76 77
};

/**
Q
qijun 已提交
78
 * \brief Make a DDim from std::vector<int64_t>
F
fengjiayi 已提交
79 80 81
 *
 * \param dims An vector of ints. Must be sized between [1, 9]
 */
Q
qijun 已提交
82
DDim make_ddim(const std::vector<int64_t>& dims);
F
fengjiayi 已提交
83

Y
Yu Yang 已提交
84 85
DDim make_ddim(const std::vector<int>& dims);

F
fengjiayi 已提交
86 87 88 89 90 91
/**
 * \brief Make a DDim from an initializer list
 *
 * \param dims An initializer list of ints. Must be sized between [1, 9]
 *
 */
Q
qijun 已提交
92
DDim make_ddim(std::initializer_list<int64_t> dims);
F
fengjiayi 已提交
93

Q
qijun 已提交
94
int64_t get(const DDim& dim, int idx);
F
fengjiayi 已提交
95 96
void set(DDim& dim, int idx, int val);

Q
qijun 已提交
97
std::vector<int64_t> vectorize(const DDim& ddim);
C
chengduoZH 已提交
98
std::vector<int> vectorize2int(const DDim& ddim);
F
fengjiayi 已提交
99

Q
qijun 已提交
100
int64_t product(const DDim& ddim);
F
fengjiayi 已提交
101

F
fengjiayi 已提交
102 103 104 105 106 107 108
/**
 * \brief Slice a ddim
 *
 * Slice dim with [begin, end).
 * e.g.  DDim d = make_ddim({1,2,3,4,5});
 *       slice_ddim(d, 1, 3); ====> {2,3}
 */
109 110
DDim slice_ddim(const DDim& dim, int begin, int end);

F
fengjiayi 已提交
111 112 113 114 115 116 117 118
/**
 * \brief What is the length of this dimension?
 *
 * \param Dynamic dimension to inspect
 */

int arity(const DDim& ddim);

119
std::ostream& operator<<(std::ostream&, const DDim&);
F
fengjiayi 已提交
120

F
fengjiayi 已提交
121
// Reshape a tensor to a matrix. The matrix's first dimension(column length)
F
test  
fengjiayi 已提交
122
// will be the product of tensor's first `num_col_dims` dimensions.
F
fengjiayi 已提交
123
DDim flatten_to_2d(const DDim& src, int num_col_dims);
124

F
fengjiayi 已提交
125 126
DDim flatten_to_1d(const DDim& src);

W
wanghaoshuang 已提交
127
DDim stride(const DDim& ddim);
128 129
}  // namespace framework
}  // namespace paddle
F
fengjiayi 已提交
130 131 132 133

namespace boost {

template <typename T>
134
T get(const paddle::framework::DDim& in) {
F
fengjiayi 已提交
135 136 137 138
  return boost::get<T>(in.var);
}

}  // namespace boost