Skip to content

fix(cuda): guard OOB absmax read in kgemm_4bit_inference_naive when row_B >= M - #2167

Open
Cek2001 wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
Cek2001:fix/gemv-4bit-absmax-oob
Open

Cek2001 wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
Cek2001:fix/gemv-4bit-absmax-oob

Conversation

@Cek2001

@Cek2001 Cek2001 commented Dec 2, 2026

Copy link
Copy Markdown

Problem

In csrc/kernels.cu (kgemm_4bit_inference_naive), each thread block processes THREADS / BNB_WARP_SIZE = 4 rows of B (one warp per row). When M (output rows N in gemv_4bit) is not a multiple of 4 (e.g., N = 1, 2, 3, 65), warps with row_B >= M unconditionally read absmax[absidx] before checking if (row_B < M):

// csrc/kernels.cu:1492-1496
const int absidx = ((2 * offset_B) + inner_idx) >> (31 - __clz(blocksize));

local_absmax = __ldg(&(absmax[absidx]));   // OOB global read when row_B >= M

if (row_B < M) {

Under PyTorch's default CUDACachingAllocator (or adjacent cudaMalloc allocations without guard padding), this out-of-bounds read is silently masked.

Minimal Reproduction

PYTORCH_NO_CUDA_MEMORY_CACHING=1 CUDA_LAUNCH_BLOCKING=1 \
compute-sanitizer --tool memcheck --padding 1024 python3 -c "
import torch, bitsandbytes.functional as bF
a = torch.randn(1, 4096, dtype=torch.float16, device='cuda')
w = torch.randn(2, 4096, dtype=torch.float16, device='cuda')
qw, qs = bF.quantize_4bit(w, blocksize=64, compress_statistics=False, quant_type='nf4')
out = bF.gemv_4bit(a, qw.t(), state=qs)
torch.cuda.synchronize()
"

Observed before this fix (NVIDIA A100-PCIE-40GB, CUDA 13.3, main @ be0de79):

========= Invalid __global__ read of size 4 bytes
=========     at void kgemm_4bit_inference_naive<__half, (int)128, (int)16>(int, int, int, T1 *, unsigned char *, float *, const float *, T1 *, int, int, int, int)+0x280
=========     by thread (64,0,0) in block (0,0,0)
=========     Access to 0x7f2b33206a00 is out of bounds
=========     and is 1 bytes after the nearest allocation at 0x7f2b33206800 of size 512 bytes
...
========= ERROR SUMMARY: 66 errors

(thread (64,0,0) has warp_idx = 2, so row_B = 2 == M, reading past the 512-byte absmax tensor.)

Fix

Move local_absmax = __ldg(&(absmax[absidx])); inside the existing if (row_B < M) guard (local_absmax is already initialized to T(0.0f) and warps with row_B >= M never write to out).

Verification

Tested on NVIDIA A100-PCIE-40GB (sm_80, CUDA 13.3, PyTorch 2.13, compute-sanitizer 2026.2.0.0):

  • compute-sanitizer --tool memcheck --padding 1024 on gemv_4bit (N=2, K=4096, fp16, nf4 and N=1, K=8192, bf16, fp4): 66 errors → 0 errors.
  • Added Test4bitBlockwiseQuantOps::test_gemv_4bit_non_multiple_of_4_rows (out_features in [1, 2, 3, 65], fp16/bf16/fp32, fp4/nf4): passed on both CPU and CUDA.
  • Existing pytest tests/test_functional.py -k "gemv_4bit": all passed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant