-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·203 lines (193 loc) · 8.92 KB
/
Copy pathcli.py
File metadata and controls
executable file
·203 lines (193 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import click
from click.core import Context, Option
from ding import __TITLE__, __VERSION__, __AUTHOR__, __AUTHOR_EMAIL__
from .predefined_config import get_predefined_config
def print_version(ctx: Context, param: Option, value: bool) -> None:
if not value or ctx.resilient_parsing:
return
click.echo('{title}, version {version}.'.format(title=__TITLE__, version=__VERSION__))
click.echo('Developed by {author}, {email}.'.format(author=__AUTHOR__, email=__AUTHOR_EMAIL__))
ctx.exit()
def print_registry(ctx: Context, param: Option, value: str):
if value is None:
return
from ding.utils import registries # noqa
if value not in registries:
click.echo('[ERROR]: not support registry name: {}'.format(value))
else:
registered_info = registries[value].query_details()
click.echo('Available {}: [{}]'.format(value, '|'.join(registered_info.keys())))
for alias, info in registered_info.items():
click.echo('\t{}: registered at {}#{}'.format(alias, info[0], info[1]))
ctx.exit()
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.command(context_settings=CONTEXT_SETTINGS)
@click.option(
'-v',
'--version',
is_flag=True,
callback=print_version,
expose_value=False,
is_eager=True,
help="Show package's version information."
)
@click.option(
'-q',
'--query-registry',
type=str,
callback=print_registry,
expose_value=False,
is_eager=True,
help='query registered module or function, show name and path'
)
@click.option(
'-m',
'--mode',
type=click.Choice(['serial', 'serial_onpolicy', 'serial_sqil', 'serial_dqfd', 'parallel', 'dist', 'eval']),
help='serial-train or parallel-train or dist-train or eval'
)
@click.option('-c', '--config', type=str, help='Path to DRL experiment config')
@click.option(
'-s',
'--seed',
type=int,
default=0,
help='random generator seed(for all the possible package: random, numpy, torch and user env)'
)
@click.option('-e', '--env', type=str, help='RL env name')
@click.option('-p', '--policy', type=str, help='DRL policy name')
@click.option('--train-iter', type=int, default=int(1e8), help='Policy training iterations')
@click.option('--load-path', type=str, default=None, help='Path to load ckpt')
@click.option('--replay-path', type=str, default=None, help='Path to save replay')
# the following arguments are only applied to dist mode
@click.option('--enable-total-log', type=bool, help='whether enable the total DI-engine system log', default=False)
@click.option('--disable-flask-log', type=bool, help='whether disable flask log', default=True)
@click.option(
'-P', '--platform', type=click.Choice(['local', 'slurm', 'k8s']), help='local or slurm or k8s', default='local'
)
@click.option(
'-M',
'--module',
type=click.Choice(['config', 'collector', 'learner', 'coordinator', 'learner_aggregator', 'spawn_learner']),
help='dist module type'
)
@click.option('--module-name', type=str, help='dist module name')
@click.option('-cdh', '--coordinator-host', type=str, help='coordinator host', default='0.0.0.0')
@click.option('-cdp', '--coordinator-port', type=int, help='coordinator port')
@click.option('-lh', '--learner-host', type=str, help='learner host', default='0.0.0.0')
@click.option('-lp', '--learner-port', type=int, help='learner port')
@click.option('-clh', '--collector-host', type=str, help='collector host', default='0.0.0.0')
@click.option('-clp', '--collector-port', type=int, help='collector port')
@click.option('-agh', '--aggregator-host', type=str, help='aggregator slave host', default='0.0.0.0')
@click.option('-agp', '--aggregator-port', type=int, help='aggregator slave port')
@click.option('--add', type=click.Choice(['collector', 'learner']), help='add replicas type')
@click.option('--delete', type=click.Choice(['collector', 'learner']), help='delete replicas type')
@click.option('--restart', type=click.Choice(['collector', 'learner']), help='restart replicas type')
@click.option('--kubeconfig', type=str, default=None, help='the path of Kubernetes configuration file')
@click.option('-cdn', '--coordinator-name', type=str, default=None, help='coordinator name')
@click.option('-ns', '--namespace', type=str, default=None, help='job namespace')
@click.option('-rs', '--replicas', type=int, default=1, help='number of replicas to add/delete/restart')
@click.option('-rpn', '--restart-pod-name', type=str, default=None, help='restart pod name')
@click.option('--cpus', type=int, default=0, help='The requested CPU, read the value from DIJob yaml by default')
@click.option('--gpus', type=int, default=0, help='The requested GPU, read the value from DIJob yaml by default')
@click.option(
'--memory', type=str, default=None, help='The requested Memory, read the value from DIJob yaml by default'
)
def cli(
# serial/eval
mode: str,
config: str,
seed: int,
env: str,
policy: str,
train_iter: int,
load_path: str,
replay_path: str,
# parallel/dist
platform: str,
coordinator_host: str,
coordinator_port: int,
learner_host: str,
learner_port: int,
collector_host: str,
collector_port: int,
aggregator_host: str,
aggregator_port: int,
enable_total_log: bool,
disable_flask_log: bool,
module: str,
module_name: str,
# add/delete/restart
add: str,
delete: str,
restart: str,
kubeconfig: str,
coordinator_name: str,
namespace: str,
replicas: int,
cpus: int,
gpus: int,
memory: str,
restart_pod_name: str,
):
if mode == 'serial':
from .serial_entry import serial_pipeline
if config is None:
config = get_predefined_config(env, policy)
serial_pipeline(config, seed, max_iterations=train_iter)
elif mode == 'serial_onpolicy':
from .serial_entry_onpolicy import serial_pipeline_onpolicy
if config is None:
config = get_predefined_config(env, policy)
serial_pipeline_onpolicy(config, seed, max_iterations=train_iter)
elif mode == 'serial_sqil':
if config == 'lunarlander_sqil_config.py' or 'cartpole_sqil_config.py' or 'pong_sqil_config.py' \
or 'spaceinvaders_sqil_config.py' or 'qbert_sqil_config.py':
from .serial_entry_sqil import serial_pipeline_sqil
if config is None:
config = get_predefined_config(env, policy)
expert_config = input("Enter the name of the config you used to generate your expert model: ")
serial_pipeline_sqil(config, expert_config, seed, max_iterations=train_iter)
elif mode == 'serial_dqfd':
from .serial_entry_dqfd import serial_pipeline_dqfd
if config is None:
config = get_predefined_config(env, policy)
expert_config = input("Enter the name of the config you used to generate your expert model: ")
serial_pipeline_dqfd(config, expert_config, seed, max_iterations=train_iter)
elif mode == 'parallel':
from .parallel_entry import parallel_pipeline
parallel_pipeline(config, seed, enable_total_log, disable_flask_log)
elif mode == 'dist':
from .dist_entry import dist_launch_coordinator, dist_launch_collector, dist_launch_learner, \
dist_prepare_config, dist_launch_learner_aggregator, dist_launch_spawn_learner, \
dist_add_replicas, dist_delete_replicas, dist_restart_replicas
if module == 'config':
dist_prepare_config(
config, seed, platform, coordinator_host, learner_host, collector_host, coordinator_port, learner_port,
collector_port
)
elif module == 'coordinator':
dist_launch_coordinator(config, seed, coordinator_port, disable_flask_log)
elif module == 'learner_aggregator':
dist_launch_learner_aggregator(
config, seed, aggregator_host, aggregator_port, module_name, disable_flask_log
)
elif module == 'collector':
dist_launch_collector(config, seed, collector_port, module_name, disable_flask_log)
elif module == 'learner':
dist_launch_learner(config, seed, learner_port, module_name, disable_flask_log)
elif module == 'spawn_learner':
dist_launch_spawn_learner(config, seed, learner_port, module_name, disable_flask_log)
elif add in ['collector', 'learner']:
dist_add_replicas(add, kubeconfig, replicas, coordinator_name, namespace, cpus, gpus, memory)
elif delete in ['collector', 'learner']:
dist_delete_replicas(delete, kubeconfig, replicas, coordinator_name, namespace)
elif restart in ['collector', 'learner']:
dist_restart_replicas(restart, kubeconfig, coordinator_name, namespace, restart_pod_name)
else:
raise Exception
elif mode == 'eval':
from .application_entry import eval
if config is None:
config = get_predefined_config(env, policy)
eval(config, seed, load_path=load_path, replay_path=replay_path)