|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 3 | +# Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team. |
| 4 | +# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +""" XLM-RoBERTa configuration""" |
| 18 | + |
| 19 | +from ..model_utils import PretrainedConfig |
| 20 | + |
| 21 | +__all__ = ["PRETRAINED_INIT_CONFIGURATION", "XLMRobertaConfig"] |
| 22 | + |
| 23 | +PRETRAINED_INIT_CONFIGURATION = { |
| 24 | + "hf-internal-testing/tiny-random-onnx-xlm-roberta": { |
| 25 | + "attention_probs_dropout_prob": 0.1, |
| 26 | + "bos_token_id": 0, |
| 27 | + "classifier_dropout": None, |
| 28 | + "eos_token_id": 2, |
| 29 | + "hidden_act": "gelu", |
| 30 | + "hidden_dropout_prob": 0.1, |
| 31 | + "hidden_size": 4, |
| 32 | + "initializer_range": 0.02, |
| 33 | + "intermediate_size": 37, |
| 34 | + "layer_norm_eps": 1e-05, |
| 35 | + "max_position_embeddings": 514, |
| 36 | + "model_type": "xlm-roberta", |
| 37 | + "num_attention_heads": 4, |
| 38 | + "num_hidden_layers": 5, |
| 39 | + "output_past": True, |
| 40 | + "pad_token_id": 1, |
| 41 | + "position_embedding_type": "absolute", |
| 42 | + "dtype": "float32", |
| 43 | + "type_vocab_size": 1, |
| 44 | + "use_cache": True, |
| 45 | + "vocab_size": 250002, |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +class XLMRobertaConfig(PretrainedConfig): |
| 51 | + r""" |
| 52 | + This is the configuration class to store the configuration of a [`XLMRobertaModel`] or a [`TFXLMRobertaModel`]. It |
| 53 | + is used to instantiate a XLM-RoBERTa model according to the specified arguments, defining the model architecture. |
| 54 | + Instantiating a configuration with the defaults will yield a similar configuration to that of the XLMRoBERTa |
| 55 | + [xlm-roberta-base](https://huggingface.co/xlm-roberta-base) architecture. |
| 56 | +
|
| 57 | + Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the |
| 58 | + documentation from [`PretrainedConfig`] for more information. |
| 59 | +
|
| 60 | +
|
| 61 | + Args: |
| 62 | + vocab_size (`int`, *optional*, defaults to 30522): |
| 63 | + Vocabulary size of the XLM-RoBERTa model. Defines the number of different tokens that can be represented by |
| 64 | + the `inputs_ids` passed when calling [`XLMRobertaModel`] or [`TFXLMRobertaModel`]. |
| 65 | + hidden_size (`int`, *optional*, defaults to 768): |
| 66 | + Dimensionality of the encoder layers and the pooler layer. |
| 67 | + num_hidden_layers (`int`, *optional*, defaults to 12): |
| 68 | + Number of hidden layers in the Transformer encoder. |
| 69 | + num_attention_heads (`int`, *optional*, defaults to 12): |
| 70 | + Number of attention heads for each attention layer in the Transformer encoder. |
| 71 | + intermediate_size (`int`, *optional*, defaults to 3072): |
| 72 | + Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder. |
| 73 | + hidden_act (`str` or `Callable`, *optional*, defaults to `"gelu"`): |
| 74 | + The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`, |
| 75 | + `"relu"`, `"silu"` and `"gelu_new"` are supported. |
| 76 | + hidden_dropout_prob (`float`, *optional*, defaults to 0.1): |
| 77 | + The dropout probability for all fully connected layers in the embeddings, encoder, and pooler. |
| 78 | + attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1): |
| 79 | + The dropout ratio for the attention probabilities. |
| 80 | + max_position_embeddings (`int`, *optional*, defaults to 512): |
| 81 | + The maximum sequence length that this model might ever be used with. Typically set this to something large |
| 82 | + just in case (e.g., 512 or 1024 or 2048). |
| 83 | + type_vocab_size (`int`, *optional*, defaults to 2): |
| 84 | + The vocabulary size of the `token_type_ids` passed when calling [`XLMRobertaModel`] or |
| 85 | + [`TFXLMRobertaModel`]. |
| 86 | + initializer_range (`float`, *optional*, defaults to 0.02): |
| 87 | + The standard deviation of the truncated_normal_initializer for initializing all weight matrices. |
| 88 | + layer_norm_eps (`float`, *optional*, defaults to 1e-12): |
| 89 | + The epsilon used by the layer normalization layers. |
| 90 | + position_embedding_type (`str`, *optional*, defaults to `"absolute"`): |
| 91 | + Type of position embedding. Choose one of `"absolute"`, `"relative_key"`, `"relative_key_query"`. For |
| 92 | + positional embeddings use `"absolute"`. For more information on `"relative_key"`, please refer to |
| 93 | + [Self-Attention with Relative Position Representations (Shaw et al.)](https://arxiv.org/abs/1803.02155). |
| 94 | + For more information on `"relative_key_query"`, please refer to *Method 4* in [Improve Transformer Models |
| 95 | + with Better Relative Position Embeddings (Huang et al.)](https://arxiv.org/abs/2009.13658). |
| 96 | + is_decoder (`bool`, *optional*, defaults to `False`): |
| 97 | + Whether the model is used as a decoder or not. If `False`, the model is used as an encoder. |
| 98 | + use_cache (`bool`, *optional*, defaults to `True`): |
| 99 | + Whether or not the model should return the last key/values attentions (not used by all models). Only |
| 100 | + relevant if `config.is_decoder=True`. |
| 101 | + classifier_dropout (`float`, *optional*): |
| 102 | + The dropout ratio for the classification head. |
| 103 | +
|
| 104 | + Examples: |
| 105 | +
|
| 106 | + ```python |
| 107 | + >>> from paddlenlp.transformers import XLMRobertaConfig, XLMRobertaModel |
| 108 | +
|
| 109 | + >>> # Initializing a XLM-RoBERTa xlm-roberta-base style configuration |
| 110 | + >>> configuration = XLMRobertaConfig() |
| 111 | +
|
| 112 | + >>> # Initializing a model (with random weights) from the xlm-roberta-base style configuration |
| 113 | + >>> model = XLMRobertaModel(configuration) |
| 114 | +
|
| 115 | + >>> # Accessing the model configuration |
| 116 | + >>> configuration = model.config |
| 117 | + ```""" |
| 118 | + |
| 119 | + model_type = "xlm-roberta" |
| 120 | + |
| 121 | + def __init__( |
| 122 | + self, |
| 123 | + vocab_size=30522, |
| 124 | + hidden_size=768, |
| 125 | + num_hidden_layers=12, |
| 126 | + num_attention_heads=12, |
| 127 | + intermediate_size=3072, |
| 128 | + hidden_act="gelu", |
| 129 | + hidden_dropout_prob=0.1, |
| 130 | + attention_probs_dropout_prob=0.1, |
| 131 | + max_position_embeddings=512, |
| 132 | + type_vocab_size=2, |
| 133 | + initializer_range=0.02, |
| 134 | + layer_norm_eps=1e-12, |
| 135 | + pad_token_id=1, |
| 136 | + bos_token_id=0, |
| 137 | + eos_token_id=2, |
| 138 | + position_embedding_type="absolute", |
| 139 | + use_cache=True, |
| 140 | + classifier_dropout=None, |
| 141 | + **kwargs, |
| 142 | + ): |
| 143 | + kwargs["return_dict"] = kwargs.pop("return_dict", False) |
| 144 | + super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs) |
| 145 | + |
| 146 | + self.vocab_size = vocab_size |
| 147 | + self.hidden_size = hidden_size |
| 148 | + self.num_hidden_layers = num_hidden_layers |
| 149 | + self.num_attention_heads = num_attention_heads |
| 150 | + self.hidden_act = hidden_act |
| 151 | + self.intermediate_size = intermediate_size |
| 152 | + self.hidden_dropout_prob = hidden_dropout_prob |
| 153 | + self.attention_probs_dropout_prob = attention_probs_dropout_prob |
| 154 | + self.max_position_embeddings = max_position_embeddings |
| 155 | + self.type_vocab_size = type_vocab_size |
| 156 | + self.initializer_range = initializer_range |
| 157 | + self.layer_norm_eps = layer_norm_eps |
| 158 | + self.position_embedding_type = position_embedding_type |
| 159 | + self.use_cache = use_cache |
| 160 | + self.classifier_dropout = classifier_dropout |
0 commit comments