U
    9%e"E                    @   s  d dl Z d dlZd dlmZmZmZmZmZmZm	Z	 d dl
Zd dl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!e"ej#e"d!d"d#Z$d$d% Z%e"ej#e"e"eee"  d&d'd(Z&G d)d* d*eZ'G d+d, d,eZ(G d-d. d.eZ)G d/d0 d0e)Z*G d1d2 d2eZ+G d3d4 d4eZ,G d5d6 d6eZ-G d7d8 d8eZ.G d9d: d:eZ/G d;d< d<eZ0G d=d> d>eeZ1G d?d@ d@eZ2G dAdB dBeZ3G dCdD dDeZ4G dEdF dFeZ5G dGdH dHeZ6G dIdJ dJeZ7G dKdL dLeZ8dS )M    N)CallableDictIterableListOptionalTupleUnion   )add_start_docstrings)
get_loggera[  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
        scores (`torch.FloatTensor` 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

    Return:
        `torch.FloatTensor` 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 )LogitsProcessorzSAbstract base class for all logit processors that can be applied during generation.	input_idsscoresreturnc                 C   s   t | j dd S NzH is an abstract class. Only classes inheriting this class can be called.NotImplementedError	__class__selfr   r    r   e/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/generation/logits_process.py__call__/   s    
zLogitsProcessor.__call__N
__name__
__module____qualname____doc__r
   !LOGITS_PROCESSOR_INPUTS_DOCSTRINGtorch
LongTensorFloatTensorr   r   r   r   r   r   ,   s   r   c                   @   s0   e Zd ZdZeeejejejdddZ	dS )LogitsWarperzjAbstract base class for all logit warpers that can be applied during generation with multinomial sampling.r   c                 C   s   t | j dd S r   r   r   r   r   r   r   9   s    
zLogitsWarper.__call__Nr   r   r   r   r   r#   6   s   r#   c                   @   s(   e Zd ZdZejejejdddZdS )LogitsProcessorLista  
    This class can be used to create a list of [`LogitsProcessor`] or [`LogitsWarper`] to subsequently process a
    `scores` input tensor. This class inherits from list and adds a specific *__call__* method to apply each
    [`LogitsProcessor`] or [`LogitsWarper`] to the inputs.
    r   c                    s   | D ]~}t |jj}t|dkrxt fddt| dd D shtdt|  d|j	 d|||f }q|||}q|S )a  
        Args:
            input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
                Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
            scores (`torch.FloatTensor` 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 kwargs that are specific to a logits processor.

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

        r	   c                 3   s   | ]}| kV  qd S Nr   ).0argkwargsr   r   	<genexpr>Z   s     z/LogitsProcessorList.__call__.<locals>.<genexpr>Nz,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)   	processorZfunction_argsr   r(   r   r   G   s    &zLogitsProcessorList.__call__N)r   r   r   r   r    r!   r"   r   r   r   r   r   r$   @   s   r$   c                   @   sL   e Zd ZdZeeeee f dddZee	e
je
je
jdddZdS )	MinLengthLogitsProcessora  
    [`LogitsProcessor`] 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 (`Union[int, List[int]]`):
            The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.
    )
min_lengtheos_token_idc                 C   st   t |tr|dk r td| t |tr0|g}tdd |D rTtdd |D rdtd|  || _|| _d S )Nr   z6`min_length` has to be a non-negative integer, but is c                 s   s   | ]}t |tV  qd S r%   
isinstanceintr&   ir   r   r   r*   v   s     z4MinLengthLogitsProcessor.__init__.<locals>.<genexpr>c                 s   s   | ]}|d k V  qdS r   Nr   r:   r   r   r   r*   v   s     =`eos_token_id` has to be a list of positive integers, but is )	r8   r9   r2   r/   anyloggerwarningr5   r6   )r   r5   r6   r   r   r   __init__p   s    
$z!MinLengthLogitsProcessor.__init__r   c                 C   s:   |j d }|| jk r6| jD ]}td |d d |f< q|S Ninf)shaper5   r6   float)r   r   r   cur_lenr;   r   r   r   r   |   s
    


z!MinLengthLogitsProcessor.__call__Nr   r   r   r   r9   r   r   rA   r
   r   r    r!   r"   r   r   r   r   r   r4   e   s   
r4   c                   @   sN   e Zd ZdZeeeeee f dddZee	e
je
je
jdddZdS )	!MinNewTokensLengthLogitsProcessora  
    [`LogitsProcessor`] enforcing a min-length of new tokens by setting EOS (End-Of-Sequence) token probability to 0.
    Note that for decoder-only models, such as Llama2, `min_length` will compute the length of `prompt + newly
    generated tokens` whereas for other models it will behave as `min_new_tokens`, that is, taking only into account
    the newly generated ones.

    Args:
        prompt_length_to_skip (`int`):
            The input tokens length. Not a valid argument when used with `generate` as it will automatically assign the
            input length.
        min_new_tokens (`int`):
            The minimum *new* tokens length below which the score of `eos_token_id` is set to `-float("Inf")`.
        eos_token_id (`Union[int, List[int]]`):
            The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.

    Examples:

    ```python
    >>> from transformers import AutoTokenizer, AutoModelForCausalLM

    >>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
    >>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
    >>> model.config.pad_token_id = model.config.eos_token_id
    >>> inputs = tokenizer(["Hugging Face Company is"], return_tensors="pt")

    >>> # If the maximum length (default = 20) is smaller than the minimum length constraint, the latter is ignored!
    >>> outputs = model.generate(**inputs, min_new_tokens=30)
    >>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
    Hugging Face Company is a company that has been working on a new product for the past year.

    >>> # For testing purposes, let's set `eos_token` to `"company"`, the first generated token. This will make
    >>> # generation end there.
    >>> outputs = model.generate(**inputs, eos_token_id=1664)
    >>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
    Hugging Face Company is a company

    >>> # Increasing `min_new_tokens` will make generation ignore occurences `"company"` (eos token) before the
    >>> # minimum length condition is honored.
    >>> outputs = model.generate(**inputs, min_new_tokens=2, eos_token_id=1664)
    >>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
    Hugging Face Company is a new company
    ```
    )prompt_length_to_skipmin_new_tokensr6   c                 C   s   d|fd|ffD ].\}}t |tr*|dk rtd| d| qt |trP|g}tdd |D rttdd |D rtd	|  || _|| _|| _	d S )
NrJ   rK   r   `z'` has to be a positive integer, but is c                 s   s   | ]}t |tV  qd S r%   r7   r:   r   r   r   r*      s     z=MinNewTokensLengthLogitsProcessor.__init__.<locals>.<genexpr>c                 s   s   | ]}|d k V  qdS r<   r   r:   r   r   r   r*      s     r=   )
r8   r9   r2   r/   r>   r?   r@   rJ   rK   r6   )r   rJ   rK   r6   Zarg_name	arg_valuer   r   r   rA      s    
$z*MinNewTokensLengthLogitsProcessor.__init__r   c                 C   s@   |j d | j }|| jk r<| jD ]}td |d d |f< q |S rB   )rE   rJ   rK   r6   rF   )r   r   r   Znew_tokens_lengthr;   r   r   r   r      s
    

z*MinNewTokensLengthLogitsProcessor.__call__NrH   r   r   r   r   rI      s   ,rI   c                   @   s>   e Zd ZdZedddZeeej	ej
ej
dddZdS )	TemperatureLogitsWarperu  
    [`LogitsWarper`] for temperature (exponential scaling output probability distribution), which effectively means
    that it can control the randomness of the predicted tokens.

    <Tip>

    Make sure that `do_sample=True` is included in the `generate` arguments otherwise the temperature value won't have
    any effect.

    </Tip>

    Args:
        temperature (`float`):
            Strictly positive float value used to modulate the logits distribution. A value smaller than `1` decreases
            randomness (and vice versa), with `0` being equivalent to shifting all probability mass to the most likely
            token.

    Examples:

    ```python
    >>> import torch
    >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed

    >>> set_seed(0)  # for reproducibility

    >>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
    >>> model = AutoModelForCausalLM.from_pretrained("gpt2")
    >>> model.config.pad_token_id = model.config.eos_token_id
    >>> inputs = tokenizer(["Hugging Face Company is"], return_tensors="pt")

    >>> # With temperature=1.0, the default, we consistently get random outputs due to random sampling.
    >>> generate_kwargs = {"max_new_tokens": 10, "do_sample": True, "temperature": 1.0, "num_return_sequences": 2}
    >>> outputs = model.generate(**inputs, **generate_kwargs)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
    ['Hugging Face Company is a joint venture between GEO Group, one of',
    'Hugging Face Company is not an exact science – but what we believe does']

    >>> # However, with temperature close to 0, it approximates greedy decoding strategies (invariant)
    >>> generate_kwargs["temperature"] = 0.0001
    >>> outputs = model.generate(**inputs, **generate_kwargs)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
    ['Hugging Face Company is a company that has been around for over 20 years',
    'Hugging Face Company is a company that has been around for over 20 years']
    ```
    temperaturec                 C   sJ   t |tr|dks@d| d}t |tr8|dkr8|d7 }t||| _d S )Nr   z`temperature` (=zX) has to be a strictly positive float, otherwise your next token scores will be invalid.        zI If you're looking for greedy decoding strategies, set `do_sample=False`.)r8   rF   r2   rP   )r   rP   Z
except_msgr   r   r   rA      s    
z TemperatureLogitsWarper.__init__r   c                 C   s   || j  }|S r%   rO   r   r   r   r   r     s    
z TemperatureLogitsWarper.__call__Nr   r   r   r   rF   rA   r
   r   r    r!   r"   r   r   r   r   r   rN      s   .rN   c                   @   s>   e Zd ZdZedddZeeej	ej
ej
dddZdS )	 RepetitionPenaltyLogitsProcessora  
    [`LogitsProcessor`] that prevents the repetition of previous tokens through an exponential penalty. This technique
    shares some similarities with coverage mechanisms and other aimed at reducing repetition. During the text
    generation process, the probability distribution for the next token is determined using a formula that incorporates
    token scores based on their occurrence in the generated sequence. Tokens with higher scores are more likely to be
    selected. The formula can be seen in the original [paper](https://arxiv.org/pdf/1909.05858.pdf). According to the
    paper a penalty of around 1.2 yields a good balance between truthful generation and lack of repetition.

    Args:
        repetition_penalty (`float`):
            The parameter for repetition penalty. 1.0 means no penalty. See [this
            paper](https://arxiv.org/pdf/1909.05858.pdf) for more details.

    Examples:

    ```py
    >>> from transformers import AutoTokenizer, AutoModelForCausalLM

    >>> # Initializing the model and tokenizer for it
    >>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
    >>> inputs = tokenizer(["I'm not going to"], return_tensors="pt")

    >>> # This shows a normal generate without any specific parameters
    >>> summary_ids = model.generate(**inputs)
    >>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True)[0])
    I'm not going to be able to do that. I'm going to be able to do that

    >>> # This generates a penalty for repeated tokens
    >>> penalized_ids = model.generate(**inputs, repetition_penalty=1.1)
    >>> print(tokenizer.batch_decode(penalized_ids, skip_special_tokens=True)[0])
    I'm not going to be able to do that. I'll just have to go out and play
    ```
    )penaltyc                 C   s*   t |tr|dks td| || _d S )Nr   6`penalty` has to be a strictly positive float, but is )r8   rF   r2   rT   )r   rT   r   r   r   rA   2  s    z)RepetitionPenaltyLogitsProcessor.__init__r   c                 C   s>   t |d|}t |dk || j || j }|d|| |S N   r   )r    gatherwhererT   scatter_r   r   r   Zscorer   r   r   r   8  s    z)RepetitionPenaltyLogitsProcessor.__call__NrR   r   r   r   r   rS     s   #rS   c                   @   sB   e Zd ZdZeejdddZee	ejej
ej
dddZdS )	'EncoderRepetitionPenaltyLogitsProcessoram  
    [`LogitsProcessor`] enforcing an exponential penalty on tokens that are not in the original input.

    Args:
        hallucination_penalty (`float`):
            The parameter for hallucination penalty. 1.0 means no penalty.
        encoder_input_ids (`torch.LongTensor`):
            The encoder_input_ids that should be repeated within the decoder ids.
    )rT   encoder_input_idsc                 C   s4   t |tr|dks td| d| | _|| _d S )Nr   rU   rW   )r8   rF   r2   rT   r]   )r   rT   r]   r   r   r   rA   N  s    
z0EncoderRepetitionPenaltyLogitsProcessor.__init__r   c                 C   sB   t |d| j}t |dk || j || j }|d| j| |S rV   )r    rX   r]   rY   rT   rZ   r[   r   r   r   r   U  s    z0EncoderRepetitionPenaltyLogitsProcessor.__call__N)r   r   r   r   rF   r    r!   rA   r
   r   r"   r   r   r   r   r   r\   C  s   
r\   c                   @   sN   e Zd ZdZed dfeeedddZeee	j
e	je	jddd	Zd
S )TopPLogitsWarpera  
    [`LogitsWarper`] 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.

    Examples:
    ```python
    >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed

    >>> set_seed(0)
    >>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")

    >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")

    >>> # With sampling, the output is unexpected -- sometimes too unexpected.
    >>> outputs = model.generate(**inputs, do_sample=True)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    A sequence: 1, 2, 0, 2, 2. 2, 2, 2, 2

    >>> # With `top_p` sampling, the output gets restricted to high-probability tokens.
    >>> # Pro tip: In practice, LLMs use `top_p` in the 0.9-0.95 range.
    >>> outputs = model.generate(**inputs, do_sample=True, top_p=0.1)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
    ```
    InfrW   )top_pfilter_valuemin_tokens_to_keepc                 C   s\   t |}|dk s|dkr&td| t|tr8|dk rFtd| || _|| _|| _d S )Nr   g      ?z.`top_p` has to be a float > 0 and < 1, but is rW   :`min_tokens_to_keep` has to be a positive integer, but is )rF   r2   r8   r9   r`   ra   rb   )r   r`   ra   rb   r   r   r   rA     s    zTopPLogitsWarper.__init__r   c                 C   sh   t j|dd\}}|jddjdd}|d| j k}d|d| j d f< |d||}||| j}|S )NFZ
descendingrC   dimrW   r   .)	r    sortsoftmaxcumsumr`   rb   scattermasked_fillra   )r   r   r   sorted_logitssorted_indicescumulative_probssorted_indices_to_removeindices_to_remover   r   r   r     s    zTopPLogitsWarper.__call__Nr   r   r   r   rF   r9   rA   r
   r   r    r!   r"   r   r   r   r   r   r^   `  s   #r^   c                   @   sN   e Zd ZdZed dfeeedddZeee	j
e	je	jddd	Zd
S )TopKLogitsWarpera  
    [`LogitsWarper`] 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.
    r_   rW   )top_kra   rb   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 )r8   r9   r2   maxrs   ra   )r   rs   ra   rb   r   r   r   rA     s    zTopKLogitsWarper.__init__r   c                 C   s<   t | j|d}|t||d d k }||| j}|S )NrC   r   .rC   N)minrs   sizer    topkrk   ra   )r   r   r   rs   rp   r   r   r   r     s    zTopKLogitsWarper.__call__Nrq   r   r   r   r   rr     s   rr   c                   @   sP   e Zd ZdZded dfeeedddZeee	j
e	je	jdd	d
ZdS )TypicalLogitsWarpera"  
    [`LogitsWarper`] that performs typical decoding. See [Typical Decoding for Natural Language
    Generation](https://arxiv.org/abs/2202.00666) for more information.

    Args:
        mass (`float`):
            Value of typical_p between 0 and 1 inclusive, defaults to 0.9.
        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.
    g?r_   rW   )massra   rb   c                 C   s\   t |}|dkr|dk s&td| t|tr8|dk rFtd| || _|| _|| _d S )Nr   rW   z2`typical_p` has to be a float > 0 and < 1, but is rc   )rF   r2   r8   r9   ra   rz   rb   )r   rz   ra   rb   r   r   r   rA     s    zTypicalLogitsWarper.__init__r   c                 C   s   t jjj|dd}t |}|| jddd }t | | }t j|dd\}}|d|}	|	j	ddj
