


import copy
from typing import Any, Dict, List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


__a     = logging.get_logger(__name__)






class   __lowercase				(  UpperCAmelCase__  ):
      UpperCamelCase								=       ['''input_features''']





      def __init__(		self						:					int					,       __lowerCamelCase						:					Dict=8_0					,       __lowerCamelCase						:					int=1_6_0_0_0					,       __lowerCamelCase						:					Optional[Any]=1_6_0					,       __lowerCamelCase						:					Union[str, Any]=3_0					,       __lowerCamelCase						:					Union[str, Any]=4_0_0					,       __lowerCamelCase						:					Optional[int]=0.0					,       __lowerCamelCase						:					Union[str, Any]=False					,       **__lowerCamelCase						:					Union[str, Any]					,       )    ->   str:






         """simple docstring"""







         super().__init__(
             feature_size=__lowerCAmelCase					,       sampling_rate=__lowerCAmelCase					,       padding_value=__lowerCAmelCase					,       return_attention_mask=__lowerCAmelCase					,       **__lowerCAmelCase					,       )
         UpperCAmelCase							=     n_fft
         UpperCAmelCase							=     hop_length
         UpperCAmelCase							=     chunk_length
         UpperCAmelCase							=     chunk_length * sampling_rate
         UpperCAmelCase							=     self.n_samples // hop_length
         UpperCAmelCase							=     sampling_rate
         UpperCAmelCase							=     mel_filter_bank(
             num_frequency_bins=1 + n_fft // 2					,       num_mel_filters=__lowerCAmelCase					,       min_frequency=0.0					,       max_frequency=8_0_0_0.0					,       sampling_rate=__lowerCAmelCase					,       norm="""slaney"""					,       mel_scale="""slaney"""					,       )





      def        _lowercase				(		self						:					Optional[Any]					,       __lowerCamelCase						:					np.array       )    ->   np.ndarray:






         """simple docstring"""







         UpperCAmelCase							=     spectrogram(
             __lowerCAmelCase					,       window_function(self.n_fft					,       """hann"""       )					,       frame_length=self.n_fft					,       hop_length=self.hop_length					,       power=2.0					,       mel_filters=self.mel_filters					,       log_mel="""log10"""					,       )
         UpperCAmelCase							=     log_spec[:, :-1]
         UpperCAmelCase							=     np.maximum(__lowerCAmelCase					,       log_spec.max() - 8.0       )
         UpperCAmelCase							=     (log_spec + 4.0) / 4.0
         return log_spec





      @staticmethod
      # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
      def        _lowercase				(		__lowerCamelCase						:					List[np.ndarray]					,       __lowerCamelCase						:					List[np.ndarray]					,       __lowerCamelCase						:					float = 0.0       )    ->   List[np.ndarray]:






         """simple docstring"""







         if attention_mask is not None:
            UpperCAmelCase							=     np.array(__lowerCAmelCase					,       np.intaa       )
            UpperCAmelCase							=     []

            for vector, length in zip(__lowerCAmelCase					,       attention_mask.sum(-1       )       ):
               UpperCAmelCase							=     (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7       )
               if length < normed_slice.shape[0]:
                  UpperCAmelCase							=     padding_value

               normed_input_values.append(__lowerCAmelCase       )
         else:
            UpperCAmelCase							=     [(x - x.mean()) / np.sqrt(x.var() + 1e-7       ) for x in input_values]

         return normed_input_values





      def __call__(		self						:					str					,       __lowerCamelCase						:					Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]					,       __lowerCamelCase						:					bool = True					,       __lowerCamelCase						:					Optional[int] = None					,       __lowerCamelCase						:					Optional[Union[str, TensorType]] = None					,       __lowerCamelCase						:					Optional[bool] = None					,       __lowerCamelCase						:					Optional[str] = "max_length"					,       __lowerCamelCase						:					Optional[int] = None					,       __lowerCamelCase						:					Optional[int] = None					,       __lowerCamelCase						:					Optional[bool] = None					,       **__lowerCamelCase						:					str					,       )    ->   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.__class__.__name__} was trained using a"""
                   F""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input"""
                   F""" was sampled with {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							=     isinstance(__lowerCAmelCase					,       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							=     is_batched_numpy or (
             isinstance(__lowerCAmelCase					,       (list, tuple)       ) and (isinstance(raw_speech[0]					,       (np.ndarray, tuple, list)       ))
         )

         if is_batched:
            UpperCAmelCase							=     [np.asarray([speech]					,       dtype=np.floataa       ).T for speech in raw_speech]
         elif not is_batched and not isinstance(__lowerCAmelCase					,       np.ndarray       ):
            UpperCAmelCase							=     np.asarray(__lowerCAmelCase					,       dtype=np.floataa       )
         elif isinstance(__lowerCAmelCase					,       np.ndarray       ) and raw_speech.dtype is np.dtype(np.floataa       ):
            UpperCAmelCase							=     raw_speech.astype(np.floataa       )

         # always return batch
         if not is_batched:
            UpperCAmelCase							=     [np.asarray([raw_speech]       ).T]

         UpperCAmelCase							=     BatchFeature({"""input_features""": raw_speech}       )

         # convert into correct format for padding

         UpperCAmelCase							=     self.pad(
             __lowerCAmelCase					,       padding=__lowerCAmelCase					,       max_length=max_length if max_length else self.n_samples					,       truncation=__lowerCAmelCase					,       pad_to_multiple_of=__lowerCAmelCase					,       return_attention_mask=return_attention_mask or do_normalize					,       )

         # zero-mean and unit-variance normalization
         if do_normalize:
            UpperCAmelCase							=     self.zero_mean_unit_var_norm(
                padded_inputs["""input_features"""]					,       attention_mask=padded_inputs["""attention_mask"""]					,       padding_value=self.padding_value					,       )
            UpperCAmelCase							=     np.stack(padded_inputs["""input_features"""]					,       axis=0       )

         # make sure list is in array format
         UpperCAmelCase							=     padded_inputs.get("""input_features"""       ).transpose(2					,       0					,       1       )

         UpperCAmelCase							=     [self._np_extract_fbank_features(__lowerCAmelCase       ) for waveform in input_features[0]]

         if isinstance(input_features[0]					,       __lowerCAmelCase       ):
            UpperCAmelCase							=     [np.asarray(__lowerCAmelCase					,       dtype=np.floataa       ) for feature in input_features]
         else:
            UpperCAmelCase							=     input_features

         if return_attention_mask:
            # rescale from sample (48000) to feature (3000)
            UpperCAmelCase							=     padded_inputs["""attention_mask"""][:, :: self.hop_length]

         if return_tensors is not None:
            UpperCAmelCase							=     padded_inputs.convert_to_tensors(__lowerCAmelCase       )

         return padded_inputs





      def        _lowercase				(		self						:					str       )    ->   Dict[str, Any]:






         """simple docstring"""







         UpperCAmelCase							=     copy.deepcopy(self.__dict__       )
         UpperCAmelCase							=     self.__class__.__name__
         if "mel_filters" in output:
            del output["mel_filters"]
         return output







from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

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 (
     TFDeiTForImageClassification,
     TFDeiTForImageClassificationWithTeacher,
     TFDeiTForMaskedImageModeling,
     TFDeiTModel,
 )
 from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import DeiTImageProcessor






class   __lowercase				:





      def __init__(		self						:					str					,       __lowerCamelCase						:					Optional[Any]					,       __lowerCamelCase						:					Optional[Any]=1_3					,       __lowerCamelCase						:					Optional[Any]=3_0					,       __lowerCamelCase						:					Any=2					,       __lowerCamelCase						:					List[str]=3					,       __lowerCamelCase						:					str=True					,       __lowerCamelCase						:					str=True					,       __lowerCamelCase						:					Union[str, Any]=3_2					,       __lowerCamelCase						:					Optional[int]=2					,       __lowerCamelCase						:					Optional[int]=4					,       __lowerCamelCase						:					Union[str, Any]=3_7					,       __lowerCamelCase						:					str="gelu"					,       __lowerCamelCase						:					str=0.1					,       __lowerCamelCase						:					List[str]=0.1					,       __lowerCamelCase						:					str=1_0					,       __lowerCamelCase						:					Union[str, Any]=0.02					,       __lowerCamelCase						:					List[str]=3					,       __lowerCamelCase						:					Optional[Any]=None					,       __lowerCamelCase						:					Any=2					,       )    ->   Tuple:






         """simple docstring"""







         UpperCAmelCase							=     parent
         UpperCAmelCase							=     batch_size
         UpperCAmelCase							=     image_size
         UpperCAmelCase							=     patch_size
         UpperCAmelCase							=     num_channels
         UpperCAmelCase							=     is_training
         UpperCAmelCase							=     use_labels
         UpperCAmelCase							=     hidden_size
         UpperCAmelCase							=     num_hidden_layers
         UpperCAmelCase							=     num_attention_heads
         UpperCAmelCase							=     intermediate_size
         UpperCAmelCase							=     hidden_act
         UpperCAmelCase							=     hidden_dropout_prob
         UpperCAmelCase							=     attention_probs_dropout_prob
         UpperCAmelCase							=     type_sequence_label_size
         UpperCAmelCase							=     initializer_range
         UpperCAmelCase							=     scope
         UpperCAmelCase							=     encoder_stride

         # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
         UpperCAmelCase							=     (image_size // patch_size) ** 2
         UpperCAmelCase							=     num_patches + 2





      def        _lowercase				(		self						:					Tuple       )    ->   int:






         """simple docstring"""







         UpperCAmelCase							=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

         UpperCAmelCase							=     self.get_config()

         return config, pixel_values, labels





      def        _lowercase				(		self						:					Tuple       )    ->   str:






         """simple docstring"""







         return DeiTConfig(
             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=__lowerCamelCase					,       initializer_range=self.initializer_range					,       encoder_stride=self.encoder_stride					,       )





      def        _lowercase				(		self						:					Any					,       __lowerCamelCase						:					Optional[int]					,       __lowerCamelCase						:					Any					,       __lowerCamelCase						:					int       )    ->   Any:






         """simple docstring"""







         UpperCAmelCase							=     TFDeiTModel(config=__lowerCamelCase       )
         UpperCAmelCase							=     model(__lowerCamelCase       )
         self.parent.assertEqual(result.last_hidden_state.shape					,       (self.batch_size, self.seq_length, self.hidden_size)       )





      def        _lowercase				(		self						:					Optional[Any]					,       __lowerCamelCase						:					int					,       __lowerCamelCase						:					Any					,       __lowerCamelCase						:					Optional[Any]       )    ->   Union[str, Any]:






         """simple docstring"""







         UpperCAmelCase							=     TFDeiTForMaskedImageModeling(config=__lowerCamelCase       )
         UpperCAmelCase							=     model(__lowerCamelCase       )
         self.parent.assertEqual(
             result.reconstruction.shape					,       (self.batch_size, self.num_channels, self.image_size, self.image_size)       )

         # test greyscale images
         UpperCAmelCase							=     1
         UpperCAmelCase							=     TFDeiTForMaskedImageModeling(__lowerCamelCase       )

         UpperCAmelCase							=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
         UpperCAmelCase							=     model(__lowerCamelCase       )
         self.parent.assertEqual(result.reconstruction.shape					,       (self.batch_size, 1, self.image_size, self.image_size)       )





      def        _lowercase				(		self						:					Union[str, Any]					,       __lowerCamelCase						:					List[Any]					,       __lowerCamelCase						:					Dict					,       __lowerCamelCase						:					int       )    ->   Any:






         """simple docstring"""







         UpperCAmelCase							=     self.type_sequence_label_size
         UpperCAmelCase							=     TFDeiTForImageClassification(__lowerCamelCase       )
         UpperCAmelCase							=     model(__lowerCamelCase					,       labels=__lowerCamelCase       )
         self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.type_sequence_label_size)       )

         # test greyscale images
         UpperCAmelCase							=     1
         UpperCAmelCase							=     TFDeiTForImageClassification(__lowerCamelCase       )

         UpperCAmelCase							=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
         UpperCAmelCase							=     model(__lowerCamelCase					,       labels=__lowerCamelCase       )
         self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.type_sequence_label_size)       )





      def        _lowercase				(		self						:					Tuple       )    ->   List[str]:






         """simple docstring"""







         UpperCAmelCase							=     self.prepare_config_and_inputs()
         UpperCAmelCase				,  UpperCAmelCase				,  UpperCAmelCase							=     config_and_inputs
         UpperCAmelCase							=     {"""pixel_values""": pixel_values}
         return config, inputs_dict






@require_tf
class   __lowercase				(  __snake_case ,					__snake_case ,					unittest.TestCase  ):
      UpperCamelCase								=       (
          (
              TFDeiTModel,
              TFDeiTForImageClassification,
              TFDeiTForImageClassificationWithTeacher,
              TFDeiTForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      UpperCamelCase								=       (
          {
              '''feature-extraction''': TFDeiTModel,
              '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
          }
          if is_tf_available()
          else {}
      )

      UpperCamelCase								=       False
      UpperCamelCase								=       False
      UpperCamelCase								=       False
      UpperCamelCase								=       False





      def        _lowercase				(		self						:					str       )    ->   str:






         """simple docstring"""







         UpperCAmelCase							=     TFDeiTModelTester(self       )
         UpperCAmelCase							=     ConfigTester(self					,       config_class=__lowerCamelCase					,       has_text_modality=__lowerCamelCase					,       hidden_size=3_7       )





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






         """simple docstring"""







         self.config_tester.run_common_tests()





      @unittest.skip(reason="""DeiT does not use inputs_embeds"""       )
      def        _lowercase				(		self						:					List[Any]       )    ->   Dict:






         """simple docstring"""







         pass





      def        _lowercase				(		self						:					str       )    ->   List[Any]:






         """simple docstring"""







         UpperCAmelCase				,  UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs_for_common()

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





      def        _lowercase				(		self						:					Optional[Any]       )    ->   List[str]:






         """simple docstring"""







         UpperCAmelCase				,  UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs_for_common()

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

            UpperCAmelCase							=     ["""pixel_values"""]
            self.assertListEqual(arg_names[:1]					,       __lowerCamelCase       )





      def        _lowercase				(		self						:					Optional[int]       )    ->   Tuple:






         """simple docstring"""







         UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__lowerCamelCase       )





      def        _lowercase				(		self						:					Any       )    ->   Optional[Any]:






         """simple docstring"""







         UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCamelCase       )





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






         """simple docstring"""







         UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase       )





      def        _lowercase				(		self						:					Optional[int]					,       __lowerCamelCase						:					List[Any]					,       __lowerCamelCase						:					List[Any]					,       __lowerCamelCase						:					Any=False       )    ->   int:






         """simple docstring"""







         UpperCAmelCase							=     super()._prepare_for_class(__lowerCamelCase					,       __lowerCamelCase					,       return_labels=__lowerCamelCase       )

         if return_labels:
            if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call       ).parameters:
               del inputs_dict["labels"]

         return inputs_dict





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






         """simple docstring"""







         for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCAmelCase							=     TFDeiTModel.from_pretrained(__lowerCamelCase       )
            self.assertIsNotNone(__lowerCamelCase       )

def   _UpperCamelCase				(						)						->Tuple:
   UpperCAmelCase							=     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""   )
   return image







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





      @cached_property
      def        _lowercase				(		self						:					List[str]       )    ->   Union[str, Any]:






         """simple docstring"""







         return (
             DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""       )
             if is_vision_available()
             else None
         )





      @slow
      def        _lowercase				(		self						:					Optional[int]       )    ->   List[str]:






         """simple docstring"""







         UpperCAmelCase							=     TFDeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""       )

         UpperCAmelCase							=     self.default_image_processor
         UpperCAmelCase							=     prepare_img()
         UpperCAmelCase							=     image_processor(images=__lowerCamelCase					,       return_tensors="""tf"""       )

         # forward pass
         UpperCAmelCase							=     model(**__lowerCamelCase       )

         # verify the logits
         UpperCAmelCase							=     tf.TensorShape((1, 1_0_0_0)       )
         self.assertEqual(outputs.logits.shape					,       __lowerCamelCase       )

         UpperCAmelCase							=     tf.constant([-1.0_266, 0.1_912, -1.2_861]       )

         self.assertTrue(np.allclose(outputs.logits[0, :3]					,       __lowerCamelCase					,       atol=1e-4       )       )



