






"""simple docstring"""
import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import DetrConfig, DetrForObjectDetection, DetrForSegmentation, DetrImageProcessor, ResNetConfig
from transformers.utils import logging


logging.set_verbosity_info()
lowerCAmelCase__    						=			logging.get_logger(__name__)





def 				lowercase__		(					lowerCamelCase       ):
				# initialize config
				if "resnet-50" in model_name:
								_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    ResNetConfig.from_pretrained('microsoft/resnet-50'       )
				elif "resnet-101" in model_name:
								_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    ResNetConfig.from_pretrained('microsoft/resnet-101'       )
				else:
								raise ValueError('Model name should include either resnet50 or resnet101'       )

				_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    DetrConfig(use_timm_backbone=lowerCamelCase,						backbone_config=lowerCamelCase       )

				# set label attributes
				_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    'panoptic' in model_name
				if is_panoptic:
								_SCREAMING_SNAKE_CASE   :		List[str]       		=    250
				else:
								_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    91
								_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    'huggingface/label-files'
								_SCREAMING_SNAKE_CASE   :		Dict       		=    'coco-detection-id2label.json'
								_SCREAMING_SNAKE_CASE   :		List[str]       		=    json.load(open(hf_hub_download(lowerCamelCase,						lowerCamelCase,						repo_type='dataset'       ),						'r'       )       )
								_SCREAMING_SNAKE_CASE   :		Dict       		=    {int(lowerCamelCase       ): v for k, v in idalabel.items()}
								_SCREAMING_SNAKE_CASE   :		List[Any]       		=    idalabel
								_SCREAMING_SNAKE_CASE   :		Tuple       		=    {v: k for k, v in idalabel.items()}

				return config, is_panoptic





