franken_ocr 0.9.0

Pure-Rust, CPU-hyper-optimized runner for the Baidu Unlimited-OCR model (single-binary CLI: focr)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
//! OneChart assembly (sub-epic D) — the fourth model path, a direct GOT
//! sibling (Vary-tiny splice; census `docs/zoo/onechart-spec.md`):
//! squash-bicubic 1024² RAW-[0,1] preprocess ([`preprocess::onechart_view_tensor`])
//! → the SAME certified SAM-ViT-B tower as GOT (prefix `model.vision_tower`,
//! share-by-import per the B3 precedent) → a `Linear(1024→768, bias)`
//! `mm_projector` → 256 `<imgpad>` (50265) slots → the OPT-125M decoder (D4,
//! pending on the shared dense engine) + the `num_decoder` number head (D5).
//!
//! Shipped here: the D3 vision seam ([`vision_features`]), the D4 embeds
//! splice ([`build_inputs_embeds`] — the decoder itself is
//! `DecoderConfig::onechart()` in the shared engine), and the D5 number head
//! + self-verify math ([`number_head`], [`extract_gt_values`],
//!   [`normalize_gt`], [`reliable_distance`]). The end-to-end `recognize`
//!   assembly + CLI routing land with D6-D8.

use crate::error::FocrResult;

use super::connector;
use super::decoder;
use super::tensor::Mat;
use super::vision_sam::{self, Linear};
use super::weights::Weights;

// Clock seam: `std::time::Instant` traps on wasm32-unknown-unknown; `web-time`
// re-exports std's types on native targets, so native behavior is unchanged.
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

/// The vision-token count (SAM 1024² → 16× compressor → 256 tokens, as GOT).
pub const VISION_TOKENS: usize = 256;
/// The decoder hidden width the projector emits (OPT hidden 768 — census §3:
/// the connector currency is 768, NOT GOT's 1024 or Baidu's 1280).
pub const HIDDEN: usize = 768;

/// D3: the OneChart vision features — the certified SAM tower at the
/// `model.vision_tower` prefix, then the `model.mm_projector`
/// `Linear(1024→768, bias=True)` (census §3). Returns `[256, 768]`
/// token-major rows, ready for the `<imgpad>` splice.
///
/// When `statics.sam` is `None` the tower streams per block from `weights`,
/// which also selects the bounded global-attention kernel. Both arms run the
/// same `forward_core` body, so the features are bit-identical.
///
/// # Errors
/// A tower/hydration error, or a projector shape violation.
pub fn vision_features(
    weights: &Weights,
    statics: &OnechartStatics,
    image: &Mat,
) -> FocrResult<Mat> {
    let side = (image.cols as f64).sqrt() as usize;
    if side * side != image.cols || image.rows != 3 {
        return Err(crate::FocrError::Other(anyhow::anyhow!(
            "onechart vision: expected [3, side*side] input, got [{}, {}]",
            image.rows,
            image.cols
        )));
    }
    let sam = match statics.sam.as_ref() {
        Some(tower) => vision_sam::forward_with(tower, image, side, side)?, // [1024, 256]
        None => vision_sam::forward_streamed(weights, image, &statics.prefix)?,
    };
    let sam_t = transpose(&sam); // [256, 1024] token-major
    statics.proj.apply(&sam_t) // [256, 768]
}

/// The per-model-constant OneChart tensors — the SAM tower, the
/// `mm_projector` connector, and the widened OPT embed table — hydrated ONCE
/// and cached on the [`super::OcrModel`] (bd-av64.10 pass 6 idiom: the page
/// loop re-widened all three per page).
pub struct OnechartStatics {
    /// The hydrated SAM-ViT-B tower (`model.vision_tower`), or `None` when the
    /// tower streams per block instead of being retained.
    pub sam: Option<vision_sam::SamWeights>,
    /// The SAM tensor-name prefix, kept so the streamed arm can re-read blocks.
    pub prefix: String,
    /// The `mm_projector` `Linear(1024→768)+bias` (pre-transposed).
    pub proj: Linear,
    /// The widened `[vocab, hidden]` OPT embed table.
    pub embed: Mat,
}

/// Hydrate an [`OnechartStatics`] from the artifact.
///
/// # Errors
/// A missing or mis-shaped tensor.
pub fn hydrate_statics(
    weights: &Weights,
    prefix: &str,
    stream_vision: bool,
) -> FocrResult<OnechartStatics> {
    let th = Instant::now();
    let statics = OnechartStatics {
        // Streaming skips the retained f32 tower; the per-block path hydrates
        // and drops one block at a time from the same bf16 source.
        sam: if stream_vision {
            None
        } else {
            Some(vision_sam::sam_weights_from(weights, prefix)?)
        },
        prefix: prefix.to_string(),
        proj: Linear::from_row_major(
            &weights.vec("model.mm_projector.weight")?,
            weights.vec("model.mm_projector.bias")?,
            HIDDEN,
            1024,
        )?,
        embed: weights.mat("model.decoder.embed_tokens.weight")?,
    };
    super::timing_log(&format!(
        "  onechart.hydrate({}) {:.2}s",
        if stream_vision { "streamed" } else { "cached" },
        th.elapsed().as_secs_f64()
    ));
    Ok(statics)
}

/// Build the OPT decoder `inputs_embeds`: embed the prompt ids against the
/// tied `model.decoder.embed_tokens.weight`, then scatter the vision rows
/// into the 256 `<imgpad>` (50265) slots in prompt order. (The learned
/// position table is added INSIDE the decoder prefill — census §4/OQ-D6.)
///
/// # Errors
/// An embed error, or a [`connector::masked_scatter`] slot-count mismatch.
pub fn build_inputs_embeds(
    statics: &OnechartStatics,
    vision: &Mat,
    prompt_ids: &[u32],
) -> FocrResult<Mat> {
    let embed = &statics.embed;
    let (vocab, hidden) = (embed.rows, embed.cols);
    let mut inputs_embeds = decoder::embed_tokens(&embed.data, vocab, hidden, prompt_ids)?;
    let mask: Vec<bool> = prompt_ids
        .iter()
        .map(|&id| id == crate::tokenizer::special_opt::IMG_PAD)
        .collect();
    connector::masked_scatter(&mut inputs_embeds, vision, &mask)?;
    Ok(inputs_embeds)
}

