MCPcopy Create free account
hub / github.com/FreedomIntelligence/CMB / forward

Method forward

workers/chatglm3_modeling.py:608–658  ·  view source on GitHub ↗
(
            self, hidden_states, attention_mask, rotary_pos_emb, kv_caches=None,
            use_cache: Optional[bool] = True,
            output_hidden_states: Optional[bool] = False,
    )

Source from the content-addressed store, hash-verified

606 return self.layers[layer_number]
607
608 def forward(
609 self, hidden_states, attention_mask, rotary_pos_emb, kv_caches=None,
610 use_cache: Optional[bool] = True,
611 output_hidden_states: Optional[bool] = False,
612 ):
613 if not kv_caches:
614 kv_caches = [None for _ in range(self.num_layers)]
615 presents = () if use_cache else None
616 if self.gradient_checkpointing and self.training:
617 if use_cache:
618 logger.warning_once(
619 "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
620 )
621 use_cache = False
622
623 all_self_attentions = None
624 all_hidden_states = () if output_hidden_states else None
625 for index in range(self.num_layers):
626 if output_hidden_states:
627 all_hidden_states = all_hidden_states + (hidden_states,)
628
629 layer = self._get_layer(index)
630 if self.gradient_checkpointing and self.training:
631 layer_ret = torch.utils.checkpoint.checkpoint(
632 layer,
633 hidden_states,
634 attention_mask,
635 rotary_pos_emb,
636 kv_caches[index],
637 use_cache
638 )
639 else:
640 layer_ret = layer(
641 hidden_states,
642 attention_mask,
643 rotary_pos_emb,
644 kv_cache=kv_caches[index],
645 use_cache=use_cache
646 )
647 hidden_states, kv_cache = layer_ret
648 if use_cache:
649 presents = presents + (kv_cache,)
650
651 if output_hidden_states:
652 all_hidden_states = all_hidden_states + (hidden_states,)
653
654 # Final layer norm.
655 if self.post_layer_norm:
656 hidden_states = self.final_layernorm(hidden_states)
657
658 return hidden_states, presents, all_hidden_states, all_self_attentions
659
660
661class ChatGLMPreTrainedModel(PreTrainedModel):

Callers

nothing calls this directly

Calls 1

_get_layerMethod · 0.95

Tested by

no test coverage detected