Data arrays¶
This part of metatensor contains advanced functionalities related to how data is
stored inside metatensor.TensorBlock. Most users should not have to
interact with this!
- class metatensor.Array¶
An
Arraycontains the actual data stored in ametatensor.TensorBlock.This data is manipulated by
metatensorin a completely opaque way: this library does not know what’s inside the arrays appart from a small set of constrains:the array contains numeric data (
metatensor.load()andmetatensor.save()additionally requires contiguous arrays of 64-bit IEEE-754 floating points numbers);they are stored as row-major, n-dimensional arrays with at least 2 dimensions;
it is possible to create new arrays and move data from one array to another.
The actual type of an
Arraydepends on how themetatensor.TensorBlockormetatensor.Labelswas created. Data created from Python can be eithernumpy.ndarrayortorch.Tensor. Data created outside of Python is accessed through DLPack, and can be returned asnumpy.ndarray,torch.Tensororctypes_dlpack.DLPackArraydepending on thearray_backendchoice made when creating the corresponding Python object.
Data allocated outside of Python¶
Data stored in a metatensor.TensorMap,
metatensor.TensorBlock or metatensor.Labels can be
allocated outside of Python, for example by the metatensor library itself, or by
code written in another language (C, C++, Rust, …) and given to Python with
metatensor.TensorMap.unsafe_from_ptr() and related functions.
Data allocated by Python (i.e. numpy.ndarray or
torch.Tensor given to metatensor) is always returned as-is. Other
data is accessed through DLPack, without copying it, and returned using one
of the following array backends:
"numpy": data is returned asnumpy.ndarray. This requires the data to live on CPU;"torch": data is returned astorch.Tensor.
The array backend is chosen with the array_backend parameter of
metatensor.TensorMap.unsafe_from_ptr(),
metatensor.TensorBlock.unsafe_from_ptr(),
metatensor.Labels.unsafe_from_ptr(),
metatensor.io.load_custom_array() and similar loading functions. It is
then used by all the objects created from these, (blocks, gradients, labels, the
output of operations like metatensor.Labels.union(), …).
The dtype and device of blocks and labels are returned using the types
of the corresponding array backend (e.g. torch.dtype and
torch.device for "torch").
To use the data with other array libraries,
metatensor.TensorBlock.values_dlpack() and
metatensor.Labels.values_dlpack() give access to the values of blocks
and labels as ctypes_dlpack.DLPackArray, regardless of where the
data was allocated. These can be given to any library supporting DLPack, for
example jax.dlpack.from_dlpack(array).
Deprecated functionalities¶
- metatensor.register_external_data_wrapper(origin, klass)[source]¶
Deprecated since version 0.2.7: This function does nothing. Data from any non-Python origin is now accessible through DLPack, use the
array_backendparameter ofTensorMap.unsafe_from_ptr(),TensorBlock.unsafe_from_ptr(),Labels.unsafe_from_ptr()or the loading functions to choose which array type to use for this data.- Parameters:
origin – data origin name as a string, corresponding to the output of
mts_array_t.origin()klass – wrapper class to use for this origin
- class metatensor.ExternalCpuArray(mts_array: mts_array_t, parent: Any)[source]¶
Deprecated since version 0.2.7: Data from non-Python origins is now returned as plain
numpy.ndarrayby default, this class is no longer used by metatensor.Small wrapper class around
np.ndarray, adding a reference to a parent Python object that actually owns the memory used inside the array. This prevents the parent from being garbage collected while the ndarray is still alive, thus preventing use-after-free.This is intended to be used to wrap Rust-owned memory inside a numpy’s ndarray, while making sure the memory owner is kept around for long enough.
- Parameters:
mts_array (mts_array_t) – raw array to wrap in a Python-compatible class
parent (Any) – owner of the raw array, we will keep a reference to this python object
- class metatensor.ExternalCudaArray(mts_array: mts_array_t, parent: Any, *, device_type: int = 2, device_id: int = 0)[source]¶
Deprecated since version 0.2.7: This class is no longer used by metatensor, use
array_backend="torch"to access data from non-Python origins astorch.Tensor.Factory that wraps non-Python data on a CUDA device as a
torch.Tensorvia DLPack, keeping a reference to a parent Python object to prevent use-after-free.This is the CUDA counterpart to
ExternalCpuArray, intended for data that lives in CUDA memory. Requires PyTorch.For CUDA data (
device_type=2), we go through CuPy when available for true zero-copy import, then convert to atorch.Tensor. If CuPy is not installed we fall back totorch.from_dlpackdirectly.