


"""simple docstring"""
import time
import warnings
from abc import ABC
from copy import deepcopy
from typing import Optional

import torch

from ..utils import add_start_docstrings, logging


__SCREAMING_SNAKE_CASE							:      Any						=      logging.get_logger(__name__)


__SCREAMING_SNAKE_CASE							:      Dict						=      R'\n    Args:\n        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):\n            Indices of input sequence tokens in the vocabulary.\n\n            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and\n            [`PreTrainedTokenizer.__call__`] for details.\n\n            [What are input IDs?](../glossary#input-ids)\n        scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`):\n            Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax\n            or scores for each vocabulary token after SoftMax.\n        kwargs (`Dict[str, Any]`, *optional*):\n            Additional stopping criteria specific kwargs.\n\n    Return:\n        `bool`. `False` indicates we should continue, `True` indicates we should stop.\n\n'





class        __A       (snake_case__):





							'''simple docstring'''

							@add_start_docstrings(UpperCAmelCase_    )
							def __call__(			self							: Any						,    UpperCAmelCase_							: torch.LongTensor						,    UpperCAmelCase_							: torch.FloatTensor						,    **UpperCAmelCase_							: Optional[Any]    )   ->bool:



														"""simple docstring"""



														raise NotImplementedError("""StoppingCriteria needs to be subclassed"""    )





class        __A       (snake_case__):





							'''simple docstring'''

							def __init__(			self							: Dict						,    UpperCAmelCase_							: int						,    UpperCAmelCase_							: Optional[int] = None    )   ->Union[str, Any]:



														"""simple docstring"""



														snake_case_          =						max_length
														snake_case_          =						max_position_embeddings



							@add_start_docstrings(UpperCAmelCase_    )
							def __call__(			self							: str						,    UpperCAmelCase_							: torch.LongTensor						,    UpperCAmelCase_							: torch.FloatTensor						,    **UpperCAmelCase_							: int    )   ->bool:



														"""simple docstring"""



														snake_case_          =						input_ids.shape[-1]
														snake_case_          =						cur_len >= self.max_length
														if self.max_position_embeddings is not None and not is_done and cur_len >= self.max_position_embeddings:
																					logger.warning_once(
																					    """This is a friendly reminder - the current text generation call will exceed the model's predefined """
																					    F"""maximum length ({self.max_position_embeddings}). Depending on the model, you may observe """
																					    """exceptions, performance degradation, or nothing at all."""    )
														return is_done





class        __A       (snake_case__):





							'''simple docstring'''

							def __init__(			self							: str						,    UpperCAmelCase_							: int						,    UpperCAmelCase_							: int    )   ->Union[str, Any]:



														"""simple docstring"""



														warnings.warn(
														    """The class `MaxNewTokensCriteria` is deprecated. """
														    F"""Please use `MaxLengthCriteria(max_length={start_length + max_new_tokens})` """
														    """with `max_length = start_length + max_new_tokens` instead."""						,    UpperCAmelCase_						,    )
														snake_case_          =						start_length
														snake_case_          =						max_new_tokens
														snake_case_          =						start_length + max_new_tokens



							@add_start_docstrings(UpperCAmelCase_    )
							def __call__(			self							: Tuple						,    UpperCAmelCase_							: torch.LongTensor						,    UpperCAmelCase_							: torch.FloatTensor						,    **UpperCAmelCase_							: List[str]    )   ->bool:



														"""simple docstring"""



														return input_ids.shape[-1] >= self.max_length





class        __A       (snake_case__):





							'''simple docstring'''

							def __init__(			self							: Any						,    UpperCAmelCase_							: float						,    UpperCAmelCase_							: Optional[float] = None    )   ->List[Any]:



														"""simple docstring"""



														snake_case_          =						max_time
														snake_case_          =						time.time() if initial_timestamp is None else initial_timestamp



							@add_start_docstrings(UpperCAmelCase_    )
							def __call__(			self							: Tuple						,    UpperCAmelCase_							: torch.LongTensor						,    UpperCAmelCase_							: torch.FloatTensor						,    **UpperCAmelCase_							: Union[str, Any]    )   ->bool:



														"""simple docstring"""



														return time.time() - self.initial_timestamp > self.max_time





class        __A       (snake_case__):





							'''simple docstring'''

							@add_start_docstrings(UpperCAmelCase_    )
							def __call__(			self							: Optional[int]						,    UpperCAmelCase_							: torch.LongTensor						,    UpperCAmelCase_							: torch.FloatTensor						,    **UpperCAmelCase_							: str    )   ->bool:



														"""simple docstring"""



														return any(criteria(UpperCAmelCase_						,    UpperCAmelCase_    ) for criteria in self    )



							@property
							def 						lowerCAmelCase    (			self							: Union[str, Any]    )   ->Optional[int]:



														"""simple docstring"""



														for stopping_criterium in self:
																					if isinstance(UpperCAmelCase_						,    UpperCAmelCase_    ):
																												return stopping_criterium.max_length
																					elif isinstance(UpperCAmelCase_						,    UpperCAmelCase_    ):
																												return stopping_criterium.max_length
														return None







def   _a			( _SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE			)      ->			StoppingCriteriaList:
							snake_case_          =						stopping_criteria.max_length
							snake_case_          =						deepcopy(_SCREAMING_SNAKE_CASE			)
							if stopping_max_length is not None and stopping_max_length != max_length:
														warnings.warn("""You set different `max_length` for stopping criteria and `max_length` parameter"""			,	_SCREAMING_SNAKE_CASE			)
							elif stopping_max_length is None:
														new_stopping_criteria.append(MaxLengthCriteria(max_length=_SCREAMING_SNAKE_CASE			)			)
							return new_stopping_criteria







"""simple docstring"""
def   _a			( _SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE = 0			)      ->			list:
							snake_case_          =						length or len(_SCREAMING_SNAKE_CASE			)
							snake_case_          =						False
							for i in range(length - 1			):
														if list_data[i] > list_data[i + 1]:
																					snake_case_    ,  snake_case_          =						list_data[i + 1], list_data[i]
																					snake_case_          =						True

							return list_data if not swapped else bubble_sort(_SCREAMING_SNAKE_CASE			,	length - 1			)


if __name__ == "__main__":
				import doctest

				doctest.testmod()



