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






						'''simple docstring'''






						def __init__( self   ,       _lowercase   ,       _lowercase = 13   ,       _lowercase = 64   ,       _lowercase = 2   ,       _lowercase = 3   ,       _lowercase = 3   ,       _lowercase = True   ,       _lowercase = True   ,       _lowercase = 128   ,       _lowercase=[16, 32, 64, 128]   ,       _lowercase = 7   ,       _lowercase = 4   ,       _lowercase = 37   ,       _lowercase = "gelu"   ,       _lowercase = 0.1   ,       _lowercase = 0.1   ,       _lowercase = 10   ,       _lowercase = 0.02   ,       _lowercase = 2   ,       _lowercase = 1   ,       _lowercase = 128   ,       _lowercase = [2, 2, 2, 2]   ,       _lowercase = 2   ,       _lowercase = 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  _lowercase     ( 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  _lowercase     ( 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=_lowercase   ,       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  _lowercase     ( self   ,       _lowercase   ,       _lowercase   ,       _lowercase				):

												"""simple docstring"""



												_lowerCAmelCase  						=			TFEfficientFormerModel(config=_lowercase				)
												_lowerCAmelCase  						=			model(_lowercase   ,       training=_lowercase				)
												self.parent.assertEqual(result.last_hidden_state.shape   ,       (self.batch_size, self.seq_length, self.hidden_size)				)




						def  _lowercase     ( self   ,       _lowercase   ,       _lowercase   ,       _lowercase				):

												"""simple docstring"""



												_lowerCAmelCase  						=			self.type_sequence_label_size
												_lowerCAmelCase  						=			TFEfficientFormerForImageClassification(_lowercase				)
												_lowerCAmelCase  						=			model(_lowercase   ,       labels=_lowercase   ,       training=_lowercase				)
												self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.type_sequence_label_size)				)

												# test greyscale images
												_lowerCAmelCase  						=			1
												_lowerCAmelCase  						=			TFEfficientFormerForImageClassification(_lowercase				)

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




						def  _lowercase     ( 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   UpperCAmelCase_					(		_SCREAMING_SNAKE_CASE	,							_SCREAMING_SNAKE_CASE	,							unittest.TestCase			):






						'''simple docstring'''






						_lowercase		:				List[str]									=       (
						    (
						        TFEfficientFormerModel,
						        TFEfficientFormerForImageClassificationWithTeacher,
						        TFEfficientFormerForImageClassification,
						    )
						    if is_tf_available()
						    else ()
						)
						_lowercase		:				str									=       (
						    {
						        '''feature-extraction''': TFEfficientFormerModel,
						        '''image-classification''': (
						            TFEfficientFormerForImageClassification,
						            TFEfficientFormerForImageClassificationWithTeacher,
						        ),
						    }
						    if is_tf_available()
						    else {}
						)

						_lowercase		:				Dict									=       False

						_lowercase		:				Optional[int]									=       False
						_lowercase		:				str									=       False
						_lowercase		:				str									=       False
						_lowercase		:				int									=       False




						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			TFEfficientFormerModelTester(self				)
												_lowerCAmelCase  						=			ConfigTester(
												    self   ,       config_class=_lowercase   ,       has_text_modality=_lowercase   ,       hidden_size=37				)




						def  _lowercase     ( self				):

												"""simple docstring"""



												self.config_tester.run_common_tests()




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

												"""simple docstring"""



												pass




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

												"""simple docstring"""



												pass




						def  _lowercase     ( 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(_lowercase				)
																		_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]   ,       _lowercase				)




						def  _lowercase     ( self				):

												"""simple docstring"""



												def check_hidden_states_output(_lowercase   ,       _lowercase   ,       _lowercase				):
																		_lowerCAmelCase  						=			model_class(_lowercase				)

																		_lowerCAmelCase  						=			model(**self._prepare_for_class(_lowercase   ,       _lowercase				)   ,       training=_lowercase				)
																		_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(_lowercase				)   ,       _lowercase				)

																		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(_lowercase   ,       (list, tuple)				)
																								self.assertEqual(len(_lowercase				)   ,       _lowercase				)
																								_lowerCAmelCase  						=			getattr(self.model_tester   ,       """seq_length"""   ,       _lowercase				)
																								_lowerCAmelCase  						=			getattr(self.model_tester   ,       """decoder_seq_length"""   ,       _lowercase				)

																								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(_lowercase   ,       _lowercase   ,       _lowercase				)

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

																		check_hidden_states_output(_lowercase   ,       _lowercase   ,       _lowercase				)




						def  _lowercase     ( self   ,       _lowercase   ,       _lowercase   ,       _lowercase=False				):

												"""simple docstring"""



												_lowerCAmelCase  						=			super()._prepare_for_class(_lowercase   ,       _lowercase   ,       return_labels=_lowercase				)

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

												return inputs_dict




						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_lowercase				)




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

												"""simple docstring"""



												_lowerCAmelCase  						=			self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_image_modeling(*_lowercase				)




						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*_lowercase				)




						@slow
						def  _lowercase     ( self				):

												"""simple docstring"""



												for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		_lowerCAmelCase  						=			TFEfficientFormerModel.from_pretrained(_lowercase				)
																		self.assertIsNotNone(_lowercase				)




						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase						,   _lowerCAmelCase  						=			self.model_tester.prepare_config_and_inputs_for_common()
												_lowerCAmelCase  						=			True

												_lowerCAmelCase  						=			getattr(self.model_tester   ,       """seq_length"""   ,       _lowercase				)
												_lowerCAmelCase  						=			getattr(self.model_tester   ,       """encoder_seq_length"""   ,       _lowercase				)
												_lowerCAmelCase  						=			getattr(self.model_tester   ,       """key_length"""   ,       _lowercase				)
												_lowerCAmelCase  						=			getattr(self.model_tester   ,       """chunk_length"""   ,       _lowercase				)

												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(_lowercase				)

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

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

																		_lowerCAmelCase  						=			outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
																		self.assertEqual(len(_lowercase				)   ,       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  _lowercase     ( self				):

												"""simple docstring"""



												_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(_lowercase				)
																		# 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=_lowercase				)
																		    for key, val in model.input_signature.items()
																		    if key in model.dummy_inputs
																		}
																		_lowerCAmelCase  						=			model(_lowercase				)
																		self.assertTrue(outputs_dict is not None				)






