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
4# from transformers.modeling_utils import no_init_weights # for transformers<5.0.0
5from transformers.initialization import no_init_weights # for transformers>=5.0.0
6pattern_dict = {
7 r"noise_refiner\.\d+": (
8 "attention.to_q",
9 "attention.to_k",
10 "attention.to_v",
11 "attention.to_out.0",
12 "feed_forward.w1",
13 "feed_forward.w2",
14 "feed_forward.w3",
15 "adaLN_modulation.0"
16 ),
17 r"context_refiner\.\d+": (
18 "attention.to_q",
19 "attention.to_k",
20 "attention.to_v",
21 "attention.to_out.0",
22 "feed_forward.w1",
23 "feed_forward.w2",
24 "feed_forward.w3",
25 ),
26 r"layers\.\d+": (
27 "attention.to_q",
28 "attention.to_k",
29 "attention.to_v",
30 "attention.to_out.0",
31 "feed_forward.w1",
32 "feed_forward.w2",
33 "feed_forward.w3",
34 "adaLN_modulation.0"
35 ),
36 r"cap_embedder": (
37 "1",
38 )
39}
40text_encoder = DFloat11Model.from_pretrained("DFloat11/Qwen3-4B-DF11", device="cpu")
41with no_init_weights():
42 transformer = ZImageTransformer2DModel.from_config(
43 ZImageTransformer2DModel.load_config(
44 "Tongyi-MAI/Z-Image-Turbo", subfolder="transformer"
45 ),
46 torch_dtype=torch.bfloat16
47 ).to(torch.bfloat16)
48# Make sure to download the file first, and edit the filepath accordingly
49DFloat11Model.from_single_file(
50 r".\RedZFUN-v6-ZIB-Distilled-AGILE-8steps-BF16-ComfyUI-DF11.safetensors",
51 device='cpu',
52 bfloat16_model=transformer,
53 pattern_dict=pattern_dict
54)
55pipe = ZImagePipeline.from_pretrained(
56 "Tongyi-MAI/Z-Image-Turbo",
57 text_encoder=text_encoder,
58 transformer=transformer,
59 torch_dtype=torch.bfloat16,
60 low_cpu_mem_usage=False,
61)
62pipe.to("cuda")pattern_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}