



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



 """simple docstring"""

 def __init__(    self					:					List[Any]			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int = 13			,						__lowerCAmelCase					:					int = 64			,						__lowerCAmelCase					:					int = 2			,						__lowerCAmelCase					:					int = 3			,						__lowerCAmelCase					:					int = 3			,						__lowerCAmelCase					:					bool = True			,						__lowerCAmelCase					:					bool = True			,						__lowerCAmelCase					:					int = 1_28			,						__lowerCAmelCase					:					Optional[int]=[16, 32, 64, 1_28]			,						__lowerCAmelCase					:					int = 7			,						__lowerCAmelCase					:					int = 4			,						__lowerCAmelCase					:					int = 37			,						__lowerCAmelCase					:					str = "gelu"			,						__lowerCAmelCase					:					float = 0.1			,						__lowerCAmelCase					:					float = 0.1			,						__lowerCAmelCase					:					int = 10			,						__lowerCAmelCase					:					float = 0.02			,						__lowerCAmelCase					:					int = 2			,						__lowerCAmelCase					:					int = 1			,						__lowerCAmelCase					:					int = 1_28			,						__lowerCAmelCase					:					List[int] = [2, 2, 2, 2]			,						__lowerCAmelCase					:					int = 2			,						__lowerCAmelCase					:					int = 2			,						)   ->	List[str]:
    _A      =       parent
    _A      =       batch_size
    _A      =       image_size
    _A      =       patch_size
    _A      =       num_channels
    _A      =       is_training
    _A      =       use_labels
    _A      =       hidden_size
    _A      =       num_hidden_layers
    _A      =       num_attention_heads
    _A      =       intermediate_size
    _A      =       hidden_act
    _A      =       hidden_dropout_prob
    _A      =       attention_probs_dropout_prob
    _A      =       type_sequence_label_size
    _A      =       initializer_range
    _A      =       encoder_stride
    _A      =       num_attention_outputs
    _A      =       embed_dim
    _A      =       embed_dim + 1
    _A      =       resolution
    _A      =       depths
    _A      =       hidden_sizes
    _A      =       dim
    _A      =       mlp_expansion_ratio

 def     snake_case_      (    self					:					List[str]      )   ->	str:
    _A      =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

    _A      =       None
    if self.use_labels:
       _A      =       ids_tensor([self.batch_size]			,						self.type_sequence_label_size      )

    _A      =       self.get_config()

    return config, pixel_values, labels

 def     snake_case_      (    self					:					Tuple      )   ->	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=snake_case__			,						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     snake_case_      (    self					:					int			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					Optional[int]      )   ->	Any:
    _A      =       TFEfficientFormerModel(config=snake_case__      )
    _A      =       model(snake_case__			,						training=snake_case__      )
    self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)      )

 def     snake_case_      (    self					:					int			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					int      )   ->	Optional[Any]:
    _A      =       self.type_sequence_label_size
    _A      =       TFEfficientFormerForImageClassification(snake_case__      )
    _A      =       model(snake_case__			,						labels=snake_case__			,						training=snake_case__      )
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.type_sequence_label_size)      )

    # test greyscale images
    _A      =       1
    _A      =       TFEfficientFormerForImageClassification(snake_case__      )

    _A      =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
    _A      =       model(snake_case__			,						labels=snake_case__      )
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.type_sequence_label_size)      )

 def     snake_case_      (    self					:					int      )   ->	Union[str, Any]:
    _A      =       self.prepare_config_and_inputs()
    _A  ,    _A  ,    _A      =       config_and_inputs
    _A      =       {'''pixel_values''': pixel_values}
    return config, inputs_dict


@require_tf
class 	lowerCamelCase__	(       SCREAMING_SNAKE_CASE__							,  SCREAMING_SNAKE_CASE__							,  unittest.TestCase):



 """simple docstring"""

 a__					: Tuple =  (
     (
         TFEfficientFormerModel,
         TFEfficientFormerForImageClassificationWithTeacher,
         TFEfficientFormerForImageClassification,
     )
     if is_tf_available()
     else ()
 )
 a__					: List[Any] =  (
     {
         "feature-extraction": TFEfficientFormerModel,
         "image-classification": (
             TFEfficientFormerForImageClassification,
             TFEfficientFormerForImageClassificationWithTeacher,
         ),
     }
     if is_tf_available()
     else {}
 )

 a__					: Dict =  False

 a__					: Optional[Any] =  False
 a__					: List[str] =  False
 a__					: Union[str, Any] =  False
 a__					: int =  False

 def     snake_case_      (    self					:					str      )   ->	List[str]:
    _A      =       TFEfficientFormerModelTester(self      )
    _A      =       ConfigTester(
        self			,						config_class=snake_case__			,						has_text_modality=snake_case__			,						hidden_size=37      )

 def     snake_case_      (    self					:					Union[str, Any]      )   ->	Optional[int]:
    self.config_tester.run_common_tests()

 @unittest.skip(reason='''EfficientFormer does not use inputs_embeds'''      )
 def     snake_case_      (    self					:					List[Any]      )   ->	List[str]:
    pass

 @unittest.skip(reason='''EfficientFormer does not support input and output embeddings'''      )
 def     snake_case_      (    self					:					Dict      )   ->	List[str]:
    pass

 def     snake_case_      (    self					:					Optional[Any]      )   ->	Optional[Any]:
    _A  ,    _A      =       self.model_tester.prepare_config_and_inputs_for_common()

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

       _A      =       ['''pixel_values''']
       self.assertListEqual(arg_names[:1]			,						snake_case__      )

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

    def check_hidden_states_output(__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					Dict      ):
       _A      =       model_class(snake_case__      )

       _A      =       model(**self._prepare_for_class(snake_case__			,						snake_case__      )			,						training=snake_case__      )
       _A      =       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

       _A      =       getattr(
           self.model_tester			,						'''expected_num_hidden_layers'''			,						self.model_tester.num_hidden_layers + 1      )
       self.assertEqual(len(snake_case__      )			,						snake_case__      )

       if hasattr(self.model_tester			,						'''encoder_seq_length'''      ):
          _A      =       self.model_tester.encoder_seq_length
          if hasattr(self.model_tester			,						'''chunk_length'''      ) and self.model_tester.chunk_length > 1:
             _A      =       seq_length * self.model_tester.chunk_length
       else:
          _A      =       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:
          _A      =       outputs.decoder_hidden_states

          self.asseretIsInstance(snake_case__			,						(list, tuple)      )
          self.assertEqual(len(snake_case__      )			,						snake_case__      )
          _A      =       getattr(self.model_tester			,						'''seq_length'''			,						snake_case__      )
          _A      =       getattr(self.model_tester			,						'''decoder_seq_length'''			,						snake_case__      )

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

    _A  ,    _A      =       self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       _A      =       True
       check_hidden_states_output(snake_case__			,						snake_case__			,						snake_case__      )

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       _A      =       True

       check_hidden_states_output(snake_case__			,						snake_case__			,						snake_case__      )

 def     snake_case_      (    self					:					str			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Optional[Any]=False      )   ->	List[str]:
    _A      =       super()._prepare_for_class(snake_case__			,						snake_case__			,						return_labels=snake_case__      )

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

    return inputs_dict

 def     snake_case_      (    self					:					Tuple      )   ->	Any:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*snake_case__      )

 @unittest.skip(reason='''EfficientFormer does not implement masked image modeling yet'''      )
 def     snake_case_      (    self					:					Any      )   ->	Tuple:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_image_modeling(*snake_case__      )

 def     snake_case_      (    self					:					Optional[Any]      )   ->	Optional[int]:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*snake_case__      )

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

    for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _A      =       TFEfficientFormerModel.from_pretrained(snake_case__      )
       self.assertIsNotNone(snake_case__      )

 def     snake_case_      (    self					:					List[str]      )   ->	Dict:
    _A  ,    _A      =       self.model_tester.prepare_config_and_inputs_for_common()
    _A      =       True

    _A      =       getattr(self.model_tester			,						'''seq_length'''			,						snake_case__      )
    _A      =       getattr(self.model_tester			,						'''encoder_seq_length'''			,						snake_case__      )
    _A      =       getattr(self.model_tester			,						'''key_length'''			,						snake_case__      )
    _A      =       getattr(self.model_tester			,						'''chunk_length'''			,						snake_case__      )

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

    for model_class in self.all_model_classes:
       _A      =       True
       _A      =       False
       _A      =       True
       _A      =       model_class(snake_case__      )

       _A      =       model(**self._prepare_for_class(snake_case__			,						snake_case__      )			,						training=snake_case__      )
       _A      =       outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
       self.assertEqual(len(snake_case__      )			,						self.model_tester.num_attention_outputs      )

       # check that output_attentions also work using config
       del inputs_dict["output_attentions"]
       _A      =       True
       _A      =       model_class(snake_case__      )
       _A      =       model(**self._prepare_for_class(snake_case__			,						snake_case__      )			,						training=snake_case__      )

       _A      =       outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
       self.assertEqual(len(snake_case__      )			,						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     snake_case_      (    self					:					List[str]      )   ->	List[str]:
    _A  ,    _A      =       self.model_tester.prepare_config_and_inputs_for_common()

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

def 	SCREAMING_SNAKE_CASE_							(	)  ->     Dict:
   _A      =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
   return image


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



 """simple docstring"""

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

 @slow
 def     snake_case_      (    self					:					List[Any]      )   ->	Optional[int]:
    _A      =       TFEfficientFormerForImageClassification.from_pretrained('''snap-research/efficientformer-l1-300'''      )
    _A      =       self.default_image_processor
    _A      =       prepare_img()
    _A      =       image_processor(images=snake_case__			,						return_tensors='''tf'''      )
    # forward pass
    _A      =       model(**snake_case__			,						training=snake_case__      )
    # verify the logits
    _A      =       tf.TensorShape((1, 10_00)      )
    self.assertEqual(outputs.logits.shape			,						snake_case__      )
    _A      =       tf.constant([-0.0555, 0.4825, -0.0852]      )
    self.assertTrue(np.allclose(outputs.logits[0, :3]			,						snake_case__			,						atol=1E-4      )      )

 @slow
 def     snake_case_      (    self					:					Optional[int]      )   ->	Dict:
    _A      =       TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
        '''snap-research/efficientformer-l1-300'''      )
    _A      =       self.default_image_processor
    _A      =       prepare_img()
    _A      =       image_processor(images=snake_case__			,						return_tensors='''tf'''      )
    # forward pass
    _A      =       model(**snake_case__			,						training=snake_case__      )
    # verify the logits
    _A      =       tf.TensorShape((1, 10_00)      )
    self.assertEqual(outputs.logits.shape			,						snake_case__      )
    _A      =       tf.constant([-0.1312, 0.4353, -1.0499]      )
    self.assertTrue(np.allclose(outputs.logits[0, :3]			,						snake_case__			,						atol=1E-4      )      )









