Ë
    T^(h‡Ð  ã                   óh  — d Z ddlZddlmZ ddlmZmZmZmZ 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mZ dd
lmZ ddlmZmZmZ ddlmZmZmZmZ ddlm Z   ejB                  e"«      Z#dZ$dZ% G d„ dejL                  «      Z' G d„ dejL                  «      Z( G d„ dejL                  «      Z)de(iZ* G d„ dejL                  «      Z+ G d„ dejL                  «      Z, G d„ dejL                  «      Z- G d„ dejL                  «      Z. G d „ d!ejL                  «      Z/ G d"„ d#e«      Z0d$Z1d%Z2 ed&e1«       G d'„ d(e0«      «       Z3 G d)„ d*ejL                  «      Z4 G d+„ d,ejL                  «      Z5 ed-e1«       G d.„ d/e0«      «       Z6e G d0„ d1e«      «       Z7 ed2e1«       G d3„ d4e0«      «       Z8g d5¢Z9y)6zPyTorch Splinter model.é    N)Ú	dataclass)ÚListÚOptionalÚTupleÚUnion)Únn)ÚCrossEntropyLossé   )ÚACT2FN)Ú)BaseModelOutputWithPastAndCrossAttentionsÚModelOutputÚQuestionAnsweringModelOutput)ÚPreTrainedModel)Úapply_chunking_to_forwardÚ find_pruneable_heads_and_indicesÚprune_linear_layer)Úadd_code_sample_docstringsÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚloggingé   )ÚSplinterConfigztau/splinter-baser   c                   ó¼   ‡ — e Zd ZdZˆ fd„Z	 	 	 	 	 d
deej                     deej                     deej                     deej                     dee	   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¬«       t+        |dd«      | _        y )	N)Úpadding_idx©ÚepsÚposition_ids)r   éÿÿÿÿF)Ú
persistentÚposition_embedding_typeÚabsolute)Ú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Úgetattrr"   ©ÚselfÚconfigÚ	__class__s     €úl/var/www/skyplay_api_hub/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ð 	ô 	
ô (/¨vÐ7PÐR\Ó']ˆÕ$ó    Ú	input_idsÚtoken_type_idsr   Úinputs_embedsÚpast_key_values_lengthÚreturnc                 óè  — |�|j                  «       }n|j                  «       d d }|d   }|€| j                  d d …|||z   …f   }|€:t        j                  |t        j                  | j                  j
                  ¬«      }|€| j                  |«      }| j                  |«      }||z   }	| j                  dk(  r| j                  |«      }
|	|
z  }	| j                  |	«      }	| j                  |	«      }	|	S )Nr    r   ©ÚdtypeÚdevicer#   )Úsizer   r5   ÚzerosÚlongrG   r*   r.   r"   r,   r/   r3   )r:   r?   r@   r   rA   rB   Úinput_shapeÚ
seq_lengthr.   Ú
embeddingsr,   s              r=   ÚforwardzSplinterEmbeddings.forward<   s  € ð Ð Ø#Ÿ.™.Ó*‰Kà'×,Ñ,Ó.¨s°Ð3ˆKà  ‘^ˆ
àÐØ×,Ñ,ªQÐ0FÈÐVlÑIlÐ0lÐ-lÑmˆLàÐ!Ü"Ÿ[™[¨¼E¿J¹JÈt×O`ÑO`×OgÑOgÔhˆNàÐ Ø ×0Ñ0°Ó;ˆMØ $× :Ñ :¸>Ó JÐà"Ð%:Ñ:ˆ
Ø×'Ñ'¨:Ò5Ø"&×":Ñ":¸<Ó"HÐØÐ-Ñ-ˆJØ—^‘^ JÓ/ˆ
Ø—\‘\ *Ó-ˆ
ØÐr>   )NNNNr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r   r5   Ú
LongTensorÚFloatTensorÚintr   rN   Ú__classcell__©r<   s   @r=   r   r   (   sŒ   ø„ ÙQô^ð& 15Ø59Ø37Ø59Ø01ñà˜E×,Ñ,Ñ-ðð ! ×!1Ñ!1Ñ2ðð ˜u×/Ñ/Ñ0ð	ð
   × 1Ñ 1Ñ2ðð !)¨¡ðð 
÷r>   r   c                   óP  ‡ — e Zd Zdˆ fd„	Zdej
                  dej
                  fd„Z	 	 	 	 	 	 ddej
                  deej                     deej                     deej                     d	eej                     d
ee	e	ej                           dee
   de	ej
                     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                  «      | _        |xs t#        |dd«      | _        | j$                  dk(  s| j$                  d	k(  rF|j&                  | _        t        j(                  d
|j&                  z  dz
  | j                  «      | _        |j,                  | _        y )Nr   Úembedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads (ú)r"   r#   Úrelative_keyÚrelative_key_queryé   r   )r$   r%   r(   Únum_attention_headsÚhasattrÚ
ValueErrorrU   Úattention_head_sizeÚall_head_sizer   ÚLinearÚqueryÚkeyÚvaluer1   Úattention_probs_dropout_probr3   r8   r"   r+   r&   Údistance_embeddingÚ
is_decoder©r:   r;   r"   r<   s      €r=   r%   zSplinterSelfAttention.__init__`   s�  ø€ Ü‰ÑÔØ×Ñ × :Ñ :Ñ:¸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ˆŒØ'>ò (
Ä'ØÐ-¨zóC
ˆÔ$ð ×'Ñ'¨>Ò9¸T×=YÑ=YÐ]qÒ=qØ+1×+IÑ+IˆDÔ(Ü&(§l¡l°1°v×7UÑ7UÑ3UÐXYÑ3YÐ[_×[sÑ[sÓ&tˆDÔ#à ×+Ñ+ˆ�r>   ÚxrC   c                 ó¤   — |j                  «       d d | j                  | j                  fz   }|j                  |«      }|j	                  dddd«      S )Nr    r   r_   r   r
   )rH   r`   rc   ÚviewÚpermute)r:   rm   Únew_x_shapes      r=   Útranspose_for_scoresz*SplinterSelfAttention.transpose_for_scoresz   sL   € Ø—f‘f“h˜s �m t×'?Ñ'?À×AYÑAYÐ&ZÑZˆØ�F‰F�;ÓˆØ�y‰y˜˜A˜q !Ó$Ð$r>   Úhidden_statesÚattention_maskÚ	head_maskÚencoder_hidden_statesÚencoder_attention_maskÚpast_key_valueÚoutput_attentionsc                 ó$  — | j                  |«      }|d u}	|	r|�|d   }
|d   }|}�n |	rC| j                  | j                  |«      «      }
| j                  | j                  |«      «      }|}n»|�y| j                  | j                  |«      «      }
| j                  | j                  |«      «      }t	        j
                  |d   |
gd¬«      }
t	        j
                  |d   |gd¬«      }n@| j                  | j                  |«      «      }
| j                  | j                  |«      «      }| j                  |«      }|d u}| j                  r|
|f}t	        j                  ||
j                  dd«      «      }| j                  dk(  s| j                  dk(  �r—|j                  d   |
j                  d   }}|rDt	        j                  |dz
  t        j                  |j                  ¬	«      j                  dd«      }n@t	        j                  |t        j                  |j                  ¬	«      j                  dd«      }t	        j                  |t        j                  |j                  ¬	«      j                  dd«      }||z
  }| j!                  || j"                  z   dz
  «      }|j%                  |j&                  ¬
«      }| j                  dk(  rt	        j(                  d||«      }||z   }nE| j                  dk(  r6t	        j(                  d||«      }t	        j(                  d|
|«      }||z   |z   }|t+        j,                  | j.                  «      z  }|�||z   }t0        j2                  j5                  |d¬«      }| j7                  |«      }|�||z  }t	        j                  ||«      }|j9                  dddd«      j;                  «       }|j=                  «       d d | j>                  fz   }|j                  |«      }|r||fn|f}| j                  r||fz   }|S )Nr   r   r_   ©Údimr    éþÿÿÿr]   r^   rE   )rF   zbhld,lrd->bhlrzbhrd,lrd->bhlrr
   ) rf   rr   rg   rh   r5   Úcatrk   ÚmatmulÚ	transposer"   ÚshapeÚtensorrJ   rG   ro   r6   rj   r+   ÚtorF   ÚeinsumÚmathÚsqrtrc   r   Ú
functionalÚsoftmaxr3   rp   Ú
contiguousrH   rd   )r:   rs   rt   ru   rv   rw   rx   ry   Úmixed_query_layerÚis_cross_attentionÚ	key_layerÚvalue_layerÚquery_layerÚ	use_cacheÚattention_scoresÚquery_lengthÚ
key_lengthÚposition_ids_lÚposition_ids_rÚdistanceÚpositional_embeddingÚrelative_position_scoresÚrelative_position_scores_queryÚrelative_position_scores_keyÚattention_probsÚcontext_layerÚnew_context_layer_shapeÚoutputss                               r=   rN   zSplinterSelfAttention.forward   sç  € ð !ŸJ™J }Ó5Ðð
 3¸$Ð>Ðá .Ð"<à& qÑ)ˆIØ(¨Ñ+ˆKØ3ŠNÙØ×1Ñ1°$·(±(Ð;PÓ2QÓRˆIØ×3Ñ3°D·J±JÐ?TÓ4UÓVˆKØ3‰NØÐ'Ø×1Ñ1°$·(±(¸=Ó2IÓJˆIØ×3Ñ3°D·J±J¸}Ó4MÓNˆKÜŸ	™	 >°!Ñ#4°iÐ"@ÀaÔHˆIÜŸ)™) ^°AÑ%6¸Ð$DÈ!ÔL‰Kà×1Ñ1°$·(±(¸=Ó2IÓJˆIØ×3Ñ3°D·J±J¸}Ó4MÓNˆKà×/Ñ/Ð0AÓBˆà"¨$Ð.ˆ	Ø�?Š?ð (¨Ð5ˆNô !Ÿ<™<¨°Y×5HÑ5HÈÈRÓ5PÓQÐà×'Ñ'¨>Ò9¸T×=YÑ=YÐ]qÓ=qØ'2×'8Ñ'8¸Ñ';¸Y¿_¹_ÈQÑ=O˜*ˆLÙÜ!&§¡¨j¸1©nÄEÇJÁJÐWd×WkÑWkÔ!l×!qÑ!qØ˜ó"‘ô "'§¡¨lÄ%Ç*Á*ÐUb×UiÑUiÔ!j×!oÑ!oÐprÐtuÓ!v�Ü"Ÿ\™\¨*¼E¿J¹JÈ}×OcÑOcÔd×iÑiÐjkÐmoÓpˆNØ%¨Ñ6ˆHà#'×#:Ñ#:¸8Àd×FbÑFbÑ;bÐefÑ;fÓ#gÐ Ø#7×#:Ñ#:À×ARÑARÐ#:Ó#SÐ à×+Ñ+¨~Ò=Ü+0¯<©<Ð8HÈ+ÐWkÓ+lÐ(Ø#3Ð6NÑ#NÑ Ø×-Ñ-Ð1EÒEÜ16·±Ð>NÐP[Ð]qÓ1rÐ.Ü/4¯|©|Ð<LÈiÐYmÓ/nÐ,Ø#3Ð6TÑ#TÐWsÑ#sÐ à+¬d¯i©i¸×8PÑ8PÓ.QÑQÐØÐ%à/°.Ñ@Ðô Ÿ-™-×/Ñ/Ð0@ÀbÐ/ÓIˆð Ÿ,™, Ó7ˆð Ð Ø-°	Ñ9ˆOäŸ™ _°kÓBˆà%×-Ñ-¨a°°A°qÓ9×DÑDÓFˆØ"/×"4Ñ"4Ó"6°s¸Ð";¸t×?QÑ?QÐ>SÑ"SÐØ%×*Ñ*Ð+BÓCˆá6G�= /Ñ2ÈmÐM]ˆà�?Š?Ø Ð 1Ñ1ˆGØˆr>   ©N©NNNNNF)rO   rP   rQ   r%   r5   ÚTensorrr   r   rT   r   ÚboolrN   rV   rW   s   @r=   rY   rY   _   så   ø„ õ,ð4% e§l¡lð %°u·|±|ó %ð 7;Ø15Ø=AØ>BØDHØ,1ñcà—|‘|ðcð ! ×!2Ñ!2Ñ3ðcð ˜E×-Ñ-Ñ.ð	cð
  (¨×(9Ñ(9Ñ:ðcð !)¨×):Ñ):Ñ ;ðcð !  u¨U×->Ñ->Ñ'?Ñ!@ÑAðcð $ D™>ðcð 
ˆu�|‰|Ñ	÷cr>   rY   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   re   r(   Údenser/   r0   r1   r2   r3   r9   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>   rs   Úinput_tensorrC   c                 ór   — | j                  |«      }| j                  |«      }| j                  ||z   «      }|S rž   ©r¦   r3   r/   ©r:   rs   r§   s      r=   rN   zSplinterSelfOutput.forwardí   ó7   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆØŸ™ }°|Ñ'CÓDˆØÐr>   ©rO   rP   rQ   r%   r5   r    rN   rV   rW   s   @r=   r£   r£   æ   ó1   ø„ ô>ð U§\¡\ð ÀÇÁð ÐRW×R^ÑR^÷ r>   r£   Úeagerc                   ó  ‡ — e Zd Zdˆ fd„	Zd„ Z	 	 	 	 	 	 ddej                  deej                     deej                     deej                     deej                     dee	e	ej                           d	ee
   d
e	ej                     fd„Zˆ xZS )ÚSplinterAttentionc                 óž   •— t         ‰| �  «        t        |j                     ||¬«      | _        t        |«      | _        t        «       | _        y )N©r"   )	r$   r%   ÚSPLINTER_SELF_ATTENTION_CLASSESÚ_attn_implementationr:   r£   ÚoutputÚsetÚpruned_headsrl   s      €r=   r%   zSplinterAttention.__init__û   sC   ø€ Ü‰ÑÔÜ3°F×4OÑ4OÑPØÐ,Cô
ˆŒ	ô )¨Ó0ˆŒÜ›EˆÕr>   c                 ó>  — t        |«      dk(  ry t        || j                  j                  | j                  j                  | j
                  «      \  }}t        | j                  j                  |«      | j                  _        t        | j                  j                  |«      | j                  _        t        | j                  j                  |«      | j                  _	        t        | j                  j                  |d¬«      | j                  _        | j                  j                  t        |«      z
  | j                  _        | j                  j                  | j                  j                  z  | j                  _        | j
                  j                  |«      | _        y )Nr   r   r{   )Úlenr   r:   r`   rc   r·   r   rf   rg   rh   rµ   r¦   rd   Úunion)r:   ÚheadsÚindexs      r=   Úprune_headszSplinterAttention.prune_heads  s  € Üˆu‹:˜Š?ØÜ7Ø�4—9‘9×0Ñ0°$·)±)×2OÑ2OÐQU×QbÑQbó
‰ˆˆuô
 -¨T¯Y©Y¯_©_¸eÓDˆ�	‰	ŒÜ*¨4¯9©9¯=©=¸%Ó@ˆ�	‰	ŒÜ,¨T¯Y©Y¯_©_¸eÓDˆ�	‰	ŒÜ.¨t¯{©{×/@Ñ/@À%ÈQÔOˆ�‰Ôð )-¯	©	×(EÑ(EÌÈEË
Ñ(Rˆ�	‰	Ô%Ø"&§)¡)×"?Ñ"?À$Ç)Á)×B_ÑB_Ñ"_ˆ�	‰	ÔØ ×-Ñ-×3Ñ3°EÓ:ˆÕr>   rs   rt   ru   rv   rw   rx   ry   rC   c           	      óp   — | j                  |||||||«      }| j                  |d   |«      }	|	f|dd  z   }
|
S )Nr   r   )r:   rµ   )r:   rs   rt   ru   rv   rw   rx   ry   Úself_outputsÚattention_outputr�   s              r=   rN   zSplinterAttention.forward  sW   € ð —y‘yØØØØ!Ø"ØØó
ˆð  Ÿ;™; |°A¡¸ÓFÐØ#Ð%¨°Q°RÐ(8Ñ8ˆØˆr>   rž   rŸ   )rO   rP   rQ   r%   r½   r5   r    r   rT   r   r¡   rN   rV   rW   s   @r=   r°   r°   ú   sÆ   ø„ õ"ò;ð* 7;Ø15Ø=AØ>BØDHØ,1ñà—|‘|ðð ! ×!2Ñ!2Ñ3ðð ˜E×-Ñ-Ñ.ð	ð
  (¨×(9Ñ(9Ñ:ðð !)¨×):Ñ):Ñ ;ðð !  u¨U×->Ñ->Ñ'?Ñ!@ÑAðð $ D™>ðð 
ˆu�|‰|Ñ	÷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   re   r(   Úintermediate_sizer¦   Ú
isinstanceÚ
hidden_actÚstrr   Úintermediate_act_fnr9   s     €r=   r%   zSplinterIntermediate.__init__/  s]   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3KÑ3KÓLˆŒ
Ü�f×'Ñ'¬Ô-Ü'-¨f×.?Ñ.?Ñ'@ˆDÕ$à'-×'8Ñ'8ˆDÕ$r>   rs   rC   c                 óJ   — | j                  |«      }| j                  |«      }|S rž   )r¦   rÈ   )r:   rs   s     r=   rN   zSplinterIntermediate.forward7  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   re   rÄ   r(   r¦   r/   r0   r1   r2   r3   r9   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>   rs   r§   rC   c                 ór   — | j                  |«      }| j                  |«      }| j                  ||z   «      }|S rž   r©   rª   s      r=   rN   zSplinterOutput.forwardE  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ej                     deej                     deej                     deej                     deeeej                           dee	   d	eej
                     fd
„Z
d„ Zˆ xZS )ÚSplinterLayerc                 óf  •— t         ‰| �  «        |j                  | _        d| _        t	        |«      | _        |j                  | _        |j                  | _        | j                  r,| j                  st        | › d�«      ‚t	        |d¬«      | _	        t        |«      | _        t        |«      | _        y )Nr   z> should be used as a decoder model if cross attention is addedr#   r²   )r$   r%   Úchunk_size_feed_forwardÚseq_len_dimr°   Ú	attentionrk   Úadd_cross_attentionrb   ÚcrossattentionrÂ   ÚintermediaterË   rµ   r9   s     €r=   r%   zSplinterLayer.__init__N  s—   ø€ Ü‰ÑÔØ'-×'EÑ'EˆÔ$ØˆÔÜ*¨6Ó2ˆŒØ ×+Ñ+ˆŒØ#)×#=Ñ#=ˆÔ Ø×#Ò#Ø—?’?Ü  D 6Ð)gÐ!hÓiÐiÜ"3°FÐT^Ô"_ˆDÔÜ0°Ó8ˆÔÜ$ VÓ,ˆ�r>   rs   rt   ru   rv   rw   rx   ry   rC   c           	      óÒ  — |�|d d nd }| j                  |||||¬«      }	|	d   }
| j                  r|	dd }|	d   }n|	dd  }d }| j                  rT|�Rt        | d«      st        d| › d�«      ‚|�|d	d  nd }| j	                  |
||||||«      }|d   }
||dd z   }|d   }|z   }t        | j                  | j                  | j                  |
«      }|f|z   }| j                  r|fz   }|S )
Nr_   )ry   rx   r   r   r    rÕ   z'If `encoder_hidden_states` are passed, z` has to be instantiated with cross-attention layers by setting `config.add_cross_attention=True`r}   )	rÓ   rk   ra   rb   rÕ   r   Úfeed_forward_chunkrÑ   rÒ   )r:   rs   rt   ru   rv   rw   rx   ry   Úself_attn_past_key_valueÚself_attention_outputsrÀ   r�   Úpresent_key_valueÚcross_attn_present_key_valueÚcross_attn_past_key_valueÚcross_attention_outputsÚlayer_outputs                    r=   rN   zSplinterLayer.forward\  s}  € ð :HÐ9S >°"°1Ñ#5ÐY]Ð Ø!%§¡ØØØØ/Ø3ð "0ó "
Ðð 2°!Ñ4Ðð �?Š?Ø,¨Q¨rÐ2ˆGØ 6°rÑ :Ñà,¨Q¨RÐ0ˆGà'+Ð$Ø�?Š?Ð4Ð@Ü˜4Ð!1Ô2Ü Ø=¸d¸Vð DDð Dóð ð @NÐ?Y¨°r°sÑ(;Ð_cÐ%Ø&*×&9Ñ&9Ø ØØØ%Ø&Ø)Ø!ó'Ð#ð  7°qÑ9ÐØÐ 7¸¸"Ð =Ñ=ˆGð ,CÀ2Ñ+FÐ(Ø 1Ð4PÑ PÐä0Ø×#Ñ# T×%AÑ%AÀ4×CSÑCSÐUeó
ˆð  �/ GÑ+ˆð �?Š?ØÐ!2Ð 4Ñ4ˆGàˆr>   c                 óL   — | j                  |«      }| j                  ||«      }|S rž   )rÖ   rµ   )r:   rÀ   Úintermediate_outputrß   s       r=   rØ   z SplinterLayer.feed_forward_chunk�  s,   € Ø"×/Ñ/Ð0@ÓAÐØ—{‘{Ð#6Ð8HÓIˆØÐr>   rŸ   )rO   rP   rQ   r%   r5   r    r   rT   r   r¡   rN   rØ   rV   rW   s   @r=   rÏ   rÏ   M  sÇ   ø„ ô-ð" 7;Ø15Ø=AØ>BØDHØ,1ñ?à—|‘|ð?ð ! ×!2Ñ!2Ñ3ð?ð ˜E×-Ñ-Ñ.ð	?ð
  (¨×(9Ñ(9Ñ:ð?ð !)¨×):Ñ):Ñ ;ð?ð !  u¨U×->Ñ->Ñ'?Ñ!@ÑAð?ð $ D™>ð?ð 
