
    Iui0                        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        P/opt/pipecat/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                   ^    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y)FairseqConverterz%Converts models trained with Fairseq.N
model_pathdata_dirsource_langtarget_langfixed_dictionaryno_default_special_tokensuser_dirc                 f    || _         || _        || _        || _        || _        || _        || _        y)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)selfrE   rF   rG   rH   rI   rJ   rK   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                               |j                         5  |j                  | j                  |j                  d      d      }|d   xs |d	   d
   }| j                  |_        | j                  | j                  |_        t!        |d      rb|j"                  rVt$        j&                  j)                  | j                  t$        j&                  j+                  |j"                              |_        | j,                  | j,                  |_        | j0                  | j0                  |_        t5        |      }|j6                  j9                  |      }|j:                  j=                  ||      }	|	j?                          |	jA                  |d
          tC        |tD        jF                        rytI        |jJ                  |	jJ                  d       |jM                  tO        |jP                               |jR                  s|jT                  jV                  |jT                  _,        nt[        |j\                  |	j\                         tI        |jJ                  |	jJ                         |j_                  tO        |j`                               |jc                  tO        |jd                               | jf                  rd |jT                  _4        n6|jT                  jV                  |jT                  _4        d|jT                  _5        |cd d d        S # 1 sw Y   y xY w)Nr   )checkpoint_utils)import_user_module)rK   cpuF)map_locationweights_onlyr8   cfgmodel	lang_dictwith_encoder_attentionT)6r   torchrW   rS   fairseq.utilsrX   argparse	Namespaceno_gradloadrM   devicerN   datarO   rI   hasattrr^   ospathr&   basenamerP   rG   rQ   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_dictionaryrR   decoder_start_tokenadd_source_eos)
rT   r   ra   rW   rX   
checkpointr8   spectaskr]   s
             r;   _loadzFairseqConverter._load   so   ,>>8x114>>JK]]_ 2	  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..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.e2	 2	 2	s   K)M

M)NNNFN)	__name__
__module____qualname____doc__strr   boolrU   r    r=   r;   rD   rD   h   st    / &*%)*.*/"&"" " c]	"
 c]" #3-" $(" 3-"<=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;   rz   rz      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 )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   r`   r   r   s        r;   rs   rs      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_embeddingsrr   
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   r`   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   )	ra   rr   nn	Embeddingr   weightsr   padding_idx	encodings)r   r   ra   r   s       r;   r   r   #  sG    (1C1CDV]]&..F\\^F$6$6$:$<=DNr=   c            	      x   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       t	        j
                  |        | j                         }t        |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   )rG   rH   rI   rJ   rK   )rc   ArgumentParserArgumentDefaultsHelpFormatteradd_argumentr   declare_arguments
parse_argsrD   rE   rF   rG   rH   rI   rJ   rK   convert_from_args)parserr8   	converters      r;   mainr   *  sK   $$ >>F MJ
L  
 6   8   ^   ^   %1	   'D $$$$.."&"@"@I %r=   __main__)T)F)"rc   rj   typingr   ctranslate2.convertersr    ctranslate2.converters.converterr   ctranslate2.specsr   r   r%   
ActivationGELUGELUTanhRELUSWISHr(   r<   rB   rD   rz   rs   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=   