Skip to content
fn

narrow

→Tensor
narrow(input: Tensor, dim: _int, start: _int, length: _int)

Implementing kernel

C++ engine symbols that back this Python API.

Return a slice of input along dim.

Equivalent to input[..., start:start+length, ...] with the slice applied at position dim. On the CPU the slice is a view of input's buffer — along a later axis a non-contiguous one; on metal it is a copy.

Parameters

inputTensor
Source tensor.
dimint
Dimension to slice.
startint
First index (inclusive).
lengthint
Number of elements to take. Must satisfy start + length <= input.size(dim).

Returns

Tensor

The slice, of size length along dim.

Notes

Unlike NumPy slicing, narrow accepts only step 1; for general strided slicing use indexing syntax.

Examples

>>> import lucid
>>> x = lucid.arange(10)
>>> lucid.narrow(x, dim=0, start=3, length=4)
Tensor([3, 4, 5, 6])