Repository navigation
refactor(kernel): select kernels, numerics and threads through KernelConfig, not env vars (#4180) - #4364
Merged
Conversation
…Config, not env vars (#4180) AGENTS.md forbids environment variables that choose a code path, a numerical path, or a thread count, and forbids tests that pick a path with monkeypatch.setenv. This replaces every remaining GPTQMODEL_* / GPTQ_TORCH_* behaviour switch with an explicit field on a new KernelConfig, installed by BaseQuantizeConfig.kernel (quantization) or GPTQModel.load(..., kernel=...) (inference). Native kernels cannot read a Python object, so the four C/CUDA switches become op arguments derived from the same policy: the AWQ fused split-K reduce, the Tenstorrent block-float cuBLAS prefill, the FloatX SIMD level, and the two Swordfish dispatch switches. The policy is runtime-only: it is never serialized into the checkpoint, because the host that loads a checkpoint is not the host that produced it. Defaults match the retired variables' defaults, and an unhonourable value is rejected rather than clamped, so a typo cannot silently change numerics. Tests drive the API argument directly; the monkeypatch.setenv calls that chose a path are gone. docs/kernel-config.md is the reference page, with the decision flow and the before/after mechanism table.
Collaborator
Author
|
CI note: the
|
Qubitium
pushed a commit
that referenced
this pull request
Nov 16, 2026
…4366) #4364 moved ParoQuantTritonLinear onto KernelConfig but dropped the accessor import, so constructing the Triton ParoQuant kernel raised NameError: kernel = resolve_kernel_config(kwargs.get("kernel")) ^^^^^^^^^^^^^^^^^^^^^ The default backend selection never instantiates that class on a CPU-only host, so the suite stayed green while the class was unimportable-in-practice. Add the import and a regression test that asserts the symbol resolves and the constructor still reads the policy. Also sort the import blocks the new imports made unsorted in #4364 (this is what the Ruff Check gate reported for 13 files) and drop the now-unused env_flag import in qqq.py. format/check.sh is green again.
This was referenced Nov 16, 2026
Merged
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.
TL;DR
Closes #4180. Every remaining
GPTQMODEL_*/GPTQ_TORCH_*/APHRODITE_*switch that selected a kernel, a numerical path, or a thread count is replaced by
a field on a new
KernelConfig. Net: 103 files, +1633/−645.The architectural consequence: the decision moved from the process
environment, read at the point of use to an object the caller owns and passes
in. Defaults are unchanged, so no existing run changes numerics.
1. The problem
AGENTS.mdsays runtime behaviour is selected by the config object or anexplicit API argument, never by an environment variable, and that tests must not
use
monkeypatch.setenvto pick a code path. Fifteen groups of reads stillviolated that. The old flow made the path a property of the process, not of the
run:
flowchart TD A["worker thread or test calls os.environ"] --> B["env_flag / os.getenv at point of use"] B --> C["fused vs eager GPTQ block update"] B --> D["fused vs eager AWQ split-K reduce"] B --> E["Marlin fp32 vs reduced precision"] B --> F["thread counts, tile sizes, capture roots"] C --> G["numerics depend on process env"] D --> G E --> G F --> G G --> H["same checkpoint, different bytes"]Mechanisms removed, with
file:lineonmainbefore this change (full table inthe committed docs page):
env_flag("GPTQMODEL_DISABLE_GPTQ_CUDA")quantization/gptq.py:2490,quantization/qqq.py:885KernelConfig.gptq_fused_block_updateos.getenv("GPTQMODEL_NPU_CHOLESKY_BLOCK_SIZE")at importquantization/npu_linalg.py:9KernelConfig.npu_cholesky_block_sizepassed per callGPTQ_TORCH_*nn_modules/qlinear/torch.py:245-276KernelConfig.torch_*GPTQMODEL_MARLIN_USE_FP32nn_modules/qlinear/marlin.py:158KernelConfig.marlin_fp32GPTQMODEL_FP32_ACCUMgemm_awq.py:30,gemm_awq_triton.py:32,quantization/awq/modules/triton/gemm.py:28,quantization/paroquant/modules/triton/gemm.py:32KernelConfig.fp32_accumGPTQMODEL_AWQ_TRITON_SCHEDULEat importquantization/awq/modules/triton/scheduler.py:27KernelConfig.awq_triton_scheduleGPTQMODEL_GGUF_FUSED_*,GGUF_CPP_NATIVE_MATMUL,GGUF_CPP_THREADSqlinear/gguf.py:1197-1215,qlinear/gguf_cpp.py:445,1190KernelConfig.gguf_*GPTQMODEL_PAROQUANT_*behaviour switchesqlinear/paroquant.py:53-66,paroquant_triton.py:66-70,quantization/paroquant/**KernelConfig.paroquant_*GPTQMODEL_BITSANDBYTES_*,GPTQMODEL_BNB_TRITON,GPTQMODEL_DISABLE_BITBLASqlinear/bitsandbytes.py:49,57,bitsandbytes_triton.py:109,bitblas.py:115KernelConfig.bitsandbytes_*,disable_bitblasGPTQMODEL_GSQ_DISABLE_*_RELAXATIONquantization/gsq_cuda_relaxation.py:400-403KernelConfig.gsq_disable_*GPTQMODEL_EXL3_*exl3_lib/quantize.py:136,exllamav3_processor.py:127-130KernelConfig.exl3_*GPTQMODEL_ROCM_QUANT_BLAS,GPTQMODEL_NO_ROCM_LLCutils/rocm.py,rocm_llc.py:414KernelConfig.rocm_quant_blas,rocm_llcGPTQMODEL_FLOATX_CPU_THREADS,MAX_PACKERS,CPU_WORKERS,CUDA_WORKERSdtype.py:166,utils/model.py:1493,__init__.py:394,411KernelConfig.floatx_cpu_threads,max_packers,cpu_workers,cuda_workersGPTQMODEL_CPU_BF16X3,GPTQMODEL_EXL3_CPU_BF16X3utils/cpu_features.py:17,103,nn_modules/exllamav3_torch.py:35,46KernelConfig.cpu_bf16x3_matmulGPTQMODEL_FLOATX_CPU_DISABLE_AVX2/_AVX512in C++gptqmodel_ext/floatx_cpu.cpp:371-386simd_levelargumentGPTQMODEL_AWQ_DISABLE_FUSED_SPLITK_REDUCEin C++gptqmodel_ext/awq/quantization/gemm_cuda_gen.cu:160fused_splitk_reduceargumentGPTQMODEL_BLOCKFLOAT_NO_CUBLASin C++tt_blockfloat_cuda.cu:723allow_cublasargumentAPHRODITE_SWORDFISH_DENSE_M,_DETERMINISTICin C++swordfish_mm.cu:84,106dense_m_override,deterministicarguments2. The new flow
flowchart TD A["QuantizeConfig(kernel=KernelConfig(...))"] --> B["BaseQuantizeConfig.__post_init__<br/>_normalize_kernel_config"] C["GPTQModel.load(..., kernel=KernelConfig(...))"] --> D["GPTQModel.load"] D --> E["configure_kernel(kernel)"] B --> F["ModuleLooper.__init__<br/>configure_kernel(quantize_config.kernel)"] B --> G["WeightOnlyLooper.__init__<br/>configure_kernel(quantize_config.kernel)"] F --> E G --> E E --> H["_ACTIVE_CONFIG (process-wide, RLock)"] H --> I["resolve_kernel_config(owner)"] I --> J["Python kernels: TorchLinear, MarlinLinear,<br/>GGUF, ParoQuant, bitsandbytes"] I --> K["Native op arguments"] J --> L["run"] K --> LPrecedence: an explicit
owner.kernelwins over the process policy, so acaller holding a config never inherits another run's tuning.
configure_kernel(None)restores the documented defaults.
3. New core
KernelConfigis a dataclass with typed fields and a__post_init__validator.to_dict()exists for diagnostics; the field is not written toquantize_config.json, because the host that loads a checkpoint is not the hostthat produced it (same reasoning as
compile).4. Failure and ambiguity contract
flowchart LR A["KernelConfig(field=value)"] --> B{"__post_init__ type and range check"} B -- valid --> C["policy installed"] B -- wrong type or out of range --> D["TypeError / ValueError, fail closed"] B -- None on an optional field --> E["documented default kept"]ValueErrorat constructiontorch_stream_tile=0)ValueErrormarlin_fp32="no")TypeErrorrocm_quant_blas=""None)rocm_llc=FalsethenTrue5. Change inventory
gptqmodel/utils/kernel.pyKernelConfig,configure_kernel,resolve_kernel_configquantization/config.py,models/{auto,loader}.py,looper/{module,weight_only}_looper.pyKernelConfiginstead of the environment.cu/.cpp+ 3 bindingsgetenvmonkeypatch.setenvdocs/kernel-config.md,README.md,docs/README.md6. Tests / guarantees
tests/test_kernel_config.py::test_defaults_are_the_retired_environment_defaults::test_invalid_values_are_rejected(16 cases)::test_policy_is_process_wide_and_replaceableconfigure_kernelreplaces,resetrestores::test_owner_config_wins_over_the_process_policy::test_rocm_quant_blas_keeps_an_explicit_empty_opt_out""≠None::test_kernel_field_is_runtime_onlytests/test_gptq_cuda.pytests/kernels/test_awq_cuda_fp32_reduce.pytests/test_tt_blockfloat_cuda.pytests/kernels/test_inference_numeric_range.py7. Validation
Also verified end to end on device: fused vs eager GPTQ quantization is
bit-identical through
KernelConfig(gptq_fused_block_update=...); the AWQfused vs eager split-K reduce agree; the blockfloat cuBLAS and fused paths both
produce the documented result; FloatX SIMD 0/1/2 agree. The AWQ CUDA extension
was rebuilt with the new op schema.
tests/test_gguf_all_formats.pyfailures(IQ* formats) reproduce unchanged on a clean
mainworktree and are due to amissing optional GGUF backend, not this change.
8. Reviewer notes
P2PConfigcontract. Thealternative — threading the object through every module constructor — was
rejected because modules are built deep inside the loader. The documented
precedence rule (
owner.kernelwins) is the escape hatch for that.swordfish_dense_mis0for "measured per-SM heuristic", not a plain intminimum, because
0is a meaningful value for the native kernel.floatx_cpu_simd_levelkeeps the old capability probe as level2; it doesnot disable SIMD by default.
GPTQMODEL_*variables that only affect build paths, cachedirectories, logging, or which model artifact is read. Those do not select a
code path and are listed as out of scope in the issue.