Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/sparse_text_embedding/bgem3_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ impl Bgem3SparseWeights {
.expect("Failed to deserialize sparse_linear.safetensors");

let weight_view = tensors.tensor("weight").expect("Missing 'weight' tensor");
let weight: Vec<f32> = weight_view
.data()
.chunks_exact(4)
.map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]]))
let (weight_chunks, weight_remainder) = weight_view.data().as_chunks::<4>();
assert!(
weight_remainder.is_empty(),
"'weight' tensor byte length is not divisible by 4"
);
let weight: Vec<f32> = weight_chunks
.iter()
.map(|b| f32::from_le_bytes(*b))
.collect();

let bias_view = tensors.tensor("bias").expect("Missing 'bias' tensor");
Expand Down
4 changes: 2 additions & 2 deletions src/sparse_text_embedding/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ impl SparseTextEmbedding {
.rows()
.into_iter()
.map(|row_scores| {
let mut values: Vec<f32> = Vec::with_capacity(scores.len());
let mut indices: Vec<usize> = Vec::with_capacity(scores.len());
let mut values: Vec<f32> = Vec::with_capacity(row_scores.len());
let mut indices: Vec<usize> = Vec::with_capacity(row_scores.len());

row_scores.into_iter().enumerate().for_each(|(idx, f)| {
if *f > 0.0 {
Expand Down
25 changes: 13 additions & 12 deletions tests/bgem3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use std::collections::HashMap;
use std::sync::Mutex;

static MODEL_LOCK: Mutex<()> = Mutex::new(());
const EPS: f32 = 2e-2;

#[test]
fn test_bgem3_joint_embeddings_match_python() {
let _guard = MODEL_LOCK.lock().unwrap();
let _guard = MODEL_LOCK.lock().unwrap_or_else(|err| err.into_inner());
let mut model = Bgem3Embedding::try_new(Bgem3InitOptions::new(Bgem3Model::BGEM3Q))
.expect("Failed to initialize BGEM3Q model");

Expand Down Expand Up @@ -43,10 +44,10 @@ fn test_bgem3_joint_embeddings_match_python() {
];

for (i, val) in expected_dense_0.iter().enumerate() {
assert!((output.dense[0][i] - val).abs() < 1e-4);
assert!((output.dense[0][i] - val).abs() < EPS);
}
for (i, val) in expected_dense_1.iter().enumerate() {
assert!((output.dense[1][i] - val).abs() < 1e-4);
assert!((output.dense[1][i] - val).abs() < EPS);
}

// 2. Verify Sparse Embeddings
Expand Down Expand Up @@ -93,7 +94,7 @@ fn test_bgem3_joint_embeddings_match_python() {
.get(idx)
.expect("Unexpected index in sparse 0");
assert!(
(val - expected_val).abs() < 1e-4,
(val - expected_val).abs() < EPS,
"Sparse 0 index {}: expected {}, got {}",
idx,
expected_val,
Expand All @@ -111,7 +112,7 @@ fn test_bgem3_joint_embeddings_match_python() {
.get(idx)
.expect("Unexpected index in sparse 1");
assert!(
(val - expected_val).abs() < 1e-4,
(val - expected_val).abs() < EPS,
"Sparse 1 index {}: expected {}, got {}",
idx,
expected_val,
Expand Down Expand Up @@ -146,22 +147,22 @@ fn test_bgem3_joint_embeddings_match_python() {
];

for (i, val) in expected_colbert_0_tok1.iter().enumerate() {
assert!((output.colbert[0][0][i] - val).abs() < 1e-4);
assert!((output.colbert[0][0][i] - val).abs() < EPS);
}
for (i, val) in expected_colbert_0_tok2.iter().enumerate() {
assert!((output.colbert[0][1][i] - val).abs() < 1e-4);
assert!((output.colbert[0][1][i] - val).abs() < EPS);
}
for (i, val) in expected_colbert_1_tok1.iter().enumerate() {
assert!((output.colbert[1][0][i] - val).abs() < 1e-4);
assert!((output.colbert[1][0][i] - val).abs() < EPS);
}
for (i, val) in expected_colbert_1_tok2.iter().enumerate() {
assert!((output.colbert[1][1][i] - val).abs() < 1e-4);
assert!((output.colbert[1][1][i] - val).abs() < EPS);
}
}

#[test]
fn test_bgem3_user_defined_model() {
let _guard = MODEL_LOCK.lock().unwrap();
let _guard = MODEL_LOCK.lock().unwrap_or_else(|err| err.into_inner());
// We will verify the user-defined loader by pulling the files from HF and feeding them manually to simulate a local deployment

// Reuse fastembed's cache — model already downloaded by test_bgem3_joint_embeddings_match_python
Expand Down Expand Up @@ -213,13 +214,13 @@ fn test_bgem3_user_defined_model() {
-0.01868816465139389,
];
for (i, val) in expected_dense_0.iter().enumerate() {
assert!((output.dense[0][i] - val).abs() < 1e-4);
assert!((output.dense[0][i] - val).abs() < EPS);
}
}

#[test]
fn test_bgem3_custom_max_length() {
let _guard = MODEL_LOCK.lock().unwrap();
let _guard = MODEL_LOCK.lock().unwrap_or_else(|err| err.into_inner());
// Verify that the user can override the max length (e.g. to 5 tokens) and it successfully truncates
let mut model =
Bgem3Embedding::try_new(Bgem3InitOptions::new(Bgem3Model::BGEM3Q).with_max_length(5))
Expand Down
2 changes: 1 addition & 1 deletion tests/text-embeddings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use fastembed::{
};

/// A small epsilon value for floating point comparisons.
const EPS: f32 = 1e-2;
const EPS: f32 = 2e-2;

/// Precalculated embeddings for the supported models using #99
/// (4f09b6842ce1fcfaf6362678afcad9a176e05304).
Expand Down