mirror of
https://github.com/huggingface/transformers.git
synced 2025-07-04 05:10:06 +06:00

* feat: add colqwen2 (wip) * tests: fix test_attention_outputs * tests: reduce hidden size to accelerate tests * tests: fix `test_attention_outputs` 🥳 * fix: fix wrong parent class for `ColQwen2ForRetrievalOutput` * fix: minor typing and style changes * chore: run `make style` * feat: remove redundant `max_num_visual_tokens` attribute in `ColQwen2Processor` * tests: tweak comments * style: apply ruff formatter * feat: move default values for `visual_prompt_prefix` and `query_prefix` * docs: update ColQwen2 model card * docs: tweak model cards * docs: add required example config checkpoint * tests: update expected scores in integration test * docs: tweak quickstart snippets * fix: address PR comments * tests: fix colqwen2 tests + tweak comment in colpali test * tests: unskip useful tests * fix: fix bug when `visual_prompt_prefix` or `query_prefix` is an empty string * fix: fix ColPali outputs when `return_dict == False` * fix: fix issue with PaliGemma output not being a dict * docs: set default dtype to bfloat16 in quickstart snippets * fix: fix error when `return_dict=False` in ColPali and ColQwen2 * tests: fix special tokens not being replaced in input_ids * style: fix lint * fix: `ColQwen2Processor`'s `padding_side` is now set from `processor_config.json` * fix: remove unused `padding_side` in ColQwen2 model * docs: update ColQwen2's model doc * fix: fix harcoded vlm backbone class in ColQwen2Config * fix: remove `padding_side` from ColQwen2Processor as should fed from kwargs * docs: fix typo in model docstring * docs: add illuin mention in model docs * fix: let `padding_size` be handled by `tokenizer_config.json` * docs: add colpali reference url in colqwen2's model doc * docs: add Hf mention in model docs * docs: add late interaction mention in model docs * docs: tweak colqwen2 model doc * docs: update reference checkpoint for ColPali to v1.3 * docs: simplify quickstart snippets * docs: remove redundant `.eval()` * refactor: use `can_return_tuple` decorator for ColPali and ColQwen2 * docs: fix copyright date * docs: add missing copyright in tests * fix: raise error when `initializer_range` is not in config * docs: remove redundant `.eval()` in colpali doc * fix: fix `get_text_config` now that Qwen2VL has a proper `text_config` attribute See https://github.com/huggingface/transformers/pull/37268 for details about changes in Qwen2VL's config. * fix: add missing `initializer_range` attribute in `ColQwen2Config` * fix: use `get_text_config` in `resize_token_embeddings` * update colwen2 with auto_docstring * docs: fix wrong copyright year * chore: remove `raise` as `initializer_range` has a default value in `ColQwen2Config` * refactor: merge `inner_forward` into `forward` * Refactor colqwen2 after refactoring of qwen2VL, use modular for modeling code * protect torch import in modular to protect in processing * protect torch import in modular to protect in processing * tests: fix hf model path in ColQwen2 integration test * docs: clarify `attn_implementation` and add comments * docs: add fallback snippet for using offline PIL dummy images * docs: temporarily revert attn_implementation to `None` while sdpa is not fixed * docs: tweaks in colpali/colqwen2 quick start snippets * fix: add missing flags to enable SDPA/Flex Attention in ColQwen2 model * fix: add missing changes in modular file * fix modeling tests --------- Co-authored-by: yonigozlan <yoni.gozlan@huggingface.co>
171 lines
6.0 KiB
Markdown
171 lines
6.0 KiB
Markdown
<!--Copyright 2024 The HuggingFace Team. All rights reserved.
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
|
the License. You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
|
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
specific language governing permissions and limitations under the License.
|
|
|
|
⚠️ Note that this file is in Markdown but contains specific syntax for our doc-builder (similar to MDX) that may not be
|
|
rendered properly in your Markdown viewer.
|
|
-->
|
|
|
|
<div style="float: right;">
|
|
<div class="flex flex-wrap space-x-1">
|
|
<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white">
|
|
</div>
|
|
</div>
|
|
|
|
# ColPali
|
|
|
|
[ColPali](https://huggingface.co/papers/2407.01449) is a model designed to retrieve documents by analyzing their visual features. Unlike traditional systems that rely heavily on text extraction and OCR, ColPali treats each page as an image. It uses [Paligemma-3B](./paligemma) 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](https://huggingface.co/tonywu71) (ILLUIN Technology) and [@yonigozlan](https://huggingface.co/yonigozlan) (HuggingFace).
|
|
|
|
You can find all the original ColPali checkpoints under Vidore's [Hf-native ColVision Models](https://huggingface.co/collections/vidore/hf-native-colvision-models-6755d68fc60a8553acaa96f7) collection.
|
|
|
|
> [!TIP]
|
|
> Click on the ColPali models in the right sidebar for more examples of how to use ColPali for image retrieval.
|
|
|
|
<hfoptions id="usage">
|
|
<hfoption id="image retrieval">
|
|
|
|
```python
|
|
import requests
|
|
import torch
|
|
from PIL import Image
|
|
|
|
from transformers import ColPaliForRetrieval, ColPaliProcessor
|
|
|
|
|
|
# Load the model and the processor
|
|
model_name = "vidore/colpali-v1.3-hf"
|
|
|
|
model = ColPaliForRetrieval.from_pretrained(
|
|
model_name,
|
|
torch_dtype=torch.bfloat16,
|
|
device_map="auto", # "cpu", "cuda", or "mps" for Apple Silicon
|
|
)
|
|
processor = ColPaliProcessor.from_pretrained(model_name)
|
|
|
|
# The document page screenshots from your corpus
|
|
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),
|
|
]
|
|
|
|
# The queries you want to retrieve documents for
|
|
queries = [
|
|
"When was the United States Declaration of Independence proclaimed?",
|
|
"Who printed the edition of Romeo and Juliet?",
|
|
]
|
|
|
|
# Process the inputs
|
|
inputs_images = processor(images=images).to(model.device)
|
|
inputs_text = processor(text=queries).to(model.device)
|
|
|
|
# Forward pass
|
|
with torch.no_grad():
|
|
image_embeddings = model(**inputs_images).embeddings
|
|
query_embeddings = model(**inputs_text).embeddings
|
|
|
|
# Score the queries against the images
|
|
scores = 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:
|
|
|
|
```python
|
|
images = [
|
|
Image.new("RGB", (128, 128), color="white"),
|
|
Image.new("RGB", (64, 32), color="black"),
|
|
]
|
|
```
|
|
|
|
</hfoption>
|
|
</hfoptions>
|
|
|
|
Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the [Quantization](../quantization/overview) overview for more available quantization backends.
|
|
|
|
The example below uses [bitsandbytes](../quantization/bitsandbytes.md) to quantize the weights to int4.
|
|
|
|
```python
|
|
import requests
|
|
import torch
|
|
from PIL import Image
|
|
|
|
from transformers import BitsAndBytesConfig, ColPaliForRetrieval, ColPaliProcessor
|
|
|
|
|
|
model_name = "vidore/colpali-v1.3-hf"
|
|
|
|
# 4-bit quantization configuration
|
|
bnb_config = BitsAndBytesConfig(
|
|
load_in_4bit=True,
|
|
bnb_4bit_use_double_quant=True,
|
|
bnb_4bit_quant_type="nf4",
|
|
bnb_4bit_compute_dtype=torch.float16,
|
|
)
|
|
|
|
model = ColPaliForRetrieval.from_pretrained(
|
|
model_name,
|
|
quantization_config=bnb_config,
|
|
device_map="cuda",
|
|
)
|
|
|
|
processor = ColPaliProcessor.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 inputs
|
|
inputs_images = processor(images=images, return_tensors="pt").to(model.device)
|
|
inputs_text = processor(text=queries, return_tensors="pt").to(model.device)
|
|
|
|
# Forward pass
|
|
with torch.no_grad():
|
|
image_embeddings = model(**inputs_images).embeddings
|
|
query_embeddings = model(**inputs_text).embeddings
|
|
|
|
# Score the queries against the images
|
|
scores = processor.score_retrieval(query_embeddings, image_embeddings)
|
|
|
|
print("Retrieval scores (query x image):")
|
|
print(scores)
|
|
```
|
|
|
|
## Notes
|
|
|
|
- [`~ColPaliProcessor.score_retrieval`] returns 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.
|
|
|
|
## ColPaliConfig
|
|
|
|
[[autodoc]] ColPaliConfig
|
|
|
|
## ColPaliProcessor
|
|
|
|
[[autodoc]] ColPaliProcessor
|
|
|
|
## ColPaliForRetrieval
|
|
|
|
[[autodoc]] ColPaliForRetrieval
|
|
- forward
|