U
    9%e*                     @   s   d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
 erLddlmZmZ ddlmZ ddlmZmZ dd	lmZmZ eeZd
ddddddZG dd deZG dd deZdS )z Bloom configuration    )OrderedDict)TYPE_CHECKINGAnyListMappingOptional)version   )PreTrainedTokenizer
TensorType)PretrainedConfig)OnnxConfigWithPastPatchingSpec)is_torch_availableloggingz@https://huggingface.co/bigscience/bloom/resolve/main/config.jsonzBhttps://huggingface.co/bigscience/bloom-560m/blob/main/config.jsonzAhttps://huggingface.co/bigscience/bloom-1b1/blob/main/config.jsonzAhttps://huggingface.co/bigscience/bloom-1b7/blob/main/config.jsonz@https://huggingface.co/bigscience/bloom-3b/blob/main/config.jsonzAhttps://huggingface.co/bigscience/bloom-7b1/blob/main/config.json)zbigscience/bloomzbigscience/bloom-560mzbigscience/bloom-1b1zbigscience/bloom-1b7zbigscience/bloom-3bzbigscience/bloom-7b1c                       s6   e Zd ZdZdZdgZdddZd fdd	Z  ZS )BloomConfigaR  
    This is the configuration class to store the configuration of a [`BloomModel`]. It is used to instantiate a Bloom
    model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
    defaults will yield a similar configuration to the Bloom architecture
    [bigscience/bloom](https://huggingface.co/bigscience/bloom).

    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
    documentation from [`PretrainedConfig`] for more information.


    Args:
        vocab_size (`int`, *optional*, defaults to 250880):
            Vocabulary size of the Bloom model. Defines the maximum number of different tokens that can be represented
            by the `inputs_ids` passed when calling [`BloomModel`]. Check [this
            discussion](https://huggingface.co/bigscience/bloom/discussions/120#633d28389addb8530b406c2a) on how the
            `vocab_size` has been defined.
        hidden_size (`int`, *optional*, defaults to 64):
            Dimensionality of the embeddings and hidden states.
        n_layer (`int`, *optional*, defaults to 2):
            Number of hidden layers in the Transformer encoder.
        n_head (`int`, *optional*, defaults to 8):
            Number of attention heads for each attention layer in the Transformer encoder.
        layer_norm_epsilon (`float`, *optional*, defaults to 1e-5):
            The epsilon to use in the layer normalization layers.
        initializer_range (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        apply_residual_connection_post_layernorm (`bool`, *optional*, defaults to `False`):
            If enabled, use the layer norm of the hidden states as the residual in the transformer blocks
        hidden_dropout (`float`, *optional*, defaults to 0.1):
            Dropout rate of the dropout function on the bias dropout.
        attention_dropout (`float`, *optional*, defaults to 0.1):
            Dropout rate applied to the attention probs
        use_cache (`bool`, *optional*, defaults to `True`):
            Whether or not the model should return the last key/values attentions (not used by all models).
        pretraining_tp (`int`, *optional*, defaults to `1`):
            Experimental feature. Tensor parallelism rank used during pretraining with Megatron. Please refer to [this
            document](https://huggingface.co/docs/transformers/parallelism) to understand more about it. This value is
            necessary to ensure exact reproducibility of the pretraining results. Please refer to [this
            issue](https://github.com/pytorch/pytorch/issues/76232). Note also that this is enabled only when
            `slow_but_exact=True`.
        slow_but_exact (`bool`, *optional*, defaults to `False`):
            Experimental feature. Whether to use slow but exact implementation of the attention mechanism. While
            merging the TP rank tensors, due to slicing operations the results may be slightly different between the
            model trained on Megatron and our model. Please refer to [this
            issue](https://github.com/pytorch/pytorch/issues/76232). A solution to obtain more accurate results is to
            enable this feature. Enabling this will hurt the computational time of the inference. Will be probably
            resolved in the future once the main model has been fine-tuned with TP_rank=1.

    Example:

    ```python
    >>> from transformers import BloomConfig, BloomModel

    >>> # Initializing a Bloom configuration
    >>> configuration = BloomConfig()

    >>> # Initializing a model (with random weights) from the configuration
    >>> model = BloomModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```Zbloompast_key_valuesn_layern_head)Znum_hidden_layersnum_attention_heads  @         h㈵>{Gz?T   F        c                    s   || _ |dd }|d kr|n|| _|| _|| _|| _|| _|| _|| _|
| _	|| _
|| _|| _|	| _|| _t jf ||	d| d S )Nn_embed)bos_token_ideos_token_id)
vocab_sizepophidden_sizer   r   layer_norm_epsiloninitializer_range	use_cachepretraining_tp(apply_residual_connection_post_layernormhidden_dropoutattention_dropoutr   r    slow_but_exactsuper__init__)selfr!   r#   r   r   r$   r%   r&   r   r    r(   r)   r*   r'   r+   kwargsr   	__class__ l/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/models/bloom/configuration_bloom.pyr-   q   s     zBloomConfig.__init__)r   r   r   r   r   r   Tr   r   Fr   r   r   F)	__name__
__module____qualname____doc__Z
model_typeZkeys_to_ignore_at_inferenceZattribute_mapr-   __classcell__r2   r2   r0   r3   r   *   s*   ?              r   c                	       s   e Zd ZedZdeeee	 e
d fddZeeeeeef f dd	d
ZeedddZeedddZeedddZddeee
ed eeef d fddZeedddZ  ZS )BloomOnnxConfigz1.12defaultNF)configtaskpatching_specsuse_pastc                    s.   t  j||||d t| jdd s*d| j_d S )N)r<   r=   r>   pad_token_idr   )r,   r-   getattr_configr?   )r.   r;   r<   r=   r>   r0   r2   r3   r-      s    zBloomOnnxConfig.__init__)returnc                 C   sJ   t ddddi}| jr8| j|ddd ddd|d	< nddd|d	< |S )
N	input_idsbatchsequence)r   r   inputsT)	directionZinverted_values_shapezpast_sequence + sequenceattention_mask)r   r>   Zfill_with_past_key_values_)r.   common_inputsr2   r2   r3   rF      s    zBloomOnnxConfig.inputsc                 C   s   | j jS N)rA   r   r.   r2   r2   r3   
num_layers   s    zBloomOnnxConfig.num_layersc                 C   s   | j jS rJ   )rA   r   rK   r2   r2   r3   r      s    z#BloomOnnxConfig.num_attention_headsc                 C   s   dS )NgMbP?r2   rK   r2   r2   r3   atol_for_validation   s    z#BloomOnnxConfig.atol_for_validationr
   r   )	tokenizer
batch_size
seq_lengthis_pair	frameworkrB   c                    s   t t| j|||||d}td|d i}| jrt s@tdnldd l|d j\}}	|	d }
| j	j
| j }|| j ||
f || j |
|f fddt| jD |d< |d	 |d	< | jr|d	 j}j|d	 j||
|d
gdd|d	< |S )N)rP   rQ   rR   rS   rC   zACannot generate dummy past_keys inputs without PyTorch installed.r   r   c                    s    g | ]}   fqS r2   )Zzeros).0_Zpast_key_shapeZpast_value_shapetorchr2   r3   
<listcomp>   s    z9BloomOnnxConfig.generate_dummy_inputs.<locals>.<listcomp>r   rH   )dtyper   )dim)r,   r   generate_dummy_inputsr   r>   r   
ValueErrorrW   shaperA   r#   r   rangerL   rY   catZones)r.   rO   rP   rQ   rR   rS   rI   Zordered_inputsrD   ZseqlenZpast_key_values_lengthZhead_dimZ
mask_dtyper0   rV   r3   r[      sD    
    


 
z%BloomOnnxConfig.generate_dummy_inputsc                 C   s   dS )N   r2   rK   r2   r2   r3   default_onnx_opset   s    z"BloomOnnxConfig.default_onnx_opset)r:   NF)rN   rN   FN)r4   r5   r6   r   parseZtorch_onnx_minimum_versionr   strr   r   boolr-   propertyr   intrF   rL   r   floatrM   r   r   r[   ra   r8   r2   r2   r0   r3   r9      s@   
        
1r9   N)r7   collectionsr   typingr   r   r   r   r   	packagingr    r
   r   Zconfiguration_utilsr   Zonnxr   r   utilsr   r   Z
get_loggerr4   loggerZ#BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAPr   r9   r2   r2   r2   r3   <module>   s$   

n