ColQwen2
This model was released on 2024-06-27 and added to Hugging Face Transformers on 2025-06-02.
ColQwen2
Section titled “ColQwen2”ColQwen2 is a variant of the ColPali model designed to retrieve documents by analyzing their visual features. Unlike traditional systems that rely heavily on text extraction and OCR, ColQwen2 treats each page as an image. It uses the Qwen2-VL backbone to capture not only text, but also the layout, tables, charts, and other visual elements to create detailed multi-vector embeddings that can be used for retrieval by computing pairwise late interaction similarity scores. This offers a more comprehensive understanding of documents and enables more efficient and accurate retrieval.
This model was contributed by @tonywu71 (ILLUIN Technology) and @yonigozlan (HuggingFace).
You can find all the original ColPali checkpoints under Vidore’s Hf-native ColVision Models collection.
import requestsimport torchfrom PIL import Image
from transformers import ColQwen2ForRetrieval, ColQwen2Processorfrom transformers.utils.import_utils import is_flash_attn_2_available
# Load the model and the processormodel_name = "vidore/colqwen2-v1.0-hf"
model = ColQwen2ForRetrieval.from_pretrained( model_name, dtype=torch.bfloat16, device_map="auto", # "cpu", "cuda", "xpu" or "mps" for Apple Silicon attn_implementation="flash_attention_2" if is_flash_attn_2_available() else "sdpa",)processor = ColQwen2Processor.from_pretrained(model_name)
# The document page screenshots from your corpusurl1 = "https://upload.wikimedia.org/wikipedia/commons/8/89/US-original-Declaration-1776.jpg"url2 = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Romeoandjuliet1597.jpg/500px-Romeoandjuliet1597.jpg"
images = [ Image.open(requests.get(url1, stream=True).raw), Image.open(requests.get(url2, stream=True).raw),]
# The queries you want to retrieve documents forqueries = [ "When was the United States Declaration of Independence proclaimed?", "Who printed the edition of Romeo and Juliet?",]
# Process the inputsinputs_images = processor(images=images).to(model.device)inputs_text = processor(text=queries).to(model.device)
# Forward passwith torch.no_grad(): image_embeddings = model(**inputs_images).embeddings query_embeddings = model(**inputs_text).embeddings
# Score the queries against the imagesscores = processor.score_retrieval(query_embeddings, image_embeddings)
print("Retrieval scores (query x image):")print(scores)If you have issue with loading the images with PIL, you can use the following code to create dummy images:
images = [ Image.new("RGB", (128, 128), color="white"), Image.new("RGB", (64, 32), color="black"),]Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the Quantization overview for more available quantization backends.
The example below uses bitsandbytes to quantize the weights to int4.
import requestsimport torchfrom PIL import Image
from transformers import BitsAndBytesConfig, ColQwen2ForRetrieval, ColQwen2Processorfrom accelerate import Accelerator
model_name = "vidore/colqwen2-v1.0-hf"device = Accelerator().device
# 4-bit quantization configurationbnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16,)
model = ColQwen2ForRetrieval.from_pretrained( model_name, quantization_config=bnb_config, device_map=device,).eval()
processor = ColQwen2Processor.from_pretrained(model_name)
url1 = "https://upload.wikimedia.org/wikipedia/commons/8/89/US-original-Declaration-1776.jpg"url2 = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Romeoandjuliet1597.jpg/500px-Romeoandjuliet1597.jpg"
images = [ Image.open(requests.get(url1, stream=True).raw), Image.open(requests.get(url2, stream=True).raw),]
queries = [ "When was the United States Declaration of Independence proclaimed?", "Who printed the edition of Romeo and Juliet?",]
# Process the inputsinputs_images = processor(images=images, return_tensors="pt").to(model.device)inputs_text = processor(text=queries, return_tensors="pt").to(model.device)
# Forward passwith torch.no_grad(): image_embeddings = model(**inputs_images).embeddings query_embeddings = model(**inputs_text).embeddings
# Score the queries against the imagesscores = processor.score_retrieval(query_embeddings, image_embeddings)
print("Retrieval scores (query x image):")print(scores)You can also use checkpoints for ColQwen2.5 that are compatible with the ColQwen2 architecture. This version of the model uses Qwen2_5_VL as the backbone.
import torchfrom transformers import ColQwen2ForRetrieval, ColQwen2Processorfrom transformers.utils.import_utils import is_flash_attn_2_available
model_name = "Sahil-Kabir/colqwen2.5-v0.2-hf" # An existing compatible checkpoint
model = ColQwen2ForRetrieval.from_pretrained( model_name, dtype=torch.bfloat16, device_map="auto", attn_implementation="flash_attention_2" if is_flash_attn_2_available() else "sdpa")processor = ColQwen2Processor.from_pretrained(model_name)score_retrievalreturns a 2D tensor where the first dimension is the number of queries and the second dimension is the number of images. A higher score indicates more similarity between the query and image.- Unlike ColPali, ColQwen2 supports arbitrary image resolutions and aspect ratios, which means images are not resized into fixed-size squares. This preserves more of the original input signal.
- Larger input images generate longer multi-vector embeddings, allowing users to adjust image resolution to balance performance and memory usage.
ColQwen2Config
Section titled “ColQwen2Config”[[autodoc]] ColQwen2Config
ColQwen2Processor
Section titled “ColQwen2Processor”[[autodoc]] ColQwen2Processor
ColQwen2ForRetrieval
Section titled “ColQwen2ForRetrieval”[[autodoc]] ColQwen2ForRetrieval - forward