-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathhyp_cli.py
More file actions
278 lines (221 loc) · 7.63 KB
/
hyp_cli.py
File metadata and controls
278 lines (221 loc) · 7.63 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import click
import yaml
import json
import os
import subprocess
from pydantic import BaseModel, ValidationError, Field
from typing import Optional, Union
from importlib.metadata import version, PackageNotFoundError
from sagemaker.hyperpod.cli.commands.cluster import list_cluster, set_cluster_context, get_cluster_context, \
get_monitoring, describe_cluster
from sagemaker.hyperpod.cli.commands.cluster_stack import create_cluster_stack, describe_cluster_stack, \
list_cluster_stacks, update_cluster, delete_cluster_stack
from sagemaker.hyperpod.cli.commands.training import (
pytorch_create,
list_jobs,
pytorch_describe,
pytorch_delete,
pytorch_list_pods,
pytorch_get_logs,
pytorch_get_operator_logs,
pytorch_exec,
list_accelerator_partition_type,
)
from sagemaker.hyperpod.cli.commands.inference import (
js_create,
custom_create,
custom_invoke,
js_list,
custom_list,
js_describe,
custom_describe,
js_delete,
custom_delete,
js_list_pods,
custom_list_pods,
js_get_logs,
custom_get_logs,
js_get_operator_logs,
custom_get_operator_logs,
)
from sagemaker.hyperpod.cli.commands.space import (
space_create,
space_list,
space_describe,
space_delete,
space_update,
space_start,
space_stop,
space_get_logs,
space_portforward,
)
from sagemaker.hyperpod.cli.commands.space_template import (
space_template_create,
space_template_list,
space_template_describe,
space_template_delete,
space_template_update,
)
from sagemaker.hyperpod.cli.commands.space_access import space_access_create
from sagemaker.hyperpod.cli.commands.init import (
init,
reset,
configure,
validate,
_default_create
)
def get_package_version(package_name):
try:
return version(package_name)
except PackageNotFoundError:
return "Not installed"
def print_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
hyp_version = get_package_version("sagemaker-hyperpod")
pytorch_template_version = get_package_version("hyperpod-pytorch-job-template")
custom_inference_version = get_package_version("hyperpod-custom-inference-template")
jumpstart_inference_version = get_package_version("hyperpod-jumpstart-inference-template")
click.echo(f"hyp version: {hyp_version}")
click.echo(f"hyperpod-pytorch-job-template version: {pytorch_template_version}")
click.echo(f"hyperpod-custom-inference-template version: {custom_inference_version}")
click.echo(f"hyperpod-jumpstart-inference-template version: {jumpstart_inference_version}")
ctx.exit()
@click.group(context_settings={'max_content_width': 200})
@click.option('--version', is_flag=True, callback=print_version, expose_value=False, is_eager=True, help='Show version information')
def cli():
pass
class CLICommand(click.Group):
def __init__(self, *args, default_cmd: Union[str, None] = None, **kwargs):
super().__init__(*args, **kwargs)
self.default_cmd = default_cmd
def parse_args(self, ctx, args):
# Only inject default subcommand when:
# - user didn't name a subcommand, and
# - user didn't ask for help
if self.default_cmd:
# any non-flag token that is a known subcommand?
has_subcmd = any((not a.startswith("-")) and (a in self.commands) for a in args)
asked_for_help = any(a in ("-h", "--help") for a in args)
if (not has_subcmd) and (not asked_for_help):
args = [self.default_cmd] + args
return super().parse_args(ctx, args)
@cli.group(cls=CLICommand, default_cmd='_default_create')
def create():
"""
Create endpoints, pytorch jobs, cluster stacks, space, space access or space admin config.
If only used as 'hyp create' without [OPTIONS] COMMAND [ARGS] during init experience,
then it will validate configuration and render template files for deployment.
The generated files in the run directory can be used for actual deployment
to SageMaker HyperPod clusters or CloudFormation stacks.
Prerequisites for directly calling 'hyp create':
- Must be run in a directory initialized with 'hyp init'
- config.yaml and the appropriate template file must exist
"""
pass
@cli.group(cls=CLICommand)
def list():
"""List endpoints, pytorch jobs, cluster stacks, spaces, and space templates."""
pass
@cli.group(cls=CLICommand)
def describe():
"""Describe endpoints, pytorch jobs or cluster stacks, spaces or space template."""
pass
@cli.group(cls=CLICommand)
def update():
"""Update an existing HyperPod cluster configuration, space, or space template."""
pass
@cli.group(cls=CLICommand)
def delete():
"""Delete endpoints, pytorch jobs, space, space access or space template."""
pass
@cli.group(cls=CLICommand)
def start():
"""Start space resources."""
pass
@cli.group(cls=CLICommand)
def stop():
"""Stop space resources."""
pass
@cli.group(cls=CLICommand)
def portforward():
"""Port forward for space resources."""
pass
@cli.group(cls=CLICommand)
def list_pods():
"""List pods for endpoints or pytorch jobs."""
pass
@cli.group(cls=CLICommand)
def get_logs():
"""Get pod logs for endpoints, pytorch jobs or spaces."""
pass
@cli.group(cls=CLICommand)
def invoke():
"""Invoke model endpoints."""
pass
@cli.group(cls=CLICommand)
def get_operator_logs():
"""Get operator logs for endpoints."""
pass
@cli.group(cls=CLICommand)
def exec():
"""Execute commands in pods for endpoints or pytorch jobs."""
pass
cli.add_command(init)
cli.add_command(reset)
cli.add_command(configure)
cli.add_command(validate)
create.add_command(pytorch_create)
create.add_command(js_create)
create.add_command(custom_create)
_default_create.hidden = True
create.add_command(_default_create)
create.add_command(space_create)
create.add_command(space_template_create)
create.add_command(space_access_create)
list.add_command(list_jobs)
list.add_command(js_list)
list.add_command(custom_list)
list.add_command(list_cluster_stacks)
list.add_command(space_list)
list.add_command(space_template_list)
describe.add_command(pytorch_describe)
describe.add_command(js_describe)
describe.add_command(custom_describe)
describe.add_command(describe_cluster_stack)
describe.add_command(describe_cluster)
describe.add_command(space_describe)
describe.add_command(space_template_describe)
update.add_command(update_cluster)
update.add_command(space_update)
update.add_command(space_template_update)
delete.add_command(pytorch_delete)
delete.add_command(js_delete)
delete.add_command(custom_delete)
delete.add_command(delete_cluster_stack)
delete.add_command(space_delete)
delete.add_command(space_template_delete)
start.add_command(space_start)
stop.add_command(space_stop)
list_pods.add_command(pytorch_list_pods)
list_pods.add_command(js_list_pods)
list_pods.add_command(custom_list_pods)
get_logs.add_command(pytorch_get_logs)
get_logs.add_command(js_get_logs)
get_logs.add_command(custom_get_logs)
get_logs.add_command(space_get_logs)
portforward.add_command(space_portforward)
get_operator_logs.add_command(pytorch_get_operator_logs)
get_operator_logs.add_command(js_get_operator_logs)
get_operator_logs.add_command(custom_get_operator_logs)
invoke.add_command(custom_invoke)
invoke.add_command(custom_invoke, name="hyp-jumpstart-endpoint")
cli.add_command(list_cluster)
cli.add_command(set_cluster_context)
cli.add_command(get_cluster_context)
cli.add_command(get_monitoring)
# cli.add_command(create_cluster_stack) # Not supported yet
cli.add_command(list_accelerator_partition_type)
exec.add_command(pytorch_exec)
if __name__ == "__main__":
cli()