def 				lowercase__		(					lowerCamelCase       ):
				# here we list all keys to be renamed (original name on the left, our name on the right)
				_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    []

				# stem
				# fmt: off
				rename_keys.append(('backbone.0.body.conv1.weight', 'backbone.conv_encoder.model.embedder.embedder.convolution.weight')       )
				rename_keys.append(('backbone.0.body.bn1.weight', 'backbone.conv_encoder.model.embedder.embedder.normalization.weight')       )
				rename_keys.append(('backbone.0.body.bn1.bias', 'backbone.conv_encoder.model.embedder.embedder.normalization.bias')       )
				rename_keys.append(('backbone.0.body.bn1.running_mean', 'backbone.conv_encoder.model.embedder.embedder.normalization.running_mean')       )
				rename_keys.append(('backbone.0.body.bn1.running_var', 'backbone.conv_encoder.model.embedder.embedder.normalization.running_var')       )
				# stages
				for stage_idx in range(len(config.backbone_config.depths       )       ):
								for layer_idx in range(config.backbone_config.depths[stage_idx]       ):
												# shortcut
												if layer_idx == 0:
																rename_keys.append(
																    (
																        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.0.weight""",
																        f"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.convolution.weight""",
																    )       )
																rename_keys.append(
																    (
																        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.weight""",
																        f"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.weight""",
																    )       )
																rename_keys.append(
																    (
																        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.bias""",
																        f"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.bias""",
																    )       )
																rename_keys.append(
																    (
																        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.running_mean""",
																        f"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.running_mean""",
																    )       )
																rename_keys.append(
																    (
																        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.running_var""",
																        f"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.running_var""",
																    )       )
												# 3 convs
												for i in range(3       ):
																rename_keys.append(
																    (
																        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.conv{i+1}.weight""",
																        f"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.convolution.weight""",
																    )       )
																rename_keys.append(
																    (
																        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.weight""",
																        f"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.weight""",
																    )       )
																rename_keys.append(
																    (
																        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.bias""",
																        f"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.bias""",
																    )       )
																rename_keys.append(
																    (
																        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.running_mean""",
																        f"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.running_mean""",
																    )       )
																rename_keys.append(
																    (
																        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.running_var""",
																        f"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.running_var""",
																    )       )
    # fmt: on

				for i in range(config.encoder_layers       ):
								# encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
								rename_keys.append(
								    (
								        f"""transformer.encoder.layers.{i}.self_attn.out_proj.weight""",
								        f"""encoder.layers.{i}.self_attn.out_proj.weight""",
								    )       )
								rename_keys.append(
								    (f"""transformer.encoder.layers.{i}.self_attn.out_proj.bias""", f"""encoder.layers.{i}.self_attn.out_proj.bias""")       )
								rename_keys.append((f"""transformer.encoder.layers.{i}.linear1.weight""", f"""encoder.layers.{i}.fc1.weight""")       )
								rename_keys.append((f"""transformer.encoder.layers.{i}.linear1.bias""", f"""encoder.layers.{i}.fc1.bias""")       )
								rename_keys.append((f"""transformer.encoder.layers.{i}.linear2.weight""", f"""encoder.layers.{i}.fc2.weight""")       )
								rename_keys.append((f"""transformer.encoder.layers.{i}.linear2.bias""", f"""encoder.layers.{i}.fc2.bias""")       )
								rename_keys.append(
								    (f"""transformer.encoder.layers.{i}.norm1.weight""", f"""encoder.layers.{i}.self_attn_layer_norm.weight""")       )
								rename_keys.append(
								    (f"""transformer.encoder.layers.{i}.norm1.bias""", f"""encoder.layers.{i}.self_attn_layer_norm.bias""")       )
								rename_keys.append(
								    (f"""transformer.encoder.layers.{i}.norm2.weight""", f"""encoder.layers.{i}.final_layer_norm.weight""")       )
								rename_keys.append((f"""transformer.encoder.layers.{i}.norm2.bias""", f"""encoder.layers.{i}.final_layer_norm.bias""")       )
								# decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms
								rename_keys.append(
								    (
								        f"""transformer.decoder.layers.{i}.self_attn.out_proj.weight""",
								        f"""decoder.layers.{i}.self_attn.out_proj.weight""",
								    )       )
								rename_keys.append(
								    (f"""transformer.decoder.layers.{i}.self_attn.out_proj.bias""", f"""decoder.layers.{i}.self_attn.out_proj.bias""")       )
								rename_keys.append(
								    (
								        f"""transformer.decoder.layers.{i}.multihead_attn.out_proj.weight""",
								        f"""decoder.layers.{i}.encoder_attn.out_proj.weight""",
								    )       )
								rename_keys.append(
								    (
								        f"""transformer.decoder.layers.{i}.multihead_attn.out_proj.bias""",
								        f"""decoder.layers.{i}.encoder_attn.out_proj.bias""",
								    )       )
								rename_keys.append((f"""transformer.decoder.layers.{i}.linear1.weight""", f"""decoder.layers.{i}.fc1.weight""")       )
								rename_keys.append((f"""transformer.decoder.layers.{i}.linear1.bias""", f"""decoder.layers.{i}.fc1.bias""")       )
								rename_keys.append((f"""transformer.decoder.layers.{i}.linear2.weight""", f"""decoder.layers.{i}.fc2.weight""")       )
								rename_keys.append((f"""transformer.decoder.layers.{i}.linear2.bias""", f"""decoder.layers.{i}.fc2.bias""")       )
								rename_keys.append(
								    (f"""transformer.decoder.layers.{i}.norm1.weight""", f"""decoder.layers.{i}.self_attn_layer_norm.weight""")       )
								rename_keys.append(
								    (f"""transformer.decoder.layers.{i}.norm1.bias""", f"""decoder.layers.{i}.self_attn_layer_norm.bias""")       )
								rename_keys.append(
								    (f"""transformer.decoder.layers.{i}.norm2.weight""", f"""decoder.layers.{i}.encoder_attn_layer_norm.weight""")       )
								rename_keys.append(
								    (f"""transformer.decoder.layers.{i}.norm2.bias""", f"""decoder.layers.{i}.encoder_attn_layer_norm.bias""")       )
								rename_keys.append(
								    (f"""transformer.decoder.layers.{i}.norm3.weight""", f"""decoder.layers.{i}.final_layer_norm.weight""")       )
								rename_keys.append((f"""transformer.decoder.layers.{i}.norm3.bias""", f"""decoder.layers.{i}.final_layer_norm.bias""")       )

				# convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads
				rename_keys.extend(
				    [
				        ('input_proj.weight', 'input_projection.weight'),
				        ('input_proj.bias', 'input_projection.bias'),
				        ('query_embed.weight', 'query_position_embeddings.weight'),
				        ('transformer.decoder.norm.weight', 'decoder.layernorm.weight'),
				        ('transformer.decoder.norm.bias', 'decoder.layernorm.bias'),
				        ('class_embed.weight', 'class_labels_classifier.weight'),
				        ('class_embed.bias', 'class_labels_classifier.bias'),
				        ('bbox_embed.layers.0.weight', 'bbox_predictor.layers.0.weight'),
				        ('bbox_embed.layers.0.bias', 'bbox_predictor.layers.0.bias'),
				        ('bbox_embed.layers.1.weight', 'bbox_predictor.layers.1.weight'),
				        ('bbox_embed.layers.1.bias', 'bbox_predictor.layers.1.bias'),
				        ('bbox_embed.layers.2.weight', 'bbox_predictor.layers.2.weight'),
				        ('bbox_embed.layers.2.bias', 'bbox_predictor.layers.2.bias'),
				    ]       )

				return rename_keys





def 				lowercase__		(					lowerCamelCase,						lowerCamelCase,						lowerCamelCase       ):
				_SCREAMING_SNAKE_CASE   :		Dict       		=    state_dict.pop(lowerCamelCase       )
				_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    val





def 				lowercase__		(					lowerCamelCase,						lowerCamelCase=False       ):
				_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    ''
				if is_panoptic:
								_SCREAMING_SNAKE_CASE   :		List[Any]       		=    'detr.'

				# first: transformer encoder
				for i in range(6       ):
								# read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias)
								_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    state_dict.pop(f"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight"""       )
								_SCREAMING_SNAKE_CASE   :		Dict       		=    state_dict.pop(f"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias"""       )
								# next, add query, keys and values (in that order) to the state dict
								_SCREAMING_SNAKE_CASE   :		Any       		=    in_proj_weight[:256, :]
								_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    in_proj_bias[:256]
								_SCREAMING_SNAKE_CASE   :		Tuple       		=    in_proj_weight[256:512, :]
								_SCREAMING_SNAKE_CASE   :		Tuple       		=    in_proj_bias[256:512]
								_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    in_proj_weight[-256:, :]
								_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    in_proj_bias[-256:]
				# next: transformer decoder (which is a bit more complex because it also includes cross-attention)
				for i in range(6       ):
								# read in weights + bias of input projection layer of self-attention
								_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    state_dict.pop(f"""{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_weight"""       )
								_SCREAMING_SNAKE_CASE   :		int       		=    state_dict.pop(f"""{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_bias"""       )
								# next, add query, keys and values (in that order) to the state dict
								_SCREAMING_SNAKE_CASE   :		Tuple       		=    in_proj_weight[:256, :]
								_SCREAMING_SNAKE_CASE   :		str       		=    in_proj_bias[:256]
								_SCREAMING_SNAKE_CASE   :		str       		=    in_proj_weight[256:512, :]
								_SCREAMING_SNAKE_CASE   :		int       		=    in_proj_bias[256:512]
								_SCREAMING_SNAKE_CASE   :		List[Any]       		=    in_proj_weight[-256:, :]
								_SCREAMING_SNAKE_CASE   :		List[str]       		=    in_proj_bias[-256:]
								# read in weights + bias of input projection layer of cross-attention
								_SCREAMING_SNAKE_CASE   :		str       		=    state_dict.pop(
								    f"""{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_weight"""       )
								_SCREAMING_SNAKE_CASE   :		List[Any]       		=    state_dict.pop(f"""{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_bias"""       )
								# next, add query, keys and values (in that order) of cross-attention to the state dict
								_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    in_proj_weight_cross_attn[:256, :]
								_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    in_proj_bias_cross_attn[:256]
								_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    in_proj_weight_cross_attn[256:512, :]
								_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    in_proj_bias_cross_attn[256:512]
								_SCREAMING_SNAKE_CASE   :		int       		=    in_proj_weight_cross_attn[-256:, :]
								_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    in_proj_bias_cross_attn[-256:]





def 				lowercase__		(					):
				_SCREAMING_SNAKE_CASE   :		List[Any]       		=    'http://images.cocodataset.org/val2017/000000039769.jpg'
				_SCREAMING_SNAKE_CASE   :		Dict       		=    Image.open(requests.get(lowerCamelCase,						stream=lowerCamelCase       ).raw       )

				return im





@torch.no_grad()
def 				lowercase__		(					lowerCamelCase,						lowerCamelCase=None,						lowerCamelCase=False       ):
				_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		str       		=    get_detr_config(lowerCamelCase       )

				# load original model from torch hub
				_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    {
				    'detr-resnet-50': 'detr_resnet50',
				    'detr-resnet-101': 'detr_resnet101',
				}
				logger.info(f"""Converting model {model_name}..."""       )
				_SCREAMING_SNAKE_CASE   :		int       		=    torch.hub.load('facebookresearch/detr',						model_name_to_original_name[model_name],						pretrained=lowerCamelCase       ).eval()
				_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    detr.state_dict()
				# rename keys
				for src, dest in create_rename_keys(lowerCamelCase       ):
								if is_panoptic:
												_SCREAMING_SNAKE_CASE   :		int       		=    'detr.' + src
								rename_key(lowerCamelCase,						lowerCamelCase,						lowerCamelCase       )
				# query, key and value matrices need special treatment
				read_in_q_k_v(lowerCamelCase,						is_panoptic=lowerCamelCase       )
				# important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them
				_SCREAMING_SNAKE_CASE   :		str       		=    'detr.model.' if is_panoptic else 'model.'
				for key in state_dict.copy().keys():
								if is_panoptic:
												if (
												    key.startswith('detr'       )
												    and not key.startswith('class_labels_classifier'       )
												    and not key.startswith('bbox_predictor'       )
												):
																_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    state_dict.pop(lowerCamelCase       )
																_SCREAMING_SNAKE_CASE   :		int       		=    val
												elif "class_labels_classifier" in key or "bbox_predictor" in key:
																_SCREAMING_SNAKE_CASE   :		Tuple       		=    state_dict.pop(lowerCamelCase       )
																_SCREAMING_SNAKE_CASE   :		Dict       		=    val
												elif key.startswith('bbox_attention'       ) or key.startswith('mask_head'       ):
																continue
												else:
																_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    state_dict.pop(lowerCamelCase       )
																_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    val
								else:
												if not key.startswith('class_labels_classifier'       ) and not key.startswith('bbox_predictor'       ):
																_SCREAMING_SNAKE_CASE   :		List[str]       		=    state_dict.pop(lowerCamelCase       )
																_SCREAMING_SNAKE_CASE   :		int       		=    val

    # finally, create HuggingFace model and load state dict
				_SCREAMING_SNAKE_CASE   :		Dict       		=    DetrForSegmentation(lowerCamelCase       ) if is_panoptic else DetrForObjectDetection(lowerCamelCase       )
				model.load_state_dict(lowerCamelCase       )
				model.eval()

				# verify our conversion on an image
				_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    'coco_panoptic' if is_panoptic else 'coco_detection'
				_SCREAMING_SNAKE_CASE   :		int       		=    DetrImageProcessor(format=lowerCamelCase       )

				_SCREAMING_SNAKE_CASE   :		Any       		=    processor(images=prepare_img(),						return_tensors='pt'       )
				_SCREAMING_SNAKE_CASE   :		Tuple       		=    encoding['pixel_values']

				_SCREAMING_SNAKE_CASE   :		List[str]       		=    detr(lowerCamelCase       )
				_SCREAMING_SNAKE_CASE   :		int       		=    model(lowerCamelCase       )

				assert torch.allclose(outputs.logits,						original_outputs['pred_logits'],						atol=1E-3       )
				assert torch.allclose(outputs.pred_boxes,						original_outputs['pred_boxes'],						atol=1E-3       )
				if is_panoptic:
								assert torch.allclose(outputs.pred_masks,						original_outputs['pred_masks'],						atol=1E-4       )
				print('Looks ok!'       )

				if pytorch_dump_folder_path is not None:
								# Save model and image processor
								logger.info(f"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}..."""       )
								Path(lowerCamelCase       ).mkdir(exist_ok=lowerCamelCase       )
								model.save_pretrained(lowerCamelCase       )
								processor.save_pretrained(lowerCamelCase       )

				if push_to_hub:
								# Upload model and image processor to the hub
								logger.info('Uploading PyTorch model and image processor to the hub...'       )
								model.push_to_hub(f"""nielsr/{model_name}"""       )
								processor.push_to_hub(f"""nielsr/{model_name}"""       )


