From a91783e931768bb7684a5364eade67926ac1a12d Mon Sep 17 00:00:00 2001 From: "Lin, KuanTing" Date: Thu, 19 Nov 2026 00:06:19 +0800 Subject: [PATCH] Restore QuantState nested_offset in its original dtype QuantState.from_dict rebuilt nested_offset with torch.tensor(float), which follows the global default dtype. Loaders that build models under torch.set_default_dtype(torch.bfloat16) (or float16) therefore got a rounded offset, and every dequantized absmax of a compress_statistics=True weight shifted by the rounding error. Build it with nested_dtype instead. Signed-off-by: Lin, KuanTing --- bitsandbytes/functional.py | 6 ++++-- tests/test_functional.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/bitsandbytes/functional.py b/bitsandbytes/functional.py index 33b2cd9ac..60948d5cf 100644 --- a/bitsandbytes/functional.py +++ b/bitsandbytes/functional.py @@ -520,12 +520,14 @@ def from_dict(cls, qs_dict: dict[str, Any], device: torch.device) -> "QuantState assert set(qs_dict.keys()).issubset(cls.valid_qs_keys) if "nested_absmax" in qs_dict: - offset = torch.tensor(float(qs_dict["nested_offset"])).to(device) + nested_dtype = getattr(torch, qs_dict["nested_dtype"]) + # Not the global default dtype, which loaders often set to bf16/fp16. + offset = torch.tensor(float(qs_dict["nested_offset"]), dtype=nested_dtype, device=device) state2 = cls( absmax=qs_dict["nested_absmax"].to(device), blocksize=qs_dict["nested_blocksize"], code=qs_dict["nested_quant_map"].to(device), - dtype=getattr(torch, qs_dict["nested_dtype"]), + dtype=nested_dtype, ) else: offset, state2 = None, None diff --git a/tests/test_functional.py b/tests/test_functional.py index e4cd6a128..3374a50f6 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -694,6 +694,28 @@ def test_4bit_compressed_stats(self, device, quant_type, blocksize, dtype): assert err.item() < 0.11 assert relerr.item() < 0.28 + @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) + @pytest.mark.parametrize("default_dtype", [torch.float16, torch.bfloat16], ids=describe_dtype) + def test_4bit_compressed_stats_from_dict_default_dtype(self, device, quant_type, default_dtype): + """from_dict must restore nested_offset exactly when the global default dtype is not float32.""" + if device == "hpu" and not is_supported_on_hpu(quant_type, torch.bfloat16): + pytest.skip("This configuration is not supported on HPU.") + + A1 = torch.randn(128, 128, device=device, dtype=torch.bfloat16) + qa, SA = F.quantize_4bit(A1, compress_statistics=True, quant_type=quant_type) + expected = F.dequantize_4bit(qa, SA) + + previous = torch.get_default_dtype() + torch.set_default_dtype(default_dtype) + try: + SB = F.QuantState.from_dict(SA.as_dict(packed=True), device=torch.device(device)) + finally: + torch.set_default_dtype(previous) + + torch.testing.assert_close(SB.offset, SA.offset, rtol=0, atol=0) + torch.testing.assert_close(F.dequantize_4bit(qa, SB), expected, rtol=0, atol=0) + @pytest.mark.parametrize("device", get_available_devices(no_cpu=True)) @pytest.mark.skipif(not get_available_devices(no_cpu=True), reason="No accelerator device") @pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16], ids=describe_dtype)