oxidized_transformers/layers/attention/
self_attention.rs

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
640
641
642
use candle_core::{DType, IndexOp, Module, ModuleT, Tensor};
use candle_nn::{linear, linear_no_bias, Linear, VarBuilder};
use snafu::{ensure, ResultExt, Snafu};

use crate::error::BoxedError;
use crate::kv_cache::LayerKeyValueCache;
use crate::layers::attention::{
    Attention, AttentionMask, AttentionMaskError, AttentionScorer, BuildAttention,
    BuildAttentionScorer, ScaledDotProductAttentionConfig, ScaledDotProductAttentionError,
};
use crate::layers::build_module::BuildModule;
use crate::layers::embeddings::{
    QueryKeyRotaryEmbeddings, QueryKeyRotaryEmbeddingsConfig, QueryKeyRotaryEmbeddingsError,
};
use crate::layers::identity::Identity;
use crate::util::tensor_ext::MinLike;

/// Attention heads configuration to use in self-attention.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AttentionHeads {
    pub n_query_heads: usize,
    pub n_key_value_heads: usize,
    pub qkv_mode: QkvMode,
}

/// Query, key, value splitting strategies.
///
/// After the input projection of the attention layer, we have an array with
/// shape `(batch_size, seq_len, n_heads * head_width)` where `n_heads` is
/// the sum of the number of query, key, and value heads. We need to split up
/// the array into separate arrays for query, key, and value heads.
#[non_exhaustive]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum QkvSplit {
    Default,
    KVSizedChunks,
}

/// How the query, key and value projections are handled in
/// the self-attention layer.
#[non_exhaustive]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum QkvMode {
    Separate,
    MergedSplitBefore,
    MergedSplitAfter(QkvSplit),
}

/// Representation of the query, key and value tensors.
pub enum QkvTensors {
    MergedSplitBefore(Linear),
    MergedSplitAfter {
        qkv: Linear,
        split: QkvSplit,
    },
    Separate {
        query: Linear,
        key: Linear,
        value: Linear,
    },
}

/// Configuration for self-attention modules.
#[derive(Debug)]
pub struct SelfAttentionConfig {
    /// Attention head configuration.
    attention_heads: AttentionHeads,

    /// Attention scorer.
    attention_scorer: Box<dyn BuildAttentionScorer>,

    /// Dropout probability to apply after attention.
    dropout: Box<dyn BuildModule>,

    /// Hidden width of the transformer.
    hidden_width: usize,

    /// Layer norm.
    layer_norm: Box<dyn BuildModule>,

    /// Rotary embedding configuration.
    rotary_embeddings: Option<QueryKeyRotaryEmbeddingsConfig>,

    /// Use bias in linear layers.
    use_bias: bool,

    /// Use parallel attention.
    use_parallel_attention: bool,
}

impl SelfAttentionConfig {
    /// Attention head configuration.
    pub fn attention_heads(mut self, attention_heads: AttentionHeads) -> Self {
        self.attention_heads = attention_heads;
        self
    }

    /// Attention scorer.
    ///
    /// Default: `ScaledDotProductAttentionConfig::default()`.
    pub fn attention_scorer(mut self, attention_scorer: Box<dyn BuildAttentionScorer>) -> Self {
        self.attention_scorer = attention_scorer;
        self
    }

    /// Dropout to apply after attention.
    ///
    /// Default: `Identity`.
    pub fn dropout(mut self, dropout: Box<dyn BuildModule>) -> Self {
        self.dropout = dropout;
        self
    }

    /// Hidden width of the transformer.
    ///
    /// Default: `768`.
    pub fn hidden_width(mut self, hidden_width: usize) -> Self {
        self.hidden_width = hidden_width;
        self
    }

    /// Layer norm applied to the input.
    ///
    /// Default: `Identity`.
    pub fn layer_norm(mut self, layer_norm: Box<dyn BuildModule>) -> Self {
        self.layer_norm = layer_norm;
        self
    }

    /// Configuration for rotary embeddings.
    ///
    /// Default: `None`.
    pub fn rotary_embeddings(
        mut self,
        rotary_embeddings: Option<QueryKeyRotaryEmbeddingsConfig>,
    ) -> Self {
        self.rotary_embeddings = rotary_embeddings;
        self
    }

    /// Use bias in linear layers.
    ///
    /// Default: `false`.
    pub fn use_bias(mut self, use_bias: bool) -> Self {
        self.use_bias = use_bias;
        self
    }

    /// Use parallel attention.
    ///
    /// Default: `false`.
    pub fn use_parallel_attention(mut self, use_parallel_attention: bool) -> Self {
        self.use_parallel_attention = use_parallel_attention;
        self
    }
}

impl Default for SelfAttentionConfig {
    fn default() -> Self {
        Self {
            attention_heads: AttentionHeads {
                n_query_heads: 12,
                n_key_value_heads: 12,
                qkv_mode: QkvMode::Separate,
            },
            attention_scorer: Box::<ScaledDotProductAttentionConfig>::default(),
            dropout: Box::new(Identity),
            hidden_width: 768,
            layer_norm: Box::new(Identity),
            rotary_embeddings: None,
            use_bias: false,
            use_parallel_attention: false,
        }
    }
}

impl BuildAttention for SelfAttentionConfig {
    /// Build a self-attention module.
    fn build(&self, vb: VarBuilder) -> Result<Box<dyn Attention>, BoxedError> {
        let hidden_width = self.hidden_width;
        let n_key_value_heads = self.attention_heads.n_key_value_heads;
        let n_query_heads = self.attention_heads.n_query_heads;

        ensure!(
            hidden_width % n_query_heads == 0,
            InvalidNQueryHeadsSnafu {
                hidden_width,
                n_query_heads,
            }
        );

        if self.attention_heads.qkv_mode == QkvMode::MergedSplitBefore {
            ensure!(
                n_query_heads == n_key_value_heads,
                InvalidMergedSplitBeforeNHeadsSnafu {
                    n_key_value_heads,
                    n_query_heads
                }
            )
        }

        let linear_ctor = if self.use_bias {
            linear
        } else {
            linear_no_bias
        };

        let head_width = hidden_width / n_query_heads;
        let key_value_width = n_key_value_heads * head_width;
        let output = linear_ctor(hidden_width, hidden_width, vb.push_prefix("output"))
            .context(SelfAttentionConstructionSnafu)?;
        let qkv = match self.attention_heads.qkv_mode {
            QkvMode::MergedSplitBefore => QkvTensors::MergedSplitBefore(
                linear_ctor(
                    hidden_width,
                    hidden_width + 2 * key_value_width,
                    vb.push_prefix("qkv"),
                )
                .context(SelfAttentionConstructionSnafu)?,
            ),
            QkvMode::MergedSplitAfter(split) => QkvTensors::MergedSplitAfter {
                qkv: linear_ctor(
                    hidden_width,
                    hidden_width + 2 * key_value_width,
                    vb.push_prefix("qkv"),
                )
                .context(SelfAttentionConstructionSnafu)?,
                split,
            },
            QkvMode::Separate => QkvTensors::Separate {
                query: linear_ctor(hidden_width, hidden_width, vb.push_prefix("query"))
                    .context(SelfAttentionConstructionSnafu)?,
                key: linear_ctor(hidden_width, key_value_width, vb.push_prefix("key"))
                    .context(SelfAttentionConstructionSnafu)?,
                value: linear_ctor(hidden_width, key_value_width, vb.push_prefix("value"))
                    .context(SelfAttentionConstructionSnafu)?,
            },
        };

        Ok(Box::new(SelfAttention {
            attention_scorer: self
                .attention_scorer
                .build(vb.clone())
                .context(BuildAttentionScorerSnafu)?,
            attention_heads: self.attention_heads.clone(),
            dropout: self
                .dropout
                .build(vb.push_prefix("dropout"))
                .context(BuildDropoutSnafu)?,
            layer_norm: self
                .layer_norm
                .build(vb.push_prefix("layer_norm"))
                .context(BuildLayerNormSnafu)?,
            output,
            qkv,
            rotary_embeds: self
                .rotary_embeddings
                .as_ref()
                .map(|config| config.build(vb))
                .transpose()
                .context(BuildQueryKeyRotaryEmbeddingsSnafu)?,
        }))
    }
}

/// Errors for self-attention.
#[derive(Debug, Snafu)]
pub enum SelfAttentionError {
    #[snafu(display("Cannot create or apply attention mask"))]
    AttentionMask { source: AttentionMaskError },

    #[snafu(display("Cannot apply attention scorer"))]
    AttentionScorer { source: BoxedError },

