Repository navigation
Accelerate 2-bit Walsh-Hadamard speculative verify forwards for prism_hadamard_qwen35 #5770
theBenForce
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
After seeing your M3 Ultra benchmark in #5660 (comment), I profiled
prism-ml/Ternary-Bonsai-2.5-27B-mlx-2bitduring multi-token verify steps to see why 2-token and 3-token forwards scaled almost linearly with token count. Even when the bundled MTP head hits an 80% to 90% acceptance rate, paying1.7xto2.8xper verify step wipes out the speculative gain.Because this is my first contribution to oMLX and the kernel patch plus tests crosses 1,000 lines, I am opening this discussion first to check the approach and PR split before opening a pull request.
Why stock
prism_hadamard_qwen35verify steps are slowThree bottlenecks stack on top of each other during a stock
mlx-vlmverify forward:float32promotion across the trunk. Stockmlx-vlmleaves unpacked trunk parameters such asRMSNormweights,conv1d.weight,in_proj_a.weight,in_proj_b.weight, anddt_biasinfloat32. That promotes trunk activations,KVCachekeys, and logits tofloat32. Execution then misses oMLX'sfloat16andbfloat16verify kernels and fused GDN and SDPA paths, and Peak Metal climbs by1.32 GiB.float32Hadamard rotations. EveryHadamardQuantizedLinearcasts activations tofloat32, multiplies signs, reshapes into blocks, runsmx.hadamard_transform, reshapes back, and casts back on every call. Sibling projections (in_proj_qkvwithin_proj_z,q_projwithk_projandv_proj, andgate_projwithup_proj) repeat the exact sameblock=1024rotation on the same normed input, and the 80 pure-rotated layer-seam norms write unrotated normed buffers to device memory that no consumer reads.M = 2..5. Stockmx.quantized_matmultreats 2-bitgroup_size=128weights as generic affine weights. All 401 packed linear tensors in Ternary Bonsai 2.5 satisfybiases == -scales, yet stock kernels stream422 MBofbiasesper verify step, computexsumreductions, and scale near-linearly acrossM = 2..5.Proposed two-PR split
All changes live in
omlx/patches/and model loader and discovery wiring, with zero edits tomlx-vlmor the C++ extensions inomlx/custom_kernels/.PR 1: Fast
prism_hadamard_qwen35verify forwards (feat/hadamard-verify-kernels)PR 1 fixes multi-row verify forwards and single-token trunk execution on its own, independent of MTP:
float16trunk normalization and zero-copy affine views inomlx/patches/prism_hadamard_verify.py. Casts finite unpackedfloat32trunk parameters tofloat16at load time and keeps.signs,A_log, and RoPE_inv_freqinfloat32, sinceA_logcan exceed thefloat16max of65504. RebasesHadamardQuantizedLinearandHadamardQuantizedEmbeddingonto cached zero-copynn.QuantizedLinearviews and shares sibling rotations throughomlx.oqh.modules._MEMO.block=1024FWHT and fusednorm_rotateseam kernel inomlx/patches/prism_hadamard_verify.py. Replaces the four-dispatchfloat32rotation with a one-barrier 128-threadmx.fast.metal_kernelthat matchesmx.hadamard_transformbit-for-bit, and fuses residual adds,RMSNorm, sign multiplies, andblock=1024FWHT across all 128 transformer layer seams. On the 80 pure-rotated seams,write_normed=Falseskips writing the unrotated normed tensor to device memory.M = 2..5verify Metal kernels inomlx/patches/q2_verify_qmv.py. Addsq2_qmm,q2_group_qmv, andq2_swigluwithhalf2IEEE-754 exponent-magic decode, Apple M2+(RPSG=2, NSG=8)or(RPSG=2, NSG=4)and(RPSG=1, NSG=8)or(RPSG=1, NSG=4)threadgroup geometry (NSG=2on M1), and a load-verifiedbiases == -scalesternary fast path that omitsbiasesbuffer reads andxsumreductions. Non-ternary 2-bit checkpoints use the generic affine path automatically.VLMBatchedEnginewiring acrossomlx/patches/qwen35_verify_qmm.py,omlx/patches/mlx_vlm_mtp/,omlx/patches/qwen35_gdn_verify_fused.py,omlx/model_discovery.py, andomlx/utils/model_loading.py. Routes 2-bitM = 2..5calls toq2_verify_qmv,M = 6..8batched verify calls tosg8, andM = 9..24calls (N >= 4096,9..16on NAX) tomma. Batches densein_proj_aandin_proj_bverify projections, leaves MoE router singletons untouched, and registersprism_hadamard_qwen35forVLMBatchedEngine.PR 2: Bonsai 2.5
mtp-v2-head.safetensorsLightning MTP sidecar (feat/bonsai-25-lightning-mtp)Once PR 1 makes multi-token verify steps cheap, PR 2 wires the bundled 4-bit group-32
mtp/mtp-v2-head.safetensorssidecar into Lightning MTP inomlx/patches/mlx_vlm_mtp/bonsai_v2_sidecar.py. It follows the same sidecar loader pattern oMLX uses for MiniMax, Inkling, and MiMo. Instead of allocating a duplicate 465 MB 3-bit coarse draft head,_coarse_draft_headreuses the target's 2-bit Hadamardlm_headdirectly by rotating the 4-bit MTP hidden state once (sample_ms = 0.02 ms,+0.248 GiBtotal sidecar memory).Benchmarks on Apple M4 Max (36 GB)
All runs below use
prism-ml/Ternary-Bonsai-2.5-27B-mlx-2bitwith thinking off (enable_thinking=False) and all 5 native custom kernels built.Isolated verify forward latency (PR 1 alone)
Medians across 5 interleaved A/B pairs after a 512-token prefill, with 5 warmup and 20 timed
language_model(..., speculative_verify=True)steps perM:main(633d6cf6)38.6075.34106.461.95x2.76xfloat3210.87182b42477)27.4526.2332.120.96x1.17xfloat169.547Across a 64-step greedy
M = 3trace against unpatched stockmlx-vlm, all 64 committed tokens and all 192 top-1 argmax positions match identically.End-to-end decode throughput (
VLMBatchedEngine.stream_chat, PR 1 + PR 2)Medians across 1 warmup and 3 counterbalanced interleaved A/B pairs with
SchedulerConfig(paged_ssd_cache_dir=None, hot_cache_max_size=0). Sampled rows usetemperature=1.0, top_p=0.95, top_k=20from#5660:mlx-vlm(tok/s)256tok)29.3138.2668.471.79x(2.34xvs stock)79.8%8.4808.806256tok)n/a38.0269.091.82x80.5%8.4818.811256tok)n/a38.0261.301.61x70.5%8.4088.811512tok)n/a38.0879.912.10x92.3%9.2639.529256x256PNG, greedy (192tok)n/a38.4170.601.84x84.3%8.6218.868Greedy parity note.
novel_code_greedyandmultimodal_shapes_greedymatch token-for-token betweenMTP OffandMTP Onacross every run.code_refactor_greedymatches byte-for-byte on 2 of 4 runs and hits afloat16logit tie betweenM = 1andM = 3accumulation order at character 930 on the other 2 runs.Questions before opening PR 1
mx.fast.metal_kernelruntime patches underomlx/patches/fit where you wantprism_hadamard_qwen35support to live, or would you prefer any piece moved intoomlx/oqh/oromlx/custom_kernels/?prism_hadamard_qwen35float16trunk, Hadamard seam fusion, and 2-bit verify kernels) first and opening PR 2 (mtp-v2-head.safetensorssidecar) once PR 1 merges match how you want to review this?All reactions