diff --git a/src/diffusers/models/model_loading_utils.py b/src/diffusers/models/model_loading_utils.py index 74fbe9c61f91..421324030aac 100644 --- a/src/diffusers/models/model_loading_utils.py +++ b/src/diffusers/models/model_loading_utils.py @@ -736,7 +736,11 @@ def load_gguf_checkpoint(gguf_checkpoint_path, return_tensors=True): 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( @@ -749,6 +753,9 @@ def load_gguf_checkpoint(gguf_checkpoint_path, return_tensors=True): ) 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 _TorchStateDict(parsed_parameters, metadata=metadata) 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..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() @@ -29,7 +30,27 @@ # 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): + 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