fuel-transformers 0.19.1

Minimalist ML framework.
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
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
// SPDX-License-Identifier: MIT OR Apache-2.0
//! XLM-RoBERTa (multilingual BERT variant) ported to the
//! lazy-graph API.
//!
//! Conneau et al. 2019. RoBERTa pre-training procedure applied
//! to multilingual data. Used widely for multilingual encoder
//! tasks (classification, NER, retrieval). v1 ports the
//! `XLMRobertaModel` returning per-token hidden states.
//!
//! Differences from DistilBERT:
//!
//!   1. **Token type embeddings** (eager `type_vocab_size = 1`
//!      for XLM-R, but the embedding is still indexed — by 0
//!      for all positions). Carried as a learned
//!      `[type_vocab_size, hidden]` table.
//!   2. **RoBERTa position-id convention**:
//!      `position_ids = padding_idx + 1 + [0, 1, 2, ...]`
//!      (cumulative-count of non-pad tokens, then offset by
//!      `padding_idx + 1`). v1 assumes no padding inside the
//!      sequence — all input tokens are non-pad — so
//!      `position_ids` is a simple arithmetic progression
//!      starting at `padding_idx + 1`.
//!   3. **Post-LN sublayer structure (BERT-shape)**: same as
//!      DistilBERT. `out = LN(x + attn(x)); out = LN(out +
//!      ffn(out))`.
//!   4. **Separate Q/K/V/O linears** (no fused QKV).
//!
//! Otherwise identical to DistilBERT structurally.
//!
//! # Scope (v1)
//!
//! Forward-only, batch == 1, F32. Cross-attention path
//! (`encoder_hidden_states`) and KV cache (`past_key_value`)
//! both deferred. Optional additive attention mask of shape
//! `(1, 1, seq, seq)` for padding masking.

use fuel_ir::Shape;
use fuel_ir::error::Result;
use fuel_tensor::Device;
use fuel_tensor::{lazy::Tensor, lazy::WeightStorage};
use std::sync::Arc;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XlmrActivation {
    Gelu,
    GeluPytorchTanh,
    Relu,
    Silu,
}

#[derive(Debug, Clone, PartialEq)]
pub struct XlmrConfig {
    pub vocab_size: usize,
    pub hidden_size: usize,
    pub num_hidden_layers: usize,
    pub num_attention_heads: usize,
    pub intermediate_size: usize,
    pub max_position_embeddings: usize,
    /// XLM-R uses `type_vocab_size = 1` (always pass token type 0).
    pub type_vocab_size: usize,
    pub hidden_activation: XlmrActivation,
    pub layer_norm_eps: f64,
    pub pad_token_id: u32,
}

impl XlmrConfig {
    pub fn head_dim(&self) -> usize {
        self.hidden_size / self.num_attention_heads
    }
    /// `xlm-roberta-base` preset.
    pub fn xlm_roberta_base() -> Self {
        Self {
            vocab_size: 250002,
            hidden_size: 768,
            num_hidden_layers: 12,
            num_attention_heads: 12,
            intermediate_size: 3072,
            max_position_embeddings: 514,
            type_vocab_size: 1,
            hidden_activation: XlmrActivation::Gelu,
            layer_norm_eps: 1e-5,
            pad_token_id: 1,
        }
    }
}

#[derive(Debug, Clone)]
pub struct XlmrLayerWeights {
    pub q_proj: WeightStorage,
    pub q_proj_bias: Arc<[f32]>,
    pub k_proj: WeightStorage,
    pub k_proj_bias: Arc<[f32]>,
    pub v_proj: WeightStorage,
    pub v_proj_bias: Arc<[f32]>,
    pub out_proj: WeightStorage,
    pub out_proj_bias: Arc<[f32]>,
    pub attn_ln_gain: Arc<[f32]>,
    pub attn_ln_bias: Arc<[f32]>,
    pub fc1: WeightStorage,
    pub fc1_bias: Arc<[f32]>,
    pub fc2: WeightStorage,
    pub fc2_bias: Arc<[f32]>,
    pub ffn_ln_gain: Arc<[f32]>,
    pub ffn_ln_bias: Arc<[f32]>,
}

#[derive(Debug, Clone)]
pub struct XlmrWeights {
    pub word_embedding: Arc<[f32]>,
    pub position_embedding: Arc<[f32]>,
    pub token_type_embedding: Arc<[f32]>,
    pub embed_ln_gain: Arc<[f32]>,
    pub embed_ln_bias: Arc<[f32]>,
    pub layers: Vec<XlmrLayerWeights>,
}

#[derive(Debug, Clone)]
pub struct XlmrModel {
    pub config: XlmrConfig,
    pub weights: XlmrWeights,
}

impl XlmrModel {
    /// Run a forward pass and return per-token hidden states
    /// of shape `(1, seq, hidden_size)`.
    pub fn forward(&self, tokens: &[u32], attention_mask: Option<&Tensor>) -> Result<Tensor> {
        let cfg = &self.config;
        let weights = &self.weights;
        let seq = tokens.len();
        let batch = 1;
        assert!(seq > 0);
        // RoBERTa position-id offset by `padding_idx + 1`.
        // GAP-308 (CONFIG_FIELD): input length (plus RoBERTa's padding_idx offset)
        // vs config limit — reject, do not assert.
        if seq + (cfg.pad_token_id as usize) >= cfg.max_position_embeddings {
            return Err(fuel_ir::error::Error::Msg(format!(
                "seq + padding_idx + 1 ({}) exceeds max_position_embeddings ({})",
                seq + cfg.pad_token_id as usize + 1,
                cfg.max_position_embeddings,
            )));
        }

        // ---- Embeddings: word + position + token_type, sum, LayerNorm ---
        let word_emb_t = Tensor::from_f32(
            weights.word_embedding.clone(),
            Shape::from_dims(&[cfg.vocab_size, cfg.hidden_size]),
            &Device::cpu(),
        )?;
        let token_ids = word_emb_t.const_u32_like(tokens.to_vec(), Shape::from_dims(&[seq]))?;
        let word_embeds = word_emb_t
            .index_select(0_usize, &token_ids)?
            .reshape(Shape::from_dims(&[batch, seq, cfg.hidden_size]))?;

        // RoBERTa positions: padding_idx + 1, padding_idx + 2, ..., padding_idx + seq.
        let pad_off = cfg.pad_token_id as usize + 1;
        let position_ids_vec: Vec<u32> = (0..seq).map(|i| (pad_off + i) as u32).collect();
        let pos_full = word_emb_t.const_f32_like(
            Arc::clone(&weights.position_embedding),
            Shape::from_dims(&[cfg.max_position_embeddings, cfg.hidden_size]),
        )?;
        let pos_ids = word_emb_t.const_u32_like(position_ids_vec, Shape::from_dims(&[seq]))?;
        let pos_embeds = pos_full
            .index_select(0_usize, &pos_ids)?
            .reshape(Shape::from_dims(&[batch, seq, cfg.hidden_size]))?;

        // Token type embeddings (XLM-R always uses type 0).
        let tt_full = word_emb_t.const_f32_like(
            Arc::clone(&weights.token_type_embedding),
            Shape::from_dims(&[cfg.type_vocab_size, cfg.hidden_size]),
        )?;
        let tt_ids = word_emb_t.const_u32_like(vec![0_u32; seq], Shape::from_dims(&[seq]))?;
        let tt_embeds = tt_full
            .index_select(0_usize, &tt_ids)?
            .reshape(Shape::from_dims(&[batch, seq, cfg.hidden_size]))?;

        let sum = word_embeds.add(&pos_embeds)?.add(&tt_embeds)?;
        let mut h = sum.layer_norm_affine(
            std::sync::Arc::clone(&weights.embed_ln_gain),
            std::sync::Arc::clone(&weights.embed_ln_bias),
            cfg.layer_norm_eps,
        )?;

        // ---- Encoder layers --------------------------------------------
        for layer in &weights.layers {
            h = self.apply_layer(&h, layer, attention_mask)?;
        }
        Ok(h)
    }

