MCPcopy Create free account
hub / github.com/zai-org/CodeGeeX / forward

Method forward

codegeex/megatron/model/transformer.py:987–1062  ·  view source on GitHub ↗
(
            self,
            hidden_states,
            query_hidden_state,
            attention_mask,
            layer_past=None,
            get_key_value=False,
            prompt_length=None,
            context_length=None,
    )

Source from the content-addressed store, hash-verified

985 self.input_tensor = input_tensor
986
987 def forward(
988 self,
989 hidden_states,
990 query_hidden_state,
991 attention_mask,
992 layer_past=None,
993 get_key_value=False,
994 prompt_length=None,
995 context_length=None,
996 ):
997
998 # Checks
999 if layer_past is not None:
1000 assert get_key_value, \
1001 'for not None values in layer_past, ' \
1002 'expected get_key_value to be set'
1003 if get_key_value:
1004 assert not self.checkpoint_activations, \
1005 'get_key_value does not work with ' \
1006 'activation checkpointing'
1007
1008 # data format change to avoid explicit tranposes : [b s h] --> [s b h]
1009 hidden_states = hidden_states.transpose(0, 1).contiguous()
1010 query_hidden_state = query_hidden_state.transpose(0, 1).contiguous()
1011
1012 if self.checkpoint_activations:
1013 hidden_states = self._checkpointed_forward(hidden_states,
1014 attention_mask)
1015 else:
1016 if get_key_value:
1017 presents = []
1018 for index in range(self.num_layers):
1019 layer = self._get_layer(index)
1020 past = None
1021 if layer_past is not None:
1022 past = layer_past[index]
1023 hidden_states = layer(hidden_states,
1024 attention_mask,
1025 layer_past=past,
1026 get_key_value=get_key_value,
1027 prompt_length=prompt_length,
1028 context_length=context_length)
1029 if get_key_value:
1030 hidden_states, present = hidden_states
1031 presents.append(present)
1032
1033 if self.ln_fp16:
1034 hidden_states_ = self.final_layernorm(hidden_states)
1035 else:
1036 hidden_states_ = self.final_layernorm(hidden_states.float()).half()
1037
1038 #################################
1039 # top query layer
1040 #################################
1041 past = None
1042 if layer_past is not None:
1043 past = layer_past[self.num_layers]
1044 hidden_states = self.topQueryLayer(hidden_states_,

Callers

nothing calls this directly

Calls 2

_checkpointed_forwardMethod · 0.95
_get_layerMethod · 0.95

Tested by

no test coverage detected