Skip to content

Instantly share code, notes, and snippets.

@asomoza
Created September 4, 2024 02:07
Show Gist options
  • Save asomoza/5286bf17060a3296b7769a5fb2d6cd08 to your computer and use it in GitHub Desktop.
Save asomoza/5286bf17060a3296b7769a5fb2d6cd08 to your computer and use it in GitHub Desktop.
import torch
from optimum.quanto import QuantizedDiffusersModel, freeze, qfloat8, quantize
from diffusers import FluxPipeline, FluxTransformer2DModel
class QuantizedFluxTransformer2DModel(QuantizedDiffusersModel):
base_class = FluxTransformer2DModel
dtype = torch.bfloat16
transformer = FluxTransformer2DModel.from_pretrained(
"black-forest-labs/FLUX.1-dev", subfolder="transformer", torch_dtype=dtype
)
quantize(transformer, weights=qfloat8)
freeze(transformer)
pipe = FluxPipeline.from_pretrained(
"black-forest-labs/FLUX.1-dev", transformer=None, torch_dtype=dtype
)
pipe.transformer = transformer
pipe.enable_model_cpu_offload()
generator = torch.Generator().manual_seed(12345)
image = pipe(
prompt="high quality photo of a dog sitting beside a tree with a red soccer ball at its side, the background it's a lake with a boat sailing in it and an airplane flying in the cloudy sky.",
width=1024,
height=1024,
num_inference_steps=20,
generator=generator,
guidance_scale=3.5,
).images[0]
image.save("fluxfp8_image.png")
@asomoza
Copy link
Author

asomoza commented Oct 15, 2024

Thanks a lot for investigating into this, I really appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment