smelte-rs 0.1.0

Efficient inference ML framework written in rust
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
use crate::cpu::f32::{matmul, matmul_t, softmax, Tensor as F32Tensor};
use crate::nn::layers::{Embedding, LayerNorm, Linear};
use crate::traits::{Tensor, TensorOps};
use crate::SmeltError;

macro_rules! debug {
    // `()` indicates that the macro takes no argument.
    ($str: expr, $tensor: expr) => {
        // The macro will expand into the contents of this block.
        // println!(
        //     "{} {:?}..{:?}",
        //     $str,
        //     &$tensor.data()[..3],
        //     &$tensor.data()[$tensor.data().len() - 3..]
        // );
        // let n = $tensor.data().len();
        // println!(
        //     "{} {:?}..{:?}",
        //     $str,
        //     &$tensor.data()[..3],
        //     &$tensor.data()[n - 768 * 3..n - 768 * 3 + 3]
        // );
    };
}

/// TODO
pub struct BertContext<T: Tensor> {
    input_ids: Vec<usize>,
    type_ids: Vec<usize>,
    position_ids: Vec<usize>,
    hidden_states: T,
    // Required to compute position_ids before adding into hidden_states
    // - Used in the MLP to prevent cloning the skip connection
    // - Used in the attention for the output Linear layer
    hidden_states_copy: T,
    // Store the hidden_states after the attention (prevents a clone in the skip connection)
    hidden_states_attn_output: T,
    q_cache: T,
    // Store the k splitted_heads
    k_cache: T,
    // Store the k splitted_heads
    v_cache: T,
    // Store the qk result
    qk: T,
    qkv: T,
    // Intermediate states (H, 4H)
    intermediate_states: T,
    pool: T,
    pool_output: T,
    probs: T,
}

impl<T: Tensor> BertContext<T> {
    /// TODO
    pub fn probs(&self) -> &T {
        &self.probs
    }
}

fn split_heads(q: &F32Tensor, out_q: &mut F32Tensor) -> Result<(), SmeltError> {
    let num_heads = out_q.shape()[0];
    let sequence_length = out_q.shape()[1];
    let head_dim = out_q.shape()[2];
    let hidden_dim = head_dim * num_heads;

    (0..num_heads).for_each(|i| {
        (0..sequence_length).for_each(|j| {
            (0..head_dim).for_each(|k| {
                let index = j * hidden_dim + i * head_dim + k;
                let out_index = i * sequence_length * head_dim + j * head_dim + k;
                out_q.data_mut()[out_index] = q.data()[index];
            });
        });
    });
    Ok(())
}

fn attention<'data, 'ctx>(
    q_weights: &Linear<F32Tensor<'data>>,
    k_weights: &Linear<F32Tensor<'data>>,
    v_weights: &Linear<F32Tensor<'data>>,
    ctx: &mut BertContext<F32Tensor<'ctx>>,
) -> Result<(), SmeltError>
where
    'data: 'ctx,
{
    q_weights.forward(&ctx.hidden_states, &mut ctx.hidden_states_copy)?;
    split_heads(&ctx.hidden_states_copy, &mut ctx.q_cache)?;

    debug!("Q head splitted", ctx.q_cache);

    k_weights.forward(&ctx.hidden_states, &mut ctx.hidden_states_copy)?;
    split_heads(&ctx.hidden_states_copy, &mut ctx.k_cache)?;

    debug!("K head splitted", ctx.k_cache);

    v_weights.forward(&ctx.hidden_states, &mut ctx.hidden_states_copy)?;
    split_heads(&ctx.hidden_states_copy, &mut ctx.v_cache)?;

    debug!("V head splitted", ctx.v_cache);

    matmul_t(&ctx.q_cache, &ctx.k_cache, &mut ctx.qk).unwrap();

    let num_heads = ctx.q_cache.shape()[0];
    let sequence_length = ctx.q_cache.shape()[1];
    let head_dim = ctx.q_cache.shape()[2];
    let hidden_dim = head_dim * num_heads;
    let scale = (head_dim as f32).sqrt();
    ctx.qk.data_mut().iter_mut().for_each(|v| *v /= scale);

    softmax(&mut ctx.qk).unwrap();
    debug!("attention_probs", ctx.qk);
    matmul(&ctx.qk, &ctx.v_cache, &mut ctx.qkv).unwrap();
    debug!("qkv", ctx.qkv);

    let new_out = &mut ctx.hidden_states_attn_output.data_mut();
    (0..num_heads).for_each(|i| {
        (0..sequence_length).for_each(|j| {
            (0..head_dim).for_each(|k| {
                let in_index = i * sequence_length * head_dim + j * head_dim + k;
                let out_index = j * hidden_dim + i * head_dim + k;
                new_out[out_index] = (ctx.qkv).data()[in_index];
            });
        });
    });
    debug!("qkv (reshaed)", ctx.hidden_states_attn_output);

    Ok(())
}

/// TODO
pub trait TensorAttention<T: Tensor> {
    /// TODO
    fn attention(
        query: &Linear<T>,
        key: &Linear<T>,
        value: &Linear<T>,
        ctx: &mut BertContext<T>,
    ) -> Result<(), SmeltError>;
}

impl<'a> TensorAttention<F32Tensor<'a>> for F32Tensor<'a> {
    fn attention(
        query: &Linear<F32Tensor<'a>>,
        key: &Linear<F32Tensor<'a>>,
        value: &Linear<F32Tensor<'a>>,
        ctx: &mut BertContext<F32Tensor<'a>>,
    ) -> Result<(), SmeltError> {
        attention(query, key, value, ctx)?;
        Ok(())
    }
}

/// TODO
pub trait Debug<T: Tensor> {
    /// TODO
    fn data(&self) -> &[f32];
}

impl<'a> Debug<F32Tensor<'a>> for F32Tensor<'a> {
    fn data(&self) -> &[f32] {
        self.data()
    }
}

/// TODO
pub trait BertOps<T: Tensor>: TensorOps<T> + TensorAttention<T> + Debug<T> {}

impl<'a> BertOps<F32Tensor<'a>> for F32Tensor<'a> {}

/// TODO
#[derive(Clone)]
pub struct BertAttention<T: Tensor> {
    query: Linear<T>,
    key: Linear<T>,
    value: Linear<T>,
    output: Linear<T>,
    output_ln: LayerNorm<T>,
}

impl<T: Tensor + BertOps<T>> BertAttention<T> {
    /// TODO
    pub fn new(
        query: Linear<T>,
        key: Linear<T>,
        value: Linear<T>,
        output: Linear<T>,
        output_ln: LayerNorm<T>,
    ) -> Self {
        Self {
            query,
            key,
            value,
            output,
            output_ln,
        }
    }

    /// TODO
    pub fn forward(&self, ctx: &mut BertContext<T>) -> Result<(), SmeltError> {
        T::attention(&self.query, &self.key, &self.value, ctx)?;

        self.output
            .forward(&ctx.hidden_states_attn_output, &mut ctx.hidden_states_copy)?;
        T::add(&ctx.hidden_states_copy, &mut ctx.hidden_states)?;
        self.output_ln.forward(&mut ctx.hidden_states)?;
        Ok(())
    }
}

/// TODO
#[derive(Clone)]
pub struct Mlp<T: Tensor> {
    intermediate: Linear<T>,
    output: Linear<T>,
    output_ln: LayerNorm<T>,
}

impl<T: Tensor + BertOps<T>> Mlp<T> {
    /// TODO
    pub fn new(intermediate: Linear<T>, output: Linear<T>, output_ln: LayerNorm<T>) -> Self {
        Self {
            intermediate,
            output,
            output_ln,
        }
    }

    /// TODO
    pub fn forward(&self, ctx: &mut BertContext<T>) -> Result<(), SmeltError> {
        // println!("=====");
        debug!("Before MLP", ctx.hidden_states);
        self.intermediate
            .forward(&ctx.hidden_states, &mut ctx.intermediate_states)?;
        debug!("Intermediate ", ctx.intermediate_states);
        T::gelu(&mut ctx.intermediate_states)?;
        debug!("Intermediate (gelu)", ctx.intermediate_states);
        self.output
            .forward(&ctx.intermediate_states, &mut ctx.hidden_states_copy)?;
        debug!("output", ctx.hidden_states_copy);
        T::add(&ctx.hidden_states_copy, &mut ctx.hidden_states)?;
        debug!("output (skip)", ctx.hidden_states);
        self.output_ln.forward(&mut ctx.hidden_states)?;
        debug!("output ln", ctx.hidden_states);
        Ok(())
    }
}

/// TODO
#[derive(Clone)]
pub struct BertLayer<T: Tensor> {
    attention: BertAttention<T>,
    mlp: Mlp<T>,
}

impl<T: Tensor + BertOps<T>> BertLayer<T> {
    /// TODO
    pub fn new(attention: BertAttention<T>, mlp: Mlp<T>) -> Self {
        Self { attention, mlp }
    }

    /// TODO
    pub fn forward(&self, ctx: &mut BertContext<T>) -> Result<(), SmeltError> {
        debug!("Before attention", ctx.hidden_states);
        self.attention.forward(ctx)?;
        debug!("After attention", ctx.hidden_states);
        self.mlp.forward(ctx)?;
        debug!("After mlp", ctx.hidden_states);
        // println!("---------");
        Ok(())
    }
}

/// TODO
#[derive(Clone)]
pub struct BertEncoder<T: Tensor> {
    layers: Vec<BertLayer<T>>,
}

impl<T: Tensor + BertOps<T>> BertEncoder<T> {
    /// TODO
    pub fn new(layers: Vec<BertLayer<T>>) -> Self {
        Self { layers }
    }

    /// TODO
    pub fn forward(&self, ctx: &mut BertContext<T>) -> Result<(), SmeltError> {
        for layer in &self.layers {
            layer.forward(ctx)?;
        }
        Ok(())
    }
}

/// TODO
#[derive(Clone)]
pub struct BertEmbeddings<T: Tensor> {
    input_embeddings: Embedding<T>,
    position_embeddings: Embedding<T>,
    type_embeddings: Embedding<T>,
    layer_norm: LayerNorm<T>,
}

impl<T: Tensor + BertOps<T>> BertEmbeddings<T> {
    /// TODO
    pub fn new(
        input_embeddings: Embedding<T>,
        position_embeddings: Embedding<T>,
        type_embeddings: Embedding<T>,
        layer_norm: LayerNorm<T>,
    ) -> Self {
        Self {
            input_embeddings,
            position_embeddings,
            type_embeddings,
            layer_norm,
        }
    }

    /// TODO
    pub fn forward(&self, ctx: &mut BertContext<T>) -> Result<(), SmeltError> {
        let input_ids = &ctx.input_ids;
        let position_ids = &ctx.position_ids;
        let type_ids = &ctx.type_ids;

        if input_ids.len() != position_ids.len() {
            return Err(SmeltError::InvalidLength {
                expected: input_ids.len(),
                got: position_ids.len(),
            });
        }
        if input_ids.len() != type_ids.len() {
            return Err(SmeltError::InvalidLength {
                expected: input_ids.len(),
                got: type_ids.len(),
            });
        }

        self.input_embeddings
            .forward(input_ids, &mut ctx.hidden_states)?;

        debug!("input embeddings", ctx.hidden_states);

        self.type_embeddings
            .forward(type_ids, &mut ctx.hidden_states_copy)?;
        debug!("type embeddings", ctx.hidden_states_copy);
        T::add(&ctx.hidden_states_copy, &mut ctx.hidden_states)?;
        debug!("After add type embeddings", ctx.hidden_states);

        self.position_embeddings
            .forward(position_ids, &mut ctx.hidden_states_copy)?;
        debug!("position embeddings", ctx.hidden_states_copy);
        T::add(&ctx.hidden_states_copy, &mut ctx.hidden_states)?;
        debug!("After add position embeddings", ctx.hidden_states);

        self.layer_norm.forward(&mut ctx.hidden_states)?;

        debug!("After embeddings", ctx.hidden_states);
        Ok(())
    }
}

/// TODO
pub struct Bert<T: Tensor + BertOps<T>> {
    embeddings: BertEmbeddings<T>,
    encoder: BertEncoder<T>,
}

impl<T: Tensor + BertOps<T>> Bert<T> {
    /// TODO
    pub fn new(embeddings: BertEmbeddings<T>, encoder: BertEncoder<T>) -> Self {
        Self {
            embeddings,
            encoder,
        }
    }
    /// TODO
    pub fn forward(&self, ctx: &mut BertContext<T>) -> Result<(), SmeltError> {
        self.embeddings.forward(ctx)?;
        self.encoder.forward(ctx)
    }
}

/// TODO
#[derive(Clone)]
pub struct BertPooler<T: Tensor> {
    pooler: Linear<T>,
}

impl<T: Tensor + BertOps<T>> BertPooler<T> {
    /// TODO
    pub fn new(pooler: Linear<T>) -> Self {
        Self { pooler }
    }

    /// TODO
    pub fn forward(&self, ctx: &mut BertContext<T>) -> Result<(), SmeltError> {
        T::select(&[0], &ctx.hidden_states, &mut ctx.pool)?;
        self.pooler.forward(&ctx.pool, &mut ctx.pool_output)?;
        T::tanh(&mut ctx.pool_output)?;
        Ok(())
    }
}

/// TODO
pub struct BertClassifier<T: Tensor + BertOps<T>> {
    bert: Bert<T>,
    pooler: BertPooler<T>,
    classifier: Linear<T>,
}

impl<T: Tensor + BertOps<T> + TensorAttention<T>> BertClassifier<T> {
    /// TODO
    pub fn new(bert: Bert<T>, pooler: BertPooler<T>, classifier: Linear<T>) -> Self {
        Self {
            bert,
            pooler,
            classifier,
        }
    }

    /// TODO
    pub fn forward(&self, ctx: &mut BertContext<T>) -> Result<(), SmeltError> {
        self.bert.forward(ctx)?;
        self.pooler.forward(ctx)?;
        self.classifier.forward(&ctx.pool_output, &mut ctx.probs)?;
        T::softmax(&mut ctx.probs)?;
        Ok(())
    }

    /// TODO
    pub fn new_context(
        &self,
        input_ids: Vec<usize>,
        position_ids: Vec<usize>,
        type_ids: Vec<usize>,
        num_heads: usize,
    ) -> BertContext<T> {
        let hidden_dim = self.bert.embeddings.input_embeddings.weight().shape()[1];
        let intermediate_dim = self.bert.encoder.layers[0]
            .mlp
            .intermediate
            .weight()
            .shape()[0];
        let num_classes = self.classifier.weight().shape()[0];
        let head_dim = hidden_dim / num_heads;
        let sequence_length = input_ids.len();

        let hidden_states = T::zeros(vec![sequence_length, hidden_dim]);
        let hidden_states_copy = T::zeros(vec![sequence_length, hidden_dim]);
        let hidden_states_attn_output = T::zeros(vec![sequence_length, hidden_dim]);
        let intermediate_states = T::zeros(vec![sequence_length, intermediate_dim]);
        let q_cache = T::zeros(vec![num_heads, sequence_length, head_dim]);
        let k_cache = T::zeros(vec![num_heads, sequence_length, head_dim]);
        let v_cache = T::zeros(vec![num_heads, sequence_length, head_dim]);
        let qk = T::zeros(vec![num_heads, sequence_length, sequence_length]);
        let qkv = T::zeros(vec![num_heads, sequence_length, head_dim]);
        let pool = T::zeros(vec![1, hidden_dim]);
        let pool_output = T::zeros(vec![1, hidden_dim]);
        let probs = T::zeros(vec![1, num_classes]);
        BertContext {
            input_ids,
            position_ids,
            type_ids,
            hidden_states,
            hidden_states_copy,
            hidden_states_attn_output,
            intermediate_states,
            q_cache,
            k_cache,
            v_cache,
            qk,
            qkv,
            pool,
            pool_output,
            probs,
        }
    }
}