|
| 1 | + |
| 2 | +import torch |
| 3 | +from torch import nn |
| 4 | +from typing import List |
| 5 | + |
| 6 | +from .loraw_module import LoRAWModule |
| 7 | + |
| 8 | +class LoRAWNetwork(nn.Module): |
| 9 | + def __init__( |
| 10 | + self, |
| 11 | + net, |
| 12 | + target_subnets=None, |
| 13 | + target_modules=[ |
| 14 | + 'SelfAttention1d' |
| 15 | + ], |
| 16 | + multiplier=1.0, |
| 17 | + lora_dim=4, |
| 18 | + alpha=1, |
| 19 | + dropout=None, |
| 20 | + module_class=LoRAWModule, |
| 21 | + verbose=False, |
| 22 | + ): |
| 23 | + super().__init__() |
| 24 | + |
| 25 | + self.lora_map = {} |
| 26 | + self.multiplier = multiplier |
| 27 | + self.lora_dim = lora_dim |
| 28 | + self.alpha = alpha |
| 29 | + self.dropout = dropout |
| 30 | + |
| 31 | + def create_modules( |
| 32 | + root_name, root_module: nn.Module, target_replace_modules |
| 33 | + ) -> nn.ModuleList: |
| 34 | + loras = nn.ModuleList() |
| 35 | + skipped = nn.ModuleList() |
| 36 | + for name, module in root_module.named_modules(): |
| 37 | + if module.__class__.__name__ in target_replace_modules: |
| 38 | + for child_name, child_module in module.named_modules(): |
| 39 | + is_linear = child_module.__class__.__name__ == "Linear" |
| 40 | + is_conv1d = child_module.__class__.__name__ == "Conv1d" |
| 41 | + |
| 42 | + if is_linear or is_conv1d: |
| 43 | + lora_name = "lora.{root_name}.{name}.{child_name}" |
| 44 | + lora_name = lora_name.replace(".", "_") |
| 45 | + |
| 46 | + lora = module_class( |
| 47 | + lora_name, |
| 48 | + child_module, |
| 49 | + multiplier=self.multiplier, |
| 50 | + lora_dim=self.lora_dim, |
| 51 | + alpha=self.alpha, |
| 52 | + dropout=self.dropout |
| 53 | + ) |
| 54 | + loras.append(lora) |
| 55 | + return loras, skipped |
| 56 | + |
| 57 | + for subnet_name in target_subnets: |
| 58 | + if hasattr(net.model, subnet_name): |
| 59 | + subnet = getattr(net.model, subnet_name) |
| 60 | + self.lora_map[subnet_name], _ = create_modules(subnet_name, subnet, target_modules) |
| 61 | + print(f"Created LoRAW for {subnet_name}: {len(self.lora_map[subnet_name])} modules.") |
| 62 | + |
| 63 | + ''' |
| 64 | + if verbose and len(skipped) > 0: |
| 65 | + print( |
| 66 | + f"because block_lr_weight is 0 or dim (rank) is 0, {len(skipped)} LoRA modules are skipped / block_lr_weightまたはdim (rank)が0の為、次の{len(skipped)}個のLoRAモジュールはスキップされます:" |
| 67 | + ) |
| 68 | + for name in skipped: |
| 69 | + print(f"\t{name}") |
| 70 | +
|
| 71 | + self.up_lr_weight: List[float] = None |
| 72 | + self.down_lr_weight: List[float] = None |
| 73 | + self.mid_lr_weight: float = None |
| 74 | + self.block_lr = False |
| 75 | +
|
| 76 | + # assertion |
| 77 | + names = set() |
| 78 | + for lora in self.unet_loras: |
| 79 | + assert ( |
| 80 | + lora.lora_name not in names |
| 81 | + ), f"duplicated lora name: {lora.lora_name}" |
| 82 | + names.add(lora.lora_name) |
| 83 | + ''' |
| 84 | + else: |
| 85 | + print(f'Skipping {subnet_name}: not present in this network') |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + def set_multiplier(self, multiplier): |
| 90 | + self.multiplier = multiplier |
| 91 | + for lora in self.unet_loras: |
| 92 | + lora.multiplier = self.multiplier |
| 93 | + |
| 94 | + def activate(self): |
| 95 | + for subnet_name, subnet in self.lora_map.items(): |
| 96 | + for lora in subnet: |
| 97 | + lora.activate() |
| 98 | + self.add_module(lora.lora_name, lora) |
| 99 | + print(f'Injected {len(subnet)} LoRAW modules into {subnet_name}') |
| 100 | + |
| 101 | + def is_mergeable(self): |
| 102 | + return True |
| 103 | + |
| 104 | + def save_weights(self, file, dtype=torch.float16): |
| 105 | + |
| 106 | + state_dict = self.state_dict() |
| 107 | + |
| 108 | + if dtype is not None: |
| 109 | + for key in list(state_dict.keys()): |
| 110 | + v = state_dict[key] |
| 111 | + v = v.detach().clone().to("cpu").to(dtype) |
| 112 | + state_dict[key] = v |
| 113 | + |
| 114 | + torch.save(state_dict, file) |
| 115 | + |
| 116 | + def load_weights(self, file): |
| 117 | + weights_sd = torch.load(file, map_location="cpu") |
| 118 | + |
| 119 | + info = self.load_state_dict(weights_sd, False) |
| 120 | + return info |
0 commit comments