aprender-serve 0.64.0

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors
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

#[test]
fn test_to_apr_bytes_metadata_contains_architecture() {
    let model = build_minimal_owned_quantized_model();
    let bytes = model.to_apr_bytes().expect("should produce bytes");

    // Metadata starts at offset 64 (HEADER_SIZE)
    let metadata_size = u32::from_le_bytes([bytes[20], bytes[21], bytes[22], bytes[23]]) as usize;
    let metadata_slice = &bytes[64..64 + metadata_size];
    let metadata_str = String::from_utf8_lossy(metadata_slice);

    assert!(
        metadata_str.contains("\"architecture\":\"llama\""),
        "Metadata should contain architecture: {metadata_str}"
    );
    assert!(
        metadata_str.contains("\"hidden_size\":8"),
        "Metadata should contain hidden_size"
    );
}

// ============================================================================
// to_apr_bytes: Multi-layer model
// ============================================================================

#[test]
fn test_to_apr_bytes_multi_layer() {
    let mut model = build_minimal_owned_quantized_model();
    // Add a second layer
    let second_layer = model.layers[0].clone();
    model.layers.push(second_layer);
    model.config.num_layers = 2;

    let bytes = model.to_apr_bytes().expect("should produce bytes");

    let bytes_str = String::from_utf8_lossy(&bytes);
    assert!(
        bytes_str.contains("blk.0.attn_q.weight"),
        "Should have layer 0"
    );
    assert!(
        bytes_str.contains("blk.1.attn_q.weight"),
        "Should have layer 1"
    );
}

// ============================================================================
// to_apr_bytes: Empty model (zero layers)
// ============================================================================

#[test]
fn test_to_apr_bytes_zero_layers() {
    let mut model = build_minimal_owned_quantized_model();
    model.layers.clear();
    model.config.num_layers = 0;

    let result = model.to_apr_bytes();
    assert!(
        result.is_ok(),
        "Zero-layer model should serialize: {:?}",
        result.err()
    );
}

// ============================================================================
// to_apr_bytes -> from_apr roundtrip
// ============================================================================

#[test]
fn test_to_apr_bytes_roundtrip_via_mapped_model() {
    let model = build_minimal_owned_quantized_model();
    let apr_bytes = model.to_apr_bytes().expect("should produce bytes");

    // Write to a temp file and load via MappedAprModel
    let dir = std::env::temp_dir();
    let path = dir.join("test_roundtrip_loader.apr");
    std::fs::write(&path, &apr_bytes).expect("should write file");

    let mapped = crate::apr::MappedAprModel::from_path(&path);
    assert!(
        mapped.is_ok(),
        "MappedAprModel should load: {:?}",
        mapped.err()
    );

    let mapped = mapped.expect("should load");

    // Verify metadata
    assert_eq!(mapped.metadata.architecture.as_deref(), Some("llama"));
    assert_eq!(mapped.metadata.hidden_size, Some(8));
    assert_eq!(mapped.metadata.num_layers, Some(1));

    // Verify tensor count (should match what we wrote)
    assert!(
        mapped.tensor_count() > 0,
        "Should have tensors loaded from index"
    );

    // Now load from_apr
    let restored = OwnedQuantizedModel::from_apr(&mapped);
    assert!(
        restored.is_ok(),
        "from_apr should succeed: {:?}",
        restored.err()
    );

    let restored = restored.expect("should restore model");
    assert_eq!(restored.config.architecture, "llama");
    assert_eq!(restored.config.hidden_dim, 8);
    assert_eq!(restored.config.num_layers, 1);
    assert_eq!(restored.config.num_heads, 2);
    assert_eq!(restored.config.num_kv_heads, 2);
    assert_eq!(restored.layers.len(), 1);
    assert!(!restored.token_embedding.is_empty());
    assert!(!restored.output_norm_weight.is_empty());

    // Clean up
    let _ = std::fs::remove_file(&path);
}

#[test]
fn test_to_apr_bytes_roundtrip_q4k() {
    let model = build_q4k_model();
    let apr_bytes = model.to_apr_bytes().expect("should produce bytes");

    let dir = std::env::temp_dir();
    let path = dir.join("test_roundtrip_q4k_loader.apr");
    std::fs::write(&path, &apr_bytes).expect("should write file");

    let mapped = crate::apr::MappedAprModel::from_path(&path).expect("should load");

    // Verify tensors have correct dtype
    let q_tensor = mapped.find_tensor("blk.0.attn_q.weight");
    assert!(q_tensor.is_some(), "Should find Q tensor");
    assert_eq!(
        q_tensor.expect("Q tensor").dtype,
        "Q4_K",
        "Q tensor should be Q4_K"
    );

    let lm_tensor = mapped.find_tensor("output.weight");
    assert!(lm_tensor.is_some(), "Should find lm_head tensor");
    assert_eq!(
        lm_tensor.expect("lm_head tensor").dtype,
        "Q6_K",
        "lm_head should be Q6_K"
    );

    // Roundtrip from_apr
    let restored = OwnedQuantizedModel::from_apr(&mapped);
    assert!(
        restored.is_ok(),
        "from_apr should succeed for Q4K model: {:?}",
        restored.err()
    );

    let restored = restored.expect("should restore");
    assert_eq!(restored.config.architecture, "qwen2");
    assert_eq!(restored.config.hidden_dim, 8);

    // Clean up
    let _ = std::fs::remove_file(&path);
}

#[test]
fn test_to_apr_bytes_roundtrip_fused_qkv() {
    let model = build_fused_qkv_model();
    let apr_bytes = model.to_apr_bytes().expect("should produce bytes");

    let dir = std::env::temp_dir();
    let path = dir.join("test_roundtrip_fused_qkv_loader.apr");
    std::fs::write(&path, &apr_bytes).expect("should write file");

    let mapped = crate::apr::MappedAprModel::from_path(&path).expect("should load");

    // Fused QKV should produce a single "blk.0.attn_qkv.weight" tensor
    let fused = mapped.find_tensor("blk.0.attn_qkv.weight");
    assert!(fused.is_some(), "Should find fused QKV tensor");

    let _ = std::fs::remove_file(&path);
}

// ============================================================================
// to_apr_bytes: Output includes all expected tensor names
// ============================================================================

#[test]
fn test_to_apr_bytes_all_tensor_names_separate_qkv() {
    let model = build_minimal_owned_quantized_model();
    let bytes = model.to_apr_bytes().expect("should produce bytes");
    let bytes_str = String::from_utf8_lossy(&bytes);

    let expected_tensors = [
        "token_embd.weight",
        "blk.0.attn_norm.weight",
        "blk.0.attn_q.weight",
        "blk.0.attn_k.weight",
        "blk.0.attn_v.weight",
        "blk.0.attn_output.weight",
        "blk.0.ffn_norm.weight",
        "blk.0.ffn_gate.weight",
        "blk.0.ffn_up.weight",
        "blk.0.ffn_down.weight",
        "output_norm.weight",
        "output.weight",
    ];

    for name in &expected_tensors {
        assert!(
            bytes_str.contains(name),
            "Missing tensor name in APR output: {name}"
        );
    }
}

// ============================================================================
// to_apr_bytes: Various qtype mappings
// ============================================================================

