Unverified Commit 91b44bc5 authored by Yifan Xiong's avatar Yifan Xiong Committed by GitHub
Browse files

CLI: Code Revision - Use omegaconf to replace hydra for configuration (#27)

Use omegaconf to replace hydra for configuration system:
* remove hydra
* use omegaconf to merge configurations
parent 0972b223
...@@ -133,9 +133,8 @@ def run(self): ...@@ -133,9 +133,8 @@ def run(self):
packages=find_packages(exclude=['tests']), packages=find_packages(exclude=['tests']),
python_requires='>=3.6, <4', python_requires='>=3.6, <4',
install_requires=[ install_requires=[
'hydra-colorlog>=1.0.0',
'hydra-core>=1.0.4',
'knack>=0.7.2', 'knack>=0.7.2',
'omegaconf>=2.0.6',
], ],
extras_require={ extras_require={
'dev': ['pre-commit>=2.10.0'], 'dev': ['pre-commit>=2.10.0'],
......
...@@ -3,6 +3,6 @@ ...@@ -3,6 +3,6 @@
"""SuperBench cli module.""" """SuperBench cli module."""
from .sb import SuperBenchCLI from superbench.cli.sb import SuperBenchCLI
__all__ = ['SuperBenchCLI'] __all__ = ['SuperBenchCLI']
...@@ -49,6 +49,7 @@ def load_arguments(self, command): ...@@ -49,6 +49,7 @@ def load_arguments(self, command):
'config_override', 'config_override',
options_list=('--config-override', '-C'), options_list=('--config-override', '-C'),
type=str, type=str,
help='Extra arguments to override config_file, following Hydra syntax.' nargs='+',
help='Extra arguments to override config_file.'
) )
super().load_arguments(command) super().load_arguments(command)
...@@ -4,13 +4,13 @@ ...@@ -4,13 +4,13 @@
"""SuperBench CLI command handler.""" """SuperBench CLI command handler."""
import os import os
import yaml
from pathlib import Path from pathlib import Path
from knack.util import CLIError from knack.util import CLIError
from omegaconf import OmegaConf
import superbench import superbench
from superbench.common.utils import get_sb_command, get_config, new_output_dir from superbench.common.utils import create_output_dir, get_sb_config
def check_argument_file(name, file): def check_argument_file(name, file):
...@@ -67,6 +67,8 @@ def deploy_command_handler( ...@@ -67,6 +67,8 @@ def deploy_command_handler(
Raises: Raises:
CLIError: If input arguments are invalid. CLIError: If input arguments are invalid.
""" """
if bool(docker_username) != bool(docker_password):
raise CLIError('Must specify both docker_username and docker_password if authentication is needed.')
if not (host_file or host_list): if not (host_file or host_list):
raise CLIError('Must specify one of host_file or host_list.') raise CLIError('Must specify one of host_file or host_list.')
check_argument_file('host_file', host_file) check_argument_file('host_file', host_file)
...@@ -95,15 +97,20 @@ def exec_command_handler( ...@@ -95,15 +97,20 @@ def exec_command_handler(
raise CLIError('Must specify both docker_username and docker_password if authentication is needed.') raise CLIError('Must specify both docker_username and docker_password if authentication is needed.')
check_argument_file('config_file', config_file) check_argument_file('config_file', config_file)
# Dump configs into outputs/date/config.merge.yaml # Docker config
config = get_config(config_file) docker_config = OmegaConf.create()
config['docker'] = {}
for key in ['image', 'username', 'password']: for key in ['image', 'username', 'password']:
config['docker'][key] = eval('docker_{}'.format(key)) docker_config[key] = eval('docker_{}'.format(key))
output_dir = new_output_dir() # SuperBench config
with (Path(output_dir) / 'config.merge.yaml').open(mode='w') as f: sb_config = get_sb_config(config_file)
yaml.safe_dump(config, f) if config_override:
os.system(get_sb_command('sb-exec', output_dir, config_override or '')) sb_config_from_override = OmegaConf.from_dotlist(config_override)
sb_config = OmegaConf.merge(sb_config, sb_config_from_override)
# Create output directory
output_dir = create_output_dir()
os.system('sb-exec {}'.format(output_dir))
def run_command_handler( def run_command_handler(
...@@ -146,15 +153,21 @@ def run_command_handler( ...@@ -146,15 +153,21 @@ def run_command_handler(
check_argument_file('private_key', private_key) check_argument_file('private_key', private_key)
check_argument_file('config_file', config_file) check_argument_file('config_file', config_file)
# Dump configs into outputs/date/config.merge.yaml # Docker config
config = get_config(config_file) docker_config = OmegaConf.create()
config['docker'] = {}
for key in ['image', 'username', 'password']: for key in ['image', 'username', 'password']:
config['docker'][key] = eval('docker_{}'.format(key)) docker_config[key] = eval('docker_{}'.format(key))
config['ansible'] = {} # Ansible config
ansible_config = OmegaConf.create()
for key in ['file', 'list', 'username', 'password']: for key in ['file', 'list', 'username', 'password']:
config['ansible']['host_{}'.format(key)] = eval('host_{}'.format(key)) ansible_config['host_{}'.format(key)] = eval('host_{}'.format(key))
output_dir = new_output_dir() # SuperBench config
with (Path(output_dir) / 'config.merge.yaml').open(mode='w') as f: sb_config = get_sb_config(config_file)
yaml.safe_dump(config, f) if config_override:
os.system(get_sb_command('sb-run', output_dir, config_override or '')) sb_config_from_override = OmegaConf.from_dotlist(config_override)
sb_config = OmegaConf.merge(sb_config, sb_config_from_override)
# Create output directory
output_dir = create_output_dir()
os.system('sb-run {}'.format(output_dir))
...@@ -5,15 +5,9 @@ ...@@ -5,15 +5,9 @@
"""SuperBench sb exec command.""" """SuperBench sb exec command."""
import hydra
from superbench.common.utils import logger def main():
@hydra.main(config_path='../config', config_name='default')
def main(config):
"""The main entrypoint for sb-exec.""" """The main entrypoint for sb-exec."""
logger.info(config)
# executor = SuperBenchExecutor(config) # executor = SuperBenchExecutor(config)
# executor.exec() # executor.exec()
......
...@@ -5,15 +5,9 @@ ...@@ -5,15 +5,9 @@
"""SuperBench sb run command.""" """SuperBench sb run command."""
import hydra
from superbench.common.utils import logger def main():
@hydra.main(config_path='../config', config_name='default')
def main(config):
"""The main entrypoint for sb-run.""" """The main entrypoint for sb-run."""
logger.info(config)
# runner = SuperBenchRunner(config) # runner = SuperBenchRunner(config)
# runner.run() # runner.run()
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
"""Exposes the interface of SuperBench common utilities.""" """Exposes the interface of SuperBench common utilities."""
from .logging import logger from .logging import logger
from .file_handler import new_output_dir, get_config from superbench.common.utils.file_handler import create_output_dir, get_sb_config
from .command import get_sb_command
__all__ = ['logger', 'new_output_dir', 'get_config', 'get_sb_command'] __all__ = ['logger', 'create_output_dir', 'get_sb_config']
...@@ -2,23 +2,3 @@ ...@@ -2,23 +2,3 @@
# Licensed under the MIT License. # Licensed under the MIT License.
"""Utilities for command.""" """Utilities for command."""
def get_sb_command(cli, output_path, config_override):
"""Get sb command for sb-run or sb-exec.
Args:
cli (str): CLI name.
output_path (str): Output directory path.
config_override (str): Extra arguments to override config.
Returns:
str: Command to run.
"""
sb_cmd = '{cli} ' \
'--config-name=config.merge ' \
'--config-dir={path} ' \
'hydra.run.dir={path} ' \
'hydra.sweep.dir={path} ' \
'{args}'.format(cli=cli, path=output_path, args=config_override)
return sb_cmd.strip()
...@@ -3,13 +3,14 @@ ...@@ -3,13 +3,14 @@
"""Utilities for file.""" """Utilities for file."""
import yaml
from pathlib import Path from pathlib import Path
from datetime import datetime from datetime import datetime
from omegaconf import OmegaConf
def new_output_dir():
"""Generate a new output directory. def create_output_dir():
"""Create a new output directory.
Generate a new output directory name based on current time and create it on filesystem. Generate a new output directory name based on current time and create it on filesystem.
...@@ -22,7 +23,7 @@ def new_output_dir(): ...@@ -22,7 +23,7 @@ def new_output_dir():
return str(output_path) return str(output_path)
def get_config(config_file): def get_sb_config(config_file):
"""Read SuperBench config yaml. """Read SuperBench config yaml.
Read config file, use default config if None is provided. Read config file, use default config if None is provided.
...@@ -31,12 +32,10 @@ def get_config(config_file): ...@@ -31,12 +32,10 @@ def get_config(config_file):
config_file (str): config file path. config_file (str): config file path.
Returns: Returns:
dict: Config object, None if file does not exist. OmegaConf: Config object, None if file does not exist.
""" """
here = Path(__file__).parent.resolve() default_config_file = Path(__file__).parent / '../../config/default.yaml'
p = Path(config_file) if config_file else here / '../../config/default.yaml' p = Path(config_file) if config_file else default_config_file
if not p.is_file(): if not p.is_file():
return None return None
with p.open() as f: return OmegaConf.load(str(p))
config = yaml.safe_load(f)
return config
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment