You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
gemv_4bit silently produces wrong results when weight is quantized in (in_features, out_features) layout #1862
The gemv_4bit CUDA kernel (the fast path for 1D inputs in matmul_4bit) implicitly assumes that quant_state.shape follows the nn.Linear convention of (out_features, in_features). If a weight matrix is quantized in the transposed layout (in_features, out_features), the gemv path silently produces wrong output shape and wrong values — no error is raised.
This does not affect production usage. All real-world code paths (Linear4bit, HuggingFace transformers, etc.) store weights as (out_features, in_features), so the gemv path works correctly. This is a documentation/validation issue for users of the low-level matmul_4bit API.
Root cause
In bitsandbytes/backends/cuda/ops.py, the gemv kernel uses:
# Line 431shape= (*A.shape[:-1], shapeB[0]) # output shape# Lines 482-484m=ct.c_int32(shapeB[0]) # treated as output dimensionk=ct.c_int32(shapeB[1]) # treated as input dimension
This hardcodes shapeB[0] as the output dimension. When quant_state.shape = (out_features, in_features) (the nn.Linear convention), this is correct. When quant_state.shape = (in_features, out_features), the kernel reads the wrong number of input elements and produces the wrong number of output elements.
Meanwhile, MatMul4Bit.forward() (the 2D matrix path) handles both conventions transparently:
Addressed in #1949. matmul_4bit now detects weights quantized in (in_features, out_features) orientation and raises a DeprecationWarning, still producing correct output via fallback. Support for that layout will be removed in a future release.
Activity
matthewdouglas commentedon May 15, 2026
Addressed in #1949.
matmul_4bitnow detects weights quantized in(in_features, out_features)orientation and raises aDeprecationWarning, still producing correct output via fallback. Support for that layout will be removed in a future release.