#[test]
fn test_to_apr_bytes_various_qtypes() {
    // Build a model with various quantization types to exercise qtype_to_dtype paths
    let hidden_dim = 8;

    fn make_tensor_with_qtype(in_dim: usize, out_dim: usize, qtype: u32) -> OwnedQuantizedTensor {
        // Size doesn't matter for header serialization test; use small data
        OwnedQuantizedTensor {
            data: vec![0u8; 64],
            in_dim,
            out_dim,
            qtype,
        }
    }

    // Test various qtypes: F16(1), Q4_0(2), Q4_1(3), Q5_0(6), Q5_1(7), Q8_0(8), Q8_1(9),
    // Q2_K(10), Q3_K(11), Q4_K(12), Q5_K(13), Q6_K(14), IQ2_XXS(16), IQ2_XS(17), BF16(30)
    let qtypes_to_test = [1u32, 2, 3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 30, 99];

    for qtype in qtypes_to_test {
        let config = GGUFConfig {
            architecture: "test".to_string(),
            constraints: crate::gguf::ArchConstraints::from_architecture("test"),
            hidden_dim,
            num_layers: 1,
            num_heads: 2,
            num_kv_heads: 2,
            vocab_size: 10,
            intermediate_dim: 16,
            context_length: 32,
            rope_theta: 10000.0,
            eps: 1e-5,
            rope_type: 0,
            explicit_head_dim: None,
            query_pre_attn_scalar: None,
            bos_token_id: None,
            eos_token_id: None,
        };

        let layer = OwnedQuantizedLayer {
            attn_norm_weight: vec![1.0; hidden_dim],
            attn_norm_bias: None,
            qkv_weight: OwnedQKVWeights::Separate {
                q: make_tensor_with_qtype(hidden_dim, hidden_dim, qtype),
                k: make_tensor_with_qtype(hidden_dim, hidden_dim, qtype),
                v: make_tensor_with_qtype(hidden_dim, hidden_dim, qtype),
            },
            qkv_bias: None,
            attn_output_weight: make_tensor_with_qtype(hidden_dim, hidden_dim, qtype),
            attn_output_bias: None,
            ffn_up_weight: make_tensor_with_qtype(hidden_dim, 16, qtype),
            ffn_up_bias: None,
            ffn_down_weight: make_tensor_with_qtype(16, hidden_dim, qtype),
            ffn_down_bias: None,
            ffn_gate_weight: None,
            ffn_gate_bias: None,
            ffn_norm_weight: None,
            ffn_norm_bias: None,
            attn_q_norm_weight: None,
            attn_k_norm_weight: None,
            post_attn_norm_weight: None,
            post_ffw_norm_weight: None,
        };

        let model = OwnedQuantizedModel {
            config,
            token_embedding: vec![0.0f32; 80],
            position_embedding: None,
            layers: vec![layer],
            encoder_layers: vec![],
            encoder_output_norm_weight: None,
            encoder_output_norm_bias: None,
            output_norm_weight: vec![1.0; hidden_dim],
            output_norm_bias: None,
            lm_head_weight: make_tensor_with_qtype(hidden_dim, 10, qtype),
            lm_head_bias: None,
            #[cfg(feature = "cuda")]
            cuda_executor: None,
            #[cfg(feature = "cuda")]
            cuda_kernel_count: std::sync::atomic::AtomicU64::new(0),
            #[cfg(feature = "cuda")]
            cached_weight_names: std::sync::Mutex::new(std::collections::HashSet::new()),
        };

        let result = model.to_apr_bytes();
        // qtype 99 is not a GGML quant type. This used to assert `is_ok()` for
        // EVERY value in the list including 99 -- pinning the defect in place:
        // `apr_qtype_to_dtype` mapped an unknown qtype to "F32" and the writer
        // emitted quantized bytes under that label, so the file was structurally
        // valid and its weights were garbage. The test asserting is_ok() on an
        // invalid input is what made that permanent.
        if crate::gguf::GgmlQuantType::from_id(qtype).is_some() {
            assert!(
                result.is_ok(),
                "to_apr_bytes should succeed for known qtype {}: {:?}",
                qtype,
                result.err()
            );
        } else {
            let err = result
                .map(|b| format!("wrote {} bytes", b.len()))
                .expect_err(&format!(
                    "to_apr_bytes must REFUSE unknown qtype {qtype} rather than label it F32"
                ));
            assert!(
                err.to_string().contains(&qtype.to_string()),
                "the error must name the offending qtype: {err}"
            );
        }
    }
}

// ============================================================================
// to_apr_bytes: Data size validation
// ============================================================================

#[test]
fn test_to_apr_bytes_total_size_reasonable() {
    let model = build_minimal_owned_quantized_model();
    let bytes = model.to_apr_bytes().expect("should produce bytes");

    // Must be larger than just header
    assert!(bytes.len() > 64, "Must be larger than header");

    // Must not be excessively large for tiny model
    assert!(
        bytes.len() < 100_000,
        "Tiny model should be < 100KB, got {} bytes",
        bytes.len()
    );
}

// ============================================================================
// from_apr: Error paths
// ============================================================================

