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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 namespace internal {
18 
19 template <typename Scalar, int Options>
21  enum {
23 
24  is_aligned = (((Options & DontAlign) == 0) && (
25 #if EIGEN_MAX_STATIC_ALIGN_BYTES > 0
27 #else
28  0
29 #endif
30  |
33 #else
34  0
35 #endif
36  )),
38  };
39 
40  public:
41  enum { ret = packet_access_bit };
42 };
43 
44 template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
45 struct traits<Tensor<Scalar_, NumIndices_, Options_, IndexType_> > {
46  typedef Scalar_ Scalar;
47  typedef Dense StorageKind;
48  typedef IndexType_ Index;
49  static constexpr int NumDimensions = NumIndices_;
50  static constexpr int Layout = Options_ & RowMajor ? RowMajor : ColMajor;
51  enum {
52  Options = Options_,
54  };
55  template <typename T>
56  struct MakePointer {
57  typedef T* Type;
58  };
60 };
61 
62 template <typename Scalar_, typename Dimensions, int Options_, typename IndexType_>
63 struct traits<TensorFixedSize<Scalar_, Dimensions, Options_, IndexType_> > {
64  typedef Scalar_ Scalar;
65  typedef Dense StorageKind;
66  typedef IndexType_ Index;
67  static constexpr int NumDimensions = array_size<Dimensions>::value;
68  static constexpr int Layout = Options_ & RowMajor ? RowMajor : ColMajor;
69  enum {
70  Options = Options_,
72  };
73  template <typename T>
74  struct MakePointer {
75  typedef T* Type;
76  };
78 };
79 
80 template <typename PlainObjectType, int Options_, template <class> class MakePointer_>
81 struct traits<TensorMap<PlainObjectType, Options_, MakePointer_> > : public traits<PlainObjectType> {
83  typedef typename BaseTraits::Scalar Scalar;
84  typedef typename BaseTraits::StorageKind StorageKind;
85  typedef typename BaseTraits::Index Index;
86  static constexpr int NumDimensions = BaseTraits::NumDimensions;
87  static constexpr int Layout = BaseTraits::Layout;
88  enum { Options = Options_, Flags = BaseTraits::Flags };
89  template <class T>
90  struct MakePointer {
91  // Intermediate typedef to workaround MSVC issue.
92  typedef MakePointer_<T> MakePointerT;
93  typedef typename MakePointerT::Type Type;
94  };
96 };
97 
98 template <typename PlainObjectType>
99 struct traits<TensorRef<PlainObjectType> > : public traits<PlainObjectType> {
101  typedef typename BaseTraits::Scalar Scalar;
102  typedef typename BaseTraits::StorageKind StorageKind;
103  typedef typename BaseTraits::Index Index;
104  static constexpr int NumDimensions = BaseTraits::NumDimensions;
105  static constexpr int Layout = BaseTraits::Layout;
106  enum { Options = BaseTraits::Options, Flags = BaseTraits::Flags };
107  typedef typename BaseTraits::PointerType PointerType;
108 };
109 
110 template <typename Scalar_, int NumIndices_, int Options, typename IndexType_>
111 struct eval<Tensor<Scalar_, NumIndices_, Options, IndexType_>, Eigen::Dense> {
113 };
114 
115 template <typename Scalar_, int NumIndices_, int Options, typename IndexType_>
116 struct eval<const Tensor<Scalar_, NumIndices_, Options, IndexType_>, Eigen::Dense> {
118 };
119 
120 template <typename Scalar_, typename Dimensions, int Options, typename IndexType_>
121 struct eval<TensorFixedSize<Scalar_, Dimensions, Options, IndexType_>, Eigen::Dense> {
123 };
124 
125 template <typename Scalar_, typename Dimensions, int Options, typename IndexType_>
126 struct eval<const TensorFixedSize<Scalar_, Dimensions, Options, IndexType_>, Eigen::Dense> {
128 };
129 
130 template <typename PlainObjectType, int Options, template <class> class MakePointer>
131 struct eval<TensorMap<PlainObjectType, Options, MakePointer>, Eigen::Dense> {
133 };
134 
135 template <typename PlainObjectType, int Options, template <class> class MakePointer>
136 struct eval<const TensorMap<PlainObjectType, Options, MakePointer>, Eigen::Dense> {
138 };
139 
140 template <typename PlainObjectType>
141 struct eval<TensorRef<PlainObjectType>, Eigen::Dense> {
143 };
144 
145 template <typename PlainObjectType>
146 struct eval<const TensorRef<PlainObjectType>, Eigen::Dense> {
148 };
149 
150 // TODO nested<> does not exist anymore in Eigen/Core, and it thus has to be removed in favor of ref_selector.
151 template <typename T, int n = 1, typename PlainObject = void>
152 struct nested {
153  typedef typename ref_selector<T>::type type;
154 };
155 
156 template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
157 struct nested<Tensor<Scalar_, NumIndices_, Options_, IndexType_> > {
159 };
160 
161 template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
162 struct nested<const Tensor<Scalar_, NumIndices_, Options_, IndexType_> > {
164 };
165 
166 template <typename Scalar_, typename Dimensions, int Options, typename IndexType_>
167 struct nested<TensorFixedSize<Scalar_, Dimensions, Options, IndexType_> > {
169 };
170 
171 template <typename Scalar_, typename Dimensions, int Options, typename IndexType_>
172 struct nested<const TensorFixedSize<Scalar_, Dimensions, Options, IndexType_> > {
174 };
175 
176 template <typename PlainObjectType>
177 struct nested<TensorRef<PlainObjectType> > {
179 };
180 
181 template <typename PlainObjectType>
182 struct nested<const TensorRef<PlainObjectType> > {
184 };
185 
186 } // end namespace internal
187 
188 // Convolutional layers take in an input tensor of shape (D, R, C, B), or (D, C,
189 // R, B), and convolve it with a set of filters, which can also be presented as
190 // a tensor (D, K, K, M), where M is the number of filters, K is the filter
191 // size, and each 3-dimensional tensor of size (D, K, K) is a filter. For
192 // simplicity we assume that we always use square filters (which is usually the
193 // case in images), hence the two Ks in the tensor dimension. It also takes in
194 // a few additional parameters:
195 // Stride (S): The convolution stride is the offset between locations where we
196 // apply the filters. A larger stride means that the output will be
197 // spatially smaller.
198 // Padding (P): The padding we apply to the input tensor along the R and C
199 // dimensions. This is usually used to make sure that the spatial
200 // dimensions of the output matches our intention.
201 //
202 // Two types of padding are often used:
203 // SAME: The pad value is computed so that the output will have size
204 // R/S and C/S.
205 // VALID: no padding is carried out.
206 // When we do padding, the padded values at the padded locations are usually
207 // zero.
208 //
209 // The output dimensions for convolution, when given all the parameters above,
210 // are as follows:
211 // When Padding = SAME: the output size is (B, R', C', M), where
212 // R' = ceil(float(R) / float(S))
213 // C' = ceil(float(C) / float(S))
214 // where ceil is the ceiling function. The input tensor is padded with 0 as
215 // needed. The number of padded rows and columns are computed as:
216 // Pr = ((R' - 1) * S + K - R) / 2
217 // Pc = ((C' - 1) * S + K - C) / 2
218 // when the stride is 1, we have the simplified case R'=R, C'=C, Pr=Pc=(K-1)/2.
219 // This is where SAME comes from - the output has the same size as the input has.
220 // When Padding = VALID: the output size is computed as
221 // R' = ceil(float(R - K + 1) / float(S))
222 // C' = ceil(float(C - K + 1) / float(S))
223 // and the number of padded rows and columns are computed in the same way as in
224 // the SAME case.
225 // When the stride is 1, we have the simplified case R'=R-K+1, C'=C-K+1, Pr=0,
226 // Pc=0.
228 
229 } // end namespace Eigen
230 
231 #endif // EIGEN_CXX11_TENSOR_TENSOR_TRAITS_H
#define EIGEN_MAX_ALIGN_BYTES
Definition: ConfigureVectorization.h:163
#define EIGEN_DEVICE_REF
Definition: TensorMacros.h:34
SCALAR Scalar
Definition: bench_gemm.cpp:45
The fixed sized version of the tensor class.
Definition: TensorFixedSize.h:30
A tensor expression mapping an existing array of data.
Definition: TensorMap.h:33
A reference to a tensor expression The expression will be evaluated lazily (as much as possible).
Definition: TensorRef.h:114
The tensor class.
Definition: Tensor.h:68
Definition: TensorTraits.h:20
@ ret
Definition: TensorTraits.h:41
@ is_dynamic_size_storage
Definition: TensorTraits.h:22
@ is_aligned
Definition: TensorTraits.h:24
@ packet_access_bit
Definition: TensorTraits.h:37
@ ColMajor
Definition: Constants.h:318
@ DontAlign
Definition: Constants.h:324
@ RowMajor
Definition: Constants.h:320
const unsigned int PacketAccessBit
Definition: Constants.h:97
const unsigned int LvalueBit
Definition: Constants.h:148
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
PaddingType
Definition: TensorTraits.h:227
@ PADDING_VALID
Definition: TensorTraits.h:227
@ PADDING_SAME
Definition: TensorTraits.h:227
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
Definition: Eigen_Colamd.h:49
Type
Type of JSON value.
Definition: rapidjson.h:513
Definition: Constants.h:519
Definition: TensorForwardDeclarations.h:25
Definition: Meta.h:305
const TensorFixedSize< Scalar_, Dimensions, Options, IndexType_ > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:122
const TensorMap< PlainObjectType, Options, MakePointer > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:132
const TensorRef< PlainObjectType > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:142
const Tensor< Scalar_, NumIndices_, Options, IndexType_ > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:112
const TensorFixedSize< Scalar_, Dimensions, Options, IndexType_ > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:127
const TensorMap< PlainObjectType, Options, MakePointer > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:137
const TensorRef< PlainObjectType > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:147
const Tensor< Scalar_, NumIndices_, Options, IndexType_ > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:117
Definition: XprHelper.h:427
Definition: Meta.h:244
const TensorFixedSize< Scalar_, Dimensions, Options, IndexType_ > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:168
const TensorRef< PlainObjectType > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:178
const Tensor< Scalar_, NumIndices_, Options_, IndexType_ > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:158
const TensorFixedSize< Scalar_, Dimensions, Options, IndexType_ > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:173
const TensorRef< PlainObjectType > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:183
const Tensor< Scalar_, NumIndices_, Options_, IndexType_ > EIGEN_DEVICE_REF type
Definition: TensorTraits.h:163
Definition: TensorTraits.h:152
ref_selector< T >::type type
Definition: TensorTraits.h:153
Definition: GenericPacketMath.h:108
std::conditional_t< bool(traits< T >::Flags &NestByRefBit), T const &, const T > type
Definition: XprHelper.h:507
MakePointer< Scalar >::Type PointerType
Definition: TensorTraits.h:77
traits< PlainObjectType > BaseTraits
Definition: TensorTraits.h:82
BaseTraits::StorageKind StorageKind
Definition: TensorTraits.h:84
MakePointer< Scalar >::Type PointerType
Definition: TensorTraits.h:95
BaseTraits::StorageKind StorageKind
Definition: TensorTraits.h:102
traits< PlainObjectType > BaseTraits
Definition: TensorTraits.h:100
BaseTraits::Index Index
Definition: TensorTraits.h:103
BaseTraits::PointerType PointerType
Definition: TensorTraits.h:107
BaseTraits::Scalar Scalar
Definition: TensorTraits.h:101
MakePointer< Scalar >::Type PointerType
Definition: TensorTraits.h:59
Definition: ForwardDeclarations.h:21