codes.tune package

Contents

codes.tune package#

Submodules#

codes.tune.evaluate_study module#

codes.tune.evaluate_study.load_model_test_losses(model_path)#

Load the test losses from the model checkpoint.

Parameters:

model_path (str) – Path to the model checkpoint.

Returns:

Test losses.

Return type:

ndarray

codes.tune.evaluate_study.load_study_config(study_name)#

Load the YAML config used by the study (optuna_config.yaml).

Return type:

dict

codes.tune.evaluate_study.main()#

Main function to evaluate an Optuna study and its top models. Usually, viewing the study database with Optuna Dashboard is more informative.

Return type:

None

codes.tune.evaluate_study.moving_average(data, window_size)#

Compute the moving average of a 1D array.

Parameters:
  • data (ndarray) – 1D array to compute the moving average.

  • window_size (int) – Size of the window for the moving average.

Returns:

Moving average of the input data.

Return type:

ndarray

Raises:

ValueError – If the window size is not a positive integer.

codes.tune.evaluate_study.parse_arguments()#

Parse command-line arguments.

Return type:

Namespace

codes.tune.evaluate_study.plot_test_losses(test_losses, labels, study_name, window_size=5)#

Plot the test losses of the top models.

Parameters:
  • test_losses (list[ndarray]) – List of test losses.

  • labels (list[str]) – List of labels for the test losses.

  • study_name (str) – Name of the study.

  • window_size (int) – Size of the window for the moving average. Defaults to 5.

Return type:

None

codes.tune.evaluate_tuning module#

codes.tune.evaluate_tuning.compute_hypervolume_over_time(study, ref_slack=1.1, ignore_last_n=10)#

Compute the 2D hypervolume curve over time while excluding the last N trials from the Pareto calculation (by completion time). This prevents unreliable late-trial improvements (e.g., inference time) from appearing on the Pareto front.

Returns (hypervolumes, reference_point).

Return type:

tuple[list[float], ndarray | None]

codes.tune.evaluate_tuning.compute_pareto_plot_data(study, suffix, ignore_last_n=10, chosen_trial_number=None)#
Return type:

dict | None

codes.tune.evaluate_tuning.evaluate_tuning(study_prefix, top_n=10, storage_name=None, ignore_last_n=10, chosen_indices=None)#

For all surrogate studies named ‘<study_prefix>_<surrogate>’, plot the top_n test-loss trajectories.

Return type:

None

codes.tune.evaluate_tuning.format_surrogate_title(name)#
Return type:

str

codes.tune.evaluate_tuning.get_best_trials(study, top_n)#

Return trial numbers of best ‘top_n’ trials. Single-objective: lowest value. Multi-objective: Pareto front; if fewer than top_n, pad with closest-to-origin among the rest.

Return type:

list[int]

codes.tune.evaluate_tuning.hypervolume_2d(pareto_points, reference)#
Return type:

float

codes.tune.evaluate_tuning.load_loss_history(model_path)#

Load loss histories from a saved .pth. If the checkpoint contains pickled Optuna storage (which may try to connect to a remote DB during unpickle), bound the timeout and fall back to skipping.

Return type:

tuple[ndarray, ndarray, int]

codes.tune.evaluate_tuning.main()#
Return type:

None

codes.tune.evaluate_tuning.pareto_front(points)#
Return type:

ndarray

codes.tune.evaluate_tuning.pareto_front_mask(points)#
Return type:

ndarray

codes.tune.evaluate_tuning.parse_args()#
Return type:

Namespace

codes.tune.evaluate_tuning.plot_losses(loss_histories, epochs, labels, title='Losses', save=False, out_dir=None, mode='')#

Plot multiple loss trajectories on a log scale.

Return type:

None

codes.tune.evaluate_tuning.save_individual_pareto_plot(data, out_dir)#
Return type:

None

codes.tune.evaluate_tuning.save_pareto_front_grid(datasets, out_dir)#
Return type:

None

codes.tune.evaluate_tuning.save_relative_hv_combined(datasets, out_dir)#
Return type:

None

codes.tune.evaluate_tuning.save_relative_hv_grid(datasets, out_dir)#
Return type:

None

codes.tune.evaluate_tuning.summarize_pareto_tradeoff(data)#
Return type:

None

codes.tune.optuna_fcts module#

class codes.tune.optuna_fcts.MaxValidTrialsCallback(target)#

Bases: object

codes.tune.optuna_fcts.build_fine_optuna_params(model_config)#
Return type:

dict

codes.tune.optuna_fcts.create_objective(config, study_name, device_queue)#

Create the objective function for Optuna.

Parameters:
  • config (dict) – Configuration dictionary.

  • study_name (str) – Name of the study.

  • device_queue (Queue) – Queue of available devices.

Returns:

Objective function for Optuna.

Return type:

callable

codes.tune.optuna_fcts.endpoint_timing_protocol(config=None)#

Version the endpoint-workload study objective and its fixed settings.

Return type:

dict

codes.tune.optuna_fcts.ensure_study_timing(study, config=None)#

Freeze objective timing and checkpoint pruning across study resumes.

Return type:

None

codes.tune.optuna_fcts.load_yaml_config(config_path)#

Load a YAML configuration file.

Parameters:

config_path (str) – Path to the YAML configuration file.

Returns:

Configuration dictionary.

Return type:

dict

codes.tune.optuna_fcts.make_optuna_params(trial, optuna_params)#
Return type:

dict

codes.tune.optuna_fcts.maybe_set_runtime_threshold(study, warmup_target, include_pruned=False)#
Return type:

None

codes.tune.optuna_fcts.training_run(trial, device, slot_id, config, study_name)#

Run the training for a single Optuna trial and return the loss. In multi-objective mode, also returns endpoint-workload geomean latency.

Parameters:
  • trial (Trial) – Optuna trial object.

  • device (str) – Device to run the training on.

  • slot_id (int) – Slot ID for the position of the progress bar.

  • config (dict) – Configuration dictionary.

  • study_name (str) – Name of the study.

Returns:

Loss value in single objective mode. tuple[float, float]: (loss, endpoint workload geomean latency) in multi-objective mode.

Return type:

float | tuple[float, float]

codes.tune.pareto_pruning module#

Checkpoint-level shadow or active pruning for multi-objective Optuna studies.

codes.tune.pareto_pruning.assess_checkpoint(trial, epoch, total_epochs, error, latency, settings)#

Publish checkpoint metrics and return whether an active prune is due.

Return type:

bool

codes.tune.pareto_pruning.checkpoint_epochs(total_epochs, settings)#

Return distinct one-based rung epochs, excluding the final epoch.

Return type:

tuple[int, ...]

codes.tune.pareto_pruning.parse_pareto_pruning(config)#

Normalize multi_objective_pruning; shadow/prune require MO studies.

Return type:

dict | None

codes.tune.postgres_fcts module#

codes.tune.postgres_fcts.initialize_optuna_database(config, study_folder_name)#
Return type:

str

codes.tune.slurm module#

Slurm workers for one persistent Optuna study.

The manifest is prepared once and read by independent array tasks. Connection credentials stay in CODES_OPTUNA_STORAGE_URL and are never written to the manifest.

codes.tune.slurm.prepare(config, path, surrogate, max_attempts)#
Return type:

None

codes.tune.slurm.status(path)#
Return type:

dict[str, int]

codes.tune.slurm.worker(path, device, cpu_threads)#
Return type:

None

codes.tune.storage module#

Optuna storage selection shared by tuning and evaluation commands.

codes.tune.storage.create_optuna_storage(config, tuning_id, *, create=True, postgres_database=None)#

Construct configured Optuna storage and return it with its backend name.

Journal uses Optuna’s JournalFileBackend. Its default JournalFileSymlinkLock is the supported lock implementation for multiple processes on shared filesystems.

Return type:

tuple[optuna.storages.BaseStorage | str, str]

codes.tune.storage.postgres_environment_url()#

Read the PostgreSQL URL without including its contents in validation errors.

Return type:

str

codes.tune.storage.storage_metadata(config, tuning_id)#

Return safe, credential-free storage metadata for manifests/config copies.

Return type:

dict[str, str]

codes.tune.tune_utils module#

codes.tune.tune_utils.apply_tuning_defaults(config)#
Return type:

dict

codes.tune.tune_utils.build_study_names(config, main_study_name)#
Return type:

list[str]

codes.tune.tune_utils.copy_config(src, dst_folder)#
Return type:

None

codes.tune.tune_utils.count_tuning_trials(study)#
Return type:

int

