




"""simple docstring"""




import inspect
import unittest
from typing import List

import numpy as np

from transformers import EfficientFormerConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import (
					    TFEfficientFormerForImageClassification,
					    TFEfficientFormerForImageClassificationWithTeacher,
					    TFEfficientFormerModel,
					)
					from transformers.models.efficientformer.modeling_tf_efficientformer import (
					    TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
					)


if is_vision_available():
					from PIL import Image

					from transformers import EfficientFormerImageProcessor






class     __magic_name__							:






				'''simple docstring'''





				def __init__(       self		, _a		, _a = 13		, _a = 64		, _a = 2		, _a = 3		, _a = 3		, _a = True		, _a = True		, _a = 128		, _a=[16, 32, 64, 128]		, _a = 7		, _a = 4		, _a = 37		, _a = "gelu"		, _a = 0.1		, _a = 0.1		, _a = 10		, _a = 0.02		, _a = 2		, _a = 1		, _a = 128		, _a = [2, 2, 2, 2]		, _a = 2		, _a = 2		, ):





							"""simple docstring"""
							lowerCamelCase			      =					parent
							lowerCamelCase			      =					batch_size
							lowerCamelCase			      =					image_size
							lowerCamelCase			      =					patch_size
							lowerCamelCase			      =					num_channels
							lowerCamelCase			      =					is_training
							lowerCamelCase			      =					use_labels
							lowerCamelCase			      =					hidden_size
							lowerCamelCase			      =					num_hidden_layers
							lowerCamelCase			      =					num_attention_heads
							lowerCamelCase			      =					intermediate_size
							lowerCamelCase			      =					hidden_act
							lowerCamelCase			      =					hidden_dropout_prob
							lowerCamelCase			      =					attention_probs_dropout_prob
							lowerCamelCase			      =					type_sequence_label_size
							lowerCamelCase			      =					initializer_range
							lowerCamelCase			      =					encoder_stride
							lowerCamelCase			      =					num_attention_outputs
							lowerCamelCase			      =					embed_dim
							lowerCamelCase			      =					embed_dim + 1
							lowerCamelCase			      =					resolution
							lowerCamelCase			      =					depths
							lowerCamelCase			      =					hidden_sizes
							lowerCamelCase			      =					dim
							lowerCamelCase			      =					mlp_expansion_ratio





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

							lowerCamelCase			      =					None
							if self.use_labels:
										lowerCamelCase			      =					ids_tensor([self.batch_size]		, self.type_sequence_label_size )

							lowerCamelCase			      =					self.get_config()

							return config, pixel_values, labels





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return EfficientFormerConfig(
							    image_size=self.image_size		, patch_size=self.patch_size		, num_channels=self.num_channels		, hidden_size=self.hidden_size		, num_hidden_layers=self.num_hidden_layers		, num_attention_heads=self.num_attention_heads		, intermediate_size=self.intermediate_size		, hidden_act=self.hidden_act		, hidden_dropout_prob=self.hidden_dropout_prob		, attention_probs_dropout_prob=self.attention_probs_dropout_prob		, is_decoder=_a		, initializer_range=self.initializer_range		, encoder_stride=self.encoder_stride		, resolution=self.resolution		, depths=self.depths		, hidden_sizes=self.hidden_sizes		, dim=self.dim		, mlp_expansion_ratio=self.mlp_expansion_ratio		, )





				def    _lowerCAmelCase				(       self		, _a		, _a		, _a ):





							"""simple docstring"""
							lowerCamelCase			      =					TFEfficientFormerModel(config=_a )
							lowerCamelCase			      =					model(_a		, training=_a )
							self.parent.assertEqual(result.last_hidden_state.shape		, (self.batch_size, self.seq_length, self.hidden_size) )





				def    _lowerCAmelCase				(       self		, _a		, _a		, _a ):





							"""simple docstring"""
							lowerCamelCase			      =					self.type_sequence_label_size
							lowerCamelCase			      =					TFEfficientFormerForImageClassification(_a )
							lowerCamelCase			      =					model(_a		, labels=_a		, training=_a )
							self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.type_sequence_label_size) )

							# test greyscale images
							lowerCamelCase			      =					1
							lowerCamelCase			      =					TFEfficientFormerForImageClassification(_a )

							lowerCamelCase			      =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
							lowerCamelCase			      =					model(_a		, labels=_a )
							self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.type_sequence_label_size) )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.prepare_config_and_inputs()
							lowerCamelCase      ,   lowerCamelCase      ,   lowerCamelCase			      =					config_and_inputs
							lowerCamelCase			      =					{"""pixel_values""": pixel_values}
							return config, inputs_dict






