bunsen 0.37.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! # GPT Module

use burn::{
    Tensor,
    module::Module,
    nn::{
        Embedding,
        EmbeddingConfig,
        Linear,
        LinearConfig,
        activation::ActivationConfig,
        norm::{
            Normalization,
            NormalizationConfig,
            RmsNormConfig,
        },
    },
    prelude::{
        Backend,
        Config,
        Int,
    },
};

use crate::{
    blocks::transformers::{
        attention::csa::{
            CausalSelfAttentionConfig,
            CausalSelfAttentionMeta,
        },
        embedding::{
            RotaryEmbedding,
            RotaryEmbeddingConfig,
            RotaryEmbeddingMeta,
        },
        mlp::MlpConfig,
    },
    burner::module::{
        ModuleInit,
        ToStructureConfig,
    },
    contracts::{
        assert_shape_contract_periodically,
        unpack_shape_contract,
    },
    errors::BunsenResult,
    kits::gpts::nanochat::blocks::{
        NanoChatGptBlock,
        NanoChatGptBlockConfig,
    },
    ops::{
        norm::{
            RmsNormOptions,
            rms_norm,
        },
        transformers::attention::{
            KVCache,
            KVCacheConfig,
        },
    },
};

/// Common meta for [`NanoChatGpt`], [`NanoChatGptStructureConfig`], and
/// [`NanoChatGptContractConfig`].
pub trait NanoChatGptMeta {
    /// 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()
    }

    /// Returns the initial sequence length.
    fn init_seq_len(&self) -> usize;

    /// Returns the maximum sequence length.
    fn max_seq_len(&self) -> usize;

    /// Returns the number of layers.
    fn n_layer(&self) -> usize;
}

/// High-level GPT Config.
///
/// User-facing configuration for the nanoChat GPT model, exposing the common
/// hyperparameters (sizes, layer/head counts, vocabulary, softcap): the
/// policy of `NanoChatGpt`'s Stacked Config. It implements
/// [`ToStructureConfig`], expanding into a [`NanoChatGptStructureConfig`], and
/// gets [`ModuleInit`] from that trait's blanket impl, so `.init(&device)`
/// builds the [`NanoChatGpt`] module directly.
#[derive(Config, Debug)]
pub struct NanoChatGptContractConfig {
    /// Initial sequence Length.
    #[config(default = "1024")]
    pub init_seq_len: usize,

    /// Max `seq_len` factor.
    #[config(default = "10")]
    pub max_seq_len_factor: usize,

    /// Vocabulary Size.
    #[config(default = "50304")]
    pub vocab_size: usize,

    /// Number of Blocks.
    #[config(default = "12")]
    pub n_layer: usize,

    /// Number of Query Heads.
    #[config(default = "6")]
    pub n_head: usize,

    /// Number of KV Heads.
    #[config(default = "6")]
    pub n_kv_head: usize,

    /// Embedding Size.
    #[config(default = "768")]
    pub n_embed: usize,

    /// Softcap for the logits.
    #[config(default = "15.0")]
    pub softcap: f64,

    /// MLP Expansion Factor.
    #[config(default = "4")]
    pub expansion_factor: usize,

    /// MLP Activation Config.
    #[config(default = "ActivationConfig::Relu")]
    pub activation: ActivationConfig,

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

impl NanoChatGptMeta for NanoChatGptContractConfig {
    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 init_seq_len(&self) -> usize {
        self.init_seq_len
    }

    fn max_seq_len(&self) -> usize {
        self.init_seq_len() * self.max_seq_len_factor
    }

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

impl NanoChatGptContractConfig {
    /// Builds the [`NanoChatGptBlockConfig`] for this config.
    pub fn block_config(&self) -> NanoChatGptBlockConfig {
        NanoChatGptBlockConfig::new(
            CausalSelfAttentionConfig::new(self.n_head, self.n_kv_head, self.n_embed)
                .with_norm(self.norm.clone()),
            MlpConfig::new(self.n_embed)
                .with_expansion_factor(self.expansion_factor)
                .with_activation(self.activation.clone())
                .with_act_exponent(Some(2.0)),
        )
        .with_norm(self.norm.clone())
    }
}

impl ToStructureConfig for NanoChatGptContractConfig {
    type Structure = NanoChatGptStructureConfig;

    fn try_to_structure(&self) -> BunsenResult<NanoChatGptStructureConfig> {
        let block_config = self.block_config();
        Ok(NanoChatGptStructureConfig {
            wte: EmbeddingConfig::new(self.vocab_size, self.n_embed),
            h: (0..self.n_layer).map(|_| block_config.clone()).collect(),
            lm_head: LinearConfig::new(self.n_embed, self.vocab_size),
            r_emb: RotaryEmbeddingConfig::new(self.max_seq_len(), self.head_dim()),
            norm: self.norm.clone(),
            init_seq_len: self.init_seq_len,
            softcap: self.softcap,
        })
    }
}

/// Low-level GPT Structure Config.
///
/// The fully-expanded structural configuration for the [`NanoChatGpt`] module,
/// holding the explicit sub-configs (embedding, per-layer blocks, head, rotary
/// embedding). [`NanoChatGptContractConfig`] lowers to it. Directly builds the
/// [`NanoChatGpt`] module via [`ModuleInit`].
///
/// This config has a lot of duplicate information.
#[derive(Config, Debug)]
pub struct NanoChatGptStructureConfig {
    /// The embedding config.
    pub wte: EmbeddingConfig,

    /// The main transformer block sequence.
    pub h: Vec<NanoChatGptBlockConfig>,

    /// The config for the final linear layer.
    pub lm_head: LinearConfig,

    /// The config for the rotary embedding.
    pub r_emb: RotaryEmbeddingConfig,

    /// Initial sequence Length.
    #[config(default = "1024")]
    pub init_seq_len: usize,

    /// Softcap for the logits.
    #[config(default = "15.0")]
    pub softcap: f64,

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

impl NanoChatGptMeta for NanoChatGptStructureConfig {
    fn n_embed(&self) -> usize {
        self.wte.d_model
    }

    fn n_head(&self) -> usize {
        self.h[0].attn.n_head()
    }

    fn n_kv_head(&self) -> usize {
        self.h[0].attn.n_kv_head()
    }

    fn head_dim(&self) -> usize {
        self.h[0].attn.head_dim()
    }

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

    fn max_seq_len(&self) -> usize {
        self.r_emb.seq_len()
    }

    fn n_layer(&self) -> usize {
        self.h.len()
    }
}

impl<B: Backend> ModuleInit<B, NanoChatGpt<B>> for NanoChatGptStructureConfig {
    fn try_init(
        &self,
        device: &B::Device,
    ) -> BunsenResult<NanoChatGpt<B>> {
        let n_embed = self.n_embed();
        Ok(NanoChatGpt {
            wte: self.wte.init(device),
            h: self
                .h
                .iter()
                .enumerate()
                .map(|(layer_idx, c)| c.try_init(layer_idx, device))
                .collect::<BunsenResult<Vec<NanoChatGptBlock<B>>>>()?,
            h_norm: self.norm.clone().with_num_features(n_embed).init(device),
            lm_head: self.lm_head.init(device),
            r_emb: self.r_emb.try_init(device)?,
            init_seq_len: self.init_seq_len,
            softcap: self.softcap,
        })
    }
}

/// nanoChat GPT language model.
///
/// A decoder-only transformer: token embedding, normalized by a
/// parameter-free [`rms_norm`] as upstream's is, a stack of
/// [`NanoChatGptBlock`] layers, a final normalization, and a linear head,
/// not tied to the embedding, producing softcapped vocabulary logits.
/// The normalized embedding starts the residual stream, and each block adds
/// its attention and MLP updates to it.
/// Incremental decoding takes a [`KVCache`] the caller holds, from
/// [`new_kv_cache`](Self::new_kv_cache).
///
/// Built by [`NanoChatGptContractConfig`] (high-level) or
/// [`NanoChatGptStructureConfig`].
#[derive(Module, Debug)]
pub struct NanoChatGpt<B: Backend> {
    wte: Embedding<B>,
    h: Vec<NanoChatGptBlock<B>>,
    h_norm: Normalization<B>,
    lm_head: Linear<B>,
    r_emb: RotaryEmbedding<B>,

    init_seq_len: usize,
    softcap: f64,
}

impl<B: Backend> NanoChatGptMeta for NanoChatGpt<B> {
    fn n_embed(&self) -> usize {
        // burn's `Embedding` weight is `[n_embedding, d_model]`.
        self.wte.weight.dims()[1]
    }

    fn n_head(&self) -> usize {
        self.h[0].attn.n_head()
    }

    fn n_kv_head(&self) -> usize {
        self.h[0].attn.n_kv_head()
    }

    fn head_dim(&self) -> usize {
        self.h[0].attn.head_dim()
    }

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

    fn max_seq_len(&self) -> usize {
        self.r_emb.seq_len()
    }

    fn n_layer(&self) -> usize {
        self.h.len()
    }
}

impl<B: Backend> NanoChatGpt<B> {
    /// Forward Pass.
    ///
    /// # Arguments
    /// - `idx`: a `[B, T]` tensor of token ids.
    /// - `kv_cache`: the decode's cache, or `None` to run the sequence on its
    ///   own. With a cache, the step's positions start at the cache's position,
    ///   and every layer appends its keys and values.
    ///
    /// # Returns
    /// The `[B, T, vocab_size]` logits, softcapped:
    /// `softcap * tanh(logits / softcap)`.
    ///
    /// # Panics
    /// When the step runs past the rotary table: when the cache's position
    /// (0 without a cache) plus `T` exceeds `max_seq_len`.
    pub fn forward(
        &self,
        idx: Tensor<B, 2, Int>,
        kv_cache: &mut Option<&mut KVCache<B>>,
    ) -> Tensor<B, 3> {
        let [b, t] = unpack_shape_contract!(["B", "T"], &idx.dims());

        let t0 = match kv_cache {
            Some(kv_cache) => kv_cache.pos(),
            None => 0,
        };
        assert!(
            t0 + t <= self.r_emb.seq_len(),
            "Sequence position grew beyond the rotary embeddings table: {t0} + {t} > {}",
            self.r_emb.seq_len()
        );
        let r_emb = self.r_emb.clip_range(t0..t0 + t);

        // As upstream (`x = norm(wte(idx))`): a parameter-free norm, so the
        // residual stream starts normalized.
        let mut x = rms_norm(self.wte.forward(idx), &RmsNormOptions::default());

        for block in &self.h {
            x = block.forward(x, &r_emb, kv_cache);
        }
        x = self.h_norm.forward(x);

        let logits = self
            .lm_head
            .forward(x)
            .div_scalar(self.softcap)
            .tanh()
            .mul_scalar(self.softcap);

        assert_shape_contract_periodically!(
            ["B", "T", "V"],
            &logits.dims(),
            &[("B", b), ("T", t), ("V", self.wte.weight.dims()[0])]
        );
        logits
    }

    /// Allocates a new, empty [`KVCache`] for one decode.
    ///
    /// The cache is sized for this model: its KV heads, head width and layer
    /// count, with room for `init_seq_len` positions before it grows. It is
    /// injected state: the caller holds it and passes it to each
    /// [`forward`](Self::forward) of the decode, and the model never owns
    /// one.
    ///
    /// # Arguments
    /// - `batch_size`: the batch size.
    pub fn new_kv_cache(
        &self,
        batch_size: usize,
    ) -> KVCache<B> {
        KVCacheConfig {
            batch_size,
            num_heads: self.n_kv_head(),
            seq_len: self.init_seq_len(),
            head_dim: self.head_dim(),
            num_layers: self.n_layer(),
        }
        .init()
    }

    /// Calculates the estimated FLOPs per token for the model.
    ///
    /// Ref: <https://arxiv.org/abs/2204.02311>
    pub fn estimate_flops_per_token(&self) -> usize {
        let nparams = self.num_params();
        let nparams_embedding = self.wte.num_params();
        let nparams = nparams - nparams_embedding;

        let l = self.n_layer();
        let h = self.n_head();
        let q = self.head_dim();
        let t = self.init_seq_len;

        6 * nparams + 12 * l * h * q * t
    }
}

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

    use super::*;
    use crate::{
        contracts::assert_shape_contract,
        support::testing::{
            DeviceMemoryGuard,
            PerformanceBackend,
            default_device,
        },
    };

    #[test]
    fn test_gpt_config() {
        let cfg = NanoChatGptContractConfig::new();
        assert_eq!(cfg.init_seq_len, 1024);
        assert_eq!(cfg.vocab_size, 50304);
        assert_eq!(cfg.n_layer, 12);
        assert_eq!(cfg.n_head, 6);
        assert_eq!(cfg.n_kv_head, 6);
        assert_eq!(cfg.n_embed, 768);
        assert_eq!(cfg.expansion_factor, 4);

        assert_eq!(cfg.n_embed(), 768);
    }

    #[test]
    #[serial]
    fn test_gpt_forward() {
        type B = PerformanceBackend;
        let device = default_device();
        let _memory = DeviceMemoryGuard::<B>::new(&device);

        let batch_size = 1;
        let seq_len = 100;
        let n_layer = 4;
        let n_embed = 36;

        let vocab_size = 1000;

        let cfg = NanoChatGptContractConfig::new()
            .with_vocab_size(vocab_size)
            .with_n_embed(n_embed)
            .with_n_layer(n_layer);
        let gpt: NanoChatGpt<B> = cfg.init(&device);

        let mut kv_cache = gpt.new_kv_cache(batch_size);

        let input_tokens = Tensor::<B, 2>::random(
            [batch_size, seq_len],
            Distribution::Uniform(0.0, vocab_size as f64),
            &device,
        )
        .int();

        let logits = gpt.forward(input_tokens, &mut Some(&mut kv_cache));
        assert_shape_contract!(
            ["B", "T", "V"],
            &logits.dims(),
            &[("B", batch_size), ("T", seq_len), ("V", vocab_size)]
        );
        assert_eq!(gpt.n_embed(), n_embed);
    }

    /// Asserts that `a` and `b` answer every [`NanoChatGptMeta`] method alike.
    fn assert_meta_agrees(
        a: &impl NanoChatGptMeta,
        b: &impl NanoChatGptMeta,
    ) {
        assert_eq!(a.n_embed(), b.n_embed());
        assert_eq!(a.n_head(), b.n_head());
        assert_eq!(a.n_kv_head(), b.n_kv_head());
        assert_eq!(a.head_dim(), b.head_dim());
        assert_eq!(a.init_seq_len(), b.init_seq_len());
        assert_eq!(a.max_seq_len(), b.max_seq_len());
        assert_eq!(a.n_layer(), b.n_layer());
    }

