U
    9%eK                     @   s  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
 e
eZdZG dd dZG dd	 d	ZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )     N   )add_start_docstrings)
get_loggerad  
    Args:
        input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

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

            [What are input IDs?](../glossary#input-ids)
        scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`):
            Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam
            search or log softmax for each vocabulary token when using beam search
        kwargs (`Dict[str, Any]`, *optional*):
            Additional logits processor specific kwargs.

    Return:
        `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores.

c                   @   s0   e Zd ZdZeeejejejdddZdS )FlaxLogitsProcessorzSAbstract base class for all logit processors that can be applied during generation.	input_idsscoresreturnc                 C   s   t | j ddS )z"Flax method for processing logits.H is an abstract class. Only classes inheriting this class can be called.NNotImplementedError	__class__selfr   r    r   j/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/generation/flax_logits_process.py__call__5   s    
zFlaxLogitsProcessor.__call__N	__name__
__module____qualname____doc__r   !LOGITS_PROCESSOR_INPUTS_DOCSTRINGjnpndarrayr   r   r   r   r   r   2   s   r   c                   @   s0   e Zd ZdZeeejejejdddZdS )FlaxLogitsWarperzjAbstract base class for all logit warpers that can be applied during generation with multinomial sampling.r   c                 C   s   t | j ddS )zFlax method for warping logits.r
   Nr   r   r   r   r   r   @   s    
zFlaxLogitsWarper.__call__Nr   r   r   r   r   r   =   s   r   c                   @   s2   e Zd ZdZeeejejeejdddZ	dS )FlaxLogitsProcessorLista.  
    This class can be used to create a list of [`FlaxLogitsProcessor`] or [`FlaxLogitsWarper`] to subsequently process
    a `scores` input tensor. This class inherits from list and adds a specific *__call__* method to apply each
    [`FlaxLogitsProcessor`] or [`FlaxLogitsWarper`] to the inputs.
    r   r   cur_lenr	   c                    s   | D ]}t |jj}t|dkrzt fddt| dd  D shtdt|  d|j	 d||||f }q||||}q|S )N   c                 3   s   | ]}| kV  qd S Nr   ).0argkwargsr   r   	<genexpr>T   s     z3FlaxLogitsProcessorList.__call__.<locals>.<genexpr>r   z,Make sure that all the required parameters: z for z$ are passed to the logits processor.)
inspect	signaturer   
parameterslenalllistkeys
ValueErrorr   )r   r   r   r   r$   	processorZfunction_argsr   r#   r   r   O   s    &z FlaxLogitsProcessorList.__call__N)
r   r   r   r   r   r   r   r   intr   r   r   r   r   r   H   s   r   c                   @   s8   e Zd ZdZedddZejejeejdddZ	dS )	FlaxTemperatureLogitsWarperz
    [`FlaxLogitsWarper`] for temperature (exponential scaling output probability distribution).

    Args:
        temperature (`float`):
            The value used to module the logits distribution.
    temperaturec                 C   s*   t |tr|dks td| || _d S )Nr   z:`temperature` has to be a strictly positive float, but is )
isinstancefloatr-   r2   )r   r2   r   r   r   __init__h   s    z$FlaxTemperatureLogitsWarper.__init__r   c                 C   s   || j  }|S r    r1   r   r   r   r   r   r   r   r   n   s    
z$FlaxTemperatureLogitsWarper.__call__N)
r   r   r   r   r4   r5   r   r   r/   r   r   r   r   r   r0   _   s   r0   c                   @   sH   e Zd ZdZed dfeeedddZejejeejddd	Z	d
S )FlaxTopPLogitsWarperaH  
    [`FlaxLogitsWarper`] that performs top-p, i.e. restricting to top tokens summing to prob_cut_off <= prob_cut_off.

    Args:
        top_p (`float`):
            If set to < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or
            higher are kept for generation.
        filter_value (`float`, *optional*, defaults to `-float("Inf")`):
            All filtered values will be set to this float value.
        min_tokens_to_keep (`int`, *optional*, defaults to 1):
            Minimum number of tokens that cannot be filtered.
    Inf   )top_pfilter_valuemin_tokens_to_keepc                 C   s^   t |tr|dk s|dkr(td| t |tr:|dk rHtd| || _|| _|| _d S )Nr   g      ?z.`top_p` has to be a float > 0 and < 1, but is r9   z:`min_tokens_to_keep` has to be a positive integer, but is )r3   r4   r-   r/   r:   r;   r<   )r   r:   r;   r<   r   r   r   r5      s    zFlaxTopPLogitsWarper.__init__r   c                 C   s   t ||jd \}}t|| j}tjj|ddj	dd}|| j
k }t|d}||jd d df dO }|jd d d | jf d}t|||}	tj ||	d }
|
S )NZaxisr9   r   T)laxtop_kshaper   Z	full_liker;   jaxnnZsoftmaxZcumsumr:   Zrollatsetr<   whereZsort_key_val)r   r   r   r   topk_scorestopk_indicesZmask_scoresZcumulative_probsZ
score_maskZtopk_next_scoresnext_scoresr   r   r   r      s    
zFlaxTopPLogitsWarper.__call__N
r   r   r   r   r4   r/   r5   r   r   r   r   r   r   r   r7   s   s   
r7   c                   @   sH   e Zd ZdZed dfeeedddZejejeejddd	Z	d
S )FlaxTopKLogitsWarpera  
    [`FlaxLogitsWarper`] that performs top-k, i.e. restricting to the k highest probability elements.

    Args:
        top_k (`int`):
            The number of highest probability vocabulary tokens to keep for top-k-filtering.
        filter_value (`float`, *optional*, defaults to `-float("Inf")`):
            All filtered values will be set to this float value.
        min_tokens_to_keep (`int`, *optional*, defaults to 1):
            Minimum number of tokens that cannot be filtered.
    r8   r9   )r@   r;   r<   c                 C   s6   t |tr|dkr td| t||| _|| _d S )Nr   z6`top_k` has to be a strictly positive integer, but is )r3   r/   r-   maxr@   r;   )r   r@   r;   r<   r   r   r   r5      s    zFlaxTopKLogitsWarper.__init__r   c                 C   s   |j \}}t|| | j}t| j|j d }t||\}}	tt|| d d d f ||f	 }
|	 }|		 |
 }|j
| |}|||}|S )Nr=   )rA   r   fullr;   minr@   r?   Zbroadcast_toZarangeflattenrD   rE   Zreshape)r   r   r   r   
batch_size
vocab_sizeZnext_scores_flatZtopkrG   rH   shiftZtopk_scores_flatZtopk_indices_flatrI   r   r   r   r      s    
*zFlaxTopKLogitsWarper.__call__NrJ   r   r   r   r   rK      s   rK   c                   @   s8   e Zd ZdZedddZejejeejdddZdS )	!FlaxForcedBOSTokenLogitsProcessorz
    [`FlaxLogitsProcessor`] that enforces the specified token as the first generated token.

    Args:
        bos_token_id (`int`):
            The id of the token to force as the first generated token.
    bos_token_idc                 C   s
   || _ d S r    rT   )r   rU   r   r   r   r5      s    z*FlaxForcedBOSTokenLogitsProcessor.__init__r   c                 C   sN   t |jtd }dt |d  }t ||jd d | jf d|}|S Ninfr9   r   )	r   rM   rA   r4   bool_rF   rD   rU   rE   r   r   r   r   
new_scoresapply_penaltyr   r   r   r      s    $z*FlaxForcedBOSTokenLogitsProcessor.__call__N	r   r   r   r   r/   r5   r   r   r   r   r   r   r   rS      s   rS   c                   @   s:   e Zd ZdZeedddZejejeejdddZdS )	!FlaxForcedEOSTokenLogitsProcessorae  
    [`FlaxLogitsProcessor`] that enforces the specified token as the last generated token when `max_length` is reached.

    Args:
        max_length (`int`):
            The maximum length of the sequence to be generated.
        eos_token_id (`int`):
            The id of the token to force as the last generated token when `max_length` is reached.
    
