-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtraining_loop.py
More file actions
189 lines (166 loc) · 5.83 KB
/
training_loop.py
File metadata and controls
189 lines (166 loc) · 5.83 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
"""
Generic training loop to understand the minimal requirements for a training loop.
Design goals:
- no torch, numpy, etc
- generate a clean list of functions to implement
"""
import time
from typing import Sequence
import torch
from training_basics import (
TrainingConfig,
EvalConfig,
DataProvider,
TrainingState,
MetricItem,
D,
)
import neptune_lib
def mem_gb():
s = torch.cuda.memory_stats()
alloc = s["allocated_bytes.all.current"] / 1e9
peak = s["allocated_bytes.all.peak"] / 1e9
resv = s["reserved_bytes.all.current"] / 1e9
frag = resv - alloc
print(f"beginning of step: {alloc=:.2f}, {peak=:.2f}, {resv=:.2f}, {frag=:.2f}")
torch.cuda.reset_peak_memory_stats()
return alloc, peak, resv, frag
def record_metrics(
metrics: dict[str, MetricItem],
metric_path: str,
step: int,
neptune_run: neptune_lib.NeptuneRunWrapper,
):
"""Record metrics with custom X-axis values.
Handles both training and evaluation metrics. Iterates through MetricItem instances
and logs them to Neptune. For each metric, uses the custom x_axis if provided,
otherwise uses the passed step.
Args:
metrics: Dict mapping metric names to MetricItem instances.
Each MetricItem contains a value and optional custom x_axis.
metric_path: Path for the metric (e.g., "eval/validation", "eval/lm_eval_harness").
step: Step to use for metrics where MetricItem.x_axis is None.
neptune_run: Neptune run for logging.
"""
print(f"{metric_path}:")
for key, item in sorted(metrics.items()):
print(f" {key}: {item.value:.6f}")
for key, item in metrics.items():
x = step if item.x_axis is None else item.x_axis
neptune_run[f"{metric_path}/{key}"].append(item.value, step=x)
def validation(
*,
config: EvalConfig,
state: TrainingState[D],
eval_data_providers: Sequence[DataProvider[D]],
epoch: int,
step: int,
neptune_run: neptune_lib.NeptuneRunWrapper,
):
"""Evaluate the model"""
print(f"Eval metrics ({epoch=}, {step=}):")
for eval_data_provider in eval_data_providers:
start_time = time.time()
print(f" {eval_data_provider.get_name()}:")
final_metrics = state.validation_loss(
eval_data_provider.generate(), config.steps
)
name = eval_data_provider.get_name()
record_metrics(
metrics=final_metrics,
metric_path=f"eval/{name}",
step=step,
neptune_run=neptune_run,
)
print(f"Eval {name} completed in {time.time() - start_time:.2f}s")
def lm_eval(
step: int,
state: TrainingState[D],
neptune_run: neptune_lib.NeptuneRunWrapper,
):
"""Evaluate the model"""
metrics = state.evaluate()
record_metrics(
metrics=metrics,
metric_path="eval/lm_eval_harness",
step=step,
neptune_run=neptune_run,
)
def train(
state: TrainingState[D],
data_provider: DataProvider[D],
config: TrainingConfig,
eval_config: EvalConfig,
neptune_run: neptune_lib.NeptuneRunWrapper,
eval_data_providers: Sequence[DataProvider[D]] = (),
):
"""Training loop using configuration object"""
print(f"Starting training for {config.num_epochs} epochs...")
for epoch in range(config.num_epochs):
print(f"Epoch {epoch + 1}")
idx = 0
for idx, data in enumerate(data_provider.generate()):
if idx % eval_config.every_n_steps == 0:
validation(
config=eval_config,
state=state,
eval_data_providers=eval_data_providers,
epoch=epoch,
step=idx,
neptune_run=neptune_run,
)
if (
eval_config.full_eval_every_n_steps is not None
and idx % eval_config.full_eval_every_n_steps == 0
):
lm_eval(idx, state, neptune_run=neptune_run)
if idx == 5:
torch.cuda.synchronize()
torch.cuda.reset_peak_memory_stats()
torch.cuda.empty_cache()
torch.cuda.synchronize()
print(torch.cuda.memory_summary())
# snap = (
# torch.cuda.memory_snapshot()
# ) # JSON-like: blocks, sizes, “active” flags
# print(snap)
metrics = state.step(data)
if idx % config.train_metrics_every_n_steps == 0 or idx == 10:
mem_gb()
record_metrics(
metrics=metrics,
metric_path="train",
step=idx,
neptune_run=neptune_run,
)
if (
config.save_checkpoint_every_n_steps is not None
and idx % config.save_checkpoint_every_n_steps == 0
and config.checkpoint_path is not None
):
state.save_checkpoint(
config.checkpoint_path, neptune_run.get_run_id(), idx, epoch
)
if (
config.training_steps_per_epoch
and idx >= config.training_steps_per_epoch
):
break
print(f"Epoch {epoch + 1} completed.")
validation(
config=eval_config,
state=state,
eval_data_providers=eval_data_providers,
epoch=epoch,
step=idx,
neptune_run=neptune_run,
)
if config.checkpoint_path is not None:
state.save_checkpoint(
config.checkpoint_path, neptune_run.get_run_id(), idx, epoch
)
if eval_config.full_eval_every_n_steps is not None:
lm_eval(idx, state, neptune_run=neptune_run)
print("-" * 50)
print("Training completed!")
return