o
    h~,                  	   @   s  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eZdeejejf dee fd	d
Zd+dejdee dee dejfddZd,ddZ	d-dee fddZd.ddZdejdejfddZd/dejded eddfd!d"Zd#d$ Zd%d& Zd'd( Zd)d* Z dS )0    )OptionalUnionN   )BatchFeature)BatchEncoding)loggingtensorreturnc                    sT   t | tjrt| jS t|  | jtdkr S | j } fddt|D S )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.
    Nc                    s$   g | ]\}}|d u r | n|qS N ).0isdynamicr   i/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/tf_utils.py
<listcomp>0      $ zshape_list.<locals>.<listcomp>)	
isinstancenpndarraylistshapetfTensorShapeas_list	enumerate)r   staticr   r   r   
shape_list   s   



r   logitsaxisnamec                 C   s   t jj| d ||d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"   r   r   r   stable_softmax3   s   r%   h㈵>c           	      C   s   |j jdks|j jdkst|tstdtjj| |gdd\}}|dkr@dg| j j }t| | ||< t	||}t	||}tjj
| |||||d}|S )Nr   zOOnly 1D weight and bias tensors are supported for now, with only a single axis.T)axeskeepdimsr'   )offsetscalevariance_epsilon)r   rankr   intNotImplementedErrorr   r#   momentsr   reshapebatch_normalization)	inputsweightbiasepsilonr    meanvariancer   outputsr   r   r   functional_layernormK   s"   "r:           Fr+   c           	      C   s   |dkrt d|r|durt d|r1tjt| d t|d ftjd}tjjj|dd}|durP|jj	s=|jj
rPt|dktd| jtd	| j}td
| |}|du ritt|d |jd }||9 }|duru||7 }tj|}|| S )zDTF equivalent for torch's nn.functional.scaled_dot_product_attentionr;   zDropout is not supported in this implementation - file an issue with Transformers and ping @Rocketknight1 if you need it for a port!Nz?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   probsr   r   r   scaled_dot_product_attentionk   s&   (&rR   c                 C   s   |dk r
|| j j7 }|dk r|| j j7 }||kr| S t | }tj|||d  }tj|d | |g||d d  gdd}t| |S )Nr   r   r    )r   r-   r   mathreduce_prodconcatr1   )input	start_dimend_dimin_shapeflattened_dim	out_shaper   r   r   flatten   s   
*r]   encoder_attention_maskc                 C   s   t | tjst| } | jjdkr| dddddddf }| jjdkr1| ddddddf }td| j| |jj }|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-   rI   r>   min)r^   encoder_extended_attention_maskr   r   r   invert_attention_mask   s   

re   	input_ids	embed_dimtensor_namec              
   C   s>   t jj| t j|| jdd| dt j|  d| dd dS )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_lessrI   r>   rT   
reduce_max)r   rg   rh   r   r   r   check_embeddings_within_bounds   s   

rm   c           	         s   d  fdd|D }|rt d  d| t|}d}t||}t fdd|D rD|d7 }t||}t fdd|D s/|dkr\t|D ]\}}|| jd	||f < qLd
S || j|< d
S )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   c                    s   g | ]
}t | kr|qS r   )lenr   xHDF5_OBJECT_HEADER_LIMITr   r   r      s    z1save_attributes_to_hdf5_group.<locals>.<listcomp>zSThe following attributes cannot be saved to HDF5 file because they are larger than z bytes: r   c                 3   s    | ]}|j  kV  qd S r
   )nbytesro   rq   r   r   	<genexpr>   s    z0save_attributes_to_hdf5_group.<locals>.<genexpr>%s%dN)RuntimeErrorr   asarrayarray_splitanyr   attrs)	groupr!   databad_attributesdata_npy
num_chunkschunked_datachunk_id
chunk_datar   rq   r   save_attributes_to_hdf5_group   s,   
r   c                 C   s|   || j v rdd | j | D }|S g }d}d||f | j v r<|dd | j d||f  D  |d7 }d||f | j v s|S )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.
    c                 S   $   g | ]}t |d r|dn|qS decodeutf8hasattrr   r   nr   r   r   r     r   z3load_attributes_from_hdf5_group.<locals>.<listcomp>r   ru   c                 S   r   r   r   r   r   r   r   r     r   r   )rz   extend)r{   r!   r|   r   r   r   r   load_attributes_from_hdf5_group   s   
	r   c                 C   s   dd }t j|| S )zwExpands 1-dimensional `Tensor`s into 2-dimensional `Tensor`s.
    Copied from Keras to here to avoid versioning issues.c                 S   s*   t | tjr| jjdkrtj| ddS | S )Nr   r'   rS   )r   r   ra   r   r-   expand_dims)tr   r   r   _expand_single_1d_tensor  s   z+expand_1d.<locals>._expand_single_1d_tensor)r   nestmap_structure)r|   r   r   r   r   	expand_1d  s   r   c                  O   sh   | rt | d ttfrt| } t| d | d< | |fS d|v r0t |d ttfr0t|d |d< | |fS )Nr   rp   )r   r   r   r   dict)argskwargsr   r   r   convert_batch_encoding  s   r   )NN)r&   r'   )Nr;   FN)r   r'   )rf   )!typingr   r   rD   r   
tensorflowr   feature_extraction_utilsr   tokenization_utils_baser   utilsr   
get_logger__name__loggerra   r   r   r.   r   strr%   r:   floatrR   r]   re   rm   r   r   r   r   r   r   r   r   <module>   s*   
"(
!

/