




'''simple docstring'''






import json
import os
import re
import unicodedata
from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


_lowerCAmelCase       =   logging.get_logger(__name__)

_lowerCAmelCase       =   {
    '''src_vocab_file''': '''vocab-src.json''',
    '''tgt_vocab_file''': '''vocab-tgt.json''',
    '''merges_file''': '''merges.txt''',
}

_lowerCAmelCase       =   {
    '''src_vocab_file''': {
        '''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/vocab-src.json'''
    },
    '''tgt_vocab_file''': {
        '''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/vocab-tgt.json'''
    },
    '''merges_file''': {'''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/merges.txt'''},
}

_lowerCAmelCase       =   {'''stas/tiny-wmt19-en-de''': 1024}
_lowerCAmelCase       =   {
    '''stas/tiny-wmt19-en-de''': {
        '''langs''': ['''en''', '''de'''],
        '''model_max_length''': 1024,
        '''special_tokens_map_file''': None,
        '''full_tokenizer_file''': None,
    }
}

def      __lowerCAmelCase   (       snake_case__		):
			__UpperCamelCase :     Tuple										=  set()
			__UpperCamelCase :     List[str]										=  word[0]
			for char in word[1:]:
						pairs.add((prev_char, char)		)
						__UpperCamelCase :     Tuple										=  char
			return pairs

def      __lowerCAmelCase   (       snake_case__		):
			__UpperCamelCase :     int										=  text.replace("，"   , ","		)
			__UpperCamelCase :     Any										=  re.sub(r"。\s*"   , ". "   , snake_case__		)
			__UpperCamelCase :     Optional[int]										=  text.replace("、"   , ","		)
			__UpperCamelCase :     List[str]										=  text.replace("”"   , "\""		)
			__UpperCamelCase :     Tuple										=  text.replace("“"   , "\""		)
			__UpperCamelCase :     Optional[int]										=  text.replace("∶"   , ":"		)
			__UpperCamelCase :     Dict										=  text.replace("："   , ":"		)
			__UpperCamelCase :     Union[str, Any]										=  text.replace("？"   , "?"		)
			__UpperCamelCase :     int										=  text.replace("《"   , "\""		)
			__UpperCamelCase :     Optional[int]										=  text.replace("》"   , "\""		)
			__UpperCamelCase :     Optional[int]										=  text.replace("）"   , ")"		)
			__UpperCamelCase :     Tuple										=  text.replace("！"   , "!"		)
			__UpperCamelCase :     str										=  text.replace("（"   , "("		)
			__UpperCamelCase :     Tuple										=  text.replace("；"   , ";"		)
			__UpperCamelCase :     Any										=  text.replace("１"   , "1"		)
			__UpperCamelCase :     Dict										=  text.replace("」"   , "\""		)
			__UpperCamelCase :     int										=  text.replace("「"   , "\""		)
			__UpperCamelCase :     Tuple										=  text.replace("０"   , "0"		)
			__UpperCamelCase :     Optional[int]										=  text.replace("３"   , "3"		)
			__UpperCamelCase :     List[str]										=  text.replace("２"   , "2"		)
			__UpperCamelCase :     List[Any]										=  text.replace("５"   , "5"		)
			__UpperCamelCase :     Dict										=  text.replace("６"   , "6"		)
			__UpperCamelCase :     Dict										=  text.replace("９"   , "9"		)
			__UpperCamelCase :     List[Any]										=  text.replace("７"   , "7"		)
			__UpperCamelCase :     Union[str, Any]										=  text.replace("８"   , "8"		)
			__UpperCamelCase :     Any										=  text.replace("４"   , "4"		)
			__UpperCamelCase :     str										=  re.sub(r"．\s*"   , ". "   , snake_case__		)
			__UpperCamelCase :     List[str]										=  text.replace("～"   , "~"		)
			__UpperCamelCase :     Optional[int]										=  text.replace("’"   , "'"		)
			__UpperCamelCase :     Optional[int]										=  text.replace("…"   , "..."		)
			__UpperCamelCase :     List[str]										=  text.replace("━"   , "-"		)
			__UpperCamelCase :     Union[str, Any]										=  text.replace("〈"   , "<"		)
			__UpperCamelCase :     Dict										=  text.replace("〉"   , ">"		)
			__UpperCamelCase :     List[Any]										=  text.replace("【"   , "["		)
			__UpperCamelCase :     str										=  text.replace("】"   , "]"		)
			__UpperCamelCase :     str										=  text.replace("％"   , "%"		)
			return text

