Views
No views yet

[!WARNING] 🚨 Migration guide to officialtransformersmodelingModernVBERT is part oftransformerssince v5.3.0!This becomes the official modeling for the model.If you have a model based on the initial modeling, you can adapt the weigths to the new format supported bytransformers. We provide a conversion script in this repo, you simply need to run:bash1python convert_model_weights.py \ 2 /path/to/legacy-modernvbert \ 3 /path/to/converted-modernvbert \The converted model:
- merges the split token embedding tables
- rewrites
model.connector.modality_projection.proj.weight- nests
model.vision_model...undermodel.vision_model.vision_model...
modernvbert.colmodernvbert (ColModernVBERT in the paper) is the late-interaction version that is fine-tuned for visual document retrieval tasks, our most performant model on this task.bimodernvbert (BiModernVBERT in the paper) is the bi-encoder version that is fine-tuned for visual document retrieval tasks.modernvbert-embed is the bi-encoder version after modality alignment (using a MLM objective) and contrastive learning, without document specialization.modernvbert is the base model after modality alignment (using a MLM objective).transformers library:pip install transformers>=5.3.0pip install flash-attn1import torch
2from transformers import AutoTokenizer, ModernVBertForMaskedLM, AutoProcessor
3from PIL import Image
4from huggingface_hub import hf_hub_download
5
6model_id = "ModernVBERT/modernvbert"
7
8processor = AutoProcessor.from_pretrained(model_id)
9tokenizer = AutoTokenizer.from_pretrained(model_id)
10model = ModernVBertForMaskedLM.from_pretrained(
11 model_id,
12 torch_dtype=torch.float32, # use torch_dtype=torch.bfloat16 for flash attention
13)
14
15image = Image.open(hf_hub_download("HuggingFaceTB/SmolVLM", "example_images/rococo.jpg", repo_type="space"))
16text = "This [MASK] is on the wall."
17
18# Create input messages
19messages = [
20 {
21 "role": "user",
22 "content": [
23 {"type": "image"},
24 {"type": "text", "text": text}
25 ]
26 },
27]
28
29# Prepare inputs
30prompt = processor.apply_chat_template(messages)
31inputs = processor(text=prompt, images=[image], return_tensors="pt")
32
33# Inference
34with torch.no_grad():
35 outputs = model(**inputs)
36
37# To get predictions for the mask:
38masked_index = inputs["input_ids"][0].tolist().index(tokenizer.mask_token_id)
39predicted_token_id = outputs.logits[0, masked_index].argmax(axis=-1)
40predicted_token = tokenizer.decode(predicted_token_id)
41print("Predicted token:", predicted_token) # Predicted token: painting
@misc{teiletche2025modernvbertsmallervisualdocument,
title={ModernVBERT: Towards Smaller Visual Document Retrievers},
author={Paul Teiletche and Quentin Macé and Max Conti and Antonio Loison and Gautier Viaud and Pierre Colombo and Manuel Faysse},
year={2025},
eprint={2510.01149},
archivePrefix={arXiv},
primaryClass={cs.IR},
url={https://arxiv.org/abs/2510.01149},
}