bunsen 0.39.0

bunsen is a batteries included common library for burn
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! # Causal Self-Attention

use burn::{
    Tensor,
    config::Config,
    module::Module,
    nn::{
        Linear,
        LinearConfig,
        norm::{
            Normalization,
            NormalizationConfig,
            RmsNormConfig,
        },
    },
    prelude::{
        Bool,
        Int,
        s,
    },
    tensor::Device,
};

use crate::{
    blocks::transformers::embedding::RotaryEmbedding,
    contracts::{
        assert_shape_contract_periodically,
        unpack_shape_contract,
    },
    errors::{
        BunsenResult,
        WithOkOrPanic,
    },
    ops::transformers::attention::{
        KVCache,
        ScaledDotProductAttentionConfig,
        scaled_dot_product_attention,
        sdpa_attn_weight,
    },
};

/// Common meta for [`CausalSelfAttention`] and [`CausalSelfAttentionConfig`].
pub trait CausalSelfAttentionMeta {
    /// Returns the size of the input and output.
    fn n_embed(&self) -> usize;

    /// Returns the number of heads.
    fn n_head(&self) -> usize;

    /// Returns the number of KV heads.
    fn n_kv_head(&self) -> usize;

    /// Returns the size of each head.
    fn head_dim(&self) -> usize {
        self.n_embed() / self.n_head()
    }

    /// Group Query Attention.
    fn gqa_enabled(&self) -> bool {
        self.n_head() != self.n_kv_head()
    }
}

/// Config for [`CausalSelfAttention`].
#[derive(Config, Debug)]
pub struct CausalSelfAttentionConfig {
    /// Number of Heads.
    pub n_head: usize,

    /// Number of KV Heads.
    pub n_kv_head: usize,

    /// Embedding Size.
    pub n_embed: usize,

    /// Attention Normalization.
    /// This normalization will be adapted to the appropriate feature count.
    #[config(default = "NormalizationConfig::Rms(RmsNormConfig::new(0))")]
    pub norm: NormalizationConfig,

    /// The width of each head; `None` is `n_embed / n_head`.
    ///
    /// A model whose heads are wider or narrower than that (Qwen3's are 128
    /// over 16 heads of a 1024-wide model) names it here; the query and
    /// output projections then map `n_embed` to and from `n_head * head_dim`.
    #[config(default = "None")]
    pub head_dim: Option<usize>,

    /// Whether the query/key normalization applies before or after the
    /// rotary embedding.
    #[config(default = "QkNormPlacement::AfterRope")]
    pub qk_norm: QkNormPlacement,
}

/// Where [`CausalSelfAttention`] normalizes its queries and keys, relative to
/// the rotary embedding.
#[derive(Config, Debug, Copy, PartialEq, Eq)]
pub enum QkNormPlacement {
    /// Rotate, then normalize: `nanochat`'s order.
    AfterRope,

    /// Normalize, then rotate: Qwen3's order.
    BeforeRope,
}

impl QkNormPlacement {
    /// Normalizes and rotates `x`, a `[B, T, H, D]` query or key, in this
    /// order.
    pub fn apply(
        self,
        x: Tensor<4>,
        norm: &Normalization,
        r_emb: &RotaryEmbedding,
    ) -> Tensor<4> {
        match self {
            Self::AfterRope => norm.forward(r_emb.apply(x)),
            Self::BeforeRope => r_emb.apply(norm.forward(x)),
        }
    }
}

impl CausalSelfAttentionMeta for CausalSelfAttentionConfig {
    fn n_embed(&self) -> usize {
        self.n_embed
    }

    fn n_head(&self) -> usize {
        self.n_head
    }

    fn n_kv_head(&self) -> usize {
        self.n_kv_head
    }

    fn head_dim(&self) -> usize {
        self.head_dim.unwrap_or(self.n_embed / self.n_head.max(1))
    }
}

impl CausalSelfAttentionConfig {
    /// Validate the config.
    pub fn validate(&self) {
        assert!(
            self.n_head > 0,
            "n_head must be > 0; got n_head={}",
            self.n_head
        );
        assert!(
            self.head_dim.is_some() || self.n_embed.is_multiple_of(self.n_head),
            "n_embed ({}) must be multiple of n_head ({})",
            self.n_embed,
            self.n_head
        );
        assert!(
            self.head_dim.is_some() || self.n_embed.is_multiple_of(self.n_kv_head),
            "n_embed ({}) must be multiple of n_kv_head ({})",
            self.n_embed,
            self.n_kv_head
        );
        assert!(
            self.head_dim() > 0,
            "head_dim must be > 0; got n_embed={}/n_head={}",
            self.n_embed,
            self.n_head
        );
        assert!(
            self.n_kv_head <= self.n_head,
            "n_kv_head must be <= n_head; got n_kv_head={}, n_head={}",
            self.n_kv_head,
            self.n_head
        );
        assert!(
            self.n_head.is_multiple_of(self.n_kv_head),
            "n_head must be divisible by n_kv_head; got n_head={}, n_kv_head={}",
            self.n_head,
            self.n_kv_head
        );
    }
}

impl CausalSelfAttentionConfig {
    /// Initializes the module.
    pub fn try_init(
        &self,
        layer_index: usize,
        device: &Device,
    ) -> BunsenResult<CausalSelfAttention> {
        self.validate();
        let head_dim = self.head_dim();

        Ok(CausalSelfAttention {
            layer_index,
            head_dim,
            qk_norm: self.qk_norm,
            c_q: LinearConfig::new(self.n_embed, self.n_head * head_dim)
                .with_bias(false)
                .init(device),
            q_norm: self
                .norm
                .clone()
                .with_num_features(self.head_dim())
                .init(device),
            c_k: LinearConfig::new(self.n_embed, self.n_kv_head * head_dim)
                .with_bias(false)
                .init(device),
            k_norm: self
                .norm
                .clone()
                .with_num_features(self.head_dim())
                .init(device),
            c_v: LinearConfig::new(self.n_embed, self.n_kv_head * head_dim)
                .with_bias(false)
                .init(device),
            c_proj: LinearConfig::new(self.n_head * head_dim, self.n_embed)
                .with_bias(false)
                .init(device),
        })
    }

    /// Initializes the module, or panic.
    pub fn init(
        &self,
        layer_index: usize,
        device: &Device,
    ) -> CausalSelfAttention {
        self.try_init(layer_index, device).ok_or_panic()
    }
}

/// Causal Self-Attention Module
///
/// Multi-head causal self-attention with rotary positional embeddings,
/// optional Group Query Attention (fewer KV heads than query heads), and
/// per-head query/key normalization. Construct via
/// [`CausalSelfAttentionConfig`] and `.init(layer_index, device)`, then call
/// [`forward`](CausalSelfAttention::forward) with an input sequence, a
/// [`RotaryEmbedding`], and an optional [`KVCache`].
///
/// Built by [`CausalSelfAttentionConfig`].
#[derive(Module, Debug)]
pub struct CausalSelfAttention {
    /// Layer Index.
    pub layer_index: usize,

    /// Head Dimension.
    pub head_dim: usize,

    /// Where the query/key normalization applies.
    #[module(skip)]
    pub qk_norm: QkNormPlacement,

    /// Query Linear.
    pub c_q: Linear,

    /// Query Normalization.
    pub q_norm: Normalization,

    /// Key Linear.
    pub c_k: Linear,

    /// Key Normalization.
    pub k_norm: Normalization,

    /// Value Linear.
    pub c_v: Linear,

    /// Output Projection Linear.
    pub c_proj: Linear,
}

impl CausalSelfAttentionMeta for CausalSelfAttention {
    fn n_embed(&self) -> usize {
        self.c_q.weight.dims()[0]
    }

    fn n_head(&self) -> usize {
        self.c_q.weight.dims()[1] / self.head_dim()
    }

    fn n_kv_head(&self) -> usize {
        self.c_k.weight.dims()[1] / self.head_dim()
    }