ˆu�|‰|Ñ	ó?öBr>   rÏ   c                   óD  ‡ — e Zd Zˆ fd„Z	 	 	 	 	 	 	 	 	 ddej
                  deej                     deej                     deej                     deej                     deeeej                           dee	   d	ee	   d
ee	   dee	   de
eej
                     ef   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;   Ú_r<   s      €r=   r%   zSplinterEncoder.__init__¥  sN   ø€ Ü‰ÑÔØˆŒÜ—]‘]Ä5È×IaÑIaÓCbÖ#c¸a¤M°&Õ$9Ò#cÓdˆŒ
Ø&+ˆÕ#ùò $ds   ½A#rs   rt   ru   rv   rw   Úpast_key_valuesr�   ry   Úoutput_hidden_statesÚreturn_dictrC   c                 óš  — |	rdnd }|rdnd }|r| j                   j                  rdnd }| j                  r%| j                  r|rt        j                  d«       d}|rdnd }t        | j                  «      D ]¤  \  }}|	r||fz   }|�||   nd }|�||   nd }| j                  r/| j                  r#| j                  |j                  |||||||«      }n ||||||||«      }|d   }|r	||d   fz  }|sŒ|||d   fz   }| j                   j                  sŒœ||d   fz   }Œ¦ |	r||fz   }|
st        d„ |||||fD «       «      S t        |||||¬	«      S )
N© zZ`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...Fr   r    r   r_   c              3   ó$   K  — | ]  }|�|–— Œ
 y ­wrž   rï   )Ú.0Úvs     r=   ú	<genexpr>z*SplinterEncoder.forward.<locals>.<genexpr>í  s   è ø€ ò 
àð �=ô ñ
ùs   ‚©Úlast_hidden_staterë   rs   Ú
attentionsÚcross_attentions)r;   rÔ   ré   ÚtrainingÚloggerÚwarning_onceÚ	enumeraterè   Ú_gradient_checkpointing_funcÚ__call__Útupler   )r:   rs   rt   ru   rv   rw   rë   r�   ry   rì   rí   Úall_hidden_statesÚall_self_attentionsÚall_cross_attentionsÚnext_decoder_cacheÚiÚlayer_moduleÚlayer_head_maskrx   Úlayer_outputss                       r=   rN   zSplinterEncoder.forward«  sÎ  € ñ #7™B¸DÐÙ$5™b¸4ÐÙ%6¸4¿;¹;×;ZÒ;Z™rÐ`dÐà×&Ò&¨4¯=ª=ÙÜ×#Ñ#Øpôð "�	á#,™R°$ÐÜ(¨¯©Ó4ò #	V‰OˆAˆ|Ù#Ø$5¸Ð8HÑ$HÐ!à.7Ð.C˜i¨šlÈˆOØ3BÐ3N˜_¨QÒ/ÐTXˆNà×*Ò*¨t¯}ª}Ø $× AÑ AØ ×)Ñ)Ø!Ø"Ø#Ø)Ø*Ø"Ø%ó	!‘ñ !-Ø!Ø"Ø#Ø)Ø*Ø"Ø%ó!�ð *¨!Ñ,ˆMÙØ" }°RÑ'8Ð&:Ñ:Ð"Ú Ø&9¸]È1Ñ=MÐ<OÑ&OÐ#Ø—;‘;×2Ó2Ø+?À=ÐQRÑCSÐBUÑ+UÑ(ðG#	VñJ  Ø 1°]Ð4DÑ DÐáÜñ 
ð "Ø&Ø%Ø'Ø(ðô
ó 
ð 
ô 9Ø+Ø.Ø+Ø*Ø1ô
ð 	
r>   )	NNNNNNFFT)rO   rP   rQ   r%   r5   r    r   rT   r   r¡   r   r   rN   rV   rW   s   @r=   rã   rã   ¤  s  ø„ ô,ð 7;Ø15Ø=AØ>BØEIØ$(Ø,1Ø/4Ø&*ñS
à—|‘|ðS
ð ! ×!2Ñ!2Ñ3ðS
ð ˜E×-Ñ-Ñ.ð	S
ð
  (¨×(9Ñ(9Ñ:ðS
ð !)¨×):Ñ):Ñ ;ðS
ð " %¨¨e×.?Ñ.?Ñ(@Ñ"AÑBðS
ð ˜D‘>ðS
ð $ D™>ðS
ð ' t™nðS
ð ˜d‘^ðS
ð 
ˆu�U—\‘\Ñ"Ð$MÐMÑ	N÷S
r>   rã   c                   ó"   — e Zd ZdZeZdZdZd„ Zy)ÚSplinterPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    ÚsplinterTc                 ó  — t        |t        j                  «      rm|j                  j                  j                  d| j                  j                  ¬«       |j                  �%|j                  j                  j                  «        yyt        |t        j                  «      rz|j                  j                  j                  d| j                  j                  ¬«       |j                  �2|j                  j                  |j                     j                  «        yyt        |t        j                  «      rJ|j                  j                  j                  «        |j                  j                  j                  d«       yy)zInitialize the weightsg        )ÚmeanÚstdNg      ð?)rÅ   r   re   ÚweightÚdataÚnormal_r;   Úinitializer_rangeÚbiasÚzero_r&   r   r/   Úfill_)r:   Úmodules     r=   Ú_init_weightsz%SplinterPreTrainedModel._init_weights  s  € ä�fœbŸi™iÔ(ð �M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜¤§¡Ô-Ø�M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ×!Ñ!Ð-Ø—‘×"Ñ" 6×#5Ñ#5Ñ6×<Ñ<Õ>ð .ä˜¤§¡Ô-Ø�K‰K×Ñ×"Ñ"Ô$Ø�M‰M×Ñ×$Ñ$ SÕ)ð .r>   N)	rO   rP   rQ   rR   r   Úconfig_classÚbase_model_prefixÚsupports_gradient_checkpointingr  rï   r>   r=   r  r    s   „ ñð
 "€LØ"ÐØ&*Ð#ó*r>   r  aK  
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`SplinterConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
a/
  
    Args:
        input_ids (`torch.LongTensor` of shape `({0})`):
            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)
        attention_mask (`torch.FloatTensor` of shape `{0}`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)
        token_type_ids (`torch.LongTensor` of shape `{0}`, *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 `{0}`, *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)
        head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules. Mask values selected in `[0, 1]`:

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.

        inputs_embeds (`torch.FloatTensor` of shape `({0}, 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.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
zbThe bare Splinter Model transformer outputting raw hidden-states without any specific head on top.c            !       óò  ‡ — e Zd ZdZˆ fd„Zd„ Zd„ Zd„ Z ee	j                  d«      «       eeee¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 ddeej"                     d	eej"                     d
eej"                     deej"                     deej"                     deej"                     deej"                     deej"                     deeej&                        dee   dee   dee   dee   deeef   fd„«       «       Zˆ xZS )ÚSplinterModela*  
    The model is an encoder (with only self-attention) following the architecture described in [Attention is all you
    need](https://arxiv.org/abs/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_initr9   s     €r=   r%   zSplinterModel.__init__d  s;   ø€ Ü‰Ñ˜Ô ØˆŒä,¨VÓ4ˆŒÜ& vÓ.ˆŒð 	�‰Õr>   c                 ó.   — | j                   j                  S rž   ©rM   r*   )r:   s    r=   Úget_input_embeddingsz"SplinterModel.get_input_embeddingsn  s   € Ø�‰×.Ñ.Ð.r>   c                 ó&   — || j                   _        y rž   r  )r:   rh   s     r=   Úset_input_embeddingsz"SplinterModel.set_input_embeddingsq  s   € Ø*/ˆ�‰Õ'r>   c                 ó˜   — |j                  «       D ]7  \  }}| j                  j                  |   j                  j	                  |«       Œ9 y)z�
        Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
        class PreTrainedModel
        N)Úitemsr  rè   rÓ   r½   )r:   Úheads_to_prunerè   r»   s       r=   Ú_prune_headszSplinterModel._prune_headst  sE   € ð
 +×0Ñ0Ó2ò 	C‰LˆE�5Ø�L‰L×Ñ˜uÑ%×/Ñ/×;Ñ;¸EÕBñ	Cr>   úbatch_size, sequence_length©Ú
