
    \j"                     0   d Z ddlmZmZmZ ddlZddlmZm	Z	m
Z
  G d de
j                        Z G d de
j                        Z G d	 d
e
j                        Z G d de
j                        Z G d de
j                        Z G d de
j                        Z G d de
j$                        Z G d de
j(                        Z G d de
j,                        Z G d de
j0                        Z G d de
j,                        Z G d de
j0                        Zy)z0Declares specification of the Transformer model.    )OptionalTupleUnionN)attention_speccommon_spec
model_specc            /       L   e Zd Zddej                  j
                  dej                  j                  ddddddddddddddddfdedede	d	e	d
ej                  dedej                  de	de	de	de	de	de	de
e   de
e   de
e   de	de
ej                     dedede
e   de
e	   de	f.dZy)TransformerEncoderSpecTF   N'  
num_layers	num_headspre_normno_final_norm
activationnum_source_embeddingsembeddings_mergelayernorm_embeddingrelative_positionrelative_attention_biasffn_glurms_normmulti_query_attentionnum_heads_kvhead_dim
rotary_dimrotary_interleaverotary_scaling_typerotary_scaling_factorrotary_basesliding_windowqk_normpre_post_layer_normc                 "   |r||dk7  rt        d      d}|| _        t        j                  d      j	                  |      | _        || _        t        j                  d      j	                  |      | _        t        j                  d      j	                  |      | _        t        |      D cg c]  }t        j                          c}| _        d| _        |	s|
st               | _        |r|st        j                   |      | _        |rt        j                   |      | _        |)t        j                  d      j	                  |      | _        t        |      D cg c]  }t)        |	|
||||||||||||	       c}| _        yc c}w c c}w )
a'  Initializes a Transformer encoder specification.

        Args:
          num_layers: Number of layers.
          num_heads: Number of attention heads.
          pre_norm: Enable the pre-norm Transformer architecture.
          no_final_norm: Disable the final layer norm in the pre-norm architecture.
          activation: Activation to apply in the feed-forward network.
          num_source_embeddings: Number of source embeddings.
          embeddings_merge: When :obj:`num_source_embeddings` > 1, specify how the
            embeddings are merged.
          layernorm_embedding: Apply layer normalization after the embedding layer.
          relative_position: Use relative position representations in the self-attention
            layers as described in https://arxiv.org/abs/1803.02155.
          relative_attention_bias: Use relative attention bias in the self-attention
            layers as described in the T5 paper https://arxiv.org/abs/1910.10683.
          ffn_glu: Use gated linear units in the FFN layers as described in
            https://arxiv.org/abs/2002.05202.
          rms_norm: Use the root mean square layer normalization.
          multi_query_attention: Use multi-query attention (alias for num_heads_kv=1).
          num_heads_kv: Number of attention heads for the key and value.
          head_dim: Number of dimensions per attention head.
          rotary_dim: Apply rotary embeddings to these first N dimensions. If 0, rotary
            embeddings are applied to all dimensions.
          rotary_interleave: Interleave the head dimensions when rotary embeddings are applied.
            Otherwise the head dimensions are sliced in half.
          rotary_scaling_type: Type of RoPE scaling.
          rotary_scaling_factor: Factor used in the RoPE scaling.
          rotary_base: The base period of the rotary embeddings.
          sliding_window: Max sequence length to retain in KV Cache.
          qk_norm: Apply layer normalization to the query and key projections.
          pre_post_layer_norm: Add post layer norm for each pre norm layer.
        Nr   5Enabling multi_query_attention implies num_heads_kv=1int16int8Tr   int32)r   r   r   r   r   r   r!   r   r   r   r   r    r"   r#   )
ValueErrorr   npdtypetyper   r   r   r   ranger   EmbeddingsSpec
embeddingsscale_embeddingsPositionEncoderSpecposition_encodingsLayerNormSpec
layer_normr   r!   TransformerEncoderLayerSpeclayer)selfr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   _s                            U/root/.hermes/venv/lib/python3.12/site-packages/ctranslate2/specs/transformer_spec.py__init__zTransformerEncoderSpec.__init__   sz   x !'LA,= K  L%:"'*//	: ((6*//
; " 0 5 56F G278M2N
-.K&&(
 !% )@&9&;D#M)77JDO'2'@'@('SD$%"$((7"3"8"8"HD& :&#
" ! ("3(?!)!-%"3$7&;'$7



s   -F F)__name__
__module____qualname__r   
ActivationRELUEmbeddingsMergeCONCATintboolr   r   RotaryScalingTypefloatr;        r:   r
   r
   
   s   
 #-8-C-C-H-H%&8C8S8S8Z8Z$)"'(-&+&*"&$("&JN'("(,"'$)1g
g
 g
 	g

 g
  **g
  #g
 &55g
 "g
  g
 "&g
 g
 g
  $g
 smg
  3-!g
" SM#g
$  %g
& &n&F&FG'g
(  %)g
* +g
, !-g
. $/g
0 "1g
rH   r
   c            M          e Zd Zdej                  j
                  ddddddddddddddddddddddddddddddddddf$ded	ed
edej                  dedededededededededededededee   dedee	j                     dedededed ed!ed"ed#ed$ee   d%ee   d&ee   d'eej                     d(ee   d)ee   d*ed+ed,ee   d-efLd.Zed/        Zy)0TransformerDecoderSpecTFr   Nr   r   r   r   r   r   r   with_encoder_attentionr   project_in_outr   r   alignment_layeralignment_headsr   r   alibialibi_use_positive_positionsscale_alibir   r   r   r   r     original_max_position_embeddingsmax_position_embeddingsparallel_residualshared_layer_normr#   r   r   r   r!   
quant_typequant_group_size
quant_bitsr"   v_norm external_pre_post_encoder_layersmerged_encoder_attentionc'           	         t               | _        |r|st        d      |rt        d      |r||dk7  rt        d      d}t        j                  d      j                  |      | _        || _        t        j                  d      j                  |      | _        t        j                  d      j                  |      | _	        t        j                  d      j                  |      | _
        t        j                         | _        d| _        t        j                   | _        || _        || _        || _        |)t        j                  d	      j                  |      | _        |	s|
s|s|t-               | _        |r|st        j0                  |
      | _        |rt        j0                  |
      | _        t        j6                         | _        t;        |      D 'cg c]M  }'t=        d&i d|d|	d|
d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|#d|$d|%d |&O c}'| _        d!| _         t        j                   | _!        |xs ||k7  | j                  d"<   |r2t        j6                         | _"        t        j6                         | _#        | r.| | j                  d#<   |"| j                  d$<   |!| j                  d%<   yyc c}'w )'a.  Initializes a Transformer decoder specification.

        Args:
          num_layers: Number of layers.
          num_heads: Number of attention heads.
          pre_norm: Enable the pre-norm Transformer architecture.
          activation: Activation to apply in the feed-forward network.
          layernorm_embedding: Apply layer normalization after the embedding layer.
          with_encoder_attention: Enable the encoder attention sublayers.
          no_final_norm: Disable the final layer norm in the pre-norm architecture.
          project_in_out: Add linear transformations after the embedding layer and before
            the final layer.
          relative_position: Use relative position representations in the self-attention
            layers as described in https://arxiv.org/abs/1803.02155.
          relative_attention_bias: Use relative attention bias in the self-attention
            layers as described in the T5 paper https://arxiv.org/abs/1910.10683.
          alignment_layer: Layer index selected for alignment.
          alignment_heads: Number of attention heads selected for alignment.
          ffn_glu: Use gated linear units in the FFN layers as described in
            https://arxiv.org/abs/2002.05202.
          rms_norm: Use the root mean square layer normalization.
          alibi: Use attention with linear biases.
          alibi_use_positive_positions: Use positive positions in the ALiBi definition.
          scale_alibi: Apply the dot product scale factor to ALiBi.
          rotary_dim: Apply rotary embeddings to these first N dimensions. If 0, rotary
            embeddings are applied to all dimensions.
          rotary_interleave: Interleave the head dimensions when rotary embeddings are applied.
            Otherwise the head dimensions are sliced in half.
          rotary_scaling_type: Type of RoPE scaling.
          rotary_scaling_factor: Factor used in the RoPE scaling.
          rotary_base: The base period of the rotary embeddings.
          original_max_position_embeddings: The original max position embeddings
            for Su rope embeddings
          max_position_embeddings: The max position embeddings for Su rope embeddings
          parallel_residual: Use parallel residual connections in each layer block, as used
            by the GPT-J and GPT-NeoX models.
          shared_layer_norm: When using parallel residual, share the input and post
            attention layer norms.
          pre_post_layer_norm: Add post layer norm for each pre norm layer
          multi_query_attention: Use multi-query attention (alias for num_heads_kv=1).
          num_heads_kv: Number of attention heads for the key and value.
          sliding_window: Max sequence length to retain in KV Cache.
          quant_type: quantization type used (like awq... for lower bit quantization)
          quant_group_size: group size of the lower bit quantization
          quant_bits: number of bit of the quantization (ex: 4bit)
          external_pre_post_encoder_layers: if the encoder attention pre and processing
            is done outside the attention.
        z/The GPT-J block expects a pre-norm architecturez-The GPT-J block does not have cross attentionNr   r%   r&   r'   Tr)   r(   rL   r   r   r   r   r   r   r   r   r    rS   rT   rU   rV   r#   r   r   r!   r"   rZ   r[   r\   Fr   quantization_typequantization_bitsquantization_group_sizerG   )$dict_configr*   r+   r,   r-   r   r   r   rN   rO   r   r/   r0   r1   r   OPTIONALscale_outputsrP   rQ   rR   r!   r2   r3   r4   r5   r   
LinearSpec
projectionr.   TransformerDecoderLayerSpecr7   start_from_zero_embeddingfinal_logit_softcapping
project_inproject_out)(r8   r   r   r   r   r   rL   r   rM   r   r   rN   rO   r   r   rP   rQ   rR   r   r   r   r   r    rS   rT   rU   rV   r#   r   r   r   r!   rW   rX   rY   r"   rZ   r[   r\   r9   s(                                           r:   r;   zTransformerDecoderSpec.__init__v   s   t v !RSS% !PQQ 'LA,= K  L'*//	: ((6*//
;!xx055oF!xx055oF%446 $'00
,H)&%"$((7"3"8"8"HD!+"&9&;D#M)77JDO'2'@'@('SD$%0024 :&3
2 1 ( '="3 )@  	
 " & #4 %8 '< ( 2R )@ #4 #4 %8  *!" "#$  .%&  '( )* 2R+, *B-

8 */&'1':':$0E 1
I% 	,- )446DO*557D0:DLL,-0:DLL,-6FDLL23 M
s   AKc                     | j                   S N)rb   r8   s    r:   configzTransformerDecoderSpec.config"  s    ||rH   )r<   r=   r>   r   r?   r@   rC   rD   r   r   rE   rF   Quantizationr;   propertyro   rG   rH   r:   rJ   rJ   u   s   
 -8-C-C-H-H$)'+#$"'(-! -2!$("&JN'("01'("'"'$)&+&*"&(,9=*.$(;@).OjGjG jG 	jG
  **jG "jG !%jG jG jG  jG "&jG jG jG jG jG  !jG" '+#jG$ %jG& SM'jG(  )jG* &n&F&FG+jG,  %-jG. /jG0 +.1jG2 "%3jG4  5jG6  7jG8 "9jG:  $;jG< sm=jG> 3-?jG@ !AjGB [556CjGD #3-EjGF SMGjGH IjGJ KjGL +34.MjGN #'OjGX  rH   rJ   c                   j    e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	dee   dedeej                     dededefdZ	y)
r6   Nr   r   r   r   r    r#   c                    t        j                  d||||||||	|
|||      | _        t        ||      | _        |rt        j                  |      | _        t        j                  |      | _        t        j                  |      | _	        t        j                  |      | _
        t        | j                  d       t        | j                  d       y y )NT)self_attentionr   r   r   r   r   r!   r   r   r   r   r    r"   glur   r(   r5   )r   MultiHeadAttentionSpecrt   FeedForwardSpecffnr   r4   input_layer_normpost_attention_layer_normpre_feedforward_layer_normpost_feedforward_layer_normdelattr)r8   r   r   r   r   r   r   r!   r   r   r   r   r    r"   r#   s                  r:   r;   z$TransformerEncoderLayerSpec.__init__(  s    " -CC/$;%)!/ 3"7#
 #wB$/$=$=x$PD!-8-F-F!.D* /:.G.G!/D+ 0;/H/H!0D, D''6DHHl+ rH   )FFFFNNNNTNr   r   FF)
r<   r=   r>   r   rC   rD   r   rE   rF   r;   rG   rH   r:   r6   r6   '  s~       %$("&JN'("$)/, SM/,  /, &n&F&FG/,  %/, /, "/,rH   r6   c                   @    e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)rg   Nc           	         t        j                  di ddd|d|d|d|d|d|d	|	d
|
d|d|d|d|d|d|d|d|| _        |r$|s"t        j                  ||||||du       | _        t	        ||      | _        |rz|rt        j                         | _        n2t        j                         | _	        t        j                         | _
        t        | j                  d       t        | j
                  d       |rt        j                  |      | _	        t        j                  |      | _
        |r8|r6t        j                  |      | _        t        j                  |      | _        t        j                  |      | _        t        j                  |      | _        t        | j                  d       t        | j
                  d       t         j"                  | _        y )Nrt   Tr   r   r   r   r   r   r   r    rS   rT   r   r   r!   r"   rZ   r\   F)r   r   r   r!   r"   has_normru   r5   r(   rG   )r   rw   rt   	attentionrx   ry   r   r4   rV   rz   r{   r~   *external_post_encoder_attention_layer_norm)external_pre_encoder_attention_layer_normr|   r}   r   rc   layer_scalar)r8   rL   r   r   r   r   r   r   r   r   r    rS   rT   rU   rV   r#   r   r   r!   r"   rZ   r[   r\   s                          r:   r;   z$TransformerDecoderLayerSpec.__init__[  s   2 -CC 

/
 %<
 	

 "
 0
 !4
 #8
 $
 .N
 %<
 &
 
 *
 
  !
" &>#
( "*B+BB!)!-9UBDN #wB )4)B)B)D&(3(A(A(C%1<1J1J1L.D''6DHHl+$/$=$=x$PD!-8-F-F!.D* &*J--x@ ?  --x@ >
 /:.G.G!/D+ 0;/H/H!0D, D''6DHHl+&//rH   )TFFFFNTNr   r   r   r   FFFNNNFFFFr<   r=   r>   r;   rG   rH   r:   rg   rg   Z  sL      $ % )* !!).!&/]0rH   rg   c                       e Zd ZddZy)rx   c                     t        j                  |      | _        t        j                         | _        t        j                         | _        |rt        j                         | _        y y )Nr(   )r   r4   r5   re   linear_0linear_1linear_0_noact)r8   rv   r   s      r:   r;   zFeedForwardSpec.__init__  sM    %33XF#..0#..0"-"8"8":D rH   N)FFr   rG   rH   r:   rx   rx     s    ;rH   rx   c                       e Zd Zd Zy)r2   c                 .    t         j                  | _        y rm   )r   rc   	encodingsrn   s    r:   r;   zPositionEncoderSpec.__init__  s    #,,rH   Nr   rG   rH   r:   r2   r2     s    -rH   r2   c                   0     e Zd ZdZddee   f fdZ xZS )TransformerConfigz%Configuration for Transformer models.layer_norm_epsilonc                 (    t        |   dd|i| y)zInitializes the configuration for Transformer models.

        Args:
          layer_norm_epsilon: The layer norm epsilon value.
          **kwargs: Additional configuration.
        r   NrG   superr;   r8   r   kwargs	__class__s      r:   r;   zTransformerConfig.__init__       	I,>I&IrH   rm   r<   r=   r>   __doc__r   rF   r;   __classcell__r   s   @r:   r   r     s    /J8E? J JrH   r   c                    T    e Zd ZdZdedef fdZedddej                  j                  dddej                  j                  dddddfd	eeeeef   f   d
ededededej                  dedededej                  dededededefd       Zed        Zed        Zd Zd Zd Z xZS )TransformerSpeczDescribes a Transformer model.

    The specification is invariant to hidden dimensions but requires to
    explicitly set the number of layers and attention heads.
    encoderdecoderc                 
   t        |t              st        d      t        |t              st        d      t        |           || _        || _        | j                  j                  d| j                  j                         y)zInitializes a Transformer model specification.

        Args:
          encoder: The encoder specification.
          decoder: The decoder specification.
        1encoder argument must be a TransformerEncoderSpec1decoder argument must be a TransformerDecoderSpecr   N)
