import inspect
import unittest
from typing import List

import numpy as np

from transformers import EfficientFormerConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import (
      TFEfficientFormerForImageClassification,
      TFEfficientFormerForImageClassificationWithTeacher,
      TFEfficientFormerModel,
  )
  from transformers.models.efficientformer.modeling_tf_efficientformer import (
      TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
  )


if is_vision_available():
  from PIL import Image

  from transformers import EfficientFormerImageProcessor


class 				_lowercase	:
      '''simple docstring'''


      def __init__(				self					,lowerCamelCase_					,lowerCamelCase_ = 13					,lowerCamelCase_ = 64					,lowerCamelCase_ = 2					,lowerCamelCase_ = 3					,lowerCamelCase_ = 3					,lowerCamelCase_ = True					,lowerCamelCase_ = True					,lowerCamelCase_ = 128					,lowerCamelCase_=[16, 32, 64, 128]					,lowerCamelCase_ = 7					,lowerCamelCase_ = 4					,lowerCamelCase_ = 37					,lowerCamelCase_ = "gelu"					,lowerCamelCase_ = 0.1					,lowerCamelCase_ = 0.1					,lowerCamelCase_ = 10					,lowerCamelCase_ = 0.02					,lowerCamelCase_ = 2					,lowerCamelCase_ = 1					,lowerCamelCase_ = 128					,lowerCamelCase_ = [2, 2, 2, 2]					,lowerCamelCase_ = 2					,lowerCamelCase_ = 2					,)			->			Tuple:



         '''simple docstring'''



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




      def 		lowerCAmelCase__					(				self							)			->			Optional[int]:



         '''simple docstring'''



         UpperCAmelCase__							:				Any   				=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

         UpperCAmelCase__							:				Optional[int]   				=  self.get_config()

         return config, pixel_values, labels




      def 		lowerCAmelCase__					(				self							)			->			str:



         '''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=_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 		lowerCAmelCase__					(				self					,lowerCamelCase_					,lowerCamelCase_					,lowerCamelCase_							)			->			Optional[int]:



         '''simple docstring'''



         UpperCAmelCase__							:				str   				=  TFEfficientFormerModel(config=_lowerCAmelCase							)
         UpperCAmelCase__							:				Tuple   				=  model(_lowerCAmelCase					,training=_lowerCAmelCase							)
         self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.hidden_size)							)




      def 		lowerCAmelCase__					(				self					,lowerCamelCase_					,lowerCamelCase_					,lowerCamelCase_							)			->			Optional[Any]:



         '''simple docstring'''



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

         # test greyscale images
         UpperCAmelCase__							:				Dict   				=  1
         UpperCAmelCase__							:				Any   				=  TFEfficientFormerForImageClassification(_lowerCAmelCase							)

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




      def 		lowerCAmelCase__					(				self							)			->			Any:



         '''simple docstring'''



         UpperCAmelCase__							:				List[str]   				=  self.prepare_config_and_inputs()
         UpperCAmelCase__				,      UpperCAmelCase__				,      UpperCAmelCase__							:				int   				=  config_and_inputs
         UpperCAmelCase__							:				int   				=  {'''pixel_values''': pixel_values}
         return config, inputs_dict




@require_tf
class 				_lowercase	(      _lowerCAmelCase		,_lowerCAmelCase		,unittest.TestCase   ):
      '''simple docstring'''


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

      UpperCAmelCase_      :      int      							=   False

      UpperCAmelCase_      :      Optional[int]      							=   False
      UpperCAmelCase_      :      int      							=   False
      UpperCAmelCase_      :      Any      							=   False
      UpperCAmelCase_      :      Any      							=   False




      def 		lowerCAmelCase__					(				self							)			->			Tuple:



         '''simple docstring'''



         UpperCAmelCase__							:				List[str]   				=  TFEfficientFormerModelTester(self							)
         UpperCAmelCase__							:				int   				=  ConfigTester(
             self					,config_class=_lowerCAmelCase					,has_text_modality=_lowerCAmelCase					,hidden_size=37							)




      def 		lowerCAmelCase__					(				self							)			->			int:



         '''simple docstring'''



         self.config_tester.run_common_tests()




      @unittest.skip(reason='''EfficientFormer does not use inputs_embeds'''							)
      def 		lowerCAmelCase__					(				self							)			->			Any:



         '''simple docstring'''



         pass




      @unittest.skip(reason='''EfficientFormer does not support input and output embeddings'''							)
      def 		lowerCAmelCase__					(				self							)			->			str:



         '''simple docstring'''



         pass




      def 		lowerCAmelCase__					(				self							)			->			List[str]:



         '''simple docstring'''



         UpperCAmelCase__				,      UpperCAmelCase__							:				int   				=  self.model_tester.prepare_config_and_inputs_for_common()

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

            UpperCAmelCase__							:				int   				=  ['''pixel_values''']
            self.assertListEqual(arg_names[:1]					,_lowerCAmelCase							)




      def 		lowerCAmelCase__					(				self							)			->			Dict:



         '''simple docstring'''



         def check_hidden_states_output(lowerCamelCase_					,lowerCamelCase_					,lowerCamelCase_							):
            UpperCAmelCase__							:				Any   				=  model_class(_lowerCAmelCase							)

            UpperCAmelCase__							:				int   				=  model(**self._prepare_for_class(_lowerCAmelCase					,_lowerCAmelCase							)					,training=_lowerCAmelCase							)
            UpperCAmelCase__							:				Union[str, Any]   				=  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

               self.asseretIsInstance(_lowerCAmelCase					,(list, tuple)							)
               self.assertEqual(len(_lowerCAmelCase							)					,_lowerCAmelCase							)
               UpperCAmelCase__							:				List[Any]   				=  getattr(self.model_tester					,'''seq_length'''					,_lowerCAmelCase							)
               UpperCAmelCase__							:				List[Any]   				=  getattr(self.model_tester					,'''decoder_seq_length'''					,_lowerCAmelCase							)

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

         UpperCAmelCase__				,      UpperCAmelCase__							:				int   				=  self.model_tester.prepare_config_and_inputs_for_common()

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

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            UpperCAmelCase__							:				Union[str, Any]   				=  True

            check_hidden_states_output(_lowerCAmelCase					,_lowerCAmelCase					,_lowerCAmelCase							)




      def 		lowerCAmelCase__					(				self					,lowerCamelCase_					,lowerCamelCase_					,lowerCamelCase_=False							)			->			Dict:



         '''simple docstring'''



         UpperCAmelCase__							:				Optional[Any]   				=  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 		lowerCAmelCase__					(				self							)			->			int:



         '''simple docstring'''



         UpperCAmelCase__							:				List[Any]   				=  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 		lowerCAmelCase__					(				self							)			->			str:



         '''simple docstring'''



         UpperCAmelCase__							:				Dict   				=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCAmelCase							)




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



         '''simple docstring'''



         UpperCAmelCase__							:				List[Any]   				=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase							)




      @slow
      def 		lowerCAmelCase__					(				self							)			->			List[Any]:



         '''simple docstring'''



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




      def 		lowerCAmelCase__					(				self							)			->			List[str]:



         '''simple docstring'''



         UpperCAmelCase__				,      UpperCAmelCase__							:				List[str]   				=  self.model_tester.prepare_config_and_inputs_for_common()
         UpperCAmelCase__							:				Dict   				=  True

         UpperCAmelCase__							:				Dict   				=  getattr(self.model_tester					,'''seq_length'''					,_lowerCAmelCase							)
         UpperCAmelCase__							:				List[str]   				=  getattr(self.model_tester					,'''encoder_seq_length'''					,_lowerCAmelCase							)
         UpperCAmelCase__							:				str   				=  getattr(self.model_tester					,'''key_length'''					,_lowerCAmelCase							)
         UpperCAmelCase__							:				List[str]   				=  getattr(self.model_tester					,'''chunk_length'''					,_lowerCAmelCase							)

         if chunk_length is not None and hasattr(self.model_tester					,'''num_hashes'''							):
            UpperCAmelCase__							:				Any   				=  encoder_seq_length * self.model_tester.num_hashes

         for model_class in self.all_model_classes:
            UpperCAmelCase__							:				Any   				=  True
            UpperCAmelCase__							:				Dict   				=  False
            UpperCAmelCase__							:				Any   				=  True
            UpperCAmelCase__							:				Union[str, Any]   				=  model_class(_lowerCAmelCase							)

            UpperCAmelCase__							:				int   				=  model(**self._prepare_for_class(_lowerCAmelCase					,_lowerCAmelCase							)					,training=_lowerCAmelCase							)
            UpperCAmelCase__							:				Optional[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"]
            UpperCAmelCase__							:				Optional[Any]   				=  True
            UpperCAmelCase__							:				Optional[int]   				=  model_class(_lowerCAmelCase							)
            UpperCAmelCase__							:				List[Any]   				=  model(**self._prepare_for_class(_lowerCAmelCase					,_lowerCAmelCase							)					,training=_lowerCAmelCase							)

            UpperCAmelCase__							:				Union[str, Any]   				=  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 		lowerCAmelCase__					(				self							)			->			Optional[int]:



         '''simple docstring'''



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

         for model_class in self.all_model_classes:
            # Prepare our model
            UpperCAmelCase__							:				Dict   				=  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
            UpperCAmelCase__							:				Tuple   				=  {
                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
            }
            UpperCAmelCase__							:				int   				=  model(_lowerCAmelCase							)
            self.assertTrue(outputs_dict is not None							)







def __UpperCamelCase(   ):
   '''simple docstring'''
   UpperCAmelCase__							:				Dict   				=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
   return image


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


      @cached_property
      def 		lowerCAmelCase__					(				self							)			->			Any:



         '''simple docstring'''



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




      @slow
      def 		lowerCAmelCase__					(				self							)			->			Any:



         '''simple docstring'''



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




      @slow
      def 		lowerCAmelCase__					(				self							)			->			Tuple:



         '''simple docstring'''



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





'''simple docstring'''

import json
import os
from typing import Optional, Tuple

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


UpperCamelCase__     :	int				=			logging.get_logger(__name__)

UpperCamelCase__     :	Optional[Any]				=			{'vocab_file': 'vocab.json'}

UpperCamelCase__     :	Tuple				=			{
    'vocab_file': {
        'mgp-str': 'https://huggingface.co/alibaba-damo/mgp-str-base/blob/main/vocab.json',
    }
}

UpperCamelCase__     :	List[Any]				=			{'mgp-str': 27}


class 				_lowercase	(      lowerCAmelCase   ):
      '''simple docstring'''


      UpperCAmelCase_      :      Any      							=   VOCAB_FILES_NAMES
      UpperCAmelCase_      :      Dict      							=   PRETRAINED_VOCAB_FILES_MAP
      UpperCAmelCase_      :      int      							=   PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES




      def __init__(				self					,lowerCamelCase_					,lowerCamelCase_="[GO]"					,lowerCamelCase_="[GO]"					,lowerCamelCase_="[s]"					,lowerCamelCase_="[GO]"					,**lowerCamelCase_							)			->			str:



         '''simple docstring'''



         super().__init__(
             unk_token=lowerCamelCase_					,bos_token=lowerCamelCase_					,eos_token=lowerCamelCase_					,pad_token=lowerCamelCase_					,**lowerCamelCase_					,)

         with open(lowerCamelCase_					,encoding='''utf-8'''							) as vocab_handle:
            UpperCAmelCase__							:				Optional[Any]   				=  json.load(lowerCamelCase_							)
         UpperCAmelCase__							:				Any   				=  {v: k for k, v in self.vocab.items()}




      @property
      def 		lowerCAmelCase__					(				self							)			->			Tuple:



         '''simple docstring'''



         return len(self.vocab							)




      def 		lowerCAmelCase__					(				self							)			->			Dict:



         '''simple docstring'''



         return dict(self.vocab					,**self.added_tokens_encoder							)




      def 		lowerCAmelCase__					(				self					,lowerCamelCase_							)			->			Any:



         '''simple docstring'''



         UpperCAmelCase__							:				List[Any]   				=  []
         for s in text:
            char_tokens.extend(lowerCamelCase_							)
         return char_tokens




      def 		lowerCAmelCase__					(				self					,lowerCamelCase_							)			->			str:



         '''simple docstring'''



         return self.vocab.get(lowerCamelCase_					,self.vocab.get(self.unk_token							)							)




      def 		lowerCAmelCase__					(				self					,lowerCamelCase_							)			->			Any:



         '''simple docstring'''



         return self.decoder.get(lowerCamelCase_							)




      def 		lowerCAmelCase__					(				self					,lowerCamelCase_					,lowerCamelCase_ = None							)			->			Tuple[str]:



         '''simple docstring'''



         if not os.path.isdir(lowerCamelCase_							):
            logger.error('''Vocabulary path ({}) should be a directory'''.format(lowerCamelCase_							)							)
            return
         UpperCAmelCase__							:				Union[str, Any]   				=  os.path.join(
             lowerCamelCase_					,(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']							)

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

         return (vocab_file,)




