Skip to content

Illegal memory access with quantize_4bit #1782

Closed

Description

@davidmezzetti

Running the latest version of bitsandbytes with all recent versions of Torch is leading to the following error. I've tried bitsandbytes version from 0.45.5 to the latest (0.48.0) and all lead to the same result.

import torch
from bitsandbytes.functional import quantize_4bit

data = torch.rand(3000000, 384, device="cuda")
quantize_4bit(data)
Error an illegal memory access was encountered at line 57 in file /src/csrc/ops.cu

I'm running this on Python 3.10, Linux and with a RTX 3090. It seems like there is more than enough free GPU memory.

Activity

  1. matthewdouglas commented on Oct 16, 2025

    @matthewdouglas
    Member

    Hi @davidmezzetti

    Thank you for reporting this. I can reproduce this with RTX 4090, Python 3.13, PyTorch 2.9.0+cu130.

    Additionally, I can reproduce the same issue for the 8bit quantize_blockwise functionality.

    This issue appears when we have a tensor with more than 2**30 elements. I also see an issue with the dequantization kernel as we approach 2**31 elements. I'll open up a PR to fix this soon!

  2. added
    CUDAIssues and PRs related to the CUDA backend, excluding installation/support help.
    on Oct 16, 2025
  3. added this to the v0.48.2 milestone on Oct 16, 2025
  4. davidmezzetti commented on Oct 16, 2025

    @davidmezzetti
    Author

    Thank you for the rapid response! Glad to hear it looks to be a simple fix.

  5. matthewdouglas commented on Oct 21, 2025

    @matthewdouglas
    Member

    @davidmezzetti I've opened #1784 to fix this, with the caveat that the upper limit is now 2**31-1 instead of 2**30 elements. So for fp32 data, this works on tensors up to 8GiB. Do you expect to need support for larger sizes? If so, we can do that, but it's a little more involved, so I would prefer we open up a feature request on that.

  6. davidmezzetti commented on Oct 21, 2025

    @davidmezzetti
    Author

    Thank you for the update! I really appreciate it.

    Ideally I would like the GPU to set the memory limit vs a software imposed limit. Is there anything I can do to help?

  7. matthewdouglas commented on Oct 21, 2025

    @matthewdouglas
    Member

    The main issue is that our C API uses int32 for tensor sizes. While we can change that to int64, it would be BC breaking for some users like Unsloth who use the C API directly, particularly for 4bit.

    Instead I could consider adding int64 versions and dispatch to those for larger problem sizes. But I want to treat that as a separate feature request since all of the kernels are designed for at most 2**31-1 with the usage of int32 everywhere. We would have to also consider if we want support for > 2**31-1 in any single dimension (which I assume we do).

  8. davidmezzetti commented on Oct 22, 2025

    @davidmezzetti
    Author

    @matthewdouglas Makes sense. I was looking at the CUDA code and wondering why that variable couldn't simply be changed to a 64-bit int but this explanation explains that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CUDAIssues and PRs related to the CUDA backend, excluding installation/support help.

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions