Skip to content

Keep the packed storage dtype when casting a quantized Linear4bit (float quant_storage) - #2173

Open
chaitanya0305 wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
chaitanya0305:fix/params4bit-float-storage-cast
Open

chaitanya0305 wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
chaitanya0305:fix/params4bit-float-storage-cast

Conversation

@chaitanya0305

Copy link
Copy Markdown

Summary

A quantized Linear4bit with a floating-point quant_storage silently corrupts its weight on a module dtype cast. The affected storages are bfloat16, float16 and float32; bfloat16 is 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.Module treats it like any other float parameter and casts it numerically, which rewrites the packed bytes.

import copy, torch, bitsandbytes as bnb

torch.manual_seed(0)
lin = torch.nn.Linear(64, 32).to(torch.bfloat16)
m = bnb.nn.Linear4bit(64, 32, quant_type="nf4", quant_storage=torch.bfloat16, compute_dtype=torch.bfloat16)
m.load_state_dict(lin.state_dict())
m = m.to("cpu")  # quantize
x = torch.randn(4, 64, dtype=torch.bfloat16)
y = m(x)

m16 = copy.deepcopy(m).half()  # same with .to(torch.float16)
print(m16.weight.dtype)  # torch.float16: the packed codes were cast
print((m16(x.half()).float() - y.float()).abs().max())  # tensor(2.5156): silently wrong

copy.deepcopy(m).float()(x.float())  # RuntimeError: shape '[32, 32]' is invalid for input of size 2048

With this PR the weight stays torch.bfloat16, and the difference is 0.

storage cast to before
bfloat16 float16 wrong outputs, no error
bfloat16 / float16 float32 RuntimeError in dequantize
float32 float16 RuntimeError in dequantize
uint8 anything fine, because nn.Module skips non-float params

PEFT's prepare_model_for_kbit_training already special-cases Params4bit when it upcasts parameters, to avoid exactly this. Any other code that calls model.half(), model.float() or model.to(dtype) on a bnb model built with a float storage still hits it.

Fix

  • In Params4bit.to(), ignore dtype once the weight is quantized (bnb_quantized=True). A quantized weight keeps its packed storage dtype, which is what nn.Module already does for the default uint8 storage. Unquantized weights, and moves between devices, behave as before.
  • nn.Module.half(), .float(), .bfloat16() and .double() call the tensor methods directly, not .to(). Params4bit now 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_weight to tests/test_linear4bit.py. It covers every quant_storage, crossed with .to(float16), .to(float32), .half(), .float() and .bfloat16(). It checks that:

  • the weight is still a Params4bit with its storage dtype;
  • the packed bytes are unchanged;
  • dequantization matches the original;
  • the layer output matches the reference.

Results:

  • Before: 20 of 40 cases fail on CPU and MPS (every float storage cast to a different dtype).
  • After: all 40 pass.
  • pytest tests/test_linear4bit.py tests/test_modules.py -k cpu: 416 passed.
  • pre-commit run on the changed files passes.

I tested on macOS arm64 (CPU and MPS).

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant