MCPcopy Create free account
hub / github.com/CompVis/diff2flow / LoRAConv

Class LoRAConv

diff2flow/lora.py:83–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

81
82
83class LoRAConv(nn.Module):
84 def __init__(
85 self,
86 in_channels: int,
87 out_channels: int,
88 kernel_size: Union[int, Tuple[int, int]],
89 stride: Union[int, Tuple[int, int]],
90 padding: Union[int, Tuple[int, int]],
91 rank: int = None,
92 lora_scale: float = 1.0,
93 ):
94 super().__init__()
95
96 # self.lora_scale = alpha / rank
97 self.lora_scale = lora_scale
98
99 self.W = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)
100 for p in self.W.parameters():
101 p.requires_grad_(False)
102
103 self.A = nn.Conv2d(in_channels, rank, kernel_size, stride, padding, bias=False)
104 self.B = nn.Conv2d(rank, out_channels, kernel_size=1, stride=1, padding=0, bias=False)
105
106 nn.init.zeros_(self.B.weight)
107 nn.init.kaiming_normal_(self.A.weight, a=1)
108
109 def forward(self, x):
110 """
111 Args:
112 x (torch.Tensor): In shape of (B, C, H, W)
113 """
114 w_out = self.W(x)
115 a_out = self.A(x)
116 b_out = self.B(a_out)
117
118 return w_out + b_out * self.lora_scale
119
120
121class LoRAAdapterConv(nn.Module):

Callers 1

add_lora_to_unetMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected