'''simple docstring'''

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     __UpperCAmelCase       :





  def __init__(	self    ,     _lowerCamelCase    ,     _lowerCamelCase=13    ,     _lowerCamelCase=30    ,     _lowerCamelCase=2    ,     _lowerCamelCase=3    ,     _lowerCamelCase=True    ,     _lowerCamelCase=True    ,     _lowerCamelCase=32    ,     _lowerCamelCase=2    ,     _lowerCamelCase=4    ,     _lowerCamelCase=37    ,     _lowerCamelCase="gelu"    ,     _lowerCamelCase=0.1    ,     _lowerCamelCase=0.1    ,     _lowerCamelCase=10    ,     _lowerCamelCase=0.02    ,     _lowerCamelCase=3    ,     _lowerCamelCase=0.6    ,     _lowerCamelCase=None    ,     ):
     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_			=      mask_ratio
     lowerCAmelCase_			=      scope

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





  def 						UpperCAmelCase_	(	self ):
     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 						UpperCAmelCase_	(	self ):
     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=_lowerCamelCase    ,     initializer_range=self.initializer_range    ,     mask_ratio=self.mask_ratio    ,     )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      TFViTMAEModel(config=_lowerCamelCase )
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     training=_lowerCamelCase )
     self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, self.seq_length, self.hidden_size) )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      TFViTMAEForPreTraining(_lowerCamelCase )
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     training=_lowerCamelCase )
     # expected sequence length = num_patches
     lowerCAmelCase_			=      (self.image_size // self.patch_size) ** 2
     lowerCAmelCase_			=      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_			=      1
     lowerCAmelCase_			=      TFViTMAEForPreTraining(_lowerCamelCase )

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







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






@require_tf
class     __UpperCAmelCase       ( __a						,       __a						,       unittest.TestCase		):
  __A :				Tuple											= (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else ()
  __A :				Any											= {'feature-extraction': TFViTMAEModel} if is_tf_available() else {}

  __A :				Optional[Any]											= False
  __A :				str											= False
  __A :				Tuple											= False
  __A :				Dict											= False





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      TFViTMAEModelTester(self )
     lowerCAmelCase_			=      ConfigTester(self    ,     config_class=_lowerCamelCase    ,     has_text_modality=_lowerCamelCase    ,     hidden_size=37 )





  def 						UpperCAmelCase_	(	self ):
     self.config_tester.run_common_tests()





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





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_	,lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs_for_common()

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





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_	,lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
        lowerCAmelCase_			=      model_class(_lowerCamelCase )
        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]    ,     _lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     # make the mask reproducible
     np.random.seed(2 )

     lowerCAmelCase_	,lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs_for_common()

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

     for model_class in self.all_model_classes:
        lowerCAmelCase_			=      model_class(_lowerCamelCase )
        lowerCAmelCase_			=      self._prepare_for_class(_lowerCamelCase    ,     _lowerCamelCase )

        lowerCAmelCase_			=      model(_lowerCamelCase    ,     noise=_lowerCamelCase )

        lowerCAmelCase_			=      copy.deepcopy(self._prepare_for_class(_lowerCamelCase    ,     _lowerCamelCase ) )
        lowerCAmelCase_			=      model(**_lowerCamelCase    ,     noise=_lowerCamelCase )
        lowerCAmelCase_			=      outputs_dict[0].numpy()
        lowerCAmelCase_			=      outputs_keywords[0].numpy()

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





  def 						UpperCAmelCase_	(	self ):
     # make the mask reproducible
     np.random.seed(2 )

     lowerCAmelCase_	,lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs_for_common()

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

     def prepare_numpy_arrays(_lowerCamelCase ):
        lowerCAmelCase_			=      {}
        for k, v in inputs_dict.items():
           if tf.is_tensor(_lowerCamelCase ):
              lowerCAmelCase_			=      v.numpy()
           else:
              lowerCAmelCase_			=      np.array(_lowerCamelCase )

        return inputs_np_dict

     for model_class in self.all_model_classes:
        lowerCAmelCase_			=      model_class(_lowerCamelCase )

        lowerCAmelCase_			=      self._prepare_for_class(_lowerCamelCase    ,     _lowerCamelCase )
        lowerCAmelCase_			=      prepare_numpy_arrays(_lowerCamelCase )

        lowerCAmelCase_			=      model(_lowerCamelCase    ,     noise=_lowerCamelCase )
        lowerCAmelCase_			=      model(**_lowerCamelCase    ,     noise=_lowerCamelCase )
        self.assert_outputs_same(_lowerCamelCase    ,     _lowerCamelCase )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     # make masks reproducible
     np.random.seed(2 )

     lowerCAmelCase_			=      int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 )
     lowerCAmelCase_			=      np.random.uniform(size=(self.model_tester.batch_size, num_patches) )
     lowerCAmelCase_			=      tf.constant(_lowerCamelCase )

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

     super().check_pt_tf_models(_lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     # make mask reproducible
     np.random.seed(2 )

     lowerCAmelCase_	,lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs_for_common()

     lowerCAmelCase_			=      {
         module_member
         for model_class in self.all_model_classes
         for module in (import_module(model_class.__module__ ),)
         for module_member_name in dir(_lowerCamelCase )
         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(_lowerCamelCase    ,     _lowerCamelCase ),)
         if isinstance(_lowerCamelCase    ,     _lowerCamelCase )
         and tf.keras.layers.Layer in module_member.__bases__
         and getattr(_lowerCamelCase    ,     '''_keras_serializable'''    ,     _lowerCamelCase )
     }

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

     for main_layer_class in tf_main_layer_classes:
        lowerCAmelCase_			=      main_layer_class(_lowerCamelCase )

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

        lowerCAmelCase_			=      tf.keras.Model(_lowerCamelCase    ,     outputs=main_layer(_lowerCamelCase ) )
        lowerCAmelCase_			=      model(_lowerCamelCase )

        with tempfile.TemporaryDirectory() as tmpdirname:
           lowerCAmelCase_			=      os.path.join(_lowerCamelCase    ,     '''keras_model.h5''' )
           model.save(_lowerCamelCase )
           lowerCAmelCase_			=      tf.keras.models.load_model(
               _lowerCamelCase    ,     custom_objects={main_layer_class.__name__: main_layer_class} )
           assert isinstance(_lowerCamelCase    ,     tf.keras.Model )
           lowerCAmelCase_			=      model(_lowerCamelCase )
           self.assert_outputs_same(_lowerCamelCase    ,     _lowerCamelCase )





  @slow
  def 						UpperCAmelCase_	(	self ):
     # make mask reproducible
     np.random.seed(2 )

     lowerCAmelCase_	,lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs_for_common()

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

     for model_class in self.all_model_classes:
        lowerCAmelCase_			=      model_class(_lowerCamelCase )
        lowerCAmelCase_			=      self._prepare_for_class(_lowerCamelCase    ,     _lowerCamelCase )
        lowerCAmelCase_			=      model(_lowerCamelCase    ,     noise=_lowerCamelCase )

        if model_class.__name__ == "TFViTMAEModel":
           lowerCAmelCase_			=      outputs.last_hidden_state.numpy()
           lowerCAmelCase_			=      0
        else:
           lowerCAmelCase_			=      outputs.logits.numpy()
           lowerCAmelCase_			=      0

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

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

           lowerCAmelCase_			=      np.amax(np.abs(out_a - out_a ) )
           self.assertLessEqual(_lowerCamelCase    ,     1E-5 )





  def 						UpperCAmelCase_	(	self ):
     # make mask reproducible
     np.random.seed(2 )

     lowerCAmelCase_	,lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs_for_common()

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

     for model_class in self.all_model_classes:
        lowerCAmelCase_			=      model_class(_lowerCamelCase )
        lowerCAmelCase_			=      self._prepare_for_class(_lowerCamelCase    ,     _lowerCamelCase )

        lowerCAmelCase_			=      model(_lowerCamelCase    ,     noise=_lowerCamelCase )
        lowerCAmelCase_			=      model.get_config()
        # make sure that returned config is jsonifiable, which is required by keras
        json.dumps(_lowerCamelCase )
        lowerCAmelCase_			=      model_class.from_config(model.get_config() )
        # make sure it also accepts a normal config
        lowerCAmelCase_			=      model_class.from_config(model.config )
        lowerCAmelCase_			=      new_model(_lowerCamelCase )  # Build model
        new_model.set_weights(model.get_weights() )
        lowerCAmelCase_			=      new_model(_lowerCamelCase    ,     noise=_lowerCamelCase )

        self.assert_outputs_same(_lowerCamelCase    ,     _lowerCamelCase )





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





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







  @slow
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      TFViTMAEModel.from_pretrained('''google/vit-base-patch16-224''' )
     self.assertIsNotNone(_lowerCamelCase )






def snake_case_      (					)			->					Dict:
   lowerCAmelCase_			=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
   return image






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





  @cached_property
  def 						UpperCAmelCase_	(	self ):
     return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''' ) if is_vision_available() else None







  @slow
  def 						UpperCAmelCase_	(	self ):
     # make random mask reproducible across the PT and TF model
     np.random.seed(2 )

     lowerCAmelCase_			=      TFViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''' )

     lowerCAmelCase_			=      self.default_image_processor
     lowerCAmelCase_			=      prepare_img()
     lowerCAmelCase_			=      image_processor(images=_lowerCamelCase    ,     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_			=      ViTMAEConfig()
     lowerCAmelCase_			=      int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 )
     lowerCAmelCase_			=      np.random.uniform(size=(1, num_patches) )

     # forward pass
     lowerCAmelCase_			=      model(**_lowerCamelCase    ,     noise=_lowerCamelCase )

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

     lowerCAmelCase_			=      tf.convert_to_tensor(
         [[-0.05_48, -1.70_23, -0.93_25], [0.37_21, -0.56_70, -0.22_33], [0.82_35, -1.38_78, -0.35_24]] )

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






'''simple docstring'''

from __future__ import annotations

from typing import Any



def snake_case_      (					__snake_case  :							list[Any])			->					None:
   create_state_space_tree(__snake_case	,   []	,   0)



def snake_case_      (					__snake_case  :							list[Any]	,   __snake_case  :							list[Any]	,   __snake_case  :							int)			->					None:

   if index == len(__snake_case):
      print(__snake_case)
      return

   create_state_space_tree(__snake_case	,   __snake_case	,   index + 1)
   current_subsequence.append(sequence[index])
   create_state_space_tree(__snake_case	,   __snake_case	,   index + 1)
   current_subsequence.pop()


if __name__ == "__main__":
 A_     :						list[Any]    							=[3, 1, 2, 4]
 generate_all_subsequences(seq)

 seq.clear()
 seq.extend(['''A''', '''B''', '''C'''])
 generate_all_subsequences(seq)





