Skip to content
fn

expand

→Tensor
expand(input: Tensor, size: _int | Sequence[_int], implicit: _bool = ...)

Implementing kernel

C++ engine symbols that back this Python API.

Broadcast the singleton dimensions of input to new sizes.

For each axis whose current size is 1, expand repeats it to the requested size. Axes that already have non-1 sizes must be passed as -1 (or the same size). On the CPU the result is a view: each expanded axis has stride 0, so every repeated element is the same memory as input's, and a write through the view is refused. On metal the result is a copy.

Parameters

inputTensor
Source tensor.
sizeint or sequence of int
Target sizes. -1 means "keep current size".

Returns

Tensor

input under the requested shape — a read-only view on the CPU, a copy on metal.

Notes

expand is the cousin of broadcast_to; they produce equal results, but expand requires the input to already have a 1-sized dim where the expansion happens. On the CPU both are views.

Examples

>>> import lucid
>>> x = lucid.tensor([[1.0], [2.0], [3.0]])  # shape (3, 1)
>>> lucid.expand(x, (3, 4)).shape
(3, 4)