    /// The policy builds the same `NanoChatGpt` through its structure as
    /// through the blanket `init`, and the module reads back the geometry
    /// its configs declare.
    #[test]
    #[serial]
    fn test_policy_pathways_agree() {
        type B = PerformanceBackend;
        let device = default_device();
        let _memory = DeviceMemoryGuard::<B>::new(&device);

        let policy = NanoChatGptContractConfig::new()
            .with_init_seq_len(16)
            .with_max_seq_len_factor(2)
            .with_vocab_size(64)
            .with_n_layer(2)
            .with_n_head(4)
            .with_n_kv_head(2)
            .with_n_embed(32);

        let structure = policy.to_structure();
        assert_meta_agrees(&policy, &structure);
        assert_eq!(structure.max_seq_len(), 32);
        assert_eq!(structure.head_dim(), 8);

        let lowered: NanoChatGpt<B> = structure.init(&device);
        let direct: NanoChatGpt<B> = policy.init(&device);

        assert_meta_agrees(&direct, &lowered);
        assert_meta_agrees(&structure, &lowered);
        assert_eq!(lowered.n_embed(), 32);
    }

    /// A tiny model whose rotary table, `max_seq_len`, is 8 positions.
    fn tiny_gpt<B: Backend>(device: &B::Device) -> NanoChatGpt<B> {
        let gpt: NanoChatGpt<B> = NanoChatGptContractConfig::new()
            .with_init_seq_len(4)
            .with_max_seq_len_factor(2)
            .with_vocab_size(16)
            .with_n_layer(2)
            .with_n_head(2)
            .with_n_kv_head(2)
            .with_n_embed(16)
            .init(device);
        assert_eq!(gpt.max_seq_len(), 8);
        gpt
    }

    /// A cached decode may fill the rotary table exactly.
    #[test]
    #[serial]
    fn test_cached_decode_fills_rotary_table() {
        type B = PerformanceBackend;
        let device = default_device();
        let _memory = DeviceMemoryGuard::<B>::new(&device);

        let gpt = tiny_gpt::<B>(&device);
        let mut cache = gpt.new_kv_cache(1);
        gpt.forward(Tensor::zeros([1, 7], &device), &mut Some(&mut cache));
        let logits = gpt.forward(Tensor::zeros([1, 1], &device), &mut Some(&mut cache));
        assert_eq!(logits.dims(), [1, 1, 16]);
        assert_eq!(cache.pos(), 8);
    }

    /// A cached decode that runs past the rotary table fails at the guard,
    /// which counts the cache's position plus the step's length.
    #[test]
    #[serial]
    #[should_panic(expected = "beyond the rotary embeddings table: 8 + 1 > 8")]
    fn test_cached_decode_past_rotary_table_panics() {
        type B = PerformanceBackend;
        let device = default_device();
        let _memory = DeviceMemoryGuard::<B>::new(&device);

        let gpt = tiny_gpt::<B>(&device);
        let mut cache = gpt.new_kv_cache(1);
        gpt.forward(Tensor::zeros([1, 8], &device), &mut Some(&mut cache));
        gpt.forward(Tensor::zeros([1, 1], &device), &mut Some(&mut cache));
    }

    /// Upstream normalizes the embedding before the first block
    /// (`x = norm(wte(idx))`), so the first block sees the same input, and the
    /// logits are the same, whatever the embedding table's scale. Fed a known
    /// embedding table and the same table times 8, the model must agree.
    #[test]
    #[serial]
    fn test_forward_normalizes_the_embedding() {
        type B = PerformanceBackend;
        let device = default_device();
        let _memory = DeviceMemoryGuard::<B>::new(&device);

        let mut gpt = tiny_gpt::<B>(&device);
        let table: Tensor<B, 2> = Tensor::random([16, 16], Distribution::Normal(0.0, 1.0), &device);
        let tokens: Tensor<B, 2, Int> = Tensor::from_data([[3, 1, 4, 1, 5, 9]], &device);

        gpt.wte.weight = Param::from_tensor(table.clone());
        let logits = gpt.forward(tokens.clone(), &mut None);

        gpt.wte.weight = Param::from_tensor(table.mul_scalar(8.0));
        let scaled = gpt.forward(tokens, &mut None);

        scaled
            .into_data()
            .assert_approx_eq::<f32>(&logits.into_data(), Tolerance::rel_abs(1e-3, 1e-4));
    }
}