Fused Qwen4 on MLX, and what the dials cost
I've been running Qwen/Qwen4-35B-A3B on an M1 Max with 64 GB. MLX 0.32.5, greedy decoding. The engine is fused MLX code for it. Plain model, mask optional, is qwen4-35b-a3b-mlx-fused.
The abliterated build, dial on by default and the mask still optional, is Qwen4-35B-A3B-MLX-Abliterated. Both repos are code and two small artifacts. They do not contain the Qwen4 weights. You download the official 4-bit weights from mlx-community and the code runs on those. On the plain repo, out of the box, it behaves like stock Qwen4. Disk never changes. The dials apply in memory, and ejecting restores the weights bit for bit. I checked by hashing the gate projection on all 40 layers.
Two dials, both optional on the plain repo. A deletion-mask tool-calling cartridge. Abliteration, a refusal-direction removal: the direction comes out of the residual stream at runtime (Arditi et al.). I measured the engine with both dials off.
Engine
Fused decode. Medians of 3 repeats, three prompts (chat, code, prose), 512 generated tokens, no swap growth. 34.61 tok/s to 38.56 tok/s. (38.56 - 34.61) / 34.61 = +11.41%. Fused decode is bit-exact. 8 of 8 greedy outputs match the unfused run, token for token, at 512 tokens.
Prefill at 4K goes from 317.73 tok/s to 426.98 tok/s. (426.98 - 317.73) / 317.73 = +34.38%. At 16K, 325.34 tok/s to 422.35 tok/s, (422.35 - 325.34) / 325.34 = +29.82%. At 32K, 365.78 tok/s to 416.89 tok/s, (416.89 - 365.78) / 365.78 = +13.97%.
DFlash2 mean goes from 38.75 tok/s to 51.38 tok/s. (51.38 - 38.75) / 38.75 = +32.59% vs fused. Against plain decode at 34.61 tok/s, (51.38 - 34.61) / 34.61 = +48.45%. The code prompt goes from 38.93 tok/s to 67.26 tok/s, (67.26 - 38.93) / 38.93 = +72.77%. Peak memory with DFlash2 is 19.96 GiB.
DFlash2 is near-lossless. 0 of 3 greedy hashes match, so it is not bit-exact. The verify step uses a different MLX kernel, off by one bfloat16 step, and a near-tied token flips sometimes. For exact outputs, use fused decode with speculation off.
The 8-bit KV cache cuts full-attention KV from 640 MiB to 340 MiB at 32K. (340 - 640) / 640 = -46.88%. 8-bit KV prefill at 16K and at 32K is slower than the BF16 cache. MTP is slightly slower than plain decode on this M1 Max with 64 GB, 37.59 tok/s against 38.87 tok/s.
The engine numbers above were measured without the dials. Dial speeds below used fused decode with speculation off. The dials were never tested together with DFlash2 or MTP.
Tool mask
The mask deletes 3,602,754 of 1,384,120,320 MoE gate weights. 3,602,754 / 1,384,120,320 = 0.2603%. That is the 32 most tool-relevant routed experts of each layer plus every shared expert. No weights are updated and nothing is added. The artifact is an 8.13 MB file listing which weights to zero, qwen4_toolalpaca_e32_step150.cart.zlib. 337 ms to apply, 335 ms to eject. It costs no speed, 38.61 tok/s against 38.64 tok/s.
Trained on the ToolAlpaca training split, checkpoint chosen on a dev split, scored on the 97 held-out ToolAlpaca items with the paper's scorer. One benchmark, one greedy run, one machine. I am not stretching it past those 97.
Base exact match is 20/97 (20.6%), valid JSON 68/97. Mask is 55/97 (56.7%), valid JSON 97/97. (55 - 20) / 20 = +175%. McNemar p = 3.1e-8, 39 items gained, 4 lost.
Mask plus abliteration v2 is 55/97, valid JSON 97/97, 37 gained and 2 lost, p = 2.8e-9. Abliteration v2 alone is 15/97, valid JSON 69/97, p = 0.42, not significant. Abliteration does not help tool calling. The mask does.
The mask does not change refusal behavior. JailbreakBench is 96 harmful refused and 45 benign refused, against base 95 and 43. On WikiText-2, 32 windows of 1024 tokens, perplexity moves from 10.033 to 9.177, KL 0.139 against base. Perplexity went down. The output distribution still moved.
Abliteration
v2 is a rank-3, iterative, biprojected direction on layers 6 to 39, scale 1.0, norm preserving. The file is 840 KB, qwen4_abliterate_v2.abl.npz. 0.35 ms to apply, 0.02 ms to eject, weights back bit for bit. abliterate_scale can lower the strength. I measured scale 1.0.
Fit on 384 harmful and 384 harmless public prompts, including mlabonne/harmful_behaviors and mlabonne/harmless_alpaca, then tuned on a separate dev set of 90 harmful and 64 harmless. JailbreakBench was the final test only.
Heretic was not used to abliterate. The direction comes from my own fitting code. Heretic's scoring code (p-e-w/heretic, commit cd315cb) was ported exactly and used only as the scorer.
100 harmful behaviors and 100 benign. Greedy, 25,600-token budget, thinking off. A refusal is a regex on the first 500 visible characters. Loops are stopped early and counted wrong. One false positive. No second judge.
Harmful refused, then benign refused. Base 95/43, loops 0/1. Mask 96/45, loops 0/0. v1 19/3, loops 0/0. Mask plus v1 21/4, loops 1/0. v2 refuses 0 harmful. Benign raw is 1, item 65, a regex false positive, and the true benign count is 0. The model had finished the story and the regex matched dialogue inside it. Loops on v2 are 2 harmful and 0 benign. Mask plus v2 is 0/0, loops 1/1.
The base model refuses 43 of the 100 benign prompts. v2's true benign count is 0.
On the 100 harmful prompts, v2 gave 0 empty, 0 short, 0 dodges, 0 lecture-only. 4 answers repeat themselves. 2 of those were stopped as loops, one at 3,072 tokens and one at 12,032. I hand-read 3 answers drawn at random, seed 0. They were real, complete answers. Raw outputs are not published.
It answers requests the stock model declines, including harmful ones, and should not be deployed where refusal behavior is needed.
Cost, fused decode, speculation off, medians of 3 repeats. 38.64 tok/s to 35.80 tok/s. (35.80 - 38.64) / 38.64 = -7.35%. WikiText-2, same 32 windows of 1024 tokens, perplexity 10.033 to 10.144, (10.144 - 10.033) / 10.033 = +1.1%, KL 0.120. Mask plus v2 is 35.77 tok/s, (35.77 - 38.64) / 38.64 = -7.43%, perplexity 9.297, KL 0.194. Peak memory is 19.22 GiB base and 19.25 GiB with the dial.
Heretic scores
Same commit, cd315cb. 33 refusal phrases, 100 prompts from mlabonne/harmful_behaviors, first-token KL on 100 prompts from mlabonne/harmless_alpaca. The port lives in eval/. It imports a local helper that is not in the repo, so treat that folder as reference.
Raw refusals out of 100: base 77, v2 5, mask plus v2 5. On the 65 prompts outside the fit pool: base 53, v2 4, mask plus v2 3. After reading the answers: base 77, v2 1 (1 of 65 held-out), mask plus v2 0 (0 of 65). First-token KL is 0.460 for v2 and 0.458 for mask plus v2. Heretic's KL is larger than the WikiText KL of 0.120 because it scores only the first answer token with thinking forced off. The two are separate measurements.
Raw is high for a dumb reason. Heretic forces a closing think tag as the start of every answer and caps the answer at 100 tokens. Qwen4 keeps thinking after that tag, so flagged answers were often cut off mid-thought. The matcher also flags a keyword anywhere. Compliant text that contains "illegal" or "harmful" counts as a refusal.
I reran the 7 flagged answers at 1,024 tokens. The keyword matcher still flagged all 7. Reading them by hand, 6 of 7 comply. 1 is a partial refusal with harm-reduction information. "After reading" is my own reading, not an automatic score.
35 of the 100 prompts overlap the fit pool. The 65-prompt column is the fairer one.
Limits
Untested: other quantizations, other models, a second judge, and the dials together with DFlash2 or MTP. The mask and the direction were fit and measured on Qwen4-35B-A3B 4-bit only. Every eval is one greedy run on one M1 Max with 64 GB. Speed figures are medians of 3 repeats.
Where to get it
The weights are mlx-community/Qwen4-35B-A3B-4bit, revision 8f55c0174eea7d6ee46ccaefdc1b59b219800209, about 32 GB, and they are not in either repo. Optional drafter: RadixArk/Qwen4-35B-A3B-Dflash2, revision cb9d9b2. Plain engine, mask optional: qwen4-35b-a3b-mlx-fused. Abliteration on by default, mask optional: Qwen4-35B-A3B-MLX-Abliterated. Code in both is Apache 2.0. Qwen/Qwen4-35B-A3B is Apache 2.0 and is not redistributed here.
The mlx-vlm loader fix is an open pull request, not merged: "qwen4_moe: load mlx-lm-converted checkpoints (fixes #2907)".
Qwen team for Qwen/Qwen4-35B-A3B (Apache 2.0). mlx-community for the 4-bit conversion. Apple's MLX and mlx-lm. RadixArk for the DFlash2 drafter. ggml-org for the MTP head. Arditi et al. for the refusal-direction method. p-e-w for the Heretic scorer. JailbreakBench, ToolAlpaca, and mlabonne for the prompt sets.
The MLX port and experiments were run with the Gemini 4 Neon coding agent.
Eject the dial and the weights are stock again, bit for bit.
Project: Skill cartridges · Tags: MLX Local inference Deletion masks Tool use Speculative decoding Benchmarks