Repository navigation
Keep the packed storage dtype when casting a quantized Linear4bit (float quant_storage) - #2173
Open
chaitanya0305 wants to merge 1 commit into
Conversation
With a floating-point quant_storage (bfloat16/float16/float32, as used for FSDP-QLoRA) the packed 4-bit weight is a floating-point tensor, so nn.Module.to(dtype), .half(), .float() and .bfloat16() cast the packed bytes numerically. The layer then returned wrong outputs (bf16 storage cast to fp16) or failed to dequantize (any storage cast to a dtype of a different width). Ignore the dtype in Params4bit.to() once the weight is quantized, as nn.Module already does for uint8 storage, and route half/float/bfloat16/ double through to().
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.
Summary
A quantized
Linear4bitwith a floating-pointquant_storagesilently corrupts its weight on a module dtype cast. The affected storages arebfloat16,float16andfloat32;bfloat16is the FSDP-QLoRA setup. The affected casts are.to(dtype),.half(),.float()and.bfloat16().The packed 4-bit weight is then a floating-point tensor.
nn.Moduletreats it like any other float parameter and casts it numerically, which rewrites the packed bytes.With this PR the weight stays
torch.bfloat16, and the difference is 0.RuntimeErrorin dequantizeRuntimeErrorin dequantizenn.Moduleskips non-float paramsPEFT's
prepare_model_for_kbit_trainingalready special-casesParams4bitwhen it upcasts parameters, to avoid exactly this. Any other code that callsmodel.half(),model.float()ormodel.to(dtype)on a bnb model built with a float storage still hits it.Fix
Params4bit.to(), ignoredtypeonce the weight is quantized (bnb_quantized=True). A quantized weight keeps its packed storage dtype, which is whatnn.Modulealready does for the defaultuint8storage. Unquantized weights, and moves between devices, behave as before.nn.Module.half(),.float(),.bfloat16()and.double()call the tensor methods directly, not.to().Params4bitnow overrides these four methods and routes them through.to()when quantized.The bias and other parameters are still cast as usual.
Tests
I added
test_linear4bit_dtype_cast_keeps_packed_weighttotests/test_linear4bit.py. It covers everyquant_storage, crossed with.to(float16),.to(float32),.half(),.float()and.bfloat16(). It checks that:Params4bitwith its storage dtype;Results:
pytest tests/test_linear4bit.py tests/test_modules.py -k cpu: 416 passed.pre-commit runon the changed files passes.I tested on macOS arm64 (CPU and MPS).