From 32eb12f782756cc315688c9c2fe33e65fa2d1d99 Mon Sep 17 00:00:00 2001 From: Michael Zuo Date: Tue, 24 Nov 2026 04:21:08 -0500 Subject: [PATCH 1/2] [gguf] load BF16 tensors as plain bfloat16 tensors gguf-py returns BF16 data as raw bytes. We kept those bytes in a GGUFParameter and only decoded them in GGUFLinear, so a BF16 tensor anywhere else (e.g. a norm weight) was used with the wrong shape and dtype. Treat BF16 like F16/F32 and return a regular tensor from load_gguf_checkpoint, and drop the BF16 dequantize path that is no longer reachable. Co-authored-by: Claude Opus 5.5 --- src/diffusers/models/model_loading_utils.py | 9 +++++++- src/diffusers/quantizers/gguf/utils.py | 11 ---------- tests/quantization/gguf/test_gguf.py | 24 ++++++++++++++++++++- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/diffusers/models/model_loading_utils.py b/src/diffusers/models/model_loading_utils.py index c35c30a023b1..58b2e8b6e155 100644 --- a/src/diffusers/models/model_loading_utils.py +++ b/src/diffusers/models/model_loading_utils.py @@ -722,7 +722,11 @@ def load_gguf_checkpoint(gguf_checkpoint_path, return_tensors=False): quant_type = tensor.tensor_type # if the tensor is a torch supported dtype do not use GGUFParameter - is_gguf_quant = quant_type not in [gguf.GGMLQuantizationType.F32, gguf.GGMLQuantizationType.F16] + is_gguf_quant = quant_type not in [ + gguf.GGMLQuantizationType.F32, + gguf.GGMLQuantizationType.F16, + gguf.GGMLQuantizationType.BF16, + ] if is_gguf_quant and quant_type not in SUPPORTED_GGUF_QUANT_TYPES: _supported_quants_str = "\n".join([str(type) for type in SUPPORTED_GGUF_QUANT_TYPES]) raise ValueError( @@ -735,6 +739,9 @@ def load_gguf_checkpoint(gguf_checkpoint_path, return_tensors=False): ) weights = torch.from_numpy(tensor.data.copy()) + if quant_type == gguf.GGMLQuantizationType.BF16: + # gguf-py returns BF16 data as raw bytes (uint8, last dim doubled) + weights = weights.view(torch.bfloat16) parsed_parameters[name] = GGUFParameter(weights, quant_type=quant_type) if is_gguf_quant else weights return parsed_parameters diff --git a/src/diffusers/quantizers/gguf/utils.py b/src/diffusers/quantizers/gguf/utils.py index e9fd8db82f9a..10a5cb3a8ea1 100644 --- a/src/diffusers/quantizers/gguf/utils.py +++ b/src/diffusers/quantizers/gguf/utils.py @@ -51,7 +51,6 @@ else: ops = None -UNQUANTIZED_TYPES = {gguf.GGMLQuantizationType.F32, gguf.GGMLQuantizationType.F16, gguf.GGMLQuantizationType.BF16} STANDARD_QUANT_TYPES = { gguf.GGMLQuantizationType.Q4_0, gguf.GGMLQuantizationType.Q4_1, @@ -87,11 +86,6 @@ def _fused_mul_mat_gguf(x: torch.Tensor, qweight: torch.Tensor, qweight_type: int) -> torch.Tensor: - # there is no need to call any kernel for fp16/bf16 - if qweight_type in UNQUANTIZED_TYPES: - weight = dequantize_gguf_tensor(qweight) - return x @ weight.to(x.dtype).T - # TODO(Isotr0py): GGUF's MMQ and MMVQ implementation are designed for # contiguous batching and inefficient with diffusers' batching, # so we disabled it now. @@ -440,10 +434,6 @@ def dequantize_blocks_Q2_K(blocks, block_size, type_size, dtype=None): return qs.reshape((n_blocks, -1)) -def dequantize_blocks_BF16(blocks, block_size, type_size, dtype=None): - return (blocks.view(torch.int16).to(torch.int32) << 16).view(torch.float32) - - # this part from calcuis (gguf.org) # more info: https://github.com/calcuis/gguf-connector/blob/main/src/gguf_connector/quant2c.py @@ -502,7 +492,6 @@ def dequantize_blocks_IQ4_XS(blocks, block_size, type_size, dtype=None): dequantize_functions = { gguf.GGMLQuantizationType.IQ4_NL: dequantize_blocks_IQ4_NL, gguf.GGMLQuantizationType.IQ4_XS: dequantize_blocks_IQ4_XS, - gguf.GGMLQuantizationType.BF16: dequantize_blocks_BF16, gguf.GGMLQuantizationType.Q8_0: dequantize_blocks_Q8_0, gguf.GGMLQuantizationType.Q5_1: dequantize_blocks_Q5_1, gguf.GGMLQuantizationType.Q5_0: dequantize_blocks_Q5_0, diff --git a/tests/quantization/gguf/test_gguf.py b/tests/quantization/gguf/test_gguf.py index 97f3c186f6d0..50f70e671f86 100644 --- a/tests/quantization/gguf/test_gguf.py +++ b/tests/quantization/gguf/test_gguf.py @@ -29,7 +29,29 @@ # Model-level GGUF tests live in `tests/models/testing_utils/quantization.py` and pipeline-level # ones in `tests/pipelines/testing_utils/quantization.py`. This module covers backend behavior that -# fits neither: CUDA kernel correctness. +# fits neither: checkpoint parsing and CUDA kernel correctness. +@is_quantization +@is_gguf +@require_gguf_version_greater_or_equal("0.10.0") +def test_load_gguf_checkpoint_bf16_tensor(tmp_path): + from diffusers.models.model_loading_utils import load_gguf_checkpoint + + path = str(tmp_path / "bf16.gguf") + expected = torch.tensor([[0.5, -1.0, 2.0], [3.0, 0.0, -0.25]], dtype=torch.bfloat16) + writer = gguf.GGUFWriter(path, arch="test") + writer.add_tensor("norm.weight", expected.view(torch.uint8).numpy(), raw_dtype=gguf.GGMLQuantizationType.BF16) + writer.write_header_to_file() + writer.write_kv_data_to_file() + writer.write_tensors_to_file() + writer.close() + + weight = load_gguf_checkpoint(path)["norm.weight"] + + assert not isinstance(weight, GGUFParameter) + assert weight.dtype == torch.bfloat16 + assert torch.equal(weight, expected) + + @is_quantization @is_gguf @nightly From 509fe2fb29f72da0814a3c4fd681eef748b283a2 Mon Sep 17 00:00:00 2001 From: Michael Zuo Date: Thu, 3 Dec 2026 01:03:06 -0500 Subject: [PATCH 2/2] import load_gguf_checkpoint at test module top --- tests/quantization/gguf/test_gguf.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/quantization/gguf/test_gguf.py b/tests/quantization/gguf/test_gguf.py index 50f70e671f86..0dcbb42fff55 100644 --- a/tests/quantization/gguf/test_gguf.py +++ b/tests/quantization/gguf/test_gguf.py @@ -22,6 +22,7 @@ if is_gguf_available(): import gguf + from diffusers.models.model_loading_utils import load_gguf_checkpoint from diffusers.quantizers.gguf.utils import GGUFParameter enable_full_determinism() @@ -34,8 +35,6 @@ @is_gguf @require_gguf_version_greater_or_equal("0.10.0") def test_load_gguf_checkpoint_bf16_tensor(tmp_path): - from diffusers.models.model_loading_utils import load_gguf_checkpoint - path = str(tmp_path / "bf16.gguf") expected = torch.tensor([[0.5, -1.0, 2.0], [3.0, 0.0, -0.25]], dtype=torch.bfloat16) writer = gguf.GGUFWriter(path, arch="test")