Miscellaneous¶
Version number¶
-
const char *mts_version(void)¶
Get the runtime version of the metatensor library as a string.
This version follows the
<major>.<minor>.<patch>[-<dev>]format.
-
METATENSOR_VERSION¶
Macro containing the compile-time version of metatensor, as a string
-
METATENSOR_VERSION_MAJOR¶
Macro containing the compile-time major version number of metatensor, as an integer
-
METATENSOR_VERSION_MINOR¶
Macro containing the compile-time minor version number of metatensor, as an integer
-
METATENSOR_VERSION_PATCH¶
Macro containing the compile-time patch version number of metatensor, as an integer
Error handling¶
-
mts_status_t mts_last_error(const char **message, const char **origin, void **data)¶
Get the last error message that was created on the current thread.
- Parameters:
message – if not NULL, this will be set to the last error message, as a NULL-terminated string
origin – if not NULL, this will be set to the origin of the last error, as a NULL-terminated string
data – if not NULL, this will be set to the custom data of the last error
- Returns:
The status code of this operation.
-
mts_status_t mts_set_last_error(const char *message, const char *origin, void *data, void (*data_deleter)(void*))¶
Set the last error message for the current thread.
This is useful when the error is created in a callback provided by the user of metatensor.
- Parameters:
message – the error message to set, as a NULL-terminated string
origin – the origin of the error, as a NULL-terminated string. This can be used to check if the
datafield was defined by the same code that is callingmts_last_errorwhen retrieving custom data.data – an arbitrary pointer that can be retrieved later with
mts_last_error.data_deleter – deleter for the custom data, this function will be called with
dataas argument when the last error is replaced by another one.
-
void mts_disable_panic_printing(void)¶
Disable printing of the message to stderr when some Rust code reaches a panic.
All panics from Rust code are caught anyway and translated to an error status code, and the message is stored and accessible through
mts_last_error. To print the error message and Rust backtrace anyway, users can set theRUST_BACKTRACEenvironment variable to 1.
-
enum mts_status_t¶
Status type returned by all functions in the C API.
The value 0 (
MTS_SUCCESS) is used to indicate successful operations.Values:
-
enumerator MTS_SUCCESS¶
Status code used when a function succeeded
-
enumerator MTS_INVALID_PARAMETER_ERROR¶
Status code used when a function got an invalid parameter
-
enumerator MTS_IO_ERROR¶
Status code indicating I/O error when loading/writing
mts_tensormap_tto a file
-
enumerator MTS_SERIALIZATION_ERROR¶
Status code indicating errors in the serialization format when loading/writing
mts_tensormap_tto a file
-
enumerator MTS_BUFFER_SIZE_ERROR¶
Status code used when a memory buffer is too small to fit the requested data
-
enumerator MTS_CALLBACK_ERROR¶
Status code indicating errors that come from callbacks provided by the user of metatensor. The error message and arbitrary custom data can be stored using
mts_set_last_errorinside the callback, and retrieved later withmts_last_error.
-
enumerator MTS_INTERNAL_ERROR¶
Status code used when there was an internal error, i.e. there is a bug inside metatensor itself
-
enumerator MTS_SUCCESS¶
Serialization¶
Tensors¶
mts_tensormap_save(): serialize and save amts_tensormap_tto a filemts_tensormap_load(): load a serializedmts_tensormap_tfrom a filemts_tensormap_save_buffer(): serialize and save amts_tensormap_tto a in-memory buffermts_tensormap_load_buffer(): load a serializedmts_tensormap_tfrom an in-memory buffer
-
struct mts_tensormap_t *mts_tensormap_load(const char *path, mts_create_array_callback_t create_array)¶
Load a tensor map from the file at the given path.
Arrays for the values and gradient data will be created with the given
create_arraycallback, and filled by this function with the corresponding data.The memory allocated by this function should be released using
mts_tensormap_free.TensorMapare serialized using numpy’s NPZ format, i.e. a ZIP file without compression (storage method is STORED), where each file is stored as a.npyarray. Both the ZIP and NPY format are well documented:ZIP: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
NPY: https://numpy.org/doc/stable/reference/generated/numpy.lib.format.html
We add other restriction on top of these formats when saving/loading data. First,
Labelsinstances are saved as structured array, see thelabelsmodule for more information.Second, the path of the files in the archive also carry meaning. The keys of the
TensorMapare stored in/keys.npy, and then different blocks are stored as/ blocks / <block_id> / values / samples.npy / values / components / 0.npy / <...>.npy / <n_components>.npy / values / properties.npy / values / data.npy # optional sections for gradients, one by parameter / gradients / <parameter> / samples.npy / components / 0.npy / <...>.npy / <n_components>.npy / data.npy
Finally, the info (i.e. global metadata) of the
TensorMapis stored inside theinfo.jsonfile, unless empty.- Parameters:
path – path to the file as a NULL-terminated UTF-8 string
create_array – callback function that will be used to create data arrays inside each block
- Returns:
A pointer to the newly allocated tensor map, or a
NULLpointer in case of error. In case of error, you can usemts_last_error()to get the error message.
-
mts_status_t mts_tensormap_save(const char *path, const struct mts_tensormap_t *tensor)¶
Save a tensor map to the file at the given path.
If the file already exists, it is overwritten. The recommended file extension when saving data is
.mts, to prevent confusion with generic.npzfiles.- Parameters:
path – path to the file as a NULL-terminated UTF-8 string
tensor – tensor map to save to the file
- Returns:
The status code of this operation. If the status is not
MTS_SUCCESS, you can usemts_last_error()to get the full error message.
-
struct mts_tensormap_t *mts_tensormap_load_buffer(const uint8_t *buffer, uintptr_t buffer_count, mts_create_array_callback_t create_array)¶
Load a tensor map from the given in-memory buffer.
Arrays for the values and gradient data will be created with the given
create_arraycallback, and filled by this function with the corresponding data.The memory allocated by this function should be released using
mts_tensormap_free.- Parameters:
buffer – buffer containing a previously serialized
mts_tensormap_tbuffer_count – number of elements in the buffer
create_array – callback function that will be used to create data arrays inside each block
- Returns:
A pointer to the newly allocated tensor map, or a
NULLpointer in case of error. In case of error, you can usemts_last_error()to get the error message.
-
mts_status_t mts_tensormap_save_buffer(uint8_t **buffer, uintptr_t *buffer_count, void *realloc_user_data, mts_realloc_buffer_t realloc, const struct mts_tensormap_t *tensor)¶
Save a tensor map to an in-memory buffer.
On input,
*buffershould contain the address of a starting buffer (which can be NULL) and*buffer_countshould contain the size of the allocation.On output,
*bufferwill contain the serialized data, and*buffer_countthe total number of written bytes (which might be less than the allocation size).Users of this function are responsible for freeing the
*bufferwhen they are done with it, using the function matching therealloccallback.- Parameters:
buffer – pointer to the buffer the tensor will be stored to, which can change due to reallocations.
buffer_count – pointer to the buffer size on input, number of written bytes on output
realloc_user_data – custom data for the
realloccallback. This will be passed as the first argument toreallocas-is.realloc – function that allows to grow the buffer allocation
tensor – tensor map that will be saved to the buffer
- Returns:
The status code of this operation. If the status is not
MTS_SUCCESS, you can usemts_last_error()to get the full error message.
-
typedef mts_status_t (*mts_create_array_callback_t)(const uintptr_t *shape, uintptr_t shape_count, DLDataType dtype, struct mts_array_t *array)¶
Function pointer to create a new
mts_array_twhen de-serializing tensor maps.This function gets the
shapeof the array (theshapecontainsshape_countelements) and thedtype(aDLDataTypedescribing the element type), and should fillarraywith a new validmts_array_tor return non-zeromts_status_t.The newly created array should live on CPU, since metatensor will use
mts_array_t.as_dlpackto get the data pointer and write to it.This function should return
MTS_SUCCESSon success, orMTS_CALLBACK_ERRORon failure. In case of failure, the implementation should callmts_set_last_errorwith an appropriate error message before returning.
-
typedef uint8_t *(*mts_realloc_buffer_t)(void *user_data, uint8_t *ptr, uintptr_t new_size)¶
Function pointer to grow in-memory buffers for
mts_tensormap_save_bufferandmts_labels_save_buffer.This function takes an existing pointer in
ptrand a new length innew_size, and grow the allocation. If the pointer isNULL, it should create a new allocation. If it is unable to allocate memory, it should return aNULLpointer. This follows the API of the standard C functionrealloc, with an additional parameteruser_datathat can be used to hold custom data.
Blocks¶
mts_block_save(): serialize and save amts_block_tto a filemts_block_load(): load a serializedmts_block_tfrom a filemts_block_save_buffer(): serialize and save amts_block_tto a in-memory buffermts_block_load_buffer(): load a serializedmts_block_tfrom a in-memory buffer
-
struct mts_block_t *mts_block_load(const char *path, mts_create_array_callback_t create_array)¶
Load a tensor block from the file at the given path.
Arrays for the values and gradient data will be created with the given
create_arraycallback, and filled by this function with the corresponding data.The memory allocated by this function should be released using
mts_block_free.See
mts_tensormap_loadfor more information about the format used to serialize the data.- Parameters:
path – path to the file as a NULL-terminated UTF-8 string
create_array – callback function that will be used to create data arrays inside each block
- Returns:
A pointer to the newly allocated block, or a
NULLpointer in case of error. In case of error, you can usemts_last_error()to get the error message.
-
mts_status_t mts_block_save(const char *path, const struct mts_block_t *block)¶
Save a tensor block to the file at the given path.
If the file already exists, it is overwritten. The recommended file extension when saving data is
.mts, to prevent confusion with generic.npzfiles.- Parameters:
path – path to the file as a NULL-terminated UTF-8 string
block – tensor block to save to the file
- Returns:
The status code of this operation. If the status is not
MTS_SUCCESS, you can usemts_last_error()to get the full error message.
-
struct mts_block_t *mts_block_load_buffer(const uint8_t *buffer, uintptr_t buffer_count, mts_create_array_callback_t create_array)¶
Load a tensor block from the given in-memory buffer.
Arrays for the values and gradient data will be created with the given
create_arraycallback, and filled by this function with the corresponding data.The memory allocated by this function should be released using
mts_block_free.- Parameters:
buffer – buffer containing a previously serialized
mts_block_tbuffer_count – number of elements in the buffer
create_array – callback function that will be used to create data arrays inside each block
- Returns:
A pointer to the newly allocated tensor block, or a
NULLpointer in case of error. In case of error, you can usemts_last_error()to get the error message.
-
mts_status_t mts_block_save_buffer(uint8_t **buffer, uintptr_t *buffer_count, void *realloc_user_data, mts_realloc_buffer_t realloc, const struct mts_block_t *block)¶
Save a tensor block to an in-memory buffer.
On input,
*buffershould contain the address of a starting buffer (which can be NULL) and*buffer_countshould contain the size of the allocation.On output,
*bufferwill contain the serialized data, and*buffer_countthe total number of written bytes (which might be less than the allocation size).Users of this function are responsible for freeing the
*bufferwhen they are done with it, using the function matching therealloccallback.- Parameters:
buffer – pointer to the buffer the block will be stored to, which can change due to reallocations.
buffer_count – pointer to the buffer size on input, number of written bytes on output
realloc_user_data – custom data for the
realloccallback. This will be passed as the first argument toreallocas-is.realloc – function that allows to grow the buffer allocation
block – tensor block that will be saved to the buffer
- Returns:
The status code of this operation. If the status is not
MTS_SUCCESS, you can usemts_last_error()to get the full error message.
Labels¶
mts_labels_save(): serialize and save amts_labels_tto a filemts_labels_load(): load a serializedmts_labels_tfrom a filemts_labels_save_buffer(): serialize and save amts_labels_tto an in-memory buffermts_labels_load_buffer(): load serializedmts_labels_tfrom an in-memory buffermts_tensormap_load(): create the Rust-side data for the labels
-
const struct mts_labels_t *mts_labels_load(const char *path)¶
Load labels from the file at the given path.
This function allocates memory which must be released with
mts_labels_freewhen you don’t need it anymore.- Parameters:
path – path to the file as a NULL-terminated UTF-8 string
- Returns:
A pointer to the newly allocated labels, or a
NULLpointer in case of error. In case of error, you can usemts_last_error()to get the error message.
-
mts_status_t mts_labels_save(const char *path, const struct mts_labels_t *labels)¶
Save labels to the file at the given path.
If the file already exists, it is overwritten. The recommended file extension when saving data is
.mts, to prevent confusion with generic.npzfiles.- Parameters:
path – path to the file as a NULL-terminated UTF-8 string
labels – pointer to labels to save to the file
- Returns:
The status code of this operation. If the status is not
MTS_SUCCESS, you can usemts_last_error()to get the full error message.
-
const struct mts_labels_t *mts_labels_load_buffer(const uint8_t *buffer, uintptr_t buffer_count)¶
Load labels from the given in-memory buffer.
This function allocates memory which must be released with
mts_labels_freewhen you don’t need it anymore.- Parameters:
buffer – buffer containing a previously serialized
mts_labels_tbuffer_count – number of elements in the buffer
- Returns:
A pointer to the newly allocated labels, or a
NULLpointer in case of error. In case of error, you can usemts_last_error()to get the error message.
-
mts_status_t mts_labels_save_buffer(uint8_t **buffer, uintptr_t *buffer_count, void *realloc_user_data, mts_realloc_buffer_t realloc, const struct mts_labels_t *labels)¶
Save labels to an in-memory buffer.
On input,
*buffershould contain the address of a starting buffer (which can be NULL) and*buffer_countshould contain the size of the allocation (0 if*bufferis NULL).On output,
*bufferwill contain the serialized data, and*buffer_countthe total number of written bytes (which might be less than the allocation size).Users of this function are responsible for freeing the
*bufferwhen they are done with it, using the function matching therealloccallback.- Parameters:
buffer – pointer to the buffer the tensor will be stored to, which can change due to reallocations.
buffer_count – pointer to the buffer size on input, number of written bytes on output
realloc_user_data – custom data for the
realloccallback. This will be passed as the first argument toreallocas-is.realloc – function that allows to grow the buffer allocation
labels – pointer to labels that will be saved to the buffer
- Returns:
The status code of this operation. If the status is not
MTS_SUCCESS, you can usemts_last_error()to get the full error message.