CPMANT
Last updated
Last updated
CPM-Ant is an open-source Chinese pre-trained language model (PLM) with 10B parameters. It is also the first milestone of the live training process of CPM-Live. The training process is cost-effective and environment-friendly. CPM-Ant also achieves promising results with delta tuning on the CUGE benchmark. Besides the full model, we also provide various compressed versions to meet the requirements of different hardware configurations.
Tips:
This model was contributed by . The original code can be found .
⚙️ Training & Inference
A tutorial on .
( vocab_size: int = 30720hidden_size: int = 4096num_attention_heads: int = 32dim_head: int = 128dim_ff: int = 10240num_hidden_layers: int = 48dropout_p: int = 0.0position_bias_num_buckets: int = 512position_bias_max_distance: int = 2048eps: int = 1e-06init_std: float = 1.0prompt_types: int = 32prompt_length: int = 32segment_types: int = 32use_cache: bool = True**kwargs )
Parameters
vocab_size (int
, optional, defaults to 30720) — Vocabulary size of the CPMAnt model. Defines the number of different tokens that can be represented by the input
passed when calling .
hidden_size (int
, optional, defaults to 4096) — Dimension of the encoder layers.
num_attention_heads (int
, optional, defaults to 32) — Number of attention heads in the Transformer encoder.
dim_head (int
, optional, defaults to 128) — Dimension of attention heads for each attention layer in the Transformer encoder.
dim_ff (int
, optional, defaults to 10240) — Dimension of the “intermediate” (i.e., feed-forward) layer in the Transformer encoder.
num_hidden_layers (int
, optional, defaults to 48) — Number of layers of the Transformer encoder.
dropout_p (float
, optional, defaults to 0.1) — The dropout probabilitiy for all fully connected layers in the embeddings, encoder.
position_bias_num_buckets (int
, optional, defaults to 512) — The number of position_bias buckets.
position_bias_max_distance (int
, optional, defaults to 2048) — The maximum sequence length that this model might ever be used with. Typically set this to something large just in case (e.g., 512 or 1024 or 2048).
eps (float
, optional, defaults to 1e-6) — The epsilon used by the layer normalization layers.
prompt_types (int
, optional, defaults to 32) — The type of prompt.
prompt_length (int
, optional, defaults to 32) — The length of prompt.
segment_types (int
, optional, defaults to 32) — The type of segment.
use_cache (bool
, optional, defaults to True
) — Whether to use cache.
init_std (float
, optional, defaults to 1.0) — Initialize parameters with std = init_std.
Example:
Copied
( vocab_filebod_token = '<d>'eod_token = '</d>'bos_token = '<s>'eos_token = '</s>'pad_token = '<pad>'unk_token = '<unk>'line_token = '</n>'space_token = '</_>'padding_side = 'left'**kwargs )
Parameters
vocab_file (str
) — Path to the vocabulary file.
bod_token (str
, optional, defaults to "<d>"
) — The beginning of document token.
eod_token (str
, optional, defaults to "</d>"
) — The end of document token.
bos_token (str
, optional, defaults to "<s>"
) — The beginning of sequence token.
eos_token (str
, optional, defaults to "</s>"
) — The end of sequence token.
pad_token (str
, optional, defaults to "<pad>"
) — The token used for padding.
unk_token (str
, optional, defaults to "<unk>"
) — The unknown token.
line_token (str
, optional, defaults to "</n>"
) — The line token.
space_token (str
, optional, defaults to "</_>"
) — The space token.
Construct a CPMAnt tokenizer. Based on byte-level Byte-Pair-Encoding.
build_inputs_with_special_tokens
( token_ids_0: typing.List[int]token_ids_1: typing.List[int] = None ) → List[int]
Parameters
token_ids_0 (List[int]
) — The first tokenized sequence that special tokens will be added.
token_ids_1 (List[int]
) — The optional second tokenized sequence that special tokens will be added.
Returns
List[int]
The model input with special tokens.
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A CPMAnt sequence has the following format:
single sequence: [BOS] Sequence
.
get_special_tokens_mask
( token_ids_0: typing.List[int]token_ids_1: typing.Optional[typing.List[int]] = Nonealready_has_special_tokens: bool = False ) → List[int]
Parameters
token_ids_0 (List[int]
) — List of IDs.
token_ids_1 (List[int]
, optional) — Optional second list of IDs for sequence pairs.
already_has_special_tokens (bool
, optional, defaults to False
) — Whether or not the token list is already formatted with special tokens for the model.
Returns
List[int]
A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer prepare_for_model
method.
( config: CpmAntConfig )
forward
Parameters
input_ids (torch.Tensor
of shape (batch_size, seq_len)
) — Indices of input sequence tokens in the vocabulary.
past_key_values (tuple(tuple(torch.FloatTensor))
, optional, returned when use_cache=True
is passed or when config.use_cache=True
) — Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention blocks) that can be used (see past_key_values
input) to speed up sequential decoding.
use_cache (bool
, optional) — If set to True
, past_key_values
key value states are returned and can be used to speed up decoding (see past_key_values
).
output_attentions (bool
, optional) — Whether or not to return the attentions tensors of all attention layers.
output_hidden_states (bool
, optional) — Whether or not to return the hidden states of all layers.
Returns
last_hidden_state (torch.FloatTensor
of shape (batch_size, sequence_length, hidden_size)
) — Sequence of hidden-states at the output of the last layer of the model.
If past_key_values
is used only the last hidden-state of the sequences of shape (batch_size, 1, hidden_size)
is output.
past_key_values (tuple(tuple(torch.FloatTensor))
, optional, returned when use_cache=True
is passed or when config.use_cache=True
) — Tuple of tuple(torch.FloatTensor)
of length config.n_layers
, with each tuple having 2 tensors of shape (batch_size, num_heads, sequence_length, embed_size_per_head)
) and optionally if config.is_encoder_decoder=True
2 additional tensors of shape (batch_size, num_heads, encoder_sequence_length, embed_size_per_head)
.
Contains pre-computed hidden-states (key and values in the self-attention blocks and optionally if config.is_encoder_decoder=True
in the cross-attention blocks) that can be used (see past_key_values
input) to speed up sequential decoding.
hidden_states (tuple(torch.FloatTensor)
, optional, returned when output_hidden_states=True
is passed or when config.output_hidden_states=True
) — Tuple of torch.FloatTensor
(one for the output of the embeddings, if the model has an embedding layer, + one for the output of each layer) of shape (batch_size, sequence_length, hidden_size)
.
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
attentions (tuple(torch.FloatTensor)
, optional, returned when output_attentions=True
is passed or when config.output_attentions=True
) — Tuple of torch.FloatTensor
(one for each layer) of shape (batch_size, num_heads, sequence_length, sequence_length)
.
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.
Example:
Copied
( config: CpmAntConfig )
The CPMAnt Model with a language modeling head on top (linear layer with weights tied to the input embeddings).
forward
Parameters
input_ids (torch.Tensor
of shape (batch_size, seq_len)
) — Indices of input sequence tokens in the vocabulary.
past_key_values (tuple(tuple(torch.FloatTensor))
, optional, returned when use_cache=True
is passed or when config.use_cache=True
) — Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention blocks) that can be used (see past_key_values
input) to speed up sequential decoding.
use_cache (bool
, optional) — If set to True
, past_key_values
key value states are returned and can be used to speed up decoding (see past_key_values
).
output_attentions (bool
, optional) — Whether or not to return the attentions tensors of all attention layers.
output_hidden_states (bool
, optional) — Whether or not to return the hidden states of all layers.
Args — input_ids (torch.Tensor
of shape (batch_size, seq_len)
): Indices of input sequence tokens in the vocabulary.
Example —
Text Generation with CpmAntForCausalLM. —
Returns
loss (torch.FloatTensor
of shape (1,)
, optional, returned when labels
is provided) — Language modeling loss (for next-token prediction).
logits (torch.FloatTensor
of shape (batch_size, sequence_length, config.vocab_size)
) — Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
past_key_values (tuple(tuple(torch.FloatTensor))
, optional, returned when use_cache=True
is passed or when config.use_cache=True
) — Tuple of tuple(torch.FloatTensor)
of length config.n_layers
, with each tuple having 2 tensors of shape (batch_size, num_heads, sequence_length, embed_size_per_head)
)
Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see past_key_values
input) to speed up sequential decoding.
hidden_states (tuple(torch.FloatTensor)
, optional, returned when output_hidden_states=True
is passed or when config.output_hidden_states=True
) — Tuple of torch.FloatTensor
(one for the output of the embeddings, if the model has an embedding layer, + one for the output of each layer) of shape (batch_size, sequence_length, hidden_size)
.
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
attentions (tuple(torch.FloatTensor)
, optional, returned when output_attentions=True
is passed or when config.output_attentions=True
) — Tuple of torch.FloatTensor
(one for each layer) of shape (batch_size, num_heads, sequence_length, sequence_length)
.
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.
Example:
Copied
This is the configuration class to store the configuration of a . It is used to instantiate an CPMAnt model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the CPMAnt architecture.
Configuration objects inherit from and can be used to control the model outputs. Read the documentation from for more information.
The bare CPMAnt Model outputting raw hidden-states without any specific head on top. This model is a PyTorch sub-class. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
Parameters config (): Model configuration class with all the parameters of the Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the method to load the model weights.
( input_ids: typing.Optional[torch.Tensor] = Noneoutput_attentions: typing.Optional[bool] = Noneoutput_hidden_states: typing.Optional[bool] = Nonepast_key_values: typing.Optional[typing.Tuple[typing.Tuple[torch.Tensor]]] = Noneuse_cache: typing.Optional[bool] = Nonereturn_dict: typing.Optional[bool] = None**kwargs ) → or tuple(torch.FloatTensor)
Indices can be obtained using CPMAntTokenizer
. See and for details.
return_dict (bool
, optional) — Whether or not to return a instead of a plain tuple.
or tuple(torch.FloatTensor)
A or a tuple of torch.FloatTensor
(if return_dict=False
is passed or when config.return_dict=False
) comprising various elements depending on the configuration () and inputs.
The forward method, overrides the __call__
special method.
This model is a PyTorch sub-class. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
Parameters config (): Model configuration class with all the parameters of the Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the method to load the model weights.
( input_ids: typing.Optional[torch.Tensor] = Nonepast_key_values: typing.Union[typing.List[typing.Tuple[torch.Tensor, torch.Tensor]], NoneType] = Noneuse_cache: typing.Optional[bool] = Noneoutput_attentions: typing.Optional[bool] = Noneoutput_hidden_states: typing.Optional[bool] = Nonelabels: typing.Optional[torch.Tensor] = Nonereturn_dict: typing.Optional[bool] = Noneattention_mask: typing.Optional[torch.Tensor] = None**kwargs ) → or tuple(torch.FloatTensor)
Indices can be obtained using CPMAntTokenizer
. See and for details.
return_dict (bool
, optional) — Whether or not to return a instead of a plain tuple.
Indices can be obtained using CPMAntTokenizer
. See and for details.
past_key_values (tuple(tuple(torch.FloatTensor))
, optional, returned when use_cache=True
is passed or when config.use_cache=True
): Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention blocks) that can be used (see past_key_values
input) to speed up sequential decoding. use_cache (bool
, optional): If set to True
, past_key_values
key value states are returned and can be used to speed up decoding (see past_key_values
). output_attentions (bool
, optional): Whether or not to return the attentions tensors of all attention layers. output_hidden_states (bool
, optional): Whether or not to return the hidden states of all layers. labels (torch.Tensor
of shape (batch_size, sequence_length)
, optional): Labels for computing the masked language modeling loss. return_dict (bool
, optional): Whether or not to return a instead of a plain tuple. attention_mask (torch.Tensor
of shape (batch_size, sequence_length)
, optional): CPMAnt will process attention mask automatically, this parameter is a dummy parameter for text-generation pipeline.
or tuple(torch.FloatTensor)
A or a tuple of torch.FloatTensor
(if return_dict=False
is passed or when config.return_dict=False
) comprising various elements depending on the configuration () and inputs.
The forward method, overrides the __call__
special method.