Skip to content
fn

broadcast_to

→Tensor
broadcast_to(input: Tensor, size: _int | Sequence[_int])

Implementing kernel

C++ engine symbols that back this Python API.

Broadcast a tensor to a target shape.

Performs the standard right-aligned broadcasting rules: dimensions of size 1 in the input may be expanded to any size in size; new leading dimensions may be prepended. On the CPU the result is a view of input's buffer, as expand's is; on metal it is a copy.

Parameters

inputTensor
Source tensor.
sizeShapeLike
Target shape. Must be broadcast-compatible with input.shape.

Returns

Tensor

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

Notes

On the CPU the broadcast axes have stride 0, so every repeated element is one of input's and a write through the result is refused; call contiguous() on it first for a writable copy.

Examples

>>> import lucid
>>> x = lucid.tensor([1.0, 2.0, 3.0])
>>> lucid.broadcast_to(x, (2, 3))
Tensor([[1., 2., 3.],
        [1., 2., 3.]])