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
1 change: 1 addition & 0 deletions Cargo.lock

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

6 changes: 1 addition & 5 deletions backends/candle/src/models/flash_modernbert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,7 @@ impl FlashModernBertModel {

let (pool, classifier) = match model_type {
ModelType::Classifier => {
let pool: Pool = config
.classifier_pooling
.as_deref()
.and_then(|s| Pool::from_str(s).ok())
.unwrap_or(Pool::Cls);
let pool: Pool = config.classifier_pooling.clone().unwrap_or(Pool::Cls);

let classifier: Box<dyn ClassificationHead + Send> =
Box::new(ModernBertClassificationHead::load(vb.clone(), config)?);
Expand Down
9 changes: 2 additions & 7 deletions backends/candle/src/models/modernbert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use candle::{DType, Device, IndexOp, Module, Result, Tensor, D};
use candle_nn::{Embedding, VarBuilder};
use serde::Deserialize;
use text_embeddings_backend_core::{Batch, ModelType, Pool};
use std::str::FromStr;

// https://github.com/huggingface/transformers/blob/main/src/transformers/models/modernbert/configuration_modernbert.py
#[derive(Debug, Clone, PartialEq, Deserialize)]
Expand Down Expand Up @@ -38,7 +37,7 @@ pub struct ModernBertConfig {
pub mlp_bias: Option<bool>,
pub mlp_dropout: Option<f64>,
pub decoder_bias: Option<bool>,
pub classifier_pooling: Option<String>,
pub classifier_pooling: Option<Pool>,
pub classifier_dropout: Option<f64>,
pub classifier_bias: Option<bool>,
pub classifier_activation: HiddenAct,
Expand Down Expand Up @@ -485,11 +484,7 @@ impl ModernBertModel {
pub fn load(vb: VarBuilder, config: &ModernBertConfig, model_type: ModelType) -> Result<Self> {
let (pool, classifier) = match model_type {
ModelType::Classifier => {
let pool: Pool = config
.classifier_pooling
.as_deref()
.and_then(|s| Pool::from_str(s).ok())
.unwrap_or(Pool::Cls);
let pool: Pool = config.classifier_pooling.clone().unwrap_or(Pool::Cls);

let classifier: Box<dyn ClassificationHead + Send> =
Box::new(ModernBertClassificationHead::load(vb.clone(), config)?);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
---
source: backends/candle/tests/test_modernbert.rs
assertion_line: 229
expression: predictions_single
---
- - -0.30617672

Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: backends/candle/tests/test_modernbert.rs
expression: predictions_single
---
- - 2.2585099
- - 2.13616
2 changes: 1 addition & 1 deletion backends/candle/tests/test_modernbert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,4 @@ fn test_modernbert_classification_mean_pooling() -> Result<()> {
);

Ok(())
}
}
1 change: 1 addition & 0 deletions backends/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ homepage.workspace = true
thiserror = { workspace = true }
clap = { workspace = true, optional = true }
nohash-hasher = { workspace = true }
serde = { workspace = true }

[features]
clap = ["dep:clap"]
21 changes: 3 additions & 18 deletions backends/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(feature = "clap")]
use clap::ValueEnum;
use nohash_hasher::IntMap;
use serde::Deserialize;
use std::fmt;
use thiserror::Error;

Expand Down Expand Up @@ -52,8 +53,9 @@ pub enum ModelType {
Embedding(Pool),
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Deserialize)]
#[cfg_attr(feature = "clap", derive(ValueEnum))]
#[serde(rename_all = "snake_case")]
pub enum Pool {
/// Select the CLS token as embedding
Cls,
Expand All @@ -78,23 +80,6 @@ impl fmt::Display for Pool {
}
}

impl std::str::FromStr for Pool {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim().to_lowercase().as_str() {
"cls" => Ok(Pool::Cls),
"mean" => Ok(Pool::Mean),
"splade" => Ok(Pool::Splade),
"last_token" => Ok(Pool::LastToken),
_ => Err(format!(
"Invalid pooling method '{}'. Valid options: cls, mean, splade, last_token",
s
)),
}
}
}

#[derive(Debug, Error, Clone)]
pub enum BackendError {
#[error("No backend found")]
Expand Down
Loading