Fix global dequantize buffer dtype mismatch across mixed-precision loads#4026
Conversation
Summary of ChangesHello @GoldenGrapeGentleman, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical issue preventing the sequential loading of 4-bit quantized models with differing data types within the same process. By introducing a dtype check for the global dequantization buffer, it ensures that the buffer is correctly reinitialized when the precision changes, thereby preventing runtime errors and enhancing the robustness of mixed-precision model workflows. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a critical bug that causes a crash due to a dtype mismatch in the global dequantize buffer when loading multiple 4-bit models with different precisions. The fix of adding a dtype check is appropriate and solves the issue. I've added a couple of suggestions to optimize the buffer allocation logic, which will prevent unnecessary re-allocation of the ABSMAX_BUFFER and improve performance slightly. Overall, this is a solid fix.
When loading multiple 4-bit quantized models with different dtypes in the same process (e.g. first bfloat16 then float16 in a notebook), fast_dequantize's WEIGHT_BUFFERS retains the dtype from the first allocation. Subsequent models receive dequantized weights in the stale dtype, causing torch.matmul to fail with: RuntimeError: expected mat1 and mat2 to have the same dtype, but got: c10::Half != c10::BFloat16 The buffer already checks whether its size is sufficient (resize_), but not whether its dtype still matches. This adds the dtype check to both the CUDA/HIP and XPU buffer paths. Tested on AMD Radeon PRO W7900 (gfx1100, ROCm 7.1): - bf16 → fp16 sequential load + 4bit LoRA backward: fixed - fp16 → bf16 sequential load: pass - Single dtype load (no regression): pass - SFTTrainer bf16 + 4bit: pass - 16-bit inference / sampling: pass
…ads (unslothai#4026) Fix global dequantize buffer dtype mismatch when loading multiple 4-bit models with different dtypes in the same process. Adds dtype check alongside existing None check for WEIGHT_BUFFER in both CUDA/HIP and XPU paths.
Summary
Fix
WEIGHT_BUFFERSdtype going stale when multiple 4-bit models are loaded with different dtypes in the same process. The global dequantize buffer checks whether its size is sufficient (resize_), but never whether its dtype still matches the current model — so loading a bfloat16 model followed by a float16 model causestorch.matmulto crash.Related: #4005 (fixed backward-path dtype casts; this fixes the root cause in the shared buffer layer).
Reproduction
Root Cause
fast_dequantize(..., use_global_buffer=True)inkernels/utils.pycaches aWEIGHT_BUFFERper device. The buffer is allocated once on first use and reused thereafter. When the requesteddtypechanges (e.g. bf16 → fp16), the stale buffer dtype propagates into the dequantized weight, creating a mismatch with the activation tensor.Fix
Add a dtype check alongside the existing
Nonecheck:Applied to both the CUDA/HIP and XPU buffer paths.
Testing
Tested on AMD Radeon PRO W7900 (gfx1100, ROCm 7.1, PyTorch 2.8.0):
cc @danielhanchen