Skip to contents

Returns a copy of x with a slice replaced by update at a runtime-determined position. This is the write counterpart of prim_dynamic_slice(): dynamic slice reads a block from an array, while dynamic update slice writes a block into an array.

Usage

prim_dynamic_update_slice(x, update, ...)

Arguments

x

(arrayish)
Arrayish value of any data type.

update

(arrayish)
The values to write at the specified position. Must have the same data type and number of axes as x, with nv_shape(update) <= nv_shape(x) per axis.

...

(arrayish of integer type)
Scalar start indices, one per axis of x. Each must be a scalar array.

Value

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

Implemented Rules

  • stablehlo

  • quickr

  • reverse

StableHLO

Lowers to hlo_dynamic_update_slice().

Out Of Bounds Behavior

Start indices are clamped before the slice is extracted: adjusted_start_indices = clamp(1, start_indices, nv_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.

Examples

# 1-D: overwrite two elements starting at position 2
x <- nv_array(1:5)
update <- nv_array(c(10L, 20L))
start <- nv_scalar(2L)
prim_dynamic_update_slice(x, update, start)
#> AnvlArray
#>   1
#>  10
#>  20
#>   4
#>   5
#> [ CPUi32{5} ] 

# 2-D: write a 2x2 block into a 3x4 matrix
x <- nv_fill(0L, shape = c(3, 4))
update <- nv_matrix(c(1L, 2L, 3L, 4L), nrow = 2, ncol = 2)
row_start <- nv_scalar(2L)
col_start <- nv_scalar(3L)
prim_dynamic_update_slice(x, update, row_start, col_start)
#> AnvlArray
#>  0 0 0 0
#>  0 0 1 3
#>  0 0 2 4
#> [ CPUi32{3,4} ]