
    n i0                     `   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
 h dZe	j        j        e	j        j        e	j        j        e	j        j        e	j        j        dZd Zd	 Z G d
 de          Zd ZddZd Zd ZddZd ZddZd Zd Zd Zd Zd Z e!dk    r e              dS dS )    N)Optional)utils)	Converter)common_spectransformer_spec>   barttransformertransformer_lmtransformer_alignmultilingual_transformer)gelugelu_accurate	gelu_fastreluswishc                    dd l }t          | dd          }|j        j        | j                 }t          j                    } ||t          v d|d| j        dd                    t                    d           |	                                  ||t          v d	|d
d                    t                                                    d            |t          | dd           d            |t          | dd           d           |dk    r || j         d            || j         d           |	                                 t          j                            | j        | j        | j        t          |         t          | dd          | j        | j        | j        k              S  || j        | j        k    d            || j        | j        k    d           |	                                 t          j                            | j        | j        f| j        | j        t          |         t          | dd          t          | dd          t          | dd                    S )Nr   activation_fnr   zModel 'z' used by architecture 'z*' is not supported (supported models are: z, )zOption --activation-fn z. is not supported (supported activations are: no_token_positional_embeddingsFz8Option --no-token-positional-embeddings is not supportedlang_tok_replacing_bos_eosz4Option --lang-tok-replacing-bos-eos is not supportedr
   z.Option --character-embeddings is not supportedz(Option --adaptive-input is not supportedlayernorm_embedding)pre_norm
activationr   no_final_normproject_in_outzZOptions --encoder-normalize-before and --decoder-normalize-before must have the same valuezXOptions --encoder-attention-heads and --decoder-attention-heads must have the same valuealignment_layeralignment_heads)r   r   r   r   r   )fairseqgetattrmodelsARCH_MODEL_NAME_REGISTRYarchr   ConfigurationChecker_SUPPORTED_MODELSjoinvalidate_SUPPORTED_ACTIVATIONSkeyscharacter_embeddingsadaptive_inputr   TransformerDecoderModelSpecfrom_configdecoder_layersdecoder_attention_headsdecoder_normalize_beforeno_decoder_final_normdecoder_input_dimdecoder_embed_dimencoder_normalize_beforeencoder_attention_headsTransformerSpecencoder_layers)argsr   r   
model_namechecks        ^/var/www/html/auto_sub_bot/venv/lib/python3.11/site-packages/ctranslate2/converters/fairseq.py_get_model_specr<      s   NNND/6::M8CJ&((E	E'''::tyyy$)),=">">">">	@  
 
NN	E///==$))$:$?$?$A$ABBBB	D  
 
ED:EBBBB   
ED6>>>>  
 %%%))<	
 	
 	
 	##6	
 	
 	
 	;GG(2-m< '.CU K K41T5KK H 
 
 	
 	)T-JJ'	
 	
 	

 	(D,HH'	
 	
 	

 	/;; $"56(2-m<#D*;R@@#D*;Q?? '.CU K K < 
 
 	
    c                 $    d | j         D             S )Nc                 "    g | ]}|d k    rdn|S )z<pad>z<blank> ).0tokens     r;   
<listcomp>z_get_vocab.<locals>.<listcomp>e   s&    UUU'))IIuUUUr=   )symbols)
dictionarys    r;   
_get_vocabrF   d   s    UU*BTUUUUr=   c                   x    e Zd ZdZ	 	 	 	 	 ddededee         dee         dee         d	ed
ee         fdZd ZdS )FairseqConverterz%Converts models trained with Fairseq.NF
model_pathdata_dirsource_langtarget_langfixed_dictionaryno_default_special_tokensuser_dirc                 h    || _         || _        || _        || _        || _        || _        || _        dS )a  Initializes the Fairseq converter.

        Arguments:
          model_path: Path to the Fairseq PyTorch model (.pt file).
          data_dir: Path to the Fairseq data directory containing vocabulary files.
          source_lang: Source language (may be required if not declared in the model).
          target_lang: Target language (may be required if not declared in the model).
          fixed_dictionary: Path to the fixed dictionary for multilingual models.
          no_default_special_tokens: Require all special tokens to be provided by the user
            (e.g. encoder end token, decoder start token).
          user_dir: Path to the user directory containing custom extensions.
        N)_model_path	_data_dir_fixed_dictionary_source_lang_target_lang_no_default_special_tokens	_user_dir)selfrI   rJ   rK   rL   rM   rN   rO   s           r;   __init__zFairseqConverter.__init__k   s>    , &!!1''*C'!r=   c                    dd l }dd l}ddl m} | j        r)ddlm}  |t          j        | j                             |                                5  |	                    | j
                  }|d         p|d         d         }| j        |_        | j        | j        |_        t          |d          rS|j        rLt"          j                            | j        t"          j                            |j                            |_        | j        | j        |_        | j        | j        |_        t3          |          }|j                            |          }|j                            ||          }	|	                                 |	                    |d                    tA          |tB          j"                  ratG          |j$        |	j$        d	
           |%                    tM          |j'                             |j(        s|j)        j*        |j)        _+        ntY          |j-        |	j-                   tG          |j$        |	j$                   |.                    tM          |j/                             |0                    tM          |j1                             | j2        rd |j)        _3        n"|j)        j*        |j)        _3        d|j)        _4        |cd d d            S # 1 swxY w Y   d S )Nr   )checkpoint_utils)import_user_module)rO   r8   cfgmodel	lang_dictFwith_encoder_attentionT)5r   torchr[   rW   fairseq.utilsr\   argparse	Namespaceno_gradload_checkpoint_to_cpurQ   rR   datarS   rM   hasattrr_   ospathr&   basenamerT   rK   rU   rL   r<   tasks
setup_taskr!   build_modelevalload_state_dict
isinstancer   r,   set_transformer_decoderdecoderregister_vocabularyrF   rE   add_bos_tokenconfig	eos_token	bos_tokenset_transformer_encoderencoderregister_source_vocabularysource_dictionaryregister_target_vocabularytarget_dictionaryrV   decoder_start_tokenadd_source_eos)
rX   r   rb   r[   r\   
checkpointr8   spectaskr^   s
             r;   _loadzFairseqConverter._load   s	   ,,,,,,> 	L888888x14>JJJKKK]]__ 0	 0	)@@AQRRJf%CE):7)CDDI%1(,(>%t[)) dn !#NBG$4$4T^$D$D" "  ,#'#4  ,#'#4 "4((D=++D11DN..tT::EJJLLL!!*W"5666$ 0 LMM 6'LM+0    ((DO)D)DEEE) B,0K,ADK) (emDDD'emDDD//
4;Q0R0RSSS//
4;Q0R0RSSS2 66:DK336:k6KDK315DK.a0	 0	 0	 0	 0	 0	 0	 0	 0	 0	 0	 0	 0	 0	 0	 0	 0	 0	s   J K  K$'K$)NNNFN)	__name__
__module____qualname____doc__strr   boolrY   r   r@   r=   r;   rH   rH   h   s        // &*%)*.*/"&" "" " c]	"
 c]" #3-" $(" 3-" " " "<; ; ; ; ;r=   rH   c                    t          | |           t          | j        |j                  D ]\  }}t	          ||           |j        t          | j        |j                   |j        t          | j        |j                   d S d S N)set_input_layersziplayerlayersset_transformer_encoder_layer
layer_normset_layer_normr   )r   module
layer_specr   s       r;   rz   rz      s    T6""" V];; 9 9
E%j%8888$t(9:::!-t/1KLLLLL .-r=   Tc                    t          | |           t          | j        |j                   t	          | j        |j                  D ]\  }}t          |||           |j        t          | j        |j                   |j
        t          | j
        |j
                   |j        t          | j        |j                   |j        t          | j        |j                   d S d S )Nr`   )r   
set_linear
projectionoutput_projectionr   r   r   set_transformer_decoder_layerr   r   r   project_in_dim
project_inproject_out_dimproject_out)r   r   ra   r   r   s        r;   rs   rs      s    T6"""t 8999 V];; 
 

E%#9	
 	
 	
 	
 	

 $t(9:::!-t/1KLLL(4?F$9:::)4#V%;<<<<< *)r=   c                     t          | j        |j                   t          t	          | j        t                    r| j        d         n| j        |j                   |j        | _	        d S )Nr   )
set_position_encodingsposition_encodingsembed_positionsset_embeddingsrr   
embeddingslistembed_tokensembed_scalescale_embeddingsr   r   s     r;   r   r      se    42F4JKKK($??TT_   #.Dr=   c                     t          | j        |           t          | j        |j        d           t          | j        j        |j                   d S NT)self_attention)set_ffnffnset_multi_head_attentionr   	self_attnr   r   self_attn_layer_normr   s     r;   r   r      sO    DHfT0&2BSWXXXX4&163NOOOOOr=   c                     t          | j        |           t          | j        |j        d           t          | j        j        |j                   |r;t          | j        |j	                   t          | j        j        |j
                   d S d S r   )r   r   r   r   r   r   r   r   	attentionencoder_attnencoder_attn_layer_norm)r   r   ra   s      r;   r   r      s    DHfT0&2BSWXXXX4&163NOOO R 1DEEEt~0&2PQQQQQR Rr=   c                     t          | j        |j                   t          | j        |j                   t          | j        |j                   d S r   )r   r   final_layer_normr   linear_0fc1linear_1fc2r   s     r;   r   r      sE    4?F$;<<<t}fj)))t}fj)))))r=   Fc                 ~   |rd t          d          D             }t          |d         |j                   t          |d         |j                   t          |d         |j                   t          j        | j        d         |           nt          | j        d         |j                   d t          d          D             }t          |d         |j                   t          |d         |j                   t          j        | j        d         |           t          | j        d         |j                   d S )Nc                 4    g | ]}t          j                    S r@   r   
LinearSpecrA   _s     r;   rC   z,set_multi_head_attention.<locals>.<listcomp>  !    CCCQ.00CCCr=      r         c                 4    g | ]}t          j                    S r@   r   r   s     r;   rC   z,set_multi_head_attention.<locals>.<listcomp>  r   r=   r   )	ranger   q_projk_projv_projr   fuse_linearlinearout_proj)r   r   r   split_layerss       r;   r   r     s    8CC%((CCC<?FM222<?FM222<?FM222$+a.,77774;q>6=111CC%((CCC<?FM222<?FM222$+a.,777t{200000r=   c                 ~    |j                                         | _        |j                                        | _        d S r   )weightnumpygammabiasbetar   s     r;   r   r     s0    $$&&DJ!!##DIIIr=   c                     |j                                         | _         |j         |j                                        | _        d S d S r   )r   r   r   r   s     r;   r   r     s@    -%%''DK{K%%''			 r=   c                 B    |j                                         | _         d S r   )r   r   r   s     r;   r   r     s    -%%''DKKKr=   c                     dd l }t          ||j        j                  r|j        n|j        }|                                |j        dz   d          | _        d S )Nr   r   )	rb   rr   nn	Embeddingr   weightsr   padding_idx	encodings)r   r   rb   r   s       r;   r   r   !  sR    LLL(1CDDXV]]&.F\\^^F$6$:$<$<=DNNNr=   c            	      x   t          j        t           j                  } |                     ddd           |                     ddd           |                     dd	
           |                     dd
           |                     dd
           |                     dd
           |                     ddd           t	          j        |            |                                 }t          |j        |j	        |j
        |j        |j        |j        |j                  }|                    |           d S )N)formatter_classz--model_pathTzModel path.)requiredhelpz
--data_dirz=Data directory containing the source and target vocabularies.z
--user_dirz'Directory containing custom extensions.)r   z--fixed_dictionaryz)Fixed dictionary for multilingual models.z--source_langzOSource language. This argument is used to find dictionary file from `data_dir`.z--target_langzOTarget language. This argument is used to find dictionary file from `data_dir`.z--no_default_special_tokens
store_truezjRequire all special tokens to be provided by the user during inference, including the decoder start token.)actionr   )rK   rL   rM   rN   rO   )rd   ArgumentParserArgumentDefaultsHelpFormatteradd_argumentr   declare_arguments
parse_argsrH   rI   rJ   rK   rL   rM   rN   rO   convert_from_args)parserr8   	converters      r;   mainr   (  s   $ >  F MJJJ
L    
 6     8     ^     ^     %1	     '''D $$."&"@  I %%%%%r=   __main__)T)F)"rd   rj   typingr   ctranslate2.convertersr    ctranslate2.converters.converterr   ctranslate2.specsr   r   r%   
ActivationGELUGELUTanhRELUSWISHr(   r<   rF   rH   rz   rs   r   r   r   r   r   r   r   r   r   r   r   r@   r=   r;   <module>r      s    				       ( ( ( ( ( ( 6 6 6 6 6 6 ; ; ; ; ; ; ; ;    "' +4'0"'#)  E
 E
 E
PV V V\ \ \ \ \y \ \ \~M M M= = = =&/ / /P P PR R R R* * *1 1 1 1 $ $ $
( ( (( ( (> > >-& -& -&` zDFFFFF r=   