fn

from_numpy

Tensor
from_numpy(arr: np.ndarray)
source

Create a CPU tensor from a NumPy ndarray with shared storage.

The returned tensor wraps the array's existing buffer — no data is copied — and inherits the array's dtype according to the canonical NumPy → Lucid mapping (np.float32lucid.float32, np.int64lucid.int64, etc.). Because storage is shared, mutations in the array are visible in the tensor and vice versa.

Parameters

arrnumpy.ndarray
Source array. Must reside in CPU memory. Any layout (C / Fortran / strided) is accepted; the resulting tensor preserves the array's strides where possible.

Returns

Tensor

A CPU tensor sharing storage with arr.

Raises

TypeError
If arr is not a NumPy ndarray.
ValueError
If arr's dtype has no corresponding Lucid dtype (e.g. np.float128 on some platforms).

Notes

This is one of the documented H4 bridge boundaries — the only places where Lucid is allowed to take a NumPy array as input. To move the result onto a Metal device, chain Tensor.to::

t = lucid.from_numpy(arr).to("metal")

Examples

>>> import lucid, numpy as np
>>> arr = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)
>>> t = lucid.from_numpy(arr)
>>> t.dtype
float32
>>> arr[0, 0] = 99.0          # mutate the source
>>> t[0, 0].item()            # change visible in the tensor
99.0