MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / layer_forward

Method layer_forward

sat/dit_video_concat.py:674–786  ·  view source on GitHub ↗
(
        self,
        hidden_states,
        mask,
        *args,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

672 return hidden_states
673
674 def layer_forward(
675 self,
676 hidden_states,
677 mask,
678 *args,
679 **kwargs,
680 ):
681 text_length = kwargs["text_length"]
682 # hidden_states (b,(n_t+t*n_i),d)
683 text_hidden_states = hidden_states[:, :text_length] # (b,n,d)
684 img_hidden_states = hidden_states[:, text_length:] # (b,(t n),d)
685
686 split_cond_flag = kwargs['split_cond_flag']
687 layer = self.transformer.layers[kwargs["layer_id"]]
688 adaLN_modulation = self.adaLN_modulations[kwargs["layer_id"]]
689
690 (
691 shift_msa,
692 scale_msa,
693 gate_msa,
694 shift_mlp,
695 scale_mlp,
696 gate_mlp,
697 text_shift_msa,
698 text_scale_msa,
699 text_gate_msa,
700 text_shift_mlp,
701 text_scale_mlp,
702 text_gate_mlp,
703 ) = adaLN_modulation(kwargs["emb"]).chunk(12, dim=1) # * modulations for img and text
704
705 gate_msa, gate_mlp, text_gate_msa, text_gate_mlp = (
706 gate_msa.unsqueeze(1),
707 gate_mlp.unsqueeze(1),
708 text_gate_msa.unsqueeze(1),
709 text_gate_mlp.unsqueeze(1),
710 )
711
712 # * prepare modulation for aug frames
713 if split_cond_flag:
714 cond_inds = kwargs["cond_inds"]
715 pred_inds = [i for i in range(self.compressed_num_frames) if i not in cond_inds]
716 assert cond_inds[-1] < pred_inds[0], f"cond frames must be ahead of pred frames, {cond_inds}, {pred_inds}"
717
718 (
719 aug_shift_msa,
720 aug_scale_msa,
721 aug_gate_msa,
722 aug_shift_mlp,
723 aug_scale_mlp,
724 aug_gate_mlp,
725 ) = adaLN_modulation(kwargs["aug_emb"]).chunk(12, dim=1)[: 6] # * modulations for aug
726 aug_gate_msa, aug_gate_mlp = aug_gate_msa.unsqueeze(1), aug_gate_mlp.unsqueeze(1)
727
728 # self full attention (b,(t n),d)
729 img_attention_input = layer.input_layernorm(img_hidden_states)
730 text_attention_input = layer.input_layernorm(text_hidden_states)
731

Callers

nothing calls this directly

Calls 4

separate_modulateMethod · 0.95
separate_gatingMethod · 0.95
modulateFunction · 0.85
attentionMethod · 0.45

Tested by

no test coverage detected