



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



   '''simple docstring'''






   def __init__(					self							:			Union[str, Any]				,		lowerCAmelCase							:			List[Any]				,		lowerCAmelCase							:			str = 13				,		lowerCAmelCase							:			Union[str, Any] = 64				,		lowerCAmelCase							:			Any = 2				,		lowerCAmelCase							:			Optional[Any] = 3				,		lowerCAmelCase							:			List[str] = 3				,		lowerCAmelCase							:			Optional[Any] = True				,		lowerCAmelCase							:			Dict = True				,		lowerCAmelCase							:			int = 128				,		lowerCAmelCase							:			int=[16, 32, 64, 128]				,		lowerCAmelCase							:			Optional[Any] = 7				,		lowerCAmelCase							:			Optional[Any] = 4				,		lowerCAmelCase							:			Union[str, Any] = 37				,		lowerCAmelCase							:			Tuple = "gelu"				,		lowerCAmelCase							:			Any = 0.1				,		lowerCAmelCase							:			Union[str, Any] = 0.1				,		lowerCAmelCase							:			int = 10				,		lowerCAmelCase							:			Tuple = 0.02				,		lowerCAmelCase							:			Tuple = 2				,		lowerCAmelCase							:			Optional[int] = 1				,		lowerCAmelCase							:			Optional[int] = 128				,		lowerCAmelCase							:			Optional[int] = [2, 2, 2, 2]				,		lowerCAmelCase							:			List[Any] = 2				,		lowerCAmelCase							:			Optional[int] = 2				,		)				->				str:

    """simple docstring"""







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

   def      UpperCamelCase_  (					self							:			Optional[Any])				->				Any:

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

    _snake_case    :  Tuple					=					self.get_config()

    return config, pixel_values, labels

   def      UpperCamelCase_  (					self							:			Optional[Any])				->				Any:

    """simple docstring"""







    return EfficientFormerConfig(
        image_size=self.image_size				,		patch_size=self.patch_size				,		num_channels=self.num_channels				,		hidden_size=self.hidden_size				,		num_hidden_layers=self.num_hidden_layers				,		num_attention_heads=self.num_attention_heads				,		intermediate_size=self.intermediate_size				,		hidden_act=self.hidden_act				,		hidden_dropout_prob=self.hidden_dropout_prob				,		attention_probs_dropout_prob=self.attention_probs_dropout_prob				,		is_decoder=a_				,		initializer_range=self.initializer_range				,		encoder_stride=self.encoder_stride				,		resolution=self.resolution				,		depths=self.depths				,		hidden_sizes=self.hidden_sizes				,		dim=self.dim				,		mlp_expansion_ratio=self.mlp_expansion_ratio				,		)

   def      UpperCamelCase_  (					self							:			Union[str, Any]				,		lowerCAmelCase							:			str				,		lowerCAmelCase							:			List[Any]				,		lowerCAmelCase							:			Optional[int])				->				Union[str, Any]:

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					TFEfficientFormerModel(config=a_)
    _snake_case    :  Optional[int]					=					model(a_				,		training=a_)
    self.parent.assertEqual(result.last_hidden_state.shape				,		(self.batch_size, self.seq_length, self.hidden_size))

   def      UpperCamelCase_  (					self							:			Optional[int]				,		lowerCAmelCase							:			Optional[Any]				,		lowerCAmelCase							:			str				,		lowerCAmelCase							:			List[str])				->				Optional[Any]:

    """simple docstring"""







    _snake_case    :  Optional[Any]					=					self.type_sequence_label_size
    _snake_case    :  List[str]					=					TFEfficientFormerForImageClassification(a_)
    _snake_case    :  Union[str, Any]					=					model(a_				,		labels=a_				,		training=a_)
    self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.type_sequence_label_size))

    # test greyscale images
    _snake_case    :  int					=					1
    _snake_case    :  Union[str, Any]					=					TFEfficientFormerForImageClassification(a_)

    _snake_case    :  Optional[Any]					=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
    _snake_case    :  List[Any]					=					model(a_				,		labels=a_)
    self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.type_sequence_label_size))


   def      UpperCamelCase_  (					self							:			Optional[int])				->				Tuple:

    """simple docstring"""







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







@require_tf
class 		snake_case						(	__lowerCamelCase					,__lowerCamelCase					,unittest.TestCase  ):



   '''simple docstring'''






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

   snake_case_					: str          =		False

   snake_case_					: Union[str, Any]          =		False
   snake_case_					: List[Any]          =		False
   snake_case_					: Union[str, Any]          =		False
   snake_case_					: Dict          =		False

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

    """simple docstring"""







    _snake_case    :  int					=					TFEfficientFormerModelTester(self)
    _snake_case    :  str					=					ConfigTester(
        self				,		config_class=a_				,		has_text_modality=a_				,		hidden_size=37)

   def      UpperCamelCase_  (					self							:			List[str])				->				Optional[int]:

    """simple docstring"""







    self.config_tester.run_common_tests()

   @unittest.skip(reason="""EfficientFormer does not use inputs_embeds""")
   def      UpperCamelCase_  (					self							:			List[str])				->				Optional[Any]:

    """simple docstring"""







    pass

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

    """simple docstring"""







    pass

   def      UpperCamelCase_  (					self							:			List[str])				->				Optional[int]:

    """simple docstring"""







    _snake_case    :  Tuple					=					self.model_tester.prepare_config_and_inputs_for_common()

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

     _snake_case    :  Optional[Any]					=					["pixel_values"]
     self.assertListEqual(arg_names[:1]				,		a_)

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

    """simple docstring"""







    def check_hidden_states_output(lowerCAmelCase							:			Union[str, Any]				,		lowerCAmelCase							:			Optional[int]				,		lowerCAmelCase							:			int):
     _snake_case    :  int					=					model_class(a_)

     _snake_case    :  int					=					model(**self._prepare_for_class(a_				,		a_)				,		training=a_)
     _snake_case    :  Any					=					outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

     _snake_case    :  List[Any]					=					getattr(
         self.model_tester				,		"""expected_num_hidden_layers"""				,		self.model_tester.num_hidden_layers + 1)
     self.assertEqual(len(a_)				,		a_)

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

      self.asseretIsInstance(a_				,		(list, tuple))
      self.assertEqual(len(a_)				,		a_)
      _snake_case    :  Any					=					getattr(self.model_tester				,		"""seq_length"""				,		a_)
      _snake_case    :  Union[str, Any]					=					getattr(self.model_tester				,		"""decoder_seq_length"""				,		a_)

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

    _snake_case    :  Any					=					self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     _snake_case    :  Optional[int]					=					True
     check_hidden_states_output(a_				,		a_				,		a_)

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

     check_hidden_states_output(a_				,		a_				,		a_)

   def      UpperCamelCase_  (					self							:			List[str]				,		lowerCAmelCase							:			List[Any]				,		lowerCAmelCase							:			str				,		lowerCAmelCase							:			Tuple=False)				->				Union[str, Any]:

    """simple docstring"""







    _snake_case    :  List[str]					=					super()._prepare_for_class(a_				,		a_				,		return_labels=a_)

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

    return inputs_dict

   def      UpperCamelCase_  (					self							:			str)				->				Optional[int]:

    """simple docstring"""







    _snake_case    :  int					=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*a_)

   @unittest.skip(reason="""EfficientFormer does not implement masked image modeling yet""")
   def      UpperCamelCase_  (					self							:			Optional[int])				->				Optional[Any]:

    """simple docstring"""







    _snake_case    :  Optional[Any]					=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_image_modeling(*a_)

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

    """simple docstring"""







    _snake_case    :  Any					=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*a_)

   @slow
   def      UpperCamelCase_  (					self							:			Optional[int])				->				Dict:

    """simple docstring"""







    for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     _snake_case    :  Tuple					=					TFEfficientFormerModel.from_pretrained(a_)
     self.assertIsNotNone(a_)

   def      UpperCamelCase_  (					self							:			Optional[Any])				->				Any:

    """simple docstring"""







    _snake_case    :  Any					=					self.model_tester.prepare_config_and_inputs_for_common()
    _snake_case    :  Tuple					=					True

    _snake_case    :  Tuple					=					getattr(self.model_tester				,		"""seq_length"""				,		a_)
    _snake_case    :  List[Any]					=					getattr(self.model_tester				,		"""encoder_seq_length"""				,		a_)
    _snake_case    :  Dict					=					getattr(self.model_tester				,		"""key_length"""				,		a_)
    _snake_case    :  Optional[Any]					=					getattr(self.model_tester				,		"""chunk_length"""				,		a_)

    if chunk_length is not None and hasattr(self.model_tester				,		"""num_hashes"""):
     _snake_case    :  Optional[int]					=					encoder_seq_length * self.model_tester.num_hashes

    for model_class in self.all_model_classes:
     _snake_case    :  Union[str, Any]					=					True
     _snake_case    :  Optional[int]					=					False
     _snake_case    :  Dict					=					True
     _snake_case    :  str					=					model_class(a_)

     _snake_case    :  Dict					=					model(**self._prepare_for_class(a_				,		a_)				,		training=a_)
     _snake_case    :  Any					=					outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
     self.assertEqual(len(a_)				,		self.model_tester.num_attention_outputs)

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

     _snake_case    :  List[str]					=					outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
     self.assertEqual(len(a_)				,		self.model_tester.num_attention_outputs)

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


   def      UpperCamelCase_  (					self							:			Optional[Any])				->				int:

    """simple docstring"""







    _snake_case    :  Dict					=					self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     # Prepare our model
     _snake_case    :  Optional[int]					=					model_class(a_)
     # These are maximally general inputs for the model, with multiple None dimensions
     # Hopefully this will catch any conditionals that fail for flexible shapes
     _snake_case    :  Optional[Any]					=					{
         key: tf.keras.Input(shape=val.shape[1:]				,		dtype=val.dtype				,		name=a_)
         for key, val in model.input_signature.items()
         if key in model.dummy_inputs
     }
     _snake_case    :  List[str]					=					model(a_)
     self.assertTrue(outputs_dict is not None)



