Skip to content

fix(cuda): guard OOB absmax read in kDequantizeBlockwise for partial tail tiles - #2168

Open
Cek2001 wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
Cek2001:fix/dequantize-blockwise-absmax-oob
Open

Cek2001 wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
Cek2001:fix/dequantize-blockwise-absmax-oob

Conversation

@Cek2001

@Cek2001 Cek2001 commented Dec 2, 2026

Copy link
Copy Markdown

Problem

In csrc/kernels.cu (kDequantizeBlockwise, launched with TILE_SIZE=512, THREADS=64, NUM_PER_TH=8 for both 8-bit dequantize_blockwise and 4-bit dequantize_4bit), line 498 unconditionally loads absmax for all 64 threads in the block without checking whether the thread's slice is within valid_items_load:

// csrc/kernels.cu:486-499
if (DATA_TYPE > 0) {
    valid_items_load = min(TILE_SIZE, static_cast<int>((static_cast<int64_t>(n) + 1) / 2) - i);
    valid_items_store = min(TILE_SIZE * 2, n - i * 2);
} else {
    valid_items_load = min(TILE_SIZE, n - i);
    valid_items_store = valid_items_load;
}

local_abs_max = __ldg(&absmax[(i + threadIdx.x * NUM_PER_TH) >> (31 - __clz(blocksize))]);

On any partial tail tile (e.g., n = 1, blocksize = 64, where absmax has only 1 float = 4 bytes), threads with threadIdx.x * NUM_PER_TH >= valid_items_load compute indices 1..7 and perform out-of-bounds 4-byte global reads past the end of absmax (affecting both 8-bit dequantize_blockwise and 4-bit dequantize_4bit fp4/nf4).

Minimal Reproduction

PYTORCH_NO_CUDA_MEMORY_CACHING=1 CUDA_LAUNCH_BLOCKING=1 \
compute-sanitizer --tool memcheck python3 -c "
import torch, bitsandbytes.functional as bF
x = torch.randn(1, dtype=torch.float16, device='cuda')
q, qs = bF.quantize_blockwise(x, blocksize=64, nested=False)
dq = bF.dequantize_blockwise(q, 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 kDequantizeBlockwise<__half, (int)512, (int)64, (int)8, (int)0>(float *, unsigned char *, float *, T1 *, int, int)+0x5e0
=========     by thread (32,0,0) in block (0,0,0)
=========     Access to 0x7fb71b200610 is out of bounds

(Same OOB read occurs for 4-bit dequantize_4bit with nf4/fp4, e.g., n = 1 or n = 17, blocksize = 64.)

Fix

Guard the absmax load with if (threadIdx.x * NUM_PER_TH < valid_items_load):

  • For 8-bit (DATA_TYPE == 0), valid_items_load and blocksize are in elements (ops.cu:97).
  • For 4-bit (DATA_TYPE > 0), valid_items_load is in packed bytes and ops.cu:94 passes blocksize / 2 in packed bytes.
  • In both paths, threadIdx.x * NUM_PER_TH < valid_items_load ensures (i + threadIdx.x * NUM_PER_TH) / blocksize stays strictly within [0, num_blocks), and threads outside valid_items_load neither load nor store valid elements. Full tiles (valid_items_load == 512) have zero branch divergence.

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 dequantize_blockwise(n=1, bs=64), dequantize_4bit(n=1, nf4, bs=64), and dequantize_4bit(n=17, fp4, bs=64): 34 errors → 0 errors on every case.
  • Added TestInt8BlockwiseQuantOps::test_dequantize_blockwise_partial_tile and Test4bitBlockwiseQuantOps::test_dequantize_4bit_partial_tile (numel in [1, 17, 65]): passed on both CPU and CUDA.
  • Existing pytest tests/test_functional.py -k "dequantize_blockwise or dequantize_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