forked from opparco/stable-diffusion-webui-composable-lora
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcomposable_lora_function_handler.py
63 lines (59 loc) · 4.35 KB
/
composable_lora_function_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import torch
import composable_lora
import composable_lycoris
def on_enable():
#backup original forward methods
composable_lora.backup_lora_Linear_forward = torch.nn.Linear.forward
composable_lora.backup_lora_Conv2d_forward = torch.nn.Conv2d.forward
composable_lora.backup_lora_MultiheadAttention_forward = torch.nn.MultiheadAttention.forward
if hasattr(torch.nn, 'Linear_forward_before_lyco'):
#if a1111-sd-webui-lycoris installed, backup it's forward methods
import lycoris
composable_lycoris.has_webui_lycoris = True
if hasattr(torch.nn, 'Linear_forward_before_lyco'):
composable_lycoris.backup_Linear_forward_before_lyco = torch.nn.Linear_forward_before_lyco
if hasattr(torch.nn, 'Linear_load_state_dict_before_lyco'):
composable_lycoris.backup_Linear_load_state_dict_before_lyco = torch.nn.Linear_load_state_dict_before_lyco
if hasattr(torch.nn, 'Conv2d_forward_before_lyco'):
composable_lycoris.backup_Conv2d_forward_before_lyco = torch.nn.Conv2d_forward_before_lyco
if hasattr(torch.nn, 'Conv2d_load_state_dict_before_lyco'):
composable_lycoris.backup_Conv2d_load_state_dict_before_lyco = torch.nn.Conv2d_load_state_dict_before_lyco
if hasattr(torch.nn, 'MultiheadAttention_forward_before_lyco'):
composable_lycoris.backup_MultiheadAttention_forward_before_lyco = torch.nn.MultiheadAttention_forward_before_lyco
if hasattr(torch.nn, 'MultiheadAttention_load_state_dict_before_lyco'):
composable_lycoris.backup_MultiheadAttention_load_state_dict_before_lyco = torch.nn.MultiheadAttention_load_state_dict_before_lyco
if hasattr(composable_lora, 'lyco_notfound'):
if composable_lora.lyco_notfound:
torch.nn.Linear_forward_before_lyco = composable_lora.Linear_forward_before_clora
torch.nn.Conv2d_forward_before_lyco = composable_lora.Conv2d_forward_before_clora
torch.nn.MultiheadAttention_forward_before_lyco = composable_lora.MultiheadAttention_forward_before_clora
torch.nn.Linear.forward = composable_lora.lora_Linear_forward
torch.nn.Conv2d.forward = composable_lora.lora_Conv2d_forward
torch.nn.MultiheadAttention.forward = lycoris.lyco_MultiheadAttention_forward
torch.nn.MultiheadAttention._load_from_state_dict = lycoris.lyco_MultiheadAttention_load_state_dict
else:
composable_lycoris.has_webui_lycoris = False
if (composable_lora.should_reload() or (torch.nn.Linear.forward != composable_lora.lora_Linear_forward)):
if composable_lora.enabled:
torch.nn.Linear.forward = composable_lora.lora_Linear_forward
torch.nn.Conv2d.forward = composable_lora.lora_Conv2d_forward
def on_disable():
torch.nn.Linear.forward = composable_lora.backup_lora_Linear_forward
torch.nn.Conv2d.forward = composable_lora.backup_lora_Conv2d_forward
torch.nn.MultiheadAttention.forward = composable_lora.backup_lora_MultiheadAttention_forward
if hasattr(torch.nn, 'Linear_forward_before_lyco'):
composable_lycoris.has_webui_lycoris = True
if hasattr(composable_lycoris, 'backup_Linear_forward_before_lyco'):
torch.nn.Linear_forward_before_lyco = composable_lycoris.backup_Linear_forward_before_lyco
if hasattr(composable_lycoris, 'backup_Linear_load_state_dict_before_lyco'):
torch.nn.Linear_load_state_dict_before_lyco = composable_lycoris.backup_Linear_load_state_dict_before_lyco
if hasattr(composable_lycoris, 'backup_Conv2d_forward_before_lyco'):
torch.nn.Conv2d_forward_before_lyco = composable_lycoris.backup_Conv2d_forward_before_lyco
if hasattr(composable_lycoris, 'backup_Conv2d_load_state_dict_before_lyco'):
torch.nn.Conv2d_load_state_dict_before_lyco = composable_lycoris.backup_Conv2d_load_state_dict_before_lyco
if hasattr(composable_lycoris, 'backup_MultiheadAttention_forward_before_lyco'):
torch.nn.MultiheadAttention_forward_before_lyco = composable_lycoris.backup_MultiheadAttention_forward_before_lyco
if hasattr(composable_lycoris, 'backup_MultiheadAttention_load_state_dict_before_lyco'):
torch.nn.MultiheadAttention_load_state_dict_before_lyco = composable_lycoris.backup_MultiheadAttention_load_state_dict_before_lyco
else:
composable_lycoris.has_webui_lycoris = False