




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




						"""simple docstring"""





						def __init__(				self    ,				A    ,				A = 1_3    ,				A = 6_4    ,				A = 2    ,				A = 3    ,				A = 3    ,				A = True    ,				A = True    ,				A = 1_2_8    ,				A=[1_6, 3_2, 6_4, 1_2_8]    ,				A = 7    ,				A = 4    ,				A = 3_7    ,				A = "gelu"    ,				A = 0.1    ,				A = 0.1    ,				A = 1_0    ,				A = 0.02    ,				A = 2    ,				A = 1    ,				A = 1_2_8    ,				A = [2, 2, 2, 2]    ,				A = 2    ,				A = 2    ,				)		->    Any:
									snake_case				:    List[str]           =	parent
									snake_case				:    int           =	batch_size
									snake_case				:    int           =	image_size
									snake_case				:    Optional[Any]           =	patch_size
									snake_case				:    Union[str, Any]           =	num_channels
									snake_case				:    Tuple           =	is_training
									snake_case				:    int           =	use_labels
									snake_case				:    Optional[Any]           =	hidden_size
									snake_case				:    Union[str, Any]           =	num_hidden_layers
									snake_case				:    Dict           =	num_attention_heads
									snake_case				:    List[str]           =	intermediate_size
									snake_case				:    Any           =	hidden_act
									snake_case				:    str           =	hidden_dropout_prob
									snake_case				:    Any           =	attention_probs_dropout_prob
									snake_case				:    Dict           =	type_sequence_label_size
									snake_case				:    List[Any]           =	initializer_range
									snake_case				:    Tuple           =	encoder_stride
									snake_case				:    Dict           =	num_attention_outputs
									snake_case				:    str           =	embed_dim
									snake_case				:    Tuple           =	embed_dim + 1
									snake_case				:    Union[str, Any]           =	resolution
									snake_case				:    Optional[Any]           =	depths
									snake_case				:    Tuple           =	hidden_sizes
									snake_case				:    List[Any]           =	dim
									snake_case				:    int           =	mlp_expansion_ratio

						def      UpperCAmelCase				(				self     )		->    int:
									snake_case				:    Optional[int]           =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

									snake_case				:    List[Any]           =	None
									if self.use_labels:
												snake_case				:    Tuple           =	ids_tensor([self.batch_size]    ,				self.type_sequence_label_size     )

									snake_case				:    Union[str, Any]           =	self.get_config()

									return config, pixel_values, labels

						def      UpperCAmelCase				(				self     )		->    int:
									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      UpperCAmelCase				(				self    ,				A    ,				A    ,				A     )		->    List[Any]:
									snake_case				:    Optional[Any]           =	TFEfficientFormerModel(config=A     )
									snake_case				:    Optional[int]           =	model(A    ,				training=A     )
									self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.hidden_size)     )

						def      UpperCAmelCase				(				self    ,				A    ,				A    ,				A     )		->    Tuple:
									snake_case				:    Union[str, Any]           =	self.type_sequence_label_size
									snake_case				:    Dict           =	TFEfficientFormerForImageClassification(A     )
									snake_case				:    Dict           =	model(A    ,				labels=A    ,				training=A     )
									self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.type_sequence_label_size)     )

									# test greyscale images
									snake_case				:    Optional[Any]           =	1
									snake_case				:    Tuple           =	TFEfficientFormerForImageClassification(A     )

									snake_case				:    Tuple           =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
									snake_case				:    List[str]           =	model(A    ,				labels=A     )
									self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.type_sequence_label_size)     )

						def      UpperCAmelCase				(				self     )		->    Dict:
									snake_case				:    Tuple           =	self.prepare_config_and_inputs()
									snake_case				:    int           =	config_and_inputs
									snake_case				:    Union[str, Any]           =	{"""pixel_values""": pixel_values}
									return config, inputs_dict




@require_tf
class 						__lowercase		(UpperCamelCase__					,		UpperCamelCase__					,		unittest.TestCase							):




						"""simple docstring"""





						_snake_case												=						(
						    (
						        TFEfficientFormerModel,
						        TFEfficientFormerForImageClassificationWithTeacher,
						        TFEfficientFormerForImageClassification,
						    )
						    if is_tf_available()
						    else ()
						)
						_snake_case												=						(
						    {
						        """feature-extraction""": TFEfficientFormerModel,
						        """image-classification""": (
						            TFEfficientFormerForImageClassification,
						            TFEfficientFormerForImageClassificationWithTeacher,
						        ),
						    }
						    if is_tf_available()
						    else {}
						)

						_snake_case												=						False

						_snake_case												=						False
						_snake_case												=						False
						_snake_case												=						False
						_snake_case												=						False

						def      UpperCAmelCase				(				self     )		->    Any:
									snake_case				:    Union[str, Any]           =	TFEfficientFormerModelTester(self     )
									snake_case				:    Tuple           =	ConfigTester(
									    self    ,				config_class=A    ,				has_text_modality=A    ,				hidden_size=3_7     )

						def      UpperCAmelCase				(				self     )		->    Union[str, Any]:
									self.config_tester.run_common_tests()

						@unittest.skip(reason="""EfficientFormer does not use inputs_embeds"""     )
						def      UpperCAmelCase				(				self     )		->    Dict:
									pass

						@unittest.skip(reason="""EfficientFormer does not support input and output embeddings"""     )
						def      UpperCAmelCase				(				self     )		->    Tuple:
									pass

						def      UpperCAmelCase				(				self     )		->    Optional[Any]:
									snake_case				:    Tuple           =	self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												snake_case				:    Union[str, Any]           =	model_class(A     )
												snake_case				:    Any           =	inspect.signature(model.call     )
												# signature.parameters is an OrderedDict => so arg_names order is deterministic
												snake_case				:    Optional[int]           =	[*signature.parameters.keys()]

												snake_case				:    str           =	["""pixel_values"""]
												self.assertListEqual(arg_names[:1]    ,				A     )

						def      UpperCAmelCase				(				self     )		->    Optional[Any]:
									def check_hidden_states_output(A    ,				A    ,				A     ):
												snake_case				:    str           =	model_class(A     )

												snake_case				:    List[str]           =	model(**self._prepare_for_class(A    ,				A     )    ,				training=A     )
												snake_case				:    Dict           =	outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

												snake_case				:    Any           =	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"""     ):
															snake_case				:    Tuple           =	self.model_tester.encoder_seq_length
															if hasattr(self.model_tester    ,				"""chunk_length"""     ) and self.model_tester.chunk_length > 1:
																		snake_case				:    Optional[int]           =	seq_length * self.model_tester.chunk_length
												else:
															snake_case				:    Optional[Any]           =	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:
															snake_case				:    Any           =	outputs.decoder_hidden_states

															self.asseretIsInstance(A    ,				(list, tuple)     )
															self.assertEqual(len(A     )    ,				A     )
															snake_case				:    List[Any]           =	getattr(self.model_tester    ,				"""seq_length"""    ,				A     )
															snake_case				:    Tuple           =	getattr(self.model_tester    ,				"""decoder_seq_length"""    ,				A     )

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

									snake_case				:    List[Any]           =	self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												snake_case				:    Union[str, Any]           =	True
												check_hidden_states_output(A    ,				A    ,				A     )

												# check that output_hidden_states also work using config
												del inputs_dict["output_hidden_states"]
												snake_case				:    Optional[int]           =	True

												check_hidden_states_output(A    ,				A    ,				A     )

						def      UpperCAmelCase				(				self    ,				A    ,				A    ,				A=False     )		->    List[str]:
									snake_case				:    Union[str, Any]           =	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      UpperCAmelCase				(				self     )		->    Union[str, Any]:
									snake_case				:    List[str]           =	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      UpperCAmelCase				(				self     )		->    Any:
									snake_case				:    List[Any]           =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*A     )

						def      UpperCAmelCase				(				self     )		->    int:
									snake_case				:    Any           =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*A     )

						@slow
						def      UpperCAmelCase				(				self     )		->    str:
									for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												snake_case				:    List[Any]           =	TFEfficientFormerModel.from_pretrained(A     )
												self.assertIsNotNone(A     )

						def      UpperCAmelCase				(				self     )		->    List[str]:
									snake_case				:    Union[str, Any]           =	self.model_tester.prepare_config_and_inputs_for_common()
									snake_case				:    List[Any]           =	True

									snake_case				:    Optional[int]           =	getattr(self.model_tester    ,				"""seq_length"""    ,				A     )
									snake_case				:    Optional[Any]           =	getattr(self.model_tester    ,				"""encoder_seq_length"""    ,				A     )
									snake_case				:    int           =	getattr(self.model_tester    ,				"""key_length"""    ,				A     )
									snake_case				:    List[str]           =	getattr(self.model_tester    ,				"""chunk_length"""    ,				A     )

									if chunk_length is not None and hasattr(self.model_tester    ,				"""num_hashes"""     ):
												snake_case				:    List[str]           =	encoder_seq_length * self.model_tester.num_hashes

									for model_class in self.all_model_classes:
												snake_case				:    Tuple           =	True
												snake_case				:    Tuple           =	False
												snake_case				:    Optional[Any]           =	True
												snake_case				:    Dict           =	model_class(A     )

												snake_case				:    Any           =	model(**self._prepare_for_class(A    ,				A     )    ,				training=A     )
												snake_case				:    Optional[int]           =	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"]
												snake_case				:    Optional[Any]           =	True
												snake_case				:    Dict           =	model_class(A     )
												snake_case				:    Tuple           =	model(**self._prepare_for_class(A    ,				A     )    ,				training=A     )

												snake_case				:    List[Any]           =	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      UpperCAmelCase				(				self     )		->    Union[str, Any]:
									# 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
									snake_case				:    Dict           =	self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												# Prepare our model
												snake_case				:    int           =	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
												snake_case				:    Any           =	{
												    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
												}
												snake_case				:    Union[str, Any]           =	model(A     )
												self.assertTrue(outputs_dict is not None     )




def 						SCREAMING_SNAKE_CASE__ (   )  ->       Any:
			snake_case				:    Dict           =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
			return image




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




						"""simple docstring"""





						@cached_property
						def      UpperCAmelCase				(				self     )		->    int:
									return (
									    EfficientFormerImageProcessor.from_pretrained("""snap-research/efficientformer-l1-300"""     )
									    if is_vision_available()
									    else None
									)

						@slow
						def      UpperCAmelCase				(				self     )		->    List[str]:
									snake_case				:    Optional[Any]           =	TFEfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300"""     )
									snake_case				:    List[str]           =	self.default_image_processor
									snake_case				:    Optional[Any]           =	prepare_img()
									snake_case				:    Tuple           =	image_processor(images=A    ,				return_tensors="""tf"""     )
									# forward pass
									snake_case				:    str           =	model(**A    ,				training=A     )
									# verify the logits
									snake_case				:    Dict           =	tf.TensorShape((1, 1_0_0_0)     )
									self.assertEqual(outputs.logits.shape    ,				A     )
									snake_case				:    List[str]           =	tf.constant([-0.05_55, 0.48_25, -0.08_52]     )
									self.assertTrue(np.allclose(outputs.logits[0, :3]    ,				A    ,				atol=1e-4     )     )

						@slow
						def      UpperCAmelCase				(				self     )		->    Tuple:
									snake_case				:    Dict           =	TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
									    """snap-research/efficientformer-l1-300"""     )
									snake_case				:    str           =	self.default_image_processor
									snake_case				:    int           =	prepare_img()
									snake_case				:    str           =	image_processor(images=A    ,				return_tensors="""tf"""     )
									# forward pass
									snake_case				:    Optional[Any]           =	model(**A    ,				training=A     )
									# verify the logits
									snake_case				:    List[Any]           =	tf.TensorShape((1, 1_0_0_0)     )
									self.assertEqual(outputs.logits.shape    ,				A     )
									snake_case				:    List[str]           =	tf.constant([-0.13_12, 0.43_53, -1.04_99]     )
									self.assertTrue(np.allclose(outputs.logits[0, :3]    ,				A    ,				atol=1e-4     )     )











def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase					)  ->       int:
   return int((input_a, input_a).count(1					) != 0					)




def 						SCREAMING_SNAKE_CASE__ (   )  ->       None:
   assert or_gate(0						,0					) == 0
   assert or_gate(0						,1					) == 1
   assert or_gate(1						,0					) == 1
   assert or_gate(1						,1					) == 1


if __name__ == "__main__":
       print(or_gate(0, 1))
       print(or_gate(1, 0))
       print(or_gate(0, 0))
       print(or_gate(1, 1))





