TensorTraits.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_TRAITS_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_TRAITS_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 namespace internal {
17 
18 
19 template<typename Scalar, int Options>
20 class compute_tensor_flags
21 {
22  enum {
23  is_dynamic_size_storage = 1,
24 
25  is_aligned =
26  (
27  ((Options&DontAlign)==0) && (
28 #if EIGEN_MAX_STATIC_ALIGN_BYTES>0
29  (!is_dynamic_size_storage)
30 #else
31  0
32 #endif
33  |
34 #if EIGEN_MAX_ALIGN_BYTES>0
35  is_dynamic_size_storage
36 #else
37  0
38 #endif
39  )
40  ),
41  packet_access_bit = packet_traits<Scalar>::Vectorizable && is_aligned ? PacketAccessBit : 0
42  };
43 
44  public:
45  enum { ret = packet_access_bit };
46 };
47 
48 
49 template<typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
50 struct traits<Tensor<Scalar_, NumIndices_, Options_, IndexType_> >
51 {
52  typedef Scalar_ Scalar;
53  typedef Dense StorageKind;
54  typedef IndexType_ Index;
55  static constexpr int NumDimensions = NumIndices_;
56  static constexpr int Layout = Options_ & RowMajor ? RowMajor : ColMajor;
57  enum {
58  Options = Options_,
59  Flags = compute_tensor_flags<Scalar_, Options_>::ret | (is_const<Scalar_>::value ? 0 : LvalueBit)
60  };
61  template <typename T> struct MakePointer {
62  typedef T* Type;
63  };
64  typedef typename MakePointer<Scalar>::Type PointerType;
65 };
66 
67 
68 template<typename Scalar_, typename Dimensions, int Options_, typename IndexType_>
69 struct traits<TensorFixedSize<Scalar_, Dimensions, Options_, IndexType_> >
70 {
71  typedef Scalar_ Scalar;
72  typedef Dense StorageKind;
73  typedef IndexType_ Index;
74  static constexpr int NumDimensions = array_size<Dimensions>::value;
75  static constexpr int Layout = Options_ & RowMajor ? RowMajor : ColMajor;
76  enum {
77  Options = Options_,
78  Flags = compute_tensor_flags<Scalar_, Options_>::ret | (is_const<Scalar_>::value ? 0: LvalueBit)
79  };
80  template <typename T> struct MakePointer {
81  typedef T* Type;
82  };
83  typedef typename MakePointer<Scalar>::Type PointerType;
84 };
85 
86 
87 template<typename PlainObjectType, int Options_, template <class> class MakePointer_>
88 struct traits<TensorMap<PlainObjectType, Options_, MakePointer_> >
89  : public traits<PlainObjectType>
90 {
91  typedef traits<PlainObjectType> BaseTraits;
92  typedef typename BaseTraits::Scalar Scalar;
93  typedef typename BaseTraits::StorageKind StorageKind;
94  typedef typename BaseTraits::Index Index;
95  static constexpr int NumDimensions = BaseTraits::NumDimensions;
96  static constexpr int Layout = BaseTraits::Layout;
97  enum {
98  Options = Options_,
99  Flags = BaseTraits::Flags
100  };
101  template <class T> struct MakePointer {
102  // Intermediate typedef to workaround MSVC issue.
103  typedef MakePointer_<T> MakePointerT;
104  typedef typename MakePointerT::Type Type;
105  };
106  typedef typename MakePointer<Scalar>::Type PointerType;
107 };
108 
109 template<typename PlainObjectType>
110 struct traits<TensorRef<PlainObjectType> >
111  : public traits<PlainObjectType>
112 {
113  typedef traits<PlainObjectType> BaseTraits;
114  typedef typename BaseTraits::Scalar Scalar;
115  typedef typename BaseTraits::StorageKind StorageKind;
116  typedef typename BaseTraits::Index Index;
117  static constexpr int NumDimensions = BaseTraits::NumDimensions;
118  static constexpr int Layout = BaseTraits::Layout;
119  enum {
120  Options = BaseTraits::Options,
121  Flags = BaseTraits::Flags
122  };
123  typedef typename BaseTraits::PointerType PointerType;
124 };
125 
126 
127 template<typename Scalar_, int NumIndices_, int Options, typename IndexType_>
128 struct eval<Tensor<Scalar_, NumIndices_, Options, IndexType_>, Eigen::Dense>
129 {
130  typedef const Tensor<Scalar_, NumIndices_, Options, IndexType_>EIGEN_DEVICE_REF type;
131 };
132 
133 template<typename Scalar_, int NumIndices_, int Options, typename IndexType_>
134 struct eval<const Tensor<Scalar_, NumIndices_, Options, IndexType_>, Eigen::Dense>
135 {
136  typedef const Tensor<Scalar_, NumIndices_, Options, IndexType_>EIGEN_DEVICE_REF type;
137 };
138 
139 template<typename Scalar_, typename Dimensions, int Options, typename IndexType_>
140 struct eval<TensorFixedSize<Scalar_, Dimensions, Options, IndexType_>, Eigen::Dense>
141 {
142  typedef const TensorFixedSize<Scalar_, Dimensions, Options, IndexType_>EIGEN_DEVICE_REF type;
143 };
144 
145 template<typename Scalar_, typename Dimensions, int Options, typename IndexType_>
146 struct eval<const TensorFixedSize<Scalar_, Dimensions, Options, IndexType_>, Eigen::Dense>
147 {
148  typedef const TensorFixedSize<Scalar_, Dimensions, Options, IndexType_>EIGEN_DEVICE_REF type;
149 };
150 
151 template<typename PlainObjectType, int Options, template <class> class MakePointer>
152 struct eval<TensorMap<PlainObjectType, Options, MakePointer>, Eigen::Dense>
153 {
154  typedef const TensorMap<PlainObjectType, Options, MakePointer>EIGEN_DEVICE_REF type;
155 };
156 
157 template<typename PlainObjectType, int Options, template <class> class MakePointer>
158 struct eval<const TensorMap<PlainObjectType, Options, MakePointer>, Eigen::Dense>
159 {
160  typedef const TensorMap<PlainObjectType, Options, MakePointer>EIGEN_DEVICE_REF type;
161 };
162 
163 template<typename PlainObjectType>
164 struct eval<TensorRef<PlainObjectType>, Eigen::Dense>
165 {
166  typedef const TensorRef<PlainObjectType>EIGEN_DEVICE_REF type;
167 };
168 
169 template<typename PlainObjectType>
170 struct eval<const TensorRef<PlainObjectType>, Eigen::Dense>
171 {
172  typedef const TensorRef<PlainObjectType>EIGEN_DEVICE_REF type;
173 };
174 
175 // TODO nested<> does not exist anymore in Eigen/Core, and it thus has to be removed in favor of ref_selector.
176 template<typename T, int n=1, typename PlainObject = void> struct nested
177 {
178  typedef typename ref_selector<T>::type type;
179 };
180 
181 template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
182 struct nested<Tensor<Scalar_, NumIndices_, Options_, IndexType_> >
183 {
184  typedef const Tensor<Scalar_, NumIndices_, Options_, IndexType_>EIGEN_DEVICE_REF type;
185 };
186 
187 template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
188 struct nested<const Tensor<Scalar_, NumIndices_, Options_, IndexType_> >
189 {
190  typedef const Tensor<Scalar_, NumIndices_, Options_, IndexType_>EIGEN_DEVICE_REF type;
191 };
192 
193 template <typename Scalar_, typename Dimensions, int Options, typename IndexType_>
194 struct nested<TensorFixedSize<Scalar_, Dimensions, Options, IndexType_> >
195 {
196  typedef const TensorFixedSize<Scalar_, Dimensions, Options, IndexType_>EIGEN_DEVICE_REF type;
197 };
198 
199 template <typename Scalar_, typename Dimensions, int Options, typename IndexType_>
200 struct nested<const TensorFixedSize<Scalar_, Dimensions, Options, IndexType_> >
201 {
202  typedef const TensorFixedSize<Scalar_, Dimensions, Options, IndexType_>EIGEN_DEVICE_REF type;
203 };
204 
205 
206 template <typename PlainObjectType>
207 struct nested<TensorRef<PlainObjectType> >
208 {
209  typedef const TensorRef<PlainObjectType>EIGEN_DEVICE_REF type;
210 };
211 
212 template <typename PlainObjectType>
213 struct nested<const TensorRef<PlainObjectType> >
214 {
215  typedef const TensorRef<PlainObjectType>EIGEN_DEVICE_REF type;
216 };
217 
218 } // end namespace internal
219 
220 // Convolutional layers take in an input tensor of shape (D, R, C, B), or (D, C,
221 // R, B), and convolve it with a set of filters, which can also be presented as
222 // a tensor (D, K, K, M), where M is the number of filters, K is the filter
223 // size, and each 3-dimensional tensor of size (D, K, K) is a filter. For
224 // simplicity we assume that we always use square filters (which is usually the
225 // case in images), hence the two Ks in the tensor dimension. It also takes in
226 // a few additional parameters:
227 // Stride (S): The convolution stride is the offset between locations where we
228 // apply the filters. A larger stride means that the output will be
229 // spatially smaller.
230 // Padding (P): The padding we apply to the input tensor along the R and C
231 // dimensions. This is usually used to make sure that the spatial
232 // dimensions of the output matches our intention.
233 //
234 // Two types of padding are often used:
235 // SAME: The pad value is computed so that the output will have size
236 // R/S and C/S.
237 // VALID: no padding is carried out.
238 // When we do padding, the padded values at the padded locations are usually
239 // zero.
240 //
241 // The output dimensions for convolution, when given all the parameters above,
242 // are as follows:
243 // When Padding = SAME: the output size is (B, R', C', M), where
244 // R' = ceil(float(R) / float(S))
245 // C' = ceil(float(C) / float(S))
246 // where ceil is the ceiling function. The input tensor is padded with 0 as
247 // needed. The number of padded rows and columns are computed as:
248 // Pr = ((R' - 1) * S + K - R) / 2
249 // Pc = ((C' - 1) * S + K - C) / 2
250 // when the stride is 1, we have the simplified case R'=R, C'=C, Pr=Pc=(K-1)/2.
251 // This is where SAME comes from - the output has the same size as the input has.
252 // When Padding = VALID: the output size is computed as
253 // R' = ceil(float(R - K + 1) / float(S))
254 // C' = ceil(float(C - K + 1) / float(S))
255 // and the number of padded rows and columns are computed in the same way as in
256 // the SAME case.
257 // When the stride is 1, we have the simplified case R'=R-K+1, C'=C-K+1, Pr=0,
258 // Pc=0.
261  PADDING_SAME = 2
262 };
263 
264 } // end namespace Eigen
265 
266 #endif // EIGEN_CXX11_TENSOR_TENSOR_TRAITS_H
#define EIGEN_DEVICE_REF
Definition: TensorMacros.h:36
const unsigned int PacketAccessBit
const unsigned int LvalueBit
Type
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
@ PADDING_VALID
Definition: TensorTraits.h:260
@ PADDING_SAME
Definition: TensorTraits.h:261
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index