





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


lowercase__   :				Union[str, Any]      =   logging.get_logger(__name__)


lowercase__   :				Tuple      =   r'''
    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.

'''


class 			SCREAMING_SNAKE_CASE   (a__							):


     @add_start_docstrings(_UpperCAmelCase)
     def __call__(						self						,     _UpperCAmelCase						,     _UpperCAmelCase						,     **_UpperCAmelCase):

          '''simple docstring'''






          raise NotImplementedError('StoppingCriteria needs to be subclassed')




class 			SCREAMING_SNAKE_CASE   (a__							):


     def __init__(						self						,     _UpperCAmelCase						,     _UpperCAmelCase = None):

          '''simple docstring'''






          __A						:			Optional[Any]								=	max_length
          __A						:			Any								=	max_position_embeddings


     @add_start_docstrings(_UpperCAmelCase)
     def __call__(						self						,     _UpperCAmelCase						,     _UpperCAmelCase						,     **_UpperCAmelCase):

          '''simple docstring'''






          __A						:			int								=	input_ids.shape[-1]
          __A						:			Optional[int]								=	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 			SCREAMING_SNAKE_CASE   (a__							):


     def __init__(						self						,     _UpperCAmelCase						,     _UpperCAmelCase):

          '''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						,     )
          __A						:			Union[str, Any]								=	start_length
          __A						:			int								=	max_new_tokens
          __A						:			str								=	start_length + max_new_tokens


     @add_start_docstrings(_UpperCAmelCase)
     def __call__(						self						,     _UpperCAmelCase						,     _UpperCAmelCase						,     **_UpperCAmelCase):

          '''simple docstring'''






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




class 			SCREAMING_SNAKE_CASE   (a__							):


     def __init__(						self						,     _UpperCAmelCase						,     _UpperCAmelCase = None):

          '''simple docstring'''






          __A						:			Optional[int]								=	max_time
          __A						:			Optional[int]								=	time.time() if initial_timestamp is None else initial_timestamp


     @add_start_docstrings(_UpperCAmelCase)
     def __call__(						self						,     _UpperCAmelCase						,     _UpperCAmelCase						,     **_UpperCAmelCase):

          '''simple docstring'''






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




class 			SCREAMING_SNAKE_CASE   (a__							):


     @add_start_docstrings(_UpperCAmelCase)
     def __call__(						self						,     _UpperCAmelCase						,     _UpperCAmelCase						,     **_UpperCAmelCase):

          '''simple docstring'''






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


     @property
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''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 	_lowerCAmelCase      (  __snake_case  :			StoppingCriteriaList			,							__snake_case  :			int      )					->					StoppingCriteriaList:
     __A						:			int								=	stopping_criteria.max_length
     __A						:			Dict								=	deepcopy(__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'			,							__snake_case      )
     elif stopping_max_length is None:
          new_stopping_criteria.append(MaxLengthCriteria(max_length=__snake_case      )      )
     return new_stopping_criteria






'''simple docstring'''




from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding


class 			SCREAMING_SNAKE_CASE   (a__							):
     lowerCAmelCase		      =	['''image_processor''', '''tokenizer''']
     lowerCAmelCase		      =	'''AutoImageProcessor'''
     lowerCAmelCase		      =	'''AutoTokenizer'''


     def __init__(						self						,     _UpperCAmelCase						,     _UpperCAmelCase):

          '''simple docstring'''






          super().__init__(_UpperCAmelCase						,     _UpperCAmelCase)
          __A						:			Tuple								=	self.image_processor


     def __call__(						self						,     _UpperCAmelCase=None						,     _UpperCAmelCase=None						,     _UpperCAmelCase=None						,     **_UpperCAmelCase):

          '''simple docstring'''






          if text is None and images is None:
               raise ValueError('You have to specify either text or images. Both cannot be none.')

          if text is not None:
               __A						:			Any								=	self.tokenizer(_UpperCAmelCase						,     return_tensors=_UpperCAmelCase						,     **_UpperCAmelCase)

          if images is not None:
               __A						:			Tuple								=	self.image_processor(_UpperCAmelCase						,     return_tensors=_UpperCAmelCase						,     **_UpperCAmelCase)

          if text is not None and images is not None:
               __A						:			Optional[int]								=	image_features.pixel_values
               return encoding
          elif text is not None:
               return encoding
          else:
               return BatchEncoding(data=dict(**_UpperCAmelCase)						,     tensor_type=_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self						,     *_UpperCAmelCase						,     **_UpperCAmelCase):

          '''simple docstring'''






          return self.tokenizer.batch_decode(*_UpperCAmelCase						,     **_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self						,     *_UpperCAmelCase						,     **_UpperCAmelCase):

          '''simple docstring'''






          return self.tokenizer.decode(*_UpperCAmelCase						,     **_UpperCAmelCase)


     @property
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          return ["input_ids", "attention_mask", "pixel_values"]