



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 		_lowercase		(							UpperCAmelCase__						):

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


    """simple docstring"""






    a							=		self.config_class(**self.inputs_dict				)
    self.parent.assertTrue(hasattr(__lowerCAmelCase				, "hidden_sizes"				)				)
    self.parent.assertTrue(hasattr(__lowerCAmelCase				, "num_encoder_blocks"				)				)



class 		_lowercase		:

   def __init__(   self      :							str				, __lowerCAmelCase      :							Union[str, Any]				, __lowerCAmelCase      :							Optional[Any]=13				, __lowerCAmelCase      :							Union[str, Any]=64				, __lowerCAmelCase      :							Dict=3				, __lowerCAmelCase      :							List[Any]=4				, __lowerCAmelCase      :							Optional[int]=[2, 2, 2, 2]				, __lowerCAmelCase      :							int=[8, 4, 2, 1]				, __lowerCAmelCase      :							Optional[int]=[16, 32, 64, 128]				, __lowerCAmelCase      :							str=[1, 4, 8, 16]				, __lowerCAmelCase      :							Union[str, Any]=False				, __lowerCAmelCase      :							Optional[Any]=True				, __lowerCAmelCase      :							List[Any]="gelu"				, __lowerCAmelCase      :							Union[str, Any]=0.1				, __lowerCAmelCase      :							Tuple=0.0_2				, __lowerCAmelCase      :							Union[str, Any]=3				, __lowerCAmelCase      :							Dict=None				, )							->					Union[str, Any]:


    """simple docstring"""






    a							=		parent
    a							=		batch_size
    a							=		image_size
    a							=		num_channels
    a							=		num_encoder_blocks
    a							=		sr_ratios
    a							=		depths
    a							=		hidden_sizes
    a							=		downsampling_rates
    a							=		is_training
    a							=		use_labels
    a							=		hidden_act
    a							=		hidden_dropout_prob
    a							=		initializer_range
    a							=		num_labels
    a							=		scope

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


    """simple docstring"""






    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.image_size, self.image_size]				, self.num_labels				)

    a							=		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 		A				(   self      :							List[str]				, __lowerCAmelCase      :							Union[str, Any]				, __lowerCAmelCase      :							List[str]				, __lowerCAmelCase      :							List[str]				)							->					List[Any]:


    """simple docstring"""






    a							=		PoolFormerModel(config=__lowerCAmelCase				)
    model.to(__lowerCAmelCase				)
    model.eval()
    a							=		model(__lowerCAmelCase				)
    a							=		a							=		self.image_size // 3_2.0
    self.parent.assertEqual(
        result.last_hidden_state.shape				, (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)				)




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


    """simple docstring"""






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



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

   _UpperCAmelCase  						=      False
   _UpperCAmelCase  						=      False
   _UpperCAmelCase  						=      False
   _UpperCAmelCase  						=      False
   _UpperCAmelCase  						=      False

   def 		A				(   self      :							Dict				)							->					List[str]:


    """simple docstring"""






    a							=		PoolFormerModelTester(self				)
    a							=		PoolFormerConfigTester(self				, config_class=__lowerCAmelCase				)

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


    """simple docstring"""






    self.config_tester.run_common_tests()

   def 		A				(   self      :							str				)							->					Any:


    """simple docstring"""






    a							=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*__lowerCAmelCase				)

   @unittest.skip("PoolFormer does not use inputs_embeds"				)
   def 		A				(   self      :							Any				)							->					Optional[Any]:


    """simple docstring"""






    pass

   @unittest.skip("PoolFormer does not have get_input_embeddings method and get_output_embeddings methods"				)
   def 		A				(   self      :							Tuple				)							->					Optional[int]:


    """simple docstring"""






    pass

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


    """simple docstring"""






    def check_hidden_states_output(__lowerCAmelCase      :							int				, __lowerCAmelCase      :							Optional[int]				, __lowerCAmelCase      :							Optional[int]				):
     a							=		model_class(__lowerCAmelCase				)
     model.to(__lowerCAmelCase				)
     model.eval()

     with torch.no_grad():
      a							=		model(**self._prepare_for_class(__lowerCAmelCase				, __lowerCAmelCase				)				)

     a							=		outputs.hidden_states

     a							=		self.model_tester.num_encoder_blocks
     self.assertEqual(len(__lowerCAmelCase				)				, __lowerCAmelCase				)

     # 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,
         ]				, )

    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(__lowerCAmelCase				, __lowerCAmelCase				, __lowerCAmelCase				)

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

     check_hidden_states_output(__lowerCAmelCase				, __lowerCAmelCase				, __lowerCAmelCase				)

   def 		A				(   self      :							Tuple				)							->					Any:


    """simple docstring"""






    if not self.model_tester.is_training:
     return

    a		,	a							=		self.model_tester.prepare_config_and_inputs_for_common()
    a							=		True

    for model_class in self.all_model_classes:
     if model_class in get_values(__lowerCAmelCase				):
      continue
     a							=		model_class(__lowerCAmelCase				)
     model.to(__lowerCAmelCase				)
     model.train()
     a							=		self._prepare_for_class(__lowerCAmelCase				, __lowerCAmelCase				, return_labels=__lowerCAmelCase				)
     a							=		model(**__lowerCAmelCase				).loss
     loss.backward()

   def 		A				(   self      :							Dict				)							->					Optional[Any]:


    """simple docstring"""






    a		,	a							=		self.model_tester.prepare_config_and_inputs_for_common()

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

     a							=		["pixel_values"]
     self.assertListEqual(arg_names[:1]				, __lowerCAmelCase				)




   @slow
   def 		A				(   self      :							Tuple				)							->					Any:


    """simple docstring"""






    for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     a							=		PoolFormerModel.from_pretrained(__lowerCAmelCase				)
     self.assertIsNotNone(__lowerCAmelCase				)


def 						UpperCAmelCase__   (   ):






 '''simple docstring'''

 a							=		Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"       )
 return image




@require_torch
class 		_lowercase		(							unittest.TestCase						):

   @slow
   def 		A				(   self      :							Any				)							->					str:


    """simple docstring"""






    a							=		PoolFormerImageProcessor()
    a							=		PoolFormerForImageClassification.from_pretrained("sail/poolformer_s12"				).to(__lowerCAmelCase				)

    a							=		image_processor(images=prepare_img()				, return_tensors="pt"				).to(__lowerCAmelCase				)

    # forward pass
    with torch.no_grad():
     a							=		model(**__lowerCAmelCase				)

    # verify the logits
    a							=		torch.Size((1, 1000)				)
    self.assertEqual(outputs.logits.shape				, __lowerCAmelCase				)

    a							=		torch.tensor([-0.6_1_1_3, 0.1_6_8_5, -0.0_4_9_2]				).to(__lowerCAmelCase				)
    self.assertTrue(torch.allclose(outputs.logits[0, :3]				, __lowerCAmelCase				, atol=1E-4				)				)






import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_deberta import DebertaTokenizer


if TYPE_CHECKING:
			from transformers.pipelines.conversational import Conversation


UpperCamelCase__			=    logging.get_logger(__name__)

UpperCamelCase__			=    {"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"}

UpperCamelCase__			=    {
    "vocab_file": {
        "microsoft/deberta-base": "https://huggingface.co/microsoft/deberta-base/resolve/main/vocab.json",
        "microsoft/deberta-large": "https://huggingface.co/microsoft/deberta-large/resolve/main/vocab.json",
        "microsoft/deberta-xlarge": "https://huggingface.co/microsoft/deberta-xlarge/resolve/main/vocab.json",
        "microsoft/deberta-base-mnli": "https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/vocab.json",
        "microsoft/deberta-large-mnli": "https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/vocab.json",
        "microsoft/deberta-xlarge-mnli": (
            "https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/vocab.json"
        ),
    },
    "merges_file": {
        "microsoft/deberta-base": "https://huggingface.co/microsoft/deberta-base/resolve/main/merges.txt",
        "microsoft/deberta-large": "https://huggingface.co/microsoft/deberta-large/resolve/main/merges.txt",
        "microsoft/deberta-xlarge": "https://huggingface.co/microsoft/deberta-xlarge/resolve/main/merges.txt",
        "microsoft/deberta-base-mnli": "https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/merges.txt",
        "microsoft/deberta-large-mnli": "https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/merges.txt",
        "microsoft/deberta-xlarge-mnli": (
            "https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/merges.txt"
        ),
    },
}

UpperCamelCase__			=    {
    "microsoft/deberta-base": 5_12,
    "microsoft/deberta-large": 5_12,
    "microsoft/deberta-xlarge": 5_12,
    "microsoft/deberta-base-mnli": 5_12,
    "microsoft/deberta-large-mnli": 5_12,
    "microsoft/deberta-xlarge-mnli": 5_12,
}