def      __lowerCAmelCase   (       snake_case__		):
			__UpperCamelCase :     str										=  []
			for char in text:
						__UpperCamelCase :     Tuple										=  unicodedata.category(snake_case__		)
						if cat.startswith("C"		):
									continue
						output.append(snake_case__		)
			return "".join(snake_case__		)




class    A						(					SCREAMING_SNAKE_CASE__						):




				'''simple docstring'''





				A       =							VOCAB_FILES_NAMES
				A       =							PRETRAINED_VOCAB_FILES_MAP
				A       =							PRETRAINED_INIT_CONFIGURATION
				A       =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				A       =							["input_ids", "attention_mask"]
				def __init__(self					,    _UpperCAmelCase=None					,    _UpperCAmelCase=None					,    _UpperCAmelCase=None					,    _UpperCAmelCase=None					,    _UpperCAmelCase=False					,    _UpperCAmelCase="<unk>"					,    _UpperCAmelCase="<s>"					,    _UpperCAmelCase="</s>"					,    _UpperCAmelCase="<pad>"					,    **_UpperCAmelCase					,    )      ->			Optional[Any]:
							super().__init__(
							    langs=_UpperCAmelCase					,    src_vocab_file=_UpperCAmelCase					,    tgt_vocab_file=_UpperCAmelCase					,    merges_file=_UpperCAmelCase					,    do_lower_case=_UpperCAmelCase					,    unk_token=_UpperCAmelCase					,    bos_token=_UpperCAmelCase					,    sep_token=_UpperCAmelCase					,    pad_token=_UpperCAmelCase					,    **_UpperCAmelCase					,    )

							try:
										import sacremoses
							except ImportError:
										raise ImportError(
										    "You need to install sacremoses to use XLMTokenizer. "
										    "See https://pypi.org/project/sacremoses/ for installation."     )

							__UpperCamelCase :     int										=  sacremoses

							__UpperCamelCase :     str										=  src_vocab_file
							__UpperCamelCase :     List[Any]										=  tgt_vocab_file
							__UpperCamelCase :     Optional[Any]										=  merges_file
							__UpperCamelCase :     Union[str, Any]										=  do_lower_case

							# cache of sm.MosesPunctNormalizer instance
							__UpperCamelCase :     List[str]										=  {}
							# cache of sm.MosesTokenizer instance
							__UpperCamelCase :     int										=  {}
							__UpperCamelCase :     int										=  {}

							if langs and len(_UpperCAmelCase     ) == 2:
										__UpperCamelCase						,   __UpperCamelCase :     int										=  langs
							else:
										raise ValueError(
										    f"arg `langs` needs to be a list of 2 langs, e.g. ['en', 'ru'], but got {langs}. "
										    "Usually that means that tokenizer can't find a mapping for the given model path "
										    "in PRETRAINED_VOCAB_FILES_MAP, and other maps of this tokenizer."     )

							with open(_UpperCAmelCase					,    encoding="utf-8"     ) as src_vocab_handle:
										__UpperCamelCase :     List[Any]										=  json.load(_UpperCAmelCase     )
							with open(_UpperCAmelCase					,    encoding="utf-8"     ) as tgt_vocab_handle:
										__UpperCamelCase :     Tuple										=  json.load(_UpperCAmelCase     )
										__UpperCamelCase :     Dict										=  {v: k for k, v in tgt_vocab.items()}
							with open(_UpperCAmelCase					,    encoding="utf-8"     ) as merges_handle:
										__UpperCamelCase :     Dict										=  merges_handle.read().split("\n"     )[:-1]
							__UpperCamelCase :     int										=  [tuple(merge.split()[:2]     ) for merge in merges]
							__UpperCamelCase :     int										=  dict(zip(_UpperCAmelCase					,    range(len(_UpperCAmelCase     )     )     )     )
							__UpperCamelCase :     int										=  {}
				def 			a_			(self     )      ->			Dict[str, int]:
							return self.get_src_vocab()
				@property
				def 			a_			(self     )      ->			int:
							return self.src_vocab_size
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase     )      ->			str:
							if lang not in self.cache_moses_punct_normalizer:
										__UpperCamelCase :     Union[str, Any]										=  self.sm.MosesPunctNormalizer(lang=_UpperCAmelCase     )
										__UpperCamelCase :     Any										=  punct_normalizer
							return self.cache_moses_punct_normalizer[lang].normalize(_UpperCAmelCase     )
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase     )      ->			List[str]:
							if lang not in self.cache_moses_tokenizer:
										__UpperCamelCase :     Optional[int]										=  self.sm.MosesTokenizer(lang=_UpperCAmelCase     )
										__UpperCamelCase :     Tuple										=  moses_tokenizer
							return self.cache_moses_tokenizer[lang].tokenize(
							    _UpperCAmelCase					,    aggressive_dash_splits=_UpperCAmelCase					,    return_str=_UpperCAmelCase					,    escape=_UpperCAmelCase     )
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase     )      ->			Tuple:
							if lang not in self.cache_moses_detokenizer:
										__UpperCamelCase :     Optional[int]										=  self.sm.MosesDetokenizer(lang=_UpperCAmelCase     )
										__UpperCamelCase :     Optional[int]										=  moses_detokenizer
							return self.cache_moses_detokenizer[lang].detokenize(_UpperCAmelCase     )
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase     )      ->			Any:
							__UpperCamelCase :     Tuple										=  replace_unicode_punct(_UpperCAmelCase     )
							__UpperCamelCase :     int										=  self.moses_punct_norm(_UpperCAmelCase					,    _UpperCAmelCase     )
							__UpperCamelCase :     Optional[int]										=  remove_non_printing_char(_UpperCAmelCase     )
							return text
				@property
				def 			a_			(self     )      ->			Optional[int]:
							return len(self.encoder     )
				@property
				def 			a_			(self     )      ->			Dict:
							return len(self.decoder     )
				def 			a_			(self     )      ->			str:
							return dict(self.encoder					,    **self.added_tokens_encoder     )
				def 			a_			(self     )      ->			Optional[int]:
							return dict(self.decoder					,    **self.added_tokens_decoder     )
				def 			a_			(self					,    _UpperCAmelCase     )      ->			Tuple:
							__UpperCamelCase :     str										=  tuple(token[:-1]     ) + (token[-1] + "</w>",)
							if token in self.cache:
										return self.cache[token]
							__UpperCamelCase :     Union[str, Any]										=  get_pairs(_UpperCAmelCase     )

							if not pairs:
										return token + "</w>"

							while True:
										__UpperCamelCase :     str										=  min(_UpperCAmelCase					,    key=lambda _UpperCAmelCase     : self.bpe_ranks.get(_UpperCAmelCase					,    float("inf"     )     )     )
										if bigram not in self.bpe_ranks:
													break
										__UpperCamelCase						,   __UpperCamelCase :     Union[str, Any]										=  bigram
										__UpperCamelCase :     Optional[Any]										=  []
										__UpperCamelCase :     List[Any]										=  0
										while i < len(_UpperCAmelCase     ):
													try:
																__UpperCamelCase :     List[str]										=  word.index(_UpperCAmelCase					,    _UpperCAmelCase     )
													except ValueError:
																new_word.extend(word[i:]     )
																break
													else:
																new_word.extend(word[i:j]     )
																__UpperCamelCase :     Optional[Any]										=  j

													if word[i] == first and i < len(_UpperCAmelCase     ) - 1 and word[i + 1] == second:
																new_word.append(first + second     )
																i += 2
													else:
																new_word.append(word[i]     )
																i += 1
										__UpperCamelCase :     str										=  tuple(_UpperCAmelCase     )
										__UpperCamelCase :     List[str]										=  new_word
										if len(_UpperCAmelCase     ) == 1:
													break
										else:
													__UpperCamelCase :     List[Any]										=  get_pairs(_UpperCAmelCase     )
							__UpperCamelCase :     Any										=  " ".join(_UpperCAmelCase     )
							if word == "\n  </w>":
										__UpperCamelCase :     Tuple										=  "\n</w>"
							__UpperCamelCase :     Optional[int]										=  word
							return word
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase="en"					,    _UpperCAmelCase=False     )      ->			List[Any]:
							__UpperCamelCase :     Optional[int]										=  self.src_lang

							if self.do_lower_case:
										__UpperCamelCase :     Optional[Any]										=  text.lower()

							if bypass_tokenizer:
										__UpperCamelCase :     Optional[Any]										=  text.split()
							else:
										__UpperCamelCase :     List[Any]										=  self.moses_pipeline(_UpperCAmelCase					,    lang=_UpperCAmelCase     )
										__UpperCamelCase :     Dict										=  self.moses_tokenize(_UpperCAmelCase					,    lang=_UpperCAmelCase     )

							__UpperCamelCase :     str										=  []
							for token in text:
										if token:
													split_tokens.extend(list(self.bpe(_UpperCAmelCase     ).split(" "     )     )     )

							return split_tokens
				def 			a_			(self					,    _UpperCAmelCase     )      ->			Union[str, Any]:
							return self.encoder.get(_UpperCAmelCase					,    self.encoder.get(self.unk_token     )     )
				def 			a_			(self					,    _UpperCAmelCase     )      ->			Union[str, Any]:
							return self.decoder.get(_UpperCAmelCase					,    self.unk_token     )
				def 			a_			(self					,    _UpperCAmelCase     )      ->			Optional[int]:
							__UpperCamelCase :     List[Any]										=  [t.replace(" "					,    ""     ).replace("</w>"					,    " "     ) for t in tokens]
							__UpperCamelCase :     Any										=  "".join(_UpperCAmelCase     ).split()
							# detokenize
							__UpperCamelCase :     Optional[int]										=  self.moses_detokenize(_UpperCAmelCase					,    self.tgt_lang     )
							return text
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase = None     )      ->			List[int]:
							__UpperCamelCase :     Optional[Any]										=  [self.sep_token_id]

							# no bos used in fairseq
							if token_ids_a is None:
										return token_ids_a + sep
							return token_ids_a + sep + token_ids_a + sep
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase = None					,    _UpperCAmelCase = False     )      ->			List[int]:

							if already_has_special_tokens:
										return super().get_special_tokens_mask(
										    token_ids_a=_UpperCAmelCase					,    token_ids_a=_UpperCAmelCase					,    already_has_special_tokens=_UpperCAmelCase     )
							# no bos used in fairseq
							if token_ids_a is not None:
										return ([0] * len(_UpperCAmelCase     )) + [1] + ([0] * len(_UpperCAmelCase     )) + [1]
							return ([0] * len(_UpperCAmelCase     )) + [1]
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase = None     )      ->			List[int]:
							__UpperCamelCase :     Dict										=  [self.sep_token_id]

							# no bos used in fairseq
							if token_ids_a is None:
										return len(token_ids_a + sep     ) * [0]
							return len(token_ids_a + sep     ) * [0] + len(token_ids_a + sep     ) * [1]
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase = None     )      ->			Tuple[str]:
							if not os.path.isdir(_UpperCAmelCase     ):
										logger.error(f"Vocabulary path ({save_directory}) should be a directory"     )
										return

							__UpperCamelCase :     Dict										=  os.path.join(
							    _UpperCAmelCase					,    (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["src_vocab_file"]     )
							__UpperCamelCase :     int										=  os.path.join(
							    _UpperCAmelCase					,    (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["tgt_vocab_file"]     )
							__UpperCamelCase :     Tuple										=  os.path.join(
							    _UpperCAmelCase					,    (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]     )

							with open(_UpperCAmelCase					,    "w"					,    encoding="utf-8"     ) as f:
										f.write(json.dumps(self.encoder					,    indent=2					,    sort_keys=_UpperCAmelCase					,    ensure_ascii=_UpperCAmelCase     ) + "\n"     )

							with open(_UpperCAmelCase					,    "w"					,    encoding="utf-8"     ) as f:
										__UpperCamelCase :     str										=  {v: k for k, v in self.decoder.items()}
										f.write(json.dumps(_UpperCAmelCase					,    indent=2					,    sort_keys=_UpperCAmelCase					,    ensure_ascii=_UpperCAmelCase     ) + "\n"     )

							__UpperCamelCase :     str										=  0
							with open(_UpperCAmelCase					,    "w"					,    encoding="utf-8"     ) as writer:
										for bpe_tokens, token_index in sorted(self.bpe_ranks.items()					,    key=lambda _UpperCAmelCase     : kv[1]     ):
													if index != token_index:
																logger.warning(
																    f"Saving vocabulary to {merges_file}: BPE merge indices are not consecutive."
																    " Please check that the tokenizer is not corrupted!"     )
																__UpperCamelCase :     Optional[int]										=  token_index
													writer.write(" ".join(_UpperCAmelCase     ) + "\n"     )
													index += 1

							return src_vocab_file, tgt_vocab_file, merges_file
				def __getstate__(self     )      ->			Dict:
							__UpperCamelCase :     str										=  self.__dict__.copy()
							__UpperCamelCase :     List[Any]										=  None
							return state





				def __setstate__(self					,    _UpperCAmelCase     )      ->			str:
							__UpperCamelCase :     Any										=  d

							try:
										import sacremoses
							except ImportError:
										raise ImportError(
										    "You need to install sacremoses to use XLMTokenizer. "
										    "See https://pypi.org/project/sacremoses/ for installation."     )

							__UpperCamelCase :     int										=  sacremoses









'''simple docstring'''






import inspect
import unittest
import warnings
from typing import List

from transformers import EfficientFormerConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
				    MODEL_MAPPING,
				    EfficientFormerForImageClassification,
				    EfficientFormerForImageClassificationWithTeacher,
				    EfficientFormerModel,
				)
				from transformers.models.efficientformer.modeling_efficientformer import (
				    EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
				)


if is_vision_available():
				from PIL import Image

				from transformers import EfficientFormerImageProcessor



class    A						:




				'''simple docstring'''





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

							__UpperCamelCase :     int										=  None
							if self.use_labels:
										__UpperCamelCase :     List[str]										=  ids_tensor([self.batch_size]					,    self.type_sequence_label_size     )

							__UpperCamelCase :     Union[str, Any]										=  self.get_config()
							return config, pixel_values, labels
				def 			a_			(self     )      ->			Optional[Any]:
							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=_UpperCAmelCase					,    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					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase     )      ->			Any:
							__UpperCamelCase :     Dict										=  EfficientFormerModel(config=_UpperCAmelCase     )
							model.to(_UpperCAmelCase     )
							model.eval()
							__UpperCamelCase :     Optional[Any]										=  model(_UpperCAmelCase     )
							self.parent.assertEqual(result.last_hidden_state.shape					,    (self.batch_size, self.seq_length, self.hidden_size)     )
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase     )      ->			str:
							__UpperCamelCase :     Dict										=  self.type_sequence_label_size
							__UpperCamelCase :     Dict										=  EfficientFormerForImageClassification(_UpperCAmelCase     )
							model.to(_UpperCAmelCase     )
							model.eval()
							__UpperCamelCase :     int										=  model(_UpperCAmelCase					,    labels=_UpperCAmelCase     )
							self.parent.assertEqual(result.logits.shape					,    (self.batch_size, self.type_sequence_label_size)     )

							# test greyscale images
							__UpperCamelCase :     Any										=  1
							__UpperCamelCase :     Optional[Any]										=  EfficientFormerForImageClassification(_UpperCAmelCase     )
							model.to(_UpperCAmelCase     )
							model.eval()

							__UpperCamelCase :     str										=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
							__UpperCamelCase :     List[str]										=  model(_UpperCAmelCase     )
							self.parent.assertEqual(result.logits.shape					,    (self.batch_size, self.type_sequence_label_size)     )





				def 			a_			(self     )      ->			Tuple:
							__UpperCamelCase :     Tuple										=  self.prepare_config_and_inputs()
							(
							    (
							    __UpperCamelCase
							)						,   (
							    __UpperCamelCase
							)						,   (
							    __UpperCamelCase
							)						,   
							) :     Tuple										=  config_and_inputs
							__UpperCamelCase :     Dict										=  {"pixel_values": pixel_values}
							return config, inputs_dict







@require_torch
class    A						(					SCREAMING_SNAKE_CASE__     ,				SCREAMING_SNAKE_CASE__     ,				unittest.TestCase						):




				'''simple docstring'''





				A       =							(
				    (
				        EfficientFormerModel,
				        EfficientFormerForImageClassificationWithTeacher,
				        EfficientFormerForImageClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				A       =							(
				    {
				        "feature-extraction": EfficientFormerModel,
				        "image-classification": (
				            EfficientFormerForImageClassification,
				            EfficientFormerForImageClassificationWithTeacher,
				        ),
				    }
				    if is_torch_available()
				    else {}
				)
				A       =							False

				A       =							False
				A       =							False
				A       =							False
				def 			a_			(self     )      ->			Optional[int]:
							__UpperCamelCase :     Any										=  EfficientFormerModelTester(self     )
							__UpperCamelCase :     List[str]										=  ConfigTester(
							    self					,    config_class=_UpperCAmelCase					,    has_text_modality=_UpperCAmelCase					,    hidden_size=3_7     )
				def 			a_			(self     )      ->			Dict:
							self.config_tester.run_common_tests()
				@unittest.skip(reason="EfficientFormer does not use inputs_embeds"     )
				def 			a_			(self     )      ->			List[str]:
							pass
				@unittest.skip(reason="EfficientFormer does not support input and output embeddings"     )
				def 			a_			(self     )      ->			List[str]:
							pass
				def 			a_			(self     )      ->			Union[str, Any]:
							__UpperCamelCase						,   __UpperCamelCase :     List[str]										=  self.model_tester.prepare_config_and_inputs_for_common()

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

										__UpperCamelCase :     Optional[Any]										=  ["pixel_values"]
										self.assertListEqual(arg_names[:1]					,    _UpperCAmelCase     )
				def 			a_			(self     )      ->			str:
							def check_hidden_states_output(_UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase     ):
										__UpperCamelCase :     str										=  model_class(_UpperCAmelCase     )
										model.to(_UpperCAmelCase     )
										model.eval()

										with torch.no_grad():
													__UpperCamelCase :     Union[str, Any]										=  model(**self._prepare_for_class(_UpperCAmelCase					,    _UpperCAmelCase     )     )

										__UpperCamelCase :     List[str]										=  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

										__UpperCamelCase :     Union[str, Any]										=  getattr(
										    self.model_tester					,    "expected_num_hidden_layers"					,    self.model_tester.num_hidden_layers + 1     )
										self.assertEqual(len(_UpperCAmelCase     )					,    _UpperCAmelCase     )

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

													self.assertIsInstance(_UpperCAmelCase					,    (list, tuple)     )
													self.assertEqual(len(_UpperCAmelCase     )					,    _UpperCAmelCase     )
													__UpperCamelCase :     Optional[int]										=  getattr(self.model_tester					,    "seq_length"					,    _UpperCAmelCase     )
													__UpperCamelCase :     Dict										=  getattr(self.model_tester					,    "decoder_seq_length"					,    _UpperCAmelCase     )

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

							__UpperCamelCase						,   __UpperCamelCase :     Optional[Any]										=  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										__UpperCamelCase :     Dict										=  True
										check_hidden_states_output(_UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase     )

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

										check_hidden_states_output(_UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase     )
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase=False     )      ->			int:
							__UpperCamelCase :     Optional[Any]										=  super()._prepare_for_class(_UpperCAmelCase					,    _UpperCAmelCase					,    return_labels=_UpperCAmelCase     )

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

							return inputs_dict
				def 			a_			(self     )      ->			List[str]:
							__UpperCamelCase :     List[str]										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_UpperCAmelCase     )
				@unittest.skip(reason="EfficientFormer does not implement masked image modeling yet"     )
				def 			a_			(self     )      ->			Dict:
							__UpperCamelCase :     str										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*_UpperCAmelCase     )
				def 			a_			(self     )      ->			Any:
							__UpperCamelCase :     Any										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase     )
				def 			a_			(self     )      ->			Any:
							if not self.model_tester.is_training:
										return

							__UpperCamelCase						,   __UpperCamelCase :     List[str]										=  self.model_tester.prepare_config_and_inputs_for_common()
							__UpperCamelCase :     List[Any]										=  True

							for model_class in self.all_model_classes:
										# EfficientFormerForImageClassificationWithTeacher supports inference-only
										if (
										    model_class in get_values(_UpperCAmelCase     )
										    or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
										):
													continue
										__UpperCamelCase :     List[str]										=  model_class(_UpperCAmelCase     )
										model.to(_UpperCAmelCase     )
										model.train()
										__UpperCamelCase :     Optional[Any]										=  self._prepare_for_class(_UpperCAmelCase					,    _UpperCAmelCase					,    return_labels=_UpperCAmelCase     )
										__UpperCamelCase :     str										=  model(**_UpperCAmelCase     ).loss
										loss.backward()
				def 			a_			(self     )      ->			Tuple:
							__UpperCamelCase						,   __UpperCamelCase :     Optional[Any]										=  self.model_tester.prepare_config_and_inputs_for_common()

							__UpperCamelCase :     List[Any]										=  [
							    {"title": "multi_label_classification", "num_labels": 2, "dtype": torch.float},
							    {"title": "single_label_classification", "num_labels": 1, "dtype": torch.long},
							    {"title": "regression", "num_labels": 1, "dtype": torch.float},
							]

							for model_class in self.all_model_classes:
										if (
										    model_class
										    not in [
										        *get_values(_UpperCAmelCase     ),
										    ]
										    or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
										):
													continue

										for problem_type in problem_types:
													with self.subTest(msg=f"Testing {model_class} with {problem_type['title']}"     ):
																__UpperCamelCase :     List[str]										=  problem_type["title"]
																__UpperCamelCase :     Dict										=  problem_type["num_labels"]

																__UpperCamelCase :     Optional[Any]										=  model_class(_UpperCAmelCase     )
																model.to(_UpperCAmelCase     )
																model.train()

																__UpperCamelCase :     Optional[Any]										=  self._prepare_for_class(_UpperCAmelCase					,    _UpperCAmelCase					,    return_labels=_UpperCAmelCase     )

																if problem_type["num_labels"] > 1:
																			__UpperCamelCase :     Optional[int]										=  inputs["labels"].unsqueeze(1     ).repeat(1					,    problem_type["num_labels"]     )

																__UpperCamelCase :     Tuple										=  inputs["labels"].to(problem_type["dtype"]     )

																# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
																# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
																# they have the same size." which is a symptom something in wrong for the regression problem.
																# See https://github.com/huggingface/transformers/issues/11780
																with warnings.catch_warnings(record=_UpperCAmelCase     ) as warning_list:
																			__UpperCamelCase :     Union[str, Any]										=  model(**_UpperCAmelCase     ).loss
																for w in warning_list:
																			if "Using a target size that is different to the input size" in str(w.message     ):
																						raise ValueError(
																						    f"Something is going wrong in the regression problem: intercepted {w.message}"     )

																loss.backward()
				@slow
				def 			a_			(self     )      ->			Optional[int]:
							for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										__UpperCamelCase :     Tuple										=  EfficientFormerModel.from_pretrained(_UpperCAmelCase     )
										self.assertIsNotNone(_UpperCAmelCase     )





				def 			a_			(self     )      ->			Tuple:
							__UpperCamelCase						,   __UpperCamelCase :     Optional[Any]										=  self.model_tester.prepare_config_and_inputs_for_common()
							__UpperCamelCase :     Optional[int]										=  True

							__UpperCamelCase :     Union[str, Any]										=  getattr(self.model_tester					,    "seq_length"					,    _UpperCAmelCase     )
							__UpperCamelCase :     int										=  getattr(self.model_tester					,    "encoder_seq_length"					,    _UpperCAmelCase     )
							__UpperCamelCase :     List[Any]										=  getattr(self.model_tester					,    "key_length"					,    _UpperCAmelCase     )
							__UpperCamelCase :     Dict										=  getattr(self.model_tester					,    "chunk_length"					,    _UpperCAmelCase     )

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

							for model_class in self.all_model_classes:
										__UpperCamelCase :     Dict										=  True
										__UpperCamelCase :     Tuple										=  False
										__UpperCamelCase :     Optional[Any]										=  True
										__UpperCamelCase :     Optional[Any]										=  model_class(_UpperCAmelCase     )
										model.to(_UpperCAmelCase     )
										model.eval()
										with torch.no_grad():
													__UpperCamelCase :     Dict										=  model(**self._prepare_for_class(_UpperCAmelCase					,    _UpperCAmelCase     )     )
										__UpperCamelCase :     str										=  outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
										self.assertEqual(len(_UpperCAmelCase     )					,    self.model_tester.num_attention_outputs     )

										# check that output_attentions also work using config
										del inputs_dict["output_attentions"]
										__UpperCamelCase :     List[str]										=  True
										__UpperCamelCase :     Optional[int]										=  model_class(_UpperCAmelCase     )
										model.to(_UpperCAmelCase     )
										model.eval()
										with torch.no_grad():
													__UpperCamelCase :     str										=  model(**self._prepare_for_class(_UpperCAmelCase					,    _UpperCAmelCase     )     )
										__UpperCamelCase :     List[Any]										=  outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
										self.assertEqual(len(_UpperCAmelCase     )					,    self.model_tester.num_attention_outputs     )

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







