


'''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    __magic_name__				:






       def __init__(     self    ,				snake_case    ,				snake_case=1_3    ,				snake_case=3_0    ,				snake_case=2    ,				snake_case=3    ,				snake_case=True    ,				snake_case=True    ,				snake_case=3_2    ,				snake_case=5    ,				snake_case=4    ,				snake_case=3_7    ,				snake_case="gelu"    ,				snake_case=0.1    ,				snake_case=0.1    ,				snake_case=1_0    ,				snake_case=0.02    ,				snake_case=3    ,				snake_case=0.6    ,				snake_case=None    ,				)		->    Dict:




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

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






       def      lowerCAmelCase						(     self)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  Dict				       =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

          _UpperCAmelCase  :  Dict				       =None
          if self.use_labels:
             _UpperCAmelCase  :  Any				       =ids_tensor([self.batch_size]    ,				self.type_sequence_label_size)

          _UpperCAmelCase  :  Dict				       =self.get_config()

          return config, pixel_values, labels






       def      lowerCAmelCase						(     self)		->    str:




          '''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    ,				hidden_act=self.hidden_act    ,				hidden_dropout_prob=self.hidden_dropout_prob    ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,				is_decoder=snake_case    ,				initializer_range=self.initializer_range    ,				mask_ratio=self.mask_ratio    ,				)






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case)		->    Tuple:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =ViTMAEModel(config=snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase  :  int				       =model(snake_case)
          self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.hidden_size))






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =ViTMAEForPreTraining(snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase  :  Union[str, Any]				       =model(snake_case)
          _UpperCAmelCase  :  int				       =(self.image_size // self.patch_size) ** 2
          _UpperCAmelCase  :  str				       =self.patch_size**2 * self.num_channels
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, num_patches, expected_num_channels))

          # test greyscale images
          _UpperCAmelCase  :  Union[str, Any]				       =1
          _UpperCAmelCase  :  Any				       =ViTMAEForPreTraining(snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase  :  Dict				       =floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
          _UpperCAmelCase  :  Tuple				       =model(snake_case)
          _UpperCAmelCase  :  Optional[int]				       =self.patch_size**2
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, num_patches, expected_num_channels))






       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =self.prepare_config_and_inputs()
          _UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase  :  List[str]				       =config_and_inputs
          _UpperCAmelCase  :  Optional[Any]				       ={'pixel_values': pixel_values}
          return config, inputs_dict






@require_torch
class    __magic_name__				(						lowerCAmelCase   ,lowerCAmelCase   ,unittest.TestCase							):
       UpperCAmelCase		 =(ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
       UpperCAmelCase		 ={"feature-extraction": ViTMAEModel} if is_torch_available() else {}

       UpperCAmelCase		 =False
       UpperCAmelCase		 =False
       UpperCAmelCase		 =False
       UpperCAmelCase		 =False






       def      lowerCAmelCase						(     self)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  Tuple				       =ViTMAEModelTester(self)
          _UpperCAmelCase  :  Optional[int]				       =ConfigTester(self    ,				config_class=snake_case    ,				has_text_modality=snake_case    ,				hidden_size=3_7)






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          self.config_tester.run_common_tests()






       @unittest.skip(reason='ViTMAE does not use inputs_embeds')
       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          pass






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase       ,			_UpperCAmelCase  :  Tuple				       =self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             _UpperCAmelCase  :  str				       =model_class(snake_case)
             self.assertIsInstance(model.get_input_embeddings()    ,				(nn.Module))
             _UpperCAmelCase  :  Optional[int]				       =model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(snake_case    ,				nn.Linear))






       def      lowerCAmelCase						(     self)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase       ,			_UpperCAmelCase  :  int				       =self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             _UpperCAmelCase  :  Optional[Any]				       =model_class(snake_case)
             _UpperCAmelCase  :  List[Any]				       =inspect.signature(model.forward)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             _UpperCAmelCase  :  Optional[int]				       =[*signature.parameters.keys()]

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






       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[Any]				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case)






       def      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_pretraining(*snake_case)






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case)		->    Any:




          '''simple docstring'''
          # make masks reproducible
          np.random.seed(2)

          _UpperCAmelCase  :  Union[str, Any]				       =int((pt_model.config.image_size // pt_model.config.patch_size) ** 2)
          _UpperCAmelCase  :  str				       =np.random.uniform(size=(self.model_tester.batch_size, num_patches))
          _UpperCAmelCase  :  Tuple				       =torch.from_numpy(snake_case)

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

          super().check_pt_tf_models(snake_case    ,				snake_case    ,				snake_case)






       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          _UpperCAmelCase       ,			_UpperCAmelCase  :  str				       =self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             _UpperCAmelCase  :  Optional[int]				       =model_class(snake_case)
             model.to(snake_case)
             model.eval()
             # make random mask reproducible
             torch.manual_seed(2)
             with torch.no_grad():
                _UpperCAmelCase  :  List[str]				       =model(**self._prepare_for_class(snake_case    ,				snake_case))

             _UpperCAmelCase  :  Any				       =outputs[0].cpu().numpy()
             _UpperCAmelCase  :  Union[str, Any]				       =0

             with tempfile.TemporaryDirectory() as tmpdirname:
                model.save_pretrained(snake_case)
                _UpperCAmelCase  :  Optional[int]				       =model_class.from_pretrained(snake_case)
                model.to(snake_case)
                # make random mask reproducible
                torch.manual_seed(2)
                with torch.no_grad():
                   _UpperCAmelCase  :  Any				       =model(**self._prepare_for_class(snake_case    ,				snake_case))

                # Make sure we don't have nans
                _UpperCAmelCase  :  Union[str, Any]				       =after_outputs[0].cpu().numpy()
                _UpperCAmelCase  :  Union[str, Any]				       =0
                _UpperCAmelCase  :  Any				       =np.amax(np.abs(out_a - out_a))
                self.assertLessEqual(snake_case    ,				1E-5)






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




          '''simple docstring'''
          pass






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




          '''simple docstring'''
          pass






       @unittest.skip(
           reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.')
       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          pass






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




          '''simple docstring'''
          pass






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




          '''simple docstring'''
          pass






       @slow
       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCAmelCase  :  str				       =ViTMAEModel.from_pretrained(snake_case)
             self.assertIsNotNone(snake_case)






def lowerCamelCase__     (		):







   '''simple docstring'''




   _UpperCAmelCase  :  Optional[int]				       =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
   return image






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






       @cached_property
       def      lowerCAmelCase						(     self)		->    Optional[int]:




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






       @slow
       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          # make random mask reproducible across the PT and TF model
          np.random.seed(2)

          _UpperCAmelCase  :  Tuple				       =ViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base').to(snake_case)

          _UpperCAmelCase  :  List[str]				       =self.default_image_processor
          _UpperCAmelCase  :  str				       =prepare_img()
          _UpperCAmelCase  :  Optional[Any]				       =image_processor(images=snake_case    ,				return_tensors='pt').to(snake_case)

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

          # forward pass
          with torch.no_grad():
             _UpperCAmelCase  :  Union[str, Any]				       =model(**snake_case    ,				noise=torch.from_numpy(snake_case).to(device=snake_case))

          # verify the logits
          _UpperCAmelCase  :  Dict				       =torch.Size((1, 1_9_6, 7_6_8))
          self.assertEqual(outputs.logits.shape    ,				snake_case)

          _UpperCAmelCase  :  int				       =torch.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]])

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





'''simple docstring'''






from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


lowercase				=logging.get_logger(__name__)






