

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  lowerCamelCase__ (					_a							,			_a = 0.0							,			_a = False):
	if drop_prob == 0.0 or not training:
		return input
	SCREAMING_SNAKE_CASE   :      List[Any]				     =       1 - drop_prob
	SCREAMING_SNAKE_CASE   :      Optional[int]				     =       (input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
	SCREAMING_SNAKE_CASE   :      Any				     =       keep_prob + torch.rand(_a							,			dtype=input.dtype							,			device=input.device)
	random_tensor.floor_()  # binarize
	SCREAMING_SNAKE_CASE   :      Tuple				     =       input.div(_a) * random_tensor
	return output






class _UpperCamelCase							(							nn.Module   ):
			'''simple docstring'''







			def __init__(			self							: str      ,      a							: Optional[float] = None	)							->       None:

				"""simple docstring"""

				super().__init__()
				SCREAMING_SNAKE_CASE   :      List[str]				     =       drop_prob
			def 							__UpperCamelCase (			self							: List[str]      ,      a							: torch.Tensor	)							->       torch.Tensor:

				"""simple docstring"""

				return drop_path(a      ,      self.drop_prob      ,      self.training	)






			def 							__UpperCamelCase (			self							: List[Any]	)							->       str:

				"""simple docstring"""

				return "p={}".format(self.drop_prob	)






class _UpperCamelCase							(							nn.Module   ):
			'''simple docstring'''







			def __init__(			self							: List[Any]      ,      a							: List[str]      ,      a							: List[str]      ,      a							: Union[str, Any]      ,      a							: int      ,      a							: Dict      ,      a							: List[Any]=None	)							->       List[Any]:

				"""simple docstring"""

				super().__init__()
				SCREAMING_SNAKE_CASE   :      Tuple				     =       patch_size if isinstance(a      ,      collections.abc.Iterable	) else (patch_size, patch_size)
				SCREAMING_SNAKE_CASE   :      str				     =       stride if isinstance(a      ,      collections.abc.Iterable	) else (stride, stride)
				SCREAMING_SNAKE_CASE   :      int				     =       padding if isinstance(a      ,      collections.abc.Iterable	) else (padding, padding)

				SCREAMING_SNAKE_CASE   :      Tuple				     =       nn.Convad(a      ,      a      ,      kernel_size=a      ,      stride=a      ,      padding=a	)
				SCREAMING_SNAKE_CASE   :      Tuple				     =       norm_layer(a	) if norm_layer else nn.Identity()






			def 							__UpperCamelCase (			self							: Any      ,      a							: str	)							->       str:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       self.projection(a	)
				SCREAMING_SNAKE_CASE   :      Any				     =       self.norm(a	)
				return embeddings






class _UpperCamelCase							(							nn.GroupNorm   ):
			'''simple docstring'''







			def __init__(			self							: int      ,      a							: Optional[int]      ,      **a							: Any	)							->       Any:

				"""simple docstring"""

				super().__init__(1      ,      a      ,      **a	)






class _UpperCamelCase							(							nn.Module   ):
			'''simple docstring'''







			def __init__(			self							: Optional[Any]      ,      a							: Tuple	)							->       List[Any]:

				"""simple docstring"""

				super().__init__()
				SCREAMING_SNAKE_CASE   :      Any				     =       nn.AvgPoolad(a      ,      stride=1      ,      padding=pool_size // 2      ,      count_include_pad=a	)






			def 							__UpperCamelCase (			self							: Optional[Any]      ,      a							: int	)							->       int:

				"""simple docstring"""

				return self.pool(a	) - hidden_states






class _UpperCamelCase							(							nn.Module   ):
			'''simple docstring'''







			def __init__(			self							: Optional[Any]      ,      a							: int      ,      a							: Optional[Any]      ,      a							: Tuple      ,      a							: Optional[Any]	)							->       Optional[int]:

				"""simple docstring"""

				super().__init__()
				SCREAMING_SNAKE_CASE   :      Tuple				     =       nn.Convad(a      ,      a      ,      1	)
				SCREAMING_SNAKE_CASE   :      Tuple				     =       nn.Convad(a      ,      a      ,      1	)
				SCREAMING_SNAKE_CASE   :      Dict				     =       PoolFormerDropPath(a	)
				if isinstance(config.hidden_act      ,      a	):
					SCREAMING_SNAKE_CASE   :      str				     =       ACTaFN[config.hidden_act]
				else:
					SCREAMING_SNAKE_CASE   :      Any				     =       config.hidden_act






			def 							__UpperCamelCase (			self							: Tuple      ,      a							: Optional[Any]	)							->       Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       self.conva(a	)
				SCREAMING_SNAKE_CASE   :      int				     =       self.act_fn(a	)
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       self.drop(a	)
				SCREAMING_SNAKE_CASE   :      int				     =       self.conva(a	)
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.drop(a	)

				return hidden_states






class _UpperCamelCase							(							nn.Module   ):
			'''simple docstring'''







			def __init__(			self							: Union[str, Any]      ,      a							: Optional[int]      ,      a							: Optional[Any]      ,      a							: List[Any]      ,      a							: int      ,      a							: str      ,      a							: List[str]	)							->       Dict:

				"""simple docstring"""

				super().__init__()
				SCREAMING_SNAKE_CASE   :      int				     =       PoolFormerPooling(a	)
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       PoolFormerOutput(a      ,      a      ,      a      ,      a	)
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       PoolFormerGroupNorm(a	)
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       PoolFormerGroupNorm(a	)

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






			def 							__UpperCamelCase (			self							: Optional[Any]      ,      a							: Tuple	)							->       Any:

				"""simple docstring"""

				if self.use_layer_scale:
					SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.pooling(self.before_norm(a	)	)
					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.layer_scale_a.unsqueeze(-1	).unsqueeze(-1	) * pooling_output
					# First residual connection
					SCREAMING_SNAKE_CASE   :      Dict				     =       hidden_states + self.drop_path(a	)
					SCREAMING_SNAKE_CASE   :      int				     =       ()

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

					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       (output,) + outputs
					return outputs

				else:
					SCREAMING_SNAKE_CASE   :      Dict				     =       self.drop_path(self.pooling(self.before_norm(a	)	)	)
					# First residual connection
					SCREAMING_SNAKE_CASE   :      List[Any]				     =       pooling_output + hidden_states
					SCREAMING_SNAKE_CASE   :      List[Any]				     =       ()

					# Second residual connection inside the PoolFormerOutput block
					SCREAMING_SNAKE_CASE   :      Optional[int]				     =       self.drop_path(self.output(self.after_norm(a	)	)	)
					SCREAMING_SNAKE_CASE   :      str				     =       hidden_states + layer_output

					SCREAMING_SNAKE_CASE   :      Tuple				     =       (output,) + outputs
					return outputs






class _UpperCamelCase							(							nn.Module   ):
			'''simple docstring'''







			def __init__(			self							: int      ,      a							: Dict	)							->       List[Any]:

				"""simple docstring"""

				super().__init__()
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       config
				# stochastic depth decay rule
				SCREAMING_SNAKE_CASE   :      Tuple				     =       [x.item() for x in torch.linspace(0      ,      config.drop_path_rate      ,      sum(config.depths	)	)]

				# patch embeddings
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       []
				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]      ,      )	)
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       nn.ModuleList(a	)

				# Transformer blocks
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       []
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       0
				for i in range(config.num_encoder_blocks	):
					# each block consists of layers
					SCREAMING_SNAKE_CASE   :      Dict				     =       []
					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	)	)

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       nn.ModuleList(a	)






			def 							__UpperCamelCase (			self							: List[Any]      ,      a							: Union[str, Any]      ,      a							: Dict=False      ,      a							: str=True	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Any				     =       () if output_hidden_states else None

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

					if output_hidden_states:
						SCREAMING_SNAKE_CASE   :      List[Any]				     =       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 _UpperCamelCase							(							__A   ):
			'''simple docstring'''







			lowerCamelCase__           =PoolFormerConfig
			lowerCamelCase__           ='poolformer'
			lowerCamelCase__           ='pixel_values'
			lowerCamelCase__           =True
			def 							__UpperCamelCase (			self							: Tuple      ,      a							: List[Any]	)							->       Optional[int]:

				"""simple docstring"""

				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							: str      ,      a							: List[Any]=False	)							->       List[str]:

				"""simple docstring"""

				if isinstance(a      ,      a	):
					SCREAMING_SNAKE_CASE   :      Dict				     =       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.'							,	__A							,	)
