


'''simple docstring'''

from __future__ import annotations

import inspect
import unittest

from transformers import MobileViTConfig
from transformers.file_utils import is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, slow

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 numpy as np
 import tensorflow as tf

 from transformers import TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel
 from transformers.models.mobilevit.modeling_tf_mobilevit import TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import MobileViTImageProcessor
class   __lowerCAmelCase						(lowercase_					):





      '''simple docstring'''






      def 				UpperCamelCase__		(self   :   Optional[Any]			):

             '''simple docstring'''







             lowercase__				      =			self.config_class(**self.inputs_dict			)
             self.parent.assertTrue(hasattr(UpperCamelCase       ,						'''hidden_sizes'''			)			)
             self.parent.assertTrue(hasattr(UpperCamelCase       ,						'''neck_hidden_sizes'''			)			)
             self.parent.assertTrue(hasattr(UpperCamelCase       ,						'''num_attention_heads'''			)			)






class   __lowerCAmelCase						:





      '''simple docstring'''






      def __init__(self   :   Union[str, Any]       ,						UpperCamelCase   :   Optional[int]       ,						UpperCamelCase   :   Tuple=13       ,						UpperCamelCase   :   Dict=32       ,						UpperCamelCase   :   str=2       ,						UpperCamelCase   :   str=3       ,						UpperCamelCase   :   List[str]=640       ,						UpperCamelCase   :   Tuple=4       ,						UpperCamelCase   :   Optional[int]="silu"       ,						UpperCamelCase   :   Dict=3       ,						UpperCamelCase   :   Optional[Any]=32       ,						UpperCamelCase   :   Optional[Any]=0.1       ,						UpperCamelCase   :   List[Any]=0.1       ,						UpperCamelCase   :   Optional[Any]=0.1       ,						UpperCamelCase   :   List[str]=0.02       ,						UpperCamelCase   :   int=True       ,						UpperCamelCase   :   Dict=True       ,						UpperCamelCase   :   Tuple=10       ,						UpperCamelCase   :   Optional[int]=None       ,						):

             '''simple docstring'''







             lowercase__				      =			parent
             lowercase__				      =			batch_size
             lowercase__				      =			image_size
             lowercase__				      =			patch_size
             lowercase__				      =			num_channels
             lowercase__				      =			last_hidden_size
             lowercase__				      =			num_attention_heads
             lowercase__				      =			hidden_act
             lowercase__				      =			conv_kernel_size
             lowercase__				      =			output_stride
             lowercase__				      =			hidden_dropout_prob
             lowercase__				      =			attention_probs_dropout_prob
             lowercase__				      =			classifier_dropout_prob
             lowercase__				      =			use_labels
             lowercase__				      =			is_training
             lowercase__				      =			num_labels
             lowercase__				      =			initializer_range
             lowercase__				      =			scope



      def 				UpperCamelCase__		(self   :   Union[str, Any]			):

             '''simple docstring'''







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

             lowercase__				      =			None
             lowercase__				      =			None
             if self.use_labels:
                    lowercase__				      =			ids_tensor([self.batch_size]       ,						self.num_labels			)
                    lowercase__				      =			ids_tensor([self.batch_size, self.image_size, self.image_size]       ,						self.num_labels			)

             lowercase__				      =			self.get_config()

             return config, pixel_values, labels, pixel_labels



      def 				UpperCamelCase__		(self   :   Union[str, Any]			):

             '''simple docstring'''







             return MobileViTConfig(
                 image_size=self.image_size       ,						patch_size=self.patch_size       ,						num_channels=self.num_channels       ,						num_attention_heads=self.num_attention_heads       ,						hidden_act=self.hidden_act       ,						conv_kernel_size=self.conv_kernel_size       ,						output_stride=self.output_stride       ,						hidden_dropout_prob=self.hidden_dropout_prob       ,						attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,						classifier_dropout_prob=self.classifier_dropout_prob       ,						initializer_range=self.initializer_range       ,						)



      def 				UpperCamelCase__		(self   :   Tuple       ,						UpperCamelCase   :   Union[str, Any]       ,						UpperCamelCase   :   List[Any]       ,						UpperCamelCase   :   List[Any]       ,						UpperCamelCase   :   Optional[int]			):

             '''simple docstring'''







             lowercase__				      =			TFMobileViTModel(config=UpperCamelCase			)
             lowercase__				      =			model(UpperCamelCase       ,						training=UpperCamelCase			)
             lowercase__				      =			lowercase__				      =			self.image_size // self.output_stride
             self.parent.assertEqual(
                 result.last_hidden_state.shape       ,						(self.batch_size, self.last_hidden_size, expected_height, expected_width)			)



      def 				UpperCamelCase__		(self   :   List[Any]       ,						UpperCamelCase   :   str       ,						UpperCamelCase   :   Any       ,						UpperCamelCase   :   Optional[int]       ,						UpperCamelCase   :   Union[str, Any]			):

             '''simple docstring'''







             lowercase__				      =			self.num_labels
             lowercase__				      =			TFMobileViTForImageClassification(UpperCamelCase			)
             lowercase__				      =			model(UpperCamelCase       ,						labels=UpperCamelCase       ,						training=UpperCamelCase			)
             self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.num_labels)			)



      def 				UpperCamelCase__		(self   :   Tuple       ,						UpperCamelCase   :   int       ,						UpperCamelCase   :   List[Any]       ,						UpperCamelCase   :   Any       ,						UpperCamelCase   :   List[Any]			):

             '''simple docstring'''







             lowercase__				      =			self.num_labels
             lowercase__				      =			TFMobileViTForSemanticSegmentation(UpperCamelCase			)
             lowercase__				      =			lowercase__				      =			self.image_size // self.output_stride

             lowercase__				      =			model(UpperCamelCase       ,						training=UpperCamelCase			)
             self.parent.assertEqual(
                 result.logits.shape       ,						(self.batch_size, self.num_labels, expected_height, expected_width)			)

             lowercase__				      =			model(UpperCamelCase       ,						labels=UpperCamelCase       ,						training=UpperCamelCase			)
             self.parent.assertEqual(
                 result.logits.shape       ,						(self.batch_size, self.num_labels, expected_height, expected_width)			)






      def 				UpperCamelCase__		(self   :   Optional[int]			):

             '''simple docstring'''







             lowercase__				      =			self.prepare_config_and_inputs()
             lowercase__      ,lowercase__      ,lowercase__      ,lowercase__				      =			config_and_inputs
             lowercase__				      =			{'''pixel_values''': pixel_values}
             return config, inputs_dict






@require_tf
class   __lowerCAmelCase						(lowercase_  ,					lowercase_  ,					unittest.TestCase					):





      '''simple docstring'''






      lowerCAmelCase__ :					Any   =							(
          (TFMobileViTModel, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation)
          if is_tf_available()
          else ()
      )
      lowerCAmelCase__ :					int   =							(
          {"""feature-extraction""": TFMobileViTModel, """image-classification""": TFMobileViTForImageClassification}
          if is_tf_available()
          else {}
      )

      lowerCAmelCase__ :					Dict   =							False
      lowerCAmelCase__ :					Dict   =							False
      lowerCAmelCase__ :					List[str]   =							False
      lowerCAmelCase__ :					Tuple   =							False
      lowerCAmelCase__ :					List[Any]   =							False



      def 				UpperCamelCase__		(self   :   Union[str, Any]			):

             '''simple docstring'''







             lowercase__				      =			TFMobileViTModelTester(self			)
             lowercase__				      =			TFMobileViTConfigTester(self       ,						config_class=UpperCamelCase       ,						has_text_modality=UpperCamelCase			)



      def 				UpperCamelCase__		(self   :   Union[str, Any]			):

             '''simple docstring'''







             self.config_tester.run_common_tests()



      @unittest.skip(reason='''MobileViT does not use inputs_embeds'''			)
      def 				UpperCamelCase__		(self   :   List[str]			):

             '''simple docstring'''







             pass



      @unittest.skip(reason='''MobileViT does not support input and output embeddings'''			)
      def 				UpperCamelCase__		(self   :   str			):

             '''simple docstring'''







             pass



      @unittest.skip(reason='''MobileViT does not output attentions'''			)
      def 				UpperCamelCase__		(self   :   str			):

             '''simple docstring'''







             pass



      def 				UpperCamelCase__		(self   :   List[Any]			):

             '''simple docstring'''







             lowercase__      ,lowercase__				      =			self.model_tester.prepare_config_and_inputs_for_common()

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

                    lowercase__				      =			['''pixel_values''']
                    self.assertListEqual(arg_names[:1]       ,						UpperCamelCase			)



      def 				UpperCamelCase__		(self   :   Optional[Any]			):

             '''simple docstring'''







             lowercase__				      =			self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*UpperCamelCase			)



      def 				UpperCamelCase__		(self   :   Tuple			):

             '''simple docstring'''







             def check_hidden_states_output(UpperCamelCase   :   Dict       ,						UpperCamelCase   :   Any       ,						UpperCamelCase   :   Any			):
                    lowercase__				      =			model_class(UpperCamelCase			)

                    lowercase__				      =			model(**self._prepare_for_class(UpperCamelCase       ,						UpperCamelCase			)			)

                    lowercase__				      =			outputs.hidden_states

                    lowercase__				      =			5
                    self.assertEqual(len(UpperCamelCase			)       ,						UpperCamelCase			)

                    # MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
                    # with the width and height being successively divided by 2.
                    lowercase__				      =			2
                    for i in range(len(UpperCamelCase			)			):
                           self.assertListEqual(
                               list(hidden_states[i].shape[-2:]			)       ,						[self.model_tester.image_size // divisor, self.model_tester.image_size // divisor]       ,						)
                           divisor *= 2

                    self.assertEqual(self.model_tester.output_stride       ,						divisor // 2			)

             lowercase__      ,lowercase__				      =			self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    lowercase__				      =			True
                    check_hidden_states_output(UpperCamelCase       ,						UpperCamelCase       ,						UpperCamelCase			)

                    # check that output_hidden_states also work using config
                    del inputs_dict["output_hidden_states"]
                    lowercase__				      =			True

                    check_hidden_states_output(UpperCamelCase       ,						UpperCamelCase       ,						UpperCamelCase			)



      def 				UpperCamelCase__		(self   :   Optional[Any]			):

             '''simple docstring'''







             lowercase__				      =			self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*UpperCamelCase			)



      def 				UpperCamelCase__		(self   :   List[Any]			):

             '''simple docstring'''







             lowercase__				      =			self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_semantic_segmentation(*UpperCamelCase			)



      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''			)			) == 0       ,						reason='''TF does not support backprop for grouped convolutions on CPU.'''       ,						)
      def 				UpperCamelCase__		(self   :   Union[str, Any]			):

             '''simple docstring'''







             super().test_dataset_conversion()



      def 				UpperCamelCase__		(self   :   str       ,						UpperCamelCase   :   Tuple       ,						UpperCamelCase   :   Any       ,						UpperCamelCase   :   Any=2E-1       ,						UpperCamelCase   :   Any=2E-1			):

             '''simple docstring'''







             self.assertTrue(np.allclose(UpperCamelCase       ,						UpperCamelCase       ,						atol=UpperCamelCase       ,						rtol=UpperCamelCase			)			)



      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''			)			) == 0       ,						reason='''TF does not support backprop for grouped convolutions on CPU.'''       ,						)
      @slow
      def 				UpperCamelCase__		(self   :   Optional[int]			):

             '''simple docstring'''







             lowercase__      ,lowercase__				      =			self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    # Since `TFMobileViTModel` cannot operate with the default `fit()` method.
                    if model_class.__name__ != "TFMobileViTModel":
                           lowercase__				      =			model_class(UpperCamelCase			)
                           if getattr(UpperCamelCase       ,						'''hf_compute_loss'''       ,						UpperCamelCase			):
                                  super().test_keras_fit()



      def 				UpperCamelCase__		(self   :   Dict			):

             '''simple docstring'''







             lowercase__      ,lowercase__				      =			self.model_tester.prepare_config_and_inputs_for_common()
             for model_class in self.all_model_classes:
                    # set an ignore index to correctly test the masked loss used in
                    # `TFMobileViTForSemanticSegmentation`.
                    if model_class.__name__ != "TFMobileViTForSemanticSegmentation":
                           lowercase__				      =			5

                    lowercase__				      =			model_class(UpperCamelCase			)
                    if getattr(UpperCamelCase       ,						'''hf_compute_loss'''       ,						UpperCamelCase			):
                           # The number of elements in the loss should be the same as the number of elements in the label
                           lowercase__				      =			self._prepare_for_class(inputs_dict.copy()       ,						UpperCamelCase       ,						return_labels=UpperCamelCase			)
                           lowercase__				      =			prepared_for_class[
                               sorted(prepared_for_class.keys() - inputs_dict.keys()       ,						reverse=UpperCamelCase			)[0]
                           ]
                           lowercase__				      =			added_label.shape.as_list()[:1]

                           # Test that model correctly compute the loss with kwargs
                           lowercase__				      =			self._prepare_for_class(inputs_dict.copy()       ,						UpperCamelCase       ,						return_labels=UpperCamelCase			)
                           lowercase__				      =			{'''input_ids''', '''pixel_values''', '''input_features'''}
                           lowercase__				      =			possible_input_names.intersection(set(UpperCamelCase			)			).pop()
                           lowercase__				      =			prepared_for_class.pop(UpperCamelCase			)

                           lowercase__				      =			model(UpperCamelCase       ,						**UpperCamelCase			)[0]
                           self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]			)

                           # Test that model correctly compute the loss when we mask some positions
                           lowercase__				      =			self._prepare_for_class(inputs_dict.copy()       ,						UpperCamelCase       ,						return_labels=UpperCamelCase			)
                           lowercase__				      =			{'''input_ids''', '''pixel_values''', '''input_features'''}
                           lowercase__				      =			possible_input_names.intersection(set(UpperCamelCase			)			).pop()
                           lowercase__				      =			prepared_for_class.pop(UpperCamelCase			)
                           if "labels" in prepared_for_class:
                                  lowercase__				      =			prepared_for_class['''labels'''].numpy()
                                  if len(labels.shape			) > 1 and labels.shape[1] != 1:
                                         # labels[0] = -100
                                         lowercase__				      =			tf.convert_to_tensor(UpperCamelCase			)
                                         lowercase__				      =			model(UpperCamelCase       ,						**UpperCamelCase			)[0]
                                         self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]			)
                                         self.assertTrue(not np.any(np.isnan(loss.numpy()			)			)			)

                # Test that model correctly compute the loss with a dict
                           lowercase__				      =			self._prepare_for_class(inputs_dict.copy()       ,						UpperCamelCase       ,						return_labels=UpperCamelCase			)
                           lowercase__				      =			model(UpperCamelCase			)[0]
                           self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]			)

                           # Test that model correctly compute the loss with a tuple
                           lowercase__				      =			self._prepare_for_class(inputs_dict.copy()       ,						UpperCamelCase       ,						return_labels=UpperCamelCase			)

                           # Get keys that were added with the _prepare_for_class function
                           lowercase__				      =			prepared_for_class.keys() - inputs_dict.keys()
                           lowercase__				      =			inspect.signature(model.call			).parameters
                           lowercase__				      =			list(signature.keys()			)

                           # Create a dictionary holding the location of the tensors in the tuple
                           lowercase__				      =			{0: input_name}
                           for label_key in label_keys:
                                  lowercase__				      =			signature_names.index(UpperCamelCase			)
                                  lowercase__				      =			label_key
                           lowercase__				      =			sorted(tuple_index_mapping.items()			)
                           # Initialize a list with their default values, update the values and convert to a tuple
                           lowercase__				      =			[]

                           for name in signature_names:
                                  if name != "kwargs":
                                         list_input.append(signature[name].default			)

                           for index, value in sorted_tuple_index_mapping:
                                  lowercase__				      =			prepared_for_class[value]

                           lowercase__				      =			tuple(UpperCamelCase			)

                           # Send to model
                           lowercase__				      =			model(tuple_input[:-1]			)[0]

                           self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]			)






      @slow
      def 				UpperCamelCase__		(self   :   Any			):

             '''simple docstring'''







             for model_name in TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    lowercase__				      =			TFMobileViTModel.from_pretrained(UpperCamelCase			)
                    self.assertIsNotNone(UpperCamelCase			)






def _SCREAMING_SNAKE_CASE				()	->	Optional[Any]:






       """simple docstring"""







       lowercase__				      =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
       return image

@require_tf
class   __lowerCAmelCase						(unittest.TestCase					):





      '''simple docstring'''






      @slow
      def 				UpperCamelCase__		(self   :   List[str]			):

             '''simple docstring'''







             lowercase__				      =			TFMobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small'''			)

             lowercase__				      =			MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small'''			)
             lowercase__				      =			prepare_img()
             lowercase__				      =			image_processor(images=UpperCamelCase       ,						return_tensors='''tf'''			)

             # forward pass
             lowercase__				      =			model(**UpperCamelCase       ,						training=UpperCamelCase			)

             # verify the logits
             lowercase__				      =			tf.TensorShape((1, 1000)			)
             self.assertEqual(outputs.logits.shape       ,						UpperCamelCase			)

             lowercase__				      =			tf.constant([-1.93_64, -1.23_27, -0.46_53]			)

             tf.debugging.assert_near(outputs.logits[0, :3]       ,						UpperCamelCase       ,						atol=1E-4       ,						rtol=1E-04			)






      @slow
      def 				UpperCamelCase__		(self   :   int			):

             '''simple docstring'''







             lowercase__				      =			TFMobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''			)

             lowercase__				      =			MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''			)

             lowercase__				      =			prepare_img()
             lowercase__				      =			image_processor(images=UpperCamelCase       ,						return_tensors='''tf'''			)

             # forward pass
             lowercase__				      =			model(inputs.pixel_values       ,						training=UpperCamelCase			)
             lowercase__				      =			outputs.logits

             # verify the logits
             lowercase__				      =			tf.TensorShape((1, 21, 32, 32)			)
             self.assertEqual(logits.shape       ,						UpperCamelCase			)

             lowercase__				      =			tf.constant(
                 [
                     [[6.97_13, 6.97_86, 7.24_22], [7.28_93, 7.28_25, 7.44_46], [7.65_80, 7.87_97, 7.94_20]],
                     [[-10.68_69, -10.32_50, -10.34_71], [-10.42_28, -9.98_68, -9.71_32], [-11.04_05, -11.02_21, -10.73_18]],
                     [[-3.30_89, -2.85_39, -2.67_40], [-3.27_06, -2.56_21, -2.51_08], [-3.25_34, -2.66_15, -2.66_51]],
                 ]			)

             tf.debugging.assert_near(logits[0, :3, :3, :3]       ,						UpperCamelCase       ,						rtol=1E-4       ,						atol=1E-4			)








