From bebe35c88429e86366005eac06adbf2a6c8d4dea Mon Sep 17 00:00:00 2001 From: aramoto99 Date: Fri, 27 Dec 2024 16:53:29 +0900 Subject: [PATCH] fix examples config --- aiaccel/apps/optimize.py | 137 ------------------------ aiaccel/hpo/apps/optimize.py | 4 +- examples/job/optuna/CmaEs/config.yaml | 6 +- examples/job/optuna/GPS/config.yaml | 4 +- examples/job/optuna/Grid/config.yaml | 4 +- examples/job/optuna/NSGAII/config.yaml | 6 +- examples/job/optuna/NSGAIII/config.yaml | 8 +- examples/job/optuna/QMC/config.yaml | 6 +- examples/job/optuna/Random/config.yaml | 31 ++---- examples/job/optuna/TPE/config.yaml | 4 +- 10 files changed, 31 insertions(+), 179 deletions(-) delete mode 100644 aiaccel/apps/optimize.py diff --git a/aiaccel/apps/optimize.py b/aiaccel/apps/optimize.py deleted file mode 100644 index 581565bd..00000000 --- a/aiaccel/apps/optimize.py +++ /dev/null @@ -1,137 +0,0 @@ -from typing import Any - -import argparse -from collections.abc import Callable -from pathlib import Path -import pickle as pkl - -from hydra.utils import instantiate -from omegaconf import OmegaConf as oc # noqa: N813 - -from optuna.trial import Trial - -from aiaccel.hpo.optuna.suggest_wrapper import Const, Suggest, SuggestFloat, T -from aiaccel.job import AbciJobExecutor, BaseJobExecutor, LocalJobExecutor - -""" -Usage (if parameters are not defined in a config file): - python -m aiaccel.apps.optimize objective.sh params.x1="[0, 10]" params.x2="[0, 10]" --config config.yaml - -Usage (if parameters are defined in a config file): - python -m aiaccel.apps.optimize objective.sh --config config.yaml -""" - -""" -config file (yaml) example: - -study: - _target_: optuna.create_study - direction: minimize - - sampler: - _target_: optuna.samplers.TPESampler - seed: 0 - -params: - _convert_: partial - _target_: aiaccel.apps.optimize.HparamsManager - x1: [0, 1] - x2: - _target_: aiaccel.apps.optimize.SuggestFloat - name: x2 - low: 0.0 - high: 1.0 - log: false - -n_trials: 30 -n_max_jobs: 4 - -group: gaa50000 - -""" - - -class HparamsManager: - def __init__(self, **params_def: dict[str, int | float | str | list[int | float] | Suggest[T]]) -> None: - self.params: dict[str, Callable[[Trial], Any]] = {} - for name, param in params_def.items(): - if callable(param): - self.params[name] = param - else: - if isinstance(param, list): - low, high = param - self.params[name] = SuggestFloat(name=name, low=low, high=high) - else: - self.params[name] = Const(name=name, value=param) - - def suggest_hparams(self, trial: Trial) -> dict[str, float | int | str | list[float | int | str]]: - return {name: param_fn(trial) for name, param_fn in self.params.items()} - - -def main() -> None: - parser = argparse.ArgumentParser() - parser.add_argument("job_filename", type=Path, help="The shell script to execute.") - parser.add_argument("--config", nargs="?", default=None) - parser.add_argument("--executor", nargs="?", default="local") - parser.add_argument("--resume", action="store_true", default=False) - - args, unk_args = parser.parse_known_args() - config = oc.merge(oc.load(args.config), oc.from_cli(unk_args)) - - if "storage" not in config.study: - config.study.storage = { - "_target_": "optuna.storages.RDBStorage", - "url": "sqlite:///optuna.db", - "engine_kwargs": {"connect_args": {"timeout": 30}}, - } - - if "study_name" not in config.study: - config.study.study_name = "aiaccel_study" - - if args.resume: - config.study.load_if_exists = True - - jobs: BaseJobExecutor - - if args.executor.lower() == "local": - jobs = LocalJobExecutor(args.job_filename, n_max_jobs=config.n_max_jobs) - elif args.executor.lower() == "abci": - jobs = AbciJobExecutor(args.job_filename, config.group, n_max_jobs=config.n_max_jobs) - else: - raise ValueError(f"Unknown executor: {args.executor}") - - study = instantiate(config.study) - params = instantiate(config.params) - - result_filename_template = "{job.cwd}/{job.job_name}_result.pkl" - - finished_job_count = 0 - - while finished_job_count < config.n_trials: - n_running_jobs = len(jobs.get_running_jobs()) - n_max_jobs = min(jobs.available_slots(), config.n_trials - finished_job_count - n_running_jobs) - for _ in range(n_max_jobs): - trial = study.ask() - - hparams = params.suggest_hparams(trial) - - jobs.job_name = str(jobs.job_filename) + f"_{trial.number}" - - job = jobs.submit( - args=[result_filename_template] + sum([[f"--{k}", f"{v:.5f}"] for k, v in hparams.items()], []), - tag=trial, - ) - - for job in jobs.collect_finished(): - trial = job.tag - - with open(result_filename_template.format(job=job), "rb") as f: - y = pkl.load(f) - - study.tell(trial, y) - - finished_job_count += 1 - - -if __name__ == "__main__": - main() diff --git a/aiaccel/hpo/apps/optimize.py b/aiaccel/hpo/apps/optimize.py index 91360400..13c7565b 100644 --- a/aiaccel/hpo/apps/optimize.py +++ b/aiaccel/hpo/apps/optimize.py @@ -96,10 +96,10 @@ def main() -> None: params: _convert_: partial - _target_: aiaccel.apps.optimize.HparamsManager + _target_: aiaccel.hpo.apps.optimize.HparamsManager x1: [0, 1] x2: - _target_: aiaccel.apps.optimize.SuggestFloat + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat name: x2 low: 0.0 high: 1.0 diff --git a/examples/job/optuna/CmaEs/config.yaml b/examples/job/optuna/CmaEs/config.yaml index 4a3825e3..584fea7e 100644 --- a/examples/job/optuna/CmaEs/config.yaml +++ b/examples/job/optuna/CmaEs/config.yaml @@ -26,15 +26,15 @@ study: params: _convert_: partial - _target_: aiaccel.apps.optimize.HparamsManager + _target_: aiaccel.hpo.apps.optimize.HparamsManager x1: - _target_: aiaccel.apps.optimize.SuggestFloat + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat name: x1 low: -1 high: 1 log: false x2: - _target_: aiaccel.apps.optimize.SuggestInt + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestInt name: x2 low: -1 high: 1 diff --git a/examples/job/optuna/GPS/config.yaml b/examples/job/optuna/GPS/config.yaml index f3babd51..7358a265 100644 --- a/examples/job/optuna/GPS/config.yaml +++ b/examples/job/optuna/GPS/config.yaml @@ -15,9 +15,9 @@ study: params: _convert_: partial - _target_: aiaccel.apps.optimize.HparamsManager + _target_: aiaccel.hpo.apps.optimize.HparamsManager x: - _target_: aiaccel.apps.optimize.SuggestFloat + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat name: x low: -10.0 high: 10.0 diff --git a/examples/job/optuna/Grid/config.yaml b/examples/job/optuna/Grid/config.yaml index fdfed352..f89e140e 100644 --- a/examples/job/optuna/Grid/config.yaml +++ b/examples/job/optuna/Grid/config.yaml @@ -10,9 +10,9 @@ study: params: _convert_: partial - _target_: aiaccel.apps.optimize.HparamsManager + _target_: aiaccel.hpo.apps.optimize.HparamsManager x: - _target_: aiaccel.apps.optimize.SuggestFloat + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat name: x low: -5 high: 5 diff --git a/examples/job/optuna/NSGAII/config.yaml b/examples/job/optuna/NSGAII/config.yaml index c9078d10..eabbbedd 100644 --- a/examples/job/optuna/NSGAII/config.yaml +++ b/examples/job/optuna/NSGAII/config.yaml @@ -17,15 +17,15 @@ study: params: _convert_: partial - _target_: aiaccel.apps.optimize.HparamsManager + _target_: aiaccel.hpo.apps.optimize.HparamsManager x1: - _target_: aiaccel.apps.optimize.SuggestFloat + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat name: x1 low: -100.0 high: 100.0 log: false x2: - _target_: aiaccel.apps.optimize.SuggestCategorical + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestCategorical name: x2 choices: [-1, 0, 1] diff --git a/examples/job/optuna/NSGAIII/config.yaml b/examples/job/optuna/NSGAIII/config.yaml index eaf9d911..989a3a12 100644 --- a/examples/job/optuna/NSGAIII/config.yaml +++ b/examples/job/optuna/NSGAIII/config.yaml @@ -19,21 +19,21 @@ study: params: _convert_: partial - _target_: aiaccel.apps.optimize.HparamsManager + _target_: aiaccel.hpo.apps.optimize.HparamsManager x1: - _target_: aiaccel.apps.optimize.SuggestFloat + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat name: x1 low: 0 high: 5 log: false x2: - _target_: aiaccel.apps.optimize.SuggestFloat + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat name: x2 low: 0 high: 5 log: false x3: - _target_: aiaccel.apps.optimize.SuggestFloat + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat name: x3 low: 0 high: 5 diff --git a/examples/job/optuna/QMC/config.yaml b/examples/job/optuna/QMC/config.yaml index 1a2755e8..ec41ff5b 100644 --- a/examples/job/optuna/QMC/config.yaml +++ b/examples/job/optuna/QMC/config.yaml @@ -15,15 +15,15 @@ study: params: _convert_: partial - _target_: aiaccel.apps.optimize.HparamsManager + _target_: aiaccel.hpo.apps.optimize.HparamsManager x1: - _target_: aiaccel.apps.optimize.SuggestFloat + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat name: x1 low: -1.0 high: 1.0 log: false x2: - _target_: aiaccel.apps.optimize.SuggestInt + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestInt name: x2 low: -1.0 high: 1.0 diff --git a/examples/job/optuna/Random/config.yaml b/examples/job/optuna/Random/config.yaml index 0b260a12..052695f9 100644 --- a/examples/job/optuna/Random/config.yaml +++ b/examples/job/optuna/Random/config.yaml @@ -1,32 +1,21 @@ +study: + _target_: optuna.create_study + direction: minimize + sampler: + _target_: optuna.samplers.RandomSampler + seed: null + params: _convert_: partial -<<<<<<< HEAD _target_: aiaccel.hpo.apps.optimize.HparamsManager - x1: [0, 1] - x2: - _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat - name: x2 - low: 0.0 - high: 1.0 -======= - _target_: aiaccel.apps.optimize.HparamsManager x: - _target_: aiaccel.apps.optimize.SuggestFloat -<<<<<<< HEAD - name: x2 - low: 0.0 - high: 1.0 -<<<<<<< HEAD ->>>>>>> 13d33e0 (add load_if_exists (#395)):examples/job/optuna/config.yaml ->>>>>>> 9938cf1 (add load_if_exists (#395)) -======= ->>>>>>> a026391 (fix conflict) -======= + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat name: x low: -10.0 high: 10.0 ->>>>>>> c133763 (fix config) log: false n_trials: 30 n_max_jobs: 4 + +group: gaa50000 diff --git a/examples/job/optuna/TPE/config.yaml b/examples/job/optuna/TPE/config.yaml index 2cfab535..f8313aea 100644 --- a/examples/job/optuna/TPE/config.yaml +++ b/examples/job/optuna/TPE/config.yaml @@ -22,9 +22,9 @@ study: params: _convert_: partial - _target_: aiaccel.apps.optimize.HparamsManager + _target_: aiaccel.hpo.apps.optimize.HparamsManager x: - _target_: aiaccel.apps.optimize.SuggestFloat + _target_: aiaccel.hpo.optuna.suggest_wrapper.SuggestFloat name: x low: -10.0 high: 10.0