    /// Extract per-token features at the requested layer
    /// indices. Output shape per layer:
    /// `(1, seq, hidden_size)`. Each captured tensor is the
    /// post-block hidden state (XLM-R is post-LN throughout
    /// like BERT, so the captures are already fully normalized).
    ///
    /// Mirrors
    /// [`crate::models::lazy_bert::BertModel::forward_intermediate_layers`]
    /// and the DistilBERT hook. Useful for multilingual
    /// layer-wise probing, multi-layer feature fusion on
    /// `sentence-transformers/paraphrase-multilingual-mpnet-base-v2`-shape
    /// checkpoints, and cross-lingual distillation.
    pub fn forward_intermediate_layers(
        &self,
        tokens: &[u32],
        layer_ids: &[usize],
        attention_mask: Option<&Tensor>,
    ) -> Result<Vec<Tensor>> {
        let cfg = &self.config;
        let weights = &self.weights;
        let seq = tokens.len();
        let batch = 1;
        assert!(seq > 0);
        // GAP-308 (CONFIG_FIELD): input length (plus RoBERTa's padding_idx offset)
        // vs config limit — reject, do not assert.
        if seq + (cfg.pad_token_id as usize) >= cfg.max_position_embeddings {
            return Err(fuel_ir::error::Error::Msg(format!(
                "seq + padding_idx + 1 ({}) exceeds max_position_embeddings ({})",
                seq + cfg.pad_token_id as usize + 1,
                cfg.max_position_embeddings,
            )));
        }
        assert!(!layer_ids.is_empty(), "layer_ids must not be empty");
        for w in layer_ids.windows(2) {
            assert!(w[0] < w[1], "layer_ids must be strictly increasing");
        }
        let depth = weights.layers.len();
        assert!(
            *layer_ids.last().unwrap() < depth,
            "layer_ids must all be in [0, num_hidden_layers = {depth})",
        );

        // Same embedding setup as `forward`.
        let word_emb_t = Tensor::from_f32(
            weights.word_embedding.clone(),
            Shape::from_dims(&[cfg.vocab_size, cfg.hidden_size]),
            &Device::cpu(),
        )?;
        let token_ids = word_emb_t.const_u32_like(tokens.to_vec(), Shape::from_dims(&[seq]))?;
        let word_embeds = word_emb_t
            .index_select(0_usize, &token_ids)?
            .reshape(Shape::from_dims(&[batch, seq, cfg.hidden_size]))?;
        let pad_off = cfg.pad_token_id as usize + 1;
        let position_ids_vec: Vec<u32> = (0..seq).map(|i| (pad_off + i) as u32).collect();
        let pos_full = word_emb_t.const_f32_like(
            Arc::clone(&weights.position_embedding),
            Shape::from_dims(&[cfg.max_position_embeddings, cfg.hidden_size]),
        )?;
        let pos_ids = word_emb_t.const_u32_like(position_ids_vec, Shape::from_dims(&[seq]))?;
        let pos_embeds = pos_full
            .index_select(0_usize, &pos_ids)?
            .reshape(Shape::from_dims(&[batch, seq, cfg.hidden_size]))?;
        let tt_full = word_emb_t.const_f32_like(
            Arc::clone(&weights.token_type_embedding),
            Shape::from_dims(&[cfg.type_vocab_size, cfg.hidden_size]),
        )?;
        let tt_ids = word_emb_t.const_u32_like(vec![0_u32; seq], Shape::from_dims(&[seq]))?;
        let tt_embeds = tt_full
            .index_select(0_usize, &tt_ids)?
            .reshape(Shape::from_dims(&[batch, seq, cfg.hidden_size]))?;
        let sum = word_embeds.add(&pos_embeds)?.add(&tt_embeds)?;
        let mut h = sum.layer_norm_affine(
            std::sync::Arc::clone(&weights.embed_ln_gain),
            std::sync::Arc::clone(&weights.embed_ln_bias),
            cfg.layer_norm_eps,
        )?;

        let mut out = Vec::with_capacity(layer_ids.len());
        let mut next_capture = 0;
        for (idx, layer) in weights.layers.iter().enumerate() {
            h = self.apply_layer(&h, layer, attention_mask)?;
            if next_capture < layer_ids.len() && layer_ids[next_capture] == idx {
                out.push(h.clone());
                next_capture += 1;
            }
        }
        Ok(out)
    }

    fn apply_layer(
        &self,
        x: &Tensor,
        layer: &XlmrLayerWeights,
        attention_mask: Option<&Tensor>,
    ) -> Result<Tensor> {
        let cfg = &self.config;
        let dims = x.shape();
        let dims = dims.dims();
        let batch = dims[0];
        let seq = dims[1];
        let d = cfg.hidden_size;
        let n_heads = cfg.num_attention_heads;
        let head_dim = cfg.head_dim();

        let q = layer.q_proj.apply_linear(x, d, d)?;
        let q = q.add_trailing_bias(std::sync::Arc::clone(&layer.q_proj_bias))?;
        let k = layer.k_proj.apply_linear(x, d, d)?;
        let k = k.add_trailing_bias(std::sync::Arc::clone(&layer.k_proj_bias))?;
        let v = layer.v_proj.apply_linear(x, d, d)?;
        let v = v.add_trailing_bias(std::sync::Arc::clone(&layer.v_proj_bias))?;

        let _ = (batch, seq);
        let q = q.split_heads(n_heads, head_dim)?;
        let k = k.split_heads(n_heads, head_dim)?;
        let v = v.split_heads(n_heads, head_dim)?;

        let k_t = k.transpose()?;
        let scale = 1.0_f64 / (head_dim as f64).sqrt();
        let scores = q.matmul(&k_t)?.mul_scalar(scale);
        let scores = match attention_mask {
            None => scores,
            Some(mask) => scores.broadcast_add(mask)?,
        };
        let probs = scores.softmax_last_dim()?;
        let ctx = probs.matmul(&v)?;
        let merged = ctx.merge_heads()?;
        let attn_out = layer.out_proj.apply_linear(&merged, d, d)?;
        let attn_out = attn_out.add_trailing_bias(std::sync::Arc::clone(&layer.out_proj_bias))?;

        // Post-LN: LN(attn + x).
        let h1 = x.add(&attn_out)?.layer_norm_affine(
            std::sync::Arc::clone(&layer.attn_ln_gain),
            std::sync::Arc::clone(&layer.attn_ln_bias),
            cfg.layer_norm_eps,
        )?;

        // FFN.
        let fc1 = layer.fc1.apply_linear(&h1, d, cfg.intermediate_size)?;
        let fc1 = fc1.add_trailing_bias(std::sync::Arc::clone(&layer.fc1_bias))?;
        let act = match cfg.hidden_activation {
            XlmrActivation::Gelu => fc1.gelu(),
            XlmrActivation::GeluPytorchTanh => fc1.gelu_tanh(),
            XlmrActivation::Relu => fc1.relu(),
            XlmrActivation::Silu => fc1.silu(),
        };
        let fc2 = layer.fc2.apply_linear(&act, cfg.intermediate_size, d)?;
        let fc2 = fc2.add_trailing_bias(std::sync::Arc::clone(&layer.fc2_bias))?;

        // Post-LN: LN(ffn + h1).
        h1.add(&fc2)?.layer_norm_affine(
            std::sync::Arc::clone(&layer.ffn_ln_gain),
            std::sync::Arc::clone(&layer.ffn_ln_bias),
            cfg.layer_norm_eps,
        )
    }
}

// ---- HuggingFace safetensors loader ----------------------------------------

impl XlmrWeights {
    /// Load XLM-RoBERTa (xlm-roberta-{base,large}) weights from a HuggingFace
    /// safetensors file. Naming follows the upstream RoBERTa layout at
    /// `roberta.embeddings.*` / `roberta.encoder.layer.{i}.*`.
    pub fn load_from_mmapped(
        st: &fuel_loaders::safetensors::MmapedSafetensors,
        cfg: &XlmrConfig,
    ) -> Result<Self> {
        use fuel_tensor::{
            lazy::load_tensor_as_f32, lazy::load_transposed_matrix_preserve_dtype as ltm,
        };
        let h = cfg.hidden_size;
        let inter = cfg.intermediate_size;

        // Some XLM-R checkpoints prefix with "roberta." and some don't;
        // probe both, prefer the prefixed form (standard HF).
        let prefix = if load_tensor_as_f32(st, "roberta.embeddings.word_embeddings.weight").is_ok()
        {
            "roberta."
        } else {
            ""
        };

        let word_embedding = Arc::from(load_tensor_as_f32(
            st,
            &format!("{prefix}embeddings.word_embeddings.weight"),
        )?);
        let position_embedding = Arc::from(load_tensor_as_f32(
            st,
            &format!("{prefix}embeddings.position_embeddings.weight"),
        )?);
        let token_type_embedding = Arc::from(load_tensor_as_f32(
            st,
            &format!("{prefix}embeddings.token_type_embeddings.weight"),
        )?);
        let embed_ln_gain = Arc::from(load_tensor_as_f32(
            st,
            &format!("{prefix}embeddings.LayerNorm.weight"),
        )?);
        let embed_ln_bias = Arc::from(load_tensor_as_f32(
            st,
            &format!("{prefix}embeddings.LayerNorm.bias"),
        )?);

        let mut layers = Vec::with_capacity(cfg.num_hidden_layers);
        for i in 0..cfg.num_hidden_layers {
            let p = format!("{prefix}encoder.layer.{i}");
            let q_proj = ltm(st, &format!("{p}.attention.self.query.weight"), h, h)?;
            let q_proj_bias = Arc::from(load_tensor_as_f32(
                st,
                &format!("{p}.attention.self.query.bias"),
            )?);
            let k_proj = ltm(st, &format!("{p}.attention.self.key.weight"), h, h)?;
            let k_proj_bias = Arc::from(load_tensor_as_f32(
                st,
                &format!("{p}.attention.self.key.bias"),
            )?);
            let v_proj = ltm(st, &format!("{p}.attention.self.value.weight"), h, h)?;
            let v_proj_bias = Arc::from(load_tensor_as_f32(
                st,
                &format!("{p}.attention.self.value.bias"),
            )?);
            let out_proj = ltm(st, &format!("{p}.attention.output.dense.weight"), h, h)?;
            let out_proj_bias = Arc::from(load_tensor_as_f32(
                st,
                &format!("{p}.attention.output.dense.bias"),
            )?);
            let attn_ln_gain = Arc::from(load_tensor_as_f32(
                st,
                &format!("{p}.attention.output.LayerNorm.weight"),
            )?);
            let attn_ln_bias = Arc::from(load_tensor_as_f32(
                st,
                &format!("{p}.attention.output.LayerNorm.bias"),
            )?);
            let fc1 = ltm(st, &format!("{p}.intermediate.dense.weight"), inter, h)?;
            let fc1_bias = Arc::from(load_tensor_as_f32(
                st,
                &format!("{p}.intermediate.dense.bias"),
            )?);
            let fc2 = ltm(st, &format!("{p}.output.dense.weight"), h, inter)?;
            let fc2_bias = Arc::from(load_tensor_as_f32(st, &format!("{p}.output.dense.bias"))?);
            let ffn_ln_gain = Arc::from(load_tensor_as_f32(
                st,
                &format!("{p}.output.LayerNorm.weight"),
            )?);
            let ffn_ln_bias = Arc::from(load_tensor_as_f32(
                st,
                &format!("{p}.output.LayerNorm.bias"),
            )?);
            layers.push(XlmrLayerWeights {
                q_proj,
                q_proj_bias,
                k_proj,
                k_proj_bias,
                v_proj,
                v_proj_bias,
                out_proj,
                out_proj_bias,
                attn_ln_gain,
                attn_ln_bias,
                fc1,
                fc1_bias,
                fc2,
                fc2_bias,
                ffn_ln_gain,
                ffn_ln_bias,
            });
        }

        Ok(Self {
            word_embedding,
            position_embedding,
            token_type_embedding,
            embed_ln_gain,
            embed_ln_bias,
            layers,
        })
    }
}

// ---- Masked LM head --------------------------------------------------------

/// Weights for the XLM-RoBERTa masked-language-model head.
///
/// HuggingFace layout (`xlm-roberta-base` / `xlm-roberta-large`):
///
///   - `lm_head.dense.weight`        `[hidden, hidden]`
///   - `lm_head.dense.bias`          `[hidden]`
///   - `lm_head.layer_norm.weight`   `[hidden]`
///   - `lm_head.layer_norm.bias`     `[hidden]`
///   - `lm_head.decoder.weight`      `[vocab, hidden]`
///   - `lm_head.decoder.bias`        `[vocab]`
///
/// The eager port tied `lm_head.decoder.weight` to
/// `roberta.embeddings.word_embeddings.weight` transposed. v1 of the
/// lazy port carries the decoder explicitly (HF checkpoints store the
/// tensor, so loading it is the simpler path), at the cost of a small
/// memory duplication. A `tied`-mode follow-up is welcome.
#[derive(Debug, Clone)]
pub struct ForMaskedLMWeights {
    pub lm_head_dense_weight: WeightStorage,
    pub lm_head_dense_bias: Arc<[f32]>,
    pub lm_head_ln_gain: Arc<[f32]>,
    pub lm_head_ln_bias: Arc<[f32]>,
    pub lm_head_decoder_weight: WeightStorage,
    pub lm_head_decoder_bias: Arc<[f32]>,
}

/// XLM-RoBERTa with a masked-language-model head on top of the base
/// encoder. Output shape: `(1, seq, vocab_size)`.
#[derive(Debug, Clone)]
pub struct XlmrForMaskedLM {
    pub base: XlmrModel,
    pub lm_head_dense_weight: WeightStorage,
    pub lm_head_dense_bias: Arc<[f32]>,
    pub lm_head_ln_gain: Arc<[f32]>,
    pub lm_head_ln_bias: Arc<[f32]>,
    pub lm_head_decoder_weight: WeightStorage,
    pub lm_head_decoder_bias: Arc<[f32]>,
}

impl XlmrForMaskedLM {
    /// Run a forward pass and return masked-LM logits over the vocab,
    /// shape `(1, seq, vocab_size)`.
    pub fn forward(&self, tokens: &[u32], attention_mask: Option<&Tensor>) -> Result<Tensor> {
        let cfg = &self.base.config;
        let h = self.base.forward(tokens, attention_mask)?;

        // lm_head: dense -> gelu_tanh -> layer_norm -> decoder.
        // `apply_linear` matmuls a `(1, seq, hidden)` activation by a
        // `(hidden, hidden)` weight to give `(1, seq, hidden)`.
        let dense = self
            .lm_head_dense_weight
            .apply_linear(&h, cfg.hidden_size, cfg.hidden_size)?;
        let dense = dense.add_trailing_bias(Arc::clone(&self.lm_head_dense_bias))?;
        let act = dense.gelu();
        let normed = act.layer_norm_affine(
            Arc::clone(&self.lm_head_ln_gain),
            Arc::clone(&self.lm_head_ln_bias),
            cfg.layer_norm_eps,
        )?;
        let logits =
            self.lm_head_decoder_weight
                .apply_linear(&normed, cfg.hidden_size, cfg.vocab_size)?;
        let logits = logits.add_trailing_bias(Arc::clone(&self.lm_head_decoder_bias))?;
        Ok(logits)
    }
}

impl ForMaskedLMWeights {
    /// Load the MLM-head tensors from a HuggingFace safetensors file
    /// using the standard `lm_head.*` naming. Tensor dtypes follow the
    /// existing XlmrWeights loader (F32 vectors via `load_tensor_as_f32`,
    /// transposed matrices via `load_transposed_matrix_preserve_dtype`).
    pub fn load_from_mmapped(
        st: &fuel_loaders::safetensors::MmapedSafetensors,
        cfg: &XlmrConfig,
    ) -> Result<Self> {
        use fuel_tensor::{
            lazy::load_tensor_as_f32, lazy::load_transposed_matrix_preserve_dtype as ltm,
        };
        let h = cfg.hidden_size;
        let v = cfg.vocab_size;
        let lm_head_dense_weight = ltm(st, "lm_head.dense.weight", h, h)?;
        let lm_head_dense_bias = Arc::from(load_tensor_as_f32(st, "lm_head.dense.bias")?);
        let lm_head_ln_gain = Arc::from(load_tensor_as_f32(st, "lm_head.layer_norm.weight")?);
        let lm_head_ln_bias = Arc::from(load_tensor_as_f32(st, "lm_head.layer_norm.bias")?);
        let lm_head_decoder_weight = ltm(st, "lm_head.decoder.weight", v, h)?;
        let lm_head_decoder_bias = Arc::from(load_tensor_as_f32(st, "lm_head.decoder.bias")?);
        Ok(Self {
            lm_head_dense_weight,
            lm_head_dense_bias,
            lm_head_ln_gain,
            lm_head_ln_bias,
            lm_head_decoder_weight,
            lm_head_decoder_bias,
        })
    }
}

impl XlmrForMaskedLM {
    /// Load the full MaskedLM model (base encoder + lm_head) from a
    /// HuggingFace safetensors file in one call.
    pub fn load_from_mmapped(
        st: &fuel_loaders::safetensors::MmapedSafetensors,
        cfg: XlmrConfig,
    ) -> Result<Self> {
        let base_weights = XlmrWeights::load_from_mmapped(st, &cfg)?;
        let head = ForMaskedLMWeights::load_from_mmapped(st, &cfg)?;
        Ok(Self {
            base: XlmrModel {
                config: cfg,
                weights: base_weights,
            },
            lm_head_dense_weight: head.lm_head_dense_weight,
            lm_head_dense_bias: head.lm_head_dense_bias,
            lm_head_ln_gain: head.lm_head_ln_gain,
            lm_head_ln_bias: head.lm_head_ln_bias,
            lm_head_decoder_weight: head.lm_head_decoder_weight,
            lm_head_decoder_bias: head.lm_head_decoder_bias,
        })
    }
}

// ---- Sequence classification head -----------------------------------------

/// Weights for the XLM-RoBERTa sequence-classification head.
///
/// HuggingFace layout (`xlm-roberta-{base,large}` fine-tunes):
///
///   - `classifier.dense.weight`     `[hidden, hidden]`
///   - `classifier.dense.bias`       `[hidden]`
///   - `classifier.out_proj.weight`  `[num_labels, hidden]`
///   - `classifier.out_proj.bias`    `[num_labels]`
#[derive(Debug, Clone)]
pub struct ForSequenceClassificationWeights {
    pub classifier_dense_weight: WeightStorage,
    pub classifier_dense_bias: Arc<[f32]>,
    pub classifier_out_proj_weight: WeightStorage,
    pub classifier_out_proj_bias: Arc<[f32]>,
}

/// XLM-RoBERTa with a sequence-classification head on top of the base
/// encoder. The eager port (and HF) take the first-token hidden state
/// (`<s>` / CLS), run it through a tanh-activated dense projection, and
/// then through an `out_proj` linear. Output shape: `(1, num_labels)`.
#[derive(Debug, Clone)]
pub struct XlmrForSequenceClassification {
    pub base: XlmrModel,
    pub num_labels: usize,
    pub classifier_dense_weight: WeightStorage,
    pub classifier_dense_bias: Arc<[f32]>,
    pub classifier_out_proj_weight: WeightStorage,
    pub classifier_out_proj_bias: Arc<[f32]>,
}

impl XlmrForSequenceClassification {
    /// Run a forward pass and return per-label logits of shape
    /// `(1, num_labels)`.
    pub fn forward(&self, tokens: &[u32], attention_mask: Option<&Tensor>) -> Result<Tensor> {
        let cfg = &self.base.config;
        let h = self.base.forward(tokens, attention_mask)?;
        // First-token hidden state: (1, seq, hidden) -> slice along dim
        // 1 starting at 0, length 1 -> (1, 1, hidden) -> reshape to
        // (1, hidden).
        let cls = h
            .slice(1_usize, 0, 1)?
            .reshape(Shape::from_dims(&[1, cfg.hidden_size]))?;
        // dense (hidden -> hidden) + bias + tanh.
        let dense =
            self.classifier_dense_weight
                .apply_linear(&cls, cfg.hidden_size, cfg.hidden_size)?;
        let dense = dense.add_trailing_bias(Arc::clone(&self.classifier_dense_bias))?;
        let act = dense.tanh();
        // out_proj (hidden -> num_labels) + bias.
        let logits =
            self.classifier_out_proj_weight
                .apply_linear(&act, cfg.hidden_size, self.num_labels)?;
        let logits = logits.add_trailing_bias(Arc::clone(&self.classifier_out_proj_bias))?;
        Ok(logits)
    }
}

impl ForSequenceClassificationWeights {
    /// Load the classifier head tensors using the HF
    /// `classifier.{dense,out_proj}.{weight,bias}` naming.
    pub fn load_from_mmapped(
        st: &fuel_loaders::safetensors::MmapedSafetensors,
        cfg: &XlmrConfig,
        num_labels: usize,
    ) -> Result<Self> {
        use fuel_tensor::{
            lazy::load_tensor_as_f32, lazy::load_transposed_matrix_preserve_dtype as ltm,
        };
        let h = cfg.hidden_size;
        let classifier_dense_weight = ltm(st, "classifier.dense.weight", h, h)?;
        let classifier_dense_bias = Arc::from(load_tensor_as_f32(st, "classifier.dense.bias")?);
        let classifier_out_proj_weight = ltm(st, "classifier.out_proj.weight", num_labels, h)?;
        let classifier_out_proj_bias =
            Arc::from(load_tensor_as_f32(st, "classifier.out_proj.bias")?);
        Ok(Self {
            classifier_dense_weight,
            classifier_dense_bias,
            classifier_out_proj_weight,
            classifier_out_proj_bias,
        })
    }
}

