


"""simple docstring"""


import math
from typing import Dict, Iterable, List, Optional, Tuple, Union

import numpy as np

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


if is_torch_available():
		import torch

if is_vision_available():
		import PIL


a_       							=    logging.get_logger(__name__)






def      a__ (				__lowercase     ,			__lowercase     ,			__lowercase     ,			__lowercase   )	->	Tuple[int, int]:
				def constraint_to_multiple_of(__lowercase     ,			__lowercase     ,			__lowercase=0     ,			__lowercase=None   ):
								_A					  =			round(val / multiple   ) * multiple

								if max_val is not None and x > max_val:
												_A					  =			math.floor(val / multiple   ) * multiple

								if x < min_val:
												_A					  =			math.ceil(val / multiple   ) * multiple

								return x

				_A					  =			(output_size, output_size) if isinstance(__lowercase     ,			__lowercase   ) else output_size

				_A  ,		_A					  =			get_image_size(__lowercase   )
				_A  ,		_A					  =			output_size

				# determine new height and width
				_A					  =			output_height / input_height
				_A					  =			output_width / input_width

				if keep_aspect_ratio:
								# scale as little as possible
								if abs(1 - scale_width   ) < abs(1 - scale_height   ):
												# fit width
												_A					  =			scale_width
								else:
												# fit height
												_A					  =			scale_height

				_A					  =			constraint_to_multiple_of(scale_height * input_height     ,			multiple=__lowercase   )
				_A					  =			constraint_to_multiple_of(scale_width * input_width     ,			multiple=__lowercase   )

				return (new_height, new_width)




class snake_case      (  _UpperCamelCase):
		__UpperCamelCase           =			['pixel_values']







		def __init__(					self      :       Any    ,   a__      :       bool = True    ,   a__      :       Dict[str, int] = None    ,   a__      :       PILImageResampling = PILImageResampling.BILINEAR    ,   a__      :       bool = False    ,   a__      :       int = 1    ,   a__      :       bool = True    ,   a__      :       Union[int, float] = 1 / 2_55    ,   a__      :       bool = True    ,   a__      :       Optional[Union[float, List[float]]] = None    ,   a__      :       Optional[Union[float, List[float]]] = None    ,   **a__      :       Optional[Any]    ,   )       ->						None:





						'''simple docstring'''







						super().__init__(**a__		)
						_A					  =			size if size is not None else {"height": 3_84, "width": 3_84}
						_A					  =			get_size_dict(a__		)
						_A					  =			do_resize
						_A					  =			size
						_A					  =			keep_aspect_ratio
						_A					  =			ensure_multiple_of
						_A					  =			resample
						_A					  =			do_rescale
						_A					  =			rescale_factor
						_A					  =			do_normalize
						_A					  =			image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
						_A					  =			image_std if image_std is not None else IMAGENET_STANDARD_STD







		def  a_ (					self      :       int    ,   a__      :       np.ndarray    ,   a__      :       Dict[str, int]    ,   a__      :       bool = False    ,   a__      :       int = 1    ,   a__      :       PILImageResampling = PILImageResampling.BICUBIC    ,   a__      :       Optional[Union[str, ChannelDimension]] = None    ,   **a__      :       List[str]    ,   )       ->						np.ndarray:





						'''simple docstring'''







						_A					  =			get_size_dict(a__		)
						if "height" not in size or "width" not in size:
										raise ValueError(F"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}"""		)
						_A					  =			get_resize_output_image_size(
						    a__    ,   output_size=(size["height"], size["width"])    ,   keep_aspect_ratio=a__    ,   multiple=a__    ,   )
						return resize(a__    ,   size=a__    ,   resample=a__    ,   data_format=a__    ,   **a__		)







		def  a_ (					self      :       int    ,   a__      :       np.ndarray    ,   a__      :       Union[int, float]    ,   a__      :       Optional[Union[str, ChannelDimension]] = None    ,   **a__      :       int    ,   )       ->						Any:





						'''simple docstring'''







						return rescale(a__    ,   scale=a__    ,   data_format=a__    ,   **a__		)







		def  a_ (					self      :       List[Any]    ,   a__      :       np.ndarray    ,   a__      :       Union[float, List[float]]    ,   a__      :       Union[float, List[float]]    ,   a__      :       Optional[Union[str, ChannelDimension]] = None    ,   **a__      :       Dict    ,   )       ->						np.ndarray:





						'''simple docstring'''







						return normalize(a__    ,   mean=a__    ,   std=a__    ,   data_format=a__    ,   **a__		)







		def  a_ (					self      :       Dict    ,   a__      :       ImageInput    ,   a__      :       bool = None    ,   a__      :       int = None    ,   a__      :       bool = None    ,   a__      :       int = None    ,   a__      :       PILImageResampling = None    ,   a__      :       bool = None    ,   a__      :       float = None    ,   a__      :       bool = None    ,   a__      :       Optional[Union[float, List[float]]] = None    ,   a__      :       Optional[Union[float, List[float]]] = None    ,   a__      :       Optional[Union[str, TensorType]] = None    ,   a__      :       ChannelDimension = ChannelDimension.FIRST    ,   **a__      :       List[str]    ,   )       ->						PIL.Image.Image:





						'''simple docstring'''







						_A					  =			do_resize if do_resize is not None else self.do_resize
						_A					  =			size if size is not None else self.size
						_A					  =			get_size_dict(a__		)
						_A					  =			keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio
						_A					  =			ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of
						_A					  =			resample if resample is not None else self.resample
						_A					  =			do_rescale if do_rescale is not None else self.do_rescale
						_A					  =			rescale_factor if rescale_factor is not None else self.rescale_factor
						_A					  =			do_normalize if do_normalize is not None else self.do_normalize
						_A					  =			image_mean if image_mean is not None else self.image_mean
						_A					  =			image_std if image_std is not None else self.image_std

						_A					  =			make_list_of_images(a__		)

						if not valid_images(a__		):
										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 or resample is None:
										raise ValueError("Size and resample must be specified if do_resize 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.
						_A					  =			[to_numpy_array(a__		) for image in images]

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

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

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

						_A					  =			[to_channel_dimension_format(a__    ,   a__		) for image in images]

						_A					  =			{"pixel_values": images}
						return BatchFeature(data=a__    ,   tensor_type=a__		)







		def  a_ (					self      :       int    ,   a__      :       str    ,   a__      :       List[Tuple] = None		)       ->						str:





						'''simple docstring'''







						_A					  =			outputs.logits

						# Resize logits and compute semantic segmentation maps
						if target_sizes is not None:
										if len(a__		) != len(a__		):
														raise ValueError(
														    "Make sure that you pass in as many target sizes as the batch dimension of the logits"		)

										if is_torch_tensor(a__		):
														_A					  =			target_sizes.numpy()

										_A					  =			[]

										for idx in range(len(a__		)		):
														_A					  =			torch.nn.functional.interpolate(
														    logits[idx].unsqueeze(dim=0		)    ,   size=target_sizes[idx]    ,   mode="bilinear"    ,   align_corners=a__		)
														_A					  =			resized_logits[0].argmax(dim=0		)
														semantic_segmentation.append(a__		)
						else:
										_A					  =			logits.argmax(dim=1		)
										_A					  =			[semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]		)]

						return semantic_segmentation