from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import TensorType, logging


if TYPE_CHECKING:
						from ...onnx.config import PatchingSpec
						from ...tokenization_utils_base import PreTrainedTokenizerBase


SCREAMING_SNAKE_CASE:     str									= logging.get_logger(__name__)

SCREAMING_SNAKE_CASE:     Optional[int]									= {
    '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json''',
    '''allenai/longformer-large-4096''': '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json''',
    '''allenai/longformer-large-4096-finetuned-triviaqa''': (
        '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json'''
    ),
    '''allenai/longformer-base-4096-extra.pos.embd.only''': (
        '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json'''
    ),
    '''allenai/longformer-large-4096-extra.pos.embd.only''': (
        '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json'''
    ),
}



class    lowercase_		(SCREAMING_SNAKE_CASE__ ):
						lowerCAmelCase__			      ="longformer"



						def __init__(			self	:   Optional[Any]					,							snake_case__	:   Union[List[int], int] = 5_12					,							snake_case__	:   int = 2					,							snake_case__	:   int = 1					,							snake_case__	:   int = 0					,							snake_case__	:   int = 2					,							snake_case__	:   int = 3_05_22					,							snake_case__	:   int = 7_68					,							snake_case__	:   int = 12					,							snake_case__	:   int = 12					,							snake_case__	:   int = 30_72					,							snake_case__	:   str = "gelu"					,							snake_case__	:   float = 0.1					,							snake_case__	:   float = 0.1					,							snake_case__	:   int = 5_12					,							snake_case__	:   int = 2					,							snake_case__	:   float = 0.02					,							snake_case__	:   float = 1e-12					,							snake_case__	:   bool = False					,							**snake_case__	:   Tuple					,							):

								"""simple docstring"""

								super().__init__(pad_token_id=snake_case__					,							**snake_case__  )

								SCREAMING_SNAKE_CASE_					      =							attention_window
								SCREAMING_SNAKE_CASE_					      =							sep_token_id
								SCREAMING_SNAKE_CASE_					      =							bos_token_id
								SCREAMING_SNAKE_CASE_					      =							eos_token_id
								SCREAMING_SNAKE_CASE_					      =							vocab_size
								SCREAMING_SNAKE_CASE_					      =							hidden_size
								SCREAMING_SNAKE_CASE_					      =							num_hidden_layers
								SCREAMING_SNAKE_CASE_					      =							num_attention_heads
								SCREAMING_SNAKE_CASE_					      =							hidden_act
								SCREAMING_SNAKE_CASE_					      =							intermediate_size
								SCREAMING_SNAKE_CASE_					      =							hidden_dropout_prob
								SCREAMING_SNAKE_CASE_					      =							attention_probs_dropout_prob
								SCREAMING_SNAKE_CASE_					      =							max_position_embeddings
								SCREAMING_SNAKE_CASE_					      =							type_vocab_size
								SCREAMING_SNAKE_CASE_					      =							initializer_range
								SCREAMING_SNAKE_CASE_					      =							layer_norm_eps
								SCREAMING_SNAKE_CASE_					      =							onnx_export