impl XlmrForSequenceClassification {
    /// Load the full sequence-classification model (base encoder +
    /// classifier head) from a HuggingFace safetensors file in one call.
    pub fn load_from_mmapped(
        st: &fuel_loaders::safetensors::MmapedSafetensors,
        cfg: XlmrConfig,
        num_labels: usize,
    ) -> Result<Self> {
        let base_weights = XlmrWeights::load_from_mmapped(st, &cfg)?;
        let head = ForSequenceClassificationWeights::load_from_mmapped(st, &cfg, num_labels)?;
        Ok(Self {
            base: XlmrModel {
                config: cfg,
                weights: base_weights,
            },
            num_labels,
            classifier_dense_weight: head.classifier_dense_weight,
            classifier_dense_bias: head.classifier_dense_bias,
            classifier_out_proj_weight: head.classifier_out_proj_weight,
            classifier_out_proj_bias: head.classifier_out_proj_bias,
        })
    }
}

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

    fn vec_of(n: usize, next: &mut dyn FnMut() -> f32) -> Arc<[f32]> {
        Arc::from((0..n).map(|_| next()).collect::<Vec<_>>())
    }

    /// GAP-308 born-red: a sequence long enough that `seq + padding_idx >=
    /// max_position_embeddings` is REJECTED with a typed error (RoBERTa offsets
    /// positions by `pad_token_id + 1`). Asserts the MESSAGE fragment, not
    /// is_err(); the decline returns before any graph op.
    #[test]
    fn forward_declines_seq_over_max_position_embeddings() {
        let mut cfg = tiny_cfg();
        cfg.max_position_embeddings = 8; // pad_token_id = 1, so seq + 1 >= 8 declines at seq >= 7
        let model = XlmrModel {
            config: cfg.clone(),
            weights: tiny_weights(&cfg),
        };
        // max_position_embeddings = 8, pad_token_id = 1. Positions are offset by
        // pad+1 = 2, so seq = 7 overflows the size-8 table and is the FIRST
        // rejected length — rejected ONLY because of the +pad offset. A plain
        // `seq <= max` guard would ALLOW seq = 7, so testing at 7 (not >= 8) is
        // what makes this born-red sensitive to the offset rather than a generic seq>max.
        // POSITIVE CONTROL: seq + pad < max must run.
        let ok_seq: Vec<u32> = (0..6).collect(); // 6 + 1 = 7 < 8 -> runs
        model
            .forward(&ok_seq, None)
            .expect("control: seq + pad < max_position_embeddings must run");
        let over: Vec<u32> = (0..7).collect(); // 7 + 1 = 8 >= 8 -> declines via the +pad offset
        let err = model.forward(&over, None).unwrap_err();
        assert!(
            format!("{err}").contains("max_position_embeddings"),
            "forward must decline seq + pad >= max_position_embeddings; got: {err}"
        );
        let err2 = model
            .forward_intermediate_layers(&over, &[0], None)
            .unwrap_err();
        assert!(
            format!("{err2}").contains("max_position_embeddings"),
            "forward_intermediate_layers must decline too; got: {err2}"
        );
    }

    fn tiny_cfg() -> XlmrConfig {
        XlmrConfig {
            vocab_size: 32,
            hidden_size: 16,
            num_hidden_layers: 2,
            num_attention_heads: 4,
            intermediate_size: 32,
            max_position_embeddings: 16,
            type_vocab_size: 1,
            hidden_activation: XlmrActivation::Gelu,
            layer_norm_eps: 1e-12,
            pad_token_id: 1,
        }
    }

    fn tiny_weights(cfg: &XlmrConfig) -> XlmrWeights {
        let mut s: u32 = 31313;
        let next = move || -> f32 {
            s = s.wrapping_mul(1103515245).wrapping_add(12345);
            ((s >> 16) as u16 as f32 / 65535.0 - 0.5) * 0.05
        };
        let mut nb: Box<dyn FnMut() -> f32> = Box::new(next);
        let d = cfg.hidden_size;
        let word_embedding = vec_of(cfg.vocab_size * d, &mut *nb);
        let position_embedding = vec_of(cfg.max_position_embeddings * d, &mut *nb);
        let token_type_embedding = vec_of(cfg.type_vocab_size * d, &mut *nb);
        let embed_ln_gain = Arc::from(vec![1.0_f32; d]);
        let embed_ln_bias = Arc::from(vec![0.0_f32; d]);

        let layers: Vec<XlmrLayerWeights> = (0..cfg.num_hidden_layers)
            .map(|_| XlmrLayerWeights {
                q_proj: WeightStorage::F32(vec_of(d * d, &mut *nb)),
                q_proj_bias: vec_of(d, &mut *nb),
                k_proj: WeightStorage::F32(vec_of(d * d, &mut *nb)),
                k_proj_bias: vec_of(d, &mut *nb),
                v_proj: WeightStorage::F32(vec_of(d * d, &mut *nb)),
                v_proj_bias: vec_of(d, &mut *nb),
                out_proj: WeightStorage::F32(vec_of(d * d, &mut *nb)),
                out_proj_bias: vec_of(d, &mut *nb),
                attn_ln_gain: Arc::from(vec![1.0_f32; d]),
                attn_ln_bias: Arc::from(vec![0.0_f32; d]),
                fc1: WeightStorage::F32(vec_of(d * cfg.intermediate_size, &mut *nb)),
                fc1_bias: vec_of(cfg.intermediate_size, &mut *nb),
                fc2: WeightStorage::F32(vec_of(cfg.intermediate_size * d, &mut *nb)),
                fc2_bias: vec_of(d, &mut *nb),
                ffn_ln_gain: Arc::from(vec![1.0_f32; d]),
                ffn_ln_bias: Arc::from(vec![0.0_f32; d]),
            })
            .collect();

        XlmrWeights {
            word_embedding,
            position_embedding,
            token_type_embedding,
            embed_ln_gain,
            embed_ln_bias,
            layers,
        }
    }

    #[test]
    fn forward_shape_and_finite() {
        let cfg = tiny_cfg();
        let model = XlmrModel {
            config: cfg.clone(),
            weights: tiny_weights(&cfg),
        };
        let tokens = [1_u32, 2, 3, 4];
        let out = model.forward(&tokens, None).unwrap();
        assert_eq!(out.shape().dims(), &[1, tokens.len(), cfg.hidden_size]);
        for &v in &out.realize_f32() {
            assert!(v.is_finite());
        }
    }

    /// Token type embedding is wired: changing it must alter
    /// the output.
    #[test]
    fn token_type_embedding_is_wired() {
        let cfg = tiny_cfg();
        let base = tiny_weights(&cfg);
        let mut modified = base.clone();
        let new_tt: Vec<f32> = (0..cfg.type_vocab_size * cfg.hidden_size)
            .map(|_| 0.5)
            .collect();
        modified.token_type_embedding = Arc::from(new_tt);
        let m_base = XlmrModel {
            config: cfg.clone(),
            weights: base,
        };
        let m_mod = XlmrModel {
            config: cfg.clone(),
            weights: modified,
        };
        let toks = [1_u32, 2, 3];
        let a = m_base.forward(&toks, None).unwrap().realize_f32();
        let b = m_mod.forward(&toks, None).unwrap().realize_f32();
        let mut max_diff = 0.0_f32;
        for (x, y) in a.iter().zip(b.iter()) {
            max_diff = max_diff.max((x - y).abs());
        }
        assert!(
            max_diff > 1e-6,
            "token type embedding must affect output, max_diff = {max_diff}"
        );
    }

    /// RoBERTa position-id convention: position_ids start at
    /// `padding_idx + 1`. Verify by checking that modifying the
    /// position embedding row at `padding_idx + 1` alters the
    /// output (proves we're reading the right row).
    #[test]
    fn roberta_position_offset() {
        let cfg = tiny_cfg();
        let base = tiny_weights(&cfg);
        let mut modified = base.clone();
        // Modify the position embedding at row padding_idx + 1
        // (= 2 for pad_token_id=1). This is the row index that
        // our forward should read for token at position 0.
        let target_row = cfg.pad_token_id as usize + 1;
        let mut pe_vec = (*base.position_embedding).to_vec();
        for i in 0..cfg.hidden_size {
            pe_vec[target_row * cfg.hidden_size + i] = 1.0;
        }
        modified.position_embedding = Arc::from(pe_vec);
        let m_base = XlmrModel {
            config: cfg.clone(),
            weights: base,
        };
        let m_mod = XlmrModel {
            config: cfg.clone(),
            weights: modified,
        };
        let toks = [1_u32, 2, 3];
        let a = m_base.forward(&toks, None).unwrap().realize_f32();
        let b = m_mod.forward(&toks, None).unwrap().realize_f32();
        let mut max_diff = 0.0_f32;
        for (x, y) in a.iter().zip(b.iter()) {
            max_diff = max_diff.max((x - y).abs());
        }
        assert!(
            max_diff > 1e-6,
            "RoBERTa position offset (modifying row pad_idx+1) must affect output, max_diff = {max_diff}"
        );
    }

    // ---- Head fixtures ---------------------------------------------------

    fn tiny_for_masked_lm(cfg: &XlmrConfig) -> XlmrForMaskedLM {
        let mut s: u32 = 71717;
        let next = move || -> f32 {
            s = s.wrapping_mul(1103515245).wrapping_add(12345);
            ((s >> 16) as u16 as f32 / 65535.0 - 0.5) * 0.05
        };
        let mut nb: Box<dyn FnMut() -> f32> = Box::new(next);
        let h = cfg.hidden_size;
        let v = cfg.vocab_size;
        XlmrForMaskedLM {
            base: XlmrModel {
                config: cfg.clone(),
                weights: tiny_weights(cfg),
            },
            lm_head_dense_weight: WeightStorage::F32(vec_of(h * h, &mut *nb)),
            lm_head_dense_bias: vec_of(h, &mut *nb),
            lm_head_ln_gain: Arc::from(vec![1.0_f32; h]),
            lm_head_ln_bias: Arc::from(vec![0.0_f32; h]),
            lm_head_decoder_weight: WeightStorage::F32(vec_of(h * v, &mut *nb)),
            lm_head_decoder_bias: vec_of(v, &mut *nb),
        }
    }

    fn tiny_for_sequence_classification(
        cfg: &XlmrConfig,
        num_labels: usize,
    ) -> XlmrForSequenceClassification {
        let mut s: u32 = 90909;
        let next = move || -> f32 {
            s = s.wrapping_mul(1103515245).wrapping_add(12345);
            ((s >> 16) as u16 as f32 / 65535.0 - 0.5) * 0.05
        };
        let mut nb: Box<dyn FnMut() -> f32> = Box::new(next);
        let h = cfg.hidden_size;
        XlmrForSequenceClassification {
            base: XlmrModel {
                config: cfg.clone(),
                weights: tiny_weights(cfg),
            },
            num_labels,
            classifier_dense_weight: WeightStorage::F32(vec_of(h * h, &mut *nb)),
            classifier_dense_bias: vec_of(h, &mut *nb),
            classifier_out_proj_weight: WeightStorage::F32(vec_of(h * num_labels, &mut *nb)),
            classifier_out_proj_bias: vec_of(num_labels, &mut *nb),
        }
    }

    /// `XlmrForMaskedLM::forward` returns logits of shape
    /// `(1, seq, vocab_size)` and all outputs are finite.
    #[test]
    fn for_masked_lm_forward_shape() {
        let cfg = tiny_cfg();
        let model = tiny_for_masked_lm(&cfg);
        let toks = [1_u32, 2, 3, 4, 5];
        let out = model.forward(&toks, None).unwrap();
        assert_eq!(out.shape().dims(), &[1, toks.len(), cfg.vocab_size]);
        for &v in &out.realize_f32() {
            assert!(v.is_finite(), "non-finite MLM logit: {v}");
        }
    }

    /// `XlmrForSequenceClassification::forward` returns logits of
    /// shape `(1, num_labels)` and all outputs are finite.
    #[test]
    fn for_sequence_classification_forward_shape() {
        let cfg = tiny_cfg();
        let num_labels = 3;
        let model = tiny_for_sequence_classification(&cfg, num_labels);
        let toks = [1_u32, 2, 3, 4];
        let out = model.forward(&toks, None).unwrap();
        assert_eq!(out.shape().dims(), &[1, num_labels]);
        for &v in &out.realize_f32() {
            assert!(v.is_finite(), "non-finite classifier logit: {v}");
        }
    }

    // ---- Safetensors round-trip fixtures --------------------------------

    /// Append a multi-dim f32 tensor of given shape, filled by `nb()`.
    fn push_f32(
        owned: &mut Vec<(String, Vec<usize>, Vec<u8>)>,
        name: &str,
        shape: Vec<usize>,
        nb: &mut dyn FnMut() -> f32,
    ) {
        let n: usize = shape.iter().product();
        let mut bytes = Vec::with_capacity(n * 4);
        for _ in 0..n {
            bytes.extend_from_slice(&nb().to_le_bytes());
        }
        owned.push((name.to_string(), shape, bytes));
    }

    /// Push a HuggingFace LayerNorm prefix (`<prefix>.{weight,bias}`).
    fn push_ln(
        owned: &mut Vec<(String, Vec<usize>, Vec<u8>)>,
        prefix: &str,
        c: usize,
        nb: &mut dyn FnMut() -> f32,
    ) {
        push_f32(owned, &format!("{prefix}.weight"), vec![c], nb);
        push_f32(owned, &format!("{prefix}.bias"), vec![c], nb);
    }

    /// Push an HF Linear (`<prefix>.{weight,bias}`). HF stores weight
    /// as `[out, in]`.
    fn push_linear(
        owned: &mut Vec<(String, Vec<usize>, Vec<u8>)>,
        prefix: &str,
        in_features: usize,
        out_features: usize,
        nb: &mut dyn FnMut() -> f32,
    ) {
        push_f32(
            owned,
            &format!("{prefix}.weight"),
            vec![out_features, in_features],
            nb,
        );
        push_f32(owned, &format!("{prefix}.bias"), vec![out_features], nb);
    }

    fn push_base_encoder(
        owned: &mut Vec<(String, Vec<usize>, Vec<u8>)>,
        cfg: &XlmrConfig,
        nb: &mut dyn FnMut() -> f32,
    ) {
        let prefix = "roberta.";
        // Embeddings.
        push_f32(
            owned,
            &format!("{prefix}embeddings.word_embeddings.weight"),
            vec![cfg.vocab_size, cfg.hidden_size],
            nb,
        );
        push_f32(
            owned,
            &format!("{prefix}embeddings.position_embeddings.weight"),
            vec![cfg.max_position_embeddings, cfg.hidden_size],
            nb,
        );
        push_f32(
            owned,
            &format!("{prefix}embeddings.token_type_embeddings.weight"),
            vec![cfg.type_vocab_size, cfg.hidden_size],
            nb,
        );
        push_ln(
            owned,
            &format!("{prefix}embeddings.LayerNorm"),
            cfg.hidden_size,
            nb,
        );
        // Encoder layers.
        for i in 0..cfg.num_hidden_layers {
            let p = format!("{prefix}encoder.layer.{i}");
            push_linear(
                owned,
                &format!("{p}.attention.self.query"),
                cfg.hidden_size,
                cfg.hidden_size,
                nb,
            );
            push_linear(
                owned,
                &format!("{p}.attention.self.key"),
                cfg.hidden_size,
                cfg.hidden_size,
                nb,
            );
            push_linear(
                owned,
                &format!("{p}.attention.self.value"),
                cfg.hidden_size,
                cfg.hidden_size,
                nb,
            );
            push_linear(
                owned,
                &format!("{p}.attention.output.dense"),
                cfg.hidden_size,
                cfg.hidden_size,
                nb,
            );
            push_ln(
                owned,
                &format!("{p}.attention.output.LayerNorm"),
                cfg.hidden_size,
                nb,
            );
            push_linear(
                owned,
                &format!("{p}.intermediate.dense"),
                cfg.hidden_size,
                cfg.intermediate_size,
                nb,
            );
            push_linear(
                owned,
                &format!("{p}.output.dense"),
                cfg.intermediate_size,
                cfg.hidden_size,
                nb,
            );
            push_ln(owned, &format!("{p}.output.LayerNorm"), cfg.hidden_size, nb);
        }
    }

    fn build_safetensors_file(
        owned: Vec<(String, Vec<usize>, Vec<u8>)>,
        tag: &str,
    ) -> std::path::PathBuf {
        use safetensors::tensor::TensorView;
        use std::collections::HashMap;
        let mut tensors: HashMap<String, TensorView<'_>> = HashMap::new();
        for (name, shape, bytes) in &owned {
            let view = TensorView::new(safetensors::Dtype::F32, shape.clone(), bytes)
                .expect("TensorView::new");
            tensors.insert(name.clone(), view);
        }
        let serialized = safetensors::serialize(&tensors, None).expect("safetensors::serialize");
        let tmp = std::env::temp_dir().join(format!(
            "fuel_xlmr_heads_{}_{tag}_{}.safetensors",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos(),
        ));
        std::fs::write(&tmp, &serialized).expect("write tmp");
        tmp
    }

    fn rng_seed(seed: u32) -> impl FnMut() -> f32 {
        let mut s = seed;
        move || {
            s = s.wrapping_mul(1103515245).wrapping_add(12345);
            ((s >> 16) as u16 as f32 / 65535.0 - 0.5) * 0.05
        }
    }

    /// Round-trip the MaskedLM head through safetensors using the
    /// standard HF naming and check the forward shape.
    #[test]
    fn for_masked_lm_load_from_mmapped_round_trip() {
        let cfg = tiny_cfg();
        let mut nb = rng_seed(12345);

        let mut owned: Vec<(String, Vec<usize>, Vec<u8>)> = Vec::new();
        push_base_encoder(&mut owned, &cfg, &mut nb);
        // lm_head.
        push_linear(
            &mut owned,
            "lm_head.dense",
            cfg.hidden_size,
            cfg.hidden_size,
            &mut nb,
        );
        push_ln(&mut owned, "lm_head.layer_norm", cfg.hidden_size, &mut nb);
        push_linear(
            &mut owned,
            "lm_head.decoder",
            cfg.hidden_size,
            cfg.vocab_size,
            &mut nb,
        );

        let tmp = build_safetensors_file(owned, "mlm");
        let st = unsafe { fuel_loaders::safetensors::MmapedSafetensors::new(&tmp) }
            .expect("MmapedSafetensors::new");

        let model = XlmrForMaskedLM::load_from_mmapped(&st, cfg.clone())
            .expect("XlmrForMaskedLM::load_from_mmapped");

        let toks = [1_u32, 2, 3, 4];
        let out = model.forward(&toks, None).unwrap();
        assert_eq!(out.shape().dims(), &[1, toks.len(), cfg.vocab_size]);
        for &v in &out.realize_f32() {
            assert!(v.is_finite(), "non-finite MLM logit from loaded model: {v}");
        }

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

    /// Round-trip the SequenceClassification head through safetensors
    /// using the standard HF naming and check the forward shape.
    #[test]
    fn for_sequence_classification_load_from_mmapped_round_trip() {
        let cfg = tiny_cfg();
        let num_labels = 4;
        let mut nb = rng_seed(54321);

        let mut owned: Vec<(String, Vec<usize>, Vec<u8>)> = Vec::new();
        push_base_encoder(&mut owned, &cfg, &mut nb);
        push_linear(
            &mut owned,
            "classifier.dense",
            cfg.hidden_size,
            cfg.hidden_size,
            &mut nb,
        );
        push_linear(
            &mut owned,
            "classifier.out_proj",
            cfg.hidden_size,
            num_labels,
            &mut nb,
        );

        let tmp = build_safetensors_file(owned, "seq");
        let st = unsafe { fuel_loaders::safetensors::MmapedSafetensors::new(&tmp) }
            .expect("MmapedSafetensors::new");

        let model = XlmrForSequenceClassification::load_from_mmapped(&st, cfg.clone(), num_labels)
            .expect("XlmrForSequenceClassification::load_from_mmapped");

        let toks = [1_u32, 2, 3, 4, 5];
        let out = model.forward(&toks, None).unwrap();
        assert_eq!(out.shape().dims(), &[1, num_labels]);
        for &v in &out.realize_f32() {
            assert!(
                v.is_finite(),
                "non-finite classifier logit from loaded model: {v}"
            );
        }

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

    /// `forward_intermediate_layers` returns one tensor per
    /// requested layer index, each shaped `(1, seq, hidden_size)`.
    #[test]
    fn forward_intermediate_layers_shape() {
        let cfg = tiny_cfg();
        let model = XlmrModel {
            config: cfg.clone(),
            weights: tiny_weights(&cfg),
        };
        let toks = [1_u32, 2, 3];
        let outs = model
            .forward_intermediate_layers(&toks, &[0_usize, 1], None)
            .unwrap();
        assert_eq!(outs.len(), 2);
        for out in &outs {
            assert_eq!(out.shape().dims(), &[1, toks.len(), cfg.hidden_size]);
            for &v in &out.realize_f32() {
                assert!(v.is_finite(), "non-finite intermediate: {v}");
            }
        }
        let a = outs[0].realize_f32();
        let b = outs[1].realize_f32();
        let mut max_diff = 0.0_f32;
        for (x, y) in a.iter().zip(b.iter()) {
            max_diff = max_diff.max((x - y).abs());
        }
        assert!(
            max_diff > 1e-7,
            "layer 0 and layer 1 intermediates must differ, max_diff = {max_diff}"
        );
    }
}