Ë
    S^(h,¼  ã                  óö  — d Z ddlmZ ddlmZmZmZ ddlZddl	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mZmZmZmZmZmZmZmZm Z  dd
l!m"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z(  e&jR                  e*«      Z+dZ,dZ-d,d„Z.d-d„Z/d.d„Z0 G d„ dejb                  jd                  «      Z3 G d„ dejb                  jd                  «      Z4 G d„ dejb                  jd                  «      Z5e G d„ dejb                  jd                  «      «       Z6 G d„ de«      Z7dZ8dZ9 ede8«       G d „ d!e7«      «       Z: ed"e8«       G d#„ d$e7e«      «       Z; ed%e8«       G d&„ d'e7e«      «       Z< ed(e8«       G d)„ d*e7e«      «       Z=g d+¢Z>y)/zTF 2.0 GPT-J model.é    )Úannotations)ÚOptionalÚTupleÚUnionNé   )Úget_tf_activation)Úadd_code_sample_docstringsÚadd_start_docstringsÚ%add_start_docstrings_to_model_forward)ÚTFBaseModelOutputWithPastÚTFCausalLMOutputWithPastÚTFQuestionAnsweringModelOutputÚ"TFSequenceClassifierOutputWithPast)
ÚTFCausalLanguageModelingLossÚTFModelInputTypeÚTFPreTrainedModelÚTFQuestionAnsweringLossÚTFSequenceClassificationLossÚTFSharedEmbeddingsÚget_initializerÚkerasÚkeras_serializableÚunpack_inputs)Úcheck_embeddings_within_boundsÚ
shape_listÚstable_softmax)Úloggingé   )Ú
GPTJConfigzEleutherAI/gpt-j-6Br   c           	     óÈ  — t        j                  ddt        j                  d|d«      |z  z  z  t         j                  «      }t        j                  t        j                  dt        j                  | t         j                  ¬«      |«      t         j                  «      }t        j
                  |«      t        j                  |«      }}t        j                  ||fd¬«      }|S )	Nç      ð?i'  r   é   zi , j -> i j©Údtyper   ©Úaxis)ÚtfÚcastÚrangeÚfloat32ÚeinsumÚsinÚcosÚconcat)Únum_posÚdimÚinv_freqÚsinusoid_inpr,   r-   Úouts          úg/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/gptj/modeling_tf_gptj.pyÚcreate_sinusoidal_positionsr5   ;   sš   € Ü�w‰w�s˜e¬¯©°°C¸Ó(;¸cÑ(AÑBÑCÄRÇZÁZÓP€HÜ—7‘7œ2Ÿ9™9 ^´R·X±X¸gÌRÏZÉZÔ5XÐZbÓcÔeg×eoÑeoÓp€LÜ�v‰v�lÓ#¤R§V¡V¨LÓ%9ˆ€CÜ
�)‰)�S˜#�J QÔ
'€CØ€Jó    c           
     ó  — t        j                  | d d …d d …d d …dd d…f    | d d …d d …d d …d d d…f   fd¬«      }t        |«      d d t         j                  j	                  t        |«      dd  «      gz   }t        j
                  ||«      }|S )Nr   r"   éÿÿÿÿr%   éþÿÿÿ)r'   Ústackr   ÚmathÚreduce_prodÚreshape)ÚxÚrotate_half_tensorÚ	new_shapes      r4   Úrotate_every_tworA   C   sŽ   € ÜŸ™ A¢aªªA¨q¨t°!¨t mÑ$4Ð#4°aºº1ºaÁÀ1À¸±oÐ"FÈRÔPÐÜÐ-Ó.¨s°Ð3´r·w±w×7JÑ7JÌ:ÐVhÓKiÐjlÐjmÐKnÓ7oÐ6pÑp€IÜŸ™Ð$6¸	ÓBÐØÐr6   c                óÄ   — |\  }}t        j                  |d d …d d …d d d …f   dd«      }t        j                  |d d …d d …d d d …f   dd«      }| |z  t        | «      |z  z   S )Nr"   r   )r'   ÚrepeatrA   )ÚtensorÚsincosÚsin_posÚcos_poss       r4   Úapply_rotary_pos_embrH   J   se   € ØÑ€GˆWÜ�i‰i˜¢¢1 dªA Ñ.°°1Ó5€GÜ�i‰i˜¢¢1 dªA Ñ.°°1Ó5€GØ�WÑÔ!1°&Ó!9¸GÑ!CÑDÐDr6   c                  ó¦   ‡ — e Zd Zd	ˆ fd„Zd
d„Zedd„«       Zdd„Zdd„Z	 	 d	 	 	 	 	 	 	 	 	 	 	 dd„Z		 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z
dd„Zˆ xZS )ÚTFGPTJAttentionc           
     ó<  •— t        ‰| �  di |¤Ž |j                  | _        |j                  | _        | j                  | j                  z  | _        | j
                  | j                  z  | j                  k7  r&t        d| j                  › d| j                  › d�«      ‚| j
                  dz  | _        |j                  | _        t        j                  j                  |j                  «      | _        t        j                  j                  |j                  «      | _        t        j                  j!                  | j                  dt#        |j$                  «      d¬«      | _        t        j                  j!                  | j                  dt#        |j$                  «      d¬«      | _        t        j                  j!                  | j                  dt#        |j$                  «      d	¬«      | _        t        j                  j!                  | j                  dt#        |j$                  «      d
¬«      | _        |j.                  | _        t3        j4                  t3        j6                  t2        j8                  j:                  j=                  t3        j>                  | j0                  | j0                  f«      «      t2        j@                  «      dd| j0                  | j0                  f«      | _!        | j                  xs | j                  }tE        | j0                  |«      | _#        y )NzEembed_dim must be divisible by num_attention_heads (got `embed_dim`: z and `num_attention_heads`: z).g      à?FÚq_proj©Úuse_biasÚkernel_initializerÚnameÚk_projÚv_projÚout_projr   © )$ÚsuperÚ__init__Úhidden_sizeÚ	embed_dimÚnum_attention_headsÚhead_dimÚ
ValueErrorÚ
scale_attnÚ
rotary_dimr   ÚlayersÚDropoutÚ
attn_pdropÚattn_dropoutÚresid_pdropÚresid_dropoutÚDenser   Úinitializer_rangerL   rQ   rR   rS   Úmax_position_embeddingsÚmax_positionsr'   r=   r(   ÚexperimentalÚnumpyÚtrilÚonesÚint8Úlower_triangle_maskr5   Úembed_positions)ÚselfÚconfigÚkwargsÚpos_embd_dimÚ	__class__s       €r4   rV   zTFGPTJAttention.__init__R   sr  ø€ Ü‰ÑÑ"˜6Ò"à×+Ñ+ˆŒØ#)×#=Ñ#=ˆÔ ØŸ™¨$×*BÑ*BÑBˆŒØ�=‰=˜4×3Ñ3Ñ3°t·~±~ÒEÜØWÐX\×XfÑXfÐWgð h+Ø+/×+CÑ+CÐ*DÀBðHóð ð Ÿ-™-¨Ñ,ˆŒØ ×+Ñ+ˆŒä!ŸL™L×0Ñ0°×1BÑ1BÓCˆÔÜ"Ÿ\™\×1Ñ1°&×2DÑ2DÓEˆÔä—l‘l×(Ñ(Ø�N‰NØÜ.¨v×/GÑ/GÓHØð	 )ó 
ˆŒô —l‘l×(Ñ(Ø�N‰NØÜ.¨v×/GÑ/GÓHØð	 )ó 
ˆŒô —l‘l×(Ñ(Ø�N‰NØÜ.¨v×/GÑ/GÓHØð	 )ó 
ˆŒô Ÿ™×*Ñ*Ø�N‰NØÜ.¨v×/GÑ/GÓHØð	 +ó 
ˆŒð $×;Ñ;ˆÔÜ#%§:¡:Ü�G‰G”B—O‘O×)Ñ)×.Ñ.¬r¯w©w¸×8JÑ8JÈD×L^ÑL^Ð7_Ó/`ÓaÔce×cjÑcjÓkØ��4×%Ñ% t×'9Ñ'9Ð:ó$
ˆÔ ð —‘Ò8¨$¯.©.ˆÜ:¸4×;MÑ;MÈ|Ó\ˆÕr6   c                ó‚   — t        j                  | j                  d d …d d …||z
  |…d |…f   t         j                  «      S ©N)r'   r(   rm   Úbool)ro   Ú
key_lengthÚquery_lengths      r4   Úget_causal_maskzTFGPTJAttention.get_causal_mask„   s@   € Ü�w‰w�t×/Ñ/²²1°jÀ<Ñ6OÐR\Ð6\Ð^iÐ_iÐ^iÐ0iÑjÔln×lsÑlsÓtÐtr6   c                óT   — t        j                  t        j                  d«      | «      S )Ng    eÍÍÁ)r'   r(   Úconstantr#   s    r4   Úget_masked_biaszTFGPTJAttention.get_masked_bias‡   s   € ä�w‰w”r—{‘{ 4Ó(¨%Ó0Ð0r6   c                óx  — t        |«      dd | j                  | j                  gz   }t        j                  ||«      }|r|S t        t        |«      «      dk(  rt        j                  |d«      S t        t        |«      «      dk(  rt        j                  |d«      S t        dt        t        |«      «      › �«      ‚)zO
        Splits hidden dim into attn_head_size and num_attention_heads
        Nr8   é   ©r   r"   r   r   é   ©r   r   r   r"   r~   ú3Input tensor rank should be one of [4, 5], but is: )r   rY   rZ   r'   r=   ÚlenÚ	transposer[   )ro   Úhidden_statesÚrotaryr@   s       r4   Ú_split_headszTFGPTJAttention._split_heads‹   s«   € ô ˜}Ó-¨c¨rÐ2°d×6NÑ6NÐPT×P]ÑP]Ð5^Ñ^ˆ	ÜŸ
™
 =°)Ó<ˆÙØ Ð ÜŒz˜-Ó(Ó)¨QÒ.Ü—<‘< ¨|Ó<Ð<ÜŒz˜-Ó(Ó)¨QÒ.Ü—<‘< ¨Ó?Ð?ÜÐNÌsÔS]Ð^kÓSlÓOmÐNnÐoÓpÐpr6   c                óx  — t        t        |«      «      dk(  rt        j                  |d«      }nNt        t        |«      «      dk(  rt        j                  |d«      }n t	        dt        t        |«      «      › �«      ‚t        |«      dd | j
                  | j                  z  gz   }t        j                  ||«      S )zR
        Merges attn_head_size dim and num_attn_heads dim into hidden dim
        r~   r   r€   r�   r‚   Nr9   )rƒ   r   r'   r„   r[   rY   rZ   r=   )ro   r…   r@   s      r4   Ú_merge_headszTFGPTJAttention._merge_heads™   s¥   € ô Œz˜-Ó(Ó)¨QÒ.ÜŸL™L¨¸ÓE‰MÜ”˜MÓ*Ó+¨qÒ0ÜŸL™L¨¸ÓH‰MäÐRÔSVÔWaÐboÓWpÓSqÐRrÐsÓtÐtÜ˜}Ó-¨c¨rÐ2°d×6NÑ6NÐQU×Q^ÑQ^Ñ6^Ð5_Ñ_ˆ	Ü�z‰z˜-¨Ó3Ð3r6   c                óh  — t        |«      d   t        |«      d   }}| j                  ||«      }t        j                  |t        j                  «      }t        j                  |t        j                  «      }t        j
                  ||d¬«      }	t        j                  ||	| j                  |	j                  «      «      }	|	| j                  z  }	|�|	|z   }	t        |	d¬«      }	t        j                  |	|j                  «      }	| j                  |	«      }	|�|	|z  }	t        j
                  |	|«      }
|
|	fS )Nr9   T)Útranspose_br8   r%   )r   ry   r'   r(   r*   ÚmatmulÚwherer|   r$   r\   r   ra   )ro   ÚqueryÚkeyÚvalueÚattention_maskÚ	head_maskrx   rw   Úcausal_maskÚattn_weightsÚattn_outputs              r4   Ú_attnzTFGPTJAttention._attn¦   s  € ô $.¨eÓ#4°RÑ#8¼*ÀS»/È"Ñ:M�jˆØ×*Ñ*¨:°|ÓDˆô —‘˜œrŸz™zÓ*ˆÜ�g‰g�cœ2Ÿ:™:Ó&ˆä—y‘y ¨¸Ô>ˆÜ—x‘x ¨\¸4×;OÑ;OÐP\×PbÑPbÓ;cÓdˆà# d§o¡oÑ5ˆàÐ%à'¨.Ñ8ˆLä% l¸Ô<ˆÜ—w‘w˜|¨U¯[©[Ó9ˆØ×(Ñ(¨Ó6ˆð Ð Ø'¨)Ñ3ˆLä—i‘i ¨eÓ4ˆà˜LÐ(Ð(r6   c                óÖ  — | j                  |«      }| j                  |«      }	| j                  |«      }
| j                  |d«      }| j                  |	d«      }	| j                  |
d«      }
t	        j
                  t	        j                  | j                  |d¬«      |j                  «      }t	        j                  |dd¬«      }| j                  �·|	d d …d d …d d …d | j                  …f   }|	d d …d d …d d …| j                  d …f   }|d d …d d …d d …d | j                  …f   }|d d …d d …d d …| j                  d …f   }t        ||«      }t        ||«      }t	        j                  ||fd¬«      }	t	        j                  ||fd¬«      }nt        |	|«      }	t        ||«      }t	        j                  |	d«      }	t	        j                  |d«      }|�<|d   }|d   }t	        j                  ||	fd	¬«      }	t	        j                  ||
fd	¬«      }
|du r|	|
f}nd }| j                  ||	|
||«      \  }}| j                  |«      }| j!                  |«      }| j#                  |«      }||f}|r||fz  }|S )
NTFr   r%   r"   r8   r   r   r9   )rL   rQ   rR   r‡   r'   r(   Úgatherrn   r$   Úsplitr]   rH   r.   r„   r–   r‰   rS   rc   )ro   r…   Ú
layer_pastr‘   Úposition_idsr’   Ú	use_cacheÚoutput_attentionsrŽ   r�   r�   rE   Úk_rotÚk_passÚq_rotÚq_passÚpast_keyÚ
past_valueÚpresentr•   r”   Úoutputss                         r4   ÚcallzTFGPTJAttention.callË   sQ  € ð —‘˜MÓ*ˆØ�k‰k˜-Ó(ˆØ—‘˜MÓ*ˆà×!Ñ! %¨Ó.ˆØ×Ñ  TÓ*ˆØ×!Ñ! %¨Ó/ˆä—‘œŸ™ 4×#7Ñ#7¸ÈAÔNÐP]×PcÑPcÓdˆÜ—‘˜& !¨"Ô-ˆØ�?‰?Ð&Øšš1šaÐ!2 4§?¡?Ð!2Ð2Ñ3ˆEØššAšq $§/¡/Ñ"3Ð3Ñ4ˆFàš!šQ¢Ð#4 T§_¡_Ð#4Ð4Ñ5ˆEØš1ša¢ D§O¡OÑ$5Ð5Ñ6ˆFä(¨°Ó7ˆEÜ(¨°Ó7ˆEä—)‘)˜U F˜O°"Ô5ˆCÜ—I‘I˜u f˜o°BÔ7‰Eä& s¨FÓ3ˆCÜ(¨°Ó7ˆEä�l‰l˜3 Ó-ˆÜ—‘˜U LÓ1ˆàÐ!Ø! !‘}ˆHØ# A™ˆJÜ—)‘)˜X s˜O°"Ô5ˆCÜ—I‘I˜z¨5Ð1¸Ô;ˆEà˜ÑØ˜E�l‰GàˆGð %)§J¡J¨u°c¸5À.ÐR[Ó$\Ñ!ˆ�\à×'Ñ'¨Ó4ˆØ—m‘m KÓ0ˆØ×(Ñ(¨Ó5ˆà Ð(ˆÙØ˜�Ñ&ˆGàˆr6   c                óÈ  — | j                   ry d| _         t        | dd «      �Zt        j                  | j                  j
                  «      5  | j                  j                  d d | j                  g«       d d d «       t        | dd «      �Zt        j                  | j                  j
                  «      5  | j                  j                  d d | j                  g«       d d d «       t        | dd «      �Zt        j                  | j                  j
                  «      5  | j                  j                  d d | j                  g«       d d d «       t        | dd «      �[t        j                  | j                  j
                  «      5  | j                  j                  d d | j                  g«       d d d «       y y # 1 sw Y   �ŒAxY w# 1 sw Y   ŒæxY w# 1 sw Y   Œ‹xY w# 1 sw Y   y xY w)NTrL   rQ   rR   rS   )ÚbuiltÚgetattrr'   Ú
name_scoperL   rP   ÚbuildrX   rQ   rR   rS   ©ro   Úinput_shapes     r4   r«   zTFGPTJAttention.build
  sŠ  € Ø�:Š:ØØˆŒ
Ü�4˜ 4Ó(Ð4Ü—‘˜tŸ{™{×/Ñ/Ó0ñ @Ø—‘×!Ñ! 4¨¨t¯~©~Ð">Ô?÷@ä�4˜ 4Ó(Ð4Ü—‘˜tŸ{™{×/Ñ/Ó0ñ @Ø—‘×!Ñ! 4¨¨t¯~©~Ð">Ô?÷@ä�4˜ 4Ó(Ð4Ü—‘˜tŸ{™{×/Ñ/Ó0ñ @Ø—‘×!Ñ! 4¨¨t¯~©~Ð">Ô?÷@ä�4˜ TÓ*Ð6Ü—‘˜tŸ}™}×1Ñ1Ó2ñ BØ—‘×#Ñ# T¨4°·±Ð$@ÔA÷Bð Bð 7÷@ñ @ú÷@ð @ú÷@ð @ú÷Bð Bús0   Á)F3Â2)G Ä)GÆ )GÆ3F=Ç G	ÇGÇG!©rp   r   )Úreturnú	tf.Tensor)r$   ztf.DTyper¯   r°   )r…   r°   r†   rv   r¯   r°   ©r…   r°   r¯   r°   ©NN)rŽ   r°   r�   r°   r�   r°   r‘   útf.Tensor | Noner’   r³   r¯   zTuple[tf.Tensor, tf.Tensor]©NNNNFF)r…   r°   rš   z%Optional[Tuple[tf.Tensor, tf.Tensor]]r‘   r³   r›   r³   r’   r³   rœ   rv   r�   rv   ru   )Ú__name__Ú
__module__Ú__qualname__rV   ry   Ústaticmethodr|   r‡   r‰   r–   r¦   r«   Ú__classcell__©rs   s   @r4   rJ   rJ   Q   sÜ   ø„ õ0]óduð ò1ó ð1óqó4ð$ ,0Ø&*ð#)àð#)ð ð#)ð ð	#)ð
 )ð#)ð $ð#)ð 
%ó#)ðP =AØ+/Ø)-Ø&*ØØ"'ð=à ð=ð :ð=ð )ð	=ð
 'ð=ð $ð=ð ð=ð  ó=÷~Br6   rJ   c                  ó0   ‡ — e Zd Zdˆ fd„Zdd„Zdd„Zˆ xZS )Ú	TFGPTJMLPc                óä  •— t        ‰| �  di |¤Ž |j                  }t        j                  j                  |t        |j                  «      d¬«      | _        t        j                  j                  |t        |j                  «      d¬«      | _	        t        |j                  «      | _        t        j                  j                  |j                  «      | _        |j                  | _        || _        y )NÚfc_in©rO   rP   Úfc_outrT   )rU   rV   Ún_embdr   r^   rd   r   re   r¾   rÀ   r   Úactivation_functionÚactr_   Ú
embd_pdropÚdropoutrX   Úintermediate_size)ro   rÆ   rp   rq   rX   rs   s        €r4   rV   zTFGPTJMLP.__init__  s½   ø€ Ü‰ÑÑ"˜6Ò"Ø—M‘Mˆ	ä—\‘\×'Ñ'Ø´/À&×BZÑBZÓ2[Ðbið (ó 
ˆŒ
ô —l‘l×(Ñ(Ø¬/¸&×:RÑ:RÓ*SÐZbð )ó 
ˆŒô % V×%?Ñ%?Ó@ˆŒÜ—|‘|×+Ñ+¨F×,=Ñ,=Ó>ˆŒØŸ™ˆŒØ!2ˆÕr6   c                óŽ   — | j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }|S ru   )r¾   rÃ   rÀ   rÅ   )ro   r…   s     r4   r¦   zTFGPTJMLP.call-  s@   € ØŸ
™
 =Ó1ˆØŸ™ Ó/ˆØŸ™ MÓ2ˆØŸ™ ]Ó3ˆØÐr6   c                óú  — | j                   ry d| _         t        | dd «      �Zt        j                  | j                  j
                  «      5  | j                  j                  d d | j                  g«       d d d «       t        | dd «      �[t        j                  | j                  j
                  «      5  | j                  j                  d d | j                  g«       d d d «       y y # 1 sw Y   ŒrxY w# 1 sw Y   y xY w)NTr¾   rÀ   )
r¨   r©   r'   rª   r¾   rP   r«   rX   rÀ   rÆ   r¬   s     r4   r«   zTFGPTJMLP.build4  sÐ   € Ø�:Š:ØØˆŒ
Ü�4˜ $Ó'Ð3Ü—‘˜tŸz™zŸ™Ó/ñ ?Ø—
‘
× Ñ  $¨¨d¯n©nÐ!=Ô>÷?ä�4˜ 4Ó(Ð4Ü—‘˜tŸ{™{×/Ñ/Ó0ñ HØ—‘×!Ñ! 4¨¨t×/EÑ/EÐ"FÔG÷Hð Hð 5÷?ð ?ú÷Hð Hús   Á)C%Â2)C1Ã%C.Ã1C:)rÆ   Úintrp   r   r±   ru   ©rµ   r¶   r·   rV   r¦   r«   r¹   rº   s   @r4   r¼   r¼     s   ø„ õ3ó ÷	Hr6   r¼   c                  óX   ‡ — e Zd Zdˆ fd„Z	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zdd„Zˆ xZS )ÚTFGPTJBlockc                ó*  •— t        ‰| �  di |¤Ž |j                  �|j                  nd|j                  z  }t        j
                  j                  |j                  d¬«      | _        t        |d¬«      | _
        t        ||d¬«      | _        || _        y )Nr~   Úln_1©ÚepsilonrP   Úattn©rP   ÚmlprT   )rU   rV   Ún_innerrÁ   r   r^   ÚLayerNormalizationÚlayer_norm_epsilonrÎ   rJ   rÑ   r¼   rÓ   rp   )ro   rp   rq   Ú	inner_dimrs   s       €r4   rV   zTFGPTJBlock.__init__A  sw   ø€ Ü‰ÑÑ"˜6Ò"Ø&,§n¡nÐ&@�F—N’NÀaÈ&Ï-É-ÑFWˆ	Ü—L‘L×3Ñ3¸F×<UÑ<UÐ\bÐ3ÓcˆŒ	Ü# F°Ô8ˆŒ	Ü˜Y¨°UÔ;ˆŒØˆ�r6   c           	     óÈ   — |}| j                  |«      }| j                  |||||||¬«      }	|	d   }
|	dd  }| j                  |«      }|
|z   |z   }|r|f|z   }|S |f|dd  z   }|S )N)r…   rš   r‘   r›   r’   rœ   r�   r   r   )rÎ   rÑ   rÓ   )ro   r…   rš   r‘   r›   r’   rœ   r�   ÚresidualÚattn_outputsr•   r¥   Úfeed_forward_hidden_statess                r4   r¦   zTFGPTJBlock.callI  s¦   € ð !ˆØŸ	™	 -Ó0ˆØ—y‘yØ'Ø!Ø)Ø%ØØØ/ð !ó 
ˆð # 1‘oˆØ˜q˜rÐ"ˆà%)§X¡X¨mÓ%<Ð"Ø#Ð&@Ñ@À8ÑKˆáØ$Ð&¨Ñ0ˆGð ˆð %Ð&¨°°¨Ñ4ˆGØˆr6   c                óÀ  — | j                   ry d| _         t        | dd «      �dt        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �Nt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       y y # 1 sw Y   Œ¿xY w# 1 sw Y   ŒqxY w# 1 sw Y   y xY w)NTrÎ   rÑ   rÓ   )r¨   r©   r'   rª   rÎ   rP   r«   rp   rÁ   rÑ   rÓ   r¬   s     r4   r«   zTFGPTJBlock.buildj  s
  € Ø�:Š:ØØˆŒ
Ü�4˜ Ó&Ð2Ü—‘˜tŸy™yŸ~™~Ó.ñ BØ—	‘	—‘  t¨T¯[©[×-?Ñ-?Ð @ÔA÷Bä�4˜ Ó&Ð2Ü—‘˜tŸy™yŸ~™~Ó.ñ &Ø—	‘	—‘ Ô%÷&ä�4˜ Ó%Ð1Ü—‘˜tŸx™xŸ}™}Ó-ñ %Ø—‘—‘˜tÔ$÷%ð %ð 2÷Bð Bú÷&ð &ú÷%ð %ús$   Á3D<Â<EÄEÄ<EÅEÅEr®   r´   )r…   r°   rš   r³   r‘   r³   r›   r³   r’   r³   rœ   rv   r�   rv   ru   rÊ   rº   s   @r4   rÌ   rÌ   @  sk   ø„ õð (,Ø+/Ø)-Ø&*ØØ"'ðà ðð %ðð )ð	ð
 'ðð $ðð ðð  ó÷B%r6   rÌ   c                  ón   ‡ — e Zd ZeZdˆ fd„Zd„ Zdd„Zd„ Ze		 	 	 	 	 	 	 	 	 	 	 	 d		 d
d„«       Z
dd„Zˆ xZS )ÚTFGPTJMainLayerc                óú  •— t        ‰| �  |i |¤Ž || _        |j                  | _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _	        |j                  | _
        |j                  | _        |j                  | _        t        |j                  |j                  |j                  d¬«      | _        t"        j$                  j'                  |j(                  «      | _        t-        |j                  «      D �cg c]  }t/        |d|› �¬«      ‘Œ c}| _        t"        j$                  j3                  |j4                  d¬«      | _        |j                  | _        y c c}w )NÚwte)re   rP   zh_._rÒ   Úln_frÏ   )rU   rV   rp   r�   Úoutput_hidden_statesrœ   Úuse_return_dictÚreturn_dictÚn_layerÚnum_hidden_layersrÁ   Ún_positionsre   r   Ú
vocab_sizerW   rà   r   r^   r_   rÄ   Údropr)   rÌ   ÚhrÕ   rÖ   rá   rX   )ro   rp   Úinputsrq   Úirs   s        €r4   rV   zTFGPTJMainLayer.__init__}  s'  ø€ Ü‰Ñ˜&Ð+ FÒ+àˆŒØ!'×!9Ñ!9ˆÔØ$*×$?Ñ$?ˆÔ!Ø×)Ñ)ˆŒØ!×1Ñ1ˆÔà!'§¡ˆÔØ—m‘mˆŒØ!×-Ñ-ˆÔØ!'×!9Ñ!9ˆÔä%Ø×Ñ˜v×1Ñ1ÀV×E]ÑE]Ðdiô
ˆŒô —L‘L×(Ñ(¨×):Ñ):Ó;ˆŒ	Ü@EÀfÇnÁnÓ@UÖV¸1”+˜f¨T°!°¨:Ö6ÒVˆŒÜ—L‘L×3Ñ3¸F×<UÑ<UÐ\bÐ3ÓcˆŒ	ØŸ™ˆ�ùò Ws   ÄE8c                ó   — | j                   S ru   )rà   ©ro   s    r4   Úget_input_embeddingsz$TFGPTJMainLayer.get_input_embeddings“  s   € Ø�x‰xˆr6   c                ó`   — || j                   _        t        |«      d   | j                   _        y )Nr   )rà   Úweightr   rè   )ro   r�   s     r4   Úset_input_embeddingsz$TFGPTJMainLayer.set_input_embeddings–  s#   € Øˆ�‰ŒÜ(¨Ó/°Ñ2ˆ�‰Õr6   c                ó   — t         ‚)zv
        Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer}
        )ÚNotImplementedError)ro   Úheads_to_prunes     r4   Ú_prune_headszTFGPTJMainLayer._prune_headsš  s
   € ô "Ð!r6   c                óð  ‡— |�|�t        d«      ‚|�'t        |«      }t        j                  |d|d   g«      }n|�t        |«      d d }nt        d«      ‚|€d}d gt	        | j
                  «      z  }nt        |d   d   «      d   }|€1t        j                  t        j                  ||d   |z   «      d¬«      }|�žt        |«      }t        j                  ||d   dd|d   f«      }t        j                  d«      }t        j                  ||j                  ¬	«      }t        j                  t        j                  ||«      t        j                  d
«      «      }|�t        ‚d g| j                  z  }t        j                  |dt        |«      d   g«      }|€3t        || j                   j"                  «       | j!                  |d¬«      }|�8t        j                  |dt        |«      d   g«      }| j!                  |d¬«      }nt        j                  d«      }t        j                  ||j                  ¬	«      }||z   }| j%                  ||¬«      }|t        |«      d   gz   }|rdnd }|	rdnd }|
rdnd }t'        t)        | j
                  |«      «      D ]W  \  }\  }}|
r|t        j                  ||«      fz   } |||||||   ||	|¬«      }|d   }|r	||d   fz   }|	sŒK|||rdnd   fz   }ŒY | j+                  |«      }t        j                  ||«      }|
r||fz   }|	r/|d d dgz   t        |d   «      dd  z   Št-        ˆfd„|D «       «      }|st-        d„ ||||fD «       «      S t/        ||||¬«      S )NzDYou cannot specify both input_ids and inputs_embeds at the same timer8   z5You have to specify either input_ids or inputs_embedsr   r9   r%   r   r!   r#   g     ˆÃÀÚ	embedding)Úmodeg        )ÚtrainingrT   )r…   rš   r‘   r›   r’   rœ   r�   rú   r"   c              3  óJ   •K  — | ]  }t        j                  |‰«      –— Œ y ­wru   )r'   r=   )Ú.0ÚtÚattention_output_shapes     €r4   ú	<genexpr>z'TFGPTJMainLayer.call.<locals>.<genexpr>  s   øè ø€ Ò"aÈQ¤2§:¡:¨aÐ1G×#HÑ"aùs   ƒ #c              3  ó&   K  — | ]	  }|€Œ|–— Œ y ­wru   rT   )rü   Úvs     r4   rÿ   z'TFGPTJMainLayer.call.<locals>.<genexpr>  s   è ø€ Òr˜qÐdeÑdqœÑrùs   ‚Š)Úlast_hidden_stateÚpast_key_valuesr…   Ú
attentions)r[   r   r'   r=   rƒ   rê   Úexpand_dimsr)   r{   r(   r$   ÚmultiplyÚsubtractrô   ræ   r   rà   rè   ré   Ú	enumerateÚziprá   Útupler   )ro   Ú	input_idsr  r‘   Útoken_type_idsr›   r’   Úinputs_embedsrœ   r�   râ   rä   rú   r­   Úpast_lengthÚattention_mask_shapeÚone_cstÚtoken_type_embedsr…   Úoutput_shapeÚpresentsÚall_attentionsÚall_hidden_statesrì   Úblockrš   r¥   rþ   s                              @r4   r¦   zTFGPTJMainLayer.call   s¼  ø€ ð  Ð  ]Ð%>ÜÐcÓdÐdØÐ"Ü$ YÓ/ˆKÜŸ
™
 9¨r°;¸r±?Ð.CÓD‰IØÐ&Ü$ ]Ó3°C°RÐ8‰KäÐTÓUÐUàÐ"ØˆKØ#˜f¤s¨4¯6©6£{Ñ2‰Oä$ _°QÑ%7¸Ñ%:Ó;¸BÑ?ˆKàÐÜŸ>™>¬"¯(©(°;ÀÈBÁÐR]Ñ@]Ó*^ÐefÔgˆLàÐ%ô $.¨nÓ#=Ð ÜŸZ™Z¨Ð9MÈaÑ9PÐRSÐUVÐXlÐmnÑXoÐ8pÓqˆNô —k‘k #Ó&ˆGÜŸW™W ^¸7¿=¹=ÔIˆNÜŸ[™[¬¯©°W¸nÓ)MÌrÏ{É{Ð[cÓOdÓeˆNð Ð Ü%Ð%à˜ ×!7Ñ!7Ñ7ˆIô —z‘z ,°´ZÀÓ5MÈbÑ5QÐ0RÓSˆàÐ Ü*¨9°d·h±h×6IÑ6IÔJØ ŸH™H Y°[˜HÓAˆMàÐ%ÜŸZ™Z¨¸¼ZÈÓ=WÐXZÑ=[Ð8\Ó]ˆNØ $§¡¨¸k Ó JÑä "§¡¨CÓ 0ÐäŸG™GÐ$5¸]×=PÑ=PÔQÐØ%Ð(9Ñ9ˆØŸ	™	 -¸(˜	ÓCˆà"¤j°Ó&?ÀÑ&CÐ%DÑDˆá"‘2¨ˆÙ0™°dˆÙ"6™B¸DÐÜ&/´°D·F±F¸OÓ0LÓ&Mò 	TÑ"ˆAÑ"��zÙ#Ø$5¼¿¹ÀMÐS_Ó9`Ð8bÑ$bÐ!áØ+Ø%Ø-Ø)Ø# A™,Ø#Ø"3Ø!ô	ˆGð $ A™JˆMÙØ# w¨q¡z mÑ3�â Ø!/°7Á	¹1ÈqÑ3QÐ2SÑ!S‘ð)	Tð, Ÿ	™	 -Ó0ˆäŸ
™
 =°,Ó?ˆáØ 1°]Ð4DÑ DÐáà%0°°"Ð%5¸¸Ñ%<¼zÈ.ÐYZÑJ[Ó?\Ð]_Ð]`Ð?aÑ%aÐ"Ü"Ó"aÐR`Ô"aÓaˆNáÜÑr ]°HÐ>OÐQ_Ð$`ÔrÓrÐrä(Ø+Ø$Ø+Ø%ô	
ð 	
r6   c                ó¦  — | j                   ry d| _         t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �Zt        j                  | j                  j
                  «      5  | j                  j                  d d | j                  g«       d d d «       t        | dd «      �K| j                  D ];  }t        j                  |j
                  «      5  |j                  d «       d d d «       Œ= y y # 1 sw Y   ŒÉxY w# 1 sw Y   ŒnxY w# 1 sw Y   ŒaxY w)NTrà   rá   rê   )
r¨   r©   r'   rª   rà   rP   r«   rá   rX   rê   )ro   r­   Úlayers      r4   r«   zTFGPTJMainLayer.build!  s  € Ø�:Š:ØØˆŒ
Ü�4˜ Ó%Ð1Ü—‘˜tŸx™xŸ}™}Ó-ñ %Ø—‘—‘˜tÔ$÷%ä�4˜ Ó&Ð2Ü—‘˜tŸy™yŸ~™~Ó.ñ >Ø—	‘	—‘  t¨T¯^©^Ð <Ô=÷>ä�4˜˜dÓ#Ð/ØŸ™ò &�Ü—]‘] 5§:¡:Ó.ñ &Ø—K‘K Ô%÷&ð &ñ&ð 0÷%ð %ú÷>ð >ú÷&ð &ús$   ÁD/Â%)D;ÄEÄ/D8Ä;EÅE	r®   )r�   r°   ©NNNNNNNNNNNF)r¯   ú2Union[TFBaseModelOutputWithPast, Tuple[tf.Tensor]]ru   )rµ   r¶   r·   r   Úconfig_classrV   rï   rò   rö   r   r¦   r«   r¹   rº   s   @r4   rÞ   rÞ   y  si   ø„ à€Lõ'ò,ó3ò"ð ð ØØØØØØØØØ!ØØð~
ð 
<ò~
ó ð~
÷@&r6   rÞ   c                  ó   — e Zd ZdZeZdZdgZy)ÚTFGPTJPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    Útransformerúh.\d+.attn.biasN)rµ   r¶   r·   Ú__doc__r   r  Úbase_model_prefixÚ"_keys_to_ignore_on_load_unexpectedrT   r6   r4   r  r  1  s   „ ñð
 €LØ%Ðà*<Ð)=Ñ&r6   r  a|	  

    This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a [keras.Model](https://www.tensorflow.org/api_docs/python/tf/keras/Model) subclass. Use it
    as a regular TF 2.0 Keras Model and refer to the TF 2.0 documentation for all matter related to general usage and
    behavior.

    <Tip>

    TensorFlow models and layers in `transformers` accept two formats as input:

    - having all inputs as keyword arguments (like PyTorch models), or
    - having all inputs as a list, tuple or dict in the first positional argument.

    The reason the second format is supported is that Keras methods prefer this format when passing inputs to models
    and layers. Because of this support, when using methods like `model.fit()` things should "just work" for you - just
    pass your inputs and labels in any format that `model.fit()` supports! If, however, you want to use the second
    format outside of Keras methods like `fit()` and `predict()`, such as when creating your own layers or models with
    the Keras `Functional` API, there are three possibilities you can use to gather all the input Tensors in the first
    positional argument:

    - a single Tensor with `input_ids` only and nothing else: `model(input_ids)`
    - a list of varying length with one or several input Tensors IN THE ORDER given in the docstring:
    `model([input_ids, attention_mask])` or `model([input_ids, attention_mask, token_type_ids])`
    - a dictionary with one or several input Tensors associated to the input names given in the docstring:
    `model({"input_ids": input_ids, "token_type_ids": token_type_ids})`

    Note that when creating models and layers with
    [subclassing](https://keras.io/guides/making_new_layers_and_models_via_subclassing/) then you don't need to worry
    about any of this, as you can just pass inputs like you would to any other Python function!

    </Tip>

    Parameters:
        config ([`GPTJConfig`]): 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 [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.
a·  
    Args:
        input_ids (`Numpy array` or `tf.Tensor` of shape `(batch_size, input_ids_length)`):
            `input_ids_length` = `sequence_length` if `past` is `None` else `past[0].shape[-2]` (`sequence_length` of
            input past key value states). Indices of input sequence tokens in the vocabulary.

            If `past` is used, only input IDs that do not have their past calculated should be passed as `input_ids`.

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

            [What are input IDs?](../glossary#input-ids)
        past_key_values (`List[tf.Tensor]` of length `config.n_layers`):
            Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model (see
            `past` output below). Can be used to speed up sequential decoding. The token ids which have their past
            given to this model should not be passed as input ids as they have already been computed.
        attention_mask (`tf.Tensor` or `Numpy array` of shape `(batch_size, sequence_length)`, *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 (`tf.Tensor` or `Numpy array` 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 (`tf.Tensor` or `Numpy array` 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)
        head_mask (`Numpy array` or `tf.Tensor` 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 (`tf.Tensor` of shape `(batch_size, 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.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail. This argument can be used only in eager mode, in graph mode the value in the
            config will be used instead.
        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. This argument can be used only in eager mode, in graph mode the value in the config will be
            used instead.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple. This argument can be used
            in eager mode, in graph mode the value will always be set to True.
        training (`bool`, *optional*, defaults to `False`):
            Whether or not to use the model in training mode (some modules like dropout modules have different
            behaviors between training and evaluation).
z_The bare GPT-J Model transformer outputting raw hidden-states without any specific head on top.c                  ó¶   ‡ — e Zd Zˆ fd„Ze ee«       eee	e
¬«      	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„«       «       «       Zdd„Zˆ xZS )ÚTFGPTJModelc                óP   •— t        ‰| �  |g|¢­i |¤Ž t        |d¬«      | _        y )Nr  rÒ   )rU   rV   rÞ   r  ©ro   rp   rë   rq   rs   s       €r4   rV   zTFGPTJModel.__init__«  s)   ø€ Ü‰Ñ˜Ð3 &Ò3¨FÒ3Ü*¨6¸ÔFˆÕr6   ©Ú
checkpointÚoutput_typer  c                ó@   — | j                  |||||||||	|
||¬«      }|S )a  
        use_cache (`bool`, *optional*, defaults to `True`):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past`). Set to `False` during training, `True` during generation
        ©r  r  r‘   r  r›   r’   r  rœ   r�   râ   rä   rú   )r  )ro   r  r  r‘   r  r›   r’   r  rœ   r�   râ   rä   rú   r¥   s                 r4   r¦   zTFGPTJModel.call¯  sD   € ð8 ×"Ñ"ØØ+Ø)Ø)Ø%ØØ'ØØ/Ø!5Ø#Øð #ó 
ˆð ˆr6   c                óú   — | j                   ry d| _         t        | dd «      �Nt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       y y # 1 sw Y   y xY w)NTr  )r¨   r©   r'   rª   r  rP   r«   r¬   s     r4   r«   zTFGPTJModel.buildÜ  sm   € Ø�:Š:ØØˆŒ
Ü�4˜¨Ó-Ð9Ü—‘˜t×/Ñ/×4Ñ4Ó5ñ -Ø× Ñ ×&Ñ& tÔ,÷-ð -ð :÷-ð -ús   ÁA1Á1A:r  )r  úTFModelInputType | Noner  ú4Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]]r‘   únp.ndarray | tf.Tensor | Noner  r/  r›   r/  r’   r/  r  r/  rœ   úOptional[bool]r�   r0  râ   r0  rä   r0  rú   r0  r¯   r  ru   )rµ   r¶   r·   rV   r   r   ÚGPTJ_INPUTS_DOCSTRINGr	   Ú_CHECKPOINT_FOR_DOCr   Ú_CONFIG_FOR_DOCr¦   r«   r¹   rº   s   @r4   r$  r$  ¦  sï   ø„ ô
Gð Ù*Ð+@ÓAÙØ&Ø-Ø$ôð .2ØPTØ8<Ø8<Ø6:Ø37Ø7;Ø$(Ø,0Ø/3Ø&*Ø#(ð$à*ð$ð Nð$ð 6ð	$ð
 6ð$ð 4ð$ð 1ð$ð 5ð$ð "ð$ð *ð$ð -ð$ð $ð$ð !ð$ð 
<ò$óó Bó ð$÷L-r6   r$  zK
    The GPT-J Model transformer with a language modeling head on top.
    c                  óî   ‡ — e Zd Zˆ fd„Zd„ Zd„ Zd	d„Ze ee	j                  d«      «       eeee¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 d
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„«       «       «       Zdd„Zˆ xZS )ÚTFGPTJForCausalLMc                óæ   •— t        ‰| �  |g|¢­i |¤Ž t        |d¬«      | _        t        j
                  j                  |j                  t        |j                  «      d¬«      | _
        || _        y )Nr  rÒ   Úlm_headr¿   )rU   rV   rÞ   r  r   r^   rd   rè   r   re   r7  rp   r&  s       €r4   rV   zTFGPTJForCausalLM.__init__ì  sf   ø€ Ü‰Ñ˜Ð3 &Ò3¨FÒ3Ü*¨6¸ÔFˆÔÜ—|‘|×)Ñ)Ø×Ñ´/À&×BZÑBZÓ2[Ðbkð *ó 
ˆŒð ˆ�r6   c                ó   — | j                   S ru   ©r7  rî   s    r4   Úget_output_embeddingsz'TFGPTJForCausalLM.get_output_embeddingsô  s   € Ø�|‰|Ðr6   c                ó   — || _         y ru   r9  )ro   Únew_embeddingss     r4   Úset_output_embeddingsz'TFGPTJForCausalLM.set_output_embeddings÷  s	   € Ø%ˆ�r6   c                ó†  — |j                  dd «      }|r<t        j                  |d d …df   d«      }|�t        j                  |d d …df   d«      }|j                  dd «      }|j                  dd «      }|�C|€At        j                  j	                  |dd¬«      }|rt        j                  |d d …df   d«      }||||||dœS )Nr  r8   r›   r‘   T)r&   Ú	exclusive)r  r‘   r›   r  rœ   r  )Úgetr'   r  r;   Úcumsum)ro   rë   r  rœ   rq   r  r›   r‘   s           r4   Úprepare_inputs_for_generationz/TFGPTJForCausalLM.prepare_inputs_for_generationú  sË   € ØŸ™Ð$4°dÓ;ˆáÜ—^‘^ Fª1¨b¨5¡M°2Ó6ˆFØÐ)Ü!#§¡°ºqÀ"¸uÑ0EÀrÓ!J�à—z‘z .°$Ó7ˆØŸ™Ð$4°dÓ;ˆàÐ%¨,Ð*>ÜŸ7™7Ÿ>™>¨.¸rÈT˜>ÓRˆLÙÜ!Ÿ~™~¨lº1¸b¸5Ñ.AÀ2ÓF�ð  Ø,Ø(Ø.Ø"Ø,ñ
ð 	
r6   úbatch_size, sequence_lengthr'  c                óF  — | j                  ||||||||	|
|||¬«      }|d   }| j                  |«      }d}|�(|dd…dd…f   }|dd…dd…f   }| j                  ||«      }|s|f|dd z   }|�|f|z   S |S t        |||j                  |j
                  |j                  ¬«      S )a¼  
        labels (`np.ndarray` or `tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
            `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
            are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`
        r+  r   Nr8   r   ©ÚlossÚlogitsr  r…   r  )r  r7  Úhf_compute_lossr   r  r…   r  )ro   r  r  r‘   r  r›   r’   r  Úlabelsrœ   r�   râ   rä   rú   Útransformer_outputsr…   Ú	lm_logitsrF  Úshifted_logitsÚoutputs                       r4   r¦   zTFGPTJForCausalLM.call  sò   € ð< #×.Ñ.ØØ+Ø)Ø)Ø%ØØ'ØØ/Ø!5Ø#Øð /ó 
Ðð ,¨AÑ.ˆØ—L‘L Ó/ˆ	àˆØÐà&¢q¨#¨2¨# vÑ.ˆNØšA˜q™r˜E‘]ˆFØ×'Ñ'¨°Ó?ˆDáØ�\Ð$7¸¸Ð$;Ñ;ˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEä'ØØØ/×?Ñ?Ø-×;Ñ;Ø*×5Ñ5ô
ð 	
r6   c                óô  — | j                   ry d| _         t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   Œ|xY w# 1 sw Y   y xY w)NTr  r7  )
r¨   r©   r'   rª   r  rP   r«   r7  rp   rÁ   r¬   s     r4   r«   zTFGPTJForCausalLM.buildU  sÏ   € Ø�:Š:ØØˆŒ
Ü�4˜¨Ó-Ð9Ü—‘˜t×/Ñ/×4Ñ4Ó5ñ -Ø× Ñ ×&Ñ& tÔ,÷-ä�4˜ DÓ)Ð5Ü—‘˜tŸ|™|×0Ñ0Ó1ñ EØ—‘×"Ñ" D¨$°·±×0BÑ0BÐ#CÔD÷Eð Eð 6÷-ð -ú÷Eð Eúó   ÁC"Â%3C.Ã"C+Ã.C7r²   ©NNNNNNNNNNNNF)r  r-  r  r.  r‘   r/  r  r/  r›   r/  r’   r/  r  r/  rI  r/  rœ   r0  r�   r0  râ   r0  rä   r0  rú   r0  r¯   z1Union[TFCausalLMOutputWithPast, Tuple[tf.Tensor]]ru   )rµ   r¶   r·   rV   r:  r=  rB  r   r   r1  Úformatr	   r2  r   r3  r¦   r«   r¹   rº   s   @r4   r5  r5  å  s  ø„ ôòò&ó
ð2 Ù*Ð+@×+GÑ+GÐHeÓ+fÓgÙØ&Ø,Ø$ôð .2ØPTØ8<Ø8<Ø6:Ø37Ø7;Ø04Ø$(Ø,0Ø/3Ø&*Ø#(ð9
à*ð9
ð Nð9
ð 6ð	9
ð
 6ð9
ð 4ð9
ð 1ð9
ð 5ð9
ð .ð9
ð "ð9
ð *ð9
ð -ð9
ð $ð9
ð !ð9
ð 
;ò9
óó hó ð9
÷v	Er6   r5  aã  
    The GPT-J Model transformer with a sequence classification head on top (linear layer).

    [`GPTJForSequenceClassification`] uses the last token in order to do the classification, as other causal models
    (e.g. GPT, GPT-2, GPT-Neo) do.

    Since it does classification on the last token, it requires to know the position of the last token. If a
    `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
    no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
    padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
    each row of the batch).
    c                  óâ   ‡ — e Zd Zg d¢Zˆ fd„Ze eej                  d«      «       e	e
ee¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„«       «       «       Zd	d„Zˆ xZS )
ÚTFGPTJForSequenceClassification©zh.\d+.attn.masked_biasr  zlm_head.weightc                ó
  •— t        ‰| �  |g|¢­i |¤Ž |j                  | _        t        |d¬«      | _        t
        j                  j                  | j                  dt        |j                  «      d¬«      | _
        || _        y )Nr  rÒ   FÚscorerM   )rU   rV   Ú
num_labelsrÞ   r  r   r^   rd   r   re   rV  rp   r&  s       €r4   rV   z(TFGPTJForSequenceClassification.__init__s  su   ø€ Ü‰Ñ˜Ð3 &Ò3¨FÒ3Ø ×+Ñ+ˆŒÜ*¨6¸ÔFˆÔÜ—\‘\×'Ñ'Ø�O‰OØÜ.¨v×/GÑ/GÓHØð	 (ó 
ˆŒ
ð ˆ�r6   rC  r'  c                óz  — |�3| j                   j                  €|j                  d   dk7  rt        d«      ‚| j	                  ||||||||	|
|||¬«      }|d   }| j                  |«      }t        |«      }|d   }| j                   j                  €t        j                  |f|d   dz
  ¬«      }n¿|�st        j                  t        |«      d   «      }t        j                  || j                   j                  k7  |j                  «      }t        j                  ||z  d¬«      }nJt        j                  |f|d   dz
  ¬«      }t        j                  | j                  j                   › d	�«       d}t        j"                  ||dd¬
«      }|�p| j                   j                  €|d   dk7  rt        d«      ‚| j%                  t        j&                  |dg«      t        j&                  |d| j(                  g«      «      }|s|f|dd z   }|�|f|z   S |S t+        |||j,                  |j.                  |j0                  ¬«      S )a–  
        labels (`np.ndarray` or `tf.Tensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        Nr   r   z=Cannot handle batch sizes > 1 if no padding token is defined.r+  )r�   r8   r%   zŠ will not detect padding tokens in `inputs_embeds`. Results may be unexpected if using padding tokens in conjunction with `inputs_embeds.`)Ú
batch_dimsr&   rE  )rp   Úpad_token_idÚshaper[   r  rV  r   r'   Úfillr)   r(   r$   Ú
reduce_maxÚloggerÚwarning_oncers   rµ   r˜   rH  r=   rW  r   r  r…   r  )ro   r  r  r‘   r  r›   r’   r  rI  rœ   r�   râ   rä   rú   rJ  r…   rG  Úlogits_shapeÚ
batch_sizeÚlast_non_pad_tokenÚtoken_indicesÚnon_pad_maskrF  Úpooled_logitsrM  s                            r4   r¦   z$TFGPTJForSequenceClassification.call  sJ  € ð: Ð $§+¡+×":Ñ":Ð"BÀyÇÁÐWXÑGYÐ]^ÒG^ÜÐ\Ó]Ð]à"×.Ñ.ØØ+Ø)Ø)Ø%ØØ'ØØ/Ø!5Ø#Øð /ó 
Ðð ,¨AÑ.ˆØ—‘˜MÓ*ˆÜ! &Ó)ˆØ! !‘_ˆ
à�;‰;×#Ñ#Ð+Ü!#§¡¨*¨¸lÈ1¹oÐPQÑ>QÔ!RÑàÐ$Ü "§¡¬°IÓ)>¸rÑ)BÓ C�Ü!Ÿw™w y°D·K±K×4LÑ4LÑ'LÈm×NaÑNaÓb�Ü%'§]¡]°=À<Ñ3OÐVXÔ%YÑ"ä%'§W¡W¨j¨]À,ÈqÁ/ÐTUÑBUÔ%VÐ"Ü×#Ñ#Ø—~‘~×.Ñ.Ð/ð 0^ð ^ôð ˆäŸ	™	 &Ð*<ÈÐQRÔSˆàÐØ�{‰{×'Ñ'Ð/°LÀ±OÀqÒ4HÜ Ð!`ÓaÐaà×'Ñ'¬¯
©
°6¸B¸4Ó(@Ä"Ç*Á*È]Ð]_Ðae×apÑapÐ\qÓBrÓsˆDáØ#Ð%Ð(;¸A¸BÐ(?Ñ?ˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEä1ØØ Ø/×?Ñ?Ø-×;Ñ;Ø*×5Ñ5ô
ð 	
r6   c                óô  — | j                   ry d| _         t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   Œ|xY w# 1 sw Y   y xY w)NTr  rV  )
r¨   r©   r'   rª   r  rP   r«   rV  rp   rÁ   r¬   s     r4   r«   z%TFGPTJForSequenceClassification.buildÕ  sÍ   € Ø�:Š:ØØˆŒ
Ü�4˜¨Ó-Ð9Ü—‘˜t×/Ñ/×4Ñ4Ó5ñ -Ø× Ñ ×&Ñ& tÔ,÷-ä�4˜ $Ó'Ð3Ü—‘˜tŸz™zŸ™Ó/ñ CØ—
‘
× Ñ  $¨¨d¯k©k×.@Ñ.@Ð!AÔB÷Cð Cð 4÷-ð -ú÷Cð CúrO  rP  )r  r-  r  r.  r‘   r/  r  r/  r›   r/  r’   r/  r  r/  rI  r/  rœ   r0  r�   r0  râ   r0  rä   r0  rú   r0  r¯   z;Union[TFSequenceClassifierOutputWithPast, Tuple[tf.Tensor]]ru   )rµ   r¶   r·   Ú_keys_to_ignore_on_load_missingrV   r   r   r1  rQ  r	   r2  r   r3  r¦   r«   r¹   rº   s   @r4   rS  rS  a  s"  ø„ ò  'iÐ#ô
ð Ù*Ð+@×+GÑ+GÐHeÓ+fÓgÙØ&Ø6Ø$ôð .2ØPTØ8<Ø8<Ø6:Ø37Ø7;Ø04Ø$(Ø,0Ø/3Ø&*Ø#(ðM
à*ðM
ð NðM
ð 6ð	M
ð
 6ðM
ð 4ðM
ð 1ðM
ð 5ðM
ð .ðM
ð "ðM
ð *ðM
ð -ðM
ð $ðM
ð !ðM
ð 
EòM
óó hó ðM
÷^	Cr6   rS  zî
    The GPT-J Model transformer 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g d¢Zˆ fd„Ze eej                  d«      «       e	e
ee¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„«       «       «       Zd	d„Zˆ xZS )
ÚTFGPTJForQuestionAnsweringrT  c                ó  •— t        ‰| �  |g|¢­i |¤Ž |j                  | _        t        |d¬«      | _        t
        j                  j                  | j                  t        |j                  «      d¬«      | _
        || _        y )Nr  rÒ   Ú
qa_outputsr¿   )rU   rV   rW  rÞ   r  r   r^   rd   r   re   rk  rp   r&  s       €r4   rV   z#TFGPTJForQuestionAnswering.__init__ë  sq   ø€ Ü‰Ñ˜Ð3 &Ò3¨FÒ3Ø ×+Ñ+ˆŒÜ*¨6¸ÔFˆÔÜŸ,™,×,Ñ,Ø�O‰O´À×@XÑ@XÓ0YÐ`lð -ó 
ˆŒð ˆ�r6   rC  r'  c                ó²  — | j                  ||||||||
|||¬«      }|d   }| j                  |«      }t        j                  |dd¬«      \  }}t        j                  |d¬«      }t        j                  |d¬«      }d}|�|	�d|i}|	|d<   | j                  |||f«      }|s||f|dd z   }|�|f|z   S |S t        ||||j                  |j                  ¬	«      S )
a  
        start_positions (`np.ndarray` or `tf.Tensor` 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 (`np.ndarray` or `tf.Tensor` 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.
        )r  r  r‘   r  r›   r’   r  r�   râ   rä   rú   r   r"   r8   r%   NÚstart_positionÚend_position)rF  Ústart_logitsÚ
end_logitsr…   r  )	r  rk  r'   r™   ÚsqueezerH  r   r…   r  )ro   r  r  r‘   r  r›   r’   r  Ústart_positionsÚend_positionsr�   râ   rä   rú   rJ  Úsequence_outputrG  ro  rp  rF  rI  rM  s                         r4   r¦   zTFGPTJForQuestionAnswering.callô  s!  € ðD #×.Ñ.ØØ+Ø)Ø)Ø%ØØ'Ø/Ø!5Ø#Øð /ó 
Ðð .¨aÑ0ˆà—‘ Ó1ˆÜ#%§8¡8¨F°A¸BÔ#?Ñ ˆ�jÜ—z‘z ,°RÔ8ˆÜ—Z‘Z 
°Ô4ˆ
àˆØÐ&¨=Ð+DØ&¨Ð8ˆFØ%2ˆF�>Ñ"Ø×'Ñ'¨°¸zÐ0JÓKˆDáØ" JÐ/Ð2EÀaÀbÐ2IÑIˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEä-ØØ%Ø!Ø-×;Ñ;Ø*×5Ñ5ô
ð 	
r6   c                óô  — | j                   ry d| _         t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   Œ|xY w# 1 sw Y   y xY w)NTr  rk  )
r¨   r©   r'   rª   r  rP   r«   rk  rp   rW   r¬   s     r4   r«   z TFGPTJForQuestionAnswering.build<  sÏ   € Ø�:Š:ØØˆŒ
Ü�4˜¨Ó-Ð9Ü—‘˜t×/Ñ/×4Ñ4Ó5ñ -Ø× Ñ ×&Ñ& tÔ,÷-ä�4˜ tÓ,Ð8Ü—‘˜tŸ™×3Ñ3Ó4ñ MØ—‘×%Ñ% t¨T°4·;±;×3JÑ3JÐ&KÔL÷Mð Mð 9÷-ð -ú÷Mð MúrO  rP  )r  r-  r  r.  r‘   r/  r  r/  r›   r/  r’   r/  r  r/  rr  r/  rs  r/  r�   r0  râ   r0  rä   r0  rú   r0  r¯   z7Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]ru   )rµ   r¶   r·   rg  rV   r   r   r1  rQ  r	   r2  r   r3  r¦   r«   r¹   rº   s   @r4   ri  ri  á  s  ø„ ò 'iÐ#ôð Ù*Ð+@×+GÑ+GÐHeÓ+fÓgÙØ&Ø2Ø$ôð .2ØPTØ8<Ø8<Ø6:Ø37Ø7;Ø9=Ø7;Ø,0Ø/3Ø&*Ø#(ð?
à*ð?
ð Nð?
ð 6ð	?
ð
 6ð?
ð 4ð?
ð 1ð?
ð 5ð?
ð 7ð?
ð 5ð?
ð *ð?
ð -ð?
ð $ð?
ð !ð?
ð 
Aò?
óó hó ð?
÷B	Mr6   ri  )r5  ri  rS  r$  r  )r/   rÉ   r0   rÉ   r¯   r°   )r>   r°   r¯   r°   )rD   r°   rE   r°   r¯   r°   )?r   Ú
__future__r   Útypingr   r   r   ri   ÚnpÚ
tensorflowr'   Úactivations_tfr   Ú
file_utilsr	   r
   r   Úmodeling_tf_outputsr   r   r   r   Úmodeling_tf_utilsr   r   r   r   r   r   r   r   r   r   Útf_utilsr   r   r   Úutilsr   Úconfiguration_gptjr   Ú
get_loggerrµ   r^  r2  r3  r5   rA   rH   r^   ÚLayerrJ   r¼   rÌ   rÞ   r  ÚGPTJ_START_DOCSTRINGr1  r$  r5  rS  ri  Ú__all__rT   r6   r4   ú<module>r…     sÞ  ðñ å "ç )Ñ )ã Û å /÷ñ ÷
ó ÷÷ ÷ ÷ SÑ RÝ Ý *ð 
ˆ×	Ñ	˜HÓ	%€à+Ð Ø€óóóEôHB�e—l‘l×(Ñ(ô HBôV!H�—‘×"Ñ"ô !HôH6%�%—,‘,×$Ñ$ô 6%ðr ôt&�e—l‘l×(Ñ(ó t&ó ðt&ôn	>Ð-ô 	>ð(Ð ðT<Ð ñ~ ØeØóô8-Ð'ó 8-ó	ð8-ñv ðð ó	ôsEÐ-Ð/Kó sEóðsEñl ðð óônCÐ&;Ð=Yó nCóðnCñb ðð óô]MÐ!6Ð8Oó ]Móð]Mò@�r6   