mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-08-25 21:12:16 +08:00
Add addcmul pattern to Qwen
This commit is contained in:
parent
714950ff07
commit
17038d592e
@ -214,9 +214,9 @@ class QwenImageTransformerBlock(nn.Module):
|
|||||||
operations=operations,
|
operations=operations,
|
||||||
)
|
)
|
||||||
|
|
||||||
def _modulate(self, x, mod_params):
|
def _modulate(self, x: torch.Tensor, mod_params: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||||
shift, scale, gate = mod_params.chunk(3, dim=-1)
|
shift, scale, gate = torch.chunk(mod_params, 3, dim=-1)
|
||||||
return torch.addcmul(shift.unsqueeze(1), x, (1 + scale.unsqueeze(1))), gate.unsqueeze(1)
|
return torch.addcmul(shift.unsqueeze(1), x, 1 + scale.unsqueeze(1)), gate.unsqueeze(1)
|
||||||
|
|
||||||
def forward(
|
def forward(
|
||||||
self,
|
self,
|
||||||
@ -248,11 +248,11 @@ class QwenImageTransformerBlock(nn.Module):
|
|||||||
|
|
||||||
img_normed2 = self.img_norm2(hidden_states)
|
img_normed2 = self.img_norm2(hidden_states)
|
||||||
img_modulated2, img_gate2 = self._modulate(img_normed2, img_mod2)
|
img_modulated2, img_gate2 = self._modulate(img_normed2, img_mod2)
|
||||||
hidden_states = hidden_states + img_gate2 * self.img_mlp(img_modulated2)
|
hidden_states = torch.addcmul(hidden_states, img_gate2, self.img_mlp(img_modulated2))
|
||||||
|
|
||||||
txt_normed2 = self.txt_norm2(encoder_hidden_states)
|
txt_normed2 = self.txt_norm2(encoder_hidden_states)
|
||||||
txt_modulated2, txt_gate2 = self._modulate(txt_normed2, txt_mod2)
|
txt_modulated2, txt_gate2 = self._modulate(txt_normed2, txt_mod2)
|
||||||
encoder_hidden_states = encoder_hidden_states + txt_gate2 * self.txt_mlp(txt_modulated2)
|
encoder_hidden_states = torch.addcmul(encoder_hidden_states, txt_gate2, self.txt_mlp(txt_modulated2))
|
||||||
|
|
||||||
return encoder_hidden_states, hidden_states
|
return encoder_hidden_states, hidden_states
|
||||||
|
|
||||||
@ -275,7 +275,7 @@ class LastLayer(nn.Module):
|
|||||||
def forward(self, x: torch.Tensor, conditioning_embedding: torch.Tensor) -> torch.Tensor:
|
def forward(self, x: torch.Tensor, conditioning_embedding: torch.Tensor) -> torch.Tensor:
|
||||||
emb = self.linear(self.silu(conditioning_embedding))
|
emb = self.linear(self.silu(conditioning_embedding))
|
||||||
scale, shift = torch.chunk(emb, 2, dim=1)
|
scale, shift = torch.chunk(emb, 2, dim=1)
|
||||||
x = self.norm(x) * (1 + scale)[:, None, :] + shift[:, None, :]
|
x = torch.addcmul(shift[:, None, :], self.norm(x), (1 + scale)[:, None, :])
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user