-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcomponents.py
444 lines (414 loc) · 16.9 KB
/
components.py
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# type: ignore
# pylint: disable=import-outside-toplevel,missing-function-docstring
from typing import Optional
from kfp import dsl
from utils.consts import PYTHON_IMAGE, RHELAI_IMAGE, TOOLBOX_IMAGE
@dsl.component(base_image=RHELAI_IMAGE, install_kfp_package=False)
def data_processing_op(
model_path: str = "/model",
sdg_path: str = "/data/sdg",
skills_path: str = "/data/skills",
knowledge_path: str = "/data/knowledge",
max_seq_len: Optional[int] = 4096,
max_batch_len: Optional[int] = 20000,
):
import os
import instructlab.training.data_process as dp
from instructlab.training import (
DataProcessArgs,
TrainingArgs,
)
# define training-specific arguments
skill_training_args = TrainingArgs(
# define data-specific arguments
model_path=model_path,
data_path=f"{sdg_path}/skills_train_msgs*.jsonl",
data_output_dir=skills_path,
# define model-trianing parameters
max_seq_len=max_seq_len,
max_batch_len=max_batch_len,
# XXX(shanand): We don't need the following arguments
# for data processing. Added them for now to avoid
# Pydantic validation errors for TrainingArgs
ckpt_output_dir="data/saved_checkpoints",
num_epochs=2,
effective_batch_size=3840,
save_samples=0,
learning_rate=2e-6,
warmup_steps=800,
is_padding_free=True,
)
knowledge_training_args = TrainingArgs(
# define data-specific arguments
model_path=model_path,
data_path=f"{sdg_path}/knowledge_train_msgs*.jsonl",
data_output_dir=knowledge_path,
# define model-trianing parameters
max_seq_len=max_seq_len,
max_batch_len=max_batch_len,
# XXX(shanand): We don't need the following arguments
# for data processing. Added them for now to avoid
# Pydantic validation errors for TrainingArgs
ckpt_output_dir="data/saved_checkpoints",
num_epochs=2,
effective_batch_size=3840,
save_samples=0,
learning_rate=2e-6,
warmup_steps=800,
is_padding_free=True,
)
def data_processing(train_args: TrainingArgs) -> None:
# early validation logic here
if train_args.max_batch_len < train_args.max_seq_len:
raise ValueError(
f"the 'max_batch_len' cannot be less than 'max_seq_len': {train_args.max_batch_len=} < {train_args.max_seq_len=}"
)
# process the training data
if not os.path.exists(train_args.data_output_dir):
os.makedirs(train_args.data_output_dir, exist_ok=True)
dp.main(
DataProcessArgs(
# XXX(osilkin): make a decision here, either:
# 1. the CLI is fully responsible for managing where the data is written
# 2. we never cache it and simply write it to a tmp file every time.
#
# An important reason for why #1 would be preferable is in the case of OpenShift/SELinux
# where the user has a defined place for new temporary data to be written.
data_output_path=train_args.data_output_dir,
model_path=train_args.model_path,
data_path=train_args.data_path,
max_seq_len=train_args.max_seq_len,
chat_tmpl_path=train_args.chat_tmpl_path,
)
)
data_processing(train_args=skill_training_args)
data_processing(train_args=knowledge_training_args)
@dsl.container_component
def skills_processed_data_to_artifact_op(
skills_processed_data: dsl.Output[dsl.Dataset],
pvc_path: str = "/data/skills",
):
return dsl.ContainerSpec(
TOOLBOX_IMAGE,
["/bin/sh", "-c"],
[f"cp -r {pvc_path} {skills_processed_data.path}"],
)
@dsl.container_component
def knowledge_processed_data_to_artifact_op(
knowledge_processed_data: dsl.Output[dsl.Dataset],
pvc_path: str = "/data/knowledge",
):
return dsl.ContainerSpec(
TOOLBOX_IMAGE,
["/bin/sh", "-c"],
[f"cp -r {pvc_path} {knowledge_processed_data.path}"],
)
@dsl.component(base_image=PYTHON_IMAGE, install_kfp_package=False)
def pytorchjob_manifest_op(
model_pvc_name: str,
input_pvc_name: str,
output_pvc_name: str,
name_suffix: str,
image: str,
# path_to_model: str,
phase_num: int,
nproc_per_node: int = 3,
nnodes: int = 2,
num_epochs: int = 2,
effective_batch_size: int = 3840,
learning_rate: float = 1e-4,
num_warmup_steps: int = 800,
save_samples: int = 0,
max_batch_len: int = 20000,
seed: int = 42,
):
import inspect
import os
import time
import kubernetes
import urllib3
import yaml
def list_phase1_final_model():
model_dir = "/output/phase_1/model/hf_format"
models = os.listdir(model_dir)
newest_idx = max(
(os.path.getmtime(f"{model_dir}/{model}"), i)
for i, model in enumerate(models)
)[-1]
newest_model = models[newest_idx]
return f"{model_dir}/{newest_model}"
name = f"train-phase-{phase_num}-{name_suffix.rstrip('-sdg')}"
if phase_num == 1:
path_to_model = "/input_model"
path_to_data = "/input_data/knowledge/data.jsonl"
elif phase_num == 2:
path_to_model = list_phase1_final_model()
path_to_data = "/input_data/skills/data.jsonl"
else:
raise RuntimeError(f"Unsupported value of {phase_num=}")
manifest = inspect.cleandoc(
f"""
apiVersion: kubeflow.org/v1
kind: PyTorchJob
metadata:
name: {name}
spec:
nprocPerNode: \"{nproc_per_node}\"
pytorchReplicaSpecs:
Master:
replicas: 1
restartPolicy: OnFailure
template:
metadata:
annotations:
sidecar.istio.io/inject: 'false'
spec:
containers:
- args:
- |
echo "Running phase {phase_num}"
echo "Using {path_to_model} model for training"
echo "Using {path_to_data} data for training"
mkdir -p /output/phase_{phase_num}/model;
mkdir -p /output/data;
torchrun --nnodes {nnodes} \
--nproc_per_node {nproc_per_node} \
--node_rank \$(RANK) \
--rdzv_endpoint \$(MASTER_ADDR):\$(MASTER_PORT) \
-m instructlab.training.main_ds \
--model_name_or_path={path_to_model} \
--data_path={path_to_data} \
--output_dir=/output/phase_{phase_num}/model \
--num_epochs={num_epochs} \
--effective_batch_size={effective_batch_size} \
--learning_rate={learning_rate} \
--num_warmup_steps={num_warmup_steps} \
--save_samples={save_samples} \
--log_level=INFO \
--max_batch_len={max_batch_len} \
--seed={seed} \
--cpu_offload_optimizer \
--cpu_offload_params_fsdp \
--distributed_training_framework fsdp \
--checkpoint_at_epoch
command:
- /bin/bash
- '-c'
- '--'
image: {image}
name: pytorch
volumeMounts:
- mountPath: /input_data
name: input-data
readOnly: true
- mountPath: /input_model
name: model
readOnly: true
- mountPath: /output
name: output
env:
- name: NNODES
value: \"{nnodes}\"
- name: NPROC_PER_NODE
value: \"{nproc_per_node}\"
- name: XDG_CACHE_HOME
value: /tmp
- name: TRITON_CACHE_DIR
value: /tmp
- name: HF_HOME
value: /tmp
- name: TRANSFORMERS_CACHE
value: /tmp
resources:
requests:
"nvidia.com/gpu": {nproc_per_node}
limits:
"nvidia.com/gpu": {nproc_per_node}
volumes:
- name: input-data
persistentVolumeClaim:
claimName: {input_pvc_name}
- name: model
persistentVolumeClaim:
claimName: {model_pvc_name}
- name: output
persistentVolumeClaim:
claimName: {output_pvc_name}
Worker:
replicas: {nnodes-1}
restartPolicy: OnFailure
template:
metadata:
annotations:
sidecar.istio.io/inject: 'false'
spec:
containers:
- args:
- |
echo "Running phase {phase_num}"
echo "Using {path_to_model} model for training"
echo "Using {path_to_data} data for training"
mkdir -p /tmp/model;
torchrun --nnodes {nnodes} \
--nproc_per_node {nproc_per_node} \
--node_rank \$(RANK) \
--rdzv_endpoint \$(MASTER_ADDR):\$(MASTER_PORT) \
-m instructlab.training.main_ds \
--model_name_or_path={path_to_model} \
--data_path={path_to_data} \
--output_dir=/tmp/model \
--num_epochs={num_epochs} \
--effective_batch_size={effective_batch_size} \
--learning_rate={learning_rate} \
--num_warmup_steps={num_warmup_steps} \
--save_samples={save_samples} \
--log_level=INFO \
--max_batch_len={max_batch_len} \
--seed={seed} \
--cpu_offload_optimizer \
--cpu_offload_params_fsdp \
--distributed_training_framework fsdp \
--checkpoint_at_epoch
command:
- /bin/bash
- '-c'
- '--'
image: {image}
name: pytorch
volumeMounts:
- mountPath: /input_data
name: input-data
readOnly: true
- mountPath: /input_model
name: model
readOnly: true
- mountPath: /output
name: output
readOnly: true
env:
- name: NNODES
value: \"{nnodes}\"
- name: NPROC_PER_NODE
value: \"{nproc_per_node}\"
- name: XDG_CACHE_HOME
value: /tmp
- name: TRITON_CACHE_DIR
value: /tmp
- name: HF_HOME
value: /tmp
- name: TRANSFORMERS_CACHE
value: /tmp
resources:
requests:
"nvidia.com/gpu": {nproc_per_node}
limits:
"nvidia.com/gpu": {nproc_per_node}
volumes:
- name: input-data
persistentVolumeClaim:
claimName: {input_pvc_name}
- name: model
persistentVolumeClaim:
claimName: {model_pvc_name}
- name: output
persistentVolumeClaim:
claimName: {output_pvc_name}
"""
)
try:
manifest_yaml = yaml.safe_load(manifest)
except yaml.YAMLError as exc:
raise RuntimeError(f"Error parsing manifest: {exc}") from exc
# Discover the namespace in which the pod is running
with open(
"/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r", encoding="utf-8"
) as f:
namespace = f.read().strip()
print(f"The pod is running in the namespace: {namespace}")
try:
kubernetes.config.load_kube_config()
print("Loaded kube config")
except kubernetes.config.ConfigException:
print("Failed to load kube config. Trying in-cluster config")
kubernetes.config.load_incluster_config()
api = kubernetes.client.CustomObjectsApi()
try:
api.create_namespaced_custom_object(
group="kubeflow.org",
version="v1",
namespace=namespace,
plural="pytorchjobs",
body=manifest_yaml,
)
except kubernetes.client.rest.ApiException as exc:
if exc.status == 409:
print(
"{} '{}/{}' already exists.".format(
manifest_yaml["kind"],
namespace,
manifest_yaml["metadata"]["name"],
)
)
else:
raise
# Get the CR status and wait for it to be completed
w = kubernetes.watch.Watch()
exit_flag = False
start_time = time.time()
timeout_seconds = 24 * 60 * 60 # 24 hours
while not exit_flag: # Keep the watch active
if time.time() - start_time > timeout_seconds:
raise RuntimeError(
"Timeout (24h) reached waiting for the PytorchJob to complete."
)
try:
print("Watching for PytorchJob")
for event in w.stream(
api.list_namespaced_custom_object,
group="kubeflow.org",
version="v1",
namespace=namespace,
plural="pytorchjobs",
timeout_seconds=60, # Timeout after 1 minute
):
pytorchjob_event = event["object"]
if (
pytorchjob_event["metadata"]["name"]
!= manifest_yaml["metadata"]["name"]
):
continue
pytorchjob_name = pytorchjob_event["metadata"]["name"]
if (
"status" not in pytorchjob_event
or "conditions" not in pytorchjob_event["status"]
):
continue
print(
f"PytorchJob: {pytorchjob_name} - {pytorchjob_event['status'].get('conditions', 'No conditions yet')}"
)
for job_condition in reversed(pytorchjob_event["status"]["conditions"]):
if job_condition["type"] == "Succeeded":
print(
f"PytorchJob '{pytorchjob_name}' completed successfully: {job_condition['reason']}"
)
print(f"Training phase {phase_num} completed.")
w.stop()
exit_flag = True
# Break here to avoid going into other conditions, we are done
break
elif job_condition["type"] == "Failed":
print(
f"PytorchJob '{pytorchjob_name}' failed: {job_condition['reason']}"
)
w.stop()
raise RuntimeError("Job failed.")
except kubernetes.client.exceptions.ApiException as e:
print(f"API exception occurred: {str(e)}")
time.sleep(5) # Backoff before retrying
# Catches the following error:
# urllib3.exceptions.ProtocolError: ("Connection broken: InvalidChunkLength
except urllib3.exceptions.ProtocolError as e:
print(f"Connection broken reconnecting the watcher {str(e)}")
time.sleep(5) # Backoff before retrying
finally:
w.stop()