    fn head_dim(&self) -> usize {
        self.head_dim
    }
}

impl CausalSelfAttention {
    /// The queries and keys of `input`: projected, normalized and rotated.
    ///
    /// # Arguments
    /// - `input`: a `[B, T, D]` sequence.
    /// - `r_emb`: a rotary embedding with len ``T``.
    ///
    /// # Returns
    /// - `[B, H_q, T, head_dim]` queries and `[B, H_kv, T, head_dim]` keys.
    fn queries_keys(
        &self,
        input: Tensor<3>,
        r_emb: &RotaryEmbedding,
    ) -> (Tensor<4>, Tensor<4>) {
        let [b, t] = unpack_shape_contract!(["B", "T", "D"], &input.dims(), &["B", "T"]);
        let q = self
            .c_q
            .forward(input.clone())
            .reshape([b, t, self.n_head(), self.head_dim()]);
        let q = self.qk_norm.apply(q, &self.q_norm, r_emb).swap_dims(1, 2);
        let k = self
            .c_k
            .forward(input)
            .reshape([b, t, self.n_kv_head(), self.head_dim()]);
        let k = self.qk_norm.apply(k, &self.k_norm, r_emb).swap_dims(1, 2);
        (q, k)
    }

    /// The attention weights an uncached [`forward`](Self::forward) of
    /// `input` applies: per query head, `softmax(q k^T / sqrt(head_dim))`
    /// over the keys at and before each query.
    ///
    /// # Arguments
    /// - `input`: a `[B, T, D]` sequence.
    /// - `r_emb`: a rotary embedding with len ``T``.
    ///
    /// # Returns
    /// - `[B, H_q, T, T]`, each row a distribution over the keys; zero above
    ///   the diagonal.
    pub fn attention_weights(
        &self,
        input: Tensor<3>,
        r_emb: &RotaryEmbedding,
    ) -> Tensor<4> {
        let [b, t] = unpack_shape_contract!(
            ["B", "T", "D"],
            &input.dims(),
            &["B", "T"],
            &[("D", self.n_embed())]
        );
        let (q, k) = self.queries_keys(input, r_emb);
        let weights = sdpa_attn_weight(
            q,
            k,
            None,
            None,
            ScaledDotProductAttentionConfig::new()
                .with_is_causal(true)
                .with_enable_gqa(self.gqa_enabled()),
        );
        assert_shape_contract_periodically!(
            ["B", "H", "T", "T"],
            &weights.dims(),
            &[("B", b), ("H", self.n_head()), ("T", t)]
        );
        weights
    }

    /// Forward Pass.
    ///
    /// # Arguments
    /// - `input`: a `[B, T, D]` sequence.
    /// - `r_emb`: a rotary embedding with len ``T``.
    /// - `kv_cache`: optional KV cache.
    ///
    /// # Returns
    /// - `[B, T, D]` attention.
    pub fn forward(
        &self,
        input: Tensor<3>,
        r_emb: &RotaryEmbedding,
        kv_cache: &mut Option<&mut KVCache>,
    ) -> Tensor<3> {
        let [b, t_q] = unpack_shape_contract!(
            ["B", "T", "D"],
            &input.dims(),
            &["B", "T"],
            &[("D", self.n_embed())]
        );

        let (q, k) = self.queries_keys(input.clone(), r_emb);

        let v = self
            .c_v
            .forward(input)
            .reshape([b, t_q, self.n_kv_head(), self.head_dim()])
            .swap_dims(1, 2);

        // B, H_?, T, D

        let mut k = k;
        let mut v = v;

        if let Some(kvc) = kv_cache {
            (k, v) = (*kvc).insert_kv(self.layer_index, k.clone(), v.clone());
        }
        let t_kv = k.dims()[2];

        let attn_mask: Option<Tensor<2, Bool>>;
        let is_causal: bool;
        if kv_cache.is_none() || t_q == t_kv {
            attn_mask = None;
            is_causal = true;
        } else if t_q == 1 {
            attn_mask = None;
            is_causal = false;
        } else {
            let device = q.device();
            let mut mask = Tensor::<2, Bool>::empty([t_q, t_kv], &device);
            let prefix_len = t_kv - t_q;
            if prefix_len > 0 {
                mask = mask.slice_fill(s![.., ..prefix_len], true);
            }
            // Causal within the chunk: each new token sees itself and the
            // new tokens before it.
            let fill = Tensor::<2, Int>::ones([t_q, t_q], &device).tril(0).bool();
            mask = mask.slice_assign(s![.., prefix_len..], fill);

            attn_mask = Some(mask);
            is_causal = false;
        };

        let cfg = ScaledDotProductAttentionConfig::new()
            .with_is_causal(is_causal)
            .with_enable_gqa(self.gqa_enabled());

        let y = scaled_dot_product_attention(q, k, v, None, attn_mask, cfg)
            .swap_dims(1, 2)
            .reshape([
                b as i32,
                t_q as i32,
                (self.n_head() * self.head_dim()) as i32,
            ]);

        let y = self.c_proj.forward(y);

        assert_shape_contract_periodically!(
            ["B", "T", "D"],
            &y.dims(),
            &[("B", b), ("T", t_q), ("D", self.n_embed())]
        );

        y
    }
}

#[cfg(test)]
mod tests {
    use burn::tensor::{
        Distribution,
        Tolerance,
    };
    use serial_test::serial;

    use super::*;
    use crate::{
        blocks::transformers::embedding::{
            RotaryEmbedding,
            RotaryEmbeddingConfig,
        },
        burner::module::ModuleInit,
        contracts::assert_shape_contract,
        ops::transformers::attention::KVCacheConfig,
        support::testing::{
            DeviceMemoryGuard,
            assert_tensors_close,
            cpu_device,
            performance_device,
            seeded_tensor,
        },
    };

    #[test]
    fn test_csa_config() {
        let cfg = CausalSelfAttentionConfig::new(3, 2, 4);
        assert_eq!(cfg.n_embed, 4);
        assert_eq!(cfg.n_head, 3);
        assert_eq!(cfg.n_kv_head, 2);
        assert_eq!(cfg.n_embed(), 4);
        assert_eq!(cfg.n_head(), 3);
        assert_eq!(cfg.n_kv_head(), 2);
        assert_eq!(cfg.head_dim(), 1);
    }

    #[test]
    #[allow(unused)]
    fn test_csa_forward() {
        let device = cpu_device();

        let batch = 1;
        let seq_len = 10;

        let n_embed = 1024;
        let n_head = 128;
        let n_kv_head = 64;
        let layer_index = 12;

        let cfg = CausalSelfAttentionConfig::new(n_head, n_kv_head, n_embed);
        let csa: CausalSelfAttention = cfg.init(layer_index, &device);

        let head_dim = csa.head_dim();

        let re_cfg = RotaryEmbeddingConfig::new(seq_len, csa.head_dim());
        let r_emb: RotaryEmbedding = re_cfg.init(&device);

        let input: Tensor<3> =
            Tensor::random([batch, seq_len, n_embed], Distribution::Default, &device);

        let mut kv_cache: Option<&mut KVCache> = None;

        let output = csa.forward(input.clone(), &r_emb, &mut kv_cache);
        assert_shape_contract!(
            ["B", "T", "D"],
            &output.dims(),
            &[("B", batch), ("T", seq_len), ("D", n_embed)]
        );
    }

