NCHWIndexMapper
Specialized IndexMapper optimized for NCHW (Batch, Channel, Height, Width) tensor layout.
NCHWIndexMapper provides highly optimized coordinate transformation for 4-dimensional tensors following the NCHW memory layout convention commonly used in deep learning frameworks. This layout stores data in row-major order with the fastest-changing dimension being Width.
NCHW Memory Layout
For a tensor with shape N, C, H, W, elements are stored with strides:
Batch (N): stride = C × H × W (largest stride)
Channel (C): stride = H × W
Height (H): stride = W
Width (W): stride = 1 (fastest changing)
Optimization Characteristics
This mapper is specifically optimized for common NCHW access patterns:
Batch slicing:
tensor[0:k, :, :, :]- Highly efficient, large contiguous blocksChannel extraction:
tensor[:, 0:k, :, :]- Very efficient, exploits NCHW layoutSpatial regions:
tensor[:, :, h1:h2, w1:w2]- Good efficiency, localized accessWidth slicing:
tensor[:, :, :, w1:w2]- Moderate efficiency, may require striding
Performance Benefits
Pre-calculated Strides: Eliminates runtime stride computation
Specialized Algorithms: Optimized coordinate mapping for each dimension
Cache-Friendly Access: Takes advantage of NCHW spatial locality
Vectorization Hints: Provides accurate contiguity information
Parameters
the NCHW shape of the parent tensor N, C, H, W
the list of slice operations for each NCHW dimension
the computed shape of the resulting view
Type Parameters
the data type constraint extending DType
the actual value type
Throws
if parentShape is not 4-dimensional