"""simple docstring"""


import os
import re
import warnings
from shutil import copyfile
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer


if TYPE_CHECKING:
		from ...tokenization_utils_base import TextInput
from ...utils import logging


a_       							=    logging.get_logger(__name__)

a_       							=    {"vocab_file": "spiece.model"}

a_       							=    {
    "vocab_file": {
        "t5-small": "https://huggingface.co/t5-small/resolve/main/spiece.model",
        "t5-base": "https://huggingface.co/t5-base/resolve/main/spiece.model",
        "t5-large": "https://huggingface.co/t5-large/resolve/main/spiece.model",
        "t5-3b": "https://huggingface.co/t5-3b/resolve/main/spiece.model",
        "t5-11b": "https://huggingface.co/t5-11b/resolve/main/spiece.model",
    }
}


# TODO(PVP) - this should be removed in Transformers v5
a_       							=    {
    "t5-small": 5_12,
    "t5-base": 5_12,
    "t5-large": 5_12,
    "t5-3b": 5_12,
    "t5-11b": 5_12,
}

a_       							=    "▁"
class snake_case      (  _UpperCamelCase):
		__UpperCamelCase           =			VOCAB_FILES_NAMES
		__UpperCamelCase           =			PRETRAINED_VOCAB_FILES_MAP
		__UpperCamelCase           =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		__UpperCamelCase           =			['input_ids', 'attention_mask']







		def __init__(					self      :       List[str]    ,   a__      :       Optional[int]    ,   a__      :       Union[str, Any]="</s>"    ,   a__      :       Union[str, Any]="<unk>"    ,   a__      :       str="<pad>"    ,   a__      :       Optional[int]=1_00    ,   a__      :       List[Any]=None    ,   a__      :       Optional[Dict[str, Any]] = None    ,   a__      :       Any=True    ,   **a__      :       Optional[int]    ,   )       ->						None:





						'''simple docstring'''







						if extra_ids > 0 and additional_special_tokens is None:
										_A					  =			[F"""<extra_id_{i}>""" for i in range(a__		)]
						elif extra_ids > 0 and additional_special_tokens is not None:
										# Check that we have the right number of extra_id special tokens
										_A					  =			len(set(filter(lambda a__		: bool("extra_id" in str(a__		)		)    ,   a__		)		)		)
										if extra_tokens != extra_ids:
														raise ValueError(
														    F"""Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are"""
														    " provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids"
														    " tokens"		)
						if legacy:
										logger.warning_once(
										    F"""You are using the legacy behaviour of the {self.__class__}. This means that tokens that come after special tokens will not be properly handled. We recommend you to"""
										    " read the related pull request available at https://github.com/huggingface/transformers/pull/24565"		)

						_A					  =			legacy
						_A					  =			{} if sp_model_kwargs is None else sp_model_kwargs

						super().__init__(
						    eos_token=a__    ,   unk_token=a__    ,   pad_token=a__    ,   extra_ids=a__    ,   additional_special_tokens=a__    ,   sp_model_kwargs=self.sp_model_kwargs    ,   legacy=a__    ,   **a__    ,   )

						_A					  =			vocab_file
						_A					  =			extra_ids

						_A					  =			spm.SentencePieceProcessor(**self.sp_model_kwargs		)
						self.sp_model.Load(a__		)







		@staticmethod
		def  a_ (					a__      :       List[str]    ,   a__      :       Optional[int]    ,   a__      :       Tuple		)       ->						Tuple:





						'''simple docstring'''







						if pretrained_model_name_or_path in TaTokenizer.max_model_input_sizes:
										_A					  =			TaTokenizer.max_model_input_sizes[pretrained_model_name_or_path]
										if init_max_model_length is not None and init_max_model_length != max_model_length:
														return init_max_model_length
										elif init_max_model_length is None:
														warnings.warn(
														    "This tokenizer was incorrectly instantiated with a model max length of"
														    F""" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this"""
														    " behavior is kept to avoid breaking backwards compatibility when padding/encoding with"
														    " `truncation is True`.\n- Be aware that you SHOULD NOT rely on"
														    F""" {pretrained_model_name_or_path} automatically truncating your input to"""
														    F""" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences"""
														    F""" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with"""
														    " `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please"
														    " instantiate this tokenizer with `model_max_length` set to your preferred value."    ,   a__    ,   )

						return max_model_length







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





						'''simple docstring'''







						return self.sp_model.get_piece_size() + self._extra_ids







		def  a_ (					self      :       Dict		)       ->						Optional[Any]:





						'''simple docstring'''







						_A					  =			{self.convert_ids_to_tokens(a__		): i for i in range(self.vocab_size		)}
						vocab.update(self.added_tokens_encoder		)
						return vocab







		def  a_ (					self      :       Optional[Any]    ,   a__      :       List[int]    ,   a__      :       Optional[List[int]] = None    ,   a__      :       bool = False		)       ->						List[int]:





						'''simple docstring'''







						if already_has_special_tokens:
										return super().get_special_tokens_mask(
										    token_ids_a=a__    ,   token_ids_a=a__    ,   already_has_special_tokens=a__		)

						# normal case: some special tokens
						if token_ids_a is None:
										return ([0] * len(a__		)) + [1]
						return ([0] * len(a__		)) + [1] + ([0] * len(a__		)) + [1]







		def  a_ (					self      :       List[str]		)       ->						List[str]:





						'''simple docstring'''







						return list(
						    set(filter(lambda a__		: bool(re.search(r"<extra_id_\d+>"    ,   a__		)		) is not None    ,   self.additional_special_tokens		)		)		)







		def  a_ (					self      :       str		)       ->						List[Any]:





						'''simple docstring'''







						return [self._convert_token_to_id(a__		) for token in self.get_sentinel_tokens()]







		def  a_ (					self      :       List[Any]    ,   a__      :       List[int]		)       ->						List[int]:





						'''simple docstring'''







						if len(a__		) > 0 and token_ids[-1] == self.eos_token_id:
										warnings.warn(
										    F"""This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated"""
										    " eos tokens being added."		)
										return token_ids
						else:
										return token_ids + [self.eos_token_id]







		def  a_ (					self      :       int    ,   a__      :       List[int]    ,   a__      :       Optional[List[int]] = None		)       ->						List[int]:





						'''simple docstring'''







						_A					  =			[self.eos_token_id]

						if token_ids_a is None:
										return len(token_ids_a + eos		) * [0]
						return len(token_ids_a + eos + token_ids_a + eos		) * [0]







		def  a_ (					self      :       Union[str, Any]    ,   a__      :       List[int]    ,   a__      :       Optional[List[int]] = None		)       ->						List[int]:





						'''simple docstring'''







						_A					  =			self._add_eos_if_not_present(a__		)
						if token_ids_a is None:
										return token_ids_a
						else:
										_A					  =			self._add_eos_if_not_present(a__		)
										return token_ids_a + token_ids_a







		def __getstate__(					self      :       Dict		)       ->						Union[str, Any]:





						'''simple docstring'''







						_A					  =			self.__dict__.copy()
						_A					  =			None
						return state







		def __setstate__(					self      :       int    ,   a__      :       Optional[int]		)       ->						Union[str, Any]:





						'''simple docstring'''







						_A					  =			d

						# for backward compatibility
						if not hasattr(self    ,   "sp_model_kwargs"		):
										_A					  =			{}

						_A					  =			spm.SentencePieceProcessor(**self.sp_model_kwargs		)
						self.sp_model.Load(self.vocab_file		)







		def  a_ (					self      :       int    ,   a__      :       "TextInput"    ,   **a__      :       List[str]		)       ->						List[str]:





						'''simple docstring'''







						if not self.legacy:
										_A					  =			SPIECE_UNDERLINE + text.replace(a__    ,   " "		)
						return super().tokenize(a__    ,   **a__		)







		def  a_ (					self      :       str    ,   a__      :       Dict    ,   **a__      :       Optional[int]		)       ->						Any:





						'''simple docstring'''







						if not self.legacy:
										_A					  =			text.startswith(a__		)
										if is_first:
														_A					  =			text[1:]

						_A					  =			self.sp_model.encode(a__    ,   out_type=a__		)

						if not self.legacy and not is_first and not text.startswith(" "		) and tokens[0].startswith(a__		):
										_A					  =			([tokens[0][1:]] if len(tokens[0]		) > 1 else []) + tokens[1:]
						return tokens







		def  a_ (					self      :       int    ,   a__      :       List[Any]		)       ->						List[str]:





						'''simple docstring'''







						if token.startswith("<extra_id_"		):
										_A					  =			re.match(r"<extra_id_(\d+)>"    ,   a__		)
										_A					  =			int(match.group(1		)		)
										return self.vocab_size - num - 1
						return self.sp_model.piece_to_id(a__		)







		def  a_ (					self      :       Dict    ,   a__      :       Union[str, Any]		)       ->						Any:





						'''simple docstring'''







						if index < self.sp_model.get_piece_size():
										_A					  =			self.sp_model.IdToPiece(a__		)
						else:
										_A					  =			F"""<extra_id_{self.vocab_size - 1 - index}>"""
						return token







		def  a_ (					self      :       Optional[int]    ,   a__      :       Tuple		)       ->						List[str]:





						'''simple docstring'''







						_A					  =			[]
						_A					  =			""
						_A					  =			False
						for token in tokens:
										# make sure that special tokens are not decoded using sentencepiece model
										if token in self.all_special_tokens:
														if not prev_is_special:
																		out_string += " "
														out_string += self.sp_model.decode(a__		) + token
														_A					  =			True
														_A					  =			[]
										else:
														current_sub_tokens.append(a__		)
														_A					  =			False
						out_string += self.sp_model.decode(a__		)
						return out_string.strip()







		def  a_ (					self      :       Dict    ,   a__      :       str    ,   a__      :       Optional[str] = None		)       ->						Tuple[str]:





						'''simple docstring'''







						if not os.path.isdir(a__		):
										logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""		)
										return
						_A					  =			os.path.join(
						    a__    ,   (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]		)

						if os.path.abspath(self.vocab_file		) != os.path.abspath(a__		) and os.path.isfile(self.vocab_file		):
										copyfile(self.vocab_file    ,   a__		)
						elif not os.path.isfile(self.vocab_file		):
										with open(a__    ,   "wb"		) as fi:
														_A					  =			self.sp_model.serialized_model_proto()
														fi.write(a__		)

						return (out_vocab_file,)