'''simple docstring'''

# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer
from .base import PipelineTool


lowerCamelCase							:       Any  						=     'Here is a text containing a lot of information: \'\'\'{text}\'\'\'.\n\nCan you answer this question about the text: \'{question}\''
class   __lowerCAmelCase						(lowercase_					):





      '''simple docstring'''






      lowerCAmelCase__ :					Dict   =							"""google/flan-t5-base"""
      lowerCAmelCase__ :					List[Any]   =							(
          """This is a tool that answers questions related to a text. It takes two arguments named `text`, which is the """
          """text where to find the answer, and `question`, which is the question, and returns the answer to the question."""
      )
      lowerCAmelCase__ :					Any   =							"""text_qa"""
      lowerCAmelCase__ :					Union[str, Any]   =							AutoTokenizer
      lowerCAmelCase__ :					Any   =							AutoModelForSeqaSeqLM

      lowerCAmelCase__ :					Any   =							["""text""", """text"""]
      lowerCAmelCase__ :					Optional[int]   =							["""text"""]



      def 				UpperCamelCase__		(self   :   Union[str, Any]       ,						UpperCamelCase   :   str       ,						UpperCamelCase   :   str			):

             '''simple docstring'''







             lowercase__				      =			QA_PROMPT.format(text=UpperCamelCase       ,						question=UpperCamelCase			)
             return self.pre_processor(UpperCamelCase       ,						return_tensors='''pt'''			)



      def 				UpperCamelCase__		(self   :   Dict       ,						UpperCamelCase   :   List[Any]			):

             '''simple docstring'''







             lowercase__				      =			self.model.generate(**UpperCamelCase			)

             lowercase__      ,lowercase__				      =			inputs['''input_ids'''].shape
             lowercase__				      =			output_ids.shape[0]

             return output_ids.reshape(UpperCamelCase       ,						out_b // in_b       ,						*output_ids.shape[1:]			)[0][0]






      def 				UpperCamelCase__		(self   :   Tuple       ,						UpperCamelCase   :   Optional[Any]			):

             '''simple docstring'''







             return self.pre_processor.decode(UpperCamelCase       ,						skip_special_tokens=UpperCamelCase       ,						clean_up_tokenization_spaces=UpperCamelCase			)




