n42 commited on
Commit
071e791
·
1 Parent(s): a447492

add vae slicing/tiling option, fix non random generator

Browse files
Files changed (2) hide show
  1. app.py +25 -4
  2. config.py +12 -4
app.py CHANGED
@@ -87,6 +87,18 @@ def guidance_scale_change(guidance_scale, config):
87
 
88
  return config, str(config), assemble_code(config)
89
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  def prompt_change(prompt, config):
91
 
92
  config = set_config(config, 'prompt', prompt)
@@ -172,7 +184,7 @@ def run_inference(config, config_history, progress=gr.Progress(track_tqdm=True))
172
  pipeline.scheduler = get_scheduler(config["scheduler"], pipeline.scheduler.config)
173
 
174
  if config["manual_seed"] < 0 or config["manual_seed"] is None or config["manual_seed"] == '':
175
- generator = torch.Generator(config["device"])
176
  else:
177
  generator = torch.manual_seed(int(config["manual_seed"]))
178
 
@@ -247,9 +259,9 @@ with gr.Blocks(analytics_enabled=False) as demo:
247
  in_schedulers = gr.Dropdown(value="", choices=list(schedulers.keys()), label="Scheduler/Solver", info="schedulers employ various strategies for noise control, the scheduler controls parameter adaption between each inference step, depending on the right scheduler for your model, it may only take 10 or 20 steps to achieve very good results, see https://huggingface.co/docs/diffusers/using-diffusers/loading#schedulers" )
248
  out_scheduler_description = gr.Textbox(value="", label="Description")
249
 
250
- gr.Markdown("### Adapters")
251
- with gr.Row():
252
- gr.Markdown('Choose an adapter.')
253
 
254
  gr.Markdown("### Inference settings")
255
  with gr.Row():
@@ -259,6 +271,11 @@ with gr.Blocks(analytics_enabled=False) as demo:
259
  in_inference_steps = gr.Number(label="Inference steps", value=config.value["inference_steps"], info="Each step improves the final result but also results in higher computation")
260
  in_manual_seed = gr.Number(label="Manual seed", value=config.value["manual_seed"], info="Set this to -1 or leave it empty to randomly generate an image. A fixed value will result in a similar image for every run")
261
  in_guidance_scale = gr.Slider(minimum=0, maximum=1, step=0.01, label="Guidance Scale", value=config.value["guidance_scale"], info="A low guidance scale leads to a faster inference time, with the drawback that negative prompts don’t have any effect on the denoising process.")
 
 
 
 
 
262
 
263
  gr.Markdown("### Output")
264
  with gr.Row():
@@ -286,6 +303,8 @@ with gr.Blocks(analytics_enabled=False) as demo:
286
  in_inference_steps.change(inference_steps_change, inputs=[in_inference_steps, config], outputs=[config, out_config, out_code])
287
  in_manual_seed.change(manual_seed_change, inputs=[in_manual_seed, config], outputs=[config, out_config, out_code])
288
  in_guidance_scale.change(guidance_scale_change, inputs=[in_guidance_scale, config], outputs=[config, out_config, out_code])
 
 
289
  in_prompt.change(prompt_change, inputs=[in_prompt, config], outputs=[config, out_config, out_code])
290
  in_trigger_token.change(trigger_token_change, inputs=[in_trigger_token, config], outputs=[config, out_config, out_code])
291
  in_negative_prompt.change(negative_prompt_change, inputs=[in_negative_prompt, config], outputs=[config, out_config, out_code])
@@ -306,6 +325,8 @@ with gr.Blocks(analytics_enabled=False) as demo:
306
  in_variant,
307
  in_safety_checker,
308
  in_requires_safety_checker,
 
 
309
  in_schedulers,
310
  in_prompt,
311
  in_trigger_token,
 
87
 
88
  return config, str(config), assemble_code(config)
89
 
90
+ def enable_vae_slicing_change(enable_vae_slicing, config):
91
+
92
+ config = set_config(config, 'enable_vae_slicing', enable_vae_slicing)
93
+
94
+ return config, str(config), assemble_code(config)
95
+
96
+ def enable_vae_tiling_change(enable_vae_tiling, config):
97
+
98
+ config = set_config(config, 'enable_vae_tiling', enable_vae_tiling)
99
+
100
+ return config, str(config), assemble_code(config)
101
+
102
  def prompt_change(prompt, config):
