SliceIndexMapper

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

General-purpose IndexMapper implementation for arbitrary slice patterns.

SliceIndexMapper handles coordinate transformation for any combination of slice types, providing a flexible foundation for tensor slicing operations. It implements the IndexMapper interface with support for NCHW memory layout and row-major storage.

Algorithm Overview

The mapper works by:

  1. Dimension Mapping: Maps each view dimension to parent dimension(s)

  2. Offset Calculation: Computes base offsets from slice start positions

  3. Stride Application: Applies step sizes and memory layout strides

  4. Bounds Validation: Ensures all coordinates remain within valid ranges

Memory Layout Support

The implementation assumes row-major memory layout with rightmost dimension having stride 1. For NCHW tensors with shape N, C, H, W, the natural strides are C×H×W, H×W, W, 1.

Performance Optimizations

  • Stride Pre-calculation: Computes and caches stride patterns on construction

  • Contiguity Detection: Identifies patterns that enable bulk operations

  • Bounds Checking: Validates slice compatibility during construction

  • Efficient Indexing: Minimizes array allocations in hot paths

Parameters

T

the data type constraint extending DType

V

the actual value type

parentShape

the shape of the parent tensor

slices

the list of slice operations, one per parent dimension

viewShape

the computed shape of the resulting view

Constructors

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

Types

Link copied to clipboard

Describes the memory access pattern for optimization decisions.

Functions

Link copied to clipboard

Returns whether this slice pattern can benefit from vectorized access.

Link copied to clipboard

Pre-calculates memory access patterns for common slice types. This enables branch prediction optimization and faster hot path execution.

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

Returns the stride pattern for the view.

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

Returns whether this mapper represents contiguous memory access.

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

Maps view coordinates to parent tensor coordinates.