



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

snake_case__					:     int    		=   random.Random()



def    snake_case_				(    _SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE=1.0  ,	_SCREAMING_SNAKE_CASE=None  ,	_SCREAMING_SNAKE_CASE=None   ):
	if rng is None:
		__lowercase		       =			global_rng

	__lowercase		       =			[]
	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   _A   ( unittest.TestCase	):


						'''simple docstring'''





						def __init__(	self	:							Optional[Any]							,      lowerCamelCase	:							Union[str, Any]							,      lowerCamelCase	:							List[str]=7							,      lowerCamelCase	:							Optional[int]=400							,      lowerCamelCase	:							Union[str, Any]=2_000							,      lowerCamelCase	:							Union[str, Any]=10							,      lowerCamelCase	:							Optional[int]=160							,      lowerCamelCase	:							Union[str, Any]=8							,      lowerCamelCase	:							str=0.0							,      lowerCamelCase	:							Dict=4_000							,      lowerCamelCase	:							Optional[Any]=False							,      lowerCamelCase	:							int=True							,      ):






							'''simple docstring'''



							__lowercase		       =			parent
							__lowercase		       =			batch_size
							__lowercase		       =			min_seq_length
							__lowercase		       =			max_seq_length
							__lowercase		       =			(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
							__lowercase		       =			padding_value
							__lowercase		       =			sampling_rate
							__lowercase		       =			return_attention_mask
							__lowercase		       =			do_normalize
							__lowercase		       =			feature_size
							__lowercase		       =			chunk_length
							__lowercase		       =			hop_length



						def     _snake_case	(	self	:							str	):






							'''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     _snake_case	(	self	:							Union[str, Any]							,      lowerCamelCase	:							int=False							,      lowerCamelCase	:							Union[str, Any]=False	):






							'''simple docstring'''



							def _flatten(lowerCamelCase	:							Any	):
								return list(itertools.chain(*lowerCamelCase	)	)

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

@require_torch
@require_torchaudio
class   _A   ( _lowercase  ,						unittest.TestCase	):


						'''simple docstring'''





						_snake_case			:     Dict      							=      WhisperFeatureExtractor if is_speech_available() else None



						def     _snake_case	(	self	:							List[Any]	):






							'''simple docstring'''



							__lowercase		       =			WhisperFeatureExtractionTester(self	)



						def     _snake_case	(	self	:							List[Any]	):






							'''simple docstring'''



							__lowercase		       =			self.feature_extraction_class(**self.feat_extract_dict	)

							with tempfile.TemporaryDirectory() as tmpdirname:
								__lowercase		       =			feat_extract_first.save_pretrained(lowerCamelCase	)[0]
								check_json_file_has_correct_format(lowerCamelCase	)
								__lowercase		       =			self.feature_extraction_class.from_pretrained(lowerCamelCase	)

							__lowercase		       =			feat_extract_first.to_dict()
							__lowercase		       =			feat_extract_second.to_dict()
							__lowercase		       =			feat_extract_first.mel_filters
							__lowercase		       =			feat_extract_second.mel_filters
							self.assertTrue(np.allclose(lowerCamelCase							,      lowerCamelCase	)	)
							self.assertEqual(lowerCamelCase							,      lowerCamelCase	)



						def     _snake_case	(	self	:							List[str]	):






							'''simple docstring'''



							__lowercase		       =			self.feature_extraction_class(**self.feat_extract_dict	)

							with tempfile.TemporaryDirectory() as tmpdirname:
								__lowercase		       =			os.path.join(lowerCamelCase							,      "feat_extract.json"	)
								feat_extract_first.to_json_file(lowerCamelCase	)
								__lowercase		       =			self.feature_extraction_class.from_json_file(lowerCamelCase	)

							__lowercase		       =			feat_extract_first.to_dict()
							__lowercase		       =			feat_extract_second.to_dict()
							__lowercase		       =			feat_extract_first.mel_filters
							__lowercase		       =			feat_extract_second.mel_filters
							self.assertTrue(np.allclose(lowerCamelCase							,      lowerCamelCase	)	)
							self.assertEqual(lowerCamelCase							,      lowerCamelCase	)



						def     _snake_case	(	self	:							int	):






							'''simple docstring'''



							__lowercase		       =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
							# create three inputs of length 800, 1000, and 1200
							__lowercase		       =			[floats_list((1, x)	)[0] for x in range(800							,      1_400							,      200	)]
							__lowercase		       =			[np.asarray(lowerCamelCase	) for speech_input in speech_inputs]

							# Test feature size
							__lowercase		       =			feature_extractor(lowerCamelCase							,      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
							__lowercase		       =			feature_extractor(speech_inputs[0]							,      return_tensors="np"	).input_features
							__lowercase		       =			feature_extractor(np_speech_inputs[0]							,      return_tensors="np"	).input_features
							self.assertTrue(np.allclose(lowerCamelCase							,      lowerCamelCase							,      atol=1e-3	)	)

							# Test batched
							__lowercase		       =			feature_extractor(lowerCamelCase							,      return_tensors="np"	).input_features
							__lowercase		       =			feature_extractor(lowerCamelCase							,      return_tensors="np"	).input_features
							for enc_seq_a, enc_seq_a in zip(lowerCamelCase							,      lowerCamelCase	):
								self.assertTrue(np.allclose(lowerCamelCase							,      lowerCamelCase							,      atol=1e-3	)	)

							# Test 2-D numpy arrays are batched.
							__lowercase		       =			[floats_list((1, x)	)[0] for x in (800, 800, 800)]
							__lowercase		       =			np.asarray(lowerCamelCase	)
							__lowercase		       =			feature_extractor(lowerCamelCase							,      return_tensors="np"	).input_features
							__lowercase		       =			feature_extractor(lowerCamelCase							,      return_tensors="np"	).input_features
							for enc_seq_a, enc_seq_a in zip(lowerCamelCase							,      lowerCamelCase	):
								self.assertTrue(np.allclose(lowerCamelCase							,      lowerCamelCase							,      atol=1e-3	)	)

							# Test truncation required
							__lowercase		       =			[floats_list((1, x)	)[0] for x in range(200							,      (feature_extractor.n_samples + 500)							,      200	)]
							__lowercase		       =			[np.asarray(lowerCamelCase	) for speech_input in speech_inputs]

							__lowercase		       =			[x[: feature_extractor.n_samples] for x in speech_inputs]
							__lowercase		       =			[np.asarray(lowerCamelCase	) for speech_input in speech_inputs_truncated]

							__lowercase		       =			feature_extractor(lowerCamelCase							,      return_tensors="np"	).input_features
							__lowercase		       =			feature_extractor(lowerCamelCase							,      return_tensors="np"	).input_features
							for enc_seq_a, enc_seq_a in zip(lowerCamelCase							,      lowerCamelCase	):
								self.assertTrue(np.allclose(lowerCamelCase							,      lowerCamelCase							,      atol=1e-3	)	)



						def     _snake_case	(	self	:							List[Any]	):






							'''simple docstring'''



							import torch

							__lowercase		       =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
							__lowercase		       =			np.random.rand(100							,      32	).astype(np.floataa	)
							__lowercase		       =			np_speech_inputs.tolist()

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



						def     _snake_case	(	self	:							str							,      lowerCamelCase	:							Optional[Any]	):






							'''simple docstring'''



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

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



						def     _snake_case	(	self	:							Union[str, Any]	):






							'''simple docstring'''



							__lowercase		       =			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

							__lowercase		       =			self._load_datasamples(1	)
							__lowercase		       =			WhisperFeatureExtractor()
							__lowercase		       =			feature_extractor(lowerCamelCase							,      return_tensors="pt"	).input_features
							self.assertEqual(input_features.shape							,      (1, 80, 3_000)	)
							self.assertTrue(torch.allclose(input_features[0, 0, :30]							,      lowerCamelCase							,      atol=1e-4	)	)



						def     _snake_case	(	self	:							Any	):






							'''simple docstring'''



							__lowercase		       =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
							__lowercase		       =			self._load_datasamples(1	)[0]
							__lowercase		       =			((audio - audio.min()) / (audio.max() - audio.min())) * 65_535  # Rescale to [0, 65535] to show issue
							__lowercase		       =			feat_extract.zero_mean_unit_var_norm([audio]							,      attention_mask=lowerCamelCase	)[0]

							self.assertTrue(np.all(np.mean(lowerCamelCase	) < 1e-3	)	)
							self.assertTrue(np.all(np.abs(np.var(lowerCamelCase	) - 1	) < 1e-3	)	)








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


snake_case__					:     Any    		=   logging.get_logger(__name__)

class   _A   ( _lowercase  ,						_lowercase	):


						'''simple docstring'''





						_snake_case			:     Dict      							=      """maskformer-swin"""

						_snake_case			:     List[str]      							=      {
						    """num_attention_heads""": """num_heads""",
						    """num_hidden_layers""": """num_layers""",
						}



						def __init__(	self	:							List[str]							,      lowerCamelCase	:							Any=224							,      lowerCamelCase	:							Optional[Any]=4							,      lowerCamelCase	:							Dict=3							,      lowerCamelCase	:							Tuple=96							,      lowerCamelCase	:							str=[2, 2, 6, 2]							,      lowerCamelCase	:							Dict=[3, 6, 12, 24]							,      lowerCamelCase	:							Optional[Any]=7							,      lowerCamelCase	:							Any=4.0							,      lowerCamelCase	:							Union[str, Any]=True							,      lowerCamelCase	:							List[str]=0.0							,      lowerCamelCase	:							Optional[int]=0.0							,      lowerCamelCase	:							List[str]=0.1							,      lowerCamelCase	:							int="gelu"							,      lowerCamelCase	:							Optional[int]=False							,      lowerCamelCase	:							List[Any]=0.02							,      lowerCamelCase	:							Tuple=1e-5							,      lowerCamelCase	:							Dict=None							,      lowerCamelCase	:							Dict=None							,      **lowerCamelCase	:							int							,      ):






							'''simple docstring'''



							super().__init__(**lowerCamelCase	)

							__lowercase		       =			image_size
							__lowercase		       =			patch_size
							__lowercase		       =			num_channels
							__lowercase		       =			embed_dim
							__lowercase		       =			depths
							__lowercase		       =			len(lowerCamelCase	)
							__lowercase		       =			num_heads
							__lowercase		       =			window_size
							__lowercase		       =			mlp_ratio
							__lowercase		       =			qkv_bias
							__lowercase		       =			hidden_dropout_prob
							__lowercase		       =			attention_probs_dropout_prob
							__lowercase		       =			drop_path_rate
							__lowercase		       =			hidden_act
							__lowercase		       =			use_absolute_embeddings
							__lowercase		       =			layer_norm_eps
							__lowercase		       =			initializer_range
							# 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
							__lowercase		       =			int(embed_dim * 2 ** (len(lowerCamelCase	) - 1)	)
							__lowercase		       =			["stem"] + [f"""stage{idx}""" for idx in range(1							,      len(lowerCamelCase	) + 1	)]
							__lowercase       ,    __lowercase		       =			get_aligned_output_features_output_indices(
							    out_features=lowerCamelCase							,      out_indices=lowerCamelCase							,      stage_names=self.stage_names	)