103
 
104
  config = set_config(config, 'prompt', prompt)
 
184
  pipeline.scheduler = get_scheduler(config["scheduler"], pipeline.scheduler.config)
185
 
186
  if config["manual_seed"] < 0 or config["manual_seed"] is None or config["manual_seed"] == '':
187
+ generator = None
188
  else:
189
  generator = torch.manual_seed(int(config["manual_seed"]))
190
 
 
259
  in_schedulers = gr.Dropdown(value="", choices=list(schedulers.keys()), label="Scheduler/Solver", info="schedulers employ various strategies for noise control, the scheduler controls parameter adaption between each inference step, depending on the right scheduler for your model, it may only take 10 or 20 steps to achieve very good results, see https://huggingface.co/docs/diffusers/using-diffusers/loading#schedulers" )
260
  out_scheduler_description = gr.Textbox(value="", label="Description")
261
 
262
+ # gr.Markdown("### Adapters")
263
+ # with gr.Row():
264
+ # gr.Markdown('Choose an adapter.')
265
 
266
  gr.Markdown("### Inference settings")
267
  with gr.Row():
 
271
  in_inference_steps = gr.Number(label="Inference steps", value=config.value["inference_steps"], info="Each step improves the final result but also results in higher computation")
272
  in_manual_seed = gr.Number(label="Manual seed", value=config.value["manual_seed"], info="Set this to -1 or leave it empty to randomly generate an image. A fixed value will result in a similar image for every run")
273
  in_guidance_scale = gr.Slider(minimum=0, maximum=1, step=0.01, label="Guidance Scale", value=config.value["guidance_scale"], info="A low guidance scale leads to a faster inference time, with the drawback that negative prompts don’t have any effect on the denoising process.")
274
+ with gr.Row():
275
+ gr.Markdown("**VAE** stands for Variational Autoencoders. An 'autoencoder' is an artificial neural network that is able to encode input data and decode to output data to bascially recreate the input. The VAE whereas adds a couple of additional layers of complexity to create new and unique output.")
276
+ with gr.Row():
277
+ in_enable_vae_slicing = gr.Radio(label="Enable VAE slicing:", value=config.value["enable_vae_slicing"], choices=["True", "False"], info="decoding the batches of latents one image at a time, which may reduce memory usage, see https://huggingface.co/docs/diffusers/main/en/optimization/memory")
278
+ in_enable_vae_tiling= gr.Radio(label="Enable VAE tiling:", value=config.value["enable_vae_tiling"], choices=["True", "False"], info="splitting the image into overlapping tiles, decoding the tiles, and then blending the outputs together to compose the final image, see https://huggingface.co/docs/diffusers/main/en/optimization/memory")
279
 
280
  gr.Markdown("### Output")
281
  with gr.Row():
 
303
  in_inference_steps.change(inference_steps_change, inputs=[in_inference_steps, config], outputs=[config, out_config, out_code])
304
  in_manual_seed.change(manual_seed_change, inputs=[in_manual_seed, config], outputs=[config, out_config, out_code])
305
  in_guidance_scale.change(guidance_scale_change, inputs=[in_guidance_scale, config], outputs=[config, out_config, out_code])
306
+ in_enable_vae_slicing.change(enable_vae_slicing_change, inputs=[in_enable_vae_slicing, config], outputs=[config, out_config, out_code])
307
+ in_enable_vae_tiling.change(enable_vae_tiling_change, inputs=[in_enable_vae_tiling, config], outputs=[config, out_config, out_code])
308
  in_prompt.change(prompt_change, inputs=[in_prompt, config], outputs=[config, out_config, out_code])
309
  in_trigger_token.change(trigger_token_change, inputs=[in_trigger_token, config], outputs=[config, out_config, out_code])
310
  in_negative_prompt.change(negative_prompt_change, inputs=[in_negative_prompt, config], outputs=[config, out_config, out_code])
 
325
  in_variant,
326
  in_safety_checker,
327
  in_requires_safety_checker,
328
+ in_enable_vae_slicing,
329
+ in_enable_vae_tiling,
330
  in_schedulers,
331
  in_prompt,
332
  in_trigger_token,
config.py CHANGED
@@ -46,12 +46,14 @@ def get_initial_config():
46
  "refiner": "none",
47
  "safety_checker": "False",
48
  "requires_safety_checker": "False",
 
 
49
  "manual_seed": 42,
50
  "inference_steps": 10,
51
  "guidance_scale": 0.5,
52
  "prompt": 'A white rabbit',
53
  "trigger_token": '',
54
- "negative_prompt": 'lowres, cropped, worst quality, low quality, chat bubble, chat bubbles, ugly',
55
  }
56
 
57
  return config
@@ -86,6 +88,8 @@ def get_config_from_url(initial_config, request: Request):
86
  return_config['variant'],
87
  return_config['safety_checker'],
88
  return_config['requires_safety_checker'],
 
 
89
  return_config['scheduler'],
90
  return_config['prompt'],
91
  return_config['trigger_token'],
@@ -147,7 +151,9 @@ def assemble_code(str_config):
147
  torch_dtype=data_type,
148
  variant=variant).to(device)''')
149
 
150
- if str(config["cpu_offload"]).lower() == 'false': code.append("pipeline.enable_model_cpu_offload()")
 
 
151
 
152
  if config['refiner'].lower() != 'none':
153
  code.append(f'''refiner = DiffusionPipeline.from_pretrained(
@@ -159,7 +165,9 @@ def assemble_code(str_config):
159
  variant=variant,
160
  ).to(device)''')
161
 
162
- if str(config["use_safetensors"]).lower() == 'false': code.append("refiner.enable_model_cpu_offload()")
 
 
163
 
164
  code.append(f'pipeline.requires_safety_checker = {config["requires_safety_checker"]}')
165
 
@@ -170,7 +178,7 @@ def assemble_code(str_config):
170
 
171
  if config['manual_seed'] < 0 or config['manual_seed'] is None or config['manual_seed'] == '':
172
  code.append(f'# manual_seed = {config["manual_seed"]}')
173
- code.append(f'generator = torch.Generator("{config["device"]}")')
174
  else:
175
  code.append(f'manual_seed = {config["manual_seed"]}')
176
  code.append(f'generator = torch.manual_seed(manual_seed)')
 
46
  "refiner": "none",
47
  "safety_checker": "False",
48
  "requires_safety_checker": "False",
49
+ "enable_vae_slicing": "True",
50
+ "enable_vae_tiling": "True",
51
  "manual_seed": 42,
52
  "inference_steps": 10,
53
  "guidance_scale": 0.5,
54
  "prompt": 'A white rabbit',
55
  "trigger_token": '',
56
+ "negative_prompt": 'lowres, cropped, worst quality, low quality',
57
  }
58
 
59
  return config
 
88
  return_config['variant'],
89
  return_config['safety_checker'],
90
  return_config['requires_safety_checker'],
91
+ return_config['enable_vae_slicing'],
92
+ return_config['enable_vae_tiling'],
93
  return_config['scheduler'],
94
  return_config['prompt'],
95
  return_config['trigger_token'],
 
151
  torch_dtype=data_type,
152
  variant=variant).to(device)''')
153
 
154
+ if str(config["cpu_offload"]).lower() != 'false': code.append("pipeline.enable_model_cpu_offload()")
155
+ if str(config["enable_vae_slicing"]).lower() != 'false': code.append("pipeline.enable_vae_slicing()")
156
+ if str(config["enable_vae_tiling"]).lower() != 'false': code.append("pipeline.enable_vae_tiling()")
157
 
158
  if config['refiner'].lower() != 'none':
159
  code.append(f'''refiner = DiffusionPipeline.from_pretrained(
 
165
  variant=variant,
166
  ).to(device)''')
167
 
168
+ if str(config["cpu_offload"]).lower() != 'false': code.append("refiner.enable_model_cpu_offload()")
169
+ if str(config["enable_vae_slicing"]).lower() != 'false': code.append("refiner.enable_vae_slicing()")
170
+ if str(config["enable_vae_tiling"]).lower() != 'false': code.append("refiner.enable_vae_tiling()")
171
 
172
  code.append(f'pipeline.requires_safety_checker = {config["requires_safety_checker"]}')
173
 
 
178
 
179
  if config['manual_seed'] < 0 or config['manual_seed'] is None or config['manual_seed'] == '':
180
  code.append(f'# manual_seed = {config["manual_seed"]}')
181
+ code.append(f'generator = None')
182
  else:
183
  code.append(f'manual_seed = {config["manual_seed"]}')
184
  code.append(f'generator = torch.manual_seed(manual_seed)')