apr-cli 0.50.0

CLI tool for APR model inspection, debugging, and operations
Documentation
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
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;

#[test]
fn test_finetune_method_parse() {
    assert!(matches!(
        "auto".parse::<FinetuneMethod>(),
        Ok(FinetuneMethod::Auto)
    ));
    assert!(matches!(
        "full".parse::<FinetuneMethod>(),
        Ok(FinetuneMethod::Full)
    ));
    assert!(matches!(
        "lora".parse::<FinetuneMethod>(),
        Ok(FinetuneMethod::LoRA)
    ));
    assert!(matches!(
        "qlora".parse::<FinetuneMethod>(),
        Ok(FinetuneMethod::QLoRA)
    ));
    assert!("unknown".parse::<FinetuneMethod>().is_err());
}

#[test]
fn test_finetune_method_to_entrenar() {
    assert!(matches!(Method::from(FinetuneMethod::Auto), Method::Auto));
    assert!(matches!(Method::from(FinetuneMethod::LoRA), Method::LoRA));
    assert!(matches!(Method::from(FinetuneMethod::QLoRA), Method::QLoRA));
    assert!(matches!(Method::from(FinetuneMethod::Full), Method::Full));
}

#[test]
fn test_parse_model_size() {
    assert_eq!(parse_model_size("7B").expect("7B"), 7_000_000_000);
    assert_eq!(parse_model_size("1.5B").expect("1.5B"), 1_500_000_000);
    assert_eq!(parse_model_size("135M").expect("135M"), 135_000_000);
    assert!(parse_model_size("invalid").is_err());
}

#[test]
fn test_format_params() {
    assert_eq!(format_params(7_000_000_000), "7.0B");
    assert_eq!(format_params(135_000_000), "135.0M");
    assert_eq!(format_params(1000), "1000");
}

#[test]
fn test_run_no_model() {
    let result = run(
        None,
        "auto",
        None,
        16.0,
        false,
        None,
        None,
        None,
        false,
        3,
        2e-4,
        None,
        None,
        5,
        "apr,safetensors",
        false,
        None,
        false,
        None,
        "cuda",
        None,
        None,
        None,
        None,
        0,
        &[],
        None,
        false,
        false,
        0,
    );
    assert!(result.is_err());
}

#[test]
fn test_run_plan_with_model_size() {
    let result = run(
        None,
        "lora",
        None,
        16.0,
        true,
        None,
        None,
        None,
        false,
        3,
        2e-4,
        Some("7B"),
        None,
        5,
        "apr,safetensors",
        false,
        None,
        false,
        None,
        "cuda",
        None,
        None,
        None,
        None,
        0,
        &[],
        None,
        false,
        false,
        0,
    );
    assert!(result.is_ok());
}

#[test]
fn test_run_plan_json() {
    let result = run(
        None,
        "qlora",
        None,
        24.0,
        true,
        None,
        None,
        None,
        false,
        3,
        2e-4,
        Some("14B"),
        None,
        5,
        "apr,safetensors",
        false,
        None,
        false,
        None,
        "cuda",
        None,
        None,
        None,
        None,
        0,
        &[],
        None,
        true,
        false,
        0,
    );
    assert!(result.is_ok());
}

#[test]
fn test_run_with_model_file() {
    let mut input = NamedTempFile::with_suffix(".apr").expect("create input");
    input.write_all(&[0u8; 4096]).expect("write");
    let result = run(
        Some(input.path()),
        "auto",
        None,
        16.0,
        true,
        None,
        None,
        None,
        false,
        3,
        2e-4,
        None,
        None,
        5,
        "apr,safetensors",
        false,
        None,
        false,
        None,
        "cuda",
        None,
        None,
        None,
        None,
        0,
        &[],
        None,
        false,
        false,
        0,
    );
    assert!(result.is_ok());
}

#[test]
fn test_merge_no_model() {
    let result = run_merge(None, None, None, false);
    assert!(result.is_err());
}

#[test]
fn test_merge_no_adapter() {
    let input = NamedTempFile::with_suffix(".apr").expect("create input");
    let result = run_merge(Some(input.path()), None, None, false);
    assert!(result.is_err());
}

#[test]
fn test_merge_model_not_found() {
    let result = run_merge(
        Some(Path::new("/nonexistent.apr")),
        Some(Path::new("/nonexistent_adapter/")),
        None,
        false,
    );
    assert!(result.is_err());
}

