Upload lora-scripts/sd-scripts/finetune/hypernetwork_nai.py with huggingface_hub
Browse files
lora-scripts/sd-scripts/finetune/hypernetwork_nai.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# NAI compatible
|
2 |
+
|
3 |
+
import torch
|
4 |
+
|
5 |
+
|
6 |
+
class HypernetworkModule(torch.nn.Module):
|
7 |
+
def __init__(self, dim, multiplier=1.0):
|
8 |
+
super().__init__()
|
9 |
+
|
10 |
+
linear1 = torch.nn.Linear(dim, dim * 2)
|
11 |
+
linear2 = torch.nn.Linear(dim * 2, dim)
|
12 |
+
linear1.weight.data.normal_(mean=0.0, std=0.01)
|
13 |
+
linear1.bias.data.zero_()
|
14 |
+
linear2.weight.data.normal_(mean=0.0, std=0.01)
|
15 |
+
linear2.bias.data.zero_()
|
16 |
+
linears = [linear1, linear2]
|
17 |
+
|
18 |
+
self.linear = torch.nn.Sequential(*linears)
|
19 |
+
self.multiplier = multiplier
|
20 |
+
|
21 |
+
def forward(self, x):
|
22 |
+
return x + self.linear(x) * self.multiplier
|
23 |
+
|
24 |
+
|
25 |
+
class Hypernetwork(torch.nn.Module):
|
26 |
+
enable_sizes = [320, 640, 768, 1280]
|
27 |
+
# return self.modules[Hypernetwork.enable_sizes.index(size)]
|
28 |
+
|
29 |
+
def __init__(self, multiplier=1.0) -> None:
|
30 |
+
super().__init__()
|
31 |
+
self.modules = []
|
32 |
+
for size in Hypernetwork.enable_sizes:
|
33 |
+
self.modules.append((HypernetworkModule(size, multiplier), HypernetworkModule(size, multiplier)))
|
34 |
+
self.register_module(f"{size}_0", self.modules[-1][0])
|
35 |
+
self.register_module(f"{size}_1", self.modules[-1][1])
|
36 |
+
|
37 |
+
def apply_to_stable_diffusion(self, text_encoder, vae, unet):
|
38 |
+
blocks = unet.input_blocks + [unet.middle_block] + unet.output_blocks
|
39 |
+
for block in blocks:
|
40 |
+
for subblk in block:
|
41 |
+
if 'SpatialTransformer' in str(type(subblk)):
|
42 |
+
for tf_block in subblk.transformer_blocks:
|
43 |
+
for attn in [tf_block.attn1, tf_block.attn2]:
|
44 |
+
size = attn.context_dim
|
45 |
+
if size in Hypernetwork.enable_sizes:
|
46 |
+
attn.hypernetwork = self
|
47 |
+
else:
|
48 |
+
attn.hypernetwork = None
|
49 |
+
|
50 |
+
def apply_to_diffusers(self, text_encoder, vae, unet):
|
51 |
+
blocks = unet.down_blocks + [unet.mid_block] + unet.up_blocks
|
52 |
+
for block in blocks:
|
53 |
+
if hasattr(block, 'attentions'):
|
54 |
+
for subblk in block.attentions:
|
55 |
+
if 'SpatialTransformer' in str(type(subblk)) or 'Transformer2DModel' in str(type(subblk)): # 0.6.0 and 0.7~
|
56 |
+
for tf_block in subblk.transformer_blocks:
|
57 |
+
for attn in [tf_block.attn1, tf_block.attn2]:
|
58 |
+
size = attn.to_k.in_features
|
59 |
+
if size in Hypernetwork.enable_sizes:
|
60 |
+
attn.hypernetwork = self
|
61 |
+
else:
|
62 |
+
attn.hypernetwork = None
|
63 |
+
return True # TODO error checking
|
64 |
+
|
65 |
+
def forward(self, x, context):
|
66 |
+
size = context.shape[-1]
|
67 |
+
assert size in Hypernetwork.enable_sizes
|
68 |
+
module = self.modules[Hypernetwork.enable_sizes.index(size)]
|
69 |
+
return module[0].forward(context), module[1].forward(context)
|
70 |
+
|
71 |
+
def load_from_state_dict(self, state_dict):
|
72 |
+
# old ver to new ver
|
73 |
+
changes = {
|
74 |
+
'linear1.bias': 'linear.0.bias',
|
75 |
+
'linear1.weight': 'linear.0.weight',
|
76 |
+
'linear2.bias': 'linear.1.bias',
|
77 |
+
'linear2.weight': 'linear.1.weight',
|
78 |
+
}
|
79 |
+
for key_from, key_to in changes.items():
|
80 |
+
if key_from in state_dict:
|
81 |
+
state_dict[key_to] = state_dict[key_from]
|
82 |
+
del state_dict[key_from]
|
83 |
+
|
84 |
+
for size, sd in state_dict.items():
|
85 |
+
if type(size) == int:
|
86 |
+
self.modules[Hypernetwork.enable_sizes.index(size)][0].load_state_dict(sd[0], strict=True)
|
87 |
+
self.modules[Hypernetwork.enable_sizes.index(size)][1].load_state_dict(sd[1], strict=True)
|
88 |
+
return True
|
89 |
+
|
90 |
+
def get_state_dict(self):
|
91 |
+
state_dict = {}
|
92 |
+
for i, size in enumerate(Hypernetwork.enable_sizes):
|
93 |
+
sd0 = self.modules[i][0].state_dict()
|
94 |
+
sd1 = self.modules[i][1].state_dict()
|
95 |
+
state_dict[size] = [sd0, sd1]
|
96 |
+
return state_dict
|