Skip to content

fix(backend): inherit explicit enum load backend into quantize (#4582) - #4606

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4582-backend-enum-inheritance
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4582-backend-enum-inheritance

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4582

TL;DR

resolve_quantize_backend recorded a dense load's backend only when it was a typed BackendConfig (qcfg._backend_config); an enum/string load (backend=BACKEND.AWQ_TORCH) was used for device selection and then dropped, so quantize(backend=AUTO) fell back to the AUTO chooser. The fix records an explicit non-AUTO enum/string selection in qcfg._load_backend and inherits it in quantize(backend=AUTO) exactly like a typed choice. Invalid values still raise at the load boundary.

Root cause

On 97c377509, the inheritance branch read only _backend_config, and the final assignment kept only typed configs:

# backend_config.py:276
backend = getattr(qcfg, "_backend_config", None) or backend
# backend_config.py:302
qcfg._backend_config = backend if isinstance(backend, BackendConfig) else None

GPTQModel.load(..., backend=BACKEND.AWQ_TORCH) therefore left no retained selection. BaseQModel.quantize(backend=AUTO) (models/base.py:2129) resolved AUTO and packed with AwqGEMMLinear instead of the requested AwqTorchLinear, which changes which shapes are quantizable.

New flow

_load_backend holds the normalized enum/string from the load boundary. A typed BackendConfig still uses _backend_config and carries its options. quantize(backend=AUTO) prefers the typed choice, then the explicit enum/string choice, then AUTO. A quantize-time explicit backend is a one-shot override and does not replace the retained load selection.

flowchart TD
    A["load(backend=X)"] --> B{"BackendConfig instance?"}
    B -- "yes" --> C["qcfg._backend_config = X"]
    B -- "no" --> D["normalize_backend(X)<br/>invalid -> ValueError/TypeError"]
    D --> E{"X == AUTO?"}
    E -- "no" --> F["qcfg._load_backend = X"]
    E -- "yes" --> G["clear retained selection"]
    C --> H["quantize(backend=AUTO)"]
    F --> H
    G --> H
    H --> I{"typed or enum retained?"}
    I -- "yes" --> J["resolve the retained selection"]
    I -- "no" --> K["BACKEND.AUTO chooser"]
    classDef failure fill:#ffe0e8,stroke:#b30000,color:#111;
    class D failure;
Loading
Old mechanism on 97c377509 New contract
Inherit read only _backend_config (backend_config.py:276) Reads typed first, then _load_backend (backend_config.py:277-283)
Recorded only BackendConfig (backend_config.py:302) Records typed or normalized non-AUTO enum/string (backend_config.py:309-320)
Enum load silently became AUTO at quantize Enum load is inherited like the typed form

The documented behavior and the typed/enum/AUTO table are updated in docs/backend-selection.md.

Tests

tests/test_backend_config_reuse_4446.py now parametrizes the typed and enum spellings:

Selection at load quantize(backend=AUTO) expected
TorchConfig() BACKEND.GPTQ_TORCH
BACKEND.GPTQ_TORCH BACKEND.GPTQ_TORCH
AwqTorchConfig() BACKEND.AWQ_TORCH
BACKEND.AWQ_TORCH BACKEND.AWQ_TORCH

It also asserts the issue's exact AWQ enum case resolves AWQ_TORCH (not AWQ_GEMM/AUTO), and that "not-a-backend", an integer, and an arbitrary object raise at load and leave no retained selection.

# before (backend_config.py reverted, tests kept)
$ pytest tests/test_backend_config_reuse_4446.py -q -k "load_backend_selection or explicit_enum_load or unknown_load_backend"
3 failed, 5 passed, 8 deselected
# the three enum cases resolved BACKEND.AUTO; typed cases and invalid-value cases passed

# after
$ pytest tests/test_backend_config_reuse_4446.py -q
16 passed

$ pytest tests/test_backend_config_reuse_4446.py tests/test_backend_config.py tests/test_runtime_config_4448.py -q
68 passed

$ HIP_VISIBLE_DEVICES=1 pytest tests/test_backend_config_reuse_4446.py -q
16 passed

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

Environment for every run: OMP_NUM_THREADS=4 MKL_NUM_THREADS=4 OPENBLAS_NUM_THREADS=4 NUMEXPR_NUM_THREADS=4, PYTHONPATH=$PWD.

The two fake-load harnesses in tests/test_backend_config_reuse_4446.py and tests/test_backend_config.py now return a no-op log_kv_cache_estimate, which current GPTQModel.load calls on the returned model. Without that test-only update those files fail on current main before reaching any backend assertion; no production behavior changed for it.

Hardware

CPU-only config/selection logic. CUDA venv /opt/venvs/cu130t (Py3.14t, torch 2.16.0.dev+cu130) and ROCm venv /opt/venvs/rocm101t. No GPU kernel or real model was executed.

Limitations

  • No end-to-end real-model AWQ quantize/pack run (the issue's FalconMamba shape); validation is the loader-equivalent resolution and the fake-load path.
  • A quantize-time explicit backend remains a one-shot override; it does not replace the load-retained selection for a later quantize(backend=AUTO).
  • Valid enum members that are incompatible with the chosen quantize method are still rejected by the existing backend selection/compatibility checks, not by a new check in this function.

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