
    \j'3                        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         yy)    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j                  t              d       |j                           ||t        v d	|d
dj                  t        j                               d        |t        | dd       d        |t        | dd       d       |dk(  r || j                   d        || j                   d       |j                          t        j                  j!                  | j"                  | j$                  | j&                  t        |   t        | dd      | j(                  | j*                  | j,                  k7        S  || j.                  | j&                  k(  d        || j0                  | j$                  k(  d       |j                          t        j2                  j!                  | j4                  | j"                  f| j0                  | 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        Q/root/.hermes/venv/lib/python3.12/site-packages/ctranslate2/converters/fairseq.py_get_model_specr<      sR   D/6:M88CJ&&(E	''tyy$)),=">	@
 
NN	//$))$:$?$?$AB	D
 
D:EBBB 
D6>>>
 %%)))<	
 	###6	
 	;;GG((22-m< '.CU K4411T5K5KK H 
 	
 	))T-J-JJ'	

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

 	//;;  $"5"56((22-m<#D*;R@#D*;Q? '.CU K < 
 	
    c                 N    | j                   D cg c]  }|dk(  rdn| c}S c c}w )Nz<pad>z<blank>)symbols)
dictionarytokens     r;   
_get_vocabrB   d   s(    BLBTBTU')Iu4UUUs   "c                   d    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   d
efdZd Zy)FairseqConverterz%Converts models trained with Fairseq.N
model_pathdata_dirsource_langtarget_langfixed_dictionaryno_default_special_tokensuser_dirunsafe_deserializationc	                 t    || _         || _        || _        || _        || _        || _        || _        || _        y)a5  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.
          unsafe_deserialization: Allow unsafe pickle deserialization when loading
            trusted legacy checkpoints.
        N)_model_path	_data_dir_fixed_dictionary_source_lang_target_lang_no_default_special_tokens	_user_dir_unsafe_deserialization)	selfrE   rF   rG   rH   rI   rJ   rK   rL   s	            r;   __init__zFairseqConverter.__init__k   sB    2 &!!1''*C'!'=$r=   c                    dd l }dd l}ddl m} | j                  r,ddlm}  |t        j                  | j                               |j                         5  |j                  | j                  |j                  d      | j                         }|d   xs |d   d	   }| j                  |_        | j                  | j                  |_        t#        |d
      rb|j$                  rVt&        j(                  j+                  | j                  t&        j(                  j-                  |j$                              |_        | j.                  | j.                  |_        | j2                  | j2                  |_        t7        |      }|j8                  j;                  |      }|j<                  j?                  ||      }	|	jA                          |	jC                  |d	          tE        |tF        jH                        rytK        |jL                  |	jL                  d       |jO                  tQ        |jR                               |jT                  s|jV                  jX                  |jV                  _-        nt]        |j^                  |	j^                         tK        |jL                  |	jL                         |ja                  tQ        |jb                               |je                  tQ        |jf                               | jh                  rd |jV                  _5        n6|jV                  jX                  |jV                  _5        d|jV                  _6        |cd d d        S # 1 sw Y   y xY w)Nr   )checkpoint_utils)import_user_module)rK   cpu)map_locationweights_onlyr8   cfgmodel	lang_dictFwith_encoder_attentionT)7r   torchrY   rT   fairseq.utilsrZ   argparse	Namespaceno_gradloadrN   devicerU   rO   datarP   rI   hasattrr`   ospathr&   basenamerQ   rG   rR   rH   r<   tasks
setup_taskr!   build_modelevalload_state_dict
isinstancer   r,   set_transformer_decoderdecoderregister_vocabularyrB   r@   add_bos_tokenconfig	eos_token	bos_tokenset_transformer_encoderencoderregister_source_vocabularysource_dictionaryregister_target_vocabularytarget_dictionaryrS   decoder_start_tokenadd_source_eos)
rV   r   rc   rY   rZ   
checkpointr8   spectaskr_   s
             r;   _loadzFairseqConverter._load   sy   ,>>8x114>>JK]]_ 4	  "\\%0!%!=!== $ J
 f%CE):7)CDDI%%1(,(>(>%t[)dnn!#NNBGG$4$4T^^$D"   ,#'#4#4   ,#'#4#4 "4(D==++D1DNN..tT:EJJL!!*W"56$ 0 L LM'LLMM+0 ((DOO)DE)),0KK,A,ADKK) (emmD'emmD//
4;Q;Q0RS//
4;Q;Q0RS226:DKK36:kk6K6KDKK315DKK.i4	 4	 4	s   K4MM)NNNFNF)	__name__
__module____qualname____doc__strr   boolrW   r    r=   r;   rD   rD   h   s    / &*%)*.*/"&', > >  > c]	 >
 c] > #3- > $( > 3- > !% >D?r=   rD   c                 6   t        | |       t        | j                  |j                        D ]  \  }}t	        ||        |j
                   t        | j
                  |j
                         |j                  !t        | j                  |j                         y y N)set_input_layersziplayerlayersset_transformer_encoder_layer