UpperCamelCase__			=    {
    "microsoft/deberta-base": {"do_lower_case": False},
    "microsoft/deberta-large": {"do_lower_case": False},
}
class       _lowerCAmelCase (			_UpperCAmelCase      ):






			"""simple docstring"""





			lowercase__   :	Dict  			=      VOCAB_FILES_NAMES
			lowercase__   :	Dict  			=      PRETRAINED_VOCAB_FILES_MAP
			lowercase__   :	str  			=      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			lowercase__   :	Tuple  			=      ["""input_ids""", """attention_mask""", """token_type_ids"""]
			lowercase__   :	List[Any]  			=      DebertaTokenizer





			def __init__(					self						:							Dict      ,  lowercase						:							Union[str, Any]=None      ,  lowercase						:							Union[str, Any]=None      ,  lowercase						:							Any=None      ,  lowercase						:							Optional[Any]="replace"      ,  lowercase						:							Optional[Any]="[CLS]"      ,  lowercase						:							List[Any]="[SEP]"      ,  lowercase						:							int="[SEP]"      ,  lowercase						:							str="[CLS]"      ,  lowercase						:							Any="[UNK]"      ,  lowercase						:							List[Any]="[PAD]"      ,  lowercase						:							List[str]="[MASK]"      ,  lowercase						:							Tuple=False      ,  **lowercase						:							Tuple      ,  )    ->       List[Any]:
									"""simple docstring"""







									super().__init__(
									    lowercase      ,  lowercase      ,  tokenizer_file=lowercase      ,  errors=lowercase      ,  bos_token=lowercase      ,  eos_token=lowercase      ,  unk_token=lowercase      ,  sep_token=lowercase      ,  cls_token=lowercase      ,  pad_token=lowercase      ,  mask_token=lowercase      ,  add_prefix_space=lowercase      ,  **lowercase      ,  )
									__lowercase											=					kwargs.pop("""add_bos_token"""      ,  lowercase )

									__lowercase											=					json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() )
									if pre_tok_state.get("""add_prefix_space"""      ,  lowercase ) != add_prefix_space:
															__lowercase											=					getattr(lowercase      ,  pre_tok_state.pop("""type""" ) )
															__lowercase											=					add_prefix_space
															__lowercase											=					pre_tok_class(**lowercase )

									__lowercase											=					add_prefix_space





			@property
			def 		snake_case__      (					self						:							Tuple )    ->       str:
									"""simple docstring"""







									if self._mask_token is None:
															if self.verbose:
																					logger.error("""Using mask_token, but it is not set yet.""" )
															return None
									return str(self._mask_token )





			@mask_token.setter
			def 		snake_case__      (					self						:							List[Any]      ,  lowercase						:							Tuple )    ->       Optional[int]:
									"""simple docstring"""







									__lowercase											=					AddedToken(lowercase      ,  lstrip=lowercase      ,  rstrip=lowercase ) if isinstance(lowercase      ,  lowercase ) else value
									__lowercase											=					value





			def 		snake_case__      (					self						:							List[str]      ,  lowercase						:							List[int]      ,  lowercase						:							Optional[List[int]] = None )    ->       List[int]:
									"""simple docstring"""







									if token_ids_a is None:
															return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
									__lowercase											=					[self.cls_token_id]
									__lowercase											=					[self.sep_token_id]
									return cls + token_ids_a + sep + token_ids_a + sep





			def 		snake_case__      (					self						:							Dict      ,  lowercase						:							List[int]      ,  lowercase						:							Optional[List[int]] = None )    ->       List[int]:
									"""simple docstring"""







									__lowercase											=					[self.sep_token_id]
									__lowercase											=					[self.cls_token_id]

									if token_ids_a is None:
															return len(cls + token_ids_a + sep ) * [0]
									return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]





			def 		snake_case__      (					self						:							Optional[int]      ,  *lowercase						:							List[str]      ,  **lowercase						:							Tuple )    ->       BatchEncoding:
									"""simple docstring"""







									__lowercase											=					kwargs.get("""is_split_into_words"""      ,  lowercase )
									assert self.add_prefix_space or not is_split_into_words, (
									    F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
									    "to use it with pretokenized inputs."
									)

									return super()._batch_encode_plus(*lowercase      ,  **lowercase )





			def 		snake_case__      (					self						:							List[Any]      ,  *lowercase						:							str      ,  **lowercase						:							List[Any] )    ->       BatchEncoding:
									"""simple docstring"""







									__lowercase											=					kwargs.get("""is_split_into_words"""      ,  lowercase )

									assert self.add_prefix_space or not is_split_into_words, (
									    F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
									    "to use it with pretokenized inputs."
									)

									return super()._encode_plus(*lowercase      ,  **lowercase )





			def 		snake_case__      (					self						:							Union[str, Any]      ,  lowercase						:							str      ,  lowercase						:							Optional[str] = None )    ->       Tuple[str]:
									"""simple docstring"""







									__lowercase											=					self._tokenizer.model.save(lowercase      ,  name=lowercase )
									return tuple(lowercase )







			def 		snake_case__      (					self						:							List[str]      ,  lowercase						:							"Conversation" )    ->       List[int]:
									"""simple docstring"""







									__lowercase											=					[]
									for is_user, text in conversation.iter_texts():
															input_ids.extend(self.encode(lowercase      ,  add_special_tokens=lowercase ) + [self.eos_token_id] )

									if len(lowercase ) > self.model_max_length:
															__lowercase											=					input_ids[-self.model_max_length :]
									return input_ids