max_lengtheos_token_idc                 C   s   || _ || _d S r    r^   )r   r_   r`   r   r   r   r5      s    z*FlaxForcedEOSTokenLogitsProcessor.__init__r   c                 C   sT   t |jtd }dt || j d  }t ||jd d | jf 	d|}|S rV   )
r   rM   rA   r4   rX   r_   rF   rD   r`   rE   rY   r   r   r   r      s    $z*FlaxForcedEOSTokenLogitsProcessor.__call__Nr\   r   r   r   r   r]      s   
r]   c                   @   s:   e Zd ZdZeedddZejejeejdddZdS )	FlaxMinLengthLogitsProcessora3  
    [`FlaxLogitsProcessor`] enforcing a min-length by setting EOS probability to 0.

    Args:
        min_length (`int`):
            The minimum length below which the score of `eos_token_id` is set to `-float("Inf")`.
        eos_token_id (`int`):
            The id of the *end-of-sequence* token.
    )
min_lengthr`   c                 C   sP   t |tr|dk r td| t |tr2|dk r@td| || _|| _d S )Nr   z2`min_length` has to be a positive integer, but is z4`eos_token_id` has to be a positive integer, but is )r3   r/   r-   rb   r`   )r   rb   r`   r   r   r   r5      s    z%FlaxMinLengthLogitsProcessor.__init__r   c                 C   sF   dt || j dd }t ||jd d | jf td |}|S )Nr9   r   rW   )r   Zcliprb   rF   rD   r`   rE   r4   r   r   r   r   r[   r   r   r   r     s    *z%FlaxMinLengthLogitsProcessor.__call__Nr\   r   r   r   r   ra      s   

ra   c                   @   s&   e Zd ZdZdd ZedddZdS )(FlaxSuppressTokensAtBeginLogitsProcessora  
    [`FlaxLogitsProcessor`] supressing a list of tokens as soon as the `generate` function starts generating using
    `begin_index` tokens. This should ensure that the tokens defined by `begin_suppress_tokens` are not sampled at the
    begining of the generation.

    Args:
        begin_suppress_tokens (`List[int]`):
            Tokens to not sample.
        begin_index (`int`):
            Index where the tokens are suppressed.
    c                 C   s   t || _|| _d S r    )r+   begin_suppress_tokensbegin_index)r   re   rf   r   r   r   r5     s    
z1FlaxSuppressTokensAtBeginLogitsProcessor.__init__)r   c                 C   sB   dt || j  }t ||jd d | jf td |}|S )Nr9   rW   )r   rX   rf   rF   rD   re   rE   r4   rc   r   r   r   r      s    *z1FlaxSuppressTokensAtBeginLogitsProcessor.__call__N)r   r   r   r   r5   r/   r   r   r   r   r   rd     s   rd   c                   @   s8   e Zd ZdZedddZejejeejdddZ	dS )	!FlaxSuppressTokensLogitsProcessorz
    [`FlaxLogitsProcessor`] suppressing a list of tokens at each decoding step. The processor will set their log probs
    to be `-inf` so they are not sampled.

    Args:
        suppress_tokens (`list`):
            Tokens to not sample.
    )suppress_tokensc                 C   s   t || _d S r    )r+   rh   )r   rh   r   r   r   r5   2  s    z*FlaxSuppressTokensLogitsProcessor.__init__r   c                 C   s    |j d| jf td }|S )N.rW   )rD   rh   rE   r4   r6   r   r   r   r   5  s    z*FlaxSuppressTokensLogitsProcessor.__call__N)
r   r   r   r   r+   r5   r   r   r/   r   r   r   r   r   rg   (  s   	rg   c                   @   s2   e Zd ZdZdd ZejejeejdddZdS )FlaxForceTokensLogitsProcessora  
    [`FlaxLogitsProcessor`] that takes a list of pairs of integers which indicates a mapping from generation indices to
    token indices that will be forced before sampling. The processor will set their log probs to 0 and all other tokens
    to `-inf` so that they are sampled at their corresponding index.

    Args:
        force_token_map (`list`):
            Map giving token ids and indices where they will be forced to be sampled.
    c                 C   sb   t |}tjt| d tjdd }| D ] \}}|d k	r0|j| |}q0t|| _	d S )Nr9   dtyper=   )
dictr   ZonesrL   r,   Zint32itemsrD   rE   force_token_array)r   Zforce_token_maprn   indextokenr   r   r   r5   F  s     z'FlaxForceTokensLogitsProcessor.__init__r   c                    sB   fdd t jjd kfdd fddS )Nc                    sX    j d }j|  }tj  jdtd  }tj|df jd}t||d|f}|S )Nr   rj   rW   r9   )	rA   rn   r   Z	ones_likerk   r4   Zzerosr?   Zdynamic_update_slice)Zgeneration_idxrP   current_tokenrZ   Zupdates)r   r   r   r   _force_tokenR  s    

z=FlaxForceTokensLogitsProcessor.__call__.<locals>._force_tokenr   c                      s    S r    r   r   r   r   r   <lambda>^      z9FlaxForceTokensLogitsProcessor.__call__.<locals>.<lambda>c                      s*   t j dk fddfddS )Nr   c                      s    S r    r   r   )rr   r   r   r   rt   c  ru   zKFlaxForceTokensLogitsProcessor.__call__.<locals>.<lambda>.<locals>.<lambda>c                      s    S r    r   r   rs   r   r   rt   e  ru   )r?   condrn   r   rr   r   r   r   r   r   rt   `  s   
)r?   rv   rn   rA   r6   r   rw   r   r   Q  s    	
z'FlaxForceTokensLogitsProcessor.__call__N)	r   r   r   r   r5   r   r   r/   r   r   r   r   r   ri   ;  s   
ri   c                   @   s    e Zd ZdZdd Zdd ZdS )#FlaxWhisperTimeStampLogitsProcessora{  
    Whisper specific Processor. This processor can be used to force a list of tokens. The processor will set their log
    probs to `inf` so that they are sampled at their corresponding index.

    Args:
        generate_config (`GenerateConfig`):
            The generate config used to generate the output. The following parameters are required:
                eos_token_id (`int`, *optional*, defaults to 50257):
                    The id of the *end-of-sequence* token.
                no_timestamps_token_id (`int`, *optional*, defaults to 50363):
                    The id of the `"<|notimestamps|>"` token.
                max_initial_timestamp_index (`int`, *optional*, defaults to 1):
                    Used to set the maximum value of the initial timestamp. This is used to prevent the model from
                    predicting timestamps that are too far in the future.
    c                 C   sl   |j | _ |j| _|jd | _|d | _|jr:|  jd7  _t|drN|j| _n|j| _| jd krh|j| _d S )Nr9   r   max_initial_timestamp_index)r`   no_timestamps_token_idtimestamp_beginrf   Zis_multilingualhasattrry   rQ   )r   Zgenerate_configZmodel_configZdecoder_input_lengthr   r   r   r5   |  s    



z,FlaxWhisperTimeStampLogitsProcessor.__init__c           	         s   |j d d jf td } fdd}t|||}t jkdd}tj	d k	dod|d}j
j	 }t||j d d |d d f td |}tjj|dd}fd	d
}t|||}|S )NrW   c                    s   t  j dkdd}t |  d  jkdo2|d}t  j dk dd}t |  d  jkd|}t |t |dk|jjd  td |jd j td |S )Nr9   TFr   r   rW   )r   rF   rf   r{   rD   rE   r4   r`   )Zinput_ids_kscores_kZlast_was_timestampZpenultimate_was_timestampr   r   r   r   handle_pairs  s*    zBFlaxWhisperTimeStampLogitsProcessor.__call__.<locals>.handle_pairsTFr9   r=   r>   c                    sX   t jj|  jd  dd}t| d  j }t||k|jd  j t	d |S )Nr=   r>   rW   )
rB   rC   Z	logsumexpr{   r   rL   rF   rD   rE   r4   )Z
logprobs_kr}   Ztimestamp_logprobZmax_text_token_logprob)r   r   r   handle_cumulative_probs  s    zMFlaxWhisperTimeStampLogitsProcessor.__call__.<locals>.handle_cumulative_probs)rD   rz   rE   r4   rB   Zvmapr   rF   rf   ry   r{   rC   Zlog_softmax)	r   r   r   r   r   Zapply_max_initial_timestampZlast_allowedZlogprobsr   r   r~   r   r     s&     $	z,FlaxWhisperTimeStampLogitsProcessor.__call__N)r   r   r   r   r5   r   r   r   r   r   rx   k  s   rx   )r&   rB   Zjax.laxr?   Z	jax.numpynumpyr   utilsr   Zutils.loggingr   r   loggerr   r   r   r+   r   r0   r7   rK   rS   r]   ra   rd   rg   ri   rx   r   r   r   r   <module>   s(   ,#0