bunsen 0.42.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
//! The `ModernBERT` encoder.
use burn::{
    Tensor,
    config::Config,
    module::Module,
    nn::LayerNormConfig,
    prelude::Int,
    tensor::Device,
};

use crate::{
    blocks::{
        activation::BunsenActivationConfig,
        norm::BunsenNormalizationConfig,
        transformers::{
            attention::csa::{
                AttentionMask,
                CausalSelfAttentionConfig,
            },
            decoders::{
                causal::CausalDecoderBlockConfig,
                stack::{
                    DecoderStack,
                    DecoderStackMeta,
                    DecoderStackStructureConfig,
                    PatternedStackConfig,
                },
            },
            embedding::{
                RotaryDirection,
                RotaryEmbeddingConfig,
                TokenEmbedding,
                TokenEmbeddingConfig,
                TokenEmbeddingMeta,
            },
            mlp::TransformerGatedMlpConfig,
        },
    },
    burner::module::{
        CanonicalKeys,
        ModuleInit,
        Rekey,
        ToStructureConfig,
    },
    errors::{
        BunsenResult,
        ConstraintError,
        Rule,
    },
};

/// Common meta for [`ModernBertModel`] and [`ModernBertConfig`].
pub trait ModernBertMeta {
    /// The token count: the embedding's rows.
    fn n_vocab(&self) -> usize;

    /// The model width.
    fn d_model(&self) -> usize;

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

/// A `ModernBERT` layer's attention: over every position, or a two-sided
/// window about the query (`transformers`' `layer_types`:
/// `full_attention`, `sliding_attention`).
#[derive(Config, Copy, Debug, PartialEq, Eq)]
pub enum ModernBertLayerKind {
    /// Attention over every position, with the global rotary base.
    Global,

    /// Attention over the positions within
    /// [`local_radius`](ModernBertConfig::local_radius) of the query, on
    /// either side, with the local rotary base.
    Local,
}

/// Config for [`ModernBertModel`]: `transformers`' `ModernBertConfig`, as
/// far as the encoder's forward needs it.
///
/// Implements [`ModernBertMeta`].
#[derive(Config, Debug)]
pub struct ModernBertConfig {
    /// The token count.
    pub n_vocab: usize,

    /// The model width.
    pub d_model: usize,

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

    /// The attention head count; each head is `d_model / n_heads` wide.
    pub n_heads: usize,

    /// The feed-forward hidden width: each half of the fused `Wi`.
    pub d_ff: usize,

    /// The `LayerNorm` epsilon.
    #[config(default = "1e-5")]
    pub norm_eps: f64,

    /// Each layer's attention kind; empty, `transformers`' pattern: every
    /// third layer global, from the first.
    #[config(default = "Vec::new()")]
    pub layer_kinds: Vec<ModernBertLayerKind>,

    /// How far a local layer's query sees on either side, in positions:
    /// `local_attention / 2` (64 of 128).
    #[config(default = "64")]
    pub local_radius: usize,

    /// The global layers' rotary base.
    #[config(default = "160000.0")]
    pub global_rope_theta: f64,

    /// The local layers' rotary base.
    #[config(default = "10000.0")]
    pub local_rope_theta: f64,
}

impl ModernBertMeta for ModernBertConfig {
    fn n_vocab(&self) -> usize {
        self.n_vocab
    }

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

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

impl ModernBertConfig {
    /// `ModernBERT-base`'s geometry: 22 layers of width 768, 12 heads, a
    /// hidden width of 1152, 50368 tokens.
    pub fn modernbert_base() -> Self {
        Self::new(50368, 768, 22, 12, 1152)
    }

    /// `ModernBERT-large`'s geometry: 28 layers of width 1024, 16 heads, a
    /// hidden width of 2624, 50368 tokens.
    pub fn modernbert_large() -> Self {
        Self::new(50368, 1024, 28, 16, 2624)
    }

    /// Layer `i`'s attention kind.
    pub fn layer_kind(
        &self,
        i: usize,
    ) -> ModernBertLayerKind {
        match self.layer_kinds.get(i) {
            Some(&kind) => kind,
            None if self.layer_kinds.is_empty() && !i.is_multiple_of(3) => {
                ModernBertLayerKind::Local
            }
            None => ModernBertLayerKind::Global,
        }
    }

    /// A `LayerNorm` without a bias, its width set where it is used.
    fn norm(&self) -> BunsenNormalizationConfig {
        BunsenNormalizationConfig::Layer(
            LayerNormConfig::new(0)
                .with_epsilon(self.norm_eps)
                .with_bias(false),
        )
    }

    /// The token embedding, its rows normalized.
    pub fn embedding(&self) -> TokenEmbeddingConfig {
        TokenEmbeddingConfig::new(self.n_vocab, self.d_model).with_norm(Some(self.norm()))
    }

    /// A layer of `kind`, with or without its attention norm.
    fn layer(
        &self,
        kind: ModernBertLayerKind,
        attn_norm: bool,
    ) -> CausalDecoderBlockConfig {
        let window = match kind {
            ModernBertLayerKind::Global => None,
            ModernBertLayerKind::Local => Some(self.local_radius + 1),
        };
        CausalDecoderBlockConfig::new(
            CausalSelfAttentionConfig::new(self.n_heads, self.n_heads, self.d_model)
                .with_norm(None)
                .with_mask(AttentionMask::Bidirectional)
                .with_sliding_window(window)
                .into(),
            TransformerGatedMlpConfig::new(self.d_model, self.d_ff)
                .with_activation(BunsenActivationConfig::Gelu)
                .into(),
        )
        .with_norm(self.norm())
        .with_attn_norm(attn_norm)
    }

    /// A rotary table of base `theta`, in the `rotate_half` convention.
    fn rotary(
        &self,
        theta: f64,
    ) -> RotaryEmbeddingConfig {
        RotaryEmbeddingConfig::new(0, self.d_model / self.n_heads)
            .with_base(theta as usize)
            .with_direction(RotaryDirection::Forward)
    }

    /// The encoder stack: three kinds of layer (the first, global and
    /// without an attention norm; global; local), the local kind with its
    /// rotary table; a `LayerNorm` over the stack's output.
    ///
    /// # Errors
    /// [`Illegal`](crate::errors::BunsenErrorKind::Illegal), with a
    /// [`ConstraintError`] cause, for `layer_kinds` of another length than
    /// the layers, or heads that do not divide the width.
    pub fn stack(&self) -> BunsenResult<PatternedStackConfig> {
        if !self.layer_kinds.is_empty() && self.layer_kinds.len() != self.n_layers {
            return Err(ConstraintError::new(
                "ModernBertConfig",
                "layer_kinds",
                Rule::Relation {
                    lhs: ("layer_kinds.len".into(), self.layer_kinds.len().to_string()),
                    op: "==",
                    rhs: ("n_layers".into(), self.n_layers.to_string()),
                },
            )
            .into());
        }
        if self.n_heads == 0 || !self.d_model.is_multiple_of(self.n_heads) {
            return Err(ConstraintError::new(
                "ModernBertConfig",
                "n_heads",
                Rule::Relation {
                    lhs: ("n_heads".into(), self.n_heads.to_string()),
                    op: "divides",
                    rhs: ("d_model".into(), self.d_model.to_string()),
                },
            )
            .into());
        }
        // The first layer's kind, then each kind with its attention norm.
        let first = self.layer_kind(0);
        let kinds = vec![
            self.layer(first, false),
            self.layer(ModernBertLayerKind::Global, true),
            self.layer(ModernBertLayerKind::Local, true),
        ];
        let layer_kinds = (0..self.n_layers)
            .map(|i| match (i, self.layer_kind(i)) {
                (0, _) => 0,
                (_, ModernBertLayerKind::Global) => 1,
                (_, ModernBertLayerKind::Local) => 2,
            })
            .collect();
        let local = Some(0);
        Ok(PatternedStackConfig::new(
            kinds,
            layer_kinds,
            self.norm(),
            self.rotary(self.global_rope_theta),
        )
        .with_rotary_alts(vec![self.rotary(self.local_rope_theta)])
        .with_kind_rotary(vec![
            (first == ModernBertLayerKind::Local).then_some(0),
            None,
            local,
        ]))
    }
}

impl ToStructureConfig for ModernBertConfig {
    type Structure = ModernBertStructureConfig;

