General Logging

In 4.0 the context of the information is expressed through the is_input_to and is_output_to arguments: strings naming the activities that consume or produce the item (for instance "Training_0", "Validation_0", "Testing_0").

Log Parameters

To specify arbitrary training parameters used during the execution of the experiment, the user can call the following function.

yprov4ml.log_param(
    key: str, 
    value: Any, 
    is_input_to : Optional[str] = None, 
    is_output_to : Optional[str] = None
)
ParameterTypeDescription
keystringRequired. Name of the parameter
valuestringRequired. Value of the parameter
is_input_toOptional[str]Optional. Activity that uses the parameter as input
is_output_toOptional[str]Optional. Activity or tool that produced the parameter

Log Artifacts

To log artifacts, the user can call the following function.

yprov4ml.log_artifact(
    artifact_name : str, 
    artifact_path : str, 
    is_input_to : Optional[str] = None, 
    is_output_to : Optional[str] = None, 
    step: int = 0, 
    is_model : bool = False, 
    other_attrs : Optional[dict] = None, 
)
ParameterTypeDescription
artifact_namestringRequired. Label to give to the artifact
artifact_pathstringRequired. Path to the artifact
is_input_toOptional[str]Optional. Activity that uses the artifact as input
is_output_toOptional[str]Optional. Activity that generates the artifact
stepintOptional. Step of the artifact (default 0)
is_modelboolOptional. Whether the artifact is a model
other_attrsOptional[dict]Optional. Extra PROV attributes to attach to the artifact entity

The function logs the artifact in the current experiment. The artifact can be a file or a directory. All logged artifacts are saved in the artifacts directory of the current experiment, while the related information is saved in the PROV-JSON file, along with a reference to the file. Use is_input_to for artifacts consumed by an activity (e.g. pretrained model weights) and is_output_to for artifacts it produces.

Log Models

yprov4ml.log_model(
    model_name: str, 
    model: Union[torch.nn.Module, Any], 
    is_input_to : Optional[str] = None, 
    is_output_to : Optional[str] = None, 
    log_model_info: bool = True, 
    log_model_layers : bool = False,
    other_attrs : Optional[dict] = None,
)
ParameterTypeDescription
model_namestringRequired. Name of the model
modelUnion[torch.nn.Module, Any]Required. The model to be logged
is_input_toOptional[str]Optional. Activity that uses the model as input
is_output_toOptional[str]Optional. Activity that produces the model
log_model_infoboolOptional. Whether to log model information
log_model_layersboolOptional. Whether to log model layers
other_attrsOptional[dict]Optional. Extra PROV attributes to attach to the model entity

It sets the model for the current experiment. It can be called anywhere before the end of the experiment. The same call also logs some model information, such as the number of parameters and the model architecture memory footprint. The saving of these information can be toggled with the log_model_info = False parameter. The model layers details can be logged in an external .json file, which will be linked to the provenance file as an artefact. The parameters saved for each layer depend on the type of the latter, but generally include input and output size, as well as dtype.

yprov4ml.save_model_version(
    model_name: str, 
    model: Union[torch.nn.Module, Any], 
    is_input_to : Optional[str] = None, 
    is_output_to : Optional[str] = None, 
    step: Optional[int] = None, 
    other_attrs : Optional[dict] = None, 
)

The save_model_version function saves the state of a PyTorch model and logs it as an artifact, enabling version control and tracking within machine learning experiments.

ParameterTypeDescription
model_nameUnion[str, Path]Required. The name (or path) under which to save the model; a .pt suffix is added if missing.
modeltorch.nn.ModuleRequired. The PyTorch model to be saved.
is_input_toOptional[str]Optional. Activity that uses the model as input.
is_output_toOptional[str]Optional. Activity that produces the model.
stepOptional[int]Optional. The step or epoch number associated with the saved model.
other_attrsOptional[dict]Optional. Extra PROV attributes to attach to the entity.

This function saves the model's state dictionary to a specified directory and logs the saved model file as an artifact for provenance tracking. It then logs the saved model file using log_artifact.

Log Datasets

yProv4ML offers helper functions to log information and stats on specific datasets.

yprov4ml.log_dataset(
    dataset_label : str, 
    dataset : Union[DataLoader, Subset, Dataset], 
    is_input_to : Optional[str] = None, 
    is_output_to : Optional[str] = None, 
    log_dataset_info : bool = True, 
    other_attrs : dict = {}
)
ParameterTypeDescription
dataset_labelstringRequired. The label of the dataset
datasetUnion[DataLoader, Subset, Dataset]Required. The dataset to be logged
is_input_toOptional[str]Optional. Activity that uses the dataset as input
is_output_toOptional[str]Optional. Activity that produces the dataset
log_dataset_infoboolOptional. Whether to log the dataset information
other_attrsdictOptional. Extra PROV attributes to attach to the dataset entity

The function logs the dataset in the current experiment. The dataset can be a DataLoader, a Subset, or a Dataset class from pytorch. A SHA-256 checksum of the dataset is always recorded. Parameters which are logged include batch size, number of workers, whether the dataset is shuffled, the number of batches and the number of total samples.

Logging through Wrappers

yProv4ML offers helper wrappers to log information automatically.

‼ These are only supported for PyTorch objects.

ProvenanceTrackedFunction

class ProvenanceTrackedFunction:
    def __init__(self, 
        func : Any, 
        is_input_to : Optional[str] = None, 
        is_output_to : Optional[str] = None
    )
ParameterTypeDefaultDescription
funcAnyRequiredThe target callable function or object to wrap (e.g., PyTorch loss function or metric evaluator).
is_input_toOptional[str]NoneActivity the call is an input to; defaults to the wrapped object's type name.
is_output_toOptional[str]NoneActivity or tool that produces the output.

WeightDistributionTrackedModel

class WeightDistributionTrackedModel(nn.Module):
    def __init__(self, 
        model_label : str, 
        model : Any, 
        is_input_to : Optional[str] = None, 
        is_output_to : Optional[str] = None, 
        chunk_size : int = 64
    )

Tracks per-layer weight statistics per epoch. Arguments follow the same convention as the other 4.0 logging calls.

ProvenanceTrackedModel

class ProvenanceTrackedModel(nn.Module):
    def __init__(self, 
        model_label : str, 
        model : Any, 
        context : Optional[str] = None, 
        chunk_size : int = 64
    )
ParameterTypeDefaultDescription
model_labelstrRequiredLabel identifier for the model. Used as the output Zarr dataset name (<model_label>.zarr).
modelAny (nn.Module)RequiredThe underlying PyTorch model instance to track.
contextOptional[str]NoneOptional execution context (e.g. stage or step name) associated with provenance tracking.
chunk_sizeint64Zarr chunk dimension along the batch axis for layer datasets.