Source code for paddlenlp.transformers.ppminilm.modeling

# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import paddle
import paddle.nn as nn

from ...utils.env import CONFIG_NAME
from .. import PretrainedModel, register_base_model
from .configuration import (
    PPMINILM_PRETRAINED_INIT_CONFIGURATION,
    PPMINILM_PRETRAINED_RESOURCE_FILES_MAP,
    PPMiniLMConfig,
)

__all__ = [
    "PPMiniLMModel",
    "PPMiniLMPretrainedModel",
    "PPMiniLMForSequenceClassification",
    "PPMiniLMForQuestionAnswering",
    "PPMiniLMForMultipleChoice",
]


class PPMiniLMEmbeddings(nn.Layer):
    r"""
    Include embeddings from word, position and token_type embeddings.
    """

    def __init__(self, config: PPMiniLMConfig):
        super(PPMiniLMEmbeddings, self).__init__()

        self.word_embeddings = nn.Embedding(config.vocab_size, config.hidden_size, padding_idx=config.pad_token_id)
        self.position_embeddings = nn.Embedding(config.max_position_embeddings, config.hidden_size)
        self.token_type_embeddings = nn.Embedding(config.type_vocab_size, config.hidden_size)
        self.layer_norm = nn.LayerNorm(config.hidden_size)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)

    def forward(self, input_ids, token_type_ids=None, position_ids=None):
        if position_ids is None:
            # maybe need use shape op to unify static graph and dynamic graph
            # seq_length = input_ids.shape[1]
            ones = paddle.ones_like(input_ids, dtype="int64")
            seq_length = paddle.cumsum(ones, axis=1)
            position_ids = seq_length - ones
            position_ids.stop_gradient = True
        if token_type_ids is None:
            token_type_ids = paddle.zeros_like(input_ids, dtype="int64")
        input_embedings = self.word_embeddings(input_ids)
        position_embeddings = self.position_embeddings(position_ids)
        token_type_embeddings = self.token_type_embeddings(token_type_ids)
        embeddings = input_embedings + position_embeddings + token_type_embeddings
        embeddings = self.layer_norm(embeddings)
        embeddings = self.dropout(embeddings)
        return embeddings


class PPMiniLMPooler(nn.Layer):
    def __init__(self, config: PPMiniLMConfig):
        super(PPMiniLMPooler, self).__init__()
        self.dense = nn.Linear(config.hidden_size, config.hidden_size)
        self.activation = nn.Tanh()

    def forward(self, hidden_states):
        # We "pool" the model by simply taking the hidden state corresponding
        # to the first token.
        first_token_tensor = hidden_states[:, 0]
        pooled_output = self.dense(first_token_tensor)
        pooled_output = self.activation(pooled_output)
        return pooled_output


