o
    hH                     @   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mZ ddlmZ dd	lmZmZmZ eeZd
diZG dd de
ZG dd deZdS )z Marian model configuration    )OrderedDict)AnyMappingOptional   )PreTrainedTokenizer)PretrainedConfig)
OnnxConfigOnnxConfigWithPastOnnxSeq2SeqConfigWithPast) compute_effective_axis_dimension)
TensorTypeis_torch_availableloggingzHelsinki-NLP/opus-mt-en-dezJhttps://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.jsonc                       sh   e Zd ZdZdZdgZdddZ					
			
																			d fdd	Z  ZS )MarianConfiga  
    This is the configuration class to store the configuration of a [`MarianModel`]. It is used to instantiate an
    Marian 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 Marian
    [Helsinki-NLP/opus-mt-en-de](https://huggingface.co/Helsinki-NLP/opus-mt-en-de) 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 58101):
            Vocabulary size of the Marian model. Defines the number of different tokens that can be represented by the
            `inputs_ids` passed when calling [`MarianModel`] or [`TFMarianModel`].
        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 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://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 0):
            The id of the token to force as the last generated token when `max_length` is reached. Usually set to
            `eos_token_id`.

    Examples:

    ```python
    >>> from transformers import MarianModel, MarianConfig

    >>> # Initializing a Marian Helsinki-NLP/opus-mt-en-de style configuration
    >>> configuration = MarianConfig()

    >>> # Initializing a model from the Helsinki-NLP/opus-mt-en-de style configuration
    >>> model = MarianModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```marianpast_key_valuesencoder_attention_headsd_model)num_attention_headshidden_size  N                    Tgelu皙?{Gz?  Fr   c                    s   || _ |p|| _|| _|| _|| _|| _|| _|| _|| _|	| _	|| _
|| _|| _|| _|| _|
| _|| _|| _|| _|| _|| _t jd|||||d| d S )N)pad_token_ideos_token_idis_encoder_decoderdecoder_start_token_idforced_eos_token_id )
vocab_sizedecoder_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_cachenum_hidden_layersscale_embedding share_encoder_decoder_embeddingssuper__init__)selfr'   r(   r)   r+   r*   r   r-   r,   r.   r4   r5   r6   r#   r2   r   r/   r0   r1   r3   r$   r8   r!   r"   r%   r9   kwargs	__class__r&   e/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/models/marian/configuration_marian.pyr;   n   s<   

zMarianConfig.__init__)r   Nr   r   r   r   r   r   r   r   r   TTr   r   r   r   r   r   r    Fr    r   r   T)	__name__
__module____qualname____doc__
model_typekeys_to_ignore_at_inferenceattribute_mapr;   __classcell__r&   r&   r>   r@   r   "   s>    G
r   c                       sV  e Zd Zedeeeeef f fddZedeeeeef f f fddZ				dd	e	d
edede
dee deeef fddZ				dd	e	d
edede
dee deeef fddZ				dd	e	d
edede
dee deeef fddZ				dd	e	d
edede
dee deeef fddZ fddZedefddZ  ZS )MarianOnnxConfigreturnc                 C   s4  | j dv r@tddddfddddfg}| jr&ddi|d< dd	d|d
< nddd|d< ddd|d
< | jr>| j|dd |S | j dkr|tddddfddddfg}| jrz| j\}}t|D ]}ddd|d| d< ddd|d| d< qa|S t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      attention_maskr   decoder_input_idsz past_decoder_sequence + sequencedecoder_attention_maskdecoder_sequenceinputs)	directionz	causal-lmpast_sequence + sequencer      zpast_key_values..key.value)taskr   use_pastfill_with_past_key_values_
num_layersrange)r<   common_inputsnum_encoder_layers_ir&   r&   r@   rU      sD   


	zMarianOnnxConfig.inputsc                    sp   | j dv rt j}|S tt| j}| jr6| j\}}t|D ]}ddd|d| d< ddd|d| d< q|S )NrK   rN   rW   rX   zpresent.rZ   r[   )r\   r:   outputsr
   r]   r_   r`   )r<   common_outputsrb   rc   rd   r>   r&   r@   re      s   

zMarianOnnxConfig.outputsFN	tokenizer
batch_size
seq_lengthis_pair	frameworkc              	   C   s  |  |||||}| js|nd}|  |||||}dd | D }tdi ||}	| jrt s5td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rdnd}t|D ]}|	d |
||
||
||
|f q|dkr|n|}t||D ]}|	d |
||
|f q|	S )NrP   c                 S   s   i | ]
\}}d | |qS )decoder_r&   ).0nametensorr&   r&   r@   
<dictcomp>   s    zVMarianOnnxConfig._generate_dummy_inputs_for_default_and_seq2seq_lm.<locals>.<dictcomp>ACannot generate dummy past_keys inputs without PyTorch installed.r   rM   rR   r   rS   dimr   encoderdecoderr&   )._generate_dummy_inputs_for_encoder_and_decoderr]   itemsdictr   
ValueErrortorchshaper   _configr   catonesr_   minmaxr`   appendzeros)r<   rh   ri   rj   rk   rl   encoder_inputsdecoder_seq_lengthdecoder_inputsra   r{   rN   encoder_seq_lengthnum_encoder_attention_headsnum_decoder_attention_headsencoder_shapedecoder_past_lengthdecoder_shaperb   num_decoder_layersmin_num_layersmax_num_layersremaining_side_namerc   r|   r&   r&   r@   1_generate_dummy_inputs_for_default_and_seq2seq_lm   s^   







	 zBMarianOnnxConfig._generate_dummy_inputs_for_default_and_seq2seq_lmc                    s   |  |||||}| jr\t std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 )Nrr   r   rM   rY   rQ   )dtyperP   rs   c                    s    g | ]}    fqS r&   )r   )rn   rc   
past_shaper{   r&   r@   
<listcomp>K  s    zIMarianOnnxConfig._generate_dummy_inputs_for_causal_lm.<locals>.<listcomp>r   )rw   r]   r   rz   r{   r|   r_   r   r}   r   r   r~   r   r`   )r<   rh   ri   rj   rk   rl   ra   rN   seqlenpast_key_values_lengthrb   rc   r   
mask_dtyper&   r   r@   $_generate_dummy_inputs_for_causal_lm*  s0   






z5MarianOnnxConfig._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   )fixed_dimensionnum_token_to_add )return_tensors)r   r	   default_fixed_batchnum_special_tokens_to_adddefault_fixed_sequencejoin	unk_tokenry   )	r<   rh   ri   rj   rk   rl   token_to_adddummy_inputra   r&   r&   r@   rw   R  s   
z?MarianOnnxConfig._generate_dummy_inputs_for_encoder_and_decoderc                 C   s:   | j dv r| j|||||d}|S | j|||||d}|S )NrK   )ri   rj   rk   rl   )r\   r   r   )r<   rh   ri   rj   rk   rl   ra   r&   r&   r@   generate_dummy_inputsl  s   

	
z&MarianOnnxConfig.generate_dummy_inputsc                    s:   | j dv rt ||||}d S tt| ||||}d S )NrK   )r\   r:   _flatten_past_key_values_r   )r<   flattened_outputro   idxtr>   r&   r@   r     s
   

z*MarianOnnxConfig._flatten_past_key_values_c                 C   s   dS )Ng-C6?r&   )r<   r&   r&   r@   atol_for_validation  s   z$MarianOnnxConfig.atol_for_validation)rg   rg   FN)rA   rB   rC   propertyr   strintrU   re   r   boolr   r   r   r   r   rw   r   r   floatr   rH   r&   r&   r>   r@   rI      s     +$

G

+



rI   N)rD   collectionsr   typingr   r   r    r   configuration_utilsr   onnxr	   r
   r   
onnx.utilsr   utilsr   r   r   
get_loggerrA   logger$MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAPr   rI   r&   r&   r&   r@   <module>   s   
 	