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(operand, ..., slice_sizes)

Arguments

operand

(arrayish)
Arrayish value of any data type.

...

(arrayish of integer type)
Scalar start indices, one per dimension. Each must be a scalar array. Pass one scalar per dimension of operand.

slice_sizes

(integer())
Size of the slice in each dimension. Must have length equal to ndims(operand) and satisfy 1 <= slice_sizes <= nv_shape(operand) per dimension.

Value

arrayish
Has the same data type as the input and shape slice_sizes. It is ambiguous if the input is ambiguous.

Out Of Bounds Behavior

Start indices are clamped before the slice is extracted: adjusted_start_indices = clamp(1, start_indices, nv_shape(operand) - 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 stablehlo::hlo_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_array(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} ]