Skip to content

Commit c06a3ee

Browse files
kurmanfacebook-github-bot
authored andcommitted
Generic config module (pytorch#508)
Summary: Pull Request resolved: pytorch#508 Extract generic config module and decouple storage concerns from configuration data. - This should allow adding new configuration options besides INI - All the client code should work before, but we are moving away from runner specific logic. Github issue (3rd task) pytorch#368 Differential Revision: D36855026 LaMa Project: L1123150 fbshipit-source-id: 3562bf258270c661354ea58264cb099037b1d984
1 parent 44f0633 commit c06a3ee

File tree

8 files changed

+832
-997
lines changed

8 files changed

+832
-997
lines changed

torchx/cli/argparse_util.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
from pathlib import Path
99
from typing import Any, Dict, Optional, Sequence, Text
1010

11-
from torchx.runner import config
12-
11+
from torchx.config.config import get_config_store
1312

1413
CONFIG_DIRS = [str(Path.home()), str(Path.cwd())]
1514

@@ -35,13 +34,11 @@ def __init__(
3534
default: Any = None,
3635
**kwargs: Any,
3736
) -> None:
37+
conf_data = get_config_store(CONFIG_DIRS).get(collection=subcmd, label="cli")
38+
conf_data_dict = dict(conf_data.items())
3839
cfg = self._subcmd_configs.setdefault(
3940
subcmd,
40-
config.get_configs(
41-
prefix="cli",
42-
name=subcmd,
43-
dirs=CONFIG_DIRS,
44-
),
41+
conf_data_dict,
4542
)
4643

4744
# if found in .torchxconfig make it the default for this argument

torchx/cli/cmd_configure.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111

1212
from torchx.cli.cmd_base import SubCommand
13-
from torchx.runner.config import dump
13+
from torchx.config import seed_config_store_with_schedulers
1414
from torchx.schedulers import get_schedulers
1515

1616

@@ -47,7 +47,11 @@ def run(self, args: argparse.Namespace) -> None:
4747
required_only = not args.all
4848

4949
if args.print:
50-
dump(f=sys.stdout, schedulers=schedulers, required_only=required_only)
50+
seed_config_store_with_schedulers(
51+
f=sys.stdout, schedulers=schedulers, required_only=required_only
52+
)
5153
else:
5254
with open(".torchxconfig", "w") as f:
53-
dump(f=f, schedulers=schedulers, required_only=required_only)
55+
seed_config_store_with_schedulers(
56+
f=f, schedulers=schedulers, required_only=required_only
57+
)

torchx/cli/cmd_run.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from torchx.cli.argparse_util import CONFIG_DIRS, torchxconfig_run
2020
from torchx.cli.cmd_base import SubCommand
2121
from torchx.cli.cmd_log import get_logs
22-
from torchx.runner import config, get_runner, Runner
23-
from torchx.runner.config import load_sections
22+
from torchx.config import get_config_store
23+
from torchx.runner import get_runner, Runner
2424
from torchx.schedulers import get_default_scheduler_name, get_scheduler_factories
2525
from torchx.specs.finder import (
2626
_Component,
@@ -63,7 +63,11 @@ def _parse_component_name_and_args(
6363
looks like an option (e.g. starts with "-")
6464
6565
"""
66-
component = config.get_config(prefix="cli", name="run", key="component", dirs=dirs)
66+
component = get_config_store(dirs).get_key(
67+
collection="run", key="component", label="cli"
68+
)
69+
70+
component = str(component) if component else None
6771
component_args = []
6872

6973
# make a copy of the input list to guard against side-effects
@@ -177,7 +181,12 @@ def _run(self, runner: Runner, args: argparse.Namespace) -> None:
177181
run_opts = runner.run_opts()
178182
scheduler_opts = run_opts[args.scheduler]
179183
cfg = scheduler_opts.cfg_from_str(args.scheduler_args)
180-
config.apply(scheduler=args.scheduler, cfg=cfg, dirs=CONFIG_DIRS)
184+
185+
type_hints = {x[0]: x[1].opt_type for x in scheduler_opts}
186+
config_data = get_config_store(CONFIG_DIRS).get(
187+
args.scheduler, type_hints=type_hints
188+
)
189+
cfg = config_data.apply_to(cfg)
181190

182191
component, component_args = _parse_component_name_and_args(
183192
args.component_name_and_args,
@@ -226,18 +235,22 @@ def _run(self, runner: Runner, args: argparse.Namespace) -> None:
226235
logger.error(error_msg)
227236
sys.exit(1)
228237
except specs.InvalidRunConfigException as e:
238+
logger.error(e)
229239
error_msg = (
230240
f"Scheduler arg is incorrect or missing required option: `{e.cfg_key}`\n"
231241
f"Run `torchx runopts` to check configuration for `{args.scheduler}` scheduler\n"
232242
f"Use `-cfg` to specify run cfg as `key1=value1,key2=value2` pair\n"
233-
"of setup `.torchxconfig` file, see: https://pytorch.org/torchx/main/experimental/runner.config.html"
243+
"of setup `.torchxconfig` file, see: https://pytorch.org/torchx/main/runner.config.html"
234244
)
235245
logger.error(error_msg)
236246
sys.exit(1)
237247

238248
def run(self, args: argparse.Namespace) -> None:
239249
os.environ["TORCHX_CONTEXT_NAME"] = os.getenv("TORCHX_CONTEXT_NAME", "cli_run")
240-
component_defaults = load_sections(prefix="component", dirs=CONFIG_DIRS)
250+
config_data = get_config_store(CONFIG_DIRS).get_all(label="component")
251+
component_defaults = {}
252+
for conf in config_data:
253+
component_defaults[conf.coll_name] = dict(conf)
241254
with get_runner(component_defaults=component_defaults) as runner:
242255
self._run(runner, args)
243256

torchx/config/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
from .config import ( # noqa
8+
ConfigData,
9+
ConfigStore,
10+
get_config_store,
11+
IniConfigStore,
12+
seed_config_store_with_schedulers,
13+
)

0 commit comments

Comments
 (0)