Ë
    ÜÓ^jÇ|  ã                   ó   — d Z ddlmZ ddlm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 dd
lmZ ddlmZ ddlmZmZm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m Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z(  e!jR                  e*«      Z+ G d„ dejX                  «      Z-	 d?dejX                  dej\                  dej\                  dej\                  dej\                  dz  de/de/fd„Z0 G d„ d ejX                  «      Z1 G d!„ d"ejX                  «      Z2 G d#„ d$ejX                  «      Z3 G d%„ d&ejX                  «      Z4 G d'„ d(ejX                  «      Z5 G d)„ d*e«      Z6 G d+„ d,ejX                  «      Z7e G d-„ d.e«      «       Z8e G d/„ d0e8«      «       Z9 G d1„ d2ejX                  «      Z: G d3„ d4ejX                  «      Z;e G d5„ d6e8«      «       Z< ed7¬8«      e G d9„ d:e«      «       «       Z= ed;¬8«       G d<„ d=e8«      «       Z>g d>¢Z?y)@zPyTorch Splinter model.é    )ÚCallable)Ú	dataclassN)Únn)ÚCrossEntropyLossé   )Úinitialization)ÚACT2FN)Úcreate_bidirectional_mask)ÚGradientCheckpointingLayer)ÚBaseModelOutputÚModelOutputÚQuestionAnsweringModelOutput)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)ÚUnpack)Úapply_chunking_to_forward)ÚTransformersKwargsÚauto_docstringÚcan_return_tupleÚloggingÚtorch_compilable_check)Úmerge_with_config_defaults)Úcapture_outputsé   )ÚSplinterConfigc                   ó°   ‡ — e Zd ZdZˆ fd„Z	 	 	 	 d
dej                  dz  dej                  dz  dej                  dz  dej                  dz  def
d	„Z	ˆ xZ
S )ÚSplinterEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                 ó|  •— t         ‰| �  «        t        j                  |j                  |j
                  |j                  ¬«      | _        t        j                  |j                  |j
                  «      | _	        t        j                  |j                  |j
                  «      | _        t        j                  |j
                  |j                  ¬«      | _        t        j                  |j                  «      | _        | j#                  dt%        j&                  |j                  «      j)                  d«      d¬«       y )N)Úpadding_idx©ÚepsÚposition_ids©r   éÿÿÿÿF)Ú
persistent)ÚsuperÚ__init__r   Ú	EmbeddingÚ
vocab_sizeÚhidden_sizeÚpad_token_idÚword_embeddingsÚmax_position_embeddingsÚposition_embeddingsÚtype_vocab_sizeÚtoken_type_embeddingsÚ	LayerNormÚlayer_norm_epsÚDropoutÚhidden_dropout_probÚdropoutÚregister_bufferÚtorchÚarangeÚexpand©ÚselfÚconfigÚ	__class__s     €úy/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/splinter/modeling_splinter.pyr'   zSplinterEmbeddings.__init__+   sá   ø€ Ü‰ÑÔÜ!Ÿ|™|¨F×,=Ñ,=¸v×?QÑ?QÐ_e×_rÑ_rÔsˆÔÜ#%§<¡<°×0NÑ0NÐPV×PbÑPbÓ#cˆÔ Ü%'§\¡\°&×2HÑ2HÈ&×J\ÑJ\Ó%]ˆÔ"äŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÜ—z‘z &×"<Ñ"<Ó=ˆŒð 	×ÑØœEŸL™L¨×)GÑ)GÓH×OÑOÐPWÓXÐejð 	õ 	
ó    NÚ	input_idsÚtoken_type_idsr"   Úinputs_embedsÚreturnc                 óÄ  — |�|j                  «       }n|j                  «       d d }|d   }|€| j                  d d …d |…f   }|€:t        j                  |t        j                  | j                  j
                  ¬«      }|€| j                  |«      }| j                  |«      }||z   }| j                  |«      }	||	z  }| j                  |«      }| j                  |«      }|S )Nr$   r   ©ÚdtypeÚdevice)Úsizer"   r7   ÚzerosÚlongrG   r,   r0   r.   r1   r5   )
r;   r@   rA   r"   rB   Úinput_shapeÚ
seq_lengthr0   Ú
embeddingsr.   s
             r>   ÚforwardzSplinterEmbeddings.forward9   sê   € ð Ð Ø#Ÿ.™.Ó*‰Kà'×,Ñ,Ó.¨s°Ð3ˆKà  ‘^ˆ
àÐØ×,Ñ,ªQ°°°¨^Ñ<ˆLàÐ!Ü"Ÿ[™[¨¼E¿J¹JÈt×O`ÑO`×OgÑOgÔhˆNàÐ Ø ×0Ñ0°Ó;ˆMØ $× :Ñ :¸>Ó JÐØ"Ð%:Ñ:ˆ
à"×6Ñ6°|ÓDÐØÐ)Ñ)ˆ
à—^‘^ JÓ/ˆ
Ø—\‘\ *Ó-ˆ
ØÐr?   )NNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r7   Ú
LongTensorÚFloatTensorÚtuplerN   Ú__classcell__©r=   s   @r>   r   r   (   sz   ø„ ÙQô
ð  .2Ø26Ø04Ø26ñà×#Ñ# dÑ*ðð ×(Ñ(¨4Ñ/ðð ×&Ñ&¨Ñ-ð	ð
 ×(Ñ(¨4Ñ/ðð 
÷r?   r   ÚmoduleÚqueryÚkeyÚvalueÚattention_maskÚscalingr5   c                 óÀ  — t        j                  ||j                  dd«      «      |z  }|�||z   }t        j                  j                  |dt         j                  ¬«      j                  |j                  «      }t        j                  j                  ||| j                  ¬«      }t        j                  ||«      }	|	j                  dd«      j                  «       }	|	|fS )Né   r   r$   )ÚdimrF   )ÚpÚtrainingr   )r7   ÚmatmulÚ	transposer   Ú
functionalÚsoftmaxÚfloat32ÚtorF   r5   rb   Ú
contiguous)
rX   rY   rZ   r[   r\   r]   r5   ÚkwargsÚattn_weightsÚattn_outputs
             r>   Úeager_attention_forwardrm   [   sº   € ô —<‘<  s§}¡}°Q¸Ó':Ó;¸gÑE€LØÐ!Ø# nÑ4ˆä—=‘=×(Ñ(¨¸2ÄUÇ]Á]Ð(ÓS×VÑVÐW\×WbÑWbÓc€LÜ—=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€Lä—,‘,˜|¨UÓ3€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€KØ˜Ð$Ð$r?   c                   ó¦   ‡ — e Zd Zˆ fd„Z	 ddej
                  dej                  dz  dee   de	ej
                  ej
                  dz  f   fd„Z
ˆ xZS )	ÚSplinterSelfAttentionc                 ó$  •— t         ‰| �  «        |j                  |j                  z  dk7  r2t	        |d«      s&t        d|j                  › d|j                  › d�«      ‚|| _        |j                  | _        t        |j                  |j                  z  «      | _        | j                  | j                  z  | _	        t        j                  |j                  | j                  «      | _        t        j                  |j                  | j                  «      | _        t        j                  |j                  | j                  «      | _        t        j                  |j                   «      | _        |j                   | _        | j                  dz  | _        y )Nr   Úembedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads (ú)g      à¿)r&   r'   r*   Únum_attention_headsÚhasattrÚ
ValueErrorr<   ÚintÚattention_head_sizeÚall_head_sizer   ÚLinearrY   rZ   r[   r3   Úattention_probs_dropout_probr5   Úattention_dropoutr]   r:   s     €r>   r'   zSplinterSelfAttention.__init__s   sC  ø€ Ü‰ÑÔØ×Ñ × :Ñ :Ñ:¸aÒ?ÌÐPVÐXhÔHiÜØ# F×$6Ñ$6Ð#7ð 8Ø ×4Ñ4Ð5°Qð8óð ð
 ˆŒØ#)×#=Ñ#=ˆÔ Ü#& v×'9Ñ'9¸F×<VÑ<VÑ'VÓ#WˆÔ Ø!×5Ñ5¸×8PÑ8PÑPˆÔä—Y‘Y˜v×1Ñ1°4×3EÑ3EÓFˆŒ
Ü—9‘9˜V×/Ñ/°×1CÑ1CÓDˆŒÜ—Y‘Y˜v×1Ñ1°4×3EÑ3EÓFˆŒ
ä—z‘z &×"EÑ"EÓFˆŒØ!'×!DÑ!DˆÔØ×/Ñ/°Ñ5ˆ�r?   NÚhidden_statesr\   rj   rC   c                 óx  — |j                   d d }g |¢d‘| j                  ‘­}| j                  |«      j                  |«      j	                  dd«      }| j                  |«      j                  |«      j	                  dd«      }| j                  |«      j                  |«      j	                  dd«      }t        j                  | j                  j                  t        «      }	 |	| ||||f| j                  sdn| j                  | j                  dœ|¤Ž\  }
} |
j                  g |¢d‘­Ž j!                  «       }
|
|fS )Nr$   r   r_   ç        )r5   r]   )Úshaperw   rY   Úviewrd   rZ   r[   r   Úget_interfacer<   Ú_attn_implementationrm   rb   r{   r]   Úreshaperi   )r;   r|   r\   rj   rK   Úhidden_shapeÚquery_statesÚ
key_statesÚvalue_statesÚattention_interfacerl   rk   s               r>   rN   zSplinterSelfAttention.forwardˆ   s>  € ð $×)Ñ)¨#¨2Ð.ˆØC˜ÐC bÐC¨$×*BÑ*BÑCˆà—z‘z -Ó0×5Ñ5°lÓC×MÑMÈaÐQRÓSˆØ—X‘X˜mÓ,×1Ñ1°,Ó?×IÑIÈ!ÈQÓOˆ
Ø—z‘z -Ó0×5Ñ5°lÓC×MÑMÈaÐQRÓSˆä(?×(MÑ(MØ�K‰K×,Ñ,Ô.Eó)
Ðñ %8ØØØØØð	%
ð  $Ÿ}š}‘C°$×2HÑ2HØ—L‘Lñ	%
ð ñ	%
Ñ!ˆ�\ð *�k×)Ñ)Ð;¨;Ð;¸Ò;×FÑFÓHˆØ˜LÐ(Ð(r?   ©N)rO   rP   rQ   r'   r7   ÚTensorrT   r   r   rU   rN   rV   rW   s   @r>   ro   ro   r   sd   ø„ ô6ð0 48ñ)à—|‘|ð)ð ×)Ñ)¨DÑ0ð)ð Ð+Ñ,ð	)ð
 
ˆu�|‰|˜UŸ\™\¨DÑ0Ð0Ñ	1÷)r?   ro   c                   ón   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  dej
                  fd„Zˆ xZS )ÚSplinterSelfOutputc                 ó(  •— t         ‰| �  «        t        j                  |j                  |j                  «      | _        t        j                  |j                  |j                  ¬«      | _        t        j                  |j                  «      | _
        y ©Nr    )r&   r'   r   ry   r*   Údenser1   r2   r3   r4   r5   r:   s     €r>   r'   zSplinterSelfOutput.__init__ª   s`   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3EÑ3EÓFˆŒ
ÜŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÜ—z‘z &×"<Ñ"<Ó=ˆ�r?   r|   Úinput_tensorrC   c                 ór   — | j                  |«      }| j                  |«      }| j                  ||z   «      }|S r‰   ©r�   r5   r1   ©r;   r|   r�   s      r>   rN   zSplinterSelfOutput.forward°   ó7   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆØŸ™ }°|Ñ'CÓDˆØÐr?   ©rO   rP   rQ   r'   r7   rŠ   rN   rV   rW   s   @r>   rŒ   rŒ   ©   ó1   ø„ ô>ð U§\¡\ð ÀÇÁð ÐRW×R^ÑR^÷ r?   rŒ   c            	       ó‚   ‡ — e Zd Zˆ fd„Z	 ddej
                  dej                  dz  dee   dej
                  fd„Z	ˆ xZ
S )	ÚSplinterAttentionc                 ób   •— t         ‰| �  «        t        |«      | _        t	        |«      | _        y r‰   )r&   r'   ro   r;   rŒ   Úoutputr:   s     €r>   r'   zSplinterAttention.__init__¹   s&   ø€ Ü‰ÑÔÜ)¨&Ó1ˆŒ	Ü(¨Ó0ˆ�r?   Nr|   r\   rj   rC   c                 ó^   — |} | j                   |fd|i|¤Ž\  }}| j                  ||«      }|S ©Nr\   )r;   rš   )r;   r|   r\   rj   ÚresidualÚ_s         r>   rN   zSplinterAttention.forward¾   sK   € ð !ˆØ$˜4Ÿ9™9Øñ
à)ð
ð ñ
Ñˆ�qð
 Ÿ™ M°8Ó<ˆØÐr?   r‰   )rO   rP   rQ   r'   r7   rŠ   rT   r   r   rN   rV   rW   s   @r>   r˜   r˜   ¸   sQ   ø„ ô1ð 48ñà—|‘|ðð ×)Ñ)¨DÑ0ðð Ð+Ñ,ð	ð
 
�‰÷r?   r˜   c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚSplinterIntermediatec                 ó  •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        |j                  t        «      rt        |j                     | _        y |j                  | _        y r‰   )r&   r'   r   ry   r*   Úintermediate_sizer�   Ú
isinstanceÚ
hidden_actÚstrr	   Úintermediate_act_fnr:   s     €r>   r'   zSplinterIntermediate.__init__Ð   s]   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3KÑ3KÓLˆŒ
Ü�f×'Ñ'¬Ô-Ü'-¨f×.?Ñ.?Ñ'@ˆDÕ$à'-×'8Ñ'8ˆDÕ$r?   r|   rC   c                 óJ   — | j                  |«      }| j                  |«      }|S r‰   )r�   r¦   )r;   r|   s     r>   rN   zSplinterIntermediate.forwardØ   s&   € ØŸ
™
 =Ó1ˆØ×0Ñ0°Ó?ˆØÐr?   r•   rW   s   @r>   r    r    Ï   s#   ø„ ô9ð U§\¡\ð °e·l±l÷ r?   r    c                   ón   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  dej
                  fd„Zˆ xZS )ÚSplinterOutputc                 ó(  •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        j                  |j
                  |j                  ¬«      | _        t        j                  |j                  «      | _        y rŽ   )r&   r'   r   ry   r¢   r*   r�   r1   r2   r3   r4   r5   r:   s     €r>   r'   zSplinterOutput.__init__à   s`   ø€ Ü‰ÑÔÜ—Y‘Y˜v×7Ñ7¸×9KÑ9KÓLˆŒ
ÜŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÜ—z‘z &×"<Ñ"<Ó=ˆ�r?   r|   r�   rC   c                 ór   — | j                  |«      }| j                  |«      }| j                  ||z   «      }|S r‰   r’   r“   s      r>   rN   zSplinterOutput.forwardæ   r”   r?   r•   rW   s   @r>   r©   r©   ß   r–   r?   r©   c            	       óˆ   ‡ — e Zd Zˆ fd„Z	 d	dej
                  dej                  dz  dee   dej
                  fd„Z	d„ Z
ˆ xZS )
ÚSplinterLayerc                 ó²   •— t         ‰| �  «        |j                  | _        d| _        t	        |«      | _        t        |«      | _        t        |«      | _	        y )Nr   )
r&   r'   Úchunk_size_feed_forwardÚseq_len_dimr˜   Ú	attentionr    Úintermediater©   rš   r:   s     €r>   r'   zSplinterLayer.__init__ï   sI   ø€ Ü‰ÑÔØ'-×'EÑ'EˆÔ$ØˆÔÜ*¨6Ó2ˆŒÜ0°Ó8ˆÔÜ$ VÓ,ˆ�r?   Nr|   r\   rj   rC   c                 óˆ   —  | j                   |fd|i|¤Ž}t        | j                  | j                  | j                  |«      }|S rœ   )r±   r   Úfeed_forward_chunkr¯   r°   )r;   r|   r\   rj   s       r>   rN   zSplinterLayer.forward÷   sY   € ð '˜Ÿ™Øñ
à)ð
ð ñ
ˆô 2Ø×#Ñ# T×%AÑ%AÀ4×CSÑCSÐUbó
ˆð Ðr?   c                 óL   — | j                  |«      }| j                  ||«      }|S r‰   )r²   rš   )r;   Úattention_outputÚintermediate_outputÚlayer_outputs       r>   r´   z SplinterLayer.feed_forward_chunk	  s,   € Ø"×/Ñ/Ð0@ÓAÐØ—{‘{Ð#6Ð8HÓIˆØÐr?   r‰   )rO   rP   rQ   r'   r7   rŠ   rT   r   r   rN   r´   rV   rW   s   @r>   r­   r­   î   sV   ø„ ô-ð 48ñà—|‘|ðð ×)Ñ)¨DÑ0ðð Ð+Ñ,ð	ð
 
�‰óö$r?   r­   c            	       ón   ‡ — e Zd Zˆ fd„Z	 ddej
                  dej                  dz  dee   de	fd„Z
ˆ xZS )	ÚSplinterEncoderc                 óÐ   •— t         ‰| �  «        || _        t        j                  t        |j                  «      D �cg c]  }t        |«      ‘Œ c}«      | _        d| _	        y c c}w )NF)
r&   r'   r<   r   Ú
ModuleListÚrangeÚnum_hidden_layersr­   ÚlayerÚgradient_checkpointing)r;   r<   Úir=   s      €r>   r'   zSplinterEncoder.__init__  sN   ø€ Ü‰ÑÔØˆŒÜ—]‘]Ä5È×IaÑIaÓCbÖ#c¸a¤M°&Õ$9Ò#cÓdˆŒ
Ø&+ˆÕ#ùò $ds   ½A#Nr|   r\   rj   rC   c                 óP   — | j                   D ]  } |||fi |¤Ž}Œ t        |¬«      S )N©Úlast_hidden_state)r¿   r   )r;   r|   r\   rj   Úlayer_modules        r>   rN   zSplinterEncoder.forward  sC   € ð !ŸJ™Jò 	ˆLÙ(ØØñð ñ‰Mð	ô Ø+ô
ð 	
r?   r‰   )rO   rP   rQ   r'   r7   rŠ   rT   r   r   r   rN   rV   rW   s   @r>   rº   rº     sM   ø„ ô,ð 48ñ
à—|‘|ð
ð ×)Ñ)¨DÑ0ð
ð Ð+Ñ,ð	
ð
 
÷
r?   rº   c                   ó<   ‡ — e Zd ZU eed<   dZdZeedœZ	ˆ fd„Z
ˆ xZS )ÚSplinterPreTrainedModelr<   ÚsplinterT)r|   Ú
attentionsc                 óø   •— t         ‰| �  |«       t        |t        «      rZt	        j
                  |j                  t        j                  |j                  j                  d   «      j                  d«      «       y y )Nr$   r#   )r&   Ú_init_weightsr£   r   ÚinitÚcopy_r"   r7   r8   r   r9   )r;   rX   r=   s     €r>   rË   z%SplinterPreTrainedModel._init_weights3  s[   ø€ Ü‰Ñ˜fÔ%Ü�fÔ0Ô1Ü�J‰J�v×*Ñ*¬E¯L©L¸×9LÑ9L×9RÑ9RÐSUÑ9VÓ,W×,^Ñ,^Ð_fÓ,gÕhð 2r?   )rO   rP   rQ   r   Ú__annotations__Úbase_model_prefixÚsupports_gradient_checkpointingr­   ro   Ú_can_record_outputsrË   rV   rW   s   @r>   rÇ   rÇ   )  s/   ø… àÓØ"ÐØ&*Ð#à&Ø+ñÐ÷
ið ir?   rÇ   c                   ó
  ‡ — e Zd ZdZˆ fd„Zd„ Zd„ Zeee		 	 	 	 	 dde
j                  dz  de
j                  dz  de
j                  dz  d	e
j                  dz  d
e
j                  dz  dee   deez  fd„«       «       «       Zˆ xZS )ÚSplinterModela2  
    The model is an encoder (with only self-attention) following the architecture described in [Attention is all you
    need](https://huggingface.co/papers/1706.03762) by Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones,
    Aidan N. Gomez, Lukasz Kaiser and Illia Polosukhin.
    c                 ó’   •— t         ‰| �  |«       || _        t        |«      | _        t        |«      | _        | j                  «        y r‰   )r&   r'   r<   r   rM   rº   ÚencoderÚ	post_initr:   s     €r>   r'   zSplinterModel.__init__A  s;   ø€ Ü‰Ñ˜Ô ØˆŒä,¨VÓ4ˆŒÜ& vÓ.ˆŒð 	�‰Õr?   c                 ó.   — | j                   j                  S r‰   ©rM   r,   )r;   s    r>   Úget_input_embeddingsz"SplinterModel.get_input_embeddingsK  s   € Ø�‰×.Ñ.Ð.r?   c                 ó&   — || j                   _        y r‰   rØ   )r;   r[   s     r>   Úset_input_embeddingsz"SplinterModel.set_input_embeddingsN  s   € Ø*/ˆ�‰Õ'r?   Nr@   r\   rA   r"   rB   rj   rC   c                 ó  — |�|�t        d«      ‚|�#| j                  ||«       |j                  «       }n!|�|j                  «       dd }nt        d«      ‚|\  }}	|�|j                  n|j                  }
|€t	        j
                  ||	f|
¬«      }|€&t	        j                  |t        j                  |
¬«      }| j                  ||||¬«      }t        | j                  ||¬«      } | j                  |fd	|i|¤Ž}|d
   }t        |¬«      S )aã  
        token_type_ids (`torch.LongTensor` of shape `batch_size, sequence_length`, *optional*):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
            1]`:

            - 0 corresponds to a *sentence A* token,
            - 1 corresponds to a *sentence B* token.

            [What are token type IDs?](../glossary#token-type-ids)
        position_ids (`torch.LongTensor` of shape `batch_size, sequence_length`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.max_position_embeddings - 1]`.

            [What are position IDs?](../glossary#position-ids)
        NzDYou cannot specify both input_ids and inputs_embeds at the same timer$   z5You have to specify either input_ids or inputs_embeds©rG   rE   )r@   r"   rA   rB   )r<   rB   r\   r\   r   rÃ   )ru   Ú%warn_if_padding_and_no_attention_maskrH   rG   r7   ÚonesrI   rJ   rM   r
   r<   rÕ   r   )r;   r@   r\   rA   r"   rB   rj   rK   Ú
batch_sizerL   rG   Úembedding_outputÚencoder_outputsÚsequence_outputs                 r>   rN   zSplinterModel.forwardQ  s9  € ð6 Ð  ]Ð%>ÜÐcÓdÐdØÐ"Ø×6Ñ6°yÀ.ÔQØ#Ÿ.™.Ó*‰KØÐ&Ø'×,Ñ,Ó.¨s°Ð3‰KäÐTÓUÐUà!,Ñˆ
�JØ%.Ð%:�×!Ò!À×@TÑ@TˆàÐ!Ü"ŸZ™Z¨*°jÐ)AÈ6ÔRˆNØÐ!Ü"Ÿ[™[¨¼E¿J¹JÈvÔVˆNàŸ?™?ØØ%Ø)Ø'ð	 +ó 
Ðô 3Ø—;‘;Ø*Ø)ô
ˆð '˜$Ÿ,™,Øñ
à)ð
ð ñ
ˆð
 *¨!Ñ,ˆäØ-ô
ð 	
r?   )NNNNN)rO   rP   rQ   rR   r'   rÙ   rÛ   r   r   r   r7   rŠ   r   r   rU   r   rN   rV   rW   s   @r>   rÓ   rÓ   9  sÑ   ø„ ñôò/ò0ð  ØØð *.Ø.2Ø.2Ø,0Ø-1ñ@
à—<‘< $Ñ&ð@
ð Ÿ™ tÑ+ð@
ð Ÿ™ tÑ+ð	@
ð
 —l‘l TÑ)ð@
ð —|‘| dÑ*ð@
ð Ð+Ñ,ð@
ð 
�Ñ	 ò@
ó ó ó  ô@
r?   rÓ   c                   óX   ‡ — e Zd Zdˆ fd„	Zdej
                  dej
                  fd„Zˆ xZS )ÚSplinterFullyConnectedLayerc                 ó   •— t         ‰| �  «        || _        || _        t	        j
                  | j                  | j                  «      | _        t        |   | _        t	        j                  | j                  «      | _	        y r‰   )
r&   r'   Ú	input_dimÚ
output_dimr   ry   r�   r	   Úact_fnr1   )r;   rç   rè   r¤   r=   s       €r>   r'   z$SplinterFullyConnectedLayer.__init__˜  sV   ø€ Ü‰ÑÔà"ˆŒØ$ˆŒä—Y‘Y˜tŸ~™~¨t¯©Ó?ˆŒ
Ü˜ZÑ(ˆŒÜŸ™ d§o¡oÓ6ˆ�r?   ÚinputsrC   c                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S r‰   )r�   ré   r1   )r;   rê   r|   s      r>   rN   z#SplinterFullyConnectedLayer.forward¢  s2   € ØŸ
™
 6Ó*ˆØŸ™ MÓ2ˆØŸ™ }Ó5ˆØÐr?   )Úgelur•   rW   s   @r>   rå   rå   —  s#   ø„ õ7ð˜eŸl™lð ¨u¯|©|÷ r?   rå   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚQuestionAwareSpanSelectionHeadzf
    Implementation of Question-Aware Span Selection (QASS) head, described in Splinter's paper:

    c                 ó  •— t         ‰| �  «        t        |j                  |j                  «      | _        t        |j                  |j                  «      | _        t        |j                  |j                  «      | _        t        |j                  |j                  «      | _        t        j                  |j                  |j                  d¬«      | _
        t        j                  |j                  |j                  d¬«      | _        y )NF)Úbias)r&   r'   rå   r*   Úquery_start_transformÚquery_end_transformÚstart_transformÚend_transformr   ry   Ústart_classifierÚend_classifierr:   s     €r>   r'   z'QuestionAwareSpanSelectionHead.__init__¯  sÀ   ø€ Ü‰ÑÔä%@À×ASÑASÐU[×UgÑUgÓ%hˆÔ"Ü#>¸v×?QÑ?QÐSY×SeÑSeÓ#fˆÔ Ü:¸6×;MÑ;MÈv×OaÑOaÓbˆÔÜ8¸×9KÑ9KÈV×M_ÑM_Ó`ˆÔä "§	¡	¨&×*<Ñ*<¸f×>PÑ>PÐW\Ô ]ˆÔÜ Ÿi™i¨×(:Ñ(:¸F×<NÑ<NÐUZÔ[ˆÕr?   c                 ó  — |j                  «       \  }}}|j                  d«      j                  dd|«      }t        j                  |d|¬«      }| j                  |«      }| j                  |«      }| j                  |«      }	| j                  |«      }
| j                  |«      }|	j                  ddd«      }	t        j                  ||	«      }| j                  |«      }|
j                  ddd«      }
t        j                  ||
«      }||fS )Nr$   r   )r`   Úindexr   r_   )rH   Ú	unsqueezeÚrepeatr7   Úgatherrñ   rò   ró   rô   rõ   Úpermuterc   rö   )r;   rê   Ú	positionsrž   r`   rø   Úgathered_repsÚquery_start_repsÚquery_end_repsÚ
start_repsÚend_repsr|   Ústart_logitsÚ
end_logitss                 r>   rN   z&QuestionAwareSpanSelectionHead.forwardº  sù   € Ø—K‘K“M‰	ˆˆ1ˆcØ×#Ñ# BÓ'×.Ñ.¨q°!°SÓ9ˆÜŸ™ V°¸%Ô@ˆà×5Ñ5°mÓDÐØ×1Ñ1°-Ó@ˆØ×)Ñ)¨&Ó1ˆ
Ø×%Ñ% fÓ-ˆà×-Ñ-Ð.>Ó?ˆØ×'Ñ'¨¨1¨aÓ0ˆ
Ü—|‘| M°:Ó>ˆà×+Ñ+¨NÓ;ˆØ×#Ñ# A q¨!Ó,ˆÜ—\‘\ -°Ó:ˆ
à˜ZÐ'Ð'r?   )rO   rP   rQ   rR   r'   rN   rV   rW   s   @r>   rî   rî   ©  s   ø„ ñô
	\ö(r?   rî   c                   óP  ‡ — e Zd Zˆ fd„Zee	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  de	e
   deez  fd„«       «       Zˆ xZS )ÚSplinterForQuestionAnsweringc                 ó¦   •— t         ‰| �  |«       t        |«      | _        t	        |«      | _        |j                  | _        | j                  «        y r‰   ©r&   r'   rÓ   rÈ   rî   Úsplinter_qassÚquestion_token_idrÖ   r:   s     €r>   r'   z%SplinterForQuestionAnswering.__init__Ñ  óC   ø€ Ü‰Ñ˜Ô ä% fÓ-ˆŒÜ;¸FÓCˆÔØ!'×!9Ñ!9ˆÔð 	�‰Õr?   Nr@   r\   rA   r"   rB   Ústart_positionsÚend_positionsÚquestion_positionsrj   rC   c	                 ó†  — d}
|€£|�Dt        j                  t        j                  || j                  «      j	                  «       d¬«      }nJt        j
                  |j                  d«      t         j                  |j                  |j                  ¬«      }|j                  d«      }d}
 | j                  |f||||dœ|	¤Ž}|d   }| j                  ||«      \  }}|
r"|j                  d	«      |j                  d	«      }}|�d|d	|z
  t        j                  |j                  «      j                   z  z   }|d	|z
  t        j                  |j                  «      j                   z  z   }d}|�·|�µt#        |j                  «       «      d	kD  r|j                  d«      }t#        |j                  «       «      d	kD  r|j                  d«      }|j                  d	«      }|j%                  d|«       |j%                  d|«       t'        |¬
«      } |||«      } |||«      }||z   dz  }t)        ||||j*                  |j,                  ¬«      S )aÉ  
        token_type_ids (`torch.LongTensor` of shape `batch_size, sequence_length`, *optional*):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
            1]`:

            - 0 corresponds to a *sentence A* token,
            - 1 corresponds to a *sentence B* token.

            [What are token type IDs?](../glossary#token-type-ids)
        position_ids (`torch.LongTensor` of shape `batch_size, sequence_length`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.max_position_embeddings - 1]`.

            [What are position IDs?](../glossary#position-ids)
        question_positions (`torch.LongTensor` of shape `(batch_size, num_questions)`, *optional*):
            The positions of all question tokens. If given, start_logits and end_logits will be of shape `(batch_size,
            num_questions, sequence_length)`. If None, the first question token in each sequence in the batch will be
            the only one for which start_logits and end_logits are calculated and they will be of shape `(batch_size,
            sequence_length)`.
        FNr$   )r`   r   )rF   ÚlayoutrG   T©r\   rA   r"   rB   r   ©Úignore_indexr_   ©Úlossr  r  r|   rÉ   )r7   ÚargmaxÚeqr
  rv   rI   rH   rJ   r  rG   rù   rÈ   r	  ÚsqueezeÚfinforF   ÚminÚlenÚclamp_r   r   r|   rÉ   )r;   r@   r\   rA   r"   rB   r  r  r  rj   Úquestion_positions_were_noneÚ"question_position_for_each_exampleÚoutputsrã   r  r  Ú
total_lossÚignored_indexÚloss_fctÚ
start_lossÚend_losss                        r>   rN   z$SplinterForQuestionAnswering.forwardÛ  sF  € ðD (-Ð$ØÐ%ØÐ$Ü5:·\±\Ü—X‘X˜i¨×)?Ñ)?Ó@×EÑEÓGÈRô6Ñ2ô 6;·[±[Ø!×&Ñ& qÓ)´·±ÀM×DXÑDXÐan×auÑauô6Ð2ð "D×!MÑ!MÈbÓ!QÐØ+/Ð(à�$—-‘-Øð
à)Ø)Ø%Ø'ñ
ð ñ
ˆð " !™*ˆØ#'×#5Ñ#5°oÐGYÓ#ZÑ ˆ�já'Ø'3×';Ñ';¸AÓ'>À
×@RÑ@RÐSTÓ@U˜*ˆLàÐ%Ø'¨1¨~Ñ+=ÄÇÁÈ\×M_ÑM_ÓA`×AdÑAdÑ*dÑdˆLØ# q¨>Ñ'9¼U¿[¹[È×IYÑIYÓ=Z×=^Ñ=^Ñ&^Ñ^ˆJàˆ
ØÐ&¨=Ð+Dä�?×'Ñ'Ó)Ó*¨QÒ.Ø"1×"9Ñ"9¸"Ó"=�Ü�=×%Ñ%Ó'Ó(¨1Ò,Ø -× 5Ñ 5°bÓ 9�à(×-Ñ-¨aÓ0ˆMØ×"Ñ" 1 mÔ4Ø× Ñ   MÔ2ä'°]ÔCˆHÙ! ,°Ó@ˆJÙ 
¨MÓ:ˆHØ$ xÑ/°1Ñ4ˆJä+ØØ%Ø!Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r?   ©NNNNNNNN)rO   rP   rQ   r'   r   r   r7   rŠ   rS   r   r   rU   r   rN   rV   rW   s   @r>   r  r  Ï  s  ø„ ôð Øð *.Ø.2Ø.2Ø,0Ø-1Ø37Ø15Ø6:ñW
à—<‘< $Ñ&ðW
ð Ÿ™ tÑ+ðW
ð Ÿ™ tÑ+ð	W
ð
 —l‘l TÑ)ðW
ð —|‘| dÑ*ðW
ð ×)Ñ)¨DÑ0ðW
ð ×'Ñ'¨$Ñ.ðW
ð "×,Ñ,¨tÑ3ðW
ð Ð+Ñ,ðW
ð 
Ð-Ñ	-òW
ó ó ôW
r?   r  zB
    Class for outputs of Splinter as a span selection model.
    )Úcustom_introc                   óæ   — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  dz  ed<   dZ
eej                     dz  ed<   dZeej                     dz  ed<   y)ÚSplinterForPreTrainingOutputaë  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when start and end positions are provided):
        Total span extraction loss is the sum of a Cross-Entropy for the start and end positions.
    start_logits (`torch.FloatTensor` of shape `(batch_size, num_questions, sequence_length)`):
        Span-start scores (before SoftMax).
    end_logits (`torch.FloatTensor` of shape `(batch_size, num_questions, sequence_length)`):
        Span-end scores (before SoftMax).
    Nr  r  r  r|   rÉ   )rO   rP   rQ   rR   r  r7   rT   rÎ   r  r  r|   rU   rÉ   © r?   r>   r(  r(  7  s|   … ñð &*€Dˆ%×
Ñ
˜dÑ
"Ó)Ø-1€L�%×#Ñ# dÑ*Ó1Ø+/€J�×!Ñ! DÑ(Ó/Ø59€M�5˜×*Ñ*Ñ+¨dÑ2Ó9Ø26€J��e×'Ñ'Ñ(¨4Ñ/Ô6r?   r(  zý
    Splinter Model for the recurring span selection task as done during the pretraining. The difference to the QA task
    is that we do not have a question, but multiple question tokens that replace the occurrences of recurring spans
    instead.
    c                   óˆ  ‡ — e Zd Zˆ fd„Zee	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  de	e
   deez  fd„«       «       Zdej                  dej                  fd„Zˆ xZS )ÚSplinterForPreTrainingc                 ó¦   •— t         ‰| �  |«       t        |«      | _        t	        |«      | _        |j                  | _        | j                  «        y r‰   r  r:   s     €r>   r'   zSplinterForPreTraining.__init__V  r  r?   Nr@   r\   rA   r"   rB   r  r  r  rj   rC   c	                 óþ  — |€|�|�t        d«      ‚|€|€t        d«      ‚|€| j                  |«      } | j                  |f||||dœ|	¤Ž}
|
d   }|j                  «       \  }}}| j	                  ||«      \  }}|j                  d«      }|�†|j                  d«      j                  |||«      }|d|z
  t        j                  |j                  «      j                  z  z   }|d|z
  t        j                  |j                  «      j                  z  z   }d}|�Ä|�Â|j                  dt        d|dz
  «      «       |j                  dt        d|dz
  «      «       t        | j                  j                  ¬«      } ||j!                  ||z  |«      |j!                  ||z  «      «      } ||j!                  ||z  |«      |j!                  ||z  «      «      }||z   dz  }t#        ||||
j$                  |
j&                  ¬	«      S )
a  
        input_ids (`torch.LongTensor` of shape `(batch_size, num_questions, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        token_type_ids (`torch.LongTensor` of shape `batch_size, num_questions, sequence_length`, *optional*):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
            1]`:

            - 0 corresponds to a *sentence A* token,
            - 1 corresponds to a *sentence B* token.

            [What are token type IDs?](../glossary#token-type-ids)
        position_ids (`torch.LongTensor` of shape `batch_size, num_questions, sequence_length`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.max_position_embeddings - 1]`.

            [What are position IDs?](../glossary#position-ids)
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_questions, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert *input_ids* indices into associated vectors than the
            model's internal embedding lookup matrix.
        start_positions (`torch.LongTensor` of shape `(batch_size, num_questions)`, *optional*):
            Labels for position (index) of the start of the labelled span for computing the token classification loss.
            Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
            are not taken into account for computing the loss.
        end_positions (`torch.LongTensor` of shape `(batch_size, num_questions)`, *optional*):
            Labels for position (index) of the end of the labelled span for computing the token classification loss.
            Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
            are not taken into account for computing the loss.
        question_positions (`torch.LongTensor` of shape `(batch_size, num_questions)`, *optional*):
            The positions of all question tokens. If given, start_logits and end_logits will be of shape `(batch_size,
            num_questions, sequence_length)`. If None, the first question token in each sequence in the batch will be
            the only one for which start_logits and end_logits are calculated and they will be of shape `(batch_size,
            sequence_length)`.
        NzCquestion_positions must be specified in order to calculate the lossz?question_positions must be specified when inputs_embeds is usedr  r   r   r  r_   r  )Ú	TypeErrorÚ_prepare_question_positionsrÈ   rH   r	  rù   r9   r7   r  rF   r  r  Úmaxr   r<   r+   r€   r(  r|   rÉ   )r;   r@   r\   rA   r"   rB   r  r  r  rj   r  rã   rà   Úsequence_lengthr`   r  r  Únum_questionsÚ attention_mask_for_each_questionr   r"  r#  r$  s                          r>   rN   zSplinterForPreTraining.forward`  sD  € ðj Ð%¨/Ð*EÈ-ÐJcÜÐaÓbÐbàÐ'¨IÐ,=ÜÐ]Ó^Ð^àÐ'Ø!%×!AÑ!AÀ)Ó!LÐà�$—-‘-Øð
à)Ø)Ø%Ø'ñ
ð ñ
ˆð " !™*ˆØ+:×+?Ñ+?Ó+AÑ(ˆ
�O Sà#'×#5Ñ#5°oÐGYÓ#ZÑ ˆ�jà*×/Ñ/°Ó2ˆØÐ%Ø/=×/GÑ/GÈÓ/J×/QÑ/QØ˜M¨?ó0Ð,ð (¨1Ð/OÑ+OÔSX×S^ÑS^Ð_k×_qÑ_qÓSr×SvÑSvÑ*vÑvˆLØ# qÐ+KÑ'KÌuÏ{É{Ð[e×[kÑ[kÓOl×OpÑOpÑ&pÑpˆJàˆ
àÐ&¨=Ð+Dà×"Ñ" 1¤c¨!¨_¸qÑ-@Ó&AÔBØ× Ñ  ¤C¨¨?¸QÑ+>Ó$?Ô@ô (°T·[±[×5MÑ5MÔNˆHÙ!Ø×!Ñ! *¨}Ñ"<¸oÓNØ×$Ñ$ Z°-Ñ%?Ó@óˆJñ  Ø—‘ 
¨]Ñ :¸OÓLØ×"Ñ" :°Ñ#=Ó>óˆHð % xÑ/°1Ñ4ˆJä+ØØ%Ø!Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r?   c                 óT  — t        j                  || j                  j                  k(  «      \  }}t        j                  |«      }t        |j                  d«      |j                  d«      k(  d«       t        j                  ||d¬«      }t        j                  |j                  d«      |j                  ¬«      |z
  }t        j                  |j                  d«      |j                  «       f| j                  j                  t         j                  |j                  ¬«      }||||f<   |S )Nr   z?All samples in the batch must have at least one question token.Úleft)ÚsiderÝ   rE   )r7   Úwherer<   r
  Úbincountr   rH   Úsearchsortedr8   rG   Úfullr0  r+   rJ   )r;   r@   ÚrowsÚflat_positionsr2  Ú	first_idxÚcolsrý   s           r>   r/  z2SplinterForPreTraining._prepare_question_positionsÒ  së   € Ü$Ÿ{™{¨9¸¿¹×8UÑ8UÑ+UÓVÑˆˆnÜŸ™ tÓ,ˆÜØ×Ñ˜qÓ! Y§^¡^°AÓ%6Ñ6ØMô	
ô
 ×&Ñ& t¨T¸Ô?ˆ	Ü�|‰|˜DŸI™I a›L°·±Ô=À	ÑIˆÜ—J‘JØ�^‰^˜AÓ × 1Ñ 1Ó 3Ð4Ø�K‰K×$Ñ$Ü—*‘*Ø×#Ñ#ô	
ˆ	ð !/ˆ	�$˜�*ÑØÐr?   r%  )rO   rP   rQ   r'   r   r   r7   rŠ   rS   r   r   rU   r(  rN   r/  rV   rW   s   @r>   r+  r+  N  s"  ø„ ôð Øð *.Ø.2Ø.2Ø,0Ø-1Ø37Ø15Ø6:ñn
à—<‘< $Ñ&ðn
ð Ÿ™ tÑ+ðn
ð Ÿ™ tÑ+ð	n
ð
 —l‘l TÑ)ðn
ð —|‘| dÑ*ðn
ð ×)Ñ)¨DÑ0ðn
ð ×'Ñ'¨$Ñ.ðn
ð "×,Ñ,¨tÑ3ðn
ð Ð+Ñ,ðn
ð 
Ð-Ñ	-òn
ó ó ðn
ð`°U·\±\ð ÀeÇlÁl÷ r?   r+  )r  r+  r­   rÓ   rÇ   )r~   )@rR   Úcollections.abcr   Údataclassesr   r7   r   Útorch.nnr   Ú r   rÌ   Úactivationsr	   Úmasking_utilsr
   Úmodeling_layersr   Úmodeling_outputsr   r   r   Úmodeling_utilsr   r   Úprocessing_utilsr   Úpytorch_utilsr   Úutilsr   r   r   r   r   Úutils.genericr   Úutils.output_capturingr   Úconfiguration_splinterr   Ú
get_loggerrO   ÚloggerÚModuler   rŠ   Úfloatrm   ro   rŒ   r˜   r    r©   r­   rº   rÇ   rÓ   rå   rî   r  r(  r+  Ú__all__r)  r?   r>   ú<module>rS     s  ðñ å $Ý !ã Ý Ý %å &Ý !Ý 6Ý 9ß ZÑ Zß FÝ &Ý 6ß jÕ jÝ 7Ý 5Ý 2ð 
ˆ×	Ñ	˜HÓ	%€ô/˜Ÿ™ô /ðt ñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 —L‘L 4Ñ'ð%ð ð%ð ó%ô.3)˜BŸI™Iô 3)ôn˜Ÿ™ô ô˜Ÿ	™	ô ô.˜2Ÿ9™9ô ô �R—Y‘Yô ôÐ.ô ôD
�b—i‘iô 
ð2 ôi˜oó ió ðið ôZ
Ð+ó Z
ó ðZ
ôz "§)¡)ô ô$#( R§Y¡Yô #(ðL ôd
Ð#:ó d
ó ðd
ñN ðôð
 ô7 ;ó 7ó óð7ñ" ðôôNÐ4ó NóðNòb�r?   