(
self, hidden_states, attention_mask, rotary_pos_emb, kv_caches=None,
use_cache: Optional[bool] = True,
output_hidden_states: Optional[bool] = False,
)
| 864 | return self.layers[layer_number] |
| 865 | |
| 866 | def forward( |
| 867 | self, hidden_states, attention_mask, rotary_pos_emb, kv_caches=None, |
| 868 | use_cache: Optional[bool] = True, |
| 869 | output_hidden_states: Optional[bool] = False, |
| 870 | ): |
| 871 | if not kv_caches: |
| 872 | kv_caches = [None for _ in range(self.num_layers)] |
| 873 | presents = () if use_cache else None |
| 874 | if self.gradient_checkpointing and self.training: |
| 875 | if use_cache: |
| 876 | logger.warning_once( |
| 877 | "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." |
| 878 | ) |
| 879 | use_cache = False |
| 880 | |
| 881 | all_self_attentions = None |
| 882 | all_hidden_states = () if output_hidden_states else None |
| 883 | for index in range(self.num_layers): |
| 884 | if output_hidden_states: |
| 885 | all_hidden_states = all_hidden_states + (hidden_states,) |
| 886 | |
| 887 | layer = self._get_layer(index) |
| 888 | if self.gradient_checkpointing and self.training: |
| 889 | layer_ret = torch.utils.checkpoint.checkpoint( |
| 890 | layer, |
| 891 | hidden_states, |
| 892 | attention_mask, |
| 893 | rotary_pos_emb, |
| 894 | kv_caches[index], |
| 895 | use_cache |
| 896 | ) |
| 897 | else: |
| 898 | layer_ret = layer( |
| 899 | hidden_states, |
| 900 | attention_mask, |
| 901 | rotary_pos_emb, |
| 902 | kv_cache=kv_caches[index], |
| 903 | use_cache=use_cache |
| 904 | ) |
| 905 | hidden_states, kv_cache = layer_ret |
| 906 | if use_cache: |
| 907 | presents = presents + (kv_cache,) |
| 908 | |
| 909 | if output_hidden_states: |
| 910 | all_hidden_states = all_hidden_states + (hidden_states,) |
| 911 | |
| 912 | # Final layer norm. |
| 913 | if self.post_layer_norm: |
| 914 | hidden_states = self.final_layernorm(hidden_states) |
| 915 | |
| 916 | return hidden_states, presents, all_hidden_states, all_self_attentions |
| 917 | |
| 918 | |
| 919 | class ChatGLMPreTrainedModel(PreTrainedModel): |
nothing calls this directly
no test coverage detected