Repository navigation
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
csrc/kernels.cu(kgemm_4bit_inference_naive), each thread block processesTHREADS / BNB_WARP_SIZE = 4rows ofB(one warp per row). WhenM(output rowsNingemv_4bit) is not a multiple of 4 (e.g.,N = 1, 2, 3, 65), warps withrow_B >= Munconditionally readabsmax[absidx]before checkingif (row_B < M):Under PyTorch's default
CUDACachingAllocator(or adjacentcudaMallocallocations without guard padding), this out-of-bounds read is silently masked.Minimal Reproduction
Observed before this fix (NVIDIA A100-PCIE-40GB, CUDA 13.3,
main @ be0de79):(
thread (64,0,0)haswarp_idx = 2, sorow_B = 2 == M, reading past the 512-byteabsmaxtensor.)Fix
Move
local_absmax = __ldg(&(absmax[absidx]));inside the existingif (row_B < M)guard (local_absmaxis already initialized toT(0.0f)and warps withrow_B >= Mnever write toout).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 1024ongemv_4bit(N=2, K=4096, fp16, nf4andN=1, K=8192, bf16, fp4): 66 errors → 0 errors.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.pytest tests/test_functional.py -k "gemv_4bit": all passed.