from __future__ import annotations


def SCREAMING_SNAKE_CASE					(  lowercase_:						int		,    lowercase_:						int		,    lowercase_:						int					):
       assert (
           c % greatest_common_divisor(lowercase_		,    lowercase_					) == 0
       )  # greatest_common_divisor(a,b) function implemented below
       ((_UpperCamelCase)    ,   (_UpperCamelCase)    ,   (_UpperCamelCase))  :							List[Any]	    =	extended_gcd(lowercase_		,    lowercase_					)  # extended_gcd(a,b) function implemented below
       _UpperCamelCase  :							Union[str, Any]	    =	c / d
       return (r * x, r * y)


def SCREAMING_SNAKE_CASE					(  lowercase_:						int		,    lowercase_:						int		,    lowercase_:						int		,    lowercase_:						int = 2					):
       ((_UpperCamelCase)    ,   (_UpperCamelCase))  :							int	    =	diophantine(lowercase_		,    lowercase_		,    lowercase_					)  # Initial value
       _UpperCamelCase  :							Optional[Any]	    =	greatest_common_divisor(lowercase_		,    lowercase_					)
       _UpperCamelCase  :							str	    =	a // d
       _UpperCamelCase  :							Dict	    =	b // d

       for i in range(lowercase_					):
              _UpperCamelCase  :							str	    =	xa + i * q
              _UpperCamelCase  :							Optional[int]	    =	ya - i * p
              print(lowercase_		,    lowercase_					)


def SCREAMING_SNAKE_CASE					(  lowercase_:						int		,    lowercase_:						int					):
       if a < b:
              _UpperCamelCase    ,   _UpperCamelCase  :							Dict	    =	b, a

       while a % b != 0:
              _UpperCamelCase    ,   _UpperCamelCase  :							int	    =	b, a % b

       return b


def SCREAMING_SNAKE_CASE					(  lowercase_:						int		,    lowercase_:						int					):
       assert a >= 0 and b >= 0

       if b == 0:
              _UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase  :							Dict	    =	a, 1, 0
       else:
              ((_UpperCamelCase)    ,   (_UpperCamelCase)    ,   (_UpperCamelCase))  :							Tuple	    =	extended_gcd(lowercase_		,    a % b					)
              _UpperCamelCase  :							Dict	    =	q
              _UpperCamelCase  :							List[str]	    =	p - q * (a // b)

       assert a % d == 0 and b % d == 0
       assert d == a * x + b * y

       return (d, x, y)


if __name__ == "__main__":
 from doctest import testmod

 testmod(name='''diophantine''', verbose=True)
 testmod(name='''diophantine_all_soln''', verbose=True)
 testmod(name='''extended_gcd''', verbose=True)
 testmod(name='''greatest_common_divisor''', verbose=True)











"""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   _lowercase			:


      def __init__(     self					:						Dict				, a					:						Optional[Any]				, a					:						int = 1_3				, a					:						int = 6_4				, a					:						int = 2				, a					:						int = 3				, a					:						int = 3				, a					:						bool = True				, a					:						bool = True				, a					:						int = 1_2_8				, a					:						Optional[Any]=[1_6, 3_2, 6_4, 1_2_8]				, a					:						int = 7				, a					:						int = 4				, a					:						int = 3_7				, a					:						str = "gelu"				, a					:						float = 0.1				, a					:						float = 0.1				, a					:						int = 1_0				, a					:						float = 0.0_2				, a					:						int = 2				, a					:						int = 1				, a					:						int = 1_2_8				, a					:						List[int] = [2, 2, 2, 2]				, a					:						int = 2				, a					:						int = 2				, ):





            """simple docstring"""







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


      def _UpperCamelCase    (     self					:						Tuple    ):





            """simple docstring"""







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

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

            __snake_case   :     Any	      =self.get_config()
            return config, pixel_values, labels


      def _UpperCamelCase    (     self					:						int    ):





            """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					:						List[Any]				, a					:						str				, a					:						Union[str, Any]				, a					:						Dict    ):





            """simple docstring"""







            __snake_case   :     Dict	      =EfficientFormerModel(config=a    )
            model.to(a    )
            model.eval()
            __snake_case   :     Dict	      =model(a    )
            self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, self.seq_length, self.hidden_size)    )


      def _UpperCamelCase    (     self					:						List[str]				, a					:						List[str]				, a					:						Optional[int]				, a					:						List[str]    ):





            """simple docstring"""







            __snake_case   :     List[str]	      =self.type_sequence_label_size
            __snake_case   :     Tuple	      =EfficientFormerForImageClassification(a    )
            model.to(a    )
            model.eval()
            __snake_case   :     int	      =model(a				, labels=a    )
            self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.type_sequence_label_size)    )

            # test greyscale images
            __snake_case   :     Tuple	      =1
            __snake_case   :     List[str]	      =EfficientFormerForImageClassification(a    )
            model.to(a    )
            model.eval()

            __snake_case   :     List[Any]	      =floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
            __snake_case   :     Tuple	      =model(a    )
            self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.type_sequence_label_size)    )







      def _UpperCamelCase    (     self					:						Tuple    ):





            """simple docstring"""







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