checkpointÚoutput_typer  r?   rt   r@   r   ru   rA   rv   rw   rë   r�   ry   rì   rí   rC   c                 óÆ  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }| j                   j                  r|
�|
n| j                   j
                  }
nd}
|�|�t        d«      ‚|�#| j                  ||«       |j                  «       }n!|�|j                  «       dd }nt        d«      ‚|\  }}|�|j                  n|j                  }|	�|	d   d   j                  d   nd}|€t        j                  |||z   f|¬«      }|€&t        j                  |t        j                  |¬	«      }| j                  ||«      }| j                   j                  rE|�C|j                  «       \  }}}||f}|€t        j                  ||¬«      }| j!                  |«      }nd}| j#                  || j                   j$                  «      }| j'                  |||||¬
«      }| j)                  ||||||	|
|||¬«
      }|d   }|s	|f|dd z   S t+        ||j,                  |j.                  |j0                  |j2                  ¬«      S )a  
        encoder_hidden_states  (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if
            the model is configured as a decoder.
        encoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used in
            the cross-attention if the model is configured as a decoder. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.
        past_key_values (`tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`):
            Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding.
            If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
            don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
            `decoder_input_ids` of shape `(batch_size, sequence_length)`.
        use_cache (`bool`, *optional*):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past_key_values`).
        NFzDYou cannot specify both input_ids and inputs_embeds at the same timer    z5You have to specify either input_ids or inputs_embedsr   r_   )rG   rE   )r?   r   r@   rA   rB   )	rt   ru   rv   rw   rë   r�   ry   rì   rí   r   rô   )r;   ry   rì   Úuse_return_dictrk   r�   rb   Ú%warn_if_padding_and_no_attention_maskrH   rG   r�   r5   ÚonesrI   rJ   Úget_extended_attention_maskÚinvert_attention_maskÚget_head_maskrç   rM   r  r   rë   rs   rö   r÷   )r:   r?   rt   r@   r   ru   rA   rv   rw   rë   r�   ry   rì   rí   rK   Ú
batch_sizerL   rG   rB   Úextended_attention_maskÚencoder_batch_sizeÚencoder_sequence_lengthrê   Úencoder_hidden_shapeÚencoder_extended_attention_maskÚembedding_outputÚencoder_outputsÚsequence_outputs                               r=   rN   zSplinterModel.forward|  s®  € ðR 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà�;‰;×!Ò!Ø%.Ð%:™	ÀÇÁ×@UÑ@U‰IàˆIàÐ  ]Ð%>ÜÐcÓdÐdØÐ"Ø×6Ñ6°yÀ.ÔQØ#Ÿ.™.Ó*‰KØÐ&Ø'×,Ñ,Ó.¨s°Ð3‰KäÐTÓUÐUà!,Ñˆ
�JØ%.Ð%:�×!Ò!À×@TÑ@Tˆð DSÐC^ °Ñ!3°AÑ!6×!<Ñ!<¸QÒ!?ÐdeÐàÐ!Ü"ŸZ™Z¨*°jÐCYÑ6YÐ)ZÐdjÔkˆNØÐ!Ü"Ÿ[™[¨¼E¿J¹JÈvÔVˆNð 15×0PÑ0PÐQ_ÐalÓ0mÐð �;‰;×!Ò!Ð&;Ð&GØ=R×=WÑ=WÓ=YÑ:ÐÐ 7¸Ø$6Ð8OÐ#PÐ Ø%Ð-Ü).¯©Ð4HÐQWÔ)XÐ&Ø.2×.HÑ.HÐI_Ó.`Ñ+à.2Ð+ð ×&Ñ& y°$·+±+×2OÑ2OÓPˆ	àŸ?™?ØØ%Ø)Ø'Ø#9ð +ó 
Ðð Ÿ,™,ØØ2ØØ"7Ø#BØ+ØØ/Ø!5Ø#ð 'ó 
ˆð *¨!Ñ,ˆáØ#Ð%¨¸¸Ð(;Ñ;Ð;ä8Ø-Ø+×;Ñ;Ø)×7Ñ7Ø&×1Ñ1Ø,×=Ñ=ô
ð 	
r>   )NNNNNNNNNNNNN)rO   rP   rQ   rR   r%   r   r"  r&  r   ÚSPLINTER_INPUTS_DOCSTRINGÚformatr   Ú_CHECKPOINT_FOR_DOCr   Ú_CONFIG_FOR_DOCr   r5   r    r   rT   r¡   r   r   rN   rV   rW   s   @r=   r  r  Y  s�  ø„ ñ
ôò/ò0òCñ +Ð+D×+KÑ+KÐLiÓ+jÓkÙØ&Ø=Ø$ôð -1Ø15Ø15Ø/3Ø,0Ø04Ø8<Ø9=Ø=AØ$(Ø,0Ø/3Ø&*ñw
à˜EŸL™LÑ)ðw
ð ! §¡Ñ.ðw
ð ! §¡Ñ.ð	w
ð
 ˜uŸ|™|Ñ,ðw
ð ˜EŸL™LÑ)ðw
ð   §¡Ñ-ðw
ð  (¨¯©Ñ5ðw
ð !)¨¯©Ñ 6ðw
ð " $ u×'8Ñ'8Ñ"9Ñ:ðw
ð ˜D‘>ðw
ð $ D™>ðw
ð ' t™nðw
ð ˜d‘^ðw
ð 
ˆuÐ?Ð?Ñ	@òw
óó lôw
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   re   r¦   r   Úact_fnr/   )r:   rB  rC  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¦   rD  r/   )r:   rE  rs   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)r  )r$   r%   r@  r(   Úquery_start_transformÚquery_end_transformÚstart_transformÚend_transformr   re   Ústart_classifierÚend_classifierr9   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|   r¼   r   r_   )rH   Ú	unsqueezeÚrepeatr5   ÚgatherrK  rL  rM  rN  rO  rp   r   rP  )r:   rE  Ú	positionsrê   r|   r¼   Úgathered_repsÚquery_start_repsÚquery_end_repsÚ
start_repsÚend_repsrs   Ú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=   rI  rI    s   ø„ ñô
	\ö(r>   rI  zá
    Splinter Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear
    layers on top of the hidden-states output to compute `span start logits` and `span end logits`).
    c                   óÊ  ‡ — e Zd Zˆ fd„Z eej                  d«      «       eee	e
¬«      	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     deej                     deej                     d	eej                     d
eej                     deej                     dee   dee   dee   deej                     deee	f   fd„«       «       Zˆ xZS )ÚSplinterForQuestionAnsweringc                 ó¦   •— t         ‰| �  |«       t        |«      | _        t	        |«      | _        |j                  | _        | j                  «        y rž   ©r$   r%   r  r	  rI  Úsplinter_qassÚquestion_token_idr  r9   s     €r=   r%   z%SplinterForQuestionAnswering.__init__<  óC   ø€ Ü‰Ñ˜Ô ä% fÓ-ˆŒÜ;¸FÓCˆÔØ!'×!9Ñ!9ˆÔð 	�‰Õr>   r'  r(  r?   rt   r@   r   ru   rA   Ústart_positionsÚend_positionsry   rì   rí   Úquestion_positionsrC   c                 óê  — |�|n| j                   j                  }d}|€£|�Dt        j                  t        j                  || j
                  «      j                  «       d¬«      }nJt        j                  |j                  d«      t        j                  |j                  |j                  ¬«      }|j                  d«      }d}| j                  |||||||	|
|¬«	      }|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  }|s||f|d	d z   }|�|f|z   S |S t-        ||||j.                  |j0                  ¬«      S )aé  
        start_positions (`torch.LongTensor` of shape `(batch_size,)`, *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,)`, *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)`.
        NFr    r{   r   )rF   ÚlayoutrG   T©rt   r@   r   ru   rA   ry   rì   rí   r   ©Úignore_indexr_   ©Úlossr[  r\  rs   rö   )r;   r,  r5   ÚargmaxÚeqrb  rU   rI   rH   rJ   rh  rG   rR  r	  ra  ÚsqueezeÚfinforF   Úminr¹   Úclamp_r	   r   rs   rö   )r:   r?   rt   r@   r   ru   rA   rd  re  ry   rì   rí   rf  Úquestion_positions_were_noneÚ"question_position_for_each_exampler�   r:  r[  r\  Ú
total_lossÚignored_indexÚloss_fctÚ
start_lossÚend_lossrµ   s                            r=   rN   z$SplinterForQuestionAnswering.forwardF  s‹  € ðH &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà',Ð$ØÐ%ØÐ$Ü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Ñ#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áØ" JÐ/°'¸!¸"°+Ñ=ˆFØ/9Ð/E�Z�M FÑ*ÐQÈ6ÐQä+ØØ%Ø!Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r>   ©NNNNNNNNNNNN)rO   rP   rQ   r%   r   r;  r<  r   r=  r   r>  r   r5   r    rS   r¡   r   r   rN   rV   rW   s   @r=   r^  r^  4  sh  ø„ ôñ +Ð+D×+KÑ+KÐLiÓ+jÓkÙØ&Ø0Ø$ôð -1Ø15Ø15Ø/3Ø,0Ø04Ø6:Ø48Ø,0Ø/3Ø&*Ø9=ñ^
à˜EŸL™LÑ)ð^
ð ! §¡Ñ.ð^
ð ! §¡Ñ.ð	^
ð
 ˜uŸ|™|Ñ,ð^
