Ë
    S^(h~,  ã            	       ó¦  — d dl 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  ej                  e«      Zdeej                   ej"                  f   dee   fd	„Zdd
ej                   dee   dee   dej                   fd„Zdd„Z	 ddee   fd„Zdd„Zdej                   dej                   fd„Zddej                   dededdfd„Zd„ Zd„ Zd„ Zd„ Z y) é    )ÚOptionalÚUnionNé   )ÚBatchFeature)ÚBatchEncoding)ÚloggingÚtensorÚreturnc                 ó\  — t        | t        j                  «      rt        | j                  «      S t        j                  | «      }| j                  t        j                  d«      k(  r|S | j                  j                  «       }t        |«      D ��cg c]  \  }}|€||   n|‘Œ c}}S c c}}w )zØ
    Deal with dynamic shape in tensorflow cleanly.

    Args:
        tensor (`tf.Tensor` or `np.ndarray`): The tensor we want the shape of.

    Returns:
        `List[int]`: The shape of the tensor as a list.
    N)	Ú
isinstanceÚnpÚndarrayÚlistÚshapeÚtfÚTensorShapeÚas_listÚ	enumerate)r	   ÚdynamicÚstaticÚiÚss        úS/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/tf_utils.pyÚ
shape_listr      s…   € ô �&œ"Ÿ*™*Ô%Ü�F—L‘LÓ!Ð!ä�h‰h�vÓ€Gà‡|�|”r—~‘~ dÓ+Ò+Øˆà�\‰\×!Ñ!Ó#€Fä7@ÀÓ7H×I©t¨q°!˜!˜)ˆG�AŠJ¨Ñ*ÓIÐIùÓIs   ÂB(ÚlogitsÚaxisÚnamec                 óL   — t         j                  j                  | dz   ||¬«      S )a‚  
    Stable wrapper that returns the same output as `tf.nn.softmax`, but that works reliably with XLA on CPU. It is
    meant as a workaround for the [following issue](https://github.com/tensorflow/tensorflow/issues/55682), and will be
    removed after it gets fixed. The arguments and outputs are the same as `tf.nn.softmax`, and relies on the fact that
    `softmax(x) = softmax(x + c)` (see https://ogunlao.github.io/2020/04/26/you_dont_really_know_softmax.html).

    Args:
        logits (`tf.Tensor`):
            Must be one of the following types: half, float32, float64.
        axis (`int`, *optional*):
            The dimension softmax would be performed on. The default is -1 which indicates the last dimension.
        name (`str`, *optional*):
            A name for the operation.

    Returns:
        `tf.Tensor`:
            A Tensor. Has the same type and shape as logits.
    g•Ö&è.>©r   r   r   )r   ÚnnÚsoftmaxr   s      r   Ústable_softmaxr"   3   s!   € ô* �5‰5�=‰= ¨¡°D¸tˆ=ÓDÐDó    c                 óî  — |j                   j                  dk7  s)|j                   j                  dk7  st        |t        «      st	        d«      ‚t
        j                  j                  | |gd¬«      \  }}|dk7  rWdg| j                   j                  z  }t        | «      |   ||<   t        j                  ||«      }t        j                  ||«      }t
        j                  j                  | |||||¬«      }|S )Nr   zOOnly 1D weight and bias tensors are supported for now, with only a single axis.T)ÚaxesÚkeepdimséÿÿÿÿ)ÚoffsetÚscaleÚvariance_epsilon)r   Úrankr   ÚintÚNotImplementedErrorr   r    Úmomentsr   ÚreshapeÚbatch_normalization)	ÚinputsÚweightÚbiasÚepsilonr   ÚmeanÚvariancer   Úoutputss	            r   Úfunctional_layernormr8   K   sà   € ð
 ‡|�|×Ñ˜AÒ §¡§¡°AÒ!5¼ZÈÌcÔ=RÜ!Ð"sÓtÐtô —U‘U—]‘] 6°°À�]ÓF�N€Dˆ(àˆr‚zð ��f—l‘l×'Ñ'Ñ'ˆÜ  Ó(¨Ñ.ˆˆd‰Ü—‘˜F EÓ*ˆÜ�z‰z˜$ Ó&ˆô �e‰e×'Ñ'ØØØØØØ ð (ó €Gð €Nr#   r)   c                 óL  — |dk7  rt        d«      ‚|r|�t        d«      ‚|r~t        j                  t        j                  | «      d   t        j                  |«      d   ft        j                  ¬«      }t        j
                  j                  j                  |d¬«      }|�‚|j                  j                  s|j                  j                  rVt        j                  |dkD  t        j                  d| j                  «      t        j                  d| j                  «      «      }t        j                  d	| |«      }|€9t        j                  t        j                  |«      d
   |j                  «      dz  }||z  }|�||z  }t        j                  j                  |«      }||z  S )zDTF equivalent for torch's nn.functional.scaled_dot_product_attentionç        z„Dropout is not supported in this implementation - file an issue with Transformers and ping @Rocketknight1 if you need it for a port!z?You cannot specify an attn_mask and is_causal at the same time!éþÿÿÿ©Údtyper   )Úkg     @�Àz...qd, ...kd -> ...qkr'   g      à¿)Ú
