Spaces:
Running on Zero
Running on Zero
Switch to FLUX.2-klein-4B: ungated, bf16 (no bnb, no ZeroGPU patch), 4-step default
Browse files- app.py +11 -23
- requirements.txt +0 -2
- zerogpu_patch.py +0 -29
app.py
CHANGED
|
@@ -5,13 +5,12 @@ import numpy as np
|
|
| 5 |
import spaces
|
| 6 |
import torch
|
| 7 |
|
| 8 |
-
import zerogpu_patch # noqa: F401 β bnb-4bit loading on ZeroGPU, see the module docstring
|
| 9 |
from diffusers import ModularPipeline
|
| 10 |
from diffusers.modular_pipelines import SequentialPipelineBlocks
|
| 11 |
from diffusers.modular_pipelines.flux2.decoders import Flux2UnpackLatentsStep
|
| 12 |
|
| 13 |
|
| 14 |
-
repo_id = "
|
| 15 |
|
| 16 |
# Take the pipeline apart into stages: each stage only loads the components it needs.
|
| 17 |
blocks = ModularPipeline.from_pretrained(repo_id).blocks
|
|
@@ -43,8 +42,7 @@ def infer(
|
|
| 43 |
randomize_seed=False,
|
| 44 |
width=1024,
|
| 45 |
height=1024,
|
| 46 |
-
|
| 47 |
-
num_inference_steps=28,
|
| 48 |
progress=gr.Progress(track_tqdm=True),
|
| 49 |
):
|
| 50 |
if randomize_seed:
|
|
@@ -56,7 +54,6 @@ def infer(
|
|
| 56 |
# `pipe.stream()` yields an event with the live pipeline state after every denoising step
|
| 57 |
stream = pipe.stream(
|
| 58 |
**text_embeddings,
|
| 59 |
-
guidance_scale=guidance_scale,
|
| 60 |
num_inference_steps=num_inference_steps,
|
| 61 |
width=width,
|
| 62 |
height=height,
|
|
@@ -93,7 +90,7 @@ css = """
|
|
| 93 |
with gr.Blocks() as demo:
|
| 94 |
with gr.Column(elem_id="col-container"):
|
| 95 |
gr.Markdown(
|
| 96 |
-
"""# FLUX.2 [
|
| 97 |
Live latent preview powered by `pipe.stream()`: the pipeline yields its live state after every
|
| 98 |
denoising step, and a preview pipeline built from flux2's own unpack + decode blocks renders it.
|
| 99 |
No custom blocks, queues, or threads β see [huggingface/diffusers#14159](https://github.com/huggingface/diffusers/pull/14159).
|
|
@@ -141,29 +138,20 @@ No custom blocks, queues, or threads β see [huggingface/diffusers#14159](https
|
|
| 141 |
value=1024,
|
| 142 |
)
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
)
|
| 152 |
-
|
| 153 |
-
num_inference_steps = gr.Slider(
|
| 154 |
-
label="Number of inference steps",
|
| 155 |
-
minimum=1,
|
| 156 |
-
maximum=50,
|
| 157 |
-
step=1,
|
| 158 |
-
value=28,
|
| 159 |
-
)
|
| 160 |
|
| 161 |
gr.Examples(examples=examples, fn=infer, inputs=[prompt], outputs=[result, seed], cache_examples=False)
|
| 162 |
|
| 163 |
gr.on(
|
| 164 |
triggers=[run_button.click, prompt.submit],
|
| 165 |
fn=infer,
|
| 166 |
-
inputs=[prompt, seed, randomize_seed, width, height,
|
| 167 |
outputs=[result, seed],
|
| 168 |
)
|
| 169 |
|
|
|
|
| 5 |
import spaces
|
| 6 |
import torch
|
| 7 |
|
|
|
|
| 8 |
from diffusers import ModularPipeline
|
| 9 |
from diffusers.modular_pipelines import SequentialPipelineBlocks
|
| 10 |
from diffusers.modular_pipelines.flux2.decoders import Flux2UnpackLatentsStep
|
| 11 |
|
| 12 |
|
| 13 |
+
repo_id = "black-forest-labs/FLUX.2-klein-4B"
|
| 14 |
|
| 15 |
# Take the pipeline apart into stages: each stage only loads the components it needs.
|
| 16 |
blocks = ModularPipeline.from_pretrained(repo_id).blocks
|
|
|
|
| 42 |
randomize_seed=False,
|
| 43 |
width=1024,
|
| 44 |
height=1024,
|
| 45 |
+
num_inference_steps=4,
|
|
|
|
| 46 |
progress=gr.Progress(track_tqdm=True),
|
| 47 |
):
|
| 48 |
if randomize_seed:
|
|
|
|
| 54 |
# `pipe.stream()` yields an event with the live pipeline state after every denoising step
|
| 55 |
stream = pipe.stream(
|
| 56 |
**text_embeddings,
|
|
|
|
| 57 |
num_inference_steps=num_inference_steps,
|
| 58 |
width=width,
|
| 59 |
height=height,
|
|
|
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
with gr.Column(elem_id="col-container"):
|
| 92 |
gr.Markdown(
|
| 93 |
+
"""# FLUX.2 [klein] β Live Preview with Modular Diffusers
|
| 94 |
Live latent preview powered by `pipe.stream()`: the pipeline yields its live state after every
|
| 95 |
denoising step, and a preview pipeline built from flux2's own unpack + decode blocks renders it.
|
| 96 |
No custom blocks, queues, or threads β see [huggingface/diffusers#14159](https://github.com/huggingface/diffusers/pull/14159).
|
|
|
|
| 138 |
value=1024,
|
| 139 |
)
|
| 140 |
|
| 141 |
+
num_inference_steps = gr.Slider(
|
| 142 |
+
label="Number of inference steps",
|
| 143 |
+
minimum=1,
|
| 144 |
+
maximum=16,
|
| 145 |
+
step=1,
|
| 146 |
+
value=4,
|
| 147 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
gr.Examples(examples=examples, fn=infer, inputs=[prompt], outputs=[result, seed], cache_examples=False)
|
| 150 |
|
| 151 |
gr.on(
|
| 152 |
triggers=[run_button.click, prompt.submit],
|
| 153 |
fn=infer,
|
| 154 |
+
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
|
| 155 |
outputs=[result, seed],
|
| 156 |
)
|
| 157 |
|
requirements.txt
CHANGED
|
@@ -2,5 +2,3 @@ accelerate
|
|
| 2 |
git+https://github.com/huggingface/diffusers.git@refs/pull/14159/head
|
| 3 |
torch
|
| 4 |
transformers
|
| 5 |
-
torchvision
|
| 6 |
-
bitsandbytes
|
|
|
|
| 2 |
git+https://github.com/huggingface/diffusers.git@refs/pull/14159/head
|
| 3 |
torch
|
| 4 |
transformers
|
|
|
|
|
|
zerogpu_patch.py
DELETED
|
@@ -1,29 +0,0 @@
|
|
| 1 |
-
# Workaround for loading a bnb-4bit diffusers model on ZeroGPU.
|
| 2 |
-
#
|
| 3 |
-
# On ZeroGPU the main process has no real GPU: the `spaces` package fakes the CUDA device, and its
|
| 4 |
-
# proxy tensors expose `.shape` as a plain tuple. diffusers' bnb shape check calls
|
| 5 |
-
# `current_param.shape.numel()`, which only exists on `torch.Size`, so loading the quantized
|
| 6 |
-
# transformer crashes with `'tuple' object has no attribute 'numel'`
|
| 7 |
-
# (src/diffusers/quantizers/bitsandbytes/bnb_quantizer.py:206). Everything else in the bnb path works
|
| 8 |
-
# under the ZeroGPU shim β the bnb-4bit text encoder loads fine through transformers, whose
|
| 9 |
-
# equivalent check doesn't assume `torch.Size`.
|
| 10 |
-
#
|
| 11 |
-
# TODO: remove once the duck-typed check (`math.prod(shape)`) lands in diffusers.
|
| 12 |
-
|
| 13 |
-
import math
|
| 14 |
-
|
| 15 |
-
from diffusers.quantizers.bitsandbytes.bnb_quantizer import BnB4BitDiffusersQuantizer
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def check_quantized_param_shape(self, param_name, current_param, loaded_param):
|
| 19 |
-
n = math.prod(current_param.shape)
|
| 20 |
-
inferred_shape = (n,) if "bias" in param_name else ((n + 1) // 2, 1)
|
| 21 |
-
if tuple(loaded_param.shape) != inferred_shape:
|
| 22 |
-
raise ValueError(
|
| 23 |
-
f"Expected the flattened shape of the current param ({param_name}) to be {tuple(loaded_param.shape)} "
|
| 24 |
-
f"but is {inferred_shape}."
|
| 25 |
-
)
|
| 26 |
-
return True
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
BnB4BitDiffusersQuantizer.check_quantized_param_shape = check_quantized_param_shape
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|