fix: 8-bit dequant for MLX mixed-precision gate quantization#14
Open
userFRM wants to merge 1 commit intodanveloper:mainfrom
Open
fix: 8-bit dequant for MLX mixed-precision gate quantization#14userFRM wants to merge 1 commit intodanveloper:mainfrom
userFRM wants to merge 1 commit intodanveloper:mainfrom
Conversation
MLX 4-bit models quantize routing gates (mlp.gate, mlp.shared_expert_gate) at 8-bit precision, specified per-tensor in config.json. The inference engine treated all tensors as 4-bit, extracting 8 nibbles per uint32 from data that actually packs 4 bytes per uint32. This corrupts routing scores, selecting wrong experts and producing nonsensical output. Changes: - Add dequant_matvec_8bit Metal kernel (4 bytes/uint32, FMA-optimized) - Add cpu_dequant_matvec_8bit CPU fallback - Add BatchMatvecSpec.bits field for per-tensor bit-width dispatch - Mark gate and shared_expert_gate as 8-bit in all dispatch sites Fixes danveloper#10 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MLX 4-bit quantized models use 8-bit precision for routing gates, specified per-tensor in
config.json:The 4-bit dequant kernel extracts 8 nibbles per uint32, but these tensors pack 4 bytes per uint32 (8-bit). This corrupts routing gate scores, selecting wrong experts every layer, producing nonsensical output.
Verification
Compared gate output against MLX Python reference for
mlx-community/Qwen3-Coder-Next-4bit:Forced full CPU computation (
g_metal = NULL) confirmed coherent output: "2 + 2 = 4", correct code generation, proper EOS handling.Changes
shaders.metal: Addeddequant_matvec_8bitkernel — same tiled ROWS_PER_TG=8 structure asdequant_matvec_4bit_v3, but extracts 4 bytes per uint32 with& 0xFFinstead of 8 nibbles with& 0xF. FMA-optimized with precomputedscale*xandbias*x.infer.m:int bitsfield toBatchMatvecSpecfor per-tensor bit-width dispatchmatvec_8bitpipeline state toMetalCtxcpu_dequant_matvec_8bitCPU fallbackgpu_encode_batch_matvecandgpu_batch_matvecto select 8-bit kernel whenbits == 8gate_wandseg_w(shared_expert_gate) asbits=8in all 7 BatchMatvecSpec initialization sitesImpact
Affects any MLX quantized model with per-tensor bit-width overrides in the quantization config. This is standard for Qwen3 family models.
Fixes #10
Test plan
./infer --prompt "Hello" --tokens 20 --k 4produces coherent output--timingshows reasonable routing)--2bit)