boostr 0.1.0

ML framework built on numr - attention, quantization, model architectures
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
//! BERT-style transformer encoder for embedding generation.
//!
//! Architecture: token_embed + position_embed → N × (SelfAttention + FFN + LayerNorm) → mean pool
//!
//! Used by sentence embedding models (all-MiniLM, BGE, nomic-embed, etc.)
//! for producing fixed-size vector representations from token sequences.

use super::config::{EncoderConfig, HiddenAct};
use crate::error::{Error, Result};
use crate::nn::{Embedding, LayerNorm, Linear};
use numr::autograd::{
    Var, var_add, var_matmul, var_narrow, var_permute, var_reshape, var_transpose,
};
use numr::dtype::DType;
use numr::ops::{
    ActivationOps, BinaryOps, IndexingOps, NormalizationOps, ReduceOps, ScalarOps, ShapeOps,
    TensorOps, UnaryOps,
};
use numr::runtime::{Runtime, RuntimeClient};
use numr::tensor::Tensor;

/// A single transformer encoder layer: self-attention + FFN with residual + LayerNorm.
struct EncoderLayer<R: Runtime> {
    q_proj: Linear<R>,
    k_proj: Linear<R>,
    v_proj: Linear<R>,
    o_proj: Linear<R>,
    attn_norm: LayerNorm<R>,
    ffn_up: Linear<R>,
    ffn_down: Linear<R>,
    ffn_norm: LayerNorm<R>,
    num_heads: usize,
    head_dim: usize,
    hidden_act: HiddenAct,
}

impl<R: Runtime<DType = DType>> EncoderLayer<R> {
    fn forward<C>(&self, client: &C, x: &Var<R>) -> Result<Var<R>>
    where
        C: RuntimeClient<R>
            + TensorOps<R>
            + ScalarOps<R>
            + BinaryOps<R>
            + ReduceOps<R>
            + ShapeOps<R>
            + IndexingOps<R>
            + ActivationOps<R>
            + UnaryOps<R>
            + NormalizationOps<R>,
        R::Client: TensorOps<R> + ScalarOps<R>,
    {
        // Self-attention with residual
        let attn_out = self.self_attention(client, x)?;
        let x = var_add(x, &attn_out, client).map_err(Error::Numr)?;
        let x = self.attn_norm.forward(client, &x)?;

        // FFN with residual
        let ffn_out = self.ffn(client, &x)?;
        let x = var_add(&x, &ffn_out, client).map_err(Error::Numr)?;
        let x = self.ffn_norm.forward(client, &x)?;

        Ok(x)
    }

    fn self_attention<C>(&self, client: &C, x: &Var<R>) -> Result<Var<R>>
    where
        C: RuntimeClient<R>
            + TensorOps<R>
            + ScalarOps<R>
            + BinaryOps<R>
            + ReduceOps<R>
            + ShapeOps<R>
            + ActivationOps<R>
            + UnaryOps<R>,
        R::Client: TensorOps<R> + ScalarOps<R>,
    {
        let shape = x.shape().to_vec();
        let batch = shape[0];
        let seq_len = shape[1];

        let q = self.q_proj.forward(client, x)?;
        let k = self.k_proj.forward(client, x)?;
        let v = self.v_proj.forward(client, x)?;

        // Reshape to [B, S, H, D] then transpose to [B, H, S, D]
        let q = var_reshape(&q, &[batch, seq_len, self.num_heads, self.head_dim])
            .map_err(Error::Numr)?;
        let k = var_reshape(&k, &[batch, seq_len, self.num_heads, self.head_dim])
            .map_err(Error::Numr)?;
        let v = var_reshape(&v, &[batch, seq_len, self.num_heads, self.head_dim])
            .map_err(Error::Numr)?;

        let q = var_permute(&q, &[0, 2, 1, 3]).map_err(Error::Numr)?;
        let k = var_permute(&k, &[0, 2, 1, 3]).map_err(Error::Numr)?;
        let v = var_permute(&v, &[0, 2, 1, 3]).map_err(Error::Numr)?;

        // Make contiguous after permute (required for matmul)
        let q = Var::new(q.tensor().contiguous(), false);
        let k = Var::new(k.tensor().contiguous(), false);
        let v = Var::new(v.tensor().contiguous(), false);

        // Scaled dot-product attention: softmax(Q @ K^T / sqrt(d)) @ V
        let k_t = var_transpose(&k).map_err(Error::Numr)?;
        let scores = var_matmul(&q, &k_t, client).map_err(Error::Numr)?;
        let scale = 1.0 / (self.head_dim as f32).sqrt();
        let scores = Var::new(
            client
                .mul_scalar(scores.tensor(), scale as f64)
                .map_err(Error::Numr)?,
            false,
        );
        let attn_weights = Var::new(
            client.softmax(scores.tensor(), -1).map_err(Error::Numr)?,
            false,
        );
        let attn_out = var_matmul(&attn_weights, &v, client).map_err(Error::Numr)?;

        // Transpose back [B, H, S, D] → [B, S, H, D] → [B, S, hidden]
        let attn_out = var_permute(&attn_out, &[0, 2, 1, 3]).map_err(Error::Numr)?;
        let attn_out = Var::new(attn_out.tensor().contiguous(), false);
        let hidden = self.num_heads * self.head_dim;
        let attn_out = var_reshape(&attn_out, &[batch, seq_len, hidden]).map_err(Error::Numr)?;

        let o = self.o_proj.forward(client, &attn_out)?;
        Ok(o)
    }

    fn ffn<C>(&self, client: &C, x: &Var<R>) -> Result<Var<R>>
    where
        C: RuntimeClient<R> + TensorOps<R> + ActivationOps<R>,
        R::Client: TensorOps<R>,
    {
        let h = self.ffn_up.forward(client, x)?;
        let h = match self.hidden_act {
            HiddenAct::Gelu => Var::new(client.gelu(h.tensor()).map_err(Error::Numr)?, false),
            HiddenAct::Relu => Var::new(client.relu(h.tensor()).map_err(Error::Numr)?, false),
        };
        let out = self.ffn_down.forward(client, &h)?;
        Ok(out)
    }
}

/// Pooling strategy for producing a single vector from encoder outputs.
#[derive(Debug, Clone, Copy)]
pub enum Pooling {
    /// Average all token hidden states (most common for sentence embeddings).
    Mean,
    /// Use the [CLS] token's hidden state (position 0).
    Cls,
}

/// BERT-style transformer encoder for producing embeddings.
///
/// Takes token IDs and returns either:
/// - `encode()`: per-token hidden states `[B, S, hidden_size]`
/// - `embed()`: pooled embeddings `[B, hidden_size]`
pub struct Encoder<R: Runtime> {
    config: EncoderConfig,
    token_embed: Embedding<R>,
    position_embed: Embedding<R>,
    layers: Vec<EncoderLayer<R>>,
    pooling: Pooling,
}

/// Client trait bounds needed by encoder forward passes.
pub trait EncoderClient<R: Runtime>:
    RuntimeClient<R>
    + TensorOps<R>
    + ScalarOps<R>
    + BinaryOps<R>
    + ReduceOps<R>
    + ShapeOps<R>
    + IndexingOps<R>
    + ActivationOps<R>
    + UnaryOps<R>
    + NormalizationOps<R>
{
}

impl<R, C> EncoderClient<R> for C
where
    R: Runtime,
    C: RuntimeClient<R>
        + TensorOps<R>
        + ScalarOps<R>
        + BinaryOps<R>
        + ReduceOps<R>
        + ShapeOps<R>
        + IndexingOps<R>
        + ActivationOps<R>
        + UnaryOps<R>
        + NormalizationOps<R>,
{
}

impl<R: Runtime<DType = DType>> Encoder<R> {
    /// Create an encoder from pre-loaded weight tensors.
    ///
    /// `weights` is a closure that fetches tensors by name (e.g., from a GGUF reader).
    pub fn from_weights<F>(config: EncoderConfig, pooling: Pooling, mut get: F) -> Result<Self>
    where
        F: FnMut(&str) -> Result<Tensor<R>>,
    {
        let token_embed = Embedding::new(get("embeddings.word_embeddings.weight")?, false);
        let position_embed = Embedding::new(get("embeddings.position_embeddings.weight")?, false);

        let mut layers = Vec::with_capacity(config.num_hidden_layers);
        for i in 0..config.num_hidden_layers {
            let p = format!("encoder.layer.{i}");

            let q_proj = Linear::new(
                get(&format!("{p}.attention.self.query.weight"))?,
                Some(get(&format!("{p}.attention.self.query.bias"))?),
                false,
            );
            let k_proj = Linear::new(
                get(&format!("{p}.attention.self.key.weight"))?,
                Some(get(&format!("{p}.attention.self.key.bias"))?),
                false,
            );
            let v_proj = Linear::new(
                get(&format!("{p}.attention.self.value.weight"))?,
                Some(get(&format!("{p}.attention.self.value.bias"))?),
                false,
            );
            let o_proj = Linear::new(
                get(&format!("{p}.attention.output.dense.weight"))?,
                Some(get(&format!("{p}.attention.output.dense.bias"))?),
                false,
            );
            let attn_norm = LayerNorm::new(
                get(&format!("{p}.attention.output.LayerNorm.weight"))?,
                get(&format!("{p}.attention.output.LayerNorm.bias"))?,
                config.layer_norm_eps as f32,
                false,
            );
            let ffn_up = Linear::new(
                get(&format!("{p}.intermediate.dense.weight"))?,
                Some(get(&format!("{p}.intermediate.dense.bias"))?),
                false,
            );
            let ffn_down = Linear::new(
                get(&format!("{p}.output.dense.weight"))?,
                Some(get(&format!("{p}.output.dense.bias"))?),
                false,
            );
            let ffn_norm = LayerNorm::new(
                get(&format!("{p}.output.LayerNorm.weight"))?,
                get(&format!("{p}.output.LayerNorm.bias"))?,
                config.layer_norm_eps as f32,
                false,
            );

            layers.push(EncoderLayer {
                q_proj,
                k_proj,
                v_proj,
                o_proj,
                attn_norm,
                ffn_up,
                ffn_down,
                ffn_norm,
                num_heads: config.num_attention_heads,
                head_dim: config.head_dim(),
                hidden_act: config.hidden_act,
            });
        }

        Ok(Self {
            config,
            token_embed,
            position_embed,
            layers,
            pooling,
        })
    }

    /// Forward pass: token IDs → per-token hidden states `[B, S, hidden_size]`.
    pub fn encode<C>(&self, client: &C, input_ids: &Tensor<R>) -> Result<Var<R>>
    where
        C: EncoderClient<R>,
        R::Client: TensorOps<R> + ScalarOps<R> + IndexingOps<R>,
    {
        let shape = input_ids.shape().to_vec();
        let seq_len = *shape.last().ok_or_else(|| Error::ModelError {
            reason: "input_ids must have at least 1 dimension".into(),
        })?;

        // Token embeddings [B, S, hidden]
        let tok_emb = self.token_embed.forward(client, input_ids)?;

        // Position IDs: [0, 1, ..., S-1]
        let device = input_ids.device();
        let pos_ids: Vec<i64> = (0..seq_len as i64).collect();
        let pos_tensor = Tensor::<R>::from_slice(&pos_ids, &[seq_len], device);
        let pos_emb = self.position_embed.forward(client, &pos_tensor)?;

        // Combine: token + position embeddings
        let mut hidden = var_add(&tok_emb, &pos_emb, client).map_err(Error::Numr)?;

        // Transformer encoder layers
        for layer in &self.layers {
            hidden = layer.forward(client, &hidden)?;
        }

        Ok(hidden)
    }

    /// Forward pass: token IDs → pooled embedding `[B, hidden_size]`.
    pub fn embed<C>(&self, client: &C, input_ids: &Tensor<R>) -> Result<Var<R>>
    where
        C: EncoderClient<R>,
        R::Client: TensorOps<R> + ScalarOps<R> + IndexingOps<R>,
    {
        let hidden = self.encode(client, input_ids)?;

        match self.pooling {
            Pooling::Mean => {
                // Mean pool over sequence dimension (dim=1): [B, S, H] → [B, H]
                let pooled = Var::new(
                    client
                        .mean(hidden.tensor(), &[1], false)
                        .map_err(Error::Numr)?,
                    false,
                );
                Ok(pooled)
            }
            Pooling::Cls => {
                // Take first token (CLS): [B, S, H] → [B, 1, H] → [B, H]
                let cls = var_narrow(&hidden, 1, 0, 1).map_err(Error::Numr)?;
                let shape = cls.shape().to_vec();
                let batch = shape[0];
                let hidden_dim = shape[2];
                var_reshape(&cls, &[batch, hidden_dim]).map_err(Error::Numr)
            }
        }
    }

    /// Returns the encoder's configuration.
    pub fn config(&self) -> &EncoderConfig {
        &self.config
    }

    /// Returns the pooling strategy used by this encoder.
    pub fn pooling(&self) -> Pooling {
        self.pooling
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::cpu_setup;
    use numr::runtime::cpu::CpuRuntime;

    fn make_test_encoder() -> (
        Encoder<CpuRuntime>,
        numr::runtime::cpu::CpuClient,
        numr::runtime::cpu::CpuDevice,
    ) {
        let (client, device) = cpu_setup();

        let config = EncoderConfig {
            vocab_size: 10,
            hidden_size: 8,
            num_hidden_layers: 1,
            num_attention_heads: 2,
            intermediate_size: 16,
            max_position_embeddings: 32,
            layer_norm_eps: 1e-12,
            hidden_act: HiddenAct::Gelu,
            type_vocab_size: 0,
        };

        let encoder = Encoder::from_weights(config, Pooling::Mean, |name| {
            // Return appropriately shaped random-ish tensors
            match name {
                "embeddings.word_embeddings.weight" => {
                    Ok(Tensor::from_slice(&vec![0.1f32; 10 * 8], &[10, 8], &device))
                }
                "embeddings.position_embeddings.weight" => Ok(Tensor::from_slice(
                    &vec![0.01f32; 32 * 8],
                    &[32, 8],
                    &device,
                )),
                n if n.ends_with("query.weight")
                    || n.ends_with("key.weight")
                    || n.ends_with("value.weight") =>
                {
                    Ok(Tensor::from_slice(&vec![0.02f32; 8 * 8], &[8, 8], &device))
                }
                n if n.ends_with("query.bias")
                    || n.ends_with("key.bias")
                    || n.ends_with("value.bias") =>
                {
                    Ok(Tensor::from_slice(&[0.0f32; 8], &[8], &device))
                }
                n if n.ends_with("attention.output.dense.weight") => {
                    Ok(Tensor::from_slice(&vec![0.02f32; 8 * 8], &[8, 8], &device))
                }
                n if n.ends_with("attention.output.dense.bias") => {
                    Ok(Tensor::from_slice(&[0.0f32; 8], &[8], &device))
                }
                n if n.ends_with("output.dense.weight") => {
                    // ffn_down: [hidden_size, intermediate_size] = [8, 16]
                    Ok(Tensor::from_slice(
                        &vec![0.02f32; 8 * 16],
                        &[8, 16],
                        &device,
                    ))
                }
                n if n.ends_with("output.dense.bias") => {
                    Ok(Tensor::from_slice(&[0.0f32; 8], &[8], &device))
                }
                n if n.ends_with("LayerNorm.weight") => {
                    Ok(Tensor::from_slice(&[1.0f32; 8], &[8], &device))
                }
                n if n.ends_with("LayerNorm.bias") => {
                    Ok(Tensor::from_slice(&[0.0f32; 8], &[8], &device))
                }
                n if n.ends_with("intermediate.dense.weight") => Ok(Tensor::from_slice(
                    &vec![0.02f32; 16 * 8],
                    &[16, 8],
                    &device,
                )),
                n if n.ends_with("intermediate.dense.bias") => {
                    Ok(Tensor::from_slice(&[0.0f32; 16], &[16], &device))
                }
                _ => Err(Error::ModelError {
                    reason: format!("unknown weight: {name}"),
                }),
            }
        })
        .unwrap();

        (encoder, client, device)
    }

    #[test]
    fn test_encode_output_shape() {
        let (encoder, client, device) = make_test_encoder();
        let input_ids = Tensor::<CpuRuntime>::from_slice(&[1i64, 2, 3], &[1, 3], &device);
        let hidden = encoder.encode(&client, &input_ids).unwrap();
        assert_eq!(hidden.shape(), &[1, 3, 8]);
    }

    #[test]
    fn test_embed_mean_pool() {
        let (encoder, client, device) = make_test_encoder();
        let input_ids = Tensor::<CpuRuntime>::from_slice(&[1i64, 2, 3, 4], &[1, 4], &device);
        let emb = encoder.embed(&client, &input_ids).unwrap();
        assert_eq!(emb.shape(), &[1, 8]);
    }

    #[test]
    fn test_embed_batched() {
        let (encoder, client, device) = make_test_encoder();
        let input_ids = Tensor::<CpuRuntime>::from_slice(&[1i64, 2, 3, 4, 5, 6], &[2, 3], &device);
        let emb = encoder.embed(&client, &input_ids).unwrap();
        assert_eq!(emb.shape(), &[2, 8]);
    }
}