|
8 | 8 | from .math import attention, rope |
9 | 9 | import comfy.ops |
10 | 10 |
|
| 11 | + |
11 | 12 | class EmbedND(nn.Module): |
12 | 13 | def __init__(self, dim: int, theta: int, axes_dim: list): |
13 | 14 | super().__init__() |
@@ -174,20 +175,19 @@ def forward(self, img: Tensor, txt: Tensor, vec: Tensor, pe: Tensor): |
174 | 175 | txt_q, txt_k = self.txt_attn.norm(txt_q, txt_k, txt_v) |
175 | 176 |
|
176 | 177 | # run actual attention |
177 | | - q = torch.cat((txt_q, img_q), dim=2) |
178 | | - k = torch.cat((txt_k, img_k), dim=2) |
179 | | - v = torch.cat((txt_v, img_v), dim=2) |
| 178 | + attn = attention(torch.cat((txt_q, img_q), dim=2), |
| 179 | + torch.cat((txt_k, img_k), dim=2), |
| 180 | + torch.cat((txt_v, img_v), dim=2), pe=pe) |
180 | 181 |
|
181 | | - attn = attention(q, k, v, pe=pe) |
182 | 182 | txt_attn, img_attn = attn[:, : txt.shape[1]], attn[:, txt.shape[1] :] |
183 | 183 |
|
184 | 184 | # calculate the img bloks |
185 | | - img = img + img_mod1.gate * self.img_attn.proj(img_attn) |
186 | | - img = img + img_mod2.gate * self.img_mlp((1 + img_mod2.scale) * self.img_norm2(img) + img_mod2.shift) |
| 185 | + img += img_mod1.gate * self.img_attn.proj(img_attn) |
| 186 | + img += img_mod2.gate * self.img_mlp((1 + img_mod2.scale) * self.img_norm2(img) + img_mod2.shift) |
187 | 187 |
|
188 | 188 | # calculate the txt bloks |
189 | | - txt = txt + txt_mod1.gate * self.txt_attn.proj(txt_attn) |
190 | | - txt = txt + txt_mod2.gate * self.txt_mlp((1 + txt_mod2.scale) * self.txt_norm2(txt) + txt_mod2.shift) |
| 189 | + txt += txt_mod1.gate * self.txt_attn.proj(txt_attn) |
| 190 | + txt += txt_mod2.gate * self.txt_mlp((1 + txt_mod2.scale) * self.txt_norm2(txt) + txt_mod2.shift) |
191 | 191 |
|
192 | 192 | if txt.dtype == torch.float16: |
193 | 193 | txt = txt.clip(-65504, 65504) |
@@ -243,7 +243,7 @@ def forward(self, x: Tensor, vec: Tensor, pe: Tensor) -> Tensor: |
243 | 243 | attn = attention(q, k, v, pe=pe) |
244 | 244 | # compute activation in mlp stream, cat again and run second linear layer |
245 | 245 | output = self.linear2(torch.cat((attn, self.mlp_act(mlp)), 2)) |
246 | | - x = x + mod.gate * output |
| 246 | + x += mod.gate * output |
247 | 247 | if x.dtype == torch.float16: |
248 | 248 | x = x.clip(-65504, 65504) |
249 | 249 | return x |
|
0 commit comments