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)
The array to write into. Can be any data type. x and update must have the same data type. An R value among them assumes the data type of the others when it is in its data type category, and its default data type when none of them has one.

update

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

...

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

Value

(arrayish)
Has the same data type and shape as x.

Out of Bounds Behavior

Start indices are clamped before the update is written: adjusted_start_indices = clamp(1, start_indices, shape(x) - shape(update) + 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_update_slice(), specified under dynamic_update_slice.

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} ]