U
    9%eHJ                     @   s   d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZmZmZ dd	lmZ dd
lmZ eeZddiZG dd de
ZG dd deZdS )z Blenderbot model configuration    )OrderedDict)AnyMappingOptional   )PreTrainedTokenizer)PretrainedConfig)
TensorTypeis_torch_available)
OnnxConfigOnnxConfigWithPastOnnxSeq2SeqConfigWithPast) compute_effective_axis_dimension)loggingzfacebook/blenderbot-3BzFhttps://huggingface.co/facebook/blenderbot-3B/resolve/main/config.jsonc                       s6   e Zd ZdZdZdgZdddZd fdd	Z  ZS )BlenderbotConfiga  
    This is the configuration class to store the configuration of a [`BlenderbotModel`]. It is used to instantiate an
    Blenderbot model according to the specified arguments, defining the model architecture. Instantiating a
    configuration with the defaults will yield a similar configuration to that of the Blenderbot
    [facebook/blenderbot-3B](https://huggingface.co/facebook/blenderbot-3B) architecture.

    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 50265):
            Vocabulary size of the Blenderbot model. Defines the number of different tokens that can be represented by
            the `inputs_ids` passed when calling [`BlenderbotModel`] or [`TFBlenderbotModel`].
        d_model (`int`, *optional*, defaults to 1024):
            Dimensionality of the layers and the pooler layer.
        encoder_layers (`int`, *optional*, defaults to 12):
            Number of encoder layers.
        decoder_layers (`int`, *optional*, defaults to 12):
            Number of decoder layers.
        encoder_attention_heads (`int`, *optional*, defaults to 16):
            Number of attention heads for each attention layer in the Transformer encoder.
        decoder_attention_heads (`int`, *optional*, defaults to 16):
            Number of attention heads for each attention layer in the Transformer decoder.
        decoder_ffn_dim (`int`, *optional*, defaults to 4096):
            Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
        encoder_ffn_dim (`int`, *optional*, defaults to 4096):
            Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
        activation_function (`str` or `function`, *optional*, defaults to `"gelu"`):
            The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
            `"relu"`, `"silu"` and `"gelu_new"` are supported.
        dropout (`float`, *optional*, defaults to 0.1):
            The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
        attention_dropout (`float`, *optional*, defaults to 0.0):
            The dropout ratio for the attention probabilities.
        activation_dropout (`float`, *optional*, defaults to 0.0):
            The dropout ratio for activations inside the fully connected layer.
        max_position_embeddings (`int`, *optional*, defaults to 128):
            The maximum sequence length that this model might ever be used with. Typically set this to something large
            just in case (e.g., 512 or 1024 or 2048).
        init_std (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        encoder_layerdrop (`float`, *optional*, defaults to 0.0):
            The LayerDrop probability for the encoder. See the [LayerDrop paper](see https://arxiv.org/abs/1909.11556)
            for more details.
        decoder_layerdrop (`float`, *optional*, defaults to 0.0):
            The LayerDrop probability for the decoder. See the [LayerDrop paper](see https://arxiv.org/abs/1909.11556)
            for more details.
        scale_embedding (`bool`, *optional*, defaults to `False`):
            Scale embeddings by diving by sqrt(d_model).
        use_cache (`bool`, *optional*, defaults to `True`):
            Whether or not the model should return the last key/values attentions (not used by all models)
        forced_eos_token_id (`int`, *optional*, defaults to 2):
            The id of the token to force as the last generated token when `max_length` is reached. Usually set to
            `eos_token_id`.

    Example:

    ```python
    >>> from transformers import BlenderbotConfig, BlenderbotModel

    >>> # Initializing a Blenderbot facebook/blenderbot-3B style configuration
    >>> configuration = BlenderbotConfig()

    >>> # Initializing a model (with random weights) from the facebook/blenderbot-3B style configuration
    >>> model = BlenderbotModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```Z
blenderbotpast_key_valuesencoder_attention_headsd_model)num_attention_headshidden_sizeH         (                 Tgelu 
  皙?{Gz?   Fr   r   c              
      s   || _ || _|| _|| _|| _|| _|| _|| _|| _|| _	|| _
|| _|| _|| _|	| _|
| _|| _|| _|| _t jf |||||||d| d S )N)pad_token_idbos_token_ideos_token_idis_encoder_decoderdecoder_start_token_idencoder_no_repeat_ngram_sizeforced_eos_token_id)
vocab_sizemax_position_embeddingsr   encoder_ffn_dimencoder_layersr   decoder_ffn_dimdecoder_layersdecoder_attention_headsdropoutattention_dropoutactivation_dropoutactivation_functioninit_stdencoder_layerdropdecoder_layerdrop	use_cacheZnum_hidden_layersscale_embeddingsuper__init__)selfr)   r*   r,   r+   r   r.   r-   r/   r5   r6   r7   r%   r3   r   r0   r1   r2   r4   r&   r8   r"   r#   r$   r'   r(   kwargs	__class__ v/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/models/blenderbot/configuration_blenderbot.pyr:   o   s<    zBlenderbotConfig.__init__)r   r   r   r   r   r   r   r   r   r   TTr   r   r   r   r   r    r!   Fr   r!   r   r   r   )	__name__
__module____qualname____doc__Z
model_typeZkeys_to_ignore_at_inferenceZattribute_mapr:   __classcell__r?   r?   r=   r@   r   $   s<   F
                         r   c                	       s  e Zd Zeeeeeef f dddZeeeeeef f d fddZde	eee
ee eeef d	d
dZde	eee
ee eeef d	ddZde	eee
ee eeef d	ddZde	eee
ee eeef d	ddZ fddZeeeeef f edddZ  ZS )BlenderbotOnnxConfig)returnc                 C   s0  | j dkr~tddddfddddfg}| jrLddi|d< dd	d|d
< nddd|d< ddd|d
< | jr|| j|dd n| j dkrtddddfddddfg}| jr| j\}}t|D ]0}ddd|d| d< ddd|d| d< qn8tddddfddddfddddfd
dddfg}|S )Ndefaultz
seq2seq-lm	input_idsbatchencoder_sequence)r   r!   attention_maskr   decoder_input_ids past_decoder_sequence + sequencedecoder_attention_maskdecoder_sequenceinputs)	direction	causal-lmpast_sequence + sequencer   r   zpast_key_values..key.value)taskr   use_pastfill_with_past_key_values_
num_layersrange)r;   common_inputs_num_decoder_layersir?   r?   r@   rR      s@    


	zBlenderbotOnnxConfig.inputsc                    sn   | j dkrt j}nVtt| j}| jrj| j\}}t|D ]0}ddd|d| d< ddd|d| d< q8|S )NrH   rK   rU   rV   zpresent.rW   rX   )rY   r9   outputsr   rZ   r\   r]   )r;   Zcommon_outputsZnum_encoder_layersr_   ra   r=   r?   r@   rb      s    


zBlenderbotOnnxConfig.outputsFN)	tokenizer
batch_size
seq_lengthis_pair	frameworkrG   c              	   C   s:  |  |||||}| js|nd}|  |||||}dd | D }tf ||}	| jr6t sjtdndd l}
|	d j\}}|	d jd }| j\}}|||| j	j
| f}|}|||| j	j
| f}|
j|	d |
||gdd	|	d< g |	d
< | j\}}t|D ]4}|	d
 |
||
||
||
|f q |	S )Nr!   c                 S   s   i | ]\}}d | |qS )Zdecoder_r?   ).0nameZtensorr?   r?   r@   
<dictcomp>   s      zZBlenderbotOnnxConfig._generate_dummy_inputs_for_default_and_seq2seq_lm.<locals>.<dictcomp>ACannot generate dummy past_keys inputs without PyTorch installed.r   rJ   rN   rP   dimr   )I_generate_dummy_inputs_for_sequence_classification_and_question_answeringrZ   itemsdictr
   
ValueErrortorchshaper   _configr   catonesr\   r]   appendzeros)r;   rd   re   rf   rg   rh   Zencoder_inputsZdecoder_seq_lengthZdecoder_inputsr^   rs   rK   Zencoder_seq_lengthnum_encoder_attention_headsZnum_decoder_attention_headsZencoder_shapeZdecoder_past_lengthZdecoder_shaper_   r`   r?   r?   r@   1_generate_dummy_inputs_for_default_and_seq2seq_lm   sd            



 

zFBlenderbotOnnxConfig._generate_dummy_inputs_for_default_and_seq2seq_lmc                    s   |  |||||}| jrt s(tdndd l|d j\}}|}	| j\}
}| j\}}
|||	| jj	| f |d j
}j|d j||	|dgdd|d<  fdd	t|D |d
< |S )Nrl   r   rJ   rM   )dtyper!   rm   c                    s    g | ]}    fqS r?   )ry   )ri   r_   Z
past_shapers   r?   r@   
<listcomp><  s    zMBlenderbotOnnxConfig._generate_dummy_inputs_for_causal_lm.<locals>.<listcomp>r   )ro   rZ   r
   rr   rs   rt   r\   r   ru   r   r|   rv   rw   r]   )r;   rd   re   rf   rg   rh   r^   rK   ZseqlenZpast_key_values_lengthr_   r`   rz   Z
mask_dtyper?   r}   r@   $_generate_dummy_inputs_for_causal_lm  s:        




 

z9BlenderbotOnnxConfig._generate_dummy_inputs_for_causal_lmc           	      C   sV   t |tjdd}||}t |tj|d}d|jg| g| }t|||d}|S )Nr   )Zfixed_dimensionZnum_token_to_add )Zreturn_tensors)r   r   Zdefault_fixed_batchZnum_special_tokens_to_addZdefault_fixed_sequencejoinZ	unk_tokenrq   )	r;   rd   re   rf   rg   rh   Ztoken_to_addZdummy_inputr^   r?   r?   r@   ro   B  s      
  z^BlenderbotOnnxConfig._generate_dummy_inputs_for_sequence_classification_and_question_answeringc                 C   sX   | j dkr | j|||||d}n4| j dkr@| j|||||d}n| j|||||d}|S )NrH   )re   rf   rg   rh   rT   )rY   r{   r   ro   )r;   rd   re   rf   rg   rh   r^   r?   r?   r@   generate_dummy_inputs]  s0    
    
        z*BlenderbotOnnxConfig.generate_dummy_inputsc                    s8   | j dkrt ||||}ntt| ||||}d S )NrH   )rY   r9   _flatten_past_key_values_r   )r;   Zflattened_outputrj   idxtr=   r?   r@   r   v  s    

   z.BlenderbotOnnxConfig._flatten_past_key_values_)inputs_or_outputsrS   c           	      C   s   |dkrt d| d|dkr$dnd}| j\}}d}|dkrBdnd	}t|D ]l}d
|d|| d| d< d
|d|| d| d< d
|d|| d| d< d
|d|| d| d< qNd S )N)rR   rb   z4direction must either be "inputs" or "outputs", but z
 was givenrR   r   ZpresentZpast_encoder_sequenceZpast_decoder_sequencerO   rK   rV   .z.decoder.keyz.decoder.valuez.encoder.keyz.encoder.value)rr   r\   r]   )	r;   r   rS   rj   r_   r`   rL   rQ   ra   r?   r?   r@   r[   ~  s    
z/BlenderbotOnnxConfig.fill_with_past_key_values_)rc   rc   FN)rc   rc   FN)rc   rc   FN)rc   rc   FN)rA   rB   rC   propertyr   strintrR   rb   r   boolr   r	   r   r{   r   ro   r   r   r[   rE   r?   r?   r=   r@   rF      sl    ($    
<    
(    
    
rF   N)rD   collectionsr   typingr   r   r    r   Zconfiguration_utilsr   Z
file_utilsr	   r
   Zonnxr   r   r   Z
onnx.utilsr   utilsr   Z
get_loggerrA   loggerZ(BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAPr   rF   r?   r?   r?   r@   <module>   s   
  	