Skip to content

Commit 37e7809

Browse files
bagxiScitator
andauthored
feat: config API v22.XX first draft (#1406)
* feat: config API v22.XX first draft * test: test_classification for config api added * test: pipeline tests for config api added * docs: CHANGELOG updated * test: cmd updated * test: link to catalyst lib for scripts added * test: debug tests for config api * test: `CONFIGS_REQUIRED` flag added * Update dl_cpu.yml * Update colab_ci_cd.ipynb * Update colab_ci_cd.ipynb * Update colab_ci_cd.ipynb * Update dl_cpu_minimal.yml * Update requirements.txt * Update requirements.txt * Update requirements.txt * Update requirements.txt * test: `TensorDataset` fix for minimal tests added * Update requirements.txt Co-authored-by: Sergey Kolesnikov <scitator@gmail.com>
1 parent 8d07ffd commit 37e7809

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2217
-19
lines changed

.github/workflows/dl_cpu.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,5 @@ jobs:
123123
OMP_NUM_THREADS: "1"
124124
MKL_NUM_THREADS: "1"
125125
run: |
126-
CPU_REQUIRED=1 CATALYST_COMPUTE_PER_CLASS_METRICS="1" OMP_NUM_THREADS="1" MKL_NUM_THREADS="1" pytest .
126+
pip install -e . --no-deps
127+
PYTHONPATH="${PYTHONPATH}:." CPU_REQUIRED="1" CONFIGS_REQUIRED="1" CATALYST_COMPUTE_PER_CLASS_METRICS="1" OMP_NUM_THREADS="1" MKL_NUM_THREADS="1" pytest .

.github/workflows/dl_cpu_minimal.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,5 @@ jobs:
125125
OMP_NUM_THREADS: "1"
126126
MKL_NUM_THREADS: "1"
127127
run: |
128-
CPU_REQUIRED=1 CATALYST_COMPUTE_PER_CLASS_METRICS="1" OMP_NUM_THREADS="1" MKL_NUM_THREADS="1" pytest .
128+
pip install -e . --no-deps
129+
PYTHONPATH="${PYTHONPATH}:." CPU_REQUIRED="1" CONFIGS_REQUIRED="1" CATALYST_COMPUTE_PER_CLASS_METRICS="1" OMP_NUM_THREADS="1" MKL_NUM_THREADS="1" pytest .

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ ENV/
112112
*.csv
113113
*.tsv
114114
*.ipynb
115+
*.pt
116+
*.pth
115117

116118
tmp/
117119
logs/

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
88

99
### Added
1010

11-
-
11+
- `catalyst-run` for Config API support added [#1406](https://github.com/catalyst-team/catalyst/pull/1406)
1212

1313
### Changed
1414

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,88 @@ print(study.best_value, study.best_params)
11621162
</p>
11631163
</details>
11641164

1165+
<details>
1166+
<summary>Config API - minimal example</summary>
1167+
<p>
1168+
1169+
```yaml title="example.yaml"
1170+
runner:
1171+
_target_: catalyst.runners.SupervisedRunner
1172+
model:
1173+
_var_: model
1174+
_target_: torch.nn.Sequential
1175+
args:
1176+
- _target_: torch.nn.Flatten
1177+
- _target_: torch.nn.Linear
1178+
in_features: 784 # 28 * 28
1179+
out_features: 10
1180+
input_key: features
1181+
output_key: &output_key logits
1182+
target_key: &target_key targets
1183+
loss_key: &loss_key loss
1184+
1185+
run:
1186+
# ≈ stage 1
1187+
- _call_: train # runner.train(...)
1188+
1189+
criterion:
1190+
_target_: torch.nn.CrossEntropyLoss
1191+
1192+
optimizer:
1193+
_target_: torch.optim.Adam
1194+
params: # model.parameters()
1195+
_var_: model.parameters
1196+
lr: 0.02
1197+
1198+
loaders:
1199+
train:
1200+
_target_: torch.utils.data.DataLoader
1201+
dataset:
1202+
_target_: catalyst.contrib.datasets.MNIST
1203+
root: data
1204+
train: y
1205+
batch_size: 32
1206+
1207+
&valid_loader_key valid:
1208+
&valid_loader
1209+
_target_: torch.utils.data.DataLoader
1210+
dataset:
1211+
_target_: catalyst.contrib.datasets.MNIST
1212+
root: data
1213+
train: n
1214+
batch_size: 32
1215+
1216+
callbacks:
1217+
- &accuracy_metric
1218+
_target_: catalyst.callbacks.AccuracyCallback
1219+
input_key: *output_key
1220+
target_key: *target_key
1221+
topk: [1,3,5]
1222+
- _target_: catalyst.callbacks.PrecisionRecallF1SupportCallback
1223+
input_key: *output_key
1224+
target_key: *target_key
1225+
1226+
num_epochs: 1
1227+
logdir: logs
1228+
valid_loader: *valid_loader_key
1229+
valid_metric: *loss_key
1230+
minimize_valid_metric: y
1231+
verbose: y
1232+
1233+
# ≈ stage 2
1234+
- _call_: evaluate_loader # runner.evaluate_loader(...)
1235+
loader: *valid_loader
1236+
callbacks:
1237+
- *accuracy_metric
1238+
1239+
```
1240+
1241+
```sh
1242+
catalyst-run --config example.yaml
1243+
```
1244+
</p>
1245+
</details>
1246+
11651247
### Tests
11661248
All Catalyst code, features, and pipelines [are fully tested](./tests).
11671249
We also have our own [catalyst-codestyle](https://github.com/catalyst-team/codestyle) and a corresponding pre-commit hook.

catalyst/contrib/scripts/run.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from typing import Iterable
2+
import argparse
3+
import logging
4+
5+
from catalyst import utils
6+
from catalyst.registry import REGISTRY
7+
8+
9+
def parse_args():
10+
"""Parses the command line arguments and returns arguments and config."""
11+
parser = argparse.ArgumentParser()
12+
parser.add_argument(
13+
"--config",
14+
"--configs",
15+
"-C",
16+
nargs="+",
17+
default=("config.yml",),
18+
type=str,
19+
help="path to config/configs",
20+
metavar="CONFIG_PATH",
21+
dest="configs",
22+
)
23+
24+
utils.boolean_flag(
25+
parser,
26+
"deterministic",
27+
default=None,
28+
help="Deterministic mode if running in CuDNN backend",
29+
)
30+
utils.boolean_flag(parser, "benchmark", default=None, help="Use CuDNN benchmark")
31+
32+
args, unknown_args = parser.parse_known_args()
33+
return vars(args), unknown_args
34+
35+
36+
def run_from_config(
37+
configs: Iterable[str],
38+
deterministic: bool = None,
39+
benchmark: bool = None,
40+
) -> None:
41+
"""Creates Runner from YAML configs and runs experiment."""
42+
logger = logging.getLogger(__name__)
43+
44+
# there is no way to set deterministic/benchmark flags with a runner,
45+
# so do it manually
46+
utils.prepare_cudnn(deterministic, benchmark)
47+
48+
config = {}
49+
for config_path in configs:
50+
config_part = utils.load_config(config_path, ordered=True)
51+
config = utils.merge_dicts(config, config_part)
52+
# config_copy = copy.deepcopy(config)
53+
54+
experiment_params = REGISTRY.get_from_params(**config)
55+
56+
runner = experiment_params["runner"]
57+
for stage_params in experiment_params["run"]:
58+
name = stage_params.pop("_call_")
59+
func = getattr(runner, name)
60+
61+
result = func(**stage_params)
62+
if result is not None:
63+
logger.info(f"{name}:\n{result}")
64+
65+
# TODO: check if needed
66+
# logdir = getattr(runner, "logdir", getattr(runner, "_logdir"), None)
67+
# if logdir and utils.get_rank() <= 0:
68+
# utils.dump_environment(logdir=logdir, config=config_copy, configs_path=configs)
69+
70+
71+
def main():
72+
"""Runs the ``catalyst-run`` script."""
73+
kwargs, unknown_args = parse_args()
74+
run_from_config(**kwargs)
75+
76+
77+
if __name__ == "__main__":
78+
main()

catalyst/runners/runner.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def train(
262262
# the data
263263
loaders: "OrderedDict[str, DataLoader]",
264264
# the core
265-
model: TorchModel,
265+
model: TorchModel = None,
266266
engine: Union["Engine", str] = None,
267267
# the components
268268
criterion: TorchCriterion = None,
@@ -344,13 +344,15 @@ def train(
344344
345345
"""
346346
# experiment setup
347-
self._engine = engine or get_available_engine(cpu=cpu, fp16=fp16, ddp=ddp)
347+
self._engine = (
348+
engine or self.engine or get_available_engine(cpu=cpu, fp16=fp16, ddp=ddp)
349+
)
348350
# self._trial = trial
349351
self._loggers = loggers
350352
# the data
351353
self._loaders = loaders
352354
# the components
353-
self._model = model
355+
self._model = model or self.model
354356
self._criterion = criterion
355357
self._optimizer = optimizer
356358
self._scheduler = scheduler

examples/notebooks/colab_ci_cd.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@
196196
},
197197
"outputs": [],
198198
"source": [
199-
"! cd catalyst && CATALYST_COMPUTE_PER_CLASS_METRICS=\"1\" OMP_NUM_THREADS=\"1\" MKL_NUM_THREADS=\"1\" pytest ."
199+
"! pip install -e ./catalyst --no-deps\n",
200+
"! cd catalyst && PYTHONPATH='${PYTHONPATH}:.' CPU_REQUIRED='1' GPU_REQUIRED='1' CONFIGS_REQUIRED='1' CATALYST_COMPUTE_PER_CLASS_METRICS='1' OMP_NUM_THREADS='1' MKL_NUM_THREADS='1' pytest ."
200201
]
201202
},
202203
{

requirements/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ torch>=1.4.0
66
accelerate
77

88
# registry
9-
hydra-slayer>=0.1.1
9+
hydra-slayer>=0.4.0
1010

1111
# progress bar
1212
tqdm>=4.33.0

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def load_version():
9999
entry_points={
100100
"console_scripts": [
101101
"catalyst-contrib=catalyst.contrib.__main__:main",
102+
"catalyst-run=catalyst.contrib.scripts.run:main",
102103
],
103104
},
104105
scripts=[

0 commit comments

Comments
 (0)