Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/diffusers/models/model_loading_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
11 changes: 0 additions & 11 deletions src/diffusers/quantizers/gguf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Comment on lines -91 to -93

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this sufficiently available from the minimum gguf version we require in diffusers?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what there is not to be available in a removal, but I checked the behavior against gguf 0.10.0, the bf16 treatment seems to be the same and the test reproduces.


# TODO(Isotr0py): GGUF's MMQ and MMVQ implementation are designed for
# contiguous batching and inefficient with diffusers' batching,
# so we disabled it now.
Expand Down Expand Up @@ -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)
Comment on lines -443 to -444

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this removal not be version-guarded?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaict this is basically an internal helper that was used exclusively in dequantize_functions that, frankly, I don't know why you'd want to reach into diffusers.quantizers.gguf.utils to import. We can leave/deprecate it if we think someone might be depending on it, but I don't think it's a documented interface or anything.



# this part from calcuis (gguf.org)
# more info: https://github.com/calcuis/gguf-connector/blob/main/src/gguf_connector/quant2c.py

Expand Down Expand Up @@ -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,
Expand Down
23 changes: 22 additions & 1 deletion tests/quantization/gguf/test_gguf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,35 @@
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()


# 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
Expand Down