
    n i=                     (   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZ ej        j        ej        j        ej        j        dZ G d de	          Z G d	 d
          Z G d de          Zd ZddZd Zedk    r e             dS dS )    N)OptionalUnion)utils)	Converter)common_spectransformer_spec)gelureluswishc                   v    e Zd ZdZe	 	 	 ddeeef         dede	e         de	e         fd            Z
d	 Zd
 ZdS )OpenNMTTFConverterzConverts OpenNMT-tf models.FNconfigauto_configcheckpoint_pathmodelc                    ddl m} ddlm} t	          |t
                    r|                    |g          }nt          j        |          }||	                    |d                   }nJt          j                            |          r|                    |          }n|                    |          }|r(|                    ||                                           |                    |d         |d                   }|                    |           |                    ||          }|                    |          }|t+          d          |                                  | |          S )	a  Creates the converter from the configuration.

        Arguments:
          config: Path to the YAML configuration, or a dictionary with the loaded configuration.
          auto_config: Whether the model automatic configuration values should be used.
          checkpoint_path: Path to the checkpoint or checkpoint directory to load. If not set,
            the latest checkpoint from the model directory is loaded.
          model: If the model instance cannot be resolved from the model directory, this argument
             can be set to either the name of the model in the catalog or the path to the model
             configuration.

        Returns:
          A :class:`ctranslate2.converters.OpenNMTTFConverter` instance.
        r   )r   )
CheckpointN	model_dirdata)r   zNo checkpoint was restored)opennmtr   opennmt.utils.checkpointr   
isinstancestrload_configcopydeepcopy
load_modelospathexistsload_model_from_fileload_model_from_catalogmerge_configr   try_prefix_paths
initializefrom_configrestoreRuntimeErrorcreate_variables)	clsr   r   r   r   config_utilr   data_config
checkpoints	            a/var/www/html/auto_sub_bot/venv/lib/python3.11/site-packages/ctranslate2/converters/opennmt_tf.pyr&   zOpenNMTTFConverter.from_config   sz   , 	211111777777fc"" 	+ ,,fX66FF]6**F=**6++>??EEW^^E"" 	?44U;;EE77>>E 	B$$VU->->-@-@AAA!226+3FvWW%%%++FE::
$,,_,MM";<<<   s5zz    c                     || _         dS )zInitializes the converter.

        Arguments:
          model: An initialized and fully-built ``opennmt.models.Model`` instance.
        N)_model)selfr   s     r.   __init__zOpenNMTTFConverter.__init__H   s     r/   c                     dd l }t          | j        |j        j                  rt                      }nt                      } || j                  S Nr   )r   r   r1   modelsLanguageModelTransformerDecoderSpecBuilderTransformerSpecBuilder)r2   r   spec_builders      r.   _loadzOpenNMTTFConverter._loadP   sN    dk7>#?@@ 	48::LL133L|DK(((r/   )FNN)__name__
__module____qualname____doc__classmethodr   r   dictboolr   r&   r3   r;    r/   r.   r   r      s        %% ")-#0 0c4i 0 0 "#	0
 }0 0 0 [0d  ) ) ) ) )r/   r   c                   L    e Zd Zd Zd Zd Zd ZddZd Zd Z	d	 Z
d
 Zd ZdS )r9   c           
      B	   dd l t          j                    } |t          |j        j                  d           |                                  |t          |j        j        j	                  d            |t          |j
        j        j        j        j        f          d           |                                 |j        j        d         j        j        }|j        j        d         j        j        }|j        d u}|j        j        j        } ||t,          v d|dd                    t,                                                    d            ||t3          |j        j                  k    d	            ||j        j        j        j        j        k    d
           t?          |j
                  }t?          |j                   }tC          |          }	|	dk    rtD          j#        j$        }
n|j
        j%        }d }
|Wt          |j        j&                  rtD          j#        j$        }
n+t          |j        j'                  rtD          j#        j(        }
 |tS          fd|D                       d            ||
d ud|z             d}d}|j        j        j        j        j*        k    rd}|                                 tW          j,        tC          |j        j                  |j        j        d         j        j        j-        |j        j.        d ut,          |         |	|
|          }tW          j/        tC          |j        j                  |j        j        d         j        j        j-        |j        j.        d ut,          |         |||          }tW          j0        ||          }t3          |d         j1                  |j2        _3        t3          |d         j4                  |j2        _5        |D ])}|6                    to          |j8                             *|D ])}|9                    to          |j8                             *| :                    |j        |j        |j
                   | ;                    |j        |j        |j                    |S )Nr   z%Only Transformer models are supportedz#Parallel encoders are not supportedz<Source inputter must be a WordEmbedder or a ParallelInputterActivation . is not supported (supported activations are: , )z^Relative position representation and position encoding cannot be both enabled or both disabledz<Averaging all multi-head attention matrices is not supported   c              3   L   K   | ]}t          |j        j                  V  d S N)r   	inputtersWordEmbedder).0inputterr   s     r.   	<genexpr>z2TransformerSpecBuilder.__call__.<locals>.<genexpr>   sG          x):)GHH     r/   z'All source inputters must WordEmbeddersz!Unsupported embeddings reducer %s)pre_norm
activationnum_source_embeddingsembeddings_mergerelative_position)rS   rT   rW   alignment_layeralignment_heads)<r   r   ConfigurationCheckerr   r6   TransformervalidateencoderencodersSelfAttentionEncoderfeatures_inputterrM   rN   ParallelInputterlayersself_attentionlayerffnmaximum_relative_positioninnerrT   r<   _SUPPORTED_ACTIVATIONSjoinkeysrB   position_encoderdecoderattention_reductionMultiHeadAttentionReductionAVERAGE_ALL_LAYERS_get_inputterslabels_inputterlenr   EmbeddingsMergeCONCATreducerConcatReducer
SumReducerADDallAVERAGE_LAST_LAYERr   TransformerEncoderSpec	num_heads
layer_normTransformerDecoderSpecTransformerSpec
mark_startr   add_source_bosmark_endadd_source_eosregister_source_vocabulary_load_vocabvocabulary_fileregister_target_vocabularyset_transformer_encoderset_transformer_decoder)r2   r   checkmhare   with_relative_positionactivation_namesource_inputterstarget_inputtersrU   rV   ru   rX   rY   encoder_specdecoder_specspecrP   r   s                     @r.   __call__zTransformerSpecBuilder.__call__\   s   *,,ugn8993	
 	
 	
 	u}g&6&KLL1	
 	
 	
 	'"/1B1ST  K	
 	
 	
 	m"1%4:m"1%)/!$!>d!J).7555		*@*E*E*G*G H H H HJ	
 	
 	

 	"d5=+I&J&JJ	
 	
 	

 	M-~9LMJ	
 	
 	
 *%*ABB)%*?@@ #$4 5 5 A%%*:A-5G#"gw~'CDD G'2'B'I$$)BCC G'2'B'F$E    $4     :   E ,3g=  
 M-~9LM M  O'>$%%M #28B]-T9-o>"7-4
 
 
 (>$%%M #28B]-T9-o>4++
 
 
  /lKK%)*:1*=*H%I%I"%)*:1*=*F%G%G"( 	S 	SH++K8P,Q,QRRRR( 	S 	SH++K8P,Q,QRRRR$$LM#	
 	
 	

 	$$LM!	
 	
 	
 r/   c                    t          |j        t          |                    D ]\  }}|                     ||           |j         |                     |j        |j                   t          |j        |j                  D ]G\  }}| 	                    |j
        |j
        d           |                     |j        |j                   H|j        "|                     |j        |j                   d S d S )NTrc   )zip
embeddingsrp   set_embeddingsrk   set_position_encodingsposition_encodingsrd   rb   set_multi_head_attentionrc   set_ffnre   r}   set_layer_norm)r2   r   modulerP   embedding_spec
layer_specrd   s          r.   r   z.TransformerSpecBuilder.set_transformer_encoder   s   (+DO^H=U=U(V(V 	: 	:$NH9999".''''  
 "%TZ!?!? 	4 	4J)))$# *    LL3333(1BCCCCC )(r/   c                 :   |                      |j        |           |j         |                     |j        |j                   t          |j        |j                  D ]v\  }}|                     |j	        |j	        d           |j
        r(|                     |j
        |j
        d         d           |                     |j        |j                   w|j         |                     |j        |j                   |                     |j        |j                   d S )NTr   r   F)r   r   rk   r   r   r   rd   rb   r   rc   	attentionr   re   r}   r   
set_linear
projectionoutput_layer)r2   r   r   rP   r   rd   s         r.   r   z.TransformerSpecBuilder.set_transformer_decoder   s3   DOX666".''''  
 "%TZ!?!? 	4 	4J)))$# *     --(OA&#( .    LL3333(1BCCC)<=====r/   c                     |                      |j        |j        j                   |                      |j        |j        j                   |                     |j        |           d S rL   )r   linear_0rd   rg   linear_1outerset_layer_norm_from_wrapperr}   r2   r   r   s      r.   r   zTransformerSpecBuilder.set_ffn  sV    v|'9:::v|'9:::((&AAAAAr/   Fc                 2   d t          d          D             }|                     |d         |j        j                   |                     |d         |j        j                   |                     |d         |j        j                   |rst          j        |j        d         |           |j        j	        F|j        j
                                        |_
        |j        j                                        |_        nPt          j        |j        d         |d d                    t          j        |j        d         |dd                     |                     |j        d         |j        j                   |                     |j        |           d S )Nc                 4    g | ]}t          j                    S rC   )r   
LinearSpec)rO   _s     r.   
<listcomp>zCTransformerSpecBuilder.set_multi_head_attention.<locals>.<listcomp>  s!    CCCQ.00CCCr/      r   rJ      rR   )ranger   rd   linear_querieslinear_keyslinear_valuesr   fuse_linearlinearrf   relative_position_keysnumpyrelative_position_valueslinear_outputr   r}   )r2   r   r   rc   split_layerss        r.   r   z/TransformerSpecBuilder.set_multi_head_attention  s]   CC%((CCCQ)DEEEQ)ABBBQ)CDDD 	@dk!nl;;;|5AL7==?? + L9??AA - dk!nl2A2.>???dk!nl122.>???B)CDDD((&AAAAAr/   c                 X    |                      ||j        |j        n|j                   d S rL   )r   input_layer_normoutput_layer_normr   s      r.   r   z2TransformerSpecBuilder.set_layer_norm_from_wrapper#  sC     *2 ((,	
 	
 	
 	
 	
r/   c                 ~    |j                                         |_         |j                                        |_        d S rL   )gammar   betar   s      r.   r   z%TransformerSpecBuilder.set_layer_norm-  s0    \''))
K%%''			r/   c                     |j                                         |_        |j        s|j                                        |_        |j         |j                                        |_        d S d S rL   )kernelr   weight	transposebiasr   s      r.   r   z!TransformerSpecBuilder.set_linear1  s`    m))++ 	2+//11DK;"))++DIII #"r/   c                 B    |j                                         |_        d S rL   )	embeddingr   r   r   s      r.   r   z%TransformerSpecBuilder.set_embeddings8  s    &,,..r/   c                     dd l }t          ||j        j                  r(|j                                        dd          |_        d S d S )Nr   rJ   )r   r   rb   PositionEmbedderr   r   	encodings)r2   r   r   r   s       r.   r   z-TransformerSpecBuilder.set_position_encodings;  sO    fgn=>> 	:#-3355abb9DNNN	: 	:r/   N)F)r<   r=   r>   r   r   r   r   r   r   r   r   r   r   rC   r/   r.   r9   r9   [   s        x x xtD D D*> > >:B B B
B B B B,
 
 
( ( (, , ,/ / /: : : : :r/   r9   c                       e Zd Zd ZdS )r8   c           
      @   dd l }t          j                    } |t          |j        |j        j                  d           |                                 |j        j        d         j	        j
        }|j        j        d         j        j
        }|j        j        j        } ||t          v d|dd                    t                                                    d           |                                 t$          j                            t+          |j        j                  |j        |j        j        d ut          |                   }|                    t3          |j        j                             |                     |j        |j        |j                   |S )Nr   z*Only self-attention decoders are supportedrF   rG   rH   rI   )rS   rT   )r   r   rZ   r   rl   decodersSelfAttentionDecoderr\   rb   rc   rd   re   rg   rT   r<   rh   ri   rj   r   TransformerDecoderModelSpecr&   rr   r|   r}   register_vocabularyr   r`   r   r   )r2   r   r   r   r   re   r   r   s           r.   r   z&TransformerDecoderSpecBuilder.__call__C  s   *,,u}g&6&KLL8	
 	
 	
 	m"1%4:m"1%)/).7555		*@*E*E*G*G H H H HJ	
 	
 	

 	;GG$%%M]-T9-o>	 H 
 
 	  U-D-T!U!UVVV$$LM#	
 	
 	
 r/   N)r<   r=   r>   r   rC   r/   r.   r8   r8   B  s#        # # # # #r/   r8   c                 R    dd l }t          | |j        j                  r| j        n| gS r5   )r   r   rM   MultiInputter)rP   r   s     r.   rp   rp   i  s7    NNN h 1 ?@@	Zr/   <unk>c                 z   dd l }t          | |j        j                  rt	          | j                  }nnt          | t                    rt	          |           }nIt          | t                    r%|j        j                            |           j        }nt          d          ||vr|	                    |           |S )Nr   zInvalid vocabulary type)