ð ˜EŸL™LÑ)ð^
ð   §¡Ñ-ð^
ð " %×"2Ñ"2Ñ3ð^
ð   × 0Ñ 0Ñ1ð^
ð $ D™>ð^
ð ' t™nð^
ð ˜d‘^ð^
ð % U×%5Ñ%5Ñ6ð^
ð 
ˆuÐ2Ð2Ñ	3ò^
óó lô^
r>   r^  c                   óæ   — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eej                     ed<   dZeeej                        ed<   dZeeej                        ed<   y)ÚSplinterForPreTrainingOutputaê  
    Class for outputs of Splinter as a span selection model.

    Args:
        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).
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
            one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
        attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.
    Nrm  r[  r\  rs   rö   )rO   rP   rQ   rR   rm  r   r5   rT   Ú__annotations__r[  r\  rs   r   rö   rï   r>   r=   r}  r}  ­  s|   … ñð. )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø04€L�(˜5×,Ñ,Ñ-Ó4Ø.2€J�˜×*Ñ*Ñ+Ó2Ø8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ó<Ø59€J�˜˜u×0Ñ0Ñ1Ñ2Ô9r>   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j                  d«      «      	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     deej                     deej                     deej                     d	eej                     d
eej                     dee   dee   dee   deej                     deeef   fd„«       Zdej                  dej                  fd„Zˆ xZS )ÚSplinterForPreTrainingc                 ó¦   •— t         ‰| �  |«       t        |«      | _        t	        |«      | _        |j                  | _        | j                  «        y rž   r`  r9   s     €r=   r%   zSplinterForPreTraining.__init__Ö  rc  r>   z*batch_size, num_questions, sequence_lengthr?   rt   r@   r   ru   rA   rd  re  ry   rì   rí   rf  rC   c                 ób  — |�|n| j                   j                  }|€|�|�t        d«      ‚|€|€t        d«      ‚|€| j                  |«      }| j	                  |||||||	|
|¬«	      }|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  }|s||f|dd z   }|�|f|z   S |S t%        ||||j&                  |j(                  ¬	«      S )
a  
        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 input_embeds is usedri  r   r   rj  r_   rl  )r;   r,  Ú	TypeErrorÚ_prepare_question_positionsr	  rH   ra  rR  r7   r5   rq  rF   rr  rs  Úmaxr	   r)   ro   r}  rs   rö   )r:   r?   rt   r@   r   ru   rA   rd  re  ry   rì   rí   rf  r�   r:  r2  Úsequence_lengthr|   r[  r\  Únum_questionsÚ attention_mask_for_each_questionrv  rx  ry  rz  rµ   s                              r=   rN   zSplinterForPreTraining.forwardà  s‰  € ðB &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐ%¨/Ð*EÈ-ÐJcÜÐaÓbÐbàÐ'¨IÐ,=ÜÐ\Ó]Ð]àÐ'Ø!%×!AÑ!AÀ)Ó!LÐà—-‘-ØØ)Ø)Ø%ØØ'Ø/Ø!5Ø#ð  ó 

ˆð " !™*ˆØ+:×+?Ñ+?Ó+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áØ" JÐ/°'¸!¸"°+Ñ=ˆFØ/9Ð/E�Z�M FÑ*ÐQÈ6ÐQä+ØØ%Ø!Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r>   c                 óÚ  — t        j                  || j                  j                  k(  «      \  }}t        j                  |«      }t        j
                  |j                  d«      |j                  «       f| j                  j                  t         j                  |j                  ¬«      }t        j                  |D �cg c]  }t        j                  |«      ‘Œ c}«      }||||f<   |S c c}w )Nr   rE   )r5   Úwherer;   rb  ÚbincountÚfullrH   r…  r)   rJ   rG   r~   r6   )r:   r?   ÚrowsÚflat_positionsr‡  rU  ÚnÚcolss           r=   r„  z2SplinterForPreTraining._prepare_question_positionsG  s´   € Ü$Ÿ{™{¨9¸¿¹×8UÑ8UÑ+UÓVÑˆˆnÜŸ™ tÓ,ˆÜ—J‘JØ�^‰^˜AÓ × 1Ñ 1Ó 3Ð4Ø�K‰K×$Ñ$Ü—*‘*Ø×#Ñ#ô	
ˆ	ô �y‰y°=ÖA¨aœ%Ÿ,™, q�/ÒAÓBˆØ .ˆ	�$˜�*ÑØÐùò Bs   Â<C(r{  )rO   rP   rQ   r%   r   r;  r<  r   r5   r    rS   r¡   r   r   r}  rN   r„  rV   rW   s   @r=   r€  r€  Í  so  ø„ ôñ +Ø!×(Ñ(Ð)UÓVóð
 -1Ø15Ø15Ø/3Ø,0Ø04Ø6:Ø48Ø,0Ø/3Ø&*Ø9=ñb
à˜EŸL™LÑ)ðb
ð ! §¡Ñ.ðb
ð ! §¡Ñ.ð	b
ð
 ˜uŸ|™|Ñ,ðb
ð ˜EŸL™LÑ)ðb
ð   §¡Ñ-ðb
ð " %×"2Ñ"2Ñ3ðb
ð   × 0Ñ 0Ñ1ðb
ð $ D™>ðb
ð ' t™nðb
ð ˜d‘^ðb
ð % U×%5Ñ%5Ñ6ðb
ð 
ˆuÐ2Ð2Ñ	3òb
óðb
ðH°U·\±\ð ÀeÇlÁl÷ r>   r€  )r^  r€  rÏ   r  r  ):rR   r…   Údataclassesr   Útypingr   r   r   r   r5   Útorch.utils.checkpointr   Útorch.nnr	   Úactivationsr   Úmodeling_outputsr   r   r   Úmodeling_utilsr   Úpytorch_utilsr   r   r   Úutilsr   r   r   r   Úconfiguration_splinterr   Ú
get_loggerrO   rù   r=  r>  ÚModuler   rY   r£   r³   r°   rÂ   rË   rÏ   rã   r  ÚSPLINTER_START_DOCSTRINGr;  r  r@  rI  r^  r}  r€  Ú__all__rï   r>   r=   ú<module>rŸ     sà  ðñ ã Ý !ß /Ó /ã Û Ý Ý %å !ß tÑ tÝ -ß lÑ lß uÓ uÝ 2ð 
ˆ×	Ñ	˜HÓ	%€à)Ð Ø"€ô3˜Ÿ™ô 3ônC˜BŸI™Iô CôN˜Ÿ™ô ð Ð"ð#Ð ô0˜Ÿ	™	ô 0ôh˜2Ÿ9™9ô ô �R—Y‘Yô ôS�B—I‘Iô SônZ
�b—i‘iô Z
ôz*˜oô *ð6	Ð ð/Ð ñd ØhØóô\
Ð+ó \
ó	ð\
ô~ "§)¡)ô ô$#( R§Y¡Yô #(ñL ðð óôo
Ð#:ó o
óðo
ðd ô: ;ó :ó ð:ñ> ðð
 óô}Ð4ó }óð}ò@�r>   