
    gG                         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j        e          Z G d
 de
          Z G 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loggingc                   j     e Zd ZdZdZdgZdddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z x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           	      p   || _         |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   r0   r/   r1   r7   r8   r9   r&   r5   r   r2   r3   r4   r6   r'   r;   r$   r%   r(   r<   kwargs	__class__s                              k/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/models/marian/configuration_marian.pyr>   zMarianConfig.__init__j   s    : %"4"B
'>$.,'>$.,'>$!2"4#6  !2!2"!/.0P- 	
%%1#9 3	
 	
 	
 	
 	
 	
 	
    )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__rA   s   @rB   r   r      s        E EN J#4"5,EV_``M  $ " ""$)-59
 9
 9
 9
 9
 9
 9
 9
 9
 9
rC   r   c                       e Zd Zedeeeeef         f         fd            Zedeeeeef         f         f fd            Z	 	 	 	 dde	ded	ed
e
dee         deeef         fdZ	 	 	 	 dde	ded	ed
e
dee         deeef         fdZ	 	 	 	 dde	ded	ed
e
dee         deeef         fdZ	 	 	 	 dde	ded	ed
e
dee         deeef         fdZ fdZedefd            Z xZS )MarianOnnxConfigreturnc           	         | j         dv rat          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|                     |d           n| j         dk    rWt          ddddfddddfg          }| j        r4| j        \  }}t          |          D ]}ddd|d| d<   ddd|d| d<   n'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_is        rB   r[   zMarianOnnxConfig.inputs   s    9111' g2D"E"EF%77I'J'JK M } ^67\12>EJl:m:m6779@EW5X5X12>EJ\:]:]67} S///RRRY+%%' g2D"E"EF%77I'J'JK M } n(,%"A122 n nADKPj@k@kM"<Q"<"<"<=FMRlBmBmM">Q">">">??' g2D"E"EF%77I'J'JK(g:L*M*MN-7?Q/R/RS	 M rC   c                     | j         dv rt                      j        }nUt          t          |           j        }| j        r4| j        \  }}t          |          D ]}ddd|d| d<   ddd|d| d<   |S )NrQ   rT   r]   r^   zpresent.r`   ra   )rb   r=   outputsr   rc   re   rf   )r?   common_outputsrh   ri   rj   rA   s        rB   rl   zMarianOnnxConfig.outputs   s     9111"WW_NN"#5t<<DN} g(,%"A122 g gA=DIc9d9dN#5a#5#5#56?FKe;f;fN#7a#7#7#788rC   FN	tokenizer
batch_size
seq_lengthis_pair	frameworkc           	      x   |                      |||||          }| j        s|nd}|                      |||||          }d |                                D             }t          di ||}	| j        rt	                      st          d          dd l}
|	d         j        \  }}|	d         j        d         }| j        \  }}|||| j	        j
        |z  f}|dz   }|||| j	        j
        |z  f}|
                    |	d         |
                    ||          gd	          |	d<   g |	d
<   | j        \  }}t          ||          }t          ||          |z
  }||k    rdnd}t!          |          D ]m}|	d
                             |
                    |          |
                    |          |
                    |          |
                    |          f           n|dk    r|n|}t!          ||          D ]E}|	d
                             |
                    |          |
                    |          f           F|	S )NrV   c                      i | ]\  }}d | |S )decoder_r)   ).0nametensors      rB   
<dictcomp>zVMarianOnnxConfig._generate_dummy_inputs_for_default_and_seq2seq_lm.<locals>.<dictcomp>   s'    ___f+T++V___rC   ACannot generate dummy past_keys inputs without PyTorch installed.r   rS   rX   r   rY   dimr   encoderdecoderr)   )._generate_dummy_inputs_for_encoder_and_decoderrc   itemsdictr   
ValueErrortorchshaper   _configr   catonesre   minmaxrf   appendzeros)r?   ro   rp   rq   rr   rs   encoder_inputsdecoder_seq_lengthdecoder_inputsrg   r   rT   encoder_seq_lengthnum_encoder_attention_headsnum_decoder_attention_headsencoder_shapedecoder_past_lengthdecoder_shaperh   num_decoder_layersmin_num_layersmax_num_layersremaining_side_nameri   r   s                            rB   1_generate_dummy_inputs_for_default_and_seq2seq_lmzBMarianOnnxConfig._generate_dummy_inputs_for_default_and_seq2seq_lm   s    LLz:w	
 

 04}CZZ!LLz#5w	
 
 `_H\H\H^H^___@@~@@@= -	b%''  !deee(5k(B(H%E%!./B!C!I!!LGKG_D')D+"(,GG	M #5q"8+#(,GG	M 7<ii78%**UL_:`:`agh 7@ 7 7M23 02M+,59_2 2 !35GHHN !35GHH>YN/ADV/V/V))\e>**  /077M22M22M22M22	    &9I%E%EMM=E>>:: b b/077U9K9KU[[Y^M_M_8`aaaarC   c                    |                      |||||          }| j        rt                      st          d          dd l|d         j        \  }}|dz   }	| j        \  }
}| j        \  }}|||	| j        j	        |z  f|d         j
        }                    |d                             ||	|          gd          |d<   fd	t          |
          D             |d
<   |S )Nr{   r   rS   r_   rW   )dtyperV   r|   c                 d    g | ],}                                                              f-S r)   )r   )rw   ri   
past_shaper   s     rB   
<listcomp>zIMarianOnnxConfig._generate_dummy_inputs_for_causal_lm.<locals>.<listcomp>G  sC     0 0 0GHZ((%++j*A*AB0 0 0rC   r   )r   rc   r   r   r   r   re   r   r   r   r   r   r   rf   )r?   ro   rp   rq   rr   rs   rg   rT   seqlenpast_key_values_lengthrh   ri   r   
mask_dtyper   r   s                 @@rB   $_generate_dummy_inputs_for_causal_lmz5MarianOnnxConfig._generate_dummy_inputs_for_causal_lm&  sC    KKz:w	
 
 = 	%''  !deee)+6<ME6%+aZ"$(O!-1-E*'+&(,GG	J ''78>J.3ii/0%**UDZbl*2m2mntu /8 / /M*+0 0 0 0 0LQRdLeLe0 0 0M+, rC   c                    t          |t          j        d          }|                    |          }t          |t          j        |          }d                    |j        g          |z  g|z  }t           |||                    }|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_tokenr   )	r?   ro   rp   rq   rr   rs   token_to_adddummy_inputrg   s	            rB   r   z?MarianOnnxConfig._generate_dummy_inputs_for_encoder_and_decoderN  s     6
(FYZ
 
 


 !::7CC5
(I\h
 
 


 xx!4 566CDzQYY{9MMMNNrC   c                     | j         dv r|                     |||||          }n|                     |||||          }|S )NrQ   )rp   rq   rr   rs   )rb   r   r   )r?   ro   rp   rq   rr   rs   rg   s          rB   generate_dummy_inputsz&MarianOnnxConfig.generate_dummy_inputsh  sn     9111 RRjZQXdm S  MM
 !EEjZQXdm F  M rC   c                     | j         dv r&t                                          ||||          }d S t          t          |                               ||||          }d S )NrQ   )rb   r=   _flatten_past_key_values_r   )r?   flattened_outputrx   idxtrA   s        rB   r   z*MarianOnnxConfig._flatten_past_key_values_}  si    9111$ww@@AQSWY\^_``$%>EE__ $Q   rC   c                     dS )Ng-C6?r)   )r?   s    rB   atol_for_validationz$MarianOnnxConfig.atol_for_validation  s    trC   )rn   rn   FN)rD   rE   rF   propertyr   strintr[   rl   r   boolr   r   r   r   r   r   r   r   floatr   rK   rL   s   @rB   rN   rN      s       )WS#X%6 67 ) ) ) X)V 
gc3h&7!78 
 
 
 
 
 X
 *.B B&B B 	B
 B J'B 
c	B B B BN *.$ $&$ $ 	$
 $ J'$ 
c	$ $ $ $V *. &  	
  J' 
c	   : *. &  	
  J' 
c	   *     U    X    rC   rN   N)rG   collectionsr   typingr   r   r    r   configuration_utilsr	   onnxr
   r   r   
onnx.utilsr   utilsr   r   r   
get_loggerrD   loggerr   rN   r)   rC   rB   <module>r      s6   !   # # # # # # ) ) ) ) ) ) ) ) ) ) # # # # # # 3 3 3 3 3 3 M M M M M M M M M M : : : : : : < < < < < < < < < < 
	H	%	%E
 E
 E
 E
 E
# E
 E
 E
Pa a a a a0 a a a a arC   