bunsen 0.41.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
//! Transformer feed-forwards: [`Mlp`], [`GatedMlp`], and [`FeedForward`],
//! either one chosen by config.

use burn::{
    config::Config,
    module::Module,
    nn::{
        Linear,
        LinearConfig,
        activation::{
            Activation,
            ActivationConfig,
        },
        norm::Normalization,
    },
    prelude::Tensor,
    tensor::Device,
};

use crate::{
    burner::module::ModuleInit,
    errors::BunsenResult,
};

/// Computes a normalized MLP: `mlp(norm(x))`.
///
/// # Arguments
/// * `norm` - the sublayer's [`Normalization`].
/// * `mlp` - `Mlp`.
/// * `x` - `[batch, seq_len, n_states]` input.
///
/// # Returns
/// `[batch, seq_len, n_states]`
pub fn norm_mlp(
    norm: &Normalization,
    mlp: &Mlp,
    x: Tensor<3>,
) -> Tensor<3> {
    mlp.forward(norm.forward(x))
}

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

    /// Returns the post-activation exponent.
    fn act_exponent(&self) -> Option<f64>;
}

/// Config for [`Mlp`].
#[derive(Config, Debug)]
pub struct MlpConfig {
    /// Embedding Size.
    pub n_embed: usize,

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

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

    /// Post-Activation Exponent.
    #[config(default = "None")]
    pub act_exponent: Option<f64>,

    /// Whether the projections carry a bias term.
    #[config(default = "false")]
    pub bias: bool,
}

impl MlpMeta for MlpConfig {
    fn n_embed(&self) -> usize {
        self.n_embed
    }

    fn act_exponent(&self) -> Option<f64> {
        self.act_exponent
    }
}

impl MlpConfig {
    /// Returns the size of the hidden layer.
    pub fn hidden_size(&self) -> usize {
        self.n_embed * self.expansion_factor
    }
}

impl ModuleInit<Mlp> for MlpConfig {
    fn try_init(
        &self,
        device: &Device,
    ) -> BunsenResult<Mlp> {
        let linear1 = LinearConfig::new(self.n_embed(), self.hidden_size())
            .with_bias(self.bias)
            .init(device);
        let linear2 = LinearConfig::new(self.hidden_size(), self.n_embed())
            .with_bias(self.bias)
            .init(device);

        Ok(Mlp {
            linear1,
            act: self.activation.init(device),
            act_exponent: self.act_exponent,
            linear2,
        })
    }
}

/// GPT Block MLP Module
///
/// The position-wise feed-forward block of a transformer: an up-projection to
/// an expanded hidden size, an activation (optionally raised to a configured
/// exponent), and a down-projection back to the embedding size. Construct via
/// [`MlpConfig`] and `.init(device)`, then call [`forward`](Mlp::forward) on a
/// `[batch, time, embed]` input.
///
/// Built by [`MlpConfig`].
#[derive(Module, Debug)]
pub struct Mlp {
    /// Feed Forward Layer.
    pub linear1: Linear,

    /// Activation.
    pub act: Activation,

    /// Post-Activation Exponent.
    pub act_exponent: Option<f64>,

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

impl MlpMeta for Mlp {
    fn n_embed(&self) -> usize {
        self.linear1.weight.dims()[0]
    }

    fn act_exponent(&self) -> Option<f64> {
        self.act_exponent
    }
}

impl Mlp {
    /// MLP Forward Pass.
    ///
    /// # Arguments
    /// - `x`: a `[batch, time, embed]` input.
    ///
    /// # Returns
    /// a `[batch, time, embed]` result.
    pub fn forward(
        &self,
        x: Tensor<3>,
    ) -> Tensor<3> {
        #[cfg(any(debug_assertions, test))]
        let [batch, time] = crate::contracts::unpack_shape_contract!(
            ["batch", "time", "embed"],
            &x,
            &["batch", "time"],
            &[("embed", self.n_embed())]
        );

        let x = self.linear1.forward(x);
        let x = self.act.forward(x);

        let x = match self.act_exponent {
            Some(exp) => x.powf_scalar(exp),
            None => x,
        };

        let x = self.linear2.forward(x);

        #[cfg(any(debug_assertions, test))]
        crate::contracts::assert_shape_contract_periodically!(
            ["batch", "time", "embed"],
            &x,
            &[("batch", batch), ("time", time), ("embed", self.n_embed())]
        );

        x
    }
}

/// Common meta for [`GatedMlp`] and [`GatedMlpConfig`].
pub trait GatedMlpMeta {
    /// The input and output width.
    fn n_embed(&self) -> usize;

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

/// Config for [`GatedMlp`].
///
/// Implements [`GatedMlpMeta`].
#[derive(Config, Debug)]
pub struct GatedMlpConfig {
    /// The input and output width.
    pub n_embed: usize,