ValueErrorr   Úonesr   Úint32ÚexperimentalÚnumpyÚtrilr=   Ú
is_integerÚis_boolÚwhereÚcastÚeinsumr    r!   )	ÚqueryÚkeyÚvalueÚ	attn_maskÚ	dropout_pÚ	is_causalr)   r   Úprobss	            r   Úscaled_dot_product_attentionrQ   k   sR  € ð �CÒÜðSó
ð 	
ñ �YÐ*ÜÐZÓ[Ð[ÙÜ—G‘GœRŸX™X e›_¨RÑ0´"·(±(¸3³-ÀÑ2CÐDÌBÏHÉHÔUˆ	Ü—O‘O×)Ñ)×.Ñ.¨y¸AÐ.Ó>ˆ	ØÐ )§/¡/×"<Ò"<À	ÇÁ×@WÒ@Wä—H‘H˜Y¨™]¬B¯G©G°C¸¿¹Ó,EÄrÇwÁwÈwÐX]×XcÑXcÓGdÓeˆ	Ü�Y‰YÐ.°°sÓ;€FØ€}Ü—‘œŸ™ › bÑ)¨6¯<©<Ó8¸DÑ@ˆØ
ˆe�O€FØÐØ�)ÑˆÜ�E‰E�M‰M˜&Ó!€EØ�5‰=Ðr#   c                 óp  — |dk  r|| j                   j                  z  }|dk  r|| j                   j                  z  }||k(  r| S t        j                   | «      }t        j                  j	                  |||dz    «      }t        j
                  |d | |g||dz   d  gd¬«      }t        j                  | |«      S )Nr   r   ©r   )r   r+   r   ÚmathÚreduce_prodÚconcatr/   )ÚinputÚ	start_dimÚend_dimÚin_shapeÚflattened_dimÚ	out_shapes         r   Úflattenr]   †   s±   € ð �‚{Ø�5—;‘;×#Ñ#Ñ#ˆØ�1‚}Ø�U—[‘[×%Ñ%Ñ%ˆ	à�GÒØˆä�x‰x˜‹€HÜ—G‘G×'Ñ'¨°¸WÀq¹[Ð(IÓJ€MÜ—	‘	˜8 J YÐ/°-°À(È7ÐUVÉ;È=ÐBYÐZÐabÔc€IÜ�:‰:�e˜YÓ'Ð'r#   Úencoder_attention_maskc                 ót  — t        | t        j                  «      st        j                  | «      } | j                  j
                  dk(  r| dd…ddd…dd…f   }| j                  j
                  dk(  r| dd…dddd…f   }t        j                  d| j                  «      z
  |j                  j                  z  }|S )zÍ
    Invert an attention mask (e.g., switches 0. and 1.).

    Args:
        encoder_attention_mask (`torch.Tensor`): An attention mask.

    Returns:
        `tf.Tensor`: The inverted attention mask.
    é   Né   r   )	r   r   ÚTensorÚconvert_to_tensorr   r+   rH   r=   Úmin)r^   Úencoder_extended_attention_masks     r   Úinvert_attention_maskrf   ˜   s±   € ô Ð,¬b¯i©iÔ8Ü!#×!5Ñ!5Ð6LÓ!MÐØ×#Ñ#×(Ñ(¨AÒ-Ø*@ÂÀDÊ!ÊQÀÑ*OÐ'Ø×#Ñ#×(Ñ(¨AÒ-Ø*@ÂÀDÈ$ÒPQÐAQÑ*RÐ'ô 	�‰�Ð)×/Ñ/Ó0Ð3RÑRØ'×-Ñ-×1Ñ1ñ'2Ð#ð +Ð*r#   Ú	embed_dimÚtensor_namec                 óÔ   — t         j                  j                  | t        j                  || j                  ¬«      d|› dt         j
                  j                  | «      › d|› d�¬«       y)aª  
    `tf.gather`, on which TF embedding layers are based, won't check positive out of bound indices on GPU, returning
    zeros instead. This function adds a check against that dangerous silent behavior.

    Args:
        tensor (`tf.Tensor`): The tensor of indices to check.
        embed_dim (`int`): The embedding dimension.
        tensor_name (`str`, *optional*): The name of the tensor to use in the error message.
    r<   zThe maximum value of z (z>) must be smaller than the embedding layer's input dimension (z9). The likely cause is some problem at tokenization time.)ÚmessageN)r   Ú	debuggingÚassert_lessrH   r=   rT   Ú
reduce_max)r	   rg   rh   s      r   Úcheck_embeddings_within_boundsrn   ´   sa   € ô ‡L�L×ÑØÜ
�‰�	 §¡Ô.à# K =°´2·7±7×3EÑ3EÀfÓ3MÐ2Nð O(Ø(1 {Ð2kðmð	 õ r#   c                 óÐ  ‡
— dŠ
|D �cg c]  }t        |«      ‰
kD  sŒ|‘Œ }}|rt        d‰
› d|› �«      ‚t        j                  |«      }d}t        j                  ||«      }t        ˆ
fd„|D «       «      r0|dz  }t        j                  ||«      }t        ˆ
fd„|D «       «      rŒ0|dkD  r(t        |«      D ]  \  }}	|	| j                  d||fz  <   Œ y|| j                  |<   yc c}w )a÷  Saves attributes (data) of the specified name into the HDF5 group.

    This method deals with an inherent problem of HDF5 file which is not able to store data larger than
    HDF5_OBJECT_HEADER_LIMIT bytes.

    Args:
        group: A pointer to a HDF5 group.
        name: A name of the attributes to save.
        data: Attributes data to store.

    Raises:
      RuntimeError: If any single attribute is too large to be saved.

    Copied from Keras to Transformers to avoid versioning issues.
    i ü  zSThe following attributes cannot be saved to HDF5 file because they are larger than z bytes: r   c              3   ó<   •K  — | ]  }|j                   ‰kD  –— Œ y ­w)N)Únbytes)Ú.0ÚxÚHDF5_OBJECT_HEADER_LIMITs     €r   ú	<genexpr>z0save_attributes_to_hdf5_group.<locals>.<genexpr>ì   s   øè ø€ ÒH°aˆa�h‰hÐ1Õ1ÑHùs   ƒú%s%dN)ÚlenÚRuntimeErrorr   ÚasarrayÚarray_splitÚanyr   Úattrs)Úgroupr   Údatars   Úbad_attributesÚdata_npyÚ
