






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


__snake_case      :      List[str]             =       logging.get_logger(__name__)


__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       __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"""
												lowerCAmelCase__        =  max_length
												lowerCAmelCase__        =  max_position_embeddings







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






												"""simple docstring"""
												lowerCAmelCase__        =  input_ids.shape[-1]
												lowerCAmelCase__        =  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       ,  )
												lowerCAmelCase__        =  start_length
												lowerCAmelCase__        =  max_new_tokens
												lowerCAmelCase__        =  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"""
												lowerCAmelCase__        =  max_time
												lowerCAmelCase__        =  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  UpperCamelCase__ (		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 		_UpperCamelCase				(      UpperCamelCase_    :   StoppingCriteriaList      ,    UpperCamelCase_    :   int						)					->				Tuple:

						"""simple docstring"""



						lowerCAmelCase__        =  stopping_criteria.max_length
						lowerCAmelCase__        =  deepcopy(lowerCamelCase__						)
						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'      ,    lowerCamelCase__						)
						elif stopping_max_length is None:
												new_stopping_criteria.append(MaxLengthCriteria(max_length=lowerCamelCase__						)						)
						return new_stopping_criteria













from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


__snake_case      :      Any             =       logging.get_logger(__name__)


class       __SCREAMING_SNAKE_CASE   (				__lowercase):
						_SCREAMING_SNAKE_CASE		:    List[Any]						=   ['''pixel_values''']






						def __init__(		self       ,  _UpperCamelCase = True       ,  _UpperCamelCase = None       ,  _UpperCamelCase = PILImageResampling.BILINEAR       ,  _UpperCamelCase = True       ,  _UpperCamelCase = None       ,  _UpperCamelCase = True       ,  _UpperCamelCase = 1 / 2_55       ,  _UpperCamelCase = True       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  **_UpperCamelCase       ,  ):






												"""simple docstring"""
												super().__init__(**_UpperCamelCase				)
												lowerCAmelCase__        =  size if size is not None else {'shortest_edge': 2_56}
												lowerCAmelCase__        =  get_size_dict(_UpperCamelCase       ,  default_to_square=_UpperCamelCase				)
												lowerCAmelCase__        =  crop_size if crop_size is not None else {'height': 2_24, 'width': 2_24}
												lowerCAmelCase__        =  get_size_dict(_UpperCamelCase				)
												lowerCAmelCase__        =  do_resize
												lowerCAmelCase__        =  size
												lowerCAmelCase__        =  resample
												lowerCAmelCase__        =  do_center_crop
												lowerCAmelCase__        =  crop_size
												lowerCAmelCase__        =  do_rescale
												lowerCAmelCase__        =  rescale_factor
												lowerCAmelCase__        =  do_normalize
												lowerCAmelCase__        =  image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
												lowerCAmelCase__        =  image_std if image_std is not None else IMAGENET_STANDARD_STD






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase = PILImageResampling.BICUBIC       ,  _UpperCamelCase = None       ,  **_UpperCamelCase       ,  ):






												"""simple docstring"""
												lowerCAmelCase__        =  get_size_dict(_UpperCamelCase       ,  default_to_square=_UpperCamelCase				)
												if "shortest_edge" not in size:
																		raise ValueError(F"The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"				)
												lowerCAmelCase__        =  get_resize_output_image_size(_UpperCamelCase       ,  size=size['shortest_edge']       ,  default_to_square=_UpperCamelCase				)
												return resize(_UpperCamelCase       ,  size=_UpperCamelCase       ,  resample=_UpperCamelCase       ,  data_format=_UpperCamelCase       ,  **_UpperCamelCase				)






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase = None       ,  **_UpperCamelCase       ,  ):






												"""simple docstring"""
												lowerCAmelCase__        =  get_size_dict(_UpperCamelCase				)
												return center_crop(_UpperCamelCase       ,  size=(size['height'], size['width'])       ,  data_format=_UpperCamelCase       ,  **_UpperCamelCase				)






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase = None       ,  **_UpperCamelCase				):






												"""simple docstring"""
												return rescale(_UpperCamelCase       ,  scale=_UpperCamelCase       ,  data_format=_UpperCamelCase       ,  **_UpperCamelCase				)






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase = None       ,  **_UpperCamelCase       ,  ):






												"""simple docstring"""
												return normalize(_UpperCamelCase       ,  mean=_UpperCamelCase       ,  std=_UpperCamelCase       ,  data_format=_UpperCamelCase       ,  **_UpperCamelCase				)







						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = ChannelDimension.FIRST       ,  **_UpperCamelCase       ,  ):






												"""simple docstring"""
												lowerCAmelCase__        =  do_resize if do_resize is not None else self.do_resize
												lowerCAmelCase__        =  size if size is not None else self.size
												lowerCAmelCase__        =  get_size_dict(_UpperCamelCase       ,  default_to_square=_UpperCamelCase				)
												lowerCAmelCase__        =  resample if resample is not None else self.resample
												lowerCAmelCase__        =  do_center_crop if do_center_crop is not None else self.do_center_crop
												lowerCAmelCase__        =  crop_size if crop_size is not None else self.crop_size
												lowerCAmelCase__        =  get_size_dict(_UpperCamelCase				)
												lowerCAmelCase__        =  do_rescale if do_rescale is not None else self.do_rescale
												lowerCAmelCase__        =  rescale_factor if rescale_factor is not None else self.rescale_factor
												lowerCAmelCase__        =  do_normalize if do_normalize is not None else self.do_normalize
												lowerCAmelCase__        =  image_mean if image_mean is not None else self.image_mean
												lowerCAmelCase__        =  image_std if image_std is not None else self.image_std

												lowerCAmelCase__        =  make_list_of_images(_UpperCamelCase				)

												if not valid_images(_UpperCamelCase				):
																		raise ValueError(
																		    'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
																		    'torch.Tensor, tf.Tensor or jax.ndarray.'				)

												if do_resize and size is None:
																		raise ValueError('Size must be specified if do_resize is True.'				)

												if do_center_crop and crop_size is None:
																		raise ValueError('Crop size must be specified if do_center_crop is True.'				)

												if do_rescale and rescale_factor is None:
																		raise ValueError('Rescale factor must be specified if do_rescale is True.'				)

												if do_normalize and (image_mean is None or image_std is None):
																		raise ValueError('Image mean and std must be specified if do_normalize is True.'				)

												# All transformations expect numpy arrays.
												lowerCAmelCase__        =  [to_numpy_array(_UpperCamelCase				) for image in images]

												if do_resize:
																		lowerCAmelCase__        =  [self.resize(image=_UpperCamelCase       ,  size=_UpperCamelCase       ,  resample=_UpperCamelCase				) for image in images]

												if do_center_crop:
																		lowerCAmelCase__        =  [self.center_crop(image=_UpperCamelCase       ,  size=_UpperCamelCase				) for image in images]

												if do_rescale:
																		lowerCAmelCase__        =  [self.rescale(image=_UpperCamelCase       ,  scale=_UpperCamelCase				) for image in images]

												if do_normalize:
																		lowerCAmelCase__        =  [self.normalize(image=_UpperCamelCase       ,  mean=_UpperCamelCase       ,  std=_UpperCamelCase				) for image in images]

												lowerCAmelCase__        =  [to_channel_dimension_format(_UpperCamelCase       ,  _UpperCamelCase				) for image in images]

												lowerCAmelCase__        =  {'pixel_values': images}
												return BatchFeature(data=_UpperCamelCase       ,  tensor_type=_UpperCamelCase				)





