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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! DistilBERT (distilled BERT) ported to the lazy-graph API.
//!
//! Sanh et al. 2019. Distilled, smaller, faster, lighter version
//! of BERT. Used for many encoder downstream tasks (sentence
//! embeddings, classification, NER, etc.). v1 ports the
//! `DistilBertModel` (embeddings + transformer encoder)
//! returning per-token hidden states.
//!
//! Architecture:
//!
//!   1. **Embeddings**: word + absolute position lookup, sum,
//!      LayerNorm. NO token type embeddings (vs. BERT).
//!   2. **TransformerBlock (post-LN)**: `out = LN(x +
//!      attn(x)); out = LN(out + ffn(out))`. The LayerNorm is
//!      applied AFTER the residual sum, matching the original
//!      BERT convention.
//!   3. **MultiHeadSelfAttention**: separate Q/K/V/O linears
//!      (NO fused QKV); Q scaled by `1/sqrt(head_dim)` at the
//!      Q projection (not at the score matmul); optional
//!      additive attention mask broadcast-added to scores.
//!   4. **FFN**: `Linear → activation (GELU/ReLU) → Linear`.
//!      No SwiGLU; no gating.
//!   5. **No CLS pooling head in the base model** — the per-
//!      token hidden states are returned. Downstream task
//!      heads (NSP, MLM, etc.) sit on top of the base model.
//!
//! # Scope (v1)
//!
//! Forward-only, batch == 1, F32. The optional attention
//! mask is an additive `(1, 1, seq, seq)` tensor with `0` for
//! keep and `-inf` for mask. The caller is responsible for
//! constructing it (e.g., from a pad-token mask).

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 DistilBertActivation {
    Gelu,
    Relu,
}

#[derive(Debug, Clone, PartialEq)]
pub struct DistilBertConfig {
    pub vocab_size: usize,
    pub dim: usize,
    pub n_layers: usize,
    pub n_heads: usize,
    pub hidden_dim: usize,
    pub activation: DistilBertActivation,
    pub max_position_embeddings: usize,
    pub layer_norm_eps: f64,
}

impl DistilBertConfig {
    pub fn head_dim(&self) -> usize {
        self.dim / self.n_heads
    }

    /// `distilbert-base-uncased`.
    pub fn distilbert_base() -> Self {
        Self {
            vocab_size: 30522,
            dim: 768,
            n_layers: 6,
            n_heads: 12,
            hidden_dim: 3072,
            activation: DistilBertActivation::Gelu,
            max_position_embeddings: 512,
            layer_norm_eps: 1e-12,
        }
    }
}

#[derive(Debug, Clone)]
pub struct DistilBertLayerWeights {
    pub q_lin: WeightStorage,
    pub q_lin_bias: Arc<[f32]>,
    pub k_lin: WeightStorage,
    pub k_lin_bias: Arc<[f32]>,
    pub v_lin: WeightStorage,
    pub v_lin_bias: Arc<[f32]>,
    pub out_lin: WeightStorage,
    pub out_lin_bias: Arc<[f32]>,
    pub sa_ln_gain: Arc<[f32]>,
    pub sa_ln_bias: Arc<[f32]>,
    pub lin1: WeightStorage,
    pub lin1_bias: Arc<[f32]>,
    pub lin2: WeightStorage,
    pub lin2_bias: Arc<[f32]>,
    pub output_ln_gain: Arc<[f32]>,
    pub output_ln_bias: Arc<[f32]>,
}

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

#[derive(Debug, Clone)]
pub struct DistilBertModel {
    pub config: DistilBertConfig,
    pub weights: DistilBertWeights,
}

impl DistilBertModel {
    /// Run a forward pass with an optional additive attention
    /// mask of shape `(1, 1, seq, seq)`. Returns the per-token
    /// hidden states of shape `(1, seq, dim)`.
    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);
        // GAP-308 (CONFIG_FIELD): input length vs config limit — reject, do not assert.
        if seq > cfg.max_position_embeddings {
            return Err(fuel_ir::error::Error::Msg(format!(
                "DistilBertModel::forward: seq {seq} > max_position_embeddings {}",
                cfg.max_position_embeddings,
            )));
        }

        // Embeddings: word + position, sum, LayerNorm.
        let word_emb_t = Tensor::from_f32(
            weights.word_embedding.clone(),
            Shape::from_dims(&[cfg.vocab_size, cfg.dim]),
            &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.dim]))?;
        let pos_full = word_emb_t.const_f32_like(
            Arc::clone(&weights.position_embedding),
            Shape::from_dims(&[cfg.max_position_embeddings, cfg.dim]),
        )?;
        let pos_slice = pos_full
            .slice(0_usize, 0, seq)?
            .reshape(Shape::from_dims(&[1, seq, cfg.dim]))?;
        let pos_bc = pos_slice.broadcast_to(Shape::from_dims(&[batch, seq, cfg.dim]))?;
        let sum = word_embeds.add(&pos_bc)?;
        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 blocks.
        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, dim)`.
    /// Each captured tensor is the post-block hidden state
    /// (DistilBERT is post-LN throughout, so it's already
    /// fully normalized).
    ///
    /// Layer-id contract: 0-based, strictly increasing, all in
    /// `[0, n_layers)`. Mirrors the
    /// [`crate::models::lazy_bert::BertModel::forward_intermediate_layers`]
    /// hook contract.
    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 vs config limit — reject, do not assert.
        if seq > cfg.max_position_embeddings {
            return Err(fuel_ir::error::Error::Msg(format!(
                "DistilBertModel::forward_intermediate_layers: seq {seq} > max_position_embeddings {}",
                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, n_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.dim]),
            &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.dim]))?;
        let pos_full = word_emb_t.const_f32_like(
            Arc::clone(&weights.position_embedding),
            Shape::from_dims(&[cfg.max_position_embeddings, cfg.dim]),
        )?;
        let pos_slice = pos_full
            .slice(0_usize, 0, seq)?
            .reshape(Shape::from_dims(&[1, seq, cfg.dim]))?;
        let pos_bc = pos_slice.broadcast_to(Shape::from_dims(&[batch, seq, cfg.dim]))?;
        let sum = word_embeds.add(&pos_bc)?;
        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: &DistilBertLayerWeights,
        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.dim;
        let n_heads = cfg.n_heads;
        let head_dim = cfg.head_dim();

        // ---- Self-attention -----------------------------------------------
        let q = layer.q_lin.apply_linear(x, d, d)?;
        let q = q.add_trailing_bias(std::sync::Arc::clone(&layer.q_lin_bias))?;
        let k = layer.k_lin.apply_linear(x, d, d)?;
        let k = k.add_trailing_bias(std::sync::Arc::clone(&layer.k_lin_bias))?;
        let v = layer.v_lin.apply_linear(x, d, d)?;
        let v = v.add_trailing_bias(std::sync::Arc::clone(&layer.v_lin_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)?;

        // Q-scale (matches eager: scaling applied to Q, not the matmul).
        let q_scaled = q.mul_scalar(1.0 / (head_dim as f64).sqrt());
        let k_t = k.transpose()?;
        let scores = q_scaled.matmul(&k_t)?;
        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_lin.apply_linear(&merged, d, d)?;
        let attn_out = attn_out.add_trailing_bias(std::sync::Arc::clone(&layer.out_lin_bias))?;

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

        // ---- FFN ----------------------------------------------------------
        let fc1 = layer.lin1.apply_linear(&h1, d, cfg.hidden_dim)?;
        let fc1 = fc1.add_trailing_bias(std::sync::Arc::clone(&layer.lin1_bias))?;
        let act = match cfg.activation {
            DistilBertActivation::Gelu => fc1.gelu(),
            DistilBertActivation::Relu => fc1.relu(),
        };
        let fc2 = layer.lin2.apply_linear(&act, cfg.hidden_dim, d)?;
        let fc2 = fc2.add_trailing_bias(std::sync::Arc::clone(&layer.lin2_bias))?;

        // Post-LN: LN(ffn + h1).
        let h2 = h1.add(&fc2)?.layer_norm_affine(
            std::sync::Arc::clone(&layer.output_ln_gain),
            std::sync::Arc::clone(&layer.output_ln_bias),
            cfg.layer_norm_eps,
        )?;
        Ok(h2)
    }
}

impl DistilBertWeights {
    /// Load DistilBERT weights from a memory-mapped safetensors file
    /// using the standard HuggingFace naming convention.
    ///
    /// Expected tensor names (with optional `distilbert.` prefix):
    ///
    /// * `embeddings.word_embeddings.weight`
    /// * `embeddings.position_embeddings.weight`
    /// * `embeddings.LayerNorm.{weight,bias}`
    /// * `transformer.layer.{i}.attention.{q_lin,k_lin,v_lin,out_lin}.{weight,bias}`
    /// * `transformer.layer.{i}.sa_layer_norm.{weight,bias}`
    /// * `transformer.layer.{i}.ffn.{lin1,lin2}.{weight,bias}`
    /// * `transformer.layer.{i}.output_layer_norm.{weight,bias}`
    ///
    /// Linear weights are stored in HuggingFace's `[out, in]` layout
    /// and transposed to `[in, out]` for fuel-graph's `matmul`. BF16
    /// linears are preserved through `WeightStorage::BF16` to avoid
    /// the 2× memory tax of an f32 copy; all other tensors (1-D
    /// gains/biases, embedding tables) round-trip through f32.
    pub fn load_from_mmapped(
        st: &fuel_loaders::safetensors::MmapedSafetensors,
        cfg: &DistilBertConfig,
    ) -> Result<Self> {
        use fuel_tensor::{lazy::load_tensor_as_f32, lazy::load_transposed_matrix_preserve_dtype};

        // Prefix detection: HF's `distilbert-base-uncased` and
        // friends save weights under the `distilbert.` wrapper, but
        // task-finetuned checkpoints sometimes drop it. Probe both.
        let prefix = {
            let probe = "distilbert.embeddings.word_embeddings.weight";
            if st.get(probe).is_ok() {
                "distilbert."
            } else {
                ""
            }
        };

        let d = cfg.dim;
        let hidden = cfg.hidden_dim;

        let word_embedding =
            load_tensor_as_f32(st, &format!("{prefix}embeddings.word_embeddings.weight"))?;
        if word_embedding.len() != cfg.vocab_size * d {
            fuel_ir::bail!(
                "{prefix}embeddings.word_embeddings.weight: {} elts, expected {} ({}×{})",
                word_embedding.len(),
                cfg.vocab_size * d,
                cfg.vocab_size,
                d,
            );
        }
        let position_embedding = load_tensor_as_f32(
            st,
            &format!("{prefix}embeddings.position_embeddings.weight"),
        )?;
        if position_embedding.len() != cfg.max_position_embeddings * d {
            fuel_ir::bail!(
                "{prefix}embeddings.position_embeddings.weight: {} elts, expected {} ({}×{})",
                position_embedding.len(),
                cfg.max_position_embeddings * d,
                cfg.max_position_embeddings,
                d,
            );
        }
        let embed_ln_gain =
            load_tensor_as_f32(st, &format!("{prefix}embeddings.LayerNorm.weight"))?;
        let embed_ln_bias = load_tensor_as_f32(st, &format!("{prefix}embeddings.LayerNorm.bias"))?;

        let mut layers: Vec<DistilBertLayerWeights> = Vec::with_capacity(cfg.n_layers);
        for i in 0..cfg.n_layers {
            let p = format!("{prefix}transformer.layer.{i}");
            let q_lin = load_transposed_matrix_preserve_dtype(
                st,
                &format!("{p}.attention.q_lin.weight"),
                d,
                d,
            )?;
            let q_lin_bias = load_tensor_as_f32(st, &format!("{p}.attention.q_lin.bias"))?;
            let k_lin = load_transposed_matrix_preserve_dtype(
                st,
                &format!("{p}.attention.k_lin.weight"),
                d,
                d,
            )?;
            let k_lin_bias = load_tensor_as_f32(st, &format!("{p}.attention.k_lin.bias"))?;
            let v_lin = load_transposed_matrix_preserve_dtype(
                st,
                &format!("{p}.attention.v_lin.weight"),
                d,
                d,
            )?;
            let v_lin_bias = load_tensor_as_f32(st, &format!("{p}.attention.v_lin.bias"))?;
            let out_lin = load_transposed_matrix_preserve_dtype(
                st,
                &format!("{p}.attention.out_lin.weight"),
                d,
                d,
            )?;
            let out_lin_bias = load_tensor_as_f32(st, &format!("{p}.attention.out_lin.bias"))?;
            let sa_ln_gain = load_tensor_as_f32(st, &format!("{p}.sa_layer_norm.weight"))?;
            let sa_ln_bias = load_tensor_as_f32(st, &format!("{p}.sa_layer_norm.bias"))?;
            let lin1 = load_transposed_matrix_preserve_dtype(
                st,
                &format!("{p}.ffn.lin1.weight"),
                hidden,
                d,
            )?;
            let lin1_bias = load_tensor_as_f32(st, &format!("{p}.ffn.lin1.bias"))?;
            let lin2 = load_transposed_matrix_preserve_dtype(
                st,
                &format!("{p}.ffn.lin2.weight"),
                d,
                hidden,
            )?;
            let lin2_bias = load_tensor_as_f32(st, &format!("{p}.ffn.lin2.bias"))?;
            let output_ln_gain = load_tensor_as_f32(st, &format!("{p}.output_layer_norm.weight"))?;
            let output_ln_bias = load_tensor_as_f32(st, &format!("{p}.output_layer_norm.bias"))?;
            layers.push(DistilBertLayerWeights {
                q_lin,
                q_lin_bias: Arc::from(q_lin_bias),
                k_lin,
                k_lin_bias: Arc::from(k_lin_bias),
                v_lin,
                v_lin_bias: Arc::from(v_lin_bias),
                out_lin,
                out_lin_bias: Arc::from(out_lin_bias),
                sa_ln_gain: Arc::from(sa_ln_gain),
                sa_ln_bias: Arc::from(sa_ln_bias),
                lin1,
                lin1_bias: Arc::from(lin1_bias),
                lin2,
                lin2_bias: Arc::from(lin2_bias),
                output_ln_gain: Arc::from(output_ln_gain),
                output_ln_bias: Arc::from(output_ln_bias),
            });
        }

        Ok(Self {
            word_embedding: Arc::from(word_embedding),
            position_embedding: Arc::from(position_embedding),
            embed_ln_gain: Arc::from(embed_ln_gain),
            embed_ln_bias: Arc::from(embed_ln_bias),
            layers,
        })
    }
}

#[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<_>>())
    }

    fn tiny_cfg() -> DistilBertConfig {
        DistilBertConfig {
            vocab_size: 32,
            dim: 16,
            n_layers: 2,
            n_heads: 4,
            hidden_dim: 32,
            activation: DistilBertActivation::Gelu,
            max_position_embeddings: 8,
            layer_norm_eps: 1e-12,
        }
    }

    fn tiny_weights(cfg: &DistilBertConfig) -> DistilBertWeights {
        let mut s: u32 = 27272;
        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.dim;
        let word_embedding = vec_of(cfg.vocab_size * d, &mut *nb);
        let position_embedding = vec_of(cfg.max_position_embeddings * 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<DistilBertLayerWeights> = (0..cfg.n_layers)
            .map(|_| DistilBertLayerWeights {
                q_lin: WeightStorage::F32(vec_of(d * d, &mut *nb)),
                q_lin_bias: vec_of(d, &mut *nb),
                k_lin: WeightStorage::F32(vec_of(d * d, &mut *nb)),
                k_lin_bias: vec_of(d, &mut *nb),
                v_lin: WeightStorage::F32(vec_of(d * d, &mut *nb)),
                v_lin_bias: vec_of(d, &mut *nb),
                out_lin: WeightStorage::F32(vec_of(d * d, &mut *nb)),
                out_lin_bias: vec_of(d, &mut *nb),
                sa_ln_gain: Arc::from(vec![1.0_f32; d]),
                sa_ln_bias: Arc::from(vec![0.0_f32; d]),
                lin1: WeightStorage::F32(vec_of(d * cfg.hidden_dim, &mut *nb)),
                lin1_bias: vec_of(cfg.hidden_dim, &mut *nb),
                lin2: WeightStorage::F32(vec_of(cfg.hidden_dim * d, &mut *nb)),
                lin2_bias: vec_of(d, &mut *nb),
                output_ln_gain: Arc::from(vec![1.0_f32; d]),
                output_ln_bias: Arc::from(vec![0.0_f32; d]),
            })
            .collect();

        DistilBertWeights {
            word_embedding,
            position_embedding,
            embed_ln_gain,
            embed_ln_bias,
            layers,
        }
    }

    #[test]
    fn forward_shape_and_finite() {
        let cfg = tiny_cfg();
        let model = DistilBertModel {
            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.dim]);
        for &v in &out.realize_f32() {
            assert!(v.is_finite());
        }
    }

    /// GAP-308 born-red: a sequence longer than `max_position_embeddings` is
    /// REJECTED with a typed error, not a panic. Asserts the error MESSAGE, not
    /// merely `is_err()`: once the guard is a decline, `seq > max` would run on
    /// to an index panic downstream, so an `is_err()`-only test would pass on a
    /// panic it never saw.
    #[test]
    fn forward_declines_seq_over_max_position_embeddings() {
        let cfg = tiny_cfg(); // max_position_embeddings = 8
        let model = DistilBertModel {
            config: cfg.clone(),
            weights: tiny_weights(&cfg),
        };
        let tokens: Vec<u32> = (0..9).collect(); // seq = 9 > 8

        let err = model.forward(&tokens, None).unwrap_err();
        assert!(
            format!("{err}").contains("max_position_embeddings"),
            "forward must decline seq > max_position_embeddings; got: {err}"
        );

        let err2 = model
            .forward_intermediate_layers(&tokens, &[0], None)
            .unwrap_err();
        assert!(
            format!("{err2}").contains("max_position_embeddings"),
            "forward_intermediate_layers must decline too; got: {err2}"
        );
    }

    /// Bidirectional attention: changing any token affects ALL
    /// positions' hidden states. Verify by changing the last
    /// token and checking that position 0's output differs.
    #[test]
    fn bidirectional_attention() {
        let cfg = tiny_cfg();
        let model = DistilBertModel {
            config: cfg.clone(),
            weights: tiny_weights(&cfg),
        };
        let toks_a = [1_u32, 2, 3, 4];
        let toks_b = [1_u32, 2, 3, 15];
        let a = model.forward(&toks_a, None).unwrap().realize_f32();
        let b = model.forward(&toks_b, None).unwrap().realize_f32();
        let d = cfg.dim;
        // Compare position 0 (which should be affected by the last-token change).
        let mut max_diff = 0.0_f32;
        for i in 0..d {
            max_diff = max_diff.max((a[i] - b[i]).abs());
        }
        assert!(
            max_diff > 1e-6,
            "last-token change must affect position 0 (bidirectional), max_diff = {max_diff}"
        );
    }

    /// Position-embedding lookup is wired: changing only the
    /// position embedding row corresponding to position 0
    /// alters the output at position 0.
    #[test]
    // needless_range_loop here: the bound is a semantic count that need not equal the
    // indexed buffer len, so a mechanical .iter()/.take() risks silently dropping
    // iterations.
    #[allow(clippy::needless_range_loop)]
    fn position_embedding_is_wired() {
        let cfg = tiny_cfg();
        let mut base = tiny_weights(&cfg);
        let original_pos_embed = (*base.position_embedding).to_vec();
        // Modify the first position's embedding.
        let mut modified = original_pos_embed.clone();
        for i in 0..cfg.dim {
            modified[i] = 1.0;
        }
        let modified_pos = Arc::from(modified);
        let mut model_zero = base.clone();
        model_zero.position_embedding = modified_pos;
        // base keeps the original pos embed.
        base.position_embedding = Arc::from(original_pos_embed);

        let m_a = DistilBertModel {
            config: cfg.clone(),
            weights: base,
        };
        let m_b = DistilBertModel {
            config: cfg,
            weights: model_zero,
        };
        let toks = [1_u32, 2, 3, 4];
        let a = m_a.forward(&toks, None).unwrap().realize_f32();
        let b = m_b.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,
            "position-embed change must alter output, max_diff = {max_diff}"
        );
    }

    /// `forward_intermediate_layers` returns one tensor per
    /// requested layer index, each shaped `(1, seq, dim)`.
    #[test]
    fn forward_intermediate_layers_shape() {
        let cfg = tiny_cfg();
        let model = DistilBertModel {
            config: cfg.clone(),
            weights: tiny_weights(&cfg),
        };
        let tokens = [1_u32, 2, 3, 4];
        let outs = model
            .forward_intermediate_layers(&tokens, &[0_usize, 1], None)
            .unwrap();
        assert_eq!(outs.len(), 2);
        for out in &outs {
            assert_eq!(out.shape().dims(), &[1, tokens.len(), cfg.dim]);
            for &v in &out.realize_f32() {
                assert!(v.is_finite(), "non-finite intermediate: {v}");
            }
        }
        // Layer 0 and layer 1 outputs must differ.
        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}"
        );
    }
}