Skip to content

raytune_parser

Ray Tune results parser for extracting and saving best model configurations and weights.

Classes:

  • RayTuneMetrics

    TypedDict for storing Ray Tune metrics results.

  • RayTuneOptimizer

    TypedDict for storing Ray Tune optimizer state.

  • RayTuneResult

    TypedDict for storing Ray Tune optimization results.

  • TuneParser

    Parser class for Ray Tune results to extract best configurations and model weights.

RayTuneMetrics

Bases: TypedDict

TypedDict for storing Ray Tune metrics results.

RayTuneOptimizer

Bases: TypedDict

TypedDict for storing Ray Tune optimizer state.

RayTuneResult

Bases: TypedDict

TypedDict for storing Ray Tune optimization results.

TuneParser

TuneParser(result: ResultGrid)

Parser class for Ray Tune results to extract best configurations and model weights.

Methods:

Source code in src/stimulus/learner/raytune_parser.py
39
40
41
42
def __init__(self, result: ResultGrid) -> None:
    """Initialize with the given Ray Tune result grid."""
    self.result: ResultGrid = result
    self.best_result: Result = self._validate_best_result()

fix_config_values

fix_config_values(config: dict[str, Any]) -> dict[str, Any]

Correct config values.

This method modifies the configuration dictionary to remove or convert non-serializable objects (such as Ray ObjectRefs) so that the entire dictionary can be safely dumped to a YAML file.

Parameters:

  • config (dict[str, Any]) –

    Configuration dictionary to fix.

Returns:

  • dict[str, Any]

    Fixed configuration dictionary.

Source code in src/stimulus/learner/raytune_parser.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def fix_config_values(self, config: dict[str, Any]) -> dict[str, Any]:
    """Correct config values.

    This method modifies the configuration dictionary to remove or convert
    non-serializable objects (such as Ray ObjectRefs) so that the entire dictionary
    can be safely dumped to a YAML file.

    Args:
        config: Configuration dictionary to fix.

    Returns:
        Fixed configuration dictionary.
    """
    # Replace the model class with its name for serialization purposes
    config["model"] = config["model"].__name__

    # Remove keys that contain non-serializable objects
    keys_to_remove = [
        "_debug",
        "tune_run_path",
        "_training_ref",
        "_validation_ref",
        "encoder_loader",  # if this key holds a non-serializable object
    ]
    for key in keys_to_remove:
        config.pop(key, None)

    return config

get_best_config

get_best_config() -> dict[str, Any]

Get the best config from the results.

Returns:

  • dict[str, Any]

    The configuration dictionary of the best result.

Raises:

Source code in src/stimulus/learner/raytune_parser.py
60
61
62
63
64
65
66
67
68
69
70
71
72
def get_best_config(self) -> dict[str, Any]:
    """Get the best config from the results.

    Returns:
        The configuration dictionary of the best result.

    Raises:
        ValueError: If the config is missing.
    """
    config: dict[str, Any] | None = self.best_result.config
    if config is None:
        raise ValueError("Best result does not contain a configuration.")
    return config

get_best_model

get_best_model() -> dict[str, Tensor]

Get the best model weights from the results.

Returns:

  • dict[str, Tensor]

    Dictionary of model weights.

Raises:

Source code in src/stimulus/learner/raytune_parser.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def get_best_model(self) -> dict[str, torch.Tensor]:
    """Get the best model weights from the results.

    Returns:
        Dictionary of model weights.

    Raises:
        ValueError: If the checkpoint is missing.
    """
    if self.best_result.checkpoint is None:
        raise ValueError("Best result does not contain a checkpoint for the model.")
    checkpoint_dir: str = self.best_result.checkpoint.to_directory()
    checkpoint: str = os.path.join(checkpoint_dir, "model.safetensors")
    return safe_load_file(checkpoint)

get_best_optimizer

get_best_optimizer() -> dict[str, Any]

Get the best optimizer state from the results.

Returns:

Raises:

Source code in src/stimulus/learner/raytune_parser.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def get_best_optimizer(self) -> dict[str, Any]:
    """Get the best optimizer state from the results.

    Returns:
        Optimizer state dictionary.

    Raises:
        ValueError: If the checkpoint is missing.
    """
    if self.best_result.checkpoint is None:
        raise ValueError("Best result does not contain a checkpoint for the optimizer.")
    checkpoint_dir: str = self.best_result.checkpoint.to_directory()
    checkpoint: str = os.path.join(checkpoint_dir, "optimizer.pt")
    return torch.load(checkpoint)

save_best_config

save_best_config(output: str) -> None

Save the best config to a file.

Todo

maybe only save the relevant config values.

Parameters:

  • output (str) –

    File path to save the configuration.

Source code in src/stimulus/learner/raytune_parser.py
74
75
76
77
78
79
80
81
82
83
84
85
def save_best_config(self, output: str) -> None:
    """Save the best config to a file.

    TODO: maybe only save the relevant config values.

    Args:
        output: File path to save the configuration.
    """
    config: dict[str, Any] = self.get_best_config()
    config = self.fix_config_values(config)
    with open(output, "w") as f:
        yaml.safe_dump(config, f)

save_best_metrics_dataframe

save_best_metrics_dataframe(output: str) -> None

Save the dataframe with the metrics at each iteration of the best sample to a file.

Parameters:

  • output (str) –

    CSV file path to save the metrics.

Source code in src/stimulus/learner/raytune_parser.py
116
117
118
119
120
121
122
123
def save_best_metrics_dataframe(self, output: str) -> None:
    """Save the dataframe with the metrics at each iteration of the best sample to a file.

    Args:
        output: CSV file path to save the metrics.
    """
    metrics_df: pd.DataFrame = pd.DataFrame([self.best_result.metrics])
    metrics_df.to_csv(output, index=False)

save_best_model

save_best_model(output: str) -> None

Save the best model weights to a file.

This method retrieves the best model weights using the get_best_model helper which loads the model data from the checkpoint's directory, then re-saves it using safe_save_file.

Parameters:

  • output (str) –

    Path where the best model weights will be saved.

Source code in src/stimulus/learner/raytune_parser.py
140
141
142
143
144
145
146
147
148
149
150
151
def save_best_model(self, output: str) -> None:
    """Save the best model weights to a file.

    This method retrieves the best model weights using the get_best_model helper
    which loads the model data from the checkpoint's directory, then re-saves
    it using safe_save_file.

    Args:
        output: Path where the best model weights will be saved.
    """
    model: dict[str, torch.Tensor] = self.get_best_model()
    safe_save_file(model, output)

save_best_optimizer

save_best_optimizer(output: str) -> None

Save the best optimizer state to a file.

Parameters:

  • output (str) –

    Path where the best optimizer state will be saved.

Source code in src/stimulus/learner/raytune_parser.py
168
169
170
171
172
173
174
175
def save_best_optimizer(self, output: str) -> None:
    """Save the best optimizer state to a file.

    Args:
        output: Path where the best optimizer state will be saved.
    """
    optimizer_state: dict[str, Any] = self.get_best_optimizer()
    torch.save(optimizer_state, output)