

"""simple docstring"""



from typing import Dict, List, Optional, Union

import numpy as np

from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy


SCREAMING_SNAKE_CASE_    :		str							     =  logging.get_logger(__name__)



class     a  (				lowerCamelCase__ ):



    """simple docstring"""



    def __init__(   self:       Tuple			,  UpperCamelCase:       int			,  UpperCamelCase:       int			,  UpperCamelCase:       float			,  **UpperCamelCase:       str				):






         """simple docstring"""


         A__								=     feature_size
         A__								=     sampling_rate
         A__								=     padding_value

         A__								=     kwargs.pop("""padding_side"""			,  """right"""				)
         A__								=     kwargs.pop("""return_attention_mask"""			,  lowercase__				)

         super().__init__(**lowercase__				)

    def  UpperCamelCase		(   self:       Dict			,  UpperCamelCase:       Union[
            BatchFeature,
            List[BatchFeature],
            Dict[str, BatchFeature],
            Dict[str, List[BatchFeature]],
            List[Dict[str, BatchFeature]],
        ]			,  UpperCamelCase:       Union[bool, str, PaddingStrategy] = True			,  UpperCamelCase:       Optional[int] = None			,  UpperCamelCase:       bool = False			,  UpperCamelCase:       Optional[int] = None			,  UpperCamelCase:       Optional[bool] = None			,  UpperCamelCase:       Optional[Union[str, TensorType]] = None			,  ):






         """simple docstring"""


         if isinstance(lowercase__			,  (list, tuple)				) and isinstance(processed_features[0]			,  (dict, BatchFeature)				):
              A__								=     {
                  key: [example[key] for example in processed_features] for key in processed_features[0].keys()
              }

         # The model's main input name, usually `input_values`, has be passed for padding
         if self.model_input_names[0] not in processed_features:
              raise ValueError(
                  """You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`"""
                  f""" to this method that includes {self.model_input_names[0]}, but you provided"""
                  f""" {list(processed_features.keys()				)}"""				)

         A__								=     processed_features[self.model_input_names[0]]
         A__								=     (
             return_attention_mask if return_attention_mask is not None else self.return_attention_mask
         )

         if len(lowercase__				) == 0:
              if return_attention_mask:
                   A__								=     []
              return processed_features

         # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays
         # and rebuild them afterwards if no return_tensors is specified
         # Note that we lose the specific device the tensor may be on for PyTorch

         A__								=     required_input[0]
         if isinstance(lowercase__			,  (list, tuple)				):
              # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
              A__								=     0
              while len(required_input[index]				) == 0:
                   index += 1
              if index < len(lowercase__				):
                   A__								=     required_input[index][0]

         if return_tensors is None:
              if is_tf_tensor(lowercase__				):
                   A__								=     """tf"""
              elif is_torch_tensor(lowercase__				):
                   A__								=     """pt"""
              elif isinstance(lowercase__			,  (int, float, list, tuple, np.ndarray)				):
                   A__								=     """np"""
              else:
                   raise ValueError(
                       f"""type of {first_element} unknown: {type(lowercase__				)}. """
                       """Should be one of a python, numpy, pytorch or tensorflow object."""				)

         for key, value in processed_features.items():
              if isinstance(value[0]			,  (int, float)				):
                   A__								=     to_numpy(lowercase__				)
              else:
                   A__								=     [to_numpy(lowercase__				) for v in value]

                      # Convert padding_strategy in PaddingStrategy
         A__								=     self._get_padding_strategies(padding=lowercase__			,  max_length=lowercase__				)

         A__								=     processed_features[self.model_input_names[0]]

         A__								=     len(lowercase__				)
         if not all(len(lowercase__				) == batch_size for v in processed_features.values()				):
              raise ValueError("""Some items in the output dictionary have a different batch size than others."""				)

         A__								=     []
         for i in range(lowercase__				):
              A__								=     {k: v[i] for k, v in processed_features.items()}
              # truncation
              A__								=     self._truncate(
                  lowercase__			,  max_length=lowercase__			,  pad_to_multiple_of=lowercase__			,  truncation=lowercase__			,  )
              truncated_inputs.append(lowercase__				)

         if padding_strategy == PaddingStrategy.LONGEST:
              # make sure that `max_length` cannot be longer than the longest truncated length
              A__								=     max(len(input_slice[self.model_input_names[0]]				) for input_slice in truncated_inputs				)
              A__								=     PaddingStrategy.MAX_LENGTH

         A__								=     {}
         for i in range(lowercase__				):
              # padding
              A__								=     self._pad(
                  truncated_inputs[i]			,  max_length=lowercase__			,  padding_strategy=lowercase__			,  pad_to_multiple_of=lowercase__			,  return_attention_mask=lowercase__			,  )

              for key, value in outputs.items():
                   if key not in batch_outputs:
                        A__								=     []
                   if value.dtype is np.dtype(np.floataa				):
                        A__								=     value.astype(np.floataa				)
                   batch_outputs[key].append(lowercase__				)

         return BatchFeature(lowercase__			,  tensor_type=lowercase__				)

    def  UpperCamelCase		(   self:       str			,  UpperCamelCase:       Union[Dict[str, np.ndarray], BatchFeature]			,  UpperCamelCase:       Optional[int] = None			,  UpperCamelCase:       PaddingStrategy = PaddingStrategy.DO_NOT_PAD			,  UpperCamelCase:       Optional[int] = None			,  UpperCamelCase:       Optional[bool] = None			,  ):






         """simple docstring"""


         A__								=     processed_features[self.model_input_names[0]]

         if padding_strategy == PaddingStrategy.LONGEST:
              A__								=     len(lowercase__				)

         if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
              A__								=     ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

         A__								=     padding_strategy != PaddingStrategy.DO_NOT_PAD and len(lowercase__				) < max_length

         if return_attention_mask and "attention_mask" not in processed_features:
              A__								=     np.ones(len(lowercase__				)			,  dtype=np.intaa				)

         if needs_to_be_padded:
              A__								=     max_length - len(lowercase__				)
              if self.padding_side == "right":
                   if return_attention_mask:
                        A__								=     np.pad(
                            processed_features["""attention_mask"""]			,  (0, difference)				)
                   A__								=     ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
                   A__								=     np.pad(
                       lowercase__			,  lowercase__			,  """constant"""			,  constant_values=self.padding_value				)
              elif self.padding_side == "left":
                   if return_attention_mask:
                        A__								=     np.pad(
                            processed_features["""attention_mask"""]			,  (difference, 0)				)
                   A__								=     ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
                   A__								=     np.pad(
                       lowercase__			,  lowercase__			,  """constant"""			,  constant_values=self.padding_value				)
              else:
                   raise ValueError("""Invalid padding strategy:""" + str(self.padding_side				)				)

         return processed_features

    def  UpperCamelCase		(   self:       Tuple			,  UpperCamelCase:       Union[Dict[str, np.ndarray], BatchFeature]			,  UpperCamelCase:       Optional[int] = None			,  UpperCamelCase:       Optional[int] = None			,  UpperCamelCase:       Optional[bool] = None			,  ):






         """simple docstring"""


         if not truncation:
              return processed_features
         elif truncation and max_length is None:
              raise ValueError("""When setting ``truncation=True``, make sure that ``max_length`` is defined."""				)

         A__								=     processed_features[self.model_input_names[0]]

         # find `max_length` that fits `pad_to_multiple_of`
         if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
              A__								=     ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

         A__								=     len(lowercase__				) > max_length

         if needs_to_be_truncated:
              A__								=     processed_features[self.model_input_names[0]][:max_length]
              if "attention_mask" in processed_features:
                   A__								=     processed_features["""attention_mask"""][:max_length]

         return processed_features





    def  UpperCamelCase		(   self:       List[str]			,  UpperCamelCase:       Dict=False			,  UpperCamelCase:       Dict=None				):






         """simple docstring"""


         if padding is not False:
              if padding is True:
                   A__								=     PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
              elif not isinstance(lowercase__			,  lowercase__				):
                   A__								=     PaddingStrategy(lowercase__				)
              elif isinstance(lowercase__			,  lowercase__				):
                   A__								=     padding
         else:
              A__								=     PaddingStrategy.DO_NOT_PAD

         # Set max length if needed
         if max_length is None:
              if padding_strategy == PaddingStrategy.MAX_LENGTH:
                   raise ValueError(
                       f"""When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined"""				)

                      # Test if we have a padding value
         if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
              raise ValueError(
                  """Asking to pad but the feature_extractor does not have a padding value. Please select a value to use"""
                  """ as `padding_value`. For example: `feature_extractor.padding_value = 0.0`."""				)

         return padding_strategy









'''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			)			)