isinstancer
   	TypeErrorrJ   r   r;   r   r   rb   add_attributer   )r8   r   r   r   s      r:   r;   zTransformerSpec.__init__  sm     '#9:OPP'#9:OPP""#T\\%G%G	
rH   FTrK   r   r   r   with_relative_positionr   r   r   rN   rO   r   r   r   r   r   r   r   c                     t        |t        t        f      r|\  }}n||}}t        ||||||	|
||||||      }t	        |||||||||||||      } | ||      S )a  Creates a Transformer model specification.

        Args:
          num_layers: Number of encoder and decoder layers, or a 2-tuple if the
            number is different.
          num_heads: Number of attention heads.
          with_relative_position: Use relative position representations in the self-attention
            layers as described in https://arxiv.org/abs/1803.02155.
          pre_norm: Enable the pre-norm Transformer architecture.
          no_final_norm: Disable the final layer norm in the pre-norm architecture.
          activation: Activation to apply in the feed-forward network.
          alignment_layer: Layer index selected for alignment.
          alignment_heads: Number of attention heads selected for alignment.
          num_source_embeddings: Number of source embeddings.
          embeddings_merge: When :obj:`num_source_embeddings` > 1, specify how the
            embeddings are merged.
          layernorm_embedding: Apply layer normalization after the embedding layer.
          relative_attention_bias: Use relative attention bias in the self-attention
            layers as described in the T5 paper https://arxiv.org/abs/1910.10683.
          ffn_glu: Use gated linear units in the FFN layer as described in
            https://arxiv.org/abs/2002.05202.
          rms_norm: Use the root mean square layer normalization.
          multi_query_attention: Use multi-query attention.
        )r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   rN   rO   r   r   r   )r   listtupler
   rJ   )clsr   r   r   r   r   r   rN   rO   r   r   r   r   r   r   r   num_encoder_layersnum_decoder_layersr   r   s                       r:   from_configzTransformerSpec.from_config  s    V j4-05?2 25? 2('!"7- 34$;"7
  )'! 34$;++"7
  7G$$rH   c                      y)Nr   rG   rn   s    r:   namezTransformerSpec.nameD  s     rH   c                      y)N   rG   rn   s    r:   revisionzTransformerSpec.revisionH      rH   c                     t               S rm   )r   rn   s    r:   get_default_configz"TransformerSpec.get_default_configL  s     ""rH   c                     | j                   j                  D cg c]  }|j                  j                  d    c}S c c}w Nr   r   r0   weightshape)r8   specs     r:   get_source_vocabulary_sizez*TransformerSpec.get_source_vocabulary_sizeO  s/    151H1HI!!!$IIIs    <c                 \    | j                   j                  j                  j                  d   S r   r   r0   r   r   rn   s    r:   get_target_vocabulary_sizez*TransformerSpec.get_target_vocabulary_sizeR  #    ||&&--33A66rH   )r<   r=   r>   r   r
   rJ   r;   classmethodr   r?   r@   rA   rB   r   rC   r   rD   r   rq   r   r   r   r   r   r   r   s   @r:   r   r     sn   
-
8N
* 
 (-#-8-C-C-H-H! %&8C8S8S8Z8Z$)(-&+!O%#uS#X./O% O% !%	O%
 O% O%  **O% O% O%  #O% &55O% "O% "&O% O% O%   $!O% O%b ! !  #J7rH   r   c                   0     e Zd ZdZddee   f fdZ xZS )TransformerDecoderModelConfigz-Configuration for Transformer decoder models.r   c                 (    t        |   dd|i| y)zInitializes the configuration for Transformer decoder models.

        Args:
          layer_norm_epsilon: The layer norm epsilon value.
          **kwargs: Additional configuration.
        r   NrG   r   r   s      r:   r;   z&TransformerDecoderModelConfig.__init__Y  r   rH   rm   r   r   s   @r:   r   r   V      7J8E? J JrH   r   c            B           e Zd ZdZdef fdZedej                  j                  ddddddddddddddd	d	ddddddddddddfd
e
de
dedej                  dedededededededededee
   dedeej                     dedede
de
deded ed!ed"ee
   d#ee
   d$ee
   d%eej                      d&ee
   d'ee
   d(ed)ef@d*       Zed+        Zed,        Zd- Zd. Z xZS )/TransformerDecoderModelSpecz3Describes a Transformer decoder model (e.g. GPT-2).r   c                     t        |t              st        d      t        |           || _        | j
                  j                  j                         D ]!  \  }}| j                  j                  ||       # y)z|Initializes a Transformer decoder model specification.

        Args:
          decoder: The decoder specification.
        r   N)
r   rJ   r   r   r;   r   ro   itemsrb   r   )r8   r   keyvaluer   s       r:   r;   z$TransformerDecoderModelSpec.__init__f  sh     '#9:OPP,,--335 	3JCLL&&sE2	3rH   TFNr   r   r   r   r   r   r   r   r   rM   r   r   r   rP   rQ   rR   r   r   r   r   r    rS   rT   rU   rV   r#   r   r   r   r!   rW   rX   rY   r"   rZ   c!                     t        ||fi d|d|d|ddd|d|d|d	|	d
|
d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d | }! | |!      S )!a!
  Creates a Transformer decoder model specification.

        Args:
          num_layers: Number of decoder layers.
          num_heads: Number of attention heads.
          pre_norm: Enable the pre-norm Transformer architecture.
          activation: Activation to apply in the feed-forward network.
          layernorm_embedding: Apply layer normalization after the embedding layer.
          no_final_norm: Do not apply layer normalization after the last decoder block.
          project_in_out: Add a linear layer after the embedding layer and another one
            before the final output projection.
          with_relative_position: Enable relative position representations modules.
          ffn_glu: Use gated linear units in the FFN layers as described in
            https://arxiv.org/abs/2002.05202.
          rms_norm: Use the root mean square layer normalization.
          alibi: Use attention with linear biases.
          alibi_use_positive_positions: Use positive positions in the ALiBi definition.
          scale_alibi: Apply the dot product scale factor to ALiBi.
          rotary_dim: Apply rotary embeddings to these first N dimensions. If 0, rotary
            embeddings are applied to all dimensions.
          rotary_interleave: Interleave the head dimensions when rotary embeddings are applied.
            Otherwise the head dimensions are sliced in half.
          rotary_scaling_type: Type of RoPE scaling.
          rotary_scaling_factor: Factor used in the RoPE scaling.
          rotary_base: The base period of the rotary embeddings.
          original_max_position_embeddings: The original max position embeddings
            for Su rope embeddings
          max_position_embeddings: The max position embeddings for Su rope embeddings
          parallel_residual: Use parallel residual connections in each layer block, as used
            by the GPT-J and GPT-NeoX models.
          shared_layer_norm: When using parallel residual, share the input and post
            attention layer norms.
          pre_post_layer_norm: add post layer norm for each pre norm layer
          multi_query_attention: Use multi-query attention (alias for num_heads_kv=1).
          num_heads_kv: Number of attention heads for the key and value.
          head_dim: Number of head
          sliding_window: max sequence length to retain KV cache
          quant_type: quantization type used (like awq... for lower bit quantization)
          quant_group_size: group size of the lower bit quantization
          quant_bits: number of bit of the quantization (ex: 4bit)
        r   r   r   rL   Fr   rM   r   r   r   rP   rQ   rR   r   r   r   r   r    rS   rT   rU   rV   r#   r   r   r   r!   rW   rX   rY   r"   rZ   )rJ   )"r   r   r   r   r   r   r   rM   r   r   r   rP   rQ   rR   r   r   r   r   r    rS   rT   rU   rV   r#   r   r   r   r!   rW   rX   rY   r"   rZ   r   s"                                     r:   r   z'TransformerDecoderModelSpec.from_configt  s^   Z )"
 "
 "	"

 !4"
 $)"
 ("
 *"
 5"
 "
 "
 "
 *F"
 $"
 ""
  0!"
" !4#"
$ #8%"
& $'"
( .N)"
* %<+"
, 0-"
. 0/"
0 !41"
2 #83"
4 &5"
6 7"
8 *9"
: ";"
< .="
> "?"
@ A"
B C"
H 7|rH   c                      y)NrJ   rG   rn   s    r:   r   z TransformerDecoderModelSpec.name      'rH   c                      y)N   rG   rn   s    r:   r   z$TransformerDecoderModelSpec.revision  r   rH   c                     t               S rm   )r   rn   s    r:   r   z.TransformerDecoderModelSpec.get_default_config      ,..rH   c                 \    | j                   j                  j                  j                  d   S r   r   rn   s    r:   get_vocabulary_sizez/TransformerDecoderModelSpec.get_vocabulary_size  r   rH   )r<   r=   r>   r   rJ   r;   r   r   r?   r@   rC   rD   r   r   rE   rF   rp   r   rq   r   r   r   r   r   r   s   @r:   r   r   c  s[   =3 6 3 
 -8-C-C-H-H$)#$',-2!$("&JN'("01'("'"'$)&+&*"&(,9=*.$(Cpp p 	p
  **p "p p p !%p p p p '+p p SMp   !p" &n&F&FG#p$  %%p& 'p( +.)p* "%+p,  -p.  /p0 "1p2  $3p4 sm5p6 3-7p8 !9p: [556;p< #3-=p> SM?p@ ApB Cp pd ( (  /7rH   r   c                   0     e Zd ZdZddee   f fdZ xZS )TransformerEncoderModelConfigz-Configuration for Transformer encoder models.r   c                 (    t        |   dd|i| y)zInitializes the configuration for Transformer encoder models.

        Args:
          layer_norm_epsilon: The layer norm epsilon value.
          **kwargs: Additional configuration.
        r   NrG   r   r   s      r:   r;   z&TransformerEncoderModelConfig.__init__  r   rH   rm   r   r   s   @r:   r   r     r   rH   r   c                        e Zd ZdZdej
                  j                  fdededej
                  f fdZ	e
