bunsen 0.40.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
//! # A pre-norm transformer encoder with rotary position embeddings.
//!
//! [`RopeTransformerEncoder`]: a norm over the input (a `LayerNorm` by
//! default), a stack of [`RopeEncoderBlock`]s sharing one rotary table over
//! the whole input, and a final norm. The encoder of NVIDIA's
//! `Nemotron-3-Diarization` (`NeMo`'s `TransformerEncoder` with `rope` and
//! `pre_block_norm`).

use burn::{
    Tensor,
    config::Config,
    module::Module,
    nn::{
        LayerNormConfig,
        activation::ActivationConfig,
        norm::{
            Normalization,
            NormalizationConfig,
        },
    },
    tensor::Device,
};

use crate::{
    blocks::transformers::{
        attention::rope::{
            RopeSelfAttention,
            RopeSelfAttentionConfig,
            RopeSelfAttentionMeta,
        },
        embedding::{
            RotaryDirection,
            RotaryEmbeddingConfig,
        },
        mlp::{
            Mlp,
            MlpConfig,
        },
    },
    burner::module::ModuleInit,
    contracts::unpack_shape_contract,
    errors::BunsenResult,
    ops::transformers::attention::length_mask,
};

/// Common meta for [`RopeEncoderBlock`] and [`RopeEncoderBlockConfig`].
pub trait RopeEncoderBlockMeta {
    /// The model width.
    fn d_model(&self) -> usize;

    /// The head count.
    fn n_heads(&self) -> usize;

    /// The feed-forward's hidden width.
    fn d_ff(&self) -> usize;
}

/// Config for [`RopeEncoderBlock`].
///
/// Implements [`RopeEncoderBlockMeta`].
#[derive(Config, Debug)]
pub struct RopeEncoderBlockConfig {
    /// The model width.
    pub d_model: usize,

    /// The head count.
    pub n_heads: usize,

    /// The feed-forward's hidden width over `d_model`.
    #[config(default = "4")]
    pub ff_expansion: usize,

    /// The normalization before each sublayer, and the encoder's over its
    /// input and output, sized to `d_model` whatever feature count it names:
    /// a `LayerNorm` (epsilon `1e-5`) by default, `NeMo`'s.
    #[config(default = "NormalizationConfig::Layer(LayerNormConfig::new(0))")]
    pub norm: NormalizationConfig,

    /// Whether the query, key and value projections carry a bias.
    #[config(default = "false")]
    pub qkv_bias: bool,
}

impl RopeEncoderBlockMeta for RopeEncoderBlockConfig {
    fn d_model(&self) -> usize {
        self.d_model
    }

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

    fn d_ff(&self) -> usize {
        self.d_model * self.ff_expansion
    }
}

impl ModuleInit<RopeEncoderBlock> for RopeEncoderBlockConfig {
    fn try_init(
        &self,
        device: &Device,
    ) -> BunsenResult<RopeEncoderBlock> {
        let norm = || {
            self.norm
                .clone()
                .with_num_features(self.d_model)
                .init(device)
        };
        Ok(RopeEncoderBlock {
            attn_norm: norm(),
            attn: RopeSelfAttentionConfig::new(self.d_model, self.n_heads)
                .with_qkv_bias(self.qkv_bias)
                .try_init(device)?,
            mlp_norm: norm(),
            mlp: MlpConfig::new(self.d_model)
                .with_expansion_factor(self.ff_expansion)
                .with_activation(ActivationConfig::Gelu)
                .with_bias(true)
                .try_init(device)?,
        })
    }
}

/// A pre-norm encoder layer with rotary attention: `x + attn(attn_norm(x))`,
/// then `x + mlp(mlp_norm(x))`, the feed-forward `Linear -> GELU -> Linear`.
///
/// Built by [`RopeEncoderBlockConfig`].
/// Implements [`RopeEncoderBlockMeta`].
#[derive(Module, Debug)]
pub struct RopeEncoderBlock {
    /// The attention's normalization.
    pub attn_norm: Normalization,

    /// The attention.
    pub attn: RopeSelfAttention,

    /// The feed-forward's normalization.
    pub mlp_norm: Normalization,

    /// The feed-forward.
    pub mlp: Mlp,
}

impl RopeEncoderBlockMeta for RopeEncoderBlock {
    fn d_model(&self) -> usize {
        self.attn.d_model()
    }

    fn n_heads(&self) -> usize {
        self.attn.n_heads()
    }

    fn d_ff(&self) -> usize {
        self.mlp.linear1.weight.dims()[1]
    }
}

impl RopeEncoderBlock {
    /// One layer.
    ///
    /// # Arguments
    /// * `x` - `[batch, time, d_model]`.
    /// * `rope` - the rotary tables over positions `0..time`.
    /// * `valid` - `[batch, time]`, true at the frames that are not padding.
    ///
    /// # Returns
    /// `[batch, time, d_model]`.
    pub fn forward(
        &self,
        x: Tensor<3>,
        rope: &crate::blocks::transformers::embedding::RotaryEmbedding,
        valid: Option<Tensor<2, burn::prelude::Bool>>,
    ) -> Tensor<3> {
        let x = x.clone() + self.attn.forward(self.attn_norm.forward(x), rope, valid);
        x.clone() + self.mlp.forward(self.mlp_norm.forward(x))
    }
}

/// Common meta for [`RopeTransformerEncoder`] and
/// [`RopeTransformerEncoderConfig`].
pub trait RopeTransformerEncoderMeta {
    /// The model width.
    fn d_model(&self) -> usize;

    /// The layer count.
    fn n_layers(&self) -> usize;

    /// The head count.
    fn n_heads(&self) -> usize;
}

/// Config for [`RopeTransformerEncoder`].
///
/// Implements [`RopeTransformerEncoderMeta`].
#[derive(Config, Debug)]
pub struct RopeTransformerEncoderConfig {
    /// The layer every block is built from.
    pub block: RopeEncoderBlockConfig,

    /// The layer count.
    pub n_layers: usize,

    /// The rotary embedding's base, `theta`.
    #[config(default = "10000")]
    pub rope_base: usize,
}

impl RopeTransformerEncoderMeta for RopeTransformerEncoderConfig {
    fn d_model(&self) -> usize {
        self.block.d_model
    }

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

    fn n_heads(&self) -> usize {
        self.block.n_heads
    }
}

impl ModuleInit<RopeTransformerEncoder> for RopeTransformerEncoderConfig {
    fn try_init(
        &self,
        device: &Device,
    ) -> BunsenResult<RopeTransformerEncoder> {
        let norm = || {
            self.block
                .norm
                .clone()
                .with_num_features(self.block.d_model)
                .init(device)
        };
        Ok(RopeTransformerEncoder {
            embed_norm: norm(),
            layers: (0..self.n_layers)
                .map(|_| self.block.try_init(device))
                .collect::<BunsenResult<_>>()?,
            final_norm: norm(),
            d_model: self.block.d_model,
            rope_base: self.rope_base,
        })
    }
}

/// A pre-norm transformer encoder with rotary position embeddings: a norm
/// over the input, the [`RopeEncoderBlock`]s, and a final norm, each its
/// block config's ([`RopeEncoderBlockConfig::norm`]).
///
/// The rotary table runs over positions `0..time` of the whole input, in the
/// `transformers` (`rotate_half`) convention.
///
/// Built by [`RopeTransformerEncoderConfig`].
/// Implements [`RopeTransformerEncoderMeta`].
#[derive(Module, Debug)]
pub struct RopeTransformerEncoder {
    /// The input's normalization.
    pub embed_norm: Normalization,

    /// The layers.
    pub layers: Vec<RopeEncoderBlock>,

    /// The output's normalization.
    pub final_norm: Normalization,

    /// The model width.
    #[module(skip)]
    pub d_model: usize,

    /// The rotary embedding's base.
    #[module(skip)]
    pub rope_base: usize,
}

impl RopeTransformerEncoderMeta for RopeTransformerEncoder {
    fn d_model(&self) -> usize {
        self.d_model
    }

    fn n_layers(&self) -> usize {
        self.layers.len()
    }

    fn n_heads(&self) -> usize {
        self.layers.first().map_or(0, |l| l.n_heads())
    }
}

impl RopeTransformerEncoder {
    /// Encodes.
    ///
    /// # Arguments
    /// * `x` - `[batch, time, d_model]`.
    /// * `lengths` - each item's valid frame count; `None` means every item
    ///   fills `time`.
    ///
    /// # Returns
    /// `[batch, time, d_model]`. Frames past an item's length hold values
    /// that mean nothing.
    pub fn forward(
        &self,
        x: Tensor<3>,
        lengths: Option<&[usize]>,
    ) -> Tensor<3> {
        let [batch, time] = unpack_shape_contract!(
            ["batch", "time", "d_model"],
            &x.dims(),
            &["batch", "time"],
            &[("d_model", self.d_model())],
        );
        let device = x.device();
        let head_dim = self.d_model() / self.n_heads().max(1);
        let rope = RotaryEmbeddingConfig::new(time, head_dim)
            .with_base(self.rope_base)
            .with_direction(RotaryDirection::Forward)
            .init(&device);
        let valid = lengths.map(|lengths| length_mask(lengths, time, &device));

        let mut x = self.embed_norm.forward(x);
        for layer in &self.layers {
            x = layer.forward(x, &rope, valid.clone());
        }
        let y = self.final_norm.forward(x);

        #[cfg(not(any(debug_assertions, test)))]
        let _ = batch;
        #[cfg(any(debug_assertions, test))]
        crate::contracts::assert_shape_contract_periodically!(
            ["batch", "time", "d_model"],
            &y.dims(),
            &[
                ("batch", batch),
                ("time", time),
                ("d_model", self.d_model())
            ],
        );
        y
    }
}

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

    use super::*;
    use crate::support::testing::{
        cpu_device,
        seeded_tensor,
    };

    /// A toy config, non-default in every field the metas read.
    fn toy() -> RopeTransformerEncoderConfig {
        RopeTransformerEncoderConfig::new(
            RopeEncoderBlockConfig::new(16, 2)
                .with_ff_expansion(3)
                .with_qkv_bias(true),
            3,
        )
        .with_rope_base(500)
    }

    #[test]
    fn test_meta() {
        let config = toy();
        let module: RopeTransformerEncoder = config.init(&cpu_device());
        assert_eq!(module.d_model(), config.d_model());
        assert_eq!(module.n_layers(), config.n_layers());
        assert_eq!(module.n_heads(), config.n_heads());
        assert_eq!(module.rope_base, 500);

        let block = &module.layers[0];
        assert_eq!(block.d_model(), config.block.d_model());
        assert_eq!(block.n_heads(), config.block.n_heads());
        assert_eq!(block.d_ff(), config.block.d_ff());
        assert!(block.attn.query.bias.is_some());
    }

    /// The stack is its parts, in order: input norm, blocks, output norm.
    #[test]
    fn test_composition() {
        let device = cpu_device();
        let module: RopeTransformerEncoder = toy().init(&device);
        let x = seeded_tensor::<3>(1, [2, 7, 16], Distribution::Default, &device);
        let got = module.forward(x.clone(), None);

        let rope = RotaryEmbeddingConfig::new(7, 8)
            .with_base(500)
            .with_direction(RotaryDirection::Forward)
            .init(&device);
        let mut want = module.embed_norm.forward(x);
        for layer in &module.layers {
            let attended = want.clone()
                + layer
                    .attn
                    .forward(layer.attn_norm.forward(want.clone()), &rope, None);
            want = attended.clone() + layer.mlp.forward(layer.mlp_norm.forward(attended));
        }
        let want = module.final_norm.forward(want);
        got.into_data()
            .assert_approx_eq::<f32>(&want.into_data(), Tolerance::absolute(1e-5));
    }

    /// A padded item's valid frames are the item's own.
    #[test]
    fn test_padding_is_ignored() {
        let device = cpu_device();
        let module: RopeTransformerEncoder = toy().init(&device);
        let x = seeded_tensor::<3>(2, [1, 5, 16], Distribution::Default, &device);
        let junk = seeded_tensor::<3>(3, [1, 4, 16], Distribution::Default, &device);
        let alone = module.forward(x.clone(), None);
        let padded = module
            .forward(Tensor::cat(vec![x, junk], 1), Some(&[5]))
            .slice_dim(1, 0..5);
        padded
            .into_data()
            .assert_approx_eq::<f32>(&alone.into_data(), Tolerance::absolute(1e-4));
    }
}