o
    h(                    @   s  d dl Z d dlZd dlmZmZmZmZmZmZ d dl	Z
d dl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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!G d d! d!eZ"d"e#d#ej$d$e#fd%d&Z%d'd( Z&d"e#d#ej$d$e#d)e#d*eee#  f
d+d,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Z/G d=d> d>eZ0G d?d@ d@eZ1G dAdB dBeZ2G dCdD dDeZ3G dEdF dFeZ4G dGdH dHeZ5G dIdJ dJeZ6G dKdL dLeZ7G dMdN dNeZ8G dOdP dPeZ9G dQdR dReZ:G dSdT dTeZ;G dUdV dVZ<G dWdX dXeZ=dS )Y    N)CallableIterableListOptionalTupleUnion   )isin_mps_friendly)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                   @   4   e Zd ZdZeedejdejdejf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)NzH is an abstract class. Only classes inheriting this class can be called.)NotImplementedError	__class__)selfr   r    r   z/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/generation/logits_process.py__call__0   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                   @   s,   e Zd ZdZdejdejdejfddZdS )LogitsProcessorListz
    This class can be used to create a list of [`LogitsProcessor`] to subsequently process a `scores` input tensor.
    This class inherits from list and adds a specific *__call__* method to apply each [`LogitsProcessor`] to the
    inputs.
    r   r   r   c                    s   | D ]A}t |jj}t|dkr>t fddt| dd D s4tdt|  d|j	 d|||fi  }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    | ]}| v V  qd S Nr   ).0argkwargsr   r   	<genexpr>Q   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%   	processorfunction_argsr   r$   r   r   >   s   &zLogitsProcessorList.__call__N)r   r   r   r   r   r   r   r   r   r   r   r   r    7   s     r    c                   @   \   e Zd ZdZddedeeee ejf de	fddZ
eedejd	ejd
ejfddZdS )MinLengthLogitsProcessora3  
    [`LogitsProcessor`] enforcing a min-length by setting EOS probability to 0. Note that, for decoder-only models
    like most LLMs, the length includes the prompt.

    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], torch.Tensor]`):
            The id(s) of the *end-of-sequence* token.
        device (`str`, *optional*, defaults to `"cpu"`):
            The device to allocate the tensors.

    Examples:

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

    >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")
    >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")

    >>> inputs = tokenizer("A number:", return_tensors="pt")
    >>> gen_out = model.generate(**inputs)
    >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
    A number: one

    >>> # setting `min_length` to a value smaller than the uncontrolled output length has no impact
    >>> gen_out = model.generate(**inputs, min_length=3)
    >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
    A number: one

    >>> # setting a larger `min_length` will force the model to generate beyond its natural ending point, which is not
    >>> # necessarily incorrect
    >>> gen_out = model.generate(**inputs, min_length=10)
    >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
    A number: one thousand, nine hundred and ninety-four
    ```
    cpu
min_lengtheos_token_iddevicec                 C   sZ   t |tr	|dk rtd| t |tjs%t |tr|g}tj||d}|| _|| _d S )Nr   z6`min_length` has to be a non-negative integer, but is r6   )
isinstanceintr.   r   Tensortensorr4   r5   )r   r4   r5   r6   r   r   r   __init__   s   

z!MinLengthLogitsProcessor.__init__r   r   r   c                 C   sP   t j|jd |jd}t|| j}| }|jd | jk r&t |t	j
 |}|S Nr7   )r   arangeshaper6   r	   r5   cloner4   wheremathinf)r   r   r   vocab_tensoreos_token_maskscores_processedr   r   r   r      s   z!MinLengthLogitsProcessor.__call__Nr3   r   r   r   r   r9   r   r   r   r:   strr<   r
   r   r   r   r   r   r   r   r   r2   ]   s
    (&"r2   c                
   @   sb   e Zd ZdZ	ddededeeee ejf de	fddZ
eed	ejd
ejdejfddZdS )!MinNewTokensLengthLogitsProcessora  
    [`LogitsProcessor`] enforcing a min-length of new tokens by setting EOS (End-Of-Sequence) token probability to 0.
    Contrarily to [`MinLengthLogitsProcessor`], this processor ignores the prompt.

    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], torch.Tensor]`):
            The id(s) of the *end-of-sequence* token.
        device (`str`, *optional*, defaults to `"cpu"`):
            The device to allocate the tensors.

    Examples:

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

    >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")
    >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")

    >>> inputs = tokenizer(["A number:"], return_tensors="pt")
    >>> gen_out = model.generate(**inputs)
    >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
    A number: one

    >>> # setting `min_new_tokens` will force the model to generate beyond its natural ending point, which is not
    >>> # necessarily incorrect
    >>> gen_out = model.generate(**inputs, min_new_tokens=2)
    >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
    A number: one thousand
    ```
    r3   prompt_length_to_skipmin_new_tokensr5   r6   c                 C   s   d|fd|ffD ]\}}t |tr|dk rtd| d| qt |tjs5t |tr.|g}tj||d}|| _|| _|| _d S )NrL   rM   r   `z'` has to be a positive integer, but is r7   )	r8   r9   r.   r   r:   r;   rL   rM   r5   )r   rL   rM   r5   r6   arg_name	arg_valuer   r   r   r<      s   

z*MinNewTokensLengthLogitsProcessor.__init__r   r   r   c                 C   sZ   |j d | j }| }tj|j d |jd}t|| j}|| jk r+t	|t
j |}|S r=   )r@   rL   rA   r   r?   r6   r	   r5   rM   rB   rC   rD   )r   r   r   new_tokens_lengthrG   rE   rF   r   r   r   r      s   
z*MinNewTokensLengthLogitsProcessor.__call__NrH   rI   r   r   r   r   rK      s    )
"rK   c                   @   B   e Zd ZdZdefddZeedej	dej
dej
fdd	Zd
S )TemperatureLogitsWarperaa  
    [`LogitsProcessor`] for temperature (exponential scaling output probability distribution), which effectively means
    that it can control the randomness of the predicted tokens. Often used together with [`TopPLogitsWarper`] and
    [`TopKLogitsWarper`].

    <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("openai-community/gpt2")
    >>> model = AutoModelForCausalLM.from_pretrained("openai-community/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 one of these companies that is going to take a',
    "Hugging Face Company is a brand created by Brian A. O'Neil"]

    >>> # 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r|dkr|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   floatr.   rT   )r   rT   
except_msgr   r   r   r<     s   

z TemperatureLogitsWarper.__init__r   r   r   c                 C   s   || j  }|S r!   )rT   r   r   r   rG   r   r   r   r     s   
z TemperatureLogitsWarper.__call__Nr   r   r   r   rV   r<   r
   r   r   r   r   r   r   r   r   r   rS      s
    /"rS   c                   @   rR   ) RepetitionPenaltyLogitsProcessora?  
    [`LogitsProcessor`] that prevents the repetition of previous tokens through a penalty. This penalty is applied at
    most once per token. Note that, for decoder-only models like most LLMs, the considered tokens include the prompt.

    In the original [paper](https://arxiv.org/pdf/1909.05858.pdf), the authors suggest the use of a penalty of around
    1.2 to achieve a good balance between truthful generation and lack of repetition. To penalize and reduce
    repetition, use `penalty` values above 1.0, where a higher value penalizes more strongly. To reward and encourage
    repetition, use `penalty` values between 0.0 and 1.0, where a lower value rewards more strongly.

    Args:
        penalty (`float`):
            The parameter for repetition penalty. 1.0 means no penalty. Above 1.0 penalizes previously generated
            tokens. Between 0.0 and 1.0 rewards previously generated tokens.

    Examples:

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

    >>> # Initializing the model and tokenizer for it
    >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/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   rV   r.   r[   )r   r[   r   r   r   r<   I  s   
z)RepetitionPenaltyLogitsProcessor.__init__r   r   r   c                 C   s>   t |d|}t |dk || j || j }|d||}|S N   r   )r   gatherrB   r[   scatterr   r   r   scorerG   r   r   r   r   O  s   z)RepetitionPenaltyLogitsProcessor.__call__NrY   r   r   r   r   rZ   $  s
    $"rZ   c                   @   H   e Zd ZdZdedejfddZee	dejdej
dej
fd	d
ZdS )'EncoderRepetitionPenaltyLogitsProcessora  
    [`LogitsProcessor`] that works similarly to [`RepetitionPenaltyLogitsProcessor`], but with an *inverse* penalty
    that is applied to the tokens present in the prompt. In other words, a penalty above 1.0 increases the odds of
    selecting tokens that were present in the prompt.

    It was designed to avoid hallucination in input-grounded tasks, like summarization. Although originally intended
    for encoder-decoder models, it can also be used with decoder-only models like LLMs.

    Args:
        penalty (`float`):
            The parameter for repetition penalty. 1.0 means no penalty. Above 1.0 rewards prompt tokens. Between 0.0
            and 1.0 penalizes prompt tokens.
        encoder_input_ids (`torch.LongTensor`):
            The encoder_input_ids that should be repeated within the decoder ids.

    Examples:

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

    >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")
    >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")

    >>> inputs = tokenizer(["Alice and Bob. The third member's name was"], return_tensors="pt")
    >>> gen_out = model.generate(**inputs)
    >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
    Alice and Bob. The third member's name was not mentioned.

    >>> # With the `encoder_repetition_penalty` argument we can trigger this logits processor in `generate`, which can
    >>> # promote the use of prompt tokens ("Bob" in this example)
    >>> gen_out = model.generate(**inputs, encoder_repetition_penalty=1.2)
    >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
    Alice and Bob. The third member's name was Bob. The third member's name was Bob.
    ```
    r[   encoder_input_idsc                 C   s4   t |tr	|dkstd| d| | _|| _d S )Nr   r\   r^   )r8   rV   r.   r[   re   )r   r[   re   r   r   r   r<     s   

z0EncoderRepetitionPenaltyLogitsProcessor.__init__r   r   r   c                 C   sB   t |d| j}t |dk || j || j }|d| j|}|S r]   )r   r_   re   rB   r[   r`   ra   r   r   r   r     s   z0EncoderRepetitionPenaltyLogitsProcessor.__call__N)r   r   r   r   rV   r   r   r<   r
   r   r   r   r   r   r   r   rd   Z  s
    $"rd   c                   @   V   e Zd ZdZed dfdededefddZeed	e	j
