


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






      def __UpperCAmelCase	(			self      :List[str]     )	-> Optional[int]:




             '''simple docstring'''
             _a   :			Any					 =self.config_class(**self.inputs_dict     )
             self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE					,						"""hidden_sizes"""     )     )
             self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE					,						"""num_encoder_blocks"""     )     )







class A__	:






      def __init__(			self      :Optional[Any]					,						SCREAMING_SNAKE_CASE      :Optional[Any]					,						SCREAMING_SNAKE_CASE      :Dict=1_3					,						SCREAMING_SNAKE_CASE      :int=6_4					,						SCREAMING_SNAKE_CASE      :Optional[int]=3					,						SCREAMING_SNAKE_CASE      :Dict=4					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=[2, 2, 2, 2]					,						SCREAMING_SNAKE_CASE      :List[str]=[8, 4, 2, 1]					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=[1_6, 3_2, 6_4, 1_2_8]					,						SCREAMING_SNAKE_CASE      :int=[1, 4, 8, 1_6]					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=False					,						SCREAMING_SNAKE_CASE      :Any=True					,						SCREAMING_SNAKE_CASE      :List[Any]="gelu"					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=0.1					,						SCREAMING_SNAKE_CASE      :Dict=0.02					,						SCREAMING_SNAKE_CASE      :Any=3					,						SCREAMING_SNAKE_CASE      :int=None					,						)	-> List[Any]:




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






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




             '''simple docstring'''
             _a   :			str					 =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

             _a   :			Union[str, Any]					 =None
             if self.use_labels:
                    _a   :			int					 =ids_tensor([self.batch_size, self.image_size, self.image_size]					,						self.num_labels     )

             _a   :			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 __UpperCAmelCase	(			self      :Optional[int]					,						SCREAMING_SNAKE_CASE      :Tuple					,						SCREAMING_SNAKE_CASE      :Dict					,						SCREAMING_SNAKE_CASE      :Any     )	-> Union[str, Any]:




             '''simple docstring'''
             _a   :			Tuple					 =PoolFormerModel(config=SCREAMING_SNAKE_CASE     )
             model.to(SCREAMING_SNAKE_CASE     )
             model.eval()
             _a   :			Union[str, Any]					 =model(SCREAMING_SNAKE_CASE     )
             _a   :			int					 =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      :Tuple     )	-> Union[str, Any]:




             '''simple docstring'''
             _a   :			str					 =self.prepare_config_and_inputs()
             _a ,   _a ,   _a   :			List[str]					 =config_and_inputs
             _a   :			int					 ={"""pixel_values""": pixel_values}
             return config, inputs_dict







@require_torch
class A__	(						UpperCAmelCase__			,					UpperCAmelCase__			,					unittest.TestCase				):
      __UpperCamelCase	:  int     	=							(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
      __UpperCamelCase	:  Union[str, Any]     	=							(
          {"feature-extraction": PoolFormerModel, "image-classification": PoolFormerForImageClassification}
          if is_torch_available()
          else {}
      )

      __UpperCamelCase	:  str     	=							False
      __UpperCamelCase	:  List[str]     	=							False
      __UpperCamelCase	:  str     	=							False
      __UpperCamelCase	:  str     	=							False
      __UpperCamelCase	:  str     	=							False






      def __UpperCAmelCase	(			self      :int     )	-> Tuple:




             '''simple docstring'''
             _a   :			Any					 =PoolFormerModelTester(self     )
             _a   :			Union[str, Any]					 =PoolFormerConfigTester(self					,						config_class=SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :Optional[int]     )	-> int:




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






      def __UpperCAmelCase	(			self      :Optional[Any]     )	-> Tuple:




             '''simple docstring'''
             _a   :			Dict					 =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE     )






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




             '''simple docstring'''
             pass






      @unittest.skip("""PoolFormer does not have get_input_embeddings method and get_output_embeddings methods"""     )
      def __UpperCAmelCase	(			self      :Union[str, Any]     )	-> Any:




             '''simple docstring'''
             pass






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




             '''simple docstring'''
             def check_hidden_states_output(SCREAMING_SNAKE_CASE      :Tuple					,						SCREAMING_SNAKE_CASE      :Any					,						SCREAMING_SNAKE_CASE      :List[str]     ):
                    _a   :			str					 =model_class(SCREAMING_SNAKE_CASE     )
                    model.to(SCREAMING_SNAKE_CASE     )
                    model.eval()

                    with torch.no_grad():
                           _a   :			int					 =model(**self._prepare_for_class(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     )     )

                    _a   :			List[Any]					 =outputs.hidden_states

                    _a   :			Dict					 =self.model_tester.num_encoder_blocks
                    self.assertEqual(len(SCREAMING_SNAKE_CASE     )					,						SCREAMING_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,
                        ]					,						)

             _a ,   _a   :			int					 =self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    _a   :			Dict					 =True
                    check_hidden_states_output(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     )

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

                    check_hidden_states_output(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     )






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




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

             _a ,   _a   :			Tuple					 =self.model_tester.prepare_config_and_inputs_for_common()
             _a   :			Union[str, Any]					 =True

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






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




             '''simple docstring'''
             _a ,   _a   :			Tuple					 =self.model_tester.prepare_config_and_inputs_for_common()

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

                    _a   :			Tuple					 =["""pixel_values"""]
                    self.assertListEqual(arg_names[:1]					,						SCREAMING_SNAKE_CASE     )






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




             '''simple docstring'''
             for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _a   :			Any					 =PoolFormerModel.from_pretrained(SCREAMING_SNAKE_CASE     )
                    self.assertIsNotNone(SCREAMING_SNAKE_CASE     )


def      SCREAMING_SNAKE_CASE_ (  )  ->      Tuple:
       _a   :			Tuple					 =Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
       return image







@require_torch
class A__	(						unittest.TestCase				):






      @slow
      def __UpperCAmelCase	(			self      :Dict     )	-> Optional[Any]:




             '''simple docstring'''
             _a   :			Dict					 =PoolFormerImageProcessor()
             _a   :			Union[str, Any]					 =PoolFormerForImageClassification.from_pretrained("""sail/poolformer_s12"""     ).to(SCREAMING_SNAKE_CASE     )

             _a   :			str					 =image_processor(images=prepare_img()					,						return_tensors="""pt"""     ).to(SCREAMING_SNAKE_CASE     )

             # forward pass
             with torch.no_grad():
                    _a   :			Union[str, Any]					 =model(**SCREAMING_SNAKE_CASE     )

             # verify the logits
             _a   :			List[str]					 =torch.Size((1, 1_0_0_0)     )
             self.assertEqual(outputs.logits.shape					,						SCREAMING_SNAKE_CASE     )

             _a   :			Optional[Any]					 =torch.tensor([-0.6_113, 0.1_685, -0.0_492]     ).to(SCREAMING_SNAKE_CASE     )
             self.assertTrue(torch.allclose(outputs.logits[0, :3]					,						SCREAMING_SNAKE_CASE					,						atol=1e-4     )     )






'''simple docstring'''







from typing import Dict, Optional, Tuple

import torch


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor  )  ->      torch.Tensor:
       _a   :			Tuple					 =boundaries[1] - boundaries[0]
       _a   :			Dict					 =boundaries + step / 2
       _a   :			List[Any]					 =torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1  )]   ,dim=0  )
       return bin_centers


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor   ,_UpperCAmelCase     :     torch.Tensor   ,)  ->      Tuple[torch.Tensor, torch.Tensor]:
       _a   :			Any					 =_calculate_bin_centers(_UpperCAmelCase  )
       return (
           torch.sum(aligned_distance_error_probs * bin_centers   ,dim=-1  ),
           bin_centers[-1],
       )


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor   ,_UpperCAmelCase     :     int = 31   ,_UpperCAmelCase     :     int = 64   ,**_UpperCAmelCase     :     Dict   ,)  ->      Dict[str, torch.Tensor]:
       _a   :			Dict					 =torch.linspace(0   ,_UpperCAmelCase   ,steps=(no_bins - 1)   ,device=logits.device  )

       _a   :			int					 =torch.nn.functional.softmax(_UpperCAmelCase   ,dim=-1  )
       _a ,   _a   :			List[Any]					 =_calculate_expected_aligned_error(
           alignment_confidence_breaks=_UpperCAmelCase   ,aligned_distance_error_probs=_UpperCAmelCase   ,)

       return {
           "aligned_confidence_probs": aligned_confidence_probs,
           "predicted_aligned_error": predicted_aligned_error,
           "max_predicted_aligned_error": max_predicted_aligned_error,
       }


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor   ,_UpperCAmelCase     :     Optional[torch.Tensor] = None   ,_UpperCAmelCase     :     int = 31   ,_UpperCAmelCase     :     int = 64   ,_UpperCAmelCase     :     float = 1e-8   ,**_UpperCAmelCase     :     Optional[Any]   ,)  ->      torch.Tensor:
       if residue_weights is None:
              _a   :			Dict					 =logits.new_ones(logits.shape[-2]  )

       _a   :			str					 =torch.linspace(0   ,_UpperCAmelCase   ,steps=(no_bins - 1)   ,device=logits.device  )

       _a   :			Optional[int]					 =_calculate_bin_centers(_UpperCAmelCase  )
       torch.sum(_UpperCAmelCase  )
       _a   :			List[str]					 =logits.shape[-2]
       _a   :			List[Any]					 =max(_UpperCAmelCase   ,19  )

       _a   :			Union[str, Any]					 =1.2_4 * (clipped_n - 15) ** (1.0 / 3) - 1.8

       _a   :			Tuple					 =torch.nn.functional.softmax(_UpperCAmelCase   ,dim=-1  )

       _a   :			Dict					 =1.0 / (1 + (bin_centers**2) / (da**2))
       _a   :			int					 =torch.sum(probs * tm_per_bin   ,dim=-1  )

       _a   :			List[Any]					 =residue_weights / (eps + residue_weights.sum())
       _a   :			List[str]					 =torch.sum(predicted_tm_term * normed_residue_mask   ,dim=-1  )

       _a   :			int					 =per_alignment * residue_weights

       _a   :			List[Any]					 =(weighted == torch.max(_UpperCAmelCase  )).nonzero()[0]
       return per_alignment[tuple(_UpperCAmelCase  )]


