



"""simple docstring"""





import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
					from transformers import WhisperFeatureExtractor

if is_torch_available():
					import torch

__lowerCAmelCase       :					Any      					=					random.Random()
def 	__lowerCAmelCase						(		__UpperCamelCase      :			List[Any]					, __UpperCamelCase      :			Union[str, Any]=1.0					, __UpperCamelCase      :			str=None					, __UpperCamelCase      :			Tuple=None    ):


						'''simple docstring'''






						if rng is None:
												snake_case_   : Union[str, Any]						=			global_rng

						snake_case_   : str						=			[]
						for batch_idx in range(shape[0]    ):
												values.append([]    )
												for _ in range(shape[1]    ):
																		values[-1].append(rng.random() * scale    )

						return values



@require_torch
@require_torchaudio
class    _lowerCAmelCase    (				unittest.TestCase		):





		"""simple docstring"""






		def __init__(					self							,    _lowercase							,    _lowercase=7							,    _lowercase=4_0_0							,    _lowercase=2_0_0_0							,    _lowercase=1_0							,    _lowercase=1_6_0							,    _lowercase=8							,    _lowercase=0.0							,    _lowercase=4_0_0_0							,    _lowercase=False							,    _lowercase=True							,    ) ->						Optional[Any]:







								'''simple docstring'''

								snake_case_   : Tuple						=			parent
								snake_case_   : Optional[Any]						=			batch_size
								snake_case_   : Optional[int]						=			min_seq_length
								snake_case_   : Optional[Any]						=			max_seq_length
								snake_case_   : int						=			(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
								snake_case_   : str						=			padding_value
								snake_case_   : Tuple						=			sampling_rate
								snake_case_   : List[str]						=			return_attention_mask
								snake_case_   : Optional[Any]						=			do_normalize
								snake_case_   : List[Any]						=			feature_size
								snake_case_   : List[Any]						=			chunk_length
								snake_case_   : int						=			hop_length





		def    UpperCAmelCase__							(					self   ) ->						Tuple:







								'''simple docstring'''

								return {
								    "feature_size": self.feature_size,
								    "hop_length": self.hop_length,
								    "chunk_length": self.chunk_length,
								    "padding_value": self.padding_value,
								    "sampling_rate": self.sampling_rate,
								    "return_attention_mask": self.return_attention_mask,
								    "do_normalize": self.do_normalize,
								}





		def    UpperCAmelCase__							(					self							,    _lowercase=False							,    _lowercase=False   ) ->						List[str]:







								'''simple docstring'''

								def _flatten(_lowercase   ):
														return list(itertools.chain(*_lowercase   )   )

								if equal_length:
														snake_case_   : List[str]						=			[floats_list((self.max_seq_length, self.feature_size)   ) for _ in range(self.batch_size   )]
								else:
														# make sure that inputs increase in size
														snake_case_   : Dict						=			[
														    floats_list((x, self.feature_size)   )
														    for x in range(self.min_seq_length							,    self.max_seq_length							,    self.seq_length_diff   )
														]
								if numpify:
														snake_case_   : List[Any]						=			[np.asarray(_lowercase   ) for x in speech_inputs]
								return speech_inputs




@require_torch
@require_torchaudio
class    _lowerCAmelCase    (				SCREAMING_SNAKE_CASE__  ,				unittest.TestCase		):





		"""simple docstring"""






		_lowerCamelCase			  =       WhisperFeatureExtractor if is_speech_available() else None





		def    UpperCAmelCase__							(					self   ) ->						str:







								'''simple docstring'''

								snake_case_   : Any						=			WhisperFeatureExtractionTester(self   )





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







								'''simple docstring'''

								snake_case_   : List[str]						=			self.feature_extraction_class(**self.feat_extract_dict   )

								with tempfile.TemporaryDirectory() as tmpdirname:
														snake_case_   : Any						=			feat_extract_first.save_pretrained(_lowercase   )[0]
														check_json_file_has_correct_format(_lowercase   )
														snake_case_   : Dict						=			self.feature_extraction_class.from_pretrained(_lowercase   )

								snake_case_   : List[Any]						=			feat_extract_first.to_dict()
								snake_case_   : Tuple						=			feat_extract_second.to_dict()
								snake_case_   : Tuple						=			feat_extract_first.mel_filters
								snake_case_   : Dict						=			feat_extract_second.mel_filters
								self.assertTrue(np.allclose(_lowercase							,    _lowercase   )   )
								self.assertEqual(_lowercase							,    _lowercase   )





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







								'''simple docstring'''

								snake_case_   : Dict						=			self.feature_extraction_class(**self.feat_extract_dict   )

								with tempfile.TemporaryDirectory() as tmpdirname:
														snake_case_   : List[str]						=			os.path.join(_lowercase							,    """feat_extract.json"""   )
														feat_extract_first.to_json_file(_lowercase   )
														snake_case_   : Tuple						=			self.feature_extraction_class.from_json_file(_lowercase   )

								snake_case_   : Dict						=			feat_extract_first.to_dict()
								snake_case_   : str						=			feat_extract_second.to_dict()
								snake_case_   : Any						=			feat_extract_first.mel_filters
								snake_case_   : Tuple						=			feat_extract_second.mel_filters
								self.assertTrue(np.allclose(_lowercase							,    _lowercase   )   )
								self.assertEqual(_lowercase							,    _lowercase   )





		def    UpperCAmelCase__							(					self   ) ->						str:







								'''simple docstring'''

								snake_case_   : List[Any]						=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
								# create three inputs of length 800, 1000, and 1200
								snake_case_   : Optional[Any]						=			[floats_list((1, x)   )[0] for x in range(8_0_0							,    1_4_0_0							,    2_0_0   )]
								snake_case_   : Union[str, Any]						=			[np.asarray(_lowercase   ) for speech_input in speech_inputs]

								# Test feature size
								snake_case_   : List[str]						=			feature_extractor(_lowercase							,    padding="""max_length"""							,    return_tensors="""np"""   ).input_features
								self.assertTrue(input_features.ndim == 3   )
								self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames   )
								self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size   )

								# Test not batched input
								snake_case_   : List[str]						=			feature_extractor(speech_inputs[0]							,    return_tensors="""np"""   ).input_features
								snake_case_   : List[str]						=			feature_extractor(np_speech_inputs[0]							,    return_tensors="""np"""   ).input_features
								self.assertTrue(np.allclose(_lowercase							,    _lowercase							,    atol=1E-3   )   )

								# Test batched
								snake_case_   : int						=			feature_extractor(_lowercase							,    return_tensors="""np"""   ).input_features
								snake_case_   : Optional[int]						=			feature_extractor(_lowercase							,    return_tensors="""np"""   ).input_features
								for enc_seq_a, enc_seq_a in zip(_lowercase							,    _lowercase   ):
														self.assertTrue(np.allclose(_lowercase							,    _lowercase							,    atol=1E-3   )   )

								# Test 2-D numpy arrays are batched.
								snake_case_   : List[str]						=			[floats_list((1, x)   )[0] for x in (8_0_0, 8_0_0, 8_0_0)]
								snake_case_   : Tuple						=			np.asarray(_lowercase   )
								snake_case_   : Dict						=			feature_extractor(_lowercase							,    return_tensors="""np"""   ).input_features
								snake_case_   : str						=			feature_extractor(_lowercase							,    return_tensors="""np"""   ).input_features
								for enc_seq_a, enc_seq_a in zip(_lowercase							,    _lowercase   ):
														self.assertTrue(np.allclose(_lowercase							,    _lowercase							,    atol=1E-3   )   )

								# Test truncation required
								snake_case_   : List[Any]						=			[floats_list((1, x)   )[0] for x in range(2_0_0							,    (feature_extractor.n_samples + 5_0_0)							,    2_0_0   )]
								snake_case_   : str						=			[np.asarray(_lowercase   ) for speech_input in speech_inputs]

								snake_case_   : List[Any]						=			[x[: feature_extractor.n_samples] for x in speech_inputs]
								snake_case_   : List[Any]						=			[np.asarray(_lowercase   ) for speech_input in speech_inputs_truncated]

								snake_case_   : Optional[Any]						=			feature_extractor(_lowercase							,    return_tensors="""np"""   ).input_features
								snake_case_   : str						=			feature_extractor(_lowercase							,    return_tensors="""np"""   ).input_features
								for enc_seq_a, enc_seq_a in zip(_lowercase							,    _lowercase   ):
														self.assertTrue(np.allclose(_lowercase							,    _lowercase							,    atol=1E-3   )   )





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







								'''simple docstring'''

								import torch

								snake_case_   : List[str]						=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
								snake_case_   : Tuple						=			np.random.rand(1_0_0							,    3_2   ).astype(np.floataa   )
								snake_case_   : Optional[int]						=			np_speech_inputs.tolist()

								for inputs in [py_speech_inputs, np_speech_inputs]:
														snake_case_   : Dict						=			feature_extractor.pad([{"""input_features""": inputs}]							,    return_tensors="""np"""   )
														self.assertTrue(np_processed.input_features.dtype == np.floataa   )
														snake_case_   : Any						=			feature_extractor.pad([{"""input_features""": inputs}]							,    return_tensors="""pt"""   )
														self.assertTrue(pt_processed.input_features.dtype == torch.floataa   )





		def    UpperCAmelCase__							(					self							,    _lowercase   ) ->						Dict:







								'''simple docstring'''

								snake_case_   : str						=			load_dataset("""hf-internal-testing/librispeech_asr_dummy"""							,    """clean"""							,    split="""validation"""   )
								# automatic decoding with librispeech
								snake_case_   : Optional[int]						=			ds.sort("""id"""   ).select(range(_lowercase   )   )[:num_samples]["""audio"""]

								return [x["array"] for x in speech_samples]





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







								'''simple docstring'''

								snake_case_   : List[str]						=			torch.tensor(
								    [
								        0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951,
								        0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678,
								        0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554,
								        -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854
								    ]   )
								# fmt: on

								snake_case_   : Tuple						=			self._load_datasamples(1   )
								snake_case_   : Dict						=			WhisperFeatureExtractor()
								snake_case_   : List[str]						=			feature_extractor(_lowercase							,    return_tensors="""pt"""   ).input_features
								self.assertEqual(input_features.shape							,    (1, 8_0, 3_0_0_0)   )
								self.assertTrue(torch.allclose(input_features[0, 0, :3_0]							,    _lowercase							,    atol=1E-4   )   )





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







								'''simple docstring'''

								snake_case_   : List[str]						=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
								snake_case_   : Dict						=			self._load_datasamples(1   )[0]
								snake_case_   : Dict						=			((audio - audio.min()) / (audio.max() - audio.min())) * 6_5_5_3_5  # Rescale to [0, 65535] to show issue
								snake_case_   : int						=			feat_extract.zero_mean_unit_var_norm([audio]							,    attention_mask=_lowercase   )[0]

								self.assertTrue(np.all(np.mean(_lowercase   ) < 1E-3   )   )
								self.assertTrue(np.all(np.abs(np.var(_lowercase   ) - 1   ) < 1E-3   )   )






"""simple docstring"""





from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__lowerCAmelCase       :					Tuple      					=					logging.get_logger(__name__)

__lowerCAmelCase       :					Dict      					=					{
    '''microsoft/swin-tiny-patch4-window7-224''': (
        '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json'''
    ),
    # See all Swin models at https://huggingface.co/models?filter=swin
}



class    _lowerCAmelCase    (				SCREAMING_SNAKE_CASE__  ,				SCREAMING_SNAKE_CASE__		):





		"""simple docstring"""






		_lowerCamelCase			  =       '''swin'''

		_lowerCamelCase			  =       {
		    '''num_attention_heads''': '''num_heads''',
		    '''num_hidden_layers''': '''num_layers''',
		}





		def __init__(					self							,    _lowercase=2_2_4							,    _lowercase=4							,    _lowercase=3							,    _lowercase=9_6							,    _lowercase=[2, 2, 6, 2]							,    _lowercase=[3, 6, 1_2, 2_4]							,    _lowercase=7							,    _lowercase=4.0							,    _lowercase=True							,    _lowercase=0.0							,    _lowercase=0.0							,    _lowercase=0.1							,    _lowercase="gelu"							,    _lowercase=False							,    _lowercase=0.02							,    _lowercase=1E-5							,    _lowercase=3_2							,    _lowercase=None							,    _lowercase=None							,    **_lowercase							,    ) ->						Union[str, Any]:







								'''simple docstring'''

								super().__init__(**_lowercase   )

								snake_case_   : str						=			image_size
								snake_case_   : int						=			patch_size
								snake_case_   : Optional[int]						=			num_channels
								snake_case_   : Union[str, Any]						=			embed_dim
								snake_case_   : Optional[int]						=			depths
								snake_case_   : Optional[int]						=			len(_lowercase   )
								snake_case_   : Optional[Any]						=			num_heads
								snake_case_   : Optional[Any]						=			window_size
								snake_case_   : Optional[Any]						=			mlp_ratio
								snake_case_   : Optional[Any]						=			qkv_bias
								snake_case_   : Optional[Any]						=			hidden_dropout_prob
								snake_case_   : Tuple						=			attention_probs_dropout_prob
								snake_case_   : Union[str, Any]						=			drop_path_rate
								snake_case_   : List[Any]						=			hidden_act
								snake_case_   : str						=			use_absolute_embeddings
								snake_case_   : str						=			layer_norm_eps
								snake_case_   : Optional[Any]						=			initializer_range
								snake_case_   : Any						=			encoder_stride
								# we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
								# this indicates the channel dimension after the last stage of the model
								snake_case_   : Tuple						=			int(embed_dim * 2 ** (len(_lowercase   ) - 1)   )
								snake_case_   : Tuple						=			["""stem"""] + [f'stage{idx}' for idx in range(1							,    len(_lowercase   ) + 1   )]
								snake_case_			,		snake_case_   : Any						=			get_aligned_output_features_output_indices(
								    out_features=_lowercase							,    out_indices=_lowercase							,    stage_names=self.stage_names   )




class    _lowerCAmelCase    (				SCREAMING_SNAKE_CASE__		):





		"""simple docstring"""






		_lowerCamelCase			  =       version.parse('''1.11'''		)





		@property
		def    UpperCAmelCase__							(					self   ) ->						Mapping[str, Mapping[int, str]]:







								'''simple docstring'''

								return OrderedDict(
								    [
								        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
								    ]   )





		@property
		def    UpperCAmelCase__							(					self   ) ->						float:







								'''simple docstring'''

								return 1E-4

