From f15705478b0da9f4517e33b65a2db7a99c562e36 Mon Sep 17 00:00:00 2001 From: Cek2001 <92498570+Cek2001@users.noreply.github.com> Date: Wed, 2 Dec 2026 11:26:42 +0800 Subject: [PATCH] fix(cuda): guard OOB absmax read in kDequantizeBlockwise for partial tail tiles --- csrc/kernels.cu | 4 +++- tests/test_ops.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/csrc/kernels.cu b/csrc/kernels.cu index df0f093df..f756856c4 100644 --- a/csrc/kernels.cu +++ b/csrc/kernels.cu @@ -495,7 +495,9 @@ __global__ void // Since blocksize will always be a power-of-2, we avoid more expensive // division by the blocksize and instead use a shift operation. // This is equivalent to (i+threadId.x*NUM_PER_TH)/blocksize. - local_abs_max = __ldg(&absmax[(i + threadIdx.x * NUM_PER_TH) >> (31 - __clz(blocksize))]); + if (threadIdx.x * NUM_PER_TH < valid_items_load) { + local_abs_max = __ldg(&absmax[(i + threadIdx.x * NUM_PER_TH) >> (31 - __clz(blocksize))]); + } __syncthreads(); LoadChar(loadchar).Load(&(A[i]), qvals, valid_items_load, 128); diff --git a/tests/test_ops.py b/tests/test_ops.py index b38521bfb..f65fc9e39 100644 --- a/tests/test_ops.py +++ b/tests/test_ops.py @@ -133,6 +133,21 @@ def test_dequantize_blockwise(self, device, dtype, blocksize): opcheck(torch.ops.bitsandbytes.dequantize_blockwise.default, (A, absmax, code, blocksize, dtype)) + @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) + @pytest.mark.parametrize("numel", [1, 17, 65]) + def test_dequantize_blockwise_partial_tile(self, device, dtype, numel): + """Regression test for OOB absmax reads in kDequantizeBlockwise on partial tail tiles.""" + blocksize = 256 if device == "cpu" else 64 + code = bitsandbytes.functional.create_dynamic_map().to(device, dtype=torch.float32) + A = torch.randn((numel,), dtype=dtype, device=device) + q, absmax = torch.ops.bitsandbytes.quantize_blockwise(A, code, blocksize) + out = torch.ops.bitsandbytes.dequantize_blockwise(q, absmax, code, blocksize, dtype) + + assert out.shape == A.shape + assert out.dtype == dtype + assert torch.isfinite(out).all() + class Test4bitBlockwiseQuantOps: @pytest.mark.parametrize("device", get_available_devices()) @@ -219,6 +234,21 @@ def test_dequantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksi (A, absmax, blocksize, quant_type, shape, dtype), ) + @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) + @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) + @pytest.mark.parametrize("numel", [1, 17, 65]) + def test_dequantize_4bit_partial_tile(self, device, dtype, quant_type, numel): + """Regression test for OOB absmax reads in 4-bit kDequantizeBlockwise on partial tail tiles.""" + blocksize = 64 + A = torch.randn((numel,), dtype=dtype, device=device) + q, absmax = torch.ops.bitsandbytes.quantize_4bit(A, blocksize, quant_type, torch.uint8) + out = torch.ops.bitsandbytes.dequantize_4bit(q, absmax, blocksize, quant_type, A.shape, dtype) + + assert out.shape == A.shape + assert out.dtype == dtype + assert torch.isfinite(out).all() + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("storage_dtype", [torch.uint8, torch.bfloat16], ids=id_formatter("storage_dtype"))