





"""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 lowerCamelCase__  (    __UpperCamelCase				):



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







      """simple docstring"""
      lowercase    :			str  =       self.config_class(**self.inputs_dict	)
      self.parent.assertTrue(hasattr(snake_case      ,   """hidden_sizes"""	)	)
      self.parent.assertTrue(hasattr(snake_case      ,   """num_encoder_blocks"""	)	)




class lowerCamelCase__  :



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







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



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







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

      lowercase    :			List[Any]  =       None
      if self.use_labels:
         lowercase    :			Tuple  =       ids_tensor([self.batch_size, self.image_size, self.image_size]      ,   self.num_labels	)

      lowercase    :			Dict  =       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      ,   snake_case      ,   snake_case      ,   snake_case	) ->    Optional[int]:







      """simple docstring"""
      lowercase    :			List[str]  =       PoolFormerModel(config=snake_case	)
      model.to(snake_case	)
      model.eval()
      lowercase    :			Optional[Any]  =       model(snake_case	)
      lowercase    :			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    _UpperCAmelCase	(       self	) ->    Dict:







      """simple docstring"""
      lowercase    :			Tuple  =       self.prepare_config_and_inputs()
      lowercase  ,					lowercase  ,					lowercase    :			Union[str, Any]  =       config_and_inputs
      lowercase    :			Dict  =       {"""pixel_values""": pixel_values}
      return config, inputs_dict




@require_torch
class lowerCamelCase__  (    __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    _UpperCAmelCase	(       self	) ->    Dict:







      """simple docstring"""
      lowercase    :			List[str]  =       PoolFormerModelTester(self	)
      lowercase    :			Union[str, Any]  =       PoolFormerConfigTester(self      ,   config_class=snake_case	)



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







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



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







      """simple docstring"""
      lowercase    :			Tuple  =       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	) ->    int:







      """simple docstring"""
      pass



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







      """simple docstring"""
      pass



   def    _UpperCAmelCase	(       self	) ->    Any:







      """simple docstring"""
      def check_hidden_states_output(snake_case      ,   snake_case      ,   snake_case	):
         lowercase    :			Any  =       model_class(snake_case	)
         model.to(snake_case	)
         model.eval()

         with torch.no_grad():
            lowercase    :			Optional[Any]  =       model(**self._prepare_for_class(snake_case      ,   snake_case	)	)

         lowercase    :			Tuple  =       outputs.hidden_states

         lowercase    :			Tuple  =       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,
             ]      ,   )

      lowercase  ,					lowercase    :			Any  =       self.model_tester.prepare_config_and_inputs_for_common()

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

         check_hidden_states_output(snake_case      ,   snake_case      ,   snake_case	)



   def    _UpperCAmelCase	(       self	) ->    Dict:







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

      lowercase  ,					lowercase    :			int  =       self.model_tester.prepare_config_and_inputs_for_common()
      lowercase    :			Tuple  =       True

      for model_class in self.all_model_classes:
         if model_class in get_values(snake_case	):
            continue
         lowercase    :			Optional[Any]  =       model_class(snake_case	)
         model.to(snake_case	)
         model.train()
         lowercase    :			Optional[Any]  =       self._prepare_for_class(snake_case      ,   snake_case      ,   return_labels=snake_case	)
         lowercase    :			Any  =       model(**snake_case	).loss
         loss.backward()



   def    _UpperCAmelCase	(       self	) ->    int:







      """simple docstring"""
      lowercase  ,					lowercase    :			Optional[Any]  =       self.model_tester.prepare_config_and_inputs_for_common()

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

         lowercase    :			Any  =       ["""pixel_values"""]
         self.assertListEqual(arg_names[:1]      ,   snake_case	)





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







      """simple docstring"""
      for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         lowercase    :			str  =       PoolFormerModel.from_pretrained(snake_case	)
         self.assertIsNotNone(snake_case	)




def 			__snake_case						(      )		->				Dict:
   lowercase    :			List[str]  =       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
   return image







@require_torch
class lowerCamelCase__  (    unittest.TestCase				):



   @slow
   def    _UpperCAmelCase	(       self	) ->    Optional[int]:







      """simple docstring"""
      lowercase    :			Optional[int]  =       PoolFormerImageProcessor()
      lowercase    :			Tuple  =       PoolFormerForImageClassification.from_pretrained("""sail/poolformer_s12"""	).to(snake_case	)

      lowercase    :			Any  =       image_processor(images=prepare_img()      ,   return_tensors="""pt"""	).to(snake_case	)

      # forward pass
      with torch.no_grad():
         lowercase    :			str  =       model(**snake_case	)

      # verify the logits
      lowercase    :			List[Any]  =       torch.Size((1, 1_0_0_0)	)
      self.assertEqual(outputs.logits.shape      ,   snake_case	)

      lowercase    :			str  =       torch.tensor([-0.61_13, 0.16_85, -0.04_92]	).to(snake_case	)
      self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,   snake_case      ,   atol=1E-4	)	)









"""simple docstring"""
from typing import Any, Dict, List, Mapping

import numpy as np
import torch

from ...utils import is_cython_available, requires_backends


if is_cython_available():
   import pyximport

   pyximport.install(setup_args={"include_dirs": np.get_include()})
   from . import algos_graphormer  # noqa E402




def 			__snake_case						(      __A ,__A = 512    )		->				Optional[Any]:
   lowercase    :			Optional[Any]  =       x.shape[1] if len(x.shape    ) > 1 else 1
   lowercase    :			Tuple  =       1 + np.arange(0 ,feature_num * offset ,__A ,dtype=np.intaa    )
   lowercase    :			Any  =       x + feature_offset
   return x




def 			__snake_case						(      __A ,__A=True    )		->				List[Any]:
   requires_backends(__A ,["""cython"""]    )

   if keep_features and "edge_attr" in item.keys():  # edge_attr
      lowercase    :			Any  =       np.asarray(item["""edge_attr"""] ,dtype=np.intaa    )
   else:
      lowercase    :			Optional[int]  =       np.ones((len(item["""edge_index"""][0]    ), 1) ,dtype=np.intaa    )  # same embedding for all

   if keep_features and "node_feat" in item.keys():  # input_nodes
      lowercase    :			Dict  =       np.asarray(item["""node_feat"""] ,dtype=np.intaa    )
   else:
      lowercase    :			Optional[Any]  =       np.ones((item["""num_nodes"""], 1) ,dtype=np.intaa    )  # same embedding for all

   lowercase    :			str  =       np.asarray(item["""edge_index"""] ,dtype=np.intaa    )

   lowercase    :			Tuple  =       convert_to_single_emb(__A    ) + 1
   lowercase    :			Dict  =       item["""num_nodes"""]

   if len(edge_attr.shape    ) == 1:
      lowercase    :			Dict  =       edge_attr[:, None]
   lowercase    :			Tuple  =       np.zeros([num_nodes, num_nodes, edge_attr.shape[-1]] ,dtype=np.intaa    )
   lowercase    :			List[Any]  =       convert_to_single_emb(__A    ) + 1

   # node adj matrix [num_nodes, num_nodes] bool
   lowercase    :			List[Any]  =       np.zeros([num_nodes, num_nodes] ,dtype=__A    )
   lowercase    :			str  =       True

   lowercase  ,					lowercase    :			List[str]  =       algos_graphormer.floyd_warshall(__A    )
   lowercase    :			str  =       np.amax(__A    )

   lowercase    :			int  =       algos_graphormer.gen_edge_input(__A ,__A ,__A    )
   lowercase    :			Optional[int]  =       np.zeros([num_nodes + 1, num_nodes + 1] ,dtype=np.single    )  # with graph token

   # combine
   lowercase    :			List[Any]  =       input_nodes + 1  # we shift all indices by one for padding
   lowercase    :			Optional[int]  =       attn_bias
   lowercase    :			str  =       attn_edge_type
   lowercase    :			int  =       shortest_path_result.astype(np.intaa    ) + 1  # we shift all indices by one for padding
   lowercase    :			Union[str, Any]  =       np.sum(__A ,axis=1    ).reshape(-1    ) + 1  # we shift all indices by one for padding
   lowercase    :			int  =       item["""in_degree"""]  # for undirected graph
   lowercase    :			Dict  =       input_edges + 1  # we shift all indices by one for padding
   if "labels" not in item:
      lowercase    :			str  =       item["""y"""]

   return item







class lowerCamelCase__  :



   def __init__(       self      ,   snake_case=2_0      ,   snake_case=False	) ->    List[Any]:







      """simple docstring"""
      if not is_cython_available():
         raise ImportError("""Graphormer preprocessing needs Cython (pyximport)"""	)

      lowercase    :			Any  =       spatial_pos_max
      lowercase    :			Dict  =       on_the_fly_processing





   def __call__(       self      ,   snake_case	) ->    Dict[str, Any]:







      """simple docstring"""
      if self.on_the_fly_processing:
         lowercase    :			Dict  =       [preprocess_item(snake_case	) for i in features]

      if not isinstance(features[0]      ,   snake_case	):
         lowercase    :			Optional[int]  =       [vars(snake_case	) for f in features]
      lowercase    :			Any  =       {}

      lowercase    :			Any  =       max(len(i["""input_nodes"""]	) for i in features	)
      lowercase    :			List[str]  =       len(features[0]["""input_nodes"""][0]	)
      lowercase    :			Tuple  =       len(features[0]["""attn_edge_type"""][0][0]	)
      lowercase    :			str  =       max(len(i["""input_edges"""][0][0]	) for i in features	)
      lowercase    :			Dict  =       len(features[0]["""input_edges"""][0][0][0]	)
      lowercase    :			Union[str, Any]  =       len(snake_case	)

      lowercase    :			List[str]  =       torch.zeros(snake_case      ,   max_node_num + 1      ,   max_node_num + 1      ,   dtype=torch.float	)
      lowercase    :			List[Any]  =       torch.zeros(snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   dtype=torch.long	)
      lowercase    :			int  =       torch.zeros(snake_case      ,   snake_case      ,   snake_case      ,   dtype=torch.long	)
      lowercase    :			List[str]  =       torch.zeros(snake_case      ,   snake_case      ,   dtype=torch.long	)
      lowercase    :			Tuple  =       torch.zeros(snake_case      ,   snake_case      ,   snake_case      ,   dtype=torch.long	)
      lowercase    :			Dict  =       torch.zeros(
          snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   dtype=torch.long	)

      for ix, f in enumerate(snake_case	):
         for k in ["attn_bias", "attn_edge_type", "spatial_pos", "in_degree", "input_nodes", "input_edges"]:
            lowercase    :			Optional[int]  =       torch.tensor(f[k]	)

         if len(f["""attn_bias"""][1:, 1:][f["""spatial_pos"""] >= self.spatial_pos_max]	) > 0:
            lowercase    :			Dict  =       float("""-inf"""	)

         lowercase    :			str  =       f["""attn_bias"""]
         lowercase    :			Union[str, Any]  =       f[
             """attn_edge_type"""
         ]
         lowercase    :			Union[str, Any]  =       f["""spatial_pos"""]
         lowercase    :			int  =       f["""in_degree"""]
         lowercase    :			int  =       f["""input_nodes"""]
         lowercase    :			Any  =       f["""input_edges"""]

      lowercase    :			Dict  =       batch["""in_degree"""]

      lowercase    :			str  =       features[0]["""labels"""]
      if len(snake_case	) == 1:  # one task
         if isinstance(sample[0]      ,   snake_case	):  # regression
            lowercase    :			List[str]  =       torch.from_numpy(np.concatenate([i["""labels"""] for i in features]	)	)
         else:  # binary classification
            lowercase    :			Tuple  =       torch.from_numpy(np.concatenate([i["""labels"""] for i in features]	)	)
      else:  # multi task classification, left to float to keep the NaNs
         lowercase    :			Optional[Any]  =       torch.from_numpy(np.stack([i["""labels"""] for i in features]      ,   axis=0	)	)

      return batch


