
    C1i0                        S SK r S SKrS SKJr  S SKJr  S SKJr  S SKJ	r	J
r
  1 Skr\	R                  R                  \	R                  R                  \	R                  R                  \	R                  R                  \	R                  R                   S.rS rS	 r " S
 S\5      rS rSS jrS rS rSS jrS rSS jrS rS rS rS rS r \!S:X  a  \ " 5         gg)    N)Optional)utils)	Converter)common_spectransformer_spec>   barttransformertransformer_lmtransformer_alignmultilingual_transformer)gelugelu_accurate	gelu_fastreluswishc                    SS K n[        U SS5      nUR                  R                  U R                     n[
        R                  " 5       nU" U[        ;   SU< SU R                  < SSR                  [        5      < S35        UR                  5         U" U[        ;   S	U< S
SR                  [        R                  5       5      < S35        U" [        U SS5      (       + S5        U" [        U SS5      (       + S5        US:X  a  U" U R                  (       + S5        U" U R                  (       + S5        UR                  5         [        R                  R!                  U R"                  U R$                  U R&                  [        U   [        U SS5      U R(                  U R*                  U R,                  :g  S9$ U" U R.                  U R&                  :H  S5        U" U R0                  U R$                  :H  S5        UR                  5         [        R2                  R!                  U R4                  U R"                  4U R0                  U R.                  [        U   [        U SS5      [        U SS5      [        U SS5      S9$ )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        X/home/james-whalen/.local/lib/python3.13/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                 T    U R                    Vs/ s H  oS:X  a  SOUPM     sn$ s  snf )Nz<pad>z<blank>)symbols)
dictionarytokens     r;   
_get_vocabrB   d   s,    BLBTBTUBT')Iu4BTUUUs   %c                   j    \ rS rSrSr     SS\S\S\\   S\\   S\\   S	\S
\\   4S jjrS r	Sr
g)FairseqConverterh   z%Converts models trained with Fairseq.N
model_pathdata_dirsource_langtarget_langfixed_dictionaryno_default_special_tokensuser_dirc                 X    Xl         X l        XPl        X0l        X@l        X`l        Xpl        g)aj  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)selfrF   rG   rH   rI   rJ   rK   rL   s           r;   __init__FairseqConverter.__init__k   s,    , &!!1''*C'!r=   c                    SS K nSS KnSSK Jn  U R                  (       a*  SSKJn  U" [        R                  " U R                  S95        UR                  5          UR                  U R                  UR                  S5      SS9nUS   =(       d    US	   S
   nU R                  Ul        U R                  b  U R                  Ul        [!        US5      (       ag  UR"                  (       aV  [$        R&                  R)                  U R                  [$        R&                  R+                  UR"                  5      5      Ul        U R,                  b  U R,                  Ul        U R0                  b  U R0                  Ul        [5        U5      nUR6                  R9                  U5      nUR:                  R=                  Xh5      n	U	R?                  5         U	RA                  US
   5        [C        U[D        RF                  5      (       az  [I        URJ                  U	RJ                  SS9  URM                  [O        URP                  5      5        URR                  (       d%  URT                  RV                  URT                  l,        O[[        UR\                  U	R\                  5        [I        URJ                  U	RJ                  5        UR_                  [O        UR`                  5      5        URc                  [O        URd                  5      5        U Rf                  (       a  S URT                  l4        O6URT                  RV                  URT                  l4        SURT                  l5        UsS S S 5        $ ! , (       d  f       g = f)Nr   )checkpoint_utils)import_user_module)rL   cpuF)map_locationweights_onlyr8   cfgmodel	lang_dictwith_encoder_attentionT)6r   torchrY   rT   fairseq.utilsrZ   argparse	Namespaceno_gradloadrN   devicerO   datarP   rJ   hasattrr`   ospathr&   basenamerQ   rH   rR   rI   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)
rU   r   rc   rY   rZ   
checkpointr8   spectaskr_   s
             r;   _loadFairseqConverter._load   si   ,>>8x114>>JK]]_  u||E/BQV $ J f%CE):7)CDDI%%1(,(>(>%t[))dnn!#NNBGG$4$4T^^$D"   ,#'#4#4   ,#'#4#4 "4(D==++D1DNN..t:EJJL!!*W"56$ 0 L LM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.e __s   LM&&
M4)rO   rP   rN   rS   rQ   rR   rT   )NNNFN)__name__
__module____qualname____firstlineno____doc__strr   boolrV   r   __static_attributes__ r=   r;   rD   rD   h   st    / &*%)*.*/"&"" " c]	"
 c]" #3-" $(" 3-"<=r=   rD   c                 8   [        X5        [        U R                  UR                  5       H  u  p#[	        X#5        M     UR
                  b   [        U R
                  UR
                  5        UR                  b!  [        U R                  UR                  5        g g N)set_input_layersziplayerlayersset_transformer_encoder_layer
layer_normset_layer_normr   )r   module
layer_specr   s       r;   r|   r|      su    T" V]];
%j8 <$t(9(9:!!-t//1K1KL .r=   c                 ,   [        X5        [        U R                  UR                  5        [	        U R
                  UR                  5       H  u  p4[        UUUS9  M     UR                  b   [        U R                  UR                  5        UR                  b   [        U R                  UR                  5        UR                  b   [        U R                  UR                  5        UR                  b!  [        U R                  UR                  5        g g )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    T"t 8 89 V]];
%#9	
 < $t(9(9:!!-t//1K1KL(4??F$9$9:)4##V%;%;< *r=   c                    [        U R                  UR                  5        [        [	        U R
                  [        5      (       a  U R
                  S   OU R
                  UR                  5        UR                  U l	        g )Nr   )
set_position_encodingsposition_encodingsembed_positionsset_embeddingsrt   
embeddingslistembed_tokensembed_scalescale_embeddingsr   r   s     r;   r   r      s[    422F4J4JK($??T__ #..Dr=   c                     [        U R                  U5        [        U R                  UR                  SS9  [        U R                  R                  UR                  5        g 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                 f   [        U R                  U5        [        U R                  UR                  SS9  [        U R                  R                  UR                  5        U(       aK  [        U R                  UR                  5        [        U R                  R                  UR                  5        g g 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                     [        U R                  UR                  5        [        U R                  UR
                  5        [        U R                  UR                  5        g 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                    U(       a  [        S5       Vs/ s H  n[        R                  " 5       PM     nn[        US   UR                  5        [        US   UR
                  5        [        US   UR                  5        [        R                  " U R                  S   U5        O[        U R                  S   UR                  5        [        S5       Vs/ s H  n[        R                  " 5       PM     nn[        US   UR
                  5        [        US   UR                  5        [        R                  " U R                  S   U5        [        U R                  S   UR                  5        g s  snf s  snf )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	   :?(C(Q..0(C<?FMM2<?FMM2<?FMM2$++a.,74;;q>6==1:?(C(Q..0(C<?FMM2<?FMM2$++a.,7t{{20 D Ds   E1E6c                     UR                   R                  5       U l        UR                  R                  5       U l        g r   )weightnumpygammabiasbetar   s     r;   r   r     s*    $$&DJ!!#DIr=   c                     UR                   R                  5       U l         UR                  b   UR                  R                  5       U l        g g r   )r   r   r   r   s     r;   r   r     s9    --%%'DK{{KK%%'	 r=   c                 B    UR                   R                  5       U l         g r   )r   r   r   s     r;   r   r     s    --%%'DKr=   c                     SS K n[        XR                  R                  5      (       a  UR                  OUR
                  nUR                  5       UR                  S-   S  U l        g )Nr   r   )	rc   rt   nn	Embeddingr   weightsr   padding_idx	encodings)r   r   rc   r   s       r;   r   r   #  sH    (1C1CDDV]]&..F\\^F$6$6$:$<=DNr=   c            
      F   [         R                  " [         R                  S9n U R                  SSSS9  U R                  SSSS9  U R                  SS	S
9  U R                  SSS
9  U R                  SSS
9  U R                  SSS
9  U R                  SSSS9  [        R
                  " U 5        U R                  5       n[        UR                  UR                  UR                  UR                  UR                  UR                  UR                  S9nUR                  U5        g )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   )rH   rI   rJ   rK   rL   )re   ArgumentParserArgumentDefaultsHelpFormatteradd_argumentr   declare_arguments
parse_argsrD   rF   rG   rH   rI   rJ   rK   rL   convert_from_args)parserr8   	converters      r;   mainr   *  sK   $$ >>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^y ^BM=&/PR*1 $
((>-&` zF r=   