alfredplpl commited on
Commit
d2065af
1 Parent(s): 9f4261d

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +145 -0
README.md CHANGED
@@ -62,6 +62,78 @@ pip install transformers diffusers
62
  2. Run the following script
63
 
64
  ```python
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  ```
66
 
67
  - diffusers for 8GB VRAM GPU
@@ -74,6 +146,79 @@ pip install transformers diffusers quanto
74
  2. Run the following script
75
 
76
  ```python
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  ```
78
 
79
  ## Uses
 
62
  2. Run the following script
63
 
64
  ```python
65
+ import torch
66
+ from diffusers import Transformer2DModel, PixArtSigmaPipeline, AutoencoderKL, DPMSolverMultistepScheduler
67
+ from transformers import AutoModelForCausalLM, AutoTokenizer
68
+
69
+ # Prompts
70
+ prompt = "カラフルなお花畑。赤、青、黄、紫、ピンクなどの色とりどりの花に溢れている。"
71
+ neg_prompt=""
72
+
73
+ # Settings
74
+ device = "cuda"
75
+ weight_dtype = torch.float32
76
+ weight_dtype_te = torch.bfloat16
77
+ generator = torch.Generator().manual_seed(44)
78
+
79
+ # Load text encoder
80
+ tokenizer = AutoTokenizer.from_pretrained("cyberagent/calm2-7b")
81
+ text_encoder = AutoModelForCausalLM.from_pretrained(
82
+ "cyberagent/calm2-7b",
83
+ torch_dtype=weight_dtype_te,
84
+ device_map=device
85
+ )
86
+
87
+ # Get text embeddings
88
+ with torch.no_grad():
89
+ pos_ids = tokenizer(
90
+ prompt, max_length=512, padding="max_length", truncation=True, return_tensors="pt",
91
+ ).to(device)
92
+ pos_emb = text_encoder(pos_ids.input_ids, output_hidden_states=True, attention_mask=pos_ids.attention_mask)
93
+ pos_emb = pos_emb.hidden_states[-1]
94
+ neg_ids = tokenizer(
95
+ neg_prompt, max_length=512, padding="max_length", truncation=True, return_tensors="pt",
96
+ ).to(device)
97
+ neg_emb = text_encoder(neg_ids.input_ids, output_hidden_states=True, attention_mask=neg_ids.attention_mask)
98
+ neg_emb = neg_emb.hidden_states[-1]
99
+
100
+ # Important
101
+ del text_encoder
102
+
103
+ # load models
104
+ transformer = Transformer2DModel.from_pretrained(
105
+ "aipicasso/commonart-beta",
106
+ torch_dtype=weight_dtype
107
+ )
108
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=weight_dtype)
109
+ scheduler=DPMSolverMultistepScheduler()
110
+
111
+ pipe = PixArtSigmaPipeline(
112
+ vae=vae,
113
+ tokenizer=None,
114
+ text_encoder=None,
115
+ transformer=transformer,
116
+ scheduler=scheduler
117
+ )
118
+
119
+ pipe.to(device)
120
+
121
+ # Generate Image
122
+ with torch.no_grad():
123
+ image = pipe(
124
+ negative_prompt=None,
125
+ prompt_embeds=pos_emb,
126
+ negative_prompt_embeds=neg_emb,
127
+ prompt_attention_mask=pos_ids.attention_mask,
128
+ negative_prompt_attention_mask=neg_ids.attention_mask,
129
+ max_sequence_length=512,
130
+ width=512,
131
+ height=512,
132
+ num_inference_steps=20,
133
+ generator=generator,
134
+ guidance_scale=4.5).images[0]
135
+ image.save("flowers.png")
136
+
137
  ```
138
 
139
  - diffusers for 8GB VRAM GPU
 
146
  2. Run the following script
147
 
148
  ```python
149
+ import torch
150
+ from diffusers import Transformer2DModel, PixArtSigmaPipeline, AutoencoderKL, DPMSolverMultistepScheduler
151
+ from transformers import AutoModelForCausalLM, AutoTokenizer, QuantoConfig
152
+
153
+ # Prompts
154
+ prompt = "カラフルなお花畑。赤、青、黄、紫、ピンクなどの色とりどりの花に溢れている。"
155
+ neg_prompt=""
156
+
157
+ # Settings
158
+ device = "cuda"
159
+ weight_dtype = torch.bfloat16
160
+ weight_dtype_te = torch.bfloat16
161
+ generator = torch.Generator().manual_seed(44)
162
+
163
+ # Load text encoder
164
+ tokenizer = AutoTokenizer.from_pretrained("cyberagent/calm2-7b")
165
+ quantization_config = QuantoConfig(weights="int8")
166
+ text_encoder = AutoModelForCausalLM.from_pretrained(
167
+ "cyberagent/calm2-7b",
168
+ quantization_config=quantization_config,
169
+ torch_dtype=weight_dtype_te,
170
+ device_map=device
171
+ )
172
+
173
+ # Get text embeddings
174
+ with torch.no_grad():
175
+ pos_ids = tokenizer(
176
+ prompt, max_length=512, padding="max_length", truncation=True, return_tensors="pt",
177
+ ).to(device)
178
+ pos_emb = text_encoder(pos_ids.input_ids, output_hidden_states=True, attention_mask=pos_ids.attention_mask)
179
+ pos_emb = pos_emb.hidden_states[-1]
180
+ neg_ids = tokenizer(
181
+ neg_prompt, max_length=512, padding="max_length", truncation=True, return_tensors="pt",
182
+ ).to(device)
183
+ neg_emb = text_encoder(neg_ids.input_ids, output_hidden_states=True, attention_mask=neg_ids.attention_mask)
184
+ neg_emb = neg_emb.hidden_states[-1]
185
+
186
+ # Important
187
+ del text_encoder
188
+
189
+ # load models
190
+ transformer = Transformer2DModel.from_pretrained(
191
+ "aipicasso/commonart-beta",
192
+ torch_dtype=weight_dtype
193
+ )
194
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=weight_dtype)
195
+ scheduler=DPMSolverMultistepScheduler()
196
+
197
+ pipe = PixArtSigmaPipeline(
198
+ vae=vae,
199
+ tokenizer=None,
200
+ text_encoder=None,
201
+ transformer=transformer,
202
+ scheduler=scheduler
203
+ )
204
+
205
+ pipe.to(device)
206
+
207
+ # Generate Image
208
+ with torch.no_grad():
209
+ image = pipe(
210
+ negative_prompt=None,
211
+ prompt_embeds=pos_emb,
212
+ negative_prompt_embeds=neg_emb,
213
+ prompt_attention_mask=pos_ids.attention_mask,
214
+ negative_prompt_attention_mask=neg_ids.attention_mask,
215
+ max_sequence_length=512,
216
+ width=512,
217
+ height=512,
218
+ num_inference_steps=20,
219
+ generator=generator,
220
+ guidance_scale=4.5).images[0]
221
+ image.save("flowers.png")
222
  ```
223
 
224
  ## Uses