r   r   r   Vocablistwordsr   	from_file	TypeErrorappend)vocab	unk_tokenr   tokenss       r.   r   r   s  s    NNN%+,, 3ek""	E4	 	  3e	E3		 3#--e44:1222i   Mr/   c                  J   t          j        t           j                  } |                     dd           |                     ddd           |                     d	d
           |                     dd           |                     dd           |                     dd           t	          j        |            |                                 }|j        }|s|j        r|j	        r|j
        st          d          t          j                            |j                  r|j        n#t          j                            |j                  }||j	        |j
        dd}t                               ||j        |j        |j                  }|                    |           d S )N)formatter_classz--configzPath to the YAML configuration.)helpz--auto_config
store_truez-Use the model automatic configuration values.)actionr   z--model_pathz}Path to the checkpoint or checkpoint directory to load. If not set, the latest checkpoint from the model directory is loaded.z--model_typezIf the model instance cannot be resolved from the model directory, this argument can be set to either the name of the model in the catalog or the path to the model configuration.z--src_vocabzDPath to the source vocabulary (required if no configuration is set).z--tgt_vocabzDPath to the target vocabulary (required if no configuration is set).z[Options --model_path, --src_vocab, --tgt_vocab are required when a configuration is not set)source_vocabularytarget_vocabulary)r   r   )r   r   r   )argparseArgumentParserArgumentDefaultsHelpFormatteradd_argumentr   declare_arguments
parse_argsr   
model_path	src_vocab	tgt_vocab
ValueErrorr   r   isdirdirnamer   r&   r   
model_typeconvert_from_args)parserargsr   r   	converters        r.   mainr     s   $ >  F 
)JKKK
<    
 H     6     S     S     '''D[F 
 	dn 	DN 	2   w}}T_--2DOO11 	 #%)^%)^ 
 
 #..$o	 /  I %%%%%r/   __main__)r   )r   r   r   typingr   r   ctranslate2.convertersr    ctranslate2.converters.converterr   ctranslate2.specsr   r   
ActivationGELUTanhRELUSWISHrh   r   r9   r8   rp   r   r   r<   rC   r/   r.   <module>r     s     				 " " " " " " " " ( ( ( ( ( ( 6 6 6 6 6 6 ; ; ; ; ; ; ; ; "+"'#)  F) F) F) F) F) F) F) F)Rd: d: d: d: d: d: d: d:N$ $ $ $ $$: $ $ $N     "?& ?& ?&D zDFFFFF r/   