@require_torch
class   _lowercase			(  lowerCAmelCase		,	lowerCAmelCase		,	unittest.TestCase    ):
      _a :  Union[str, Any]										= (
          (
              EfficientFormerModel,
              EfficientFormerForImageClassificationWithTeacher,
              EfficientFormerForImageClassification,
          )
          if is_torch_available()
          else ()
      )
      _a :  List[str]										= (
          {
              '''feature-extraction''': EfficientFormerModel,
              '''image-classification''': (
                  EfficientFormerForImageClassification,
                  EfficientFormerForImageClassificationWithTeacher,
              ),
          }
          if is_torch_available()
          else {}
      )
      _a :  Optional[Any]										= False

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


      def _UpperCamelCase    (     self					:						Union[str, Any]    ):





            """simple docstring"""







            __snake_case   :     Dict	      =EfficientFormerModelTester(self    )
            __snake_case   :     Dict	      =ConfigTester(
                self				, config_class=a				, has_text_modality=a				, hidden_size=3_7    )


      def _UpperCamelCase    (     self					:						str    ):





            """simple docstring"""







            self.config_tester.run_common_tests()


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





            """simple docstring"""







            pass


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





            """simple docstring"""







            pass


      def _UpperCamelCase    (     self					:						Tuple    ):





            """simple docstring"""







            __snake_case      ,					__snake_case   :     Optional[int]	      =self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                  __snake_case   :     List[Any]	      =model_class(a    )
                  __snake_case   :     Tuple	      =inspect.signature(model.forward    )
                  # signature.parameters is an OrderedDict => so arg_names order is deterministic
                  __snake_case   :     Dict	      =[*signature.parameters.keys()]

                  __snake_case   :     List[str]	      =['''pixel_values''']
                  self.assertListEqual(arg_names[:1]				, a    )


      def _UpperCamelCase    (     self					:						Dict    ):





            """simple docstring"""







            def check_hidden_states_output(a					:						Any				, a					:						List[Any]				, a					:						int    ):
                  __snake_case   :     List[str]	      =model_class(a    )
                  model.to(a    )
                  model.eval()

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

                  __snake_case   :     List[Any]	      =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

                        self.assertIsInstance(a				, (list, tuple)    )
                        self.assertEqual(len(a    )				, a    )
                        __snake_case   :     List[str]	      =getattr(self.model_tester				, '''seq_length'''				, a    )
                        __snake_case   :     Optional[int]	      =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      ,					__snake_case   :     str	      =self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                  __snake_case   :     str	      =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   :     Dict	      =True

                  check_hidden_states_output(a				, a				, a    )


      def _UpperCamelCase    (     self					:						List[Any]				, a					:						Optional[int]				, a					:						List[Any]				, a					:						int=False    ):





            """simple docstring"""







            __snake_case   :     List[str]	      =super()._prepare_for_class(a				, a				, return_labels=a    )

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

            return inputs_dict


      def _UpperCamelCase    (     self					:						List[str]    ):





            """simple docstring"""







            __snake_case   :     Dict	      =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					:						Any    ):





            """simple docstring"""







            __snake_case   :     int	      =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_image_modeling(*a    )


      def _UpperCamelCase    (     self					:						Any    ):





            """simple docstring"""







            __snake_case   :     int	      =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*a    )


      def _UpperCamelCase    (     self					:						Tuple    ):





            """simple docstring"""







            if not self.model_tester.is_training:
                  return

            __snake_case      ,					__snake_case   :     Optional[Any]	      =self.model_tester.prepare_config_and_inputs_for_common()
            __snake_case   :     Union[str, Any]	      =True

            for model_class in self.all_model_classes:
                  # EfficientFormerForImageClassificationWithTeacher supports inference-only
                  if (
                      model_class in get_values(a    )
                      or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
                  ):
                        continue
                  __snake_case   :     Any	      =model_class(a    )
                  model.to(a    )
                  model.train()
                  __snake_case   :     Optional[int]	      =self._prepare_for_class(a				, a				, return_labels=a    )
                  __snake_case   :     Union[str, Any]	      =model(**a    ).loss
                  loss.backward()


      def _UpperCamelCase    (     self					:						Optional[int]    ):





            """simple docstring"""







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

            __snake_case   :     Union[str, Any]	      =[
                {'''title''': '''multi_label_classification''', '''num_labels''': 2, '''dtype''': torch.float},
                {'''title''': '''single_label_classification''', '''num_labels''': 1, '''dtype''': torch.long},
                {'''title''': '''regression''', '''num_labels''': 1, '''dtype''': torch.float},
            ]

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

                  for problem_type in problem_types:
                        with self.subTest(msg=f'''Testing {model_class} with {problem_type["title"]}'''    ):
                              __snake_case   :     str	      =problem_type['''title''']
                              __snake_case   :     Union[str, Any]	      =problem_type['''num_labels''']

                              __snake_case   :     Optional[int]	      =model_class(a    )
                              model.to(a    )
                              model.train()

                              __snake_case   :     List[str]	      =self._prepare_for_class(a				, a				, return_labels=a    )

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

                              __snake_case   :     str	      =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=a    ) as warning_list:
                                    __snake_case   :     List[str]	      =model(**a    ).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 _UpperCamelCase    (     self					:						List[str]    ):





            """simple docstring"""







            for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  __snake_case   :     Union[str, Any]	      =EfficientFormerModel.from_pretrained(a    )
                  self.assertIsNotNone(a    )







      def _UpperCamelCase    (     self					:						List[Any]    ):





            """simple docstring"""







            __snake_case      ,					__snake_case   :     Optional[int]	      =self.model_tester.prepare_config_and_inputs_for_common()
            __snake_case   :     List[Any]	      =True

            __snake_case   :     Optional[Any]	      =getattr(self.model_tester				, '''seq_length'''				, a    )
            __snake_case   :     Optional[Any]	      =getattr(self.model_tester				, '''encoder_seq_length'''				, a    )
            __snake_case   :     int	      =getattr(self.model_tester				, '''key_length'''				, a    )
            __snake_case   :     Union[str, Any]	      =getattr(self.model_tester				, '''chunk_length'''				, a    )

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

            for model_class in self.all_model_classes:
                  __snake_case   :     List[str]	      =True
                  __snake_case   :     Any	      =False
                  __snake_case   :     Dict	      =True
                  __snake_case   :     int	      =model_class(a    )
                  model.to(a    )
                  model.eval()
                  with torch.no_grad():
                        __snake_case   :     Any	      =model(**self._prepare_for_class(a				, a    )    )
                  __snake_case   :     int	      =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   :     List[Any]	      =model_class(a    )
                  model.to(a    )
                  model.eval()
                  with torch.no_grad():
                        __snake_case   :     List[Any]	      =model(**self._prepare_for_class(a				, a    )    )
                  __snake_case   :     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      __lowercase       (     )       ->       Any:
      __snake_case   :     List[Any]	      =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
      return image


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


      @cached_property
      def _UpperCamelCase    (     self					:						Any    ):





            """simple docstring"""







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


      @slow
      def _UpperCamelCase    (     self					:						List[str]    ):





            """simple docstring"""







            __snake_case   :     int	      =EfficientFormerForImageClassification.from_pretrained('''snap-research/efficientformer-l1-300'''    ).to(
                a    )

            __snake_case   :     Union[str, Any]	      =self.default_image_processor
            __snake_case   :     Tuple	      =prepare_img()
            __snake_case   :     Optional[Any]	      =image_processor(images=a				, return_tensors='''pt'''    ).to(a    )

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

            # verify the logits
            __snake_case   :     Optional[Any]	      =(1, 1_0_0_0)
            self.assertEqual(outputs.logits.shape				, a    )

            __snake_case   :     List[Any]	      =torch.tensor([-0.0_5_5_5, 0.4_8_2_5, -0.0_8_5_2]    ).to(a    )
            self.assertTrue(torch.allclose(outputs.logits[0][:3]				, a				, atol=1e-4    )    )







      @slow
      def _UpperCamelCase    (     self					:						Optional[Any]    ):





            """simple docstring"""







            __snake_case   :     Union[str, Any]	      =EfficientFormerForImageClassificationWithTeacher.from_pretrained(
                '''snap-research/efficientformer-l1-300'''    ).to(a    )

            __snake_case   :     List[str]	      =self.default_image_processor
            __snake_case   :     Optional[Any]	      =prepare_img()
            __snake_case   :     Optional[Any]	      =image_processor(images=a				, return_tensors='''pt'''    ).to(a    )

            # forward pass
            with torch.no_grad():
                  __snake_case   :     List[Any]	      =model(**a    )

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

            __snake_case   :     str	      =torch.tensor([-0.1_3_1_2, 0.4_3_5_3, -1.0_4_9_9]    ).to(a    )
            self.assertTrue(torch.allclose(outputs.logits[0][:3]				, a				, atol=1e-4    )    )


