



'''simple docstring'''




import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


UpperCamelCase__ :			Union[str, Any]												=				random.Random()


def        lowerCAmelCase_     (			_lowerCamelCase:     Dict ,   _lowerCamelCase:     Union[str, Any]=1.0 ,   _lowerCamelCase:     str=None ,   _lowerCamelCase:     Optional[int]=None      ):
		if rng is None:
				__SCREAMING_SNAKE_CASE :	int					 =	global_rng

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

		return values






class 		_UpperCamelCase						(							unittest.TestCase					):


			'''simple docstring'''

			def __init__(							self  :				Optional[int]							,  lowerCAmelCase__  :				Union[str, Any]							,  lowerCAmelCase__  :				str=7							,  lowerCAmelCase__  :				Tuple=4_0_0							,  lowerCAmelCase__  :				int=2_0_0_0							,  lowerCAmelCase__  :				Any=1							,  lowerCAmelCase__  :				Union[str, Any]=0.0							,  lowerCAmelCase__  :				Optional[Any]=1_6_0_0_0							,  lowerCAmelCase__  :				Optional[Any]=True							,  lowerCAmelCase__  :				Dict=True							,  ):





					"""simple docstring"""






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

			def UpperCamelCase__      (							self  :				Optional[int]		):





					"""simple docstring"""






					return {
					    "feature_size": self.feature_size,
					    "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  :				Any							,  lowerCAmelCase__  :				int=False							,  lowerCAmelCase__  :				int=False		):





					"""simple docstring"""






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

					if equal_length:
							__SCREAMING_SNAKE_CASE :	Dict					 =	floats_list((self.batch_size, self.max_seq_length)		)
					else:
							# make sure that inputs increase in size
							__SCREAMING_SNAKE_CASE :	str					 =	[
							    _flatten(floats_list((x, self.feature_size)		)		)
							    for x in range(self.min_seq_length							,  self.max_seq_length							,  self.seq_length_diff		)
							]

					if numpify:
							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	[np.asarray(lowerCAmelCase__		) for x in speech_inputs]

					return speech_inputs






class 		_UpperCamelCase						(							lowerCamelCase__					,      unittest.TestCase					):


			'''simple docstring'''

			_A   :	Any    					=	WavaVecaFeatureExtractor

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	WavaVecaFeatureExtractionTester(self		)

			def UpperCamelCase__      (							self  :				Union[str, Any]							,  lowerCAmelCase__  :				Optional[int]		):





					"""simple docstring"""






					self.assertTrue(np.all(np.mean(lowerCAmelCase__							,  axis=0		) < 1E-3		)		)
					self.assertTrue(np.all(np.abs(np.var(lowerCAmelCase__							,  axis=0		) - 1		) < 1E-3		)		)

			def UpperCamelCase__      (							self  :				Optional[Any]		):





					"""simple docstring"""






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

					# Test not batched input
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	feat_extract(speech_inputs[0]							,  return_tensors="""np"""		).input_values
					__SCREAMING_SNAKE_CASE :	str					 =	feat_extract(np_speech_inputs[0]							,  return_tensors="""np"""		).input_values
					self.assertTrue(np.allclose(lowerCAmelCase__							,  lowerCAmelCase__							,  atol=1E-3		)		)

					# Test batched
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	feat_extract(lowerCAmelCase__							,  return_tensors="""np"""		).input_values
					__SCREAMING_SNAKE_CASE :	Tuple					 =	feat_extract(lowerCAmelCase__							,  return_tensors="""np"""		).input_values
					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.
					__SCREAMING_SNAKE_CASE :	Tuple					 =	[floats_list((1, x)		)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
					__SCREAMING_SNAKE_CASE :	List[str]					 =	np.asarray(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	str					 =	feat_extract(lowerCAmelCase__							,  return_tensors="""np"""		).input_values
					__SCREAMING_SNAKE_CASE :	Any					 =	feat_extract(lowerCAmelCase__							,  return_tensors="""np"""		).input_values
					for enc_seq_a, enc_seq_a in zip(lowerCAmelCase__							,  lowerCAmelCase__		):
							self.assertTrue(np.allclose(lowerCAmelCase__							,  lowerCAmelCase__							,  atol=1E-3		)		)

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	List[Any]					 =	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
					__SCREAMING_SNAKE_CASE :	List[str]					 =	[floats_list((1, x)		)[0] for x in range(8_0_0							,  1_4_0_0							,  2_0_0		)]

					__SCREAMING_SNAKE_CASE :	Tuple					 =	["""longest""", """max_length""", """do_not_pad"""]
					__SCREAMING_SNAKE_CASE :	str					 =	[None, 1_6_0_0, None]
					for max_length, padding in zip(lowerCAmelCase__							,  lowerCAmelCase__		):
							__SCREAMING_SNAKE_CASE :	Dict					 =	feat_extract(lowerCAmelCase__							,  padding=lowerCAmelCase__							,  max_length=lowerCAmelCase__							,  return_tensors="""np"""		)
							__SCREAMING_SNAKE_CASE :	int					 =	processed.input_values

							self._check_zero_mean_unit_variance(input_values[0][:8_0_0]		)
							self.assertTrue(input_values[0][8_0_0:].sum() < 1E-6		)
							self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0]		)
							self.assertTrue(input_values[0][1_0_0_0:].sum() < 1E-6		)
							self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0]		)

			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
					__SCREAMING_SNAKE_CASE :	int					 =	range(8_0_0							,  1_4_0_0							,  2_0_0		)
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	[floats_list((1, x)		)[0] for x in lengths]

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	["""longest""", """max_length""", """do_not_pad"""]
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	[None, 1_6_0_0, None]

					for max_length, padding in zip(lowerCAmelCase__							,  lowerCAmelCase__		):
							__SCREAMING_SNAKE_CASE :	Dict					 =	feat_extract(lowerCAmelCase__							,  max_length=lowerCAmelCase__							,  padding=lowerCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	processed.input_values

							self._check_zero_mean_unit_variance(input_values[0][:8_0_0]		)
							self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0]		)
							self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0]		)

			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	int					 =	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	[floats_list((1, x)		)[0] for x in range(8_0_0							,  1_4_0_0							,  2_0_0		)]
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	feat_extract(
					    lowerCAmelCase__							,  truncation=lowerCAmelCase__							,  max_length=1_0_0_0							,  padding="""max_length"""							,  return_tensors="""np"""		)
					__SCREAMING_SNAKE_CASE :	Any					 =	processed.input_values

					self._check_zero_mean_unit_variance(input_values[0, :8_0_0]		)
					self._check_zero_mean_unit_variance(input_values[1]		)
					self._check_zero_mean_unit_variance(input_values[2]		)

			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	[floats_list((1, x)		)[0] for x in range(8_0_0							,  1_4_0_0							,  2_0_0		)]
					__SCREAMING_SNAKE_CASE :	Dict					 =	feat_extract(
					    lowerCAmelCase__							,  truncation=lowerCAmelCase__							,  max_length=1_0_0_0							,  padding="""longest"""							,  return_tensors="""np"""		)
					__SCREAMING_SNAKE_CASE :	Any					 =	processed.input_values

					self._check_zero_mean_unit_variance(input_values[0, :8_0_0]		)
					self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0]		)
					self._check_zero_mean_unit_variance(input_values[2]		)

					# make sure that if max_length < longest -> then pad to max_length
					self.assertTrue(input_values.shape == (3, 1_0_0_0)		)

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	[floats_list((1, x)		)[0] for x in range(8_0_0							,  1_4_0_0							,  2_0_0		)]
					__SCREAMING_SNAKE_CASE :	str					 =	feat_extract(
					    lowerCAmelCase__							,  truncation=lowerCAmelCase__							,  max_length=2_0_0_0							,  padding="""longest"""							,  return_tensors="""np"""		)
					__SCREAMING_SNAKE_CASE :	Dict					 =	processed.input_values

					self._check_zero_mean_unit_variance(input_values[0, :8_0_0]		)
					self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0]		)
					self._check_zero_mean_unit_variance(input_values[2]		)

					# make sure that if max_length > longest -> then pad to longest
					self.assertTrue(input_values.shape == (3, 1_2_0_0)		)

			@require_torch
			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					import torch

					__SCREAMING_SNAKE_CASE :	Dict					 =	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
					__SCREAMING_SNAKE_CASE :	Any					 =	np.random.rand(1_0_0		).astype(np.floataa		)
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	np_speech_inputs.tolist()

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



			@slow
			@require_torch
			def UpperCamelCase__      (							self  :				Optional[int]		):





					"""simple docstring"""






					for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
							__SCREAMING_SNAKE_CASE :	List[str]					 =	WavaVecaConfig.from_pretrained(lowerCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__		)

							# only "layer" feature extraction norm should make use of
							# attention_mask
							self.assertEqual(feat_extract.return_attention_mask							,  config.feat_extract_norm == """layer"""		)




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


UpperCamelCase__ :			Any												=				logging.get_logger(__name__)

UpperCamelCase__ :			Dict												=				{
    '''microsoft/beit-base-patch16-224-pt22k''': (
        '''https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json'''
    ),
    # See all BEiT models at https://huggingface.co/models?filter=beit
}






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Optional[int]    					=	'''beit'''

			def __init__(							self  :				Tuple							,  lowerCAmelCase__  :				List[Any]=8_1_9_2							,  lowerCAmelCase__  :				List[Any]=7_6_8							,  lowerCAmelCase__  :				List[Any]=1_2							,  lowerCAmelCase__  :				Union[str, Any]=1_2							,  lowerCAmelCase__  :				Dict=3_0_7_2							,  lowerCAmelCase__  :				List[Any]="gelu"							,  lowerCAmelCase__  :				List[Any]=0.0							,  lowerCAmelCase__  :				int=0.0							,  lowerCAmelCase__  :				Tuple=0.02							,  lowerCAmelCase__  :				Any=1E-12							,  lowerCAmelCase__  :				Dict=2_2_4							,  lowerCAmelCase__  :				List[str]=1_6							,  lowerCAmelCase__  :				int=3							,  lowerCAmelCase__  :				Union[str, Any]=False							,  lowerCAmelCase__  :				Union[str, Any]=False							,  lowerCAmelCase__  :				Any=False							,  lowerCAmelCase__  :				Union[str, Any]=False							,  lowerCAmelCase__  :				Optional[int]=0.1							,  lowerCAmelCase__  :				Dict=0.1							,  lowerCAmelCase__  :				Tuple=True							,  lowerCAmelCase__  :				str=[3, 5, 7, 1_1]							,  lowerCAmelCase__  :				str=[1, 2, 3, 6]							,  lowerCAmelCase__  :				Dict=True							,  lowerCAmelCase__  :				Dict=0.4							,  lowerCAmelCase__  :				int=2_5_6							,  lowerCAmelCase__  :				Any=1							,  lowerCAmelCase__  :				str=False							,  lowerCAmelCase__  :				Union[str, Any]=2_5_5							,  **lowerCAmelCase__  :				Optional[int]							,  ):





					"""simple docstring"""






					super().__init__(**lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	int					 =	vocab_size
					__SCREAMING_SNAKE_CASE :	int					 =	hidden_size
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	num_hidden_layers
					__SCREAMING_SNAKE_CASE :	List[str]					 =	num_attention_heads
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	intermediate_size
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	hidden_act
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	hidden_dropout_prob
					__SCREAMING_SNAKE_CASE :	Any					 =	attention_probs_dropout_prob
					__SCREAMING_SNAKE_CASE :	Dict					 =	initializer_range
					__SCREAMING_SNAKE_CASE :	Any					 =	layer_norm_eps

					__SCREAMING_SNAKE_CASE :	Dict					 =	image_size
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	patch_size
					__SCREAMING_SNAKE_CASE :	int					 =	num_channels
					__SCREAMING_SNAKE_CASE :	Tuple					 =	use_mask_token
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	use_absolute_position_embeddings
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	use_relative_position_bias
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	use_shared_relative_position_bias
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	layer_scale_init_value
					__SCREAMING_SNAKE_CASE :	Any					 =	drop_path_rate
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	use_mean_pooling
					# decode head attributes (semantic segmentation)
					__SCREAMING_SNAKE_CASE :	Any					 =	out_indices
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	pool_scales
					# auxiliary head attributes (semantic segmentation)
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	use_auxiliary_head
					__SCREAMING_SNAKE_CASE :	str					 =	auxiliary_loss_weight
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	auxiliary_channels
					__SCREAMING_SNAKE_CASE :	str					 =	auxiliary_num_convs
					__SCREAMING_SNAKE_CASE :	int					 =	auxiliary_concat_input
					__SCREAMING_SNAKE_CASE :	Dict					 =	semantic_loss_ignore_index






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			_A   :	List[str]    					=	version.parse('''1.11'''					)

			@property
			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






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



			@property
			def UpperCamelCase__      (							self  :				Any		):





					"""simple docstring"""






					return 1E-4