
"""simple docstring"""




import inspect
import unittest

from transformers import ResNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
    import jax
    import jax.numpy as jnp

    from transformers.models.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor







class 	_lowercase    (							unittest.TestCase					):


     '''simple docstring'''





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



     def 			lowerCAmelCase__							(   self	)->       int:
          UpperCAmelCase__				:			Tuple							=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

          UpperCAmelCase__				:			int							=    self.get_config()

          return config, pixel_values



     def 			lowerCAmelCase__							(   self	)->       List[Any]:
          return ResNetConfig(
              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			,      image_size=self.image_size			,      )



     def 			lowerCAmelCase__							(   self			,      __UpperCamelCase			,      __UpperCamelCase	)->       Any:
          UpperCAmelCase__				:			Any							=    FlaxResNetModel(config=UpperCamelCase__	)
          UpperCAmelCase__				:			int							=    model(UpperCamelCase__	)

          # Output shape (b, c, h, w)
          self.parent.assertEqual(
              result.last_hidden_state.shape			,      (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)			,      )



     def 			lowerCAmelCase__							(   self			,      __UpperCamelCase			,      __UpperCamelCase	)->       Union[str, Any]:
          UpperCAmelCase__				:			Tuple							=    self.num_labels
          UpperCAmelCase__				:			Optional[Any]							=    FlaxResNetForImageClassification(config=UpperCamelCase__	)
          UpperCAmelCase__				:			Union[str, Any]							=    model(UpperCamelCase__	)
          self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.num_labels)	)



     def 			lowerCAmelCase__							(   self	)->       Tuple:
          UpperCAmelCase__				:			Union[str, Any]							=    self.prepare_config_and_inputs()
          UpperCAmelCase__				:			Optional[int]							=    config_and_inputs
          UpperCAmelCase__				:			Any							=    {'pixel_values': pixel_values}
          return config, inputs_dict







@require_flax
class 	_lowercase    (							__A						, unittest.TestCase					):


     '''simple docstring'''





     _A		    =				(FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

     _A		    =				False
     _A		    =				False
     _A		    =				False



     def 			lowerCAmelCase__							(   self	)->       List[Any]:
          UpperCAmelCase__				:			int							=    FlaxResNetModelTester(self	)
          UpperCAmelCase__				:			Optional[int]							=    ConfigTester(self			,      config_class=UpperCamelCase__			,      has_text_modality=UpperCamelCase__	)



     def 			lowerCAmelCase__							(   self	)->       Union[str, Any]:
          self.create_and_test_config_common_properties()
          self.config_tester.create_and_test_config_to_json_string()
          self.config_tester.create_and_test_config_to_json_file()
          self.config_tester.create_and_test_config_from_and_save_pretrained()
          self.config_tester.create_and_test_config_with_num_labels()
          self.config_tester.check_config_can_be_init_without_params()
          self.config_tester.check_config_arguments_init()



     def 			lowerCAmelCase__							(   self	)->       int:
          return



     def 			lowerCAmelCase__							(   self	)->       List[str]:
          UpperCAmelCase__				:			Dict							=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*UpperCamelCase__	)



     def 			lowerCAmelCase__							(   self	)->       Any:
          UpperCAmelCase__				:			Optional[int]							=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__	)



     @unittest.skip(reason="ResNet does not use inputs_embeds"	)
     def 			lowerCAmelCase__							(   self	)->       int:
          pass



     @unittest.skip(reason="ResNet does not support input and output embeddings"	)
     def 			lowerCAmelCase__							(   self	)->       int:
          pass



     def 			lowerCAmelCase__							(   self	)->       Optional[int]:
          UpperCAmelCase__				:			Optional[Any]							=    self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               UpperCAmelCase__				:			Optional[int]							=    model_class(UpperCamelCase__	)
               UpperCAmelCase__				:			List[str]							=    inspect.signature(model.__call__	)
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               UpperCAmelCase__				:			Any							=    [*signature.parameters.keys()]

               UpperCAmelCase__				:			Tuple							=    ['pixel_values']
               self.assertListEqual(arg_names[:1]			,      UpperCamelCase__	)



     def 			lowerCAmelCase__							(   self	)->       Dict:
          def check_hidden_states_output(__UpperCamelCase			,      __UpperCamelCase			,      __UpperCamelCase	):
               UpperCAmelCase__				:			List[Any]							=    model_class(UpperCamelCase__	)

               UpperCAmelCase__				:			Tuple							=    model(**self._prepare_for_class(UpperCamelCase__			,      UpperCamelCase__	)	)

               UpperCAmelCase__				:			Optional[Any]							=    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

               UpperCAmelCase__				:			int							=    self.model_tester.num_stages
               self.assertEqual(len(UpperCamelCase__	)			,      expected_num_stages + 1	)



     @unittest.skip(reason="ResNet does not use feedforward chunking"	)
     def 			lowerCAmelCase__							(   self	)->       Any:
          pass



     def 			lowerCAmelCase__							(   self	)->       Union[str, Any]:
          UpperCAmelCase__				:			List[str]							=    self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               with self.subTest(model_class.__name__	):
                    UpperCAmelCase__				:			int							=    self._prepare_for_class(UpperCamelCase__			,      UpperCamelCase__	)
                    UpperCAmelCase__				:			int							=    model_class(UpperCamelCase__	)

                    @jax.jit
                    def model_jitted(__UpperCamelCase			,      **__UpperCamelCase	):
                         return model(pixel_values=UpperCamelCase__			,      **UpperCamelCase__	)

                    with self.subTest("JIT Enabled"	):
                         UpperCAmelCase__				:			Optional[Any]							=    model_jitted(**UpperCamelCase__	).to_tuple()

                    with self.subTest("JIT Disabled"	):
                         with jax.disable_jit():
                              UpperCAmelCase__				:			int							=    model_jitted(**UpperCamelCase__	).to_tuple()

                    self.assertEqual(len(UpperCamelCase__	)			,      len(UpperCamelCase__	)	)
                    for jitted_output, output in zip(UpperCamelCase__			,      UpperCamelCase__	):
                         self.assertEqual(jitted_output.shape			,      output.shape	)





