Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support bm42 embeddings #338

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backends/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tokio = { workspace = true }
tracing = { workspace = true }

[features]
default = ["ort"]
clap = ["dep:clap", "text-embeddings-backend-core/clap"]
python = ["dep:text-embeddings-backend-python"]
ort = ["dep:text-embeddings-backend-ort"]
Expand Down
3 changes: 2 additions & 1 deletion backends/candle/src/models/bert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ impl BertModel {

(outputs.sum(1)?.broadcast_div(&input_lengths))?
}
Pool::BM42 => unreachable!(),
Pool::Splade => {
// Unwrap is safe here
let splade_head = self.splade.as_ref().unwrap();
Expand All @@ -874,7 +875,7 @@ impl BertModel {
}

relu_log.max(1)?
}
},
};
Some(pooled_embeddings)
} else {
Expand Down
1 change: 1 addition & 0 deletions backends/candle/src/models/distilbert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ impl DistilBertModel {

(outputs.sum(1)?.broadcast_div(&input_lengths))?
}
Pool::BM42 => unreachable!(),
Pool::Splade => {
// Unwrap is safe here
let splade_head = self.splade.as_ref().unwrap();
Expand Down
1 change: 1 addition & 0 deletions backends/candle/src/models/jina.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ impl JinaBertModel {

(outputs.sum(1)?.broadcast_div(&input_lengths))?
}
Pool::BM42 => unreachable!(),
Pool::Splade => unreachable!(),
};
Some(pooled_embeddings)
Expand Down
3 changes: 2 additions & 1 deletion backends/candle/src/models/jina_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,8 @@ impl JinaCodeBertModel {
}

(outputs.sum(1)?.broadcast_div(&input_lengths))?
}
},
Pool::BM42 => unreachable!(),
Pool::Splade => unreachable!(),
};
Some(pooled_embeddings)
Expand Down
1 change: 1 addition & 0 deletions backends/candle/src/models/nomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ impl NomicBertModel {

(outputs.sum(1)?.broadcast_div(&input_lengths))?
}
Pool::BM42 => unreachable!(),
Pool::Splade => unreachable!(),
};
Some(pooled_embeddings)
Expand Down
18 changes: 18 additions & 0 deletions backends/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,34 @@ pub enum Pool {
/// This option is only available if the loaded model is a `ForMaskedLM` Transformer
/// model.
Splade,
/// Apply BM42 to the model embeddings.
/// This option is only availale if the loaded model is Qdrant/all_miniLM_L6_v2_with_attentions
BM42,
/// Select the last token as embedding
LastToken,
}

#[derive(Debug, Clone)]
pub struct Bm42Params {
pub invert_vocab: std::collections::HashMap<u32, String>,
pub stopwords: Vec<String>,
pub special_tokens: Vec<String>,
}

#[derive(Debug, Clone)]
pub enum ModelParams {
Bm42(Bm42Params),
None
}


impl fmt::Display for Pool {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Pool::Cls => write!(f, "cls"),
Pool::Mean => write!(f, "mean"),
Pool::Splade => write!(f, "splade"),
Pool::BM42 => write!(f, "bm42"),
Pool::LastToken => write!(f, "last_token"),
}
}
Expand Down
2 changes: 2 additions & 0 deletions backends/ort/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ tracing = { workspace = true }
thiserror = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
rust-stemmers = "1.2.0"
murmur3 = "0.5.2"
Loading