Views
No views yet
| Component | Precision | Notes |
|---|---|---|
| Vision Encoder (ViT) | BF16 | Preserved for accuracy |
| LLM Decoder Layers | FP8 | Quantized for efficiency |
| Embeddings | BF16 | Preserved |
1from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
2from llmcompressor import oneshot
3from llmcompressor.modifiers.quantization import QuantizationModifier
4# Load model
5model = Qwen3VLForConditionalGeneration.from_pretrained(
6 "Qwen/Qwen3-VL-Embedding-2B",
7 torch_dtype=torch.bfloat16,
8 trust_remote_code=True,
9 device_map="auto",
10)
11# FP8 quantization recipe (data-free)
12recipe = QuantizationModifier(
13 targets="Linear",
14 scheme="FP8_DYNAMIC",
15 ignore=[
16 "lm_head",
17 r"re:model\.visual\..*", # Keep vision encoder in BF16
18 ]
19)
20# Apply quantization
21oneshot(model=model, recipe=recipe)
22# Save
23model.save_pretrained("Qwen3-VL-Embedding-2B-FP8", save_compressed=True)1transformers>=4.57.0
2qwen-vl-utils>=0.0.14
3torch==2.8.0
4llmcompressor==0.9.0.21from scripts.qwen3_vl_embedding import Qwen3VLEmbedder
2import numpy as np
3import torch
4
5# Define a list of query texts
6queries = [
7 {"text": "Visible embers scatter across the ground."}, # Fire prompt
8 {"text": "Routine scene with no disturbances."}, # Normal prompt
9]
10
11# Define a list of document (images, texts, videos)
12documents = [
13 {"text": "A woman shares a joyful moment with her golden retriever on a sun-drenched beach at sunset, as the dog offers its paw in a heartwarming display of companionship and trust."},
14 {"image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"},
15 {"video": "video.mp4"}
16]
17
18# Initialize the Qwen3VLEmbedder model
19model_name_or_path = "PIA-SPACE-LAB/Qwen3-VL-Embedding-2B-FP8"
20model = Qwen3VLEmbedder(model_name_or_path=model_name_or_path, max_frames=8, fps=8)
21
22# Combine queries and documents into a single input list
23inputs = queries + documents
24
25# Process the inputs to get embeddings
26embeddings = model.process(inputs)
27
28# Compute similarity scores between query embeddings and document embeddings
29similarity_scores = (embeddings[:4] @ embeddings[4:].T)
30
31# Print out the similarity scores in a list format
32print(similarity_scores.tolist())