



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				)				)





"""simple docstring"""






import os
from typing import List, Union

import tensorflow as tf
from tensorflow_text import BertTokenizer as BertTokenizerLayer
from tensorflow_text import FastBertTokenizer, ShrinkLongestTrimmer, case_fold_utfa, combine_segments, pad_model_inputs

from .tokenization_bert import BertTokenizer


class 	A_       (     tf.keras.layers.Layer   ):





  """simple docstring"""

  def __init__(						self  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase = None  ,					__UpperCAmelCase = None  ,					__UpperCAmelCase = None  ,					__UpperCAmelCase = "longest"  ,					__UpperCAmelCase = True  ,					__UpperCAmelCase = 5_12  ,					__UpperCAmelCase = None  ,					__UpperCAmelCase = True  ,					__UpperCAmelCase = True  ,					__UpperCAmelCase = True  ,					**__UpperCAmelCase  ,					)				->					Union[str, Any]:
     super().__init__()
     if use_fast_bert_tokenizer:
        a   :	int						=    FastBertTokenizer(
            __UpperCAmelCase  ,					token_out_type=tf.intaa  ,					lower_case_nfd_strip_accents=__UpperCAmelCase  ,					**__UpperCAmelCase						)
     else:
        a   :	Optional[Any]						=    tf.lookup.StaticVocabularyTable(
            tf.lookup.KeyValueTensorInitializer(
                keys=__UpperCAmelCase  ,					key_dtype=tf.string  ,					values=tf.range(tf.size(__UpperCAmelCase  ,					out_type=tf.intaa						)  ,					dtype=tf.intaa						)  ,					value_dtype=tf.intaa  ,					)  ,					num_oov_buckets=1  ,					)
        a   :	str						=    BertTokenizerLayer(
            __UpperCAmelCase  ,					token_out_type=tf.intaa  ,					lower_case=__UpperCAmelCase  ,					**__UpperCAmelCase						)

     a   :	str						=    vocab_list
     a   :	Dict						=    do_lower_case
     a   :	Dict						=    cls_token_id or vocab_list.index('[CLS]'						)
     a   :	Tuple						=    sep_token_id or vocab_list.index('[SEP]'						)
     a   :	Optional[Any]						=    pad_token_id or vocab_list.index('[PAD]'						)
     a   :	Tuple						=    ShrinkLongestTrimmer(max_length - 3  ,					axis=1						)  # Allow room for special tokens
     a   :	Union[str, Any]						=    max_length
     a   :	Tuple						=    padding
     a   :	List[Any]						=    truncation
     a   :	Union[str, Any]						=    pad_to_multiple_of
     a   :	List[Any]						=    return_token_type_ids
     a   :	Optional[int]						=    return_attention_mask
  @classmethod
  def 							lowercase_     (						cls  ,					__UpperCAmelCase  ,					**__UpperCAmelCase						)				->					Tuple:  # noqa: F821
     a   :	List[Any]						=    kwargs.pop('do_lower_case'  ,					__UpperCAmelCase						)
     a   :	str						=    tokenizer.do_lower_case if do_lower_case is None else do_lower_case
     a   :	List[Any]						=    kwargs.pop('cls_token_id'  ,					__UpperCAmelCase						)
     a   :	int						=    tokenizer.cls_token_id if cls_token_id is None else cls_token_id
     a   :	Tuple						=    kwargs.pop('sep_token_id'  ,					__UpperCAmelCase						)
     a   :	Optional[int]						=    tokenizer.sep_token_id if sep_token_id is None else sep_token_id
     a   :	List[Any]						=    kwargs.pop('pad_token_id'  ,					__UpperCAmelCase						)
     a   :	Any						=    tokenizer.pad_token_id if pad_token_id is None else pad_token_id

     a   :	Optional[Any]						=    tokenizer.get_vocab()
     a   :	Optional[Any]						=    sorted([(wordpiece, idx) for wordpiece, idx in vocab.items()]  ,					key=lambda __UpperCAmelCase						: x[1]						)
     a   :	Optional[Any]						=    [entry[0] for entry in vocab]
     return cls(
         vocab_list=__UpperCAmelCase  ,					do_lower_case=__UpperCAmelCase  ,					cls_token_id=__UpperCAmelCase  ,					sep_token_id=__UpperCAmelCase  ,					pad_token_id=__UpperCAmelCase  ,					**__UpperCAmelCase  ,					)
  @classmethod
  def 							lowercase_     (						cls  ,					__UpperCAmelCase  ,					*__UpperCAmelCase  ,					**__UpperCAmelCase						)				->					Tuple:
     try:
        a   :	Dict						=    BertTokenizer.from_pretrained(__UpperCAmelCase  ,					*__UpperCAmelCase  ,					**__UpperCAmelCase						)
     except:  # noqa: E722
        from .tokenization_bert_fast import BertTokenizerFast

        a   :	Tuple						=    BertTokenizerFast.from_pretrained(__UpperCAmelCase  ,					*__UpperCAmelCase  ,					**__UpperCAmelCase						)
     return cls.from_tokenizer(__UpperCAmelCase  ,					**__UpperCAmelCase						)
  def 							lowercase_     (						self  ,					__UpperCAmelCase						)				->					List[Any]:
     if self.do_lower_case:
        a   :	int						=    case_fold_utfa(__UpperCAmelCase						)
     a   :	Union[str, Any]						=    self.tf_tokenizer.tokenize(__UpperCAmelCase						)
     return tokens.merge_dims(1  ,					-1						)
  def 							lowercase_     (						self  ,					__UpperCAmelCase  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					)				->					Optional[int]:
     if padding is None:
        a   :	List[Any]						=    self.padding
     if padding not in ("longest", "max_length"):
        raise ValueError('Padding must be either \'longest\' or \'max_length\'!'						)
     if max_length is not None and text_pair is not None:
        # Because we have to instantiate a Trimmer to do it properly
        raise ValueError('max_length cannot be overridden at call time when truncating paired texts!'						)
     if max_length is None:
        a   :	Any						=    self.max_length
     if truncation is None:
        a   :	Any						=    self.truncation
     if pad_to_multiple_of is None:
        a   :	Optional[int]						=    self.pad_to_multiple_of
     if return_token_type_ids is None:
        a   :	str						=    self.return_token_type_ids
     if return_attention_mask is None:
        a   :	Dict						=    self.return_attention_mask
     if not isinstance(__UpperCAmelCase  ,					tf.Tensor						):
        a   :	List[str]						=    tf.convert_to_tensor(__UpperCAmelCase						)
     if text_pair is not None and not isinstance(__UpperCAmelCase  ,					tf.Tensor						):
        a   :	Any						=    tf.convert_to_tensor(__UpperCAmelCase						)
     if text_pair is not None:
        if text.shape.rank > 1:
           raise ValueError('text argument should not be multidimensional when a text pair is supplied!'						)
        if text_pair.shape.rank > 1:
           raise ValueError('text_pair should not be multidimensional!'						)
     if text.shape.rank == 2:
        a	, a   :	Union[str, Any]						=    text[:, 0], text[:, 1]
     a   :	Union[str, Any]						=    self.unpaired_tokenize(__UpperCAmelCase						)
     if text_pair is None:  # Unpaired text
        if truncation:
           a   :	List[Any]						=    text[:, : max_length - 2]  # Allow room for special tokens
        a	, a   :	Dict						=    combine_segments(
            (text,)  ,					start_of_sequence_id=self.cls_token_id  ,					end_of_segment_id=self.sep_token_id						)
     else:  # Paired text
        a   :	Optional[Any]						=    self.unpaired_tokenize(__UpperCAmelCase						)
        if truncation:
           a	, a   :	List[str]						=    self.paired_trimmer.trim([text, text_pair]						)
        a	, a   :	List[Any]						=    combine_segments(
            (text, text_pair)  ,					start_of_sequence_id=self.cls_token_id  ,					end_of_segment_id=self.sep_token_id						)
     if padding == "longest":
        a   :	List[Any]						=    input_ids.bounding_shape(axis=1						)
        if pad_to_multiple_of is not None:
           # No ceiling division in tensorflow, so we negate floordiv instead
           a   :	Optional[int]						=    pad_to_multiple_of * (-tf.math.floordiv(-pad_length  ,					__UpperCAmelCase						))
     else:
        a   :	Any						=    max_length

     a	, a   :	Any						=    pad_model_inputs(__UpperCAmelCase  ,					max_seq_length=__UpperCAmelCase  ,					pad_value=self.pad_token_id						)
     a   :	Optional[Any]						=    {'input_ids': input_ids}
     if return_attention_mask:
        a   :	Tuple						=    attention_mask
     if return_token_type_ids:
        a	, a   :	int						=    pad_model_inputs(
            __UpperCAmelCase  ,					max_seq_length=__UpperCAmelCase  ,					pad_value=self.pad_token_id						)
        a   :	Union[str, Any]						=    token_type_ids
     return output






  def 							lowercase_     (						self						)				->					List[str]:
     return {
         "vocab_list": self.vocab_list,
         "do_lower_case": self.do_lower_case,
         "cls_token_id": self.cls_token_id,
         "sep_token_id": self.sep_token_id,
         "pad_token_id": self.pad_token_id,
     }





