An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
| 570 | |
| 571 | |
| 572 | class ChatGLMPreTrainedModel(PreTrainedModel): |
| 573 | """ |
| 574 | An abstract class to handle weights initialization and |
| 575 | a simple interface for downloading and loading pretrained models. |
| 576 | """ |
| 577 | |
| 578 | is_parallelizable = False |
| 579 | supports_gradient_checkpointing = True |
| 580 | config_class = ChatGLMConfig |
| 581 | base_model_prefix = "transformer" |
| 582 | _no_split_modules = ["GLMBlock"] |
| 583 | |
| 584 | def _init_weights(self, module: nn.Module): |
| 585 | """Initialize the weights.""" |
| 586 | return |
| 587 | |
| 588 | def get_masks(self, input_ids, past_key_values, padding_mask=None): |
| 589 | batch_size, seq_length = input_ids.shape |
| 590 | full_attention_mask = torch.ones(batch_size, seq_length, seq_length, device=input_ids.device) |
| 591 | full_attention_mask.tril_() |
| 592 | past_length = 0 |
| 593 | if past_key_values: |
| 594 | past_length = past_key_values[0][0].shape[0] |
| 595 | if past_length: |
| 596 | full_attention_mask = torch.cat((torch.ones(batch_size, seq_length, past_length, |
| 597 | device=input_ids.device), full_attention_mask), dim=-1) |
| 598 | if padding_mask is not None: |
| 599 | full_attention_mask = full_attention_mask * padding_mask.unsqueeze(1) |
| 600 | if not past_length and padding_mask is not None: |
| 601 | full_attention_mask -= padding_mask.unsqueeze(-1) - 1 |
| 602 | full_attention_mask = (full_attention_mask < 0.5).bool() |
| 603 | full_attention_mask.unsqueeze_(1) |
| 604 | return full_attention_mask |
| 605 | |
| 606 | def get_position_ids(self, input_ids, device): |
| 607 | batch_size, seq_length = input_ids.shape |
| 608 | position_ids = torch.arange(seq_length, dtype=torch.long, device=device).unsqueeze(0).repeat(batch_size, 1) |
| 609 | return position_ids |
| 610 | |
| 611 | def _set_gradient_checkpointing(self, module, value=False): |
| 612 | if isinstance(module, GLMTransformer): |
| 613 | module.gradient_checkpointing = value |
| 614 | |
| 615 | |
| 616 | class Embedding(torch.nn.Module): |
nothing calls this directly
no outgoing calls
no test coverage detected