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

Class ChatGLMPreTrainedModel

workers/chatglm3_modeling.py:661–702  ·  view source on GitHub ↗

An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.

Source from the content-addressed store, hash-verified

659
660
661class ChatGLMPreTrainedModel(PreTrainedModel):
662 """
663 An abstract class to handle weights initialization and
664 a simple interface for downloading and loading pretrained models.
665 """
666
667 is_parallelizable = False
668 supports_gradient_checkpointing = True
669 config_class = ChatGLMConfig
670 base_model_prefix = "transformer"
671 _no_split_modules = ["GLMBlock"]
672
673 def _init_weights(self, module: nn.Module):
674 """Initialize the weights."""
675 return
676
677 def get_masks(self, input_ids, past_key_values, padding_mask=None):
678 batch_size, seq_length = input_ids.shape
679 full_attention_mask = torch.ones(batch_size, seq_length, seq_length, device=input_ids.device)
680 full_attention_mask.tril_()
681 past_length = 0
682 if past_key_values:
683 past_length = past_key_values[0][0].shape[0]
684 if past_length:
685 full_attention_mask = torch.cat((torch.ones(batch_size, seq_length, past_length,
686 device=input_ids.device), full_attention_mask), dim=-1)
687 if padding_mask is not None:
688 full_attention_mask = full_attention_mask * padding_mask.unsqueeze(1)
689 if not past_length and padding_mask is not None:
690 full_attention_mask -= padding_mask.unsqueeze(-1) - 1
691 full_attention_mask = (full_attention_mask < 0.5).bool()
692 full_attention_mask.unsqueeze_(1)
693 return full_attention_mask
694
695 def get_position_ids(self, input_ids, device):
696 batch_size, seq_length = input_ids.shape
697 position_ids = torch.arange(seq_length, dtype=torch.long, device=device).unsqueeze(0).repeat(batch_size, 1)
698 return position_ids
699
700 def _set_gradient_checkpointing(self, module, value=False):
701 if isinstance(module, GLMTransformer):
702 module.gradient_checkpointing = value
703
704
705class Embedding(torch.nn.Module):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected