mlx/docs/build/html/_sources/usage/saving_and_loading.rst

82 lines
2.0 KiB
ReStructuredText
Raw Permalink Normal View History

2024-01-11 06:14:38 +08:00
.. _saving_and_loading:
Saving and Loading Arrays
=========================
.. currentmodule:: mlx.core
MLX supports multiple array serialization formats.
.. list-table:: Serialization Formats
2024-10-26 04:23:45 +08:00
:widths: 20 8 25 25
2024-01-11 06:14:38 +08:00
:header-rows: 1
2024-10-26 04:23:45 +08:00
* - Format
- Extension
2024-01-11 06:14:38 +08:00
- Function
2024-10-26 04:23:45 +08:00
- Notes
* - NumPy
- ``.npy``
2024-01-11 06:14:38 +08:00
- :func:`save`
- Single arrays only
2024-10-26 04:23:45 +08:00
* - NumPy archive
- ``.npz``
2024-01-11 06:14:38 +08:00
- :func:`savez` and :func:`savez_compressed`
2024-10-26 04:23:45 +08:00
- Multiple arrays
2024-01-11 06:14:38 +08:00
* - Safetensors
2024-10-26 04:23:45 +08:00
- ``.safetensors``
2024-01-11 06:14:38 +08:00
- :func:`save_safetensors`
2024-10-26 04:23:45 +08:00
- Multiple arrays
* - GGUF
- ``.gguf``
2024-01-11 06:14:38 +08:00
- :func:`save_gguf`
- Multiple arrays
The :func:`load` function will load any of the supported serialization
formats. It determines the format from the extensions. The output of
2024-10-26 04:23:45 +08:00
:func:`load` depends on the format.
2024-01-11 06:14:38 +08:00
Here's an example of saving a single array to a file:
.. code-block:: shell
>>> a = mx.array([1.0])
>>> mx.save("array", a)
The array ``a`` will be saved in the file ``array.npy`` (notice the extension
is automatically added). Including the extension is optional; if it is missing
it will be added. You can load the array with:
.. code-block:: shell
2024-03-31 08:32:20 +08:00
>>> mx.load("array.npy")
2024-01-11 06:14:38 +08:00
array([1], dtype=float32)
Here's an example of saving several arrays to a single file:
.. code-block:: shell
>>> a = mx.array([1.0])
>>> b = mx.array([2.0])
>>> mx.savez("arrays", a, b=b)
For compatibility with :func:`numpy.savez` the MLX :func:`savez` takes arrays
as arguments. If the keywords are missing, then default names will be
provided. This can be loaded with:
.. code-block:: shell
>>> mx.load("arrays.npz")
{'b': array([2], dtype=float32), 'arr_0': array([1], dtype=float32)}
In this case :func:`load` returns a dictionary of names to arrays.
The functions :func:`save_safetensors` and :func:`save_gguf` are similar to
:func:`savez`, but they take as input a :obj:`dict` of string names to arrays:
.. code-block:: shell
>>> a = mx.array([1.0])
>>> b = mx.array([2.0])
>>> mx.save_safetensors("arrays", {"a": a, "b": b})