#[test]
fn test_from_apr_missing_embedding_tensor() {
    // Build an APR file with no embedding tensor
    use crate::apr::{HEADER_SIZE, MAGIC};

    let metadata = r#"{"architecture":"llama","hidden_size":8,"num_layers":1,"num_heads":2,"num_kv_heads":2,"vocab_size":10,"intermediate_size":16,"rms_norm_eps":1e-6}"#;
    let metadata_bytes = metadata.as_bytes();
    let metadata_padded_len = metadata_bytes.len().div_ceil(64) * 64;

    // Empty tensor index (no tensors)
    let tensor_index_bytes: Vec<u8> = Vec::new();

    let metadata_offset = HEADER_SIZE as u64;
    let tensor_index_offset = metadata_offset + metadata_padded_len as u64;
    let data_offset = tensor_index_offset + tensor_index_bytes.len() as u64;

    let mut header = vec![0u8; HEADER_SIZE];
    header[0..4].copy_from_slice(&MAGIC);
    header[4] = 2;
    header[5] = 0;
    header[8..12].copy_from_slice(&0u32.to_le_bytes());
    header[12..20].copy_from_slice(&metadata_offset.to_le_bytes());
    header[20..24].copy_from_slice(&(metadata_bytes.len() as u32).to_le_bytes());
    header[24..32].copy_from_slice(&tensor_index_offset.to_le_bytes());
    header[32..40].copy_from_slice(&data_offset.to_le_bytes());

    let total_size = HEADER_SIZE + metadata_padded_len;
    let mut data = Vec::with_capacity(total_size);
    data.extend_from_slice(&header);
    data.extend_from_slice(metadata_bytes);
    data.resize(total_size, 0);

    let dir = std::env::temp_dir();
    let path = dir.join("test_missing_embed.apr");
    std::fs::write(&path, &data).expect("should write");

    let mapped = crate::apr::MappedAprModel::from_path(&path);
    if let Ok(mapped) = mapped {
        let result = OwnedQuantizedModel::from_apr(&mapped);
        // Should fail because embedding tensor is missing
        assert!(
            result.is_err(),
            "from_apr should fail when embedding is missing"
        );
    }

    let _ = std::fs::remove_file(&path);
}

// ============================================================================
// from_apr: Vocab size inference from embedding tensor shape
// ============================================================================

#[test]
fn test_from_apr_infers_vocab_from_embedding_shape() {
    let model = build_minimal_owned_quantized_model();
    let apr_bytes = model.to_apr_bytes().expect("should produce bytes");

    let dir = std::env::temp_dir();
    let path = dir.join("test_vocab_inference.apr");
    std::fs::write(&path, &apr_bytes).expect("should write");

    let mapped = crate::apr::MappedAprModel::from_path(&path).expect("should load");

    // The metadata has vocab_size, but if it were 0, from_apr should infer from embedding shape
    let restored = OwnedQuantizedModel::from_apr(&mapped).expect("should load model");
    assert_eq!(
        restored.config.vocab_size, 10,
        "Should have correct vocab_size"
    );

    let _ = std::fs::remove_file(&path);
}

// ============================================================================
// from_apr: Config defaults
// ============================================================================

#[test]
fn test_from_apr_uses_metadata_defaults() {
    let model = build_minimal_owned_quantized_model();
    let apr_bytes = model.to_apr_bytes().expect("should produce bytes");

    let dir = std::env::temp_dir();
    let path = dir.join("test_metadata_defaults.apr");
    std::fs::write(&path, &apr_bytes).expect("should write");

    let mapped = crate::apr::MappedAprModel::from_path(&path).expect("should load");
    let restored = OwnedQuantizedModel::from_apr(&mapped).expect("should load model");

    // Verify config was populated from metadata
    assert_eq!(restored.config.hidden_dim, 8);
    assert_eq!(restored.config.num_heads, 2);
    assert_eq!(restored.config.num_kv_heads, 2);
    assert!(restored.config.eps > 0.0);
    assert!(restored.config.rope_theta > 0.0);

    let _ = std::fs::remove_file(&path);
}

// ============================================================================
// PMAT-888: .apr non-Gemma2 post-norm collision (APR-PARITY falsifier)
// ============================================================================

