Skip to content

Commit 8393d1e

Browse files
committed
add the test script
1 parent d08e6a7 commit 8393d1e

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import torch
2+
from diffusers import (
3+
FluxPipeline,
4+
FluxInpaintPipeline,
5+
FluxTransformer2DModel,
6+
FlowMatchEulerDiscreteScheduler,
7+
AutoencoderKL,
8+
FluxControlNetModel,
9+
FluxControlNetPipeline,
10+
FluxFillPipeline
11+
)
12+
from diffusers.models import FluxMultiControlNetModel
13+
from transformers import T5EncoderModel, CLIPTextModel, CLIPTokenizer,T5TokenizerFast
14+
from nunchaku.models.transformers.transformer_flux import NunchakuFluxTransformer2dModel
15+
from nunchaku import NunchakuT5EncoderModel
16+
from nunchaku.utils import get_precision
17+
from nunchaku.caching.diffusers_adapters import apply_cache_on_pipe
18+
from diffusers.utils import load_image
19+
20+
bfl_repo = "black-forest-labs/FLUX.1-dev"
21+
dtype = torch.bfloat16 # or torch.float16, or torch.float32
22+
device = "cuda" # or "cpu" if you want to run on CPU
23+
24+
scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(bfl_repo, subfolder="scheduler", torch_dtype=dtype)
25+
text_encoder = CLIPTextModel.from_pretrained(bfl_repo, subfolder="text_encoder", torch_dtype=dtype)
26+
text_encoder_2 = T5EncoderModel.from_pretrained(bfl_repo, subfolder="text_encoder_2", torch_dtype=dtype)
27+
tokenizer = CLIPTokenizer.from_pretrained(bfl_repo, subfolder="tokenizer", torch_dtype=dtype, clean_up_tokenization_spaces=True)
28+
tokenizer_2 = T5TokenizerFast.from_pretrained(bfl_repo, subfolder="tokenizer_2", torch_dtype=dtype, clean_up_tokenization_spaces=True)
29+
vae = AutoencoderKL.from_pretrained(bfl_repo, subfolder="vae", torch_dtype=dtype)
30+
precision = get_precision()
31+
transformer = NunchakuFluxTransformer2dModel.from_pretrained(
32+
f"mit-han-lab/nunchaku-flux.1-dev/svdq-{precision}_r32-flux.1-dev.safetensors",
33+
# offload=True
34+
)
35+
transformer.set_attention_impl("nunchaku-fp16")
36+
37+
# qencoder
38+
text_encoder_2 = NunchakuT5EncoderModel.from_pretrained("mit-han-lab/nunchaku-t5/awq-int4-flux.1-t5xxl.safetensors")
39+
controlnet_union = FluxControlNetModel.from_pretrained("Shakker-Labs/FLUX.1-dev-ControlNet-Union-Pro-2.0", torch_dtype=torch.bfloat16)
40+
controlnet = FluxMultiControlNetModel([controlnet_union]) # we always recommend loading via FluxMultiControlNetModel
41+
42+
43+
params = {
44+
"scheduler": scheduler,
45+
"vae": vae,
46+
"tokenizer": tokenizer,
47+
"tokenizer_2": tokenizer_2,
48+
"text_encoder": text_encoder,
49+
"text_encoder_2": text_encoder_2,
50+
"transformer": transformer
51+
}
52+
# pipe
53+
pipe = FluxPipeline(**params).to(device, dtype=dtype)
54+
pipe_cn = FluxControlNetPipeline(**params, controlnet=controlnet).to(device, dtype)
55+
56+
# offload
57+
pipe.enable_sequential_cpu_offload(device=device)
58+
pipe_cn.enable_sequential_cpu_offload(device=device)
59+
60+
# cache
61+
apply_cache_on_pipe(
62+
pipe_cn,
63+
use_double_fb_cache=True,
64+
residual_diff_threshold_multi=0.09,
65+
residual_diff_threshold_single=0.12,
66+
)
67+
apply_cache_on_pipe(
68+
pipe_cn,
69+
use_double_fb_cache=False,
70+
residual_diff_threshold_multi=0,
71+
residual_diff_threshold_single=0,
72+
)
73+
74+
params = {
75+
"prompt": "A bohemian-style female travel blogger with sun-kissed skin and messy beach waves.",
76+
"height": 1152,
77+
"width": 768,
78+
"num_inference_steps": 30,
79+
"guidance_scale": 3.5,
80+
}
81+
82+
# pipe
83+
txt2img_res = pipe(
84+
**params,
85+
).images[0]
86+
txt2img_res.save("flux.1-dev-txt2img.jpg")
87+
88+
# cache
89+
apply_cache_on_pipe(
90+
pipe_cn,
91+
use_double_fb_cache=True,
92+
residual_diff_threshold_multi=0.09,
93+
residual_diff_threshold_single=0.12,
94+
)
95+
96+
# pipe_cn
97+
control_iamge = load_image("https://huggingface.co/Shakker-Labs/FLUX.1-dev-ControlNet-Union-Pro/resolve/main/assets/openpose.jpg")
98+
params["control_image"] = [control_iamge]
99+
params["controlnet_conditioning_scale"] = [0.9]
100+
params['control_guidance_end'] = [0.65]
101+
cn_res = pipe_cn(
102+
**params,
103+
).images[0]
104+
cn_res.save("flux.1-dev-cn-txt2img.jpg")

0 commit comments

Comments
 (0)