-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optuna tune script PoC (#1411)
* feat: optuna tune script PoC * fix: workaround for latest version of Jinja2 * fix: workaround for latest version of click * fix: workaround for minimal tests added * fix: workaround for new version of "accelerate" added * tests: test_tune minimal updated * docs: changelog updated * tests: "check_config_api.sh" workflow added
- Loading branch information
Showing
15 changed files
with
375 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Cause the script to exit if a single command fails | ||
set -eo pipefail -v | ||
|
||
DEVICE=${DEVICE:="cpu"} | ||
|
||
pip install -e . --quiet --no-deps --upgrade-strategy only-if-needed | ||
|
||
################################ pipeline 00 ################################ | ||
# checking `catalyst-run` console script entry point | ||
|
||
PYTHONPATH="${PYTHONPATH}:." catalyst-run \ | ||
-C "tests/pipelines/configs/test_mnist.yml" \ | ||
"tests/pipelines/configs/engine_${DEVICE}.yml" | ||
|
||
rm -rf tests/logs | ||
|
||
################################ pipeline 01 ################################ | ||
# checking `catalyst-tune` console script entry point | ||
|
||
pip install -r requirements/requirements-optuna.txt --quiet \ | ||
--find-links https://download.pytorch.org/whl/cpu/torch_stable.html \ | ||
--upgrade-strategy only-if-needed | ||
|
||
PYTHONPATH="${PYTHONPATH}:." catalyst-tune \ | ||
-C "tests/contrib/scripts/test_tune.yml" \ | ||
"tests/pipelines/configs/engine_${DEVICE}.yml" \ | ||
--n-trials 2 | ||
|
||
rm -rf tests/logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python | ||
"""Config API and Optuna integration for AutoML hyperparameters tuning.""" | ||
from typing import Iterable | ||
import argparse | ||
import copy | ||
|
||
import optuna | ||
|
||
from catalyst import utils | ||
from catalyst.contrib.scripts import run | ||
from catalyst.registry import REGISTRY | ||
from hydra_slayer import functional as F | ||
|
||
|
||
def parse_args(args: Iterable = None, namespace: argparse.Namespace = None): | ||
"""Parses the command line arguments and returns arguments and config.""" | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--n-trials", type=int, default=None) | ||
parser.add_argument("--timeout", type=int, default=None) | ||
parser.add_argument("--n-jobs", type=int, default=1) | ||
utils.boolean_flag(parser, "gc-after-trial", default=False) | ||
utils.boolean_flag(parser, "show-progress-bar", default=False) | ||
|
||
args, unknown_args = parser.parse_known_args(args=args, namespace=namespace) | ||
return vars(args), unknown_args | ||
|
||
|
||
def main(): | ||
"""Runs the ``catalyst-tune`` script.""" | ||
kwargs_run, unknown_args = run.parse_args() | ||
kwargs_tune, _ = parse_args(args=unknown_args) | ||
|
||
config_full = run.process_configs(**kwargs_run) | ||
config = copy.copy(config_full) | ||
config_study = config.pop("study") | ||
|
||
# optuna objective | ||
def objective(trial: optuna.trial): | ||
# workaround for `REGISTRY.get_from_params` - redefine `trial` var | ||
experiment_params, _ = F._recursive_get_from_params( | ||
factory_key=REGISTRY.name_key, | ||
get_factory_func=REGISTRY.get, | ||
params=config, | ||
shared_params={}, | ||
var_key=REGISTRY.var_key, | ||
attrs_delimiter=REGISTRY.attrs_delimiter, | ||
vars_dict={**REGISTRY._vars_dict, "trial": trial}, | ||
) | ||
runner = experiment_params["runner"] | ||
runner._trial = trial | ||
|
||
run.run_from_params(experiment_params) | ||
score = runner.epoch_metrics[runner._valid_loader][runner._valid_metric] | ||
|
||
return score | ||
|
||
study = REGISTRY.get_from_params(**config_study) | ||
study.optimize(objective, **kwargs_tune) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
pytest==5.3.1 | ||
sphinx==2.2.1 | ||
Jinja2<=3.0.3 | ||
docutils==0.17.1 | ||
# sphinx==4.2.0 | ||
# git+https://github.com/bitprophet/releases/#egg=releases | ||
# git+https://github.com/readthedocs/sphinx_rtd_theme | ||
mock==3.0.5 | ||
catalyst-codestyle==21.09.2 | ||
black==21.8b0 | ||
click<=8.0.4 | ||
catalyst-sphinx-theme==1.2.0 | ||
tomlkit==0.7.2 | ||
pre-commit==2.13.0 | ||
path | ||
path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.