num_chunksÚchunked_dataÚchunk_idÚ
chunk_datart   s             @r   Úsave_attributes_to_hdf5_groupr…   È   s	  ø€ ð   %Ðð "&ÖK˜A¬¨Q«Ð2JÓ)J’aÐK€NÐKñ Üð$Ø$<Ð#=ð >Ø$Ð%ð'ó
ð 	
ô �z‰z˜$Ó€Hà€JÜ—>‘> (¨JÓ7€Lô ÓH¸<ÔHÔ
HØ�a‰ˆ
Ü—~‘~ h°
Ó;ˆô ÓH¸<ÔHÕ
Hð �A‚~Ü$-¨lÓ$;ò 	@Ñ ˆH�jØ5?ˆE�K‰K˜ $¨Ð!1Ñ1Ò2ñ	@ð !ˆ�‰�DÒùò1 Ls
   ˆC#œC#c           	      ó¨  — || j                   v r;| j                   |   D �cg c]!  }t        |d«      r|j                  d«      n|‘Œ# }}|S g }d}d||fz  | j                   v rg|j                  | j                   d||fz     D �cg c]!  }t        |d«      r|j                  d«      n|‘Œ# c}«       |dz  }d||fz  | j                   v rŒg|S c c}w c c}w )a¢  Loads attributes of the specified name from the HDF5 group.

    This method deals with an inherent problem of HDF5 file which is not able to store data larger than
    HDF5_OBJECT_HEADER_LIMIT bytes.

    Args:
        group: A pointer to a HDF5 group.
        name: A name of the attributes to load.

    Returns:
        data: Attributes data.

    Copied from Keras to Transformers to avoid versioning issues.
    ÚdecodeÚutf8r   rv   r   )r|   Úhasattrr‡   Úextend)r}   r   Únr~   rƒ   s        r   Úload_attributes_from_hdf5_grouprŒ   ÷   sê   € ð ˆu�{‰{ÑØINÏÉÐUYÑIZÖ[ÀA¤G¨A¨xÔ$8�—‘˜Ô ¸aÑ?Ð[ˆÐ[ð €Kð ˆØˆØ˜˜hÐ'Ñ'¨5¯;©;Ñ6Ø�K‰KØJOÏ+É+ÐV\Ð`dÐfnÐ_oÑVoÑJpÖqÀQ¤W¨Q°Ô%9�—‘˜&Ô!¸qÑ@Òqôð ˜‰MˆHð	 ˜˜hÐ'Ñ'¨5¯;©;Ò6ð
 €Kùò \ùò rs    &C
Â&Cc                 óH   — d„ }t         j                  j                  || «      S )zwExpands 1-dimensional `Tensor`s into 2-dimensional `Tensor`s.
    Copied from Keras to here to avoid versioning issues.c                 óš   — t        | t        j                  «      r0| j                  j                  dk(  rt        j
                  | d¬«      S | S )Nr   r'   rS   )r   r   rb   r   r+   Úexpand_dims)Úts    r   Ú_expand_single_1d_tensorz+expand_1d.<locals>._expand_single_1d_tensor  s5   € Ü�aœŸ™Ô#¨¯©¯©¸Ò(9Ü—>‘> !¨"Ô-Ð-Øˆr#   )r   ÚnestÚmap_structure)r~   r‘   s     r   Ú	expand_1dr”     s!   € òô
 �7‰7× Ñ Ð!9¸4Ó@Ð@r#   c                  óÜ   — | r9t        | d   t        t        f«      r t        | «      } t	        | d   «      | d<   | |fS d|v r*t        |d   t        t        f«      rt	        |d   «      |d<   | |fS )Nr   rs   )r   r   r   r   Údict)ÚargsÚkwargss     r   Úconvert_batch_encodingr™     st   € á”
˜4 ™7¤]´LÐ$AÔBÜ�D‹zˆÜ�t˜A‘w“-ˆˆQ‰ð �ˆ<Ðð 
�‰œ: f¨S¡k´MÄ<Ð3PÔQÜ˜6 #™;Ó'ˆˆs‰Ø�ˆ<Ðr#   )NN)gñhãˆµøä>r'   )Nr:   FN)r   r'   )Ú	input_ids)!Útypingr   r   rC   r   Ú
tensorflowr   Úfeature_extraction_utilsr   Útokenization_utils_baser   Úutilsr   Ú
get_loggerÚ__name__Úloggerrb   r   r   r,   r   Ústrr"   r8   ÚfloatrQ   r]   rf   rn   r…   rŒ   r”   r™   © r#   r   ú<module>r¦      s  ð÷ #ã Û å 2Ý 2Ý ð 
ˆ×	Ñ	˜HÓ	%€ðJ�u˜RŸY™Y¨¯
©
Ð2Ñ3ð J¸¸S¹	ó Jñ.E˜2Ÿ9™9ð E¨H°S©Mð EÈÐQTÉð EÐac×ajÑajó Eó0ðB aeñØNVÐW\Éoóó6(ð$+°"·)±)ð +ÀÇ	Á	ó +ñ8¨2¯9©9ð Àð ÐSVð Ðimó ò(,!ò^ò8	Aór#   