Skip to content

Adding missing head. prefix in the weight name in ModernBertClassificationHead #591

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

Merged
merged 5 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Below are some examples of the currently supported models:
| Re-Ranking | XLM-RoBERTa | [BAAI/bge-reranker-large](https://huggingface.co/BAAI/bge-reranker-large) |
| Re-Ranking | XLM-RoBERTa | [BAAI/bge-reranker-base](https://huggingface.co/BAAI/bge-reranker-base) |
| Re-Ranking | GTE | [Alibaba-NLP/gte-multilingual-reranker-base](https://huggingface.co/Alibaba-NLP/gte-multilingual-reranker-base) |
| Re-Ranking | ModernBert | [Alibaba-NLP/gte-reranker-modernbert-base](https://huggingface.co/Alibaba-NLP/gte-reranker-modernbert-base) |
| Sentiment Analysis | RoBERTa | [SamLowe/roberta-base-go_emotions](https://huggingface.co/SamLowe/roberta-base-go_emotions) |

### Docker
Expand Down
15 changes: 9 additions & 6 deletions backends/candle/src/models/modernbert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,24 +412,27 @@ pub struct ModernBertClassificationHead {
impl ModernBertClassificationHead {
pub(crate) fn load(vb: VarBuilder, config: &ModernBertConfig) -> Result<Self> {
let dense_weight = vb
.pp("dense")
.pp("head.dense")
.get((config.hidden_size, config.hidden_size), "weight")?;
let dense_bias = vb.pp("dense").get(config.hidden_size, "bias").ok();
let dense_bias = vb.pp("head.dense").get(config.hidden_size, "bias").ok();
let dense = Linear::new(
dense_weight,
dense_bias,
Some(config.classifier_activation.clone()),
);

let norm =
LayerNormNoBias::load(vb.pp("norm"), config.hidden_size, config.norm_eps as f32)?;
let norm = LayerNormNoBias::load(
vb.pp("head.norm"),
config.hidden_size,
config.norm_eps as f32,
)?;

let classifier_weight = vb.pp("dense").get(
let classifier_weight = vb.pp("classifier").get(
(config.num_labels.unwrap_or(1), config.hidden_size),
"weight",
)?;
let classifier_bias = vb
.pp("dense")
.pp("classifier")
.get(config.num_labels.unwrap_or(1), "bias")
.ok();
let classifier = Linear::new(classifier_weight, classifier_bias, None);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: backends/candle/tests/test_modernbert.rs
expression: predictions_single
---
- - 2.2585099
37 changes: 36 additions & 1 deletion backends/candle/tests/test_modernbert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ mod common;

use crate::common::{sort_embeddings, SnapshotEmbeddings};
use anyhow::Result;
use common::{batch, cosine_matcher, download_artifacts, load_tokenizer};
use common::{
batch, cosine_matcher, download_artifacts, load_tokenizer, relative_matcher, SnapshotScores,
};
use text_embeddings_backend_candle::CandleBackend;
use text_embeddings_backend_core::{Backend, ModelType, Pool};

Expand Down Expand Up @@ -135,3 +137,36 @@ fn test_mini_pooled_raw() -> Result<()> {

Ok(())
}

#[test]
#[serial_test::serial]
fn test_modernbert_classification() -> Result<()> {
let model_root = download_artifacts("Alibaba-NLP/gte-reranker-modernbert-base", None).unwrap();
let tokenizer = load_tokenizer(&model_root)?;

let backend = CandleBackend::new(&model_root, "float32".to_string(), ModelType::Classifier)?;

let input_single = batch(
vec![tokenizer
.encode(("What is Deep Learning?", "Deep Learning is not..."), true)
.unwrap()],
[0].to_vec(),
vec![],
);

let predictions: Vec<Vec<f32>> = backend
.predict(input_single)?
.into_iter()
.map(|(_, v)| v)
.collect();
let predictions_single = SnapshotScores::from(predictions);

let matcher = relative_matcher();
insta::assert_yaml_snapshot!(
"modernbert_classification_single",
predictions_single,
&matcher
);

Ok(())
}
Loading