Views
No views yet
diffusers library, ComfyUI, or any other model), although models that use architectures which are unfamiliar to me might be more difficult.diffusers1import torch
2from diffusers import ZImagePipeline, ZImageTransformer2DModel
3from dfloat11 import DFloat11Model
4from transformers.modeling_utils import no_init_weights
5
6text_encoder = DFloat11Model.from_pretrained("DFloat11/Qwen3-4B-DF11", device="cpu")
7with no_init_weights():
8 transformer = ZImageTransformer2DModel.from_config(
9 ZImageTransformer2DModel.load_config(
10 "Tongyi-MAI/Z-Image-Turbo", subfolder="transformer"
11 ),
12 torch_dtype=torch.bfloat16
13 ).to(torch.bfloat16)
14DFloat11Model.from_pretrained("mingyi456/Z-Image-Turbo-DF11", device="cpu", bfloat16_model=transformer)
15
16pipe = ZImagePipeline.from_pretrained(
17 "Tongyi-MAI/Z-Image-Turbo",
18 text_encoder=text_encoder,
19 transformer=transformer,
20 torch_dtype=torch.bfloat16,
21 low_cpu_mem_usage=False,
22)
23pipe.to("cuda")
24
25prompt = "Young Chinese woman in red Hanfu, intricate embroidery. Impeccable makeup, red floral forehead pattern. Elaborate high bun, golden phoenix headdress, red flowers, beads. Holds round folding fan with lady, trees, bird. Neon lightning-bolt lamp (⚡️), bright yellow glow, above extended left palm. Soft-lit outdoor night background, silhouetted tiered pagoda (西安大雁塔), blurred colorful distant lights."
26
27# 2. Generate Image
28image = pipe(
29 prompt=prompt,
30 height=1024,
31 width=1024,
32 num_inference_steps=9, # This actually results in 8 DiT forwards
33 guidance_scale=0.0, # Guidance should be 0 for the Turbo models
34 generator=torch.Generator("cuda").manual_seed(42),
35).images[0]
36
37image.save("example.png")
38pattern_dict for compression:1pattern_dict = {
2 r"noise_refiner\.\d+": (
3 "attention.to_q",
4 "attention.to_k",
5 "attention.to_v",
6 "attention.to_out.0",
7 "feed_forward.w1",
8 "feed_forward.w2",
9 "feed_forward.w3",
10 "adaLN_modulation.0"
11 ),
12 r"context_refiner\.\d+": (
13 "attention.to_q",
14 "attention.to_k",
15 "attention.to_v",
16 "attention.to_out.0",
17 "feed_forward.w1",
18 "feed_forward.w2",
19 "feed_forward.w3",
20 ),
21 r"layers\.\d+": (
22 "attention.to_q",
23 "attention.to_k",
24 "attention.to_v",
25 "attention.to_out.0",
26 "feed_forward.w1",
27 "feed_forward.w2",
28 "feed_forward.w3",
29 "adaLN_modulation.0"
30 ),
31 r"cap_embedder": (
32 "1",
33 )
34}