Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Single File] Add GGUF support #9964

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open

[Single File] Add GGUF support #9964

wants to merge 17 commits into from

Conversation

DN6
Copy link
Collaborator

@DN6 DN6 commented Nov 19, 2024

What does this PR do?

Adds support for loading GGUF checkpoints via from_single_file.

import torch

from diffusers import FluxPipeline, FluxTransformer2DModel, GGUFQuantizationConfig

ckpt_path = (
    "https://huggingface.co/city96/FLUX.1-dev-gguf/blob/main/flux1-dev-Q2_K.gguf"
)
transformer = FluxTransformer2DModel.from_single_file(
    ckpt_path,
    quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
    torch_dtype=torch.bfloat16,
)
pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-dev",
    transformer=transformer,
    generator=torch.manual_seed(0),
    torch_dtype=torch.bfloat16,
)
pipe.enable_model_cpu_offload()
prompt = "A cat holding a sign that says hello world"
image = pipe(
    prompt_embeds=prompt_embeds, pooled_prompt_embeds=pooled_prompt_embeds
).images[0]
image.save("flux-gguf.png")

Notes:

  1. API for loading GGUF is a bit overkill, but it's consistent with quantized loading in from_pretrained. GGUF files have enough metadata that we can automatically infer everything we need from the file itself. We don't really need a quantization config, but it becomes necessary as we expand to support to other quant loading methods (BnB, TorchAO etc)

TODOS:

  • Benchmark loading and inference speed.
  • Verify output quality
  • Add tests

Fixes # (issue)

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@@ -204,7 +204,10 @@ def create_quantized_param(

module._parameters[tensor_name] = new_value

def check_quantized_param_shape(self, param_name, current_param_shape, loaded_param_shape):
def check_quantized_param_shape(self, param_name, current_param, loaded_param):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

GGUF needs to access the tensor quant type to run a shape check. So this needs to change from passing in shapes to passing in params directly.

Copy link
Member

Choose a reason for hiding this comment

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

Why not add this method to the gguf_quantizer.py file instead of modifying this? This would be a breaking change no?

Copy link
Member

Choose a reason for hiding this comment

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

I see you're already adding this to the GGUF quantizer class. So, maybe okay to not modify this?

import torch.nn as nn


def _replace_with_gguf_linear(model, compute_dtype):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

GGUF files contain a mix of quantized linear and unquantized linear layers. It's not trivial to selectively replace layers. We can replace all of them and then check the parameter type when running forward instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

nvm

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

compute_dtype=compute_dtype,
)
model._modules[name].source_cls = type(module)
# Force requires grad to False to avoid unexpected errors
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Force requires grad to False to avoid unexpected errors
# Force requires_grad to False to avoid unexpected errors

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.

4 participants