def      __lowerCAmelCase   (       ):
			__UpperCamelCase :     Any										=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
			return image




@require_torch
@require_vision
class    A						(					unittest.TestCase						):




				'''simple docstring'''





				@cached_property
				def 			a_			(self     )      ->			Optional[Any]:
							return (
							    EfficientFormerImageProcessor.from_pretrained("snap-research/efficientformer-l1-300"     )
							    if is_vision_available()
							    else None
							)
				@slow
				def 			a_			(self     )      ->			Optional[Any]:
							__UpperCamelCase :     str										=  EfficientFormerForImageClassification.from_pretrained("snap-research/efficientformer-l1-300"     ).to(
							    _UpperCAmelCase     )

							__UpperCamelCase :     Optional[int]										=  self.default_image_processor
							__UpperCamelCase :     Optional[Any]										=  prepare_img()
							__UpperCamelCase :     Any										=  image_processor(images=_UpperCAmelCase					,    return_tensors="pt"     ).to(_UpperCAmelCase     )

							# forward pass
							with torch.no_grad():
										__UpperCamelCase :     Optional[Any]										=  model(**_UpperCAmelCase     )

							# verify the logits
							__UpperCamelCase :     Tuple										=  (1, 1_0_0_0)
							self.assertEqual(outputs.logits.shape					,    _UpperCAmelCase     )

							__UpperCamelCase :     Optional[int]										=  torch.tensor([-0.0_555, 0.4_825, -0.0_852]     ).to(_UpperCAmelCase     )
							self.assertTrue(torch.allclose(outputs.logits[0][:3]					,    _UpperCAmelCase					,    atol=1E-4     )     )





				@slow
				def 			a_			(self     )      ->			List[Any]:
							__UpperCamelCase :     Optional[int]										=  EfficientFormerForImageClassificationWithTeacher.from_pretrained(
							    "snap-research/efficientformer-l1-300"     ).to(_UpperCAmelCase     )

							__UpperCamelCase :     str										=  self.default_image_processor
							__UpperCamelCase :     List[str]										=  prepare_img()
							__UpperCamelCase :     List[Any]										=  image_processor(images=_UpperCAmelCase					,    return_tensors="pt"     ).to(_UpperCAmelCase     )

							# forward pass
							with torch.no_grad():
										__UpperCamelCase :     str										=  model(**_UpperCAmelCase     )

							# verify the logits
							__UpperCamelCase :     int										=  (1, 1_0_0_0)
							self.assertEqual(outputs.logits.shape					,    _UpperCAmelCase     )

							__UpperCamelCase :     int										=  torch.tensor([-0.1_312, 0.4_353, -1.0_499]     ).to(_UpperCAmelCase     )
							self.assertTrue(torch.allclose(outputs.logits[0][:3]					,    _UpperCAmelCase					,    atol=1E-4     )     )