    /// The weights are what the forward pass applies: each row a causal
    /// distribution, and the values they weigh, projected, the output.
    #[test]
    fn test_attention_weights_are_the_forwards() {
        let device = cpu_device();
        let (batch, seq_len, n_embed, n_head, n_kv_head) = (2, 6, 32, 4, 2);
        let csa: CausalSelfAttention =
            CausalSelfAttentionConfig::new(n_head, n_kv_head, n_embed).init(0, &device);
        let head_dim = csa.head_dim();
        let r_emb: RotaryEmbedding = RotaryEmbeddingConfig::new(seq_len, head_dim).init(&device);
        let input: Tensor<3> =
            Tensor::random([batch, seq_len, n_embed], Distribution::Default, &device);

        let weights = csa.attention_weights(input.clone(), &r_emb);
        assert_eq!(weights.dims(), [batch, n_head, seq_len, seq_len]);
        let sums = weights.clone().sum_dim(3);
        assert_tensors_close(&sums, &Tensor::ones_like(&sums), Tolerance::default());
        let upper = weights.clone().triu(1).abs().max().into_scalar::<f32>();
        assert_eq!(upper, 0.0);

        let v = csa
            .c_v
            .forward(input.clone())
            .reshape([batch, seq_len, n_kv_head, head_dim])
            .swap_dims(1, 2);
        let v = crate::ops::repeat::repeat_interleave::<4, 5, _>(v, n_head / n_kv_head, 1);
        let applied = csa
            .c_proj
            .forward(weights.matmul(v).swap_dims(1, 2).reshape([
                batch,
                seq_len,
                n_head * head_dim,
            ]));
        let forward = csa.forward(input, &r_emb, &mut None);
        assert_tensors_close(&applied, &forward, Tolerance::default());
    }

    /// A chunk of several new tokens after a cached prefix attends as the
    /// uncached pass does: each new token sees the prefix, the earlier new
    /// tokens, and itself.
    #[test]
    #[serial]
    fn test_csa_cached_chunk_matches_full_pass() {
        let device = performance_device();
        let _memory = DeviceMemoryGuard::new(&device);

        let [batch, seq_len, n_embed] = [2, 6, 16];
        let prefix = 3;

        let csa: CausalSelfAttention =
            CausalSelfAttentionConfig::new(4, 2, n_embed).init(0, &device);
        let r_emb: RotaryEmbedding =
            RotaryEmbeddingConfig::new(seq_len, csa.head_dim()).init(&device);
        let input =
            seeded_tensor::<3>(1, [batch, seq_len, n_embed], Distribution::Default, &device);

        let full = csa.forward(input.clone(), &r_emb, &mut None);

        let mut cache: KVCache =
            KVCacheConfig::new(batch, csa.n_kv_head(), seq_len, csa.head_dim(), 1).init();
        let head = csa.forward(
            input.clone().slice(s![.., ..prefix]),
            &r_emb.clip_range(0..prefix),
            &mut Some(&mut cache),
        );
        let chunk = csa.forward(
            input.slice(s![.., prefix..]),
            &r_emb.clip_range(prefix..seq_len),
            &mut Some(&mut cache),
        );

        assert_tensors_close(
            &head,
            &full.clone().slice(s![.., ..prefix]),
            Tolerance::default(),
        );
        assert_tensors_close(&chunk, &full.slice(s![.., prefix..]), Tolerance::default());
    }

    /// Heads wider than `n_embed / n_head` project to and from
    /// `n_head * head_dim`; either norm placement keeps the shape.
    #[test]
    fn test_explicit_head_dim() {
        use crate::{
            blocks::transformers::embedding::RotaryEmbeddingConfig,
            burner::module::ModuleInit,
            support::testing::cpu_device,
        };
        let device = cpu_device();
        for placement in [QkNormPlacement::AfterRope, QkNormPlacement::BeforeRope] {
            let cfg = CausalSelfAttentionConfig::new(2, 1, 12)
                .with_head_dim(Some(8))
                .with_qk_norm(placement);
            assert_eq!(cfg.head_dim(), 8);
            let attn = cfg.init(0, &device);
            assert_eq!(attn.n_head(), 2);
            assert_eq!(attn.n_kv_head(), 1);
            assert_eq!(attn.c_proj.weight.dims(), [16, 12]);
            let r_emb = RotaryEmbeddingConfig::new(5, 8).init(&device);
            let y = attn.forward(Tensor::zeros([2, 5, 12], &device), &r_emb, &mut None);
            assert_eq!(y.dims(), [2, 5, 12]);
        }
    }
}