

'''simple docstring'''






import inspect
import unittest

from transformers import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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_MAPPING, PoolFormerConfig, PoolFormerForImageClassification, PoolFormerModel
      from transformers.models.poolformer.modeling_poolformer import POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import PoolFormerImageProcessor
class 	SCREAMING_SNAKE_CASE__			(				_UpperCAmelCase      ):




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			List[Any]     =       self.config_class(**self.inputs_dict			)
          self.parent.assertTrue(hasattr(lowercase__				,     "hidden_sizes"			)			)
          self.parent.assertTrue(hasattr(lowercase__				,     "num_encoder_blocks"			)			)






class 	SCREAMING_SNAKE_CASE__			:




    def __init__(					self				,     lowercase__				,     lowercase__=13				,     lowercase__=64				,     lowercase__=3				,     lowercase__=4				,     lowercase__=[2, 2, 2, 2]				,     lowercase__=[8, 4, 2, 1]				,     lowercase__=[16, 32, 64, 128]				,     lowercase__=[1, 4, 8, 16]				,     lowercase__=False				,     lowercase__=True				,     lowercase__="gelu"				,     lowercase__=0.1				,     lowercase__=0.02				,     lowercase__=3				,     lowercase__=None				,     ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			str     =       parent
          SCREAMING_SNAKE_CASE_      :			Any     =       batch_size
          SCREAMING_SNAKE_CASE_      :			int     =       image_size
          SCREAMING_SNAKE_CASE_      :			int     =       num_channels
          SCREAMING_SNAKE_CASE_      :			List[Any]     =       num_encoder_blocks
          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       sr_ratios
          SCREAMING_SNAKE_CASE_      :			Dict     =       depths
          SCREAMING_SNAKE_CASE_      :			List[str]     =       hidden_sizes
          SCREAMING_SNAKE_CASE_      :			Any     =       downsampling_rates
          SCREAMING_SNAKE_CASE_      :			List[Any]     =       is_training
          SCREAMING_SNAKE_CASE_      :			Union[str, Any]     =       use_labels
          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       hidden_act
          SCREAMING_SNAKE_CASE_      :			Tuple     =       hidden_dropout_prob
          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       initializer_range
          SCREAMING_SNAKE_CASE_      :			Any     =       num_labels
          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       scope




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






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

          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       None
          if self.use_labels:
                SCREAMING_SNAKE_CASE_      :			Any     =       ids_tensor([self.batch_size, self.image_size, self.image_size]				,     self.num_labels			)

          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       PoolFormerConfig(
              image_size=self.image_size				,     num_channels=self.num_channels				,     num_encoder_blocks=self.num_encoder_blocks				,     depths=self.depths				,     hidden_sizes=self.hidden_sizes				,     hidden_act=self.hidden_act				,     hidden_dropout_prob=self.hidden_dropout_prob				,     initializer_range=self.initializer_range				,     )

          return config, pixel_values, labels




    def   __lowerCamelCase      (					self				,     lowercase__				,     lowercase__				,     lowercase__			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Union[str, Any]     =       PoolFormerModel(config=lowercase__			)
          model.to(lowercase__			)
          model.eval()
          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       model(lowercase__			)
          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       self.image_size // 32.0
          self.parent.assertEqual(
              result.last_hidden_state.shape				,     (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)			)







    def   __lowerCamelCase      (					self			):






          """simple docstring"""






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






@require_torch
class 	SCREAMING_SNAKE_CASE__			(				_UpperCAmelCase,_UpperCAmelCase,unittest.TestCase      ):
    _A			    =							(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
    _A			    =							(
        {"feature-extraction": PoolFormerModel, "image-classification": PoolFormerForImageClassification}
        if is_torch_available()
        else {}
    )

    _A			    =							False
    _A			    =							False
    _A			    =							False
    _A			    =							False
    _A			    =							False




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Any     =       PoolFormerModelTester(self			)
          SCREAMING_SNAKE_CASE_      :			Dict     =       PoolFormerConfigTester(self				,     config_class=lowercase__			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          self.config_tester.run_common_tests()




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowercase__			)




    @unittest.skip("PoolFormer does not use inputs_embeds"			)
    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          pass




    @unittest.skip("PoolFormer does not have get_input_embeddings method and get_output_embeddings methods"			)
    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          pass




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          def check_hidden_states_output(lowercase__				,     lowercase__				,     lowercase__			):
                SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       model_class(lowercase__			)
                model.to(lowercase__			)
                model.eval()

                with torch.no_grad():
                      SCREAMING_SNAKE_CASE_      :			Any     =       model(**self._prepare_for_class(lowercase__				,     lowercase__			)			)

                SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       outputs.hidden_states

                SCREAMING_SNAKE_CASE_      :			List[Any]     =       self.model_tester.num_encoder_blocks
                self.assertEqual(len(lowercase__			)				,     lowercase__			)

                # verify the first hidden states (first block)
                self.assertListEqual(
                    list(hidden_states[0].shape[-3:]			)				,     [
                        self.model_tester.hidden_sizes[0],
                        self.model_tester.image_size // 4,
                        self.model_tester.image_size // 4,
                    ]				,     )

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

          for model_class in self.all_model_classes:
                SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       True
                check_hidden_states_output(lowercase__				,     lowercase__				,     lowercase__			)

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

                check_hidden_states_output(lowercase__				,     lowercase__				,     lowercase__			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          if not self.model_tester.is_training:
                return

          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_      :			Tuple     =       self.model_tester.prepare_config_and_inputs_for_common()
          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       True

          for model_class in self.all_model_classes:
                if model_class in get_values(lowercase__			):
                      continue
                SCREAMING_SNAKE_CASE_      :			Optional[int]     =       model_class(lowercase__			)
                model.to(lowercase__			)
                model.train()
                SCREAMING_SNAKE_CASE_      :			Union[str, Any]     =       self._prepare_for_class(lowercase__				,     lowercase__				,     return_labels=lowercase__			)
                SCREAMING_SNAKE_CASE_      :			Any     =       model(**lowercase__			).loss
                loss.backward()




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_      :			str     =       self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                SCREAMING_SNAKE_CASE_      :			Optional[int]     =       model_class(lowercase__			)
                SCREAMING_SNAKE_CASE_      :			str     =       inspect.signature(model.forward			)
                # signature.parameters is an OrderedDict => so arg_names order is deterministic
                SCREAMING_SNAKE_CASE_      :			List[Any]     =       [*signature.parameters.keys()]

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







    @slow
    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                SCREAMING_SNAKE_CASE_      :			List[str]     =       PoolFormerModel.from_pretrained(lowercase__			)
                self.assertIsNotNone(lowercase__			)






def 					__lowerCamelCase			(      )							->   Union[str, Any]:
      """simple docstring"""

      SCREAMING_SNAKE_CASE_      :			List[str]     =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
      return image





@require_torch
class 	SCREAMING_SNAKE_CASE__			(				unittest.TestCase      ):




    @slow
    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       PoolFormerImageProcessor()
          SCREAMING_SNAKE_CASE_      :			Dict     =       PoolFormerForImageClassification.from_pretrained("sail/poolformer_s12"			).to(lowercase__			)

          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       image_processor(images=prepare_img()				,     return_tensors="pt"			).to(lowercase__			)

          # forward pass
          with torch.no_grad():
                SCREAMING_SNAKE_CASE_      :			Optional[int]     =       model(**lowercase__			)

          # verify the logits
          SCREAMING_SNAKE_CASE_      :			Any     =       torch.Size((1, 1000)			)
          self.assertEqual(outputs.logits.shape				,     lowercase__			)

          SCREAMING_SNAKE_CASE_      :			List[Any]     =       torch.tensor([-0.6113, 0.1685, -0.0492]			).to(lowercase__			)
          self.assertTrue(torch.allclose(outputs.logits[0, :3]				,     lowercase__				,     atol=1e-4			)			)







import os
import unittest

from transformers import MPNetTokenizerFast
from transformers.models.mpnet.tokenization_mpnet import VOCAB_FILES_NAMES, MPNetTokenizer
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class 			a__       (					UpperCamelCase__   ,					unittest.TestCase      ):
     a		:			Dict							=	MPNetTokenizer
     a		:			Dict							=	MPNetTokenizerFast
     a		:			Optional[int]							=	True
     a		:			Any							=	True







     def 			lowerCAmelCase_				(	self )  ->    Any:



          '''simple docstring'''



          super().setUp()

          a      =      [
              "[UNK]",
              "[CLS]",
              "[SEP]",
              "[PAD]",
              "[MASK]",
              "want",
              "##want",
              "##ed",
              "wa",
              "un",
              "runn",
              "##ing",
              ",",
              "low",
              "lowest",
          ]
          a      =      os.path.join(self.tmpdirname  ,		VOCAB_FILES_NAMES["vocab_file"] )
          with open(self.vocab_file  ,		"w"  ,		encoding="utf-8" ) as vocab_writer:
               vocab_writer.write("".join([x + "\n" for x in vocab_tokens] ) )







     def 			lowerCAmelCase_				(	self  ,		A )  ->    Any:



          '''simple docstring'''



          a      =      "UNwant\u00E9d,running"
          a      =      "unwanted, running"
          return input_text, output_text







     def 			lowerCAmelCase_				(	self )  ->    List[Any]:



          '''simple docstring'''



          a      =      self.tokenizer_class(self.vocab_file )

          a      =      tokenizer.tokenize("UNwant\u00E9d,running" )
          self.assertListEqual(A  ,		["un", "##want", "##ed", ",", "runn", "##ing"] )
          self.assertListEqual(tokenizer.convert_tokens_to_ids(A )  ,		[9, 6, 7, 12, 10, 11] )







     @slow
     def 			lowerCAmelCase_				(	self )  ->    Dict:



          '''simple docstring'''



          a      =      self.tokenizer_class.from_pretrained("microsoft/mpnet-base" )

          a      =      tokenizer.encode("sequence builders"  ,		add_special_tokens=A )
          a      =      tokenizer.encode("multi-sequence build"  ,		add_special_tokens=A )

          a      =      tokenizer.build_inputs_with_special_tokens(A )
          a      =      tokenizer.build_inputs_with_special_tokens(A  ,		A )

          assert encoded_sentence == [0] + text + [2]
          assert encoded_pair == [0] + text + [2] + [2] + text_a + [2]

