_handler.py 10.9 KB
Newer Older
1
2
3
4
5
6
7
8
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""SuperBench CLI command handler."""

from pathlib import Path

from knack.util import CLIError
9
from omegaconf import OmegaConf
10
11

import superbench
12
13
from superbench.runner import SuperBenchRunner
from superbench.executor import SuperBenchExecutor
14
from superbench.common.utils import create_sb_output_dir, get_sb_config
15
16
17
18
19
20
21
22
23


def check_argument_file(name, file):
    """Check file path in CLI arguments.

    Args:
        name (str): argument name.
        file (str): file path.

Yifan Xiong's avatar
Yifan Xiong committed
24
25
26
    Returns:
        str: Absolute file path if it exists.

27
28
29
    Raises:
        CLIError: If file does not exist.
    """
Yifan Xiong's avatar
Yifan Xiong committed
30
31
32
33
34
    if file:
        if not Path(file).exists():
            raise CLIError('{} {} does not exist.'.format(name, file))
        return str(Path(file).resolve())
    return file
35
36


Yifan Xiong's avatar
Yifan Xiong committed
37
38
39
40
41
42
43
def split_docker_domain(name):
    """Split Docker image name to domain and remainder part.

    Ported from https://github.com/distribution/distribution/blob/v2.7.1/reference/normalize.go#L62-L76.

    Args:
        name (str): Docker image name.
44
45

    Returns:
Yifan Xiong's avatar
Yifan Xiong committed
46
47
        str: Docker registry domain.
        str: Remainder part.
48
    """
Yifan Xiong's avatar
Yifan Xiong committed
49
50
    legacy_default_domain = 'index.docker.io'
    default_domain = 'docker.io'
51

Yifan Xiong's avatar
Yifan Xiong committed
52
53
54
55
56
57
58
59
60
61
62
    i = name.find('/')
    domain, remainder = '', ''
    if i == -1 or ('.' not in name[:i] and ':' not in name[:i] and name[:i] != 'localhost'):
        domain, remainder = default_domain, name
    else:
        domain, remainder = name[:i], name[i + 1:]
    if domain == legacy_default_domain:
        domain = default_domain
    if domain == default_domain and '/' not in remainder:
        remainder = 'library/{}'.format(remainder)
    return domain, remainder
63

Yifan Xiong's avatar
Yifan Xiong committed
64

65
def process_config_arguments(config_file=None, config_override=None, output_dir=None):
Yifan Xiong's avatar
Yifan Xiong committed
66
67
68
69
70
71
    """Process configuration arguments.

    Args:
        config_file (str, optional): Path to SuperBench config file. Defaults to None.
        config_override (str, optional): Extra arguments to override config_file,
            following [Hydra syntax](https://hydra.cc/docs/advanced/override_grammar/basic). Defaults to None.
72
        output_dir (str, optional): Path to output directory. Defaults to None.
Yifan Xiong's avatar
Yifan Xiong committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

    Returns:
        DictConfig: SuperBench config object.
        str: Dir for output.

    Raises:
        CLIError: If input arguments are invalid.
    """
    config_file = check_argument_file('config_file', config_file)

    # SuperBench config
    sb_config = get_sb_config(config_file)
    if config_override:
        sb_config_from_override = OmegaConf.from_dotlist(config_override)
        sb_config = OmegaConf.merge(sb_config, sb_config_from_override)

    # Create output directory
90
    sb_output_dir = create_sb_output_dir(output_dir)
Yifan Xiong's avatar
Yifan Xiong committed
91

92
    return sb_config, sb_output_dir
Yifan Xiong's avatar
Yifan Xiong committed
93
94
95
96


def process_runner_arguments(
    docker_image='superbench/superbench',
97
98
    docker_username=None,
    docker_password=None,
99
    no_docker=False,
100
101
102
103
    host_file=None,
    host_list=None,
    host_username=None,
    host_password=None,
Yifan Xiong's avatar
Yifan Xiong committed
104
    private_key=None,
105
    output_dir=None,
Yifan Xiong's avatar
Yifan Xiong committed
106
107
    config_file=None,
    config_override=None
108
):
Yifan Xiong's avatar
Yifan Xiong committed
109
    """Process runner related arguments.
110
111

    Args:
Yifan Xiong's avatar
Yifan Xiong committed
112
        docker_image (str, optional): Docker image URI. Defaults to superbench/superbench:latest.
113
114
        docker_username (str, optional): Docker registry username if authentication is needed. Defaults to None.
        docker_password (str, optional): Docker registry password if authentication is needed. Defaults to None.
115
        no_docker (bool, optional): Run on host directly without Docker. Defaults to False.
116
117
118
119
120
        host_file (str, optional): Path to Ansible inventory host file. Defaults to None.
        host_list (str, optional): Comma separated host list. Defaults to None.
        host_username (str, optional): Host username if needed. Defaults to None.
        host_password (str, optional): Host password or key passphase if needed. Defaults to None.
        private_key (str, optional): Path to private key if needed. Defaults to None.
121
        output_dir (str, optional): Path to output directory. Defaults to None.
Yifan Xiong's avatar
Yifan Xiong committed
122
123
124
125
126
127
128
129
130
        config_file (str, optional): Path to SuperBench config file. Defaults to None.
        config_override (str, optional): Extra arguments to override config_file,
            following [Hydra syntax](https://hydra.cc/docs/advanced/override_grammar/basic). Defaults to None.

    Returns:
        DictConfig: SuperBench config object.
        DictConfig: Docker config object.
        DictConfig: Ansible config object.
        str: Dir for output.
131
132
133
134

    Raises:
        CLIError: If input arguments are invalid.
    """
135
136
    if bool(docker_username) != bool(docker_password):
        raise CLIError('Must specify both docker_username and docker_password if authentication is needed.')
137
138
    if not (host_file or host_list):
        raise CLIError('Must specify one of host_file or host_list.')
Yifan Xiong's avatar
Yifan Xiong committed
139
140
    host_file = check_argument_file('host_file', host_file)
    private_key = check_argument_file('private_key', private_key)
141

Yifan Xiong's avatar
Yifan Xiong committed
142
143
144
145
146
147
148
    # Docker config
    docker_config = OmegaConf.create(
        {
            'image': docker_image,
            'username': docker_username,
            'password': docker_password,
            'registry': split_docker_domain(docker_image)[0],
149
            'skip': no_docker,
Yifan Xiong's avatar
Yifan Xiong committed
150
151
152
153
154
155
156
157
158
159
160
161
        }
    )
    # Ansible config
    ansible_config = OmegaConf.create(
        {
            'host_file': host_file,
            'host_list': host_list,
            'host_username': host_username,
            'host_password': host_password,
            'private_key': private_key,
        }
    )
162

163
164
165
166
167
    sb_config, sb_output_dir = process_config_arguments(
        config_file=config_file,
        config_override=config_override,
        output_dir=output_dir,
    )
168

169
    return docker_config, ansible_config, sb_config, sb_output_dir
Yifan Xiong's avatar
Yifan Xiong committed
170
171
172
173
174
175
176
177
178
179
180


def version_command_handler():
    """Print the current SuperBench tool version.

    Returns:
        str: current SuperBench tool version.
    """
    return superbench.__version__


181
def exec_command_handler(config_file=None, config_override=None, output_dir=None):
182
183
184
185
186
187
    """Run the SuperBench benchmarks locally.

    Args:
        config_file (str, optional): Path to SuperBench config file. Defaults to None.
        config_override (str, optional): Extra arguments to override config_file,
            following [Hydra syntax](https://hydra.cc/docs/advanced/override_grammar/basic). Defaults to None.
188
        output_dir (str, optional): Path to output directory. Defaults to None.
189
190
191
192

    Raises:
        CLIError: If input arguments are invalid.
    """
193
194
195
196
197
    sb_config, sb_output_dir = process_config_arguments(
        config_file=config_file,
        config_override=config_override,
        output_dir=output_dir,
    )
198

199
    executor = SuperBenchExecutor(sb_config, sb_output_dir)
Yifan Xiong's avatar
Yifan Xiong committed
200
    executor.exec()
201
202


Yifan Xiong's avatar
Yifan Xiong committed
203
204
205
206
207
208
209
210
def deploy_command_handler(
    docker_image='superbench/superbench',
    docker_username=None,
    docker_password=None,
    host_file=None,
    host_list=None,
    host_username=None,
    host_password=None,
211
    output_dir=None,
Yifan Xiong's avatar
Yifan Xiong committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    private_key=None
):
    """Deploy the SuperBench environments to all given nodes.

    Deploy SuperBench environments on all nodes, including:
    1. check drivers
    2. install required system dependencies
    3. install Docker and container runtime
    4. pull Docker image

    Args:
        docker_image (str, optional): Docker image URI. Defaults to superbench/superbench:latest.
        docker_username (str, optional): Docker registry username if authentication is needed. Defaults to None.
        docker_password (str, optional): Docker registry password if authentication is needed. Defaults to None.
        host_file (str, optional): Path to Ansible inventory host file. Defaults to None.
        host_list (str, optional): Comma separated host list. Defaults to None.
        host_username (str, optional): Host username if needed. Defaults to None.
        host_password (str, optional): Host password or key passphase if needed. Defaults to None.
230
        output_dir (str, optional): Path to output directory. Defaults to None.
Yifan Xiong's avatar
Yifan Xiong committed
231
232
233
234
235
        private_key (str, optional): Path to private key if needed. Defaults to None.

    Raises:
        CLIError: If input arguments are invalid.
    """
236
    docker_config, ansible_config, sb_config, sb_output_dir = process_runner_arguments(
Yifan Xiong's avatar
Yifan Xiong committed
237
238
239
        docker_image=docker_image,
        docker_username=docker_username,
        docker_password=docker_password,
240
        no_docker=False,
Yifan Xiong's avatar
Yifan Xiong committed
241
242
243
244
        host_file=host_file,
        host_list=host_list,
        host_username=host_username,
        host_password=host_password,
245
        output_dir=output_dir,
Yifan Xiong's avatar
Yifan Xiong committed
246
247
248
        private_key=private_key,
    )

249
    runner = SuperBenchRunner(sb_config, docker_config, ansible_config, sb_output_dir)
250
    runner.deploy()
251
252
253


def run_command_handler(
Yifan Xiong's avatar
Yifan Xiong committed
254
    docker_image='superbench/superbench',
255
256
    docker_username=None,
    docker_password=None,
257
    no_docker=False,
258
259
260
261
    host_file=None,
    host_list=None,
    host_username=None,
    host_password=None,
262
    output_dir=None,
263
264
265
266
267
268
269
270
271
    private_key=None,
    config_file=None,
    config_override=None
):
    """Run the SuperBench benchmarks distributedly.

    Run all benchmarks on given nodes.

    Args:
Yifan Xiong's avatar
Yifan Xiong committed
272
        docker_image (str, optional): Docker image URI. Defaults to superbench/superbench:latest.
273
274
        docker_username (str, optional): Docker registry username if authentication is needed. Defaults to None.
        docker_password (str, optional): Docker registry password if authentication is needed. Defaults to None.
275
        no_docker (bool, optional): Run on host directly without Docker. Defaults to False.
276
277
278
279
        host_file (str, optional): Path to Ansible inventory host file. Defaults to None.
        host_list (str, optional): Comma separated host list. Defaults to None.
        host_username (str, optional): Host username if needed. Defaults to None.
        host_password (str, optional): Host password or key passphase if needed. Defaults to None.
280
        output_dir (str, optional): Path to output directory. Defaults to None.
281
282
283
284
285
286
287
288
        private_key (str, optional): Path to private key if needed. Defaults to None.
        config_file (str, optional): Path to SuperBench config file. Defaults to None.
        config_override (str, optional): Extra arguments to override config_file,
            following [Hydra syntax](https://hydra.cc/docs/advanced/override_grammar/basic). Defaults to None.

    Raises:
        CLIError: If input arguments are invalid.
    """
289
    docker_config, ansible_config, sb_config, sb_output_dir = process_runner_arguments(
Yifan Xiong's avatar
Yifan Xiong committed
290
291
292
        docker_image=docker_image,
        docker_username=docker_username,
        docker_password=docker_password,
293
        no_docker=no_docker,
Yifan Xiong's avatar
Yifan Xiong committed
294
295
296
297
        host_file=host_file,
        host_list=host_list,
        host_username=host_username,
        host_password=host_password,
298
        output_dir=output_dir,
Yifan Xiong's avatar
Yifan Xiong committed
299
300
301
302
        private_key=private_key,
        config_file=config_file,
        config_override=config_override,
    )
303

304
    runner = SuperBenchRunner(sb_config, docker_config, ansible_config, sb_output_dir)
305
    runner.run()