


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





  """simple docstring"""




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







   """simple docstring"""
   lowerCamelCase_								=  parent
   lowerCamelCase_								=  batch_size
   lowerCamelCase_								=  image_size
   lowerCamelCase_								=  patch_size
   lowerCamelCase_								=  num_channels
   lowerCamelCase_								=  is_training
   lowerCamelCase_								=  use_labels
   lowerCamelCase_								=  hidden_size
   lowerCamelCase_								=  num_hidden_layers
   lowerCamelCase_								=  num_attention_heads
   lowerCamelCase_								=  intermediate_size
   lowerCamelCase_								=  hidden_act
   lowerCamelCase_								=  hidden_dropout_prob
   lowerCamelCase_								=  attention_probs_dropout_prob
   lowerCamelCase_								=  type_sequence_label_size
   lowerCamelCase_								=  initializer_range
   lowerCamelCase_								=  encoder_stride
   lowerCamelCase_								=  num_attention_outputs
   lowerCamelCase_								=  embed_dim
   lowerCamelCase_								=  embed_dim + 1
   lowerCamelCase_								=  resolution
   lowerCamelCase_								=  depths
   lowerCamelCase_								=  hidden_sizes
   lowerCamelCase_								=  dim
   lowerCamelCase_								=  mlp_expansion_ratio






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

   lowerCamelCase_								=  self.get_config()
   return config, pixel_values, labels






  def   snake_case		(   self					):







   """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=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   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase					):







   """simple docstring"""
   lowerCamelCase_								=  EfficientFormerModel(config=UpperCamelCase					)
   model.to(UpperCamelCase					)
   model.eval()
   lowerCamelCase_								=  model(UpperCamelCase					)
   self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size)					)






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase					):







   """simple docstring"""
   lowerCamelCase_								=  self.type_sequence_label_size
   lowerCamelCase_								=  EfficientFormerForImageClassification(UpperCamelCase					)
   model.to(UpperCamelCase					)
   model.eval()
   lowerCamelCase_								=  model(UpperCamelCase      ,  labels=UpperCamelCase					)
   self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.type_sequence_label_size)					)

   # test greyscale images
   lowerCamelCase_								=  1
   lowerCamelCase_								=  EfficientFormerForImageClassification(UpperCamelCase					)
   model.to(UpperCamelCase					)
   model.eval()

   lowerCamelCase_								=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
   lowerCamelCase_								=  model(UpperCamelCase					)
   self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.type_sequence_label_size)					)







  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.prepare_config_and_inputs()
   (
       (
       lowerCamelCase_
   )			,(
       lowerCamelCase_
   )			,(
       lowerCamelCase_
   )			,
   )								=  config_and_inputs
   lowerCamelCase_								=  {"pixel_values": pixel_values}
   return config, inputs_dict





@require_torch
class 					snake_case   (   lowercase      ,						lowercase      ,						unittest.TestCase      ):





  """simple docstring"""




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

  _lowerCamelCase		   =  False
  _lowerCamelCase		   =  False
  _lowerCamelCase		   =  False






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  EfficientFormerModelTester(self					)
   lowerCamelCase_								=  ConfigTester(
       self      ,  config_class=UpperCamelCase      ,  has_text_modality=UpperCamelCase      ,  hidden_size=37					)






  def   snake_case		(   self					):







   """simple docstring"""
   self.config_tester.run_common_tests()






  @unittest.skip(reason="EfficientFormer does not use inputs_embeds"					)
  def   snake_case		(   self					):







   """simple docstring"""
   pass






  @unittest.skip(reason="EfficientFormer does not support input and output embeddings"					)
  def   snake_case		(   self					):







   """simple docstring"""
   pass






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_			,lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs_for_common()

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

    lowerCamelCase_								=  ["pixel_values"]
    self.assertListEqual(arg_names[:1]      ,  UpperCamelCase					)






  def   snake_case		(   self					):







   """simple docstring"""
   def check_hidden_states_output(UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase					):
    lowerCamelCase_								=  model_class(UpperCamelCase					)
    model.to(UpperCamelCase					)
    model.eval()

    with torch.no_grad():
     lowerCamelCase_								=  model(**self._prepare_for_class(UpperCamelCase      ,  UpperCamelCase					)					)

    lowerCamelCase_								=  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

     self.assertIsInstance(UpperCamelCase      ,  (list, tuple)					)
     self.assertEqual(len(UpperCamelCase					)      ,  UpperCamelCase					)
     lowerCamelCase_								=  getattr(self.model_tester      ,  "seq_length"      ,  UpperCamelCase					)
     lowerCamelCase_								=  getattr(self.model_tester      ,  "decoder_seq_length"      ,  UpperCamelCase					)

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

   lowerCamelCase_			,lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
    lowerCamelCase_								=  True
    check_hidden_states_output(UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase					)

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

    check_hidden_states_output(UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase					)






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase=False					):







   """simple docstring"""
   lowerCamelCase_								=  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   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  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   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*UpperCamelCase					)






  def   snake_case		(   self					):







   """simple docstring"""
   if not self.model_tester.is_training:
    return

   lowerCamelCase_			,lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs_for_common()
   lowerCamelCase_								=  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
    lowerCamelCase_								=  model_class(UpperCamelCase					)
    model.to(UpperCamelCase					)
    model.train()
    lowerCamelCase_								=  self._prepare_for_class(UpperCamelCase      ,  UpperCamelCase      ,  return_labels=UpperCamelCase					)
    lowerCamelCase_								=  model(**UpperCamelCase					).loss
    loss.backward()






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_			,lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs_for_common()

   lowerCamelCase_								=  [
       {"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"]}'''					):
      lowerCamelCase_								=  problem_type["title"]
      lowerCamelCase_								=  problem_type["num_labels"]

      lowerCamelCase_								=  model_class(UpperCamelCase					)
      model.to(UpperCamelCase					)
      model.train()

      lowerCamelCase_								=  self._prepare_for_class(UpperCamelCase      ,  UpperCamelCase      ,  return_labels=UpperCamelCase					)

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

      lowerCamelCase_								=  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:
       lowerCamelCase_								=  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   snake_case		(   self					):







   """simple docstring"""
   for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    lowerCamelCase_								=  EfficientFormerModel.from_pretrained(UpperCamelCase					)
    self.assertIsNotNone(UpperCamelCase					)







  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_			,lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs_for_common()
   lowerCamelCase_								=  True

   lowerCamelCase_								=  getattr(self.model_tester      ,  "seq_length"      ,  UpperCamelCase					)
   lowerCamelCase_								=  getattr(self.model_tester      ,  "encoder_seq_length"      ,  UpperCamelCase					)
   lowerCamelCase_								=  getattr(self.model_tester      ,  "key_length"      ,  UpperCamelCase					)
   lowerCamelCase_								=  getattr(self.model_tester      ,  "chunk_length"      ,  UpperCamelCase					)

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

   for model_class in self.all_model_classes:
    lowerCamelCase_								=  True
    lowerCamelCase_								=  False
    lowerCamelCase_								=  True
    lowerCamelCase_								=  model_class(UpperCamelCase					)
    model.to(UpperCamelCase					)
    model.eval()
    with torch.no_grad():
     lowerCamelCase_								=  model(**self._prepare_for_class(UpperCamelCase      ,  UpperCamelCase					)					)
    lowerCamelCase_								=  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"]
    lowerCamelCase_								=  True
    lowerCamelCase_								=  model_class(UpperCamelCase					)
    model.to(UpperCamelCase					)
    model.eval()
    with torch.no_grad():
     lowerCamelCase_								=  model(**self._prepare_for_class(UpperCamelCase      ,  UpperCamelCase					)					)
    lowerCamelCase_								=  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 __snake_case (				):
 lowerCamelCase_								=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
 return image







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





  """simple docstring"""




  @cached_property
  def   snake_case		(   self					):







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






  @slow
  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  EfficientFormerForImageClassification.from_pretrained("snap-research/efficientformer-l1-300"					).to(
       UpperCamelCase					)

   lowerCamelCase_								=  self.default_image_processor
   lowerCamelCase_								=  prepare_img()
   lowerCamelCase_								=  image_processor(images=UpperCamelCase      ,  return_tensors="pt"					).to(UpperCamelCase					)

   # forward pass
   with torch.no_grad():
    lowerCamelCase_								=  model(**UpperCamelCase					)

   # verify the logits
   lowerCamelCase_								=  (1, 1000)
   self.assertEqual(outputs.logits.shape      ,  UpperCamelCase					)

   lowerCamelCase_								=  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   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  EfficientFormerForImageClassificationWithTeacher.from_pretrained(
       "snap-research/efficientformer-l1-300"					).to(UpperCamelCase					)

   lowerCamelCase_								=  self.default_image_processor
   lowerCamelCase_								=  prepare_img()
   lowerCamelCase_								=  image_processor(images=UpperCamelCase      ,  return_tensors="pt"					).to(UpperCamelCase					)

   # forward pass
   with torch.no_grad():
    lowerCamelCase_								=  model(**UpperCamelCase					)

   # verify the logits
   lowerCamelCase_								=  (1, 1000)
   self.assertEqual(outputs.logits.shape      ,  UpperCamelCase					)

   lowerCamelCase_								=  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					)					)







'''simple docstring'''

import unittest

import numpy as np

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
     import tensorflow as tf

     from transformers.activations_tf import get_tf_activation





@require_tf
class 					snake_case   (   unittest.TestCase      ):





  """simple docstring"""




  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  tf.constant([-100, -1.0, -0.1, 0, 0.1, 1.0, 100.0]					)
   lowerCamelCase_								=  get_tf_activation("gelu"					)
   lowerCamelCase_								=  get_tf_activation("gelu_10"					)

   lowerCamelCase_								=  gelu(UpperCamelCase					)
   lowerCamelCase_								=  geluaa(UpperCamelCase					)

   lowerCamelCase_								=  tf.where(y_gelu_aa < 10.0      ,  1.0      ,  0.0					)

   self.assertEqual(tf.math.reduce_max(UpperCamelCase					).numpy().item()      ,  10.0					)
   self.assertTrue(np.allclose(y_gelu * clipped_mask      ,  y_gelu_aa * clipped_mask					)					)







  def   snake_case		(   self					):







   """simple docstring"""
   get_tf_activation("gelu"					)
   get_tf_activation("gelu_10"					)
   get_tf_activation("gelu_fast"					)
   get_tf_activation("gelu_new"					)
   get_tf_activation("glu"					)
   get_tf_activation("mish"					)
   get_tf_activation("quick_gelu"					)
   get_tf_activation("relu"					)
   get_tf_activation("sigmoid"					)
   get_tf_activation("silu"					)
   get_tf_activation("swish"					)
   get_tf_activation("tanh"					)
   with self.assertRaises(UpperCamelCase					):
    get_tf_activation("bogus"					)
   with self.assertRaises(UpperCamelCase					):
    get_tf_activation(UpperCamelCase					)



