Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
TensorVolumePatch.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3// SPDX-FileCopyrightText: The Eigen Authors
4// SPDX-License-Identifier: MPL-2.0
5
6#ifndef EIGEN_TENSOR_TENSOR_VOLUME_PATCH_H
7#define EIGEN_TENSOR_TENSOR_VOLUME_PATCH_H
8
9// IWYU pragma: private
10#include "./InternalHeaderCheck.h"
11
12namespace Eigen {
13
14namespace internal {
15
16template <DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
17struct traits<TensorVolumePatchOp<Planes, Rows, Cols, XprType>> : traits<XprType> {
18 typedef std::remove_const_t<typename XprType::Scalar> Scalar;
19 typedef traits<XprType> XprTraits;
20 typedef typename XprTraits::StorageKind StorageKind;
21 typedef typename XprTraits::Index Index;
22 static constexpr int NumDimensions = XprTraits::NumDimensions + 1;
23 static constexpr int Layout = XprTraits::Layout;
24 typedef typename XprTraits::PointerType PointerType;
25};
26
27template <DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
28struct eval<TensorVolumePatchOp<Planes, Rows, Cols, XprType>, Eigen::Dense> {
29 typedef const TensorVolumePatchOp<Planes, Rows, Cols, XprType>& type;
30};
31
32} // end namespace internal
33
49template <DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
50class TensorVolumePatchOp : public TensorBase<TensorVolumePatchOp<Planes, Rows, Cols, XprType>, ReadOnlyAccessors> {
51 public:
52 typedef typename Eigen::internal::traits<TensorVolumePatchOp>::Scalar Scalar;
53 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
54 typedef typename XprType::CoeffReturnType CoeffReturnType;
55 typedef typename Eigen::internal::ref_selector<TensorVolumePatchOp>::type Nested;
56 typedef typename Eigen::internal::traits<TensorVolumePatchOp>::StorageKind StorageKind;
57 typedef typename Eigen::internal::traits<TensorVolumePatchOp>::Index Index;
58
59 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorVolumePatchOp(
60 const XprType& expr, DenseIndex patch_planes, DenseIndex patch_rows, DenseIndex patch_cols,
61 DenseIndex plane_strides, DenseIndex row_strides, DenseIndex col_strides, DenseIndex in_plane_strides,
62 DenseIndex in_row_strides, DenseIndex in_col_strides, DenseIndex plane_inflate_strides,
63 DenseIndex row_inflate_strides, DenseIndex col_inflate_strides, PaddingType padding_type, Scalar padding_value)
64 : m_xpr(expr),
65 m_patch_planes(patch_planes),
66 m_patch_rows(patch_rows),
67 m_patch_cols(patch_cols),
68 m_plane_strides(plane_strides),
69 m_row_strides(row_strides),
70 m_col_strides(col_strides),
71 m_in_plane_strides(in_plane_strides),
72 m_in_row_strides(in_row_strides),
73 m_in_col_strides(in_col_strides),
74 m_plane_inflate_strides(plane_inflate_strides),
75 m_row_inflate_strides(row_inflate_strides),
76 m_col_inflate_strides(col_inflate_strides),
77 m_padding_explicit(false),
78 m_padding_top_z(0),
79 m_padding_bottom_z(0),
80 m_padding_top(0),
81 m_padding_bottom(0),
82 m_padding_left(0),
83 m_padding_right(0),
84 m_padding_type(padding_type),
85 m_padding_value(padding_value) {}
86
87 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorVolumePatchOp(
88 const XprType& expr, DenseIndex patch_planes, DenseIndex patch_rows, DenseIndex patch_cols,
89 DenseIndex plane_strides, DenseIndex row_strides, DenseIndex col_strides, DenseIndex in_plane_strides,
90 DenseIndex in_row_strides, DenseIndex in_col_strides, DenseIndex plane_inflate_strides,
91 DenseIndex row_inflate_strides, DenseIndex col_inflate_strides, DenseIndex padding_top_z,
92 DenseIndex padding_bottom_z, DenseIndex padding_top, DenseIndex padding_bottom, DenseIndex padding_left,
93 DenseIndex padding_right, Scalar padding_value)
94 : m_xpr(expr),
95 m_patch_planes(patch_planes),
96 m_patch_rows(patch_rows),
97 m_patch_cols(patch_cols),
98 m_plane_strides(plane_strides),
99 m_row_strides(row_strides),
100 m_col_strides(col_strides),
101 m_in_plane_strides(in_plane_strides),
102 m_in_row_strides(in_row_strides),
103 m_in_col_strides(in_col_strides),
104 m_plane_inflate_strides(plane_inflate_strides),
105 m_row_inflate_strides(row_inflate_strides),
106 m_col_inflate_strides(col_inflate_strides),
107 m_padding_explicit(true),
108 m_padding_top_z(padding_top_z),
109 m_padding_bottom_z(padding_bottom_z),
110 m_padding_top(padding_top),
111 m_padding_bottom(padding_bottom),
112 m_padding_left(padding_left),
113 m_padding_right(padding_right),
114 m_padding_type(PADDING_VALID),
115 m_padding_value(padding_value) {}
116
117 EIGEN_DEVICE_FUNC DenseIndex patch_planes() const { return m_patch_planes; }
118 EIGEN_DEVICE_FUNC DenseIndex patch_rows() const { return m_patch_rows; }
119 EIGEN_DEVICE_FUNC DenseIndex patch_cols() const { return m_patch_cols; }
120 EIGEN_DEVICE_FUNC DenseIndex plane_strides() const { return m_plane_strides; }
121 EIGEN_DEVICE_FUNC DenseIndex row_strides() const { return m_row_strides; }
122 EIGEN_DEVICE_FUNC DenseIndex col_strides() const { return m_col_strides; }
123 EIGEN_DEVICE_FUNC DenseIndex in_plane_strides() const { return m_in_plane_strides; }
124 EIGEN_DEVICE_FUNC DenseIndex in_row_strides() const { return m_in_row_strides; }
125 EIGEN_DEVICE_FUNC DenseIndex in_col_strides() const { return m_in_col_strides; }
126 EIGEN_DEVICE_FUNC DenseIndex plane_inflate_strides() const { return m_plane_inflate_strides; }
127 EIGEN_DEVICE_FUNC DenseIndex row_inflate_strides() const { return m_row_inflate_strides; }
128 EIGEN_DEVICE_FUNC DenseIndex col_inflate_strides() const { return m_col_inflate_strides; }
129 EIGEN_DEVICE_FUNC bool padding_explicit() const { return m_padding_explicit; }
130 EIGEN_DEVICE_FUNC DenseIndex padding_top_z() const { return m_padding_top_z; }
131 EIGEN_DEVICE_FUNC DenseIndex padding_bottom_z() const { return m_padding_bottom_z; }
132 EIGEN_DEVICE_FUNC DenseIndex padding_top() const { return m_padding_top; }
133 EIGEN_DEVICE_FUNC DenseIndex padding_bottom() const { return m_padding_bottom; }
134 EIGEN_DEVICE_FUNC DenseIndex padding_left() const { return m_padding_left; }
135 EIGEN_DEVICE_FUNC DenseIndex padding_right() const { return m_padding_right; }
136 EIGEN_DEVICE_FUNC PaddingType padding_type() const { return m_padding_type; }
137 EIGEN_DEVICE_FUNC Scalar padding_value() const { return m_padding_value; }
138
139 EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
140
141 protected:
142 typename XprType::Nested m_xpr;
143 const DenseIndex m_patch_planes;
144 const DenseIndex m_patch_rows;
145 const DenseIndex m_patch_cols;
146 const DenseIndex m_plane_strides;
147 const DenseIndex m_row_strides;
148 const DenseIndex m_col_strides;
149 const DenseIndex m_in_plane_strides;
150 const DenseIndex m_in_row_strides;
151 const DenseIndex m_in_col_strides;
152 const DenseIndex m_plane_inflate_strides;
153 const DenseIndex m_row_inflate_strides;
154 const DenseIndex m_col_inflate_strides;
155 const bool m_padding_explicit;
156 const DenseIndex m_padding_top_z;
157 const DenseIndex m_padding_bottom_z;
158 const DenseIndex m_padding_top;
159 const DenseIndex m_padding_bottom;
160 const DenseIndex m_padding_left;
161 const DenseIndex m_padding_right;
162 const PaddingType m_padding_type;
163 const Scalar m_padding_value;
164};
165
166// Eval as rvalue
167template <DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename ArgType, typename Device>
168struct TensorEvaluator<const TensorVolumePatchOp<Planes, Rows, Cols, ArgType>, Device> {
170 typedef typename XprType::Index Index;
171 static constexpr int NumInputDims =
172 internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
173 static constexpr int NumDims = NumInputDims + 1;
174 typedef DSizes<Index, NumDims> Dimensions;
175 typedef std::remove_const_t<typename XprType::Scalar> Scalar;
176 typedef typename XprType::CoeffReturnType CoeffReturnType;
177 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
178 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
179 typedef StorageMemory<CoeffReturnType, Device> Storage;
180 typedef typename Storage::Type EvaluatorPointerType;
181
182 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
183 enum {
184 IsAligned = false,
185 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
186 // block() reads the argument one coefficient at a time through coeff() --
187 // the contract the scalar executors already rely on for every evaluator --
188 // so it requires no capability bit from the argument (same as
189 // TensorReverse).
190 BlockAccess = true,
191 // The coeff/packet path pays ~10 divisions of index math per element; the
192 // block path amortizes all of it over whole depth runs.
193 PreferBlockAccess = true,
194 CoordAccess = false,
195 RawAccess = false
196 };
197
198 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
199 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
200 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
201 typedef typename internal::TensorMaterializedBlock<Scalar, NumDims, Layout, Index> TensorBlock;
202 //===--------------------------------------------------------------------===//
203
204 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
205 : m_impl(op.expression(), device), m_device(device) {
206 EIGEN_STATIC_ASSERT((NumDims >= 5), YOU_MADE_A_PROGRAMMING_MISTAKE);
207
208 m_paddingValue = op.padding_value();
209
210 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
211
212 // Cache a few variables.
213 EIGEN_IF_CONSTEXPR (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
214 m_inputDepth = input_dims[0];
215 m_inputPlanes = input_dims[1];
216 m_inputRows = input_dims[2];
217 m_inputCols = input_dims[3];
218 } else {
219 m_inputDepth = input_dims[NumInputDims - 1];
220 m_inputPlanes = input_dims[NumInputDims - 2];
221 m_inputRows = input_dims[NumInputDims - 3];
222 m_inputCols = input_dims[NumInputDims - 4];
223 }
224
225 m_plane_strides = op.plane_strides();
226 m_row_strides = op.row_strides();
227 m_col_strides = op.col_strides();
228
229 // Input strides and effective input/patch size
230 m_in_plane_strides = op.in_plane_strides();
231 m_in_row_strides = op.in_row_strides();
232 m_in_col_strides = op.in_col_strides();
233 m_plane_inflate_strides = op.plane_inflate_strides();
234 m_row_inflate_strides = op.row_inflate_strides();
235 m_col_inflate_strides = op.col_inflate_strides();
236
237 // The "effective" spatial size after inflating data with zeros.
238 m_input_planes_eff = (m_inputPlanes - 1) * m_plane_inflate_strides + 1;
239 m_input_rows_eff = (m_inputRows - 1) * m_row_inflate_strides + 1;
240 m_input_cols_eff = (m_inputCols - 1) * m_col_inflate_strides + 1;
241 m_patch_planes_eff = op.patch_planes() + (op.patch_planes() - 1) * (m_in_plane_strides - 1);
242 m_patch_rows_eff = op.patch_rows() + (op.patch_rows() - 1) * (m_in_row_strides - 1);
243 m_patch_cols_eff = op.patch_cols() + (op.patch_cols() - 1) * (m_in_col_strides - 1);
244
245 if (op.padding_explicit()) {
246 m_outputPlanes =
247 numext::ceil((m_input_planes_eff + op.padding_top_z() + op.padding_bottom_z() - m_patch_planes_eff + 1.f) /
248 static_cast<float>(m_plane_strides));
249 m_outputRows = numext::ceil((m_input_rows_eff + op.padding_top() + op.padding_bottom() - m_patch_rows_eff + 1.f) /
250 static_cast<float>(m_row_strides));
251 m_outputCols = numext::ceil((m_input_cols_eff + op.padding_left() + op.padding_right() - m_patch_cols_eff + 1.f) /
252 static_cast<float>(m_col_strides));
253 m_planePaddingTop = op.padding_top_z();
254 m_rowPaddingTop = op.padding_top();
255 m_colPaddingLeft = op.padding_left();
256 } else {
257 // Computing padding from the type
258 switch (op.padding_type()) {
259 case PADDING_VALID:
260 m_outputPlanes =
261 numext::ceil((m_input_planes_eff - m_patch_planes_eff + 1.f) / static_cast<float>(m_plane_strides));
262 m_outputRows = numext::ceil((m_input_rows_eff - m_patch_rows_eff + 1.f) / static_cast<float>(m_row_strides));
263 m_outputCols = numext::ceil((m_input_cols_eff - m_patch_cols_eff + 1.f) / static_cast<float>(m_col_strides));
264 m_planePaddingTop = 0;
265 m_rowPaddingTop = 0;
266 m_colPaddingLeft = 0;
267 break;
268 case PADDING_SAME: {
269 m_outputPlanes = numext::ceil(m_input_planes_eff / static_cast<float>(m_plane_strides));
270 m_outputRows = numext::ceil(m_input_rows_eff / static_cast<float>(m_row_strides));
271 m_outputCols = numext::ceil(m_input_cols_eff / static_cast<float>(m_col_strides));
272 const Index dz = (m_outputPlanes - 1) * m_plane_strides + m_patch_planes_eff - m_input_planes_eff;
273 const Index dy = (m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff;
274 const Index dx = (m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff;
275 m_planePaddingTop = dz / 2;
276 m_rowPaddingTop = dy / 2;
277 m_colPaddingLeft = dx / 2;
278 break;
279 }
280 default: {
281 eigen_assert(false && "unexpected padding");
282 return;
283 }
284 }
285 }
286 eigen_assert(m_outputRows > 0);
287 eigen_assert(m_outputCols > 0);
288 eigen_assert(m_outputPlanes > 0);
289
290 // Dimensions for result of extraction.
291 EIGEN_IF_CONSTEXPR (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
292 // ColMajor
293 // 0: depth
294 // 1: patch_planes
295 // 2: patch_rows
296 // 3: patch_cols
297 // 4: number of patches
298 // 5 and beyond: anything else (such as batch).
299 m_dimensions[0] = input_dims[0];
300 m_dimensions[1] = op.patch_planes();
301 m_dimensions[2] = op.patch_rows();
302 m_dimensions[3] = op.patch_cols();
303 m_dimensions[4] = m_outputPlanes * m_outputRows * m_outputCols;
304 for (int i = 5; i < NumDims; ++i) {
305 m_dimensions[i] = input_dims[i - 1];
306 }
307 } else {
308 // RowMajor
309 // NumDims-1: depth
310 // NumDims-2: patch_planes
311 // NumDims-3: patch_rows
312 // NumDims-4: patch_cols
313 // NumDims-5: number of patches
314 // NumDims-6 and beyond: anything else (such as batch).
315 m_dimensions[NumDims - 1] = input_dims[NumInputDims - 1];
316 m_dimensions[NumDims - 2] = op.patch_planes();
317 m_dimensions[NumDims - 3] = op.patch_rows();
318 m_dimensions[NumDims - 4] = op.patch_cols();
319 m_dimensions[NumDims - 5] = m_outputPlanes * m_outputRows * m_outputCols;
320 for (int i = NumDims - 6; i >= 0; --i) {
321 m_dimensions[i] = input_dims[i];
322 }
323 }
324
325 // Strides for the output tensor.
326 EIGEN_IF_CONSTEXPR (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
327 m_rowStride = m_dimensions[1];
328 m_colStride = m_dimensions[2] * m_rowStride;
329 m_patchStride = m_colStride * m_dimensions[3] * m_dimensions[0];
330 m_otherStride = m_patchStride * m_dimensions[4];
331 } else {
332 m_rowStride = m_dimensions[NumDims - 2];
333 m_colStride = m_dimensions[NumDims - 3] * m_rowStride;
334 m_patchStride = m_colStride * m_dimensions[NumDims - 4] * m_dimensions[NumDims - 1];
335 m_otherStride = m_patchStride * m_dimensions[NumDims - 5];
336 }
337
338 // Strides for navigating through the input tensor.
339 m_planeInputStride = m_inputDepth;
340 m_rowInputStride = m_inputDepth * m_inputPlanes;
341 m_colInputStride = m_inputDepth * m_inputRows * m_inputPlanes;
342 m_otherInputStride = m_inputDepth * m_inputRows * m_inputCols * m_inputPlanes;
343
344 m_outputPlanesRows = m_outputPlanes * m_outputRows;
345
346 // Fast representations of different variables.
347 m_fastOtherStride = internal::TensorIntDivisor<Index>(m_otherStride);
348
349 m_fastPatchStride = internal::TensorIntDivisor<Index>(m_patchStride);
350 m_fastColStride = internal::TensorIntDivisor<Index>(m_colStride);
351 m_fastRowStride = internal::TensorIntDivisor<Index>(m_rowStride);
352 m_fastInputRowStride = internal::TensorIntDivisor<Index>(m_row_inflate_strides);
353 m_fastInputColStride = internal::TensorIntDivisor<Index>(m_col_inflate_strides);
354 m_fastInputPlaneStride = internal::TensorIntDivisor<Index>(m_plane_inflate_strides);
355 m_fastInputColsEff = internal::TensorIntDivisor<Index>(m_input_cols_eff);
356 m_fastOutputPlanes = internal::TensorIntDivisor<Index>(m_outputPlanes);
357 m_fastOutputPlanesRows = internal::TensorIntDivisor<Index>(m_outputPlanesRows);
358
359 EIGEN_IF_CONSTEXPR (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
360 m_fastOutputDepth = internal::TensorIntDivisor<Index>(m_dimensions[0]);
361 } else {
362 m_fastOutputDepth = internal::TensorIntDivisor<Index>(m_dimensions[NumDims - 1]);
363 }
364 }
365
366 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
367
368 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
369 m_impl.evalSubExprsIfNeeded(nullptr);
370 return true;
371 }
372
373#ifdef EIGEN_USE_THREADS
374 template <typename EvalSubExprsCallback>
375 EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(EvaluatorPointerType /*data*/, EvalSubExprsCallback done) {
376 m_impl.evalSubExprsIfNeededAsync(nullptr, [done](bool) { done(true); });
377 }
378#endif // EIGEN_USE_THREADS
379
380 EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
381
382 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
383 // Patch index corresponding to the passed in index.
384 const Index patchIndex = index / m_fastPatchStride;
385
386 // Spatial offset within the patch. This has to be translated into 3D
387 // coordinates within the patch.
388 const Index patchOffset = (index - patchIndex * m_patchStride) / m_fastOutputDepth;
389
390 // Batch, etc.
391 const Index otherIndex = (NumDims == 5) ? 0 : index / m_fastOtherStride;
392 const Index patch3DIndex = (NumDims == 5) ? patchIndex : (index - otherIndex * m_otherStride) / m_fastPatchStride;
393
394 // Calculate column index in the input original tensor.
395 const Index colIndex = patch3DIndex / m_fastOutputPlanesRows;
396 const Index colOffset = patchOffset / m_fastColStride;
397 const Index inputCol = colIndex * m_col_strides + colOffset * m_in_col_strides - m_colPaddingLeft;
398 const Index origInputCol =
399 (m_col_inflate_strides == 1) ? inputCol : ((inputCol >= 0) ? (inputCol / m_fastInputColStride) : 0);
400 if (inputCol < 0 || inputCol >= m_input_cols_eff ||
401 ((m_col_inflate_strides != 1) && (inputCol != origInputCol * m_col_inflate_strides))) {
402 return Scalar(m_paddingValue);
403 }
404
405 // Calculate row index in the original input tensor.
406 const Index rowIndex = (patch3DIndex - colIndex * m_outputPlanesRows) / m_fastOutputPlanes;
407 const Index rowOffset = (patchOffset - colOffset * m_colStride) / m_fastRowStride;
408 const Index inputRow = rowIndex * m_row_strides + rowOffset * m_in_row_strides - m_rowPaddingTop;
409 const Index origInputRow =
410 (m_row_inflate_strides == 1) ? inputRow : ((inputRow >= 0) ? (inputRow / m_fastInputRowStride) : 0);
411 if (inputRow < 0 || inputRow >= m_input_rows_eff ||
412 ((m_row_inflate_strides != 1) && (inputRow != origInputRow * m_row_inflate_strides))) {
413 return Scalar(m_paddingValue);
414 }
415
416 // Calculate plane index in the original input tensor.
417 const Index planeIndex = patch3DIndex - m_outputPlanes * (colIndex * m_outputRows + rowIndex);
418 const Index planeOffset = patchOffset - colOffset * m_colStride - rowOffset * m_rowStride;
419 const Index inputPlane = planeIndex * m_plane_strides + planeOffset * m_in_plane_strides - m_planePaddingTop;
420 const Index origInputPlane =
421 (m_plane_inflate_strides == 1) ? inputPlane : ((inputPlane >= 0) ? (inputPlane / m_fastInputPlaneStride) : 0);
422 if (inputPlane < 0 || inputPlane >= m_input_planes_eff ||
423 ((m_plane_inflate_strides != 1) && (inputPlane != origInputPlane * m_plane_inflate_strides))) {
424 return Scalar(m_paddingValue);
425 }
426
427 constexpr int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
428 const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
429
430 const Index inputIndex = depth + origInputRow * m_rowInputStride + origInputCol * m_colInputStride +
431 origInputPlane * m_planeInputStride + otherIndex * m_otherInputStride;
432
433 return m_impl.coeff(inputIndex);
434 }
435
436 template <int LoadMode>
437 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
438 eigen_assert(index + PacketSize - 1 < dimensions().TotalSize());
439
440 if (m_in_row_strides != 1 || m_in_col_strides != 1 || m_row_inflate_strides != 1 || m_col_inflate_strides != 1 ||
441 m_in_plane_strides != 1 || m_plane_inflate_strides != 1) {
442 return packetWithPossibleZero(index);
443 }
444
445 const Index indices[2] = {index, index + PacketSize - 1};
446 const Index patchIndex = indices[0] / m_fastPatchStride;
447 if (patchIndex != indices[1] / m_fastPatchStride) {
448 return packetWithPossibleZero(index);
449 }
450 const Index otherIndex = (NumDims == 5) ? 0 : indices[0] / m_fastOtherStride;
451 eigen_assert(otherIndex == indices[1] / m_fastOtherStride);
452
453 // Find the offset of the element wrt the location of the first element.
454 Index first_entry = (indices[0] - patchIndex * m_patchStride) / m_fastOutputDepth;
455 Index second_entry = PacketSize == 1 ? first_entry : (indices[1] - patchIndex * m_patchStride) / m_fastOutputDepth;
456
457 const Index patchOffsets[2] = {first_entry, second_entry};
458
459 const Index patch3DIndex =
460 (NumDims == 5) ? patchIndex : (indices[0] - otherIndex * m_otherStride) / m_fastPatchStride;
461 eigen_assert(patch3DIndex == (indices[1] - otherIndex * m_otherStride) / m_fastPatchStride);
462
463 const Index colIndex = patch3DIndex / m_fastOutputPlanesRows;
464 const Index colOffsets[2] = {patchOffsets[0] / m_fastColStride, patchOffsets[1] / m_fastColStride};
465
466 // Calculate col indices in the original input tensor.
467 const Index inputCols[2] = {colIndex * m_col_strides + colOffsets[0] - m_colPaddingLeft,
468 colIndex * m_col_strides + colOffsets[1] - m_colPaddingLeft};
469 if (inputCols[1] < 0 || inputCols[0] >= m_inputCols) {
470 return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
471 }
472
473 if (inputCols[0] != inputCols[1]) {
474 return packetWithPossibleZero(index);
475 }
476
477 const Index rowIndex = (patch3DIndex - colIndex * m_outputPlanesRows) / m_fastOutputPlanes;
478 const Index rowOffsets[2] = {(patchOffsets[0] - colOffsets[0] * m_colStride) / m_fastRowStride,
479 (patchOffsets[1] - colOffsets[1] * m_colStride) / m_fastRowStride};
480 eigen_assert(rowOffsets[0] <= rowOffsets[1]);
481 // Calculate row indices in the original input tensor.
482 const Index inputRows[2] = {rowIndex * m_row_strides + rowOffsets[0] - m_rowPaddingTop,
483 rowIndex * m_row_strides + rowOffsets[1] - m_rowPaddingTop};
484
485 if (inputRows[1] < 0 || inputRows[0] >= m_inputRows) {
486 return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
487 }
488
489 if (inputRows[0] != inputRows[1]) {
490 return packetWithPossibleZero(index);
491 }
492
493 const Index planeIndex = patch3DIndex - m_outputPlanes * (colIndex * m_outputRows + rowIndex);
494 const Index planeOffsets[2] = {patchOffsets[0] - colOffsets[0] * m_colStride - rowOffsets[0] * m_rowStride,
495 patchOffsets[1] - colOffsets[1] * m_colStride - rowOffsets[1] * m_rowStride};
496 eigen_assert(planeOffsets[0] <= planeOffsets[1]);
497 const Index inputPlanes[2] = {planeIndex * m_plane_strides + planeOffsets[0] - m_planePaddingTop,
498 planeIndex * m_plane_strides + planeOffsets[1] - m_planePaddingTop};
499
500 if (inputPlanes[1] < 0 || inputPlanes[0] >= m_inputPlanes) {
501 return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
502 }
503
504 if (inputPlanes[0] >= 0 && inputPlanes[1] < m_inputPlanes) {
505 // no padding
506 constexpr int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
507 const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
508 const Index inputIndex = depth + inputRows[0] * m_rowInputStride + inputCols[0] * m_colInputStride +
509 m_planeInputStride * inputPlanes[0] + otherIndex * m_otherInputStride;
510 return m_impl.template packet<Unaligned>(inputIndex);
511 }
512
513 return packetWithPossibleZero(index);
514 }
515
516 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const {
517 const size_t target_size = m_device.firstLevelCacheSize();
518 // In-bounds output coefficients read the argument once and every output
519 // coefficient is stored once (padding runs make this a slight
520 // over-estimate). Pass the full cost explicitly rather than adding to
521 // skewed()'s default load+store seed, which would double-count the
522 // baseline byte traffic and halve the tile size.
523 const TensorOpCost cost_per_coeff = m_impl.costPerCoeff(/*vectorized=*/false) + TensorOpCost(0, sizeof(Scalar), 0);
524 return internal::TensorBlockResourceRequirements::withShapeAndSize<Scalar>(
525 internal::TensorBlockShapeType::kSkewedInnerDims, target_size, cost_per_coeff);
526 }
527
528 // Materializes the block by iterating patch/col/row/plane coordinates and
529 // either copying the (always input-contiguous) depth run or filling it with
530 // the padding value. All per-coordinate index math and bounds checks are
531 // amortized over a whole depth run instead of paid per coefficient.
532 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
533 bool /*root_of_expr_ast*/ = false) const {
534 constexpr bool is_col_major = static_cast<int>(Layout) == static_cast<int>(ColMajor);
535
536 if (desc.size() == 0) {
537 return TensorBlock(internal::TensorBlockKind::kView, nullptr, desc.dimensions());
538 }
539
540 typename TensorBlock::Storage block_storage = TensorBlock::prepareStorage(desc, scratch);
541 Scalar* block_buffer = block_storage.data();
542
543 // Output coordinates of the block's corner.
544 const DSizes<Index, NumDims> output_strides = internal::strides<Layout>(m_dimensions);
545 array<Index, NumDims> coords;
546 Index remaining = desc.offset();
547 EIGEN_IF_CONSTEXPR (is_col_major) {
548 for (int i = NumDims - 1; i > 0; --i) {
549 coords[i] = remaining / output_strides[i];
550 remaining -= coords[i] * output_strides[i];
551 }
552 coords[0] = remaining;
553 } else {
554 for (int i = 0; i < NumDims - 1; ++i) {
555 coords[i] = remaining / output_strides[i];
556 remaining -= coords[i] * output_strides[i];
557 }
558 coords[NumDims - 1] = remaining;
559 }
560
561 // Output dimensions: depth, patch plane/row/col offset, 3d patch index.
562 const int dd = is_col_major ? 0 : NumDims - 1;
563 const int nd = is_col_major ? 1 : NumDims - 2;
564 const int rd = is_col_major ? 2 : NumDims - 3;
565 const int cd = is_col_major ? 3 : NumDims - 4;
566 const int pd = is_col_major ? 4 : NumDims - 5;
567
568 const Index depth_start = coords[dd];
569 const Index depth_size = desc.dimension(dd);
570 const Index plane_start = coords[nd];
571 const Index plane_size = desc.dimension(nd);
572 const Index row_start = coords[rd];
573 const Index row_size = desc.dimension(rd);
574 const Index col_start = coords[cd];
575 const Index col_size = desc.dimension(cd);
576 const Index patch_start = coords[pd];
577 const Index patch_size = desc.dimension(pd);
578
579 // Odometer over the remaining (batch etc.) dimensions, tracking the input
580 // offset they contribute.
581 array<Index, NumDims> other_sizes;
582 array<Index, NumDims> other_src_stride;
583 array<Index, NumDims> other_count;
584 int num_other = 0;
585 Index src_other = 0;
586 {
587 Index in_stride = m_otherInputStride;
588 for (int k = 5; k < NumDims; ++k) {
589 const int d = is_col_major ? k : NumDims - 1 - k;
590 other_sizes[num_other] = desc.dimension(d);
591 other_src_stride[num_other] = in_stride;
592 other_count[num_other] = 0;
593 src_other += coords[d] * in_stride;
594 in_stride *= m_dimensions[d];
595 ++num_other;
596 }
597 }
598
599 typedef internal::StridedLinearBufferCopy<Scalar, Index> LinCopy;
600
601 // The loop nest below visits the block in exactly its memory order (the
602 // storage returned by prepareStorage() is dense with the block's own
603 // layout-order strides), so the destination is one running cursor.
604 Index dst = 0;
605 for (;;) {
606 for (Index p = 0; p < patch_size; ++p) {
607 const Index patch3DIndex = patch_start + p;
608 const Index colIndex = patch3DIndex / m_fastOutputPlanesRows;
609 const Index rowIndex = (patch3DIndex - colIndex * m_outputPlanesRows) / m_fastOutputPlanes;
610 const Index planeIndex = patch3DIndex - m_outputPlanes * (colIndex * m_outputRows + rowIndex);
611
612 for (Index c = 0; c < col_size; ++c) {
613 const Index colOffset = col_start + c;
614 const Index inputCol = colIndex * m_col_strides + colOffset * m_in_col_strides - m_colPaddingLeft;
615 Index origInputCol = inputCol;
616 bool col_valid = inputCol >= 0 && inputCol < m_input_cols_eff;
617 if (col_valid && m_col_inflate_strides != 1) {
618 origInputCol = inputCol / m_fastInputColStride;
619 col_valid = (inputCol == origInputCol * m_col_inflate_strides);
620 }
621
622 for (Index r = 0; r < row_size; ++r) {
623 const Index rowOffset = row_start + r;
624 bool row_valid = col_valid;
625 Index origInputRow = 0;
626 if (row_valid) {
627 const Index inputRow = rowIndex * m_row_strides + rowOffset * m_in_row_strides - m_rowPaddingTop;
628 row_valid = inputRow >= 0 && inputRow < m_input_rows_eff;
629 if (row_valid) {
630 origInputRow = inputRow;
631 if (m_row_inflate_strides != 1) {
632 origInputRow = inputRow / m_fastInputRowStride;
633 row_valid = (inputRow == origInputRow * m_row_inflate_strides);
634 }
635 }
636 }
637
638 for (Index n = 0; n < plane_size; ++n) {
639 const Index planeOffset = plane_start + n;
640 bool valid = row_valid;
641 Index origInputPlane = 0;
642 if (valid) {
643 const Index inputPlane =
644 planeIndex * m_plane_strides + planeOffset * m_in_plane_strides - m_planePaddingTop;
645 valid = inputPlane >= 0 && inputPlane < m_input_planes_eff;
646 if (valid) {
647 origInputPlane = inputPlane;
648 if (m_plane_inflate_strides != 1) {
649 origInputPlane = inputPlane / m_fastInputPlaneStride;
650 valid = (inputPlane == origInputPlane * m_plane_inflate_strides);
651 }
652 }
653 }
654
655 if (valid) {
656 const Index src = depth_start + origInputPlane * m_planeInputStride + origInputRow * m_rowInputStride +
657 origInputCol * m_colInputStride + src_other;
658 for (Index d = 0; d < depth_size; ++d) {
659 block_buffer[dst + d] = m_impl.coeff(src + d);
660 }
661 } else {
662 LinCopy::template Run<LinCopy::Kind::FillLinear>(typename LinCopy::Dst(dst, 1, block_buffer),
663 typename LinCopy::Src(0, 0, &m_paddingValue),
664 depth_size);
665 }
666 dst += depth_size;
667 }
668 }
669 }
670 }
671
672 int k = 0;
673 for (; k < num_other; ++k) {
674 if (++other_count[k] < other_sizes[k]) {
675 src_other += other_src_stride[k];
676 break;
677 }
678 other_count[k] = 0;
679 src_other -= other_src_stride[k] * (other_sizes[k] - 1);
680 }
681 if (k == num_other) break;
682 }
683 eigen_assert(dst == desc.size());
684
685 return block_storage.AsTensorMaterializedBlock();
686 }
687
688 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
689 const double compute_cost =
690 10 * TensorOpCost::DivCost<Index>() + 21 * TensorOpCost::MulCost<Index>() + 8 * TensorOpCost::AddCost<Index>();
691 return TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
692 }
693
694 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return nullptr; }
695
696 const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
697
698 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index planePaddingTop() const { return m_planePaddingTop; }
699 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rowPaddingTop() const { return m_rowPaddingTop; }
700 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index colPaddingLeft() const { return m_colPaddingLeft; }
701 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outputPlanes() const { return m_outputPlanes; }
702 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outputRows() const { return m_outputRows; }
703 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outputCols() const { return m_outputCols; }
704 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userPlaneStride() const { return m_plane_strides; }
705 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userRowStride() const { return m_row_strides; }
706 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userColStride() const { return m_col_strides; }
707 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userInPlaneStride() const { return m_in_plane_strides; }
708 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userInRowStride() const { return m_in_row_strides; }
709 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userInColStride() const { return m_in_col_strides; }
710 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index planeInflateStride() const { return m_plane_inflate_strides; }
711 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rowInflateStride() const { return m_row_inflate_strides; }
712 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index colInflateStride() const { return m_col_inflate_strides; }
713
714 protected:
715 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packetWithPossibleZero(Index index) const {
716 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
717 std::remove_const_t<CoeffReturnType> values[PacketSize];
718 EIGEN_UNROLL_LOOP
719 for (int i = 0; i < PacketSize; ++i) {
720 values[i] = coeff(index + i);
721 }
722 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
723 return rslt;
724 }
725
726 Dimensions m_dimensions;
727
728 // Parameters passed to the constructor.
729 Index m_plane_strides;
730 Index m_row_strides;
731 Index m_col_strides;
732
733 Index m_outputPlanes;
734 Index m_outputRows;
735 Index m_outputCols;
736
737 Index m_planePaddingTop;
738 Index m_rowPaddingTop;
739 Index m_colPaddingLeft;
740
741 Index m_in_plane_strides;
742 Index m_in_row_strides;
743 Index m_in_col_strides;
744
745 Index m_plane_inflate_strides;
746 Index m_row_inflate_strides;
747 Index m_col_inflate_strides;
748
749 // Cached input size.
750 Index m_inputDepth;
751 Index m_inputPlanes;
752 Index m_inputRows;
753 Index m_inputCols;
754
755 // Other cached variables.
756 Index m_outputPlanesRows;
757
758 // Effective input/patch post-inflation size.
759 Index m_input_planes_eff;
760 Index m_input_rows_eff;
761 Index m_input_cols_eff;
762 Index m_patch_planes_eff;
763 Index m_patch_rows_eff;
764 Index m_patch_cols_eff;
765
766 // Strides for the output tensor.
767 Index m_otherStride;
768 Index m_patchStride;
769 Index m_rowStride;
770 Index m_colStride;
771
772 // Strides for the input tensor.
773 Index m_planeInputStride;
774 Index m_rowInputStride;
775 Index m_colInputStride;
776 Index m_otherInputStride;
777
778 internal::TensorIntDivisor<Index> m_fastOtherStride;
779 internal::TensorIntDivisor<Index> m_fastPatchStride;
780 internal::TensorIntDivisor<Index> m_fastColStride;
781 internal::TensorIntDivisor<Index> m_fastRowStride;
782 internal::TensorIntDivisor<Index> m_fastInputPlaneStride;
783 internal::TensorIntDivisor<Index> m_fastInputRowStride;
784 internal::TensorIntDivisor<Index> m_fastInputColStride;
785 internal::TensorIntDivisor<Index> m_fastInputColsEff;
786 internal::TensorIntDivisor<Index> m_fastOutputPlanesRows;
787 internal::TensorIntDivisor<Index> m_fastOutputPlanes;
788 internal::TensorIntDivisor<Index> m_fastOutputDepth;
789
790 Scalar m_paddingValue;
791
792 TensorEvaluator<ArgType, Device> m_impl;
793 const Device EIGEN_DEVICE_REF m_device;
794};
795
796} // end namespace Eigen
797
798#endif // EIGEN_TENSOR_TENSOR_VOLUME_PATCH_H
The tensor base class.
Definition TensorForwardDeclarations.h:69
Patch extraction specialized for processing of volumetric data. This assumes that the input has at le...
Definition TensorVolumePatch.h:50
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47