layer_normset_layer_normr   )r   module
layer_specr   s       r;   r|   r|      s    T6" V]]; 9
E%j%89$t(9(9:!!-t//1K1KL .r=   c                 *   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                         y y )Nra   )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   rb   r   r   s        r;   ru   ru      s    T6"t 8 89 V]]; 

E%#9	

 $t(9(9:!!-t//1K1KL(4??F$9$9:)4##V%;%;< *r=   c                     t        | j                  |j                         t        t	        | j
                  t              r| j
                  d   n| j
                  |j                         |j                  | _	        y )Nr   )
set_position_encodingsposition_encodingsembed_positionsset_embeddingsrt   
embeddingslistembed_tokensembed_scalescale_embeddingsr   r   s     r;   r   r      sX    422F4J4JK($?T__ #..Dr=   c                     t        | j                  |       t        | j                  |j                  d       t        | j                  j                  |j                         y NT)self_attention)set_ffnffnset_multi_head_attentionr   	self_attnr   r   self_attn_layer_normr   s     r;   r   r      sE    DHHfT00&2B2BSWX4&&1163N3NOr=   c                 b   t        | j                  |       t        | j                  |j                  d       t        | j                  j                  |j                         |rKt        | j                  |j                         t        | j                  j                  |j                         y y r   )r   r   r   r   r   r   r   r   	attentionencoder_attnencoder_attn_layer_norm)r   r   rb   s      r;   r   r      s{    DHHfT00&2B2BSWX4&&1163N3NO 1D1DEt~~00&2P2PQ r=   c                     t        | j                  |j                         t        | j                  |j
                         t        | j                  |j                         y r   )r   r   final_layer_normr   linear_0fc1linear_1fc2r   s     r;   r   r     s:    4??F$;$;<t}}fjj)t}}fjj)r=   c                    |rt        d      D cg c]  }t        j                          }}t        |d   |j                         t        |d   |j
                         t        |d   |j                         t        j                  | j                  d   |       nt        | j                  d   |j                         t        d      D cg c]  }t        j                          }}t        |d   |j
                         t        |d   |j                         t        j                  | j                  d   |       t        | j                  d   |j                         y c c}w c c}w )N   r         r   )ranger   
LinearSpecr   q_projk_projv_projr   fuse_linearlinearout_proj)r   r   r   _split_layerss        r;   r   r   
  s   :?(CQ..0CC<?FMM2<?FMM2<?FMM2$++a.,74;;q>6==1:?(CQ..0CC<?FMM2<?FMM2$++a.,7t{{20 D Ds   E"E'c                     |j                   j                         | _        |j                  j                         | _        y r   )weightnumpygammabiasbetar   s     r;   r   r     s*    $$&DJ!!#DIr=   c                     |j                   j                         | _         |j                   |j                  j                         | _        y y r   )r   r   r   r   s     r;   r   r     s9    --%%'DK{{KK%%'	 r=   c                 B    |j                   j                         | _         y r   )r   r   r   s     r;   r   r   %  s    --%%'DKr=   c                     dd l }t        ||j                  j                        r|j                  n|j
                  }|j                         |j                  dz   d  | _        y )Nr   r   )	rc   rt   nn	Embeddingr   weightsr   padding_idx	encodings)r   r   rc   r   s       r;   r   r   )  sG    (1C1CDV]]&..F\\^F$6$6$:$<=DNr=   c            
         t        j                  t         j                        } | j                  ddd       | j                  ddd       | j                  dd	
       | j                  dd
       | j                  dd
       | j                  dd
       | j                  ddd       | j                  ddd       t	        j
                  |        | j                         }t        |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                        }|j!                  |       y )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   z--unsafe_deserializationzuAllow loading legacy checkpoints with unsafe pickle deserialization. Only enable this option for trusted checkpoints.)rG   rH   rI   rJ   rK   rL   )re   ArgumentParserArgumentDefaultsHelpFormatteradd_argumentr   declare_arguments
parse_argsrD   rE   rF   rG   rH   rI   rJ   rK   rL   convert_from_args)parserr8   	converters      r;   mainr   0  st   $$ >>F MJ
L  
 6   8   ^   ^   %1	   "?	   'D $$$$.."&"@"@#::	I %r=   __main__)T)F)"re   rl   typingr   ctranslate2.convertersr    ctranslate2.converters.converterr   ctranslate2.specsr   r   r%   
ActivationGELUGELUTanhRELUSWISHr(   r<   rB   rD   r|   ru   r   r   r   r   r   r   r   r   r   r   r   r   r=   r;   <module>r      s     	  ( 6 ;  ""'' ++44''00""''##)) E
PVdy dNM=&/PR*1 $
((>6&r zF r=   