MCPcopy Create free account
hub / github.com/ToTheBeginning/PuLID / forward

Method forward

flux/modules/layers.py:158–191  ·  view source on GitHub ↗
(self, img: Tensor, txt: Tensor, vec: Tensor, pe: Tensor)

Source from the content-addressed store, hash-verified

156 )
157
158 def forward(self, img: Tensor, txt: Tensor, vec: Tensor, pe: Tensor) -> tuple[Tensor, Tensor]:
159 img_mod1, img_mod2 = self.img_mod(vec)
160 txt_mod1, txt_mod2 = self.txt_mod(vec)
161
162 # prepare image for attention
163 img_modulated = self.img_norm1(img)
164 img_modulated = (1 + img_mod1.scale) * img_modulated + img_mod1.shift
165 img_qkv = self.img_attn.qkv(img_modulated)
166 img_q, img_k, img_v = rearrange(img_qkv, "B L (K H D) -> K B H L D", K=3, H=self.num_heads)
167 img_q, img_k = self.img_attn.norm(img_q, img_k, img_v)
168
169 # prepare txt for attention
170 txt_modulated = self.txt_norm1(txt)
171 txt_modulated = (1 + txt_mod1.scale) * txt_modulated + txt_mod1.shift
172 txt_qkv = self.txt_attn.qkv(txt_modulated)
173 txt_q, txt_k, txt_v = rearrange(txt_qkv, "B L (K H D) -> K B H L D", K=3, H=self.num_heads)
174 txt_q, txt_k = self.txt_attn.norm(txt_q, txt_k, txt_v)
175
176 # 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)
180
181 attn = attention(q, k, v, pe=pe)
182 txt_attn, img_attn = attn[:, : txt.shape[1]], attn[:, txt.shape[1] :]
183
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)
187
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)
191 return img, txt
192
193
194class SingleStreamBlock(nn.Module):

Callers

nothing calls this directly

Calls 1

attentionFunction · 0.90

Tested by

no test coverage detected