Loading...
Searching...
No Matches
TensorPadding.h
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_PADDING_H
11#define EIGEN_CXX11_TENSOR_TENSOR_PADDING_H
12
13namespace Eigen {
14
15namespace internal {
16template<typename PaddingDimensions, typename XprType>
17struct traits<TensorPaddingOp<PaddingDimensions, XprType> > : public traits<XprType>
18{
19 typedef typename XprType::Scalar Scalar;
20 typedef traits<XprType> XprTraits;
21 typedef typename XprTraits::StorageKind StorageKind;
22 typedef typename XprTraits::Index Index;
23 typedef typename XprType::Nested Nested;
24 typedef typename remove_reference<Nested>::type _Nested;
25 static const int NumDimensions = XprTraits::NumDimensions;
26 static const int Layout = XprTraits::Layout;
27};
28
29template<typename PaddingDimensions, typename XprType>
30struct eval<TensorPaddingOp<PaddingDimensions, XprType>, Eigen::Dense>
31{
32 typedef const TensorPaddingOp<PaddingDimensions, XprType>& type;
33};
34
35template<typename PaddingDimensions, typename XprType>
36struct nested<TensorPaddingOp<PaddingDimensions, XprType>, 1, typename eval<TensorPaddingOp<PaddingDimensions, XprType> >::type>
37{
38 typedef TensorPaddingOp<PaddingDimensions, XprType> type;
39};
40
41} // end namespace internal
42
50template <typename PaddingDimensions, typename XprType>
51class TensorPaddingOp : public TensorBase<TensorPaddingOp<PaddingDimensions, XprType>, ReadOnlyAccessors> {
52 public:
53 typedef typename Eigen::internal::traits<TensorPaddingOp>::Scalar Scalar;
54 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
55 typedef typename XprType::CoeffReturnType CoeffReturnType;
56 typedef typename Eigen::internal::nested<TensorPaddingOp>::type Nested;
57 typedef typename Eigen::internal::traits<TensorPaddingOp>::StorageKind StorageKind;
58 typedef typename Eigen::internal::traits<TensorPaddingOp>::Index Index;
59
60 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPaddingOp(const XprType& expr, const PaddingDimensions& padding_dims, const Scalar padding_value)
61 : m_xpr(expr), m_padding_dims(padding_dims), m_padding_value(padding_value) {}
62
63 EIGEN_DEVICE_FUNC
64 const PaddingDimensions& padding() const { return m_padding_dims; }
65 EIGEN_DEVICE_FUNC
66 Scalar padding_value() const { return m_padding_value; }
67
68 EIGEN_DEVICE_FUNC
69 const typename internal::remove_all<typename XprType::Nested>::type&
70 expression() const { return m_xpr; }
71
72 protected:
73 typename XprType::Nested m_xpr;
74 const PaddingDimensions m_padding_dims;
75 const Scalar m_padding_value;
76};
77
78
79// Eval as rvalue
80template<typename PaddingDimensions, typename ArgType, typename Device>
81struct TensorEvaluator<const TensorPaddingOp<PaddingDimensions, ArgType>, Device>
82{
84 typedef typename XprType::Index Index;
85 static const int NumDims = internal::array_size<PaddingDimensions>::value;
86 typedef DSizes<Index, NumDims> Dimensions;
87 typedef typename XprType::Scalar Scalar;
88 typedef typename XprType::CoeffReturnType CoeffReturnType;
89 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
90 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
91
92 enum {
93 IsAligned = true,
94 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
95 Layout = TensorEvaluator<ArgType, Device>::Layout,
96 CoordAccess = true,
97 RawAccess = false
98 };
99
100 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
101 : m_impl(op.expression(), device), m_padding(op.padding()), m_paddingValue(op.padding_value())
102 {
103 // The padding op doesn't change the rank of the tensor. Directly padding a scalar would lead
104 // to a vector, which doesn't make sense. Instead one should reshape the scalar into a vector
105 // of 1 element first and then pad.
106 EIGEN_STATIC_ASSERT((NumDims > 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
107
108 // Compute dimensions
109 m_dimensions = m_impl.dimensions();
110 for (int i = 0; i < NumDims; ++i) {
111 m_dimensions[i] += m_padding[i].first + m_padding[i].second;
112 }
113 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
114 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
115 m_inputStrides[0] = 1;
116 m_outputStrides[0] = 1;
117 for (int i = 1; i < NumDims; ++i) {
118 m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
119 m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
120 }
121 m_outputStrides[NumDims] = m_outputStrides[NumDims-1] * m_dimensions[NumDims-1];
122 } else {
123 m_inputStrides[NumDims - 1] = 1;
124 m_outputStrides[NumDims] = 1;
125 for (int i = NumDims - 2; i >= 0; --i) {
126 m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
127 m_outputStrides[i+1] = m_outputStrides[i+2] * m_dimensions[i+1];
128 }
129 m_outputStrides[0] = m_outputStrides[1] * m_dimensions[0];
130 }
131 }
132
133 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
134
135 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar*) {
136 m_impl.evalSubExprsIfNeeded(NULL);
137 return true;
138 }
139 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
140 m_impl.cleanup();
141 }
142
143 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
144 {
145 eigen_assert(index < dimensions().TotalSize());
146 Index inputIndex = 0;
147 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
148 for (int i = NumDims - 1; i > 0; --i) {
149 const Index idx = index / m_outputStrides[i];
150 if (isPaddingAtIndexForDim(idx, i)) {
151 return m_paddingValue;
152 }
153 inputIndex += (idx - m_padding[i].first) * m_inputStrides[i];
154 index -= idx * m_outputStrides[i];
155 }
156 if (isPaddingAtIndexForDim(index, 0)) {
157 return m_paddingValue;
158 }
159 inputIndex += (index - m_padding[0].first);
160 } else {
161 for (int i = 0; i < NumDims - 1; ++i) {
162 const Index idx = index / m_outputStrides[i+1];
163 if (isPaddingAtIndexForDim(idx, i)) {
164 return m_paddingValue;
165 }
166 inputIndex += (idx - m_padding[i].first) * m_inputStrides[i];
167 index -= idx * m_outputStrides[i+1];
168 }
169 if (isPaddingAtIndexForDim(index, NumDims-1)) {
170 return m_paddingValue;
171 }
172 inputIndex += (index - m_padding[NumDims-1].first);
173 }
174 return m_impl.coeff(inputIndex);
175 }
176
177 template<int LoadMode>
178 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
179 {
180 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
181 return packetColMajor(index);
182 }
183 return packetRowMajor(index);
184 }
185
186 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
187 TensorOpCost cost = m_impl.costPerCoeff(vectorized);
188 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
189 for (int i = 0; i < NumDims; ++i)
190 updateCostPerDimension(cost, i, i == 0);
191 } else {
192 for (int i = NumDims - 1; i >= 0; --i)
193 updateCostPerDimension(cost, i, i == NumDims - 1);
194 }
195 return cost;
196 }
197
198 EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
199
200 private:
201 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool isPaddingAtIndexForDim(
202 Index index, int dim_index) const {
203#if defined(EIGEN_HAS_INDEX_LIST)
204 return (!internal::index_pair_first_statically_eq<PaddingDimensions>(dim_index, 0) &&
205 index < m_padding[dim_index].first) ||
206 (!internal::index_pair_second_statically_eq<PaddingDimensions>(dim_index, 0) &&
207 index >= m_dimensions[dim_index] - m_padding[dim_index].second);
208#else
209 return (index < m_padding[dim_index].first) ||
210 (index >= m_dimensions[dim_index] - m_padding[dim_index].second);
211#endif
212 }
213
214 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool isLeftPaddingCompileTimeZero(
215 int dim_index) const {
216#if defined(EIGEN_HAS_INDEX_LIST)
217 return internal::index_pair_first_statically_eq<PaddingDimensions>(dim_index, 0);
218#else
219 EIGEN_UNUSED_VARIABLE(dim_index);
220 return false;
221#endif
222 }
223
224 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool isRightPaddingCompileTimeZero(
225 int dim_index) const {
226#if defined(EIGEN_HAS_INDEX_LIST)
227 return internal::index_pair_second_statically_eq<PaddingDimensions>(dim_index, 0);
228#else
229 EIGEN_UNUSED_VARIABLE(dim_index);
230 return false;
231#endif
232 }
233
234
235 void updateCostPerDimension(TensorOpCost& cost, int i, bool first) const {
236 const double in = static_cast<double>(m_impl.dimensions()[i]);
237 const double out = in + m_padding[i].first + m_padding[i].second;
238 if (out == 0)
239 return;
240 const double reduction = in / out;
241 cost *= reduction;
242 if (first) {
243 cost += TensorOpCost(0, 0, 2 * TensorOpCost::AddCost<Index>() +
244 reduction * (1 * TensorOpCost::AddCost<Index>()));
245 } else {
246 cost += TensorOpCost(0, 0, 2 * TensorOpCost::AddCost<Index>() +
247 2 * TensorOpCost::MulCost<Index>() +
248 reduction * (2 * TensorOpCost::MulCost<Index>() +
249 1 * TensorOpCost::DivCost<Index>()));
250 }
251 }
252
253 protected:
254
255 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packetColMajor(Index index) const
256 {
257 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
258 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
259
260 const Index initialIndex = index;
261 Index inputIndex = 0;
262 for (int i = NumDims - 1; i > 0; --i) {
263 const Index first = index;
264 const Index last = index + PacketSize - 1;
265 const Index lastPaddedLeft = m_padding[i].first * m_outputStrides[i];
266 const Index firstPaddedRight = (m_dimensions[i] - m_padding[i].second) * m_outputStrides[i];
267 const Index lastPaddedRight = m_outputStrides[i+1];
268
269 if (!isLeftPaddingCompileTimeZero(i) && last < lastPaddedLeft) {
270 // all the coefficient are in the padding zone.
271 return internal::pset1<PacketReturnType>(m_paddingValue);
272 }
273 else if (!isRightPaddingCompileTimeZero(i) && first >= firstPaddedRight && last < lastPaddedRight) {
274 // all the coefficient are in the padding zone.
275 return internal::pset1<PacketReturnType>(m_paddingValue);
276 }
277 else if ((isLeftPaddingCompileTimeZero(i) && isRightPaddingCompileTimeZero(i)) || (first >= lastPaddedLeft && last < firstPaddedRight)) {
278 // all the coefficient are between the 2 padding zones.
279 const Index idx = index / m_outputStrides[i];
280 inputIndex += (idx - m_padding[i].first) * m_inputStrides[i];
281 index -= idx * m_outputStrides[i];
282 }
283 else {
284 // Every other case
285 return packetWithPossibleZero(initialIndex);
286 }
287 }
288
289 const Index last = index + PacketSize - 1;
290 const Index first = index;
291 const Index lastPaddedLeft = m_padding[0].first;
292 const Index firstPaddedRight = (m_dimensions[0] - m_padding[0].second);
293 const Index lastPaddedRight = m_outputStrides[1];
294
295 if (!isLeftPaddingCompileTimeZero(0) && last < lastPaddedLeft) {
296 // all the coefficient are in the padding zone.
297 return internal::pset1<PacketReturnType>(m_paddingValue);
298 }
299 else if (!isRightPaddingCompileTimeZero(0) && first >= firstPaddedRight && last < lastPaddedRight) {
300 // all the coefficient are in the padding zone.
301 return internal::pset1<PacketReturnType>(m_paddingValue);
302 }
303 else if ((isLeftPaddingCompileTimeZero(0) && isRightPaddingCompileTimeZero(0)) || (first >= lastPaddedLeft && last < firstPaddedRight)) {
304 // all the coefficient are between the 2 padding zones.
305 inputIndex += (index - m_padding[0].first);
306 return m_impl.template packet<Unaligned>(inputIndex);
307 }
308 // Every other case
309 return packetWithPossibleZero(initialIndex);
310 }
311
312 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packetRowMajor(Index index) const
313 {
314 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
315 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
316
317 const Index initialIndex = index;
318 Index inputIndex = 0;
319
320 for (int i = 0; i < NumDims - 1; ++i) {
321 const Index first = index;
322 const Index last = index + PacketSize - 1;
323 const Index lastPaddedLeft = m_padding[i].first * m_outputStrides[i+1];
324 const Index firstPaddedRight = (m_dimensions[i] - m_padding[i].second) * m_outputStrides[i+1];
325 const Index lastPaddedRight = m_outputStrides[i];
326
327 if (!isLeftPaddingCompileTimeZero(i) && last < lastPaddedLeft) {
328 // all the coefficient are in the padding zone.
329 return internal::pset1<PacketReturnType>(m_paddingValue);
330 }
331 else if (!isRightPaddingCompileTimeZero(i) && first >= firstPaddedRight && last < lastPaddedRight) {
332 // all the coefficient are in the padding zone.
333 return internal::pset1<PacketReturnType>(m_paddingValue);
334 }
335 else if ((isLeftPaddingCompileTimeZero(i) && isRightPaddingCompileTimeZero(i)) || (first >= lastPaddedLeft && last < firstPaddedRight)) {
336 // all the coefficient are between the 2 padding zones.
337 const Index idx = index / m_outputStrides[i+1];
338 inputIndex += (idx - m_padding[i].first) * m_inputStrides[i];
339 index -= idx * m_outputStrides[i+1];
340 }
341 else {
342 // Every other case
343 return packetWithPossibleZero(initialIndex);
344 }
345 }
346
347 const Index last = index + PacketSize - 1;
348 const Index first = index;
349 const Index lastPaddedLeft = m_padding[NumDims-1].first;
350 const Index firstPaddedRight = (m_dimensions[NumDims-1] - m_padding[NumDims-1].second);
351 const Index lastPaddedRight = m_outputStrides[NumDims-1];
352
353 if (!isLeftPaddingCompileTimeZero(NumDims-1) && last < lastPaddedLeft) {
354 // all the coefficient are in the padding zone.
355 return internal::pset1<PacketReturnType>(m_paddingValue);
356 }
357 else if (!isRightPaddingCompileTimeZero(NumDims-1) && first >= firstPaddedRight && last < lastPaddedRight) {
358 // all the coefficient are in the padding zone.
359 return internal::pset1<PacketReturnType>(m_paddingValue);
360 }
361 else if ((isLeftPaddingCompileTimeZero(NumDims-1) && isRightPaddingCompileTimeZero(NumDims-1)) || (first >= lastPaddedLeft && last < firstPaddedRight)) {
362 // all the coefficient are between the 2 padding zones.
363 inputIndex += (index - m_padding[NumDims-1].first);
364 return m_impl.template packet<Unaligned>(inputIndex);
365 }
366 // Every other case
367 return packetWithPossibleZero(initialIndex);
368 }
369
370 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packetWithPossibleZero(Index index) const
371 {
372 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
373 for (int i = 0; i < PacketSize; ++i) {
374 values[i] = coeff(index+i);
375 }
376 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
377 return rslt;
378 }
379
380 Dimensions m_dimensions;
381 array<Index, NumDims+1> m_outputStrides;
382 array<Index, NumDims> m_inputStrides;
383 TensorEvaluator<ArgType, Device> m_impl;
384 PaddingDimensions m_padding;
385
386 Scalar m_paddingValue;
387};
388
389
390
391
392} // end namespace Eigen
393
394#endif // EIGEN_CXX11_TENSOR_TENSOR_PADDING_H
The tensor base class.
Definition TensorForwardDeclarations.h:29
Tensor padding class. At the moment only padding with a constant value is supported.
Definition TensorPadding.h:51
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:27
const Device & device() const
required by sycl in order to construct sycl buffer from raw pointer
Definition TensorEvaluator.h:112