d        Ze
d        Zd	 Zd
 Z xZS )TransformerEncoderModelSpecz2Describes a Transformer encoder model (e.g. BERT).Fr   pooling_layerpooling_activationc                 P   t        |t              st        d      t        |           || _        | j                  j                  d| j
                  j                         |rCt        j                         | _        t        j                  d      j                  |      | _        yy)zInitializes a Transformer encoder model specification.

        Args:
          encoder: The encoder specification.
          pooling_layer: Add the pooling layer.
          pooling_activation: The activation to apply after the pooling layer.
        r   r   r'   N)r   r
   r   r   r;   r   rb   r   r   r   re   pooler_denser+   r,   r-   pooler_activation)r8   r   r   r   r   s       r:   r;   z$TransformerEncoderModelSpec.__init__  s     '#9:OPP""#T\\%G%G	
  + 6 6 8D%'XXf%5%:%:;M%ND" rH   c                      y)Nr
   rG   rn   s    r:   r   z TransformerEncoderModelSpec.name   r   rH   c                      y)Nr   rG   rn   s    r:   r   z$TransformerEncoderModelSpec.revision$  r   rH   c                     t               S rm   )r   rn   s    r:   r   z.TransformerEncoderModelSpec.get_default_config(  r   rH   c                 b    | j                   j                  d   j                  j                  d   S r   r   rn   s    r:   r   z/TransformerEncoderModelSpec.get_vocabulary_size+  s(    ||&&q)0066q99rH   )r<   r=   r>   r   r   r?   Tanhr
   rD   r;   rq   r   r   r   r   r   r   s   @r:   r   r     sw    <
 $5@5K5K5P5P	O'O O (22	O4 ( (  /:rH   r   )r   typingr   r   r   numpyr+   ctranslate2.specsr   r   r   	LayerSpecr
   rJ   r6   rg   rx   r2   SequenceToSequenceModelConfigr   SequenceToSequenceModelSpecr   LanguageModelConfigr   LanguageModelSpecr   r   r   rG   rH   r:   <module>r      s   6 ) )  E Eh
Z11 h
VoZ11 od0,*"6"6 0,f^0*"6"6 ^0B;j** ;-*.. -

J
@@ 
J}7j<< }7@
JJ$B$B 
JP7*">"> P7f
JJ$B$B 
J):*">"> ):rH   