class _UpperCamelCase							(							__A   ):
			'''simple docstring'''







			def __init__(			self							: List[Any]      ,      a							: int	)							->       Optional[int]:

				"""simple docstring"""

				super().__init__(a	)
				SCREAMING_SNAKE_CASE   :      Dict				     =       config

				SCREAMING_SNAKE_CASE   :      Any				     =       PoolFormerEncoder(a	)

				# Initialize weights and apply final processing
				self.post_init()
			def 							__UpperCamelCase (			self							: Optional[Any]	)							->       Optional[Any]:

				"""simple docstring"""

				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							: Dict      ,      a							: Optional[torch.FloatTensor] = None      ,      a							: Optional[bool] = None      ,      a							: Optional[bool] = None      ,      )							->       Union[Tuple, BaseModelOutputWithNoAttention]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       (
				    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
				)
				SCREAMING_SNAKE_CASE   :      Optional[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"	)

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.encoder(
				    a      ,      output_hidden_states=a      ,      return_dict=a      ,      )
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       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 _UpperCamelCase							(							nn.Module   ):
			'''simple docstring'''







			def __init__(			self							: List[str]      ,      a							: List[Any]	)							->       List[str]:

				"""simple docstring"""

				super().__init__()
				SCREAMING_SNAKE_CASE   :      Any				     =       nn.Linear(config.hidden_size      ,      config.hidden_size	)






			def 							__UpperCamelCase (			self							: Union[str, Any]      ,      a							: Union[str, Any]	)							->       str:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       self.dense(a	)
				return output






@add_start_docstrings(
    '\n    PoolFormer Model transformer with an image classification head on top\n    '							,	__A							,	)
class _UpperCamelCase							(							__A   ):
			'''simple docstring'''







			def __init__(			self							: Optional[Any]      ,      a							: Any	)							->       Any:

				"""simple docstring"""

				super().__init__(a	)
				SCREAMING_SNAKE_CASE   :      int				     =       config.num_labels
				SCREAMING_SNAKE_CASE   :      str				     =       PoolFormerModel(a	)

				# Final norm
				SCREAMING_SNAKE_CASE   :      Tuple				     =       PoolFormerGroupNorm(config.hidden_sizes[-1]	)
				# Classifier head
				SCREAMING_SNAKE_CASE   :      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							: int      ,      a							: Optional[torch.FloatTensor] = None      ,      a							: Optional[torch.LongTensor] = None      ,      a							: Optional[bool] = None      ,      a							: Optional[bool] = None      ,      )							->       Union[Tuple, ImageClassifierOutputWithNoAttention]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       return_dict if return_dict is not None else self.config.use_return_dict

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       self.poolformer(
				    a      ,      output_hidden_states=a      ,      return_dict=a      ,      )

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       outputs[0]

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.classifier(self.norm(a	).mean([-2, -1]	)	)

				SCREAMING_SNAKE_CASE   :      int				     =       None
				if labels is not None:
					if self.config.problem_type is None:
						if self.num_labels == 1:
							SCREAMING_SNAKE_CASE   :      Dict				     =       "regression"
						elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
							SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       "single_label_classification"
						else:
							SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       "multi_label_classification"

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

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

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


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							: int      ,      a							: Optional[Any]      ,      a							: int = 13      ,      a							: int = 64      ,      a							: int = 2      ,      a							: int = 3      ,      a							: int = 3      ,      a							: bool = True      ,      a							: bool = True      ,      a							: int = 128      ,      a							: Any=[16, 32, 64, 128]      ,      a							: int = 7      ,      a							: int = 4      ,      a							: int = 37      ,      a							: str = "gelu"      ,      a							: float = 0.1      ,      a							: float = 0.1      ,      a							: int = 10      ,      a							: float = 0.02      ,      a							: int = 2      ,      a							: int = 1      ,      a							: int = 128      ,      a							: List[int] = [2, 2, 2, 2]      ,      a							: int = 2      ,      a							: int = 2      ,      )							->       Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       parent
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       batch_size
				SCREAMING_SNAKE_CASE   :      str				     =       image_size
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       patch_size
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       num_channels
				SCREAMING_SNAKE_CASE   :      int				     =       is_training
				SCREAMING_SNAKE_CASE   :      str				     =       use_labels
				SCREAMING_SNAKE_CASE   :      Tuple				     =       hidden_size
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       num_hidden_layers
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       num_attention_heads
				SCREAMING_SNAKE_CASE   :      Tuple				     =       intermediate_size
				SCREAMING_SNAKE_CASE   :      List[str]				     =       hidden_act
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       hidden_dropout_prob
				SCREAMING_SNAKE_CASE   :      int				     =       attention_probs_dropout_prob
				SCREAMING_SNAKE_CASE   :      int				     =       type_sequence_label_size
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       initializer_range
				SCREAMING_SNAKE_CASE   :      str				     =       encoder_stride
				SCREAMING_SNAKE_CASE   :      Dict				     =       num_attention_outputs
				SCREAMING_SNAKE_CASE   :      Any				     =       embed_dim
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       embed_dim + 1
				SCREAMING_SNAKE_CASE   :      Dict				     =       resolution
				SCREAMING_SNAKE_CASE   :      str				     =       depths
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       hidden_sizes
				SCREAMING_SNAKE_CASE   :      str				     =       dim
				SCREAMING_SNAKE_CASE   :      List[str]				     =       mlp_expansion_ratio
			def 							__UpperCamelCase (			self							: Optional[Any]	)							->       str:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      str				     =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

				SCREAMING_SNAKE_CASE   :      int				     =       None
				if self.use_labels:
					SCREAMING_SNAKE_CASE   :      Optional[int]				     =       ids_tensor([self.batch_size]      ,      self.type_sequence_label_size	)

				SCREAMING_SNAKE_CASE   :      Any				     =       self.get_config()

				return config, pixel_values, labels
			def 							__UpperCamelCase (			self							: Union[str, Any]	)							->       Any:

				"""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 							__UpperCamelCase (			self							: Any      ,      a							: Union[str, Any]      ,      a							: Tuple      ,      a							: Union[str, Any]	)							->       List[str]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Tuple				     =       TFEfficientFormerModel(config=a	)
				SCREAMING_SNAKE_CASE   :      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							: Optional[Any]      ,      a							: str      ,      a							: Union[str, Any]      ,      a							: Dict	)							->       Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[str]				     =       self.type_sequence_label_size
				SCREAMING_SNAKE_CASE   :      Tuple				     =       TFEfficientFormerForImageClassification(a	)
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       model(a      ,      labels=a      ,      training=a	)
				self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.type_sequence_label_size)	)

				# test greyscale images
				SCREAMING_SNAKE_CASE   :      Dict				     =       1
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       TFEfficientFormerForImageClassification(a	)

				SCREAMING_SNAKE_CASE   :      str				     =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
				SCREAMING_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							: List[str]	)							->       Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       self.prepare_config_and_inputs()
				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       config_and_inputs
				SCREAMING_SNAKE_CASE   :      Tuple				     =       {"pixel_values": pixel_values}
				return config, inputs_dict






@require_tf
class _UpperCamelCase							(							__A							,	__A							,	unittest.TestCase   ):
			'''simple docstring'''







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

			lowerCamelCase__           =False

			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			def 							__UpperCamelCase (			self							: List[Any]	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       TFEfficientFormerModelTester(self	)
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       ConfigTester(
				    self      ,      config_class=a      ,      has_text_modality=a      ,      hidden_size=37	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Optional[Any]:

				"""simple docstring"""

				self.config_tester.run_common_tests()
			@unittest.skip(reason="EfficientFormer does not use inputs_embeds"	)
			def 							__UpperCamelCase (			self							: List[str]	)							->       str:

				"""simple docstring"""

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

				"""simple docstring"""

				pass
			def 							__UpperCamelCase (			self							: Tuple	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Optional[int]				     =       self.model_tester.prepare_config_and_inputs_for_common()

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

					SCREAMING_SNAKE_CASE   :      Tuple				     =       ["pixel_values"]
					self.assertListEqual(arg_names[:1]      ,      a	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Optional[Any]:

				"""simple docstring"""

				def check_hidden_states_output(a							: Optional[int]      ,      a							: Any      ,      a							: List[Any]	):
					SCREAMING_SNAKE_CASE   :      Dict				     =       model_class(a	)

					SCREAMING_SNAKE_CASE   :      str				     =       model(**self._prepare_for_class(a      ,      a	)      ,      training=a	)
					SCREAMING_SNAKE_CASE   :      Any				     =       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

						self.asseretIsInstance(a      ,      (list, tuple)	)
						self.assertEqual(len(a	)      ,      a	)
						SCREAMING_SNAKE_CASE   :      Tuple				     =       getattr(self.model_tester      ,      "seq_length"      ,      a	)
						SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       getattr(self.model_tester      ,      "decoder_seq_length"      ,      a	)

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

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Any				     =       self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE   :      int				     =       True
					check_hidden_states_output(a      ,      a      ,      a	)

					# check that output_hidden_states also work using config
					del inputs_dict["output_hidden_states"]
					SCREAMING_SNAKE_CASE   :      str				     =       True

					check_hidden_states_output(a      ,      a      ,      a	)
			def 							__UpperCamelCase (			self							: Dict      ,      a							: Optional[int]      ,      a							: List[str]      ,      a							: Optional[Any]=False	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       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							: Tuple	)							->       List[str]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Tuple				     =       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	)							->       Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_image_modeling(*a	)
			def 							__UpperCamelCase (			self							: Dict	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*a	)
			@slow
			def 							__UpperCamelCase (			self							: int	)							->       Any:

				"""simple docstring"""

				for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					SCREAMING_SNAKE_CASE   :      List[Any]				     =       TFEfficientFormerModel.from_pretrained(a	)
					self.assertIsNotNone(a	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       List[str]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Tuple				     =       self.model_tester.prepare_config_and_inputs_for_common()
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       True

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

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

				for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE   :      Any				     =       True
					SCREAMING_SNAKE_CASE   :      Optional[int]				     =       False
					SCREAMING_SNAKE_CASE   :      Tuple				     =       True
					SCREAMING_SNAKE_CASE   :      Any				     =       model_class(a	)

					SCREAMING_SNAKE_CASE   :      Tuple				     =       model(**self._prepare_for_class(a      ,      a	)      ,      training=a	)
					SCREAMING_SNAKE_CASE   :      Dict				     =       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"]
					SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       True
					SCREAMING_SNAKE_CASE   :      Dict				     =       model_class(a	)
					SCREAMING_SNAKE_CASE   :      Any				     =       model(**self._prepare_for_class(a      ,      a	)      ,      training=a	)

					SCREAMING_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							: Optional[int]	)							->       List[str]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Tuple				     =       self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					# Prepare our model
					SCREAMING_SNAKE_CASE   :      str				     =       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
					SCREAMING_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
					}
					SCREAMING_SNAKE_CASE   :      Optional[int]				     =       model(a	)
					self.assertTrue(outputs_dict is not None	)

def  lowerCamelCase__ (					):
	SCREAMING_SNAKE_CASE   :      Tuple				     =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
	return image






@require_tf
@require_vision
class _UpperCamelCase							(							unittest.TestCase   ):
			'''simple docstring'''







			@cached_property
			def 							__UpperCamelCase (			self							: Dict	)							->       Dict:

				"""simple docstring"""

				return (
				    EfficientFormerImageProcessor.from_pretrained("snap-research/efficientformer-l1-300"	)
				    if is_vision_available()
				    else None
				)
			@slow
			def 							__UpperCamelCase (			self							: Tuple	)							->       List[str]:

				"""simple docstring"""

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






			@slow
			def 							__UpperCamelCase (			self							: Union[str, Any]	)							->       str:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Any				     =       TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
				    "snap-research/efficientformer-l1-300"	)
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.default_image_processor
				SCREAMING_SNAKE_CASE   :      Dict				     =       prepare_img()
				SCREAMING_SNAKE_CASE   :      str				     =       image_processor(images=a      ,      return_tensors="tf"	)
				# forward pass
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       model(**a      ,      training=a	)
				# verify the logits
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       tf.TensorShape((1, 1000)	)
				self.assertEqual(outputs.logits.shape      ,      a	)
				SCREAMING_SNAKE_CASE   :      Tuple				     =       tf.constant([-0.1312, 0.4353, -1.0499]	)
				self.assertTrue(np.allclose(outputs.logits[0, :3]      ,      a      ,      atol=1e-4	)	)