Views
No views yet


1pip install torch==2.5.0 torchvision==0.20.0 torchaudio==2.5.0
2pip install flash-attn --no-build-isolation
3pip install transformers==4.47.1
4pip install qwen-vl-utils[decord]==0.0.81# get inference code from https://huggingface.co/friedrichor/Unite-Base-Qwen2-VL-2B/tree/main/inference_demo
2cd inference_demo1import torch
2from transformers import AutoTokenizer, AutoProcessor
3from qwen_vl_utils import process_vision_info
4from modeling_unite import UniteQwen2VL
5
6model_path = 'friedrichor/Unite-Instruct-Qwen2-VL-2B'
7model = UniteQwen2VL.from_pretrained(
8 model_path,
9 device_map="cuda",
10 torch_dtype=torch.bfloat16,
11)
12
13# We recommend enabling flash_attention_2 for better acceleration and memory saving.
14# model = UniteQwen2VL.from_pretrained(
15# model_path,
16# device_map="cuda",
17# torch_dtype=torch.bfloat16,
18# attn_implementation='flash_attention_2',
19# low_cpu_mem_usage=True,
20# )
21
22tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
23processor = AutoProcessor.from_pretrained(model_path, min_pixels=256*28*28, max_pixels=1280*28*28)
24
25def process_messages(msg):
26 text = processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True) + "<|endoftext|>"
27 image_inputs, video_inputs = process_vision_info(msg)
28 inputs = processor(
29 text=[text],
30 images=image_inputs,
31 videos=video_inputs,
32 padding=True,
33 return_tensors="pt",
34 )
35 inputs = inputs.to("cuda")
36
37 return inputs1messages_txt = [
2 {
3 "role": "user",
4 "content": [
5 {"type": "text", "text": "The book titled 'Riding with Reindeer - A Bicycle Odyssey through Finland, Lapland, and the Arctic' provides a detailed account of a journey that explores the regions of Lapland and the Arctic, focusing on the experience of riding with reindeer."},
6 {"type": "text", "text": "\nSummary above sentence in one word:"},
7 ],
8 }
9]
10
11messages_img = [
12 {
13 "role": "user",
14 "content": [
15 {"type": "image", "image": "./examples/518L0uDGe0L.jpg"},
16 {"type": "text", "text": "\nSummary above image in one word:"},
17 ],
18 }
19]
20
21inputs_txt = process_messages(messages_txt)
22inputs_img = process_messages(messages_img)
23
24with torch.no_grad():
25 embeddings_txt = model(**inputs_txt) # [1, 1536]
26 embeddings_img = model(**inputs_img) # [1, 1536]
27
28 print(torch.matmul(embeddings_txt, embeddings_img.T))
29 # tensor([[0.7266]], dtype=torch.bfloat16)1messages_txt = [
2 {
3 "role": "user",
4 "content": [
5 {"type": "text", "text": "Timelapse of stormy clouds over open sea and snowcapped mountain"},
6 {"type": "text", "text": "\nSummary above sentence in one word:"},
7 ],
8 }
9]
10
11messages_vid = [
12 {
13 "role": "user",
14 "content": [
15 {
16 "type": "video",
17 "video": "./examples/stock-footage-timelapse-of-stormy-clouds-over-open-sea-and-snowcapped-mountain.mp4",
18 "max_pixels": 360 * 420,
19 "fps": 1,
20 "max_frames": 32
21 },
22 {"type": "text", "text": "\nSummary above video in one word:"},
23 ],
24 }
25]
26
27inputs_txt = process_messages(messages_txt)
28inputs_vid = process_messages(messages_vid)
29
30with torch.no_grad():
31 embeddings_txt = model(**inputs_txt) # [1, 1536]
32 embeddings_vid = model(**inputs_vid) # [1, 1536]
33
34 print(torch.matmul(embeddings_txt, embeddings_vid.T))
35 # tensor([[0.5234]], dtype=torch.bfloat16)1messages_qry = [
2 {
3 "role": "user",
4 "content": [
5 {"type": "image", "image": "./examples/oven_05011373.jpg"},
6 {"type": "text", "text": "What is the name of this place?"},
7 {"type": "text", "text": "\nSummary above sentence and image in one word:"},
8 ],
9 }
10]
11
12messages_tgt = [
13 {
14 "role": "user",
15 "content": [
16 {"type": "image", "image": "./examples/Q673659.jpg"},
17 {"type": "text", "text": "Marina Beach."},
18 {"type": "text", "text": "\nSummary above sentence and image in one word:"},
19 ],
20 }
21]
22
23inputs_qry = process_messages(messages_qry)
24inputs_tgt = process_messages(messages_tgt)
25
26with torch.no_grad():
27 embeddings_qry = model(**inputs_qry) # [1, 1536]
28 embeddings_tgt = model(**inputs_tgt) # [1, 1536]
29
30 print(torch.matmul(embeddings_qry, embeddings_tgt.T))
31 # tensor([[0.5977]], dtype=torch.bfloat16)@article{kong2025modality,
title={Modality Curation: Building Universal Embeddings for Advanced Multimodal Information Retrieval},
author={Kong, Fanheng and Zhang, Jingyuan and Liu, Yahui and Zhang, Hongzhi and Feng, Shi and Yang, Xiaocui and Wang, Daling and Tian, Yu and W., Victoria and Zhang, Fuzheng and Zhou, Guorui},
journal={arXiv preprint arXiv:2505.19650},
year={2025}
}