


"""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 unittest

from transformers import CamembertTokenizer, CamembertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow
from transformers.utils import is_torch_available

from ...test_tokenization_common import TokenizerTesterMixin


__magic_name__						: Dict    =				get_tests_dir('fixtures/test_sentencepiece.model')
__magic_name__						: int    =				get_tests_dir('fixtures/test_sentencepiece_bpe.model')

__magic_name__						: Any    =				'pt' if is_torch_available() else 'tf'




@require_sentencepiece
@require_tokenizers
class 			__snake_case   (lowerCamelCase			,		unittest.TestCase  ):
							__a					 =       CamembertTokenizer
							__a					 =       CamembertTokenizerFast
							__a					 =       True
							__a					 =       True




							def   __a   (     self:    List[Any]   ):
										super().setUp()

										# We have a SentencePiece fixture for testing
										__lowerCamelCase          =      CamembertTokenizer(A_   )
										tokenizer.save_pretrained(self.tmpdirname   )




							def   __a   (     self:    Union[str, Any]   ):
										__lowerCamelCase          =      """<pad>"""
										__lowerCamelCase          =      1

										self.assertEqual(self.get_tokenizer()._convert_token_to_id(A_   )     ,							A_   )
										self.assertEqual(self.get_tokenizer()._convert_id_to_token(A_   )     ,							A_   )




							def   __a   (     self:    Any   ):
										__lowerCamelCase          =      list(self.get_tokenizer().get_vocab().keys()   )

										self.assertEqual(vocab_keys[0]     ,							"""<s>NOTUSED"""   )
										self.assertEqual(vocab_keys[1]     ,							"""<pad>"""   )
										self.assertEqual(vocab_keys[-1]     ,							"""<mask>"""   )
										self.assertEqual(len(A_   )     ,							10_04   )




							def   __a   (     self:    Any   ):
										self.assertEqual(self.get_tokenizer().vocab_size     ,							10_05   )




							def   __a   (     self:    Optional[Any]   ):
										__lowerCamelCase          =      CamembertTokenizer(A_   )
										tokenizer.save_pretrained(self.tmpdirname   )
										__lowerCamelCase          =      CamembertTokenizerFast.from_pretrained(self.tmpdirname   )

										__lowerCamelCase          =      """I was born in 92000, and this is falsé."""

										__lowerCamelCase          =      tokenizer.encode(A_   )
										__lowerCamelCase          =      rust_tokenizer.encode(A_   )
										self.assertListEqual(A_     ,							A_   )

										__lowerCamelCase          =      tokenizer.encode(A_     ,							add_special_tokens=A_   )
										__lowerCamelCase          =      rust_tokenizer.encode(A_     ,							add_special_tokens=A_   )
										self.assertListEqual(A_     ,							A_   )

										# <unk> tokens are not the same for `rust` than for `slow`.
										# Because spm gives back raw token instead of `unk` in EncodeAsPieces
										# tokens = tokenizer.tokenize(sequence)
										__lowerCamelCase          =      tokenizer.convert_ids_to_tokens(A_   )
										__lowerCamelCase          =      rust_tokenizer.tokenize(A_   )
										self.assertListEqual(A_     ,							A_   )




							def   __a   (     self:    List[str]   ):
										if not self.test_rust_tokenizer:
													return

										__lowerCamelCase          =      self.get_tokenizer()
										__lowerCamelCase          =      self.get_rust_tokenizer()

										__lowerCamelCase          =      """I was born in 92000, and this is falsé."""

										__lowerCamelCase          =      tokenizer.tokenize(A_   )
										__lowerCamelCase          =      rust_tokenizer.tokenize(A_   )
										self.assertListEqual(A_     ,							A_   )

										__lowerCamelCase          =      tokenizer.encode(A_     ,							add_special_tokens=A_   )
										__lowerCamelCase          =      rust_tokenizer.encode(A_     ,							add_special_tokens=A_   )
										self.assertListEqual(A_     ,							A_   )

										__lowerCamelCase          =      self.get_rust_tokenizer()
										__lowerCamelCase          =      tokenizer.encode(A_   )
										__lowerCamelCase          =      rust_tokenizer.encode(A_   )
										self.assertListEqual(A_     ,							A_   )




							@slow
							def   __a   (     self:    Optional[Any]   ):
										# fmt: off
										__lowerCamelCase          =      {"""input_ids""": [[5, 54, 71_96, 2_97, 30, 23, 7_76, 18, 11, 32_15, 37_05, 82_52, 22, 31_64, 11_81, 21_16, 29, 16, 8_13, 25, 7_91, 33_14, 20, 34_46, 38, 2_75_75, 1_20, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 4_68, 17, 11, 90_88, 20, 15_17, 8, 2_28_04, 1_88_18, 10, 38, 6_29, 6_07, 6_07, 1_42, 19, 71_96, 8_67, 56, 1_03_26, 24, 22_67, 20, 4_16, 50_72, 1_56_12, 2_33, 7_34, 7, 23_99, 27, 16, 30_15, 16_49, 7, 24, 20, 43_38, 23_99, 27, 13, 34_00, 14, 13, 61_89, 8, 9_30, 9, 6]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
										# fmt: on

										# camembert is a french model. So we also use french texts.
										__lowerCamelCase          =      [
										    """Le transformeur est un modèle d'apprentissage profond introduit en 2017, """
										    """utilisé principalement dans le domaine du traitement automatique des langues (TAL).""",
										    """À l'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus """
										    """pour gérer des données séquentielles, telles que le langage naturel, pour des tâches """
										    """telles que la traduction et la synthèse de texte.""",
										]

										self.tokenizer_integration_test_util(
										    expected_encoding=A_     ,							model_name="""camembert-base"""     ,							revision="""3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf"""     ,							sequences=A_     ,							)




