Views
No views yet
Qwen/Qwen3-Embedding-0.6B.input_ids, attention_mask, and position_ids as int64
inputs, and returns last_hidden_state. For retrieval, use Qwen's query-side
instruction format, last-token pooling, and L2 normalization.onnx/model.onnxverify_onnxruntime.pyqwen_onnx_release_tools.pyonnxruntime-gpu==1.26.0.1optimum-cli export onnx `
2 --model Qwen/Qwen3-Embedding-0.6B `
3 --library-name transformers `
4 --task text-generation `
5 --trust-remote-code `
6 --opset 18 `
7 --device cuda `
8 --dtype fp16 `
9 --no-post-process `
10 qwen3-embedding-0.6b-onnx-fp161python -m pip install "huggingface_hub[cli]" onnx onnxruntime-gpu transformers numpy
2hf download icosahedron10/qwen3-embedding-0.6b-onnx-fp16 --local-dir qwen3-embedding-0.6b-onnx-fp16
3cd qwen3-embedding-0.6b-onnx-fp16
4python verify_onnxruntime.pyonnxruntime instead of onnxruntime-gpu and
change the provider in your own loader to CPUExecutionProvider.1import numpy as np
2import onnxruntime as ort
3from transformers import AutoTokenizer
4
5model_dir = "qwen3-embedding-0.6b-onnx-fp16"
6tokenizer = AutoTokenizer.from_pretrained(
7 model_dir,
8 local_files_only=True,
9 padding_side="left",
10 fix_mistral_regex=True,
11)
12
13session = ort.InferenceSession(
14 f"{model_dir}/onnx/model.onnx",
15 providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
16)
17
18instruction = "Given a web search query, retrieve relevant passages that answer the query"
19queries = [
20 f"Instruct: {instruction}\nQuery: How can I reset my PostgreSQL password?",
21 f"Instruct: {instruction}\nQuery: How do I make sourdough bread?",
22 f"Instruct: {instruction}\nQuery: Who wrote Pride and Prejudice?",
23]
24documents = [
25 "To reset a PostgreSQL password, connect as a superuser and run ALTER USER with a new password.",
26 "Sourdough bread is made by mixing flour, water, salt, and starter, then fermenting, shaping, proofing, and baking.",
27 "Jane Austen wrote the novel Pride and Prejudice.",
28]
29
30encoded = tokenizer(
31 queries + documents,
32 padding=True,
33 truncation=True,
34 max_length=512,
35 return_tensors="np",
36)
37
38input_ids = encoded["input_ids"].astype(np.int64)
39attention_mask = encoded["attention_mask"].astype(np.int64)
40position_ids = np.repeat(
41 np.arange(input_ids.shape[1], dtype=np.int64)[None, :],
42 input_ids.shape[0],
43 axis=0,
44)
45
46last_hidden_state = session.run(
47 None,
48 {
49 "input_ids": input_ids,
50 "attention_mask": attention_mask,
51 "position_ids": position_ids,
52 },
53)[0]
54
55sequence_lengths = attention_mask.sum(axis=1) - 1
56rows = np.arange(last_hidden_state.shape[0])
57embeddings = last_hidden_state[rows, sequence_lengths]
58embeddings = embeddings / np.linalg.norm(embeddings, axis=1, keepdims=True)
59
60similarities = embeddings[:3] @ embeddings[3:].T
61print(similarities)1ORT providers used: ['CUDAExecutionProvider', 'CPUExecutionProvider']
2similarity matrix:
3[[0.5550 0.0997 0.1455]
4 [0.1194 0.6390 0.1434]
5 [0.1284 0.0251 0.3975]]
6Embedding CUDA verification passed