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 arrayConstraints:
LayoutType must be flat: nested layouts have no single extent per
axis to hand to NumPy.
Parameters:
- dtype (
DType): The element dtype of the tensor. - LayoutType (
TensorLayout): The layout of the tensor. - origin (
Origin[mut=origin.mut]): The origin of the tensor. - Engine (
TensorEngine): The engine of the tensor.
Args:
- tensor (
TileTensor[dtype, LayoutType, origin, Engine=Engine]): The tensor to copy.
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.