Skip to content

Qwen3-Embedding on Metal: 0.6B, 4B and 8B through /v1/embeddings - #3494

Merged
ashhart merged 3 commits into
mainfrom
lead/qwen3-embedding-metal
Nov 16, 2026
Merged

ashhart merged 3 commits into
mainfrom
lead/qwen3-embedding-metal

Conversation

@ashhart

@ashhart ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Metal serves no embedding model today; /v1/embeddings serves Qwen3-Embedding on CUDA (#2285) and HIP. This serves Qwen3-Embedding 0.6B, 4B and 8B on Metal through the same route, with the same refusals and MRL widths.

How: the closest Metal forward is Llama's BF16 dense GQA/SwiGLU path. It gains a compile-time embedding branch that adds Qwen3's per-head Q/K RMSNorm before RoPE and returns the last row's normalized hidden state without the output head. Llama generation takes the original branch, with the same dispatches and kernel sources. zig/src/native/qwen3_metal.zig reuses the shared Qwen3 folder admission, prompt lane, memory-fit planner and lane host; no second server path. The first commit plants the Llama GEMM layout control in its macro instance, which the new contract entries need.

Checked on an M5 Ultra at the tested head:

Size Dimensions Max abs error vs FP32 reference Minimum cosine 10 inputs, 2,330 tokens
0.6B 1024 0.0037 0.99977 0.12 s
4B 2560 0.0015 0.99984 0.50 s
8B 4096 0.0050 0.99984 0.90 s
  • The FP32 reference (zig/tests/qwen3_metal/reference.py and reference.c) runs from the checkpoints' stored BF16 weights. The 0.001 cosine-distance ceiling was fixed before measuring.
  • Batched vectors equal single-input vectors bit for bit at lengths 1 to 2,049. Repeats, float and base64 outputs, and four concurrent inputs are also equal.
  • Kernel contract: the new head-norm kernel passes gates 1-4 with 2 of 2 controls caught; the dense subset is 31 of 31 with 14 of 14 controls caught and no limit problems.
  • lean adds nothing, fmt is clean, and every file is under 600 lines.

Not covered yet: chips without tensor units (M1-M4) are refused by name until they are qualified, other Qwen3 geometries are unchecked, and there is no CUDA vector comparison on the same inputs. A batch's members run one after another.

Board card OE-6. Checkpoints: Qwen/Qwen3-Embedding-0.6B 97b0c614be, -4B 5cf2132abc, -8B 1d8ad4ca9b (Apache-2.0).

  • Synthena (Ash's Agent)

Extend the dense BF16 forward with projected Q/K head norms and last-row pooling. Reuse the shared embedding admission and HTTP route, refuse reranking and unsupported options, and add contract controls plus a checkpoint-only FP32 reference and served qualification.
@ashhart
ashhart force-pushed the lead/qwen3-embedding-metal branch from f46044e to 07661c3 Compare November 16, 2026 11:55
@ashhart
ashhart merged commit ca0962d into main Nov 16, 2026
2 of 11 checks passed
@ashhart
ashhart deleted the lead/qwen3-embedding-metal branch November 16, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant