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
3 changes: 1 addition & 2 deletions csrc/kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1491,9 +1491,8 @@ __global__ void kgemm_4bit_inference_naive(
// This is equivalent to (i+threadId.x*NUM_PER_TH)/blocksize.
const int absidx = ((2 * offset_B) + inner_idx) >> (31 - __clz(blocksize));

local_absmax = __ldg(&(absmax[absidx]));

if (row_B < M) {
local_absmax = __ldg(&(absmax[absidx]));
if ((inner_idx_halved + num_values_8bit) < (K / 2)) {
// this is the most important for performance considerations
reinterpret_cast<int4(&)[num_values_8bit]>(local_B_4bit)[0] =
Expand Down
26 changes: 26 additions & 0 deletions tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,32 @@ def test_gemv_4bit(self, device, dtype, storage_dtype, quant_type, blocksize):

opcheck(torch.ops.bitsandbytes.gemv_4bit.default, (A, B_q, B.shape, absmax, code, blocksize))

@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("out_features", [1, 2, 3, 65])
def test_gemv_4bit_non_multiple_of_4_rows(self, device, dtype, quant_type, out_features):
"""Regression test for OOB absmax reads when M % 4 != 0 in kgemm_4bit_inference_naive."""
in_features = 256
blocksize = 64

torch.manual_seed(0)
A = torch.randn((1, in_features), dtype=dtype, device=device)
B = torch.randn((out_features, in_features), dtype=dtype, device=device)
B_q, qs = bitsandbytes.functional.quantize_4bit(
B, blocksize=blocksize, compress_statistics=False, quant_type=quant_type
)
out = bitsandbytes.functional.gemv_4bit(A, B_q.t(), state=qs)
deq = bitsandbytes.functional.dequantize_4bit(B_q, qs, quant_type=quant_type)
ref = torch.matmul(A.float(), deq.float().t()).to(dtype)

assert out.shape == (1, out_features)
assert out.dtype == dtype
assert torch.isfinite(out).all()
atol = 2e-1 if dtype == torch.bfloat16 else 5e-2
rtol = 5e-2 if dtype == torch.bfloat16 else 1e-2
torch.testing.assert_close(out, ref, rtol=rtol, atol=atol)

@pytest.mark.parametrize("device", get_available_devices())
@pytest.mark.parametrize("requires_grad", TRUE_FALSE, ids=id_formatter("requires_grad"))
@pytest.mark.parametrize("storage_dtype", [torch.uint8, torch.bfloat16], ids=id_formatter("storage_dtype"))
Expand Down