Views
No views yet

| Model Type | Models | Size | Layers | Sequence Length | Embedding Dimension | MRL Support | Instruction Aware |
|---|---|---|---|---|---|---|---|
| Text Embedding | Qwen3-Embedding-0.6B | 0.6B | 28 | 32K | 1024 | Yes | Yes |
| Text Embedding | Qwen3-Embedding-4B | 4B | 36 | 32K | 2560 | Yes | Yes |
| Text Embedding | Qwen3-Embedding-8B | 8B | 36 | 32K | 4096 | Yes | Yes |
| Text Reranking | Qwen3-Reranker-0.6B | 0.6B | 28 | 32K | - | - | Yes |
| Text Reranking | Qwen3-Reranker-4B | 4B | 36 | 32K | - | - | Yes |
| Text Reranking | Qwen3-Reranker-8B | 8B | 36 | 32K | - | - | Yes |
Note:
MRL Supportindicates whether the embedding model supports custom dimensions for the final embedding.Instruction Awarenotes whether the embedding or reranking model supports customizing the input instruction according to different tasks.- Our evaluation indicates that, for most downstream tasks, using instructions (instruct) typically yields an improvement of 1% to 5% compared to not using them. Therefore, we recommend that developers create tailored instructions specific to their tasks and scenarios. In multilingual contexts, we also advise users to write their instructions in English, as most instructions utilized during the model training process were originally written in English.
KeyError: 'qwen3'1# Requires transformers>=4.51.0
2import torch
3from transformers import AutoModel, AutoTokenizer, AutoModelForCausalLM
4
5def format_instruction(instruction, query, doc):
6 if instruction is None:
7 instruction = 'Given a web search query, retrieve relevant passages that answer the query'
8 output = "<Instruct>: {instruction}\n<Query>: {query}\n<Document>: {doc}".format(instruction=instruction,query=query, doc=doc)
9 return output
10
11def process_inputs(pairs):
12 inputs = tokenizer(
13 pairs, padding=False, truncation='longest_first',
14 return_attention_mask=False, max_length=max_length - len(prefix_tokens) - len(suffix_tokens)
15 )
16 for i, ele in enumerate(inputs['input_ids']):
17 inputs['input_ids'][i] = prefix_tokens + ele + suffix_tokens
18 inputs = tokenizer.pad(inputs, padding=True, return_tensors="pt", max_length=max_length)
19 for key in inputs:
20 inputs[key] = inputs[key].to(model.device)
21 return inputs
22
23@torch.no_grad()
24def compute_logits(inputs, **kwargs):
25 batch_scores = model(**inputs).logits[:, -1, :]
26 true_vector = batch_scores[:, token_true_id]
27 false_vector = batch_scores[:, token_false_id]
28 batch_scores = torch.stack([false_vector, true_vector], dim=1)
29 batch_scores = torch.nn.functional.log_softmax(batch_scores, dim=1)
30 scores = batch_scores[:, 1].exp().tolist()
31 return scores
32
33tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-Reranker-0.6B", padding_side='left')
34model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-Reranker-0.6B").eval()
35# We recommend enabling flash_attention_2 for better acceleration and memory saving.
36# model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-Reranker-0.6B", torch_dtype=torch.float16, attn_implementation="flash_attention_2").cuda().eval()
37token_false_id = tokenizer.convert_tokens_to_ids("no")
38token_true_id = tokenizer.convert_tokens_to_ids("yes")
39max_length = 8192
40
41prefix = "<|im_start|>system\nJudge whether the Document meets the requirements based on the Query and the Instruct provided. Note that the answer can only be \"yes\" or \"no\".<|im_end|>\n<|im_start|>user\n"
42suffix = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
43prefix_tokens = tokenizer.encode(prefix, add_special_tokens=False)
44suffix_tokens = tokenizer.encode(suffix, add_special_tokens=False)
45
46task = 'Given a web search query, retrieve relevant passages that answer the query'
47
48queries = ["What is the capital of China?",
49 "Explain gravity",
50]
51
52documents = [
53 "The capital of China is Beijing.",
54 "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun.",
55]
56
57pairs = [format_instruction(task, query, doc) for query, doc in zip(queries, documents)]
58
59# Tokenize the input texts
60inputs = process_inputs(pairs)
61scores = compute_logits(inputs)
62
63print("scores: ", scores)1# Requires vllm>=0.8.5
2import logging
3from typing import Dict, Optional, List
4
5import json
6import logging
7
8import torch
9
10from transformers import AutoTokenizer, is_torch_npu_available
11from vllm import LLM, SamplingParams
12from vllm.distributed.parallel_state import destroy_model_parallel
13import gc
14import math
15from vllm.inputs.data import TokensPrompt
16
17
18
19def format_instruction(instruction, query, doc):
20 text = [
21 {"role": "system", "content": "Judge whether the Document meets the requirements based on the Query and the Instruct provided. Note that the answer can only be \"yes\" or \"no\"."},
22 {"role": "user", "content": f"<Instruct>: {instruction}\n\n<Query>: {query}\n\n<Document>: {doc}"}
23 ]
24 return text
25
26def process_inputs(pairs, instruction, max_length, suffix_tokens):
27 messages = [format_instruction(instruction, query, doc) for query, doc in pairs]
28 messages = tokenizer.apply_chat_template(
29 messages, tokenize=True, add_generation_prompt=False, enable_thinking=False
30 )
31 messages = [ele[:max_length] + suffix_tokens for ele in messages]
32 messages = [TokensPrompt(prompt_token_ids=ele) for ele in messages]
33 return messages
34
35def compute_logits(model, messages, sampling_params, true_token, false_token):
36 outputs = model.generate(messages, sampling_params, use_tqdm=False)
37 scores = []
38 for i in range(len(outputs)):
39 final_logits = outputs[i].outputs[0].logprobs[-1]
40 token_count = len(outputs[i].outputs[0].token_ids)
41 if true_token not in final_logits:
42 true_logit = -10
43 else:
44 true_logit = final_logits[true_token].logprob
45 if false_token not in final_logits:
46 false_logit = -10
47 else:
48 false_logit = final_logits[false_token].logprob
49 true_score = math.exp(true_logit)
50 false_score = math.exp(false_logit)
51 score = true_score / (true_score + false_score)
52 scores.append(score)
53 return scores
54
55number_of_gpu = torch.cuda.device_count()
56tokenizer = AutoTokenizer.from_pretrained('Qwen/Qwen3-Reranker-0.6B')
57model = LLM(model='Qwen/Qwen3-Reranker-0.6B', tensor_parallel_size=number_of_gpu, max_model_len=10000, enable_prefix_caching=True, gpu_memory_utilization=0.8)
58tokenizer.padding_side = "left"
59tokenizer.pad_token = tokenizer.eos_token
60suffix = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
61max_length=8192
62suffix_tokens = tokenizer.encode(suffix, add_special_tokens=False)
63true_token = tokenizer("yes", add_special_tokens=False).input_ids[0]
64false_token = tokenizer("no", add_special_tokens=False).input_ids[0]
65sampling_params = SamplingParams(temperature=0,
66 max_tokens=1,
67 logprobs=20,
68 allowed_token_ids=[true_token, false_token],
69)
70
71
72task = 'Given a web search query, retrieve relevant passages that answer the query'
73queries = ["What is the capital of China?",
74 "Explain gravity",
75]
76documents = [
77 "The capital of China is Beijing.",
78 "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun.",
79]
80
81pairs = list(zip(queries, documents))
82inputs = process_inputs(pairs, task, max_length-len(suffix_tokens), suffix_tokens)
83scores = compute_logits(model, inputs, sampling_params, true_token, false_token)
84print('scores', scores)
85
86destroy_model_parallel()instruct according to their specific scenarios, tasks, and languages. Our tests have shown that in most retrieval scenarios, not using an instruct on the query side can lead to a drop in retrieval performance by approximately 1% to 5%.| Model | Param | MTEB-R | CMTEB-R | MMTEB-R | MLDR | MTEB-Code | FollowIR |
|---|---|---|---|---|---|---|---|
| Qwen3-Embedding-0.6B | 0.6B | 61.82 | 71.02 | 64.64 | 50.26 | 75.41 | 5.09 |
| Jina-multilingual-reranker-v2-base | 0.3B | 58.22 | 63.37 | 63.73 | 39.66 | 58.98 | -0.68 |
| gte-multilingual-reranker-base | 0.3B | 59.51 | 74.08 | 59.44 | 66.33 | 54.18 | -1.64 |
| BGE-reranker-v2-m3 | 0.6B | 57.03 | 72.16 | 58.36 | 59.51 | 41.38 | -0.01 |
| Qwen3-Reranker-0.6B | 0.6B | 65.80 | 71.31 | 66.36 | 67.28 | 73.42 | 5.41 |
| Qwen3-Reranker-4B | 1.7B | 69.76 | 75.94 | 72.74 | 69.97 | 81.20 | 14.84 |
| Qwen3-Reranker-8B | 8B | 69.02 | 77.45 | 72.94 | 70.19 | 81.22 | 8.05 |
Note:
- Evaluation results for reranking models. We use the retrieval subsets of MTEB(eng, v2), MTEB(cmn, v1), MMTEB and MTEB (Code), which are MTEB-R, CMTEB-R, MMTEB-R and MTEB-Code.
- All scores are our runs based on the top-100 candidates retrieved by dense embedding model Qwen3-Embedding-0.6B.
@misc{qwen3-embedding,
title = {Qwen3-Embedding},
url = {https://qwenlm.github.io/blog/qwen3/},
author = {Qwen Team},
month = {May},
year = {2025}
}