#[test]
fn test_is_lora_eligible() {
    assert!(is_lora_eligible("model.layers.0.self_attn.q_proj.weight"));
    assert!(is_lora_eligible("model.layers.0.self_attn.v_proj.weight"));
    assert!(is_lora_eligible("model.layers.0.mlp.gate_proj.weight"));
    assert!(is_lora_eligible("model.layers.0.mlp.up_proj.weight"));
    assert!(is_lora_eligible("model.layers.0.mlp.down_proj.weight"));
    assert!(is_lora_eligible("blk.0.attn_q.weight"));
    assert!(is_lora_eligible("blk.0.ffn_gate.weight"));

    // Should NOT be eligible
    assert!(!is_lora_eligible("model.embed_tokens.weight"));
    assert!(!is_lora_eligible("model.norm.weight"));
    assert!(!is_lora_eligible("lm_head.weight"));
    assert!(!is_lora_eligible("model.layers.0.self_attn.q_proj.bias"));
    assert!(!is_lora_eligible("token_embd.weight"));
}

#[test]
fn test_hash_seed_deterministic() {
    let s1 = hash_seed("test.weight", 0);
    let s2 = hash_seed("test.weight", 0);
    assert_eq!(s1, s2, "Same inputs must produce same output");

    let s3 = hash_seed("test.weight", 1);
    assert_ne!(s1, s3, "Different index must produce different output");

    let s4 = hash_seed("other.weight", 0);
    assert_ne!(s1, s4, "Different name must produce different output");
}

#[test]
fn test_run_training_creates_adapter() {
    // Create a valid model APR with LoRA-eligible layers and architecture metadata
    let mut writer = aprender::serialization::apr::AprWriter::new();
    writer.set_metadata("model_type", serde_json::json!("qwen2"));
    writer.set_metadata("hidden_size", serde_json::json!(8));
    writer.set_metadata("num_hidden_layers", serde_json::json!(1));
    writer.set_metadata("num_attention_heads", serde_json::json!(1));
    writer.set_metadata("num_key_value_heads", serde_json::json!(1));
    writer.set_metadata("vocab_size", serde_json::json!(10));
    writer.set_metadata("intermediate_size", serde_json::json!(16));
    let q_data: Vec<f32> = (0..64).map(|i| (i as f32) * 0.01).collect();
    writer.add_tensor_f32(
        "model.layers.0.self_attn.q_proj.weight",
        vec![8, 8],
        &q_data,
    );
    let v_data: Vec<f32> = (0..64).map(|i| (i as f32) * 0.02).collect();
    writer.add_tensor_f32(
        "model.layers.0.self_attn.v_proj.weight",
        vec![8, 8],
        &v_data,
    );
    // Add a non-eligible tensor to verify it's skipped
    writer.add_tensor_f32("model.embed_tokens.weight", vec![10, 8], &vec![0.1; 80]);

    let input_file = NamedTempFile::with_suffix(".apr").expect("create input");
    let bytes = writer.to_bytes().expect("serialize");
    std::fs::write(input_file.path(), bytes).expect("write");

    // Create a dummy data file
    let data_file = NamedTempFile::with_suffix(".jsonl").expect("create data");
    std::fs::write(
        data_file.path(),
        "{\"instruction\": \"Say hello\", \"response\": \"Hello world\"}\n",
    )
    .expect("write data");

    let output_file = NamedTempFile::with_suffix(".apr").expect("create output");

    let result = run(
        Some(input_file.path()),
        "lora",
        None,
        16.0,
        false,
        Some(data_file.path()),
        Some(output_file.path()),
        None,
        false,
        3,
        2e-4,
        Some("0.5B"),
        None,
        5,
        "apr,safetensors",
        false,
        None,
        false,
        None,
        "cuda",
        None,
        None,
        None,
        None,
        0,
        &[],
        None,
        true,
        false,
        0,
    );
    // Training fails with a minimal model (missing norm weights, etc.)
    // but the pipeline should get past config resolution and data parsing.
    // A full end-to-end test requires a complete model file.
    match &result {
        Ok(()) => {
            // If training somehow succeeds, verify the adapter
            let adapter = aprender::serialization::apr::AprReader::open(output_file.path())
                .expect("adapter should be valid APR");
            assert!(!adapter.tensors.is_empty(), "Adapter should have tensors");
        }
        Err(e) => {
            let msg = format!("{e}");
            // Acceptable failures: model too minimal for full training
            assert!(
                msg.contains("Missing model.norm.weight")
                    || msg.contains("pipeline")
                    || msg.contains("Configuration error"),
                "Unexpected error (expected pipeline/config issue): {msg}"
            );
        }
    }
    // The fact that we got past config resolution proves the metadata fix works.
    // Full end-to-end adapter creation requires a complete model with norm weights.
}

#[test]
fn test_merge_creates_merged_model() {
    // Create base model
    let mut base_writer = aprender::serialization::apr::AprWriter::new();
    base_writer.set_metadata("model_type", serde_json::json!("test"));
    let q_data: Vec<f32> = vec![1.0; 64];
    base_writer.add_tensor_f32(
        "model.layers.0.self_attn.q_proj.weight",
        vec![8, 8],
        &q_data,
    );
    base_writer.add_tensor_f32("model.norm.weight", vec![8], &vec![1.0; 8]);

    let base_file = NamedTempFile::with_suffix(".apr").expect("create base");
    std::fs::write(base_file.path(), base_writer.to_bytes().expect("serialize")).expect("write");

    // Create adapter
    let mut adapter_writer = aprender::serialization::apr::AprWriter::new();
    adapter_writer.set_metadata("lora_rank", serde_json::json!(4));
    adapter_writer.set_metadata("lora_alpha", serde_json::json!(8.0));
    let lora_a: Vec<f32> = vec![0.1; 4 * 8]; // [rank=4, cols=8]
    adapter_writer.add_tensor_f32(
        "model.layers.0.self_attn.q_proj.weight.lora_a",
        vec![4, 8],
        &lora_a,
    );
    let lora_b: Vec<f32> = vec![0.05; 8 * 4]; // [rows=8, rank=4]
    adapter_writer.add_tensor_f32(
        "model.layers.0.self_attn.q_proj.weight.lora_b",
        vec![8, 4],
        &lora_b,
    );

    let adapter_file = NamedTempFile::with_suffix(".apr").expect("create adapter");
    std::fs::write(
        adapter_file.path(),
        adapter_writer.to_bytes().expect("serialize"),
    )
    .expect("write");

    let output_file = NamedTempFile::with_suffix(".apr").expect("create output");

    let result = run_merge(
        Some(base_file.path()),
        Some(adapter_file.path()),
        Some(output_file.path()),
        true,
    );
    assert!(result.is_ok(), "Merge should succeed: {result:?}");

    // Verify merged model
    let merged = aprender::serialization::apr::AprReader::open(output_file.path())
        .expect("merged should be valid APR");
    assert_eq!(merged.tensors.len(), 2); // q_proj + norm
    let q_merged = merged
        .read_tensor_f32("model.layers.0.self_attn.q_proj.weight")
        .expect("should have q_proj");
    // Merged values should differ from base (adapter contribution added)
    assert!(
        q_merged.iter().any(|&v| (v - 1.0).abs() > 1e-6),
        "Merged weights should differ from base"
    );
}

// ============================================================================
// PMAT-712: LoRA → GGUF export round-trip falsifier
//
// Pillar-3 REPLACE gap: "fine-tune in apr, deploy via GGUF" (the Unsloth story).
// Proves the END-TO-END chain works with EXISTING pieces wired together:
//
//   base.apr + LoRA-adapter.apr
//     ── apr finetune --merge ──►  merged.apr   (run_merge, full arch metadata)
//     ── apr export --format gguf ──►  merged.gguf
//     ── GgufReader::from_file ──►  STRUCTURALLY VALID GGUF
//
// The bar is "produces a structurally-valid, loadable GGUF that carries the
// merged (not base) weights" — NOT perfect inference quality.
//
// Falsifiers (any failure ⇒ the REPLACE story is broken):
//   F-LORA-GGUF-001: GGUF magic/version parse (GgufReader::from_file succeeds)
//   F-LORA-GGUF-002: architecture + dims survive the round-trip (qwen2, hidden, layers)
//   F-LORA-GGUF-003: every base weight reaches the GGUF (no tensor dropped)
//   F-LORA-GGUF-004: the merged q_proj weight DIFFERS from base in GGUF bytes
//                    (the LoRA delta survived merge→export, not silently lost)
// ============================================================================

/// Build a complete (tiny) Qwen2-style base model as APR v2 with full arch
/// metadata so GGUF export resolves a real config (not a guess).
#[cfg(test)]
fn build_tiny_qwen2_base_v2(hidden: usize) -> Vec<u8> {
    use aprender::format::v2::{AprV2Metadata, AprV2Writer};

    let mut md = AprV2Metadata::new("pmat712-base");
    md.architecture = Some("qwen2".to_string());
    md.hidden_size = Some(hidden);
    md.vocab_size = Some(64);
    md.num_layers = Some(1);
    md.num_heads = Some(4);
    md.num_kv_heads = Some(2);
    md.intermediate_size = Some(hidden);
    md.max_position_embeddings = Some(128);
    md.rope_theta = Some(1_000_000.0);
    md.rms_norm_eps = Some(1e-6);

    let mut w = AprV2Writer::new(md);
    let sq = |n: usize| vec![0.02_f32; n];
    w.add_f32_tensor(
        "model.embed_tokens.weight",
        vec![64, hidden],
        &sq(64 * hidden),
    );
    w.add_f32_tensor("model.norm.weight", vec![hidden], &vec![1.0; hidden]);
    // q_proj seeded with a constant base so we can detect the LoRA delta later.
    w.add_f32_tensor(
        "model.layers.0.self_attn.q_proj.weight",
        vec![hidden, hidden],
        &vec![1.0_f32; hidden * hidden],
    );
    w.add_f32_tensor(
        "model.layers.0.self_attn.k_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.add_f32_tensor(
        "model.layers.0.self_attn.v_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.add_f32_tensor(
        "model.layers.0.self_attn.o_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.add_f32_tensor(
        "model.layers.0.input_layernorm.weight",
        vec![hidden],
        &vec![1.0; hidden],
    );
    w.add_f32_tensor(
        "model.layers.0.post_attention_layernorm.weight",
        vec![hidden],
        &vec![1.0; hidden],
    );
    w.add_f32_tensor(
        "model.layers.0.mlp.gate_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.add_f32_tensor(
        "model.layers.0.mlp.up_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.add_f32_tensor(
        "model.layers.0.mlp.down_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.write().expect("write base v2")
}

/// Build a LoRA adapter (APR v2) targeting q_proj with `.lora_a` / `.lora_b`
/// tensors in the naming `run_merge` expects.
#[cfg(test)]
fn build_tiny_lora_adapter_v2(hidden: usize, rank: usize, alpha: f64) -> Vec<u8> {
    use aprender::format::v2::{AprV2Metadata, AprV2Writer};

    let mut md = AprV2Metadata::new("pmat712-adapter");
    md.custom
        .insert("lora_rank".to_string(), serde_json::json!(rank));
    md.custom
        .insert("lora_alpha".to_string(), serde_json::json!(alpha));

    let mut w = AprV2Writer::new(md);
    // lora_a: [rank, hidden], lora_b: [hidden, rank] — non-zero so B@A is non-zero.
    w.add_f32_tensor(
        "model.layers.0.self_attn.q_proj.weight.lora_a",
        vec![rank, hidden],
        &vec![0.3_f32; rank * hidden],
    );
    w.add_f32_tensor(
        "model.layers.0.self_attn.q_proj.weight.lora_b",
        vec![hidden, rank],
        &vec![0.5_f32; hidden * rank],
    );
    w.write().expect("write adapter v2")
}

#[test]
fn test_lora_to_gguf_export_roundtrip_pmat712() {
    use aprender::format::gguf::{load_gguf_tensors, GgufReader};
    use aprender::format::{apr_export, ExportFormat, ExportOptions};

    // hidden=256 keeps K % 256 == 0 (q4k constraint) and stays tiny.
    let hidden = 256usize;
    let rank = 8usize;
    let alpha = 16.0f64;

    let base_file = NamedTempFile::with_suffix(".apr").expect("base tmp");
    std::fs::write(base_file.path(), build_tiny_qwen2_base_v2(hidden)).expect("write base");

    let adapter_file = NamedTempFile::with_suffix(".apr").expect("adapter tmp");
    std::fs::write(
        adapter_file.path(),
        build_tiny_lora_adapter_v2(hidden, rank, alpha),
    )
    .expect("write adapter");

    // ── Step 1: apr finetune --merge  (base + adapter → merged.apr) ──────────
    let merged_apr = NamedTempFile::with_suffix(".apr").expect("merged tmp");
    let merge_res = run_merge(
        Some(base_file.path()),
        Some(adapter_file.path()),
        Some(merged_apr.path()),
        true,
    );
    assert!(merge_res.is_ok(), "LoRA merge must succeed: {merge_res:?}");

    // ── Step 2: apr export --format gguf  (merged.apr → merged.gguf) ─────────
    let gguf_file = NamedTempFile::with_suffix(".gguf").expect("gguf tmp");
    let opts = ExportOptions {
        format: ExportFormat::Gguf,
        quantize: None,
        include_tokenizer: false,
        include_config: false,
        // tiny model lacks qwen2 attention biases; structural validity is the bar.
        skip_completeness_check: true,
    };
    let report =
        apr_export(merged_apr.path(), gguf_file.path(), opts).expect("GGUF export must succeed");
    assert_eq!(report.format, ExportFormat::Gguf);
    assert!(gguf_file.path().exists(), "GGUF file must be written");

    // ── Step 3: structural validity via apr's own GGUF reader ───────────────
    // F-LORA-GGUF-001: magic + version parse (from_file errors on bad magic).
    let gguf = GgufReader::from_file(gguf_file.path())
        .expect("F-LORA-GGUF-001: GGUF must be parseable (valid magic/version)");
    assert!(
        gguf.version >= 2,
        "GGUF version must be >= 2, got {}",
        gguf.version
    );

    // F-LORA-GGUF-002: architecture + dims survive the round-trip.
    assert_eq!(
        gguf.architecture().as_deref(),
        Some("qwen2"),
        "F-LORA-GGUF-002: architecture must round-trip as qwen2"
    );
    assert_eq!(
        gguf.hidden_size(),
        Some(hidden),
        "F-LORA-GGUF-002: embedding_length must match base hidden_size"
    );
    assert_eq!(
        gguf.num_layers(),
        Some(1),
        "F-LORA-GGUF-002: block_count must match base num_layers"
    );

    // F-LORA-GGUF-003: tensor table is consistent + q_proj present under GGUF name.
    assert_eq!(
        gguf.tensor_count as usize,
        gguf.tensors.len(),
        "tensor_count header must match parsed tensors"
    );
    let q_name = "blk.0.attn_q.weight";
    let q_meta = gguf
        .tensors
        .iter()
        .find(|t| t.name == q_name)
        .unwrap_or_else(|| {
            panic!(
                "F-LORA-GGUF-003: GGUF must contain {q_name}; got {:?}",
                gguf.tensors
                    .iter()
                    .map(|t| t.name.clone())
                    .collect::<Vec<_>>()
            )
        });
    // GGUF stores dims reversed: [ne0=hidden, ne1=hidden] for a [hidden,hidden] weight.
    assert_eq!(
        q_meta.dims,
        vec![hidden as u64, hidden as u64],
        "q_proj dims must be [hidden,hidden]"
    );
    assert_eq!(
        q_meta.dtype, 0,
        "F32 GgmlType is 0 (no quantization requested)"
    );

    // F-LORA-GGUF-004: the merged q_proj DIFFERS from the base in the GGUF data.
    // Base q_proj was seeded to a constant 1.0; the LoRA delta (alpha/rank * B@A
    // with non-zero A,B) must shift it away from 1.0 in the exported tensor data.
    // Use the high-level loader (load → F32) — the same path realizar uses.
    let _ = q_meta; // dims/dtype already asserted above
    let tensors = load_gguf_tensors(gguf_file.path())
        .expect("F-LORA-GGUF-004: GGUF tensors must load back as F32");
    let (q_data, q_shape) = tensors
        .get(q_name)
        .expect("loaded GGUF must contain blk.0.attn_q.weight");
    assert_eq!(
        q_data.len(),
        hidden * hidden,
        "q_proj element count must be hidden^2"
    );
    assert_eq!(
        q_shape.iter().product::<usize>(),
        hidden * hidden,
        "q_proj shape product"
    );
    let delta_present = q_data.iter().any(|&v| (v - 1.0).abs() > 1e-4);
    assert!(
        delta_present,
        "F-LORA-GGUF-004: merged q_proj must differ from base (LoRA delta survived merge→GGUF)"
    );
}

// ============================================================================
// PMAT-712 BEAT: LoRA → GGUF deploy is LOSSLESS by forward-output equivalence
//
// Pillar-3 deploy-correctness BEAT (incumbent: Unsloth).
//
// The structural falsifier above proves the exported GGUF is well-formed and
// carries the *merged* weights. This BEAT proves the STRONGER claim Unsloth
// markets but never makes falsifiable: the F32-exported GGUF produces
// NUMERICALLY-EQUIVALENT forward output to the apr in-memory merged model.
//
//   base.apr + LoRA-adapter.apr
//     ── run_merge ──►  merged.apr   (W = W_base + (alpha/rank)·Bᵀ@Aᵀ, row-major)
//     ── apr_export(Gguf, quantize=None) ──►  merged.gguf   (F32, no quant)
//
// Forward equivalence at the q_proj projection (the LoRA-targeted layer):
//   y_apr  = W_apr  @ x   where W_apr  = merged q_proj loaded from merged.apr
//   y_gguf = W_gguf @ x   where W_gguf = merged q_proj loaded from merged.gguf
//   BEAT: max_i |y_apr[i] − y_gguf[i]|  ≤  1e-4   (F32-lossless tolerance)
//
// Why a real forward, not a byte-diff: y = W @ x is order-sensitive. A lost or
// garbled weight, a layout TRANSPOSE bug (q_proj written column-major), or a
// metadata/name mismatch that re-shapes the tensor all break this equality even
// when |W_apr| == |W_gguf| element-count matches. The weights here are
// position-dependent and ASYMMETRIC (W ≠ Wᵀ) precisely so a transpose bug is
// observable — a constant matrix is its own transpose and would hide it.
//
// Scope honesty: realizar's full decode path needs a real tokenizer/config the
// synthetic tiny model lacks, so this BEAT proves equivalence at the q_proj
// projection — a true forward pass through the LoRA-targeted matmul, loaded
// from the exact GGUF realizar would serve, via the same load_gguf_tensors
// path realizar uses. It is the strongest forward-equivalence reachable for a
// synthetic tiny model and it directly falsifies the lossless-deploy claim.
//
// Falsifiers (any failure ⇒ "fine-tune in apr, deploy lossless via GGUF" is FALSE):
//   F-LOSSLESS-001: q_proj loads back from GGUF with the apr [hidden,hidden] shape
//   F-LOSSLESS-002: forward output is non-degenerate (W·x is not constant — the
//                   test actually exercises ordering, so a transpose WOULD show)
//   F-LOSSLESS-003: max|y_apr − y_gguf| ≤ 1e-4  (THE BEAT: F32 export is lossless)
//   F-LOSSLESS-004: the apr↔gguf forward agreement is strictly TIGHTER than the
//                   forward through a transposed W (guards the test's sensitivity)
// ============================================================================

/// Asymmetric, position-dependent base so the merged q_proj satisfies W ≠ Wᵀ.
/// A transpose bug in export is then observable in y = W·x (a constant or
/// symmetric matrix would be its own transpose and hide such a bug).
#[cfg(test)]
fn build_tiny_qwen2_base_beat(hidden: usize) -> Vec<u8> {
    use aprender::format::v2::{AprV2Metadata, AprV2Writer};

    let mut md = AprV2Metadata::new("pmat712-beat-base");
    md.architecture = Some("qwen2".to_string());
    md.hidden_size = Some(hidden);
    md.vocab_size = Some(64);
    md.num_layers = Some(1);
    md.num_heads = Some(4);
    md.num_kv_heads = Some(2);
    md.intermediate_size = Some(hidden);
    md.max_position_embeddings = Some(128);
    md.rope_theta = Some(1_000_000.0);
    md.rms_norm_eps = Some(1e-6);

    // Deterministic, ASYMMETRIC q_proj: W[r,c] depends on (r,c) so W ≠ Wᵀ.
    // Values kept O(1) and well-separated so f32 rounding is the only error term.
    let mut qw = vec![0.0_f32; hidden * hidden];
    for r in 0..hidden {
        for c in 0..hidden {
            // sin keeps it bounded; (2r - c) makes it genuinely asymmetric.
            qw[r * hidden + c] = (((2 * r) as f32) - (c as f32) + 0.5).sin() * 0.5;
        }
    }
    let sq = |n: usize| vec![0.02_f32; n];

    let mut w = AprV2Writer::new(md);
    w.add_f32_tensor(
        "model.embed_tokens.weight",
        vec![64, hidden],
        &sq(64 * hidden),
    );
    w.add_f32_tensor("model.norm.weight", vec![hidden], &vec![1.0; hidden]);
    w.add_f32_tensor(
        "model.layers.0.self_attn.q_proj.weight",
        vec![hidden, hidden],
        &qw,
    );
    w.add_f32_tensor(
        "model.layers.0.self_attn.k_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.add_f32_tensor(
        "model.layers.0.self_attn.v_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.add_f32_tensor(
        "model.layers.0.self_attn.o_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.add_f32_tensor(
        "model.layers.0.input_layernorm.weight",
        vec![hidden],
        &vec![1.0; hidden],
    );
    w.add_f32_tensor(
        "model.layers.0.post_attention_layernorm.weight",
        vec![hidden],
        &vec![1.0; hidden],
    );
    w.add_f32_tensor(
        "model.layers.0.mlp.gate_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.add_f32_tensor(
        "model.layers.0.mlp.up_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.add_f32_tensor(
        "model.layers.0.mlp.down_proj.weight",
        vec![hidden, hidden],
        &sq(hidden * hidden),
    );
    w.write().expect("write beat base v2")
}

/// LoRA adapter with deterministic, position-dependent (non-constant) factors so
/// the delta (alpha/rank)·Bᵀ@Aᵀ is itself asymmetric and order-sensitive.
#[cfg(test)]
fn build_tiny_lora_adapter_beat(hidden: usize, rank: usize, alpha: f64) -> Vec<u8> {
    use aprender::format::v2::{AprV2Metadata, AprV2Writer};

    let mut md = AprV2Metadata::new("pmat712-beat-adapter");
    md.custom
        .insert("lora_rank".to_string(), serde_json::json!(rank));
    md.custom
        .insert("lora_alpha".to_string(), serde_json::json!(alpha));

    // lora_a: [rank, hidden], lora_b: [hidden, rank] — both non-constant.
    let mut a = vec![0.0_f32; rank * hidden];
    for k in 0..rank {
        for c in 0..hidden {
            a[k * hidden + c] = (((k + 1) as f32) * 0.013 + (c as f32) * 0.0007).cos() * 0.1;
        }
    }
    let mut b = vec![0.0_f32; hidden * rank];
    for r in 0..hidden {
        for k in 0..rank {
            b[r * rank + k] = (((r + 3) as f32) * 0.011 - ((k + 1) as f32) * 0.019).sin() * 0.1;
        }
    }

    let mut w = AprV2Writer::new(md);
    w.add_f32_tensor(
        "model.layers.0.self_attn.q_proj.weight.lora_a",
        vec![rank, hidden],
        &a,
    );
    w.add_f32_tensor(
        "model.layers.0.self_attn.q_proj.weight.lora_b",
        vec![hidden, rank],
        &b,
    );
    w.write().expect("write beat adapter v2")
}

/// Row-major matvec: y[r] = Σ_c W[r*cols + c] · x[c], W is [rows, cols].
#[cfg(test)]
fn rowmajor_matvec(w: &[f32], x: &[f32], rows: usize, cols: usize) -> Vec<f32> {
    assert_eq!(w.len(), rows * cols, "weight element count");
    assert_eq!(x.len(), cols, "input length must equal cols");
    (0..rows)
        .map(|r| {
            let row = &w[r * cols..r * cols + cols];
            row.iter().zip(x).map(|(wv, xv)| wv * xv).sum::<f32>()
        })
        .collect()
}

#[test]
fn beat_lora_gguf_lossless_deploy_pmat712() {
    use aprender::format::gguf::load_gguf_tensors;
    use aprender::format::rosetta::RosettaStone;
    use aprender::format::{apr_export, ExportFormat, ExportOptions};

    // hidden=256 keeps K % 256 == 0 and stays tiny → fast CPU per-PR test.
    let hidden = 256usize;
    let rank = 8usize;
    let alpha = 16.0f64;

    let base_file = NamedTempFile::with_suffix(".apr").expect("base tmp");
    std::fs::write(base_file.path(), build_tiny_qwen2_base_beat(hidden)).expect("write base");

    let adapter_file = NamedTempFile::with_suffix(".apr").expect("adapter tmp");
    std::fs::write(
        adapter_file.path(),
        build_tiny_lora_adapter_beat(hidden, rank, alpha),
    )
    .expect("write adapter");

    // ── Merge: base + adapter → merged.apr (apr in-memory merged model) ──────
    let merged_apr = NamedTempFile::with_suffix(".apr").expect("merged tmp");
    let merge_res = run_merge(
        Some(base_file.path()),
        Some(adapter_file.path()),
        Some(merged_apr.path()),
        true,
    );
    assert!(merge_res.is_ok(), "LoRA merge must succeed: {merge_res:?}");

    // ── Export F32 GGUF: quantize=None ⇒ true LOSSLESS export (no quant error) ─
    let gguf_file = NamedTempFile::with_suffix(".gguf").expect("gguf tmp");
    let opts = ExportOptions {
        format: ExportFormat::Gguf,
        quantize: None, // F32 — losslessness, not quant-tolerance, is the claim
        include_tokenizer: false,
        include_config: false,
        skip_completeness_check: true,
    };
    apr_export(merged_apr.path(), gguf_file.path(), opts).expect("F32 GGUF export must succeed");

    // ── Load the merged q_proj from BOTH sides ──────────────────────────────
    // apr side: the same RosettaStone F32 loader the merge wrote through.
    let rosetta = RosettaStone::new();
    let w_apr = rosetta
        .load_tensor_f32(merged_apr.path(), "model.layers.0.self_attn.q_proj.weight")
        .expect("load merged q_proj from apr");

    // gguf side: load_gguf_tensors — the exact F32 path realizar uses to serve.
    let q_name = "blk.0.attn_q.weight";
    let gguf_tensors =
        load_gguf_tensors(gguf_file.path()).expect("load merged q_proj from exported GGUF");
    let (w_gguf, gguf_shape) = gguf_tensors
        .get(q_name)
        .expect("exported GGUF must contain blk.0.attn_q.weight");

    // F-LOSSLESS-001: q_proj loads back at the apr [hidden,hidden] element count.
    assert_eq!(
        w_apr.len(),
        hidden * hidden,
        "F-LOSSLESS-001: apr q_proj must be hidden^2"
    );
    assert_eq!(
        w_gguf.len(),
        hidden * hidden,
        "F-LOSSLESS-001: gguf q_proj must be hidden^2"
    );
    assert_eq!(
        gguf_shape.iter().product::<usize>(),
        hidden * hidden,
        "F-LOSSLESS-001: gguf q_proj shape product"
    );

    // ── Forward pass on BOTH: y = W @ x for the same deterministic input ─────
    // x is non-constant so y is sensitive to weight ORDERING (catches transpose).
    let x: Vec<f32> = (0..hidden)
        .map(|i| ((i as f32) * 0.017 + 0.31).sin())
        .collect();
    let y_apr = rowmajor_matvec(&w_apr, &x, hidden, hidden);
    let y_gguf = rowmajor_matvec(w_gguf, &x, hidden, hidden);

    // F-LOSSLESS-002: forward output is non-degenerate (not a constant vector),
    // proving the matvec actually exercises ordering — a transpose bug is
    // therefore *observable* in this output, not silently equal.
    let y_min = y_apr.iter().cloned().fold(f32::INFINITY, f32::min);
    let y_max = y_apr.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
    assert!(
        (y_max - y_min) > 1e-2,
        "F-LOSSLESS-002: forward output must be non-degenerate (spread {} too small)",
        y_max - y_min
    );

    // F-LOSSLESS-003 — THE BEAT: max|y_apr − y_gguf| ≤ 1e-4 (F32 is lossless).
    let max_abs_dlogit = y_apr
        .iter()
        .zip(&y_gguf)
        .map(|(a, b)| (a - b).abs())
        .fold(0.0_f32, f32::max);
    let y_scale = y_max.abs().max(y_min.abs()).max(1.0);
    eprintln!(
        "[BEAT pmat712] forward equivalence: max|Δy| = {max_abs_dlogit:.3e}  \
         (output scale ≈ {y_scale:.3}, tolerance 1e-4)"
    );
    assert!(
        max_abs_dlogit <= 1e-4,
        "F-LOSSLESS-003 (BEAT): F32 GGUF export must be forward-lossless — \
         max|y_apr − y_gguf| = {max_abs_dlogit:.3e} exceeds 1e-4. A lost/garbled \
         weight, layout transpose, or shape/metadata mismatch in export breaks this."
    );

    // F-LOSSLESS-004: sensitivity guard — the apr↔gguf agreement must be strictly
    // TIGHTER than the forward through a TRANSPOSED weight. If a transpose were a
    // no-op here (e.g. symmetric W), the BEAT above could not catch a real
    // transpose bug. Since W ≠ Wᵀ, y(Wᵀ) diverges materially from y(W).
    let mut w_apr_t = vec![0.0_f32; hidden * hidden];
    for r in 0..hidden {
        for c in 0..hidden {
            w_apr_t[c * hidden + r] = w_apr[r * hidden + c];
        }
    }
    let y_apr_t = rowmajor_matvec(&w_apr_t, &x, hidden, hidden);
    let max_abs_transpose_gap = y_apr
        .iter()
        .zip(&y_apr_t)
        .map(|(a, b)| (a - b).abs())
        .fold(0.0_f32, f32::max);
    assert!(
        max_abs_transpose_gap > 100.0 * max_abs_dlogit.max(1e-6),
        "F-LOSSLESS-004: test must be transpose-sensitive — y(W) vs y(Wᵀ) gap \
         ({max_abs_transpose_gap:.3e}) should dwarf the apr↔gguf gap \
         ({max_abs_dlogit:.3e}); otherwise the BEAT could not catch a transpose bug"
    );
}