Skip to content

fix(export): load the mlx export path on the host (#4809) - #4834

Closed
Qubitium wants to merge 1 commit into
mainfrom
fix/mlx-export-cpu-load
Closed

Qubitium wants to merge 1 commit into
mainfrom
fix/mlx-export-cpu-load

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

GPTQModel.export(path, out, "mlx") always failed on a host with a visible accelerator for AWQ FORMAT.GEMV / FORMAT.GEMV_FAST checkpoints:

ValueError: <class 'gptqmodel.nn_modules.qlinear.gemv_awq.AwqGEMVTorchLinear'> does not support device: cuda:0
ValueError: <class 'gptqmodel.nn_modules.qlinear.gemv_fast_awq.AwqGEMVFastTorchLinear'> does not support device: cuda:0

The export picks a backend per method (models/auto.py) and passed no device, so the loader took the default device — the accelerator whenever one is visible. For AWQ it picks BACKEND.AWQ_TORCH, whose GEMV/GEMV_FAST readers are CPU-only (SUPPORTS_DEVICES = [DEVICE.CPU], added for #4554/#4736). The failure happens before any conversion, and CUDA_VISIBLE_DEVICES="" was the only workaround.

The MLX converter only reads the packed tensors from the checkpoint; it never runs a quantized forward. The accelerator is not needed at all.

Fixes #4809.

What Changed

  • gptqmodel/models/auto.py — the MLX export path now loads the checkpoint with device="cpu". That is the only load on this branch: the hf path returns earlier, so no other export is affected.
  • tests/test_export_contract_4386.py — test_export_mlx_loads_the_checkpoint_on_cpu, 4 nodes (GPTQ, AWQ, Paro, GGUF), extending the existing mocked-load contract tests in that file.

Behaviour change: export(..., "mlx") is now host-only and works identically with or without a GPU visible. No API change.

Tests

cd tests
python -m pytest test_export_contract_4386.py -q -ra
# 16 passed in 5.94s   (4 new)

python -m pytest test_export_contract_4386.py test_export.py \
    test_mlx_awq_formats.py test_mlx_awq_variants.py -q -ra
# 28 passed, 2 skipped
#   skips are the pre-existing "could not import 'mlx.core'" guards

ruff check gptqmodel docs/eora tests setup.py: All checks passed.

Red / green evidence

Reverting only the auto.py hunk, keeping the new test:

Test node Broken code With fix
test_export_mlx_loads_the_checkpoint_on_cpu[gptq-...] FAIL assert None == 'cpu' — kwargs {'backend': GPTQ_TORCH, 'trust_remote_code': False} PASS
...[awq-...] FAIL same, {'backend': AWQ_TORCH, ...} PASS
...[paro-...] FAIL same, {'backend': PARO_CUDA, ...} PASS
...[gguf-...] FAIL same, {'backend': GGUF_TORCH, ...} PASS

Red run: 4 failed. Green run: 16 passed. The assertion shows the load receiving no device at all, which is exactly the reported defect.

Real-device evidence (not the mock)

I quantized a real AWQ format="gemv" checkpoint (Qwen2.5-0.5B-Instruct, 32 calibration rows, CUDA_VISIBLE_DEVICES=0 so the accelerator is visible) and drove the load the export path performs:

cuda_visible True
DEFAULT_DEVICE ValueError <class '...AwqGEMVTorchLinear'> does not support device: cuda:0
CPU_LOAD ok modules=168
CPU_FORWARD ok shape=(1, 5, 151936) finite=True

Before the fix, export(..., "mlx") of that checkpoint on this GPU host failed with the reported ValueError. After the fix the same call gets past the load and stops at the MLX import, which is this host's limitation (below), not the device.

Limit: the full conversion could not be exercised here. mlx==0.32.4 installs on this platform but import mlx.core fails with libmlx.so: cannot open shared object file, and mlx-core has no wheel for this Python. Both packages were uninstalled afterwards, so the environment is back to its original state. The load is the half that failed, and that half is verified on a real checkpoint above.

Kernel Accuracy Requirements

Not applicable — no kernel or numerics change. The export path does not run a quantized forward.

Logic Flow Documentation

Not applicable: this pins one argument on one existing load call. No stage is added or reordered, no dispatch table changes, and no fallback is introduced — the MLX branch already existed and already only reads host tensors.

Model Support / Module Plan

Not applicable — no MODEL_MAP entry, definition or fusion config is touched.

Review Requirements

  • Reviewed this PR.
  • Matches existing project structure and conventions (the new test sits next to test_export_mlx_forwards_trust_remote_code, same mocked-load style).
  • No monkeypatching in the code under test; the test swaps GPTQModel.load the way the file already does.
  • Minimal surface: one keyword argument plus a comment.
  • Regression considered: paro, gguf, gptq, awq GEMM and GEMV, nf12, macaq and fp8 all resolve to a CPU-capable kernel for device=cpu (checked with select_quant_linear). exl3 uses BACKEND.EXL3_TORCH, the documented CPU/ROCm portable kernel, and bitsandbytes has no select_quant_linear entry to check here (the package is not installed on this host); both are unaffected by the hf path.

Notes

  • FORMAT.GEMV_FAST could not be selected in isolation in a select_quant_linear probe because the checkpoint's own pack_dtype is required; the real end-to-end repro above used FORMAT.GEMV. The two readers share the CPU-only declaration (gemv_awq.py:510, gemv_fast_awq.py:397/469) and the same export branch.

export(..., 'mlx') left the device implicit, so the loader picked the
accelerator whenever one was visible. The MLX converter only reads packed
tensors from the checkpoint and never runs a quantized forward, and the AWQ
GEMV/GEMV_FAST Torch readers are CPU-only, so those two formats always failed
with 'does not support device: cuda:0' on a GPU host before any conversion.

Load with device='cpu' instead: the conversion is host-only and now works on
any host.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant