caikit.core.modules.saver ========================= .. py:module:: caikit.core.modules.saver .. autoapi-nested-parse:: Local saver implementation for saving modules to disk. Contains recursive functions for loading modules saved inside modules. Attributes ---------- .. autoapisummary:: caikit.core.modules.saver.log caikit.core.modules.saver.error Classes ------- .. autoapisummary:: caikit.core.modules.saver.ModuleSaver Module Contents --------------- .. py:data:: log .. py:data:: error .. py:class:: ModuleSaver(module: caikit.core.modules.base.ModuleBase, model_path, exist_ok=True) 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. .. py:attribute:: SAVED_KEY_NAME :value: 'saved' .. py:attribute:: CREATED_KEY_NAME :value: 'created' .. py:attribute:: TRACKING_KEY_NAME :value: 'tracking_id' .. py:attribute:: MODULE_VERSION_KEY_NAME :value: 'version' .. py:attribute:: MODULE_ID_KEY_NAME :value: 'module_id' .. py:attribute:: MODULE_CLASS_KEY_NAME :value: 'module_class' .. py:attribute:: model_path :value: b'.' .. py:attribute:: exist_ok :value: True .. py:attribute:: config .. py:method:: add_dir(relative_path, base_relative_path='') 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 .. py:method:: copy_file(file_path, relative_path='') 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. .. py:method:: save_object(obj, filename, serializer, relative_path='') 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 .. py:method:: update_config(additional_config) 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. .. py:method:: save_module(module, relative_path, **kwargs) 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 .. py:method:: save_module_list(modules, config_key, **kwargs) 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 .. py:method:: __enter__() 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. .. py:method:: __exit__(exc_type, exc_val, exc_tb) 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.