File size: 35,668 Bytes
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b4e31e
 
 
 
 
 
 
 
 
 
 
 
ef7a73f
 
 
 
 
 
 
 
 
 
 
 
 
 
426ccd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
fbd4e33
 
 
 
 
 
 
 
 
 
 
f1a4107
 
ed624e0
f1a4107
ed624e0
 
 
 
 
f1a4107
 
 
 
 
 
426ccd2
 
 
 
 
 
 
 
 
 
f1a4107
 
 
 
 
 
 
 
5c9fbe6
 
 
 
 
 
 
 
0155deb
 
 
 
 
f1a4107
 
 
 
 
 
 
 
 
 
3c0665e
 
 
 
 
 
 
 
 
 
 
 
 
8760dcb
 
 
 
3c0665e
8760dcb
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c198ca
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c198ca
f1a4107
973da54
 
f1a4107
973da54
 
 
 
 
d2136d7
 
 
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbd4e33
f1a4107
 
fbd4e33
f1a4107
 
 
 
 
 
 
 
 
 
426ccd2
 
 
 
 
 
 
f1a4107
426ccd2
 
 
 
 
 
ed624e0
426ccd2
 
 
 
 
 
 
 
 
 
f1a4107
 
 
 
 
 
ed624e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbd4e33
 
 
 
 
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
fbd4e33
f1a4107
fbd4e33
 
f1a4107
 
 
 
426ccd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbd4e33
 
426ccd2
f1a4107
 
 
 
 
 
 
5c9fbe6
 
 
 
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c9fbe6
 
 
 
 
 
 
 
 
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef7a73f
 
 
 
 
f1a4107
 
 
 
 
 
ef7a73f
f1a4107
 
 
 
 
 
 
 
ef7a73f
 
 
426ccd2
f1a4107
 
 
 
 
 
 
fbd4e33
 
 
426ccd2
f1a4107
 
 
 
426ccd2
 
 
 
 
 
 
 
 
 
 
f1a4107
ef7a73f
 
 
 
 
 
 
 
 
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
426ccd2
 
 
 
 
 
 
 
 
 
f1a4107
 
ef7a73f
 
 
 
f1a4107
 
 
 
 
 
 
 
 
ef7a73f
 
 
 
 
 
 
 
 
 
f1a4107
 
 
 
 
 
 
426ccd2
 
f1a4107
 
 
 
 
 
 
 
ef7a73f
f1a4107
 
426ccd2
f1a4107
 
 
ef7a73f
5b4e31e
 
f1a4107
5b4e31e
ef7a73f
 
 
 
 
f1a4107
5b4e31e
 
 
 
 
 
 
 
ef7a73f
5b4e31e
f1a4107
5b4e31e
f26e497
 
 
4d9b333
 
 
 
 
f26e497
426ccd2
 
f1a4107
5b4e31e
 
 
 
 
 
 
 
f1a4107
 
 
 
 
5b4e31e
f1a4107
 
5b4e31e
f1a4107
 
f26e497
 
 
 
 
 
 
 
 
 
5b4e31e
f1a4107
 
 
 
 
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
#!/usr/bin/env python3
"""Minimal Quantization-Aware Distillation (QAD) for lalamo HybridMatrix models.

Pipeline:
  1. Load a BF16 teacher and an RHT+INT4 PTQ student (lalamo format).
  2. Switch student HybridMatrix nodes to CompressionImplementation.TRAINING.
  3. Teacher-forced forward on a corpus; KL(teacher || student) loss.
  4. Optimize:
       - master_weights: Muon (optax.contrib.muon)
       - scales / zero points: AdamW
     with deterministic STE from lalamo's rounding.
  5. Switch back to INFERENCE, save, rewrite metadata for uzu.

This is a first-stage QAD: the loss uses teacher-forced corpus text. For the
full Mirai recipe, replace the corpus with teacher-generated rollouts.

Usage:
  JAX_PLATFORM_NAME=cpu python scripts/qad_finetune.py \
    --teacher-dir /tmp/qwen35_0.8b_bf16_lalamo_lalamo_origcfg \
    --student-dir /tmp/qwen35_0.8b_yaqa_hin_hout_lalamo_eval \
    --corpus experiments/qwen35-0.8b/data/calibration_en_wikitext2_dedup.txt \
    --eval-corpus experiments/qwen35-0.8b/data/eval_en_wikitext2.txt \
    --eval-corpus experiments/qwen35-0.8b/data/eval_ja_wikipedia.txt \
    --output-dir /tmp/qwen35_0.8b_qad_lalamo \
    --steps 50 --seq 128 --lr 1e-4
"""

from __future__ import annotations