d
e	jde	jfddZdS )TopPLogitsWarpera  
    [`LogitsProcessor`] that performs top-p, i.e. restricting to top tokens summing to prob_cut_off <= prob_cut_off.
    Often used together with [`TemperatureLogitsWarper`] and [`TopKLogitsWarper`].

    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 -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(1)
    >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/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, 3 | < 4 (left-hand pointer) ;
    <BLANKLINE>
    <BLANKLINE>

    >>> # 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
    ```
    Infr^   top_pfilter_valuemin_tokens_to_keepc                 C   s\   t |}|dk s|dkrtd| t|tr|dk r#td| || _|| _|| _d S )Nr         ?z.`top_p` has to be a float > 0 and < 1, but is r^   :`min_tokens_to_keep` has to be a positive integer, but is )rV   r.   r8   r9   ri   rj   rk   )r   ri   rj   rk   r   r   r   r<        
zTopPLogitsWarper.__init__r   r   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
descendingr>   dimr^   r   .)	r   sortsoftmaxcumsumri   rk   r`   masked_fillrj   )	r   r   r   sorted_logitssorted_indicescumulative_probssorted_indices_to_removeindices_to_removerG   r   r   r   r     s   zTopPLogitsWarper.__call__Nr   r   r   r   rV   r9   r<   r
   r   r   r   r   r   r   r   r   r   rg     s
    "'"rg   c                   @   sV   e Zd ZdZed dfdededefddZeed	e	j
d
e	jde	jfddZdS )TopKLogitsWarperu  
    [`LogitsProcessor`] that performs top-k, i.e. restricting to the k highest probability elements. Often used
    together with [`TemperatureLogitsWarper`] and [`TopPLogitsWarper`].

    Args:
        top_k (`int`):
            The number of highest probability vocabulary tokens to keep for top-k-filtering.
        filter_value (`float`, *optional*, defaults to -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(1)
    >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")

    >>> inputs = tokenizer("A sequence: A, B, C, D", 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: A, B, C, D, E — S — O, P — R

    >>> # With `top_k` sampling, the output gets restricted the k most likely tokens.
    >>> # Pro tip: In practice, LLMs use `top_k` in the 5-50 range.
    >>> outputs = model.generate(**inputs, do_sample=True, top_k=2)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    A sequence: A, B, C, D, E, F, G, H, I
    ```
    rh   r^   top_krj   rk   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   r.   maxr~   rj   )r   r~   rj   rk   r   r   r   r<     s   
zTopKLogitsWarper.__init__r   r   r   c                 C   s<   t | j|d}|t||d d k }||| j}|S )Nr>   r   .r>   N)minr~   sizer   topkrv   rj   )r   r   r   r~   r{   rG   r   r   r   r      s   zTopKLogitsWarper.__call__Nr|   r   r   r   r   r}     s
    "$"r}   c                   @   sN   e Zd ZdZed dfdededefddZd	ejd
ej	dej	fddZ
dS )MinPLogitsWarpera  
    [`LogitsProcessor`] that performs min-p, i.e. keeps all tokens that are above a minimum probability, scaled by the
    probability of the most likely token. As a result, the filter becomes more agressive in the presence of
    high-probability tokens, which is a sign of a confident output that we shouldn't deviate from.

    Often used together with [`TemperatureLogitsWarper`]. Used as an alternative to [`TopPLogitsWarper`] and
    [`TopKLogitsWarper`].

    Created by @menhguin and @kalomaze (github handles). Code adapted from [this external PR](https://github.com/oobabooga/text-generation-webui/pull/4449/files)

    Args:
        min_p (`float`):
            Minimum token probability, which will be scaled by the probability of the most likely token. It must be a
            value between 0 and 1. Typical values are in the 0.01-0.2 range, comparably selective as setting `top_p` in
            the 0.99-0.8 range (use the opposite of normal `top_p` values).
        filter_value (`float`, *optional*, defaults to -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(1)
    >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/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, 3 | < 4 (left-hand pointer) ;
    <BLANKLINE>
    <BLANKLINE>

    >>> # With `min_p` sampling, the output gets restricted to high-probability tokens.
    >>> # Pro tip: In practice, LLMs use `min_p` in the 0.01-0.2 range.
    >>> outputs = model.generate(**inputs, do_sample=True, min_p=0.1)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
    ```
    rh   r^   min_prj   rk   c                 C   sZ   d|  kr
dksn t d| t|tr|dk r"t d| || _|| _|| _d S )Nr   rl   z9`min_p` has to be a float in the [0, 1] interval, but is r^   rm   )r.   r8   r9   r   rj   rk   )r   r   rj   rk   r   r   r   r<   8  s   
zMinPLogitsWarper.__init__r   r   r   c                 C   s   t j|dd}|jddd\}}| j| }||k }t j|ddd}t j|d|d}	d|	dd | jf< |	d	||	}
||
| j	}|S )
Nr>   rq   Trr   keepdim)rp   rr   )rr   indexF.r^   )
r   rt   r   r   argsortr_   rk   r`   rv   rj   )r   r   r   probs	top_probs_scaled_min_ptokens_to_removerx   rz   r{   rG   r   r   r   r   B  s   
zMinPLogitsWarper.__call__Nr   r   r   r   rV   r9   r<   r   r   r   r   r   r   r   r   r   	  s    ". 
r   c                   @   sX   e Zd ZdZded dfdededefdd	Zeed
e	j
de	jde	jfddZdS )TypicalLogitsWarpera	  
    [`LogitsProcessor`] that performs typical decoding. Inspired on how humans use language, it prioritizes tokens
    whose log probability is close to the entropy of the token probability distribution. This means that the most
    likely tokens may be discarded in the process.

    See [Typical Decoding for Natural Language Generation](https://arxiv.org/abs/2202.00666) for more information.

    Args:
        mass (`float`, *optional*, defaults to 0.9):
            Value of typical_p between 0 and 1 inclusive, defaults to 0.9.
        filter_value (`float`, *optional*, defaults to -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

    >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")
    >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")

    >>> inputs = tokenizer("1, 2, 3", return_tensors="pt")

    >>> # We can see that greedy decoding produces a sequence of numbers
    >>> outputs = model.generate(**inputs)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

    >>> # For this particular seed, we can see that sampling produces nearly the same low-information (= low entropy)
    >>> # sequence
    >>> set_seed(18)
    >>> outputs = model.generate(**inputs, do_sample=True)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    1, 2, 3, 4, 5, 6, 7, 8, 9 and 10

    >>> # With `typical_p` set, the most obvious sequence is no longer produced, which may be good for your problem
    >>> set_seed(18)
    >>> outputs = model.generate(
    ...     **inputs, do_sample=True, typical_p=0.1, return_dict_in_generate=True, output_scores=True
    ... )
    >>> print(tokenizer.batch_decode(outputs.sequences, skip_special_tokens=True)[0])
    1, 2, 3 and 5

    >>> # We can see that the token corresponding to "4" (token 934) in the second position, the most likely token
    >>> # as seen with greedy decoding, was entirely blocked out
    >>> print(outputs.scores[1][0, 934])
    tensor(-inf)
    ```
    g?rh   r^   massrj   rk   c                 C   s\   t |}|dkr|dk std| t|tr|dk r#td| || _|| _|| _d S )Nr   r^   z2`typical_p` has to be a float > 0 and < 1, but is rm   )rV   r.   r8   r9   rj   r   rk   )r   r   rj   rk   r   r   r   r<     rn   zTypicalLogitsWarper.__init__r   r   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}|j|jd d d ||d|ddk}d	|d
d | jf< |d||}||| j}|S )Nr>   rq   T)r   Fro   r^   )r   r   .)r   nn
functionallog_softmaxexpnansumabsrs   r_   rt   ru   r   sumclamp_r@   viewrk   r`   rv   rj   )r   r   r   
normalizedpentshifted_scoressorted_scoresrx   rw   ry   last_indrz   r{   rG   r   r   r   r     s   
zTypicalLogitsWarper.__call__Nr|   r   r   r   r   r   V  s
    $4"r   c                   @   rf   )EpsilonLogitsWarperaL  
    [`LogitsProcessor`] 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 -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(1)
    >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/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, 3 | < 4 (left-hand pointer) ;
    <BLANKLINE>
    <BLANKLINE>

    >>> # 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
    ```
    rh   r^   epsilonrj   rk   c                 C   sZ   t |}|dks|dkrtd| t|}|dk r"td| || _|| _|| _d S )Nr   r^   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 )rV   r.   r9   r   rj   rk   )r   r   rj   rk   r   r   r   r<     s   
zEpsilonLogitsWarper.__init__r   r   r   c                 C   sV   |j dd}|| jk }t| j|d}||t||d d k @ }||| j}|S )Nr>   rq   r   r   )	rt   r   r   rk   r   r   r   rv   rj   )r   r   r   probabilitiesr{   r~   rG   r   r   r   r     s   
zEpsilonLogitsWarper.__call__Nr|   r   r   r   r   r     s
    "'"r   c                	   @   s\   e Zd ZdZed ddfdedededefd	d
Zee	de
jde
jde
jfddZdS )EtaLogitsWarpera  
    [`LogitsProcessor`] 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 `LogitsProcessor` 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 -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`.
        device (`str`, *optional*, defaults to `"cpu"`):
            The device to allocate the tensors.

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

    >>> set_seed(1)
    >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/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, 3 | < 4 (left-hand pointer) ;
    <BLANKLINE>
    <BLANKLINE>

    >>> # 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
    ```
    rh   r^   r3   r   rj   rk   r6   c                 C   sd   t |}|dks|dkrtd| t|}|dk r"td| tj||d| _|| _|| _d S )Nr   r^   z3`eta_cutoff` has to be a float > 0 and < 1, but is r   r7   )rV   r.   r9   r   r;   r   rj   rk   )r   r   rj   rk   r6   r   r   r   r<   '  s   
zEtaLogitsWarper.__init__r   r   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 )Nr>   rq   )logits).Nr   r   )rt   r   distributionsCategoricalentropyr   r   sqrtr   rk   r   r   rv   rj   )	r   r   r   r   r   etar{   r~   rG   r   r   r   r   8  s   &zEtaLogitsWarper.__call__N)r   r   r   r   rV   r9   rJ   r<   r
   r   r   r   r   r   r   r   r   r   r     s    4
"r   
ngram_sizeprev_input_ids	num_hyposc                    s   dd t |D }t |D ]1}||   || }t fddt | D  D ]}t|dd }||g |d g ||< q&q|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   r   
<listcomp>Y      z_get_ngrams.<locals>.<listcomp>c                    s   g | ]} |d  qS r!   r   r"   i
gen_tokensr   r   r   ^  s    Nr>   )rangetolistziptupleget)r   r   r   generated_ngramsidxgenerated_ngramngramprev_ngram_tupler   r   r   _get_ngramsG  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.
    r^   )r   r   r   )banned_ngramsr   r   cur_len	start_idx	ngram_idxr   r   r   _get_generated_ngramsd  s   r   r   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_searchr^   c                 S   s   g | ]}g qS r   r   r   r   r   r   r     r   z-_calc_banned_ngram_tokens.<locals>.<listcomp>c                    s"   g | ]}t | |  qS r   )r   r"   hypo_idxr   r   r   r   r   r   r     s    )r   r   )r   r   r   r   banned_tokensr   r   r   _calc_banned_ngram_tokens{  s   r   c                   @   rR   )NoRepeatNGramLogitsProcessoru  
    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. Note that, for decoder-only models like most LLMs, the
    prompt is also considered to obtain the n-grams.
    [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("distilbert/distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/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   r.   r   )r   r   r   r   r   r<     s   
z%NoRepeatNGramLogitsProcessor.__init__r   r   r   c           	      C   sT   |j d }|j d }| }t| j|||}t|D ]\}}td |||f< q|S )Nr   r>   rD   )r@   rA   r   r   	enumeraterV   )	r   r   r   num_batch_hypothesesr   rG   banned_batch_tokensr   r   r   r   r   r     s   

z%NoRepeatNGramLogitsProcessor.__call__Nr   r   r   r   r9   r<   r
   r   r   r   r   r   r   r   r   r   r     s
    )"r   c                   @   rc   )#EncoderNoRepeatNGramLogitsProcessora  
    [`LogitsProcessor`] that works similarly to [`NoRepeatNGramLogitsProcessor`], but applied exclusively to prevent
    the repetition of n-grams present in the prompt.

    It was designed to promote chattiness in a language model, by preventing the generation of n-grams present in
    previous conversation rounds.

    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.

    Examples:

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

    >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")
    >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")

    >>> inputs = tokenizer("Alice: I love cats. What do you love?\nBob:", return_tensors="pt")

    >>> # With greedy decoding, we see Bob repeating Alice's opinion. If Bob was a chatbot, it would be a poor one.
    >>> outputs = model.generate(**inputs)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    Alice: I love cats. What do you love?
    Bob: I love cats. What do you

    >>> # With this logits processor, we can prevent Bob from repeating Alice's opinion.
    >>> outputs = model.generate(**inputs, encoder_no_repeat_ngram_size=2)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    Alice: I love cats. What do you love?
    Bob: My cats are very cute.
    ```
    encoder_ngram_sizere   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 r^   )
r8   r9   r.   r   r*   r@   	unsqueeze
batch_sizer   r   )r   r   re   r   r   r   r<     s   
z,EncoderNoRepeatNGramLogitsProcessor.__init__r   r   r   c                    sj   |j d }|j j d  | } fddt|D }t|D ]\}}td |||f< q%|S )Nr   r>   c                    s*   g | ]}t j|  | j qS r   )r   r   r   r   r   r   	num_beamsr   r   r   r     s    z@EncoderNoRepeatNGramLogitsProcessor.__call__.<locals>.<listcomp>rD   )r@   r   rA   r   r   rV   )r   r   r   r   rG   r   r   r   r   r   r   r     s   


z,EncoderNoRepeatNGramLogitsProcessor.__call__N)r   r   r   r   r9   r   r   r<   r
   r   r   r   r   r   r   r   r     s
    %"r   c                   @   sv   e Zd ZdZdeeeee ef   fddZe	e
dejdejdejfdd	Zdejfd
dZdd Zdd ZdS )SequenceBiasLogitsProcessoraG  
    [`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>

    At a token-level, biasing a word is different from biasing a word with a space before it. If you want to bias
    "foo" mid-sentence, you'll likely want to add a prefix space and bias " foo" instead. Check the tokenizer section
    of our NLP course to find out why: https://huggingface.co/learn/nlp-course/chapter2/4?fw=pt

    </Tip>

    Args:
        sequence_bias (`List[List[Union[List[int], float]]]`):
            List of lists that maps a sequence of tokens to its bias term (e.g. `[[[10, 45], -2.0],
            [[64], -7.5]]`). 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("Qwen/Qwen2.5-0.5B-Instruct")
    >>> tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
    >>> inputs = tokenizer(["The full name of Donald is Donald"], return_tensors="pt")

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

    >>> def get_tokens(word):
    ...     return tokenizer([word], add_special_tokens=False).input_ids[0]

    >>> # IMPORTANT: Remember our tip about adding spaces before words to bias them correctly.
    >>> sequence_bias = [[get_tokens("Trump"), -10.0],]  # will fail to apply bias
    >>> biased_ids = model.generate(
    ...     inputs["input_ids"], max_new_tokens=4, do_sample=False, sequence_bias=sequence_bias
    ... )
    >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
    The full name of Donald is Donald John Trump Sr.

    >>> sequence_bias = [[get_tokens(" Trump"), -10.0],]  # will work
    >>> biased_ids = model.generate(
    ...     inputs["input_ids"], max_new_tokens=4, do_sample=False, sequence_bias=sequence_bias
    ... )
    >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
    The full name of Donald is Donald John Harper. He

    >>> # We can also add a positive bias to nudge the model towards specific tokens or continuations. This technique
    >>> # is also more effective when paired up with beam search.
    >>> sequence_bias = [[get_tokens(" Donald Duck"), 10.0],]
    >>> biased_ids = model.generate(
    ...     inputs["input_ids"], max_new_tokens=4, num_beams=4, do_sample=False, sequence_bias=sequence_bias
    ... )
    >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
    The full name of Donald is Donald Duck. He is
    ```
    sequence_biasc                 C   s&   || _ |   |   d | _d| _d S )NF)r   _validate_arguments!_convert_list_arguments_into_dictlength_1_biasprepared_bias_variablesr   r   r   r   r   r<   K  s
   
z$SequenceBiasLogitsProcessor.__init__r   r   r   c           
      C   s   | j s| | t|}|| j7 }| j D ]]\}}t|dkr"qt||jd kr,q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 )Nr^   r>   dtyper6   rq   r7   rU   )r   _prepare_bias_variablesr   
zeros_liker   r   itemsr*   r@   eqr;   r   r6   prodrB   bool)
r   r   r   biassequence_idsr   prefix_length
last_tokenmatching_rowsrG   r   r   r   r   U  s0   



z$SequenceBiasLogitsProcessor.__call__c                 C   s   |j d }g }| jD ]}|D ]}||kr|| qq
t|dkr+td| d| tj|ftj|jd| _	| j
 D ]\}}t|dkrM|| j	|d < q<d| _d S )Nr>   r   zThe model vocabulary size is z., but the following tokens were being biased: r   r^   T)r@   r   appendr*   r.   r   zerosrV   r6   r   r   r   )r   r   vocabulary_sizeinvalid_biasesr   token_idr   r   r   r   r   w  s(   



z3SequenceBiasLogitsProcessor._prepare_bias_variablesc                    s  | j }t|tst|trt|dkrtd| dt|tr3tdd | D r3td| dt|trKtdd | D rKtd| dd	d
  t|trgt fdd|D rgtd| dt|trtdd | D rtd| dd S d S )Nr   zT`sequence_bias` has to be a non-empty dictionary, or non-empty list of lists but is .c                 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   *    | ]}t |ttjf p|d k V  qdS r   Nr8   r9   npintegerr"   r   r   r   r   r&        ( zLSequenceBiasLogitsProcessor._validate_arguments.<locals>.<genexpr>.<genexpr>r   N)anyr*   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   s2   t | d totdd | d D ot | d tS )Nr   c                 s   s(    | ]}t |ttjfo|d kV  qdS r   r   r   r   r   r   r&     s   & zjSequenceBiasLogitsProcessor._validate_arguments.<locals>.all_token_bias_pairs_are_valid.<locals>.<genexpr>r^   )r8   r,   r+   rV   )sequencer   r   r   all_token_bias_pairs_are_valid  s
   zWSequenceBiasLogitsProcessor._validate_arguments.<locals>.all_token_bias_pairs_are_validc                 3   s&    | ]} | pt |d kV  qdS r   )r*   r"   r   r   r   r   r&     s    
zkEach element in `sequence_bias` has to be a non-empty list of lists of positive integers and float, but is c                 s   r   r!   )r8   rV   )r"   r   r   r   r   r&         z?`sequence_bias` has to be a dict with floats as values, but is )	r   r8   dictr,   r*   r.   r   r-   valuesr   r   r   r   r     s:    
 z/SequenceBiasLogitsProcessor._validate_argumentsc                 C   s*   t | jtr| j}dd |D | _dS dS )zSBC: we used to accept `dict{tuple of tokens: float}` directly, now we expect a listc                 S   s   i | ]}t |d  |d qS )r   r^   )r   )r"   sublistr   r   r   
<dictcomp>  s    zQSequenceBiasLogitsProcessor._convert_list_arguments_into_dict.<locals>.<dictcomp>N)r8   r   r,   )r   temp_sequencer   r   r   r     s   z=SequenceBiasLogitsProcessor._convert_list_arguments_into_dictN)r   r   r   r   r   r   r9   rV   r<   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   
  s    "@
!%r   c                	       sR   e Zd ZdZ	d	deee  deeeee ej	f  f fddZ
dd Z  ZS )
NoBadWordsLogitsProcessora8	  
    [`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], torch.Tensor]`, *optional*):
            The id(s) of the *end-of-sequence* token.

    Examples:

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

    >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/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("openai-community/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.
    ```
    Nbad_words_idsr5   c                    sr   || _ |    d ur)t tjst tr g t  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   bad_token_seqr   r   r&      s    zGNoBadWordsLogitsProcessor.__init__.<locals>.<lambda>.<locals>.<genexpr>)r+   r  r5   r  r   <lambda>   s    z4NoBadWordsLogitsProcessor.__init__.<locals>.<lambda>c                 S   s   i | ]	}t |td qS )z-inf)r   rV   r   r   r   r   r    s    z6NoBadWordsLogitsProcessor.__init__.<locals>.<dictcomp>)r   )bad_word_idsr   r8   r   r:   r9   r;   r,   filtersuperr<   )r   r  r5   r   r   r  r   r<     s   

z"NoBadWordsLogitsProcessor.__init__c                 C   st   | j }t|trt|dkrtd| dtdd |D r'td| dtdd |D r8td| dd S )	Nr   z3`bad_words_ids` has to be a non-empty list, but is r   c                 s   r   r!   )r8   r,   r"   r
  r   r   r   r&     r   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   r   r   r   r   r   r   r   r&     r   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   r,   r*   r.   r   )r   r  r   r   r   r     s   
z-NoBadWordsLogitsProcessor._validate_argumentsr!   )r   r   r   r   r   r9   r   r   r   r:   r<   r   __classcell__r   r   r  r   r    s    7
r  c                   @   sX   e Zd ZdZdeeejgee f defddZ	e
edejdejdejf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`.

    Examples:

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

    >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")
    >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")

    >>> inputs = tokenizer("Alice and Bob", return_tensors="pt")

    >>> # By default, it continues generating according to the model's logits
    >>> outputs = model.generate(**inputs, max_new_tokens=5)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    Alice and Bob are friends

    >>> # We can contrain it with `prefix_allowed_tokens_fn` to force a certain behavior based on a prefix.
    >>> # For instance, we can force an entire entity to be generated when its beginning is detected.
    >>> entity = tokenizer(" Bob Marley", return_tensors="pt").input_ids[0]  # 3 tokens
    >>> def prefix_allowed_tokens_fn(batch_id, input_ids):
    ...     '''
    ...     Attempts to generate 'Bob Marley' when 'Bob' is detected.
    ...     In this case, `batch_id` is not used, but you can set rules for each batch member.
    ...     '''
    ...     if input_ids[-1] == entity[0]:
    ...         return [entity[1].item()]
    ...     elif input_ids[-2] == entity[0] and input_ids[-1] == entity[1]:
    ...         return [entity[2].item()]
    ...     return list(range(tokenizer.vocab_size))  # If no match, allow all tokens

    >>> outputs = model.generate(**inputs, max_new_tokens=5, prefix_allowed_tokens_fn=prefix_allowed_tokens_fn)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    Alice and Bob Marley
    ```
    prefix_allowed_tokens_fnr   c                 C   s   || _ || _d S r!   )_prefix_allowed_tokens_fn
_num_beams)r   r  r   r   r   r   r<   E  s   
z)PrefixConstrainedLogitsProcessor.__init__r   r   r   c           
      C   s   t |tj }|jd | j }t|D ]2}t| jD ]*}||| j |  }| ||}t|dkr:t	d| dd||| j | |f< qq|| }	|	S )Nr   z?`prefix_allowed_tokens_fn` returned an empty list for batch ID zp.This means that the constraint is unsatisfiable. Please check your implementationof `prefix_allowed_tokens_fn` )
r   	full_likerC   rD   r@   r  r   r  r*   r.   )
r   r   r   maskr   batch_idbeam_idsentprefix_allowed_tokensrG   r   r   r   r   I  s   
z)PrefixConstrainedLogitsProcessor.__call__N)r   r   r   r   r   r9   r   r:   r   r<   r
   r   r   r   r   r   r   r   r   r    s
    $."r  c                
   @   sL   e Zd ZdZdededefddZdejdej	d	ejd
edej	f
ddZ
dS )HammingDiversityLogitsProcessoraJ  
    [`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.

    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.

    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. A higher `diversity_penalty` will enforce greater diversity among the beams. Adjusting
            this value can help strike a balance between diversity and natural likelihood.
        num_beams (`int`):
            Number of beams for beam search. 1 means no beam search.
        num_beam_groups (`int`):
            Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams.
            [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("google-t5/t5-base")
    >>> model = AutoModelForSeq2SeqLM.from_pretrained("google-t5/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r|dk rtd|| _t |tr)|dk r-td||kr5td|| | _d S )NrU   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   rV   r.   _diversity_penaltyr9   r  _num_sub_beams)r   r  r   r  r   r   r   r<     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r%|S | }
t|D ].}||| j || j |  }tj||	d|j	}|
|| |d |   | j
| 8  < 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
            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   r>   )	minlengthr^   )r@   r  r  r   rA   r   r   bincounttor6   r  )r   r   r   r  r   r   group_start_idxgroup_end_idx
group_size
vocab_sizerG   	batch_idxprevious_group_tokenstoken_frequencyr   r   r   r     s"   

z(HammingDiversityLogitsProcessor.__call__Nr   r   r   r   r   r  ^  s    Ir  c                   @   rR   )ForcedBOSTokenLogitsProcessora  
    [`LogitsProcessor`] that enforces the specified token as the first generated token. Used with encoder-decoder
    models.

    Args:
        bos_token_id (`int`):
            The id of the token to force as the first generated token.

    Examples:

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

    >>> model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
    >>> tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")

    >>> inputs = tokenizer("Translate from English to German: I love cats.", return_tensors="pt")

    >>> # By default, it continues generating according to the model's logits
    >>> outputs = model.generate(**inputs, max_new_tokens=10)
    >>> print(tokenizer.batch_decode(outputs)[0])
    <pad> Ich liebe Kitty.</s>

    >>> # We can use `forced_bos_token_id` to force the start of generation with an encoder-decoder model
    >>> # (including forcing it to end straight away with an EOS token)
    >>> outputs = model.generate(**inputs, max_new_tokens=10, forced_bos_token_id=tokenizer.eos_token_id)
    >>> print(tokenizer.batch_decode(outputs)[0])
    <pad></s>
    ```
    bos_token_idc                 C   
   || _ d S r!   )r,  )r   r,  r   r   r   r<        
z&ForcedBOSTokenLogitsProcessor.__init__r   r   r   c                 C   s<   |j d }|}|dkrt|tj }d|d d | jf< |S Nr>   r^   r   )r@   r   r  rC   rD   r,  r   r   r   r   rG   r   r   r   r   	  s   
z&ForcedBOSTokenLogitsProcessor.__call__Nr   r   r   r   r   r+    s
    "r+  c                   @   r1   )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], torch.Tensor]`):
            The id(s) of the *end-of-sequence* token.
        device (`str`, *optional*, defaults to `"cpu"`):
            The device to allocate the tensors.

    Examples:

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

    >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")

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

    >>> # By default, it continues generating according to the model's logits
    >>> outputs = model.generate(**inputs, max_new_tokens=10)
    >>> print(tokenizer.batch_decode(outputs)[0])
    A sequence: 1, 2, 3, 4, 5, 6, 7, 8

    >>> # `forced_eos_token_id` ensures the generation ends with a EOS token
    >>> outputs = model.generate(**inputs, max_new_tokens=10, forced_eos_token_id=tokenizer.eos_token_id)
    >>> print(tokenizer.batch_decode(outputs)[0])
    A sequence: 1, 2, 3, 4, 5, 6, 7,<|endoftext|>
    ```
    r3   
max_lengthr5   r6   c                 C   s^   || _ t|tjst|tr|g}tj||d}|| _t|s&|dk  r-t	d| d S )Nr7   r   =`eos_token_id` has to be a list of positive integers, but is )
r2  r8   r   r:   r9   r;   r5   is_floating_pointr   r.   )r   r2  r5   r6   r   r   r   r<   5  s   
z&ForcedEOSTokenLogitsProcessor.__init__r   r   r   c                 C   sB   |j d }|}|| jd krt|tj }d|d d | jf< |S r/  )r@   r2  r   r  rC   rD   r5   r0  r   r   r   r   A  s   
z&ForcedEOSTokenLogitsProcessor.__call__NrH   rI   r   r   r   r   r1    s
    (!"r1  c                   @   r   )InfNanRemoveLogitsProcessorak  
    [`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.

    This logits processor has no `generate` example, as there shouldn't be a correct combination of flags that warrants
    its use.
    r   r   r   c                 C   sX   t ||kd|}t |tdkt |jj|}t |td kt |jj|}|S )NrU   rD   )r   rB   rV   finfor   r   r   rX   r   r   r   r   T  s    "z$InfNanRemoveLogitsProcessor.__call__Nr   r   r   r   r   r5  K  s    "r5  c                   @   sb   e Zd ZdZdeeef deeee e	j
f defddZeede	jde	jd	e	jf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], torch.Tensor]`):
            The id(s) of the *end-of-sequence* token.
        input_ids_seq_length (`int`):
            The length of the input sequence.

    Examples:

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

    >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")

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

    >>> # Let's consider that we want short sentences, so we limit `max_length=30`. However, we observe that the answer
    >>> # tends to end abruptly.
    >>> set_seed(1)
    >>> 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 received a link to an ebook, the book How To Start A Social Network which was
    published in 2010. Although

    >>> # To promote the appearance of the EOS token at the right time, we add the `exponential_decay_length_penalty =
    >>> # (start_index, decay_factor)`. Instead of cutting at max_tokens, the output comes to an end before and usually
    >>> # with more meaning. What happens is that starting from `start_index` the EOS token score will be increased
    >>> # by `decay_factor` exponentially. However, if you set a high decay factor, you may also end up with abruptly
    >>> # ending sequences.
    >>> set_seed(1)
    >>> 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 received a link to an ebook, the book How To Start A Social Network
    which<|endoftext|>

    >>> # With a small decay factor, you will have a higher chance of getting a meaningful sequence.
    >>> set_seed(1)
    >>> outputs = model.generate(
    ...     **inputs,
    ...     do_sample=True,
    ...     temperature=0.9,
    ...     max_length=30,
    ...     pad_token_id=50256,
    ...     exponential_decay_length_penalty=(15, 1.01),
    ... )
    >>> print(tokenizer.batch_decode(outputs)[0])
    Just wanted to let you know, I received a link to an ebook, the book How To Start A Social Network which was
    published in 2010.<|endoftext|>
    ```
     exponential_decay_length_penaltyr5   input_ids_seq_lengthc                 C   sl   |d | | _ |d | _t|tjst|tr|g}t|}|| _t|s-|dk 	 r4t
d| d S )Nr   r^   r3  )regulation_startregulation_factorr8   r   r:   r9   r;   r5   r4  r   r.   )r   r8  r5   r9  r   r   r   r<     s   


z&ExponentialDecayLengthPenalty.__init__r   r   r   c                 C   s   |j d }| j|j| _t|}|}|| jkr?|| j }t|d d | jf t| j	|d  }||d d | jf< || }|S )Nr>   r^   )
r@   r5   r#  r6   r   r   r:  r   powr;  )r   r   r   r   	penaltiesrG   penalty_idxr[   r   r   r   r     s   



(z&ExponentialDecayLengthPenalty.__call__N)r   r   r   r   r   r9   rV   r   r   r   r:   r<   r
   r   r   r   r   r   r   r   r   r7  `  s    C

"r7  c                   @   r   )LogitNormalizationa  
    [`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.

    Examples:

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

    >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")

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

    >>> # By default, the scores are not normalized -- the sum of their exponentials is NOT a normalized probability
    >>> # distribution, summing to 1
    >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True)
    >>> print(torch.allclose(torch.sum(torch.exp(outputs.scores[-1])), torch.Tensor((1.000,)), rtol=1e-4))
    False

    >>> # Normalizing them may have a positive impact on beam methods, or when using the scores on your application
    >>> outputs = model.generate(**inputs, renormalize_logits=True, return_dict_in_generate=True, output_scores=True)
    >>> print(torch.allclose(torch.sum(torch.exp(outputs.scores[-1])), torch.Tensor((1.000,)), rtol=1e-4))
    True
    ```
    r   r   r   c                 C   s   |j dd}|S )Nr>   rq   )r   rX   r   r   r   r     s   zLogitNormalization.__call__Nr   r   r   r   r   r?    s    "r?  c                   @   sL   e Zd ZdZddefddZdd Zeede	j
d	e	jd
e	jf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` are
    not generated at the beginning. Originally created for
    [Whisper](https://huggingface.co/docs/transformers/model_doc/whisper).

    Examples:

    ```python
    >>> from transformers import AutoProcessor, WhisperForConditionalGeneration
    >>> from datasets import load_dataset

    >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en")
    >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en")
    >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
    >>> inputs = processor(ds[0]["audio"]["array"], return_tensors="pt")

    >>> # Whisper has `begin_suppress_tokens` set by default (= `[220, 50256]`). 50256 is the EOS token, so this means
    >>> # it can't generate and EOS token in the first iteration, but it can in the others.
    >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True)
    >>> print(outputs.scores[0][0, 50256])
    tensor(-inf)
    >>> print(outputs.scores[-1][0, 50256])  # in other places we can see some probability mass for EOS
    tensor(29.9010)

    >>> # If we disable `begin_suppress_tokens`, we can generate EOS in the first iteration.
    >>> outputs = model.generate(
    ...     **inputs, return_dict_in_generate=True, output_scores=True, begin_suppress_tokens=None
    ... )
    >>> print(outputs.scores[0][0, 50256])
    tensor(11.2027)
    ```
    r3   r6   c                 C   s   t jt||d| _|| _d S Nr7   )r   r;   r,   begin_suppress_tokensbegin_index)r   rB  rC  r6   r   r   r   r<     s   
z-SuppressTokensAtBeginLogitsProcessor.__init__c                 C   r-  r!   rC  r   rC  r   r   r   set_begin_index  r.  z4SuppressTokensAtBeginLogitsProcessor.set_begin_indexr   r   r   c                 C   sN   t j|jd |jd}t|| j}|}|jd | jkr%t |td |}|S Nr>   r7   rD   )	r   r?   r@   r6   r	   rB  rC  rB   rV   )r   r   r   rE   suppress_token_maskrG   r   r   r   r     s   z-SuppressTokensAtBeginLogitsProcessor.__call__NrH   )r   r   r   r   rJ   r<   rF  r
   r   r   r   r   r   r   r   r   r   r@    s    ""r@  c                   @   sD   e Zd ZdZddefddZeedej	dej
dej
fd	d
ZdS )SuppressTokensLogitsProcessora  
    This processor can be used to suppress a list of tokens. The processor will set their log probs to `-inf` so
    that they are not generated. Originally created for
    [Whisper](https://huggingface.co/docs/transformers/model_doc/whisper).

    Examples:

    ```python
    >>> from transformers import AutoProcessor, WhisperForConditionalGeneration
    >>> from datasets import load_dataset

    >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en")
    >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en")
    >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
    >>> inputs = processor(ds[0]["audio"]["array"], return_tensors="pt")

    >>> # Whisper has a long list of suppressed tokens. For instance, in this case, the token 1 is suppressed by default.
    >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True)
    >>> print(outputs.scores[1][0, 1])  # 1 (and not 0) is the first freely generated token
    tensor(-inf)

    >>> # If we disable `suppress_tokens`, we can generate it.
    >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True, suppress_tokens=None)
    >>> print(outputs.scores[1][0, 1])
    tensor(6.0678)
    ```
    r3   r6   c                 C   s   t jt||d| _d S rA  )r   r;   r,   suppress_tokens)r   rJ  r6   r   r   r   r<   <  s   z&SuppressTokensLogitsProcessor.__init__r   r   r   c                 C   s:   t j|jd |jd}t|| j}t |td |}|S rG  )r   r?   r@   r6   r	   rJ  rB   rV   )r   r   r   rE   rH  r   r   r   r   ?  s   z&SuppressTokensLogitsProcessor.__call__NrH   )r   r   r   r   rJ   r<   r
   r   r   r   r   r   r   r   r   r   rI    s
    "rI  c                   @   s\   e Zd ZdZ		ddee dee fddZdd Ze	e
d	ejd
ejdejfddZdS )WhisperTimeStampLogitsProcessora  

    [`LogitsProcessor`] that modifies the logits for the generation of timestamps in the transcription. When the input
    tokens are at a specific threshold, the processor sets the scores to negative infinity. The processor makes sure
    that timestamp tokens appear in pairs, by masking out the logits that would break this pairing pattern. This is
    done to maintain the consistency and structure of generated timestamps. It also ensures that when the predicted
    probability of sampling any of the timestamp token is greater than any individual non-timestamp token, those
    non-timestamp logits are set to negative infinity. This is done to ensure the generation of timestamps over other
    potential tokens.


    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.
        begin_index (`Optional`, *optional*): Token index of the first token that is generated by the model.
        _detect_timestamp_from_logprob (`bool`, *optional*): Whether timestamps can be predicted from logprobs over all timestamps.

    Examples:
    ``` python
    >>> import torch
    >>> from transformers import AutoProcessor, WhisperForConditionalGeneration, GenerationConfig
    >>> from datasets import load_dataset

    >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en")
    >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en")
    >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
    >>> inputs = processor(ds[3]["audio"]["array"], return_tensors="pt")
    >>> input_features = inputs.input_features

    >>> #Displaying timestamps
    >>> generated_ids = model.generate(inputs=input_features, return_timestamps=True)
    >>> transcription = processor.batch_decode(generated_ids, decode_with_timestamps=True)[0]
    >>> print("Transcription:", transcription)
    Transcription: <|startoftranscript|><|0.00|> He has grave doubts whether Sir Frederick Layton's work is really Greek after all, and can<|6.44|><|6.44|> discover in it but little of rocky Ithaca.<|9.44|><|endoftext|>


    >>> #No timestamps & change EOS:
    >>> #This allows the user to select a specific token to terminate the sequence on, in this case it's the word "can"(460)
    >>> model.generation_config.eos_token_id = 460
    >>> generated_ids = model.generate(inputs=input_features,return_timestamps=False)
    >>> transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    >>> print("Transcription:", transcription)
    Transcription:  He has grave doubts whether Sir Frederick Layton's work is really Greek after all and can
    ```
    NrC  _detect_timestamp_from_logprobc                 C   st   |j | _ |j d | _|jp|j| _|d ur|nt|dd| _|jd ur(t|jnd}|p/|d | _t|dd | _	d S )Nr^   rL  Tr   max_initial_timestamp_index)
no_timestamps_token_idtimestamp_beginr5   r,  getattrrL  forced_decoder_idsr*   rC  rM  )r   generate_configrC  rL  num_forced_idsr   r   r   r<     s   
z(WhisperTimeStampLogitsProcessor.__init__c                 C   r-  r!   rD  rE  r   r   r   rF    r.  z/WhisperTimeStampLogitsProcessor.set_begin_indexr   r   r   c                 C   s  |  }td |d d | jf< t|jd D ]q}||| jd f }t| }t|dko4|d | j	k}t|dk pA|d | j	k}|r_|rStd ||| j	d f< ntd ||d | j
f< ||| j	 }	|	 dkr|rv|sv|	d }
n|	d d }
td ||| j	|
f< q|jd | jkrtd |d d d | j	f< | jd ur| j	| j }td |d d |d d f< tjjj| dd}t|jd D ]-}||| j	d f jdd}||d | j	f  }||kr| jrtd ||d | j	f< q|S )NrD   r   r^   r>   r   rq   )rA   rV   rN  r   r@   rC  r,   r   r*   rO  r5   genumelrM  r   r   r   r   	logsumexpr   rL  )r   r   r   rG   ksampled_tokensseqlast_was_timestamppenultimate_was_timestamp
timestampstimestamp_lastlast_allowedlogprobstimestamp_logprobmax_text_token_logprobr   r   r   r     s>   

z(WhisperTimeStampLogitsProcessor.__call__)NN)r   r   r   r   r   r9   r   r<   rF  r
   r   r   r   r   r   r   r   r   r   rK  G  s    :
"rK  c                   @   sp   e Zd ZdZddededefddZdd	 Zd
d Ze	dd Z
dd ZeedejdejdejfddZdS )WhisperNoSpeechDetectionzThis processor can be used to detect silence when using Whisper. It should take as input unprocessed logits to follow the original implementationFno_speech_tokenrC  scores_is_logprobsc                 C   s0   || _ || _|| _dg| _|| _d | _d | _d S )NrU   )rd  start_of_trans_offsetrC  _no_speech_probis_scores_logprobsmodelinputs)r   rd  rC  re  r   r   r   r<     s   
z!WhisperNoSpeechDetection.__init__c                 C   r-  r!   )ri  )r   ri  r   r   r   	set_model  r.  z"WhisperNoSpeechDetection.set_modelc                 C   s2   i | j jdi ||| _| jd| jd< d S )Nrj  input_featuresr   )ri  prepare_inputs_for_generationrj  pop)r   rj  r   r   r   
set_inputs  s   z#WhisperNoSpeechDetection.set_inputsc                 C   s   | j S r!   )rg  )r   r   r   r   no_speech_prob  s   z'WhisperNoSpeechDetection.no_speech_probc                 C   r-  r!   rD  rE  r   r   r   rF    r.  z(WhisperNoSpeechDetection.set_begin_indexr   r   r   c                 C   s   | j }|jd | jkrZ| jdkr?t  | jdi | jj}W d    n1 s)w   Y  | j| j }|d d |f }d}n|}|rH|	 }n|
 jdd}|d d | jf | _|S )Nr^   Fr>   rq   r   )rh  r@   rC  rf  r   no_gradri  rj  r   r   rV   rt   rd  rg  )r   r   r   rh  r   no_speech_indexno_speech_scoresr   r   r   r   r     s   


z!WhisperNoSpeechDetection.__call__N)F)r   r   r   r   r9   r   r<   rk  ro  propertyrp  rF  r
   r   r   r   r   r   r   r   r   r   rc    s    
"rc  c                   @   s<   e Zd ZdZdd Zeedejdej	dej	fddZ
d	S )
%ClassifierFreeGuidanceLogitsProcessoran  
    [`LogitsProcessor`] 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.

    <Tip warning={true}>

    This logits processor is exclusively compatible with
    [MusicGen](https://huggingface.co/docs/transformers/main/en/model_doc/musicgen)

    </Tip>

    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.

    Examples:

    ```python
    >>> from transformers import AutoProcessor, MusicgenForConditionalGeneration

    >>> processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
    >>> model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")

    >>> inputs = processor(
    ...     text=["80s pop track with bassy drums and synth", "90s rock song with loud guitars and heavy drums"],
    ...     padding=True,
    ...     return_tensors="pt",
    ... )
    >>> audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=256)
    ```
    c                 C   s"   |dkr	|| _ d S td| d)Nr^   z\Require guidance scale >1 to use the classifier free guidance processor, got guidance scale r   )guidance_scaler.   )r   rv  r   r   r   r<   /  s   
z.ClassifierFreeGuidanceLogitsProcessor.__init__r   r   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.rq   )r@   r.   splitrv  )r   r   r   unguided_bszcond_logitsuncond_logitsrG   r   r   r   r   8  s   z.ClassifierFreeGuidanceLogitsProcessor.__call__N)r   r   r   r   r<   r
   r   r   r   r   r   r   r   r   r   ru    s
    &	"ru  c                   @   sB   e Zd ZdZdededefddZdejdejd	ejfd
dZ	dS )#AlternatingCodebooksLogitsProcessora  
    [`LogitsProcessor`] enforcing alternated generation between the two codebooks of Bark.

    <Tip warning={true}>

    This logits processor is exclusively compatible with
    [Bark](https://huggingface.co/docs/transformers/en/model_doc/bark)'s fine submodel. See the model documentation
    for examples.

    </Tip>

    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   r.   r|  r}  r~  )r   r|  r}  r~  r   r   r   r<   ]  s
   
z,AlternatingCodebooksLogitsProcessor.__init__r   r   r   c                 C   s   |j d }|| j d dk}| }|r5td |d d d | jf< td |d d | j| j d f< |S td |d d d | j| j f< |S )Nr>   r   r   rD   )r@   r|  rA   rV   r}  r~  )r   r   r   curr_lenis_first_codebookrG   r   r   r   r   e  s   
""z,AlternatingCodebooksLogitsProcessor.__call__N)
r   r   r   r   r9   r<   r   r   r   r   r   r   r   r   r{  H  s     r{  c                
   @   sR   e Zd ZdZ			ddedeej deej dee f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.
        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.
        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.
        use_cache (`bool`, *optional*, defaults to `True`):
            Whether to cache key/values during the negative prompt forward pass.


    Examples:

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

    >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/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"
    ```
    NTrv  unconditional_idsunconditional_attention_mask	use_cachec                 C   s"   || _ || _|||d dd| _d S )NT)r   attention_maskr  past_key_values
first_pass)rv  ri  unconditional_context)r   rv  ri  r  r  r  r   r   r   r<     s   z7UnbatchedClassifierFreeGuidanceLogitsProcessor.__init__c                 C   sB  | j d r>| j d d u r|d d dd f | j d< | j d d u r.tj| j d tjd| j d< | j d }| j d }d| j d< nHtj| j d tj|d d dd f tjdgdd}| j d	 srt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   r>   r  )r   Fr^   rq   r  r  )r  r  r  )r  r   	ones_likelongcatri  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 )Nr>   rq   r^   )r   r   r   r   rv  r  )r   r   r   r   unconditional_logitsrG   r   r   r   r     s   

z7UnbatchedClassifierFreeGuidanceLogitsProcessor.__call__)NNT)r   r   r   r   rV   r   r   r   r   r<   r  r   r   r   r   r   r  u  s     7
$r  c                   @   s\   e Zd ZdZddeeee ejf de	de
fddZeedejd	ejd
ejfddZdS )!BarkEosPrioritizerLogitsProcessora  This processor ensures that the EOS token is selected if its probability is greater than the `min_eos_p`.

    <Tip warning={true}>

    This logits processor is exclusively compatible with
    [Bark](https://huggingface.co/docs/transformers/en/model_doc/bark). See the model documentation for examples.

    </Tip>

    Args:
        eos_token_id (`Union[int, List[int], torch.Tensor]`):
            The id(s) of the *end-of-sequence* token.
        min_eos_p (`float`, *optional*):
            Minimum end of speech threshold.
    r3   r5   	min_eos_pr6   c                 C   s|   t |tjst |tr|g}tj||d}|| _t|s#|dk  r*td| |d ur9|dkr9td| || _	d S )Nr7   r   r3  z/`min_eos_p` has to be a positive float, but is )
r8   r   r:   r9   r;   r5   r4  r   r.   r  )r   r5   r  r6   r   r   r   r<     s   

z*BarkEosPrioritizerLogitsProcessor.__init__r   r   r   c                 C   s   |}| j rEtjjj| dd}t|td  }|d d | jf |d d | jf< |d d | jf | j k}tj|ddd}t	|||}|S )Nr>   rq   rD   r^   Tr   )
r  r   r   r   rt   rV   r  r5   r   rB   )r   r   r   rG   r   early_stop_scoresdo_early_stopr   r   r   r   
	  s    z*BarkEosPrioritizerLogitsProcessor.__call__NrH   )r   r   r   r   r   r9   r   r   r:   rV   rJ   r<   r
   r   r   r   r   r   r   r   r   r    s
    ("r  c                   @   s   e Zd ZdZ					ddeded	ed
edef
ddZdej	fddZ
dej	dej	fddZdej	dejdej	fddZeedej	dejdejfddZdS )WatermarkLogitsProcessora  
    Logits processor for watermarking generated text. The processor modifies model output scores by adding a small bias to
    randomized set of "green" tokens before generating the next token. "Green" tokens selection process depends on the
    `seeding_scheme` used. The code was based on the [original repo](https://github.com/jwkirchenbauer/lm-watermarking/tree/main).

    The text generated by this `LogitsProcessor` can be detected using `WatermarkDetector`. See [`~WatermarkDetector.__call__`] for details,

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

    Args:
        vocab_size (`int`):
            The model tokenizer's vocab_size. Used to calculate "green" tokens ratio.
        device (`str`):
            The device where model is allocated.
        greenlist_ratio (`float`, optional, *optional*, defaults to 0.25):
            The ratio of "green" tokens used to the vocabulary size. Defaults to 0.25.
        bias (`float`, optional, *optional*, defaults to 2.0):
            The bias added to the selected "green" tokens' logits. Consider lowering the
            `bias` if the text generation quality degrades. Recommended values are in the
            range of [0.5, 2.0]. Defaults to 2.0.
        hashing_key (`int`, optional, *optional*, defaults to 15485863):
            Key used for hashing. If you deploy this watermark, we advise using another private key.
            Defaults to 15485863 (the millionth prime).
        seeding_scheme (`str`, optional, *optional*, defaults to `"lefthash"`):
            The seeding scheme used for selecting "green" tokens. Accepts values:
                - "lefthash" (default): "green" tokens selection depend on the last token (Algorithm 2 from paper)
                - "selfhash": "green" tokens selection depends on the current token itself (Algorithm 3 from paper)
                    The downside of this scheme is that it considers all possible next tokens and can be slower than "lefthash".
            The context length of previous tokens to use in seeding. Higher context length makes watermarking more robust.
        context_width (`int`, *optional*, defaults to 1):
            The number of previous tokens to use when setting the seed.

    Examples:

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

    >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
    >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
    >>> inputs = tokenizer(["Alice and Bob are"], return_tensors="pt")

    >>> # normal generation
    >>> out = model.generate(inputs["input_ids"], max_length=20, do_sample=False)
    >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
    'Alice and Bob are both in the same room.\n\n"I\'m not sure if you\'re'

    >>> # watermarked generation
    >>> watermarking_config = WatermarkingConfig(bias=2.5, context_width=2, seeding_scheme="selfhash")
    >>> out = model.generate(inputs["input_ids"], watermarking_config=watermarking_config, max_length=20, do_sample=False)
    >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
    'Alice and Bob are both still alive and well and the story is pretty much a one-hour adventure'

    >>> # to detect watermarked text use the WatermarkDetector class
    >>> from transformers import WatermarkDetector
    >>> detector = WatermarkDetector(model_config=model.config, device="cpu", watermarking_config= watermarking_config)
    >>> detection_preds = detector(out)
    >>> detection_preds
    array([ True])
    ```
          ?       @K lefthashr^   greenlist_ratior   hashing_keyseeding_schemecontext_widthc                 C   s   |dvrt d| |dks|dkrt d| || _t| j| | _|| _|| _tj|d| _|| _	|| _
| j| d| _tj| j| j|d| _d S )	N)selfhashr  zDseeding_scheme has to be one of [`selfhash`, `lefthash`], but found rl   rU   zLgreenlist_ratio has be in range between 0.0 and 1.0, exclusively. but found r7   iCB )	generatorr6   )r.   r'  r9   greenlist_sizer   r  r   	Generatorrnghash_keyr  manual_seed
table_sizerandpermfixed_table)r   r'  r6   r  r   r  r  r  r   r   r   r<   X	  s    
z!WatermarkLogitsProcessor.__init__	input_seqc                 C   s   || j  d  }| jdkr/| j|| j  d }| j|d | j  d }| j| |   }n	| j|d   }| j|d  d S )Nr  r^   r>   l    )	r  r  r  r  r  r   itemr  r  )r   r  abseedr   r   r   set_seedu	  s   
z!WatermarkLogitsProcessor.set_seedr   c                 C   s2   |  | tj| j|j| jd}|d | j }|S )N)r6   r  )r  r   r  r'  r6   r  r  )r   r  vocab_permutationgreenlist_idsr   r   r   _get_greenlist_ids	  s   
z+WatermarkLogitsProcessor._get_greenlist_idsr   c                 C   sn   g }|j ddd\}}tdD ]}| tj|||df gdd}|| |v r.|||  qtj||jdS )z
        Generate greenlist based on current candidate next token. Reject and move on if necessary.
        Runs for a fixed number of steps only for efficiency, since the methods is not batched.
        r>   T)rr   rp   (   Nrq   r7   )rs   r   r  r   r  r   r;   r6   )r   r  r   final_greenlistr   greedy_predictionsr   r  r   r   r   _score_rejection_sampling	  s    z2WatermarkLogitsProcessor._score_rejection_samplingr   c                 C   s   |j d | jk rtd| j d|j d  d |S | }t|D ]$\}}| jdkr4| ||| }n| |}|||f | j	 |||f< q"|S )Nr>   z"`input_ids` should have at least `z` tokens but has z7. The seeding will be skipped for this generation step!r  )
r@   r  loggerwarningrA   r   r  r  r  r   )r   r   r   rG   b_idxr  r  r   r   r   r   	  s   

z!WatermarkLogitsProcessor.__call__N)r  r  r  r  r^   )r   r   r   r   rV   r9   rJ   r<   r   r   r  r  r   r  r
   r   r   r   r   r   r   r  	  s.    A

"r  c                   @   s,   e Zd ZdZdedededejfddZdS )	SynthIDTextWatermarkStatezSynthID watermarking state.r   	ngram_lencontext_history_sizer6   c                 C   s>   t j||d ft j|d| _t j||ft j|d| _d| _dS )a  Initializes the state.

        Args:
            batch_size (`int`): Batch size.
            ngram_len (`int`): Ngram length.
            context_history_size (`int`): Size of the tensor to keep track of seen contexts.
            device (`int`): Device to use.
        r^   r   r   N)r   r   int64contextcontext_history	num_calls)r   r   r  r  r6   r   r   r   r<   	  s   

z"SynthIDTextWatermarkState.__init__N)r   r   r   r   r9   r   r6   r<   r   r   r   r   r  	  s    r  c                   @   s~  e Zd ZdZ		d9dedee dedededejd	ed
efddZ	defddZ
dejdejdejfddZeedejdejdejfddZ		d:dejdejdededejf
ddZd ejdejfd!d"Zd#ejd$ejdeejejf fd%d&Zd'ejdejfd(d)Zdejfd*d+Zdejdejfd,d-Zdejdejfd.d/Zdejd0edejfd1d2Zd;d4ed5edefd6d7Zd8S )<#SynthIDTextWatermarkLogitsProcessora  
    Logits processor that implements watermarking techniques for text generation models.
    This class facilitates the application of SynthID text watermarking, a method for embedding imperceptible signals
    into generated text to aid in detecting synthetic content. It operates by subtly manipulating the probabilities of
    token selection during text generation in a manner that can be reliably recovered later for verification.

    Key Features:
    * **State Management:** Maintains internal state to track token sequences and generate watermarking keys
    dynamically.

    * **Key Generation:** Computes hashes based on token sequences and watermarking parameters to create unique keys
    for each position.

    * **G-Value Sampling:** Employs a pre-computed sampling table to sample watermarking values (g-values) based on
    the generated keys.

    * **Score Adjustment:** Applies calculated g-values to modify token probabilities during generation, embedding the
    watermark.

    * **Context Repetition Handling:** Incorporates logic to avoid watermarking tokens in repeated contexts,
    preserving naturalness.

    * **EOS Token Masking:** Supports masking end-of-sentence tokens to prevent their inclusion in watermarking
    calculations.

    * **Utility Functions:** Provides functions to compute g-values directly, check for context repetition, create
    EOS token masks, and estimate expected mean g-values.

    Refer to paper url: https://www.nature.com/articles/s41586-024-08025-4 for more details around this.

    Args:
        ngram_len (`int`):
            Ngram length.
        keys (`List[int]`):
            A sequence of watermarking keys, one for each depth.
        sampling_table_size (`int`):
            Size of the sampling table.
        sampling_table_seed (`int`):
            Random seed to generate the sampling table.
        context_history_size (`int`):
            Size of the tensor to keep track of seen contexts.
        device (`torch.device`):
            Device to use.
        skip_first_ngram_calls (`bool`, *optional*, defaults to `False`):
            Whether to skip first ngram calls.
        debug_mode (`bool`, optional, *optional*, defaults to `False`):
            Logits are modified to uniform one got before watermarking modification is applied. This is to test the
            implementation.

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

    >>> tokenizer = AutoTokenizer.from_pretrained('google/gemma-2-2b', padding_side="left")
    >>> model = AutoModelForCausalLM.from_pretrained('google/gemma-2-2b')

    >>> # SynthID Text configuration
    >>> watermarking_config = SynthIDTextWatermarkingConfig(
    ...     keys=[654, 400, 836, 123, 340, 443, 597, 160, 57],
    ...     ngram_len=5,
    ... )

    >>> # Generation with watermarking
    >>> tokenized_prompts = tokenizer(["Once upon a time, "], return_tensors="pt", padding=True)
    >>> output_sequences = model.generate(
    ...     **tokenized_prompts, watermarking_config=watermarking_config, do_sample=True, max_new_tokens=10
    ... )
    >>> watermarked_text = tokenizer.batch_decode(output_sequences, skip_special_tokens=True)
    ```
    Fr  r-   sampling_table_sizesampling_table_seedr  r6   skip_first_ngram_calls
debug_modec	           
      C   sb   || _ tj||d| _tj|d|}	tjdd|f|	|d| _|| _|| _	d | _
|| _|| _d S )Nr7   r   r   )lowhighr   r  r6   )r  r   r;   r-   r  r  randintsampling_tabler  r6   stater  r  )
r   r  r-   r  r  r  r6   r  r  r  r   r   r   r<   
  s   
z,SynthIDTextWatermarkLogitsProcessor.__init__r   c                 C   s   t || j| j| jd| _dS )zInitializes the state.r   r  r  r6   N)r  r  r  r6   r  )r   r   r   r   r   _init_state0
  s   z/SynthIDTextWatermarkLogitsProcessor._init_stater   g_valuesr   c           
      C   s   |j \}}}tj|dd}t|D ]}|dddd|f }|| jddd}|d| |  }qt|}	tt|	|	t|	j	j
}	|	S )aY  Updates scores using the g values.

        We assume that the scores are in the log space.
        Args:
            scores (`torch.FloatTensor`): Scores (batch_size, vocab_size).
            g_values (`torch.FloatTensor`): G valus (batch_size, vocab_size, depth).

        Returns:
            Updated scores (batch_size, vocab_size).
        r^   rq   NT)axiskeepdims)r@   r   rt   r   r   logrB   isfiniter6  r   r   )
r   r   r  r   depthr   r   g_values_at_depthg_mass_at_depth	log_probsr   r   r   update_scores9
  s   
z1SynthIDTextWatermarkLogitsProcessor.update_scoresr   c                    sf    | |j\} jrt|}t fddt|D } jd u r, | n#tj	 jj
|d d dd f fdd j_
 jj
d d dd f  j_
 jd u rXtd j jd7  _ jrl jj jk rl|S   jj
|\}} |} ||}|d d d f } jj|kjddd}	tj	| jjfddd d d df  j_tj|	||d	}
|
S )
Nc                    s   g | ]
}t j jd qS )r7   )r   r?   r6   r   r   r'  r   r   r   Z
  s    z@SynthIDTextWatermarkLogitsProcessor.__call__.<locals>.<listcomp>r>   r^   rq   zJself.state can't be None! Call `self._init_state` to initialize the state.Tr   )inputother)_check_input_ids_shaper@   r  r   r  stackr   r  r  concatr  r.   r  r  r  _compute_keyssample_g_valuesr  r  r   rB   )r   r   r   r   all_indices
ngram_keyshash_result_with_just_contextr  updated_scoresis_repeated_contextupdated_watermarked_scoresr   r  r   r   Q
  sJ   







z,SynthIDTextWatermarkLogitsProcessor.__call__   -*PB r^   current_hashdata
multiplier	incrementc                 C   sD   t |jd D ]}t||d|f }t||}t||}q|S )aG  
        Accumulate hash of data on current hash.

        Method uses adapted linear congruential generator with newlib/musl parameters.

        This function has following property -
        f(x, data[T]) = f(f(x, data[:T - 1]), data[T])

        This function expects current_hash.shape and data.shape[:-1] to
        match/broadcastable.

        Args:
            current_hash (`torch.LongTensor`):
                (shape,)
            data (`torch.LongTensor`):
                (shape, tensor_len)
            multiplier (`int`, optional, *optional*, defaults to 6364136223846793005):
                multiplier of linear congruential generator
            increment (`int`, optional, *optional*, defaults to 1):
                increment of linear congruential generator

        Returns:
            updated hash (shape,)
        r>   .)r   r@   r   addmul)r   r  r  r  r  r   r   r   r   accumulate_hash
  s
   z3SynthIDTextWatermarkLogitsProcessor.accumulate_hashngramsc                 C   s   t |jdkrtd|j |jd | jkr#td| j d|j |j\}}}tj|| jtjd}tj| j	ddd	||}| j
d
d
d
d
d
f }tj| j	ddd	||}|S )zComputes random keys for each ngram and depth.

        Args:
            ngrams (`torch.LongTensor`):
                Ngrams (batch_size, num_ngrams, ngram_len).

        Returns:
            ngram keys (batch_size, num_ngrams, depth).
           zFNgrams should be of shape (batch_size, num_ngrams, ngram_len), but is r   zRNgrams should be of shape (batch_size, num_ngrams, ngram_len), where ngram_len is z	, but is r6   r   Nr^   r^   in_dimsout_dimsNNr   )r*   r@   r.   r  r   onesr6   r  vmapr  r-   )r   r  r   r   hash_resultr-   r   r   r   compute_ngram_keys
  s    
z6SynthIDTextWatermarkLogitsProcessor.compute_ngram_keysn_minus_1_gramsindicesc                 C   s   |j \}}tj|| jtjd}| ||}tj| jddd||dddddf }| jdddddf }tj| jddd||}||fS )ah  Computes random keys for each ngram and depth.

        Args:
            n_minus_1_grams (`torch.LongTensor`):
                Ngrams (batch_size, ngram_len - 1).
            indices (`torch.LongTensor`):
                indices of the continuations (batch_size, num_indices)

        Returns:
            Ngram keys (batch_size, num_indices, depth).
        r  r  r^   r  Nr  r   )r@   r   r  r6   r  r  r  r-   )r   r  r  r   r   r  r  r-   r   r   r   r  
  s   

z1SynthIDTextWatermarkLogitsProcessor._compute_keysr  c                 C   s4   | j j\}| j dd|f}|| }tj||ddS )a  
        Samples g values from Bernoulli distribution.

        It is not possible to pass random keys in a vectorized way in torch. Instead
        we pre-compute a random sampling table, and use apply modulo table size to
        map from ngram keys (int64) to g values.

        Args:
            ngram_keys (`torch.LongTensor`):
                Random keys (batch_size, num_ngrams, depth).

        Returns:
            G values (batch_size, num_ngrams, depth).
        r^   r   )r  rr   )r  r@   reshaper   take_along_dim)r   r  r  r  r   r   r   r  
  s   
z3SynthIDTextWatermarkLogitsProcessor.sample_g_valuesc                 C   s"   t |jdkrtd|j dS )zChecks the shape of input ids.r   z=Input ids should be of shape (batch_size, input_len), but is N)r*   r@   r.   )r   r   r   r   r   r    s   z:SynthIDTextWatermarkLogitsProcessor._check_input_ids_shapec                 C   s0   |  | |jd| jdd}| |}| |S )a"  
        Computes g values for each ngram from the given sequence of tokens.

        Args:
            input_ids (`torch.LongTensor`):
                Input token ids (batch_size, input_len).

        Returns:
            G values (batch_size, input_len - (ngram_len - 1), depth).
        r^   	dimensionr   step)r  unfoldr  r  r  )r   r   r  r  r   r   r   compute_g_values  s   


z4SynthIDTextWatermarkLogitsProcessor.compute_g_valuesc                 C   s  |  | |j\}}t|| j| j| jd}|ddddf jd| jd dd}|j\}}}g }t|D ]E}|dd|ddf }	tj	|| jtj
d}
| |
|	dddf }|j|kjddd}|| tj||jfdd	ddddf |_q4tj|dd	}t|S )
aN  
        Computes repetition mask.

        0 and 1 stand for repeated and not repeated context n-1 grams respectively.

        Args:
            input_ids (`torch.LongTensor`):
                Input token ids (batch_size, input_len).

        Returns:
            Repetitions mask (batch_size, input_len - (ngram_len - 1)).
        r  Nr>   r^   r  r  Tr   rq   )r  r@   r  r  r  r6   r  r   r   r  r  r  r  r   r   r  logical_not)r   r   r   r   r  contextsnum_contextsare_repeated_contextsr   r  r  context_hashr  r   r   r   compute_context_repetition_mask'  s@   




zCSynthIDTextWatermarkLogitsProcessor.compute_context_repetition_maskr5   c                 C   sn   |  | g }||k}|D ]"}t|}t|}|jd dkr*d||d d d< || qtj|ddS )aq  
        Computes repetitions mask.

        1 stands for ngrams that don't contain EOS tokens and vice versa.

        Args:
            input_ids (`torch.LongTensor`):
                Input token ids (batch_size, input_len).
            eos_token_id (`int`):
                EOS token ID.

        Returns:
            EOS token mask (batch_size, input_len).
        r   Nrq   )r  r   nonzeror  r@   r   r  )r   r   r5   noneos_masksall_eos_equatedeos_equatednonzero_idxnoneos_maskr   r   r   compute_eos_token_maskU  s   


z:SynthIDTextWatermarkLogitsProcessor.compute_eos_token_mask      ?r'  coinflip_probc                 C   s   ||d|  dd|    S )a  
        Compute expected mean g-value after watermarking, assuming uniform LM dist.

        This is the theoretical expected value for single-layer watermarking.

        Args:
            vocab_size (`int`):
                The size of the vocabulary.
            coinflip_prob arg_name (`float`, *optional*, defaults to 0.5):
                Probability of 1 in boolean prf.

        Returns:
            The expected mean g-value for watermarked text.
        r^   r   )r   r'  r  r   r   r   expected_mean_g_valueo  s   z9SynthIDTextWatermarkLogitsProcessor.expected_mean_g_valueN)FF)r  r^   )r  )r   r   r   r   r9   r   r   r6   r   r<   r  r   r  r
   r   r   r   r  r  r   r  r  r  r  r  r
  rV   r  r   r   r   r   r  	  sh    O	
!	B
%!
(.r  )>r'   rC   typingr   r   r   r   r   r   numpyr   r   pytorch_utilsr	   utilsr
   utils.loggingr   r   r  r   r   r,   r    r2   rK   rS   rZ   rd   rg   r}   r   r   r   r   r9   r:   r   r   r   r   r   r   r  r  r  r+  r1  r5  r7  r?  r@  rI  rK  rc  ru  r{  r  r  r  r  r  r   r   r   r   <module>   s|    
&=HB67C5MXET

;E 2[H 	-8e%5( 	9@-v/ 