IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Mojo function

to_numpy

def to_numpy[dtype: DType, LayoutType: TensorLayout, origin: Origin[mut=origin.mut], Engine: TensorEngine](tensor: TileTensor[dtype, LayoutType, origin, Engine=Engine]) -> PythonObject

Copies a TileTensor into a new NumPy array of the same shape.

Tensors whose layout is row-major and gap-free are copied with a single memcpy. Other layouts are gathered element by element.

This calls into CPython, so it is a host-side entry point.

Example:

from layout import TileTensor, row_major, to_numpy
from layout.coord import Coord, Idx

var storage = Array[Float32, 6](uninitialized=True)
var tensor = TileTensor(Span(storage), row_major(Coord(Idx[2], Idx[3])))
var array = to_numpy(tensor)  # a 2x3 NumPy float32 array

Constraints:

LayoutType must be flat: nested layouts have no single extent per axis to hand to NumPy.

Parameters:

Args:

Returns:

PythonObject: A NumPy ndarray of dtype dtype with the tensor's shape.

Raises:

If NumPy is unavailable, or if the underlying NumPy calls fail.

Was this page helpful?