import argparse
import hashlib
import json
import os
import pickle
import shutil
import sys
import time
from pathlib import Path

os.environ.setdefault("JAX_PLATFORM_NAME", "cpu")


def peak_rss_mb() -> float:
    """Peak resident memory of this process, in MB.

    A silent death right after the last step (no traceback in the log, no
    checkpoint) looks like the OOM killer; this number says which phase grew.
    """
    import resource

    usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    return usage / (1024 * 1024) if sys.platform == "darwin" else usage / 1024


def early_stop_decision(bad_val_evals: int, patience: int, step: int, steps: int) -> str:
    """Decide what to do after a non-improving validation.

    ``step`` is 1-based. At the final step there is nothing left to stop, so a
    non-improvement must not abort the run: aborting also skipped the checkpoint
    save and export, which turned a completed 10-step run into a failed kernel.
    """
    if patience <= 0 or bad_val_evals < patience:
        return "continue"
    if step >= steps:
        return "ignore"
    return "stop"


def median_quantization_step(scale_arrays) -> float | None:
    """Median int4 quantisation step implied by the stored scales.

    The 4-bit forward rounds ``weight / (scale / 7)``, so an optimizer update
    much smaller than ``scale / 7`` cannot change a single code. The forward
    also casts master weights to the matmul dtype (bf16) first, which is why a
    float32 master weight alone does not rescue a finetuning-sized lr.
    """
    import numpy as np

    values = [np.asarray(array, dtype=np.float32).ravel() for array in scale_arrays]
    values = [value for value in values if value.size]
    if not values:
        return None
    return float(np.median(np.concatenate(values))) / 7.0


def update_is_absorbed(lr: float, step: float | None, ratio: float = 0.05) -> bool:
    """True when a single update is far below one quantisation step."""
    return step is not None and step > 0.0 and lr < ratio * step


def validation_is_unchanged(best_val: float | None, val_loss: float) -> bool:
    """True when validation did not move at all.

    An identical val_kl is evidence that the master weights did not change, not
    that the model stopped improving. Counting it as a non-improvement early
    stopped a 10-step run whose optimizer was a silent no-op: at bf16 the
    AdamW update (2e-5) is an order of magnitude below one ulp, so every step
    rounded to the same weights and val_kl stayed at 0.262012.
    """
    return best_val is not None and val_loss == best_val


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument("--teacher-dir", type=Path, required=True)
    parser.add_argument("--student-dir", type=Path, required=True)
    parser.add_argument("--output-dir", type=Path, required=True)
    parser.add_argument("--corpus", type=Path, required=True)
    parser.add_argument("--eval-corpus", type=Path, action="append", default=[], help="evaluation corpus to keep disjoint")
    parser.add_argument("--allow-overlap", action="store_true", help="allow training/eval overlap for analysis only")
    parser.add_argument("--lalamo-path", default=os.environ.get("LALAMO_PATH", "/tmp/lalamo-direct"))
    parser.add_argument("--steps", type=int, default=50)
    parser.add_argument("--seq", type=int, default=128)
    parser.add_argument("--lr", type=float, default=1e-4)
    parser.add_argument("--muon-lr", type=float, default=1e-3)
    parser.add_argument(
        "--freeze-scales",
        action="store_true",
        help="train master_weights only; leave scales and zero-points fixed",
    )
    parser.add_argument(
        "--warmup-steps",
        type=int,
        default=0,
        help="linearly ramp the learning rate over this many steps",
    )
    parser.add_argument(
        "--optimizer",
        choices=("muon", "adamw", "sgd"),
        default="muon",
        help=(
            "muon: master=Muon + scales=AdamW; adamw: all trainables=AdamW; "
            "sgd: momentum SGD for master weights, AdamW for scales (one state "
            "per weight, tensor-parallel safe — use when AdamW does not fit)"
        ),
    )
    parser.add_argument("--adam-b1", type=float, default=0.9)
    parser.add_argument("--adam-b2", type=float, default=0.95, help="short QAD (<100 steps) uses 0.95, not 0.999")
    parser.add_argument("--scale-min", type=float, default=1e-6, help="lower clamp for scale params; 0 disables")
    parser.add_argument("--grad-clip", type=float, default=1.0, help="global norm clip; 0 disables")
    parser.add_argument("--scale-lr", type=float, default=1e-6, help="AdamW lr for scales/zero-points")
    parser.add_argument(
        "--master-dtype",
        choices=("float32", "bfloat16"),
        default="float32",
        help=(
            "dtype the trainable master weights accumulate in. float32 by default: "
            "at bf16 a 2e-5 update is below one ulp (0.008 -> 6.1e-5) and "
            "apply_updates rounds it away, leaving the 4-bit forward bit-identical"
        ),
    )
    parser.add_argument("--early-stop-patience", type=int, default=2, help="stop after N non-improving val evals")
    parser.add_argument("--student-provenance", type=Path, default=None, help="default: <student-dir>/provenance.json")
    parser.add_argument("--allow-unverified-student", action="store_true")
    parser.add_argument("--temperature", type=float, default=1.0)
    parser.add_argument("--seed", type=int, default=0)
    parser.add_argument("--val-chunks", type=int, default=2, help="fixed chunks used for comparable validation loss")
    parser.add_argument("--val-every", type=int, default=5, help="evaluate fixed validation chunks every N steps")
    parser.add_argument("--save-every", type=int, default=10, help="checkpoint interval; 0 = only final")
    parser.add_argument(
        "--checkpoint-params-only",
        action="store_true",
        help=(
            "save only master weights (no AdamW moments). Roughly a third of the "
            "checkpoint memory, at the cost of reinitialising the optimizer on resume"
        ),
    )
    parser.add_argument(
        "--skip-checkpoint",
        action="store_true",
        help="do not write orbax checkpoints (isolates the export-phase OOM)",
    )
    parser.add_argument("--checkpoint-dir", type=Path, default=None, help="default: <output-dir>/checkpoints")
    parser.add_argument("--resume", default=None, help="'auto' or a checkpoint directory")
    parser.add_argument("--keep-checkpoints", type=int, default=2, help="retain the newest N checkpoints")
    parser.add_argument("--parallel", choices=("replicated", "tensor", "fsdp", "data"), default="replicated")
    parser.add_argument("--dry-run", action="store_true")
    return parser.parse_args()


