$darkmode
Eigen-unsupported  5.0.1-dev
TensorVolumePatch.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 
4 #ifndef EIGEN_CXX11_TENSOR_TENSOR_VOLUME_PATCH_H
5 #define EIGEN_CXX11_TENSOR_TENSOR_VOLUME_PATCH_H
6 
7 // IWYU pragma: private
8 #include "./InternalHeaderCheck.h"
9 
10 namespace Eigen {
11 
12 namespace internal {
13 
14 template <DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
15 struct traits<TensorVolumePatchOp<Planes, Rows, Cols, XprType> > : public traits<XprType> {
16  typedef std::remove_const_t<typename XprType::Scalar> Scalar;
17  typedef traits<XprType> XprTraits;
18  typedef typename XprTraits::StorageKind StorageKind;
19  typedef typename XprTraits::Index Index;
20  typedef typename XprType::Nested Nested;
21  typedef std::remove_reference_t<Nested> Nested_;
22  static constexpr int NumDimensions = XprTraits::NumDimensions + 1;
23  static constexpr int Layout = XprTraits::Layout;
24  typedef typename XprTraits::PointerType PointerType;
25 };
26 
27 template <DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
28 struct eval<TensorVolumePatchOp<Planes, Rows, Cols, XprType>, Eigen::Dense> {
29  typedef const TensorVolumePatchOp<Planes, Rows, Cols, XprType>& type;
30 };
31 
32 template <DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
33 struct nested<TensorVolumePatchOp<Planes, Rows, Cols, XprType>, 1,
34  typename eval<TensorVolumePatchOp<Planes, Rows, Cols, XprType> >::type> {
35  typedef TensorVolumePatchOp<Planes, Rows, Cols, XprType> type;
36 };
37 
38 } // end namespace internal
39 
55 template <DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
56 class TensorVolumePatchOp : public TensorBase<TensorVolumePatchOp<Planes, Rows, Cols, XprType>, ReadOnlyAccessors> {
57  public:
58  typedef typename Eigen::internal::traits<TensorVolumePatchOp>::Scalar Scalar;
59  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
60  typedef typename XprType::CoeffReturnType CoeffReturnType;
61  typedef typename Eigen::internal::nested<TensorVolumePatchOp>::type Nested;
62  typedef typename Eigen::internal::traits<TensorVolumePatchOp>::StorageKind StorageKind;
63  typedef typename Eigen::internal::traits<TensorVolumePatchOp>::Index Index;
64 
65  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorVolumePatchOp(
66  const XprType& expr, DenseIndex patch_planes, DenseIndex patch_rows, DenseIndex patch_cols,
67  DenseIndex plane_strides, DenseIndex row_strides, DenseIndex col_strides, DenseIndex in_plane_strides,
68  DenseIndex in_row_strides, DenseIndex in_col_strides, DenseIndex plane_inflate_strides,
69  DenseIndex row_inflate_strides, DenseIndex col_inflate_strides, PaddingType padding_type, Scalar padding_value)
70  : m_xpr(expr),
71  m_patch_planes(patch_planes),
72  m_patch_rows(patch_rows),
73  m_patch_cols(patch_cols),
74  m_plane_strides(plane_strides),
75  m_row_strides(row_strides),
76  m_col_strides(col_strides),
77  m_in_plane_strides(in_plane_strides),
78  m_in_row_strides(in_row_strides),
79  m_in_col_strides(in_col_strides),
80  m_plane_inflate_strides(plane_inflate_strides),
81  m_row_inflate_strides(row_inflate_strides),
82  m_col_inflate_strides(col_inflate_strides),
83  m_padding_explicit(false),
84  m_padding_top_z(0),
85  m_padding_bottom_z(0),
86  m_padding_top(0),
87  m_padding_bottom(0),
88  m_padding_left(0),
89  m_padding_right(0),
90  m_padding_type(padding_type),
91  m_padding_value(padding_value) {}
92 
93  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorVolumePatchOp(
94  const XprType& expr, DenseIndex patch_planes, DenseIndex patch_rows, DenseIndex patch_cols,
95  DenseIndex plane_strides, DenseIndex row_strides, DenseIndex col_strides, DenseIndex in_plane_strides,
96  DenseIndex in_row_strides, DenseIndex in_col_strides, DenseIndex plane_inflate_strides,
97  DenseIndex row_inflate_strides, DenseIndex col_inflate_strides, DenseIndex padding_top_z,
98  DenseIndex padding_bottom_z, DenseIndex padding_top, DenseIndex padding_bottom, DenseIndex padding_left,
99  DenseIndex padding_right, Scalar padding_value)
100  : m_xpr(expr),
101  m_patch_planes(patch_planes),
102  m_patch_rows(patch_rows),
103  m_patch_cols(patch_cols),
104  m_plane_strides(plane_strides),
105  m_row_strides(row_strides),
106  m_col_strides(col_strides),
107  m_in_plane_strides(in_plane_strides),
108  m_in_row_strides(in_row_strides),
109  m_in_col_strides(in_col_strides),
110  m_plane_inflate_strides(plane_inflate_strides),
111  m_row_inflate_strides(row_inflate_strides),
112  m_col_inflate_strides(col_inflate_strides),
113  m_padding_explicit(true),
114  m_padding_top_z(padding_top_z),
115  m_padding_bottom_z(padding_bottom_z),
116  m_padding_top(padding_top),
117  m_padding_bottom(padding_bottom),
118  m_padding_left(padding_left),
119  m_padding_right(padding_right),
120  m_padding_type(PADDING_VALID),
121  m_padding_value(padding_value) {}
122 
123  EIGEN_DEVICE_FUNC DenseIndex patch_planes() const { return m_patch_planes; }
124  EIGEN_DEVICE_FUNC DenseIndex patch_rows() const { return m_patch_rows; }
125  EIGEN_DEVICE_FUNC DenseIndex patch_cols() const { return m_patch_cols; }
126  EIGEN_DEVICE_FUNC DenseIndex plane_strides() const { return m_plane_strides; }
127  EIGEN_DEVICE_FUNC DenseIndex row_strides() const { return m_row_strides; }
128  EIGEN_DEVICE_FUNC DenseIndex col_strides() const { return m_col_strides; }
129  EIGEN_DEVICE_FUNC DenseIndex in_plane_strides() const { return m_in_plane_strides; }
130  EIGEN_DEVICE_FUNC DenseIndex in_row_strides() const { return m_in_row_strides; }
131  EIGEN_DEVICE_FUNC DenseIndex in_col_strides() const { return m_in_col_strides; }
132  EIGEN_DEVICE_FUNC DenseIndex plane_inflate_strides() const { return m_plane_inflate_strides; }
133  EIGEN_DEVICE_FUNC DenseIndex row_inflate_strides() const { return m_row_inflate_strides; }
134  EIGEN_DEVICE_FUNC DenseIndex col_inflate_strides() const { return m_col_inflate_strides; }
135  EIGEN_DEVICE_FUNC bool padding_explicit() const { return m_padding_explicit; }
136  EIGEN_DEVICE_FUNC DenseIndex padding_top_z() const { return m_padding_top_z; }
137  EIGEN_DEVICE_FUNC DenseIndex padding_bottom_z() const { return m_padding_bottom_z; }
138  EIGEN_DEVICE_FUNC DenseIndex padding_top() const { return m_padding_top; }
139  EIGEN_DEVICE_FUNC DenseIndex padding_bottom() const { return m_padding_bottom; }
140  EIGEN_DEVICE_FUNC DenseIndex padding_left() const { return m_padding_left; }
141  EIGEN_DEVICE_FUNC DenseIndex padding_right() const { return m_padding_right; }
142  EIGEN_DEVICE_FUNC PaddingType padding_type() const { return m_padding_type; }
143  EIGEN_DEVICE_FUNC Scalar padding_value() const { return m_padding_value; }
144 
145  EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
146 
147  protected:
148  typename XprType::Nested m_xpr;
149  const DenseIndex m_patch_planes;
150  const DenseIndex m_patch_rows;
151  const DenseIndex m_patch_cols;
152  const DenseIndex m_plane_strides;
153  const DenseIndex m_row_strides;
154  const DenseIndex m_col_strides;
155  const DenseIndex m_in_plane_strides;
156  const DenseIndex m_in_row_strides;
157  const DenseIndex m_in_col_strides;
158  const DenseIndex m_plane_inflate_strides;
159  const DenseIndex m_row_inflate_strides;
160  const DenseIndex m_col_inflate_strides;
161  const bool m_padding_explicit;
162  const DenseIndex m_padding_top_z;
163  const DenseIndex m_padding_bottom_z;
164  const DenseIndex m_padding_top;
165  const DenseIndex m_padding_bottom;
166  const DenseIndex m_padding_left;
167  const DenseIndex m_padding_right;
168  const PaddingType m_padding_type;
169  const Scalar m_padding_value;
170 };
171 
172 // Eval as rvalue
173 template <DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename ArgType, typename Device>
174 struct TensorEvaluator<const TensorVolumePatchOp<Planes, Rows, Cols, ArgType>, Device> {
175  typedef TensorVolumePatchOp<Planes, Rows, Cols, ArgType> XprType;
176  typedef typename XprType::Index Index;
177  static constexpr int NumInputDims =
178  internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
179  static constexpr int NumDims = NumInputDims + 1;
180  typedef DSizes<Index, NumDims> Dimensions;
181  typedef std::remove_const_t<typename XprType::Scalar> Scalar;
182  typedef typename XprType::CoeffReturnType CoeffReturnType;
183  typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
184  static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
185  typedef StorageMemory<CoeffReturnType, Device> Storage;
186  typedef typename Storage::Type EvaluatorPointerType;
187 
188  static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
189  enum {
190  IsAligned = false,
191  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
192  BlockAccess = false,
193  PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
194  CoordAccess = false,
195  RawAccess = false
196  };
197 
198  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
199  typedef internal::TensorBlockNotImplemented TensorBlock;
200  //===--------------------------------------------------------------------===//
201 
202  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) : m_impl(op.expression(), device) {
203  EIGEN_STATIC_ASSERT((NumDims >= 5), YOU_MADE_A_PROGRAMMING_MISTAKE);
204 
205  m_paddingValue = op.padding_value();
206 
207  const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
208 
209  // Cache a few variables.
210  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
211  m_inputDepth = input_dims[0];
212  m_inputPlanes = input_dims[1];
213  m_inputRows = input_dims[2];
214  m_inputCols = input_dims[3];
215  } else {
216  m_inputDepth = input_dims[NumInputDims - 1];
217  m_inputPlanes = input_dims[NumInputDims - 2];
218  m_inputRows = input_dims[NumInputDims - 3];
219  m_inputCols = input_dims[NumInputDims - 4];
220  }
221 
222  m_plane_strides = op.plane_strides();
223  m_row_strides = op.row_strides();
224  m_col_strides = op.col_strides();
225 
226  // Input strides and effective input/patch size
227  m_in_plane_strides = op.in_plane_strides();
228  m_in_row_strides = op.in_row_strides();
229  m_in_col_strides = op.in_col_strides();
230  m_plane_inflate_strides = op.plane_inflate_strides();
231  m_row_inflate_strides = op.row_inflate_strides();
232  m_col_inflate_strides = op.col_inflate_strides();
233 
234  // The "effective" spatial size after inflating data with zeros.
235  m_input_planes_eff = (m_inputPlanes - 1) * m_plane_inflate_strides + 1;
236  m_input_rows_eff = (m_inputRows - 1) * m_row_inflate_strides + 1;
237  m_input_cols_eff = (m_inputCols - 1) * m_col_inflate_strides + 1;
238  m_patch_planes_eff = op.patch_planes() + (op.patch_planes() - 1) * (m_in_plane_strides - 1);
239  m_patch_rows_eff = op.patch_rows() + (op.patch_rows() - 1) * (m_in_row_strides - 1);
240  m_patch_cols_eff = op.patch_cols() + (op.patch_cols() - 1) * (m_in_col_strides - 1);
241 
242  if (op.padding_explicit()) {
243  m_outputPlanes =
244  numext::ceil((m_input_planes_eff + op.padding_top_z() + op.padding_bottom_z() - m_patch_planes_eff + 1.f) /
245  static_cast<float>(m_plane_strides));
246  m_outputRows = numext::ceil((m_input_rows_eff + op.padding_top() + op.padding_bottom() - m_patch_rows_eff + 1.f) /
247  static_cast<float>(m_row_strides));
248  m_outputCols = numext::ceil((m_input_cols_eff + op.padding_left() + op.padding_right() - m_patch_cols_eff + 1.f) /
249  static_cast<float>(m_col_strides));
250  m_planePaddingTop = op.padding_top_z();
251  m_rowPaddingTop = op.padding_top();
252  m_colPaddingLeft = op.padding_left();
253  } else {
254  // Computing padding from the type
255  switch (op.padding_type()) {
256  case PADDING_VALID:
257  m_outputPlanes =
258  numext::ceil((m_input_planes_eff - m_patch_planes_eff + 1.f) / static_cast<float>(m_plane_strides));
259  m_outputRows = numext::ceil((m_input_rows_eff - m_patch_rows_eff + 1.f) / static_cast<float>(m_row_strides));
260  m_outputCols = numext::ceil((m_input_cols_eff - m_patch_cols_eff + 1.f) / static_cast<float>(m_col_strides));
261  m_planePaddingTop = 0;
262  m_rowPaddingTop = 0;
263  m_colPaddingLeft = 0;
264  break;
265  case PADDING_SAME: {
266  m_outputPlanes = numext::ceil(m_input_planes_eff / static_cast<float>(m_plane_strides));
267  m_outputRows = numext::ceil(m_input_rows_eff / static_cast<float>(m_row_strides));
268  m_outputCols = numext::ceil(m_input_cols_eff / static_cast<float>(m_col_strides));
269  const Index dz = (m_outputPlanes - 1) * m_plane_strides + m_patch_planes_eff - m_input_planes_eff;
270  const Index dy = (m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff;
271  const Index dx = (m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff;
272  m_planePaddingTop = dz / 2;
273  m_rowPaddingTop = dy / 2;
274  m_colPaddingLeft = dx / 2;
275  break;
276  }
277  default: {
278  eigen_assert(false && "unexpected padding");
279  return;
280  }
281  }
282  }
283  eigen_assert(m_outputRows > 0);
284  eigen_assert(m_outputCols > 0);
285  eigen_assert(m_outputPlanes > 0);
286 
287  // Dimensions for result of extraction.
288  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
289  // ColMajor
290  // 0: depth
291  // 1: patch_planes
292  // 2: patch_rows
293  // 3: patch_cols
294  // 4: number of patches
295  // 5 and beyond: anything else (such as batch).
296  m_dimensions[0] = input_dims[0];
297  m_dimensions[1] = op.patch_planes();
298  m_dimensions[2] = op.patch_rows();
299  m_dimensions[3] = op.patch_cols();
300  m_dimensions[4] = m_outputPlanes * m_outputRows * m_outputCols;
301  for (int i = 5; i < NumDims; ++i) {
302  m_dimensions[i] = input_dims[i - 1];
303  }
304  } else {
305  // RowMajor
306  // NumDims-1: depth
307  // NumDims-2: patch_planes
308  // NumDims-3: patch_rows
309  // NumDims-4: patch_cols
310  // NumDims-5: number of patches
311  // NumDims-6 and beyond: anything else (such as batch).
312  m_dimensions[NumDims - 1] = input_dims[NumInputDims - 1];
313  m_dimensions[NumDims - 2] = op.patch_planes();
314  m_dimensions[NumDims - 3] = op.patch_rows();
315  m_dimensions[NumDims - 4] = op.patch_cols();
316  m_dimensions[NumDims - 5] = m_outputPlanes * m_outputRows * m_outputCols;
317  for (int i = NumDims - 6; i >= 0; --i) {
318  m_dimensions[i] = input_dims[i];
319  }
320  }
321 
322  // Strides for the output tensor.
323  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
324  m_rowStride = m_dimensions[1];
325  m_colStride = m_dimensions[2] * m_rowStride;
326  m_patchStride = m_colStride * m_dimensions[3] * m_dimensions[0];
327  m_otherStride = m_patchStride * m_dimensions[4];
328  } else {
329  m_rowStride = m_dimensions[NumDims - 2];
330  m_colStride = m_dimensions[NumDims - 3] * m_rowStride;
331  m_patchStride = m_colStride * m_dimensions[NumDims - 4] * m_dimensions[NumDims - 1];
332  m_otherStride = m_patchStride * m_dimensions[NumDims - 5];
333  }
334 
335  // Strides for navigating through the input tensor.
336  m_planeInputStride = m_inputDepth;
337  m_rowInputStride = m_inputDepth * m_inputPlanes;
338  m_colInputStride = m_inputDepth * m_inputRows * m_inputPlanes;
339  m_otherInputStride = m_inputDepth * m_inputRows * m_inputCols * m_inputPlanes;
340 
341  m_outputPlanesRows = m_outputPlanes * m_outputRows;
342 
343  // Fast representations of different variables.
344  m_fastOtherStride = internal::TensorIntDivisor<Index>(m_otherStride);
345 
346  m_fastPatchStride = internal::TensorIntDivisor<Index>(m_patchStride);
347  m_fastColStride = internal::TensorIntDivisor<Index>(m_colStride);
348  m_fastRowStride = internal::TensorIntDivisor<Index>(m_rowStride);
349  m_fastInputRowStride = internal::TensorIntDivisor<Index>(m_row_inflate_strides);
350  m_fastInputColStride = internal::TensorIntDivisor<Index>(m_col_inflate_strides);
351  m_fastInputPlaneStride = internal::TensorIntDivisor<Index>(m_plane_inflate_strides);
352  m_fastInputColsEff = internal::TensorIntDivisor<Index>(m_input_cols_eff);
353  m_fastOutputPlanes = internal::TensorIntDivisor<Index>(m_outputPlanes);
354  m_fastOutputPlanesRows = internal::TensorIntDivisor<Index>(m_outputPlanesRows);
355 
356  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
357  m_fastOutputDepth = internal::TensorIntDivisor<Index>(m_dimensions[0]);
358  } else {
359  m_fastOutputDepth = internal::TensorIntDivisor<Index>(m_dimensions[NumDims - 1]);
360  }
361  }
362 
363  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
364 
365  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
366  m_impl.evalSubExprsIfNeeded(NULL);
367  return true;
368  }
369 
370 #ifdef EIGEN_USE_THREADS
371  template <typename EvalSubExprsCallback>
372  EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(EvaluatorPointerType /*data*/, EvalSubExprsCallback done) {
373  m_impl.evalSubExprsIfNeededAsync(nullptr, [done](bool) { done(true); });
374  }
375 #endif // EIGEN_USE_THREADS
376 
377  EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
378 
379  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
380  // Patch index corresponding to the passed in index.
381  const Index patchIndex = index / m_fastPatchStride;
382 
383  // Spatial offset within the patch. This has to be translated into 3D
384  // coordinates within the patch.
385  const Index patchOffset = (index - patchIndex * m_patchStride) / m_fastOutputDepth;
386 
387  // Batch, etc.
388  const Index otherIndex = (NumDims == 5) ? 0 : index / m_fastOtherStride;
389  const Index patch3DIndex = (NumDims == 5) ? patchIndex : (index - otherIndex * m_otherStride) / m_fastPatchStride;
390 
391  // Calculate column index in the input original tensor.
392  const Index colIndex = patch3DIndex / m_fastOutputPlanesRows;
393  const Index colOffset = patchOffset / m_fastColStride;
394  const Index inputCol = colIndex * m_col_strides + colOffset * m_in_col_strides - m_colPaddingLeft;
395  const Index origInputCol =
396  (m_col_inflate_strides == 1) ? inputCol : ((inputCol >= 0) ? (inputCol / m_fastInputColStride) : 0);
397  if (inputCol < 0 || inputCol >= m_input_cols_eff ||
398  ((m_col_inflate_strides != 1) && (inputCol != origInputCol * m_col_inflate_strides))) {
399  return Scalar(m_paddingValue);
400  }
401 
402  // Calculate row index in the original input tensor.
403  const Index rowIndex = (patch3DIndex - colIndex * m_outputPlanesRows) / m_fastOutputPlanes;
404  const Index rowOffset = (patchOffset - colOffset * m_colStride) / m_fastRowStride;
405  const Index inputRow = rowIndex * m_row_strides + rowOffset * m_in_row_strides - m_rowPaddingTop;
406  const Index origInputRow =
407  (m_row_inflate_strides == 1) ? inputRow : ((inputRow >= 0) ? (inputRow / m_fastInputRowStride) : 0);
408  if (inputRow < 0 || inputRow >= m_input_rows_eff ||
409  ((m_row_inflate_strides != 1) && (inputRow != origInputRow * m_row_inflate_strides))) {
410  return Scalar(m_paddingValue);
411  }
412 
413  // Calculate plane index in the original input tensor.
414  const Index planeIndex = (patch3DIndex - m_outputPlanes * (colIndex * m_outputRows + rowIndex));
415  const Index planeOffset = patchOffset - colOffset * m_colStride - rowOffset * m_rowStride;
416  const Index inputPlane = planeIndex * m_plane_strides + planeOffset * m_in_plane_strides - m_planePaddingTop;
417  const Index origInputPlane =
418  (m_plane_inflate_strides == 1) ? inputPlane : ((inputPlane >= 0) ? (inputPlane / m_fastInputPlaneStride) : 0);
419  if (inputPlane < 0 || inputPlane >= m_input_planes_eff ||
420  ((m_plane_inflate_strides != 1) && (inputPlane != origInputPlane * m_plane_inflate_strides))) {
421  return Scalar(m_paddingValue);
422  }
423 
424  const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
425  const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
426 
427  const Index inputIndex = depth + origInputRow * m_rowInputStride + origInputCol * m_colInputStride +
428  origInputPlane * m_planeInputStride + otherIndex * m_otherInputStride;
429 
430  return m_impl.coeff(inputIndex);
431  }
432 
433  template <int LoadMode>
434  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
435  eigen_assert(index + PacketSize - 1 < dimensions().TotalSize());
436 
437  if (m_in_row_strides != 1 || m_in_col_strides != 1 || m_row_inflate_strides != 1 || m_col_inflate_strides != 1 ||
438  m_in_plane_strides != 1 || m_plane_inflate_strides != 1) {
439  return packetWithPossibleZero(index);
440  }
441 
442  const Index indices[2] = {index, index + PacketSize - 1};
443  const Index patchIndex = indices[0] / m_fastPatchStride;
444  if (patchIndex != indices[1] / m_fastPatchStride) {
445  return packetWithPossibleZero(index);
446  }
447  const Index otherIndex = (NumDims == 5) ? 0 : indices[0] / m_fastOtherStride;
448  eigen_assert(otherIndex == indices[1] / m_fastOtherStride);
449 
450  // Find the offset of the element wrt the location of the first element.
451  Index first_entry = (indices[0] - patchIndex * m_patchStride) / m_fastOutputDepth;
452  Index second_entry = PacketSize == 1 ? first_entry :
453  (indices[1] - patchIndex * m_patchStride) / m_fastOutputDepth;
454 
455  const Index patchOffsets[2] = {first_entry, second_entry};
456 
457  const Index patch3DIndex =
458  (NumDims == 5) ? patchIndex : (indices[0] - otherIndex * m_otherStride) / m_fastPatchStride;
459  eigen_assert(patch3DIndex == (indices[1] - otherIndex * m_otherStride) / m_fastPatchStride);
460 
461  const Index colIndex = patch3DIndex / m_fastOutputPlanesRows;
462  const Index colOffsets[2] = {patchOffsets[0] / m_fastColStride, patchOffsets[1] / m_fastColStride};
463 
464  // Calculate col indices in the original input tensor.
465  const Index inputCols[2] = {colIndex * m_col_strides + colOffsets[0] - m_colPaddingLeft,
466  colIndex * m_col_strides + colOffsets[1] - m_colPaddingLeft};
467  if (inputCols[1] < 0 || inputCols[0] >= m_inputCols) {
468  return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
469  }
470 
471  if (inputCols[0] != inputCols[1]) {
472  return packetWithPossibleZero(index);
473  }
474 
475  const Index rowIndex = (patch3DIndex - colIndex * m_outputPlanesRows) / m_fastOutputPlanes;
476  const Index rowOffsets[2] = {(patchOffsets[0] - colOffsets[0] * m_colStride) / m_fastRowStride,
477  (patchOffsets[1] - colOffsets[1] * m_colStride) / m_fastRowStride};
478  eigen_assert(rowOffsets[0] <= rowOffsets[1]);
479  // Calculate col indices in the original input tensor.
480  const Index inputRows[2] = {rowIndex * m_row_strides + rowOffsets[0] - m_rowPaddingTop,
481  rowIndex * m_row_strides + rowOffsets[1] - m_rowPaddingTop};
482 
483  if (inputRows[1] < 0 || inputRows[0] >= m_inputRows) {
484  return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
485  }
486 
487  if (inputRows[0] != inputRows[1]) {
488  return packetWithPossibleZero(index);
489  }
490 
491  const Index planeIndex = (patch3DIndex - m_outputPlanes * (colIndex * m_outputRows + rowIndex));
492  const Index planeOffsets[2] = {patchOffsets[0] - colOffsets[0] * m_colStride - rowOffsets[0] * m_rowStride,
493  patchOffsets[1] - colOffsets[1] * m_colStride - rowOffsets[1] * m_rowStride};
494  eigen_assert(planeOffsets[0] <= planeOffsets[1]);
495  const Index inputPlanes[2] = {planeIndex * m_plane_strides + planeOffsets[0] - m_planePaddingTop,
496  planeIndex * m_plane_strides + planeOffsets[1] - m_planePaddingTop};
497 
498  if (inputPlanes[1] < 0 || inputPlanes[0] >= m_inputPlanes) {
499  return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
500  }
501 
502  if (inputPlanes[0] >= 0 && inputPlanes[1] < m_inputPlanes) {
503  // no padding
504  const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
505  const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
506  const Index inputIndex = depth + inputRows[0] * m_rowInputStride + inputCols[0] * m_colInputStride +
507  m_planeInputStride * inputPlanes[0] + otherIndex * m_otherInputStride;
508  return m_impl.template packet<Unaligned>(inputIndex);
509  }
510 
511  return packetWithPossibleZero(index);
512  }
513 
514  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
515  const double compute_cost =
516  10 * TensorOpCost::DivCost<Index>() + 21 * TensorOpCost::MulCost<Index>() + 8 * TensorOpCost::AddCost<Index>();
517  return TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
518  }
519 
520  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
521 
522  const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
523 
524  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index planePaddingTop() const { return m_planePaddingTop; }
525  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rowPaddingTop() const { return m_rowPaddingTop; }
526  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index colPaddingLeft() const { return m_colPaddingLeft; }
527  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outputPlanes() const { return m_outputPlanes; }
528  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outputRows() const { return m_outputRows; }
529  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outputCols() const { return m_outputCols; }
530  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userPlaneStride() const { return m_plane_strides; }
531  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userRowStride() const { return m_row_strides; }
532  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userColStride() const { return m_col_strides; }
533  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userInPlaneStride() const { return m_in_plane_strides; }
534  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userInRowStride() const { return m_in_row_strides; }
535  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userInColStride() const { return m_in_col_strides; }
536  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index planeInflateStride() const { return m_plane_inflate_strides; }
537  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rowInflateStride() const { return m_row_inflate_strides; }
538  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index colInflateStride() const { return m_col_inflate_strides; }
539 
540  protected:
541  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packetWithPossibleZero(Index index) const {
542  EIGEN_ALIGN_MAX std::remove_const_t<CoeffReturnType> values[PacketSize];
543  EIGEN_UNROLL_LOOP
544  for (int i = 0; i < PacketSize; ++i) {
545  values[i] = coeff(index + i);
546  }
547  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
548  return rslt;
549  }
550 
551  Dimensions m_dimensions;
552 
553  // Parameters passed to the constructor.
554  Index m_plane_strides;
555  Index m_row_strides;
556  Index m_col_strides;
557 
558  Index m_outputPlanes;
559  Index m_outputRows;
560  Index m_outputCols;
561 
562  Index m_planePaddingTop;
563  Index m_rowPaddingTop;
564  Index m_colPaddingLeft;
565 
566  Index m_in_plane_strides;
567  Index m_in_row_strides;
568  Index m_in_col_strides;
569 
570  Index m_plane_inflate_strides;
571  Index m_row_inflate_strides;
572  Index m_col_inflate_strides;
573 
574  // Cached input size.
575  Index m_inputDepth;
576  Index m_inputPlanes;
577  Index m_inputRows;
578  Index m_inputCols;
579 
580  // Other cached variables.
581  Index m_outputPlanesRows;
582 
583  // Effective input/patch post-inflation size.
584  Index m_input_planes_eff;
585  Index m_input_rows_eff;
586  Index m_input_cols_eff;
587  Index m_patch_planes_eff;
588  Index m_patch_rows_eff;
589  Index m_patch_cols_eff;
590 
591  // Strides for the output tensor.
592  Index m_otherStride;
593  Index m_patchStride;
594  Index m_rowStride;
595  Index m_colStride;
596 
597  // Strides for the input tensor.
598  Index m_planeInputStride;
599  Index m_rowInputStride;
600  Index m_colInputStride;
601  Index m_otherInputStride;
602 
603  internal::TensorIntDivisor<Index> m_fastOtherStride;
604  internal::TensorIntDivisor<Index> m_fastPatchStride;
605  internal::TensorIntDivisor<Index> m_fastColStride;
606  internal::TensorIntDivisor<Index> m_fastRowStride;
607  internal::TensorIntDivisor<Index> m_fastInputPlaneStride;
608  internal::TensorIntDivisor<Index> m_fastInputRowStride;
609  internal::TensorIntDivisor<Index> m_fastInputColStride;
610  internal::TensorIntDivisor<Index> m_fastInputColsEff;
611  internal::TensorIntDivisor<Index> m_fastOutputPlanesRows;
612  internal::TensorIntDivisor<Index> m_fastOutputPlanes;
613  internal::TensorIntDivisor<Index> m_fastOutputDepth;
614 
615  Scalar m_paddingValue;
616 
617  TensorEvaluator<ArgType, Device> m_impl;
618 };
619 
620 } // end namespace Eigen
621 
622 #endif // EIGEN_CXX11_TENSOR_TENSOR_VOLUME_PATCH_H
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index