


"""simple docstring"""
from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor




class 	lowercase__				:





   def __init__(							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE=3      ,   SCREAMING_SNAKE_CASE=32      ,   SCREAMING_SNAKE_CASE=3      ,   SCREAMING_SNAKE_CASE=10      ,   SCREAMING_SNAKE_CASE=[10, 20, 30, 40]      ,   SCREAMING_SNAKE_CASE=[1, 1, 2, 1]      ,   SCREAMING_SNAKE_CASE=True      ,   SCREAMING_SNAKE_CASE=True      ,   SCREAMING_SNAKE_CASE="relu"      ,   SCREAMING_SNAKE_CASE=3      ,   SCREAMING_SNAKE_CASE=None      ,   )  ->  Any:
      _lowerCamelCase	:					Dict   =		parent
      _lowerCamelCase	:					Dict   =		batch_size
      _lowerCamelCase	:					Optional[Any]   =		image_size
      _lowerCamelCase	:					Any   =		num_channels
      _lowerCamelCase	:					Any   =		embeddings_size
      _lowerCamelCase	:					Optional[int]   =		hidden_sizes
      _lowerCamelCase	:					Tuple   =		depths
      _lowerCamelCase	:					List[Any]   =		is_training
      _lowerCamelCase	:					int   =		use_labels
      _lowerCamelCase	:					str   =		hidden_act
      _lowerCamelCase	:					Dict   =		num_labels
      _lowerCamelCase	:					int   =		scope
      _lowerCamelCase	:					Optional[Any]   =		len(SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  List[Any]:
      _lowerCamelCase	:					Optional[int]   =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

      _lowerCamelCase	:					Dict   =		None
      if self.use_labels:
         _lowerCamelCase	:					Tuple   =		ids_tensor([self.batch_size]      ,   self.num_labels)

      _lowerCamelCase	:					Tuple   =		self.get_config()
      return config, pixel_values, labels





   def 		UpperCamelCase_  (							self)  ->  Dict:
      return RegNetConfig(
          num_channels=self.num_channels      ,   embeddings_size=self.embeddings_size      ,   hidden_sizes=self.hidden_sizes      ,   depths=self.depths      ,   hidden_act=self.hidden_act      ,   num_labels=self.num_labels      ,   )





   def 		UpperCamelCase_  (							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)  ->  Any:
      _lowerCamelCase	:					Optional[int]   =		TFRegNetModel(config=SCREAMING_SNAKE_CASE)
      _lowerCamelCase	:					int   =		model(SCREAMING_SNAKE_CASE      ,   training=SCREAMING_SNAKE_CASE)
      # expected last hidden states: B, C, H // 32, W // 32
      self.parent.assertEqual(
          result.last_hidden_state.shape      ,   (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)      ,   )





   def 		UpperCamelCase_  (							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)  ->  int:
      _lowerCamelCase	:					Optional[Any]   =		self.num_labels
      _lowerCamelCase	:					List[str]   =		TFRegNetForImageClassification(SCREAMING_SNAKE_CASE)
      _lowerCamelCase	:					Tuple   =		model(SCREAMING_SNAKE_CASE      ,   labels=SCREAMING_SNAKE_CASE      ,   training=SCREAMING_SNAKE_CASE)
      self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.num_labels))





   def 		UpperCamelCase_  (							self)  ->  List[str]:
      _lowerCamelCase	:					Optional[int]   =		self.prepare_config_and_inputs()
      _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase	:					Optional[Any]   =		config_and_inputs
      _lowerCamelCase	:					Tuple   =		{"""pixel_values""": pixel_values}
      return config, inputs_dict







@require_tf
class 	lowercase__				(					A_ ,A_ ,unittest.TestCase				):
   __UpperCAmelCase							       =					(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
   __UpperCAmelCase							       =					(
       {'''feature-extraction''': TFRegNetModel, '''image-classification''': TFRegNetForImageClassification}
       if is_tf_available()
       else {}
   )

   __UpperCAmelCase							       =					False
   __UpperCAmelCase							       =					False
   __UpperCAmelCase							       =					False
   __UpperCAmelCase							       =					False
   __UpperCAmelCase							       =					False





   def 		UpperCamelCase_  (							self)  ->  List[str]:
      _lowerCamelCase	:					str   =		TFRegNetModelTester(self)
      _lowerCamelCase	:					Union[str, Any]   =		ConfigTester(self      ,   config_class=SCREAMING_SNAKE_CASE      ,   has_text_modality=SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Optional[Any]:
      return





   @unittest.skip(reason="""RegNet does not use inputs_embeds""")
   def 		UpperCamelCase_  (							self)  ->  List[str]:
      pass





   @unittest.skipIf(
       not is_tf_available() or len(tf.config.list_physical_devices("""GPU""")) == 0      ,   reason="""TF does not support backprop for grouped convolutions on CPU."""      ,   )
   @slow
   def 		UpperCamelCase_  (							self)  ->  List[str]:
      super().test_keras_fit()





   @unittest.skip(reason="""RegNet does not support input and output embeddings""")
   def 		UpperCamelCase_  (							self)  ->  Optional[Any]:
      pass





   def 		UpperCamelCase_  (							self)  ->  Dict:
      _lowerCamelCase   ,    _lowerCamelCase	:					int   =		self.model_tester.prepare_config_and_inputs_for_common()

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

         _lowerCamelCase	:					List[str]   =		["""pixel_values"""]
         self.assertListEqual(arg_names[:1]      ,   SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Union[str, Any]:
      _lowerCamelCase	:					Union[str, Any]   =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Union[str, Any]:
      def check_hidden_states_output(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE):
         _lowerCamelCase	:					int   =		model_class(SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Tuple   =		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)      ,   training=SCREAMING_SNAKE_CASE)

         _lowerCamelCase	:					Any   =		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

         _lowerCamelCase	:					Union[str, Any]   =		self.model_tester.num_stages
         self.assertEqual(len(SCREAMING_SNAKE_CASE)      ,   expected_num_stages + 1)

         # RegNet's feature maps are of shape (batch_size, num_channels, height, width)
         self.assertListEqual(
             list(hidden_states[0].shape[-2:])      ,   [self.model_tester.image_size // 2, self.model_tester.image_size // 2]      ,   )

      _lowerCamelCase   ,    _lowerCamelCase	:					List[Any]   =		self.model_tester.prepare_config_and_inputs_for_common()
      _lowerCamelCase	:					Any   =		["""basic""", """bottleneck"""]
      for model_class in self.all_model_classes:
         for layer_type in layers_type:
            _lowerCamelCase	:					Union[str, Any]   =		layer_type
            _lowerCamelCase	:					List[Any]   =		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"]
            _lowerCamelCase	:					Tuple   =		True

            check_hidden_states_output(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Union[str, Any]:
      _lowerCamelCase   ,    _lowerCamelCase	:					str   =		self.model_tester.prepare_config_and_inputs_for_common()

      def check_equivalence(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE={}):
         _lowerCamelCase	:					int   =		model(SCREAMING_SNAKE_CASE      ,   return_dict=SCREAMING_SNAKE_CASE      ,   **SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Optional[Any]   =		model(SCREAMING_SNAKE_CASE      ,   return_dict=SCREAMING_SNAKE_CASE      ,   **SCREAMING_SNAKE_CASE).to_tuple()

         def recursive_check(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE):
            if isinstance(SCREAMING_SNAKE_CASE      ,   (List, Tuple)):
               for tuple_iterable_value, dict_iterable_value in zip(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE):
                  recursive_check(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)
            elif tuple_object is None:
               return
            else:
               self.assertTrue(
                   all(tf.equal(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE))      ,   msg=(
                       """Tuple and dict output are not equal. Difference:"""
                       F' {tf.math.reduce_max(tf.abs(tuple_object - dict_object))}'
                   )      ,   )

            recursive_check(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)

      for model_class in self.all_model_classes:
         _lowerCamelCase	:					Tuple   =		model_class(SCREAMING_SNAKE_CASE)

         _lowerCamelCase	:					str   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Optional[int]   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)
         check_equivalence(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)

         _lowerCamelCase	:					Optional[Any]   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   return_labels=SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Optional[Any]   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   return_labels=SCREAMING_SNAKE_CASE)
         check_equivalence(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)

         _lowerCamelCase	:					Any   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Any   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)
         check_equivalence(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   {"""output_hidden_states""": True})

         _lowerCamelCase	:					int   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   return_labels=SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Any   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   return_labels=SCREAMING_SNAKE_CASE)
         check_equivalence(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   {"""output_hidden_states""": True})





   def 		UpperCamelCase_  (							self)  ->  Optional[int]:
      _lowerCamelCase	:					Union[str, Any]   =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE)





   @slow
   def 		UpperCamelCase_  (							self)  ->  int:
      for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _lowerCamelCase	:					List[str]   =		TFRegNetModel.from_pretrained(SCREAMING_SNAKE_CASE)
         self.assertIsNotNone(SCREAMING_SNAKE_CASE)







def 				_snake_case      (   ):




   """simple docstring"""







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





@require_tf
@require_vision
class 	lowercase__				(					unittest.TestCase				):





   @cached_property
   def 		UpperCamelCase_  (							self)  ->  Any:
      return (
          AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0])
          if is_vision_available()
          else None
      )





   @slow
   def 		UpperCamelCase_  (							self)  ->  List[Any]:
      _lowerCamelCase	:					str   =		TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0])

      _lowerCamelCase	:					Optional[int]   =		self.default_image_processor
      _lowerCamelCase	:					int   =		prepare_img()
      _lowerCamelCase	:					List[Any]   =		image_processor(images=SCREAMING_SNAKE_CASE      ,   return_tensors="""tf""")

      # forward pass
      _lowerCamelCase	:					Dict   =		model(**SCREAMING_SNAKE_CASE      ,   training=SCREAMING_SNAKE_CASE)

      # verify the logits
      _lowerCamelCase	:					Tuple   =		tf.TensorShape((1, 1000))
      self.assertEqual(outputs.logits.shape      ,   SCREAMING_SNAKE_CASE)

      _lowerCamelCase	:					Optional[Any]   =		tf.constant([-0.41_80, -1.50_51, -3.48_36])

      tf.debugging.assert_near(outputs.logits[0, :3]      ,   SCREAMING_SNAKE_CASE      ,   atol=1e-4)