@require_tf
class     __magic_name__							(       UpperCAmelCase__						,					UpperCAmelCase__						,					unittest.TestCase				):






				'''simple docstring'''
				__UpperCamelCase			  =						(
				    (
				        TFEfficientFormerModel,
				        TFEfficientFormerForImageClassificationWithTeacher,
				        TFEfficientFormerForImageClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				__UpperCamelCase			  =						(
				    {
				        "feature-extraction": TFEfficientFormerModel,
				        "image-classification": (
				            TFEfficientFormerForImageClassification,
				            TFEfficientFormerForImageClassificationWithTeacher,
				        ),
				    }
				    if is_tf_available()
				    else {}
				)

				__UpperCamelCase			  =						False

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





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					TFEfficientFormerModelTester(self )
							lowerCamelCase			      =					ConfigTester(
							    self		, config_class=_a		, has_text_modality=_a		, hidden_size=37 )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							self.config_tester.run_common_tests()





				@unittest.skip(reason="""EfficientFormer does not use inputs_embeds""" )
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							pass





				@unittest.skip(reason="""EfficientFormer does not support input and output embeddings""" )
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							pass





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase      ,   lowerCamelCase			      =					self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										lowerCamelCase			      =					model_class(_a )
										lowerCamelCase			      =					inspect.signature(model.call )
										# signature.parameters is an OrderedDict => so arg_names order is deterministic
										lowerCamelCase			      =					[*signature.parameters.keys()]

										lowerCamelCase			      =					["""pixel_values"""]
										self.assertListEqual(arg_names[:1]		, _a )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							def check_hidden_states_output(_a		, _a		, _a ):
										lowerCamelCase			      =					model_class(_a )

										lowerCamelCase			      =					model(**self._prepare_for_class(_a		, _a )		, training=_a )
										lowerCamelCase			      =					outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

										lowerCamelCase			      =					getattr(
										    self.model_tester		, """expected_num_hidden_layers"""		, self.model_tester.num_hidden_layers + 1 )
										self.assertEqual(len(_a )		, _a )

										if hasattr(self.model_tester		, """encoder_seq_length""" ):
													lowerCamelCase			      =					self.model_tester.encoder_seq_length
													if hasattr(self.model_tester		, """chunk_length""" ) and self.model_tester.chunk_length > 1:
																lowerCamelCase			      =					seq_length * self.model_tester.chunk_length
										else:
													lowerCamelCase			      =					self.model_tester.seq_length

										self.assertListEqual(
										    list(hidden_states[-1].shape[-2:] )		, [seq_length, self.model_tester.hidden_size]		, )

										if config.is_encoder_decoder:
													lowerCamelCase			      =					outputs.decoder_hidden_states

													self.asseretIsInstance(_a		, (list, tuple) )
													self.assertEqual(len(_a )		, _a )
													lowerCamelCase			      =					getattr(self.model_tester		, """seq_length"""		, _a )
													lowerCamelCase			      =					getattr(self.model_tester		, """decoder_seq_length"""		, _a )

													self.assertListEqual(
													    list(hidden_states[-1].shape[-2:] )		, [decoder_seq_length, self.model_tester.hidden_size]		, )

							lowerCamelCase      ,   lowerCamelCase			      =					self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										lowerCamelCase			      =					True
										check_hidden_states_output(_a		, _a		, _a )

										# check that output_hidden_states also work using config
										del inputs_dict["output_hidden_states"]
										lowerCamelCase			      =					True

										check_hidden_states_output(_a		, _a		, _a )





				def    _lowerCAmelCase				(       self		, _a		, _a		, _a=False ):





							"""simple docstring"""
							lowerCamelCase			      =					super()._prepare_for_class(_a		, _a		, return_labels=_a )

							if return_labels:
										if model_class.__name__ == "TFEfficientFormerForImageClassificationWithTeacher":
													del inputs_dict["labels"]

							return inputs_dict





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_a )





				@unittest.skip(reason="""EfficientFormer does not implement masked image modeling yet""" )
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*_a )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_a )





				@slow
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCamelCase			      =					TFEfficientFormerModel.from_pretrained(_a )
										self.assertIsNotNone(_a )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase      ,   lowerCamelCase			      =					self.model_tester.prepare_config_and_inputs_for_common()
							lowerCamelCase			      =					True

							lowerCamelCase			      =					getattr(self.model_tester		, """seq_length"""		, _a )
							lowerCamelCase			      =					getattr(self.model_tester		, """encoder_seq_length"""		, _a )
							lowerCamelCase			      =					getattr(self.model_tester		, """key_length"""		, _a )
							lowerCamelCase			      =					getattr(self.model_tester		, """chunk_length"""		, _a )

							if chunk_length is not None and hasattr(self.model_tester		, """num_hashes""" ):
										lowerCamelCase			      =					encoder_seq_length * self.model_tester.num_hashes

							for model_class in self.all_model_classes:
										lowerCamelCase			      =					True
										lowerCamelCase			      =					False
										lowerCamelCase			      =					True
										lowerCamelCase			      =					model_class(_a )

										lowerCamelCase			      =					model(**self._prepare_for_class(_a		, _a )		, training=_a )
										lowerCamelCase			      =					outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
										self.assertEqual(len(_a )		, self.model_tester.num_attention_outputs )

										# check that output_attentions also work using config
										del inputs_dict["output_attentions"]
										lowerCamelCase			      =					True
										lowerCamelCase			      =					model_class(_a )
										lowerCamelCase			      =					model(**self._prepare_for_class(_a		, _a )		, training=_a )

										lowerCamelCase			      =					outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
										self.assertEqual(len(_a )		, self.model_tester.num_attention_outputs )

										if chunk_length is not None:
													self.assertListEqual(
													    list(attentions[0].shape[-4:] )		, [self.model_tester.num_attention_heads, encoder_seq_length, chunk_length, encoder_key_length]		, )
										else:
													self.assertListEqual(
													    list(attentions[0].shape[-3:] )		, [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]		, )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							# We use a simplified version of this test for EfficientFormer because it requires training=False
							# and Keras refuses to let us force that during functional construction
							lowerCamelCase      ,   lowerCamelCase			      =					self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										# Prepare our model
										lowerCamelCase			      =					model_class(_a )
										# These are maximally general inputs for the model, with multiple None dimensions
										# Hopefully this will catch any conditionals that fail for flexible shapes
										lowerCamelCase			      =					{
										    key: tf.keras.Input(shape=val.shape[1:]		, dtype=val.dtype		, name=_a )
										    for key, val in model.input_signature.items()
										    if key in model.dummy_inputs
										}
										lowerCamelCase			      =					model(_a )
										self.assertTrue(outputs_dict is not None )




def     a__ (				)						->					Tuple:
			lowerCamelCase			      =					Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""				)
			return image






@require_tf
@require_vision
class     __magic_name__							(       unittest.TestCase				):






				'''simple docstring'''





				@cached_property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return (
							    EfficientFormerImageProcessor.from_pretrained("""snap-research/efficientformer-l1-300""" )
							    if is_vision_available()
							    else None
							)





				@slow
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					TFEfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300""" )
							lowerCamelCase			      =					self.default_image_processor
							lowerCamelCase			      =					prepare_img()
							lowerCamelCase			      =					image_processor(images=_a		, return_tensors="""tf""" )
							# forward pass
							lowerCamelCase			      =					model(**_a		, training=_a )
							# verify the logits
							lowerCamelCase			      =					tf.TensorShape((1, 1_000) )
							self.assertEqual(outputs.logits.shape		, _a )
							lowerCamelCase			      =					tf.constant([-0.0_555, 0.4_825, -0.0_852] )
							self.assertTrue(np.allclose(outputs.logits[0, :3]		, _a		, atol=1e-4 ) )





				@slow
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
							    """snap-research/efficientformer-l1-300""" )
							lowerCamelCase			      =					self.default_image_processor
							lowerCamelCase			      =					prepare_img()
							lowerCamelCase			      =					image_processor(images=_a		, return_tensors="""tf""" )
							# forward pass
							lowerCamelCase			      =					model(**_a		, training=_a )
							# verify the logits
							lowerCamelCase			      =					tf.TensorShape((1, 1_000) )
							self.assertEqual(outputs.logits.shape		, _a )
							lowerCamelCase			      =					tf.constant([-0.1_312, 0.4_353, -1.0_499] )
							self.assertTrue(np.allclose(outputs.logits[0, :3]		, _a		, atol=1e-4 ) )









"""simple docstring"""




from math import ceil
from typing import List, Optional, Union

import numpy as np

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


lowerCAmelCase							:			List[str]     							=							logging.get_logger(__name__)






class     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''
				__UpperCamelCase			  =						["audio_values", "audio_mask"]





				def __init__(       self		, _a=2_048		, _a=1		, _a=[16, 16]		, _a=128		, _a=44_100		, _a=86		, _a=2_048		, _a=0.0		, **_a		, ):





							"""simple docstring"""
							super().__init__(
							    feature_size=_a		, sampling_rate=_a		, padding_value=_a		, **_a		, )

							lowerCamelCase			      =					spectrogram_length
							lowerCamelCase			      =					num_channels
							lowerCamelCase			      =					patch_size
							lowerCamelCase			      =					feature_size // self.patch_size[1]
							lowerCamelCase			      =					n_fft
							lowerCamelCase			      =					sampling_rate // hop_length_to_sampling_rate
							lowerCamelCase			      =					sampling_rate
							lowerCamelCase			      =					padding_value
							lowerCamelCase			      =					mel_filter_bank(
							    num_frequency_bins=1 + n_fft // 2		, num_mel_filters=_a		, min_frequency=0.0		, max_frequency=22_050.0		, sampling_rate=_a		, norm="""slaney"""		, mel_scale="""slaney"""		, ).T





				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							lowerCamelCase			      =					spectrogram(
							    _a		, window_function(self.n_fft		, """hann""" )		, frame_length=self.n_fft		, hop_length=self.hop_length		, power=2.0		, mel_filters=self.mel_filters.T		, log_mel="""dB"""		, db_range=80.0		, )
							lowerCamelCase			      =					log_spec[:, :-1]
							lowerCamelCase			      =					log_spec - 20.0
							lowerCamelCase			      =					np.clip(log_spec / 40.0		, -2.0		, 0.0 ) + 1.0
							return log_spec





				def __call__(       self		, _a		, _a = None		, _a = True		, _a = None		, _a = False		, _a = False		, **_a		, ):





							"""simple docstring"""

							if sampling_rate is not None:
										if sampling_rate != self.sampling_rate:
													raise ValueError(
													    """This feature extractor is set to support sampling rate"""
													    f' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled'
													    f' with {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.""" )

							lowerCamelCase			      =					isinstance(_a		, np.ndarray ) and len(raw_speech.shape ) > 1
							if is_batched_numpy and len(raw_speech.shape ) > 2:
										raise ValueError(f'Only mono-channel audio is supported for input to {self}' )
							lowerCamelCase			      =					is_batched_numpy or (
							    isinstance(_a		, (list, tuple) ) and (isinstance(raw_speech[0]		, (np.ndarray, tuple, list) ))
							)
							if is_batched:
										lowerCamelCase			      =					[np.asarray([speech]		, dtype=np.floataa ).T for speech in raw_speech]
							elif not is_batched and not isinstance(_a		, np.ndarray ):
										lowerCamelCase			      =					np.asarray(_a		, dtype=np.floataa )
							elif isinstance(_a		, np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ):
										lowerCamelCase			      =					raw_speech.astype(np.floataa )
							# always return batch
							if not is_batched:
										lowerCamelCase			      =					[np.asarray([raw_speech] ).T]

							# Convert audio signals to log mel spectrograms, truncate by time axis
							lowerCamelCase			      =					[
							    self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech
							]
							if isinstance(audio_features[0]		, _a ):
										lowerCamelCase			      =					[np.asarray(_a		, dtype=np.floataa ) for feature in audio_features]

							# Create audio attention mask
							lowerCamelCase			      =					max(
							    [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] )  # The maximum number of audio patches in a batch
							if return_attention_mask:
										lowerCamelCase			      =					[
										    (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1]
										    + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0]
										    for feature in audio_features
										]
										lowerCamelCase			      =					np.array(_a ).astype(np.floataa )

							# convert into correct format for padding
							lowerCamelCase			      =					max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
							lowerCamelCase			      =					np.ones([len(_a ), 1, max_time_len, self.feature_size] ).astype(np.floataa )
							lowerCamelCase			      =					padded_audio_features * self.padding_value
							for i in range(len(_a ) ):
										lowerCamelCase			      =					audio_features[i]
										lowerCamelCase			      =					feature

							# return as BatchFeature
							if return_attention_mask:
										lowerCamelCase			      =					{"""audio_values""": padded_audio_features, """audio_mask""": audio_mask}
							else:
										lowerCamelCase			      =					{"""audio_values""": padded_audio_features}

							lowerCamelCase			      =					BatchFeature(data=_a		, tensor_type=_a )
							return encoded_inputs



