

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_       (    a_	):



      def 		UpperCamelCase_						(     self :							str						)       ->	Optional[int]:
        _snake_case		=				self.config_class(**self.inputs_dict						)
        self.parent.assertTrue(hasattr(snake_case__     ,   '''hidden_sizes'''						)						)
        self.parent.assertTrue(hasattr(snake_case__     ,   '''num_encoder_blocks'''						)						)



class     lowercase_       :



      def __init__(     self :							Optional[int]     ,   A__ :							Optional[int]     ,   A__ :							List[str]=13     ,   A__ :							Dict=64     ,   A__ :							int=3     ,   A__ :							str=4     ,   A__ :							Optional[Any]=[2, 2, 2, 2]     ,   A__ :							int=[8, 4, 2, 1]     ,   A__ :							str=[16, 32, 64, 128]     ,   A__ :							Optional[int]=[1, 4, 8, 16]     ,   A__ :							List[Any]=False     ,   A__ :							List[str]=True     ,   A__ :							Optional[Any]="gelu"     ,   A__ :							Tuple=0.1     ,   A__ :							str=0.02     ,   A__ :							Union[str, Any]=3     ,   A__ :							Optional[Any]=None     ,   )       ->	Dict:
        _snake_case		=				parent
        _snake_case		=				batch_size
        _snake_case		=				image_size
        _snake_case		=				num_channels
        _snake_case		=				num_encoder_blocks
        _snake_case		=				sr_ratios
        _snake_case		=				depths
        _snake_case		=				hidden_sizes
        _snake_case		=				downsampling_rates
        _snake_case		=				is_training
        _snake_case		=				use_labels
        _snake_case		=				hidden_act
        _snake_case		=				hidden_dropout_prob
        _snake_case		=				initializer_range
        _snake_case		=				num_labels
        _snake_case		=				scope



      def 		UpperCamelCase_						(     self :							Union[str, Any]						)       ->	List[Any]:
        _snake_case		=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

        _snake_case		=				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 		UpperCamelCase_						(     self :							int     ,   A__ :							Tuple     ,   A__ :							str     ,   A__ :							List[str]						)       ->	Optional[Any]:
        _snake_case		=				PoolFormerModel(config=snake_case__						)
        model.to(snake_case__						)
        model.eval()
        _snake_case		=				model(snake_case__						)
        _snake_case		=				_snake_case		=				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 		UpperCamelCase_						(     self :							List[str]						)       ->	str:
        _snake_case		=				self.prepare_config_and_inputs()
        _snake_case,	_snake_case,	_snake_case		=				config_and_inputs
        _snake_case		=				{'''pixel_values''': pixel_values}
        return config, inputs_dict



@require_torch
class     lowercase_       (    a_  ,				a_  ,				unittest.TestCase	):
      UpperCamelCase_     :						Union[str, Any]		 =							(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
      UpperCamelCase_     :						Dict		 =							(
          {"feature-extraction": PoolFormerModel, "image-classification": PoolFormerForImageClassification}
          if is_torch_available()
          else {}
      )

      UpperCamelCase_     :						Dict		 =							False
      UpperCamelCase_     :						List[Any]		 =							False
      UpperCamelCase_     :						Optional[int]		 =							False
      UpperCamelCase_     :						Dict		 =							False
      UpperCamelCase_     :						Optional[Any]		 =							False



      def 		UpperCamelCase_						(     self :							Any						)       ->	str:
        _snake_case		=				PoolFormerModelTester(self						)
        _snake_case		=				PoolFormerConfigTester(self     ,   config_class=snake_case__						)



      def 		UpperCamelCase_						(     self :							List[Any]						)       ->	str:
        self.config_tester.run_common_tests()



      def 		UpperCamelCase_						(     self :							List[str]						)       ->	Dict:
        _snake_case		=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case__						)



      @unittest.skip('''PoolFormer does not use inputs_embeds'''						)
      def 		UpperCamelCase_						(     self :							Dict						)       ->	str:
        pass



      @unittest.skip('''PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'''						)
      def 		UpperCamelCase_						(     self :							Dict						)       ->	Optional[Any]:
        pass



      def 		UpperCamelCase_						(     self :							int						)       ->	Dict:


        def check_hidden_states_output(A__ :							List[Any]     ,   A__ :							int     ,   A__ :							int						):
          _snake_case		=				model_class(snake_case__						)
          model.to(snake_case__						)
          model.eval()

          with torch.no_grad():
            _snake_case		=				model(**self._prepare_for_class(snake_case__     ,   snake_case__						)						)

          _snake_case		=				outputs.hidden_states

          _snake_case		=				self.model_tester.num_encoder_blocks
          self.assertEqual(len(snake_case__						)     ,   snake_case__						)

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

        _snake_case,	_snake_case		=				self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          _snake_case		=				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"]
          _snake_case		=				True

          check_hidden_states_output(snake_case__     ,   snake_case__     ,   snake_case__						)



      def 		UpperCamelCase_						(     self :							int						)       ->	List[Any]:


        if not self.model_tester.is_training:
          return

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

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



      def 		UpperCamelCase_						(     self :							Tuple						)       ->	Any:
        _snake_case,	_snake_case		=				self.model_tester.prepare_config_and_inputs_for_common()

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

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







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


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



def  snake_case_()				->      Optional[int]:



  """simple docstring"""






  _snake_case		=				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
  return image



@require_torch
class     lowercase_       (    unittest.TestCase	):



      @slow
      def 		UpperCamelCase_						(     self :							Dict						)       ->	int:
        _snake_case		=				PoolFormerImageProcessor()
        _snake_case		=				PoolFormerForImageClassification.from_pretrained('''sail/poolformer_s12'''						).to(snake_case__						)

        _snake_case		=				image_processor(images=prepare_img()     ,   return_tensors='''pt'''						).to(snake_case__						)

        # forward pass
        with torch.no_grad():
          _snake_case		=				model(**snake_case__						)

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

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






from typing import TYPE_CHECKING

from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


UpperCAmelCase :				List[Any]							={
    """configuration_retribert""": ["""RETRIBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RetriBertConfig"""],
    """tokenization_retribert""": ["""RetriBertTokenizer"""],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase :				int							=["""RetriBertTokenizerFast"""]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase :				str							=[
	    """RETRIBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """RetriBertModel""",
	    """RetriBertPreTrainedModel""",
	]


if TYPE_CHECKING:
	from .configuration_retribert import RETRIBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RetriBertConfig
	from .tokenization_retribert import RetriBertTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_retribert_fast import RetriBertTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_retribert import (
		    RETRIBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    RetriBertModel,
		    RetriBertPreTrainedModel,
		)

else:
	import sys

	UpperCAmelCase :				List[Any]							=_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
