akuna-embed 0.1.0

Simple text embedding models built with 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
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
//! Simple text embedding models built with Burn.
//!
//! # Example
//!
//! ```rust,no_run
//! use akuna_embed::{EmbeddingModel, TextEmbedding, TextEmbeddingOptions};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let model = TextEmbedding::new(TextEmbeddingOptions {
//!         model: EmbeddingModel::MiniLmL12,
//!         ..Default::default()
//!     })
//!     .await?;
//!
//!     let single = model.embed("Hello world")?;
//!     assert!(!single.is_empty());
//!
//!     let batch = model.embed_batch(&["Hello world", "Rust embeddings"], None)?;
//!     assert_eq!(batch.len(), 2);
//!
//!     Ok(())
//! }
//! ```

mod bert;

use std::path::PathBuf;

use anyhow::{Context, Result, bail};
use burn::tensor::{Tensor, backend::Backend};
use burn_wgpu::{Wgpu, WgpuDevice};

use crate::bert::{
    BertEmbeddingModel, BertEmbeddingVariant, EmbeddingInputKind,
    load_pretrained_bert_embedding,
};

pub type DefaultBackend = Wgpu;
pub type DefaultDevice = WgpuDevice;
const DEFAULT_BATCH_SIZE: usize = 32;

/// Supported embedding model checkpoints.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum EmbeddingModel {
    MiniLmL6,
    #[default]
    MiniLmL12,
    BgeSmallEnV15,
    BgeBaseEnV15,
}

impl From<EmbeddingModel> for BertEmbeddingVariant {
    fn from(value: EmbeddingModel) -> Self {
        match value {
            EmbeddingModel::MiniLmL6 => BertEmbeddingVariant::MiniLmL6,
            EmbeddingModel::MiniLmL12 => BertEmbeddingVariant::MiniLmL12,
            EmbeddingModel::BgeSmallEnV15 => {
                BertEmbeddingVariant::BgeSmallEnV15
            }
            EmbeddingModel::BgeBaseEnV15 => BertEmbeddingVariant::BgeBaseEnV15,
        }
    }
}

/// Options for [`TextEmbedding`].
#[derive(Debug, Clone, Default)]
pub struct TextEmbeddingOptions {
    /// Which embedding checkpoint to load.
    pub model: EmbeddingModel,
    /// Optional Hugging Face cache directory override.
    pub cache_dir: Option<PathBuf>,
}

/// Minimal text embedding interface inspired by `fastembed-rs`.
#[derive(Debug)]
pub struct TextEmbedding<B: Backend = DefaultBackend> {
    model: BertEmbeddingModel<B>,
    device: B::Device,
}

impl TextEmbedding<DefaultBackend> {
    /// Loads a MiniLM text embedding model onto the default WGPU device.
    pub async fn new(options: TextEmbeddingOptions) -> Result<Self> {
        let device = WgpuDevice::default();
        Self::new_with_device(&device, options).await
    }
}

