(self, hidden_states, conditioning, freqs_cis=None, txt_len=256, token_replace_vec=None, tr_token=None, split_token=71)
| 535 | ) |
| 536 | |
| 537 | def forward(self, hidden_states, conditioning, freqs_cis=None, txt_len=256, token_replace_vec=None, tr_token=None, split_token=71): |
| 538 | mod_shift, mod_scale, mod_gate = self.mod(conditioning).chunk(3, dim=-1) |
| 539 | if token_replace_vec is not None: |
| 540 | assert tr_token is not None |
| 541 | tr_mod_shift, tr_mod_scale, tr_mod_gate = self.mod(token_replace_vec).chunk(3, dim=-1) |
| 542 | else: |
| 543 | tr_mod_shift, tr_mod_scale, tr_mod_gate = None, None, None |
| 544 | |
| 545 | norm_hidden_states = self.norm(hidden_states) |
| 546 | norm_hidden_states = modulate(norm_hidden_states, shift=mod_shift, scale=mod_scale, |
| 547 | tr_shift=tr_mod_shift, tr_scale=tr_mod_scale, tr_token=tr_token) |
| 548 | qkv = self.to_qkv(norm_hidden_states) |
| 549 | |
| 550 | q, k, v = rearrange(qkv, "B L (K H D) -> K B L H D", K=3, H=self.heads_num) |
| 551 | |
| 552 | q = self.norm_q(q) |
| 553 | k = self.norm_k(k) |
| 554 | |
| 555 | q_a, q_b = q[:, :-txt_len, :, :], q[:, -txt_len:, :, :] |
| 556 | k_a, k_b = k[:, :-txt_len, :, :], k[:, -txt_len:, :, :] |
| 557 | q_a, k_a = apply_rotary_emb(q_a, k_a, freqs_cis, head_first=False) |
| 558 | |
| 559 | v_len = txt_len - split_token |
| 560 | q_a, q_b = torch.concat([q_a, q_b[:, :split_token]], dim=1), q_b[:, split_token:].contiguous() |
| 561 | k_a, k_b = torch.concat([k_a, k_b[:, :split_token]], dim=1), k_b[:, split_token:].contiguous() |
| 562 | v_a, v_b = v[:, :-v_len].contiguous(), v[:, -v_len:].contiguous() |
| 563 | |
| 564 | attn_output_a = attention(q_a, k_a, v_a) |
| 565 | attn_output_b = attention(q_b, k_b, v_b) |
| 566 | attn_output = torch.concat([attn_output_a, attn_output_b], dim=1) |
| 567 | |
| 568 | hidden_states = hidden_states + apply_gate(self.to_out(attn_output), mod_gate, tr_mod_gate, tr_token) |
| 569 | hidden_states = hidden_states + apply_gate(self.ff(norm_hidden_states), mod_gate, tr_mod_gate, tr_token) |
| 570 | return hidden_states |
| 571 | |
| 572 | |
| 573 | class FinalLayer(torch.nn.Module): |
nothing calls this directly
no test coverage detected