





from __future__ import annotations

import copy
import inspect
import json
import math
import os
import tempfile
import unittest
from importlib import import_module

import numpy as np

from transformers import ViTMAEConfig
from transformers.file_utils import cached_property, is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, require_vision, 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 tensorflow as tf

       from transformers import TFViTMAEForPreTraining, TFViTMAEModel


if is_vision_available():
       from PIL import Image

       from transformers import ViTImageProcessor







class       __A							:




      """simple docstring"""







      def __init__(		self   ,				a__   ,				a__=13   ,				a__=30   ,				a__=2   ,				a__=3   ,				a__=True   ,				a__=True   ,				a__=32   ,				a__=2   ,				a__=4   ,				a__=37   ,				a__="gelu"   ,				a__=0.1   ,				a__=0.1   ,				a__=10   ,				a__=0.02   ,				a__=3   ,				a__=0.6   ,				a__=None   ,				):



             """simple docstring"""





             _lowerCamelCase				:  List[str]						=		parent
             _lowerCamelCase				:  List[str]						=		batch_size
             _lowerCamelCase				:  Any						=		image_size
             _lowerCamelCase				:  str						=		patch_size
             _lowerCamelCase				:  Optional[Any]						=		num_channels
             _lowerCamelCase				:  Tuple						=		is_training
             _lowerCamelCase				:  List[Any]						=		use_labels
             _lowerCamelCase				:  Union[str, Any]						=		hidden_size
             _lowerCamelCase				:  Any						=		num_hidden_layers
             _lowerCamelCase				:  List[str]						=		num_attention_heads
             _lowerCamelCase				:  Union[str, Any]						=		intermediate_size
             _lowerCamelCase				:  Dict						=		hidden_act
             _lowerCamelCase				:  int						=		hidden_dropout_prob
             _lowerCamelCase				:  str						=		attention_probs_dropout_prob
             _lowerCamelCase				:  Optional[Any]						=		type_sequence_label_size
             _lowerCamelCase				:  Optional[Any]						=		initializer_range
             _lowerCamelCase				:  Union[str, Any]						=		mask_ratio
             _lowerCamelCase				:  Tuple						=		scope

             # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above
             # (we add 1 for the [CLS] token)
             _lowerCamelCase				:  Union[str, Any]						=		(image_size // patch_size) ** 2
             _lowerCamelCase				:  Optional[Any]						=		int(math.ceil((1 - mask_ratio) * (num_patches + 1)))


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Tuple						=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

             _lowerCamelCase				:  Tuple						=		None
             if self.use_labels:
                    _lowerCamelCase				:  int						=		ids_tensor([self.batch_size]   ,				self.type_sequence_label_size)

             _lowerCamelCase				:  Any						=		self.get_config()

             return config, pixel_values, labels


      def 					__snake_case      (		self):



             """simple docstring"""





             return ViTMAEConfig(
                 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   ,				decoder_hidden_size=self.hidden_size   ,				decoder_num_hidden_layers=self.num_hidden_layers   ,				decoder_num_attention_heads=self.num_attention_heads   ,				decoder_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   ,				mask_ratio=self.mask_ratio   ,				)


      def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  Optional[Any]						=		TFViTMAEModel(config=a__)
             _lowerCamelCase				:  List[Any]						=		model(a__   ,				training=a__)
             self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size))


      def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  Optional[int]						=		TFViTMAEForPreTraining(a__)
             _lowerCamelCase				:  List[Any]						=		model(a__   ,				training=a__)
             # expected sequence length = num_patches
             _lowerCamelCase				:  List[str]						=		(self.image_size // self.patch_size) ** 2
             _lowerCamelCase				:  int						=		self.patch_size**2 * self.num_channels
             self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, num_patches, expected_num_channels))

             # test greyscale images
             _lowerCamelCase				:  int						=		1
             _lowerCamelCase				:  Optional[Any]						=		TFViTMAEForPreTraining(a__)

             _lowerCamelCase				:  Dict						=		floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
             _lowerCamelCase				:  List[Any]						=		model(a__   ,				training=a__)
             _lowerCamelCase				:  List[Any]						=		self.patch_size**2
             self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, num_patches, expected_num_channels))



      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  int						=		self.prepare_config_and_inputs()
             ((_lowerCamelCase),	(_lowerCamelCase),	(_lowerCamelCase))				:  Union[str, Any]						=		config_and_inputs
             _lowerCamelCase				:  Tuple						=		{'''pixel_values''': pixel_values}
             return config, inputs_dict







