-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathtraining.py
More file actions
360 lines (313 loc) · 12.5 KB
/
training.py
File metadata and controls
360 lines (313 loc) · 12.5 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import click
import logging
import os
import yaml
import shutil
import subprocess
from pathlib import Path
from sagemaker.hyperpod.training.hyperpod_pytorch_job import HyperPodPytorchJob
from sagemaker.hyperpod.common.config import Metadata
import tempfile
from typing import List, Dict, Any, Optional, Callable, get_args, get_origin, Literal
from sagemaker.hyperpod.cli.training_utils import generate_click_command
from importlib.metadata import entry_points
from hyperpod_pytorch_job_template.registry import SCHEMA_REGISTRY
from sagemaker.hyperpod.common.telemetry.telemetry_logging import (
_hyperpod_telemetry_emitter,
)
from sagemaker.hyperpod.common.telemetry.constants import Feature
@click.command("hyp-pytorch-job")
@click.option("--version", default="1.0", help="Schema version to use")
@click.option("--debug", default=False, help="Enable debug mode")
@generate_click_command(
schema_pkg="hyperpod_pytorch_job_template",
registry=SCHEMA_REGISTRY,
)
@_hyperpod_telemetry_emitter(Feature.HYPERPOD_CLI, "create_pytorchjob_cli")
def pytorch_create(version, debug, config):
"""Create a PyTorch job."""
try:
click.echo(f"Using version: {version}")
job_name = config.get("name")
namespace = config.get("namespace")
spec = config.get("spec")
metadata_labels = config.get("labels")
# Prepare metadata
metadata_kwargs = {"name": job_name}
if namespace:
metadata_kwargs["namespace"] = namespace
if metadata_labels:
metadata_kwargs["labels"] = metadata_labels
# Prepare job kwargs
job_kwargs = {
"metadata": Metadata(**metadata_kwargs),
"replica_specs": spec.get("replica_specs"),
}
# Add nproc_per_node if present
if "nproc_per_node" in spec:
job_kwargs["nproc_per_node"] = spec.get("nproc_per_node")
# Add run_policy if present
if "run_policy" in spec:
job_kwargs["run_policy"] = spec.get("run_policy")
# Create job
job = HyperPodPytorchJob(**job_kwargs)
job.create(debug=debug)
except Exception as e:
raise click.UsageError(f"Failed to create job: {str(e)}")
@click.command("hyp-pytorch-job")
@click.option(
"--namespace",
"-n",
default="default",
help="Optional. The namespace to list jobs from. Defaults to 'default' namespace.",
)
@_hyperpod_telemetry_emitter(Feature.HYPERPOD_CLI, "list_pytorchjobs_cli")
def list_jobs(namespace: str):
"""List all HyperPod PyTorch jobs."""
try:
jobs = HyperPodPytorchJob.list(namespace=namespace)
if not jobs:
click.echo("No jobs found.")
return
# Define headers and widths
headers = ["NAME", "NAMESPACE", "STATUS", "AGE"]
widths = [30, 20, 15, 15]
# Print header
header = "".join(f"{h:<{w}}" for h, w in zip(headers, widths))
click.echo("\n" + header)
click.echo("-" * sum(widths))
# Print each job
for job in jobs:
# Get status from conditions
status = "Unknown"
age = "N/A"
if job.status and job.status.conditions:
for condition in reversed(job.status.conditions):
if condition.status == "True":
status = condition.type
break
# Calculate age
if job.status and job.status.conditions:
# Find the 'Created' condition to get the start time
created_condition = next(
(c for c in job.status.conditions if c.type == "Created"), None
)
if created_condition and created_condition.lastTransitionTime:
from datetime import datetime, timezone
start_time = datetime.fromisoformat(
created_condition.lastTransitionTime.replace("Z", "+00:00")
)
now = datetime.now(timezone.utc)
delta = now - start_time
if delta.days > 0:
age = f"{delta.days}d"
else:
hours = delta.seconds // 3600
if hours > 0:
age = f"{hours}h"
else:
minutes = (delta.seconds % 3600) // 60
age = f"{minutes}m"
# Format row
row = "".join(
[
f"{job.metadata.name:<{widths[0]}}",
f"{job.metadata.namespace:<{widths[1]}}",
f"{status:<{widths[2]}}",
f"{age:<{widths[3]}}",
]
)
click.echo(row)
click.echo() # Add empty line at the end
except Exception as e:
raise click.UsageError(f"Failed to list jobs: {str(e)}")
@click.command("hyp-pytorch-job")
@click.option(
"--job-name", required=True, help="Required. The name of the job to describe"
)
@click.option(
"--namespace",
"-n",
default="default",
help="Optional. The namespace of the job. Defaults to 'default' namespace.",
)
@_hyperpod_telemetry_emitter(Feature.HYPERPOD_CLI, "get_pytorchjob_cli")
def pytorch_describe(job_name: str, namespace: str):
"""Describe a HyperPod PyTorch job."""
try:
job = HyperPodPytorchJob.get(name=job_name, namespace=namespace)
if job is None:
raise click.UsageError(f"Job {job_name} not found in namespace {namespace}")
# Print basic info
click.echo("\nJob Details:")
click.echo("=" * 80)
click.echo(f"Name: {job.metadata.name}")
click.echo(f"Namespace: {job.metadata.namespace}")
# Print Spec details
click.echo("\nSpec:")
click.echo("-" * 80)
click.echo(f"Processes per Node: {getattr(job, 'nprocPerNode', 'N/A')}")
# Print Replica Specs
for replica in job.replicaSpecs:
click.echo(f"\nReplica Spec:")
click.echo(f" Name: {getattr(replica, 'name', 'N/A')}")
click.echo(f" Replicas: {getattr(replica, 'replicas', 'N/A')}")
click.echo(f" Spares: {getattr(replica, 'spares', 'N/A')}")
# Container details
if (
hasattr(replica, "template")
and hasattr(replica.template, "spec")
and hasattr(replica.template.spec, "containers")
):
for container in replica.template.spec.containers:
click.echo("\n Container:")
click.echo(
f" Name: {getattr(container, 'name', 'N/A')}"
)
click.echo(
f" Image: {getattr(container, 'image', 'N/A')}"
)
click.echo(
f" Image Pull Policy: {getattr(container, 'imagePullPolicy', 'N/A')}"
)
if container.resources:
click.echo(" Resources:")
if container.resources.limits:
click.echo(f" Limits: {container.resources.limits}")
if container.resources.requests:
click.echo(
f" Requests: {container.resources.requests}"
)
# Print Run Policy
click.echo("\nRun Policy:")
click.echo("-" * 80)
if hasattr(job, "runPolicy"):
click.echo(
f"Clean Pod Policy: {getattr(job.runPolicy, 'cleanPodPolicy', 'N/A')}"
)
click.echo(
f"TTL Seconds After Finished: {getattr(job.runPolicy, 'ttlSecondsAfterFinished', 'N/A')}"
)
else:
click.echo("Run Policy: N/A")
# Print Status
click.echo("\nStatus:")
click.echo("-" * 80)
if job.status:
if job.status.conditions:
click.echo("Conditions:")
for condition in job.status.conditions:
click.echo(
f" Type: {getattr(condition, 'type', 'N/A')}"
)
click.echo(
f" Status: {getattr(condition, 'status', 'N/A')}"
)
click.echo(
f" Last Transition: {getattr(condition, 'lastTransitionTime', 'N/A')}"
)
if condition.message:
click.echo(f" Message: {condition.message}")
click.echo()
else:
click.echo("No status information available")
except Exception as e:
raise click.UsageError(f"Failed to describe job: {str(e)}")
@click.command("hyp-pytorch-job")
@click.option(
"--job-name", required=True, help="Required. The name of the job to delete"
)
@click.option(
"--namespace",
"-n",
default="default",
help="Optional. The namespace of the job. Defaults to 'default' namespace.",
)
@_hyperpod_telemetry_emitter(Feature.HYPERPOD_CLI, "delete_pytorchjob_cli")
def pytorch_delete(job_name: str, namespace: str):
"""Delete a HyperPod PyTorch job."""
try:
job = HyperPodPytorchJob.get(name=job_name, namespace=namespace)
job.delete()
if job is None:
raise click.UsageError(f"Job {job_name} not found in namespace {namespace}")
except Exception as e:
raise click.UsageError(f"Failed to describe job: {str(e)}")
@click.command("hyp-pytorch-job")
@click.option(
"--job-name",
required=True,
help="Required. Specify the job name to list its associated pods.",
)
@click.option(
"--namespace",
"-n",
default="default",
help="Optional. The namespace of the job. Defaults to 'default' namespace.",
)
@_hyperpod_telemetry_emitter(Feature.HYPERPOD_CLI, "list_pods_pytorchjob_cli")
def pytorch_list_pods(job_name: str, namespace: str):
"""List all HyperPod PyTorch pods related to the job."""
try:
job = HyperPodPytorchJob.get(name=job_name, namespace=namespace)
pods = job.list_pods()
if not pods:
click.echo(f"\nNo pods found for job: {job_name}")
return
# Define headers and widths
headers = ["POD NAME", "NAMESPACE"]
widths = [50, 20]
# Print header
click.echo(f"\nPods for job: {job_name}")
header = "".join(f"{h:<{w}}" for h, w in zip(headers, widths))
click.echo("\n" + header)
click.echo("-" * sum(widths))
# Print each pod
for pod in pods:
row = "".join([f"{pod:<{widths[0]}}", f"{namespace:<{widths[1]}}"])
click.echo(row)
click.echo()
except Exception as e:
raise click.UsageError(f"Failed to list jobs: {str(e)}")
@click.command("hyp-pytorch-job")
@click.option(
"--job-name",
required=True,
help="Required. Specify the job name for pod log retrieval.",
)
@click.option(
"--pod-name", required=True, help="Required. The name of the pod to get logs from."
)
@click.option(
"--namespace",
"-n",
default="default",
help="Optional. The namespace of the job. Defaults to 'default' namespace.",
)
@_hyperpod_telemetry_emitter(Feature.HYPERPOD_CLI, "get_pytorchjob_logs_from_pod_cli")
def pytorch_get_logs(job_name: str, pod_name: str, namespace: str):
"""Get specific pod log for Hyperpod Pytorch job."""
try:
click.echo("Listing logs for pod: " + pod_name)
job = HyperPodPytorchJob.get(name=job_name, namespace=namespace)
logs = job.get_logs_from_pod(pod_name=pod_name)
if not logs:
click.echo("No logs available.")
return
# Split logs into lines and display them
log_lines = logs.split("\n")
for line in log_lines:
if line.strip(): # Skip empty lines
# Color coding based on log level
if "ERROR" in line.upper():
click.secho(line, fg="red")
elif "WARNING" in line.upper():
click.secho(line, fg="yellow")
elif "INFO" in line.upper():
click.secho(line, fg="green")
else:
click.echo(line)
click.echo("\nEnd of logs")
click.echo("=" * 80)
except Exception as e:
raise click.UsageError(f"Failed to list jobs: {str(e)}")