Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion csrc/kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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"))
Expand Down