codes.tune.tune_utils.create_sampler(config, seed)#

Build the selected sampler for either objective type.

Return type:

BaseSampler

codes.tune.tune_utils.delete_studies_if_requested(config, main_study_name, db_url)#

Delete studies if user overwrote run folder.

Return type:

None

codes.tune.tune_utils.is_pareto_pruned(trial)#
Return type:

bool

codes.tune.tune_utils.is_valid_complete(trial)#
Return type:

bool

codes.tune.tune_utils.prepare_workspace(master_cfg_path, config)#
Return type:

dict

codes.tune.tune_utils.sampler_name(config)#

Resolve configured sampler, retaining the historical objective-based default.

Return type:

str

codes.tune.tune_utils.yes_no(prompt, default=False)#

Simple Y/N prompt. default=False -> [y/N], True -> [Y/n].

Return type:

bool

Module contents#

class codes.tune.MaxValidTrialsCallback(target)#

Bases: object

codes.tune.apply_tuning_defaults(config)#
Return type:

dict

codes.tune.build_fine_optuna_params(model_config)#
Return type:

dict

codes.tune.build_study_names(config, main_study_name)#
Return type:

list[str]

codes.tune.copy_config(src, dst_folder)#
Return type:

None

codes.tune.create_objective(config, study_name, device_queue)#

Create the objective function for Optuna.

Parameters:
  • config (dict) – Configuration dictionary.

  • study_name (str) – Name of the study.

  • device_queue (Queue) – Queue of available devices.

Returns:

Objective function for Optuna.

Return type:

callable

codes.tune.delete_studies_if_requested(config, main_study_name, db_url)#

Delete studies if user overwrote run folder.

Return type:

None

codes.tune.endpoint_timing_protocol(config=None)#

Version the endpoint-workload study objective and its fixed settings.

Return type:

dict

codes.tune.ensure_study_timing(study, config=None)#

Freeze objective timing and checkpoint pruning across study resumes.

Return type:

None

codes.tune.initialize_optuna_database(config, study_folder_name)#
Return type:

str

codes.tune.load_model_test_losses(model_path)#

Load the test losses from the model checkpoint.

Parameters:

model_path (str) – Path to the model checkpoint.

Returns:

Test losses.

Return type:

ndarray

codes.tune.load_study_config(study_name)#

Load the YAML config used by the study (optuna_config.yaml).

Return type:

dict

codes.tune.load_yaml_config(config_path)#

Load a YAML configuration file.

Parameters:

config_path (str) – Path to the YAML configuration file.

Returns:

Configuration dictionary.

Return type:

dict

codes.tune.make_optuna_params(trial, optuna_params)#
Return type:

dict

codes.tune.maybe_set_runtime_threshold(study, warmup_target, include_pruned=False)#
Return type:

None

codes.tune.moving_average(data, window_size)#

Compute the moving average of a 1D array.

Parameters:
  • data (ndarray) – 1D array to compute the moving average.

  • window_size (int) – Size of the window for the moving average.

Returns:

Moving average of the input data.

Return type:

ndarray

Raises:

ValueError – If the window size is not a positive integer.

codes.tune.plot_test_losses(test_losses, labels, study_name, window_size=5)#

Plot the test losses of the top models.

Parameters:
  • test_losses (list[ndarray]) – List of test losses.

  • labels (list[str]) – List of labels for the test losses.

  • study_name (str) – Name of the study.

  • window_size (int) – Size of the window for the moving average. Defaults to 5.

Return type:

None

codes.tune.prepare_workspace(master_cfg_path, config)#
Return type:

dict

codes.tune.training_run(trial, device, slot_id, config, study_name)#

Run the training for a single Optuna trial and return the loss. In multi-objective mode, also returns endpoint-workload geomean latency.

Parameters:
  • trial (Trial) – Optuna trial object.

  • device (str) – Device to run the training on.

  • slot_id (int) – Slot ID for the position of the progress bar.

  • config (dict) – Configuration dictionary.

  • study_name (str) – Name of the study.

Returns:

Loss value in single objective mode. tuple[float, float]: (loss, endpoint workload geomean latency) in multi-objective mode.

Return type:

float | tuple[float, float]

codes.tune.yes_no(prompt, default=False)#

Simple Y/N prompt. default=False -> [y/N], True -> [Y/n].

Return type:

bool