U
    9%e                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlZddl	m
Z
mZ eeZ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eeedddZdS )    N)ABC)deepcopy)Optional   )add_start_docstringsloggingaK  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

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

            [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 scores for each vocabulary token before SoftMax
            or scores for each vocabulary token after SoftMax.
        kwargs (`Dict[str, Any]`, *optional*):
            Additional stopping criteria specific kwargs.

    Return:
        `bool`. `False` indicates we should continue, `True` indicates we should stop.

c                   @   s.   e Zd ZdZeeejeje	dddZ
dS )StoppingCriteriazTAbstract base class for all stopping criteria that can be applied during generation.	input_idsscoresreturnc                 K   s   t dd S )Nz'StoppingCriteria needs to be subclassed)NotImplementedErrorselfr
   r   kwargs r   h/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/generation/stopping_criteria.py__call__'   s    zStoppingCriteria.__call__N)__name__
__module____qualname____doc__r   "STOPPING_CRITERIA_INPUTS_DOCSTRINGtorch
LongTensorFloatTensorboolr   r   r   r   r   r   $   s   r   c                   @   sD   e Zd ZdZd	eee dddZeee	j
e	jedddZdS )
MaxLengthCriteriaa  
    This class can be used to stop generation whenever the full generated number of tokens exceeds `max_length`. Keep
    in mind for decoder-only type of transformers, this will include the initial prompted tokens.

    Args:
        max_length (`int`):
            The maximum length that the output sequence can have in number of tokens.
        max_position_embeddings (`int`, `optional`):
            The maximum model length, as defined by the model's `config.max_position_embeddings` attribute.
    N
max_lengthmax_position_embeddingsc                 C   s   || _ || _d S Nr   )r   r   r    r   r   r   __init__8   s    zMaxLengthCriteria.__init__r	   c                 K   sD   |j d }|| jk}| jd k	r@|s@|| jkr@td| j d |S )NzrThis is a friendly reminder - the current text generation call will exceed the model's predefined maximum length (zb). Depending on the model, you may observe exceptions, performance degradation, or nothing at all.)shaper   r    loggerZwarning_once)r   r
   r   r   cur_lenZis_doner   r   r   r   <   s    

zMaxLengthCriteria.__call__)N)r   r   r   r   intr   r"   r   r   r   r   r   r   r   r   r   r   r   r   ,   s   r   c                   @   s>   e Zd ZdZeedddZeeej	ej
edddZdS )	MaxNewTokensCriteriaa  
    This class can be used to stop generation whenever the generated number of tokens exceeds `max_new_tokens`. Keep in
    mind for decoder-only type of transformers, this will **not** include the initial prompted tokens. This is very
    close to `MaxLengthCriteria` but ignores the number of initial tokens.

    Args:
        start_length (`int`):
            The number of initial tokens.
        max_new_tokens (`int`):
            The maximum number of tokens to generate.
    )start_lengthmax_new_tokensc                 C   s2   t d||  dt || _|| _|| | _d S )NzYThe class `MaxNewTokensCriteria` is deprecated. Please use `MaxLengthCriteria(max_length=z=)` with `max_length = start_length + max_new_tokens` instead.)warningswarnFutureWarningr)   r*   r   )r   r)   r*   r   r   r   r"   V   s    zMaxNewTokensCriteria.__init__r	   c                 K   s   |j d | jkS )Nr#   )r$   r   r   r   r   r   r   a   s    zMaxNewTokensCriteria.__call__N)r   r   r   r   r'   r"   r   r   r   r   r   r   r   r   r   r   r   r(   I   s   r(   c                   @   sD   e Zd ZdZd	eee dddZeee	j
e	jedddZdS )
MaxTimeCriteriaa  
    This class can be used to stop generation whenever the full generation exceeds some amount of time. By default, the
    time will start being counted when you initialize this function. You can override this by passing an
    `initial_time`.

    Args:
        max_time (`float`):
            The maximum allowed time in seconds for the generation.
        initial_time (`float`, *optional*, defaults to `time.time()`):
            The start of the generation allowed time.
    N)max_timeinitial_timestampc                 C   s    || _ |d krt n|| _d S r!   )r/   timer0   )r   r/   r0   r   r   r   r"   s   s    zMaxTimeCriteria.__init__r	   c                 K   s   t   | j | jkS r!   )r1   r0   r/   r   r   r   r   r   w   s    zMaxTimeCriteria.__call__)N)r   r   r   r   floatr   r"   r   r   r   r   r   r   r   r   r   r   r   r.   f   s   r.   c                   @   s@   e Zd ZeeejejedddZ	e
ee dddZdS )StoppingCriteriaListr	   c                    s   t  fdd| D S )Nc                 3   s   | ]}| V  qd S r!   r   ).0criteriar
   r   r   r   	<genexpr>   s     z0StoppingCriteriaList.__call__.<locals>.<genexpr>)anyr   r   r6   r   r   }   s    zStoppingCriteriaList.__call__)r   c                 C   s6   | D ],}t |tr|j  S t |tr|j  S qd S r!   )
isinstancer   r   r(   )r   Zstopping_criteriumr   r   r   r      s    


zStoppingCriteriaList.max_lengthN)r   r   r   r   r   r   r   r   r   r   propertyr   r'   r   r   r   r   r   r3   |   s   r3   )stopping_criteriar   r   c                 C   sH   | j }t| }|d k	r,||kr,tdt n|d krD|t|d |S )NzOYou set different `max_length` for stopping criteria and `max_length` parameter)r   )r   r   r+   r,   UserWarningappendr   )r;   r   Zstopping_max_lengthZnew_stopping_criteriar   r   r   validate_stopping_criteria   s    r>   )r1   r+   abcr   copyr   typingr   r   utilsr   r   Z
get_loggerr   r%   r   r   r   r(   r.   listr3   r'   r>   r   r   r   r   <module>   s   