class    __magic_name__				(						lowerCAmelCase							):
       UpperCAmelCase		 =["input_features", "attention_mask"]






       def __init__(     self    ,				snake_case=8_0    ,				snake_case=1_6_0_0_0    ,				snake_case=8_0    ,				snake_case=0.0    ,				snake_case=True    ,				snake_case=True    ,				snake_case=True    ,				**snake_case    ,				)		->    str:




          '''simple docstring'''
          super().__init__(feature_size=snake_case    ,				sampling_rate=snake_case    ,				padding_value=snake_case    ,				**snake_case)
          _UpperCAmelCase  :  Optional[Any]				       =num_mel_bins
          _UpperCAmelCase  :  Optional[int]				       =do_ceptral_normalize
          _UpperCAmelCase  :  Optional[Any]				       =normalize_means
          _UpperCAmelCase  :  Optional[Any]				       =normalize_vars
          _UpperCAmelCase  :  Tuple				       =True






       def      lowerCAmelCase						(     self    ,				snake_case    ,				)		->    np.ndarray:




          '''simple docstring'''
          _UpperCAmelCase  :  List[Any]				       =waveform * (2**1_5)  # Kaldi compliance: 16-bit signed integers
          _UpperCAmelCase  :  Dict				       =torch.from_numpy(snake_case).unsqueeze(0)
          _UpperCAmelCase  :  Union[str, Any]				       =ta_kaldi.fbank(snake_case    ,				num_mel_bins=self.num_mel_bins    ,				sample_frequency=self.sampling_rate)
          return features.numpy()






       @staticmethod
       def      lowerCAmelCase						(     snake_case    ,				snake_case    ,				snake_case = True    ,				snake_case = True    ,				snake_case = 0.0    ,				)		->    np.ndarray:




          '''simple docstring'''
          # make sure we normalize float32 arrays
          if normalize_means:
             _UpperCAmelCase  :  int				       =x[:input_length].mean(axis=0)
             _UpperCAmelCase  :  List[Any]				       =np.subtract(snake_case    ,				snake_case)
          if normalize_vars:
             _UpperCAmelCase  :  List[Any]				       =x[:input_length].std(axis=0)
             _UpperCAmelCase  :  Optional[int]				       =np.divide(snake_case    ,				snake_case)

          if input_length < x.shape[0]:
             _UpperCAmelCase  :  Dict				       =padding_value

          # make sure array is in float32
          _UpperCAmelCase  :  str				       =x.astype(np.floataa)

          return x






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case = None)		->    List[np.ndarray]:




          '''simple docstring'''
          _UpperCAmelCase  :  str				       =attention_mask.sum(-1) if attention_mask is not None else [x.shape[0] for x in input_features]
          return [
              self.utterance_cmvn(snake_case    ,				snake_case    ,				self.normalize_means    ,				self.normalize_vars    ,				self.padding_value)
              for x, n in zip(snake_case    ,				snake_case)
          ]






       def __call__(     self    ,				snake_case    ,				snake_case = False    ,				snake_case = None    ,				snake_case = False    ,				snake_case = None    ,				snake_case = None    ,				snake_case = None    ,				snake_case = None    ,				**snake_case    ,				)		->    BatchFeature:




          '''simple docstring'''

          if sampling_rate is not None:
             if sampling_rate != self.sampling_rate:
                raise ValueError(
                    f"The model corresponding to this feature extractor: {self} was trained using a sampling rate of"
                    f" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with"
                    f" {self.sampling_rate} and not {sampling_rate}.")
          else:
             logger.warning(
                 'It is strongly recommended to pass the `sampling_rate` argument to this function. '
                 'Failing to do so can result in silent errors that might be hard to debug.')

          _UpperCAmelCase  :  Optional[int]				       =isinstance(snake_case    ,				np.ndarray) and len(raw_speech.shape) > 1
          if is_batched_numpy and len(raw_speech.shape) > 2:
             raise ValueError(f"Only mono-channel audio is supported for input to {self}")
          _UpperCAmelCase  :  List[Any]				       =is_batched_numpy or (
              isinstance(snake_case    ,				(list, tuple)) and (isinstance(raw_speech[0]    ,				(np.ndarray, tuple, list)))
          )

          if is_batched:
             _UpperCAmelCase  :  int				       =[np.asarray(snake_case    ,				dtype=np.floataa) for speech in raw_speech]
          elif not is_batched and not isinstance(snake_case    ,				np.ndarray):
             _UpperCAmelCase  :  Tuple				       =np.asarray(snake_case    ,				dtype=np.floataa)
          elif isinstance(snake_case    ,				np.ndarray) and raw_speech.dtype is np.dtype(np.floataa):
             _UpperCAmelCase  :  int				       =raw_speech.astype(np.floataa)

          # always return batch
          if not is_batched:
             _UpperCAmelCase  :  Dict				       =[raw_speech]

          # extract fbank features
          _UpperCAmelCase  :  Optional[Any]				       =[self._extract_fbank_features(snake_case) for waveform in raw_speech]

          # convert into correct format for padding
          _UpperCAmelCase  :  List[str]				       =BatchFeature({'input_features': features})

          _UpperCAmelCase  :  Any				       =self.pad(
              snake_case    ,				padding=snake_case    ,				max_length=snake_case    ,				truncation=snake_case    ,				pad_to_multiple_of=snake_case    ,				return_attention_mask=snake_case    ,				**snake_case    ,				)

          # make sure list is in array format
          _UpperCAmelCase  :  Dict				       =padded_inputs.get('input_features')
          if isinstance(input_features[0]    ,				snake_case):
             _UpperCAmelCase  :  Any				       =[np.asarray(snake_case    ,				dtype=np.floataa) for feature in input_features]

          _UpperCAmelCase  :  int				       =padded_inputs.get('attention_mask')
          if attention_mask is not None:
             _UpperCAmelCase  :  Tuple				       =[np.asarray(snake_case    ,				dtype=np.intaa) for array in attention_mask]

          # Utterance-level cepstral mean and variance normalization
          if self.do_ceptral_normalize:
             _UpperCAmelCase  :  Optional[Any]				       =(
                 np.array(snake_case    ,				dtype=np.intaa)
                 if self._get_padding_strategies(snake_case    ,				max_length=snake_case) is not PaddingStrategy.DO_NOT_PAD
                 else None
             )
             _UpperCAmelCase  :  Optional[int]				       =self.normalize(
                 padded_inputs['input_features']    ,				attention_mask=snake_case)

          if return_tensors is not None:
             _UpperCAmelCase  :  List[Any]				       =padded_inputs.convert_to_tensors(snake_case)

          return padded_inputs

