'''simple docstring'''




from __future__ import annotations

from collections.abc import Callable
from typing import Generic, TypeVar

__SCREAMING_SNAKE_CASE       :     List[str]     =  TypeVar("""T""")
__SCREAMING_SNAKE_CASE       :     str     =  TypeVar("""U""")

class 	lowerCamelCase_	(Generic[T, U]    ):
      '''simple docstring'''

      def __init__(       self						:				Optional[int]						,					A						:				T | None						,					A						:				U | None       ):
             _UpperCAmelCase     :  Any							      =    key
             _UpperCAmelCase     :  List[str]							      =    val
             _UpperCAmelCase     :  DoubleLinkedListNode[T, U] | None							      =    None
             _UpperCAmelCase     :  DoubleLinkedListNode[T, U] | None							      =    None





      def __repr__(       self						:				str       ):
             return (
                 F"""Node: key: {self.key}, val: {self.val}, """
                 F"""has next: {bool(self.next       )}, has prev: {bool(self.prev       )}"""
             )

class 	lowerCamelCase_	(Generic[T, U]    ):
      '''simple docstring'''

      def __init__(       self						:				Any       ):
             _UpperCAmelCase     :  DoubleLinkedListNode[T, U]							      =    DoubleLinkedListNode(A						,					A       )
             _UpperCAmelCase     :  DoubleLinkedListNode[T, U]							      =    DoubleLinkedListNode(A						,					A       )
             _UpperCAmelCase      ,  _UpperCAmelCase     :  Dict							      =    self.rear, self.head




      def __repr__(       self						:				Optional[int]       ):
             _UpperCAmelCase     :  List[str]							      =    ["DoubleLinkedList"]
             _UpperCAmelCase     :  Tuple							      =    self.head
             while node.next is not None:
                    rep.append(str(A       )       )
                    _UpperCAmelCase     :  str							      =    node.next
             rep.append(str(self.rear       )       )
             return ",\n    ".join(A       )




      def      _A	(       self						:				Union[str, Any]						,					A						:				DoubleLinkedListNode[T, U]       ):
             _UpperCAmelCase     :  Optional[Any]							      =    self.rear.prev

             # All nodes other than self.head are guaranteed to have non-None previous
             assert previous is not None

             _UpperCAmelCase     :  Dict							      =    node
             _UpperCAmelCase     :  List[Any]							      =    previous
             _UpperCAmelCase     :  Optional[int]							      =    node
             _UpperCAmelCase     :  str							      =    self.rear





      def      _A	(       self						:				List[Any]						,					A						:				DoubleLinkedListNode[T, U]       ):

             if node.prev is None or node.next is None:
                    return None

             _UpperCAmelCase     :  str							      =    node.next
             _UpperCAmelCase     :  List[Any]							      =    node.prev
             _UpperCAmelCase     :  Tuple							      =    None
             _UpperCAmelCase     :  List[str]							      =    None
             return node

class 	lowerCamelCase_	(Generic[T, U]    ):
      '''simple docstring'''

      __UpperCamelCase:  dict[Callable[[T], U], LRUCache[T, U]]						=						{}




      def __init__(       self						:				Dict						,					A						:				int       ):
             _UpperCAmelCase     :  DoubleLinkedList[T, U]							      =    DoubleLinkedList()
             _UpperCAmelCase     :  int							      =    capacity
             _UpperCAmelCase     :  Tuple							      =    0
             _UpperCAmelCase     :  Any							      =    0
             _UpperCAmelCase     :  str							      =    0
             _UpperCAmelCase     :  dict[T, DoubleLinkedListNode[T, U]]							      =    {}




      def __repr__(       self						:				List[Any]       ):
             return (
                 F"""CacheInfo(hits={self.hits}, misses={self.miss}, """
                 F"""capacity={self.capacity}, current size={self.num_keys})"""
             )




      def __contains__(       self						:				Optional[Any]						,					A						:				T       ):
             return key in self.cache




      def      _A	(       self						:				int						,					A						:				T       ):
             # Note: pythonic interface would throw KeyError rather than return None

             if key in self.cache:
                    self.hits += 1
                    _UpperCAmelCase     :  DoubleLinkedListNode[T, U]							      =    self.cache[key]
                    _UpperCAmelCase     :  Optional[int]							      =    self.list.remove(self.cache[key]       )
                    assert node == value_node

                    # node is guaranteed not None because it is in self.cache
                    assert node is not None
                    self.list.add(A       )
                    return node.val
             self.miss += 1
             return None




      def      _A	(       self						:				List[str]						,					A						:				T						,					A						:				U       ):

             if key not in self.cache:
                    if self.num_keys >= self.capacity:
                           # delete first node (oldest) when over capacity
                           _UpperCAmelCase     :  Tuple							      =    self.list.head.next

                           # guaranteed to have a non-None first node when num_keys > 0
                           # explain to type checker via assertions
                           assert first_node is not None
                           assert first_node.key is not None
                           assert (
                               self.list.remove(A       ) is not None
                           )  # node guaranteed to be in list assert node.key is not None

                           del self.cache[first_node.key]
                           self.num_keys -= 1
                    _UpperCAmelCase     :  List[str]							      =    DoubleLinkedListNode(A						,					A       )
                    self.list.add(self.cache[key]       )
                    self.num_keys += 1

             else:
                    # bump node to the end of the list, update value
                    _UpperCAmelCase     :  int							      =    self.list.remove(self.cache[key]       )
                    assert node is not None  # node guaranteed to be in list
                    _UpperCAmelCase     :  int							      =    value
                    self.list.add(A       )





      @classmethod
      def      _A	(       cls						:				Optional[int]						,					A						:				int = 128       ):

             def cache_decorator_inner(A						:				Callable[[T], U]       ) -> Callable[..., U]:
                    def cache_decorator_wrapper(*A						:				T       ) -> U:
                           if func not in cls.decorator_function_to_instance_map:
                                  _UpperCAmelCase     :  Tuple							      =    LRUCache(A       )

                           _UpperCAmelCase     :  List[str]							      =    cls.decorator_function_to_instance_map[func].get(args[0]       )
                           if result is None:
                                  _UpperCAmelCase     :  List[Any]							      =    func(*A       )
                                  cls.decorator_function_to_instance_map[func].put(args[0]						,					A       )
                           return result

                    def cache_info() -> LRUCache[T, U]:
                           return cls.decorator_function_to_instance_map[func]

                    setattr(A						,					"cache_info"						,					A       )  # noqa: B010

                    return cache_decorator_wrapper

             return cache_decorator_inner


if __name__ == "__main__":
  import doctest

  doctest.testmod()




'''simple docstring'''






import inspect
import math
import tempfile
import unittest

import numpy as np

from transformers import ViTMAEConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch
 from torch import nn

 from transformers import ViTMAEForPreTraining, ViTMAEModel
 from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import ViTImageProcessor






