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
16 changes: 13 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ env:
jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
pull-requests: write
steps:
- name: "☁️ checkout repository"
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
Expand All @@ -24,12 +28,18 @@ jobs:
run: bun install -D @semantic-release/git conventional-changelog-conventionalcommits@8 semantic-release-cargo

- name: Get Author Name and Email
id: author_info
run: |
AUTHOR_NAME=$(git log -1 --pretty=format:%an ${{ github.sha }})
AUTHOR_EMAIL=$(git log -1 --pretty=format:%ae ${{ github.sha }})
echo "AUTHOR_NAME=$AUTHOR_NAME" >> $GITHUB_OUTPUT
echo "AUTHOR_EMAIL=$AUTHOR_EMAIL" >> $GITHUB_OUTPUT
id: author_info
{
echo "AUTHOR_NAME<<EOF"
echo "$AUTHOR_NAME"
echo "EOF"
echo "AUTHOR_EMAIL<<EOF"
echo "$AUTHOR_EMAIL"
echo "EOF"
} >> "$GITHUB_OUTPUT"
Comment thread
anush008 marked this conversation as resolved.

- name: "Semantic release🚀"
id: release
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ env:
jobs:
build-onnx:
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Restore ONNX Build
Expand Down Expand Up @@ -40,6 +42,8 @@ jobs:
test:
needs: build-onnx
runs-on: ubuntu-latest
permissions:
contents: read
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
strategy:
Expand Down Expand Up @@ -74,6 +78,8 @@ jobs:

lint:
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
Expand All @@ -92,6 +98,8 @@ jobs:

clippy-features:
runs-on: ${{ matrix.os }}
permissions:
contents: read
strategy:
fail-fast: false
matrix:
Expand Down
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ pub fn normalize(v: &[f32]) -> Vec<f32> {
v.iter().map(|&val| val / (norm + epsilon)).collect()
}

/// Pulls a model repo from HuggingFace..
/// Pulls a model repo from HuggingFace.
/// HF_HOME decides the location of the cache folder
/// HF_ENDPOINT modifies the URL for the HuggingFace location.
#[cfg(feature = "hf-hub")]
Expand Down
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;
Comment thread
anush008 marked this conversation as resolved.

#[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