ONNX export of
nvidia/llama-nemotron-rerank-1b-v2 for CPU/GPU inference via ONNX Runtime and
fastembed-rs.
1[dependencies]
2fastembed = "5"
1use fastembed::{TextRerank, RerankInitOptions, RerankerModel};
2
3let mut model = TextRerank::try_new(
4 RerankInitOptions::new(RerankerModel::LlamaNemotronRerank1BV2Int4Full)
5 .with_show_download_progress(true),
6)?;
7
8let results = model.rerank(
9 "what is a panda?",
10 vec![
11 "A panda is a large black-and-white bear native to China.",
12 "The sky is blue and the grass is green.",
13 ],
14 true,
15 None,
16)?;
17// results[0].score ≈ 1.50, results[1].score ≈ -6.87
1import numpy as np
2import onnxruntime as ort
3from transformers import AutoTokenizer
4
5session = ort.InferenceSession(
6 "model_int4_full.onnx",
7 providers=["CPUExecutionProvider"],
8)
9tokenizer = AutoTokenizer.from_pretrained(
10 "nvidia/llama-nemotron-rerank-1b-v2", trust_remote_code=True
11)
12
13query = "what is a panda?"
14documents = [
15 "A panda is a large black-and-white bear native to China.",
16 "The sky is blue and the grass is green.",
17]
18
19enc = tokenizer(
20 [query] * len(documents),
21 documents,
22 padding=True,
23 truncation=True,
24 max_length=512,
25 return_tensors="np",
26)
27logits = session.run(
28 ["logits"],
29 {
30 "input_ids": enc["input_ids"].astype(np.int64),
31 "attention_mask": enc["attention_mask"].astype(np.int64),
32 },
33)[0]
34scores = logits[:, 0].tolist()
35ranked = sorted(zip(documents, scores), key=lambda x: x[1], reverse=True)
36for doc, score in ranked:
37 print(f"{score:7.3f} {doc[:80]}")
The Llama Nemotron Reranking 1B model is optimized for providing a logit score representing how relevant a document is to a given query. Fine-tuned for multilingual, cross-lingual text question-answering retrieval, with support for long documents (up to 8192 tokens). Evaluated on 26 languages.
The model is a transformer cross-encoder fine-tuned with contrastive learning. Bidirectional attention is applied during fine-tuning for higher accuracy. Mean pooling over the last decoder output is used with a binary classification head for ranking.