impl<B> TextEmbedding<B>
where
    B: Backend,
{
    /// Loads a MiniLM text embedding model onto the provided device.
    pub async fn new_with_device(
        device: &B::Device,
        options: TextEmbeddingOptions,
    ) -> Result<Self> {
        let model = load_pretrained_bert_embedding(
            device,
            options.model.into(),
            options.cache_dir,
        )
        .await?;

        Ok(Self {
            model,
            device: device.clone(),
        })
    }

    /// Embeds a single document and returns one embedding vector.
    pub fn embed(&self, document: impl AsRef<str>) -> Result<Vec<f32>> {
        let document = document.as_ref();
        let documents = [document];
        let mut embeddings = self.embed_batch(documents.as_slice(), None)?;
        embeddings
            .pop()
            .context("expected one embedding for a single input document")
    }

    /// Embeds a search query using any model-specific retrieval prompt.
    ///
    /// Some retrieval models train queries and documents with different text
    /// prefixes. Use this when the text is the thing being searched for.
    /// Use [`TextEmbedding::embed`] when the text is the content being indexed.
    pub fn embed_query(&self, query: impl AsRef<str>) -> Result<Vec<f32>> {
        let query = query.as_ref();
        let queries = [query];
        let mut embeddings =
            self.embed_query_batch(queries.as_slice(), None)?;
        embeddings
            .pop()
            .context("expected one embedding for a single input query")
    }

    /// Embeds documents in batches and returns one vector per input string.
    pub fn embed_batch<S: AsRef<str>>(
        &self,
        documents: &[S],
        batch_size: Option<usize>,
    ) -> Result<Vec<Vec<f32>>> {
        self.embed_batch_with_kind(
            documents,
            batch_size,
            EmbeddingInputKind::Document,
        )
    }

    /// Embeds search queries in batches using model-specific retrieval prompts.
    ///
    /// See [`TextEmbedding::embed_query`] for when query embeddings differ from
    /// document embeddings.
    pub fn embed_query_batch<S: AsRef<str>>(
        &self,
        queries: &[S],
        batch_size: Option<usize>,
    ) -> Result<Vec<Vec<f32>>> {
        self.embed_batch_with_kind(
            queries,
            batch_size,
            EmbeddingInputKind::Query,
        )
    }

    fn embed_batch_with_kind<S: AsRef<str>>(
        &self,
        inputs: &[S],
        batch_size: Option<usize>,
        input_kind: EmbeddingInputKind,
    ) -> Result<Vec<Vec<f32>>> {
        if inputs.is_empty() {
            return Ok(Vec::new());
        }

        let batch_size = batch_size_or_default(inputs.len(), batch_size)?;

        let mut embeddings = Vec::with_capacity(inputs.len());
        for batch in inputs.chunks(batch_size) {
            let batch_inputs =
                batch.iter().map(AsRef::as_ref).collect::<Vec<_>>();
            let batch_embeddings =
                self.model.encode(&batch_inputs, input_kind, &self.device)?;
            embeddings.extend(tensor_to_rows(batch_embeddings)?);
        }

        Ok(embeddings)
    }

    /// Returns the loaded embedding checkpoint.
    pub fn model(&self) -> EmbeddingModel {
        match self.model.variant {
            BertEmbeddingVariant::MiniLmL6 => EmbeddingModel::MiniLmL6,
            BertEmbeddingVariant::MiniLmL12 => EmbeddingModel::MiniLmL12,
            BertEmbeddingVariant::BgeSmallEnV15 => {
                EmbeddingModel::BgeSmallEnV15
            }
            BertEmbeddingVariant::BgeBaseEnV15 => EmbeddingModel::BgeBaseEnV15,
        }
    }
}

fn batch_size_or_default(
    document_count: usize,
    batch_size: Option<usize>,
) -> Result<usize> {
    let batch_size =
        batch_size.unwrap_or(document_count.min(DEFAULT_BATCH_SIZE));
    if batch_size == 0 {
        bail!("batch size must be greater than zero");
    }

    Ok(batch_size)
}

fn tensor_to_rows<B: Backend>(
    embeddings: Tensor<B, 2>,
) -> Result<Vec<Vec<f32>>> {
    let [row_count, column_count] = embeddings.dims();
    let data = embeddings.into_data().convert::<f32>();
    let values = data
        .as_slice::<f32>()
        .map_err(|error| anyhow::anyhow!(error.to_string()))
        .context("failed to read embedding output tensor")?;

    Ok(values
        .chunks(column_count)
        .take(row_count)
        .map(|row| row.to_vec())
        .collect())
}

#[cfg(test)]
mod tests {
    use super::*;
    use burn::tensor::Tensor;
    use burn_wgpu::{Wgpu, WgpuDevice};
    use std::sync::OnceLock;
    use tokio::sync::Mutex;

    static LIVE_MODEL_TEST_LOCK: OnceLock<Mutex<()>> = OnceLock::new();

    #[test]
    fn api_model_mapping_converts_all_public_variants() {
        assert_eq!(
            BertEmbeddingVariant::from(EmbeddingModel::MiniLmL6),
            BertEmbeddingVariant::MiniLmL6
        );
        assert_eq!(
            BertEmbeddingVariant::from(EmbeddingModel::MiniLmL12),
            BertEmbeddingVariant::MiniLmL12
        );
        assert_eq!(
            BertEmbeddingVariant::from(EmbeddingModel::BgeSmallEnV15),
            BertEmbeddingVariant::BgeSmallEnV15
        );
        assert_eq!(
            BertEmbeddingVariant::from(EmbeddingModel::BgeBaseEnV15),
            BertEmbeddingVariant::BgeBaseEnV15
        );
    }

    #[test]
    fn api_model_metadata_returns_bge_repo_ids() {
        assert_eq!(
            BertEmbeddingVariant::BgeSmallEnV15.repo_id(),
            "BAAI/bge-small-en-v1.5"
        );
        assert_eq!(
            BertEmbeddingVariant::BgeBaseEnV15.repo_id(),
            "BAAI/bge-base-en-v1.5"
        );
    }

    #[test]
    fn api_options_default_uses_minilm_l12() {
        assert_eq!(
            TextEmbeddingOptions::default().model,
            EmbeddingModel::MiniLmL12
        );
    }

    #[tokio::test]
    async fn model_bge_small_embed_returns_document_and_query_vectors() {
        let _guard = live_model_test_lock().lock().await;
        let model = TextEmbedding::new(TextEmbeddingOptions {
            model: EmbeddingModel::BgeSmallEnV15,
            ..Default::default()
        })
        .await
        .expect("model should load");

        let document = model
            .embed("Hello world")
            .expect("document embed should work");
        let query = model
            .embed_query("Hello world")
            .expect("query embed should work");

        assert_eq!(document.len(), 384);
        assert_eq!(query.len(), 384);
    }

    #[tokio::test]
    async fn model_minilm_l6_backend_supports_i32_indices() {
        let _guard = live_model_test_lock().lock().await;
        let device = WgpuDevice::default();
        let model = TextEmbedding::<Wgpu<f32, i32>>::new_with_device(
            &device,
            TextEmbeddingOptions {
                model: EmbeddingModel::MiniLmL6,
                cache_dir: None,
            },
        )
        .await
        .expect("model should load");

        let single = model
            .embed("Hello world")
            .expect("single embed should work");
        assert!(!single.is_empty());
    }

    #[tokio::test]
    async fn model_minilm_l6_embed_returns_vectors() {
        let _guard = live_model_test_lock().lock().await;
        let model = TextEmbedding::new(TextEmbeddingOptions {
            model: EmbeddingModel::MiniLmL6,
            ..Default::default()
        })
        .await
        .expect("model should load");

        let single = model
            .embed("Hello world")
            .expect("single embed should work");
        assert!(!single.is_empty());

        let batch = model
            .embed_batch(&["Hello world", "Rust embeddings"], None)
            .expect("batch embed should work");
        assert_eq!(batch.len(), 2);
        assert!(batch.iter().all(|embedding| !embedding.is_empty()));
    }

    #[tokio::test]
    async fn parity_bge_base_document_matches_sentence_transformers() {
        assert_model_matches_sentence_transformers(
            EmbeddingModel::BgeBaseEnV15,
            "BAAI/bge-base-en-v1.5",
            ReferenceInputKind::Document,
        )
        .await;
    }

    #[tokio::test]
    async fn parity_bge_base_query_matches_sentence_transformers() {
        assert_model_matches_sentence_transformers(
            EmbeddingModel::BgeBaseEnV15,
            "BAAI/bge-base-en-v1.5",
            ReferenceInputKind::Query,
        )
        .await;
    }

    #[tokio::test]
    async fn parity_bge_small_document_matches_sentence_transformers() {
        assert_model_matches_sentence_transformers(
            EmbeddingModel::BgeSmallEnV15,
            "BAAI/bge-small-en-v1.5",
            ReferenceInputKind::Document,
        )
        .await;
    }

    #[tokio::test]
    async fn parity_bge_small_query_matches_sentence_transformers() {
        assert_model_matches_sentence_transformers(
            EmbeddingModel::BgeSmallEnV15,
            "BAAI/bge-small-en-v1.5",
            ReferenceInputKind::Query,
        )
        .await;
    }

    #[tokio::test]
    async fn parity_minilm_l12_document_matches_sentence_transformers() {
        assert_model_matches_sentence_transformers(
            EmbeddingModel::MiniLmL12,
            "sentence-transformers/all-MiniLM-L12-v2",
            ReferenceInputKind::Document,
        )
        .await;
    }

    #[tokio::test]
    async fn parity_minilm_l12_query_matches_sentence_transformers() {
        assert_model_matches_sentence_transformers(
            EmbeddingModel::MiniLmL12,
            "sentence-transformers/all-MiniLM-L12-v2",
            ReferenceInputKind::Query,
        )
        .await;
    }

    #[tokio::test]
    async fn parity_minilm_l6_document_matches_sentence_transformers() {
        assert_model_matches_sentence_transformers(
            EmbeddingModel::MiniLmL6,
            "sentence-transformers/all-MiniLM-L6-v2",
            ReferenceInputKind::Document,
        )
        .await;
    }

    #[tokio::test]
    async fn parity_minilm_l6_query_matches_sentence_transformers() {
        assert_model_matches_sentence_transformers(
            EmbeddingModel::MiniLmL6,
            "sentence-transformers/all-MiniLM-L6-v2",
            ReferenceInputKind::Query,
        )
        .await;
    }

    #[test]
    fn util_batch_size_default_caps_large_batches() {
        let batch_size = batch_size_or_default(128, None)
            .expect("default batch size should work");
        assert_eq!(batch_size, DEFAULT_BATCH_SIZE);
    }

    #[test]
    fn util_batch_size_default_uses_document_count_when_small() {
        let batch_size = batch_size_or_default(4, None)
            .expect("default batch size should work");
        assert_eq!(batch_size, 4);
    }

    #[test]
    fn util_batch_size_validate_rejects_zero() {
        let error = batch_size_or_default(1, Some(0))
            .expect_err("zero batch size should fail");
        assert!(
            error
                .to_string()
                .contains("batch size must be greater than zero")
        );
    }

    #[test]
    fn util_tensor_rows_extract_returns_rows() {
        let device = WgpuDevice::default();
        let embeddings = Tensor::<Wgpu<f32, i64>, 2>::from_floats(
            [[1.0, 2.0], [3.0, 4.0]],
            &device,
        );

        let rows = tensor_to_rows(embeddings).expect("rows should extract");
        assert_eq!(rows, vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
    }

    #[derive(Debug, Clone, Copy)]
    enum ReferenceInputKind {
        Document,
        Query,
    }

    impl ReferenceInputKind {
        fn as_str(self) -> &'static str {
            match self {
                Self::Document => "document",
                Self::Query => "query",
            }
        }
    }

    async fn assert_model_matches_sentence_transformers(
        model: EmbeddingModel,
        reference_model: &str,
        input_kind: ReferenceInputKind,
    ) {
        let _guard = live_model_test_lock().lock().await;
        let texts =
            vec!["Hello world".to_string(), "Rust embeddings".to_string()];
        let model = TextEmbedding::new(TextEmbeddingOptions {
            model,
            ..Default::default()
        })
        .await
        .expect("model should load");
        let actual = match input_kind {
            ReferenceInputKind::Document => model
                .embed_batch(&texts, Some(2))
                .expect("Burn document embeddings should work"),
            ReferenceInputKind::Query => model
                .embed_query_batch(&texts, Some(2))
                .expect("Burn query embeddings should work"),
        };
        let expected =
            reference_embeddings(reference_model, input_kind.as_str(), &texts)
                .expect("reference embeddings should work");

        assert_embedding_batches_close(&actual, &expected, 1e-3, 0.999);
    }

    fn live_model_test_lock() -> &'static Mutex<()> {
        LIVE_MODEL_TEST_LOCK.get_or_init(|| Mutex::new(()))
    }

    fn reference_embeddings(
        model: &str,
        kind: &str,
        texts: &[String],
    ) -> Result<Vec<Vec<f32>>> {
        use std::io::Write;
        use std::process::{Command, Stdio};

        let mut child = Command::new("uv")
            .args([
                "run",
                "scripts/reference_embeddings.py",
                "--model",
                model,
                "--kind",
                kind,
            ])
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .context("failed to spawn uv reference embedding script")?;

        let mut stdin = child
            .stdin
            .take()
            .context("failed to open reference script stdin")?;
        let input = serde_json::to_vec(texts)
            .context("failed to serialize reference input")?;
        stdin
            .write_all(&input)
            .context("failed to write reference input")?;
        drop(stdin);

        let output = child
            .wait_with_output()
            .context("failed to wait for reference script")?;
        if !output.status.success() {
            bail!(
                "reference script failed: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }

        serde_json::from_slice(&output.stdout)
            .context("failed to parse reference embeddings")
    }

    fn assert_embedding_batches_close(
        actual: &[Vec<f32>],
        expected: &[Vec<f32>],
        tolerance: f32,
        min_cosine_similarity: f32,
    ) {
        assert_eq!(actual.len(), expected.len());
        for (actual, expected) in actual.iter().zip(expected) {
            assert_eq!(actual.len(), expected.len());
            let max_delta = actual
                .iter()
                .zip(expected)
                .map(|(actual, expected)| (actual - expected).abs())
                .fold(0.0f32, f32::max);
            assert!(
                max_delta <= tolerance,
                "max embedding delta {max_delta} exceeded tolerance {tolerance}"
            );
            let cosine_similarity = cosine_similarity(actual, expected);
            assert!(
                cosine_similarity >= min_cosine_similarity,
                "cosine similarity {cosine_similarity} fell below {min_cosine_similarity}"
            );
        }
    }

    fn cosine_similarity(left: &[f32], right: &[f32]) -> f32 {
        let dot_product = left
            .iter()
            .zip(right)
            .map(|(left, right)| left * right)
            .sum::<f32>();
        let left_norm =
            left.iter().map(|value| value * value).sum::<f32>().sqrt();
        let right_norm =
            right.iter().map(|value| value * value).sum::<f32>().sqrt();

        dot_product / (left_norm * right_norm)
    }
}