    /// The hidden width.
    pub d_ff: usize,

    /// Whether the three projections carry a bias.
    #[config(default = "false")]
    pub bias: bool,
}

impl GatedMlpMeta for GatedMlpConfig {
    fn n_embed(&self) -> usize {
        self.n_embed
    }

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

impl ModuleInit<GatedMlp> for GatedMlpConfig {
    fn try_init(
        &self,
        device: &Device,
    ) -> BunsenResult<GatedMlp> {
        let linear = |i, o| LinearConfig::new(i, o).with_bias(self.bias).init(device);
        Ok(GatedMlp {
            gate_proj: linear(self.n_embed, self.d_ff),
            up_proj: linear(self.n_embed, self.d_ff),
            down_proj: linear(self.d_ff, self.n_embed),
        })
    }
}

/// The `SwiGLU` feed-forward block (Shazeer 2020): `down(silu(gate(x)) *
/// up(x))`, the feed-forward of Llama, Qwen and their kin.
///
/// The fields carry the names `transformers` gives them.
///
/// Built by [`GatedMlpConfig`].
/// Implements [`GatedMlpMeta`].
#[derive(Module, Debug)]
pub struct GatedMlp {
    /// `n_embed -> d_ff`, the gate.
    pub gate_proj: Linear,

    /// `n_embed -> d_ff`.
    pub up_proj: Linear,

    /// `d_ff -> n_embed`.
    pub down_proj: Linear,
}

impl GatedMlpMeta for GatedMlp {
    fn n_embed(&self) -> usize {
        self.gate_proj.weight.dims()[0]
    }

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

impl GatedMlp {
    /// Applies the block.
    ///
    /// # Arguments
    /// * `x` - `[batch, seq, n_embed]`.
    ///
    /// # Returns
    /// `[batch, seq, n_embed]`.
    pub fn forward(
        &self,
        x: Tensor<3>,
    ) -> Tensor<3> {
        let [batch, seq] = crate::contracts::unpack_shape_contract!(
            ["batch", "seq", "n_embed"],
            &x.dims(),
            &["batch", "seq"],
            &[("n_embed", self.n_embed())]
        );
        let gate = burn::tensor::activation::silu(self.gate_proj.forward(x.clone()));
        let y = self.down_proj.forward(gate * self.up_proj.forward(x));
        crate::contracts::assert_shape_contract_periodically!(
            ["batch", "seq", "n_embed"],
            &y.dims(),
            &[("batch", batch), ("seq", seq), ("n_embed", self.n_embed())]
        );
        y
    }
}

/// Common meta for [`FeedForward`] and [`FeedForwardConfig`].
pub trait FeedForwardMeta {
    /// The input and output width.
    fn n_embed(&self) -> usize;
}

/// Config for [`FeedForward`]: which position-wise feed-forward, and its
/// config.
///
/// Implements [`FeedForwardMeta`].
#[derive(Config, Debug)]
pub enum FeedForwardConfig {
    /// [`Mlp`]: expand, activate, project back.
    Mlp(MlpConfig),

