CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
configuration_gptj.py221 linesDownload Raw Back to gptj
1# coding=utf-82# Copyright 2021 The EleutherAI and HuggingFace Teams. All rights reserved.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8#     http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15"""GPT-J model configuration"""16 17from collections import OrderedDict18from collections.abc import Mapping19from typing import Any, Optional20 21from ... import PreTrainedTokenizer, TensorType, is_torch_available22from ...configuration_utils import PretrainedConfig23from ...onnx import OnnxConfigWithPast, PatchingSpec24from ...utils import logging25 26 27logger = logging.get_logger(__name__)28 29 30class GPTJConfig(PretrainedConfig):31    r"""32    This is the configuration class to store the configuration of a [`GPTJModel`]. It is used to instantiate a GPT-J33    model according to the specified arguments, defining the model architecture. Instantiating a configuration with the34    defaults will yield a similar configuration to that of the GPT-J35    [EleutherAI/gpt-j-6B](https://huggingface.co/EleutherAI/gpt-j-6B) architecture. Configuration objects inherit from36    [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`]37    for more information.38 39    Args:40        vocab_size (`int`, *optional*, defaults to 50400):41            Vocabulary size of the GPT-J model. Defines the number of different tokens that can be represented by the42            `inputs_ids` passed when calling [`GPTJModel`].43        n_positions (`int`, *optional*, defaults to 2048):44            The maximum sequence length that this model might ever be used with. Typically set this to something large45            just in case (e.g., 512 or 1024 or 2048).46        n_embd (`int`, *optional*, defaults to 4096):47            Dimensionality of the embeddings and hidden states.48        n_layer (`int`, *optional*, defaults to 28):49            Number of hidden layers in the Transformer encoder.50        n_head (`int`, *optional*, defaults to 16):51            Number of attention heads for each attention layer in the Transformer encoder.52        rotary_dim (`int`, *optional*, defaults to 64):53            Number of dimensions in the embedding that Rotary Position Embedding is applied to.54        n_inner (`int`, *optional*, defaults to None):55            Dimensionality of the inner feed-forward layers. `None` will set it to 4 times n_embd56        activation_function (`str`, *optional*, defaults to `"gelu_new"`):57            Activation function, to be selected in the list `["relu", "silu", "gelu", "tanh", "gelu_new"]`.58        resid_pdrop (`float`, *optional*, defaults to 0.1):59            The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.60        embd_pdrop (`int`, *optional*, defaults to 0.1):61            The dropout ratio for the embeddings.62        attn_pdrop (`float`, *optional*, defaults to 0.1):63            The dropout ratio for the attention.64        layer_norm_epsilon (`float`, *optional*, defaults to 1e-5):65            The epsilon to use in the layer normalization layers.66        initializer_range (`float`, *optional*, defaults to 0.02):67            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.68        use_cache (`bool`, *optional*, defaults to `True`):69            Whether or not the model should return the last key/values attentions (not used by all models).70 71    Example:72 73    ```python74    >>> from transformers import GPTJModel, GPTJConfig75 76    >>> # Initializing a GPT-J 6B configuration77    >>> configuration = GPTJConfig()78 79    >>> # Initializing a model from the configuration80    >>> model = GPTJModel(configuration)81 82    >>> # Accessing the model configuration83    >>> configuration = model.config84    ```"""85 86    model_type = "gptj"87    attribute_map = {88        "max_position_embeddings": "n_positions",89        "hidden_size": "n_embd",90        "num_attention_heads": "n_head",91        "num_hidden_layers": "n_layer",92    }93 94    def __init__(95        self,96        vocab_size=50400,97        n_positions=2048,98        n_embd=4096,99        n_layer=28,100        n_head=16,101        rotary_dim=64,102        n_inner=None,103        activation_function="gelu_new",104        resid_pdrop=0.0,105        embd_pdrop=0.0,106        attn_pdrop=0.0,107        layer_norm_epsilon=1e-5,108        initializer_range=0.02,109        use_cache=True,110        bos_token_id=50256,111        eos_token_id=50256,112        tie_word_embeddings=False,113        **kwargs,114    ):115        self.vocab_size = vocab_size116        self.n_positions = n_positions117        self.n_embd = n_embd118        self.n_layer = n_layer119        self.n_head = n_head120        self.n_inner = n_inner121        self.rotary_dim = rotary_dim122        self.activation_function = activation_function123        self.resid_pdrop = resid_pdrop124        self.embd_pdrop = embd_pdrop125        self.attn_pdrop = attn_pdrop126        self.layer_norm_epsilon = layer_norm_epsilon127        self.initializer_range = initializer_range128        self.use_cache = use_cache129 130        self.bos_token_id = bos_token_id131        self.eos_token_id = eos_token_id132 133        super().__init__(134            bos_token_id=bos_token_id, eos_token_id=eos_token_id, tie_word_embeddings=tie_word_embeddings, **kwargs135        )136 137 138# Copied from transformers.models.gpt2.configuration_gpt2.GPT2OnnxConfig139class GPTJOnnxConfig(OnnxConfigWithPast):140    def __init__(141        self,142        config: PretrainedConfig,143        task: str = "default",144        patching_specs: Optional[list[PatchingSpec]] = None,145        use_past: bool = False,146    ):147        super().__init__(config, task=task, patching_specs=patching_specs, use_past=use_past)148        if not getattr(self._config, "pad_token_id", None):149            # TODO: how to do that better?150            self._config.pad_token_id = 0151 152    @property153    def inputs(self) -> Mapping[str, Mapping[int, str]]:154        common_inputs = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}})155        if self.use_past:156            self.fill_with_past_key_values_(common_inputs, direction="inputs")157            common_inputs["attention_mask"] = {0: "batch", 1: "past_sequence + sequence"}158        else:159            common_inputs["attention_mask"] = {0: "batch", 1: "sequence"}160 161        return common_inputs162 163    @property164    def num_layers(self) -> int:165        return self._config.n_layer166 167    @property168    def num_attention_heads(self) -> int:169        return self._config.n_head170 171    def generate_dummy_inputs(172        self,173        tokenizer: PreTrainedTokenizer,174        batch_size: int = -1,175        seq_length: int = -1,176        is_pair: bool = False,177        framework: Optional[TensorType] = None,178    ) -> Mapping[str, Any]:179        common_inputs = super(OnnxConfigWithPast, self).generate_dummy_inputs(180            tokenizer, batch_size=batch_size, seq_length=seq_length, is_pair=is_pair, framework=framework181        )182 183        # We need to order the input in the way they appears in the forward()184        ordered_inputs = OrderedDict({"input_ids": common_inputs["input_ids"]})185 186        # Need to add the past_keys187        if self.use_past:188            if not is_torch_available():189                raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed.")190            else:191                import torch192 193                batch, seqlen = common_inputs["input_ids"].shape194                # Not using the same length for past_key_values195                past_key_values_length = seqlen + 2196                past_shape = (197                    batch,198                    self.num_attention_heads,199                    past_key_values_length,200                    self._config.hidden_size // self.num_attention_heads,201                )202                ordered_inputs["past_key_values"] = [203                    (torch.zeros(past_shape), torch.zeros(past_shape)) for _ in range(self.num_layers)204                ]205 206        ordered_inputs["attention_mask"] = common_inputs["attention_mask"]207        if self.use_past:208            mask_dtype = ordered_inputs["attention_mask"].dtype209            ordered_inputs["attention_mask"] = torch.cat(210                [ordered_inputs["attention_mask"], torch.ones(batch, past_key_values_length, dtype=mask_dtype)], dim=1211            )212 213        return ordered_inputs214 215    @property216    def default_onnx_opset(self) -> int:217        return 13218 219 220__all__ = ["GPTJConfig", "GPTJOnnxConfig"]221 
Aluode/PerceptionLabPortable · CoolFace