Spaces:
Running
on
Zero
Running
on
Zero
adamelliotfields
commited on
Commit
•
c348e53
1
Parent(s):
579e8d0
FreeU
Browse files- README.md +3 -1
- app.py +12 -6
- cli.py +2 -0
- lib/inference.py +2 -0
- lib/loader.py +29 -5
- usage.md +10 -6
README.md
CHANGED
@@ -52,7 +52,9 @@ Gradio app for Stable Diffusion 1.5 including:
|
|
52 |
* multiple samplers with Karras schedule
|
53 |
* Compel prompting
|
54 |
* 100+ styles from sdxl_prompt_styler
|
55 |
-
*
|
|
|
|
|
56 |
* optional TAESD
|
57 |
|
58 |
## Usage
|
|
|
52 |
* multiple samplers with Karras schedule
|
53 |
* Compel prompting
|
54 |
* 100+ styles from sdxl_prompt_styler
|
55 |
+
* FreeU and Clip Skip for quality
|
56 |
+
* DeepCache and ToMe for speed
|
57 |
+
* Real-ESRGAN upscaling
|
58 |
* optional TAESD
|
59 |
|
60 |
## Usage
|
app.py
CHANGED
@@ -171,9 +171,19 @@ with gr.Blocks(
|
|
171 |
label="Karras σ",
|
172 |
value=True,
|
173 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
increment_seed = gr.Checkbox(
|
175 |
elem_classes=["checkbox"],
|
176 |
-
label="Autoincrement",
|
177 |
value=True,
|
178 |
)
|
179 |
|
@@ -207,11 +217,6 @@ with gr.Blocks(
|
|
207 |
label="Tiny VAE",
|
208 |
value=False,
|
209 |
)
|
210 |
-
use_clip_skip = gr.Checkbox(
|
211 |
-
elem_classes=["checkbox"],
|
212 |
-
label="Clip skip",
|
213 |
-
value=False,
|
214 |
-
)
|
215 |
truncate_prompts = gr.Checkbox(
|
216 |
elem_classes=["checkbox"],
|
217 |
label="Truncate prompts",
|
@@ -296,6 +301,7 @@ with gr.Blocks(
|
|
296 |
num_images,
|
297 |
use_karras,
|
298 |
use_taesd,
|
|
|
299 |
use_clip_skip,
|
300 |
truncate_prompts,
|
301 |
increment_seed,
|
|
|
171 |
label="Karras σ",
|
172 |
value=True,
|
173 |
)
|
174 |
+
use_freeu = gr.Checkbox(
|
175 |
+
elem_classes=["checkbox"],
|
176 |
+
label="FreeU",
|
177 |
+
value=False,
|
178 |
+
)
|
179 |
+
use_clip_skip = gr.Checkbox(
|
180 |
+
elem_classes=["checkbox"],
|
181 |
+
label="Clip skip",
|
182 |
+
value=False,
|
183 |
+
)
|
184 |
increment_seed = gr.Checkbox(
|
185 |
elem_classes=["checkbox"],
|
186 |
+
label="Autoincrement seed",
|
187 |
value=True,
|
188 |
)
|
189 |
|
|
|
217 |
label="Tiny VAE",
|
218 |
value=False,
|
219 |
)
|
|
|
|
|
|
|
|
|
|
|
220 |
truncate_prompts = gr.Checkbox(
|
221 |
elem_classes=["checkbox"],
|
222 |
label="Truncate prompts",
|
|
|
301 |
num_images,
|
302 |
use_karras,
|
303 |
use_taesd,
|
304 |
+
use_freeu,
|
305 |
use_clip_skip,
|
306 |
truncate_prompts,
|
307 |
increment_seed,
|
cli.py
CHANGED
@@ -34,6 +34,7 @@ def main():
|
|
34 |
parser.add_argument("--clip-skip", action="store_true")
|
35 |
parser.add_argument("--truncate", action="store_true")
|
36 |
parser.add_argument("--karras", action="store_true")
|
|
|
37 |
parser.add_argument("--no-increment", action="store_false")
|
38 |
# fmt: on
|
39 |
|
@@ -52,6 +53,7 @@ def main():
|
|
52 |
args.images,
|
53 |
args.karras,
|
54 |
args.taesd,
|
|
|
55 |
args.clip_skip,
|
56 |
args.truncate,
|
57 |
args.no_increment,
|
|
|
34 |
parser.add_argument("--clip-skip", action="store_true")
|
35 |
parser.add_argument("--truncate", action="store_true")
|
36 |
parser.add_argument("--karras", action="store_true")
|
37 |
+
parser.add_argument("--freeu", action="store_true")
|
38 |
parser.add_argument("--no-increment", action="store_false")
|
39 |
# fmt: on
|
40 |
|
|
|
53 |
args.images,
|
54 |
args.karras,
|
55 |
args.taesd,
|
56 |
+
args.freeu,
|
57 |
args.clip_skip,
|
58 |
args.truncate,
|
59 |
args.no_increment,
|
lib/inference.py
CHANGED
@@ -86,6 +86,7 @@ def generate(
|
|
86 |
num_images=1,
|
87 |
karras=False,
|
88 |
taesd=False,
|
|
|
89 |
clip_skip=False,
|
90 |
truncate_prompts=False,
|
91 |
increment_seed=True,
|
@@ -124,6 +125,7 @@ def generate(
|
|
124 |
scheduler,
|
125 |
karras,
|
126 |
taesd,
|
|
|
127 |
deepcache_interval,
|
128 |
scale,
|
129 |
DTYPE,
|
|
|
86 |
num_images=1,
|
87 |
karras=False,
|
88 |
taesd=False,
|
89 |
+
freeu=False,
|
90 |
clip_skip=False,
|
91 |
truncate_prompts=False,
|
92 |
increment_seed=True,
|
|
|
125 |
scheduler,
|
126 |
karras,
|
127 |
taesd,
|
128 |
+
freeu,
|
129 |
deepcache_interval,
|
130 |
scale,
|
131 |
DTYPE,
|
lib/loader.py
CHANGED
@@ -64,6 +64,17 @@ class Loader:
|
|
64 |
self.pipe.deepcache.set_params(cache_interval=interval)
|
65 |
self.pipe.deepcache.enable()
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
def _load_vae(self, model_name=None, taesd=False, variant=None):
|
68 |
vae_type = type(self.pipe.vae)
|
69 |
is_kl = issubclass(vae_type, (AutoencoderKL, OptimizedModule))
|
@@ -93,7 +104,18 @@ class Loader:
|
|
93 |
model=model,
|
94 |
)
|
95 |
|
96 |
-
def load(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
model_lower = model.lower()
|
98 |
|
99 |
schedulers = {
|
@@ -155,8 +177,9 @@ class Loader:
|
|
155 |
if not same_scheduler or not same_karras:
|
156 |
self.pipe.scheduler = schedulers[scheduler](**scheduler_kwargs)
|
157 |
self._load_vae(model_lower, taesd, variant)
|
158 |
-
self.
|
159 |
-
self.
|
|
|
160 |
torch.cuda.empty_cache()
|
161 |
return self.pipe, self.upscaler
|
162 |
else:
|
@@ -173,7 +196,8 @@ class Loader:
|
|
173 |
tokens=list(EMBEDDINGS.values()),
|
174 |
)
|
175 |
self._load_vae(model_lower, taesd, variant)
|
176 |
-
self.
|
177 |
-
self.
|
|
|
178 |
torch.cuda.empty_cache()
|
179 |
return self.pipe, self.upscaler
|
|
|
64 |
self.pipe.deepcache.set_params(cache_interval=interval)
|
65 |
self.pipe.deepcache.enable()
|
66 |
|
67 |
+
def _load_freeu(self, freeu=False):
|
68 |
+
# https://github.com/huggingface/diffusers/blob/v0.30.0/src/diffusers/models/unets/unet_2d_condition.py
|
69 |
+
block = self.pipe.unet.up_blocks[0]
|
70 |
+
attrs = ["b1", "b2", "s1", "s2"]
|
71 |
+
has_freeu = all(getattr(block, attr, None) is not None for attr in attrs)
|
72 |
+
if has_freeu and not freeu:
|
73 |
+
self.pipe.disable_freeu()
|
74 |
+
elif not has_freeu and freeu:
|
75 |
+
# https://github.com/ChenyangSi/FreeU
|
76 |
+
self.pipe.enable_freeu(b1=1.5, b2=1.6, s1=0.9, s2=0.2)
|
77 |
+
|
78 |
def _load_vae(self, model_name=None, taesd=False, variant=None):
|
79 |
vae_type = type(self.pipe.vae)
|
80 |
is_kl = issubclass(vae_type, (AutoencoderKL, OptimizedModule))
|
|
|
104 |
model=model,
|
105 |
)
|
106 |
|
107 |
+
def load(
|
108 |
+
self,
|
109 |
+
model,
|
110 |
+
scheduler,
|
111 |
+
karras,
|
112 |
+
taesd,
|
113 |
+
freeu,
|
114 |
+
deepcache_interval,
|
115 |
+
scale,
|
116 |
+
dtype,
|
117 |
+
device,
|
118 |
+
):
|
119 |
model_lower = model.lower()
|
120 |
|
121 |
schedulers = {
|
|
|
177 |
if not same_scheduler or not same_karras:
|
178 |
self.pipe.scheduler = schedulers[scheduler](**scheduler_kwargs)
|
179 |
self._load_vae(model_lower, taesd, variant)
|
180 |
+
self._load_freeu(freeu)
|
181 |
+
self._load_deepcache(deepcache_interval)
|
182 |
+
self._load_upscaler(device, scale)
|
183 |
torch.cuda.empty_cache()
|
184 |
return self.pipe, self.upscaler
|
185 |
else:
|
|
|
196 |
tokens=list(EMBEDDINGS.values()),
|
197 |
)
|
198 |
self._load_vae(model_lower, taesd, variant)
|
199 |
+
self._load_freeu(freeu)
|
200 |
+
self._load_deepcache(deepcache_interval)
|
201 |
+
self._load_upscaler(device, scale)
|
202 |
torch.cuda.empty_cache()
|
203 |
return self.pipe, self.upscaler
|
usage.md
CHANGED
@@ -32,6 +32,14 @@ Arrays allow you to generate different images from a single prompt. For example,
|
|
32 |
|
33 |
Styles are prompt templates from twri's [sdxl_prompt_styler](https://github.com/twri/sdxl_prompt_styler) Comfy node. Start with a subject like "cat", pick a style, and iterate from there.
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
### Scale
|
36 |
|
37 |
Rescale up to 4x using [Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN).
|
@@ -63,7 +71,7 @@ Optionally, the [Karras](https://arxiv.org/abs/2206.00364) noise schedule can be
|
|
63 |
|
64 |
#### DeepCache
|
65 |
|
66 |
-
[DeepCache](https://github.com/horseee/DeepCache) (Ma et al. 2023) caches lower
|
67 |
* `1`: no caching
|
68 |
* `2`: more quality (default)
|
69 |
* `3`: balanced
|
@@ -71,16 +79,12 @@ Optionally, the [Karras](https://arxiv.org/abs/2206.00364) noise schedule can be
|
|
71 |
|
72 |
#### ToMe
|
73 |
|
74 |
-
[Token merging](https://
|
75 |
|
76 |
#### Tiny VAE
|
77 |
|
78 |
Enable [madebyollin/taesd](https://github.com/madebyollin/taesd) for almost instant latent decoding with a minor loss in detail. Useful for development.
|
79 |
|
80 |
-
#### Clip Skip
|
81 |
-
|
82 |
-
When enabled, the last CLIP layer is skipped. This _can_ improve image quality with anime models.
|
83 |
-
|
84 |
#### Prompt Truncation
|
85 |
|
86 |
When enabled, prompts will be truncated to CLIP's limit of 77 tokens. By default this is _disabled_, so Compel will chunk prompts into segments rather than cutting them off.
|
|
|
32 |
|
33 |
Styles are prompt templates from twri's [sdxl_prompt_styler](https://github.com/twri/sdxl_prompt_styler) Comfy node. Start with a subject like "cat", pick a style, and iterate from there.
|
34 |
|
35 |
+
#### FreeU
|
36 |
+
|
37 |
+
[FreeU](https://github.com/ChenyangSi/FreeU) (Si et al. 2023) re-weights the contributions sourced from the U-Net’s skip connections and backbone feature maps to potentially improve image quality.
|
38 |
+
|
39 |
+
#### Clip Skip
|
40 |
+
|
41 |
+
When enabled, the last CLIP layer is skipped. This _can_ improve image quality with anime models.
|
42 |
+
|
43 |
### Scale
|
44 |
|
45 |
Rescale up to 4x using [Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN).
|
|
|
71 |
|
72 |
#### DeepCache
|
73 |
|
74 |
+
[DeepCache](https://github.com/horseee/DeepCache) (Ma et al. 2023) caches lower U-Net layers and reuses them every `Interval` steps:
|
75 |
* `1`: no caching
|
76 |
* `2`: more quality (default)
|
77 |
* `3`: balanced
|
|
|
79 |
|
80 |
#### ToMe
|
81 |
|
82 |
+
[Token merging](https://github.com/dbolya/tomesd) (Bolya & Hoffman 2023) reduces the number of tokens processed by the model. Set `Ratio` to the desired reduction factor. ToMe's impact is more noticeable on larger images.
|
83 |
|
84 |
#### Tiny VAE
|
85 |
|
86 |
Enable [madebyollin/taesd](https://github.com/madebyollin/taesd) for almost instant latent decoding with a minor loss in detail. Useful for development.
|
87 |
|
|
|
|
|
|
|
|
|
88 |
#### Prompt Truncation
|
89 |
|
90 |
When enabled, prompts will be truncated to CLIP's limit of 77 tokens. By default this is _disabled_, so Compel will chunk prompts into segments rather than cutting them off.
|