class    lowercase_		(SCREAMING_SNAKE_CASE__ ):



						def __init__(			self	:   Dict					,							snake_case__	:   "PretrainedConfig"					,							snake_case__	:   str = "default"					,							snake_case__	:   "List[PatchingSpec]" = None  ):

								"""simple docstring"""

								super().__init__(snake_case__					,							snake_case__					,							snake_case__  )
								SCREAMING_SNAKE_CASE_					      =							True



						@property
						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								if self.task == "multiple-choice":
										SCREAMING_SNAKE_CASE_					      =							{0: 'batch', 1: 'choice', 2: 'sequence'}
								else:
										SCREAMING_SNAKE_CASE_					      =							{0: 'batch', 1: 'sequence'}
								return OrderedDict(
								    [
								        ('input_ids', dynamic_axis),
								        ('attention_mask', dynamic_axis),
								        ('global_attention_mask', dynamic_axis),
								    ]  )



						@property
						def 	__a					(			self	:   Optional[int]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							super().outputs
								if self.task == "default":
										SCREAMING_SNAKE_CASE_					      =							{0: 'batch'}
								return outputs



						@property
						def 	__a					(			self	:   Optional[int]  ):

								"""simple docstring"""

								return 1e-4



						@property
						def 	__a					(			self	:   List[str]  ):

								"""simple docstring"""

								return max(super().default_onnx_opset					,							14  )






						def 	__a					(			self	:   List[str]					,							snake_case__	:   "PreTrainedTokenizerBase"					,							snake_case__	:   int = -1					,							snake_case__	:   int = -1					,							snake_case__	:   bool = False					,							snake_case__	:   Optional[TensorType] = None					,							):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							super().generate_dummy_inputs(
								    preprocessor=snake_case__					,							batch_size=snake_case__					,							seq_length=snake_case__					,							is_pair=snake_case__					,							framework=snake_case__  )
								import torch

								# for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64)
								# makes the export fail randomly
								SCREAMING_SNAKE_CASE_					      =							torch.zeros_like(inputs['input_ids']  )
								# make every second token global
								SCREAMING_SNAKE_CASE_					      =							1

								return inputs