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(kDequantizeBlockwise, launched withTILE_SIZE=512, THREADS=64, NUM_PER_TH=8for both 8-bitdequantize_blockwiseand 4-bitdequantize_4bit), line 498 unconditionally loadsabsmaxfor all 64 threads in the block without checking whether the thread's slice is withinvalid_items_load:On any partial tail tile (e.g.,
n = 1, blocksize = 64, whereabsmaxhas only 1 float = 4 bytes), threads withthreadIdx.x * NUM_PER_TH >= valid_items_loadcompute indices1..7and perform out-of-bounds 4-byte global reads past the end ofabsmax(affecting both 8-bitdequantize_blockwiseand 4-bitdequantize_4bitfp4/nf4).Minimal Reproduction
Observed before this fix (NVIDIA A100-PCIE-40GB, CUDA 13.3,
main @ be0de79):(Same OOB read occurs for 4-bit
dequantize_4bitwithnf4/fp4, e.g.,n = 1orn = 17,blocksize = 64.)Fix
Guard the
absmaxload withif (threadIdx.x * NUM_PER_TH < valid_items_load):DATA_TYPE == 0),valid_items_loadandblocksizeare in elements (ops.cu:97).DATA_TYPE > 0),valid_items_loadis in packed bytes andops.cu:94passesblocksize / 2in packed bytes.threadIdx.x * NUM_PER_TH < valid_items_loadensures(i + threadIdx.x * NUM_PER_TH) / blocksizestays strictly within[0, num_blocks), and threads outsidevalid_items_loadneither 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 1024ondequantize_blockwise(n=1, bs=64),dequantize_4bit(n=1, nf4, bs=64), anddequantize_4bit(n=17, fp4, bs=64): 34 errors → 0 errors on every case.TestInt8BlockwiseQuantOps::test_dequantize_blockwise_partial_tileandTest4bitBlockwiseQuantOps::test_dequantize_4bit_partial_tile(numel in [1, 17, 65]): passed on both CPU and CUDA.pytest tests/test_functional.py -k "dequantize_blockwise or dequantize_4bit": all passed.