nnictl.py 11.7 KB
Newer Older
Deshui Yu's avatar
Deshui Yu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Copyright (c) Microsoft Corporation
# All rights reserved.
#
# MIT License
#
# Permission is hereby granted, free of charge,
# to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and
# to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


import argparse
Gems Guo's avatar
Gems Guo committed
23
import pkg_resources
Deshui Yu's avatar
Deshui Yu committed
24
from .launcher import create_experiment, resume_experiment
QuanluZhang's avatar
QuanluZhang committed
25
from .updater import update_searchspace, update_concurrency, update_duration, update_trialnum
Deshui Yu's avatar
Deshui Yu committed
26
from .nnictl_utils import *
QuanluZhang's avatar
QuanluZhang committed
27
from .package_management import *
goooxu's avatar
goooxu committed
28
from .constants import *
SparkSnail's avatar
SparkSnail committed
29
from .tensorboard_utils import *
Deshui Yu's avatar
Deshui Yu committed
30

chicm-ms's avatar
chicm-ms committed
31
32
33
34
if os.environ.get('COVERAGE_PROCESS_START'):
    import coverage
    coverage.process_startup()

Gems Guo's avatar
Gems Guo committed
35
36
def nni_info(*args):
    if args[0].version:
37
38
39
40
        try:
            print(pkg_resources.get_distribution('nni').version)
        except pkg_resources.ResolutionError as err:
            print_error('Get version failed, please use `pip3 list | grep nni` to check nni version!')
Gems Guo's avatar
Gems Guo committed
41
42
    else:
        print('please run "nnictl {positional argument} --help" to see nnictl guidance')
Deshui Yu's avatar
Deshui Yu committed
43
44
45

def parse_args():
    '''Definite the arguments users need to follow and input'''
46
    parser = argparse.ArgumentParser(prog='nnictl', description='use nnictl command to control nni experiments')
Gems Guo's avatar
Gems Guo committed
47
48
    parser.add_argument('--version', '-v', action='store_true')
    parser.set_defaults(func=nni_info)
Deshui Yu's avatar
Deshui Yu committed
49
50
51
52
53
54
55

    # create subparsers for args with sub values
    subparsers = parser.add_subparsers()

    # parse start command
    parser_start = subparsers.add_parser('create', help='create a new experiment')
    parser_start.add_argument('--config', '-c', required=True, dest='config', help='the path of yaml config file')
goooxu's avatar
goooxu committed
56
    parser_start.add_argument('--port', '-p', default=DEFAULT_REST_PORT, dest='port', help='the port of restful server')
57
    parser_start.add_argument('--debug', '-d', action='store_true', help=' set debug mode')
Deshui Yu's avatar
Deshui Yu committed
58
59
60
61
    parser_start.set_defaults(func=create_experiment)

    # parse resume command
    parser_resume = subparsers.add_parser('resume', help='resume a new experiment')
62
    parser_resume.add_argument('id', nargs='?', help='The id of the experiment you want to resume')
goooxu's avatar
goooxu committed
63
    parser_resume.add_argument('--port', '-p', default=DEFAULT_REST_PORT, dest='port', help='the port of restful server')
64
    parser_resume.add_argument('--debug', '-d', action='store_true', help=' set debug mode')
Deshui Yu's avatar
Deshui Yu committed
65
66
67
68
69
70
71
    parser_resume.set_defaults(func=resume_experiment)

    # parse update command
    parser_updater = subparsers.add_parser('update', help='update the experiment')
    #add subparsers for parser_updater
    parser_updater_subparsers = parser_updater.add_subparsers()
    parser_updater_searchspace = parser_updater_subparsers.add_parser('searchspace', help='update searchspace')
72
    parser_updater_searchspace.add_argument('id', nargs='?', help='the id of experiment')
Deshui Yu's avatar
Deshui Yu committed
73
74
    parser_updater_searchspace.add_argument('--filename', '-f', required=True)
    parser_updater_searchspace.set_defaults(func=update_searchspace)
goooxu's avatar
goooxu committed
75
    parser_updater_concurrency = parser_updater_subparsers.add_parser('concurrency', help='update concurrency')
76
    parser_updater_concurrency.add_argument('id', nargs='?', help='the id of experiment')
goooxu's avatar
goooxu committed
77
78
79
    parser_updater_concurrency.add_argument('--value', '-v', required=True)
    parser_updater_concurrency.set_defaults(func=update_concurrency)
    parser_updater_duration = parser_updater_subparsers.add_parser('duration', help='update duration')
80
    parser_updater_duration.add_argument('id', nargs='?', help='the id of experiment')
