2023-10-18 06:35:50 +00:00
|
|
|
import torch
|
|
|
|
import network
|
2023-11-02 05:34:27 +00:00
|
|
|
from einops import rearrange
|
2023-10-18 06:35:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ModuleTypeOFT(network.ModuleType):
|
|
|
|
def create_module(self, net: network.Network, weights: network.NetworkWeights):
|
|
|
|
if all(x in weights.w for x in ["oft_blocks"]):
|
|
|
|
return NetworkModuleOFT(net, weights)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2023-10-19 19:41:17 +00:00
|
|
|
# adapted from kohya's implementation https://github.com/kohya-ss/sd-scripts/blob/main/networks/oft.py
|
2023-10-18 06:35:50 +00:00
|
|
|
class NetworkModuleOFT(network.NetworkModule):
|
|
|
|
def __init__(self, net: network.Network, weights: network.NetworkWeights):
|
2023-10-18 11:16:01 +00:00
|
|
|
|
2023-10-18 06:35:50 +00:00
|
|
|
super().__init__(net, weights)
|
|
|
|
|
|
|
|
self.oft_blocks = weights.w["oft_blocks"]
|
|
|
|
self.alpha = weights.w["alpha"]
|
|
|
|
self.dim = self.oft_blocks.shape[0]
|
|
|
|
self.num_blocks = self.dim
|
|
|
|
|
|
|
|
if "Linear" in self.sd_module.__class__.__name__:
|
|
|
|
self.out_dim = self.sd_module.out_features
|
|
|
|
elif "Conv" in self.sd_module.__class__.__name__:
|
|
|
|
self.out_dim = self.sd_module.out_channels
|
|
|
|
|
2023-10-19 19:41:17 +00:00
|
|
|
self.constraint = self.alpha * self.out_dim
|
2023-10-18 06:35:50 +00:00
|
|
|
self.block_size = self.out_dim // self.num_blocks
|
|
|
|
|
2023-10-18 11:16:01 +00:00
|
|
|
self.org_module: list[torch.Module] = [self.sd_module]
|
2023-10-21 23:07:45 +00:00
|
|
|
|
2023-11-02 05:34:27 +00:00
|
|
|
# def merge_weight(self, R_weight, org_weight):
|
|
|
|
# R_weight = R_weight.to(org_weight.device, dtype=org_weight.dtype)
|
|
|
|
# if org_weight.dim() == 4:
|
|
|
|
# weight = torch.einsum("oihw, op -> pihw", org_weight, R_weight)
|
|
|
|
# else:
|
|
|
|
# weight = torch.einsum("oi, op -> pi", org_weight, R_weight)
|
|
|
|
# weight = torch.einsum(
|
|
|
|
# "k n m, k n ... -> k m ...",
|
|
|
|
# self.oft_diag * scale + torch.eye(self.block_size, device=device),
|
|
|
|
# org_weight
|
|
|
|
# )
|
|
|
|
# return weight
|
2023-10-21 23:07:45 +00:00
|
|
|
|
2023-10-19 19:41:17 +00:00
|
|
|
def get_weight(self, oft_blocks, multiplier=None):
|
2023-11-02 05:34:27 +00:00
|
|
|
# constraint = self.constraint.to(oft_blocks.device, dtype=oft_blocks.dtype)
|
2023-10-22 15:54:24 +00:00
|
|
|
|
2023-11-02 05:34:27 +00:00
|
|
|
# block_Q = oft_blocks - oft_blocks.transpose(1, 2)
|
|
|
|
# norm_Q = torch.norm(block_Q.flatten())
|
|
|
|
# new_norm_Q = torch.clamp(norm_Q, max=constraint)
|
|
|
|
# block_Q = block_Q * ((new_norm_Q + 1e-8) / (norm_Q + 1e-8))
|
|
|
|
# m_I = torch.eye(self.block_size, device=oft_blocks.device).unsqueeze(0).repeat(self.num_blocks, 1, 1)
|
|
|
|
# block_R = torch.matmul(m_I + block_Q, (m_I - block_Q).inverse())
|
2023-10-22 15:54:24 +00:00
|
|
|
|
2023-11-02 05:34:27 +00:00
|
|
|
# block_R_weighted = multiplier * block_R + (1 - multiplier) * m_I
|
|
|
|
# R = torch.block_diag(*block_R_weighted)
|
|
|
|
#return R
|
|
|
|
return self.oft_blocks
|
2023-10-18 06:35:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def calc_updown(self, orig_weight):
|
2023-10-22 16:27:48 +00:00
|
|
|
multiplier = self.multiplier() * self.calc_scale()
|
2023-11-02 05:34:27 +00:00
|
|
|
#R = self.get_weight(self.oft_blocks, multiplier)
|
|
|
|
R = self.oft_blocks.to(orig_weight.device, dtype=orig_weight.dtype)
|
|
|
|
#merged_weight = self.merge_weight(R, orig_weight)
|
|
|
|
|
|
|
|
orig_weight = rearrange(orig_weight, '(k n) ... -> k n ...', k=self.num_blocks, n=self.block_size)
|
|
|
|
weight = torch.einsum(
|
|
|
|
'k n m, k n ... -> k m ...',
|
|
|
|
R * multiplier + torch.eye(self.block_size, device=orig_weight.device),
|
|
|
|
orig_weight
|
|
|
|
)
|
|
|
|
weight = rearrange(weight, 'k m ... -> (k m) ...')
|
|
|
|
|
|
|
|
#updown = merged_weight.to(orig_weight.device, dtype=orig_weight.dtype) - orig_weight
|
|
|
|
updown = weight.to(orig_weight.device, dtype=orig_weight.dtype) - orig_weight
|
2023-10-21 21:42:24 +00:00
|
|
|
output_shape = orig_weight.shape
|
2023-10-22 15:54:24 +00:00
|
|
|
orig_weight = orig_weight
|
2023-10-18 11:27:44 +00:00
|
|
|
|
2023-10-18 06:35:50 +00:00
|
|
|
return self.finalize_updown(updown, orig_weight, output_shape)
|
2023-10-22 16:31:15 +00:00
|
|
|
|
2023-10-22 16:27:48 +00:00
|
|
|
# override to remove the multiplier/scale factor; it's already multiplied in get_weight
|
|
|
|
def finalize_updown(self, updown, orig_weight, output_shape, ex_bias=None):
|
|
|
|
#return super().finalize_updown(updown, orig_weight, output_shape, ex_bias)
|
|
|
|
|
|
|
|
if self.bias is not None:
|
|
|
|
updown = updown.reshape(self.bias.shape)
|
|
|
|
updown += self.bias.to(orig_weight.device, dtype=orig_weight.dtype)
|
|
|
|
updown = updown.reshape(output_shape)
|
|
|
|
|
|
|
|
if len(output_shape) == 4:
|
|
|
|
updown = updown.reshape(output_shape)
|
|
|
|
|
|
|
|
if orig_weight.size().numel() == updown.size().numel():
|
|
|
|
updown = updown.reshape(orig_weight.shape)
|
|
|
|
|
|
|
|
if ex_bias is not None:
|
|
|
|
ex_bias = ex_bias * self.multiplier()
|
|
|
|
|
|
|
|
return updown, ex_bias
|