| 84 | from .chatglm_model import ChatGLMFinalMixin |
| 85 | |
| 86 | class ChatGLM2Model(BaseModel): |
| 87 | def __init__(self, args, transformer=None, **kwargs): |
| 88 | super(ChatGLM2Model, self).__init__(args, transformer=transformer, activation_func=F.silu, layernorm=RMSNorm, **kwargs) |
| 89 | del self.transformer.position_embeddings |
| 90 | self.add_mixin("chatglm-final", ChatGLMFinalMixin(args.vocab_size, args.hidden_size)) |
| 91 | self.add_mixin("attn", ChatGLM2AttnMixin(args.hidden_size, args.num_attention_heads, args.max_sequence_length)) |
| 92 | if not (hasattr(args, 'is_gated_mlp') and args.is_gated_mlp): |
| 93 | self.add_mixin("mlp", SwiGLUMixin(args.num_layers, args.hidden_size, args.inner_hidden_size, bias=args.use_bias)) |
| 94 | |
| 95 | def position_embedding_forward(self, position_ids, output_cross_layer, **kw_args): |
| 96 | return None |
| 97 | |
| 98 | def get_masks(self, input_ids, past_key_values, padding_mask=None): |
| 99 | batch_size, seq_length = input_ids.shape |
| 100 | full_attention_mask = torch.ones(batch_size, seq_length, seq_length, dtype=next(self.parameters()).dtype, device=input_ids.device) |
| 101 | full_attention_mask.tril_() |
| 102 | past_length = 0 |
| 103 | if past_key_values: |
| 104 | past_length = past_key_values[0][0].shape[2] |
| 105 | if past_length: |
| 106 | full_attention_mask = torch.cat((torch.ones(batch_size, seq_length, past_length, dtype=next(self.parameters()).dtype, |
| 107 | device=input_ids.device), full_attention_mask), dim=-1) |
| 108 | if padding_mask is not None: |
| 109 | full_attention_mask = full_attention_mask * padding_mask.unsqueeze(1) |
| 110 | if not past_length and padding_mask is not None: |
| 111 | full_attention_mask -= padding_mask.unsqueeze(-1) - 1 |
| 112 | full_attention_mask = (full_attention_mask < 0.5).bool() |
| 113 | full_attention_mask.unsqueeze_(1) |
| 114 | return full_attention_mask |
| 115 | |
| 116 | def get_position_ids(self, input_ids): |
| 117 | batch_size, seq_length = input_ids.shape |
| 118 | position_ids = torch.arange(seq_length, dtype=torch.long, device=input_ids.device).unsqueeze(0).repeat(batch_size, 1) |
| 119 | return position_ids |
| 120 | |
| 121 | def forward(self, input_ids, position_ids=None, attention_mask=None, past_key_values=None, **kwargs): |
| 122 | if position_ids is None: |
| 123 | position_ids = self.get_position_ids(input_ids) |
| 124 | if attention_mask is not None and attention_mask.ndim == 4: |
| 125 | pass |
| 126 | elif past_key_values is not None and input_ids.size(0) == 1: |
| 127 | attention_mask = torch.tensor([[1]], dtype=torch.long, device=input_ids.device) |
| 128 | else: |
| 129 | attention_mask = self.get_masks(input_ids, past_key_values, padding_mask=attention_mask) |
| 130 | if attention_mask is not None and attention_mask.dtype is torch.bool: |
| 131 | attention_mask = ~attention_mask |
| 132 | attention_mask = attention_mask.to(next(self.parameters()).dtype) |
| 133 | if past_key_values is not None: |
| 134 | input_ids = input_ids[:, -1:] |
| 135 | position_ids = position_ids[..., -1:] |
| 136 | if input_ids.size(0) != 1: |
| 137 | attention_mask = attention_mask[:, :, -1:] |
| 138 | return super().forward(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, past_key_values=past_key_values, **kwargs) |
| 139 | |
| 140 | @classmethod |
| 141 | def add_model_specific_args(cls, parser): |
| 142 | group = parser.add_argument_group('ChatGLM2', 'ChatGLM2 Configurations') |
| 143 | return super().add_model_specific_args(parser) |
no outgoing calls
no test coverage detected