if __name__ == "__main__":
			lowerCAmelCase__    						=			argparse.ArgumentParser()

			parser.add_argument(
			    '--model_name',
			    default='detr-resnet-50',
			    type=str,
			    choices=['detr-resnet-50', 'detr-resnet-101'],
			    help='Name of the DETR model you\'d like to convert.',
			)
			parser.add_argument(
			    '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.'
			)
			parser.add_argument('--push_to_hub', action='store_true', help='Whether to push the model to the hub or not.')
			lowerCAmelCase__    						=			parser.parse_args()
			convert_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)









"""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 _lowerCAmelCase						:







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







						def 			A      (    self							)		->							List[str]:
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

										_SCREAMING_SNAKE_CASE   :		Tuple       		=    self.get_config()

										return config, pixel_values, labels







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







						def 			A      (    self   ,				lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_							)		->							Tuple:
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    self.type_sequence_label_size
										_SCREAMING_SNAKE_CASE   :		Any       		=    TFEfficientFormerForImageClassification(lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    model(lowerCAmelCase_   ,				labels=lowerCAmelCase_   ,				training=lowerCAmelCase_							)
										self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.type_sequence_label_size)							)

										# test greyscale images
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    1
										_SCREAMING_SNAKE_CASE   :		Any       		=    TFEfficientFormerForImageClassification(lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    model(lowerCAmelCase_   ,				labels=lowerCAmelCase_							)
										self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.type_sequence_label_size)							)







						def 			A      (    self							)		->							Dict:
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    self.prepare_config_and_inputs()
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		Dict       		=    config_and_inputs
										_SCREAMING_SNAKE_CASE   :		Dict       		=    {'pixel_values': pixel_values}
										return config, inputs_dict







@require_tf
class _lowerCAmelCase						(    __UpperCAmelCase   ,  __UpperCAmelCase   ,  unittest.TestCase       ):
						SCREAMING_SNAKE_CASE_:					List[str] 					=				(
						    (
						        TFEfficientFormerModel,
						        TFEfficientFormerForImageClassificationWithTeacher,
						        TFEfficientFormerForImageClassification,
						    )
						    if is_tf_available()
						    else ()
						)
						SCREAMING_SNAKE_CASE_:					List[str] 					=				(
						    {
						        'feature-extraction': TFEfficientFormerModel,
						        'image-classification': (
						            TFEfficientFormerForImageClassification,
						            TFEfficientFormerForImageClassificationWithTeacher,
						        ),
						    }
						    if is_tf_available()
						    else {}
						)

						SCREAMING_SNAKE_CASE_:					List[Any] 					=				False

						SCREAMING_SNAKE_CASE_:					Optional[Any] 					=				False
						SCREAMING_SNAKE_CASE_:					List[str] 					=				False
						SCREAMING_SNAKE_CASE_:					Optional[Any] 					=				False
						SCREAMING_SNAKE_CASE_:					str 					=				False







						def 			A      (    self							)		->							List[str]:
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    TFEfficientFormerModelTester(self							)
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    ConfigTester(
										    self   ,				config_class=lowerCAmelCase_   ,				has_text_modality=lowerCAmelCase_   ,				hidden_size=3_7							)







						def 			A      (    self							)		->							Tuple:
										self.config_tester.run_common_tests()







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







						@unittest.skip(reason='EfficientFormer does not support input and output embeddings'							)
						def 			A      (    self							)		->							Optional[Any]:
										pass







						def 			A      (    self							)		->							Any:
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		List[str]       		=    self.model_tester.prepare_config_and_inputs_for_common()

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

														_SCREAMING_SNAKE_CASE   :		List[Any]       		=    ['pixel_values']
														self.assertListEqual(arg_names[:1]   ,				lowerCAmelCase_							)







						def 			A      (    self							)		->							Union[str, Any]:
										def check_hidden_states_output(lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_							):
														_SCREAMING_SNAKE_CASE   :		List[str]       		=    model_class(lowerCAmelCase_							)

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

														_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    getattr(
														    self.model_tester   ,				'expected_num_hidden_layers'   ,				self.model_tester.num_hidden_layers + 1							)
														self.assertEqual(len(lowerCAmelCase_							)   ,				lowerCAmelCase_							)

														if hasattr(self.model_tester   ,				'encoder_seq_length'							):
																		_SCREAMING_SNAKE_CASE   :		List[str]       		=    self.model_tester.encoder_seq_length
																		if hasattr(self.model_tester   ,				'chunk_length'							) and self.model_tester.chunk_length > 1:
																						_SCREAMING_SNAKE_CASE   :		Tuple       		=    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   :		List[str]       		=    outputs.decoder_hidden_states

																		self.asseretIsInstance(lowerCAmelCase_   ,				(list, tuple)							)
																		self.assertEqual(len(lowerCAmelCase_							)   ,				lowerCAmelCase_							)
																		_SCREAMING_SNAKE_CASE   :		Dict       		=    getattr(self.model_tester   ,				'seq_length'   ,				lowerCAmelCase_							)
																		_SCREAMING_SNAKE_CASE   :		Tuple       		=    getattr(self.model_tester   ,				'decoder_seq_length'   ,				lowerCAmelCase_							)

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

										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		Dict       		=    self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE   :		str       		=    True
														check_hidden_states_output(lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_							)

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

														check_hidden_states_output(lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_							)







						def 			A      (    self   ,				lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_=False							)		->							Any:
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    super()._prepare_for_class(lowerCAmelCase_   ,				lowerCAmelCase_   ,				return_labels=lowerCAmelCase_							)

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

										return inputs_dict







						def 			A      (    self							)		->							str:
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase_							)







						@unittest.skip(reason='EfficientFormer does not implement masked image modeling yet'							)
						def 			A      (    self							)		->							Tuple:
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase_							)







						def 			A      (    self							)		->							str:
										_SCREAMING_SNAKE_CASE   :		str       		=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_							)







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







						def 			A      (    self							)		->							List[Any]:
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		List[Any]       		=    self.model_tester.prepare_config_and_inputs_for_common()
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    True

										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    getattr(self.model_tester   ,				'seq_length'   ,				lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		int       		=    getattr(self.model_tester   ,				'encoder_seq_length'   ,				lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    getattr(self.model_tester   ,				'key_length'   ,				lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		int       		=    getattr(self.model_tester   ,				'chunk_length'   ,				lowerCAmelCase_							)

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

										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE   :		str       		=    True
														_SCREAMING_SNAKE_CASE   :		Tuple       		=    False
														_SCREAMING_SNAKE_CASE   :		Dict       		=    True
														_SCREAMING_SNAKE_CASE   :		List[Any]       		=    model_class(lowerCAmelCase_							)

														_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    model(**self._prepare_for_class(lowerCAmelCase_   ,				lowerCAmelCase_							)   ,				training=lowerCAmelCase_							)
														_SCREAMING_SNAKE_CASE   :		Any       		=    outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
														self.assertEqual(len(lowerCAmelCase_							)   ,				self.model_tester.num_attention_outputs							)

														# check that output_attentions also work using config
														del inputs_dict["output_attentions"]
														_SCREAMING_SNAKE_CASE   :		List[str]       		=    True
														_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    model_class(lowerCAmelCase_							)
														_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    model(**self._prepare_for_class(lowerCAmelCase_   ,				lowerCAmelCase_							)   ,				training=lowerCAmelCase_							)

														_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
														self.assertEqual(len(lowerCAmelCase_							)   ,				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 			A      (    self							)		->							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
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		Dict       		=    self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														# Prepare our model
														_SCREAMING_SNAKE_CASE   :		int       		=    model_class(lowerCAmelCase_							)
														# 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   :		Optional[Any]       		=    {
														    key: tf.keras.Input(shape=val.shape[1:]   ,				dtype=val.dtype   ,				name=lowerCAmelCase_							)
														    for key, val in model.input_signature.items()
														    if key in model.dummy_inputs
														}
														_SCREAMING_SNAKE_CASE   :		List[Any]       		=    model(lowerCAmelCase_							)
														self.assertTrue(outputs_dict is not None							)







def 				lowercase__		(					):
				_SCREAMING_SNAKE_CASE   :		List[Any]       		=    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
				return image






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







						@cached_property
						def 			A      (    self							)		->							str:
										return (
										    EfficientFormerImageProcessor.from_pretrained('snap-research/efficientformer-l1-300'							)
										    if is_vision_available()
										    else None
										)







						@slow
						def 			A      (    self							)		->							Dict:
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    TFEfficientFormerForImageClassification.from_pretrained('snap-research/efficientformer-l1-300'							)
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    self.default_image_processor
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    prepare_img()
										_SCREAMING_SNAKE_CASE   :		int       		=    image_processor(images=lowerCAmelCase_   ,				return_tensors='tf'							)
										# forward pass
										_SCREAMING_SNAKE_CASE   :		Dict       		=    model(**lowerCAmelCase_   ,				training=lowerCAmelCase_							)
										# verify the logits
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    tf.TensorShape((1, 1_0_0_0)							)
										self.assertEqual(outputs.logits.shape   ,				lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    tf.constant([-0.0_555, 0.4_825, -0.0_852]							)
										self.assertTrue(np.allclose(outputs.logits[0, :3]   ,				lowerCAmelCase_   ,				atol=1e-4							)							)







						@slow
						def 			A      (    self							)		->							Optional[Any]:
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
										    'snap-research/efficientformer-l1-300'							)
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    self.default_image_processor
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    prepare_img()
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    image_processor(images=lowerCAmelCase_   ,				return_tensors='tf'							)
										# forward pass
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    model(**lowerCAmelCase_   ,				training=lowerCAmelCase_							)
										# verify the logits
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    tf.TensorShape((1, 1_0_0_0)							)
										self.assertEqual(outputs.logits.shape   ,				lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		Dict       		=    tf.constant([-0.1_312, 0.4_353, -1.0_499]							)
										self.assertTrue(np.allclose(outputs.logits[0, :3]   ,				lowerCAmelCase_   ,				atol=1e-4							)							)

