-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.py
More file actions
1270 lines (1164 loc) · 46.1 KB
/
Copy pathcompile.py
File metadata and controls
1270 lines (1164 loc) · 46.1 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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "gguf>=0.10,<1",
# "httpx>=0.27,<1",
# "peft>=0.15,<1",
# "safetensors>=0.4,<1",
# "torch>=2.4,<2.15",
# "transformers>=4.57,<6",
# ]
# ///
"""Compile a natural-language specification into a finetuned PAW program."""
from __future__ import annotations
import argparse
import asyncio
import hashlib
import json
import math
import os
import random
import shutil
import sys
import tempfile
import time
import zipfile
from collections.abc import Iterable
from dataclasses import asdict, dataclass, field
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
TEACHER_SYSTEM = (
"You synthesize tiny supervised datasets for finetuning small language "
"models from a natural-language task specification. Return strict JSON "
"with a top-level key 'examples'. Each example must be an object with "
"string keys 'input' and 'output'."
)
TEACHER_USER = (
"Generate {n} diverse (input, output) example pairs that follow this "
"specification.\n\n[SPEC]\n{spec}\n[END_SPEC]"
)
TEACHER_TEMPLATE_VERSION = "T1S4-v1-2026-05-10"
DEFAULT_API_URL = "https://programasweights.com"
DEFAULT_TEACHER_API_URL = "https://api.openai.com/v1"
DEFAULT_HF_ASSET_URL = "https://huggingface.co/programasweights/paw-programs/resolve/main"
DEFAULT_INTERPRETER = "Qwen/Qwen3-0.6B"
DEFAULT_BASE_COMPILER = "paw-4b-qwen3-0.6b"
DEFAULT_RECIPE_MEMORY_GIB = 38.0
DEFAULT_TARGET_MODULES = (
"q_proj",
"k_proj",
"v_proj",
"o_proj",
"gate_proj",
"up_proj",
"down_proj",
)
MODULE_TO_GGUF = {
"q_proj": "attn_q",
"k_proj": "attn_k",
"v_proj": "attn_v",
"o_proj": "attn_output",
"gate_proj": "ffn_gate",
"up_proj": "ffn_up",
"down_proj": "ffn_down",
}
MODULE_TO_PEFT = {
"q_proj": "model.layers.{}.self_attn.q_proj",
"k_proj": "model.layers.{}.self_attn.k_proj",
"v_proj": "model.layers.{}.self_attn.v_proj",
"o_proj": "model.layers.{}.self_attn.o_proj",
"gate_proj": "model.layers.{}.mlp.gate_proj",
"up_proj": "model.layers.{}.mlp.up_proj",
"down_proj": "model.layers.{}.mlp.down_proj",
}
RUNTIME_MANIFEST = {
"runtime_id": "qwen3-0.6b-q6_k",
"manifest_version": 1,
"display_name": "Qwen3 0.6B (Q6_K)",
"interpreter": DEFAULT_INTERPRETER,
"inference_provider_url": "http://localhost:9000",
"adapter_format": "gguf_lora",
"prompt_template": {
"format": "rendered_text",
"placeholder": "{INPUT_PLACEHOLDER}",
},
"program_assets": {
"adapter_filename": "adapter.gguf",
"prefix_cache_required": False,
"prefix_cache_filename": None,
"prefix_tokens_filename": None,
},
"local_sdk": {
"supported": True,
"base_model": {
"provider": "huggingface",
"repo": "programasweights/Qwen3-0.6B-GGUF-Q6_K",
"file": "qwen3-0.6b-q6_k.gguf",
"url": "https://huggingface.co/programasweights/Qwen3-0.6B-GGUF-Q6_K/resolve/main/qwen3-0.6b-q6_k.gguf",
"size_bytes": 622733120,
"sha256": "9a16ed5cacba959e63b62e2b6840c3eca2b51c3c3e51d31367ef8e4aafeae33c",
},
"n_ctx": 2048,
},
"js_sdk": {
"supported": False,
"base_model": None,
"prefix_cache_supported": False,
},
"capabilities": {"python_local": True, "js_browser": False},
"base_inference": {
"contract_version": 1,
"format": "rendered_text",
"placeholder": "{INPUT_PLACEHOLDER}",
"template": (
"<|im_start|>user\n{INPUT_PLACEHOLDER}<|im_end|>\n"
"<|im_start|>assistant\n<think>\n\n</think>\n\n"
),
},
}
@dataclass(frozen=True)
class Teacher:
model: str
examples: int
temperature: float = 1.0
examples_per_call: int = 8
seed: int = 0
@dataclass
class Recipe:
teachers: list[Teacher] = field(
default_factory=lambda: [
Teacher("gpt-5.4-mini", 2400),
Teacher("gpt-5.5", 1200),
]
)
target_examples: int = 4800
steps: int = 100
batch_size: int = 48
micro_batch_size: int = 48
learning_rate: float = 2e-4
min_learning_rate: float = 2e-5
warmup: float = 0.0
lora_rank: int = 64
lora_alpha: float = 16.0
lora_init: str = "mapper"
target_modules: tuple[str, ...] = DEFAULT_TARGET_MODULES
max_length: int = 2048
loss_chunk_size: int = 128
synth_concurrency: int = 64
retries: int = 3
seed: int = 0
base_compiler: str = DEFAULT_BASE_COMPILER
interpreter: str = DEFAULT_INTERPRETER
gradient_checkpointing: bool = False
def log(message: str) -> None:
print(message, file=sys.stderr, flush=True)
def parse_teacher(value: str, defaults: Recipe) -> Teacher:
try:
model, raw_count = value.rsplit("=", 1)
count = int(raw_count)
except ValueError as exc:
raise argparse.ArgumentTypeError("teacher must be MODEL=EXAMPLES") from exc
if not model or count <= 0:
raise argparse.ArgumentTypeError("teacher must be MODEL=EXAMPLES with EXAMPLES > 0")
return Teacher(
model=model,
examples=count,
temperature=1.0,
examples_per_call=defaults.teachers[0].examples_per_call,
seed=defaults.seed,
)
def build_parser() -> argparse.ArgumentParser:
defaults = Recipe()
parser = argparse.ArgumentParser(
description="Turn a natural-language specification into a reusable .paw program.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("spec", nargs="?", help="Natural-language function specification")
parser.add_argument("--spec-file", type=Path, help="Read the specification from a file")
parser.add_argument("-o", "--output", type=Path, default=Path("program.paw"))
parser.add_argument(
"--teacher",
action="append",
metavar="MODEL=EXAMPLES",
help="Teacher and unique example count; repeat to mix teachers",
)
parser.add_argument("--teacher-temperature", type=float, default=1.0)
parser.add_argument("--examples-per-call", type=int, default=8)
parser.add_argument("--target-examples", type=int, default=defaults.target_examples)
parser.add_argument("--steps", type=int, default=defaults.steps)
parser.add_argument("--batch-size", type=int, default=defaults.batch_size)
parser.add_argument("--micro-batch-size", type=int, default=defaults.micro_batch_size)
parser.add_argument("--learning-rate", type=float, default=defaults.learning_rate)
parser.add_argument("--min-learning-rate", type=float, default=defaults.min_learning_rate)
parser.add_argument("--warmup", type=float, default=defaults.warmup)
parser.add_argument("--lora-rank", type=int, default=defaults.lora_rank)
parser.add_argument("--lora-alpha", type=float, default=defaults.lora_alpha)
parser.add_argument("--lora-init", choices=("mapper", "random"), default=defaults.lora_init)
parser.add_argument(
"--target-modules",
default=",".join(defaults.target_modules),
help="Comma-separated LoRA target modules",
)
parser.add_argument("--max-length", type=int, default=defaults.max_length)
parser.add_argument("--loss-chunk-size", type=int, default=defaults.loss_chunk_size)
parser.add_argument("--synth-concurrency", type=int, default=defaults.synth_concurrency)
parser.add_argument("--retries", type=int, default=defaults.retries)
parser.add_argument("--seed", type=int, default=defaults.seed)
parser.add_argument("--base-compiler", default=defaults.base_compiler)
parser.add_argument("--interpreter", default=defaults.interpreter)
parser.add_argument("--gradient-checkpointing", action="store_true")
parser.add_argument("--device", default="auto", help="auto, cuda, mps, or cpu")
parser.add_argument(
"--cache-dir",
type=Path,
default=Path(
os.environ.get("PAW_COMPILE_CACHE", "~/.cache/programasweights/compile-by-training")
).expanduser(),
)
parser.add_argument("--save-examples", type=Path)
parser.add_argument("--keep-adapter", type=Path)
parser.add_argument("--asset-timeout", type=int, default=180)
parser.add_argument("--force", action="store_true", help="Replace an existing output file")
parser.add_argument("--print-config", action="store_true")
parser.add_argument("--json", action="store_true", help="Print the final result as JSON")
return parser
def recipe_from_args(args: argparse.Namespace) -> Recipe:
defaults = Recipe()
if args.teacher:
teachers = [parse_teacher(value, defaults) for value in args.teacher]
else:
teachers = defaults.teachers
teachers = [
Teacher(
teacher.model,
teacher.examples,
args.teacher_temperature,
args.examples_per_call,
args.seed,
)
for teacher in teachers
]
target_modules = tuple(x.strip() for x in args.target_modules.split(",") if x.strip())
recipe = Recipe(
teachers=teachers,
target_examples=args.target_examples,
steps=args.steps,
batch_size=args.batch_size,
micro_batch_size=args.micro_batch_size,
learning_rate=args.learning_rate,
min_learning_rate=args.min_learning_rate,
warmup=args.warmup,
lora_rank=args.lora_rank,
lora_alpha=args.lora_alpha,
lora_init=args.lora_init,
target_modules=target_modules,
max_length=args.max_length,
loss_chunk_size=args.loss_chunk_size,
synth_concurrency=args.synth_concurrency,
retries=args.retries,
seed=args.seed,
base_compiler=args.base_compiler,
interpreter=args.interpreter,
gradient_checkpointing=args.gradient_checkpointing,
)
validate_recipe(recipe)
return recipe
def validate_recipe(recipe: Recipe) -> None:
positive_ints = {
"target_examples": recipe.target_examples,
"steps": recipe.steps,
"batch_size": recipe.batch_size,
"micro_batch_size": recipe.micro_batch_size,
"lora_rank": recipe.lora_rank,
"max_length": recipe.max_length,
"loss_chunk_size": recipe.loss_chunk_size,
"synth_concurrency": recipe.synth_concurrency,
}
for name, value in positive_ints.items():
if value <= 0:
raise ValueError(f"{name} must be positive")
if recipe.batch_size % recipe.micro_batch_size:
raise ValueError("micro_batch_size must divide batch_size")
if not 0.0 <= recipe.warmup < 1.0:
raise ValueError("warmup must be in [0, 1)")
if recipe.learning_rate <= 0 or recipe.min_learning_rate < 0:
raise ValueError("learning rates must be non-negative and learning_rate must be positive")
if not recipe.target_modules:
raise ValueError("target_modules cannot be empty")
unknown_modules = set(recipe.target_modules) - set(MODULE_TO_GGUF)
if unknown_modules:
raise ValueError(f"unsupported Qwen3 target modules: {sorted(unknown_modules)}")
if recipe.interpreter != DEFAULT_INTERPRETER:
raise ValueError(
f"this script currently packages {DEFAULT_INTERPRETER}; got {recipe.interpreter}"
)
def serializable_recipe(recipe: Recipe) -> dict[str, Any]:
result = asdict(recipe)
result["target_modules"] = list(recipe.target_modules)
result["teacher_template"] = TEACHER_TEMPLATE_VERSION
result["public_mapper_compile"] = True
return result
def read_spec(args: argparse.Namespace) -> str:
if args.spec and args.spec_file:
raise ValueError("pass either a positional specification or --spec-file, not both")
if args.spec_file:
spec = args.spec_file.read_text(encoding="utf-8").strip()
else:
spec = (args.spec or "").strip()
if len(spec) < 10:
raise ValueError("the specification must contain at least 10 characters")
return spec
def validate_destinations(args: argparse.Namespace) -> None:
destinations = (
("output", args.output),
("saved examples", args.save_examples),
("kept adapter", args.keep_adapter),
)
for label, path in destinations:
if path is not None and path.exists() and not args.force:
raise FileExistsError(f"{label} path {path} already exists; pass --force to replace it")
def auth_headers(api_key: str | None = None) -> dict[str, str]:
headers = {"Content-Type": "application/json", "User-Agent": "compile-by-training/1"}
if api_key:
headers["X-API-Key"] = api_key
return headers
def compile_mapper(
spec: str, recipe: Recipe, cache_dir: Path, asset_timeout: int
) -> dict[str, Any]:
import httpx
api_url = os.environ.get("PAW_API_URL", DEFAULT_API_URL).rstrip("/")
paw_key = os.environ.get("PAW_API_KEY")
log(f"1/4 Initializing with {recipe.base_compiler}...")
response = httpx.post(
f"{api_url}/api/v1/compile",
json={
"spec": spec,
"compiler": recipe.base_compiler,
"public": True,
"ephemeral": False,
},
headers=auth_headers(paw_key),
timeout=300.0,
)
response.raise_for_status()
body = response.json()
program_id = body.get("program_id")
if not isinstance(program_id, str) or len(program_id) != 20:
raise RuntimeError(f"PAW compiler returned an invalid program ID: {program_id!r}")
pseudo_program = body.get("pseudo_program")
if not isinstance(pseudo_program, str):
raise RuntimeError("PAW compiler returned no pseudo-program")
mapper_dir = cache_dir / "mapper" / program_id
mapper_dir.mkdir(parents=True, exist_ok=True)
download_mapper_asset(
api_url,
paw_key,
program_id,
"adapter_config.json",
mapper_dir,
asset_timeout,
)
download_mapper_asset(
api_url,
paw_key,
program_id,
"adapter_model.safetensors",
mapper_dir,
asset_timeout,
)
return {
"program_id": program_id,
"compiler_snapshot": body.get("compiler_snapshot") or recipe.base_compiler,
"pseudo_program": pseudo_program,
"pseudo_program_strategy": body.get("pseudo_program_strategy") or "vllm_generate",
"adapter_dir": mapper_dir,
}
def download_mapper_asset(
api_url: str,
api_key: str | None,
program_id: str,
filename: str,
destination: Path,
timeout: int,
) -> Path:
import httpx
output = destination / filename
if output.is_file() and output.stat().st_size:
return output
hf_base = os.environ.get("PAW_HF_ASSET_URL", DEFAULT_HF_ASSET_URL).rstrip("/")
urls = (
f"{api_url}/api/v1/programs/{program_id}/asset/{filename}",
f"{hf_base}/{program_id}/{filename}",
)
deadline = time.monotonic() + timeout
last_status: dict[str, int | None] = {url: None for url in urls}
while time.monotonic() < deadline:
for url in urls:
headers = auth_headers(api_key) if url.startswith(api_url) else None
with httpx.stream(
"GET",
url,
headers=headers,
follow_redirects=True,
timeout=120.0,
) as response:
last_status[url] = response.status_code
if response.status_code == 200:
temporary = output.with_suffix(output.suffix + ".part")
with temporary.open("wb") as handle:
for chunk in response.iter_bytes(1024 * 1024):
handle.write(chunk)
if temporary.stat().st_size == 0:
temporary.unlink(missing_ok=True)
raise RuntimeError(f"downloaded empty asset: {filename}")
temporary.replace(output)
return output
time.sleep(3)
statuses = ", ".join(str(status) for status in last_status.values())
raise RuntimeError(f"timed out waiting for {filename} (HTTP {statuses})")
def parse_examples(content: Any) -> list[dict[str, str]]:
if isinstance(content, str):
try:
value = json.loads(content)
except json.JSONDecodeError:
return []
else:
value = content
if isinstance(value, dict):
value = next(
(
value.get(key)
for key in ("examples", "data", "items")
if isinstance(value.get(key), list)
),
[],
)
if not isinstance(value, list):
return []
return [
{"input": item["input"], "output": item["output"]}
for item in value
if isinstance(item, dict)
and isinstance(item.get("input"), str)
and isinstance(item.get("output"), str)
]
async def one_teacher_call(
client: Any,
semaphore: asyncio.Semaphore,
spec: str,
teacher: Teacher,
seed_offset: int,
api_key: str,
api_url: str,
retries: int,
) -> list[dict[str, str]]:
payload = {
"model": teacher.model,
"temperature": teacher.temperature,
"seed": teacher.seed + seed_offset,
"messages": [
{"role": "system", "content": TEACHER_SYSTEM},
{
"role": "user",
"content": TEACHER_USER.format(n=teacher.examples_per_call, spec=spec),
},
],
"response_format": {"type": "json_object"},
}
async with semaphore:
for attempt in range(retries + 1):
try:
response = await client.post(
f"{api_url}/chat/completions",
headers={"Authorization": f"Bearer {api_key}"},
json=payload,
timeout=240.0,
)
response.raise_for_status()
content = response.json()["choices"][0]["message"]["content"]
examples = parse_examples(content)
if len(examples) >= teacher.examples_per_call:
return examples[: teacher.examples_per_call]
except Exception:
if attempt == retries:
raise
if attempt < retries:
await asyncio.sleep(2**attempt)
return []
def synth_cache_path(cache_dir: Path, spec: str, teacher: Teacher) -> Path:
payload = {
"spec": spec,
"teacher": asdict(teacher),
"template": TEACHER_TEMPLATE_VERSION,
}
digest = hashlib.sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest()
return cache_dir / "synth" / f"{digest}.json"
def cached_teacher_examples(
cache_dir: Path, spec: str, teacher: Teacher
) -> list[dict[str, str]] | None:
cache_path = synth_cache_path(cache_dir, spec, teacher)
if not cache_path.is_file():
return None
try:
cached = json.loads(cache_path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
return None
if not isinstance(cached, list) or len(cached) < teacher.examples:
return None
examples = parse_examples(cached)
if len(examples) < teacher.examples:
return None
return examples[: teacher.examples]
def validate_teacher_access(spec: str, recipe: Recipe, cache_dir: Path) -> bool:
if os.environ.get("OPENAI_API_KEY") or os.environ.get("TEACHER_API_KEY"):
return False
missing = [
teacher.model
for teacher in recipe.teachers
if cached_teacher_examples(cache_dir, spec, teacher) is None
]
if missing:
raise RuntimeError(
"set OPENAI_API_KEY (or TEACHER_API_KEY) for teacher synthesis; "
f"no complete cache for {', '.join(missing)}"
)
return True
async def synthesize_teacher(
client: Any,
spec: str,
teacher: Teacher,
recipe: Recipe,
api_key: str,
api_url: str,
cache_dir: Path,
) -> list[dict[str, str]]:
cache_path = synth_cache_path(cache_dir, spec, teacher)
cached = cached_teacher_examples(cache_dir, spec, teacher)
if cached is not None:
log(f" {teacher.model}: using {teacher.examples} cached examples")
return cached
semaphore = asyncio.Semaphore(recipe.synth_concurrency)
required_calls = math.ceil(teacher.examples / teacher.examples_per_call)
next_seed = 0
def new_task() -> asyncio.Task:
nonlocal next_seed
task = asyncio.create_task(
one_teacher_call(
client,
semaphore,
spec,
teacher,
next_seed,
api_key,
api_url,
recipe.retries,
)
)
next_seed += 1
return task
pending = {new_task() for _ in range(required_calls)}
examples: list[dict[str, str]] = []
replacements = 0
max_replacements = max(16, required_calls)
while pending and len(examples) < teacher.examples:
done, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
for task in done:
result = await task
if len(result) < teacher.examples_per_call:
if replacements >= max_replacements:
continue
pending.add(new_task())
replacements += 1
continue
examples.extend(result)
if len(examples) % 400 == 0 or len(examples) >= teacher.examples:
count = min(len(examples), teacher.examples)
log(f" {teacher.model}: {count}/{teacher.examples}")
if len(examples) >= teacher.examples:
break
for task in pending:
task.cancel()
await asyncio.gather(*pending, return_exceptions=True)
if len(examples) < teacher.examples:
raise RuntimeError(
f"{teacher.model} produced only {len(examples)}/{teacher.examples} examples"
)
examples = examples[: teacher.examples]
cache_path.parent.mkdir(parents=True, exist_ok=True)
temporary = cache_path.with_suffix(f".{os.getpid()}.part")
temporary.write_text(json.dumps(examples, ensure_ascii=False), encoding="utf-8")
temporary.replace(cache_path)
return examples
async def synthesize(spec: str, recipe: Recipe, cache_dir: Path) -> list[list[dict[str, str]]]:
import httpx
api_key = os.environ.get("OPENAI_API_KEY") or os.environ.get("TEACHER_API_KEY") or ""
validate_teacher_access(spec, recipe, cache_dir)
api_url = os.environ.get("TEACHER_API_URL", DEFAULT_TEACHER_API_URL).rstrip("/")
log("2/4 Synthesizing training examples...")
limits = httpx.Limits(
max_connections=max(32, recipe.synth_concurrency * len(recipe.teachers) * 2),
max_keepalive_connections=max(16, recipe.synth_concurrency * len(recipe.teachers)),
)
async with httpx.AsyncClient(limits=limits, timeout=240.0) as client:
return await asyncio.gather(
*[
synthesize_teacher(client, spec, teacher, recipe, api_key, api_url, cache_dir)
for teacher in recipe.teachers
]
)
def render_prompt_template(pseudo_program: str, tokenizer: Any) -> str:
pseudo = pseudo_program.strip()
raw = (
f"{pseudo}\n\n[INPUT]\n{{INPUT_PLACEHOLDER}}\n[END_INPUT]"
if pseudo
else "[INPUT]\n{INPUT_PLACEHOLDER}\n[END_INPUT]"
)
messages = [{"role": "user", "content": raw}]
try:
return tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=False,
enable_thinking=False,
)
except TypeError:
return tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
def tokenize_example(
tokenizer: Any, prompt_template: str, item: dict[str, str], max_length: int
) -> dict[str, list[int]]:
prompt = prompt_template.replace("{INPUT_PLACEHOLDER}", item["input"])
prompt_ids = tokenizer.encode(prompt, add_special_tokens=False)
output_ids = tokenizer.encode(item["output"], add_special_tokens=False)
if tokenizer.eos_token_id is not None:
output_ids.append(tokenizer.eos_token_id)
input_ids = (prompt_ids + output_ids)[:max_length]
labels = ([-100] * len(prompt_ids) + output_ids)[:max_length]
return {"input_ids": input_ids, "labels": labels}
def allocate_quotas(counts: list[int], target: int) -> list[int]:
total = sum(counts)
exact = [target * count / total for count in counts]
quotas = [math.floor(value) for value in exact]
for index in sorted(range(len(counts)), key=lambda i: exact[i] - quotas[i], reverse=True)[
: target - sum(quotas)
]:
quotas[index] += 1
return quotas
def build_schedule(
pools: list[list[dict[str, list[int]]]], recipe: Recipe
) -> list[dict[str, list[int]]]:
quotas = allocate_quotas(
[teacher.examples for teacher in recipe.teachers], recipe.target_examples
)
required = recipe.steps * recipe.batch_size
indexed_schedule: list[tuple[int, int]] = []
epochs = math.ceil(required / recipe.target_examples)
for epoch in range(epochs):
one_epoch: list[tuple[int, int]] = []
for component, (pool, quota) in enumerate(zip(pools, quotas, strict=True)):
one_epoch.extend((component, index % len(pool)) for index in range(quota))
random.Random(recipe.seed + epoch).shuffle(one_epoch)
indexed_schedule.extend(one_epoch)
if len(indexed_schedule) < required:
raise RuntimeError("training schedule is shorter than steps * batch_size")
# Production's "relabel" schedule preserves the shuffled repetition
# pattern while assigning synthesized examples in completion order.
mappings: list[dict[int, int]] = [{} for _ in pools]
schedule: list[dict[str, list[int]]] = []
for component, original_slot in indexed_schedule[:required]:
mapping = mappings[component]
if original_slot not in mapping:
mapping[original_slot] = len(mapping)
schedule.append(pools[component][mapping[original_slot]])
return schedule
def collate(items: list[dict[str, list[int]]], pad_id: int, device: Any) -> dict[str, Any]:
import torch
max_len = max(len(item["input_ids"]) for item in items)
input_ids, labels, attention = [], [], []
for item in items:
length = len(item["input_ids"])
padding = max_len - length
input_ids.append(item["input_ids"] + [pad_id] * padding)
labels.append(item["labels"] + [-100] * padding)
attention.append([1] * length + [0] * padding)
return {
"input_ids": torch.tensor(input_ids, dtype=torch.long, device=device),
"labels": torch.tensor(labels, dtype=torch.long, device=device),
"attention_mask": torch.tensor(attention, dtype=torch.long, device=device),
}
def unwrap_encoder_and_head(model: Any) -> tuple[Any, Any]:
inner = getattr(getattr(model, "base_model", model), "model", None)
if inner is None:
raise RuntimeError("could not locate the PEFT base model")
encoder = getattr(inner, "model", None) or getattr(inner, "transformer", None)
head = getattr(inner, "lm_head", None)
if encoder is None or head is None:
raise RuntimeError(f"unsupported model layout: {type(inner).__name__}")
return encoder, head
def chunked_loss_backward(
model: Any,
batch: dict[str, Any],
loss_chunk_size: int,
total_output_tokens: int,
) -> float:
import torch
import torch.nn.functional as functional
encoder, head = unwrap_encoder_and_head(model)
labels = batch["labels"]
shifted = torch.full_like(labels, -100)
shifted[:, :-1] = labels[:, 1:]
hidden = encoder(
input_ids=batch["input_ids"],
attention_mask=batch["attention_mask"],
use_cache=False,
).last_hidden_state
detached = hidden.detach().requires_grad_(True)
loss_sum = 0.0
for start in range(0, detached.size(1), loss_chunk_size):
end = min(start + loss_chunk_size, detached.size(1))
labels_chunk = shifted[:, start:end]
if not (labels_chunk != -100).any():
continue
logits = head(detached[:, start:end, :]).float()
cross_entropy = functional.cross_entropy(
logits.reshape(-1, logits.size(-1)),
labels_chunk.reshape(-1),
ignore_index=-100,
reduction="sum",
)
(cross_entropy / total_output_tokens).backward()
loss_sum += float(cross_entropy.detach())
del logits, cross_entropy
hidden.backward(detached.grad)
return loss_sum
def cuda_is_advertised(torch: Any) -> bool:
try:
if torch.cuda.device_count() > 0:
return True
except Exception:
pass
for name in ("CUDA_VISIBLE_DEVICES", "NVIDIA_VISIBLE_DEVICES", "SLURM_JOB_GPUS"):
value = os.environ.get(name)
if value is not None and value.strip().lower() not in ("", "-1", "none", "void"):
return True
return False
def choose_device(requested: str) -> Any:
import torch
if requested != "auto":
device = torch.device(requested)
if device.type not in ("cuda", "mps", "cpu"):
raise ValueError("device must be auto, cuda, mps, or cpu")
return device
if torch.cuda.is_available():
return torch.device("cuda")
if cuda_is_advertised(torch):
raise RuntimeError(
"a CUDA GPU is visible but PyTorch cannot initialize CUDA; "
"check the driver/runtime instead of falling back to CPU"
)
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return torch.device("mps")
return torch.device("cpu")
def preflight_device(requested: str, recipe: Recipe) -> Any:
import torch
device = choose_device(requested)
if device.type == "cuda" and not torch.cuda.is_available():
raise RuntimeError("CUDA was requested but PyTorch cannot initialize CUDA")
if device.type == "mps" and not (
hasattr(torch.backends, "mps") and torch.backends.mps.is_available()
):
raise RuntimeError("MPS was requested but is not available")
try:
probe = torch.empty(1, device=device)
if device.type == "cuda":
torch.cuda.synchronize(device)
elif device.type == "mps":
torch.mps.synchronize()
del probe
except Exception as exc:
raise RuntimeError(f"{device} preflight allocation failed: {exc}") from exc
if device.type == "cuda":
free_bytes, total_bytes = torch.cuda.mem_get_info(device)
free_gib = free_bytes / 1024**3
total_gib = total_bytes / 1024**3
name = torch.cuda.get_device_name(device)
log(f"Preflight: {device} ({name}; {free_gib:.1f}/{total_gib:.1f} GiB free)")
if (
free_gib < DEFAULT_RECIPE_MEMORY_GIB
and recipe.micro_batch_size >= Recipe().micro_batch_size
and not recipe.gradient_checkpointing
):
log(
"Warning: the default recipe uses about 38 GiB; reduce "
"--micro-batch-size or enable --gradient-checkpointing."
)
elif device.type == "mps":
log("Preflight: mps")
else:
log("Preflight: cpu")
log("Warning: the default recipe is slow on CPU; use an accelerator when available.")
return device
def cosine_learning_rate(step: int, total: int, high: float, low: float, warmup: float) -> float:
warmup_steps = max(1, int(total * warmup))
if step < warmup_steps:
return high * (step + 1) / warmup_steps
progress = (step - warmup_steps) / max(1, total - warmup_steps)
return low + 0.5 * (high - low) * (1.0 + math.cos(math.pi * progress))
def train(
recipe: Recipe,
mapper_dir: Path,
pseudo_program: str,
raw_pools: list[list[dict[str, str]]],
output_dir: Path,
device: Any,
) -> tuple[Path, str, list[float]]:
import torch
from peft import LoraConfig, PeftModel, get_peft_model
from transformers import AutoModelForCausalLM, AutoTokenizer
log("3/4 Finetuning the neural program...")
random.seed(recipe.seed)
torch.manual_seed(recipe.seed)
if device.type == "cuda":
torch.cuda.manual_seed_all(recipe.seed)
if device.type == "cuda":
dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
elif device.type == "mps":
dtype = torch.float16
else:
dtype = torch.float32
log(f" device: {device}; dtype: {dtype}")
tokenizer = AutoTokenizer.from_pretrained(recipe.interpreter, trust_remote_code=True)
if tokenizer.pad_token_id is None:
tokenizer.pad_token_id = tokenizer.eos_token_id
prompt_template = render_prompt_template(pseudo_program, tokenizer)
token_pools = [
[tokenize_example(tokenizer, prompt_template, item, recipe.max_length) for item in pool]
for pool in raw_pools
]
schedule = build_schedule(token_pools, recipe)
model = AutoModelForCausalLM.from_pretrained(
recipe.interpreter,
dtype=dtype,
trust_remote_code=True,
).to(device)
model.config.use_cache = False
if recipe.lora_init == "mapper":
mapper_config = json.loads((mapper_dir / "adapter_config.json").read_text(encoding="utf-8"))
mapper_rank = int(mapper_config.get("r", 0))
mapper_alpha = float(mapper_config.get("lora_alpha", 0))
mapper_modules = set(mapper_config.get("target_modules") or [])
if mapper_rank != recipe.lora_rank or mapper_alpha != recipe.lora_alpha:
raise ValueError(
f"mapper initialization has rank={mapper_rank}, alpha={mapper_alpha}; "
"use those values or pass --lora-init random"
)
if mapper_modules != set(recipe.target_modules):
raise ValueError(
"mapper initialization has different target modules; "
"use the defaults or pass --lora-init random"
)
peft_model = PeftModel.from_pretrained(model, str(mapper_dir), is_trainable=True)
else:
config = LoraConfig(
r=recipe.lora_rank,
lora_alpha=recipe.lora_alpha,
target_modules=list(recipe.target_modules),
bias="none",
task_type="CAUSAL_LM",
)
peft_model = get_peft_model(model, config)
if recipe.gradient_checkpointing:
peft_model.gradient_checkpointing_enable(
gradient_checkpointing_kwargs={"use_reentrant": False}
)
peft_model.enable_input_require_grads()
peft_model.train()
optimizer = torch.optim.AdamW(
(parameter for parameter in peft_model.parameters() if parameter.requires_grad),
lr=recipe.learning_rate,
betas=(0.9, 0.999),
weight_decay=0.0,
)
losses: list[float] = []
for step in range(recipe.steps):
start = step * recipe.batch_size
step_items = schedule[start : start + recipe.batch_size]
total_output_tokens = sum(
sum(label != -100 for label in item["labels"]) for item in step_items
)
if total_output_tokens == 0:
raise RuntimeError("training batch contains no output tokens")
learning_rate = cosine_learning_rate(
step,
recipe.steps,
recipe.learning_rate,
recipe.min_learning_rate,
recipe.warmup,
)
for group in optimizer.param_groups:
group["lr"] = learning_rate
optimizer.zero_grad(set_to_none=True)
loss_sum = 0.0
for micro_start in range(0, recipe.batch_size, recipe.micro_batch_size):
micro_items = step_items[micro_start : micro_start + recipe.micro_batch_size]
batch = collate(micro_items, tokenizer.pad_token_id, device)
loss_sum += chunked_loss_backward(
peft_model,
batch,
recipe.loss_chunk_size,
total_output_tokens,
)
del batch
optimizer.step()
if device.type == "cuda":