
"""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


lowerCamelCase__			:							List[Any]      =      logging.get_logger(__name__)


lowerCamelCase__			:							List[Any]      =      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 					lowercase__(					lowerCamelCase__ ):
	'''simple docstring'''

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







			'''simple docstring'''




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





class 					lowercase__(					lowerCamelCase__ ):
	'''simple docstring'''

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







			'''simple docstring'''




			SCREAMING_SNAKE_CASE							:  Optional[Any]					=			max_length
			SCREAMING_SNAKE_CASE							:  List[str]					=			max_position_embeddings







	@add_start_docstrings(__snake_case       )
	def __call__(    self     :List[Any]		,			lowerCamelCase_     :torch.LongTensor		,			lowerCamelCase_     :torch.FloatTensor		,			**lowerCamelCase_     :Union[str, Any]       )				->      List[Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE							:  Any					=			input_ids.shape[-1]
			SCREAMING_SNAKE_CASE							:  Union[str, Any]					=			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 					lowercase__(					lowerCamelCase__ ):
	'''simple docstring'''

	def __init__(    self     :Tuple		,			lowerCamelCase_     :int		,			lowerCamelCase_     :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.'''		,			__snake_case		,			)
			SCREAMING_SNAKE_CASE							:  int					=			start_length
			SCREAMING_SNAKE_CASE							:  Union[str, Any]					=			max_new_tokens
			SCREAMING_SNAKE_CASE							:  Optional[int]					=			start_length + max_new_tokens







	@add_start_docstrings(__snake_case       )
	def __call__(    self     :Union[str, Any]		,			lowerCamelCase_     :torch.LongTensor		,			lowerCamelCase_     :torch.FloatTensor		,			**lowerCamelCase_     :Any       )				->      int:







			'''simple docstring'''




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





class 					lowercase__(					lowerCamelCase__ ):
	'''simple docstring'''

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







			'''simple docstring'''




			SCREAMING_SNAKE_CASE							:  int					=			max_time
			SCREAMING_SNAKE_CASE							:  Dict					=			time.time() if initial_timestamp is None else initial_timestamp







	@add_start_docstrings(__snake_case       )
	def __call__(    self     :Optional[Any]		,			lowerCamelCase_     :torch.LongTensor		,			lowerCamelCase_     :torch.FloatTensor		,			**lowerCamelCase_     :Dict       )				->      Union[str, Any]:







			'''simple docstring'''




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





class 					lowercase__(					lowerCamelCase__ ):
	'''simple docstring'''

	@add_start_docstrings(__snake_case       )
	def __call__(    self     :str		,			lowerCamelCase_     :torch.LongTensor		,			lowerCamelCase_     :torch.FloatTensor		,			**lowerCamelCase_     :List[str]       )				->      int:







			'''simple docstring'''




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







	@property
	def 	__lowerCAmelCase			(    self     :Dict       )				->      List[Any]:







			'''simple docstring'''




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





def __A    ( a_			:				Optional[int]		,		a_			:				Optional[Any]  )->				Union[str, Any]:




		'''simple docstring'''






		SCREAMING_SNAKE_CASE							:  Optional[int]					=			stopping_criteria.max_length
		SCREAMING_SNAKE_CASE							:  Union[str, Any]					=			deepcopy(_A  )
		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'''		,		_A  )
		elif stopping_max_length is None:
				new_stopping_criteria.append(MaxLengthCriteria(max_length=_A  )  )
		return new_stopping_criteria










'''simple docstring'''






from typing import Callable, Optional

from .. import Features
from ..packaged_modules.generator.generator import Generator
from .abc import AbstractDatasetInputStream

class 				a__(   lowerCamelCase__      ):







    def __init__( self					:    int		,     __snake_case					:    Callable		,     __snake_case					:    Optional[Features] = None		,     __snake_case					:    str = None		,     __snake_case					:    bool = False		,     __snake_case					:    bool = False		,     __snake_case					:    Optional[dict] = None		,     __snake_case					:    Optional[int] = None		,     **__snake_case					:    Optional[int]		,     ):
      super().__init__(
          features=__snake_case		,     cache_dir=__snake_case		,     keep_in_memory=__snake_case		,     streaming=__snake_case		,     num_proc=__snake_case		,     **__snake_case		,     )
      a		:   List[Any]	  =	Generator(
          cache_dir=__snake_case		,     features=__snake_case		,     generator=__snake_case		,     gen_kwargs=__snake_case		,     **__snake_case		,     )







    def      lowercase_			( self					:    Any      ):
      # Build iterable dataset
      if self.streaming:
        a		:   Optional[int]	  =	self.builder.as_streaming_dataset(split='train'      )
      # Build regular (map-style) dataset
      else:
        a		:   List[Any]	  =	None
        a		:   Any	  =	None
        a		:   Optional[int]	  =	None
        a		:   List[str]	  =	None

        self.builder.download_and_prepare(
            download_config=__snake_case		,     download_mode=__snake_case		,     verification_mode=__snake_case		,     base_path=__snake_case		,     num_proc=self.num_proc		,     )
        a		:   Dict	  =	self.builder.as_dataset(
            split='train'		,     verification_mode=__snake_case		,     in_memory=self.keep_in_memory      )
      return dataset