




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

import numpy as np

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


_lowerCamelCase	:		int							       =   logging.get_logger(__name__)


class 				UpperCamelCase_    ( UpperCAmelCase__     ):

    '''simple docstring'''






    UpperCAmelCase__							  =      ['''input_values''', '''attention_mask''']


    def __init__(			self      :	Optional[int]					, UpperCAmelCase__      :	int = 1					, UpperCAmelCase__      :	int = 16_000					, UpperCAmelCase__      :	float = 0.0					, UpperCAmelCase__      :	bool = False					, UpperCAmelCase__      :	int = 80					, UpperCAmelCase__      :	int = 16					, UpperCAmelCase__      :	int = 64					, UpperCAmelCase__      :	str = "hann_window"					, UpperCAmelCase__      :	float = 1.0					, UpperCAmelCase__      :	float = 80					, UpperCAmelCase__      :	float = 7_600					, UpperCAmelCase__      :	float = 1e-10					, UpperCAmelCase__      :	int = 2					, UpperCAmelCase__      :	bool = True					, **UpperCAmelCase__      :	int					, )						->int:





           '''simple docstring'''

           super().__init__(feature_size=UpperCAmelCase__					, sampling_rate=UpperCAmelCase__					, padding_value=UpperCAmelCase__					, **UpperCAmelCase__)
           A__       =    do_normalize
           A__       =    return_attention_mask

           A__       =    num_mel_bins
           A__       =    hop_length
           A__       =    win_length
           A__       =    win_function
           A__       =    frame_signal_scale
           A__       =    fmin
           A__       =    fmax
           A__       =    mel_floor
           A__       =    reduction_factor

           A__       =    win_length * sampling_rate // 1_000
           A__       =    hop_length * sampling_rate // 1_000
           A__       =    optimal_fft_length(self.sample_size)
           A__       =    (self.n_fft // 2) + 1

           A__       =    window_function(window_length=self.sample_size					, name=self.win_function					, periodic=UpperCAmelCase__)

           A__       =    mel_filter_bank(
               num_frequency_bins=self.n_freqs					, num_mel_filters=self.num_mel_bins					, min_frequency=self.fmin					, max_frequency=self.fmax					, sampling_rate=self.sampling_rate					, norm='''slaney'''					, mel_scale='''slaney'''					, )

           if frame_signal_scale != 1.0:
                  warnings.warn(
                      '''The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers'''					, UpperCAmelCase__					, )
           if reduction_factor != 2.0:
                  warnings.warn(
                      '''The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers'''					, UpperCAmelCase__					, )


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





           '''simple docstring'''

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

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

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

           return normed_input_values


    def        SCREAMING_SNAKE_CASE							(			self      :	Optional[Any]					, UpperCAmelCase__      :	np.ndarray					, )						->np.ndarray:





           '''simple docstring'''

           A__       =    spectrogram(
               UpperCAmelCase__					, window=self.window					, frame_length=self.sample_size					, hop_length=self.sample_stride					, fft_length=self.n_fft					, mel_filters=self.mel_filters					, mel_floor=self.mel_floor					, log_mel='''log10'''					, )
           return log_mel_spec.T


    def __call__(			self      :	Optional[int]					, UpperCAmelCase__      :	Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None					, UpperCAmelCase__      :	Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None					, UpperCAmelCase__      :	Union[bool, str, PaddingStrategy] = False					, UpperCAmelCase__      :	Optional[int] = None					, UpperCAmelCase__      :	bool = False					, UpperCAmelCase__      :	Optional[int] = None					, UpperCAmelCase__      :	Optional[bool] = None					, UpperCAmelCase__      :	Optional[Union[str, TensorType]] = None					, UpperCAmelCase__      :	Optional[int] = None					, **UpperCAmelCase__      :	int					, )						->BatchFeature:





           '''simple docstring'''

           if audio is None and audio_target is None:
                  raise ValueError('''You must provide either `audio` or `audio_target` values.''')

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

           if audio is not None:
                  A__       =    self._process_audio(
                      UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, **UpperCAmelCase__					, )
           else:
                  A__       =    None

           if audio_target is not None:
                  A__       =    self._process_audio(
                      UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, **UpperCAmelCase__					, )

                  if inputs is None:
                         return inputs_target
                  else:
                         A__       =    inputs_target['''input_values''']
                         A__       =    inputs_target.get('''attention_mask''')
                         if decoder_attention_mask is not None:
                                A__       =    decoder_attention_mask

           return inputs


    def        SCREAMING_SNAKE_CASE							(			self      :	List[str]					, UpperCAmelCase__      :	Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]					, UpperCAmelCase__      :	bool = False					, UpperCAmelCase__      :	Union[bool, str, PaddingStrategy] = False					, UpperCAmelCase__      :	Optional[int] = None					, UpperCAmelCase__      :	bool = False					, UpperCAmelCase__      :	Optional[int] = None					, UpperCAmelCase__      :	Optional[bool] = None					, UpperCAmelCase__      :	Optional[Union[str, TensorType]] = None					, **UpperCAmelCase__      :	int					, )						->BatchFeature:





           '''simple docstring'''

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

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

           # always return batch
           if not is_batched:
                  A__       =    [speech]

           # needed to make pad() work on spectrogram inputs
           A__       =    self.feature_size

           # convert into correct format for padding
           if is_target:
                  A__       =    [self._extract_mel_features(UpperCAmelCase__) for waveform in speech]
                  A__       =    BatchFeature({'''input_values''': features})
                  A__       =    self.num_mel_bins
           else:
                  A__       =    BatchFeature({'''input_values''': speech})

           A__       =    self.pad(
               UpperCAmelCase__					, padding=UpperCAmelCase__					, max_length=UpperCAmelCase__					, truncation=UpperCAmelCase__					, pad_to_multiple_of=UpperCAmelCase__					, return_attention_mask=UpperCAmelCase__					, **UpperCAmelCase__					, )

           A__       =    feature_size_hack

           # convert input values to correct format
           A__       =    padded_inputs['''input_values''']
           if not isinstance(input_values[0]					, np.ndarray):
                  A__       =    [np.asarray(UpperCAmelCase__					, dtype=np.floataa) for array in input_values]
           elif (
               not isinstance(UpperCAmelCase__					, np.ndarray)
               and isinstance(input_values[0]					, np.ndarray)
               and input_values[0].dtype is np.dtype(np.floataa)
           ):
                  A__       =    [array.astype(np.floataa) for array in input_values]
           elif isinstance(UpperCAmelCase__					, np.ndarray) and input_values.dtype is np.dtype(np.floataa):
                  A__       =    input_values.astype(np.floataa)

           # convert attention_mask to correct format
           A__       =    padded_inputs.get('''attention_mask''')
           if attention_mask is not None:
                  A__       =    [np.asarray(UpperCAmelCase__					, dtype=np.intaa) for array in attention_mask]

           # zero-mean and unit-variance normalization
           if not is_target and self.do_normalize:
                  A__       =    (
                      attention_mask
                      if self._get_padding_strategies(UpperCAmelCase__					, max_length=UpperCAmelCase__) is not PaddingStrategy.DO_NOT_PAD
                      else None
                  )
                  A__       =    self.zero_mean_unit_var_norm(
                      padded_inputs['''input_values''']					, attention_mask=UpperCAmelCase__					, padding_value=self.padding_value)

           if return_tensors is not None:
                  A__       =    padded_inputs.convert_to_tensors(UpperCAmelCase__)

           return padded_inputs





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





           '''simple docstring'''

           A__       =    super().to_dict()

           # Don't serialize these as they are derived from the other properties.
           A__       =    ['''window''', '''mel_filters''', '''sample_size''', '''sample_stride''', '''n_fft''', '''n_freqs''']
           for name in names:
                  if name in output:
                         del output[name]

           return output






"""simple docstring"""





import collections
import inspect
import unittest

from transformers import SwinvaConfig
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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch
 from torch import nn

 from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
 from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor



class 						snake_case_:






     def __init__(		self     :    Dict    , UpperCamelCase_     :    str    , UpperCamelCase_     :    Dict=1_3    , UpperCamelCase_     :    Union[str, Any]=3_2    , UpperCamelCase_     :    str=2    , UpperCamelCase_     :    int=3    , UpperCamelCase_     :    Any=1_6    , UpperCamelCase_     :    int=[1, 2, 1]    , UpperCamelCase_     :    Optional[int]=[2, 2, 4]    , UpperCamelCase_     :    Any=2    , UpperCamelCase_     :    Any=2.0    , UpperCamelCase_     :    Union[str, Any]=True    , UpperCamelCase_     :    int=0.0    , UpperCamelCase_     :    Optional[Any]=0.0    , UpperCamelCase_     :    Any=0.1    , UpperCamelCase_     :    Tuple="gelu"    , UpperCamelCase_     :    Union[str, Any]=False    , UpperCamelCase_     :    Any=True    , UpperCamelCase_     :    List[Any]=0.02    , UpperCamelCase_     :    Tuple=1E-5    , UpperCamelCase_     :    Optional[int]=True    , UpperCamelCase_     :    List[Any]=None    , UpperCamelCase_     :    str=True    , UpperCamelCase_     :    List[Any]=1_0    , UpperCamelCase_     :    Dict=8    , ):
           lowerCAmelCase    :		Union[str, Any]				=      parent
           lowerCAmelCase    :		int				=      batch_size
           lowerCAmelCase    :		List[str]				=      image_size
           lowerCAmelCase    :		Union[str, Any]				=      patch_size
           lowerCAmelCase    :		int				=      num_channels
           lowerCAmelCase    :		Any				=      embed_dim
           lowerCAmelCase    :		Any				=      depths
           lowerCAmelCase    :		Any				=      num_heads
           lowerCAmelCase    :		int				=      window_size
           lowerCAmelCase    :		List[Any]				=      mlp_ratio
           lowerCAmelCase    :		int				=      qkv_bias
           lowerCAmelCase    :		Optional[Any]				=      hidden_dropout_prob
           lowerCAmelCase    :		str				=      attention_probs_dropout_prob
           lowerCAmelCase    :		str				=      drop_path_rate
           lowerCAmelCase    :		Union[str, Any]				=      hidden_act
           lowerCAmelCase    :		int				=      use_absolute_embeddings
           lowerCAmelCase    :		Union[str, Any]				=      patch_norm
           lowerCAmelCase    :		int				=      layer_norm_eps
           lowerCAmelCase    :		str				=      initializer_range
           lowerCAmelCase    :		Optional[int]				=      is_training
           lowerCAmelCase    :		int				=      scope
           lowerCAmelCase    :		List[str]				=      use_labels
           lowerCAmelCase    :		str				=      type_sequence_label_size
           lowerCAmelCase    :		Union[str, Any]				=      encoder_stride






     def    lowerCamelCase__   (		self     :    Any ):
           lowerCAmelCase    :		str				=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

           lowerCAmelCase    :		Union[str, Any]				=      None
           if self.use_labels:
                 lowerCAmelCase    :		Union[str, Any]				=      ids_tensor([self.batch_size]    , self.type_sequence_label_size )

           lowerCAmelCase    :		Tuple				=      self.get_config()

           return config, pixel_values, labels






     def    lowerCamelCase__   (		self     :    List[Any] ):
           return SwinvaConfig(
               image_size=self.image_size    , patch_size=self.patch_size    , num_channels=self.num_channels    , embed_dim=self.embed_dim    , depths=self.depths    , num_heads=self.num_heads    , window_size=self.window_size    , mlp_ratio=self.mlp_ratio    , qkv_bias=self.qkv_bias    , hidden_dropout_prob=self.hidden_dropout_prob    , attention_probs_dropout_prob=self.attention_probs_dropout_prob    , drop_path_rate=self.drop_path_rate    , hidden_act=self.hidden_act    , use_absolute_embeddings=self.use_absolute_embeddings    , path_norm=self.patch_norm    , layer_norm_eps=self.layer_norm_eps    , initializer_range=self.initializer_range    , encoder_stride=self.encoder_stride    , )






     def    lowerCamelCase__   (		self     :    Union[str, Any]    , UpperCamelCase_     :    Any    , UpperCamelCase_     :    str    , UpperCamelCase_     :    Dict ):
           lowerCAmelCase    :		List[str]				=      SwinvaModel(config=UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()
           lowerCAmelCase    :		List[str]				=      model(UpperCamelCase_ )

           lowerCAmelCase    :		Tuple				=      ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1))
           lowerCAmelCase    :		List[Any]				=      int(config.embed_dim * 2 ** (len(config.depths ) - 1) )

           self.parent.assertEqual(result.last_hidden_state.shape    , (self.batch_size, expected_seq_len, expected_dim) )






     def    lowerCamelCase__   (		self     :    Tuple    , UpperCamelCase_     :    int    , UpperCamelCase_     :    str    , UpperCamelCase_     :    Optional[int] ):
           lowerCAmelCase    :		Tuple				=      SwinvaForMaskedImageModeling(config=UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()
           lowerCAmelCase    :		Dict				=      model(UpperCamelCase_ )
           self.parent.assertEqual(
               result.logits.shape    , (self.batch_size, self.num_channels, self.image_size, self.image_size) )

           # test greyscale images
           lowerCAmelCase    :		List[Any]				=      1
           lowerCAmelCase    :		List[str]				=      SwinvaForMaskedImageModeling(UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()

           lowerCAmelCase    :		int				=      floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
           lowerCAmelCase    :		int				=      model(UpperCamelCase_ )
           self.parent.assertEqual(result.logits.shape    , (self.batch_size, 1, self.image_size, self.image_size) )






     def    lowerCamelCase__   (		self     :    Union[str, Any]    , UpperCamelCase_     :    Tuple    , UpperCamelCase_     :    List[str]    , UpperCamelCase_     :    int ):
           lowerCAmelCase    :		List[str]				=      self.type_sequence_label_size
           lowerCAmelCase    :		Optional[Any]				=      SwinvaForImageClassification(UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()
           lowerCAmelCase    :		Optional[int]				=      model(UpperCamelCase_    , labels=UpperCamelCase_ )
           self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.type_sequence_label_size) )






     def    lowerCamelCase__   (		self     :    str ):
           lowerCAmelCase    :		Optional[int]				=      self.prepare_config_and_inputs()
           lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase    :		str				=      config_and_inputs
           lowerCAmelCase    :		Dict				=      {'''pixel_values''': pixel_values}
           return config, inputs_dict



@require_torch
class 						snake_case_(				a__							,    a__							,    unittest.TestCase	):
     __UpperCamelCase     =    (
         (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
     )
     __UpperCamelCase     =    (
         {'''feature-extraction''': SwinvaModel, '''image-classification''': SwinvaForImageClassification}
         if is_torch_available()
         else {}
     )

     __UpperCamelCase     =    False
     __UpperCamelCase     =    False
     __UpperCamelCase     =    False
     __UpperCamelCase     =    False






     def    lowerCamelCase__   (		self     :    int ):
           lowerCAmelCase    :		Dict				=      SwinvaModelTester(self )
           lowerCAmelCase    :		List[str]				=      ConfigTester(self    , config_class=UpperCamelCase_    , embed_dim=3_7 )






     def    lowerCamelCase__   (		self     :    Optional[int] ):
           self.config_tester.create_and_test_config_to_json_string()
           self.config_tester.create_and_test_config_to_json_file()
           self.config_tester.create_and_test_config_from_and_save_pretrained()
           self.config_tester.create_and_test_config_with_num_labels()
           self.config_tester.check_config_can_be_init_without_params()
           self.config_tester.check_config_arguments_init()






     def    lowerCamelCase__   (		self     :    List[str] ):
           lowerCAmelCase    :		Tuple				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase_ )






     @unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.''' )
     def    lowerCamelCase__   (		self     :    Dict ):
           pass






     @unittest.skip(reason='''Swinv2 does not use inputs_embeds''' )
     def    lowerCamelCase__   (		self     :    int ):
           pass






     def    lowerCamelCase__   (		self     :    List[Any] ):
           lowerCAmelCase,					lowerCAmelCase    :		Dict				=      self.model_tester.prepare_config_and_inputs_for_common()

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






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           lowerCAmelCase,					lowerCAmelCase    :		Tuple				=      self.model_tester.prepare_config_and_inputs_for_common()

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

                 lowerCAmelCase    :		int				=      ['''pixel_values''']
                 self.assertListEqual(arg_names[:1]    , UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    Tuple ):
           lowerCAmelCase,					lowerCAmelCase    :		Dict				=      self.model_tester.prepare_config_and_inputs_for_common()
           lowerCAmelCase    :		Optional[Any]				=      True

           for model_class in self.all_model_classes:
                 lowerCAmelCase    :		Any				=      True
                 lowerCAmelCase    :		List[str]				=      False
                 lowerCAmelCase    :		int				=      True
                 lowerCAmelCase    :		int				=      model_class(UpperCamelCase_ )
                 model.to(UpperCamelCase_ )
                 model.eval()
                 with torch.no_grad():
                       lowerCAmelCase    :		Optional[Any]				=      model(**self._prepare_for_class(UpperCamelCase_    , UpperCamelCase_ ) )
                 lowerCAmelCase    :		str				=      outputs.attentions
                 lowerCAmelCase    :		int				=      len(self.model_tester.depths )
                 self.assertEqual(len(UpperCamelCase_ )    , UpperCamelCase_ )

                 # check that output_attentions also work using config
                 del inputs_dict["output_attentions"]
                 lowerCAmelCase    :		Any				=      True
                 lowerCAmelCase    :		Union[str, Any]				=      config.window_size**2
                 lowerCAmelCase    :		int				=      model_class(UpperCamelCase_ )
                 model.to(UpperCamelCase_ )
                 model.eval()
                 with torch.no_grad():
                       lowerCAmelCase    :		Optional[int]				=      model(**self._prepare_for_class(UpperCamelCase_    , UpperCamelCase_ ) )
                 lowerCAmelCase    :		Dict				=      outputs.attentions
                 self.assertEqual(len(UpperCamelCase_ )    , UpperCamelCase_ )

                 self.assertListEqual(
                     list(attentions[0].shape[-3:] )    , [self.model_tester.num_heads[0], window_size_squared, window_size_squared]    , )
                 lowerCAmelCase    :		str				=      len(UpperCamelCase_ )

                 # Check attention is always last and order is fine
                 lowerCAmelCase    :		Optional[int]				=      True
                 lowerCAmelCase    :		int				=      True
                 lowerCAmelCase    :		Optional[Any]				=      model_class(UpperCamelCase_ )
                 model.to(UpperCamelCase_ )
                 model.eval()
                 with torch.no_grad():
                       lowerCAmelCase    :		Tuple				=      model(**self._prepare_for_class(UpperCamelCase_    , UpperCamelCase_ ) )

                 if hasattr(self.model_tester    , '''num_hidden_states_types''' ):
                       lowerCAmelCase    :		List[Any]				=      self.model_tester.num_hidden_states_types
                 else:
                       # also another +1 for reshaped_hidden_states
                       lowerCAmelCase    :		Union[str, Any]				=      2
                 self.assertEqual(out_len + added_hidden_states    , len(UpperCamelCase_ ) )

                 lowerCAmelCase    :		List[str]				=      outputs.attentions

                 self.assertEqual(len(UpperCamelCase_ )    , UpperCamelCase_ )

                 self.assertListEqual(
                     list(self_attentions[0].shape[-3:] )    , [self.model_tester.num_heads[0], window_size_squared, window_size_squared]    , )






     def    lowerCamelCase__   (		self     :    int    , UpperCamelCase_     :    Tuple    , UpperCamelCase_     :    Dict    , UpperCamelCase_     :    List[Any]    , UpperCamelCase_     :    Optional[Any] ):
           lowerCAmelCase    :		int				=      model_class(UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()

           with torch.no_grad():
                 lowerCAmelCase    :		Union[str, Any]				=      model(**self._prepare_for_class(UpperCamelCase_    , UpperCamelCase_ ) )

           lowerCAmelCase    :		str				=      outputs.hidden_states

           lowerCAmelCase    :		List[str]				=      getattr(
               self.model_tester    , '''expected_num_hidden_layers'''    , len(self.model_tester.depths ) + 1 )
           self.assertEqual(len(UpperCamelCase_ )    , UpperCamelCase_ )

           # Swinv2 has a different seq_length
           lowerCAmelCase    :		Any				=      (
               config.patch_size
               if isinstance(config.patch_size    , collections.abc.Iterable )
               else (config.patch_size, config.patch_size)
           )

           lowerCAmelCase    :		str				=      (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

           self.assertListEqual(
               list(hidden_states[0].shape[-2:] )    , [num_patches, self.model_tester.embed_dim]    , )

           lowerCAmelCase    :		List[str]				=      outputs.reshaped_hidden_states
           self.assertEqual(len(UpperCamelCase_ )    , UpperCamelCase_ )

           lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase    :		str				=      reshaped_hidden_states[0].shape
           lowerCAmelCase    :		Optional[Any]				=      (
               reshaped_hidden_states[0].view(UpperCamelCase_    , UpperCamelCase_    , height * width ).permute(0    , 2    , 1 )
           )
           self.assertListEqual(
               list(reshaped_hidden_states.shape[-2:] )    , [num_patches, self.model_tester.embed_dim]    , )






     def    lowerCamelCase__   (		self     :    Optional[int] ):
           lowerCAmelCase,					lowerCAmelCase    :		Union[str, Any]				=      self.model_tester.prepare_config_and_inputs_for_common()

           lowerCAmelCase    :		Any				=      (
               self.model_tester.image_size
               if isinstance(self.model_tester.image_size    , collections.abc.Iterable )
               else (self.model_tester.image_size, self.model_tester.image_size)
           )

           for model_class in self.all_model_classes:
                 lowerCAmelCase    :		Union[str, Any]				=      True
                 self.check_hidden_states_output(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_ )

                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 lowerCAmelCase    :		Tuple				=      True

                 self.check_hidden_states_output(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           lowerCAmelCase,					lowerCAmelCase    :		Union[str, Any]				=      self.model_tester.prepare_config_and_inputs_for_common()
           lowerCAmelCase    :		Dict				=      3

           lowerCAmelCase    :		Dict				=      (
               self.model_tester.image_size
               if isinstance(self.model_tester.image_size    , collections.abc.Iterable )
               else (self.model_tester.image_size, self.model_tester.image_size)
           )
           lowerCAmelCase    :		Dict				=      (
               config.patch_size
               if isinstance(config.patch_size    , collections.abc.Iterable )
               else (config.patch_size, config.patch_size)
           )

           lowerCAmelCase    :		List[str]				=      image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
           lowerCAmelCase    :		Tuple				=      image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

           for model_class in self.all_model_classes:
                 lowerCAmelCase    :		str				=      True
                 self.check_hidden_states_output(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_    , (padded_height, padded_width) )

                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 lowerCAmelCase    :		Optional[int]				=      True
                 self.check_hidden_states_output(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_    , (padded_height, padded_width) )






     def    lowerCamelCase__   (		self     :    int ):
           lowerCAmelCase    :		str				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    str ):
           lowerCAmelCase    :		Dict				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_ )






     @slow
     def    lowerCamelCase__   (		self     :    int ):
           for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCAmelCase    :		int				=      SwinvaModel.from_pretrained(UpperCamelCase_ )
                 self.assertIsNotNone(UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    Optional[int] ):
           lowerCAmelCase,					lowerCAmelCase    :		int				=      self.model_tester.prepare_config_and_inputs_for_common()

           lowerCAmelCase    :		Union[str, Any]				=      _config_zero_init(UpperCamelCase_ )
           for model_class in self.all_model_classes:
                 lowerCAmelCase    :		Union[str, Any]				=      model_class(config=UpperCamelCase_ )
                 for name, param in model.named_parameters():
                       if "embeddings" not in name and "logit_scale" not in name and param.requires_grad:
                             self.assertIn(
                                 ((param.data.mean() * 1E9).round() / 1E9).item()    , [0.0, 1.0]    , msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''    , )



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






     @cached_property
     def    lowerCamelCase__   (		self     :    Dict ):
           return (
               AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' )
               if is_vision_available()
               else None
           )






     @slow
     def    lowerCamelCase__   (		self     :    Dict ):
           lowerCAmelCase    :		str				=      SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ).to(
               UpperCamelCase_ )
           lowerCAmelCase    :		List[Any]				=      self.default_image_processor

           lowerCAmelCase    :		int				=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
           lowerCAmelCase    :		Union[str, Any]				=      image_processor(images=UpperCamelCase_    , return_tensors='''pt''' ).to(UpperCamelCase_ )

           # forward pass
           with torch.no_grad():
                 lowerCAmelCase    :		Dict				=      model(**UpperCamelCase_ )

           # verify the logits
           lowerCAmelCase    :		List[Any]				=      torch.Size((1, 1_0_0_0) )
           self.assertEqual(outputs.logits.shape    , UpperCamelCase_ )
           lowerCAmelCase    :		Any				=      torch.tensor([-0.3_947, -0.4_306, 0.0_026] ).to(UpperCamelCase_ )
           self.assertTrue(torch.allclose(outputs.logits[0, :3]    , UpperCamelCase_    , atol=1E-4 ) )


