NCHWIndexMapper

class NCHWIndexMapper<T : DType, V>(parentShape: Shape, slices: List<Slice<T, V>>, viewShape: Shape) : IndexMapper(source)

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 blocks

  • Channel extraction: tensor[:, 0:k, :, :] - Very efficient, exploits NCHW layout

  • Spatial regions: tensor[:, :, h1:h2, w1:w2] - Good efficiency, localized access

  • Width 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

T

the data type constraint extending DType

V

the actual value type

parentShape

the NCHW shape of the parent tensor N, C, H, W

slices

the list of slice operations for each NCHW dimension

viewShape

the computed shape of the resulting view

Throws

if parentShape is not 4-dimensional

Constructors

Link copied to clipboard
constructor(parentShape: Shape, slices: List<Slice<T, V>>, viewShape: Shape)

Functions

Link copied to clipboard
open override fun getStride(): IntArray

Returns NCHW-optimized view strides.

Link copied to clipboard
open override fun isContiguous(): Boolean

Returns contiguity information optimized for NCHW patterns.

Link copied to clipboard
open override fun mapToParent(childIndices: IntArray): IntArray

Highly optimized coordinate mapping for NCHW layout.