Qwen3-VL-Embedding and Qwen3-VL-Reranker

repository·main·Indexed 23 days ago

https://github.com/qwenlm/qwen3-vl-embedding

Official implementation of Qwen3-VL-Embedding and Qwen3-VL-Reranker, multimodal models built on the Qwen3-VL foundation. The series supports text, images, screenshots, and videos for information retrieval and cross-modal understanding. It features a two-stage pipeline: a Dual-Tower embedding model for efficient initial recall and a Single-Tower reranker for precise relevance scoring. Available in 2B and 8B parameter versions, with support for vLLM accelerated inference.

Tokens
9.4K
Snippets
17
Records
46
Agent score
80%

What's inside qwen3-vl-embedding

  1. Compare Qwen3-VL-Embedding and Qwen3-VL-Reranker

    main
    FeatureQwen3-VL-EmbeddingQwen3-VL-Reranker
    Core FunctionSemantic Representation, Embedding GenerationRelevance Scoring, Pointwise Re-ranking
    InputSingle modality or mixed modalities(Query, Document) pair with single- or mixed-modal inputs
    ArchitectureDual-TowerSingle-Tower
    MechanismEfficient RetrievalDeep Inter-Modal Interaction, Precise Alignment
    OutputSemantic VectorRelevance Score
  2. How Qwen3-VL-Embedding and Qwen3-VL-Reranker work together

    main

    The Qwen3-VL model series is designed for multimodal information retrieval using a two-stage pipeline:

    1. Recall Stage (Embedding Model): The Qwen3-VL-Embedding model (Dual-Tower architecture) generates semantically rich vectors from single or mixed-modal inputs (text, images, screenshots, videos). These vectors are used for efficient initial retrieval.
    2. Reranking Stage (Reranker Model): The Qwen3-VL-Reranker model (Single-Tower architecture) takes (Query, Document) pairs and performs pointwise reranking. It uses cross-attention for deep inter-modal interaction and outputs a precise relevance score by predicting the generation probability of special tokens (yes and no).
  3. Download Qwen3-VL models from ModelScope

    main

    To download models from ModelScope, install modelscope using uv and use the modelscope CLI.

    Example for Qwen3-VL-Embedding-2B:

    uv pip install modelscope
    
    modelscope download --model qwen/Qwen3-VL-Embedding-2B --local_dir ./models/Qwen3-VL-Embedding-2B
    uv pip install modelscope
    
    modelscope download --model qwen/Qwen3-VL-Embedding-2B --local_dir ./models/Qwen3-VL-Embedding-2B
  4. Quick Start: Using the Embedding Model with Transformers

    main

    To generate embeddings for text, images, or multimodal inputs using the Qwen3VLEmbedder class, provide a list of dictionaries where each dictionary represents a multimodal object. You can include text, image, and video keys. For text-only tasks, you can optionally include an instruction to guide the embedding representation.

    Supported input types for image and video include local file paths, URLs, or PIL.Image.Image instances. All input types support both single objects and lists of objects.

    import torch
    from src.models.qwen3_vl_embedding import Qwen3VLEmbedder
    
    model = Qwen3VLEmbedder(
        model_name_or_path="./models/Qwen3-VL-Embedding-2B",
        # flash_attention_2 for better acceleration and memory saving
        # torch_dtype=torch.bfloat16, 
        # attn_implementation="flash_attention_2"
    )
    
    inputs = [{
        "text": "A woman playing with her dog on a beach at sunset.",
        "instruction": "Retrieve images or text relevant to the user's query.",
    }, {
        "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."
    }, {
        "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
    }, {
        "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.", 
        "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
    }]
    
    embeddings = model.process(inputs)
    print(embeddings @ embeddings.T)
  5. Use vLLM for Embedding and Reranking

    main

    The models support vLLM for accelerated inference.

    Requirements:

    • Requires vLLM >= 0.14.0.

    Usage:

    • For embedding model examples with vLLM, see examples/embedding_vllm.ipynb.
    • For reranker model examples with vLLM, see examples/reranker_vllm.ipynb.
  6. Reproduce Embedding Model evaluation

    main

    To reproduce the evaluation results for the Embedding Model on the MMEB v2 benchmark, follow these steps:

    1. Download the evaluation data:

      bash data/evaluation/mmeb_v2/download_data.sh
    2. Run the evaluation script:

      bash scripts/evaluation/mmeb_v2/eval_embedding.sh

      Running the script without arguments will display the required parameters. The script automatically evaluates tasks and collects results.

    bash data/evaluation/mmeb_v2/download_data.sh
    bash scripts/evaluation/mmeb_v2/eval_embedding.sh
  7. Install Qwen3-VL-Embedding and setup environment

    main

    To install the project, clone the repository and run the provided setup script which uses uv to manage dependencies.

    # Clone the repository
    git clone https://github.com/QwenLM/Qwen3-VL-Embedding.git
    cd Qwen3-VL-Embedding
    
    # Run the script to setup the environment
    bash scripts/setup_environment.sh
    
    # Activate the environment
    source .venv/bin/activate
    git clone https://github.com/QwenLM/Qwen3-VL-Embedding.git
    cd Qwen3-VL-Embedding
    
    bash scripts/setup_environment.sh
    
    source .venv/bin/activate
  8. Download Qwen3-VL models from Hugging Face

    main

    To download models from Hugging Face, install huggingface-hub using uv and use the huggingface-cli.

    Example for Qwen3-VL-Embedding-2B:

    uv pip install huggingface-hub
    
    huggingface-cli download Qwen/Qwen3-VL-Embedding-2B --local-dir ./models/Qwen3-VL-Embedding-2B
    uv pip install huggingface-hub
    
    huggingface-cli download Qwen/Qwen3-VL-Embedding-2B --local-dir ./models/Qwen3-VL-Embedding-2B
  9. Quick Start: Using the Reranking Model with Transformers

    main

    To perform reranking tasks, use the Qwen3VLReranker class. The input must be a dictionary containing a query (a multimodal object) and a list of documents (a list of multimodal objects). You can also provide an optional instruction for the task and video sampling settings (fps, max_frames).

    import torch
    from src.models.qwen3_vl_reranker import Qwen3VLReranker
    
    model = Qwen3VLReranker(
        model_name_or_path="./models/Qwen3-VL-Reranker-2B",
        # flash_attention_2 for better acceleration and memory saving
        # torch_dtype=torch.bfloat16, 
        # attn_implementation="flash_attention_2"
    )
    
    inputs = {
        "instruction": "Retrieve images or text relevant to the user's query.",
        "query": {"text": "A woman playing with her dog on a beach at sunset."},
        "documents": [
            {"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."},
            {"image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"},
            {"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.", 
             "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"}
        ],
        "fps": 1.0, 
        "max_frames": 64
    }
    
    scores = model.process(inputs)
    print(scores)
  10. Reproduce Reranking Model evaluation

    main

    To reproduce the evaluation results for the Reranking Model on the MMEB v2 retrieval split, follow these steps:

    1. Download the evaluation data:

      bash data/evaluation/mmeb_v2/download_data.sh
    2. Run the evaluation script:

      bash scripts/evaluation/mmeb_v2/eval_reranker.sh

      Running the script without arguments will display the required parameters. The script automatically evaluates tasks and collects results.

    bash data/evaluation/mmeb_v2/download_data.sh
    bash scripts/evaluation/mmeb_v2/eval_reranker.sh
  11. Prepare multimodal input data for reranking

    main

    The reranker accepts multimodal inputs (text and/or images) for both the query and the candidate documents.

    Input structure:

    • instruction: (Optional) A string describing the task.
    • query: A dictionary containing 'text' and/or 'image'.
    • documents: A list of dictionaries, where each dictionary contains 'text' and/or 'image'.

    Images can be provided as:

    1. A URL (string starting with http:// or https://).
    2. A local absolute file path (string).
    3. A PIL Image object.
  12. Evaluate Qwen3-VL Reranker models via CLI

    main

    The eval_reranker.py script is a distributed evaluation tool for reranker models. It takes TopK retrieval results (generated by an embedding model) and uses a Qwen3VLReranker to re-score and re-order them.

    To run the evaluation, use the script with arguments parsed from three main dataclasses: RerankArguments, DataArguments, and EvalArguments. The script supports distributed evaluation using PyTorch's dist (NCCL backend) and expects a dataset configuration YAML file.

    Workflow:

    1. Load Model: Loads the Qwen3VLReranker (supports flash_attention_2 and bfloat16).
    2. Load Retrieval Results: Reads *_pred.jsonl files from the embedding evaluation output directory to get the initial TopK candidate IDs.
    3. Rerank: Processes queries and their retrieved candidates in batches.
    4. Calculate Metrics: Computes ranking metrics (e.g., hit, ndcg, precision, recall, f1, map, mrr) using RankingMetrics.
    5. Save Results: Outputs a JSON file containing scores and a .jsonl file containing the reranked predictions.