81
    parser_updater_duration.add_argument('--value', '-v', required=True, help='the unit of time should in {\'s\', \'m\', \'h\', \'d\'}')
goooxu's avatar
goooxu committed
82
    parser_updater_duration.set_defaults(func=update_duration)
83
84
85
86
    parser_updater_trialnum = parser_updater_subparsers.add_parser('trialnum', help='update maxtrialnum')
    parser_updater_trialnum.add_argument('--id', '-i', dest='id', help='the id of experiment')
    parser_updater_trialnum.add_argument('--value', '-v', required=True)
    parser_updater_trialnum.set_defaults(func=update_trialnum)
Deshui Yu's avatar
Deshui Yu committed
87
88
89

    #parse stop command
    parser_stop = subparsers.add_parser('stop', help='stop the experiment')
90
    parser_stop.add_argument('id', nargs='?', help='the id of experiment, use \'all\' to stop all running experiments')
Deshui Yu's avatar
Deshui Yu committed
91
92
93
94
95
96
97
    parser_stop.set_defaults(func=stop_experiment)

    #parse trial command
    parser_trial = subparsers.add_parser('trial', help='get trial information')
    #add subparsers for parser_trial
    parser_trial_subparsers = parser_trial.add_subparsers()
    parser_trial_ls = parser_trial_subparsers.add_parser('ls', help='list trial jobs')
98
    parser_trial_ls.add_argument('id', nargs='?', help='the id of experiment')
Deshui Yu's avatar
Deshui Yu committed
99
100
    parser_trial_ls.set_defaults(func=trial_ls)
    parser_trial_kill = parser_trial_subparsers.add_parser('kill', help='kill trial jobs')
101
102
    parser_trial_kill.add_argument('id', nargs='?', help='id of the trial to be killed')
    parser_trial_kill.add_argument('--experiment', '-E', required=True, dest='experiment', help='experiment id of the trial')
Deshui Yu's avatar
Deshui Yu committed
103
    parser_trial_kill.set_defaults(func=trial_kill)
Yan Ni's avatar
Yan Ni committed
104
105
106
107
    parser_trial_export = parser_trial_subparsers.add_parser('export', help='export trial job results to csv')
    parser_trial_export.add_argument('id', nargs='?', help='the id of experiment')
    parser_trial_export.add_argument('--file', '-f', required=True, dest='csv_path', help='target csv file path')
    parser_trial_export.set_defaults(func=export_trials_data)
Deshui Yu's avatar
Deshui Yu committed
108
109
110
111
112

    #parse experiment command
    parser_experiment = subparsers.add_parser('experiment', help='get experiment information')
    #add subparsers for parser_experiment
    parser_experiment_subparsers = parser_experiment.add_subparsers()
113
    parser_experiment_show = parser_experiment_subparsers.add_parser('show', help='show the information of experiment')
114
    parser_experiment_show.add_argument('id', nargs='?', help='the id of experiment')
115
    parser_experiment_show.set_defaults(func=list_experiment)
116
    parser_experiment_status = parser_experiment_subparsers.add_parser('status', help='show the status of experiment')
117
    parser_experiment_status.add_argument('id', nargs='?', help='the id of experiment')
118
    parser_experiment_status.set_defaults(func=experiment_status)
119
    parser_experiment_list = parser_experiment_subparsers.add_parser('list', help='list all of running experiment ids')
120
121
    parser_experiment_list.add_argument('all', nargs='?', help='list all of experiments')
    parser_experiment_list.set_defaults(func=experiment_list)
122
123
124
125
126
127
128

    #TODO:finish webui function
    #parse board command
    parser_webui = subparsers.add_parser('webui', help='get web ui information')
    #add subparsers for parser_board
    parser_webui_subparsers = parser_webui.add_subparsers()
    parser_webui_url = parser_webui_subparsers.add_parser('url', help='show the url of web ui')
129
    parser_webui_url.add_argument('id', nargs='?', help='the id of experiment')
130
    parser_webui_url.set_defaults(func=webui_url)
Deshui Yu's avatar
Deshui Yu committed
131
132
133
134

    #parse config command
    parser_config = subparsers.add_parser('config', help='get config information')
    parser_config_subparsers = parser_config.add_subparsers()
135
    parser_config_show = parser_config_subparsers.add_parser('show', help='show the information of config')
136
    parser_config_show.add_argument('id', nargs='?', help='the id of experiment')
137
    parser_config_show.set_defaults(func=get_config)
