CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
configuration_ernie.py164 linesDownload Raw Back to ernie
1# coding=utf-82# Copyright 2022 The Google AI Language Team Authors and The HuggingFace Inc. team.3# Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.4#5# Licensed under the Apache License, Version 2.0 (the "License");6# you may not use this file except in compliance with the License.7# You may obtain a copy of the License at8#9#     http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16"""ERNIE model configuration"""17 18from collections import OrderedDict19from collections.abc import Mapping20 21from ...configuration_utils import PretrainedConfig22from ...onnx import OnnxConfig23from ...utils import logging24 25 26logger = logging.get_logger(__name__)27 28 29class ErnieConfig(PretrainedConfig):30    r"""31    This is the configuration class to store the configuration of a [`ErnieModel`] or a [`TFErnieModel`]. It is used to32    instantiate a ERNIE model according to the specified arguments, defining the model architecture. Instantiating a33    configuration with the defaults will yield a similar configuration to that of the ERNIE34    [nghuyong/ernie-3.0-base-zh](https://huggingface.co/nghuyong/ernie-3.0-base-zh) architecture.35 36    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the37    documentation from [`PretrainedConfig`] for more information.38 39 40    Args:41        vocab_size (`int`, *optional*, defaults to 30522):42            Vocabulary size of the ERNIE model. Defines the number of different tokens that can be represented by the43            `inputs_ids` passed when calling [`ErnieModel`] or [`TFErnieModel`].44        hidden_size (`int`, *optional*, defaults to 768):45            Dimensionality of the encoder layers and the pooler layer.46        num_hidden_layers (`int`, *optional*, defaults to 12):47            Number of hidden layers in the Transformer encoder.48        num_attention_heads (`int`, *optional*, defaults to 12):49            Number of attention heads for each attention layer in the Transformer encoder.50        intermediate_size (`int`, *optional*, defaults to 3072):51            Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.52        hidden_act (`str` or `Callable`, *optional*, defaults to `"gelu"`):53            The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,54            `"relu"`, `"silu"` and `"gelu_new"` are supported.55        hidden_dropout_prob (`float`, *optional*, defaults to 0.1):56            The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.57        attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):58            The dropout ratio for the attention probabilities.59        max_position_embeddings (`int`, *optional*, defaults to 512):60            The maximum sequence length that this model might ever be used with. Typically set this to something large61            just in case (e.g., 512 or 1024 or 2048).62        type_vocab_size (`int`, *optional*, defaults to 2):63            The vocabulary size of the `token_type_ids` passed when calling [`ErnieModel`] or [`TFErnieModel`].64        task_type_vocab_size (`int`, *optional*, defaults to 3):65            The vocabulary size of the `task_type_ids` for ERNIE2.0/ERNIE3.0 model66        use_task_id (`bool`, *optional*, defaults to `False`):67            Whether or not the model support `task_type_ids`68        initializer_range (`float`, *optional*, defaults to 0.02):69            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.70        layer_norm_eps (`float`, *optional*, defaults to 1e-12):71            The epsilon used by the layer normalization layers.72        pad_token_id (`int`, *optional*, defaults to 0):73            Padding token id.74        position_embedding_type (`str`, *optional*, defaults to `"absolute"`):75            Type of position embedding. Choose one of `"absolute"`, `"relative_key"`, `"relative_key_query"`. For76            positional embeddings use `"absolute"`. For more information on `"relative_key"`, please refer to77            [Self-Attention with Relative Position Representations (Shaw et al.)](https://huggingface.co/papers/1803.02155).78            For more information on `"relative_key_query"`, please refer to *Method 4* in [Improve Transformer Models79            with Better Relative Position Embeddings (Huang et al.)](https://huggingface.co/papers/2009.13658).80        use_cache (`bool`, *optional*, defaults to `True`):81            Whether or not the model should return the last key/values attentions (not used by all models). Only82            relevant if `config.is_decoder=True`.83        classifier_dropout (`float`, *optional*):84            The dropout ratio for the classification head.85 86    Examples:87 88    ```python89    >>> from transformers import ErnieConfig, ErnieModel90 91    >>> # Initializing a ERNIE nghuyong/ernie-3.0-base-zh style configuration92    >>> configuration = ErnieConfig()93 94    >>> # Initializing a model (with random weights) from the nghuyong/ernie-3.0-base-zh style configuration95    >>> model = ErnieModel(configuration)96 97    >>> # Accessing the model configuration98    >>> configuration = model.config99    ```"""100 101    model_type = "ernie"102 103    def __init__(104        self,105        vocab_size=30522,106        hidden_size=768,107        num_hidden_layers=12,108        num_attention_heads=12,109        intermediate_size=3072,110        hidden_act="gelu",111        hidden_dropout_prob=0.1,112        attention_probs_dropout_prob=0.1,113        max_position_embeddings=512,114        type_vocab_size=2,115        task_type_vocab_size=3,116        use_task_id=False,117        initializer_range=0.02,118        layer_norm_eps=1e-12,119        pad_token_id=0,120        position_embedding_type="absolute",121        use_cache=True,122        classifier_dropout=None,123        **kwargs,124    ):125        super().__init__(pad_token_id=pad_token_id, **kwargs)126 127        self.vocab_size = vocab_size128        self.hidden_size = hidden_size129        self.num_hidden_layers = num_hidden_layers130        self.num_attention_heads = num_attention_heads131        self.hidden_act = hidden_act132        self.intermediate_size = intermediate_size133        self.hidden_dropout_prob = hidden_dropout_prob134        self.attention_probs_dropout_prob = attention_probs_dropout_prob135        self.max_position_embeddings = max_position_embeddings136        self.type_vocab_size = type_vocab_size137        self.task_type_vocab_size = task_type_vocab_size138        self.use_task_id = use_task_id139        self.initializer_range = initializer_range140        self.layer_norm_eps = layer_norm_eps141        self.position_embedding_type = position_embedding_type142        self.use_cache = use_cache143        self.classifier_dropout = classifier_dropout144 145 146class ErnieOnnxConfig(OnnxConfig):147    @property148    def inputs(self) -> Mapping[str, Mapping[int, str]]:149        if self.task == "multiple-choice":150            dynamic_axis = {0: "batch", 1: "choice", 2: "sequence"}151        else:152            dynamic_axis = {0: "batch", 1: "sequence"}153        return OrderedDict(154            [155                ("input_ids", dynamic_axis),156                ("attention_mask", dynamic_axis),157                ("token_type_ids", dynamic_axis),158                ("task_type_ids", dynamic_axis),159            ]160        )161 162 163__all__ = ["ErnieConfig", "ErnieOnnxConfig"]164 
Aluode/PerceptionLabPortable · CoolFace