/// PMAT-888 RED→GREEN falsifier: `OwnedQuantizedModel::from_apr` must NOT
/// populate the Gemma2-only `post_attn_norm_weight` / `post_ffw_norm_weight`
/// slots for a non-Gemma2 (`llama`/`qwen2`/...) `.apr`.
///
/// ROOT CAUSE: the HF tensor name `post_attention_layernorm.weight` is the
/// *FFN (pre-feedforward) norm* for llama/qwen2/mistral/phi/deepseek/qwen3 (see
/// `tensor_names_fallback::FfnNormWeight`). PMAT-810b added a Gemma2 post-norm
/// load to the APR loader keyed on that exact HF name, so for every non-Gemma2
/// `.apr` the FFN norm was ALSO loaded into the post-attention-norm slot, and
/// `ffn_block::forward_single_with_cache` — which gates only on `is_some()`, not
/// on arch — applied a spurious extra RMSNorm to the attention output before the
/// residual add. Result: garbage output (`çļĦåıªæĺ¯…`) for every non-Gemma2
/// `.apr` (CPU and GPU), while the byte-identical GGUF ran coherently (the GGUF
/// loader reads the disambiguated `post_attention_norm.weight`, no "layer").
///
/// `build_executable_pygmy_apr` is `architecture == "llama"` and DOES contain
/// `model.layers.0.post_attention_layernorm.weight` (its FFN norm), so it is the
/// exact collision fixture.
///
/// RED before the fix: `post_attn_norm_weight == Some(..)` (the FFN norm).
/// GREEN after the fix: `None` (gated on `config.is_gemma2()`).
///
/// Contract: apr-load-fail-closed-gemma-v1.yaml §NON-GEMMA-APR-POSTNORM-NONE.
#[test]
fn test_pmat888_non_gemma2_apr_has_no_post_attn_norm() {
    let apr_bytes = crate::apr::test_factory::build_executable_pygmy_apr();
    let dir = std::env::temp_dir();
    let path = dir.join("test_pmat888_non_gemma2_postnorm.apr");
    std::fs::write(&path, &apr_bytes).expect("should write file");

    let mapped = crate::apr::MappedAprModel::from_path(&path).expect("should load apr");
    // Sanity: the FFN-norm tensor whose HF name collides with the Gemma2
    // post-attn-norm name IS present in this non-Gemma2 model.
    assert!(
        mapped
            .find_tensor("model.layers.0.post_attention_layernorm.weight")
            .is_some(),
        "fixture must contain the colliding FFN-norm tensor for the test to be load-bearing"
    );

    let model = OwnedQuantizedModel::from_apr(&mapped).expect("from_apr should succeed");
    assert_eq!(model.config.architecture, "llama");
    assert!(!model.config.is_gemma2(), "fixture is non-Gemma2");

    for (i, layer) in model.layers.iter().enumerate() {
        assert!(
            layer.post_attn_norm_weight.is_none(),
            "PMAT-888: non-Gemma2 .apr layer {i} must NOT load post_attn_norm_weight \
             (the HF name post_attention_layernorm.weight is the FFN norm, not a \
             post-attention norm — loading + applying it corrupts all output)"
        );
        assert!(
            layer.post_ffw_norm_weight.is_none(),
            "PMAT-888: non-Gemma2 .apr layer {i} must NOT load post_ffw_norm_weight"
        );
        // The FFN norm itself MUST still be loaded into its correct slot.
        assert!(
            layer.ffn_norm_weight.is_some(),
            "PMAT-888: the post_attention_layernorm.weight tensor must still populate \
             the FFN-norm slot (ffn_norm_weight) for layer {i}"
        );
    }

    let _ = std::fs::remove_file(&path);
}

// ============================================================================
// #2309 / #2441: tied word embeddings — 0-byte lm_head placeholder
// ============================================================================

/// Raw bytes of a named tensor in an APR file, straight out of the mapped model.
fn apr_tensor_bytes(mapped: &crate::apr::MappedAprModel, name: &str) -> Vec<u8> {
    let t = mapped
        .find_tensor(name)
        .unwrap_or_else(|| panic!("fixture must contain {name}"));
    let start = mapped.data_offset() as usize + t.offset as usize;
    mapped.data()[start..start + t.size as usize].to_vec()
}

/// #2309 RED→GREEN falsifier: `apr run` on a tied-embedding `.apr` must decode.
///
/// A `tie_word_embeddings=true` checkpoint converts to an `.apr` whose
/// `lm_head.weight` descriptor carries the full `[vocab, hidden]` shape but ZERO
/// bytes of data — the matrix lives once, under `model.embed_tokens.weight`.
/// `OwnedQuantizedModel::from_apr` registered that descriptor verbatim, so the
/// output projection was an `OwnedQuantizedTensor` with an empty `data` buffer and
/// EVERY decode died in `fused_matmul`:
///
///   Inference failed: Invalid shape: matmul weight has EMPTY data buffer
///   (in_dim=896, out_dim=151936, qtype=0)
///
/// (The error's MoE/#1789 hypothesis is a red herring — the model is not MoE.)
///
/// RED before the fix: `forward` returns that error. GREEN after: the loader ties
/// the head to the embedding matrix and the forward pass produces real logits.
#[test]
fn test_2309_tied_lm_head_placeholder_decodes() {
    let apr_bytes = crate::apr::test_factory::build_executable_pygmy_apr_tied_lm_head_placeholder();
    let dir = std::env::temp_dir();
    let path = dir.join("test_2309_tied_lm_head_placeholder.apr");
    std::fs::write(&path, &apr_bytes).expect("should write file");

    let mapped = crate::apr::MappedAprModel::from_path(&path).expect("should load apr");

    // The fixture must actually BE the defect shape, or this test proves nothing.
    let lm = mapped
        .find_tensor("lm_head.weight")
        .expect("fixture must declare lm_head.weight");
    assert_eq!(lm.size, 0, "fixture's lm_head must be a 0-byte placeholder");
    assert_eq!(
        lm.shape,
        vec![10, 8],
        "the placeholder still carries the full [vocab, hidden] shape"
    );

    let model =
        OwnedQuantizedModel::from_apr(&mapped).expect("#2309: a tied-embedding .apr must load");

    // Behaviour first: the forward pass that used to die on the empty buffer.
    let logits = model
        .forward(&[1u32])
        .expect("#2309: decode must not fail with 'matmul weight has EMPTY data buffer'");
    assert_eq!(logits.len(), 10, "one logit per vocab entry");
    assert!(
        logits.iter().all(|v| v.is_finite()),
        "tied lm_head must produce finite logits, got {logits:?}"
    );
    assert!(
        logits.iter().any(|v| *v != 0.0),
        "tied lm_head must produce non-degenerate logits (an all-zero head would \
         also 'succeed'), got {logits:?}"
    );

    // And it must be the RIGHT matrix: the output projection IS the embedding,
    // byte for byte, shaped in_dim=hidden / out_dim=vocab for the logits matmul.
    let embed_bytes = apr_tensor_bytes(&mapped, "model.embed_tokens.weight");
    assert_eq!(
        model.lm_head_weight.data, embed_bytes,
        "#2309: the tied lm_head must be the embedding matrix, byte for byte"
    );
    assert_eq!(model.lm_head_weight.in_dim, 8);
    assert_eq!(model.lm_head_weight.out_dim, 10);

    let _ = std::fs::remove_file(&path);
}

/// #2309, second tie spelling: an `.apr` that OMITS `lm_head.weight` entirely.
///
/// GGUF-derived and explicitly-tied conversions drop the descriptor rather than
/// writing a 0-byte one. That spelling used to fail earlier and louder —
/// "APR: tensor not found (tried: lm_head.weight, output.weight)" — so the quantized
/// loader could not load a tied model under either spelling.
#[test]
fn test_2309_omitted_lm_head_ties_to_embeddings() {
    let apr_bytes = crate::apr::test_factory::build_executable_pygmy_apr_embed_tied();
    let dir = std::env::temp_dir();
    let path = dir.join("test_2309_omitted_lm_head.apr");
    std::fs::write(&path, &apr_bytes).expect("should write file");

    let mapped = crate::apr::MappedAprModel::from_path(&path).expect("should load apr");
    assert!(
        mapped.find_tensor("lm_head.weight").is_none(),
        "fixture must omit lm_head.weight for this test to bite"
    );

    let model = OwnedQuantizedModel::from_apr(&mapped)
        .expect("#2309: an .apr with no lm_head descriptor is tied, not broken");
    let logits = model.forward(&[1u32]).expect("#2309: tied decode must work");
    assert_eq!(logits.len(), 10);
    assert!(logits.iter().all(|v| v.is_finite()));

    let _ = std::fs::remove_file(&path);
}

/// #2309 negative control: a model with a REAL lm_head must keep its own weights.
///
/// Guards the fix from being over-broad — "always tie" would silently replace a
/// genuinely separate output projection with the embedding matrix and change what
/// the model emits.
#[test]
fn test_2309_untied_lm_head_is_not_replaced_by_embeddings() {
    let apr_bytes = crate::apr::test_factory::build_executable_pygmy_apr();
    let dir = std::env::temp_dir();
    let path = dir.join("test_2309_untied_lm_head_control.apr");
    std::fs::write(&path, &apr_bytes).expect("should write file");

    let mapped = crate::apr::MappedAprModel::from_path(&path).expect("should load apr");
    let lm_bytes = apr_tensor_bytes(&mapped, "lm_head.weight");
    let embed_bytes = apr_tensor_bytes(&mapped, "model.embed_tokens.weight");
    assert_ne!(
        lm_bytes, embed_bytes,
        "fixture must have a genuinely different lm_head for this control to bite"
    );

    let model = OwnedQuantizedModel::from_apr(&mapped).expect("untied .apr must load");
    assert_eq!(
        model.lm_head_weight.data, lm_bytes,
        "#2309: an untied lm_head must keep its OWN weights, not the embeddings"
    );

    let _ = std::fs::remove_file(&path);
}