ford442 commited on
Commit
d7343f7
·
verified ·
1 Parent(s): 7bb757d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -21
app.py CHANGED
@@ -20,6 +20,7 @@ import paramiko
20
  import datetime
21
  #from diffusers import DPMSolverSDEScheduler
22
  from diffusers.models.attention_processor import AttnProcessor2_0
 
23
 
24
  torch.backends.cuda.matmul.allow_tf32 = False
25
  torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
@@ -85,10 +86,10 @@ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
85
 
86
  def load_and_prepare_model():
87
  #vaeRV = AutoencoderKL.from_pretrained("SG161222/RealVisXL_V5.0", subfolder='vae', safety_checker=None, use_safetensors=True, token=True)
88
- vaeXL = AutoencoderKL.from_pretrained("stabilityai/sdxl-vae", safety_checker=None, use_safetensors=False) #.to(device).to(torch.bfloat16) #.to(device=device, dtype=torch.bfloat16)
89
  #sched = EulerAncestralDiscreteScheduler.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='scheduler',beta_schedule="scaled_linear", beta_start=0.00085, beta_end=0.012, steps_offset=1,use_karras_sigmas=True)
90
  #sched = DPMSolverSDEScheduler.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='scheduler')
91
- sched = EulerAncestralDiscreteScheduler.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='scheduler',beta_schedule="scaled_linear", token=True) #, beta_start=0.00085, beta_end=0.012, steps_offset=1,use_karras_sigmas=True, token=True)
92
  #sched = EulerAncestralDiscreteScheduler.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='scheduler',beta_schedule="scaled_linear")
93
  pipe = StableDiffusionXLPipeline.from_pretrained(
94
  'ford442/RealVisXL_V5.0_BF16',
@@ -97,7 +98,6 @@ def load_and_prepare_model():
97
  # low_cpu_mem_usage = False,
98
  add_watermarker=False,
99
  )
100
- pipe.vae = vaeXL #.to(torch.bfloat16)
101
  pipe.scheduler = sched
102
  #pipe.vae.do_resize=False
103
  #pipe.vae.vae_scale_factor=8
@@ -110,15 +110,8 @@ def load_and_prepare_model():
110
  #pipe.unet.to(memory_format=torch.channels_last)
111
  #pipe.enable_vae_tiling()
112
  pipe.to(device=device, dtype=torch.bfloat16)
113
- '''
114
- pipe.unet.set_attn_processor(AttnProcessor2_0())
115
- for name, param in pipe.unet.named_parameters():
116
- if 'qkv' in name or 'norm' in name or 'proj' in name:
117
- param.requires_grad_()
118
- else:
119
- param.requires_grad_(False)
120
- '''
121
- pipe.unet.requires_grad_(False)
122
  return pipe
123
 
124
  pipe = load_and_prepare_model()
@@ -171,6 +164,11 @@ def uploadNote(prompt,num_inference_steps,guidance_scale,timestamp):
171
  f.write(f"To cuda and bfloat \n")
172
  upload_to_ftp(filename)
173
 
 
 
 
 
 
174
  @spaces.GPU(duration=30)
175
  def generate_30(
176
  prompt: str,
@@ -186,10 +184,24 @@ def generate_30(
186
  ):
187
  seed = random.randint(0, MAX_SEED)
188
  generator = torch.Generator(device='cuda').manual_seed(seed)
 
 
 
 
 
 
 
 
 
 
189
  options = {
190
- "prompt": [prompt],
191
- "negative_prompt": [negative_prompt],
192
- "negative_prompt_2": [neg_prompt_2],
 
 
 
 
193
  "width": width,
194
  "height": height,
195
  "guidance_scale": guidance_scale,
@@ -226,10 +238,24 @@ def generate_60(
226
  ):
227
  seed = random.randint(0, MAX_SEED)
228
  generator = torch.Generator(device='cuda').manual_seed(seed)
 
 
 
 
 
 
 
 
 
 
229
  options = {
230
- "prompt": [prompt],
231
- "negative_prompt": [negative_prompt],
232
- "negative_prompt_2": [neg_prompt_2],
 
 
 
 
233
  "width": width,
234
  "height": height,
235
  "guidance_scale": guidance_scale,
@@ -266,10 +292,24 @@ def generate_90(
266
  ):
267
  seed = random.randint(0, MAX_SEED)
268
  generator = torch.Generator(device='cuda').manual_seed(seed)
 
 
 
 
 
 
 
 
 
 
269
  options = {
270
- "prompt": [prompt],
271
- "negative_prompt": [negative_prompt],
272
- "negative_prompt_2": [neg_prompt_2],
 
 
 
 
273
  "width": width,
274
  "height": height,
275
  "guidance_scale": guidance_scale,
 
20
  import datetime
21
  #from diffusers import DPMSolverSDEScheduler
22
  from diffusers.models.attention_processor import AttnProcessor2_0
23
+ import gc
24
 
25
  torch.backends.cuda.matmul.allow_tf32 = False
26
  torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
 
86
 
87
  def load_and_prepare_model():
88
  #vaeRV = AutoencoderKL.from_pretrained("SG161222/RealVisXL_V5.0", subfolder='vae', safety_checker=None, use_safetensors=True, token=True)
89
+ vaeXL = AutoencoderKL.from_pretrained("stabilityai/sdxl-vae", safety_checker=None, use_safetensors=False).to(device) # .to(torch.bfloat16) #.to(device=device, dtype=torch.bfloat16)
90
  #sched = EulerAncestralDiscreteScheduler.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='scheduler',beta_schedule="scaled_linear", beta_start=0.00085, beta_end=0.012, steps_offset=1,use_karras_sigmas=True)
91
  #sched = DPMSolverSDEScheduler.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='scheduler')
92
+ #sched = EulerAncestralDiscreteScheduler.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='scheduler',beta_schedule="scaled_linear", token=True) #, beta_start=0.00085, beta_end=0.012, steps_offset=1,use_karras_sigmas=True, token=True)
93
  #sched = EulerAncestralDiscreteScheduler.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='scheduler',beta_schedule="scaled_linear")
94
  pipe = StableDiffusionXLPipeline.from_pretrained(
95
  'ford442/RealVisXL_V5.0_BF16',
 
98
  # low_cpu_mem_usage = False,
99
  add_watermarker=False,
100
  )
 
101
  pipe.scheduler = sched
102
  #pipe.vae.do_resize=False
103
  #pipe.vae.vae_scale_factor=8
 
110
  #pipe.unet.to(memory_format=torch.channels_last)
111
  #pipe.enable_vae_tiling()
112
  pipe.to(device=device, dtype=torch.bfloat16)
113
+
114
+ pipe.vae = vaeXL #.to(torch.bfloat16)
 
 
 
 
 
 
 
115
  return pipe
116
 
117
  pipe = load_and_prepare_model()
 
164
  f.write(f"To cuda and bfloat \n")
165
  upload_to_ftp(filename)
166
 
167
+ def flush():
168
+ gc.collect()
169
+ torch.cuda.empty_cache()
170
+ torch.cuda.reset_peak_memory_stats()
171
+
172
  @spaces.GPU(duration=30)
173
  def generate_30(
174
  prompt: str,
 
184
  ):
185
  seed = random.randint(0, MAX_SEED)
186
  generator = torch.Generator(device='cuda').manual_seed(seed)
187
+
188
+ # get prompt embeds first
189
+ (
190
+ prompt_embeds,
191
+ negative_prompt_embeds,
192
+ pooled_prompt_embeds,
193
+ negative_pooled_prompt_embeds
194
+ ) = pipe.encode_prompt(prompt)
195
+ del pipe.text_encoder, pipe.text_encoder_2
196
+ flush()
197
  options = {
198
+ # "prompt": [prompt],
199
+ # "negative_prompt": [negative_prompt],
200
+ # "negative_prompt_2": [neg_prompt_2],
201
+ "prompt_embeds": = [prompt_embeds],
202
+ "negative_prompt_embeds": = [negative_prompt_embeds],
203
+ "pooled_prompt_embeds": = [pooled_prompt_embeds],
204
+ "negative_pooled_prompt_embeds": = [negative_pooled_prompt_embeds],
205
  "width": width,
206
  "height": height,
207
  "guidance_scale": guidance_scale,
 
238
  ):
239
  seed = random.randint(0, MAX_SEED)
240
  generator = torch.Generator(device='cuda').manual_seed(seed)
241
+
242
+ # get prompt embeds first
243
+ (
244
+ prompt_embeds,
245
+ negative_prompt_embeds,
246
+ pooled_prompt_embeds,
247
+ negative_pooled_prompt_embeds
248
+ ) = pipe.encode_prompt(prompt)
249
+ del pipe.text_encoder, pipe.text_encoder_2
250
+ flush()
251
  options = {
252
+ # "prompt": [prompt],
253
+ # "negative_prompt": [negative_prompt],
254
+ # "negative_prompt_2": [neg_prompt_2],
255
+ "prompt_embeds": = [prompt_embeds],
256
+ "negative_prompt_embeds": = [negative_prompt_embeds],
257
+ "pooled_prompt_embeds": = [pooled_prompt_embeds],
258
+ "negative_pooled_prompt_embeds": = [negative_pooled_prompt_embeds],
259
  "width": width,
260
  "height": height,
261
  "guidance_scale": guidance_scale,
 
292
  ):
293
  seed = random.randint(0, MAX_SEED)
294
  generator = torch.Generator(device='cuda').manual_seed(seed)
295
+
296
+ # get prompt embeds first
297
+ (
298
+ prompt_embeds,
299
+ negative_prompt_embeds,
300
+ pooled_prompt_embeds,
301
+ negative_pooled_prompt_embeds
302
+ ) = pipe.encode_prompt(prompt)
303
+ del pipe.text_encoder, pipe.text_encoder_2
304
+ flush()
305
  options = {
306
+ # "prompt": [prompt],
307
+ # "negative_prompt": [negative_prompt],
308
+ # "negative_prompt_2": [neg_prompt_2],
309
+ "prompt_embeds": = [prompt_embeds],
310
+ "negative_prompt_embeds": = [negative_prompt_embeds],
311
+ "pooled_prompt_embeds": = [pooled_prompt_embeds],
312
+ "negative_pooled_prompt_embeds": = [negative_pooled_prompt_embeds],
313
  "width": width,
314
  "height": height,
315
  "guidance_scale": guidance_scale,