[docs]class PPMiniLMPretrainedModel(PretrainedModel): r""" An abstract class for pretrained PPMiniLM models. It provides PPMiniLM related `model_config_file`, `pretrained_init_configuration`, `resource_files_names`, `pretrained_resource_files_map`, `base_model_prefix` for downloading and loading pretrained models. Refer to :class:`~paddlenlp.transformers.model_utils.PretrainedModel` for more details. """ model_config_file = CONFIG_NAME config_class = PPMiniLMConfig resource_files_names = {"model_state": "model_state.pdparams"} base_model_prefix = "ppminilm" pretrained_init_configuration = PPMINILM_PRETRAINED_INIT_CONFIGURATION pretrained_resource_files_map = PPMINILM_PRETRAINED_RESOURCE_FILES_MAP def _init_weights(self, layer): """Initialization hook""" if isinstance(layer, (nn.Linear, nn.Embedding)): # only support dygraph, use truncated_normal and make it inplace # and configurable later if isinstance(layer.weight, paddle.Tensor): layer.weight.set_value( paddle.tensor.normal( mean=0.0, std=self.config.initializer_range, shape=layer.weight.shape, ) ) elif isinstance(layer, nn.LayerNorm): layer._epsilon = self.config.layer_norm_eps
[docs]@register_base_model class PPMiniLMModel(PPMiniLMPretrainedModel): r""" The bare PPMiniLM Model transformer outputting raw hidden-states. This model inherits from :class:`~paddlenlp.transformers.model_utils.PretrainedModel`. Refer to the superclass documentation for the generic methods. This model is also a Paddle `paddle.nn.Layer <https://www.paddlepaddle.org.cn/documentation /docs/zh/api/paddle/nn/Layer_cn.html>`__ subclass. Use it as a regular Paddle Layer and refer to the Paddle documentation for all matter related to general usage and behavior. Args: config (:class:`PPMiniLMConfig`): An instance of PPMiniLMConfig used to construct PPMiniLMModel. """ def __init__(self, config: PPMiniLMConfig): super(PPMiniLMModel, self).__init__(config) self.pad_token_id = config.pad_token_id self.embeddings = PPMiniLMEmbeddings(config) encoder_layer = nn.TransformerEncoderLayer( config.hidden_size, config.num_attention_heads, config.intermediate_size, dropout=config.hidden_dropout_prob, activation=config.hidden_act, attn_dropout=config.attention_probs_dropout_prob, act_dropout=0.0, ) self.encoder = nn.TransformerEncoder(encoder_layer, config.num_hidden_layers) self.pooler = PPMiniLMPooler(config)
[docs] def get_input_embeddings(self): return self.embeddings.word_embeddings
[docs] def set_input_embeddings(self, value): self.embeddings.word_embeddings = value
[docs] def forward(self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None): r""" Args: input_ids (Tensor): If `input_ids` is a Tensor object, it is an indices of input sequence tokens in the vocabulary. They are numerical representations of tokens that build the input sequence. It's data type should be `int64` and has a shape of [batch_size, sequence_length]. token_type_ids (Tensor, string, optional): If `token_type_ids` is a Tensor object: Segment token indices to indicate different portions of the inputs. Selected in the range ``[0, type_vocab_size - 1]``. If `type_vocab_size` is 2, which means the inputs have two portions. Indices can either be 0 or 1: - 0 corresponds to a *sentence A* token, - 1 corresponds to a *sentence B* token. Its data type should be `int64` and it has a shape of [batch_size, sequence_length]. Defaults to `None`, which means we don't add segment embeddings. position_ids (Tensor, optional): Indices of positions of each input sequence tokens in the position embeddings. Selected in the range ``[0, max_position_embeddings - 1]``. Shape as `[batch_size, num_tokens]` and dtype as int64. Defaults to `None`. attention_mask (Tensor, optional): Mask used in multi-head attention to avoid performing attention on to some unwanted positions, usually the paddings or the subsequent positions. Its data type can be int, float and bool. When the data type is bool, the `masked` tokens have `False` values and the others have `True` values. When the data type is int, the `masked` tokens have `0` values and the others have `1` values. When the data type is float, the `masked` tokens have `-INF` values and the others have `0` values. It is a tensor with shape broadcasted to `[batch_size, num_attention_heads, sequence_length, sequence_length]`. For example, its shape can be [batch_size, sequence_length], [batch_size, sequence_length, sequence_length], [batch_size, num_attention_heads, sequence_length, sequence_length]. We use whole-word-mask in PPMiniLM, so the whole word will have the same value. For example, "使用" as a word, "使" and "用" will have the same value. Defaults to `None`, which means nothing needed to be prevented attention to. Returns: tuple: Returns tuple (``sequence_output``, ``pooled_output``). With the fields: - `sequence_output` (Tensor): Sequence of hidden-states at the last layer of the model. It's data type should be float32 and its shape is [batch_size, sequence_length, hidden_size]. - `pooled_output` (Tensor): The output of first token (`[CLS]`) in sequence. We "pool" the model by simply taking the hidden state corresponding to the first token. Its data type should be float32 and its shape is [batch_size, hidden_size]. Example: .. code-block:: import paddle from paddlenlp.transformers import PPMiniLMModel, PPMiniLMTokenizer tokenizer = PPMiniLMTokenizer.from_pretrained('ppminilm-6l-768h') model = PPMiniLMModel.from_pretrained('ppminilm-6l-768h') inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!") inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()} sequence_output, pooled_output = model(**inputs) """ if attention_mask is None: attention_mask = paddle.unsqueeze( (input_ids == self.pad_token_id).astype(self.pooler.dense.weight.dtype) * -1e4, axis=[1, 2] ) else: if attention_mask.ndim == 2: # attention_mask [batch_size, sequence_length] -> [batch_size, 1, 1, sequence_length] attention_mask = attention_mask.unsqueeze(axis=[1, 2]).astype(paddle.get_default_dtype()) attention_mask = (1.0 - attention_mask) * -1e4 embedding_output = self.embeddings( input_ids=input_ids, position_ids=position_ids, token_type_ids=token_type_ids ) encoder_outputs = self.encoder(embedding_output, attention_mask) sequence_output = encoder_outputs pooled_output = self.pooler(sequence_output) return sequence_output, pooled_output
[docs]class PPMiniLMForSequenceClassification(PPMiniLMPretrainedModel): r""" PPMiniLM Model with a linear layer on top of the output layer, designed for sequence classification/regression tasks like GLUE tasks. Args: ppminilm (PPMiniLMModel): An instance of `paddlenlp.transformers.PPMiniLMModel`. num_classes (int, optional): The number of classes. Default to `2`. dropout (float, optional): The dropout probability for output of PPMiniLM. If None, use the same value as `hidden_dropout_prob` of `paddlenlp.transformers.PPMiniLMModel` instance. Defaults to `None`. """ def __init__(self, config: PPMiniLMConfig): super(PPMiniLMForSequenceClassification, self).__init__(config) self.ppminilm = PPMiniLMModel(config) self.num_labels = config.num_labels self.dropout = nn.Dropout( config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob ) self.classifier = nn.Linear(config.hidden_size, config.num_labels)
[docs] def forward(self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None): r""" Args: input_ids (Tensor): See :class:`PPMiniLMModel`. token_type_ids (Tensor, optional): See :class:`PPMiniLMModel`. position_ids (Tensor, optional): See :class:`PPMiniLMModel`. attention_mask (Tensor, optional): See :class:`MiniLMModel`. Returns: Tensor: Returns tensor `logits`, a tensor of the input text classification logits. Shape as `[batch_size, num_classes]` and dtype as float32. Example: .. code-block:: import paddle from paddlenlp.transformers import PPMiniLMForSequenceClassification, PPMiniLMTokenizer tokenizer = PPMiniLMTokenizer.from_pretrained('ppminilm-6l-768h') model = PPMiniLMForSequenceClassification.from_pretrained('ppminilm-6l-768h0') inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!") inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()} logits = model(**inputs) """ _, pooled_output = self.ppminilm( input_ids, token_type_ids=token_type_ids, position_ids=position_ids, attention_mask=attention_mask ) pooled_output = self.dropout(pooled_output) logits = self.classifier(pooled_output) return logits
[docs]class PPMiniLMForQuestionAnswering(PPMiniLMPretrainedModel): """ PPMiniLM Model with a linear layer on top of the hidden-states output to compute `span_start_logits` and `span_end_logits`, designed for question-answering tasks like SQuAD. Args: ppminilm (`PPMiniLMModel`): An instance of `PPMiniLMModel`. """ def __init__(self, config: PPMiniLMConfig): super(PPMiniLMForQuestionAnswering, self).__init__(config) self.ppminilm = PPMiniLMModel(config) self.dropout = nn.Dropout( config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob ) self.classifier = nn.Linear(config.hidden_size, 2)
[docs] def forward(self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None): r""" Args: input_ids (Tensor): See :class:`PPMiniLMModel`. token_type_ids (Tensor, optional): See :class:`PPMiniLMModel`. position_ids (Tensor, optional): See :class:`PPMiniLMModel`. attention_mask (Tensor, optional): See :class:`PPMiniLMModel`. Returns: tuple: Returns tuple (`start_logits`, `end_logits`). With the fields: - `start_logits` (Tensor): A tensor of the input token classification logits, indicates the start position of the labelled span. Its data type should be float32 and its shape is [batch_size, sequence_length]. - `end_logits` (Tensor): A tensor of the input token classification logits, indicates the end position of the labelled span. Its data type should be float32 and its shape is [batch_size, sequence_length]. Example: .. code-block:: import paddle from paddlenlp.transformers import PPMiniLMForQuestionAnswering, PPMiniLMTokenizer tokenizer = PPMiniLMTokenizer.from_pretrained('ppminilm-6l-768h') model = PPMiniLMForQuestionAnswering.from_pretrained('ppminilm-6l-768h') inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!") inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()} logits = model(**inputs) """ sequence_output, _ = self.ppminilm( input_ids, token_type_ids=token_type_ids, position_ids=position_ids, attention_mask=attention_mask ) logits = self.classifier(sequence_output) logits = paddle.transpose(logits, perm=[2, 0, 1]) start_logits, end_logits = paddle.unstack(x=logits, axis=0) return start_logits, end_logits
[docs]class PPMiniLMForMultipleChoice(PPMiniLMPretrainedModel): """ PPMiniLM Model with a linear layer on top of the hidden-states output layer, designed for multiple choice tasks like RocStories/SWAG tasks. Args: ppminilm (:class:`PPMiniLMModel`): An instance of PPMiniLMModel. num_choices (int, optional): The number of choices. Defaults to `2`. dropout (float, optional): The dropout probability for output of PPMiniLM. If None, use the same value as `hidden_dropout_prob` of `PPMiniLMModel` instance `ppminilm`. Defaults to None. """ def __init__(self, config: PPMiniLMConfig): super(PPMiniLMForMultipleChoice, self).__init__(config) self.num_choices = config.num_choices self.ppminilm = PPMiniLMModel(config) self.dropout = nn.Dropout( config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob ) self.classifier = nn.Linear(config.hidden_size, 1)
[docs] def forward(self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None): r""" The PPMiniLMForMultipleChoice forward method, overrides the __call__() special method. Args: input_ids (Tensor): See :class:`PPMiniLMModel` and shape as [batch_size, num_choice, sequence_length]. token_type_ids(Tensor, optional): See :class:`PPMiniLMModel` and shape as [batch_size, num_choice, sequence_length]. position_ids(Tensor, optional): See :class:`PPMiniLMModel` and shape as [batch_size, num_choice, sequence_length]. attention_mask (list, optional): See :class:`PPMiniLMModel` and shape as [batch_size, num_choice, sequence_length]. Returns: Tensor: Returns tensor `reshaped_logits`, a tensor of the multiple choice classification logits. Shape as `[batch_size, num_choice]` and dtype as `float32`. """ # input_ids: [bs, num_choice, seq_l] input_ids = input_ids.reshape(shape=(-1, input_ids.shape[-1])) # flat_input_ids: [bs*num_choice,seq_l] if position_ids is not None: position_ids = position_ids.reshape(shape=(-1, position_ids.shape[-1])) if token_type_ids is not None: token_type_ids = token_type_ids.reshape(shape=(-1, token_type_ids.shape[-1])) if attention_mask is not None: attention_mask = attention_mask.reshape(shape=(-1, attention_mask.shape[-1])) _, pooled_output = self.ppminilm( input_ids, token_type_ids=token_type_ids, position_ids=position_ids, attention_mask=attention_mask ) pooled_output = self.dropout(pooled_output) logits = self.classifier(pooled_output) # logits: (bs*num_choice,1) reshaped_logits = logits.reshape(shape=(-1, self.num_choices)) # logits: (bs, num_choice) return reshaped_logits