U
    ,È-e®.  ã                   @   sö  d dl Z d dlmZmZmZmZmZmZ d dlZd dl	m
Z
 d dlmZmZ d dlmZ ddlmZmZ ejgZe e¡Ze
 e
 ej¡j¡Zee
 d¡kZee
 d	¡kZee
 d
¡kZee
 d
¡k Zee
 d¡kZdd„ Z d%ej!ej"e#ej!dœdd„Z$G dd„ dej%ƒZ&d&e&ej"e#e&dœdd„Z'd'eej!e&f ej"ee# eej!e&f dœdd„Z(edej)f e#e#ej)dœdd„Z*ee# e#e#ee# eee# ej"f dœdd„Z+ddœeej)eej) f ee, eej)df dœd d!„Z-ej)eej.e#e#f d"œd#d$„Z/dS )(é    N)ÚCallableÚListÚOptionalÚSetÚTupleÚUnion)Úversion)Ústorage_ptrÚstorage_size)Únné   )Úis_torch_tpu_availableÚloggingz2.0z1.12z1.11z1.8.0c                 C   s6   ddl m} tr |||| j|ƒS |||| j|jƒS dS )z 
    A function that calls the internal `_softmax_backward_data` PyTorch method and that adjusts the arguments according
    to the torch version detected.
    r   )Ú_softmax_backward_dataN)Útorchr   Úis_torch_less_than_1_11ÚdimZdtype)ÚparentZgrad_outputÚoutputr   Úselfr   © r   ú[/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/transformers/pytorch_utils.pyÚsoftmax_backward_data&   s    r   )ÚlayerÚindexr   Úreturnc                 C   sè   |  | jj¡}| j ||¡ ¡  ¡ }| jdk	rX|dkrF| j ¡  ¡ }n| j|  ¡  ¡ }t| j ¡ ƒ}t	|ƒ||< t
j|d |d | jdk	d  | jj¡}d|j_|j | ¡ ¡ d|j_| jdk	räd|j_|j | ¡ ¡ d|j_|S )a   
    Prune a linear layer to keep only entries in index.

    Used to remove heads.

    Args:
        layer (`torch.nn.Linear`): The layer to prune.
        index (`torch.LongTensor`): The indices to keep in the layer.
        dim (`int`, *optional*, defaults to 0): The dimension on which to keep the indices.

    Returns:
        `torch.nn.Linear`: The pruned layer as a new layer with `requires_grad=True`.
    Nr   r   )ÚbiasFT)ÚtoÚweightÚdeviceÚindex_selectÚcloneÚdetachr   ÚlistÚsizeÚlenr   ÚLinearÚrequires_gradÚcopy_Ú
contiguous©r   r   r   ÚWÚbÚnew_sizeZ	new_layerr   r   r   Úprune_linear_layer4   s"    
(
r.   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚConv1Da  
    1D-convolutional layer as defined by Radford et al. for OpenAI GPT (and also used in GPT-2).

    Basically works like a linear layer but the weights are transposed.

    Args:
        nf (`int`): The number of output features.
        nx (`int`): The number of input features.
    c                    sL   t ƒ  ¡  || _t t ||¡¡| _t t |¡¡| _	tj
j| jdd d S )Ng{®Gáz”?)Zstd)ÚsuperÚ__init__Únfr   Ú	Parameterr   Úemptyr   Zzerosr   ÚinitZnormal_)r   r2   Únx©Ú	__class__r   r   r1   a   s
    
zConv1D.__init__c              	   C   sF   |  ¡ d d… | jf }t | j| d|  d¡¡| j¡}| |¡}|S )Néÿÿÿÿ)r$   r2   r   Zaddmmr   Úviewr   )r   ÚxZsize_outr   r   r   Úforwardh   s     
zConv1D.forward)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r1   r<   Ú__classcell__r   r   r7   r   r/   V   s   
r/   c                 C   sÈ   |  | jj¡}| j ||¡ ¡  ¡ }|dkr<| j ¡  ¡ }n| j|  ¡  ¡ }t| j ¡ ƒ}t	|ƒ||< t
|d |d ƒ  | jj¡}d|j_|j | ¡ ¡ d|j_d|j_|j | ¡ ¡ d|j_|S )a  
    Prune a Conv1D layer to keep only entries in index. A Conv1D work as a Linear layer (see e.g. BERT) but the weights
    are transposed.

    Used to remove heads.

    Args:
        layer ([`~pytorch_utils.Conv1D`]): The layer to prune.
        index (`torch.LongTensor`): The indices to keep in the layer.
        dim (`int`, *optional*, defaults to 1): The dimension on which to keep the indices.

    Returns:
        [`~pytorch_utils.Conv1D`]: The pruned layer as a new layer with `requires_grad=True`.
    r   r   FT)r   r   r   r    r!   r"   r   r#   r$   r%   r/   r'   r(   r)   r*   r   r   r   Úprune_conv1d_layero   s    rB   c                 C   s^   t | tjƒr&t| ||dkrdn|dS t | tƒrJt| ||dkrBdn|dS td| j› ƒ‚dS )aÇ  
    Prune a Conv1D or linear layer to keep only entries in index.

    Used to remove heads.

    Args:
        layer (`Union[torch.nn.Linear, Conv1D]`): The layer to prune.
        index (`torch.LongTensor`): The indices to keep in the layer.
        dim (`int`, *optional*): The dimension on which to keep the indices.

    Returns:
        `torch.nn.Linear` or [`~pytorch_utils.Conv1D`]: The pruned layer as a new layer with `requires_grad=True`.
    Nr   ©r   r   zCan't prune layer of class )Ú
isinstancer   r&   r.   r/   rB   Ú
ValueErrorr8   )r   r   r   r   r   r   Úprune_layer   s
    
rF   .)Ú
forward_fnÚ
chunk_sizeÚ	chunk_dimr   c           	         s(  t |ƒdkst|› dƒ‚t t ˆ¡jƒ}|t |ƒkrPtd|› dt |ƒ› dƒ‚|dkr |d jˆ  }|D ],}|jˆ  |krltd|› d|jˆ  › ƒ‚ql|d jˆ  | dkrÎtd|d jˆ  › d	|› ƒ‚|d jˆ  | ‰t‡ ‡fd
d„|D ƒƒ}t‡fdd„t|Ž D ƒƒ}t	j
|ˆ dS ˆ|Ž S )aZ  
    This function chunks the `input_tensors` into smaller input tensor parts of size `chunk_size` over the dimension
    `chunk_dim`. It then applies a layer `forward_fn` to each chunk independently to save memory.

    If the `forward_fn` is independent across the `chunk_dim` this function will yield the same result as directly
    applying `forward_fn` to `input_tensors`.

    Args:
        forward_fn (`Callable[..., torch.Tensor]`):
            The forward function of the model.
        chunk_size (`int`):
            The chunk size of a chunked tensor: `num_chunks = len(input_tensors[0]) / chunk_size`.
        chunk_dim (`int`):
            The dimension over which the `input_tensors` should be chunked.
        input_tensors (`Tuple[torch.Tensor]`):
            The input tensors of `forward_fn` which will be chunked

    Returns:
        `torch.Tensor`: A tensor with the same shape as the `forward_fn` would have given if applied`.


    Examples:

    ```python
    # rename the usual forward() fn to forward_chunk()
    def forward_chunk(self, hidden_states):
        hidden_states = self.decoder(hidden_states)
        return hidden_states


    # implement a chunked forward function
    def forward(self, hidden_states):
        return apply_chunking_to_forward(self.forward_chunk, self.chunk_size_lm_head, self.seq_len_dim, hidden_states)
    ```r   z" has to be a tuple/list of tensorszforward_chunk_fn expects z arguments, but only z input tensors are givenz/All input tenors have to be of the same shape: z, found shape zThe dimension to be chunked z( has to be a multiple of the chunk size c                 3   s   | ]}|j ˆˆ d V  qdS )rC   N)Úchunk)Ú.0Úinput_tensor)rI   Ú
num_chunksr   r   Ú	<genexpr>ê   s     z,apply_chunking_to_forward.<locals>.<genexpr>c                 3   s   | ]}ˆ |Ž V  qd S )Nr   )rK   Zinput_tensors_chunk)rG   r   r   rN   ì   s     rC   )r%   ÚAssertionErrorÚinspectÚ	signatureÚ
parametersrE   ÚshapeÚtupleÚzipr   Úcat)	rG   rH   rI   Zinput_tensorsZnum_args_in_forward_chunk_fnZtensor_shaperL   Zinput_tensors_chunksZoutput_chunksr   )rI   rG   rM   r   Úapply_chunking_to_forward¨   s,    &ÿ
ÿÿrW   )ÚheadsÚn_headsÚ	head_sizeÚalready_pruned_headsr   c                    sv   t  ||¡}t| ƒ| } | D ]&‰ ˆ t‡ fdd„|D ƒƒ ‰ d|ˆ < q| d¡ ¡  d¡}t  t|ƒ¡|  	¡ }| |fS )a3  
    Finds the heads and their indices taking `already_pruned_heads` into account.

    Args:
        heads (`List[int]`): List of the indices of heads to prune.
        n_heads (`int`): The number of heads in the model.
        head_size (`int`): The size of each head.
        already_pruned_heads (`Set[int]`): A set of already pruned heads.

    Returns:
        `Tuple[Set[int], torch.LongTensor]`: A tuple with the indices of heads to prune taking `already_pruned_heads`
        into account and the indices of rows/columns to keep in the layer weight.
    c                 3   s   | ]}|ˆ k rd ndV  qdS )r   r   Nr   )rK   Úh©Úheadr   r   rN     s     z3find_pruneable_heads_and_indices.<locals>.<genexpr>r   r9   r   )
r   ZonesÚsetÚsumr:   r)   ÚeqZaranger%   Úlong)rX   rY   rZ   r[   Úmaskr   r   r]   r   Ú find_pruneable_heads_and_indicesó   s    
rd   )Úindexing)Útensorsre   r   c                 G   s   t j|d| iŽS )z·
    Wrapper around torch.meshgrid to avoid warning messages about the introduced `indexing` argument.

    Reference: https://pytorch.org/docs/1.13/generated/torch.meshgrid.html
    re   )r   Úmeshgrid)re   rf   r   r   r   rg     s    rg   )Útensorr   c                 C   s@   | j jdkr(tƒ r(ddl}|j | ¡}nt| ƒ}| j |t| ƒfS )a“  
    Unique identifier to a tensor storage. Multiple different tensors can share the same underlying storage. For
    example, "meta" tensors all share the same storage, and thus their identifier will all be equal. This identifier is
    guaranteed to be unique and constant for this tensor's storage during its lifetime. Two tensor storages with
    non-overlapping lifetimes may have the same id.
    Zxlar   N)r   Útyper   Ú	torch_xlaZ_XLACZ_xla_get_tensor_idr	   r
   )rh   rj   Ú	unique_idr   r   r   Úid_tensor_storage  s
    rl   )r   )r   )N)0rP   Útypingr   r   r   r   r   r   r   Ú	packagingr   Zsafetensors.torchr	   r
   r   Úutilsr   r   Z	LayerNormZALL_LAYERNORM_LAYERSZ
get_loggerr=   ÚloggerÚparseÚ__version__Úbase_versionZparsed_torch_version_baseZ"is_torch_greater_or_equal_than_2_0Z#is_torch_greater_or_equal_than_1_12Z#is_torch_greater_or_equal_than_1_11r   Zis_torch_1_8_0r   r&   Z
LongTensorÚintr.   ÚModuler/   rB   rF   ZTensorrW   rd   Ústrrg   r   rl   r   r   r   r   Ú<module>   sV    
"" ÿ  þ  þL   þÿ þ