class 			lowercase_	:




  """simple docstring"""




  def __init__(      self   :     str					,lowercase__   :     Tuple					,lowercase__   :     Dict=1_3					,lowercase__   :     List[str]=3_0					,lowercase__   :     Tuple=2					,lowercase__   :     Optional[int]=3					,lowercase__   :     List[str]=True					,lowercase__   :     Tuple=True					,lowercase__   :     int=3_2					,lowercase__   :     List[str]=5					,lowercase__   :     Tuple=4					,lowercase__   :     Any=3_7					,lowercase__   :     Any="gelu"					,lowercase__   :     Union[str, Any]=0.1					,lowercase__   :     Optional[int]=0.1					,lowercase__   :     str=1_0					,lowercase__   :     Optional[int]=0.0_2					,lowercase__   :     Union[str, Any]=3					,lowercase__   :     Optional[int]=0.6					,lowercase__   :     List[Any]=None					,):
   __lowercase							  = parent
   __lowercase							  = batch_size
   __lowercase							  = image_size
   __lowercase							  = patch_size
   __lowercase							  = num_channels
   __lowercase							  = is_training
   __lowercase							  = use_labels
   __lowercase							  = hidden_size
   __lowercase							  = num_hidden_layers
   __lowercase							  = num_attention_heads
   __lowercase							  = intermediate_size
   __lowercase							  = hidden_act
   __lowercase							  = hidden_dropout_prob
   __lowercase							  = attention_probs_dropout_prob
   __lowercase							  = type_sequence_label_size
   __lowercase							  = initializer_range
   __lowercase							  = mask_ratio
   __lowercase							  = scope

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






  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]			):
   __lowercase							  = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

   __lowercase							  = self.get_config()

   return config, pixel_values, labels






  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[Any]			):
   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					,hidden_act=self.hidden_act					,hidden_dropout_prob=self.hidden_dropout_prob					,attention_probs_dropout_prob=self.attention_probs_dropout_prob					,is_decoder=lowercase__					,initializer_range=self.initializer_range					,mask_ratio=self.mask_ratio					,)






  def 					SCREAMING_SNAKE_CASE      (      self   :     List[Any]					,lowercase__   :     str					,lowercase__   :     Optional[int]					,lowercase__   :     List[str]			):
   __lowercase							  = ViTMAEModel(config=lowercase__			)
   model.to(lowercase__			)
   model.eval()
   __lowercase							  = model(lowercase__			)
   self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.hidden_size)			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Any					,lowercase__   :     List[Any]					,lowercase__   :     int					,lowercase__   :     Optional[Any]			):
   __lowercase							  = ViTMAEForPreTraining(lowercase__			)
   model.to(lowercase__			)
   model.eval()
   __lowercase							  = model(lowercase__			)
   __lowercase							  = (self.image_size // self.patch_size) ** 2
   __lowercase							  = self.patch_size**2 * self.num_channels
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, num_patches, expected_num_channels)			)

   # test greyscale images
   __lowercase							  = 1
   __lowercase							  = ViTMAEForPreTraining(lowercase__			)
   model.to(lowercase__			)
   model.eval()
   __lowercase							  = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
   __lowercase							  = model(lowercase__			)
   __lowercase							  = self.patch_size**2
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, num_patches, expected_num_channels)			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]			):
   __lowercase							  = self.prepare_config_and_inputs()
   __lowercase  ,	__lowercase  ,	__lowercase							  = config_and_inputs
   __lowercase							  = {'''pixel_values''': pixel_values}
   return config, inputs_dict






@require_torch
class 			lowercase_	(lowerCamelCase__				,       lowerCamelCase__				,       unittest.TestCase      ):




  """simple docstring"""




  SCREAMING_SNAKE_CASE    :  str							=			(ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
  SCREAMING_SNAKE_CASE    :  Dict							=			{'feature-extraction': ViTMAEModel} if is_torch_available() else {}

  SCREAMING_SNAKE_CASE    :  Any							=			False
  SCREAMING_SNAKE_CASE    :  Optional[int]							=			False
  SCREAMING_SNAKE_CASE    :  List[str]							=			False
  SCREAMING_SNAKE_CASE    :  Union[str, Any]							=			False






  def 					SCREAMING_SNAKE_CASE      (      self   :     int			):
   __lowercase							  = ViTMAEModelTester(self			)
   __lowercase							  = ConfigTester(self					,config_class=lowercase__					,has_text_modality=lowercase__					,hidden_size=3_7			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]			):
   self.config_tester.run_common_tests()






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






  def 					SCREAMING_SNAKE_CASE      (      self   :     Tuple			):
   __lowercase  ,	__lowercase							  = self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
    __lowercase							  = model_class(lowercase__			)
    self.assertIsInstance(model.get_input_embeddings()					,(nn.Module)			)
    __lowercase							  = model.get_output_embeddings()
    self.assertTrue(x is None or isinstance(lowercase__					,nn.Linear			)			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[Any]			):
   __lowercase  ,	__lowercase							  = self.model_tester.prepare_config_and_inputs_for_common()

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

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






  def 					SCREAMING_SNAKE_CASE      (      self   :     List[Any]			):
   __lowercase							  = self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Any			):
   __lowercase							  = self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_pretraining(*lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Any					,lowercase__   :     Optional[int]					,lowercase__   :     List[str]					,lowercase__   :     Optional[Any]			):
   # make masks reproducible
   np.random.seed(2			)

   __lowercase							  = int((pt_model.config.image_size // pt_model.config.patch_size) ** 2			)
   __lowercase							  = np.random.uniform(size=(self.model_tester.batch_size, num_patches)			)
   __lowercase							  = torch.from_numpy(lowercase__			)

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

   super().check_pt_tf_models(lowercase__					,lowercase__					,lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]			):
   __lowercase  ,	__lowercase							  = self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
    __lowercase							  = model_class(lowercase__			)
    model.to(lowercase__			)
    model.eval()
    # make random mask reproducible
    torch.manual_seed(2			)
    with torch.no_grad():
     __lowercase							  = model(**self._prepare_for_class(lowercase__					,lowercase__			)			)

    __lowercase							  = outputs[0].cpu().numpy()
    __lowercase							  = 0

    with tempfile.TemporaryDirectory() as tmpdirname:
     model.save_pretrained(lowercase__			)
     __lowercase							  = model_class.from_pretrained(lowercase__			)
     model.to(lowercase__			)
     # make random mask reproducible
     torch.manual_seed(2			)
     with torch.no_grad():
      __lowercase							  = model(**self._prepare_for_class(lowercase__					,lowercase__			)			)

     # Make sure we don't have nans
     __lowercase							  = after_outputs[0].cpu().numpy()
     __lowercase							  = 0
     __lowercase							  = np.amax(np.abs(out_a - out_a			)			)
     self.assertLessEqual(lowercase__					,1e-5			)






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






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






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






  @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load'''			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]			):
   pass






  @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Any			):
   pass






  @slow
  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[Any]			):
   for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    __lowercase							  = ViTMAEModel.from_pretrained(lowercase__			)
    self.assertIsNotNone(lowercase__			)


def 		_A     (    ):






 """simple docstring"""



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






@require_torch
@require_vision
class 			lowercase_	(unittest.TestCase      ):




  """simple docstring"""




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






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

   __lowercase							  = ViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base'''			).to(lowercase__			)

   __lowercase							  = self.default_image_processor
   __lowercase							  = prepare_img()
   __lowercase							  = image_processor(images=lowercase__					,return_tensors='''pt'''			).to(lowercase__			)

   # 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)
   __lowercase							  = ViTMAEConfig()
   __lowercase							  = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2			)
   __lowercase							  = np.random.uniform(size=(1, num_patches)			)

   # forward pass
   with torch.no_grad():
    __lowercase							  = model(**lowercase__					,noise=torch.from_numpy(lowercase__			).to(device=lowercase__			)			)

   # verify the logits
   __lowercase							  = torch.Size((1, 1_9_6, 7_6_8)			)
   self.assertEqual(outputs.logits.shape					,lowercase__			)

   __lowercase							  = torch.tensor(
       [[-0.0_5_4_8, -1.7_0_2_3, -0.9_3_2_5], [0.3_7_2_1, -0.5_6_7_0, -0.2_2_3_3], [0.8_2_3_5, -1.3_8_7_8, -0.3_5_2_4]]			)

   self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]					,expected_slice.to(lowercase__			)					,atol=1e-4			)			)

