Repository navigation
native: add Qwen3.5-2B MLX affine 4-bit support - #435
Conversation
|
Thank you, @JRaxworthy. The receipt is honest about what it couldn't run: This is Zig work, so it's reviewed for landing rather than rewritten. Before it can land it has to pass the gates your receipt lists as remaining: |
|
Thank you, @JRaxworthy. Your Qwen3.5-2B family is merged into |
…joins everything on zig-flashnext # Conflicts: # ZIG-PREVIEW.md # build.zig # zig/src/core/checkpoint_metal.zig # zig/src/native/metal.zig # zig/src/tensorfold.zig
…joins everything on zig-flashnext # Conflicts: # ZIG-PREVIEW.md # build.zig # zig/src/core/checkpoint_metal.zig # zig/src/native/metal.zig # zig/src/tensorfold.zig
What this changes
Adds native Zig/Metal text support for the pinned Qwen3.5-2B MLX affine 4-bit checkpoint, which the existing native engine rejects. Its tied embedding and output projection share the packed checkpoint buffer; the hybrid DeltaNet/full-attention forward integrates with the existing lane engine and native server. Ordinary chat replies also honor the tokenizer's chat EOS when it differs from the model config's end token. Implements the bounded proposal in #431; no separate dependent PR or unrelated optimization.
Draft: the implementation runs on the tested M5 Max, but the required full
zig build testcommand cannot finish on this host because the Metal command-line compiler is unavailable. This receipt records the successful runtime checks and the remaining qualification work. It does not claim completed qualification on other Macs or CUDA. No response to the proposal issue has been received; additional maintainer landing requirements remain to be established during review.Receipt
Environment: upstream
zig-previewbase73cbd2f269c8771046f65d5b4630be25b8f86f17; contribution5dc7d540d87cf1b95d84fa789d1d2ce4092bf2a6(initial measurements atd400abbd43ba8d4b76b2cde83d597a4c5df73255). Mac Studio, Apple M5 Max GPU, 18-core CPU, 64 GB unified memory; macOS 27.0 / 26A428; Command Line Tools with macOS SDK 27.0. Zig 0.17.0, builds use-Dcpu=apple_m1; pkgconf 3.0.7. Python 3.12.14, MLX 0.32.3, mlx-lm 0.32.0, NumPy 2.5.3, transformers 5.18.0, pytest 9.1.1. Native decoding/tokenization does not invoke Python or MLX. Runtime Metal compilation uses the checkout's MLX compile settings (safe math, fp32 functions fast, Metal 4.1 on this OS). CLImetal/metallibare absent.Checkpoint:
mlx-community/Qwen3.5-2B-MLX-4bit, revision93760be4f1f69842a46bc13dbdc0f19e291392a3;model.safetensorsSHA-256713fe7e5d3c3965f7106b0d0ee17615f7869c23c8d327996df8c1196fbcf07d5. Reused weights outside the source checkout. Affine 4-bit/group 64, bf16 scales/biases, 24 layers, hidden 2048, FFN 6144, vocabulary 248320. Eighteen Gated DeltaNet layers (16 heads × 128) and six full-attention layers (8 Q / 2 KV heads × 256), partial rotary dimension 64. The sanitized convolution layout is loaded directly; norm weights receive no extra offset.Exactness:
tools/bench_concurrent.py --alone --serialat its default 4 streams × 3 repetitions × 256 output tokens, both public prompts and temperatures 1.0 and 0: 48/48 concurrent-versus-solo comparisons and 10/10 solo-versus-draft:falsecomparisons equal; 0 unequal, failed or unmeasured. These comparisons use the server's existingtoken_shafield. An additional--mixed --levels 2,8 --reps 3 --tokens 128 --alone --serial --stagger-ms 37run passed 30/30 concurrent-versus-solo comparisons and all 8 distinct solo-versus-plain comparisons (the same 8 references appear in each level's summary). Default drafts stayed enabled; context-copy proposals were accepted. For example, one sampled raw-code request and its plain repetition both producedb82953975509: draftedrounds=171 accepted=84/90, plainrounds=255 accepted=0/0.tf-qwen35-exactpassed 49/49 full-forward/cache checks, comparing every bf16 logit and committed attention KV, fp32 recurrence, convolution state, accepted-row logits and cache length. Prefixes 0/127/128/129/255/256; windows 1/2/7/8/15/16/17/32; partial acceptance followed by continuation; unequal 7/13-row streams and different physical KV capacities. Runtime width qualification also checks two unequal 16-row streams and partial keeps against one-row execution before enabling wider lanes. A numerical mismatch retains one-row shared lane rounds with a warning; GPU failures stop loading. This preserves the preview's M1–M4 qualification.The server regression passed thinking on/off, automatic tools and tool-result history, leading/later instructions, a rewritten earlier answer, unequal concurrent conversations and top-k on/off sampling. All five conversations matched plain and concurrent token hashes; native rendered prompt counts matched the checkpoint template/tokenizer. Context overflow, media, forced tool choice and structured output were rejected, then a valid request still matched. Cross-request prompt reuse/resume is not implemented in the native preview: repeated/follow-up requests run afresh (
cached_tokens=0). No restored-prefix exactness claim is made.Decode speed before/after: the upstream native baseline rejects
qwen3_5before loading. Last release v0.6.5 (609ca419abecebdc5a059498a613680bd3aa847f) also rejects this checkpoint in its packed Python admission function because the embedding head is tied; that release function was evaluated against the actual checkpoint config. Thus both before baselines are unsupported, with no throughput or improvement ratio. After measurements below are from unmodifiedtools/bench_openai.py --tokens 64 --reps 5 --temperatures 1.0,0, default warm-ups, seeds 1234–1238, drafts on, no competing validation/build work.tools/prefill_cold.py build; unmodifiedtools/prefill_cold.py run, default 2k/8k/16k/32k/64k lengths, three repetitions each, one 1k warm-up, two reply tokens, greedy/no thinking. Server context 70,000, parallel 8, prompt chunks up to 128, drafts enabled. Every measured request completed with positive endpoint prompt usage and two output tokens; all server receipts havecached=0. These are whole-request client TTFT and the tool's prompt-tokens/TTFT rate, including dependent attention and recurrence, rather than isolated kernel throughput. Measurements ran sequentially after all builds/tests; earlier runs that overlapped compilation were discarded. One nominal 8k fixture contains 8,191 actual tokens; the others contain the nominal count.There is no before/after speed-up claim and no comparison claiming to outperform MLX. An independent same-machine backend comparison used the same two public fixtures, preloaded stock
mlx_lm.stream_generate, greedy/no-thinking settings, 64 output tokens, ignored EOS and a 256 MiB MLX allocator cache behind a serial HTTP wrapper. Unmodifiedtools/bench_openai.py::streammade five measurements per prompt per block in native/MLX/MLX/native process order. MLX decode medians were 298.98 tok/s (raw code) and 309.18 tok/s (chat), versus native 181.98 and 190.30 tok/s. Native first-visible-token medians were 10.41/13.81 ms versus MLX18.90/20.21 ms. This comparison establishes support rather than a general speed advantage over MLX; it is a warm component benchmark, not a whole application or hardware-interference qualification. ABBA comparison ordering cannot be applied to an unsupported before engine. The native preview has no prompt reuse, so these cold figures do not describe cached follow-up turns. The configured 262,144-position maximum is not a memory/throughput qualification at that length.Tests run: isolated
.[test,tui]environment. Before fullpython -m pytest -q tests: 5,057 passed, 10 failed, 627 skipped, 6 subtests passed (597.57 s). Initial after suite: 5,061 passed, 10 failed, 627 skipped, 6 subtests passed (576.31 s). After independent peer review and the EOS repair, the complete suite with pinned weights/native tools reports 5,062 passed, 10 failed, 627 skipped, 6 subtests passed (338.12 s). The 10 failure names are unchanged: TUI small-logo snapshot; GLM fused index scores; seventest_tree_forward_fused_equals_unfusedshapes andtest_models_of_other_widths_in_one_process. No unrelated fixes are bundled. The final focused native suite, including the subsequently added server regression, passed 5/5, 0 failed/skipped (33.47 s); without the model dependency it passed 2, skipped 3, failed 0. The capability assertion fails against the saved pre-change native binary (no Qwen family); the baseline cannot build/run the new operation/forward tools.zig build native test-native check-generated tf-qwen35-check tf-qwen35-forward tf-qwen35-exact -Dcpu=apple_m1: 20/20 build steps, 1/1 test passed. Fresh-cachezig build test: 38/38 runnable tests passed, including shared parser/loader and sampler tests, but 16/24 build steps succeeded, 3 failed atxcrun metaland remaining dependent steps did not run. This command did not pass. A direct shared Metal checkpoint test also passed 3/3, 0 failed/skipped, checking unchanged full-shard views versus selected namespace views, retained bytes, relative offsets, absent namespaces and duplicate cleanup. Generated sources and Zig formatting checks andgit diff --checkpass. Host CUDA/shared-core tests run within the Zig host target; CUDA GPU tests skip without their dependencies/hardware.Independent ordinary-chat regression: before the repair, a greedy request to reply with the word
amberproducedamber<|im_end|>\n. The native text loader returned config EOS248044 before reading tokenizer EOS248046. It now retains declared model end IDs and adds the tokenizer EOS without duplicates, as the MLX tokenizer wrapper does. The added real-model server assertion fails before and passes afterwards, with draft/plain equality; all five native tests pass in the latest complete suite. The direct native text host test passes1/1 and checks distinct IDs, same-ID deduplication, added-token object syntax and tokenizer-only fallback. Independent original focused suite also passed5/5 (81.61 s). A subsequentzig build testreported9/9 executed host tests passed, other host tests cached,16/24 build steps succeeded and the same3 unavailable CLI-Metal compilation failures. This remains an incomplete command, not a full pass. The repair changes stop selection, not forward arithmetic; the fixed-length measurements above useignore_eos:trueand remain labeled with their original measured commit.Operation bring-up passed 109/109, 0 failed/skipped, with 93 checkpoint weight bindings checked by SHA-256. Exact captures validate native binding against the generated arithmetic and do not independently establish model fidelity. Separately, 112 stock-MLX operation comparisons were finite; maximum relative L2 difference was 0.008466 (row-one packed projection), with maximum absolute difference 0.03125. Rotary, embedding and cache-layout comparisons were exact in these captures.
Trusted model fidelity: teacher-forced next-token NLL/top-one logits use stock
mlx_lmwith the same weights/tokenizer, independently of the native forward. Stock execution at one-row, 32-row chunks and full prompt supplies its own variation control. All native logits were finite; no cross-backend bitwise requirement was imposed.The longer fixtures use the first 512 tokens of this base's public CONTRIBUTING.md and the first 2,048 tokens of the benchmark helper's public Python standard-library corpus (Python 3.12.14). No private prompts or fixtures. Maximum native-versus-stock one-row RMS logit difference across fixtures was 0.071394; maximum absolute difference was 1.515625. These measured differences are comparable to the reported stock shape variation on these fixtures; this is limited fixture evidence, not universal equivalence or a model-quality improvement claim.
Fidelity input token SHA-256 values
fibonacci-raw:b3fd1dfdf7de931abd0762f111185cd194b859ed6572c91c62c7ec8ccffb950dgpu-chat-no-think:d3c4dba98f8291245251a7cf40145ee22ec9641b86a32e38a118c0241a92fce2public-contribution-guide-512:df87f87833e33efa04b66eb39b4845dceae31e395354dddbbf42660d4e169045python-stdlib-2048:d238f329029b6b204cf8886eda2bd67215fa53722e38d19806eac3e868d1bf631e-6/128), and attention uses fixed causal 128-position partitions. These choices change cross-backend bits at the same precision and are disclosed in the recipe. The existing Python and Nemotron arithmetic is not changed.Remaining landing work: run the complete
zig build testwith the documented Metal toolchain; review the unchanged Python failures; qualify available M3/M5 hardware and the shared-parser CUDA path, with broader M1/M2/M4 validation as available. The portable recipe and operation/fidelity/cache tools are included for those reruns. An M5 result does not establish support on other Macs. The proposal/PR are the contribution-guide coordination; no separate outside contact is needed.Portable build/run instructions:
docs/recipes/qwen3.5-2b-native.mddocuments the pinned download outside the checkout, Zig 0.17.0/Metal requirements, native build/server launch, optional-dependency handling and public verification commands. Use the native binary directly on this preview branch; no Python-only fallback is presented as native support.Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Draft/plain and concurrent/solo pass on the tested M5; cross-request resumed native execution does not exist, so the full item is not marked complete.