



'''simple docstring'''

import collections.abc
from typing import Optional, Tuple, Union

import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_poolformer import PoolFormerConfig


a							   =				logging.get_logger(__name__)

# General docstring
a							   =				'PoolFormerConfig'

# Base docstring
a							   =				'sail/poolformer_s12'
a							   =				[1, 512, 7, 7]

# Image classification docstring
a							   =				'sail/poolformer_s12'
a							   =				'tabby, tabby cat'

a							   =				[
    'sail/poolformer_s12',
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]







def a_     (    __UpperCAmelCase				,							__UpperCAmelCase = 0.0				,							__UpperCAmelCase = False					)				->					Tuple:





			"""simple docstring"""







			if drop_prob == 0.0 or not training:
						return input
			snake_case:    List[str]      							=1 - drop_prob
			snake_case:    Dict      							=(input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
			snake_case:    int      							=keep_prob + torch.rand(__UpperCAmelCase				,							dtype=input.dtype				,							device=input.device					)
			random_tensor.floor_()  # binarize
			snake_case:    Tuple      							=input.div(__UpperCAmelCase					) * random_tensor
			return output






class 						a_		(     nn.Module    ):


							def __init__(				self   :						Optional[int]      ,						a_   :						Optional[float] = None			)			->					None:
										super().__init__()
										snake_case:    Optional[int]      							=drop_prob


							def    UpperCamelCase     (				self   :						Any      ,						a_   :						torch.Tensor			)			->					torch.Tensor:
										return drop_path(a_      ,						self.drop_prob      ,						self.training			)




							def    UpperCamelCase     (				self   :						List[Any]			)			->					str:
										return "p={}".format(self.drop_prob			)






class 						a_		(     nn.Module    ):


							def __init__(				self   :						int      ,						a_   :						Union[str, Any]      ,						a_   :						Optional[Any]      ,						a_   :						List[str]      ,						a_   :						List[str]      ,						a_   :						Dict      ,						a_   :						Tuple=None			)			->					Dict:
										super().__init__()
										snake_case:    Dict      							=patch_size if isinstance(a_      ,						collections.abc.Iterable			) else (patch_size, patch_size)
										snake_case:    Dict      							=stride if isinstance(a_      ,						collections.abc.Iterable			) else (stride, stride)
										snake_case:    Dict      							=padding if isinstance(a_      ,						collections.abc.Iterable			) else (padding, padding)

										snake_case:    Optional[Any]      							=nn.Convad(a_      ,						a_      ,						kernel_size=a_      ,						stride=a_      ,						padding=a_			)
										snake_case:    Dict      							=norm_layer(a_			) if norm_layer else nn.Identity()




							def    UpperCamelCase     (				self   :						Dict      ,						a_   :						List[str]			)			->					Optional[int]:
										snake_case:    Union[str, Any]      							=self.projection(a_			)
										snake_case:    Dict      							=self.norm(a_			)
										return embeddings






class 						a_		(     nn.GroupNorm    ):


							def __init__(				self   :						str      ,						a_   :						Optional[Any]      ,						**a_   :						str			)			->					Dict:
										super().__init__(1      ,						a_      ,						**a_			)






class 						a_		(     nn.Module    ):


							def __init__(				self   :						Optional[Any]      ,						a_   :						Union[str, Any]			)			->					int:
										super().__init__()
										snake_case:    Tuple      							=nn.AvgPoolad(a_      ,						stride=1      ,						padding=pool_size // 2      ,						count_include_pad=a_			)




							def    UpperCamelCase     (				self   :						List[Any]      ,						a_   :						Any			)			->					Any:
										return self.pool(a_			) - hidden_states






class 						a_		(     nn.Module    ):


							def __init__(				self   :						Tuple      ,						a_   :						Union[str, Any]      ,						a_   :						str      ,						a_   :						Optional[Any]      ,						a_   :						Optional[Any]			)			->					List[str]:
										super().__init__()
										snake_case:    Any      							=nn.Convad(a_      ,						a_      ,						1			)
										snake_case:    List[Any]      							=nn.Convad(a_      ,						a_      ,						1			)
										snake_case:    Tuple      							=PoolFormerDropPath(a_			)
										if isinstance(config.hidden_act      ,						a_			):
													snake_case:    Optional[int]      							=ACTaFN[config.hidden_act]
										else:
													snake_case:    Any      							=config.hidden_act




							def    UpperCamelCase     (				self   :						str      ,						a_   :						int			)			->					int:
										snake_case:    int      							=self.conva(a_			)
										snake_case:    Tuple      							=self.act_fn(a_			)
										snake_case:    List[Any]      							=self.drop(a_			)
										snake_case:    Optional[Any]      							=self.conva(a_			)
										snake_case:    Tuple      							=self.drop(a_			)

										return hidden_states






class 						a_		(     nn.Module    ):


							def __init__(				self   :						Any      ,						a_   :						List[Any]      ,						a_   :						Any      ,						a_   :						Optional[int]      ,						a_   :						str      ,						a_   :						List[str]      ,						a_   :						Union[str, Any]			)			->					str:
										super().__init__()
										snake_case:    Dict      							=PoolFormerPooling(a_			)
										snake_case:    List[Any]      							=PoolFormerOutput(a_      ,						a_      ,						a_      ,						a_			)
										snake_case:    Any      							=PoolFormerGroupNorm(a_			)
										snake_case:    List[Any]      							=PoolFormerGroupNorm(a_			)

										# Useful for training neural nets
										snake_case:    Any      							=PoolFormerDropPath(a_			) if drop_path > 0.0 else nn.Identity()
										snake_case:    Union[str, Any]      							=config.use_layer_scale
										if config.use_layer_scale:
													snake_case:    List[str]      							=nn.Parameter(
													    config.layer_scale_init_value * torch.ones((a_)			)      ,						requires_grad=a_			)
													snake_case:    List[Any]      							=nn.Parameter(
													    config.layer_scale_init_value * torch.ones((a_)			)      ,						requires_grad=a_			)




							def    UpperCamelCase     (				self   :						Optional[Any]      ,						a_   :						Union[str, Any]			)			->					List[str]:
										if self.use_layer_scale:
													snake_case:    int      							=self.pooling(self.before_norm(a_			)			)
													snake_case:    Optional[Any]      							=self.layer_scale_a.unsqueeze(-1			).unsqueeze(-1			) * pooling_output
													# First residual connection
													snake_case:    Tuple      							=hidden_states + self.drop_path(a_			)
													snake_case:    Optional[Any]      							=()

													snake_case:    Optional[Any]      							=self.output(self.after_norm(a_			)			)
													snake_case:    int      							=self.layer_scale_a.unsqueeze(-1			).unsqueeze(-1			) * layer_output
													# Second residual connection
													snake_case:    str      							=hidden_states + self.drop_path(a_			)

													snake_case:    Dict      							=(output,) + outputs
													return outputs

										else:
													snake_case:    Dict      							=self.drop_path(self.pooling(self.before_norm(a_			)			)			)
													# First residual connection
													snake_case:    Any      							=pooling_output + hidden_states
													snake_case:    Union[str, Any]      							=()

													# Second residual connection inside the PoolFormerOutput block
													snake_case:    List[str]      							=self.drop_path(self.output(self.after_norm(a_			)			)			)
													snake_case:    Tuple      							=hidden_states + layer_output

													snake_case:    List[Any]      							=(output,) + outputs
													return outputs






class 						a_		(     nn.Module    ):


							def __init__(				self   :						List[Any]      ,						a_   :						int			)			->					Optional[int]:
										super().__init__()
										snake_case:    int      							=config
										# stochastic depth decay rule
										snake_case:    int      							=[x.item() for x in torch.linspace(0      ,						config.drop_path_rate      ,						sum(config.depths			)			)]

										# patch embeddings
										snake_case:    List[str]      							=[]
										for i in range(config.num_encoder_blocks			):
													embeddings.append(
													    PoolFormerEmbeddings(
													        patch_size=config.patch_sizes[i]      ,						stride=config.strides[i]      ,						padding=config.padding[i]      ,						num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1]      ,						hidden_size=config.hidden_sizes[i]      ,						)			)
										snake_case:    Union[str, Any]      							=nn.ModuleList(a_			)

										# Transformer blocks
										snake_case:    List[str]      							=[]
										snake_case:    str      							=0
										for i in range(config.num_encoder_blocks			):
													# each block consists of layers
													snake_case:    Optional[int]      							=[]
													if i != 0:
																cur += config.depths[i - 1]
													for j in range(config.depths[i]			):
																layers.append(
																    PoolFormerLayer(
																        a_      ,						num_channels=config.hidden_sizes[i]      ,						pool_size=config.pool_size      ,						hidden_size=config.hidden_sizes[i]      ,						intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio			)      ,						drop_path=dpr[cur + j]      ,						)			)
													blocks.append(nn.ModuleList(a_			)			)

										snake_case:    Dict      							=nn.ModuleList(a_			)




							def    UpperCamelCase     (				self   :						Any      ,						a_   :						Tuple      ,						a_   :						str=False      ,						a_   :						int=True			)			->					Dict:
										snake_case:    List[Any]      							=() if output_hidden_states else None

										snake_case:    Tuple      							=pixel_values
										for idx, layers in enumerate(zip(self.patch_embeddings      ,						self.block			)			):
													snake_case							,  snake_case:    str      							=layers
													# Get patch embeddings from hidden_states
													snake_case:    List[str]      							=embedding_layer(a_			)
													# Send the embeddings through the blocks
													for _, blk in enumerate(a_			):
																snake_case:    Optional[int]      							=blk(a_			)
																snake_case:    int      							=layer_outputs[0]

													if output_hidden_states:
																snake_case:    str      							=all_hidden_states + (hidden_states,)

										if not return_dict:
													return tuple(v for v in [hidden_states, all_hidden_states] if v is not None			)

										return BaseModelOutputWithNoAttention(last_hidden_state=a_      ,						hidden_states=a_			)






class 						a_		(     snake_case    ):
							UpperCAmelCase			:		int        =				PoolFormerConfig
							UpperCAmelCase			:		Optional[Any]        =				"""poolformer"""
							UpperCAmelCase			:		List[str]        =				"""pixel_values"""
							UpperCAmelCase			:		List[str]        =				True


							def    UpperCamelCase     (				self   :						Optional[Any]      ,						a_   :						List[Any]			)			->					int:
										if isinstance(a_      ,						(nn.Linear, nn.Convad)			):
													module.weight.data.normal_(mean=0.0      ,						std=self.config.initializer_range			)
													if module.bias is not None:
																module.bias.data.zero_()
										elif isinstance(a_      ,						nn.LayerNorm			):
													module.bias.data.zero_()
													module.weight.data.fill_(1.0			)




							def    UpperCamelCase     (				self   :						Optional[int]      ,						a_   :						Optional[Any]      ,						a_   :						Optional[Any]=False			)			->					str:
										if isinstance(a_      ,						a_			):
													snake_case:    Any      							=value


a							   =				r'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

a							   =				r'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`PoolFormerImageProcessor.__call__`] for details.\n'






@add_start_docstrings(
    """The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top."""    ,   snake_case    ,   )
class 						a_		(     snake_case    ):


							def __init__(				self   :						List[Any]      ,						a_   :						Dict			)			->					Union[str, Any]:
										super().__init__(a_			)
										snake_case:    List[str]      							=config

										snake_case:    Optional[int]      							=PoolFormerEncoder(a_			)

										# Initialize weights and apply final processing
										self.post_init()


							def    UpperCamelCase     (				self   :						Optional[int]			)			->					List[Any]:
										return self.embeddings.patch_embeddings




							@add_start_docstrings_to_model_forward(a_			)
							@add_code_sample_docstrings(
							    checkpoint=_CHECKPOINT_FOR_DOC      ,						output_type=a_      ,						config_class=_CONFIG_FOR_DOC      ,						modality='vision'      ,						expected_output=_EXPECTED_OUTPUT_SHAPE      ,						)
							def    UpperCamelCase     (				self   :						List[Any]      ,						a_   :						Optional[torch.FloatTensor] = None      ,						a_   :						Optional[bool] = None      ,						a_   :						Optional[bool] = None      ,						)			->					Union[Tuple, BaseModelOutputWithNoAttention]:
										snake_case:    Optional[Any]      							=(
										    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
										)
										snake_case:    int      							=return_dict if return_dict is not None else self.config.use_return_dict

										if pixel_values is None:
													raise ValueError('You have to specify pixel_values'			)

										snake_case:    List[str]      							=self.encoder(
										    a_      ,						output_hidden_states=a_      ,						return_dict=a_      ,						)
										snake_case:    Tuple      							=encoder_outputs[0]

										if not return_dict:
													return (sequence_output, None) + encoder_outputs[1:]

										return BaseModelOutputWithNoAttention(
										    last_hidden_state=a_      ,						hidden_states=encoder_outputs.hidden_states      ,						)






class 						a_		(     nn.Module    ):


							def __init__(				self   :						Optional[Any]      ,						a_   :						str			)			->					Optional[int]:
										super().__init__()
										snake_case:    str      							=nn.Linear(config.hidden_size      ,						config.hidden_size			)




							def    UpperCamelCase     (				self   :						Dict      ,						a_   :						Tuple			)			->					int:
										snake_case:    Tuple      							=self.dense(a_			)
										return output






@add_start_docstrings(
    """
    PoolFormer Model transformer with an image classification head on top
    """    ,   snake_case    ,   )
class 						a_		(     snake_case    ):


							def __init__(				self   :						int      ,						a_   :						int			)			->					Optional[int]:
										super().__init__(a_			)
										snake_case:    List[str]      							=config.num_labels
										snake_case:    List[Any]      							=PoolFormerModel(a_			)

										# Final norm
										snake_case:    Optional[Any]      							=PoolFormerGroupNorm(config.hidden_sizes[-1]			)
										# Classifier head
										snake_case:    List[Any]      							=(
										    nn.Linear(config.hidden_sizes[-1]      ,						config.num_labels			) if config.num_labels > 0 else nn.Identity()
										)

										# Initialize weights and apply final processing
										self.post_init()




							@add_start_docstrings_to_model_forward(a_			)
							@add_code_sample_docstrings(
							    checkpoint=_IMAGE_CLASS_CHECKPOINT      ,						output_type=a_      ,						config_class=_CONFIG_FOR_DOC      ,						expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT      ,						)
							def    UpperCamelCase     (				self   :						Optional[Any]      ,						a_   :						Optional[torch.FloatTensor] = None      ,						a_   :						Optional[torch.LongTensor] = None      ,						a_   :						Optional[bool] = None      ,						a_   :						Optional[bool] = None      ,						)			->					Union[Tuple, ImageClassifierOutputWithNoAttention]:
										snake_case:    Optional[int]      							=return_dict if return_dict is not None else self.config.use_return_dict

										snake_case:    int      							=self.poolformer(
										    a_      ,						output_hidden_states=a_      ,						return_dict=a_      ,						)

										snake_case:    Optional[Any]      							=outputs[0]

										snake_case:    Any      							=self.classifier(self.norm(a_			).mean([-2, -1]			)			)

										snake_case:    int      							=None
										if labels is not None:
													if self.config.problem_type is None:
																if self.num_labels == 1:
																			snake_case:    Any      							='regression'
																elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																			snake_case:    Dict      							='single_label_classification'
																else:
																			snake_case:    Dict      							='multi_label_classification'

													if self.config.problem_type == "regression":
																snake_case:    Dict      							=MSELoss()
																if self.num_labels == 1:
																			snake_case:    Any      							=loss_fct(logits.squeeze()      ,						labels.squeeze()			)
																else:
																			snake_case:    Dict      							=loss_fct(a_      ,						a_			)
													elif self.config.problem_type == "single_label_classification":
																snake_case:    str      							=CrossEntropyLoss()
																snake_case:    Dict      							=loss_fct(logits.view(-1      ,						self.num_labels			)      ,						labels.view(-1			)			)
													elif self.config.problem_type == "multi_label_classification":
																snake_case:    int      							=BCEWithLogitsLoss()
																snake_case:    Union[str, Any]      							=loss_fct(a_      ,						a_			)

										if not return_dict:
													snake_case:    Optional[int]      							=(logits,) + outputs[2:]
													return ((loss,) + output) if loss is not None else output

										return ImageClassifierOutputWithNoAttention(loss=a_      ,						logits=a_      ,						hidden_states=outputs.hidden_states			)









'''simple docstring'''

import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_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_torch_available():
	import torch

if is_datasets_available():
	from datasets import load_dataset

a							   =				random.Random()







def a_     (    __UpperCAmelCase				,							__UpperCAmelCase=1.0				,							__UpperCAmelCase=None				,							__UpperCAmelCase=None					)				->					List[str]:





			"""simple docstring"""







			if rng is None:
						snake_case:    Dict      							=global_rng

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

			return values






class 						a_		(     unittest.TestCase    ):


							def __init__(				self   :						int      ,						a_   :						Any      ,						a_   :						str=7      ,						a_   :						Tuple=4_0_0      ,						a_   :						List[Any]=2_0_0_0      ,						a_   :						str=2_0_4_8      ,						a_   :						List[str]=1_2_8      ,						a_   :						int=1      ,						a_   :						Tuple=5_1_2      ,						a_   :						Dict=3_0      ,						a_   :						Optional[int]=4_4_1_0_0      ,						)			->					Union[str, Any]:
										snake_case:    Union[str, Any]      							=parent
										snake_case:    Optional[Any]      							=batch_size
										snake_case:    Union[str, Any]      							=min_seq_length
										snake_case:    List[str]      							=max_seq_length
										snake_case:    str      							=(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
										snake_case:    int      							=spectrogram_length
										snake_case:    List[str]      							=feature_size
										snake_case:    Dict      							=num_audio_channels
										snake_case:    int      							=hop_length
										snake_case:    List[Any]      							=chunk_length
										snake_case:    Optional[Any]      							=sampling_rate


							def    UpperCamelCase     (				self   :						Any			)			->					Dict:
										return {
										    "spectrogram_length": self.spectrogram_length,
										    "feature_size": self.feature_size,
										    "num_audio_channels": self.num_audio_channels,
										    "hop_length": self.hop_length,
										    "chunk_length": self.chunk_length,
										    "sampling_rate": self.sampling_rate,
										}




							def    UpperCamelCase     (				self   :						Tuple      ,						a_   :						Optional[Any]=False      ,						a_   :						Tuple=False			)			->					Optional[int]:
										def _flatten(a_   :						Dict			):
													return list(itertools.chain(*a_			)			)

										if equal_length:
													snake_case:    Union[str, Any]      							=[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:    Union[str, Any]      							=[
													    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:    Any      							=[np.asarray(a_			) for x in speech_inputs]
										return speech_inputs






@require_torch
@require_torchaudio
class 						a_		(     snake_case    ,   unittest.TestCase    ):
							UpperCAmelCase			:		Optional[Any]        =				TvltFeatureExtractor


							def    UpperCamelCase     (				self   :						int			)			->					int:
										snake_case:    Union[str, Any]      							=TvltFeatureExtractionTester(self			)


							def    UpperCamelCase     (				self   :						Optional[Any]			)			->					str:
										snake_case:    int      							=self.feature_extraction_class(**self.feat_extract_dict			)
										self.assertTrue(hasattr(a_      ,						'spectrogram_length'			)			)
										self.assertTrue(hasattr(a_      ,						'feature_size'			)			)
										self.assertTrue(hasattr(a_      ,						'num_audio_channels'			)			)
										self.assertTrue(hasattr(a_      ,						'hop_length'			)			)
										self.assertTrue(hasattr(a_      ,						'chunk_length'			)			)
										self.assertTrue(hasattr(a_      ,						'sampling_rate'			)			)


							def    UpperCamelCase     (				self   :						Tuple			)			->					Optional[int]:
										snake_case:    Tuple      							=self.feature_extraction_class(**self.feat_extract_dict			)

										with tempfile.TemporaryDirectory() as tmpdirname:
													snake_case:    Optional[Any]      							=feat_extract_first.save_pretrained(a_			)[0]
													check_json_file_has_correct_format(a_			)
													snake_case:    str      							=self.feature_extraction_class.from_pretrained(a_			)

										snake_case:    Optional[int]      							=feat_extract_first.to_dict()
										snake_case:    List[str]      							=feat_extract_second.to_dict()
										snake_case:    Optional[int]      							=dict_first.pop('mel_filters'			)
										snake_case:    List[Any]      							=dict_second.pop('mel_filters'			)
										self.assertTrue(np.allclose(a_      ,						a_			)			)
										self.assertEqual(a_      ,						a_			)


							def    UpperCamelCase     (				self   :						str			)			->					str:
										snake_case:    Any      							=self.feature_extraction_class(**self.feat_extract_dict			)

										with tempfile.TemporaryDirectory() as tmpdirname:
													snake_case:    int      							=os.path.join(a_      ,						'feat_extract.json'			)
													feat_extract_first.to_json_file(a_			)
													snake_case:    Union[str, Any]      							=self.feature_extraction_class.from_json_file(a_			)

										snake_case:    Optional[Any]      							=feat_extract_first.to_dict()
										snake_case:    Optional[Any]      							=feat_extract_second.to_dict()
										snake_case:    Union[str, Any]      							=dict_first.pop('mel_filters'			)
										snake_case:    Union[str, Any]      							=dict_second.pop('mel_filters'			)
										self.assertTrue(np.allclose(a_      ,						a_			)			)
										self.assertEqual(a_      ,						a_			)


							def    UpperCamelCase     (				self   :						Union[str, Any]			)			->					List[str]:
										# Initialize feature_extractor
										snake_case:    Tuple      							=self.feature_extraction_class(**self.feat_extract_dict			)

										# create three inputs of length 800, 1000, and 1200
										snake_case:    Any      							=[floats_list((1, x)			)[0] for x in range(8_0_0      ,						1_4_0_0      ,						2_0_0			)]
										snake_case:    Optional[Any]      							=[np.asarray(a_			) for speech_input in speech_inputs]

										# Test not batched input
										snake_case:    List[Any]      							=feature_extractor(np_speech_inputs[0]      ,						return_tensors='np'      ,						sampling_rate=4_4_1_0_0			).audio_values

										self.assertTrue(encoded_audios.ndim == 4			)
										self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size			)
										self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length			)
										self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels			)

										# Test batched
										snake_case:    Any      							=feature_extractor(a_      ,						return_tensors='np'      ,						sampling_rate=4_4_1_0_0			).audio_values

										self.assertTrue(encoded_audios.ndim == 4			)
										self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size			)
										self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length			)
										self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels			)

										# Test audio masking
										snake_case:    Optional[Any]      							=feature_extractor(
										    a_      ,						return_tensors='np'      ,						sampling_rate=4_4_1_0_0      ,						mask_audio=a_			).audio_values

										self.assertTrue(encoded_audios.ndim == 4			)
										self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size			)
										self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length			)
										self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels			)

										# Test 2-D numpy arrays are batched.
										snake_case:    List[Any]      							=[floats_list((1, x)			)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
										snake_case:    List[str]      							=np.asarray(a_			)
										snake_case:    str      							=feature_extractor(a_      ,						return_tensors='np'      ,						sampling_rate=4_4_1_0_0			).audio_values
										self.assertTrue(encoded_audios.ndim == 4			)
										self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size			)
										self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length			)
										self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels			)


							def    UpperCamelCase     (				self   :						Any      ,						a_   :						str			)			->					Union[str, Any]:
										snake_case:    Dict      							=load_dataset('hf-internal-testing/librispeech_asr_dummy'      ,						'clean'      ,						split='validation'			)
										# automatic decoding with librispeech
										snake_case:    Optional[int]      							=ds.sort('id'			).select(range(a_			)			)[:num_samples]['audio']

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




							def    UpperCamelCase     (				self   :						Optional[Any]			)			->					Optional[int]:
										snake_case:    Tuple      							=self._load_datasamples(1			)
										snake_case:    Any      							=TvltFeatureExtractor()
										snake_case:    Any      							=feature_extractor(a_      ,						return_tensors='pt'			).audio_values

										self.assertEquals(audio_values.shape      ,						(1, 1, 1_9_2, 1_2_8)			)

										snake_case:    int      							=torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]]			)
										self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]      ,						a_      ,						atol=1E-4			)			)




