Repository navigation
Expand file tree
/
Copy pathtrain.py
More file actions
349 lines (324 loc) · 13.9 KB
/
Copy pathtrain.py
File metadata and controls
349 lines (324 loc) · 13.9 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
"""Optional, explicitly invoked offline LoRA training. Never imported by the runtime."""
import argparse
import hashlib
import inspect
import json
import time
from pathlib import Path
from backend.paths import output_path
def validate_optimizer_updates(steps, changed_parameters):
if steps["applied"] < 1:
raise RuntimeError(
"Training made no optimizer updates: "
f"{steps['applied']} applied, {steps['skipped']} skipped; "
"no completed adapter was saved"
)
if changed_parameters < 1:
raise RuntimeError(
"Training left all adapter weights unchanged; no completed adapter was saved"
)
def verified_submission_index(messages):
if not messages or messages[-1].get("role") != "tool":
raise ValueError("Training trajectory must end with a verified task result")
result = messages[-1]
outcome = json.loads(result.get("content", "{}"))
if (
result.get("name") != "task"
or not isinstance(outcome, dict)
or outcome.get("status") != "Verified"
):
raise ValueError("Training trajectory must end with a verified task result")
index = next(
(i for i in range(len(messages) - 2, -1, -1) if messages[i].get("role") == "assistant"),
None,
)
if index is None:
raise ValueError("Verified result has no assistant submission")
calls = messages[index].get("tool_calls", [])
if (
len(calls) != 1
or not result.get("tool_call_id")
or calls[0].get("id") != result["tool_call_id"]
):
raise ValueError("Training target must contain exactly one verified submission")
call = calls[0].get("function", {})
arguments = call.get("arguments", {})
if isinstance(arguments, str):
arguments = json.loads(arguments)
if (
call.get("name") != "task"
or not isinstance(arguments, dict)
or set(arguments) != {"operation", "answer"}
or arguments["operation"] != "submit"
or not isinstance(arguments["answer"], str)
):
raise ValueError("Training target must be a task submit call with an answer")
return index
def verified_sample(tokenizer, messages, max_length):
from backend.tools import TOOLS
index = verified_submission_index(messages)
prefix = tokenizer.apply_chat_template(
messages[:index],
tokenize=False,
add_generation_prompt=True,
enable_thinking=False,
tools=TOOLS,
)
text = tokenizer.apply_chat_template(
messages[: index + 1],
tokenize=False,
enable_thinking=False,
tools=TOOLS,
)
without_calls = messages[:index] + [
{key: value for key, value in messages[index].items() if key != "tool_calls"}
]
if text == tokenizer.apply_chat_template(
without_calls, tokenize=False, enable_thinking=False, tools=TOOLS
):
raise ValueError("Chat template omitted the verified tool call")
encoded = tokenizer(text, add_special_tokens=False, truncation=False)
prefix_ids = tokenizer(prefix, add_special_tokens=False, truncation=False)["input_ids"]
if encoded["input_ids"][: len(prefix_ids)] != prefix_ids:
raise ValueError("Chat template cannot safely separate context from the assistant target")
if len(encoded["input_ids"]) > max_length:
raise ValueError("Trajectory exceeds max-length; no silent training truncation")
if len(prefix_ids) >= len(encoded["input_ids"]):
raise ValueError("Chat template omitted the verified assistant target")
encoded["labels"] = [-100] * len(prefix_ids) + encoded["input_ids"][len(prefix_ids) :]
return encoded
def main():
parser = argparse.ArgumentParser(description="Offline LoRA from exported public trajectories")
parser.add_argument("--dataset", required=True, type=Path)
parser.add_argument("--model", required=True, help="Hugging Face model ID or local directory")
parser.add_argument("--output", required=True, type=Path)
parser.add_argument("--epochs", type=float, default=1)
parser.add_argument("--iteration", type=int, default=1)
parser.add_argument("--max-length", type=int, default=1024)
parser.add_argument(
"--load-in-4bit", action="store_true", help="Train LoRA on a CUDA NF4 quantized base"
)
parser.add_argument(
"--bf16", action="store_true", help="Use BF16 compute and training with four-bit CUDA"
)
parser.add_argument(
"--fp16-frozen-embeddings",
action="store_true",
help="Keep frozen input embeddings in FP16 during four-bit CUDA training",
)
parser.add_argument(
"--resume-adapter", type=Path, help="Continue a prior adapter in a new output directory"
)
args = parser.parse_args()
args.output = output_path(args.output) if args.output else None
if args.output.exists() and any(args.output.iterdir()):
raise SystemExit("Output directory must be empty; existing adapters are preserved")
args.output.parent.mkdir(parents=True, exist_ok=True)
event_path = args.output.parent / f"{args.output.name}.training-events.jsonl"
def record(kind, payload):
with event_path.open("a", encoding="utf-8") as stream:
stream.write(
json.dumps(
{
"schema_version": 1,
"wall_ms": int(time.time() * 1000),
"kind": kind,
"payload": payload,
}
)
+ "\n"
)
record(
"training.started",
{
"model": args.model,
"iteration": args.iteration,
"dataset": str(args.dataset),
"output": str(args.output),
},
)
try:
manifest = train(args)
except BaseException as error:
record("training.failed", {"error": str(error)})
raise
record("training.completed", manifest)
def train(args):
parent = None
resume = getattr(args, "resume_adapter", None)
bf16 = getattr(args, "bf16", False)
quantization = (
f"nf4-double-{'bf16' if bf16 else 'fp16'}"
if getattr(args, "load_in_4bit", False)
else "none"
)
if bf16 and quantization == "none":
raise ValueError("BF16 requires four-bit CUDA training")
fp16_embeddings = getattr(args, "fp16_frozen_embeddings", False)
if fp16_embeddings and quantization == "none":
raise ValueError("FP16 frozen embeddings require four-bit CUDA training")
if resume:
parent = json.loads((resume / "manifest.json").read_text(encoding="utf-8"))
if parent["model_version"] != args.model:
raise ValueError("Adapter base model must match this training model")
if args.iteration <= parent["training_iteration"]:
raise ValueError("Training iteration must advance beyond the parent adapter")
if parent.get("base_quantization", "none") != quantization:
raise ValueError("Adapter base quantization must match this training run")
parent_precision = parent.get("fp16_frozen_embeddings", False)
if not isinstance(parent_precision, bool) or parent_precision != fp16_embeddings:
raise ValueError("Frozen embedding precision must match this training run")
import torch
from datasets import Dataset
from peft import LoraConfig, PeftModel, get_peft_model, prepare_model_for_kbit_training
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
DataCollatorForSeq2Seq,
Trainer,
TrainerCallback,
TrainingArguments,
)
model_options = {}
compute_dtype = torch.bfloat16 if bf16 else torch.float16
if quantization != "none":
if not torch.cuda.is_available():
raise ValueError("Four-bit training requires CUDA and the training-4bit dependencies")
if bf16 and not torch.cuda.is_bf16_supported():
raise ValueError("BF16 training requires a supported CUDA GPU")
model_options = {
"device_map": {"": 0},
"quantization_config": BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True,
bnb_4bit_compute_dtype=compute_dtype,
),
}
rows = [
json.loads(line) for line in args.dataset.read_text(encoding="utf-8").splitlines() if line
]
if not rows or any(
set(row) - {"observed_task_prompts"}
!= {"agent_id", "model_version", "context_version", "messages"}
for row in rows
):
raise SystemExit("Use a public training-data export, never a private research export")
tokenizer = AutoTokenizer.from_pretrained(args.model, trust_remote_code=False)
tokenizer.pad_token = tokenizer.pad_token or tokenizer.eos_token
samples = []
for row in rows:
samples.append(verified_sample(tokenizer, row["messages"], args.max_length))
model = AutoModelForCausalLM.from_pretrained(
args.model,
trust_remote_code=False,
torch_dtype=compute_dtype if torch.cuda.is_available() else torch.float32,
**model_options,
)
if quantization != "none":
model = prepare_model_for_kbit_training(model)
if fp16_embeddings:
model.get_input_embeddings().to(dtype=torch.float16)
if resume:
model = PeftModel.from_pretrained(model, resume, is_trainable=True)
else:
model = get_peft_model(
model,
LoraConfig(r=8, lora_alpha=16, target_modules="all-linear", task_type="CAUSAL_LM"),
)
model.config.use_cache = False
model.enable_input_require_grads()
optimizer_steps = {"applied": 0, "skipped": 0}
initial_weights = {
name: parameter.detach().cpu().clone()
for name, parameter in model.named_parameters()
if parameter.requires_grad
}
class UpdateCounter(TrainerCallback):
def on_pre_optimizer_step(self, args, state, control, **kwargs):
gradients = [
parameter.grad
for parameter in kwargs["model"].parameters()
if parameter.requires_grad and parameter.grad is not None
]
if not gradients or any(not torch.isfinite(gradient).all() for gradient in gradients):
raise RuntimeError(
"Missing or non-finite adapter gradients; optimizer update aborted"
)
def on_optimizer_step(self, args, state, control, **kwargs):
skipped = kwargs["optimizer"].step_was_skipped
optimizer_steps["skipped" if skipped else "applied"] += 1
restrict_logits = (
"logits_to_keep" in inspect.signature(model.get_base_model().forward).parameters
)
class SubmissionTrainer(Trainer):
def compute_loss(self, model, inputs, return_outputs=False, num_items_in_batch=None):
labels = inputs.pop("labels")
positions = torch.nonzero((labels[:, 1:] != -100).any(dim=0), as_tuple=True)[0]
outputs = model(**inputs, logits_to_keep=positions)
targets = labels[:, positions + 1]
loss = torch.nn.functional.cross_entropy(
outputs.logits.float().reshape(-1, outputs.logits.shape[-1]),
targets.reshape(-1),
ignore_index=-100,
)
return (loss, outputs) if return_outputs else loss
trainer = (SubmissionTrainer if restrict_logits else Trainer)(
model=model,
args=TrainingArguments(
output_dir=str(args.output),
num_train_epochs=args.epochs,
per_device_train_batch_size=1,
gradient_accumulation_steps=8,
gradient_checkpointing=True,
report_to="none",
fp16=torch.cuda.is_available() and not bf16,
bf16=bf16,
seed=42,
),
train_dataset=Dataset.from_list(samples),
data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer, label_pad_token_id=-100),
callbacks=[UpdateCounter()],
)
if restrict_logits:
trainer.model_accepts_loss_kwargs = False
started = time.perf_counter()
if torch.cuda.is_available():
torch.cuda.reset_peak_memory_stats()
metrics = trainer.train().metrics
changed_parameters = sum(
not torch.equal(initial_weights[name], parameter.detach().cpu())
for name, parameter in model.named_parameters()
if name in initial_weights
)
validate_optimizer_updates(optimizer_steps, changed_parameters)
model.save_pretrained(args.output)
tokenizer.save_pretrained(args.output)
manifest = {
"model_version": args.model,
"adapter_version": f"lora-{args.iteration}",
"dataset_version": hashlib.sha256(args.dataset.read_bytes()).hexdigest(),
"training_iteration": args.iteration,
"agent_identities": sorted({r["agent_id"] for r in rows}),
"context_versions": sorted({r["context_version"] for r in rows}),
"continuous_training": False,
"training_objective": "verified-assistant-submission-only",
"base_quantization": quantization,
"fp16_frozen_embeddings": fp16_embeddings,
"restricted_vocab_projection": restrict_logits,
"supervised_tokens": sum(
sum(label != -100 for label in sample["labels"]) for sample in samples
),
"parent_adapter": parent,
"training_seconds": round(time.perf_counter() - started, 3),
"training_metrics": metrics,
"optimizer_steps": optimizer_steps,
"changed_adapter_parameters": changed_parameters,
"device": torch.cuda.get_device_name(0) if torch.cuda.is_available() else "cpu",
"peak_gpu_bytes": torch.cuda.max_memory_allocated() if torch.cuda.is_available() else None,
}
(args.output / "manifest.json").write_text(json.dumps(manifest, indent=2), encoding="utf-8")
return manifest
if __name__ == "__main__":
main()