模型概述
模型特點
模型能力
使用案例
🚀 Qwen3-Embedding-4B GGUF模型
Qwen3-Embedding-4B GGUF模型是專為文本嵌入和排序任務設計的模型,基於Qwen3系列的密集基礎模型構建,在多語言文本處理和代碼檢索等方面具有出色表現。
🚀 快速開始
環境要求
使用該模型前,請確保你的transformers
版本大於等於4.51.0,否則可能會遇到如下錯誤:
KeyError: 'qwen3'
代碼示例
Sentence Transformers使用示例
# Requires transformers>=4.51.0
from sentence_transformers import SentenceTransformer
# Load the model
model = SentenceTransformer("Qwen/Qwen3-Embedding-4B")
# We recommend enabling flash_attention_2 for better acceleration and memory saving,
# together with setting `padding_side` to "left":
# model = SentenceTransformer(
# "Qwen/Qwen3-Embedding-4B",
# model_kwargs={"attn_implementation": "flash_attention_2", "device_map": "auto"},
# tokenizer_kwargs={"padding_side": "left"},
# )
# The queries and documents to embed
queries = [
"What is the capital of China?",
"Explain gravity",
]
documents = [
"The capital of China is Beijing.",
"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.",
]
# Encode the queries and documents. Note that queries benefit from using a prompt
# Here we use the prompt called "query" stored under `model.prompts`, but you can
# also pass your own prompt via the `prompt` argument
query_embeddings = model.encode(queries, prompt_name="query")
document_embeddings = model.encode(documents)
# Compute the (cosine) similarity between the query and document embeddings
similarity = model.similarity(query_embeddings, document_embeddings)
print(similarity)
# tensor([[0.7534, 0.1147],
# [0.0320, 0.6258]])
Transformers使用示例
# Requires transformers>=4.51.0
import torch
import torch.nn.functional as F
from torch import Tensor
from transformers import AutoTokenizer, AutoModel
def last_token_pool(last_hidden_states: Tensor,
attention_mask: Tensor) -> Tensor:
left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
if left_padding:
return last_hidden_states[:, -1]
else:
sequence_lengths = attention_mask.sum(dim=1) - 1
batch_size = last_hidden_states.shape[0]
return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
def get_detailed_instruct(task_description: str, query: str) -> str:
return f'Instruct: {task_description}\nQuery:{query}'
# Each query must come with a one-sentence instruction that describes the task
task = 'Given a web search query, retrieve relevant passages that answer the query'
queries = [
get_detailed_instruct(task, 'What is the capital of China?'),
get_detailed_instruct(task, 'Explain gravity')
]
# No need to add instruction for retrieval documents
documents = [
"The capital of China is Beijing.",
"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."
]
input_texts = queries + documents
tokenizer = AutoTokenizer.from_pretrained('Qwen/Qwen3-Embedding-4B', padding_side='left')
model = AutoModel.from_pretrained('Qwen/Qwen3-Embedding-4B')
# We recommend enabling flash_attention_2 for better acceleration and memory saving.
# model = AutoModel.from_pretrained('Qwen/Qwen3-Embedding-4B', attn_implementation="flash_attention_2", torch_dtype=torch.float16).cuda()
max_length = 8192
# Tokenize the input texts
batch_dict = tokenizer(
input_texts,
padding=True,
truncation=True,
max_length=max_length,
return_tensors="pt",
)
batch_dict.to(model.device)
outputs = model(**batch_dict)
embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
# normalize embeddings
embeddings = F.normalize(embeddings, p=2, dim=1)
scores = (embeddings[:2] @ embeddings[2:].T)
print(scores.tolist())
# [[0.7534257769584656, 0.1146894246339798], [0.03198453038930893, 0.6258305311203003]]
💡 使用建議:
- 建議開發者根據具體場景、任務和語言自定義
instruct
。測試表明,在大多數檢索場景中,查詢端不使用instruct
會導致檢索性能下降約1% - 5%。- 推薦啟用
flash_attention_2
以獲得更好的加速和內存節省效果,並將padding_side
設置為"left"。
✨ 主要特性
卓越的通用性
嵌入模型在廣泛的下游應用評估中達到了最先進的性能。8B大小的嵌入模型在MTEB多語言排行榜上排名第一(截至2025年6月5日,得分70.58),而重排序模型在各種文本檢索場景中表現出色。
全面的靈活性
Qwen3 Embedding系列為嵌入和重排序模型提供了全範圍的大小(從0.6B到8B),滿足了優先考慮效率和效果的各種用例。開發者可以無縫組合這兩個模塊。此外,嵌入模型允許在所有維度上靈活定義向量,並且嵌入和重排序模型都支持用戶定義的指令,以提高特定任務、語言或場景的性能。
多語言能力
由於Qwen3模型的多語言能力,Qwen3 Embedding系列支持100多種語言,包括各種編程語言,並提供強大的多語言、跨語言和代碼檢索能力。
📦 安裝指南
文檔未提及具體安裝步驟,可參考transformers官方文檔進行安裝。
💻 使用示例
上述快速開始部分已給出Sentence Transformers和Transformers的使用示例。
📚 詳細文檔
模型概述
Qwen3-Embedding-4B 具有以下特點:
屬性 | 詳情 |
---|---|
模型類型 | 文本嵌入 |
支持語言 | 100多種語言 |
參數數量 | 4B |
上下文長度 | 32k |
嵌入維度 | 最大2560,支持用戶定義的32到2560的輸出維度 |
更多詳細信息,包括基準評估、硬件要求和推理性能,請參考我們的博客、GitHub。
Qwen3 Embedding系列模型列表
模型類型 | 模型 | 大小 | 層數 | 序列長度 | 嵌入維度 | MRL支持 | 指令感知 |
---|---|---|---|---|---|---|---|
文本嵌入 | Qwen3-Embedding-0.6B | 0.6B | 28 | 32K | 1024 | 是 | 是 |
文本嵌入 | Qwen3-Embedding-4B | 4B | 36 | 32K | 2560 | 是 | 是 |
文本嵌入 | Qwen3-Embedding-8B | 8B | 36 | 32K | 4096 | 是 | 是 |
文本重排序 | Qwen3-Reranker-0.6B | 0.6B | 28 | 32K | - | - | 是 |
文本重排序 | Qwen3-Reranker-4B | 4B | 36 | 32K | - | - | 是 |
文本重排序 | Qwen3-Reranker-8B | 8B | 36 | 32K | - | - | 是 |
注意:
MRL支持
表示嵌入模型是否支持最終嵌入的自定義維度。指令感知
表示嵌入或重排序模型是否支持根據不同任務自定義輸入指令。- 我們的評估表明,對於大多數下游任務,使用指令(instruct)通常比不使用指令提高1%到5%的性能。因此,建議開發者為其任務和場景創建量身定製的指令。在多語言環境中,也建議用戶用英語編寫指令,因為模型訓練過程中使用的大多數指令最初是用英語編寫的。
模型格式選擇
選擇正確的模型格式取決於你的硬件能力和內存限制。以下是不同模型格式的介紹:
BF16(Brain Float 16)
- 一種16位浮點格式,專為更快的計算而設計,同時保持良好的精度。
- 提供與FP32相似的動態範圍,但內存使用更低。
- 如果你的硬件支持BF16加速(檢查設備規格),則推薦使用。
- 與FP32相比,適用於具有減少內存佔用的高性能推理。
使用場景:
- 你的硬件具有原生BF16支持(例如,較新的GPU、TPU)。
- 你希望在節省內存的同時獲得更高的精度。
- 你計劃將模型重新量化為另一種格式。
避免場景:
- 你的硬件不支持BF16(可能會回退到FP32並運行較慢)。
- 你需要與缺乏BF16優化的舊設備兼容。
F16(Float 16)
- 一種16位浮點格式,具有高精度,但值的範圍比BF16小。
- 適用於大多數支持FP16加速的設備(包括許多GPU和一些CPU)。
- 數值精度略低於BF16,但通常足以進行推理。
使用場景:
- 你的硬件支持FP16但不支持BF16。
- 你需要在速度、內存使用和準確性之間取得平衡。
- 你在GPU或其他針對FP16計算優化的設備上運行。
避免場景:
- 你的設備缺乏原生FP16支持(可能運行比預期慢)。
- 你有內存限制。
混合精度模型(例如,bf16_q8_0
,f16_q4_K
)
這些格式選擇性地對非關鍵層進行量化,同時保持關鍵層的全精度(例如,注意力和輸出層)。
- 命名方式如
bf16_q8_0
(表示全精度BF16核心層 + 量化Q8_0其他層)。 - 在內存效率和準確性之間取得平衡,比完全量化的模型有所改進,而不需要BF16/F16的全部內存。
使用場景:
- 你需要比僅量化模型更好的準確性,但無法在所有地方使用全BF16/F16。
- 你的設備支持混合精度推理。
- 你希望在受限硬件上優化生產級模型的權衡。
避免場景:
- 你的目標設備不支持混合或全精度加速。
- 你在超嚴格的內存限制下操作(在這種情況下使用完全量化的格式)。
量化模型(Q4_K,Q6_K,Q8等)
量化在儘可能保持準確性的同時減小模型大小和內存使用。
- 低比特模型(Q4_K):最適合最小的內存使用,可能精度較低。
- 高比特模型(Q6_K,Q8_0):更好的準確性,需要更多的內存。
使用場景:
- 你在CPU上運行推理,需要優化的模型。
- 你的設備具有低VRAM,無法加載全精度模型。
- 你希望在保持合理準確性的同時減少內存佔用。
避免場景:
- 你需要最大的準確性(全精度模型更適合此需求)。
- 你的硬件有足夠的VRAM用於更高精度的格式(BF16/F16)。
極低比特量化(IQ3_XS,IQ3_S,IQ3_M,Q4_K,Q4_0)
這些模型針對非常高的內存效率進行了優化,使其非常適合低功耗設備或內存是關鍵限制的大規模部署。
- IQ3_XS:超低比特量化(3位),具有非常高的內存效率。適用於超低內存設備,即使Q4_K也太大的情況。準確性較低。
- IQ3_S:小塊大小,以實現最大的內存效率。適用於低內存設備,IQ3_XS過於激進的情況。
- IQ3_M:中等塊大小,比IQ3_S具有更好的準確性。適用於低內存設備,IQ3_S過於受限的情況。
- Q4_K:4位量化,具有逐塊優化,以提高準確性。適用於低內存設備,Q6_K太大的情況。
- Q4_0:純4位量化,針對ARM設備進行了優化。適用於基於ARM的設備或低內存環境。
超極低比特量化(IQ1_S,IQ1_M,IQ2_S,IQ2_M,IQ2_XS,IQ2_XSS)
- 超極低比特量化(1 - 2位),具有極高的內存效率。
- 使用場景:適用於必須將模型裝入非常受限內存的情況。
- 權衡:準確性非常低,可能無法按預期運行。使用前請充分測試。
模型格式選擇總結表
模型格式 | 精度 | 內存使用 | 設備要求 | 最佳用例 |
---|---|---|---|---|
BF16 | 非常高 | 高 | 支持BF16的GPU/CPU | 具有減少內存的高速推理 |
F16 | 高 | 高 | 支持FP16的GPU/CPU | BF16不可用時的推理 |
Q4_K | 中低 | 低 | CPU或低VRAM設備 | 內存受限的推理 |
Q6_K | 中等 | 適中 | 內存較多的CPU | 量化時更好的準確性 |
Q8_0 | 高 | 適中 | 具有適中VRAM的GPU/CPU | 量化模型中最高的準確性 |
IQ3_XS | 低 | 非常低 | 超低內存設備 | 最大內存效率,低準確性 |
IQ3_S | 低 | 非常低 | 低內存設備 | 比IQ3_XS略更可用 |
IQ3_M | 中低 | 低 | 低內存設備 | 比IQ3_S更好的準確性 |
Q4_0 | 低 | 低 | 基於ARM/嵌入式設備 | Llama.cpp自動優化ARM推理 |
超極低比特(IQ1/2_*) | 非常低 | 極低 | 小型邊緣/嵌入式設備 | 將模型裝入極其受限的內存;低準確性 |
混合(例如,bf16_q8_0 ) |
中高 | 適中 | 支持混合精度的硬件 | 平衡性能和內存,關鍵層接近FP準確性 |
🔧 技術細節
模型生成細節
該模型使用llama.cpp在提交版本1f63e75f
生成。
超越IMatrix的量化
測試一種新的量化方法,使用規則將重要層的量化級別提高到標準IMatrix使用的級別之上。發現標準IMatrix在低比特量化和MOE模型中表現不佳,因此使用llama.cpp的--tensor-type
來提高選定層的量化級別。這會創建更大的模型文件,但會提高給定模型大小的精度。
評估結果
MTEB(多語言)
模型 | 大小 | 平均(任務) | 平均(類型) | 雙語挖掘 | 分類 | 聚類 | 指令檢索 | 多分類 | 成對分類 | 重排序 | 檢索 | STS |
---|---|---|---|---|---|---|---|---|---|---|---|---|
NV-Embed-v2 | 7B | 56.29 | 49.58 | 57.84 | 57.29 | 40.80 | 1.04 | 18.63 | 78.94 | 63.82 | 56.72 | 71.10 |
GritLM-7B | 7B | 60.92 | 53.74 | 70.53 | 61.83 | 49.75 | 3.45 | 22.77 | 79.94 | 63.78 | 58.31 | 73.33 |
BGE-M3 | 0.6B | 59.56 | 52.18 | 79.11 | 60.35 | 40.88 | -3.11 | 20.1 | 80.76 | 62.79 | 54.60 | 74.12 |
multilingual-e5-large-instruct | 0.6B | 63.22 | 55.08 | 80.13 | 64.94 | 50.75 | -0.40 | 22.91 | 80.86 | 62.61 | 57.12 | 76.81 |
gte-Qwen2-1.5B-instruct | 1.5B | 59.45 | 52.69 | 62.51 | 58.32 | 52.05 | 0.74 | 24.02 | 81.58 | 62.58 | 60.78 | 71.61 |
gte-Qwen2-7b-Instruct | 7B | 62.51 | 55.93 | 73.92 | 61.55 | 52.77 | 4.94 | 25.48 | 85.13 | 65.55 | 60.08 | 73.98 |
text-embedding-3-large | - | 58.93 | 51.41 | 62.17 | 60.27 | 46.89 | -2.68 | 22.03 | 79.17 | 63.89 | 59.27 | 71.68 |
Cohere-embed-multilingual-v3.0 | - | 61.12 | 53.23 | 70.50 | 62.95 | 46.89 | -1.89 | 22.74 | 79.88 | 64.07 | 59.16 | 74.80 |
gemini-embedding-exp-03-07 | - | 68.37 | 59.59 | 79.28 | 71.82 | 54.59 | 5.18 | 29.16 | 83.63 | 65.58 | 67.71 | 79.40 |
Qwen3-Embedding-0.6B | 0.6B | 64.33 | 56.00 | 72.22 | 66.83 | 52.33 | 5.09 | 24.59 | 80.83 | 61.41 | 64.64 | 76.17 |
Qwen3-Embedding-4B | 4B | 69.45 | 60.86 | 79.36 | 72.33 | 57.15 | 11.56 | 26.77 | 85.05 | 65.08 | 69.60 | 80.86 |
Qwen3-Embedding-8B | 8B | 70.58 | 61.69 | 80.89 | 74.00 | 57.65 | 10.06 | 28.66 | 86.40 | 65.63 | 70.88 | 81.08 |
注意:對於比較的模型,分數從MTEB在線排行榜獲取。
📄 許可證
本項目採用Apache-2.0許可證。







