sliceView

fun <T : DType, V> Tensor<T, V>.sliceView(builder: TensorSliceBuilder<T, V>.() -> Unit): TensorView<T, V>(source)

Creates a zero-copy view of this tensor using the provided slicing DSL.

This method enables fluent syntax for tensor slicing that creates efficient views without copying data. The resulting TensorView shares memory with the parent tensor and applies coordinate transformations for element access.

Usage Example

val view = tensor.sliceView {
segment { range(0, 10) } // First dimension: indices 0-9
segment { at(5) } // Second dimension: index 5 (reduces rank)
segment { all() } // Third dimension: all indices
segment { step(0, 20, 2) } // Fourth dimension: every 2nd index
}

Performance Characteristics

  • Zero-copy: No data duplication, constant memory overhead

  • Lazy: Coordinate mapping applied during element access

  • Efficient: Optimized for NCHW layout and contiguous patterns

Return

a TensorView providing sliced access to this tensor's data

Parameters

builder

the DSL block that configures the slicing operation

Throws

if slice configuration is invalid


fun <T : DType, V> TensorView<T, V>.sliceView(builder: TensorSliceBuilder<T, V>.() -> Unit): TensorView<T, V>(source)

Creates a view of an existing tensor view, enabling view chaining.

This extension allows creating views of views (view composition) while optimizing the composition by flattening slice operations to prevent deep nesting and maintain performance.

Usage Example

val baseView = tensor.sliceView {
segment { range(0, 20) } // First 20 batches
segment { all() } // All channels
segment { all() } // All height
segment { all() } // All width
}

val refinedView = baseView.sliceView {
segment { range(5, 15) } // Batches 5-14 from the base view (actual: 5-14 from original)
segment { range(0, 64) } // First 64 channels
segment { all() } // All height
segment { all() } // All width
}

Optimization Features

  • Slice flattening: Combines multiple slice operations into a single optimized slice

  • Bounds validation: Ensures composed slices remain within valid ranges

  • Contiguity preservation: Maintains memory access optimizations where possible

  • Chain optimization: Prevents excessive view nesting

Return

a TensorView that represents the composed slice operation

Parameters

builder

the DSL block that configures the additional slicing operation

Throws

if slice composition results in invalid bounds

if slice dimensions don't match view rank