2023-10-18 06:35:50 +00:00
|
|
|
import torch
|
|
|
|
import network
|
|
|
|
|
|
|
|
|
|
|
|
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-19 19:41:17 +00:00
|
|
|
self.R = self.get_weight(self.oft_blocks)
|
2023-10-18 11:16:01 +00:00
|
|
|
self.apply_to()
|
|
|
|
|
|
|
|
# replace forward method of original linear rather than replacing the module
|
2023-10-19 19:41:17 +00:00
|
|
|
# how do we revert this to unload the weights?
|
2023-10-18 11:16:01 +00:00
|
|
|
def apply_to(self):
|
|
|
|
self.org_forward = self.org_module[0].forward
|
|
|
|
self.org_module[0].forward = self.forward
|
2023-10-18 06:35:50 +00:00
|
|
|
|
2023-10-19 19:41:17 +00:00
|
|
|
def get_weight(self, oft_blocks, multiplier=None):
|
|
|
|
block_Q = oft_blocks - oft_blocks.transpose(1, 2)
|
2023-10-18 06:35:50 +00:00
|
|
|
norm_Q = torch.norm(block_Q.flatten())
|
|
|
|
new_norm_Q = torch.clamp(norm_Q, max=self.constraint)
|
|
|
|
block_Q = block_Q * ((new_norm_Q + 1e-8) / (norm_Q + 1e-8))
|
|
|
|
I = torch.eye(self.block_size, device=self.oft_blocks.device).unsqueeze(0).repeat(self.num_blocks, 1, 1)
|
|
|
|
block_R = torch.matmul(I + block_Q, (I - block_Q).inverse())
|
2023-10-19 19:41:17 +00:00
|
|
|
#block_R_weighted = multiplier * block_R + (1 - multiplier) * I
|
|
|
|
#R = torch.block_diag(*block_R_weighted)
|
|
|
|
R = torch.block_diag(*block_R)
|
2023-10-18 06:35:50 +00:00
|
|
|
|
|
|
|
return R
|
|
|
|
|
|
|
|
def calc_updown(self, orig_weight):
|
2023-10-19 19:41:17 +00:00
|
|
|
oft_blocks = self.oft_blocks.to(orig_weight.device, dtype=orig_weight.dtype)
|
2023-10-18 11:27:44 +00:00
|
|
|
|
2023-10-19 19:41:17 +00:00
|
|
|
R = self.get_weight(oft_blocks)
|
|
|
|
self.R = R
|
2023-10-18 11:27:44 +00:00
|
|
|
|
2023-10-18 11:56:53 +00:00
|
|
|
# if orig_weight.dim() == 4:
|
|
|
|
# weight = torch.einsum("oihw, op -> pihw", orig_weight, R)
|
|
|
|
# else:
|
|
|
|
# weight = torch.einsum("oi, op -> pi", orig_weight, R)
|
2023-10-18 11:27:44 +00:00
|
|
|
|
2023-10-19 19:41:17 +00:00
|
|
|
updown = orig_weight @ R
|
2023-10-18 11:27:44 +00:00
|
|
|
output_shape = self.oft_blocks.shape
|
|
|
|
|
2023-10-18 06:35:50 +00:00
|
|
|
return self.finalize_updown(updown, orig_weight, output_shape)
|
|
|
|
|
2023-10-18 11:16:01 +00:00
|
|
|
def forward(self, x, y=None):
|
|
|
|
x = self.org_forward(x)
|
|
|
|
if self.multiplier() == 0.0:
|
|
|
|
return x
|
2023-10-19 19:41:17 +00:00
|
|
|
|
|
|
|
# calculating R here is excruciatingly slow
|
2023-10-18 11:27:44 +00:00
|
|
|
#R = self.get_weight().to(x.device, dtype=x.dtype)
|
|
|
|
R = self.R.to(x.device, dtype=x.dtype)
|
2023-10-19 19:41:17 +00:00
|
|
|
|
2023-10-18 11:16:01 +00:00
|
|
|
if x.dim() == 4:
|
|
|
|
x = x.permute(0, 2, 3, 1)
|
|
|
|
x = torch.matmul(x, R)
|
|
|
|
x = x.permute(0, 3, 1, 2)
|
|
|
|
else:
|
|
|
|
x = torch.matmul(x, R)
|
|
|
|
return x
|