Skip to content
fn

chunk

→list of Tensor
chunk(input: Tensor, chunks: _int, dim: _int = ...)

Implementing kernel

C++ engine symbols that back this Python API.

Split a tensor into at most chunks pieces along dim.

Each piece has size ⌈n/c⌉\lceil n / c \rceil along dim except the last, which holds what remains — so fewer than chunks pieces come back when that size exhausts the dimension early (n=6, c=4 gives [2, 2, 2]). split takes the piece size instead.

Parameters

inputTensor
Source tensor.
chunksint
Desired number of pieces. Must be positive.
dimint= 0
Dimension along which to split.

Returns

list of Tensor

The pieces of input in order — views of its buffer on the CPU, copies on metal.

Raises

lucid._C.engine.InvalidArgument
chunks is not positive.

Examples

>>> import lucid
>>> x = lucid.arange(7)
>>> [t.shape for t in lucid.chunk(x, 3, dim=0)]
[(3,), (3,), (1,)]
>>> [t.shape for t in lucid.chunk(x, 4, dim=0)]
[(2,), (2,), (2,), (1,)]