a
    hLG                     @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	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mZmZ eeZG dd deZG dd deZddgZdS )zMBART model configuration    )OrderedDict)Mapping)AnyOptional   )PreTrainedTokenizer)PretrainedConfig)
OnnxConfigOnnxConfigWithPastOnnxSeq2SeqConfigWithPast) compute_effective_axis_dimension)
TensorTypeis_torch_availableloggingc                       s6   e Zd ZdZdZdgZdddZd fdd	Z  ZS )MBartConfigaA  
    This is the configuration class to store the configuration of a [`MBartModel`]. It is used to instantiate an MBART
    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 MBART
    [facebook/mbart-large-cc25](https://huggingface.co/facebook/mbart-large-cc25) 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 MBART model. Defines the number of different tokens that can be represented by the
            `inputs_ids` passed when calling [`MBartModel`] or [`TFMBartModel`].
        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.
        classifier_dropout (`float`, *optional*, defaults to 0.0):
            The dropout ratio for classifier.
        max_position_embeddings (`int`, *optional*, defaults to 1024):
            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://huggingface.co/papers/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://huggingface.co/papers/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 MBartConfig, MBartModel

    >>> # Initializing a MBART facebook/mbart-large-cc25 style configuration
    >>> configuration = MBartConfig()

    >>> # Initializing a model (with random weights) from the facebook/mbart-large-cc25 style configuration
    >>> model = MBartModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```Zmbartpast_key_valuesencoder_attention_headsd_model)num_attention_headshidden_sizeY                      Tgelu皙?{Gz?F   r      c                    s   || _ || _|| _|| _|| _|| _|| _|| _|| _|| _	|| _
|| _|| _|| _|	| _|
| _|| _|| _|| _|| _t jf |||||d| d S )N)pad_token_idbos_token_ideos_token_idis_encoder_decoder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classifier_dropout	use_cacheZnum_hidden_layersscale_embeddingsuper__init__)selfr&   r'   r)   r(   r   r+   r*   r,   r2   r3   r5   r$   r0   r   r-   r.   r/   r1   r4   r6   r!   r"   r#   r%   kwargs	__class__ i/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/models/mbart/configuration_mbart.pyr8   m   s:    zMBartConfig.__init__)r   r   r   r   r   r   r   r   r   r   TTr   r   r   r   r   r   r   Fr   r   r    r    )	__name__
__module____qualname____doc__Z
model_typeZkeys_to_ignore_at_inferenceZattribute_mapr8   __classcell__r=   r=   r;   r>   r      s:   I
                        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  ZS )MBartOnnxConfig)returnc                 C   s0  | j dv 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batchZencoder_sequence)r   r   attention_maskr   decoder_input_idsz past_decoder_sequence + sequencedecoder_attention_maskZdecoder_sequenceinputs)	direction	causal-lmpast_sequence + sequencer   r    zpast_key_values..key.value)taskr   use_pastZfill_with_past_key_values_
num_layersrange)r9   common_inputsnum_encoder_layers_ir=   r=   r>   rM      s@    


	zMBartOnnxConfig.inputsc                    sn   | j dv rt j}nVtt| j}| jrj| j\}}t|D ]0}ddd|d| d< ddd|d| d< q8|S )NrF   rI   rP   rQ   zpresent.rR   rS   )rT   r7   outputsr
   rU   rV   rW   )r9   Zcommon_outputsrY   rZ   r[   r;   r=   r>   r\      s    


zMBartOnnxConfig.outputsFN)	tokenizer
batch_size
seq_lengthis_pair	frameworkrE   c              	   C   s  |  |||||}| js|nd}|  |||||}dd | D }tf i ||}	| jrt sntdndd l}
|	d j\}}|	d jd }| j\}}|||| j	j
| f}|d }|||| j	j
| f}|
j|	d	 |
||gdd
|	d	< g |	d< | j\}}t||}t||| }||kr&dnd}t|D ]4}|	d |
||
||
||
|f q2|dkrv|n|}t||D ]$}|	d |
||
|f q|	S )Nr   c                 S   s   i | ]\}}d | |qS )Zdecoder_r=   ).0nameZtensorr=   r=   r>   
<dictcomp>       zUMBartOnnxConfig._generate_dummy_inputs_for_default_and_seq2seq_lm.<locals>.<dictcomp>ACannot generate dummy past_keys inputs without PyTorch installed.r   rH   rK   r   rL   dimr   encoderdecoder)I_generate_dummy_inputs_for_sequence_classification_and_question_answeringrU   itemsdictr   
ValueErrortorchshaper   _configr   catonesrV   minmaxrW   appendzeros)r9   r^   r_   r`   ra   rb   Zencoder_inputsZdecoder_seq_lengthZdecoder_inputsrX   rp   rI   Zencoder_seq_lengthnum_encoder_attention_headsZnum_decoder_attention_headsZencoder_shapeZdecoder_past_lengthZdecoder_shaperY   Znum_decoder_layersZmin_num_layersZmax_num_layersZremaining_side_namerZ   rq   r=   r=   r>   1_generate_dummy_inputs_for_default_and_seq2seq_lm   s^    








	"zAMBartOnnxConfig._generate_dummy_inputs_for_default_and_seq2seq_lmc                    s   |  |||||}| jrt s(tdndd l|d j\}}|d }	| j\}
}| j\}}|||	| jj	| f |d j
}j|d j||	|dgdd|d<  fd	d
t|
D |d< |S )Nrg   r   rH   r    rJ   )dtyper   rh   c                    s    g | ]}    fqS r=   )rx   )rc   rZ   Z
past_shaperp   r=   r>   
<listcomp>G  s   zHMBartOnnxConfig._generate_dummy_inputs_for_causal_lm.<locals>.<listcomp>r   )rl   rU   r   ro   rp   rq   rV   r   rr   r   r{   rs   rt   rW   )r9   r^   r_   r`   ra   rb   rX   rI   ZseqlenZpast_key_values_lengthrY   rZ   ry   Z
mask_dtyper=   r|   r>   $_generate_dummy_inputs_for_causal_lm&  s0    







z4MBartOnnxConfig._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_tokenrn   )	r9   r^   r_   r`   ra   rb   Ztoken_to_addZdummy_inputrX   r=   r=   r>   rl   L  s    
zYMBartOnnxConfig._generate_dummy_inputs_for_sequence_classification_and_question_answeringc                 C   sX   | j dv r | j|||||d}n4| j dkr@| j|||||d}n| j|||||d}|S )NrF   )r_   r`   ra   rb   rO   )rT   rz   r~   rl   )r9   r^   r_   r`   ra   rb   rX   r=   r=   r>   generate_dummy_inputsf  s    




z%MBartOnnxConfig.generate_dummy_inputsc                    s8   | j dv rt ||||}ntt| ||||}d S )NrF   )rT   r7   _flatten_past_key_values_r   )r9   Zflattened_outputrd   idxtr;   r=   r>   r   ~  s
    

z)MBartOnnxConfig._flatten_past_key_values_)r]   r]   FN)r]   r]   FN)r]   r]   FN)r]   r]   FN)r?   r@   rA   propertyr   strintrM   r\   r   boolr   r   r   rz   r~   rl   r   r   rC   r=   r=   r;   r>   rD      sj    +$    
G    
)    
    
rD   N)rB   collectionsr   collections.abcr   typingr   r    r   Zconfiguration_utilsr   Zonnxr	   r
   r   Z
onnx.utilsr   utilsr   r   r   Z
get_loggerr?   loggerr   rD   __all__r=   r=   r=   r>   <module>   s   
 
 `