caikit.core.modules.saver

Local saver implementation for saving modules to disk. Contains recursive functions for loading modules saved inside modules.

Attributes

log

error

Classes

ModuleSaver

A module saver that provides common functionality used for saving modules and also a context

Module Contents

caikit.core.modules.saver.log[source]
caikit.core.modules.saver.error
class caikit.core.modules.saver.ModuleSaver(module: caikit.core.modules.base.ModuleBase, model_path, exist_ok=True)[source]

A module saver that provides common functionality used for saving modules and also a context manager that cleans up in case an error is encountered during the save process for a model_path that did not already exist.

SAVED_KEY_NAME = 'saved'
CREATED_KEY_NAME = 'created'
TRACKING_KEY_NAME = 'tracking_id'
MODULE_VERSION_KEY_NAME = 'version'
MODULE_ID_KEY_NAME = 'module_id'
MODULE_CLASS_KEY_NAME = 'module_class'
model_path = b'.'
exist_ok = True
config
add_dir(relative_path, base_relative_path='')[source]

Create a directory inside the model_path for this saver.

Args:
relative_path (str): A path relative to this saver’s model_path

denoting the directory to create.

base_relative_path (str): A path, relative to this saver’s

model_path, in which relative_path will be created.

Returns:
str, str: A tuple containing both the relative_path and

absolute_path to the directory created.

Examples:
>>> with ModelSaver('/path/to/model') as saver:
>>>     rel_path, abs_path = saver.add_dir('word_embeddings', 'model_data')
>>> print(rel_path)
model_data/word_embeddings
>>> print(abs_path)
/path/to/model/model_data/word_embeddings
copy_file(file_path, relative_path='')[source]

Copy an external file into a subdirectory of the model_path for this saver.

Args:

file_path (str): Absolute path to the external file to copy. relative_path (str): The relative path inside of model_path where

the file will be copied to. If set to the empty string (default) then the file will be placed directly in the model_path directory.

Returns:
str, str: A tuple containing both the relative_path and

absolute_path to the copied file.

save_object(obj, filename, serializer, relative_path='')[source]

Save a Python object using the provided ObjectSerializer.

Args:

obj (any): The Python object to save filename (str): The filename to use for the saved object serializer (ObjectSerializer): An ObjectSerializer instance (e.g.,

YAMLSerializer) that should be used to serialize the object

relative_path (str): The relative path inside of model_path where

the object will be saved

update_config(additional_config)[source]

Add items to this saver’s config dictionary.

Args:
additional_config (dict): A dictionary of config options to add the

this saver’s configuration.

Notes:

The behavior of this method matches dict.update and is equivalent to calling saver.config.update. The saver.config dictionary may be accessed directly for more sophisticated manipulation of the configuration.

save_module(module, relative_path, **kwargs)[source]

Save a CaikitCore module within a workflow artifact and add a reference to the config.

Args:
module (caikit.core.ModuleBase): The CaikitCore module to save as

part of this workflow

relative_path (str): The relative path inside of model_path where

the module will be saved

**kwargs: dict

key-value pair of parameters to be passed to module.save

save_module_list(modules, config_key, **kwargs)[source]

Save a list of CaikitCore modules within a workflow artifact and add a reference to the config.

Args:
modules (dict{str -> caikit.core.ModuleBase}): A dict with module

relative path as key and a CaikitCore module as value to save as part of this workflow

config_key (str): The config key inside of model_path where the

modules’ relative path with be referenced

**kwargs: dict

key-value pair of parameters to be passed to module.save

Returns:
list_of_rel_path: list(str)

List of relative paths where the modules are saved

list_of_abs_path: list(str)

List of absolute paths where the modules are saved

__enter__()[source]

Enter the module saver context. This creates the model_path directory. If this context successfully exits, then the model configuration and all files it contains will be written and saved to disk inside the model_path directory.

If exist_ok is False, an exception will be raised before touching existing model_path files.

If any uncaught exceptions are thrown inside this context, and exist_ok is False, then this new model_path will be removed. If exist_ok is True, the files will be kept and may include incomplete updates.

__exit__(exc_type, exc_val, exc_tb)[source]

Exit the module saver context. If this context successfully exits, then the model configuration and all files it contains will be written and saved to disk inside the model_path directory.

If any uncaught exceptions are thrown inside this context, and exist_ok is False, then this new model_path will be removed. If exist_ok is True, the files will be kept and may include incomplete updates.