o
    Ti0                     @   s  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d	 Zd
d ZG dd deZdd Zd)ddZdd Zdd Zd)ddZdd Zd*ddZdd Zd d! Zd"d# Zd$d% Zd&d' Z e!d(kre   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                 C   sj  dd l }t| dd}|jj| j }t }||tv d|| jdtf  |	  ||t
v d|dt
 f  |t| dd d	 |t| d
d d |dkr~|| j d || j d |	  tjj| j| j| jt
| t| dd| j| j| jkdS || j| jkd || j| jkd |	  tjj| j| jf| j| jt
| t| ddt| ddt| dddS )Nr   activation_fnr   zPModel '%s' used by architecture '%s' is not supported (supported models are: %s)z, zJOption --activation-fn %s is not supported (supported activations are: %s)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check r8   R/home/ubuntu/.local/lib/python3.10/site-packages/ctranslate2/converters/fairseq.py_get_model_spec   s|   







r:   c                 C   s   dd | j D S )Nc                 S   s   g | ]
}|d kr
dn|qS )z<pad>z<blank>r8   ).0tokenr8   r8   r9   
<listcomp>e   s    z_get_vocab.<locals>.<listcomp>)symbols)
dictionaryr8   r8   r9   
_get_vocabd      r@   c                   @   sZ   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dZd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                 C   s.   || _ || _|| _|| _|| _|| _|| _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)selfrC   rD   rE   rF   rG   rH   rI   r8   r8   r9   __init__k   s   
zFairseqConverter.__init__c           
      C   s  dd l }dd l}ddl m} | jr ddlm} |tj| jd |  |j	| j
|ddd}|d p:|d	 d
 }| j|_| jd urH| j|_t|dr^|jr^tj| jtj|j|_| jd urg| j|_| jd urp| j|_t|}|j|}|j||}	|	  |	 |d
  t!|t"j#rt$|j%|	j%dd |&t'|j( |j)s|j*j+|j*_,n0t-|j.|	j. t$|j%|	j% |/t'|j0 |1t'|j2 | j3rd |j*_4n
|j*j+|j*_4d|j*_5|W  d    S 1 sw   Y  d S )Nr   )checkpoint_utils)import_user_module)rI   cpuF)map_locationweights_onlyr5   cfgmodel	lang_dictwith_encoder_attentionT)6r   torchrS   rP   fairseq.utilsrT   argparse	Namespaceno_gradloadrJ   devicerK   datarL   rG   hasattrrZ   ospathr#   basenamerM   rE   rN   rF   r:   tasks
setup_taskr   build_modelevalload_state_dict
isinstancer   r)   set_transformer_decoderdecoderregister_vocabularyr@   r?   add_bos_tokenconfig	eos_token	bos_tokenset_transformer_encoderencoderregister_source_vocabularysource_dictionaryregister_target_vocabularytarget_dictionaryrO   decoder_start_tokenadd_source_eos)
rQ   r   r]   rS   rT   
checkpointr5   spectaskrY   r8   r8   r9   _load   s^   




$zFairseqConverter._load)NNNFN)	__name__
__module____qualname____doc__strr   boolrR   r   r8   r8   r8   r9   rB   h   s.    
rB   c                 C   sd   t | | t| j|jD ]	\}}t|| q|jd ur"t| j|j |jd ur0t| 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   r8   r8   r9   rv      s   


rv   Tc                 C   s   t | | t| j|j t| j|jD ]\}}t|||d q|jd ur+t	| j|j |j
d ur7t	| j
|j
 |jd urCt| j|j |jd urQt| 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   r\   r   r   r8   r8   r9   ro      s"   




ro   c                 C   s>   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_embeddingsrn   
embeddingslistembed_tokensembed_scalescale_embeddingsr   r   r8   r8   r9   r      s   r   c                 C   s2   t | j| t| j|jdd t| jj|j d S NT)self_attention)set_ffnffnset_multi_head_attentionr   	self_attnr   r   self_attn_layer_normr   r8   r8   r9   r      s   r   c                 C   sX   t | j| t| j|jd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   r\   r8   r8   r9   r      s   r   c                 C   s.   t | j|j t| j|j t| j|j d S r   )r   r   final_layer_normr   linear_0fc1linear_1fc2r   r8   r8   r9   r      s   r   Fc                 C   s   |r-dd t dD }t|d |j t|d |j t|d |j t| jd | n+t| jd |j dd t dD }t|d |j t|d |j t| jd | t| jd |j d S )	Nc                 S      g | ]}t  qS r8   r   
LinearSpecr;   _r8   r8   r9   r=         z,set_multi_head_attention.<locals>.<listcomp>   r         c                 S   r   r8   r   r   r8   r8   r9   r=     r   r   )	ranger   q_projk_projv_projr   fuse_linearlinearout_proj)r   r   r   split_layersr8   r8   r9   r     s   r   c                 C   s   |j  | _|j | _d S r   )weightnumpygammabiasbetar   r8   r8   r9   r     s   r   c                 C   s*   |j  | _ |jd ur|j | _d S d S r   )r   r   r   r   r8   r8   r9   r     s   
r   c                 C   s   |j  | _ d S r   )r   r   r   r8   r8   r9   r     rA   r   c                 C   s>   dd l }t||jjr|jn|j}| |jd d  | _d S )Nr   r   )	r]   rn   nn	Embeddingr   weightsr   padding_idx	encodings)r   r   r]   r   r8   r8   r9   r   #  s   r   c               	   C   s   t jt jd} | jdddd | jdddd | jdd	d
 | jddd
 | jddd
 | jddd
 | jdddd t|  |  }t|j|j	|j
|j|j|j|jd}|| 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   )rE   rF   rG   rH   rI   )r_   ArgumentParserArgumentDefaultsHelpFormatteradd_argumentr   declare_arguments
parse_argsrB   rC   rD   rE   rF   rG   rH   rI   convert_from_args)parserr5   	converterr8   r8   r9   main*  sT   
	r   __main__)T)F)"r_   rf   typingr   ctranslate2.convertersr    ctranslate2.converters.converterr   ctranslate2.specsr   r   r"   
ActivationGELUGELUTanhRELUSWISHr%   r:   r@   rB   rv   ro   r   r   r   r   r   r   r   r   r   r   r   r8   r8   r8   r9   <module>   s>    
	Ha

	
	
0