    /// # Errors
    /// As [`stack`](Self::stack) and its lowering.
    fn try_to_structure(&self) -> BunsenResult<ModernBertStructureConfig> {
        Ok(ModernBertStructureConfig::new(
            self.embedding(),
            self.stack()?.try_to_structure()?,
        ))
    }
}

/// Structure config for [`ModernBertModel`]: its token embedding and its
/// encoder stack, unrolled. [`ModernBertConfig`] lowers to it.
#[derive(Config, Debug)]
pub struct ModernBertStructureConfig {
    /// The token embedding, its rows normalized.
    pub embedding: TokenEmbeddingConfig,

    /// The encoder: its layers, final norm and rotary tables.
    pub stack: DecoderStackStructureConfig,
}

impl ModuleInit<ModernBertModel> for ModernBertStructureConfig {
    fn try_init(
        &self,
        device: &Device,
    ) -> BunsenResult<ModernBertModel> {
        Ok(ModernBertModel {
            embeddings: self.embedding.try_init(device)?,
            stack: self.stack.try_init(device)?,
        })
    }
}

/// Answer.AI's `ModernBERT` encoder: a normalized token embedding, then a
/// [`DecoderStack`] of bidirectional `GeGLU` layers, global and local by
/// layer ([`ModernBertConfig::stack`]), and a final `LayerNorm`.
///
/// [`transformers_rekey`](crate::kits::text::encoders::modernbert::weights::transformers_rekey)
/// maps a `transformers` state dict's names onto it, after
/// [`transformers_row_splits`](crate::kits::text::encoders::modernbert::weights::transformers_row_splits)
/// has split its fused projections.
///
/// Built by [`ModernBertConfig`] (high-level) or
/// [`ModernBertStructureConfig`].
/// Implements [`ModernBertMeta`].
#[derive(Module, Debug)]
pub struct ModernBertModel {
    /// The token embedding, its rows normalized.
    pub embeddings: TokenEmbedding,

    /// The encoder: its layers, final norm and rotary tables.
    pub stack: DecoderStack,
}

impl ModernBertMeta for ModernBertModel {
    fn n_vocab(&self) -> usize {
        self.embeddings.n_vocab()
    }

    fn d_model(&self) -> usize {
        self.embeddings.d_model()
    }

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

impl ModernBertModel {
    /// The final hidden states: the tokens embedded, then encoded, each
    /// item's padding keys masked by its length.
    ///
    /// # Arguments
    /// * `ids` - `[batch, seq]`.
    /// * `lengths` - each item's tokens, the rest padding; `None`, no padding.
    ///
    /// # Returns
    /// `[batch, seq, d_model]`, normalized. A padding position's state means
    /// nothing.
    pub fn forward(
        &self,
        ids: Tensor<2, Int>,
        lengths: Option<&[usize]>,
    ) -> Tensor<3> {
        self.stack
            .encoder_forward(self.embeddings.forward(ids), lengths)
    }
}

impl CanonicalKeys for ModernBertModel {
    fn canonical_rekey(&self) -> Rekey {
        Rekey::keep_rest().mount_canonical("stack.", &self.stack)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::support::testing::cpu_device;

    /// `transformers`' default pattern: every third layer global, from the
    /// first; the first layer alone without an attention norm; the local
    /// layers windowed `local_radius + 1` (two-sided) with the local table.
    #[test]
    fn test_layer_pattern() {
        let config = ModernBertConfig::new(32, 16, 5, 2, 24).with_local_radius(3);
        let kinds: Vec<_> = (0..5).map(|i| config.layer_kind(i)).collect();
        use ModernBertLayerKind::*;
        assert_eq!(kinds, [Global, Local, Local, Global, Local]);

        let model: ModernBertModel = config.init(&cpu_device());
        let windows = model.stack.layer_windows();
        assert_eq!(windows, [None, Some(4), Some(4), None, Some(4)]);
        let norms: Vec<bool> = model
            .stack
            .layers
            .iter()
            .map(|l| l.attn.pre.is_some())
            .collect();
        assert_eq!(norms, [false, true, true, true, true]);
        assert_eq!(
            model.stack.layer_rotary,
            [None, Some(0), Some(0), None, Some(0)]
        );
        assert!(model.embeddings.norm.is_some());
    }

    #[test]
    fn test_layer_kinds_must_cover_the_layers() {
        let config = ModernBertConfig::new(32, 16, 3, 2, 24)
            .with_layer_kinds(vec![ModernBertLayerKind::Global]);
        assert!(config.stack().is_err());
        assert!(
            ModernBertConfig::new(32, 16, 3, 3, 24).stack().is_err(),
            "3 heads over 16"
        );
    }
}