dd}
|
| jk jdd}d||dk < ||d|ddk}d|d	d | jf< |d||}||| j}|S )
NrC   re   T)ZkeepdimFrd   rW   r   .)r    nn
functionallog_softmaxexpZnansumabsrg   rX   rh   ri   rz   sumviewrb   rj   rk   ra   )r   r   r   
normalizedpentZshifted_scoresZsorted_scoresrm   rl   rn   Zlast_indro   rp   r   r   r   r     s    
zTypicalLogitsWarper.__call__Nrq   r   r   r   r   ry     s    ry   c                   @   sN   e Zd ZdZed dfeeedddZeee	j
e	je	jddd	Zd
S )EpsilonLogitsWarpera  
    [`LogitsWarper`] that performs epsilon-sampling, i.e. restricting to tokens with `prob >= epsilon`. Takes the
    largest min_tokens_to_keep tokens if no tokens satisfy this constraint. See [Truncation Sampling as Language Model
    Desmoothing](https://arxiv.org/abs/2210.15191) for more information.

    Args:
        epsilon (`float`):
            If set to > 0, only the most tokens with probabilities `epsilon` 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.

    Examples:
    ```python
    >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed

    >>> set_seed(0)
    >>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")

    >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")

    >>> # With sampling, the output is unexpected -- sometimes too unexpected.
    >>> outputs = model.generate(**inputs, do_sample=True)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    A sequence: 1, 2, 0, 2, 2. 2, 2, 2, 2

    >>> # With epsilon sampling, the output gets restricted to high-probability tokens. Note that this is similar to
    >>> # Top P sampling, which restricts tokens based on their cumulative probability.
    >>> # Pro tip: The paper recomends using `epsilon_cutoff` values between 3e-4 and 9e-4
    >>> outputs = model.generate(**inputs, do_sample=True, epsilon_cutoff=0.1)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
    ```
    r_   rW   epsilonra   rb   c                 C   sZ   t |}|dks|dkr&td| t|}|dk rDtd| || _|| _|| _d S )Nr   rW   z7`epsilon_cutoff` has to be a float > 0 and < 1, but is C`min_tokens_to_keep` has to be a strictly positive integer, but is )rF   r2   r9   r   ra   rb   r   r   ra   rb   r   r   r   rA     s    zEpsilonLogitsWarper.__init__r   c                 C   sV   |j dd}|| jk }t| j|d}||t||d d k @ }||| j}|S )NrC   re   r   ru   )	rh   r   rv   rb   rw   r    rx   rk   ra   )r   r   r   probabilitiesrp   rs   r   r   r   r   "  s    
zEpsilonLogitsWarper.__call__Nrq   r   r   r   r   r     s   %r   c                   @   sN   e Zd ZdZed dfeeedddZeee	j
e	je	jddd	Zd
S )EtaLogitsWarpera(  
    [`LogitsWarper`] that performs eta-sampling, a technique to filter out tokens with probabilities below a dynamic
    cutoff value, `eta`, which is calculated based on a combination of the hyperparameter `epsilon` and the entropy of
    the token probabilities, i.e. `eta := min(epsilon, sqrt(epsilon * e^-entropy(probabilities)))`. Takes the largest
    min_tokens_to_keep tokens if no tokens satisfy this constraint. It addresses the issue of poor quality in long
    samples of text generated by neural language models leading to more coherent and fluent text. See [Truncation
    Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more information. Note: `do_sample`
    must be set to `True` for this `LogitsWarper` to work.


    Args:
        epsilon (`float`):
            A float value in the range (0, 1). Hyperparameter used to calculate the dynamic cutoff value, `eta`. The
            suggested values from the paper ranges from 3e-4 to 4e-3 depending on the size of the model.
        filter_value (`float`, *optional*, defaults to `-float("Inf")`):
            All values that are found to be below the dynamic cutoff value, `eta`, are set to this float value. This
            parameter is useful when logits need to be modified for very low probability tokens that should be excluded
            from generation entirely.
        min_tokens_to_keep (`int`, *optional*, defaults to 1):
            Specifies the minimum number of tokens that must be kept for generation, regardless of their probabilities.
            For example, if `min_tokens_to_keep` is set to 1, at least one token will always be kept for generation,
            even if all tokens have probabilities below the cutoff `eta`.

    Examples:
    ```python
    >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed

    >>> set_seed(0)
    >>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")

    >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")

    >>> # With sampling, the output is unexpected -- sometimes too unexpected.
    >>> outputs = model.generate(**inputs, do_sample=True)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    A sequence: 1, 2, 0, 2, 2. 2, 2, 2, 2

    >>> # With eta sampling, the output gets restricted to high-probability tokens. You can see it as a dynamic form of
    >>> # epsilon sampling that adapts its cutoff probability based on the entropy (high entropy = lower cutoff).
    >>> # Pro tip: The paper recomends using `eta_cutoff` values between 3e-4 to 4e-3
    >>> outputs = model.generate(**inputs, do_sample=True, eta_cutoff=0.1)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
    ```
    r_   rW   r   c                 C   s`   t |}|dks|dkr&td| t|}|dk rDtd| t|| _|| _|| _d S )Nr   rW   z3`eta_cutoff` has to be a float > 0 and < 1, but is r   )rF   r2   r9   r    tensorr   ra   rb   r   r   r   r   rA   `  s    zEtaLogitsWarper.__init__r   c                 C   s   |j dd}tjj|d }t| jt| jt|  d }||k }t| j	|
d}||t||d d k @ }||| j}|S )NrC   re   )logits).Nr   ru   )rh   r    distributionsZCategoricalentropyrv   r   sqrtr~   rb   rw   rx   rk   ra   )r   r   r   r   r   etarp   rs   r   r   r   r   o  s    &zEtaLogitsWarper.__call__Nrq   r   r   r   r   r   0  s   /r   )
ngram_sizeprev_input_ids	num_hyposc                    s   dd t |D }t |D ]b}||   || }t fddt | D  D ].}t|dd }||g |d g ||< qLq|S )ap  
    Assume ngram_size=2 and prev_input_ids=tensor([[40, 2883, 2712, 4346]]). The output of generated ngrams look like
    this {(40,): [2883], (2883,): [2712], (2712,): [4346]}.

    Args:
        ngram_size (`int`):
            The number sequential tokens taken as a group which may only occur once before being banned.
        prev_input_ids (`torch.Tensor`):
           Generated token ids for the current hypothesis.
        num_hypos (`int`):
            The number of hypotheses for which n-grams need to be generated.

    Returns:
        generated_ngrams (`dict`):
            Dictionary of generated ngrams.
    c                 S   s   g | ]}i qS r   r   r&   _r   r   r   
<listcomp>  s     z_get_ngrams.<locals>.<listcomp>c                    s   g | ]} |d  qS r%   r   r:   Z
gen_tokensr   r   r     s     NrC   )rangetolistziptupleget)r   r   r   generated_ngramsidxZgenerated_ngramZngramZprev_ngram_tupler   r   r   _get_ngrams  s    r   c                 C   s,   |d | }t |||  }| |g S )a  
    Determines the banned tokens for the current hypothesis based on previously generated n-grams.

    Args:
        banned_ngrams (`dict`):
            A dictionary containing previously generated n-grams for each hypothesis.
        prev_input_ids (`torch.Tensor`):
            Generated token ids for the current hypothesis.
        ngram_size (`int`):
            The number sequential tokens taken as a group which may only occur once before being banned.
        cur_len (`int`):
            The current length of the token sequences for which the n-grams are being checked.

    Returns:
        List of tokens that are banned.
    rW   )r   r   r   )Zbanned_ngramsr   r   rG   Z	start_idxZ	ngram_idxr   r   r   _get_generated_ngrams  s    r   )r   r   r   rG   r   c                    sJ    d k rdd t |D S t| fddt |D }|S )z6Copied from fairseq for no_repeat_ngram in beam_searchrW   c                 S   s   g | ]}g qS r   r   r   r   r   r   r     s     z-_calc_banned_ngram_tokens.<locals>.<listcomp>c                    s"   g | ]}t | |  qS r   )r   r&   Zhypo_idxrG   r   r   r   r   r   r     s   )r   r   )r   r   r   rG   banned_tokensr   r   r   _calc_banned_ngram_tokens  s    r   c                   @   s>   e Zd ZdZedddZeeej	ej
ej
dddZdS )	NoRepeatNGramLogitsProcessoruP  
    N-grams are groups of "n" consecutive words, characters, or tokens taken from a sequence of text. Given the
    sentence: "She runs fast", the bi-grams (n=2) would be ("she", "runs") and ("runs", "fast"). In text generation,
    avoiding repetitions of word sequences provides a more diverse output. This [`LogitsProcessor`] enforces no
    repetition of n-grams by setting the scores of banned tokens to negative infinity which eliminates those tokens
    from consideration when further processing the scores.
    [Fairseq](https://github.com/pytorch/fairseq/blob/a07cb6f40480928c9e0548b737aadd36ee66ac76/fairseq/sequence_generator.py#L345).

    <Tip>

    Use n-gram penalties with care. For instance, penalizing 2-grams (bigrams) in an article about the city of New York
    might lead to undesirable outcomes where the city's name appears only once in the entire text.
    [Reference](https://huggingface.co/blog/how-to-generate)

    </Tip>

    Args:
        ngram_size (`int`):
            All ngrams of size `ngram_size` can only occur once.

    Examples:

    ```py
    >>> from transformers import AutoTokenizer, AutoModelForCausalLM

    >>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
    >>> inputs = tokenizer(["Today I"], return_tensors="pt")

    >>> output = model.generate(**inputs)
    >>> print(tokenizer.decode(output[0], skip_special_tokens=True))
    Today I’m not sure if I’m going to be able to do it.

    >>> # Now let's add ngram size using `no_repeat_ngram_size`. This stops the repetitions ("I’m") in the output.
    >>> output = model.generate(**inputs, no_repeat_ngram_size=2)
    >>> print(tokenizer.decode(output[0], skip_special_tokens=True))
    Today I’m not sure if I can get a better understanding of the nature of this issue
    ```
    )r   c                 C   s*   t |tr|dkr td| || _d S )Nr   z;`ngram_size` has to be a strictly positive integer, but is )r8   r9   r2   r   )r   r   r   r   r   rA     s    z%NoRepeatNGramLogitsProcessor.__init__r   c                 C   sL   |j d }|j d }t| j|||}t|D ]\}}td |||f< q,|S )Nr   rC   rD   )rE   r   r   	enumeraterF   )r   r   r   Znum_batch_hypothesesrG   banned_batch_tokensr;   r   r   r   r   r     s    

z%NoRepeatNGramLogitsProcessor.__call__Nr   r   r   r   r9   rA   r
   r   r    r!   r"   r   r   r   r   r   r     s   (r   c                   @   sB   e Zd ZdZeejdddZee	ejej
ej
dddZdS )	#EncoderNoRepeatNGramLogitsProcessora  
    [`LogitsProcessor`] that enforces no repetition of encoder input ids n-grams for the decoder ids. See
    [ParlAI](https://github.com/facebookresearch/ParlAI/blob/master/parlai/core/torch_generator_agent.py#L1350).

    Args:
        encoder_ngram_size (`int`):
            All ngrams of size `ngram_size` can only occur within the encoder input ids.
        encoder_input_ids (`int`):
            The encoder_input_ids that should not be repeated within the decoder ids.
    )encoder_ngram_sizer]   c                 C   s^   t |tr|dkr td| || _t|jdkr>|d}|jd | _t||| j| _	d S )Nr   zC`encoder_ngram_size` has to be a strictly positive integer, but is rW   )
r8   r9   r2   r   r.   rE   Z	unsqueeze
batch_sizer   r   )r   r   r]   r   r   r   rA     s    
z,EncoderNoRepeatNGramLogitsProcessor.__init__r   c                    sb   |j d }|j j d   fddt|D }t|D ]\}}td |||f< qB|S )Nr   rC   c                    s*   g | ]"}t j|  | j qS r   )r   r   r   r   rG   r   	num_beamsr   r   r   r     s      z@EncoderNoRepeatNGramLogitsProcessor.__call__.<locals>.<listcomp>rD   )rE   r   r   r   rF   )r   r   r   r   r   r;   r   r   r   r   r     s    


z,EncoderNoRepeatNGramLogitsProcessor.__call__N)r   r   r   r   r9   r    r!   rA   r
   r   r"   r   r   r   r   r   r     s   r   c                   @   sb   e Zd ZdZeee ef dddZe	e
ejejejdddZejdd	d
Zdd ZdS )SequenceBiasLogitsProcessora-  
    [`LogitsProcessor`] that applies an additive bias on sequences. The bias is applied to the last token of a sequence
    when the next generated token can complete it. Consequently, to take the most of biasing sequences with more than
    one token, consider using beam methods (to gracefully work around partially completed sequences that have a
    negative bias) and applying the bias to their prefixes (to ensure the bias is applied earlier).

    <Tip>

    In order to get the token ids of the sequences that you want to bias, make sure to set `add_prefix_space=True` when
    initializing the tokenizer, and use `tokenizer(bad_words, add_special_tokens=False).input_ids`. The
    `add_prefix_space` argument is only supported for some slow tokenizers, as fast tokenizers' prefixing behaviours
    come from `pre tokenizers`. Read more [here](https://huggingface.co/docs/tokenizers/api/pre-tokenizers).

    </Tip>

    Args:
        sequence_bias (`Dict[Tuple[int], float]`):
            Dictionary that maps a sequence of tokens to its bias term. Positive biases increase the odds of the
            sequence being selected, while negative biases do the opposite. If a sequence has a length of 1, its bias
            will always be applied. Otherwise, the bias will only be applied if the sequence in question is about to be
            completed (in the token selection step after this processor is applied).

    Examples:

    ```python
    >>> from transformers import AutoTokenizer, AutoModelForCausalLM

    >>> model = AutoModelForCausalLM.from_pretrained("gpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
    >>> inputs = tokenizer(["The full name of Donald is Donald"], return_tensors="pt")

    >>> summary_ids = model.generate(inputs["input_ids"], max_new_tokens=4)
    >>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True)[0])
    The full name of Donald is Donald J. Trump Jr

    >>> # Now let's control generation through a bias. Please note that the tokenizer is initialized differently!
    >>> tokenizer_with_prefix_space = AutoTokenizer.from_pretrained("gpt2", add_prefix_space=True)


    >>> def get_tokens_as_tuple(word):
    ...     return tuple(tokenizer_with_prefix_space([word], add_special_tokens=False).input_ids[0])


    >>> # If we add a negative bias without beam search, it may become "stuck" in a prefix without good continuations
    >>> sequence_bias = {get_tokens_as_tuple("Trump"): -10.0}
    >>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, sequence_bias=sequence_bias)
    >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
    The full name of Donald is Donald J. Donald,

    >>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, num_beams=4, sequence_bias=sequence_bias)
    >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
    The full name of Donald is Donald Rumsfeld,

    >>> # We can also add a positive bias to nudge the model towards specific tokens or continuations
    >>> sequence_bias = {get_tokens_as_tuple("Donald Duck"): 10.0}
    >>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, num_beams=4, sequence_bias=sequence_bias)
    >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
    The full name of Donald is Donald Duck.
    ```
    sequence_biasc                 C   s   || _ |   d | _d| _d S )NF)r   _validate_argumentslength_1_biasprepared_bias_variablesr   r   r   r   r   rA   c  s    z$SequenceBiasLogitsProcessor.__init__r   c           	      C   s   | j s| | t|}|| j7 }| j D ]\}}t|dkrDq.t||jd krXq.t|d }|d }t	|d d | d f tj
|d d |j|jdjdd}|d d |f  t| tj
||jdtj
d|jd7  < q.|| }|S )NrW   rC   )dtypedevicere   )r   rQ   )r   _prepare_bias_variablesr    Z
zeros_liker   r   itemsr.   rE   eqr   r   r   prodrY   bool)	r   r   r   biassequence_idsr   Zprefix_lengthZ
last_tokenZmatching_rowsr   r   r   r   l  s0    



z$SequenceBiasLogitsProcessor.__call__)r   c                 C   s   |j d }g }| jD ] }|D ]}||kr|| qqt|dkrVtd| d| tj|ftjd|j	| _
| j D ]"\}}t|dkr||| j
|d < q|d| _d S )NrC   r   zThe model vocabulary size is z., but the following tokens were being biased: r   rW   T)rE   r   appendr.   r2   r    ZzerosrF   tor   r   r   r   )r   r   Zvocabulary_sizeZinvalid_biasesr   token_idr   r   r   r   r     s    

z3SequenceBiasLogitsProcessor._prepare_bias_variablesc                 C   s   | j }t|trt|dkr,td| dtdd | D rRtd| dtdd | D rxtd| dtd	d | D rtd
| dd S )Nr   z9`sequence_bias` has to be a non-empty dictionary, but is .c                 s   s   | ]}t |t V  qd S r%   )r8   r   r&   r   r   r   r   r*     s     zBSequenceBiasLogitsProcessor._validate_arguments.<locals>.<genexpr>z=`sequence_bias` has to be a dict with tuples as keys, but is c                 s   s,   | ]$}t d d |D p"t|dkV  qdS )c                 s   s(   | ] }t |ttjf p|d k V  qdS r<   r8   r9   npintegerr&   r   r   r   r   r*     s     zLSequenceBiasLogitsProcessor._validate_arguments.<locals>.<genexpr>.<genexpr>r   N)r>   r.   r   r   r   r   r*     s   zUEach key in `sequence_bias` has to be a non-empty tuple of positive integers, but is c                 s   s   | ]}t |t V  qd S r%   )r8   rF   )r&   r   r   r   r   r*     s     z?`sequence_bias` has to be a dict with floats as values, but is )r   r8   dictr.   r2   r>   r1   valuesr   r   r   r   r     s    
z/SequenceBiasLogitsProcessor._validate_argumentsN)r   r   r   r   r   r   r9   rF   rA   r
   r   r    r!   r"   r   r   r   r   r   r   r   r   %  s   =	!r   c                       sD   e Zd ZdZeee  eeee f d fddZdd Z  Z	S )NoBadWordsLogitsProcessora)	  
    [`LogitsProcessor`] that enforces that specified sequences will never be selected.

    <Tip>

    In order to get the token ids of the words that should not appear in the generated text, make sure to set
    `add_prefix_space=True` when initializing the tokenizer, and use `tokenizer(bad_words,
    add_special_tokens=False).input_ids`. The `add_prefix_space` argument is only supported for some slow tokenizers,
    as fast tokenizers' prefixing behaviours come from `pre tokenizers`. Read more
    [here](https://huggingface.co/docs/tokenizers/api/pre-tokenizers).

    </Tip>

    Args:
        bad_words_ids (`List[List[int]]`):
            List of list of token ids that are not allowed to be generated.
        eos_token_id (`Union[int, List[int]]`):
            The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.

    Examples:

    ```python
    >>> from transformers import AutoTokenizer, AutoModelForCausalLM

    >>> model = AutoModelForCausalLM.from_pretrained("gpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
    >>> inputs = tokenizer(["In a word, the cake is a"], return_tensors="pt")

    >>> output_ids = model.generate(inputs["input_ids"], max_new_tokens=5, pad_token_id=tokenizer.eos_token_id)
    >>> print(tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0])
    In a word, the cake is a bit of a mess.

    >>> # Now let's take the bad words out. Please note that the tokenizer is initialized differently
    >>> tokenizer_with_prefix_space = AutoTokenizer.from_pretrained("gpt2", add_prefix_space=True)


    >>> def get_tokens_as_list(word_list):
    ...     "Converts a sequence of words into a list of tokens"
    ...     tokens_list = []
    ...     for word in word_list:
    ...         tokenized_word = tokenizer_with_prefix_space([word], add_special_tokens=False).input_ids[0]
    ...         tokens_list.append(tokenized_word)
    ...     return tokens_list


    >>> bad_words_ids = get_tokens_as_list(word_list=["mess"])
    >>> output_ids = model.generate(
    ...     inputs["input_ids"], max_new_tokens=5, bad_words_ids=bad_words_ids, pad_token_id=tokenizer.eos_token_id
    ... )
    >>> print(tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0])
    In a word, the cake is a bit of a surprise.
    ```
    )bad_words_idsr6   c                    s`   || _ |    d krg  t tr* g tt fdd|}dd |D }t j|d d S )Nc                    s   t  fddD S )Nc                 3   s   | ]} |gkV  qd S r%   r   r:   Zbad_token_seqr   r   r*     s     zGNoBadWordsLogitsProcessor.__init__.<locals>.<lambda>.<locals>.<genexpr>)r/   r   r6   r   r   <lambda>      z4NoBadWordsLogitsProcessor.__init__.<locals>.<lambda>c                 S   s   i | ]}t |td qS )z-inf)r   rF   )r&   sequencer   r   r   
<dictcomp>  s      z6NoBadWordsLogitsProcessor.__init__.<locals>.<dictcomp>r   )bad_word_idsr   r8   r9   r0   filtersuperrA   )r   r   r6   r   r   r   r   rA     s    
z"NoBadWordsLogitsProcessor.__init__c                 C   st   | j }t|trt|dkr,td| dtdd |D rNtd| dtdd |D rptd| dd S )	Nr   z3`bad_words_ids` has to be a non-empty list, but is r   c                 s   s   | ]}t |t V  qd S r%   )r8   r0   r&   r   r   r   r   r*     s     z@NoBadWordsLogitsProcessor._validate_arguments.<locals>.<genexpr>z2`bad_words_ids` has to be a list of lists, but is c                 s   s    | ]}t d d |D V  qdS )c                 s   s(   | ] }t |ttjf p|d k V  qdS r<   r   r   r   r   r   r*     s     zJNoBadWordsLogitsProcessor._validate_arguments.<locals>.<genexpr>.<genexpr>N)r>   r   r   r   r   r*     s   zKEach list in `bad_words_ids` has to be a list of positive integers, but is )r   r8   r0   r.   r2   r>   )r   r   r   r   r   r     s    
z-NoBadWordsLogitsProcessor._validate_arguments)
r   r   r   r   r   r9   r   rA   r   __classcell__r   r   r   r   r     s   6(r   c                   @   sR   e Zd ZdZeeejgee f edddZ	e
eejejejdddZdS )	 PrefixConstrainedLogitsProcessora  
    [`LogitsProcessor`] that enforces constrained generation and is useful for prefix-conditioned constrained
    generation. See [Autoregressive Entity Retrieval](https://arxiv.org/abs/2010.00904) for more information.

    Args:
        prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], List[int]]`):
            This function constraints the beam search to allowed tokens only at each step. This function takes 2
            arguments `inputs_ids` and the batch ID `batch_id`. It has to return a list with the allowed tokens for the
            next generation step conditioned on the previously generated tokens `inputs_ids` and the batch ID
            `batch_id`.
    )prefix_allowed_tokens_fnr   c                 C   s   || _ || _d S r%   )_prefix_allowed_tokens_fn
_num_beams)r   r   r   r   r   r   rA     s    z)PrefixConstrainedLogitsProcessor.__init__r   c              	   C   sl   t |tj }t|d| j|jd D ]8\}}t|D ]&\}}d||| j | | ||f< q:q*|| S )NrC   r   )	r    Z	full_likemathrD   r   r   r   rE   r   )r   r   r   maskZbatch_idZ	beam_sentZbeam_idsentr   r   r   r   !  s
    ""z)PrefixConstrainedLogitsProcessor.__call__N)r   r   r   r   r   r9   r    Tensorr   rA   r
   r   r!   r"   r   r   r   r   r   r     s   "r   c                   @   s@   e Zd ZdZeeedddZejej	ejeej	dddZ
dS )	HammingDiversityLogitsProcessora  
    [`LogitsProcessor`] that enforces diverse beam search.

    Note that this logits processor is only effective for [`PreTrainedModel.group_beam_search`]. See [Diverse Beam
    Search: Decoding Diverse Solutions from Neural Sequence Models](https://arxiv.org/pdf/1610.02424.pdf) for more
    details.

    <Tip>

    Diverse beam search can be particularly useful in scenarios where a variety of different outputs is desired, rather
    than multiple similar sequences. It allows the model to explore different generation paths and provides a broader
    coverage of possible outputs.

    </Tip>

    <Tip warning={true}>

    This logits processor can be resource-intensive, especially when using large models or long sequences.

    </Tip>

    Traditional beam search often generates very similar sequences across different beams.
    `HammingDiversityLogitsProcessor` addresses this by penalizing beams that generate tokens already chosen by other
    beams in the same time step.

    How It Works:
    - **Grouping Beams**: Beams are divided into groups. Each group selects tokens independently of the others.
    - **Penalizing Repeated Tokens**: If a beam in a group selects a token already chosen by another group in the
        same step, a penalty is applied to that token's score.
    - **Promoting Diversity**: This penalty discourages beams within a group from selecting the same tokens as
        beams in other groups.

    Benefits:
    - **Diverse Outputs**: Produces a variety of different sequences.
    - **Exploration**: Allows the model to explore different paths.

    Args:
        diversity_penalty (`float`):
            This value is subtracted from a beam's score if it generates a token same as any beam from other group at a
            particular time. Note that `diversity_penalty` is only effective if group beam search is enabled. The
            penalty applied to a beam's score when it generates a token that has already been chosen by another beam
            within the same group during the same time step. A higher `diversity_penalty` will enforce greater
            diversity among the beams, making it less likely for multiple beams to choose the same token. Conversely, a
            lower penalty will allow beams to more freely choose similar tokens. Adjusting this value can help strike a
            balance between diversity and natural likelihood.
        num_beams (`int`):
            Number of beams used for group beam search. Beam search is a method used that maintains beams (or "multiple
            hypotheses") at each step, expanding each one and keeping the top-scoring sequences. A higher `num_beams`
            will explore more potential sequences. This can increase chances of finding a high-quality output but also
            increases computational cost.
        num_beam_groups (`int`):
            Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams.
            Each group of beams will operate independently, selecting tokens without considering the choices of other
            groups. This division promotes diversity by ensuring that beams within different groups explore different
            paths. For instance, if `num_beams` is 6 and `num_beam_groups` is 2, there will be 2 groups each containing
            3 beams. The choice of `num_beam_groups` should be made considering the desired level of output diversity
            and the total number of beams. See [this paper](https://arxiv.org/pdf/1610.02424.pdf) for more details.

    Examples:

    ```python
    >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
    >>> import torch

    >>> # Initialize the model and tokenizer
    >>> tokenizer = AutoTokenizer.from_pretrained("t5-base")
    >>> model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")

    >>> # A long text about the solar system
    >>> text = "The Solar System is a gravitationally bound system comprising the Sun and the objects that orbit it, either directly or indirectly. Of the objects that orbit the Sun directly, the largest are the eight planets, with the remainder being smaller objects, such as the five dwarf planets and small Solar System bodies. The Solar System formed 4.6 billion years ago from the gravitational collapse of a giant interstellar molecular cloud."
    >>> inputs = tokenizer("summarize: " + text, return_tensors="pt")

    >>> # Generate diverse summary
    >>> outputs_diverse = model.generate(
    ...     **inputs,
    ...     num_beam_groups=2,
    ...     diversity_penalty=10.0,
    ...     max_length=100,
    ...     num_beams=4,
    ...     num_return_sequences=2,
    ... )
    >>> summaries_diverse = tokenizer.batch_decode(outputs_diverse, skip_special_tokens=True)

    >>> # Generate non-diverse summary
    >>> outputs_non_diverse = model.generate(
    ...     **inputs,
    ...     max_length=100,
    ...     num_beams=4,
    ...     num_return_sequences=2,
    ... )
    >>> summary_non_diverse = tokenizer.batch_decode(outputs_non_diverse, skip_special_tokens=True)

    >>> # With `diversity_penalty`, the resulting beams are much more diverse
    >>> print(summary_non_diverse)
    ['the solar system formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets.',
    'the Solar System formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets.']

    >>> print(summaries_diverse)
    ['the solar system formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets.',
    'the solar system formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets. the rest of the objects are smaller objects, such as the five dwarf planets and small solar system bodies.']
    ```
    )diversity_penaltyr   num_beam_groupsc                 C   sx   t |tr|dkstd|| _t |tr2|dk r:td|| _t |trR|dk rZtd||krjtd|| | _d S )NrQ   z=`diversity_penalty` should be a float strictly larger than 0.r	   z8`num_beams` should be an integer strictly larger than 1.z>`num_beam_groups` should be an integer strictly larger than 1.z8`beam_groups` has to be smaller or equal to `num_beams`.)r8   rF   r2   _diversity_penaltyr9   r   _num_sub_beams)r   r   r   r   r   r   r   rA     s    z(HammingDiversityLogitsProcessor.__init__)r   r   current_tokensbeam_group_idxr   c                 C   s   |j d | j }|| j }t|| j | j}|| }|j d }	|dkrJ|S t|D ]\}
||
| j |
| j |  }tj||	d|j}||
| |
d |   | j	| 8  < qR|S )a  
        Args:
            input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
                Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
            scores (`torch.FloatTensor` 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
            current_tokens (`torch.LongTensor` of shape `(batch_size)`):
                Indices of input sequence tokens in the vocabulary, corresponding to the tokens selected by the other
                beam groups in the current generation step.
            beam_group_idx (`int`):
                The index of the beam group currently being processed.

        Return:
            `torch.FloatTensor` of shape `(batch_size, config.vocab_size)`:
                The processed prediction scores.
        r   rC   )Z	minlengthrW   )
rE   r   r   rv   r   r    Zbincountr   r   r   )r   r   r   r   r   r   Zgroup_start_idxZgroup_end_idxZ
group_sizeZ
vocab_sizeZ	batch_idxZprevious_group_tokensZtoken_frequencyr   r   r   r     s    

 (z(HammingDiversityLogitsProcessor.__call__N)r   r   r   r   rF   r9   rA   r    r!   r"   r   r   r   r   r   r   +  s   gr   c                   @   s>   e Zd ZdZedddZeeej	ej
ej
dddZdS )	ForcedBOSTokenLogitsProcessorz
    [`LogitsProcessor`] 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%   r   )r   r   r   r   r   rA     s    z&ForcedBOSTokenLogitsProcessor.__init__r   c                    sZ   |j d }|dkrV|j d }td |d d  fddt|D f< d|d d  jf< |S )NrC   rW   rD   c                    s   g | ]}| j kr|qS r   r   r:   r   r   r   r     s     
 z:ForcedBOSTokenLogitsProcessor.__call__.<locals>.<listcomp>r   )rE   rF   r   r   )r   r   r   rG   
num_tokensr   r   r   r     s    

(z&ForcedBOSTokenLogitsProcessor.__call__Nr   r   r   r   r   r     s   r   c                   @   sL   e Zd ZdZeeeee f dddZee	e
je
je
jdddZdS )	ForcedEOSTokenLogitsProcessora  
    [`LogitsProcessor`] 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 (`Union[int, List[int]]`):
            The id of the token to force as the last generated token when `max_length` is reached. Optionally, use a
            list to set multiple *end-of-sequence* tokens.
    )
max_lengthr6   c                 C   s    || _ t|tr|g}|| _d S r%   )r   r8   r9   r6   )r   r   r6   r   r   r   rA     s    
z&ForcedEOSTokenLogitsProcessor.__init__r   c                    sj   |j d }| jd krf|j d }td |d d  fddt|D f<  jD ]}d|d d |f< qP|S )NrC   rW   rD   c                    s   g | ]}| j kr|qS r   r   r:   r   r   r   r     s     
 z:ForcedEOSTokenLogitsProcessor.__call__.<locals>.<listcomp>r   )rE   r   rF   r   r6   )r   r   r   rG   r   r;   r   r   r   r     s    

(
z&ForcedEOSTokenLogitsProcessor.__call__NrH   r   r   r   r   r     s   r   c                   @   s0   e Zd ZdZeeejejejdddZ	dS )InfNanRemoveLogitsProcessorz
    [`LogitsProcessor`] that removes all `nan` and `inf` values to avoid the generation method to fail. Note that using
    the logits processor should only be used if necessary since it can slow down the generation method.
    r   c                 C   s*   d|||k< t |jj||tdk< |S )NrQ   rD   )r    Zfinfor   rt   rF   r   r   r   r   r     s    z$InfNanRemoveLogitsProcessor.__call__Nr   r   r   r   r   r     s   r   c                   @   sV   e Zd ZdZeeef eeee f edddZ	e
eejejejdddZdS )	ExponentialDecayLengthPenaltya?  
    [`LogitsProcessor`] that exponentially increases the score of the `eos_token_id` after `start_index` has been
    reached. This allows generating shorter sequences without having a hard cutoff, allowing the `eos_token` to be
    predicted in a meaningful position.

    Args:
        exponential_decay_length_penalty (`tuple(int, float)`):
            This tuple shall consist of: `(start_index, decay_factor)` where `start_index` indicates where penalty
            starts and `decay_factor` represents the factor of exponential decay
        eos_token_id (`Union[int, List[int]]`):
            The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.
        input_ids_seq_length (`int`):
            The length of the input sequence.

    Examples:

    ```python
    >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed

    >>> set_seed(1)
    >>> model = AutoModelForCausalLM.from_pretrained("gpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("gpt2")

    >>> text = "Just wanted to let you know, I"
    >>> inputs = tokenizer(text, return_tensors="pt")

    >>> # Generate sequences without exponential penalty. We want short sentences, so we limit max_length=30
    >>> # see that the answer tends to end abruptly
    >>> outputs = model.generate(**inputs, do_sample=True, temperature=0.9, max_length=30, pad_token_id=50256)
    >>> print(tokenizer.batch_decode(outputs)[0])
    Just wanted to let you know, I'm not even a lawyer. I'm a man. I have no real knowledge of politics. I'm a

    >>> # Generate sequences with exponential penalty, we add the exponential_decay_length_penalty=(start_index, decay_factor)
    >>> # We see that instead of cutting at max_tokens, the output comes to an end before (at 25 tokens) and with more meaning
    >>> # What happens is that starting from `start_index` the EOS token score will be increased by decay_factor exponentially
    >>> outputs = model.generate(
    ...     **inputs,
    ...     do_sample=True,
    ...     temperature=0.9,
    ...     max_length=30,
    ...     pad_token_id=50256,
    ...     exponential_decay_length_penalty=(15, 1.6),
    ... )
    >>> print(tokenizer.batch_decode(outputs)[0])
    Just wanted to let you know, I've got a very cool t-shirt educating people on how to use the Internet<|endoftext|>

    >>> # Generate sequences with smaller decay_factor, still improving the hard cutoff mid-sentence
    >>> outputs = model.generate(
    ...     **inputs,
    ...     do_sample=True,
    ...     temperature=0.9,
    ...     max_length=30,
    ...     pad_token_id=50256,
    ...     exponential_decay_length_penalty=(15, 1.05),
    ... )
    >>> print(tokenizer.batch_decode(outputs)[0])
    Just wanted to let you know, I've been working on it for about 6 months and now it's in Alpha.<|endoftext|>
    ```
    ) exponential_decay_length_penaltyr6   input_ids_seq_lengthc                 C   s2   |d | | _ |d | _t|tr(|g}|| _d S )Nr   rW   )regulation_startregulation_factorr8   r9   r6   )r   r   r6   r   r   r   r   rA   O  s
    

z&ExponentialDecayLengthPenalty.__init__r   c                 C   sp   |j d }|| jkrl| jD ]P}|| j }|d d |f t|d d |f t| j|d   |d d |f< q|S )NrC   rW   )rE   r   r6   r    r   powr   )r   r   r   rG   r;   Zpenalty_idxr   r   r   r   [  s    



Dz&ExponentialDecayLengthPenalty.__call__N)r   r   r   r   r   r9   rF   r   r   rA   r
   r   r    r!   r"   r   r   r   r   r   r     s   >
r   c                   @   s0   e Zd ZdZeeejejejdddZ	dS )LogitNormalizationa  
    [`LogitsWarper`] and [`LogitsProcessor`] for normalizing the scores using log-softmax. It's important to normalize
    the scores during beam search, after applying the logits processors or warpers, since the search algorithm used in
    this library doesn't do it (it only does it before, but they may need re-normalization) but it still supposes that
    the scores are normalized when comparing the hypotheses.
    r   c                 C   s   |j dd}|S )NrC   re   )r}   r   r   r   r   r   n  s    zLogitNormalization.__call__Nr   r   r   r   r   r   f  s   r   c                   @   s8   e Zd ZdZdd Zeeejej	ej	dddZ
dS )$SuppressTokensAtBeginLogitsProcessora  
    [`SuppressTokensAtBeginLogitsProcessor`] supresses 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` at not
    sampled at the begining of the generation.
    c                 C   s   t || _|| _d S r%   )r0   begin_suppress_tokensbegin_index)r   r   r   r   r   r   rA   {  s    
z-SuppressTokensAtBeginLogitsProcessor.__init__r   c                 C   s,   |j d | jkr(td |d d | jf< |S )NrW   rD   )rE   r   rF   r   r   r   r   r   r     s    z-SuppressTokensAtBeginLogitsProcessor.__call__Nr   r   r   r   rA   r
   r   r    r!   r"   r   r   r   r   r   r   t  s   r   c                   @   s8   e Zd ZdZdd Zeeejej	ej	dddZ
dS )SuppressTokensLogitsProcessorzThis processor can be used to suppress a list of tokens. The processor will set their log probs to `-inf` so that they
    are not sampled.c                 C   s   t || _d S r%   )r0   suppress_tokens)r   r   r   r   r   rA     s    z&SuppressTokensLogitsProcessor.__init__r   c                 C   s   t d |d d | jf< |S )NrD   )rF   r   r   r   r   r   r     s    z&SuppressTokensLogitsProcessor.__call__Nr   r   r   r   r   r     s   r   c                   @   sF   e Zd ZdZeee  dddZeee	j
e	je	jdddZdS )	ForceTokensLogitsProcessora  This processor 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 `inf` so that they are
    sampled at their corresponding index.)force_token_mapc                 C   s   t || _d S r%   )r   r   )r   r   r   r   r   rA     s    z#ForceTokensLogitsProcessor.__init__r   c                 C   sN   |j d }| j|d }|d k	rJtd |d d d d f< d|d d |f< |S )NrC   rD   r   )rE   r   r   rF   )r   r   r   Zgeneration_idxcurrent_tokenr   r   r   r     s    
z#ForceTokensLogitsProcessor.__call__N)r   r   r   r   r   r9   rA   r
   r   r    r!   r"   r   r   r   r   r   r     s   r   c                   @   s8   e Zd ZdZdd Zeeejej	ej	dddZ
dS )WhisperTimeStampLogitsProcessora  
    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.

    See [the paper](https://arxiv.org/abs/2212.04356) for more information.

    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   sZ   |j | _ |j| _|jd | _t|jd | _|jd d | jkrN|  jd8  _|j| _d S )NrW   r	   rC   )r6   no_timestamps_token_idtimestamp_beginr.   Zforced_decoder_idsr   max_initial_timestamp_index)r   Zgenerate_configr   r   r   rA     s    z(WhisperTimeStampLogitsProcessor.__init__r   c                 C   s  t d |d d | jf< |jd | jd kr\t d |d d d d f< d|d d | jf< |S t|jd D ]}t||| jd f  }t|dko|d | jk}t|dk p|d | jk}|r|rt d ||| jd f< nt d ||d | j	f< |jd | jkrj| j
d k	rj| j| j
 }t d |d d |d d f< qjtjjj|  dd}t|jd D ]X}||| jd f jdd}	||d | jf  }
|	|
kr`t d ||d | jf< q`|S )NrD   rW   r   rC   r	   re   )rF   r  rE   r   r  r   r0   r   r.   r6   r  r    r{   r|   r}   Z	logsumexprt   )r   r   r   kseqZlast_was_timestampZpenultimate_was_timestampZlast_allowedZlogprobsZtimestamp_logprobZmax_text_token_logprobr   r   r   r     s.     
z(WhisperTimeStampLogitsProcessor.__call__Nr   r   r   r   r   r     s   
r   c                   @   s8   e Zd ZdZdd Zeeejej	ej	dddZ
dS )%ClassifierFreeGuidanceLogitsProcessoraW  Logits processor for classifier free guidance (CFG). The scores are split over the batch dimension,
    where the first half correspond to the conditional logits (predicted from the input prompt) and the second half
    correspond to the unconditional logits (predicted from an empty or 'null' prompt). The processor computes a
    weighted average across the conditional and unconditional logits, parameterised by the `guidance_scale`.

    See [the paper](https://arxiv.org/abs/2306.05284) for more information.

    Args:
        guidance_scale (float):
            The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`.
            Higher guidance scale encourages the model to generate samples that are more closely linked to the input
            prompt, usually at the expense of poorer quality.
    c                 C   s$   |dkr|| _ ntd| dd S )NrW   z\Require guidance scale >1 to use the classifier free guidance processor, got guidance scale r   )guidance_scaler2   )r   r  r   r   r   rA     s
    
z.ClassifierFreeGuidanceLogitsProcessor.__init__r   c                 C   sp   |j d d|j d  kr:td|j d  d|j d  d|j d d }|j|dd\}}||| | j  }|S )Nr   r	   zLogits should have twice the batch size of the input ids, the first half of batches corresponding to the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got batch size z for the logits and z for the input ids.re   )rE   r2   splitr  )r   r   r   Zunguided_bszZcond_logitsZuncond_logitsr   r   r   r     s    z.ClassifierFreeGuidanceLogitsProcessor.__call__Nr   r   r   r   r   r    s   	r  c                   @   s:   e Zd ZdZeeedddZejejejdddZ	dS )	#AlternatingCodebooksLogitsProcessora  
    [`LogitsProcessor`] enforcing alternated generation between the two codebooks of [`Bark`]'s fine submodel.

    Args:
        input_start_len (`int`):
            The length of the initial input sequence.
        semantic_vocab_size (`int`):
            Vocabulary size of the semantic part, i.e number of tokens associated to the semantic vocabulary.
        codebook_size (`int`):
            Number of tokens associated to the codebook.
    )input_start_lensemantic_vocab_sizecodebook_sizec                 C   s6   t |tr|dk r td| || _|| _|| _d S )Nr   zA`input_starting_length` has to be a non-negative integer, but is )r8   r9   r2   r  r  r  )r   r  r  r  r   r   r   rA     s
    z,AlternatingCodebooksLogitsProcessor.__init__r   c                 C   s   |j d }|| j d dk}|r`td |d d d | jf< td |d d | j| j d f< n"td |d d d | j| j f< |S )NrC   r	   r   rD   )rE   r  rF   r  r  )r   r   r   Zcurr_lenZis_first_codebookr   r   r   r   &  s    
$"z,AlternatingCodebooksLogitsProcessor.__call__N)
r   r   r   r   r9   rA   r    r!   r"   r   r   r   r   r   r
    s   r
  c                   @   sF   e Zd ZdZdeeej eej ee dddZ	dd Z
d	d
 ZdS ).UnbatchedClassifierFreeGuidanceLogitsProcessora  Logits processor for Classifier-Free Guidance (CFG). The processors
    computes a weighted average across scores from prompt conditional and prompt unconditional (or negative) logits,
    parameterized by the `guidance_scale`. The unconditional scores are computed internally by prompting `model` with
    the `unconditional_ids` branch.

    See [the paper](https://arxiv.org/abs/2306.17806) for more information.

    Args:
        guidance_scale (`float`):
            The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale != 1`.
            Higher guidance scale encourages the model to generate samples that are more closely linked to the input
            prompt, usually at the expense of poorer quality. A value smaller than 1 has the opposite effect, while
            making the negative prompt provided with negative_prompt_ids (if any) act as a positive prompt.
        unconditional_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of input sequence tokens in the vocabulary for the unconditional branch. If unset, will default to
            the last token of the prompt.
        unconditional_attention_mask (`torch.LongTensor` of shape `(batch_size, sequence_length)`, **optional**):
            Attention mask for unconditional_ids.
        model (`PreTrainedModel`):
            The model computing the unconditional scores. Supposedly the same as the one computing the conditional
            scores. Both models must use the same tokenizer.
        smooth_factor (`float`, **optional**):
            The interpolation weight for CFG Rescale. 1 means no rescaling, 0 reduces to the conditional scores without
            CFG. Turn it lower if the output degenerates.
        use_cache (`bool`, **optional**):
            Whether to cache key/values during the negative prompt forward pass.


    Examples:

    ```python
    >>> from transformers import AutoTokenizer, AutoModelForCausalLM

    >>> model = AutoModelForCausalLM.from_pretrained("gpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
    >>> inputs = tokenizer(["Today, a dragon flew over Paris, France,"], return_tensors="pt")
    >>> out = model.generate(inputs["input_ids"], guidance_scale=1.5)
    >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
    'Today, a dragon flew over Paris, France, killing at least 50 people and injuring more than 100'

    >>> # with a negative prompt
    >>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt")
    >>> out = model.generate(inputs["input_ids"], guidance_scale=2, negative_prompt_ids=neg_inputs["input_ids"])
    >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
    'Today, a dragon flew over Paris, France, killing at least 130 people. French media reported that'

    >>> # with a positive prompt
    >>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt")
    >>> out = model.generate(inputs["input_ids"], guidance_scale=0, negative_prompt_ids=neg_inputs["input_ids"])
    >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
    "Today, a dragon flew over Paris, France, and I'm very happy to be here. I"
    ```
    NT)r  unconditional_idsunconditional_attention_mask	use_cachec                 C   s"   || _ || _|||d dd| _d S )NT)r   attention_maskr  past_key_values
first_pass)r  modelunconditional_context)r   r  r  r  r  r  r   r   r   rA   l  s    z7UnbatchedClassifierFreeGuidanceLogitsProcessor.__init__c                 C   sB  | j d r|| j d d kr2|d d dd f | j d< | j d d kr\tj| j d tjd| j d< | j d }| j d }d| j d< ntj| j d tj|d d dd f tjdgdd}| j d	 stj| j d |d d dd f gdd}n|d d dd f }|| j d< || j d< | j||| j d	 | j d
 d}|d
d | j d
< |jS )Nr  r   rC   r  r   FrW   re   r  r  )r  r  r  )r  r    Z	ones_likelongcatr  r   r   )r   r   r  outr   r   r   get_unconditional_logits~  s<    
 


*

zGUnbatchedClassifierFreeGuidanceLogitsProcessor.get_unconditional_logitsc                 C   s^   t jjj|dd}| jdkr |S | |}t jjj|d d df dd}| j||  | }|S )NrC   re   rW   )r    r{   r|   r}   r  r  )r   r   r   r   Zunconditional_logitsr  r   r   r   r     s    

z7UnbatchedClassifierFreeGuidanceLogitsProcessor.__call__)NNT)r   r   r   r   rF   r   r    r!   r   rA   r  r   r   r   r   r   r  5  s   :   $r  )9r+   r   typingr   r   r   r   r   r   r   numpyr   r    utilsr
   Zutils.loggingr   r   r?   r   r   r#   r0   r$   r4   rI   rN   rS   r\   r^   rr   ry   r   r   r9   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r
  r  r   r   r   r   <module>   sb   $

% HA5?1CO   
9* W $TC($