def A				():
						_lowerCAmelCase  						=			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
						return image





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






						'''simple docstring'''






						@cached_property
						def  _lowercase     ( self				):

												"""simple docstring"""



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




						@slow
						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			TFEfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300"""				)
												_lowerCAmelCase  						=			self.default_image_processor
												_lowerCAmelCase  						=			prepare_img()
												_lowerCAmelCase  						=			image_processor(images=_lowercase   ,       return_tensors="""tf"""				)
												# forward pass
												_lowerCAmelCase  						=			model(**_lowercase   ,       training=_lowercase				)
												# verify the logits
												_lowerCAmelCase  						=			tf.TensorShape((1, 1_000)				)
												self.assertEqual(outputs.logits.shape   ,       _lowercase				)
												_lowerCAmelCase  						=			tf.constant([-0.0555, 0.4825, -0.0852]				)
												self.assertTrue(np.allclose(outputs.logits[0, :3]   ,       _lowercase   ,       atol=1e-4				)				)




						@slow
						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
												    """snap-research/efficientformer-l1-300"""				)
												_lowerCAmelCase  						=			self.default_image_processor
												_lowerCAmelCase  						=			prepare_img()
												_lowerCAmelCase  						=			image_processor(images=_lowercase   ,       return_tensors="""tf"""				)
												# forward pass
												_lowerCAmelCase  						=			model(**_lowercase   ,       training=_lowercase				)
												# verify the logits
												_lowerCAmelCase  						=			tf.TensorShape((1, 1_000)				)
												self.assertEqual(outputs.logits.shape   ,       _lowercase				)
												_lowerCAmelCase  						=			tf.constant([-0.1312, 0.4353, -1.0499]				)
												self.assertTrue(np.allclose(outputs.logits[0, :3]   ,       _lowercase   ,       atol=1e-4				)				)


'''simple docstring'''



import logging

from transformers import PretrainedConfig


_lowercase    =		logging.getLogger(__name__)


_lowercase    =		{
    """bertabs-finetuned-cnndm""": """https://huggingface.co/remi/bertabs-finetuned-cnndm-extractive-abstractive-summarization/resolve/main/config.json""",
}




class   UpperCAmelCase_					(		_SCREAMING_SNAKE_CASE			):






						'''simple docstring'''






						_lowercase		:				Optional[Any]									=       '''bertabs'''




						def __init__( self   ,       _lowercase=30_522   ,       _lowercase=512   ,       _lowercase=6   ,       _lowercase=512   ,       _lowercase=8   ,       _lowercase=512   ,       _lowercase=0.2   ,       _lowercase=6   ,       _lowercase=768   ,       _lowercase=8   ,       _lowercase=2_048   ,       _lowercase=0.2   ,       **_lowercase   ,       ):

												"""simple docstring"""



												super().__init__(**_lowercase				)

												_lowerCAmelCase  						=			vocab_size
												_lowerCAmelCase  						=			max_pos

												_lowerCAmelCase  						=			enc_layers
												_lowerCAmelCase  						=			enc_hidden_size
												_lowerCAmelCase  						=			enc_heads
												_lowerCAmelCase  						=			enc_ff_size
												_lowerCAmelCase  						=			enc_dropout

												_lowerCAmelCase  						=			dec_layers
												_lowerCAmelCase  						=			dec_hidden_size
												_lowerCAmelCase  						=			dec_heads
												_lowerCAmelCase  						=			dec_ff_size
												_lowerCAmelCase  						=			dec_dropout
