Skip to contents

Extracts a slice from an array whose start position is determined at runtime via array-valued indices. The slice shape (slice_sizes) is a fixed R integer vector.

Use prim_static_slice() instead when all indices are known at compile time and you need stride support.

Usage

prim_dynamic_slice(x, ..., slice_sizes)

Arguments

x

(arrayish)
One input. Can be any data type. An R value materializes at its default data type.

...

(arrayish)
Scalar start indices of an integer data type, one per axis of x. They are brought to one data type among themselves, never x's.

slice_sizes

(integer())
Size of the slice in each axis. Must have length equal to naxes(x) and satisfy 1 <= slice_sizes <= shape(x) per axis.

Value

(arrayish)
Has the input's data type and shape slice_sizes.

Out of Bounds Behavior

Start indices are clamped before the slice is extracted: adjusted_start_indices = clamp(1, start_indices, shape(x) - slice_sizes + 1). This means that out-of-bounds indices will not cause an error, but the effective start position may differ from the requested one.

Implemented Rules

  • stablehlo

  • quickr

  • reverse

StableHLO

Lowers to hlo_dynamic_slice(), specified under dynamic_slice.

Examples

# 1-D: extract 3 elements starting at position 3
x <- nv_array(1:10)
start <- nv_scalar(3L)
prim_dynamic_slice(x, start, slice_sizes = 3L)
#> AnvlArray
#>  3
#>  4
#>  5
#> [ CPUi32{3} ] 

# 2-D: extract a 2x2 block from a matrix
x <- nv_matrix(1:12, nrow = 3, ncol = 4)
row_start <- nv_scalar(2L)
col_start <- nv_scalar(1L)
prim_dynamic_slice(x, row_start, col_start, slice_sizes = c(2L, 2L))
#> AnvlArray
#>  2 5
#>  3 6
#> [ CPUi32{2,2} ]