


"""simple docstring"""







import enum
import warnings

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
		import tensorflow as tf

		from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

if is_torch_available():
		from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

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




class 			UpperCamelCase_     (			enum.Enum):

	"""simple docstring"""





	snake_case__    :  Dict    =      0
	snake_case__    :  Tuple    =      1




@add_end_docstrings(UpperCamelCase)
class 			UpperCamelCase_     (			UpperCamelCase):

	"""simple docstring"""





	snake_case__    :  Dict    =      "generated"




	def __init__(			self      :    Any      ,      *UpperCAmelCase__      :    Dict      ,      **UpperCAmelCase__      :    int       )	->     Optional[Any]:
						super().__init__(*UpperCAmelCase__      ,      **UpperCAmelCase__       )

						self.check_model_type(
						    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
						    if self.framework == "tf"
						    else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING       )




	def   UpperCAmelCase_				(			self      :    Optional[int]      ,      UpperCAmelCase__      :    Tuple=None      ,      UpperCAmelCase__      :    Dict=None      ,      UpperCAmelCase__      :    List[str]=None      ,      UpperCAmelCase__      :    List[Any]=None      ,      UpperCAmelCase__      :    Tuple=None      ,      UpperCAmelCase__      :    Union[str, Any]=None      ,      **UpperCAmelCase__      :    Optional[int]      ,      )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    {}
						if truncation is not None:
											__SCREAMING_SNAKE_CASE						       =    truncation

						__SCREAMING_SNAKE_CASE						       =    generate_kwargs

						__SCREAMING_SNAKE_CASE						       =    {}
						if return_tensors is not None and return_type is None:
											__SCREAMING_SNAKE_CASE						       =    ReturnType.TENSORS if return_tensors else ReturnType.TEXT
						if return_type is not None:
											__SCREAMING_SNAKE_CASE						       =    return_type

						if clean_up_tokenization_spaces is not None:
											__SCREAMING_SNAKE_CASE						       =    clean_up_tokenization_spaces

						if stop_sequence is not None:
											__SCREAMING_SNAKE_CASE						       =    self.tokenizer.encode(UpperCAmelCase__      ,      add_special_tokens=UpperCAmelCase__       )
											if len(UpperCAmelCase__       ) > 1:
																warnings.warn(
																    "Stopping on a multiple token sequence is not yet supported on transformers. The first token of"
																    " the stop sequence will be used as the stop sequence string in the interim."       )
											__SCREAMING_SNAKE_CASE						       =    stop_sequence_ids[0]

						return preprocess_params, forward_params, postprocess_params




	def   UpperCAmelCase_				(			self      :    List[str]      ,      UpperCAmelCase__      :    int      ,      UpperCAmelCase__      :    int      ,      UpperCAmelCase__      :    int       )	->     Any:
						return True




	def   UpperCAmelCase_				(			self      :    List[str]      ,      *UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    List[str]       )	->     int:
						__SCREAMING_SNAKE_CASE						       =    self.model.config.prefix if self.model.config.prefix is not None else ""
						if isinstance(args[0]      ,      UpperCAmelCase__       ):
											if self.tokenizer.pad_token_id is None:
																raise ValueError("Please make sure that the tokenizer has a pad_token_id when using a batch input"       )
											__SCREAMING_SNAKE_CASE						       =    ([prefix + arg for arg in args[0]],)
											__SCREAMING_SNAKE_CASE						       =    True

						elif isinstance(args[0]      ,      UpperCAmelCase__       ):
											__SCREAMING_SNAKE_CASE						       =    (prefix + args[0],)
											__SCREAMING_SNAKE_CASE						       =    False
						else:
											raise ValueError(
											    F""" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`"""       )
						__SCREAMING_SNAKE_CASE						       =    self.tokenizer(*UpperCAmelCase__      ,      padding=UpperCAmelCase__      ,      truncation=UpperCAmelCase__      ,      return_tensors=self.framework       )
						# This is produced by tokenizers but is an invalid generate kwargs
						if "token_type_ids" in inputs:
											del inputs["token_type_ids"]
						return inputs




	def __call__(			self      :    int      ,      *UpperCAmelCase__      :    Union[str, Any]      ,      **UpperCAmelCase__      :    Optional[Any]       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    super().__call__(*UpperCAmelCase__      ,      **UpperCAmelCase__       )
						if (
						    isinstance(args[0]      ,      UpperCAmelCase__       )
						    and all(isinstance(UpperCAmelCase__      ,      UpperCAmelCase__       ) for el in args[0]       )
						    and all(len(UpperCAmelCase__       ) == 1 for res in result       )
						):
											return [res[0] for res in result]
						return result




	def   UpperCAmelCase_				(			self      :    Union[str, Any]      ,      UpperCAmelCase__      :    List[Any]      ,      UpperCAmelCase__      :    Tuple=TruncationStrategy.DO_NOT_TRUNCATE      ,      **UpperCAmelCase__      :    Union[str, Any]       )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    self._parse_and_tokenize(UpperCAmelCase__      ,      truncation=UpperCAmelCase__      ,      **UpperCAmelCase__       )
						return inputs




	def   UpperCAmelCase_				(			self      :    Optional[int]      ,      UpperCAmelCase__      :    Optional[Any]      ,      **UpperCAmelCase__      :    Tuple       )	->     Union[str, Any]:
						if self.framework == "pt":
											__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    model_inputs["input_ids"].shape
						elif self.framework == "tf":
											__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    tf.shape(model_inputs["input_ids"]       ).numpy()

						__SCREAMING_SNAKE_CASE						       =    generate_kwargs.get("min_length"      ,      self.model.config.min_length       )
						__SCREAMING_SNAKE_CASE						       =    generate_kwargs.get("max_length"      ,      self.model.config.max_length       )
						self.check_inputs(UpperCAmelCase__      ,      generate_kwargs["min_length"]      ,      generate_kwargs["max_length"]       )
						__SCREAMING_SNAKE_CASE						       =    self.model.generate(**UpperCAmelCase__      ,      **UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    output_ids.shape[0]
						if self.framework == "pt":
											__SCREAMING_SNAKE_CASE						       =    output_ids.reshape(UpperCAmelCase__      ,      out_b // in_b      ,      *output_ids.shape[1:]       )
						elif self.framework == "tf":
											__SCREAMING_SNAKE_CASE						       =    tf.reshape(UpperCAmelCase__      ,      (in_b, out_b // in_b, *output_ids.shape[1:])       )
						return {"output_ids": output_ids}




	def   UpperCAmelCase_				(			self      :    int      ,      UpperCAmelCase__      :    Any      ,      UpperCAmelCase__      :    List[Any]=ReturnType.TEXT      ,      UpperCAmelCase__      :    Dict=False       )	->     Optional[Any]:
						__SCREAMING_SNAKE_CASE						       =    []
						for output_ids in model_outputs["output_ids"][0]:
											if return_type == ReturnType.TENSORS:
																__SCREAMING_SNAKE_CASE						       =    {F"""{self.return_name}_token_ids""": output_ids}
											elif return_type == ReturnType.TEXT:
																__SCREAMING_SNAKE_CASE						       =    {
																    F"""{self.return_name}_text""": self.tokenizer.decode(
																        UpperCAmelCase__      ,      skip_special_tokens=UpperCAmelCase__      ,      clean_up_tokenization_spaces=UpperCAmelCase__      ,      )
																}
											records.append(UpperCAmelCase__       )
						return records




@add_end_docstrings(UpperCamelCase)
class 			UpperCamelCase_     (			UpperCamelCase):

	"""simple docstring"""





	snake_case__    :  str    =      "summary"




	def __call__(			self      :    List[Any]      ,      *UpperCAmelCase__      :    int      ,      **UpperCAmelCase__      :    Any       )	->     Dict:
						return super().__call__(*UpperCAmelCase__      ,      **UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    str      ,      UpperCAmelCase__      :    int      ,      UpperCAmelCase__      :    int      ,      UpperCAmelCase__      :    int       )	->     bool:
						if max_length < min_length:
											logger.warning(F"""Your min_length={min_length} must be inferior than your max_length={max_length}."""       )

						if input_length < max_length:
											logger.warning(
											    F"""Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is """
											    "a summarization task, where outputs shorter than the input are typically wanted, you might "
											    F"""consider decreasing max_length manually, e.g. summarizer('...', max_length={input_length//2})"""       )




@add_end_docstrings(UpperCamelCase)
class 			UpperCamelCase_     (			UpperCamelCase):

	"""simple docstring"""





	snake_case__    :  Union[str, Any]    =      "translation"




	def   UpperCAmelCase_				(			self      :    str      ,      UpperCAmelCase__      :    int      ,      UpperCAmelCase__      :    int      ,      UpperCAmelCase__      :    int       )	->     str:
						if input_length > 0.9 * max_length:
											logger.warning(
											    F"""Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider """
											    "increasing your max_length manually, e.g. translator('...', max_length=400)"       )
						return True




	def   UpperCAmelCase_				(			self      :    List[Any]      ,      *UpperCAmelCase__      :    Any      ,      UpperCAmelCase__      :    List[Any]=TruncationStrategy.DO_NOT_TRUNCATE      ,      UpperCAmelCase__      :    Any=None      ,      UpperCAmelCase__      :    Dict=None       )	->     Dict:
						if getattr(self.tokenizer      ,      "_build_translation_inputs"      ,      UpperCAmelCase__       ):
											return self.tokenizer._build_translation_inputs(
											    *UpperCAmelCase__      ,      return_tensors=self.framework      ,      truncation=UpperCAmelCase__      ,      src_lang=UpperCAmelCase__      ,      tgt_lang=UpperCAmelCase__       )
						else:
											return super()._parse_and_tokenize(*UpperCAmelCase__      ,      truncation=UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Dict      ,      UpperCAmelCase__      :    Optional[Any]=None      ,      UpperCAmelCase__      :    int=None      ,      **UpperCAmelCase__      :    Union[str, Any]       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    super()._sanitize_parameters(**UpperCAmelCase__       )
						if src_lang is not None:
											__SCREAMING_SNAKE_CASE						       =    src_lang
						if tgt_lang is not None:
											__SCREAMING_SNAKE_CASE						       =    tgt_lang
						if src_lang is None and tgt_lang is None:
											# Backward compatibility, direct arguments use is preferred.
											__SCREAMING_SNAKE_CASE						       =    kwargs.get("task"      ,      self.task       )
											__SCREAMING_SNAKE_CASE						       =    task.split("_"       )
											if task and len(UpperCAmelCase__       ) == 4:
																# translation, XX, to YY
																__SCREAMING_SNAKE_CASE						       =    items[1]
																__SCREAMING_SNAKE_CASE						       =    items[3]
						return preprocess_params, forward_params, postprocess_params




	def __call__(			self      :    List[str]      ,      *UpperCAmelCase__      :    Dict      ,      **UpperCAmelCase__      :    Tuple       )	->     Any:
						return super().__call__(*UpperCAmelCase__      ,      **UpperCAmelCase__       )




"""simple docstring"""







import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
		import jax

		from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel




class 			UpperCamelCase_     (			unittest.TestCase):

	"""simple docstring"""





	def __init__(			self      :    Optional[Any]      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    Tuple=1_3      ,      UpperCAmelCase__      :    Optional[int]=3_0      ,      UpperCAmelCase__      :    Any=2      ,      UpperCAmelCase__      :    List[str]=3      ,      UpperCAmelCase__      :    Optional[Any]=True      ,      UpperCAmelCase__      :    Optional[int]=True      ,      UpperCAmelCase__      :    int=3_2      ,      UpperCAmelCase__      :    Any=5      ,      UpperCAmelCase__      :    Union[str, Any]=4      ,      UpperCAmelCase__      :    List[str]=3_7      ,      UpperCAmelCase__      :    Tuple="gelu"      ,      UpperCAmelCase__      :    List[str]=0.1      ,      UpperCAmelCase__      :    str=0.1      ,      UpperCAmelCase__      :    Any=1_0      ,      UpperCAmelCase__      :    str=0.02      ,      )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    parent
						__SCREAMING_SNAKE_CASE						       =    batch_size
						__SCREAMING_SNAKE_CASE						       =    image_size
						__SCREAMING_SNAKE_CASE						       =    patch_size
						__SCREAMING_SNAKE_CASE						       =    num_channels
						__SCREAMING_SNAKE_CASE						       =    is_training
						__SCREAMING_SNAKE_CASE						       =    use_labels
						__SCREAMING_SNAKE_CASE						       =    hidden_size
						__SCREAMING_SNAKE_CASE						       =    num_hidden_layers
						__SCREAMING_SNAKE_CASE						       =    num_attention_heads
						__SCREAMING_SNAKE_CASE						       =    intermediate_size
						__SCREAMING_SNAKE_CASE						       =    hidden_act
						__SCREAMING_SNAKE_CASE						       =    hidden_dropout_prob
						__SCREAMING_SNAKE_CASE						       =    attention_probs_dropout_prob
						__SCREAMING_SNAKE_CASE						       =    type_sequence_label_size
						__SCREAMING_SNAKE_CASE						       =    initializer_range

						# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
						__SCREAMING_SNAKE_CASE						       =    (image_size // patch_size) ** 2
						__SCREAMING_SNAKE_CASE						       =    num_patches + 1




	def   UpperCAmelCase_				(			self      :    Union[str, Any]       )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

						__SCREAMING_SNAKE_CASE						       =    ViTConfig(
						    image_size=self.image_size      ,      patch_size=self.patch_size      ,      num_channels=self.num_channels      ,      hidden_size=self.hidden_size      ,      num_hidden_layers=self.num_hidden_layers      ,      num_attention_heads=self.num_attention_heads      ,      intermediate_size=self.intermediate_size      ,      hidden_act=self.hidden_act      ,      hidden_dropout_prob=self.hidden_dropout_prob      ,      attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,      is_decoder=UpperCAmelCase__      ,      initializer_range=self.initializer_range      ,      )

						return config, pixel_values




	def   UpperCAmelCase_				(			self      :    Tuple      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    List[str]       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    FlaxViTModel(config=UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__       )
						# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
						__SCREAMING_SNAKE_CASE						       =    (self.image_size, self.image_size)
						__SCREAMING_SNAKE_CASE						       =    (self.patch_size, self.patch_size)
						__SCREAMING_SNAKE_CASE						       =    (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
						self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, num_patches + 1, self.hidden_size)       )




	def   UpperCAmelCase_				(			self      :    str      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    Tuple       )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    self.type_sequence_label_size
						__SCREAMING_SNAKE_CASE						       =    FlaxViTForImageClassification(config=UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__       )
						self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.type_sequence_label_size)       )

						# test greyscale images
						__SCREAMING_SNAKE_CASE						       =    1
						__SCREAMING_SNAKE_CASE						       =    FlaxViTForImageClassification(UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Union[str, Any]       )	->     List[Any]:
						__SCREAMING_SNAKE_CASE						       =    self.prepare_config_and_inputs()
						(
						    (
						    __SCREAMING_SNAKE_CASE
						)       ,  (
						    __SCREAMING_SNAKE_CASE
						)       ,  
						)						       =    config_and_inputs
						__SCREAMING_SNAKE_CASE						       =    {"pixel_values": pixel_values}
						return config, inputs_dict




@require_flax
class 			UpperCamelCase_     (			UpperCamelCase      ,  unittest.TestCase):

	"""simple docstring"""





	snake_case__    :  List[str]    =      (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()




	def   UpperCAmelCase_				(			self      :    int       )	->     None:
						__SCREAMING_SNAKE_CASE						       =    FlaxViTModelTester(self       )
						__SCREAMING_SNAKE_CASE						       =    ConfigTester(self      ,      config_class=UpperCAmelCase__      ,      has_text_modality=UpperCAmelCase__      ,      hidden_size=3_7       )




	def   UpperCAmelCase_				(			self      :    int       )	->     Union[str, Any]:
						self.config_tester.run_common_tests()




	def   UpperCAmelCase_				(			self      :    Tuple       )	->     List[str]:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Dict       )	->     Dict:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Tuple       )	->     Any:
						__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											__SCREAMING_SNAKE_CASE						       =    model_class(UpperCAmelCase__       )
											__SCREAMING_SNAKE_CASE						       =    inspect.signature(model.__call__       )
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											__SCREAMING_SNAKE_CASE						       =    [*signature.parameters.keys()]

											__SCREAMING_SNAKE_CASE						       =    ["pixel_values"]
											self.assertListEqual(arg_names[:1]      ,      UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Any       )	->     List[Any]:
						__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											with self.subTest(model_class.__name__       ):
																__SCREAMING_SNAKE_CASE						       =    self._prepare_for_class(UpperCAmelCase__      ,      UpperCAmelCase__       )
																__SCREAMING_SNAKE_CASE						       =    model_class(UpperCAmelCase__       )

																@jax.jit
																def model_jitted(UpperCAmelCase__      :    Optional[int]      ,      **UpperCAmelCase__      :    Any       ):
																					return model(pixel_values=UpperCAmelCase__      ,      **UpperCAmelCase__       )

																with self.subTest("JIT Enabled"       ):
																					__SCREAMING_SNAKE_CASE						       =    model_jitted(**UpperCAmelCase__       ).to_tuple()

																with self.subTest("JIT Disabled"       ):
																					with jax.disable_jit():
																										__SCREAMING_SNAKE_CASE						       =    model_jitted(**UpperCAmelCase__       ).to_tuple()

																self.assertEqual(len(UpperCAmelCase__       )      ,      len(UpperCAmelCase__       )       )
																for jitted_output, output in zip(UpperCAmelCase__      ,      UpperCAmelCase__       ):
																					self.assertEqual(jitted_output.shape      ,      output.shape       )




	@slow
	def   UpperCAmelCase_				(			self      :    int       )	->     Union[str, Any]:
						for model_class_name in self.all_model_classes:
											__SCREAMING_SNAKE_CASE						       =    model_class_name.from_pretrained("google/vit-base-patch16-224"       )
											__SCREAMING_SNAKE_CASE						       =    model(np.ones((1, 3, 2_2_4, 2_2_4)       )       )
											self.assertIsNotNone(UpperCAmelCase__       )