def main() -> None:
    args = parse_args()
    if args.parallel == "data":
        raise SystemExit(
            "data parallelism cannot work here: the script trains on a single sequence, "
            "so the batch axis has size 1 and cannot be split across devices "
            "(lalamo also shards the PRNG keychain along it). Use --parallel tensor "
            "or --parallel replicated."
        )
    if args.parallel == "fsdp":
        raise SystemExit(
            "fsdp cannot shard this model: it splits every array along axis 0, and "
            "dimension-1 parameters such as norm scales cannot be divided. "
            "Use --parallel tensor or --parallel replicated."
        )
    if args.optimizer == "muon" and args.parallel == "tensor":
        raise SystemExit(
            "muon cannot run with tensor parallelism: Newton-Schulz contracts the "
            "sharded gradient (x @ x.T), and JAX cannot infer the output sharding. "
            "Use --parallel replicated or --optimizer adamw."
        )
    sys.path.insert(0, args.lalamo_path)
    sys.path.insert(0, str(Path(__file__).resolve().parent))

    from corpus_guard import assert_disjoint

    assert_disjoint(args.corpus, args.eval_corpus, allow_overlap=args.allow_overlap)

    import equinox as eqx
    import jax
    import jax.numpy as jnp
    import numpy as np
    import optax

    from lalamo.compressed.hybrid import HybridMatrix
    from lalamo.model import Model
    from lalamo_forward import apply_attention_env, describe, enter_mesh, force_float32_activations
    from lalamo.module import Keychain
    from lalamo.modules import DecoderForwardPassConfig, ForwardPassMode
    from lalamo.utils.sharding import ShardingConfig
    from lalamo.utils.surgery import map_nodes_of_type_with_path
    from lalamo.weight_matrix import CompressionImplementation, GradientEstimator

    from normalize_lalamo_config import normalize
    from rewrite_lalamo_hybrid_spec import rewrite

    if args.parallel == "tensor":
        sharding = ShardingConfig.tensor_parallel()
    elif args.parallel == "fsdp":
        sharding = ShardingConfig.fully_sharded_data_parallel()
    elif args.parallel == "data":
        sharding = ShardingConfig.data_parallel()
    else:
        sharding = ShardingConfig.replicated()
    _mesh = enter_mesh(sharding)
    keychain = Keychain.init(seed=args.seed, sharding_config=sharding)
    inference_config = force_float32_activations(
        apply_attention_env(DecoderForwardPassConfig.for_inference(mode=ForwardPassMode.MULTI_TOKEN))
    )
    training_config = force_float32_activations(
        apply_attention_env(
            DecoderForwardPassConfig.for_training(
                gradient_estimator=GradientEstimator.DETERMINISTIC_ROUNDING
            )
        )
    )
    print(f"attention: {describe()}", flush=True)

    def tokenizer_hash(directory: Path) -> str:
        return hashlib.sha256((directory / "tokenizer.json").read_bytes()).hexdigest()

    for label, directory in (("teacher", args.teacher_dir), ("student", args.student_dir)):
        config = json.loads((directory / "config.json").read_text())
        if "response_format" not in config.get("token_codec_config", {}):
            raise SystemExit(
                f"{label}: {directory} is uzu-normalized; QAD requires raw lalamo config"
            )
    if tokenizer_hash(args.teacher_dir) != tokenizer_hash(args.student_dir):
        raise SystemExit("teacher/student tokenizer.json mismatch")

    provenance_path = args.student_provenance or (args.student_dir / "provenance.json")
    if not provenance_path.exists():
        if not args.allow_unverified_student:
            raise SystemExit(
                f"student provenance missing: {provenance_path}. "
                "Rebuild the student with a clean calibration or pass --allow-unverified-student for analysis."
            )
        provenance = {"overlap_checked": False, "kind": "unknown"}
    else:
        provenance = json.loads(provenance_path.read_text(encoding="utf-8"))
        if not provenance.get("overlap_checked") and not args.allow_unverified_student:
            raise SystemExit(
                f"student provenance says overlap_checked=false: {provenance_path}"
            )
    print(f"student provenance: kind={provenance.get('kind')} overlap_checked={provenance.get('overlap_checked')}")

    print("loading teacher...")
    teacher = Model.load(args.teacher_dir, sharding, dtype=jnp.bfloat16)
    print("loading student...")
    student = Model.load(args.student_dir, sharding, dtype=jnp.bfloat16)

    def to_training(path, matrix: HybridMatrix) -> HybridMatrix:
        return matrix.switch_implementation(CompressionImplementation.TRAINING)

    student = map_nodes_of_type_with_path(HybridMatrix, to_training, student)
    print("student switched to TRAINING implementation")

    def logits_for(model, ids, config):
        token_ids = jnp.asarray([ids], dtype=jnp.int32)
        positions = jnp.arange(len(ids), dtype=jnp.int32)[None, :]
        result = model.decoder(
            token_ids=token_ids,
            token_positions=positions,
            state=None,
            return_updated_state=False,
            lengths_without_padding=None,
            forward_pass_config=config,
            keychain=keychain.split(1)[0],
        )
        return result.logits[0]

    texts = [line.strip() for line in args.corpus.read_text(encoding="utf-8").splitlines() if line.strip()]
    token_ids = teacher.token_codec.tokenizer.encode("\n\n".join(texts), add_special_tokens=True).ids
    chunks = [token_ids[start : start + args.seq] for start in range(0, len(token_ids), args.seq)]
    chunks = [chunk for chunk in chunks if len(chunk) >= 16]
    if not chunks:
        raise SystemExit("no usable calibration chunks")
    print(f"chunks: {len(chunks)} x {args.seq} tokens")
    val_ids = chunks[-args.val_chunks :] if args.val_chunks > 0 else []

    def collect_params(model):
        params = {}
        trainable = ("master_weights",) if args.freeze_scales else ("master_weights", "scales", "master_zero_points")
        for path, leaf in jax.tree_util.tree_flatten_with_path(model)[0]:
            key = jax.tree_util.keystr(path)
            if any(name in key for name in trainable) and isinstance(leaf, jax.Array):
                params[key] = leaf
        return params

    def inject_params(model, params):
        return jax.tree_util.tree_map_with_path(
            lambda path, leaf: params.get(jax.tree_util.keystr(path), leaf),
            model,
        )

    params = collect_params(student)
    # Remember the dtype the model was loaded with so the export keeps it: the
    # compressed format only stores 4-bit codes plus scales, and packing from
    # bf16 master weights is what the inference matrix expects.
    master_dtypes = {key: value.dtype for key, value in params.items()}
    if args.master_dtype == "float32":
        params = {key: value.astype(jnp.float32) for key, value in params.items()}
    print(f"master weights: {args.master_dtype}")
    print(f"trainable arrays: {len(params)}")
    scale_leaves = [
        leaf
        for path, leaf in jax.tree_util.tree_flatten_with_path(student)[0]
        if jax.tree_util.keystr(path).endswith(".scales") and isinstance(leaf, jax.Array)
    ]
    quant_step = median_quantization_step(scale_leaves)
    planned_lr = args.muon_lr if args.optimizer == "muon" else args.lr
    if quant_step is not None:
        print(f"median int4 step: {quant_step:.2e}  planned lr: {planned_lr:g}", flush=True)
    if update_is_absorbed(planned_lr, quant_step):
        print(
            f"WARNING: lr {planned_lr:g} is {planned_lr / quant_step:.1%} of the median int4 "
            f"step ({quant_step:.2e}). An update this small is rounded away by the 4-bit "
            "forward, so val_kl cannot move and early stop fires on a run that never "
            "learned anything. QAD master-weight lr is typically 1e-3.",
            flush=True,
        )
    total_elements = sum(int(np.prod(value.shape)) for value in params.values())
    print(f"trainable elements: {total_elements / 1e6:.1f}M")

    def is_scale_key(key: str) -> bool:
        return key.endswith("scales") or key.endswith("master_zero_points")

    if args.optimizer == "sgd":
        labels = {
            key: ("scale" if is_scale_key(key) else "weight")
            for key in params
        }
        optimizer = optax.multi_transform(
            {
                # One momentum buffer per weight instead of AdamW's two, so this fits
                # where AdamW is a few MiB short. Elementwise, so TP-safe.
                "weight": optax.sgd(args.lr, momentum=0.9),
                "scale": optax.adamw(args.scale_lr, b1=args.adam_b1, b2=args.adam_b2, weight_decay=0.0),
            },
            labels,
        )
        print(f"optimizer: SGD(momentum=0.9, lr={args.lr}) + AdamW(scale_lr={args.scale_lr})", flush=True)
    elif args.optimizer == "adamw":
        labels = {
            key: ("scale" if is_scale_key(key) else "weight")
            for key in params
        }
        optimizer = optax.multi_transform(
            {
                "weight": optax.adamw(args.lr, b1=args.adam_b1, b2=args.adam_b2, weight_decay=0.0),
                "scale": optax.adamw(args.scale_lr, b1=args.adam_b1, b2=args.adam_b2, weight_decay=0.0),
            },
            labels,
        )
        print(f"optimizer: AdamW weight_lr={args.lr} scale_lr={args.scale_lr}", flush=True)
    else:
        labels = {
            key: (
                "muon"
                if key.endswith("master_weights") and value.ndim >= 2
                else "scale"
                if is_scale_key(key)
                else "adam"
            )
            for key, value in params.items()
        }
        optimizer = optax.multi_transform(
            {
                "muon": optax.contrib.muon(args.muon_lr),
                # weight_decay=0.0 is required: optax.adamw defaults to 1e-4 and would
                # shrink scales/zero-points towards zero every step (scale collapse).
                "adam": optax.adamw(args.lr, b1=args.adam_b1, b2=args.adam_b2, weight_decay=0.0),
                "scale": optax.adamw(args.scale_lr, b1=args.adam_b1, b2=args.adam_b2, weight_decay=0.0),
            },
            labels,
        )
        print(f"optimizer: Muon(master) + AdamW(scale_lr={args.scale_lr})", flush=True)

    if args.grad_clip > 0:
        optimizer = optax.chain(optax.clip_by_global_norm(args.grad_clip), optimizer)
        print(f"grad clip: global norm {args.grad_clip}", flush=True)

    scale_min = float(args.scale_min)

    def clamp_scales(model_params):
        if scale_min <= 0.0:
            return model_params
        return {
            key: (jnp.maximum(value, scale_min) if key.endswith("scales") else value)
            for key, value in model_params.items()
        }

    params = clamp_scales(params)
    opt_state = optimizer.init(params)
    print(
        f"trainable: {'master_weights only' if args.freeze_scales else 'master_weights + scales + zero_points'}"
        f"  warmup_steps={args.warmup_steps}",
        flush=True,
    )

    def loss_fn(current_params, ids):
        model = inject_params(student, current_params)
        teacher_logits = jax.lax.stop_gradient(logits_for(teacher, ids, inference_config))
        student_logits = logits_for(model, ids, training_config)
        temperature = args.temperature
        teacher_log_probs = jax.nn.log_softmax(teacher_logits / temperature, axis=-1)
        student_log_probs = jax.nn.log_softmax(student_logits / temperature, axis=-1)
        teacher_probs = jnp.exp(teacher_log_probs)
        kl = jnp.sum(teacher_probs * (teacher_log_probs - student_log_probs), axis=-1).mean()
        return kl * (temperature**2)

    @jax.jit
    def train_step(current_params, opt_state, ids, lr_factor):
        loss, grads = jax.value_and_grad(loss_fn)(current_params, ids)
        if args.warmup_steps > 0:
            grads = jax.tree_util.tree_map(lambda grad: grad * lr_factor, grads)
        grad_norm = optax.global_norm(grads)
        updates, new_state = optimizer.update(grads, opt_state, current_params)
        new_params = optax.apply_updates(current_params, updates)
        new_params = clamp_scales(new_params)
        # A weight that does not change bit for bit cannot move the 4-bit
        # forward: the quantizer rounds such an update away. Count what moved so
        # a no-op optimizer cannot pass for a successful run.
        old_leaves = jax.tree_util.tree_leaves(current_params)
        new_leaves = jax.tree_util.tree_leaves(new_params)
        if old_leaves:
            changed = sum(jnp.sum(new != old) for new, old in zip(new_leaves, old_leaves))
            max_delta = jnp.max(
                jnp.stack(
                    [jnp.max(jnp.abs(new - old)) for new, old in zip(new_leaves, old_leaves)]
                )
            )
        else:
            changed = jnp.array(0, jnp.int32)
            max_delta = jnp.array(0.0, jnp.float32)
        return new_params, new_state, loss, grad_norm, changed, max_delta

    def validation_loss(current_params):
        if not val_ids:
            return None
        values = [float(loss_fn(current_params, ids)) for ids in val_ids]
        return sum(values) / len(values)

    if args.dry_run:
        print("dry-run: no training")
        return

    import orbax.checkpoint as ocp

    args.output_dir.mkdir(parents=True, exist_ok=True)
    checkpoint_dir = args.checkpoint_dir or (args.output_dir / "checkpoints")
    checkpoint_dir.mkdir(parents=True, exist_ok=True)
    checkpointer = ocp.PyTreeCheckpointer()
    checkpoint_meta = {
        "teacher_dir": str(args.teacher_dir),
        "student_dir": str(args.student_dir),
        "eval_corpora": [str(path) for path in args.eval_corpus],
        "overlap_checked": bool(args.eval_corpus),
        "seq": args.seq,
        "optimizer": args.optimizer,
        "lr": args.lr,
        "scale_lr": args.scale_lr,
        "muon_lr": args.muon_lr,
        "student_provenance_sha256": (
            hashlib.sha256(provenance_path.read_bytes()).hexdigest()
            if provenance_path.exists()
            else None
        ),
        "adam_b1": args.adam_b1,
        "adam_b2": args.adam_b2,
        "scale_min": args.scale_min,
        "freeze_scales": bool(args.freeze_scales),
        "warmup_steps": args.warmup_steps,
        "master_dtype": args.master_dtype,
    }

    def save_checkpoint(step: int, current_params, current_state, current_rng) -> None:
        path = checkpoint_dir / f"step_{step:05d}"
        tmp_path = checkpoint_dir / f".{path.name}.tmp"
        if tmp_path.exists():
            shutil.rmtree(tmp_path)
        payload = {"params": current_params, "step": step}
        if not args.checkpoint_params_only:
            payload["opt_state"] = current_state
        checkpointer.save(tmp_path, payload, force=True)
        with open(tmp_path / "rng.pkl", "wb") as file:
            pickle.dump(current_rng.bit_generator.state, file)
        with open(tmp_path / "meta.json", "w", encoding="utf-8") as file:
            json.dump(checkpoint_meta, file, indent=2, sort_keys=True)
        (tmp_path / "COMPLETE").write_text("ok\n", encoding="utf-8")
        if path.exists():
            shutil.rmtree(path)
        os.replace(tmp_path, path)
        keep = max(args.keep_checkpoints, 1)
        for old in sorted(checkpoint_dir.glob("step_*"))[:-keep]:
            shutil.rmtree(old, ignore_errors=True)
        print(f"checkpoint -> {path}", flush=True)

    rng = np.random.default_rng(args.seed)
    start_step = 0
    if args.resume:
        if args.resume == "auto":
            candidates = sorted(
                path for path in checkpoint_dir.glob("step_*") if (path / "COMPLETE").exists()
            )
            resume_path = candidates[-1] if candidates else None
        else:
            resume_path = Path(args.resume)
        if resume_path is None:
            print("resume: no complete checkpoint found; starting from 0", flush=True)
        elif not resume_path.exists():
            raise SystemExit(f"checkpoint not found: {resume_path}")
        else:
            meta_file = resume_path / "meta.json"
            if not meta_file.exists():
                raise RuntimeError(f"checkpoint missing meta.json: {resume_path}")
            saved_meta = json.loads(meta_file.read_text(encoding="utf-8"))
            for key, expected in checkpoint_meta.items():
                if saved_meta.get(key) != expected:
                    raise RuntimeError(
                        f"checkpoint meta mismatch for {key}: saved={saved_meta.get(key)!r} current={expected!r}"
                    )
            restored = checkpointer.restore(resume_path)
            params = restored["params"]
            if "opt_state" in restored:
                opt_state = restored["opt_state"]
            else:
                opt_state = optimizer.init(params)
                print(
                    "resume: checkpoint has no optimizer state "
                    "(--checkpoint-params-only); AdamW moments reinitialised",
                    flush=True,
                )
            start_step = int(restored["step"])
            rng_file = resume_path / "rng.pkl"
            if not rng_file.exists():
                raise RuntimeError(f"checkpoint missing rng.pkl: {resume_path}")
            with open(rng_file, "rb") as file:
                rng.bit_generator.state = pickle.load(file)
            print(f"resumed from {resume_path} at step {start_step}", flush=True)

    run_id = time.strftime("%Y%m%d-%H%M%S")
    progress_path = args.output_dir / "progress.jsonl"
    if not args.resume:
        progress_path.write_text("", encoding="utf-8")

    def progress(record: dict) -> None:
        record = {"run_id": run_id, **record}
        with open(progress_path, "a", encoding="utf-8") as file:
            file.write(json.dumps(record) + "\n")

    def scale_stats(current_params):
        """Return (min, max) over scale arrays, or (None, None) when there are none.

        With ``--freeze-scales`` the scales are not in the trainable dict at all.
        Returning 0.0 there looked like a collapsed scale, so absence is explicit.
        """
        values = [
            np.asarray(value, dtype=np.float32).ravel()
            for key, value in current_params.items()
            if key.endswith("scales")
        ]
        if not values:
            return None, None
        merged = np.concatenate(values)
        return float(merged.min()), float(merged.max())

    initial_scale_min, initial_scale_max = scale_stats(params)
    started_at = time.time()
    initial_val = validation_loss(params)
    best_val = initial_val
    bad_val_evals = 0
    stop_reason = None
    completed_steps = start_step
    last_saved_step = start_step
    warned_frozen = False
    if initial_val is not None:
        progress({"step": 0, "val_loss": initial_val, "elapsed_s": 0.0})
        print(f"step 0/{args.steps}  val_kl={initial_val:.6f}", flush=True)

    for step in range(start_step, args.steps):
        ids = chunks[int(rng.integers(0, len(chunks)))]
        step_started = time.time()
        lr_factor = 1.0
        if args.warmup_steps > 0:
            lr_factor = min(1.0, (step + 1) / args.warmup_steps)
        params, opt_state, loss, grad_norm, changed, max_delta = train_step(params, opt_state, ids, lr_factor)
        jax.block_until_ready(params)
        now = time.time()
        loss_value = float(loss)
        grad_value = float(grad_norm)
        changed_count = int(changed)
        max_delta_value = float(max_delta)
        if changed_count == 0 and not warned_frozen:
            warned_frozen = True
            print(
                "WARNING: no master weight changed this step. The update is below the "
                f"storage resolution (master dtype={args.master_dtype}), so the 4-bit "
                "forward and val_kl cannot move. Raise --lr: QAD master-weight lr is "
                "typically 1e-3, not the 2e-5 used for plain finetuning.",
                flush=True,
            )
        scale_lo, scale_hi = scale_stats(params)
        has_scales = scale_lo is not None and scale_hi is not None
        bad = not (np.isfinite(loss_value) and np.isfinite(grad_value))
        if has_scales:
            bad = bad or not (np.isfinite(scale_lo) and np.isfinite(scale_hi))
        collapsed = (
            has_scales
            and initial_scale_max is not None
            and initial_scale_max > scale_min * 10
            and scale_hi <= scale_min * 10
        )
        if bad or collapsed:
            reason = "non-finite loss/grad/scale" if bad else "scale collapse"
            progress(
                {
                    "step": step + 1,
                    "status": "failed",
                    "reason": reason,
                    "loss": loss_value,
                    "grad_norm": grad_value,
                    "scale_min": scale_lo,
                    "scale_max": scale_hi,
                    "elapsed_s": now - started_at,
                }
            )
            raise RuntimeError(f"QAD stopped at step {step + 1}: {reason}")
        val_loss = None
        if val_ids and ((step + 1) % args.val_every == 0 or step + 1 == args.steps):
            val_loss = validation_loss(params)
            if val_loss is not None:
                if best_val is None or val_loss < best_val:
                    best_val = val_loss
                    bad_val_evals = 0
                elif validation_is_unchanged(best_val, val_loss):
                    # Bit-identical val means the weights did not move, not that
                    # the model is overfitting. Counting this as a bad eval is
                    # what turned a no-op optimizer into a failed kernel.
                    print(
                        f"val unchanged at step {step + 1} (val_kl={val_loss:.6f}, "
                        f"changed={changed_count}, max_delta={max_delta_value:.2e}); "
                        "not counted as a bad eval",
                        flush=True,
                    )
                else:
                    bad_val_evals += 1
                    decision = early_stop_decision(
                        bad_val_evals, args.early_stop_patience, step + 1, args.steps
                    )
                    if decision == "stop":
                        progress(
                            {
                                "step": step + 1,
                                "status": "early_stop",
                                "val_loss": val_loss,
                                "best_val_loss": best_val,
                                "elapsed_s": now - started_at,
                            }
                        )
                        stop_reason = (
                            f"val loss did not improve (best={best_val:.6f}, current={val_loss:.6f})"
                        )
                        completed_steps = step + 1
                        break
                    if decision == "ignore":
                        print(
                            "final step: val loss did not improve "
                            f"(best={best_val:.6f}, current={val_loss:.6f})",
                            flush=True,
                        )
        record = {
            "run_id": run_id,
            "step": step + 1,
            "loss": loss_value,
            "val_loss": val_loss,
            "grad_norm": grad_value,
            "changed": changed_count,
            "max_delta": max_delta_value,
            "scale_min": scale_lo,
            "scale_max": scale_hi,
            "step_s": now - step_started,
            "elapsed_s": now - started_at,
        }
        with open(progress_path, "a", encoding="utf-8") as file:
            file.write(json.dumps(record) + "\n")
        val_text = "" if val_loss is None else f"  val_kl={val_loss:.6f}"
        scale_text = "scale=n/a" if not has_scales else f"scale=[{scale_lo:.3g},{scale_hi:.3g}]"
        print(
            f"step {step + 1}/{args.steps}  kl={loss_value:.6f}{val_text}  "
            f"grad={grad_value:.3g}  changed={changed_count}  {scale_text}  "
            f"step_s={now - step_started:.2f}  elapsed_s={now - started_at:.1f}",
            flush=True,
        )
        completed_steps = step + 1
        if args.save_every and (step + 1) % args.save_every == 0 and not args.skip_checkpoint:
            print(f"checkpoint {step + 1} start peak_rss={peak_rss_mb():.0f}MB", flush=True)
            save_checkpoint(step + 1, params, opt_state, rng)
            print(f"checkpoint {step + 1} done  peak_rss={peak_rss_mb():.0f}MB", flush=True)
            last_saved_step = step + 1

    if stop_reason:
        completed_text = f"final step {completed_steps}" if completed_steps >= args.steps else f"step {completed_steps}"
        print(f"early stop at {completed_text}: {stop_reason}", flush=True)

    print(f"training done peak_rss={peak_rss_mb():.0f}MB", flush=True)
    if (
        args.steps > 0
        and completed_steps > 0
        and completed_steps > last_saved_step
        and not args.skip_checkpoint
    ):
        print(f"final checkpoint start peak_rss={peak_rss_mb():.0f}MB", flush=True)
        save_checkpoint(completed_steps, params, opt_state, rng)
        print(f"final checkpoint done  peak_rss={peak_rss_mb():.0f}MB", flush=True)

    print(f"export start peak_rss={peak_rss_mb():.0f}MB", flush=True)
    # A reused output directory can hold the previous run's lalamo backup and
    # encoding. The backup wins in make_lalamo_eval_copy, so a stale one makes
    # the "after" measurement silently read the pre-training weights.
    for stale in (
        "model.safetensors.lalamo.bak",
        "model.safetensors.lalamo.bak.stamp",
        "encoding.json",
    ):
        (args.output_dir / stale).unlink(missing_ok=True)
    if args.master_dtype != "bfloat16":
        params = {key: value.astype(master_dtypes.get(key, value.dtype)) for key, value in params.items()}
    trained = inject_params(student, params)
    del params
    opt_state = None
    try:
        import jax

        jax.clear_caches()
    except Exception:  # noqa: BLE001 - freeing memory must never break export
        pass
    trained = map_nodes_of_type_with_path(
        HybridMatrix,
        lambda path, matrix: matrix.switch_implementation(CompressionImplementation.INFERENCE),
        trained,
    )
    print(f"export injected peak_rss={peak_rss_mb():.0f}MB", flush=True)
    args.output_dir.mkdir(parents=True, exist_ok=True)
    trained.save(args.output_dir)
    print(f"export saved peak_rss={peak_rss_mb():.0f}MB", flush=True)
    rewrite(args.output_dir / "model.safetensors")
    normalize(args.output_dir)
    # uzu refuses a model without encoding.json; it is architectural, so the
    # student's copy is still valid after the weights change.
    encoded = args.student_dir / "encoding.json"
    payload = json.loads(encoded.read_text(encoding="utf-8")) if encoded.exists() else {
        "type": "hanashi",
        "name": "qwen3.5",
    }
    (args.output_dir / "encoding.json").write_text(
        json.dumps(payload, indent=1) + "\n", encoding="utf-8"
    )
    print(f"export done peak_rss={peak_rss_mb():.0f}MB", flush=True)
    print(f"saved QAD model -> {args.output_dir}")


if __name__ == "__main__":
    main()