def 				a__			(					):






     '''simple docstring'''
     UpperCAmelCase__				:			Dict							=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
     return image







@require_flax
class 	_lowercase    (							unittest.TestCase					):


     '''simple docstring'''





     @cached_property
     def 			lowerCAmelCase__							(   self	)->       Tuple:
          return AutoImageProcessor.from_pretrained("microsoft/resnet-50"	) if is_vision_available() else None



     @slow
     def 			lowerCAmelCase__							(   self	)->       Any:
          UpperCAmelCase__				:			List[str]							=    FlaxResNetForImageClassification.from_pretrained("microsoft/resnet-50"	)

          UpperCAmelCase__				:			List[Any]							=    self.default_image_processor
          UpperCAmelCase__				:			int							=    prepare_img()
          UpperCAmelCase__				:			List[Any]							=    image_processor(images=UpperCamelCase__			,      return_tensors="np"	)

          UpperCAmelCase__				:			Optional[Any]							=    model(**UpperCamelCase__	)

          # verify the logits
          UpperCAmelCase__				:			List[str]							=    (1, 10_00)
          self.assertEqual(outputs.logits.shape			,      UpperCamelCase__	)

          UpperCAmelCase__				:			Optional[int]							=    jnp.array([-11.1069, -9.7877, -8.3777]	)

          self.assertTrue(jnp.allclose(outputs.logits[0, :3]			,      UpperCamelCase__			,      atol=1E-4	)	)











import json
import os
import unittest

from transformers import HerbertTokenizer, HerbertTokenizerFast
from transformers.models.herbert.tokenization_herbert import VOCAB_FILES_NAMES
from transformers.testing_utils import get_tests_dir, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class 				__a				(	__A			,						unittest.TestCase    ):





		'''simple docstring'''



		UpperCAmelCase__   :					Optional[Any]					     =    HerbertTokenizer
		UpperCAmelCase__   :					List[Any]					     =    HerbertTokenizerFast
		UpperCAmelCase__   :					Optional[Any]					     =    True


		def 	__snake_case      (    self							):
						super().setUp()

						# Use a simpler test file without japanese/chinese characters
						with open(F'''{get_tests_dir()}/fixtures/sample_text_no_unicode.txt'''			,     encoding='utf-8'							) as f_data:
										SCREAMING_SNAKE_CASE_ :						Dict    				= f_data.read().replace('\n\n'			,     '\n'							).strip()

						SCREAMING_SNAKE_CASE_ :						str    				= [
						    '<s>',
						    '</s>',
						    'l',
						    'o',
						    'w',
						    'e',
						    'r',
						    's',
						    't',
						    'i',
						    'd',
						    'n',
						    'w</w>',
						    'r</w>',
						    't</w>',
						    'lo',
						    'low',
						    'er</w>',
						    'low</w>',
						    'lowest</w>',
						    'newer</w>',
						    'wider</w>',
						    ',</w>',
						    '<unk>',
						]
						SCREAMING_SNAKE_CASE_ :						str    				= dict(zip(UpperCamelCase__			,     range(len(UpperCamelCase__							)							)							)							)
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= ['l o 123', 'lo w 1456', 'e r</w> 1789', '']

						SCREAMING_SNAKE_CASE_ :						int    				= os.path.join(self.tmpdirname			,     VOCAB_FILES_NAMES['vocab_file']							)
						SCREAMING_SNAKE_CASE_ :						int    				= os.path.join(self.tmpdirname			,     VOCAB_FILES_NAMES['merges_file']							)
						with open(self.vocab_file			,     'w'							) as fp:
										fp.write(json.dumps(UpperCamelCase__							)							)
						with open(self.merges_file			,     'w'							) as fp:
										fp.write('\n'.join(UpperCamelCase__							)							)


		def 	__snake_case      (    self			,     UpperCamelCase__							):
						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= 'lower newer'
						SCREAMING_SNAKE_CASE_ :						int    				= 'lower newer'
						return input_text, output_text


		def 	__snake_case      (    self							):
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= self.tokenizer_class(vocab_file=self.vocab_file			,     merges_file=self.merges_file							)

						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= 'lower'
						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= ['low', 'er</w>']
						SCREAMING_SNAKE_CASE_ :						int    				= tokenizer.tokenize(UpperCamelCase__							)
						self.assertListEqual(UpperCamelCase__			,     UpperCamelCase__							)

						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= tokens + ['<unk>']
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= [16, 17, 23]
						self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__							)			,     UpperCamelCase__							)


		def 	__snake_case      (    self							):
						if not self.test_rust_tokenizer:
										return

						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= self.get_tokenizer()
						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= self.get_rust_tokenizer()

						SCREAMING_SNAKE_CASE_ :						List[str]    				= 'lower,newer'

						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= tokenizer.tokenize(UpperCamelCase__							)
						SCREAMING_SNAKE_CASE_ :						Tuple    				= rust_tokenizer.tokenize(UpperCamelCase__							)
						self.assertListEqual(UpperCamelCase__			,     UpperCamelCase__							)

						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= tokenizer.encode(UpperCamelCase__			,     add_special_tokens=UpperCamelCase__							)
						SCREAMING_SNAKE_CASE_ :						Any    				= rust_tokenizer.encode(UpperCamelCase__			,     add_special_tokens=UpperCamelCase__							)
						self.assertListEqual(UpperCamelCase__			,     UpperCamelCase__							)

						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= self.get_rust_tokenizer()
						SCREAMING_SNAKE_CASE_ :						Dict    				= tokenizer.encode(UpperCamelCase__							)
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= rust_tokenizer.encode(UpperCamelCase__							)
						self.assertListEqual(UpperCamelCase__			,     UpperCamelCase__							)






		@slow
		def 	__snake_case      (    self							):
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= self.tokenizer_class.from_pretrained('allegro/herbert-base-cased'							)

						SCREAMING_SNAKE_CASE_ :						List[str]    				= tokenizer.encode('konstruowanie sekwencji'			,     add_special_tokens=UpperCamelCase__							)
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= tokenizer.encode('konstruowanie wielu sekwencji'			,     add_special_tokens=UpperCamelCase__							)

						SCREAMING_SNAKE_CASE_ :						Any    				= tokenizer.build_inputs_with_special_tokens(UpperCamelCase__							)
						SCREAMING_SNAKE_CASE_ :						Dict    				= tokenizer.build_inputs_with_special_tokens(UpperCamelCase__			,     UpperCamelCase__							)

						assert encoded_sentence == [0] + text + [2]
						assert encoded_pair == [0] + text + [2] + text_a + [2]