




from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


UpperCamelCase__         =      logging.get_logger(__name__)
class       a__   ( snake_case__				):






						def     __SCREAMING_SNAKE_CASE(       self		,      _A      ):




											"""simple docstring"""



											if isinstance(_A		,      _A      ):
																__lowerCAmelCase           =	[label.strip() for label in labels.split(","      ) if label.strip()]
											return labels






						def __call__(       self		,      _A		,      _A		,      _A      ):




											"""simple docstring"""



											if len(_A      ) == 0 or len(_A      ) == 0:
																raise ValueError("You must include at least one label and at least one sequence."      )
											if hypothesis_template.format(labels[0]      ) == hypothesis_template:
																raise ValueError(
																    (
																        "The provided hypothesis_template \"{}\" was not able to be formatted with the target labels. "
																        "Make sure the passed template includes formatting syntax such as {{}} where the label should go."
																    ).format(_A      )      )

											if isinstance(_A		,      _A      ):
																__lowerCAmelCase           =	[sequences]

											__lowerCAmelCase           =	[]
											for sequence in sequences:
																sequence_pairs.extend([[sequence, hypothesis_template.format(_A      )] for label in labels]      )

											return sequence_pairs, sequences






@add_end_docstrings(snake_case__				)
class       a__   ( snake_case__				):






						def __init__(       self		,      _A=ZeroShotClassificationArgumentHandler()		,      *_A		,      **_A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	args_parser
											super().__init__(*_A		,      **_A      )
											if self.entailment_id == -1:
																logger.warning(
																    "Failed to determine 'entailment' label id from the label2id mapping in the model config. Setting to "
																    "-1. Define a descriptive label2id mapping in the model config to ensure correct outputs."      )






						@property
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											for label, ind in self.model.config.labelaid.items():
																if label.lower().startswith("entail"      ):
																					return ind
											return -1






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A=True		,      _A=True		,      _A=TruncationStrategy.ONLY_FIRST		,      **_A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.framework
											if self.tokenizer.pad_token is None:
																# Override for tokenizers not supporting padding
																logger.error(
																    "Tokenizer was not supporting padding necessary for zero-shot, attempting to use "
																    " `pad_token=eos_token`"      )
																__lowerCAmelCase           =	self.tokenizer.eos_token
											try:
																__lowerCAmelCase           =	self.tokenizer(
																    _A		,      add_special_tokens=_A		,      return_tensors=_A		,      padding=_A		,      truncation=_A		,      )
											except Exception as e:
																if "too short" in str(_A      ):
																					# tokenizers might yell that we want to truncate
																					# to a value that is not even reached by the input.
																					# In that case we don't want to truncate.
																					# It seems there's not a really better way to catch that
																					# exception.

																					__lowerCAmelCase           =	self.tokenizer(
																					    _A		,      add_special_tokens=_A		,      return_tensors=_A		,      padding=_A		,      truncation=TruncationStrategy.DO_NOT_TRUNCATE		,      )
																else:
																					raise e

											return inputs






						def     __SCREAMING_SNAKE_CASE(       self		,      **_A      ):




											"""simple docstring"""



											if kwargs.get("multi_class"		,      _A      ) is not None:
																__lowerCAmelCase           =	kwargs["multi_class"]
																logger.warning(
																    "The `multi_class` argument has been deprecated and renamed to `multi_label`. "
																    "`multi_class` will be removed in a future version of Transformers."      )
											__lowerCAmelCase           =	{}
											if "candidate_labels" in kwargs:
																__lowerCAmelCase           =	self._args_parser._parse_labels(kwargs["candidate_labels"]      )
											if "hypothesis_template" in kwargs:
																__lowerCAmelCase           =	kwargs["hypothesis_template"]

											__lowerCAmelCase           =	{}
											if "multi_label" in kwargs:
																__lowerCAmelCase           =	kwargs["multi_label"]
											return preprocess_params, {}, postprocess_params






						def __call__(       self		,      _A		,      *_A		,      **_A		,      ):




											"""simple docstring"""



											if len(_A      ) == 0:
																pass
											elif len(_A      ) == 1 and "candidate_labels" not in kwargs:
																__lowerCAmelCase           =	args[0]
											else:
																raise ValueError(f"""Unable to understand extra arguments {args}"""      )

											return super().__call__(_A		,      **_A      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A=None		,      _A="This example is {}."      ):




											"""simple docstring"""



											__lowerCAmelCase       ,							__lowerCAmelCase           =	self._args_parser(_A		,      _A		,      _A      )

											for i, (candidate_label, sequence_pair) in enumerate(zip(_A		,      _A      )      ):
																__lowerCAmelCase           =	self._parse_and_tokenize([sequence_pair]      )

																yield {
																    "candidate_label": candidate_label,
																    "sequence": sequences[0],
																    "is_last": i == len(_A      ) - 1,
																    **model_input,
																}






						def     __SCREAMING_SNAKE_CASE(       self		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	inputs["candidate_label"]
											__lowerCAmelCase           =	inputs["sequence"]
											__lowerCAmelCase           =	{k: inputs[k] for k in self.tokenizer.model_input_names}
											__lowerCAmelCase           =	self.model(**_A      )

											__lowerCAmelCase           =	{
											    "candidate_label": candidate_label,
											    "sequence": sequence,
											    "is_last": inputs["is_last"],
											    **outputs,
											}
											return model_outputs






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A=False      ):




											"""simple docstring"""



											__lowerCAmelCase           =	[outputs["candidate_label"] for outputs in model_outputs]
											__lowerCAmelCase           =	[outputs["sequence"] for outputs in model_outputs]
											__lowerCAmelCase           =	np.concatenate([output["logits"].numpy() for output in model_outputs]      )
											__lowerCAmelCase           =	logits.shape[0]
											__lowerCAmelCase           =	len(_A      )
											__lowerCAmelCase           =	N // n
											__lowerCAmelCase           =	logits.reshape((num_sequences, n, -1)      )

											if multi_label or len(_A      ) == 1:
																# softmax over the entailment vs. contradiction dim for each label independently
																__lowerCAmelCase           =	self.entailment_id
																__lowerCAmelCase           =	-1 if entailment_id == 0 else 0
																__lowerCAmelCase           =	reshaped_outputs[..., [contradiction_id, entailment_id]]
																__lowerCAmelCase           =	np.exp(_A      ) / np.exp(_A      ).sum(-1		,      keepdims=_A      )
																__lowerCAmelCase           =	scores[..., 1]
											else:
																# softmax the "entailment" logits over all candidate labels
																__lowerCAmelCase           =	reshaped_outputs[..., self.entailment_id]
																__lowerCAmelCase           =	np.exp(_A      ) / np.exp(_A      ).sum(-1		,      keepdims=_A      )

											__lowerCAmelCase           =	list(reversed(scores[0].argsort()      )      )
											return {
											    "sequence": sequences[0],
											    "labels": [candidate_labels[i] for i in top_inds],
											    "scores": scores[0, top_inds].tolist(),
											}










from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import (
					    TFDeiTForImageClassification,
					    TFDeiTForImageClassificationWithTeacher,
					    TFDeiTForMaskedImageModeling,
					    TFDeiTModel,
					)
					from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import DeiTImageProcessor
class       a__   :






						def __init__(       self		,      _A		,      _A=1_3		,      _A=3_0		,      _A=2		,      _A=3		,      _A=True		,      _A=True		,      _A=3_2		,      _A=2		,      _A=4		,      _A=3_7		,      _A="gelu"		,      _A=0.1		,      _A=0.1		,      _A=1_0		,      _A=0.02		,      _A=3		,      _A=None		,      _A=2		,      ):




											"""simple docstring"""



											__lowerCAmelCase           =	parent
											__lowerCAmelCase           =	batch_size
											__lowerCAmelCase           =	image_size
											__lowerCAmelCase           =	patch_size
											__lowerCAmelCase           =	num_channels
											__lowerCAmelCase           =	is_training
											__lowerCAmelCase           =	use_labels
											__lowerCAmelCase           =	hidden_size
											__lowerCAmelCase           =	num_hidden_layers
											__lowerCAmelCase           =	num_attention_heads
											__lowerCAmelCase           =	intermediate_size
											__lowerCAmelCase           =	hidden_act
											__lowerCAmelCase           =	hidden_dropout_prob
											__lowerCAmelCase           =	attention_probs_dropout_prob
											__lowerCAmelCase           =	type_sequence_label_size
											__lowerCAmelCase           =	initializer_range
											__lowerCAmelCase           =	scope
											__lowerCAmelCase           =	encoder_stride

											# in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
											__lowerCAmelCase           =	(image_size // patch_size) ** 2
											__lowerCAmelCase           =	num_patches + 2






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

											__lowerCAmelCase           =	None
											if self.use_labels:
																__lowerCAmelCase           =	ids_tensor([self.batch_size]		,      self.type_sequence_label_size      )

											__lowerCAmelCase           =	self.get_config()

											return config, pixel_values, labels






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											return DeiTConfig(
											    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=_A		,      initializer_range=self.initializer_range		,      encoder_stride=self.encoder_stride		,      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	TFDeiTModel(config=_A      )
											__lowerCAmelCase           =	model(_A      )
											self.parent.assertEqual(result.last_hidden_state.shape		,      (self.batch_size, self.seq_length, self.hidden_size)      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	TFDeiTForMaskedImageModeling(config=_A      )
											__lowerCAmelCase           =	model(_A      )
											self.parent.assertEqual(
											    result.reconstruction.shape		,      (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

											# test greyscale images
											__lowerCAmelCase           =	1
											__lowerCAmelCase           =	TFDeiTForMaskedImageModeling(_A      )

											__lowerCAmelCase           =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
											__lowerCAmelCase           =	model(_A      )
											self.parent.assertEqual(result.reconstruction.shape		,      (self.batch_size, 1, self.image_size, self.image_size)      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.type_sequence_label_size
											__lowerCAmelCase           =	TFDeiTForImageClassification(_A      )
											__lowerCAmelCase           =	model(_A		,      labels=_A      )
											self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.type_sequence_label_size)      )

											# test greyscale images
											__lowerCAmelCase           =	1
											__lowerCAmelCase           =	TFDeiTForImageClassification(_A      )

											__lowerCAmelCase           =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
											__lowerCAmelCase           =	model(_A		,      labels=_A      )
											self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.type_sequence_label_size)      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.prepare_config_and_inputs()
											__lowerCAmelCase       ,							__lowerCAmelCase       ,							__lowerCAmelCase           =	config_and_inputs
											__lowerCAmelCase           =	{"pixel_values": pixel_values}
											return config, inputs_dict






@require_tf
class       a__   ( snake_case__			,    snake_case__			,    unittest.TestCase				):
						_a							:   Optional[Any] =   (
						    (
						        TFDeiTModel,
						        TFDeiTForImageClassification,
						        TFDeiTForImageClassificationWithTeacher,
						        TFDeiTForMaskedImageModeling,
						    )
						    if is_tf_available()
						    else ()
						)
						_a							:   Optional[Any] =   (
						    {
						        """feature-extraction""": TFDeiTModel,
						        """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
						    }
						    if is_tf_available()
						    else {}
						)

						_a							:   str =   False
						_a							:   str =   False
						_a							:   List[str] =   False
						_a							:   Optional[int] =   False






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	TFDeiTModelTester(self      )
											__lowerCAmelCase           =	ConfigTester(self		,      config_class=_A		,      has_text_modality=_A		,      hidden_size=3_7      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											self.config_tester.run_common_tests()






						@unittest.skip(reason="DeiT does not use inputs_embeds"      )
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											pass






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase       ,							__lowerCAmelCase           =	self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																__lowerCAmelCase           =	model_class(_A      )
																self.assertIsInstance(model.get_input_embeddings()		,      (tf.keras.layers.Layer)      )
																__lowerCAmelCase           =	model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(_A		,      tf.keras.layers.Dense      )      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase       ,							__lowerCAmelCase           =	self.model_tester.prepare_config_and_inputs_for_common()

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

																__lowerCAmelCase           =	["pixel_values"]
																self.assertListEqual(arg_names[:1]		,      _A      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_A      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*_A      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_A      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A		,      _A=False      ):




											"""simple docstring"""



											__lowerCAmelCase           =	super()._prepare_for_class(_A		,      _A		,      return_labels=_A      )

											if return_labels:
																if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call      ).parameters:
																					del inputs_dict["labels"]

											return inputs_dict






						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__lowerCAmelCase           =	TFDeiTModel.from_pretrained(_A      )
																self.assertIsNotNone(_A      )







def      _a				(						):
					__lowerCAmelCase           =	Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
					return image


@require_tf
@require_vision
class       a__   ( unittest.TestCase				):






						@cached_property
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											return (
											    DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224"      )
											    if is_vision_available()
											    else None
											)






						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"      )

											__lowerCAmelCase           =	self.default_image_processor
											__lowerCAmelCase           =	prepare_img()
											__lowerCAmelCase           =	image_processor(images=_A		,      return_tensors="tf"      )

											# forward pass
											__lowerCAmelCase           =	model(**_A      )

											# verify the logits
											__lowerCAmelCase           =	tf.TensorShape((1, 1_0_0_0)      )
											self.assertEqual(outputs.logits.shape		,      _A      )

											__lowerCAmelCase           =	tf.constant([-1.02_66, 0.19_12, -1.28_61]      )

											self.assertTrue(np.allclose(outputs.logits[0, :3]		,      _A		,      atol=1E-4      )      )