Deshui Yu's avatar
Deshui Yu committed
138
139
140

    #parse log command
    parser_log = subparsers.add_parser('log', help='get log information')
QuanluZhang's avatar
QuanluZhang committed
141
    # add subparsers for parser_log
Deshui Yu's avatar
Deshui Yu committed
142
143
    parser_log_subparsers = parser_log.add_subparsers()
    parser_log_stdout = parser_log_subparsers.add_parser('stdout', help='get stdout information')
144
    parser_log_stdout.add_argument('id', nargs='?', help='the id of experiment')
Deshui Yu's avatar
Deshui Yu committed
145
146
    parser_log_stdout.add_argument('--tail', '-T', dest='tail', type=int, help='get tail -100 content of stdout')
    parser_log_stdout.add_argument('--head', '-H', dest='head', type=int, help='get head -100 content of stdout')
goooxu's avatar
goooxu committed
147
    parser_log_stdout.add_argument('--path', action='store_true', default=False, help='get the path of stdout file')
Deshui Yu's avatar
Deshui Yu committed
148
149
    parser_log_stdout.set_defaults(func=log_stdout)
    parser_log_stderr = parser_log_subparsers.add_parser('stderr', help='get stderr information')
150
    parser_log_stderr.add_argument('id', nargs='?', help='the id of experiment')
Deshui Yu's avatar
Deshui Yu committed
151
152
    parser_log_stderr.add_argument('--tail', '-T', dest='tail', type=int, help='get tail -100 content of stderr')
    parser_log_stderr.add_argument('--head', '-H', dest='head', type=int, help='get head -100 content of stderr')
goooxu's avatar
goooxu committed
153
    parser_log_stderr.add_argument('--path', action='store_true', default=False, help='get the path of stderr file')
Deshui Yu's avatar
Deshui Yu committed
154
    parser_log_stderr.set_defaults(func=log_stderr)
155
    parser_log_trial = parser_log_subparsers.add_parser('trial', help='get trial log path')
156
157
    parser_log_trial.add_argument('id', nargs='?', help='id of the trial to be found the log path')
    parser_log_trial.add_argument('--experiment', '-E', dest='experiment', help='experiment id of the trial, xperiment ID of the trial, required when id is not empty.')
158
159
    parser_log_trial.set_defaults(func=log_trial)

QuanluZhang's avatar
QuanluZhang committed
160
161
162
163
164
165
    #parse package command
    parser_package = subparsers.add_parser('package', help='control nni tuner and assessor packages')
    # add subparsers for parser_package
    parser_package_subparsers = parser_package.add_subparsers()
    parser_package_install = parser_package_subparsers.add_parser('install', help='install packages')
    parser_package_install.add_argument('--name', '-n', dest='name', help='package name to be installed')
166
    parser_package_install.set_defaults(func=package_install) 
QuanluZhang's avatar
QuanluZhang committed
167
168
169
    parser_package_show = parser_package_subparsers.add_parser('show', help='show the information of packages')
    parser_package_show.set_defaults(func=package_show)

SparkSnail's avatar
SparkSnail committed
170
171
172
173
174
175
176
177
178
179
180
181
    #parse tensorboard command
    parser_tensorboard = subparsers.add_parser('tensorboard', help='manage tensorboard')
    parser_tensorboard_subparsers = parser_tensorboard.add_subparsers()
    parser_tensorboard_start = parser_tensorboard_subparsers.add_parser('start', help='start tensorboard')
    parser_tensorboard_start.add_argument('id', nargs='?', help='the id of experiment')
    parser_tensorboard_start.add_argument('--trialid', dest='trialid', help='the id of trial')
    parser_tensorboard_start.add_argument('--port', dest='port', default=6006, help='the port to start tensorboard')
    parser_tensorboard_start.set_defaults(func=start_tensorboard)
    parser_tensorboard_start = parser_tensorboard_subparsers.add_parser('stop', help='stop tensorboard')
    parser_tensorboard_start.add_argument('id', nargs='?', help='the id of experiment')
    parser_tensorboard_start.set_defaults(func=stop_tensorboard)

SparkSnail's avatar
SparkSnail committed
182
183
184
185
186
187
    #parse top command
    parser_top = subparsers.add_parser('top', help='monitor the experiment')
    parser_top.add_argument('--time', '-t', dest='time', type=int, default=3, help='the time interval to update the experiment status, ' \
    'the unit is second')
    parser_top.set_defaults(func=monitor_experiment)

Deshui Yu's avatar
Deshui Yu committed
188
189
190
191
192
    args = parser.parse_args()
    args.func(args)

if __name__ == '__main__':
    parse_args()