    /// [`GatedMlp`]: the `SwiGLU` feed-forward.
    Gated(GatedMlpConfig),
}

impl From<MlpConfig> for FeedForwardConfig {
    fn from(config: MlpConfig) -> Self {
        Self::Mlp(config)
    }
}

impl From<GatedMlpConfig> for FeedForwardConfig {
    fn from(config: GatedMlpConfig) -> Self {
        Self::Gated(config)
    }
}

impl FeedForwardMeta for FeedForwardConfig {
    fn n_embed(&self) -> usize {
        match self {
            Self::Mlp(c) => MlpMeta::n_embed(c),
            Self::Gated(c) => GatedMlpMeta::n_embed(c),
        }
    }
}

impl ModuleInit<FeedForward> for FeedForwardConfig {
    fn try_init(
        &self,
        device: &Device,
    ) -> BunsenResult<FeedForward> {
        Ok(match self {
            Self::Mlp(c) => FeedForward::Mlp(c.try_init(device)?),
            Self::Gated(c) => FeedForward::Gated(c.try_init(device)?),
        })
    }
}

/// A transformer layer's position-wise feed-forward, whichever its config
/// chose: the generic sublayer a layer holds, as
/// [`Normalization`] is for a norm. The variant
/// is not part of its tensors' paths for the loaders bunsen uses.
///
/// Built by [`FeedForwardConfig`].
/// Implements [`FeedForwardMeta`].
#[derive(Module, Debug)]
pub enum FeedForward {
    /// [`Mlp`].
    Mlp(Mlp),

    /// [`GatedMlp`].
    Gated(GatedMlp),
}

impl FeedForwardMeta for FeedForward {
    fn n_embed(&self) -> usize {
        match self {
            Self::Mlp(m) => MlpMeta::n_embed(m),
            Self::Gated(m) => GatedMlpMeta::n_embed(m),
        }
    }
}

impl FeedForward {
    /// The feed-forward, position by position.
    ///
    /// # Arguments
    /// * `x` - `[batch, seq_len, n_embed]`.
    ///
    /// # Returns
    /// `[batch, seq_len, n_embed]`.
    pub fn forward(
        &self,
        x: Tensor<3>,
    ) -> Tensor<3> {
        match self {
            Self::Mlp(m) => m.forward(x),
            Self::Gated(m) => m.forward(x),
        }
    }
}

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

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

    #[test]
    fn test_mlp_config() {
        let cfg = MlpConfig::new(3);

        assert_eq!(cfg.n_embed, 3);
        assert_eq!(cfg.expansion_factor, 4);

        assert_eq!(cfg.n_embed(), 3);
        assert_eq!(cfg.hidden_size(), 3 * 4);
    }

    #[test]
    #[serial]
    fn test_mlp() {
        let device = performance_device();
        let _memory = DeviceMemoryGuard::new(&device);

        for activation in [ActivationConfig::Relu, ActivationConfig::Gelu] {
            for ef in [4, 3] {
                let b = 2;
                let t = 3;
                let n_embed = 10;

                let cfg = MlpConfig::new(n_embed)
                    .with_expansion_factor(ef)
                    .with_activation(activation.clone())
                    .with_act_exponent(Some(2.0));

                let mlp: Mlp = cfg.init(&device);

                assert_eq!(mlp.n_embed(), n_embed);

                let input = Tensor::random([b, t, n_embed], Distribution::Default, &device);
                let output = mlp.forward(input.clone());

                let x = input;
                assert_shape_contract!(
                    ["batch", "time", "embed"],
                    &x.dims(),
                    &[("batch", b), ("time", t), ("embed", n_embed)]
                );

                let x = mlp.linear1.forward(x);
                assert_shape_contract!(
                    ["batch", "time", "hidden"],
                    &x.dims(),
                    &[("batch", b), ("time", t), ("hidden", ef * n_embed)]
                );

                let x = mlp.act.forward(x);
                assert_shape_contract!(
                    ["batch", "time", "hidden"],
                    &x.dims(),
                    &[("batch", b), ("time", t), ("hidden", ef * n_embed)]
                );

                let x = x.clone() * x;
                let x = mlp.linear2.forward(x);
                assert_shape_contract!(
                    ["batch", "time", "embed"],
                    &x.dims(),
                    &[("batch", b), ("time", t), ("embed", n_embed)]
                );

                output.to_data().assert_eq(&x.to_data(), true);
            }
        }
    }

    #[test]
    fn test_gated_mlp() {
        use crate::support::testing::cpu_device;
        let config = GatedMlpConfig::new(6, 10).with_bias(true);
        let mlp: GatedMlp = config.init(&cpu_device());
        assert_eq!(mlp.n_embed(), config.n_embed());
        assert_eq!(mlp.d_ff(), config.d_ff());
        assert!(mlp.up_proj.bias.is_some());
        // silu(0) = 0, so a zero input gives the down projection's bias.
        let y = mlp.forward(Tensor::zeros([2, 3, 6], &cpu_device()));
        assert_eq!(y.dims(), [2, 3, 6]);
    }
}