"""simple docstring"""






import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_funnel import FunnelTokenizer


__SCREAMING_SNAKE_CASE							=     logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE							=     {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

__SCREAMING_SNAKE_CASE							=     [
    'small',
    'small-base',
    'medium',
    'medium-base',
    'intermediate',
    'intermediate-base',
    'large',
    'large-base',
    'xlarge',
    'xlarge-base',
]

__SCREAMING_SNAKE_CASE							=     {
    'vocab_file': {
        'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt',
        'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt',
        'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt',
        'funnel-transformer/medium-base': (
            'https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt'
        ),
        'funnel-transformer/intermediate': (
            'https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt'
        ),
        'funnel-transformer/intermediate-base': (
            'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt'
        ),
        'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt',
        'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt',
        'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt',
        'funnel-transformer/xlarge-base': (
            'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json',
        'funnel-transformer/small-base': (
            'https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json',
        'funnel-transformer/medium-base': (
            'https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/intermediate': (
            'https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/intermediate-base': (
            'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json',
        'funnel-transformer/large-base': (
            'https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json',
        'funnel-transformer/xlarge-base': (
            'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json'
        ),
    },
}
__SCREAMING_SNAKE_CASE							=     {F"""funnel-transformer/{name}""": 512 for name in _model_names}
__SCREAMING_SNAKE_CASE							=     {F"""funnel-transformer/{name}""": {'do_lower_case': True} for name in _model_names}





class 				a__					(						A__       ):
     UpperCAmelCase__         =      VOCAB_FILES_NAMES
     UpperCAmelCase__         =      PRETRAINED_VOCAB_FILES_MAP
     UpperCAmelCase__         =      PRETRAINED_INIT_CONFIGURATION
     UpperCAmelCase__         =      FunnelTokenizer
     UpperCAmelCase__         =      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     UpperCAmelCase__         =      2



     def __init__(							self :str      ,  _lowerCamelCase :int=None      ,  _lowerCamelCase :List[Any]=None      ,  _lowerCamelCase :Dict=True      ,  _lowerCamelCase :Optional[Any]="<unk>"      ,  _lowerCamelCase :Optional[Any]="<sep>"      ,  _lowerCamelCase :List[Any]="<pad>"      ,  _lowerCamelCase :List[Any]="<cls>"      ,  _lowerCamelCase :Union[str, Any]="<mask>"      ,  _lowerCamelCase :Any="<s>"      ,  _lowerCamelCase :Union[str, Any]="</s>"      ,  _lowerCamelCase :str=True      ,  _lowerCamelCase :Dict=True      ,  _lowerCamelCase :Tuple=None      ,  _lowerCamelCase :Tuple="##"      ,  **_lowerCamelCase :int      ,  ):







      '''simple docstring'''



      super().__init__(
          _lowerCamelCase      ,  tokenizer_file=_lowerCamelCase      ,  do_lower_case=_lowerCamelCase      ,  unk_token=_lowerCamelCase      ,  sep_token=_lowerCamelCase      ,  pad_token=_lowerCamelCase      ,  cls_token=_lowerCamelCase      ,  mask_token=_lowerCamelCase      ,  bos_token=_lowerCamelCase      ,  eos_token=_lowerCamelCase      ,  clean_text=_lowerCamelCase      ,  tokenize_chinese_chars=_lowerCamelCase      ,  strip_accents=_lowerCamelCase      ,  wordpieces_prefix=_lowerCamelCase      ,  **_lowerCamelCase      ,  )

      UpperCamelCase_     :					str   	=json.loads(self.backend_tokenizer.normalizer.__getstate__()   )
      if (
          normalizer_state.get('lowercase'      ,  _lowerCamelCase   ) != do_lower_case
          or normalizer_state.get('strip_accents'      ,  _lowerCamelCase   ) != strip_accents
          or normalizer_state.get('handle_chinese_chars'      ,  _lowerCamelCase   ) != tokenize_chinese_chars
      ):
       UpperCamelCase_     :					int   	=getattr(_lowerCamelCase      ,  normalizer_state.pop('type'   )   )
       UpperCamelCase_     :					Dict   	=do_lower_case
       UpperCamelCase_     :					Tuple   	=strip_accents
       UpperCamelCase_     :					int   	=tokenize_chinese_chars
       UpperCamelCase_     :					Tuple   	=normalizer_class(**_lowerCamelCase   )

      UpperCamelCase_     :					Dict   	=do_lower_case



     def  lowerCamelCase_						(							self :Optional[int]      ,  _lowerCamelCase :str      ,  _lowerCamelCase :Optional[int]=None   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[Any]   	=[self.cls_token_id] + token_ids_a + [self.sep_token_id]

      if token_ids_a:
       output += token_ids_a + [self.sep_token_id]

      return output



     def  lowerCamelCase_						(							self :List[Any]      ,  _lowerCamelCase :List[int]      ,  _lowerCamelCase :Optional[List[int]] = None   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[int]   	=[self.sep_token_id]
      UpperCamelCase_     :					List[Any]   	=[self.cls_token_id]
      if token_ids_a is None:
       return len(cls   ) * [self.cls_token_type_id] + len(token_ids_a + sep   ) * [0]
      return len(cls   ) * [self.cls_token_type_id] + len(token_ids_a + sep   ) * [0] + len(token_ids_a + sep   ) * [1]




     def  lowerCamelCase_						(							self :Optional[Any]      ,  _lowerCamelCase :str      ,  _lowerCamelCase :Optional[str] = None   ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=self._tokenizer.model.save(_lowerCamelCase      ,  name=_lowerCamelCase   )
      return tuple(_lowerCamelCase   )
