Skip to content

[BUG] MLX: runtime rejects standard generate() inputs (prompts/input_ids) and ignores max_new_tokens; export requant ignores dynamic (excluded modules quantized, 8-bit overrides -> 4-bit); KeyError on dynamic without bits #4076

Closed

Description

@Qubitium

Audit of main @ ad0525c. Repros used mlx-cpu 0.32.4 + mlx-lm 0.32.2 on Linux: the real mlx_generate / BaseQModel.generate wiring and the real export path, with tiny or real models.

1. The MLX runtime can't be driven through the standard generate() API, and max_new_tokens is ignored (high for Apple users)

models/base.py:2841-2848 (_generate_with_runtime) normalises inputs to prompts= (str / list[str]) or input_ids= (tensor). The MLX adapter (loader.py:3027, partial(mlx_generate, ...)) reads neither key: utils/mlx.py:498 does prompt = kwargs.pop("prompt", None) and raises MLX requires prompts to be provided. max_tokens = kwargs.pop("max_tokens", 256) at :506 ignores max_new_tokens, and eos_token_id, stop, seed and do_sample are dropped as well.

generate('Hello')                          -> ValueError: MLX requires prompts to be provided
generate(input_ids=tensor)                 -> ValueError: MLX requires prompts to be provided
generate(prompts=['Hello'])                -> ValueError: MLX requires prompts to be provided
generate(prompt='Hello', max_new_tokens=5) -> generated 256 tokens
generate(prompt='Hello', max_tokens=5)     -> generated 5 tokens

_auto_select_mlx_backend (loader.py:166) picks MLX under BACKEND.AUTO on Apple Silicon, so a plain GPTQModel.load(path).generate("...") fails there. Reading the code, the OpenAI server always passes inputs=<tensor>, so every chat completion on an MLX-loaded model would fail (not run: fastapi isn't installed here). Fix: accept prompts / input_ids (mlx_lm generate takes token lists), map max_new_tokens → max_tokens, and map EOS/stop. Add a test that goes through BaseQModel.generate; test_mlx_packing.py::test_mlx_generate_maps_sampling_options calls mlx_generate(prompt=...) directly.

2. MLX export/convert ignores dynamic: excluded modules get quantized and per-module 8-bit/g32 overrides become 4-bit/g128 (medium-high, silent)

utils/mlx.py:487 calls quantize_model(mlx_model, config, group_size=gptq_config["group_size"], bits=gptq_config["bits"]) with no quant_predicate. This requantization path runs whenever the exact packed transfer is unavailable. That is always the case on non-macOS (including GPTQModel.export(..., format="mlx")), because nn_modules/qlinear/mlx.py:53 requires Darwin + arm64 + Metal, even though repack_gptq is pure numpy.

Qwen2.5-0.5B RTN 4-bit g128, dynamic={"-:.*mlp\.down_proj": {}, "+:.*self_attn\.o_proj": {"bits": 8, "group_size": 32}}
exported mlx quantization cfg: {'group_size': 128, 'bits': 4, 'mode': 'affine'}   (no per-module entries)
layers.0.self_attn.o_proj: source 8-bit g32 rel err vs fp16 0.0064 -> exported MLX 0.1105
layers.0.mlp.down_proj:    source excluded (fp16) 0.0000         -> exported MLX 0.1089

Even without dynamic, this path re-rounds the existing GPTQ grid. On Qwen1.5-0.5B-Chat-GPTQ-4bits-gp32, KL vs fp16 is 0.0437 for the torch quantized model and 0.0839 for the exported MLX model. Fix: pass a quant_predicate built from dynamic_get (False for -: matches, per-module {bits, group_size} for overrides), and allow the exact numpy repack for export on any OS (only the runtime needs Metal).

3. MLX conversion raises KeyError: 'bits' for a valid dynamic override without a bits key (low)

utils/mlx.py:405-410 reads config["bits"] for every non-empty override, so RTNConfig(bits=4, group_size=128, dynamic={"+:.*o_proj": {"group_size": 32}}) raises KeyError 'bits'. Fix: config.get("bits", gptq_config["bits"]), and drop the stray print(gptq_config["dynamic"]) at :406.

Checked, clean

  • vLLM/SGLang config translation: read against vLLM main auto_gptq.py / auto_awq.py / moe_wna16.py / gptq_utils.py and SGLang gptq.py. The fields (bits, group_size, desc_act, sym, lm_head, dynamic, quant_method, checkpoint_format, zero_point) and the dynamic first-match/-: semantics agree.
  • Tenstorrent block-float CUDA emulation: bit-exact vs the reference quantize/decompose/recompose/pack (bf4/bf8, odd shapes, fp16/bf16/fp32). The linear has rel err ≤ 5e-7 vs fp64.

Plausible, not verified

  • On a Mac where the packed transfer succeeds, export saves *.linear.weight-prefixed custom-runtime tensors plus a _gptqmodel_custom_mlx_runtime flag that auto.py:1124-1133 never strips. The export may then not load with stock mlx_lm.load.
  • vLLM AWQ ignores version, so a GEMV/GEMV_FAST checkpoint saved as quant_method=awq is parsed as GEMM. That should fail loudly with a shape mismatch rather than silently.

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions