Skip to content

Commit bad7cb2

Browse files
committed
added support for hypernetworks (???)
1 parent 2995107 commit bad7cb2

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

modules/hypernetwork.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import glob
2+
import os
3+
import torch
4+
from modules import devices
5+
6+
7+
class HypernetworkModule(torch.nn.Module):
8+
def __init__(self, dim, state_dict):
9+
super().__init__()
10+
11+
self.linear1 = torch.nn.Linear(dim, dim * 2)
12+
self.linear2 = torch.nn.Linear(dim * 2, dim)
13+
14+
self.load_state_dict(state_dict, strict=True)
15+
self.to(devices.device)
16+
17+
def forward(self, x):
18+
return x + (self.linear2(self.linear1(x)))
19+
20+
21+
class Hypernetwork:
22+
filename = None
23+
name = None
24+
25+
def __init__(self, filename):
26+
self.filename = filename
27+
self.name = os.path.splitext(os.path.basename(filename))[0]
28+
self.layers = {}
29+
30+
state_dict = torch.load(filename, map_location='cpu')
31+
for size, sd in state_dict.items():
32+
self.layers[size] = (HypernetworkModule(size, sd[0]), HypernetworkModule(size, sd[1]))
33+
34+
35+
def load_hypernetworks(path):
36+
res = {}
37+
38+
for filename in glob.iglob(path + '**/*.pt', recursive=True):
39+
hn = Hypernetwork(filename)
40+
res[hn.name] = hn
41+
42+
return res
43+
44+
def apply(self, x, context=None, mask=None, original=None):
45+
46+
47+
if CrossAttention.hypernetwork is not None and context.shape[2] in CrossAttention.hypernetwork:
48+
if context.shape[1] == 77 and CrossAttention.noise_cond:
49+
context = context + (torch.randn_like(context) * 0.1)
50+
h_k, h_v = CrossAttention.hypernetwork[context.shape[2]]
51+
k = self.to_k(h_k(context))
52+
v = self.to_v(h_v(context))
53+
else:
54+
k = self.to_k(context)
55+
v = self.to_v(context)

modules/sd_hijack_optimizations.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from ldm.util import default
66
from einops import rearrange
77

8+
from modules import shared
9+
810

911
# see https://github.com/basujindal/stable-diffusion/pull/117 for discussion
1012
def split_cross_attention_forward_v1(self, x, context=None, mask=None):
@@ -42,8 +44,19 @@ def split_cross_attention_forward(self, x, context=None, mask=None):
4244

4345
q_in = self.to_q(x)
4446
context = default(context, x)
45-
k_in = self.to_k(context) * self.scale
46-
v_in = self.to_v(context)
47+
48+
hypernetwork = shared.selected_hypernetwork()
49+
hypernetwork_layers = (hypernetwork.layers if hypernetwork is not None else {}).get(context.shape[2], None)
50+
51+
if hypernetwork_layers is not None:
52+
k_in = self.to_k(hypernetwork_layers[0](context))
53+
v_in = self.to_v(hypernetwork_layers[1](context))
54+
else:
55+
k_in = self.to_k(context)
56+
v_in = self.to_v(context)
57+
58+
k_in *= self.scale
59+
4760
del context, x
4861

4962
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q_in, k_in, v_in))

modules/shared.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import modules.sd_models
1414
import modules.styles
1515
import modules.devices as devices
16-
from modules import sd_samplers
16+
from modules import sd_samplers, hypernetwork
1717
from modules.paths import models_path, script_path, sd_path
1818

1919
sd_model_file = os.path.join(script_path, 'model.ckpt')
@@ -76,6 +76,12 @@
7676

7777
config_filename = cmd_opts.ui_settings_file
7878

79+
hypernetworks = hypernetwork.load_hypernetworks(os.path.join(models_path, 'hypernetworks'))
80+
81+
82+
def selected_hypernetwork():
83+
return hypernetworks.get(opts.sd_hypernetwork, None)
84+
7985

8086
class State:
8187
interrupted = False
@@ -206,6 +212,7 @@ def options_section(section_identifer, options_dict):
206212

207213
options_templates.update(options_section(('sd', "Stable Diffusion"), {
208214
"sd_model_checkpoint": OptionInfo(None, "Stable Diffusion checkpoint", gr.Dropdown, lambda: {"choices": modules.sd_models.checkpoint_tiles()}),
215+
"sd_hypernetwork": OptionInfo("None", "Stable Diffusion finetune hypernetwork", gr.Dropdown, lambda: {"choices": ["None"] + [x for x in hypernetworks.keys()]}),
209216
"img2img_color_correction": OptionInfo(False, "Apply color correction to img2img results to match original colors."),
210217
"save_images_before_color_correction": OptionInfo(False, "Save a copy of image before applying color correction to img2img results"),
211218
"img2img_fix_steps": OptionInfo(False, "With img2img, do exactly the amount of steps the slider specifies (normally you'd do less with less denoising)."),

scripts/xy_grid.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ def apply_checkpoint(p, x, xs):
7777
modules.sd_models.reload_model_weights(shared.sd_model, info)
7878

7979

80+
def apply_hypernetwork(p, x, xs):
81+
hn = shared.hypernetworks.get(x, None)
82+
opts.data["sd_hypernetwork"] = hn.name if hn is not None else 'None'
83+
84+
8085
def format_value_add_label(p, opt, x):
8186
if type(x) == float:
8287
x = round(x, 8)
@@ -122,6 +127,7 @@ def str_permutations(x):
122127
AxisOption("Prompt order", str_permutations, apply_order, format_value_join_list),
123128
AxisOption("Sampler", str, apply_sampler, format_value),
124129
AxisOption("Checkpoint name", str, apply_checkpoint, format_value),
130+
AxisOption("Hypernetwork", str, apply_hypernetwork, format_value),
125131
AxisOption("Sigma Churn", float, apply_field("s_churn"), format_value_add_label),
126132
AxisOption("Sigma min", float, apply_field("s_tmin"), format_value_add_label),
127133
AxisOption("Sigma max", float, apply_field("s_tmax"), format_value_add_label),
@@ -193,6 +199,8 @@ def run(self, p, x_type, x_values, y_type, y_values, draw_legend, no_fixed_seeds
193199
modules.processing.fix_seed(p)
194200
p.batch_size = 1
195201

202+
initial_hn = opts.sd_hypernetwork
203+
196204
def process_axis(opt, vals):
197205
if opt.label == 'Nothing':
198206
return [0]
@@ -300,4 +308,6 @@ def cell(x, y):
300308
# restore checkpoint in case it was changed by axes
301309
modules.sd_models.reload_model_weights(shared.sd_model)
302310

311+
opts.data["sd_hypernetwork"] = initial_hn
312+
303313
return processed

0 commit comments

Comments
 (0)