    #[snafu(display("Cannot create causal mask"))]
    CausalMask { source: CausalMaskError },

    #[snafu(display("Cannot build attention scorer"))]
    BuildAttentionScorer { source: BoxedError },

    #[snafu(display("Cannot build dropout"))]
    BuildDropout { source: BoxedError },

    #[snafu(display("Cannot build layer norm"))]
    BuildLayerNorm { source: BoxedError },

    #[snafu(display("Cannot build query-key rotary embeddings"))]
    BuildQueryKeyRotaryEmbeddings {
        source: QueryKeyRotaryEmbeddingsError,
    },

    #[snafu(display("Cannot combine heads"))]
    CombineHeads { source: candle_core::Error },

    #[snafu(display(
        "The hidden size ({hidden_width}) must be divisble by the number of query heads ({n_query_heads})"
    ))]
    InvalidNQueryHeads {
        hidden_width: usize,
        n_query_heads: usize,
    },

    #[snafu(display(
        "The number of query ({n_query_heads}) and key-value ({n_key_value_heads}) heads \
         must be equal when using merged QKV matrix and splitting before projection"
    ))]
    InvalidMergedSplitBeforeNHeads {
        n_key_value_heads: usize,
        n_query_heads: usize,
    },

    #[snafu(display("Cannot concatenate append key and value to cache"))]
    ConcatKVCache { source: candle_core::Error },

    #[snafu(display("Cannot apply layer norm"))]
    LayerNorm { source: candle_core::Error },

    #[snafu(display("Cannot apply output layer"))]
    Output { source: candle_core::Error },

    #[snafu(display("Cannot calculate query, key, or value"))]
    Qkv { source: candle_core::Error },

    #[snafu(display("Cannot chunk query, key, and value representations"))]
    QkvChunk { source: candle_core::Error },

    #[snafu(display("Cannot apply rotary embeddings"))]
    RotaryEmbeddings {
        source: QueryKeyRotaryEmbeddingsError,
    },

    #[snafu(display("Cannot apply the scaled dot product attention"))]
    ScaledDotProductAttention {
        source: ScaledDotProductAttentionError,
    },

    #[snafu(display("Cannot construct layer"))]
    SelfAttentionConstruction { source: candle_core::Error },

    #[snafu(display("Cannot update self-attention mask"))]
    SelfAttentionMask { source: SelfAttentionMaskError },

    #[snafu(display("Cannot split heads"))]
    SplitHeads { source: candle_core::Error },
}

/// Transformer self-attention layer.
///
/// See [Vaswani et al., 2017](https://arxiv.org/abs/1706.03762).
pub struct SelfAttention {
    attention_scorer: Box<dyn AttentionScorer>,
    attention_heads: AttentionHeads,
    dropout: Box<dyn ModuleT>,
    layer_norm: Box<dyn ModuleT>,
    output: Linear,
    qkv: QkvTensors,
    rotary_embeds: Option<QueryKeyRotaryEmbeddings>,
}

impl Attention for SelfAttention {
    fn forward_t(
        &self,
        input: &Tensor,
        attention_mask: &AttentionMask,
        cache: &mut LayerKeyValueCache,
        positions: Option<&Tensor>,
        train: bool,
        use_causal_mask: bool,
    ) -> Result<Tensor, BoxedError> {
        let input = self
            .layer_norm
            .forward_t(input, train)
            .context(LayerNormSnafu)?;

        let (mut query, mut key, value) = match &self.qkv {
            QkvTensors::Separate { query, key, value } => {
                self.query_key_value_separate(value, query, key, &input)?
            }
            QkvTensors::MergedSplitAfter { .. } => todo!(),
            QkvTensors::MergedSplitBefore(qkv) => self.query_key_value_split_before(qkv, &input)?,
        };

        if let Some(rotary_embeds) = &self.rotary_embeds {
            let (query_rot, key_rot) = rotary_embeds
                .forward(&query, &key, cache, positions)
                .context(RotaryEmbeddingsSnafu)?;
            query = query_rot;
            key = key_rot;
        }

        cache.update(&key, &value)?;
        let key = cache.key().unwrap_or(&key);
        let value = cache.value().unwrap_or(&value);

        // TODO: rotary embeddings positions

        // TODO: causal mask

        // TODO: ALiBi

        let mut combined_mask: SelfAttentionMask = attention_mask.into();
        if use_causal_mask {
            let causal_mask =
                SelfAttentionMask::causal_mask(&query, key).context(CausalMaskSnafu)?;
            combined_mask = combined_mask
                .intersect(&causal_mask)
                .context(SelfAttentionMaskSnafu)?;
        }

        let attn = self
            .attention_scorer
            .forward(&query, key, value, &combined_mask, train)
            .context(AttentionScorerSnafu)?
            .combine_heads()?;

        let output = self
            .output
            .forward(&attn)
            .and_then(|xs| self.dropout.forward_t(&xs, train))
            .context(OutputSnafu)?;

        Ok(output)
    }
}

impl SelfAttention {
    /// Compute query, key, and value with separate projections.
    fn query_key_value_separate(
        &self,
        value: &Linear,
        query: &Linear,
        key: &Linear,
        input: &Tensor,
    ) -> Result<(Tensor, Tensor, Tensor), BoxedError> {
        let query = query
            .forward(input)
            .context(QkvSnafu)?
            .split_heads(self.attention_heads.n_query_heads)?;
        let key = key
            .forward(input)
            .context(QkvSnafu)?
            .split_heads(self.attention_heads.n_key_value_heads)?;
        let value = value
            .forward(input)
            .context(QkvSnafu)?
            .split_heads(self.attention_heads.n_key_value_heads)?;
        Ok((query, key, value))
    }

    /// Compute query, key, and value with a single projection.
    ///
    /// Split heads before query, key, and value.
    fn query_key_value_split_before(
        &self,
        qkv: &Linear,
        input: &Tensor,
    ) -> Result<(Tensor, Tensor, Tensor), BoxedError> {
        let proj = qkv
            .forward(input)
            .context(QkvSnafu)?
            .split_heads(self.attention_heads.n_query_heads)?;

        let (_, _, _, all_heads_size) = proj.shape().dims4().context(QkvSnafu)?;
        let head_size = all_heads_size / 3;

        // Similar to chunk, but avoid intermediate Vec.
        let query = proj.narrow(3, 0, head_size).context(QkvChunkSnafu)?;
        let key = proj
            .narrow(3, head_size, head_size)
            .context(QkvChunkSnafu)?;
        let value = proj
            .narrow(3, 2 * head_size, head_size)
            .context(QkvChunkSnafu)?;

        Ok((query, key, value))
    }
}

trait CombineHeads {
    fn combine_heads(&self) -> Result<Tensor, SelfAttentionError>;
}

impl CombineHeads for Tensor {
    fn combine_heads(&self) -> Result<Tensor, SelfAttentionError> {
        let (batch_size, n_heads, seq_len, model_width) =
            self.dims4().context(CombineHeadsSnafu)?;
        self.transpose(1, 2)
            .and_then(|heads| heads.reshape((batch_size, seq_len, n_heads * model_width)))
            .context(CombineHeadsSnafu)
    }
}

trait SplitHeads {
    fn split_heads(&self, n_heads: usize) -> Result<Tensor, SelfAttentionError>;
}

impl SplitHeads for Tensor {
    fn split_heads(&self, n_heads: usize) -> Result<Tensor, SelfAttentionError> {
        let (batch_size, seq_len, model_width) = self.dims3().context(SplitHeadsSnafu)?;
        let head_width = model_width / n_heads;
        self.reshape((batch_size, seq_len, n_heads, head_width))
            .and_then(|heads| heads.transpose(1, 2))
            .context(SplitHeadsSnafu)
    }
}

#[derive(Debug, Snafu)]
pub enum SelfAttentionMaskError {
    #[snafu(display("Cannot apply logits mask"))]
    ApplyLogitsMask { source: candle_core::Error },

    #[snafu(display("Cannot intersect masks"))]
    IntersectMasks { source: candle_core::Error },
}

/// Mask for self-attention.
///
/// This type of mask is used in self-attention to mask out tokens that
/// should not be attended to by setting their elements to `False`.
///
/// In contrast to `AttentionMask`, this mask is shaped for use in
/// self-attention. `AttentionMask` has the shape `(batch_size, seq_len)`,
/// where `seq_len` it typically the length of the key. `SelfAttentionMask`
/// has the shape `(batch_size, heads, query_len, key_len)`, to account for
/// use cases where the query and key lengths are different. This occurs e.g.
/// when decoding a sequence with a cache.
///
/// The 4-dimensional mask also supports applying a causal mask, so that a
/// token cannot attend to any succeeding tokens.
#[derive(Clone, Debug)]
pub struct SelfAttentionMask {
    bool_mask: Tensor,
}

impl From<AttentionMask> for SelfAttentionMask {
    fn from(attention_mask: AttentionMask) -> Self {
        SelfAttentionMask::from(&attention_mask)
    }
}

impl From<&AttentionMask> for SelfAttentionMask {
    fn from(attention_mask: &AttentionMask) -> Self {
        let (batch_len, key_len) = attention_mask
            .bool_mask
            .shape()
            .dims2()
            .expect("input mask must have two dimensions");
        SelfAttentionMask {
            bool_mask: attention_mask
                .bool_mask
                .reshape((batch_len, 1, 1, key_len))
                .expect("Cannot reshape input mask"),
        }
    }
}

impl SelfAttentionMask {
    /// Use the self-attention mask to mask logits.
    ///
    /// * input - Tensor to which the mask is applied.
    ///   *Shape:* `(batch_size, heads, query_len, key_len)`
    ///
    /// Returns: Logits with the attention mask applied.
    /// *Shape:* `(batch_size, heads, query_len, key_len)`
    pub fn apply_logit_mask(&self, input: &Tensor) -> Result<Tensor, SelfAttentionMaskError> {
        // Underflows to -inf for more narrow floating point types, which
        // is ok for masking.
        let blocked_value = input.min_like().context(ApplyLogitsMaskSnafu)?;
        self.bool_mask
            .broadcast_as(input.shape())
            .and_then(|xs| xs.where_cond(input, &blocked_value))
            .context(ApplyLogitsMaskSnafu)
    }

    /// Merge this attention mask with another self-attention mask.
    pub fn intersect(
        &self,
        other: &SelfAttentionMask,
    ) -> Result<SelfAttentionMask, SelfAttentionMaskError> {
        Ok(SelfAttentionMask {
            bool_mask: self
                .bool_mask
                .broadcast_mul(&other.bool_mask)
                .context(IntersectMasksSnafu)?,
        })
    }
}

#[derive(Debug, Snafu)]
pub enum CausalMaskError {
    #[snafu(display("Cannot create causal mask"))]
    CreateMask { source: candle_core::Error },

    #[snafu(display("Key has invalid number of dimensions"))]
    KeyDim { source: candle_core::Error },

    #[snafu(display("Query has invalid number of dimensions"))]
    QueryDim { source: candle_core::Error },

    #[snafu(display("Query length {query_len} must not be larger than key length {key_len}"))]
    QueryLen { key_len: usize, query_len: usize },

    #[snafu(display("Cannot slice causal mask to key/query size"))]
    SliceMask { source: candle_core::Error },
}

/// Trait for creating causal masks.
pub trait CausalMask: Sized {
    type Error;

    /// Create a causal mask for the given query and key.
    ///
    /// A causal mask ensures that tokens cannot attend to succeeding tokens.
    ///
    /// * `query` - Query tensor.
    ///   *Shape:* `(batch_size, heads, query_len, width)`
    /// * `key` - Key tensor.
    ///   *Shape:* `(batch_size, heads, key_len, width)`
    fn causal_mask(query: &Tensor, key: &Tensor) -> Result<Self, Self::Error>;
}

impl CausalMask for SelfAttentionMask {
    type Error = CausalMaskError;

    fn causal_mask(query: &Tensor, key: &Tensor) -> Result<Self, Self::Error> {
        let (_, _, query_len, _) = query.shape().dims4().context(QueryDimSnafu)?;
        let (_, _, key_len, _) = key.shape().dims4().context(KeyDimSnafu)?;

        // Slicing will fail down the line if the query length is greater than
        // the key length.
        ensure!(query_len <= key_len, QueryLenSnafu { key_len, query_len });

        let causal_mask = Tensor::tril2(key_len, DType::U8, key.device())
            .and_then(|mask| mask.reshape((1, 1, key_len, key_len)))
            .context(CreateMaskSnafu)?;
        Ok(Self {
            bool_mask: causal_mask
                .i((.., .., key_len - query_len..key_len, ..key_len))
                .context(SliceMaskSnafu)?,
        })
    }
}