/// D5: the `num_decoder` number head (census §8) —
/// `Linear(768→384)·ReLU·Linear(384→384)·ReLU·Linear(384→256)`, all biased,
/// applied to the **post-`final_layer_norm`** hidden of the decode step whose
/// INPUT token is the generated `<Number>` (50268). `pred_locs[i]` ≈ the
/// i-th chart value min-max normalized to [0,1] (upstream keeps the first
/// 100). Port contract: computed PER REQUEST — never a stale attribute
/// (OQ-D4).
///
/// # Errors
/// A missing tensor or a shape violation.
pub fn number_head(weights: &Weights, hidden_row: &[f32]) -> FocrResult<Vec<f32>> {
    let lin = |i: usize, out, in_| -> FocrResult<Linear> {
        Linear::from_row_major(
            &weights.vec(&format!("num_decoder.{i}.weight"))?,
            weights.vec(&format!("num_decoder.{i}.bias"))?,
            out,
            in_,
        )
    };
    let x = Mat::from_vec(1, HIDDEN, hidden_row.to_vec());
    let mut x = lin(0, 384, HIDDEN)?.apply(&x)?;
    super::nn::relu(&mut x);
    let mut x = lin(2, 384, 384)?.apply(&x)?;
    super::nn::relu(&mut x);
    Ok(lin(4, 256, 384)?.apply(&x)?.data)
}

/// The `reliable_check` verdict threshold (census §8: mean-L1 < 0.1 ⇒
/// "reliable").
pub const RELIABLE_THRESHOLD: f64 = 0.1;

/// D5: extract the ground-truth numeric list from a parsed `values` JSON
/// object (census §8 step 1): dicts recurse (multi-series), a LIST anywhere
/// aborts (`None` = unverifiable), numeric leaves pass through, string
/// leaves drop `(\d+)`/`[\d+]` spans then every char outside `[0-9.-]`, and
/// the residues `-`/`*`/`none`/`None`/`` are skipped.
#[must_use]
pub fn extract_gt_values(values: &serde_json::Value) -> Option<Vec<f64>> {
    fn walk(v: &serde_json::Value, out: &mut Vec<f64>) -> bool {
        match v {
            serde_json::Value::Object(map) => map.values().all(|x| walk(x, out)),
            serde_json::Value::Array(_) => false,
            serde_json::Value::Number(n) => {
                if let Some(f) = n.as_f64() {
                    out.push(f);
                }
                true
            }
            serde_json::Value::String(s) => {
                let cleaned = strip_index_spans(s);
                let filtered: String = cleaned
                    .chars()
                    .filter(|c| c.is_ascii_digit() || *c == '.' || *c == '-')
                    .collect();
                if matches!(filtered.as_str(), "-" | "*" | "none" | "None" | "") {
                    return true;
                }
                if let Ok(f) = filtered.parse::<f64>() {
                    out.push(f);
                }
                true
            }
            _ => true,
        }
    }
    let mut out = Vec::new();
    walk(values, &mut out).then_some(out)
}

/// `re.sub(r'\(\d+\)|\[\d+\]', '', s)` — drop parenthesized/bracketed pure
/// digit spans (footnote markers) before the numeric filter.
fn strip_index_spans(s: &str) -> String {
    let chars: Vec<char> = s.chars().collect();
    let mut out = String::with_capacity(s.len());
    let mut i = 0;
    while i < chars.len() {
        let (open, close) = match chars[i] {
            '(' => ('(', ')'),
            '[' => ('[', ']'),
            _ => {
                out.push(chars[i]);
                i += 1;
                continue;
            }
        };
        let _ = open;
        // A span matches only if ≥1 digit then the matching close bracket.
        let mut j = i + 1;
        while j < chars.len() && chars[j].is_ascii_digit() {
            j += 1;
        }
        if j > i + 1 && j < chars.len() && chars[j] == close {
            i = j + 1; // drop the whole span
        } else {
            out.push(chars[i]);
            i += 1;
        }
    }
    out
}

/// D5: min-max normalize the GT list (census §8 step 2): `len < 2` is the
/// identity (rounded); else `(x−min)/(max−min+1e-9)`, each rounded to 4
/// decimals with round-half-even (python `round`).
#[must_use]
pub fn normalize_gt(xs: &[f64]) -> Vec<f64> {
    let round4 = |x: f64| (x * 10_000.0).round_ties_even() / 10_000.0;
    if xs.len() < 2 {
        return xs.iter().map(|&x| round4(x)).collect();
    }
    let (lo, hi) = xs
        .iter()
        .fold((f64::INFINITY, f64::NEG_INFINITY), |(l, h), &x| {
            (l.min(x), h.max(x))
        });
    xs.iter()
        .map(|&x| round4((x - lo) / (hi - lo + 1e-9)))
        .collect()
}

/// D5: `reliable_distance = mean-L1(pred_locs[..n], gt)` (census §8 step 3).
#[must_use]
pub fn reliable_distance(pred_locs: &[f32], gt: &[f64]) -> f64 {
    if gt.is_empty() {
        return f64::INFINITY;
    }
    let n = gt.len().min(pred_locs.len());
    gt[..n]
        .iter()
        .zip(&pred_locs[..n])
        .map(|(&g, &p)| (g - f64::from(p)).abs())
        .sum::<f64>()
        / n as f64
}

/// The single fixed OneChart prompt (census §5 — conv_vicuna_v1_1, no modes):
/// system text + `<img>` + 256 `<imgpad>` + `</img>` + the hardcoded query.
/// Token-id-exact to the committed 308-id L0c fixture.
///
/// # Errors
/// A tokenizer encode error (impossible for this fixed ASCII prompt).
pub fn chart_prompt_ids(tk: &crate::tokenizer::Tokenizer) -> FocrResult<Vec<u32>> {
    let imgpad = "<imgpad>".repeat(VISION_TOKENS);
    let prompt = format!(
        "A chat between a curious user and an artificial intelligence assistant. \
         The assistant gives helpful, detailed, and polite answers to the user's \
         questions. USER: <img>{imgpad}</img>Convert the key information of the \
         chart to a python dict:\n ASSISTANT:"
    );
    tk.encode(&prompt)
}

/// Brace-complete a possibly-truncated JSON object (the upstream
/// `complete_json_string` robustness shim, census §9): append the `}`s an
/// unbalanced object is missing (string-aware).
#[must_use]
pub fn complete_json_string(s: &str) -> String {
    let mut depth = 0i32;
    let mut in_str = false;
    let mut esc = false;
    for c in s.chars() {
        if esc {
            esc = false;
            continue;
        }
        match c {
            '\\' if in_str => esc = true,
            '"' => in_str = !in_str,
            '{' if !in_str => depth += 1,
            '}' if !in_str => depth -= 1,
            _ => {}
        }
    }
    let mut out = s.to_string();
    if in_str {
        out.push('"');
    }
    for _ in 0..depth.max(0) {
        out.push('}');
    }
    out
}

/// The structured OneChart result (census §8/§9 — "emit as structured
/// fields, not string concat").
#[derive(Debug, Clone)]
pub struct ChartResult {
    /// The (brace-completed) chart dict text, specials stripped.
    pub json_text: String,
    /// The number head's normalized predictions (first 100 of 256), when the
    /// model emitted `<Number>`; `None` = unverifiable (OQ-D4).
    pub pred_locs: Option<Vec<f32>>,
    /// mean-L1 between `pred_locs` and the parsed values, when both exist.
    pub reliable_distance: Option<f64>,
    /// `Some(distance < 0.1)` when a distance was computable.
    pub reliable: Option<bool>,
}

/// End-to-end OneChart chart→dict extraction (D6-D8 assembly): raw-[0,1]
/// squash preprocess → certified SAM tower + projector → `<imgpad>` splice →
/// OPT KV-cache greedy (eos 2, seq hard-capped at 4096 — OQ-D7) → the D5
/// `<Number>` tap (one re-prefill of `prompt + generated[..=pos]` through the
/// certified path, post-final-norm hidden → [`number_head`]) → strip/
/// brace-complete → [`extract_gt_values`]/[`normalize_gt`]/
/// [`reliable_distance`] self-verify.
///
/// # Errors
/// A preprocess, vision, decode, or tokenizer error.
pub fn recognize(
    weights: &Weights,
    statics: &OnechartStatics,
    tk: &crate::tokenizer::Tokenizer,
    img: &image::DynamicImage,
    max_new: usize,
) -> FocrResult<ChartResult> {
    let tv = Instant::now();
    let image = crate::preprocess::onechart_view_tensor(img);
    let vision = vision_features(weights, statics, &image)?;
    let prompt_ids = chart_prompt_ids(tk)?;
    let embeds = build_inputs_embeds(statics, &vision, &prompt_ids)?;
    super::timing_log(&format!(
        "  onechart.vision+splice {:.2}s",
        tv.elapsed().as_secs_f64()
    ));

    let tg = Instant::now();
    let cfg = super::decoder_qwen2::DecoderConfig::onechart();
    // OQ-D7: the learned position table has 4096 usable rows — hard-stop.
    let max_new = max_new.min(4096usize.saturating_sub(embeds.rows));
    let ids = super::decoder_qwen2::generate_greedy_kvcache(
        weights,
        &cfg,
        &embeds,
        max_new,
        crate::tokenizer::special_opt::BOS_EOS,
    )?;
    super::timing_log(&format!(
        "  onechart.generate {} tokens {:.2}s",
        ids.len(),
        tg.elapsed().as_secs_f64()
    ));

    finish_recognition(weights, statics, tk, &cfg, &prompt_ids, &vision, ids)
}

/// The shared post-decode finish (D5-D8): the `<Number>` head tap (first fire
/// wins, recomputed through the certified prefill), the strip/brace-complete
/// repair, and the self-verify verdict — identical for the sequential and
/// batched paths by construction (this IS the sequential tail, extracted).
fn finish_recognition(
    weights: &Weights,
    statics: &OnechartStatics,
    tk: &crate::tokenizer::Tokenizer,
    cfg: &super::decoder_qwen2::DecoderConfig,
    prompt_ids: &[u32],
    vision: &Mat,
    ids: Vec<u32>,
) -> FocrResult<ChartResult> {
    let pred_locs = match ids
        .iter()
        .position(|&id| id == crate::tokenizer::special_opt::NUMBER)
    {
        Some(pos) => {
            let mut full = prompt_ids.to_vec();
            full.extend_from_slice(&ids[..=pos]);
            let embeds_tap = build_inputs_embeds(statics, vision, &full)?;
            let hidden = super::decoder_qwen2::prefill_final_hidden(weights, cfg, &embeds_tap)?;
            let last = &hidden.data[(hidden.rows - 1) * hidden.cols..];
            let mut locs = number_head(weights, last)?;
            locs.truncate(100);
            Some(locs)
        }
        None => None,
    };

    let json_text = complete_json_string(tk.decode_skip_special(&ids)?.trim());
    let (reliable_distance_v, reliable) = match (&pred_locs, parse_values(&json_text)) {
        (Some(locs), Some(gt)) if !gt.is_empty() => {
            let d = reliable_distance(locs, &normalize_gt(&gt));
            (Some(d), Some(d < RELIABLE_THRESHOLD))
        }
        _ => (None, None),
    };
    Ok(ChartResult {
        json_text,
        pred_locs,
        reliable_distance: reliable_distance_v,
        reliable,
    })
}

/// Parse the generated dict text and extract its `values` (or `data` alias)
/// numeric list; `None` when the JSON or the walk is unusable.
fn parse_values(json_text: &str) -> Option<Vec<f64>> {
    let v: serde_json::Value = serde_json::from_str(json_text).ok()?;
    let values = v.get("values").or_else(|| v.get("data"))?;
    extract_gt_values(values)
}

/// `[r, c]` row-major → `[c, r]` row-major (channel-major SAM output →
/// token-major rows; the same reshape GOT's assembly performs).
fn transpose(m: &Mat) -> Mat {
    let (r, c) = (m.rows, m.cols);
    let mut out = vec![0.0f32; r * c];
    for i in 0..r {
        for j in 0..c {
            out[j * r + i] = m.data[i * c + j];
        }
    }
    Mat::from_vec(c, r, out)
}

/// Batched OneChart chart→dict over MANY images (A7.5): vision + splice per
/// image sequentially, ONE continuous-batch OPT greedy decode, then the
/// per-page `<Number>` head tap + repair + verdict — per image byte-identical
/// to [`recognize`] (the learned-position budget rides the per-stream cap).
///
/// # Errors
/// As [`recognize`].
pub fn recognize_batch(
    weights: &Weights,
    statics: &OnechartStatics,
    tk: &crate::tokenizer::Tokenizer,
    imgs: &[&image::DynamicImage],
    max_new: usize,
) -> FocrResult<Vec<ChartResult>> {
    let tv = Instant::now();
    let prompt_ids = chart_prompt_ids(tk)?;
    let mut visions: Vec<Mat> = Vec::with_capacity(imgs.len());
    let mut embeds_list: Vec<Mat> = Vec::with_capacity(imgs.len());
    let mut caps: Vec<usize> = Vec::with_capacity(imgs.len());
    for img in imgs {
        let image = crate::preprocess::onechart_view_tensor(img);
        let vision = vision_features(weights, statics, &image)?;
        let embeds = build_inputs_embeds(statics, &vision, &prompt_ids)?;
        // OQ-D7: the learned position table has 4096 usable rows — hard-stop.
        caps.push(max_new.min(4096usize.saturating_sub(embeds.rows)));
        visions.push(vision);
        embeds_list.push(embeds);
    }
    super::timing_log(&format!(
        "  onechart.vision+splice(batch of {}) {:.2}s",
        imgs.len(),
        tv.elapsed().as_secs_f64()
    ));
    let tg = Instant::now();
    let cfg = super::decoder_qwen2::DecoderConfig::onechart();
    let id_streams = super::decoder_qwen2::generate_greedy_batched(
        weights,
        &cfg,
        &embeds_list,
        &caps,
        crate::tokenizer::special_opt::BOS_EOS,
    )?;
    super::timing_log(&format!(
        "  onechart.generate(batch of {}) {} tokens {:.2}s",
        imgs.len(),
        id_streams.iter().map(Vec::len).sum::<usize>(),
        tg.elapsed().as_secs_f64()
    ));
    // The number-head tap + repair + verdict stay the per-page path — the
    // SAME finish() the sequential recognize applies.
    id_streams
        .into_iter()
        .zip(&visions)
        .map(|(ids, vision)| {
            finish_recognition(weights, statics, tk, &cfg, &prompt_ids, vision, ids)
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn transpose_round_trips() {
        let m = Mat::from_vec(2, 3, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
        let t = transpose(&m);
        assert_eq!((t.rows, t.cols), (3, 2));
        assert_eq!(t.data, vec![1.0, 4.0, 2.0, 5.0, 3.0, 6.0]);
        assert_eq!(transpose(&t).data, m.data);
    }

    #[test]
    fn vision_features_error_handling() {
        // An empty weights bundle must fail loud (missing tower tensors), not
        // panic — the ModelNotFound/FormatMismatch rail.
        let w = Weights::default();
        assert!(hydrate_statics(&w, "model.vision_tower", false).is_err());
    }

    /// **D4-prefill — the OPT decoder vs the torch oracle** (skip-with-SUCCESS
    /// without `FOCR_ONECHART_DIR`): embed the committed 309-id prompt, splice
    /// the ORACLE's own projector rows into the 256 `<imgpad>` slots
    /// (seam-isolated from the D3 vision drift), run the new
    /// `DecoderConfig::onechart()` prefill (LayerNorm+bias, learned offset-2
    /// positions, ReLU fc1/fc2, tied head), and hold the last-pos logits to
    /// argmax-exact + cosine ≥ 0.9999 vs `onechart_final_logits.bin`.
    #[test]
    fn opt_prefill_matches_torch_oracle() {
        let Ok(dir) = std::env::var("FOCR_ONECHART_DIR") else {
            return;
        };
        let proj_path = format!("{dir}/onechart_proj_out.bin");
        let logits_path = format!("{dir}/onechart_final_logits.bin");
        let model_path = format!("{dir}/model.safetensors");
        if !std::path::Path::new(&proj_path).is_file() {
            eprintln!("skip-with-SUCCESS: {proj_path} absent (run the oracle script)");
            return;
        }
        let read_f32 = |p: &str| -> Vec<f32> {
            std::fs::read(p)
                .expect("oracle blob reads")
                .as_chunks::<4>()
                .0
                .iter()
                .map(|c| f32::from_le_bytes(*c))
                .collect()
        };
        let fx: serde_json::Value = serde_json::from_str(
            &std::fs::read_to_string(concat!(
                env!("CARGO_MANIFEST_DIR"),
                "/tests/fixtures/onechart/oracle_fixtures.json"
            ))
            .expect("oracle fixtures read"),
        )
        .expect("oracle fixtures parse");
        let prompt_ids: Vec<u32> = fx["l0c_prompt"]["ids"]
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_u64().unwrap() as u32)
            .collect();
        // 308 measured ids (the census §5 estimated 309; the fixture's own
        // `n` is the truth — 256 <imgpad> + 52 text/bracket ids, no bos).
        assert_eq!(
            prompt_ids.len(),
            fx["l0c_prompt"]["n"].as_u64().unwrap() as usize,
            "prompt drifted from its own fixture"
        );
        assert_eq!(prompt_ids.len(), 308, "measured census prompt length");

        let weights = Weights::load(std::path::Path::new(&model_path)).expect("weights");
        let statics = hydrate_statics(&weights, "model.vision_tower", false).expect("statics");
        let vision = Mat::from_vec(VISION_TOKENS, HIDDEN, read_f32(&proj_path));
        let embeds = build_inputs_embeds(&statics, &vision, &prompt_ids).expect("splice");
        let cfg = super::super::decoder_qwen2::DecoderConfig::onechart();
        let logits =
            super::super::decoder_qwen2::forward_prefill(&weights, &cfg, &embeds).expect("prefill");
        let ours = &logits.data[(logits.rows - 1) * logits.cols..];
        let want = read_f32(&logits_path);
        assert_eq!(ours.len(), want.len(), "vocab width");

        let argmax = |v: &[f32]| {
            v.iter()
                .enumerate()
                .max_by(|a, b| a.1.total_cmp(b.1))
                .map(|(i, _)| i)
                .unwrap()
        };
        let mut dot = 0.0f64;
        let (mut na, mut nb) = (0.0f64, 0.0f64);
        let mut max_abs = 0.0f64;
        for (a, b) in ours.iter().zip(&want) {
            let (a, b) = (f64::from(*a), f64::from(*b));
            dot += a * b;
            na += a * a;
            nb += b * b;
            max_abs = max_abs.max((a - b).abs());
        }
        let cos = dot / (na.sqrt() * nb.sqrt());
        eprintln!(
            "[D4 prefill] argmax={} (oracle {}) cos={cos:.8} maxabs={max_abs:.3e}",
            argmax(ours),
            argmax(&want)
        );
        assert_eq!(argmax(ours), argmax(&want), "next-token argmax diverged");
        assert!(cos >= 0.9999, "prefill logit cosine {cos:.8} < 0.9999");
    }

    #[test]
    fn complete_json_string_balances_braces() {
        assert_eq!(
            complete_json_string(r#"{"a": {"b": 1}"#),
            r#"{"a": {"b": 1}}"#
        );
        assert_eq!(complete_json_string(r#"{"a": 1}"#), r#"{"a": 1}"#);
        // String-aware: braces inside strings don't count.
        assert_eq!(complete_json_string(r#"{"a": "{{"#), r#"{"a": "{{"}"#);
        assert_eq!(complete_json_string(""), "");
    }

    /// **D8 L0c — the fixed chart prompt is id-EXACT vs the oracle's 308 ids**
    /// (skip-with-SUCCESS without the tokenizer files).
    #[test]
    fn chart_prompt_ids_match_oracle_l0c() {
        let Ok(dir) = std::env::var("FOCR_ONECHART_DIR") else {
            return;
        };
        let tk = crate::tokenizer::Tokenizer::from_opt_dir(std::path::Path::new(&dir))
            .expect("onechart tokenizer");
        let fx: serde_json::Value = serde_json::from_str(
            &std::fs::read_to_string(concat!(
                env!("CARGO_MANIFEST_DIR"),
                "/tests/fixtures/onechart/oracle_fixtures.json"
            ))
            .expect("oracle fixtures read"),
        )
        .expect("oracle fixtures parse");
        let want: Vec<u32> = fx["l0c_prompt"]["ids"]
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_u64().unwrap() as u32)
            .collect();
        let got = chart_prompt_ids(&tk).expect("prompt encode");
        assert_eq!(got, want, "chart prompt diverged from the 308-id oracle");
        eprintln!("[D8 L0c] {} prompt ids exact", got.len());
    }

    /// **D6/D8 e2e — full recognize on the committed chart** (skip-with-SUCCESS
    /// without weights). The HARD leg runs the **f32** reference weights (the
    /// oracle's own precision): all four bar values (30/45/25/10) must read
    /// back, the dict must open, verdict fields must be coherent, and the
    /// number head must land near the normalized truth. The **int8** leg is
    /// INFORMATIONAL: on this chart the int8 text decode repetition-runs (the
    /// bd-ic8/bd-ff4i class — OneChart has NO upstream ngram guard) while its
    /// pred_locs stay near-exact; the ngram-guard kill-switch is the
    /// documented mitigation (measured, not asserted here).
    #[test]
    fn recognize_reads_the_committed_chart() {
        let Ok(dir) = std::env::var("FOCR_ONECHART_DIR") else {
            return;
        };
        let f32_path = format!("{dir}/model.safetensors");
        if !std::path::Path::new(&f32_path).is_file() {
            eprintln!("skip-with-SUCCESS: {f32_path} absent");
            return;
        }
        let tk = crate::tokenizer::Tokenizer::from_opt_dir(std::path::Path::new(&dir))
            .expect("onechart tokenizer");
        let img = image::open(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/tests/fixtures/onechart/sample_chart.png"
        ))
        .expect("sample chart decodes");

        // HARD leg: f32.
        let weights = Weights::load(std::path::Path::new(&f32_path)).expect("f32 weights");
        let statics = hydrate_statics(&weights, "model.vision_tower", false).expect("f32 statics");
        let res = recognize(&weights, &statics, &tk, &img, 512).expect("recognize");
        eprintln!("[D6 e2e f32] json: {}", res.json_text);
        eprintln!(
            "[D6 e2e f32] pred_locs[:4]: {:?} distance {:?} reliable {:?}",
            res.pred_locs.as_ref().map(|l| &l[..4.min(l.len())]),
            res.reliable_distance,
            res.reliable
        );
        assert!(res.json_text.trim_start().starts_with('{'), "dict open");
        // TEXT value containment is NOT a stable gate on this OOD synthetic
        // chart: the oracle's own f32 chat() half-garbled the title/labels,
        // and greedy trajectories diverge chaotically past the near-tie
        // horizon in every precision (measured: ours reads 30 and 45, drops
        // 25/10 into a repetition run). The measured floor is 2/4; the
        // in-distribution SCRM-proxy corpus (D6 remaining scope) is where
        // text-value quality belongs. The STABLE end-to-end gate is the
        // number head below.
        let n_vals = ["30", "45", "25", "10"]
            .iter()
            .filter(|v| res.json_text.contains(**v))
            .count();
        eprintln!("[D6 e2e f32] text values: {n_vals}/4");
        assert!(
            n_vals >= 2,
            "text values collapsed below the measured floor"
        );
        let locs = res.pred_locs.as_ref().expect("<Number> must fire");
        // The head's first four slots vs the normalized truth (census §8
        // training semantics) — generous per-slot budget, this is a 0.5M-param
        // regression head.
        for (i, want) in [0.5714, 1.0, 0.4286, 0.0].iter().enumerate() {
            assert!(
                (f64::from(locs[i]) - want).abs() < 0.1,
                "pred_locs[{i}] = {} vs normalized truth {want}",
                locs[i]
            );
        }
        if res.pred_locs.is_some() && res.reliable_distance.is_some() {
            assert!(res.reliable.is_some());
        }

        // INFORMATIONAL leg: int8 (repetition-run class recorded, not gated).
        let int8_path = format!("{dir}/onechart.int8.focrq");
        if std::path::Path::new(&int8_path).is_file() {
            let w8 = Weights::load(std::path::Path::new(&int8_path)).expect("int8 artifact");
            let statics8 = hydrate_statics(&w8, "model.vision_tower", false).expect("int8 statics");
            if let Ok(r8) = recognize(&w8, &statics8, &tk, &img, 256) {
                let n_vals = ["30", "45", "25", "10"]
                    .iter()
                    .filter(|v| r8.json_text.contains(**v))
                    .count();
                eprintln!(
                    "[D6 e2e int8] {n_vals}/4 values, pred_locs[:4]: {:?}",
                    r8.pred_locs.as_ref().map(|l| &l[..4.min(l.len())])
                );
            }
        }
    }

    /// **bd-2lje — the in-distribution SCRM-proxy quality corpus** (skip-with-
    /// SUCCESS without weights): six default-style matplotlib charts with
    /// exact known values (`tests/fixtures/onechart/corpus/`), run through
    /// the PRODUCT path (int8 artifact). Per census §13-L5 the metrics are
    /// (a) valid-JSON rate, (b) per-value relative error (order-independent
    /// sorted pairing), (c) the number head's distance to the normalized GT.
    /// Gates are pinned from the first measurement pass.
    #[test]
    fn corpus_quality_scrm_proxy() {
        let Ok(dir) = std::env::var("FOCR_ONECHART_DIR") else {
            return;
        };
        let int8_path = format!("{dir}/onechart.int8.focrq");
        if !std::path::Path::new(&int8_path).is_file() {
            eprintln!("skip-with-SUCCESS: {int8_path} absent");
            return;
        }
        let corpus_dir = concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/tests/fixtures/onechart/corpus"
        );
        let manifest: serde_json::Value = serde_json::from_str(
            &std::fs::read_to_string(format!("{corpus_dir}/manifest.json"))
                .expect("corpus manifest"),
        )
        .expect("manifest parses");
        let tk = crate::tokenizer::Tokenizer::from_opt_dir(std::path::Path::new(&dir))
            .expect("onechart tokenizer");
        // Both precisions measured: int8 = the product path; f32 = the
        // reference (discriminates model garble from int8 drift).
        let mut legs: Vec<(&str, Weights)> = vec![(
            "int8",
            Weights::load(std::path::Path::new(&int8_path)).expect("int8 artifact"),
        )];
        let f32_path = format!("{dir}/model.safetensors");
        if std::path::Path::new(&f32_path).is_file() {
            legs.push((
                "f32",
                Weights::load(std::path::Path::new(&f32_path)).expect("f32 weights"),
            ));
        }
        for (label, weights) in &legs {
            run_corpus_leg(label, weights, &tk, corpus_dir, &manifest);
        }
    }

    fn run_corpus_leg(
        label: &str,
        weights: &Weights,
        tk: &crate::tokenizer::Tokenizer,
        corpus_dir: &str,
        manifest: &serde_json::Value,
    ) {
        let mut n_valid_json = 0usize;
        let mut head_dists = Vec::new();
        let mut value_errs = Vec::new();
        let statics = hydrate_statics(weights, "model.vision_tower", false).expect("statics");
        let charts = manifest["charts"].as_array().unwrap();
        for chart in charts {
            let file = chart["file"].as_str().unwrap();
            let img = image::open(format!("{corpus_dir}/{file}")).expect("chart decodes");
            let res = recognize(weights, &statics, tk, &img, 512).expect("recognize");
            let gt = extract_gt_values(&chart["values"]).expect("manifest GT is list-free");
            let gt_norm = normalize_gt(&gt);

            let parsed: Option<serde_json::Value> = serde_json::from_str(&res.json_text).ok();
            let valid = parsed.is_some();
            n_valid_json += usize::from(valid);

            // (b) per-value relative error, order-independent (sorted pairing).
            let ours_vals = parsed
                .as_ref()
                .and_then(|p| p.get("values").or_else(|| p.get("data")).cloned())
                .and_then(|v| extract_gt_values(&v));
            let rel_err = ours_vals.as_ref().map(|ov| {
                let mut a = ov.clone();
                let mut b = gt.clone();
                a.sort_by(f64::total_cmp);
                b.sort_by(f64::total_cmp);
                let n = a.len().min(b.len()).max(1);
                let pair_err: f64 = a
                    .iter()
                    .zip(&b)
                    .map(|(x, y)| (x - y).abs() / y.abs().max(1.0))
                    .sum::<f64>()
                    / n as f64;
                // Count mismatch is itself an error signal.
                let miss = (a.len() as f64 - b.len() as f64).abs() / b.len().max(1) as f64;
                pair_err + miss
            });
            if let Some(e) = rel_err {
                value_errs.push(e);
            }

            // (c) the number head vs the normalized GT.
            let head_dist = res
                .pred_locs
                .as_ref()
                .map(|locs| reliable_distance(locs, &gt_norm));
            if let Some(d) = head_dist {
                head_dists.push(d);
            }
            eprintln!(
                "[bd-2lje {label}] {file}: valid_json={valid} rel_err={rel_err:?} head_dist={head_dist:?}"
            );
            let snip: String = res.json_text.chars().take(160).collect();
            eprintln!("[bd-2lje {label}] {file}: text={snip:?}");
        }
        let mean = |v: &[f64]| v.iter().sum::<f64>() / v.len().max(1) as f64;
        eprintln!(
            "[bd-2lje {label}] SUMMARY: valid_json {n_valid_json}/{} | mean rel_err {:.3} (n={}) | \
             mean head_dist {:.3} (n={})",
            charts.len(),
            mean(&value_errs),
            value_errs.len(),
            mean(&head_dists),
            head_dists.len()
        );
        assert_eq!(charts.len(), 6, "corpus shrank");
        // MEASURED gates (2026-07-05, release, BOTH legs): the number head
        // fires on ALL six charts, mean distance 0.015 int8 / 0.014 f32
        // (max 0.034) — values read to ~1.5% of range; the stable
        // in-distribution signal. valid-JSON is the weak leg in EVERY
        // precision (1/6, same chart): the decoded text is BYTE-IDENTICAL
        // f32-vs-int8 on all six charts, so the garble is the model's own
        // text decoder, not quantization — int8 is token-exact-lossless on
        // this corpus. rel_err has n=0 because the garbled text never emits
        // a proper "values" dict; the metric stays armed for regressions
        // that would fix or further break the text leg.
        assert_eq!(
            head_dists.len(),
            6,
            "{label}: the number head must fire on every chart"
        );
        assert!(
            mean(&head_dists) < 0.05,
            "{label}: mean head distance {} regressed past the 0.05 gate (measured 0.015)",
            mean(&head_dists)
        );
        assert!(n_valid_json >= 1, "{label}: valid-JSON collapsed");
    }

    /// D5: the reliable_check pure math vs upstream-exact golden vectors
    /// (computed with the reference python: the two regex subs, min-max
    /// `(x−min)/(max−min+1e-9)`, python banker's `round(x,4)`).
    #[test]
    fn reliable_check_matches_upstream_goldens() {
        let case = |json: &str| -> Option<Vec<f64>> {
            extract_gt_values(&serde_json::from_str(json).unwrap()).map(|v| normalize_gt(&v))
        };
        assert_eq!(
            case(r#"{"A":"30","B":"45","C":"25","D":"10"}"#).unwrap(),
            vec![0.5714, 1.0, 0.4286, 0.0]
        );
        assert_eq!(
            case(r#"{"x":"6.12%","y":"1,234"}"#).unwrap(),
            vec![0.0, 1.0]
        );
        assert_eq!(
            case(r#"{"s1":{"a":1,"b":3},"s2":{"c":5}}"#).unwrap(),
            vec![0.0, 0.5, 1.0]
        );
        // len<2 identity + skip tokens.
        assert_eq!(case(r#"{"a":"none","b":"5","c":"-"}"#).unwrap(), vec![5.0]);
        // Footnote spans removed BEFORE the numeric filter.
        assert_eq!(case(r#"{"t":"ab(3)cd [7] 12.5x"}"#).unwrap(), vec![12.5]);
        // A list anywhere aborts (unverifiable).
        assert_eq!(case(r#"{"a":[1,2]}"#), None);
        // Distance + verdict.
        let gt = vec![0.5714, 1.0, 0.4286, 0.0];
        let pred: Vec<f32> = vec![0.57, 1.0, 0.43, 0.0];
        let d = reliable_distance(&pred, &gt);
        assert!(d < RELIABLE_THRESHOLD, "near-exact pred must verify ({d})");
        assert!(reliable_distance(&[0.9, 0.1], &[0.0, 1.0]) > RELIABLE_THRESHOLD);
        assert!(reliable_distance(&pred, &[]).is_infinite());
    }

    /// **D5 — the num_decoder MLP vs the numpy-over-real-weights golden**
    /// (skip-with-SUCCESS without `FOCR_ONECHART_DIR`): same input hidden,
    /// cosine ≥ 0.9999 + tight max-abs (an f32 3-layer MLP).
    #[test]
    fn number_head_matches_golden() {
        let Ok(dir) = std::env::var("FOCR_ONECHART_DIR") else {
            return;
        };
        let model_path = format!("{dir}/model.safetensors");
        if !std::path::Path::new(&model_path).is_file() {
            eprintln!("skip-with-SUCCESS: {model_path} absent");
            return;
        }
        let fx: serde_json::Value = serde_json::from_str(
            &std::fs::read_to_string(concat!(
                env!("CARGO_MANIFEST_DIR"),
                "/tests/fixtures/onechart/num_decoder_golden.json"
            ))
            .expect("golden read"),
        )
        .expect("golden parse");
        let hidden: Vec<f32> = fx["input_hidden"]
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_f64().unwrap() as f32)
            .collect();
        let want: Vec<f32> = fx["pred_locs_256"]
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_f64().unwrap() as f32)
            .collect();
        let weights = Weights::load(std::path::Path::new(&model_path)).expect("weights");
        let ours = number_head(&weights, &hidden).expect("number head");
        assert_eq!(ours.len(), 256);
        let mut max_abs = 0.0f64;
        let mut dot = 0.0f64;
        let (mut na, mut nb) = (0.0f64, 0.0f64);
        for (a, b) in ours.iter().zip(&want) {
            let (a, b) = (f64::from(*a), f64::from(*b));
            max_abs = max_abs.max((a - b).abs());
            dot += a * b;
            na += a * a;
            nb += b * b;
        }
        let cos = dot / (na.sqrt() * nb.sqrt());
        eprintln!("[D5 parity] num_decoder cos={cos:.8} maxabs={max_abs:.3e}");
        assert!(cos >= 0.9999, "num_decoder cosine {cos:.8}");
        assert!(max_abs <= 1e-4, "num_decoder maxabs {max_abs:.3e}");
    }

    /// **D4-decode — the Opt KV-cache path** (skip-with-SUCCESS without
    /// `FOCR_ONECHART_DIR`): from the same oracle-vision embeds as the prefill
    /// cert, (a) the O(n) KV-cache greedy and the O(n²) re-prefill greedy must
    /// agree on a 24-token window (the B9 identity at OPT geometry), (b) the
    /// first generated id must be 50268 `<Number>` (the certified prefill
    /// argmax / census §8 protocol), and (c) the decoded text must
    /// prefix-match the oracle `chat()` answer.
    #[test]
    fn opt_kvcache_matches_greedy_and_oracle() {
        let Ok(dir) = std::env::var("FOCR_ONECHART_DIR") else {
            return;
        };
        let proj_path = format!("{dir}/onechart_proj_out.bin");
        let model_path = format!("{dir}/model.safetensors");
        if !std::path::Path::new(&proj_path).is_file() {
            eprintln!("skip-with-SUCCESS: {proj_path} absent (run the oracle script)");
            return;
        }
        let read_f32 = |p: &str| -> Vec<f32> {
            std::fs::read(p)
                .expect("oracle blob reads")
                .as_chunks::<4>()
                .0
                .iter()
                .map(|c| f32::from_le_bytes(*c))
                .collect()
        };
        let fx: serde_json::Value = serde_json::from_str(
            &std::fs::read_to_string(concat!(
                env!("CARGO_MANIFEST_DIR"),
                "/tests/fixtures/onechart/oracle_fixtures.json"
            ))
            .expect("oracle fixtures read"),
        )
        .expect("oracle fixtures parse");
        let prompt_ids: Vec<u32> = fx["l0c_prompt"]["ids"]
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_u64().unwrap() as u32)
            .collect();
        // B9 identity holds on the SAME quantization: the kvcache path
        // pre-quantizes int8, so pair it with the int8 artifact (the C5
        // precedent — on raw f32 safetensors `generate_greedy` runs f32 GEMMs
        // and near-ties may flip between precisions, DISC-002/DISC-003).
        let int8_path = format!("{dir}/onechart.int8.focrq");
        let weights = if std::path::Path::new(&int8_path).is_file() {
            Weights::load(std::path::Path::new(&int8_path)).expect("int8 artifact")
        } else {
            Weights::load(std::path::Path::new(&model_path)).expect("weights")
        };
        let vision = Mat::from_vec(VISION_TOKENS, HIDDEN, read_f32(&proj_path));
        let statics = hydrate_statics(&weights, "model.vision_tower", false).expect("statics");
        let embeds = build_inputs_embeds(&statics, &vision, &prompt_ids).expect("splice");
        let cfg = super::super::decoder_qwen2::DecoderConfig::onechart();

        let ids_kv = super::super::decoder_qwen2::generate_greedy_kvcache(
            &weights,
            &cfg,
            &embeds,
            24,
            crate::tokenizer::special_opt::BOS_EOS,
        )
        .expect("kvcache greedy");
        let ids_greedy = super::super::decoder_qwen2::generate_greedy(
            &weights,
            &cfg,
            &embeds,
            24,
            crate::tokenizer::special_opt::BOS_EOS,
        )
        .expect("re-prefill greedy");
        eprintln!("[D4 decode] kvcache: {ids_kv:?}");
        // The bespoke decode-attention's f32 reduction order differs from the
        // flash-blocked sdpa prefill, and at ~320 positions that flips
        // near-ties (DISC-003 — measured: 13 exact steps, then a whitespace/
        // quote-class JSON near-tie). The gate is the measured exact prefix;
        // a structural bug (positions, biases, norms) diverges at step 0-1.
        let b9_prefix = ids_kv
            .iter()
            .zip(&ids_greedy)
            .take_while(|(a, b)| a == b)
            .count();
        eprintln!("[D4 decode] kvcache-vs-greedy exact prefix: {b9_prefix}/24");
        assert!(
            b9_prefix >= 12,
            "kvcache vs re-prefill diverged at step {b9_prefix} — earlier than the \
             measured near-tie horizon (13); a structural decode-path defect"
        );
        assert_eq!(
            ids_kv[0],
            crate::tokenizer::special_opt::NUMBER,
            "first generated id must be the <Number> trigger (census §8)"
        );

        // Structural check: the chart-dict protocol opens a python dict after
        // the (stripped) <Number> trigger. A full text-vs-oracle comparison is
        // precision-crossed here (our int8 trajectory vs the f32 chat() run on
        // a high-entropy hallucinated title) — informational only; the L3/L4
        // parity anchors are the certified prefill logits + the B9 prefix.
        let tk = crate::tokenizer::Tokenizer::from_opt_dir(std::path::Path::new(&dir))
            .expect("onechart tokenizer");
        let ours = tk.decode_skip_special(&ids_kv).expect("decode");
        let oracle: String = fx["l4_chat"]["answer"]
            .as_str()
            .unwrap()
            .chars()
            .take(60)
            .collect();
        eprintln!("[D4 decode] ours:   {:?}", ours.trim());
        eprintln!("[D4 decode] oracle: {oracle:?}");
        assert!(
            ours.trim_start().starts_with('{'),
            "decoded output does not open the chart dict: {ours:?}"
        );
    }

    /// **D3 — OneChart vision + projector vs the torch oracle**
    /// (skip-with-SUCCESS without `FOCR_ONECHART_DIR`): feed the oracle's own
    /// preprocessed tensor (seam-isolated from resize parity, OQ-D3) through
    /// the certified SAM tower at the OneChart prefix + the `mm_projector`,
    /// and hold the `[256, 768]` output to cosine ≥ 0.9999 + a bounded
    /// max-abs vs `onechart_proj_out.bin`.
    #[test]
    fn vision_features_match_torch_oracle() {
        let Ok(dir) = std::env::var("FOCR_ONECHART_DIR") else {
            return;
        };
        let pre_path = format!("{dir}/onechart_preproc.bin");
        let want_path = format!("{dir}/onechart_proj_out.bin");
        let model_path = format!("{dir}/model.safetensors");
        if !std::path::Path::new(&pre_path).is_file() {
            eprintln!("skip-with-SUCCESS: {pre_path} absent (run the oracle script)");
            return;
        }
        let read_f32 = |p: &str| -> Vec<f32> {
            std::fs::read(p)
                .expect("oracle blob reads")
                .as_chunks::<4>()
                .0
                .iter()
                .map(|c| f32::from_le_bytes(*c))
                .collect()
        };
        let pre = read_f32(&pre_path);
        assert_eq!(pre.len(), 3 * 1024 * 1024, "preproc not [3,1024,1024]");
        let want = read_f32(&want_path);
        assert_eq!(want.len(), VISION_TOKENS * HIDDEN, "proj_out not [256,768]");

        let weights = Weights::load(std::path::Path::new(&model_path)).expect("weights");
        let statics = hydrate_statics(&weights, "model.vision_tower", false).expect("statics");
        let image = Mat::from_vec(3, 1024 * 1024, pre);
        let ours = vision_features(&weights, &statics, &image).expect("vision");
        assert_eq!((ours.rows, ours.cols), (VISION_TOKENS, HIDDEN));

        let mut dot = 0.0f64;
        let (mut na, mut nb) = (0.0f64, 0.0f64);
        let mut max_abs = 0.0f64;
        for (a, b) in ours.data.iter().zip(&want) {
            let (a, b) = (f64::from(*a), f64::from(*b));
            dot += a * b;
            na += a * a;
            nb += b * b;
            max_abs = max_abs.max((a - b).abs());
        }
        let cos = dot / (na.sqrt() * nb.sqrt());
        eprintln!("[D3 parity] proj_out cos={cos:.8} maxabs={max_abs:.3e}");
        assert!(cos >= 0.9999, "OneChart vision cosine {cos:.8} < 0.9999");
        assert!(
            max_abs <= 1e-2,
            "OneChart proj_out maxabs {max_abs:.3e} > 1e-2 — investigate before tightening"
        );
    }
}