mingyi456 commited on
Commit
3f4b2a3
·
verified ·
1 Parent(s): 0528eab

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +117 -1
README.md CHANGED
@@ -10,4 +10,120 @@ pipeline_tag: text-to-image
10
  library_name: diffusers
11
  tags:
12
  - diffusion-single-file
13
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  library_name: diffusers
11
  tags:
12
  - diffusion-single-file
13
+ ---
14
+ For more information (including how to compress models yourself), check out https://huggingface.co/DFloat11 and https://github.com/LeanModels/DFloat11
15
+
16
+ Feel free to request for other models for compression as well (for either the `diffusers` library, ComfyUI, or any other model), although models that use architectures which are unfamiliar to me might be more difficult.
17
+
18
+ ### How to Use
19
+
20
+ #### `diffusers`
21
+
22
+ ```python
23
+ import torch
24
+ from diffusers import ZImagePipeline, ZImageTransformer2DModel
25
+ from dfloat11 import DFloat11Model
26
+ # from transformers.modeling_utils import no_init_weights # for transformers<5.0.0
27
+ from transformers.initialization import no_init_weights # for transformers>=5.0.0
28
+ pattern_dict = {
29
+ r"noise_refiner\.\d+": (
30
+ "attention.to_q",
31
+ "attention.to_k",
32
+ "attention.to_v",
33
+ "attention.to_out.0",
34
+ "feed_forward.w1",
35
+ "feed_forward.w2",
36
+ "feed_forward.w3",
37
+ "adaLN_modulation.0"
38
+ ),
39
+ r"context_refiner\.\d+": (
40
+ "attention.to_q",
41
+ "attention.to_k",
42
+ "attention.to_v",
43
+ "attention.to_out.0",
44
+ "feed_forward.w1",
45
+ "feed_forward.w2",
46
+ "feed_forward.w3",
47
+ ),
48
+ r"layers\.\d+": (
49
+ "attention.to_q",
50
+ "attention.to_k",
51
+ "attention.to_v",
52
+ "attention.to_out.0",
53
+ "feed_forward.w1",
54
+ "feed_forward.w2",
55
+ "feed_forward.w3",
56
+ "adaLN_modulation.0"
57
+ ),
58
+ r"cap_embedder": (
59
+ "1",
60
+ )
61
+ }
62
+ text_encoder = DFloat11Model.from_pretrained("DFloat11/Qwen3-4B-DF11", device="cpu")
63
+ with no_init_weights():
64
+ transformer = ZImageTransformer2DModel.from_config(
65
+ ZImageTransformer2DModel.load_config(
66
+ "Tongyi-MAI/Z-Image-Turbo", subfolder="transformer"
67
+ ),
68
+ torch_dtype=torch.bfloat16
69
+ ).to(torch.bfloat16)
70
+ # Make sure to download the file first, and edit the filepath accordingly
71
+ DFloat11Model.from_single_file(
72
+ r".\RedZFUN-v6-ZIB-Distilled-AGILE-8steps-BF16-ComfyUI-DF11.safetensors",
73
+ device='cpu',
74
+ bfloat16_model=transformer,
75
+ pattern_dict=pattern_dict
76
+ )
77
+ pipe = ZImagePipeline.from_pretrained(
78
+ "Tongyi-MAI/Z-Image-Turbo",
79
+ text_encoder=text_encoder,
80
+ transformer=transformer,
81
+ torch_dtype=torch.bfloat16,
82
+ low_cpu_mem_usage=False,
83
+ )
84
+ pipe.to("cuda")
85
+ ```
86
+
87
+ #### ComfyUI
88
+ Refer to this [model](https://huggingface.co/mingyi456/Z-Image-Distilled-DF11-ComfyUI) instead.
89
+
90
+ ### Compression details
91
+
92
+ This is the `pattern_dict` for compression:
93
+
94
+ ```python
95
+ pattern_dict = {
96
+ r"noise_refiner\.\d+": (
97
+ "attention.to_q",
98
+ "attention.to_k",
99
+ "attention.to_v",
100
+ "attention.to_out.0",
101
+ "feed_forward.w1",
102
+ "feed_forward.w2",
103
+ "feed_forward.w3",
104
+ "adaLN_modulation.0"
105
+ ),
106
+ r"context_refiner\.\d+": (
107
+ "attention.to_q",
108
+ "attention.to_k",
109
+ "attention.to_v",
110
+ "attention.to_out.0",
111
+ "feed_forward.w1",
112
+ "feed_forward.w2",
113
+ "feed_forward.w3",
114
+ ),
115
+ r"layers\.\d+": (
116
+ "attention.to_q",
117
+ "attention.to_k",
118
+ "attention.to_v",
119
+ "attention.to_out.0",
120
+ "feed_forward.w1",
121
+ "feed_forward.w2",
122
+ "feed_forward.w3",
123
+ "adaLN_modulation.0"
124
+ ),
125
+ r"cap_embedder": (
126
+ "1",
127
+ )
128
+ }
129
+ ```