mlboydaisuke commited on
Commit
c8e5ad6
·
verified ·
1 Parent(s): ba47d43

Mirror of mlboydaisuke/Qwen3-Embedding-0.6B-CoreAI

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ qwen3-embedding-0.6b_float16_s512_static.aimodel/main.mlirb filter=lfs diff=lfs merge=lfs -text
37
+ tokenizer/tokenizer.json filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ base_model: Qwen/Qwen3-Embedding-0.6B
4
+ tags:
5
+ - coreai
6
+ - sentence-similarity
7
+ - feature-extraction
8
+ - apple-silicon
9
+ - on-device
10
+ language:
11
+ - multilingual
12
+ pipeline_tag: sentence-similarity
13
+ ---
14
+
15
+ > **Mirror** of [`mlboydaisuke/Qwen3-Embedding-0.6B-CoreAI`](https://huggingface.co/mlboydaisuke/Qwen3-Embedding-0.6B-CoreAI) — the canonical repo ([CoreAI Model Zoo](https://github.com/john-rocky/coreai-model-zoo)). Updates land there first.
16
+
17
+
18
+ # Qwen3-Embedding-0.6B — Core AI export
19
+
20
+ [Qwen/Qwen3-Embedding-0.6B](https://huggingface.co/Qwen/Qwen3-Embedding-0.6B) as a single static
21
+ Core AI graph for macOS 27 / iOS 27: the full sentence-transformers pipeline (Qwen3-0.6B backbone
22
+ → **last-token pooling** → **L2 normalize**) runs in-graph, so one call returns a normalized,
23
+ **MRL-truncatable 1024-d** embedding. Multilingual (incl. Japanese), instruction-aware
24
+ on-device semantic search / RAG.
25
+
26
+ **This is an encoder** — one forward over the (right-padded) input → one pooled vector. No
27
+ autoregressive loop, no KV cache, no LM head. It runs as a plain `.aimodel` via `AIModel.run`
28
+ (like the vision encoders), not the pipelined generate engine.
29
+
30
+ ## Graph contract
31
+
32
+ | | name | shape | dtype |
33
+ |---|---|---|---|
34
+ | input | `input_ids` | [1, 512] | int32 (right-padded; pad id 151643) |
35
+ | input | `attention_mask` | [1, 512] | int32 (1 = real token, 0 = padding) |
36
+ | output | `embedding` | [1, 1024] | fp16, L2-normalized |
37
+
38
+ The grid (512) is an export-time choice — a smaller grid is proportionally faster for short
39
+ queries. Last-token pooling under the causal mask is right-pad safe (real tokens never attend to
40
+ trailing pads), so the host just right-pads to the grid.
41
+
42
+ ## Host recipe (everything else is in-graph)
43
+
44
+ - **Query** → prepend the instruction prefix:
45
+ `Instruct: Given a web search query, retrieve relevant passages that answer the query\nQuery:`
46
+ **Document** → no prefix.
47
+ - Tokenize, **right-pad** to 512 (truncate longer text). Run → 1024-d unit vector.
48
+ - **Similarity** = cosine = dot product (vectors are unit-norm).
49
+ - **Matryoshka (MRL)**: to shrink, take the first D dims (32 ≤ D ≤ 1024) and **re-L2-normalize**
50
+ on the host. Rankings are preserved down to 256; verified to 128.
51
+
52
+ ```python
53
+ # Core AI runtime (Python), GPU delegate
54
+ import coreai.runtime as rt, numpy as np
55
+ from transformers import AutoTokenizer
56
+
57
+ tok = AutoTokenizer.from_pretrained("tokenizer")
58
+ m = await rt.AIModel.load("qwen3-embedding-0.6b_float16_s512_static.aimodel",
59
+ rt.SpecializationOptions.from_preferred_compute_unit_kind(rt.ComputeUnitKind.gpu()))
60
+ fn = m.load_function("main")
61
+
62
+ def embed(text, is_query):
63
+ prefix = ("Instruct: Given a web search query, retrieve relevant passages that "
64
+ "answer the query\nQuery:") if is_query else ""
65
+ enc = tok(prefix + text, padding="max_length", truncation=True, max_length=512,
66
+ return_tensors="np", padding_side="right")
67
+ res = await fn({"input_ids": rt.NDArray(enc["input_ids"].astype(np.int32)),
68
+ "attention_mask": rt.NDArray(enc["attention_mask"].astype(np.int32))})
69
+ return res["embedding"].numpy()[0] # [1024], unit-norm
70
+ ```
71
+
72
+ ### Swift — [CoreAIKit](https://github.com/john-rocky/coreai-kit)
73
+
74
+ Downloads this repo on first use and applies the prompts in-process:
75
+
76
+ ```swift
77
+ import CoreAIKitEmbeddings
78
+
79
+ let embedder = try await TextEmbedder(model: .qwen3Embedding0_6B, prompts: .qwen3Embedding)
80
+ let query = try await embedder.embed(query: "What is the capital of Japan?")
81
+ let doc = try await embedder.embed(document: "Tokyo is the capital and largest city of Japan.")
82
+ let score = TextEmbedder.cosineSimilarity(query, doc) // unit vectors → dot product = cosine
83
+ ```
84
+
85
+ ## Bundle layout
86
+
87
+ ```
88
+ qwen3-embedding-0.6b_float16_s512_static.aimodel (~1.1 GB, fp16)
89
+ tokenizer/ (HF tokenizer files)
90
+ reference.json (torch reference embeddings + cosines)
91
+ ```
92
+
93
+ ## Parity
94
+
95
+ Precision **fp16**. Verified against the official `sentence-transformers` pipeline (fp32):
96
+ per-text embedding cosine **1.000000**, retrieval order identical, MRL rankings preserved at
97
+ 512 / 256 / 128. On the Core AI GPU delegate the `.aimodel` reproduces the torch reference at
98
+ cosine **0.999998** end-to-end (host tokenize → run). Measured ~25 ms (256-grid) / ~45 ms
99
+ (512-grid) per embedding on an M4 Max GPU.
100
+
101
+ ## License
102
+
103
+ Apache-2.0 (upstream model and code are Apache-2.0). Conversion script:
104
+ [`conversion/export_qwen3_embedding.py`](https://github.com/john-rocky/coreai-model-zoo/blob/main/conversion/export_qwen3_embedding.py)
105
+ in the coreai-model-zoo.
qwen3-embedding-0.6b_float16_s512_static.aimodel/main.hash ADDED
@@ -0,0 +1 @@
 
 
1
+ B
qwen3-embedding-0.6b_float16_s512_static.aimodel/main.mlirb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:420dd36581e8feb1afed13b8f6212b89d2e06573c2c8933d4d36e8623f89735f
3
+ size 1192497696
qwen3-embedding-0.6b_float16_s512_static.aimodel/metadata.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "description" : "Qwen3-Embedding-0.6B text embedding model (Qwen3-0.6B backbone, last-token pooling, L2-normalized 1024-d, MRL-truncatable 32-1024). Source: https:\/\/huggingface.co\/Qwen\/Qwen3-Embedding-0.6B",
3
+ "creationDate" : "20260614T050108Z",
4
+ "author" : "Alibaba Qwen",
5
+ "assetVersion" : "2.0",
6
+ "license" : "Apache-2.0"
7
+ }
reference.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer/chat_template.jinja ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if tools %}
2
+ {{- '<|im_start|>system\n' }}
3
+ {%- if messages[0].role == 'system' %}
4
+ {{- messages[0].content + '\n\n' }}
5
+ {%- endif %}
6
+ {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
7
+ {%- for tool in tools %}
8
+ {{- "\n" }}
9
+ {{- tool | tojson }}
10
+ {%- endfor %}
11
+ {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
12
+ {%- else %}
13
+ {%- if messages[0].role == 'system' %}
14
+ {{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
15
+ {%- endif %}
16
+ {%- endif %}
17
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
18
+ {%- for message in messages[::-1] %}
19
+ {%- set index = (messages|length - 1) - loop.index0 %}
20
+ {%- if ns.multi_step_tool and message.role == "user" and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
21
+ {%- set ns.multi_step_tool = false %}
22
+ {%- set ns.last_query_index = index %}
23
+ {%- endif %}
24
+ {%- endfor %}
25
+ {%- for message in messages %}
26
+ {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
27
+ {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
28
+ {%- elif message.role == "assistant" %}
29
+ {%- set content = message.content %}
30
+ {%- set reasoning_content = '' %}
31
+ {%- if message.reasoning_content is defined and message.reasoning_content is not none %}
32
+ {%- set reasoning_content = message.reasoning_content %}
33
+ {%- else %}
34
+ {%- if '</think>' in message.content %}
35
+ {%- set content = message.content.split('</think>')[-1].lstrip('\n') %}
36
+ {%- set reasoning_content = message.content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
37
+ {%- endif %}
38
+ {%- endif %}
39
+ {%- if loop.index0 > ns.last_query_index %}
40
+ {%- if loop.last or (not loop.last and reasoning_content) %}
41
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
42
+ {%- else %}
43
+ {{- '<|im_start|>' + message.role + '\n' + content }}
44
+ {%- endif %}
45
+ {%- else %}
46
+ {{- '<|im_start|>' + message.role + '\n' + content }}
47
+ {%- endif %}
48
+ {%- if message.tool_calls %}
49
+ {%- for tool_call in message.tool_calls %}
50
+ {%- if (loop.first and content) or (not loop.first) %}
51
+ {{- '\n' }}
52
+ {%- endif %}
53
+ {%- if tool_call.function %}
54
+ {%- set tool_call = tool_call.function %}
55
+ {%- endif %}
56
+ {{- '<tool_call>\n{"name": "' }}
57
+ {{- tool_call.name }}
58
+ {{- '", "arguments": ' }}
59
+ {%- if tool_call.arguments is string %}
60
+ {{- tool_call.arguments }}
61
+ {%- else %}
62
+ {{- tool_call.arguments | tojson }}
63
+ {%- endif %}
64
+ {{- '}\n</tool_call>' }}
65
+ {%- endfor %}
66
+ {%- endif %}
67
+ {{- '<|im_end|>\n' }}
68
+ {%- elif message.role == "tool" %}
69
+ {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
70
+ {{- '<|im_start|>user' }}
71
+ {%- endif %}
72
+ {{- '\n<tool_response>\n' }}
73
+ {{- message.content }}
74
+ {{- '\n</tool_response>' }}
75
+ {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
76
+ {{- '<|im_end|>\n' }}
77
+ {%- endif %}
78
+ {%- endif %}
79
+ {%- endfor %}
80
+ {%- if add_generation_prompt %}
81
+ {{- '<|im_start|>assistant\n' }}
82
+ {%- if enable_thinking is defined and enable_thinking is false %}
83
+ {{- '<think>\n\n</think>\n\n' }}
84
+ {%- endif %}
85
+ {%- endif %}
tokenizer/tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4b325f4efa94e389220ec20b055177a846870f4614762b59b1d6c7450c2830ec
3
+ size 11423979
tokenizer/tokenizer_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "backend": "tokenizers",
4
+ "bos_token": null,
5
+ "clean_up_tokenization_spaces": false,
6
+ "eos_token": "<|im_end|>",
7
+ "errors": "replace",
8
+ "is_local": false,
9
+ "local_files_only": false,
10
+ "model_max_length": 32768,
11
+ "pad_token": "<|endoftext|>",
12
+ "split_special_tokens": false,
13
+ "tokenizer_class": "Qwen2Tokenizer",
14
+ "unk_token": null
15
+ }