@require_tf
class       __A							(   lowerCamelCase__			,lowerCamelCase__			,unittest.TestCase	):




      """simple docstring"""







      UpperCAmelCase__							=   (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else ()
      UpperCAmelCase__							=   {"""feature-extraction""": TFViTMAEModel} if is_tf_available() else {}

      UpperCAmelCase__							=   False
      UpperCAmelCase__							=   False
      UpperCAmelCase__							=   False
      UpperCAmelCase__							=   False


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[Any]						=		TFViTMAEModelTester(self)
             _lowerCamelCase				:  Tuple						=		ConfigTester(self   ,				config_class=a__   ,				has_text_modality=a__   ,				hidden_size=37)


      def 					__snake_case      (		self):



             """simple docstring"""





             self.config_tester.run_common_tests()


      @unittest.skip(reason='''ViTMAE does not use inputs_embeds''')
      def 					__snake_case      (		self):



             """simple docstring"""





             pass


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase,	_lowerCamelCase				:  Optional[Any]						=		self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    _lowerCamelCase				:  Dict						=		model_class(a__)
                    self.assertIsInstance(model.get_input_embeddings()   ,				(tf.keras.layers.Layer))
                    _lowerCamelCase				:  Optional[Any]						=		model.get_output_embeddings()
                    self.assertTrue(x is None or isinstance(a__   ,				tf.keras.layers.Layer))


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase,	_lowerCamelCase				:  Dict						=		self.model_tester.prepare_config_and_inputs_for_common()

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

                    _lowerCamelCase				:  List[Any]						=		['''pixel_values''']
                    self.assertListEqual(arg_names[:1]   ,				a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Tuple						=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_pretraining(*a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             np.random.seed(2)

             _lowerCamelCase,	_lowerCamelCase				:  Optional[Any]						=		self.model_tester.prepare_config_and_inputs_for_common()

             _lowerCamelCase				:  Union[str, Any]						=		int((config.image_size // config.patch_size) ** 2)
             _lowerCamelCase				:  Optional[Any]						=		np.random.uniform(size=(self.model_tester.batch_size, num_patches))

             for model_class in self.all_model_classes:
                    _lowerCamelCase				:  List[Any]						=		model_class(a__)
                    _lowerCamelCase				:  Optional[Any]						=		self._prepare_for_class(a__   ,				a__)

                    _lowerCamelCase				:  Dict						=		model(a__   ,				noise=a__)

                    _lowerCamelCase				:  Optional[int]						=		copy.deepcopy(self._prepare_for_class(a__   ,				a__))
                    _lowerCamelCase				:  List[Any]						=		model(**a__   ,				noise=a__)
                    _lowerCamelCase				:  List[Any]						=		outputs_dict[0].numpy()
                    _lowerCamelCase				:  Dict						=		outputs_keywords[0].numpy()

                    self.assertLess(np.sum(np.abs(output_dict - output_keywords))   ,				1e-6)


      def 					__snake_case      (		self):



             """simple docstring"""





             np.random.seed(2)

             _lowerCamelCase,	_lowerCamelCase				:  int						=		self.model_tester.prepare_config_and_inputs_for_common()

             _lowerCamelCase				:  int						=		int((config.image_size // config.patch_size) ** 2)
             _lowerCamelCase				:  Any						=		np.random.uniform(size=(self.model_tester.batch_size, num_patches))

             def prepare_numpy_arrays(a__):
                    _lowerCamelCase				:  List[str]						=		{}
                    for k, v in inputs_dict.items():
                           if tf.is_tensor(a__):
                                  _lowerCamelCase				:  Optional[int]						=		v.numpy()
                           else:
                                  _lowerCamelCase				:  int						=		np.array(a__)

                    return inputs_np_dict

             for model_class in self.all_model_classes:
                    _lowerCamelCase				:  List[str]						=		model_class(a__)

                    _lowerCamelCase				:  Any						=		self._prepare_for_class(a__   ,				a__)
                    _lowerCamelCase				:  List[Any]						=		prepare_numpy_arrays(a__)

                    _lowerCamelCase				:  str						=		model(a__   ,				noise=a__)
                    _lowerCamelCase				:  List[str]						=		model(**a__   ,				noise=a__)
                    self.assert_outputs_same(a__   ,				a__)


      def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__):



             """simple docstring"""





             np.random.seed(2)

             _lowerCamelCase				:  Tuple						=		int((tf_model.config.image_size // tf_model.config.patch_size) ** 2)
             _lowerCamelCase				:  int						=		np.random.uniform(size=(self.model_tester.batch_size, num_patches))
             _lowerCamelCase				:  Optional[int]						=		tf.constant(a__)

             # Add `noise` argument.
             # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
             _lowerCamelCase				:  str						=		tf_noise

             super().check_pt_tf_models(a__   ,				a__   ,				a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             np.random.seed(2)

             _lowerCamelCase,	_lowerCamelCase				:  Any						=		self.model_tester.prepare_config_and_inputs_for_common()

             _lowerCamelCase				:  Optional[int]						=		{
                 module_member
                 for model_class in self.all_model_classes
                 for module in (import_module(model_class.__module__),)
                 for module_member_name in dir(a__)
                 if module_member_name.endswith('''MainLayer''')
                 # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`.
                 and module_member_name[: -len('''MainLayer''')] == model_class.__name__[: -len('''Model''')]
                 for module_member in (getattr(a__   ,				a__),)
                 if isinstance(a__   ,				a__)
                 and tf.keras.layers.Layer in module_member.__bases__
                 and getattr(a__   ,				'''_keras_serializable'''   ,				a__)
             }

             _lowerCamelCase				:  Union[str, Any]						=		int((config.image_size // config.patch_size) ** 2)
             _lowerCamelCase				:  str						=		np.random.uniform(size=(self.model_tester.batch_size, num_patches))
             _lowerCamelCase				:  Union[str, Any]						=		tf.convert_to_tensor(a__)
             inputs_dict.update({'''noise''': noise})

             for main_layer_class in tf_main_layer_classes:
                    _lowerCamelCase				:  List[str]						=		main_layer_class(a__)

                    _lowerCamelCase				:  Dict						=		{
                        name: tf.keras.Input(tensor.shape[1:]   ,				dtype=tensor.dtype) for name, tensor in inputs_dict.items()
                    }

                    _lowerCamelCase				:  str						=		tf.keras.Model(a__   ,				outputs=main_layer(a__))
                    _lowerCamelCase				:  Any						=		model(a__)

                    with tempfile.TemporaryDirectory() as tmpdirname:
                           _lowerCamelCase				:  Optional[int]						=		os.path.join(a__   ,				'''keras_model.h5''')
                           model.save(a__)
                           _lowerCamelCase				:  List[str]						=		tf.keras.models.load_model(
                               a__   ,				custom_objects={main_layer_class.__name__: main_layer_class})
                           assert isinstance(a__   ,				tf.keras.Model)
                           _lowerCamelCase				:  Union[str, Any]						=		model(a__)
                           self.assert_outputs_same(a__   ,				a__)


      @slow
      def 					__snake_case      (		self):



             """simple docstring"""





             np.random.seed(2)

             _lowerCamelCase,	_lowerCamelCase				:  Any						=		self.model_tester.prepare_config_and_inputs_for_common()

             _lowerCamelCase				:  Any						=		int((config.image_size // config.patch_size) ** 2)
             _lowerCamelCase				:  str						=		np.random.uniform(size=(self.model_tester.batch_size, num_patches))

             for model_class in self.all_model_classes:
                    _lowerCamelCase				:  Union[str, Any]						=		model_class(a__)
                    _lowerCamelCase				:  Optional[int]						=		self._prepare_for_class(a__   ,				a__)
                    _lowerCamelCase				:  Dict						=		model(a__   ,				noise=a__)

                    if model_class.__name__ == "TFViTMAEModel":
                           _lowerCamelCase				:  Dict						=		outputs.last_hidden_state.numpy()
                           _lowerCamelCase				:  Optional[int]						=		0
                    else:
                           _lowerCamelCase				:  Dict						=		outputs.logits.numpy()
                           _lowerCamelCase				:  Optional[Any]						=		0

                    with tempfile.TemporaryDirectory() as tmpdirname:
                           model.save_pretrained(a__   ,				saved_model=a__)
                           _lowerCamelCase				:  Tuple						=		model_class.from_pretrained(a__)
                           _lowerCamelCase				:  Dict						=		model(a__   ,				noise=a__)

                           if model_class.__name__ == "TFViTMAEModel":
                                  _lowerCamelCase				:  Optional[Any]						=		after_outputs['''last_hidden_state'''].numpy()
                                  _lowerCamelCase				:  Optional[Any]						=		0
                           else:
                                  _lowerCamelCase				:  Any						=		after_outputs['''logits'''].numpy()
                                  _lowerCamelCase				:  Optional[Any]						=		0

                           _lowerCamelCase				:  Any						=		np.amax(np.abs(out_a - out_a))
                           self.assertLessEqual(a__   ,				1e-5)


      def 					__snake_case      (		self):



             """simple docstring"""





             np.random.seed(2)

             _lowerCamelCase,	_lowerCamelCase				:  List[Any]						=		self.model_tester.prepare_config_and_inputs_for_common()

             _lowerCamelCase				:  Optional[Any]						=		int((config.image_size // config.patch_size) ** 2)
             _lowerCamelCase				:  str						=		np.random.uniform(size=(self.model_tester.batch_size, num_patches))

             for model_class in self.all_model_classes:
                    _lowerCamelCase				:  Dict						=		model_class(a__)
                    _lowerCamelCase				:  Optional[Any]						=		self._prepare_for_class(a__   ,				a__)

                    _lowerCamelCase				:  Dict						=		model(a__   ,				noise=a__)
                    _lowerCamelCase				:  Optional[Any]						=		model.get_config()
                    # make sure that returned config is jsonifiable, which is required by keras
                    json.dumps(a__)
                    _lowerCamelCase				:  int						=		model_class.from_config(model.get_config())
                    # make sure it also accepts a normal config
                    _lowerCamelCase				:  Any						=		model_class.from_config(model.config)
                    _lowerCamelCase				:  Optional[int]						=		new_model(a__)  # Build model
                    new_model.set_weights(model.get_weights())
                    _lowerCamelCase				:  str						=		new_model(a__   ,				noise=a__)

                    self.assert_outputs_same(a__   ,				a__)


      @unittest.skip(
          reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load
    to get deterministic results.''')
      def 					__snake_case      (		self):



             """simple docstring"""





             pass


      @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load''')
      def 					__snake_case      (		self):



             """simple docstring"""





             pass



      @slow
      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  int						=		TFViTMAEModel.from_pretrained('''google/vit-base-patch16-224''')
             self.assertIsNotNone(a__)




def  __UpperCAmelCase(   ):
       _lowerCamelCase				:  Any						=		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
       return image







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




      """simple docstring"""







      @cached_property
      def 					__snake_case      (		self):



             """simple docstring"""





             return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''') if is_vision_available() else None



      @slow
      def 					__snake_case      (		self):



             """simple docstring"""





             np.random.seed(2)

             _lowerCamelCase				:  List[Any]						=		TFViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''')

             _lowerCamelCase				:  Tuple						=		self.default_image_processor
             _lowerCamelCase				:  int						=		prepare_img()
             _lowerCamelCase				:  Optional[Any]						=		image_processor(images=a__   ,				return_tensors='''tf''')

             # prepare a noise vector that will be also used for testing the TF model
             # (this way we can ensure that the PT and TF models operate on the same inputs)
             _lowerCamelCase				:  str						=		ViTMAEConfig()
             _lowerCamelCase				:  Union[str, Any]						=		int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2)
             _lowerCamelCase				:  Union[str, Any]						=		np.random.uniform(size=(1, num_patches))

             # forward pass
             _lowerCamelCase				:  Optional[int]						=		model(**a__   ,				noise=a__)

             # verify the logits
             _lowerCamelCase				:  int						=		tf.convert_to_tensor([1, 196, 768])
             self.assertEqual(outputs.logits.shape   ,				a__)

             _lowerCamelCase				:  Optional[Any]						=		tf.convert_to_tensor(
                 [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]])

             tf.debugging.assert_near(outputs.logits[0, :3, :3]   ,				a__   ,				atol=1e-4)








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowerCamelCase      							=   {
    'configuration_clipseg': [
        'CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'CLIPSegConfig',
        'CLIPSegTextConfig',
        'CLIPSegVisionConfig',
    ],
    'processing_clipseg': ['CLIPSegProcessor'],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       _lowerCamelCase      							=   [
           'CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST',
           'CLIPSegModel',
           'CLIPSegPreTrainedModel',
           'CLIPSegTextModel',
           'CLIPSegVisionModel',
           'CLIPSegForImageSegmentation',
       ]

if TYPE_CHECKING:
       from .configuration_clipseg import (
           CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP,
           CLIPSegConfig,
           CLIPSegTextConfig,
           CLIPSegVisionConfig,
       )
       from .processing_clipseg import CLIPSegProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_clipseg import (
                  CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST,
                  CLIPSegForImageSegmentation,
                  CLIPSegModel,
                  CLIPSegPreTrainedModel,
                  CLIPSegTextModel,
                  CLIPSegVisionModel,
              )

else:
       import sys

       _lowerCamelCase      							=   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