def       lowercase       (   )  ->     Optional[int]:
 _snake_case    :  Dict					=					Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""				)
 return image







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



   '''simple docstring'''






   @cached_property
   def      UpperCamelCase_  (					self							:			str)				->				str:

    """simple docstring"""







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

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

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					TFEfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300""")
    _snake_case    :  List[str]					=					self.default_image_processor
    _snake_case    :  Tuple					=					prepare_img()
    _snake_case    :  str					=					image_processor(images=a_				,		return_tensors="""tf""")
    # forward pass
    _snake_case    :  Any					=					model(**a_				,		training=a_)
    # verify the logits
    _snake_case    :  Optional[Any]					=					tf.TensorShape((1, 1000))
    self.assertEqual(outputs.logits.shape				,		a_)
    _snake_case    :  Any					=					tf.constant([-0.0_555, 0.4_825, -0.0_852])
    self.assertTrue(np.allclose(outputs.logits[0, :3]				,		a_				,		atol=1E-4))


   @slow
   def      UpperCamelCase_  (					self							:			Optional[Any])				->				Optional[int]:

    """simple docstring"""







    _snake_case    :  Optional[Any]					=					TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
        """snap-research/efficientformer-l1-300""")
    _snake_case    :  List[str]					=					self.default_image_processor
    _snake_case    :  int					=					prepare_img()
    _snake_case    :  Optional[int]					=					image_processor(images=a_				,		return_tensors="""tf""")
    # forward pass
    _snake_case    :  int					=					model(**a_				,		training=a_)
    # verify the logits
    _snake_case    :  Dict					=					tf.TensorShape((1, 1000))
    self.assertEqual(outputs.logits.shape				,		a_)
    _snake_case    :  List[str]					=					tf.constant([-0.1_312, 0.4_353, -1.0_499])
    self.assertTrue(np.allclose(outputs.logits[0, :3]				,		a_				,		atol=1E-4))





import json
import os
import unittest

from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin







class 		snake_case						(	SCREAMING_SNAKE_CASE_					,unittest.TestCase  ):



   '''simple docstring'''






   snake_case_					: Optional[Any]          =		CTRLTokenizer
   snake_case_					: Optional[Any]          =		False
   snake_case_					: List[Any]          =		False

   def      UpperCamelCase_  (					self							:			int)				->				Dict:

    """simple docstring"""







    super().setUp()

    # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
    _snake_case    :  Dict					=					["""adapt""", """re@@""", """a@@""", """apt""", """c@@""", """t""", """<unk>"""]
    _snake_case    :  Optional[int]					=					dict(zip(lowerCAmelCase				,		range(len(lowerCAmelCase))))
    _snake_case    :  int					=					["""#version: 0.2""", """a p""", """ap t</w>""", """r e""", """a d""", """ad apt</w>""", """"""]
    _snake_case    :  List[Any]					=					{"""unk_token""": """<unk>"""}

    _snake_case    :  Dict					=					os.path.join(self.tmpdirname				,		VOCAB_FILES_NAMES["""vocab_file"""])
    _snake_case    :  Any					=					os.path.join(self.tmpdirname				,		VOCAB_FILES_NAMES["""merges_file"""])
    with open(self.vocab_file				,		"""w"""				,		encoding="""utf-8""") as fp:
     fp.write(json.dumps(lowerCAmelCase) + """\n""")
    with open(self.merges_file				,		"""w"""				,		encoding="""utf-8""") as fp:
     fp.write("""\n""".join(lowerCAmelCase))

   def      UpperCamelCase_  (					self							:			Dict				,		**lowerCAmelCase							:			int)				->				Optional[Any]:

    """simple docstring"""







    kwargs.update(self.special_tokens_map)
    return CTRLTokenizer.from_pretrained(self.tmpdirname				,		**lowerCAmelCase)

   def      UpperCamelCase_  (					self							:			Any				,		lowerCAmelCase							:			Optional[int])				->				Optional[int]:

    """simple docstring"""







    _snake_case    :  Tuple					=					"""adapt react readapt apt"""
    _snake_case    :  Tuple					=					"""adapt react readapt apt"""
    return input_text, output_text


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

    """simple docstring"""







    _snake_case    :  str					=					CTRLTokenizer(self.vocab_file				,		self.merges_file				,		**self.special_tokens_map)
    _snake_case    :  Dict					=					"""adapt react readapt apt"""
    _snake_case    :  int					=					"""adapt re@@ a@@ c@@ t re@@ adapt apt""".split()
    _snake_case    :  Tuple					=					tokenizer.tokenize(lowerCAmelCase)
    self.assertListEqual(lowerCAmelCase				,		lowerCAmelCase)

    _snake_case    :  str					=					tokens + [tokenizer.unk_token]

    _snake_case    :  Union[str, Any]					=					[0, 1, 2, 4, 5, 1, 0, 3, 6]
    self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase)				,		lowerCAmelCase)
