From 2c150815f5de1f1a72b8d28baed3ce2b2c408582 Mon Sep 17 00:00:00 2001 From: Anush008 Date: Fri, 8 Aug 2025 11:47:48 +0530 Subject: [PATCH 1/2] fix: Fix nondeterministic output tesnor selection behaviour Signed-off-by: Anush008 --- src/output/embedding_output.rs | 11 +++++++++-- tests/embeddings.rs | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/output/embedding_output.rs b/src/output/embedding_output.rs index 384d918..e94316b 100644 --- a/src/output/embedding_output.rs +++ b/src/output/embedding_output.rs @@ -10,7 +10,7 @@ use super::{OutputKey, OutputPrecedence}; /// pooling etc. This struct should contain all the necessary information for the /// post-processing to be performed. pub struct SingleBatchOutput { - pub outputs: std::collections::HashMap, + pub outputs: std::collections::BTreeMap, pub attention_mask_array: Array2, } @@ -26,7 +26,14 @@ impl SingleBatchOutput { let ort_output: &ort::value::Value = precedence .key_precedence() .find_map(|key| match key { - OutputKey::OnlyOne => self.outputs.values().next(), + // Only select the sole output if and only if there is exactly one. + OutputKey::OnlyOne => { + if self.outputs.len() == 1 { + self.outputs.values().next() + } else { + None + } + } OutputKey::ByOrder(idx) => self.outputs.values().nth(*idx), OutputKey::ByName(name) => self.outputs.get(*name), }) diff --git a/tests/embeddings.rs b/tests/embeddings.rs index 900b57e..70e7772 100644 --- a/tests/embeddings.rs +++ b/tests/embeddings.rs @@ -659,3 +659,24 @@ fn test_allminilml6v2_match_python_counterpart() { assert!((expected - actual).abs() < tolerance); } } + +// Ref: https://github.com/Anush008/fastembed-rs/issues/171#issue-3209484009 +#[test] +fn clip_vit_b32_deterministic_across_calls() { + let q = "red car"; + let mut fe = TextEmbedding::try_new(InitOptions::new(EmbeddingModel::ClipVitB32)).unwrap(); + let mut first: Option> = None; + for i in 0..100 { + let vecs = fe.embed(vec![q], None).unwrap(); + if first.is_none() { + first = Some(vecs[0].clone()); + } else { + assert_eq!( + vecs[0], + *first.as_ref().unwrap(), + "Embedding changed after {} iterations", + i + ); + } + } +} From c059f4923846c6476bb7a78e9ae64d620999c79f Mon Sep 17 00:00:00 2001 From: Anush Date: Fri, 8 Aug 2025 11:56:35 +0530 Subject: [PATCH 2/2] ci: Updated test.yml --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1a3c434..7f7e64c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,7 +5,7 @@ on: env: CARGO_TERM_COLOR: always RUSTFLAGS: "-Dwarnings" - ONNX_VERSION: v1.22.0 + ONNX_VERSION: v1.22.1 jobs: test: