





'''simple docstring'''

from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import ResNetConfig
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 TFResNetForImageClassification, TFResNetModel
   from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor
class 							__lowerCAmelCase	:






       """simple docstring"""





       def __init__(				self  :						Optional[int]			,					lowerCAmelCase__  :						str			,					lowerCAmelCase__  :						List[Any]=3			,					lowerCAmelCase__  :						List[Any]=32			,					lowerCAmelCase__  :						Optional[Any]=3			,					lowerCAmelCase__  :						List[Any]=10			,					lowerCAmelCase__  :						List[str]=[10, 20, 30, 40]			,					lowerCAmelCase__  :						Dict=[1, 1, 2, 1]			,					lowerCAmelCase__  :						List[str]=True			,					lowerCAmelCase__  :						Any=True			,					lowerCAmelCase__  :						Union[str, Any]="relu"			,					lowerCAmelCase__  :						List[str]=3			,					lowerCAmelCase__  :						int=None			,					)       ->	int:

            '''simple docstring'''




            _UpperCamelCase							       =			parent
            _UpperCamelCase							       =			batch_size
            _UpperCamelCase							       =			image_size
            _UpperCamelCase							       =			num_channels
            _UpperCamelCase							       =			embeddings_size
            _UpperCamelCase							       =			hidden_sizes
            _UpperCamelCase							       =			depths
            _UpperCamelCase							       =			is_training
            _UpperCamelCase							       =			use_labels
            _UpperCamelCase							       =			hidden_act
            _UpperCamelCase							       =			num_labels
            _UpperCamelCase							       =			scope
            _UpperCamelCase							       =			len(lowerCAmelCase__	)





       def snake_case__		(				self  :						Any	)       ->	Tuple:

            '''simple docstring'''




            _UpperCamelCase							       =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

            _UpperCamelCase							       =			None
            if self.use_labels:
                 _UpperCamelCase							       =			ids_tensor([self.batch_size]			,					self.num_labels	)

            _UpperCamelCase							       =			self.get_config()

            return config, pixel_values, labels





       def snake_case__		(				self  :						Optional[int]	)       ->	List[str]:

            '''simple docstring'''




            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 snake_case__		(				self  :						Optional[int]			,					lowerCAmelCase__  :						Optional[int]			,					lowerCAmelCase__  :						List[Any]			,					lowerCAmelCase__  :						Optional[int]	)       ->	int:

            '''simple docstring'''




            _UpperCamelCase							       =			TFResNetModel(config=lowerCAmelCase__	)
            _UpperCamelCase							       =			model(lowerCAmelCase__	)
            # 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 snake_case__		(				self  :						Any			,					lowerCAmelCase__  :						int			,					lowerCAmelCase__  :						Union[str, Any]			,					lowerCAmelCase__  :						Tuple	)       ->	Optional[Any]:

            '''simple docstring'''




            _UpperCamelCase							       =			self.num_labels
            _UpperCamelCase							       =			TFResNetForImageClassification(lowerCAmelCase__	)
            _UpperCamelCase							       =			model(lowerCAmelCase__			,					labels=lowerCAmelCase__	)
            self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.num_labels)	)







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

            '''simple docstring'''




            _UpperCamelCase							       =			self.prepare_config_and_inputs()
            _UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase							       =			config_and_inputs
            _UpperCamelCase							       =			{'''pixel_values''': pixel_values}
            return config, inputs_dict

@require_tf
class 							__lowerCAmelCase	(       __magic_name__		,		__magic_name__		,		unittest.TestCase					):






       """simple docstring"""
       _snake_case      :	List[Any]									=       (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
       _snake_case      :	Optional[Any]									=       (
           {'feature-extraction': TFResNetModel, 'image-classification': TFResNetForImageClassification}
           if is_tf_available()
           else {}
       )

       _snake_case      :	Optional[Any]									=       False
       _snake_case      :	str									=       False
       _snake_case      :	int									=       False
       _snake_case      :	List[str]									=       False
       _snake_case      :	Optional[Any]									=       False





       def snake_case__		(				self  :						List[str]	)       ->	Dict:

            '''simple docstring'''




            _UpperCamelCase							       =			TFResNetModelTester(self	)
            _UpperCamelCase							       =			ConfigTester(self			,					config_class=lowerCAmelCase__			,					has_text_modality=lowerCAmelCase__	)





       def snake_case__		(				self  :						Optional[Any]	)       ->	str:

            '''simple docstring'''




            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 snake_case__		(				self  :						Optional[Any]	)       ->	List[Any]:

            '''simple docstring'''




            return





       @unittest.skip(reason='''ResNet does not use inputs_embeds'''	)
       def snake_case__		(				self  :						Tuple	)       ->	Tuple:

            '''simple docstring'''




            pass





       @unittest.skip(reason='''ResNet does not support input and output embeddings'''	)
       def snake_case__		(				self  :						Dict	)       ->	Optional[int]:

            '''simple docstring'''




            pass





       def snake_case__		(				self  :						List[Any]	)       ->	str:

            '''simple docstring'''




            _UpperCamelCase       ,							_UpperCamelCase							       =			self.model_tester.prepare_config_and_inputs_for_common()

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

                 _UpperCamelCase							       =			['''pixel_values''']
                 self.assertListEqual(arg_names[:1]			,					lowerCAmelCase__	)





       def snake_case__		(				self  :						List[str]	)       ->	List[str]:

            '''simple docstring'''




            _UpperCamelCase							       =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowerCAmelCase__	)





       def snake_case__		(				self  :						int	)       ->	str:

            '''simple docstring'''




            def check_hidden_states_output(lowerCAmelCase__  :						Any			,					lowerCAmelCase__  :						Optional[int]			,					lowerCAmelCase__  :						Dict	):
                 _UpperCamelCase							       =			model_class(lowerCAmelCase__	)
                 _UpperCamelCase							       =			model(**self._prepare_for_class(lowerCAmelCase__			,					lowerCAmelCase__	)	)

                 _UpperCamelCase							       =			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                 _UpperCamelCase							       =			self.model_tester.num_stages
                 self.assertEqual(len(lowerCAmelCase__	)			,					expected_num_stages + 1	)

                 # ResNet'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 // 4, self.model_tester.image_size // 4]			,					)

            _UpperCamelCase       ,							_UpperCamelCase							       =			self.model_tester.prepare_config_and_inputs_for_common()
            _UpperCamelCase							       =			['''basic''', '''bottleneck''']
            for model_class in self.all_model_classes:
                 for layer_type in layers_type:
                      _UpperCamelCase							       =			layer_type
                      _UpperCamelCase							       =			True
                      check_hidden_states_output(lowerCAmelCase__			,					lowerCAmelCase__			,					lowerCAmelCase__	)

                      # check that output_hidden_states also work using config
                      del inputs_dict["output_hidden_states"]
                      _UpperCamelCase							       =			True

                      check_hidden_states_output(lowerCAmelCase__			,					lowerCAmelCase__			,					lowerCAmelCase__	)





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

            '''simple docstring'''




            _UpperCamelCase							       =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__	)







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

            '''simple docstring'''




            for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _UpperCamelCase							       =			TFResNetModel.from_pretrained(lowerCAmelCase__	)
                 self.assertIsNotNone(lowerCAmelCase__	)







def    a__							(							)						->						Dict:





     """simple docstring"""

     _UpperCamelCase							       =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
     return image




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






       """simple docstring"""





       @cached_property
       def snake_case__		(				self  :						Union[str, Any]	)       ->	int:

            '''simple docstring'''




            return (
                AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
                if is_vision_available()
                else None
            )







       @slow
       def snake_case__		(				self  :						Any	)       ->	Any:

            '''simple docstring'''




            _UpperCamelCase							       =			TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)

            _UpperCamelCase							       =			self.default_image_processor
            _UpperCamelCase							       =			prepare_img()
            _UpperCamelCase							       =			image_processor(images=lowerCAmelCase__			,					return_tensors='''tf'''	)

            # forward pass
            _UpperCamelCase							       =			model(**lowerCAmelCase__	)

            # verify the logits
            _UpperCamelCase							       =			tf.TensorShape((1, 1000)	)
            self.assertEqual(outputs.logits.shape			,					lowerCAmelCase__	)

            _UpperCamelCase							       =			tf.constant([-11.1069, -9.7877, -8.3777]	)

            self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()			,					lowerCAmelCase__			,					atol=1e-4	)	)










'''simple docstring'''

import json
import os
import unittest

from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast
from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin
@require_tokenizers
class 							__lowerCAmelCase	(       __magic_name__		,		unittest.TestCase					):






       """simple docstring"""
       _snake_case      :	int									=       OpenAIGPTTokenizer
       _snake_case      :	Tuple									=       OpenAIGPTTokenizerFast
       _snake_case      :	Union[str, Any]									=       True
       _snake_case      :	Optional[int]									=       False





       def snake_case__		(				self  :						Dict	)       ->	List[Any]:

            '''simple docstring'''




            super().setUp()

            # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
            _UpperCamelCase							       =			[
                '''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>''',
                '''<unk>''',
            ]
            _UpperCamelCase							       =			dict(zip(lowerCAmelCase__			,					range(len(lowerCAmelCase__	)	)	)	)
            _UpperCamelCase							       =			['''#version: 0.2''', '''l o''', '''lo w''', '''e r</w>''', '''''']

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





       def snake_case__		(				self  :						Tuple			,					lowerCAmelCase__  :						List[str]	)       ->	Optional[Any]:

            '''simple docstring'''




            return "lower newer", "lower newer"





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

            '''simple docstring'''




            _UpperCamelCase							       =			OpenAIGPTTokenizer(self.vocab_file			,					self.merges_file	)

            _UpperCamelCase							       =			'''lower'''
            _UpperCamelCase							       =			['''low''', '''er</w>''']
            _UpperCamelCase							       =			tokenizer.tokenize(lowerCAmelCase__	)
            self.assertListEqual(lowerCAmelCase__			,					lowerCAmelCase__	)

            _UpperCamelCase							       =			tokens + ['''<unk>''']
            _UpperCamelCase							       =			[14, 15, 20]
            self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__	)			,					lowerCAmelCase__	)





       def snake_case__		(				self  :						Union[str, Any]			,					lowerCAmelCase__  :						Tuple=15	)       ->	Union[str, Any]:

            '''simple docstring'''




            for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
                 with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""	):
                      _UpperCamelCase							       =			self.rust_tokenizer_class.from_pretrained(lowerCAmelCase__			,					**lowerCAmelCase__	)

                      # Simple input
                      _UpperCamelCase							       =			'''This is a simple input'''
                      _UpperCamelCase							       =			['''This is a simple input 1''', '''This is a simple input 2''']
                      _UpperCamelCase							       =			('''This is a simple input''', '''This is a pair''')
                      _UpperCamelCase							       =			[
                          ('''This is a simple input 1''', '''This is a simple input 2'''),
                          ('''This is a simple pair 1''', '''This is a simple pair 2'''),
                      ]

                      # Simple input tests
                      self.assertRaises(lowerCAmelCase__			,					tokenizer_r.encode			,					lowerCAmelCase__			,					max_length=lowerCAmelCase__			,					padding='''max_length'''	)

                      # Simple input
                      self.assertRaises(lowerCAmelCase__			,					tokenizer_r.encode_plus			,					lowerCAmelCase__			,					max_length=lowerCAmelCase__			,					padding='''max_length'''	)

                      # Simple input
                      self.assertRaises(
                          lowerCAmelCase__			,					tokenizer_r.batch_encode_plus			,					lowerCAmelCase__			,					max_length=lowerCAmelCase__			,					padding='''max_length'''			,					)

                      # Pair input
                      self.assertRaises(lowerCAmelCase__			,					tokenizer_r.encode			,					lowerCAmelCase__			,					max_length=lowerCAmelCase__			,					padding='''max_length'''	)

                      # Pair input
                      self.assertRaises(lowerCAmelCase__			,					tokenizer_r.encode_plus			,					lowerCAmelCase__			,					max_length=lowerCAmelCase__			,					padding='''max_length'''	)

                      # Pair input
                      self.assertRaises(
                          lowerCAmelCase__			,					tokenizer_r.batch_encode_plus			,					lowerCAmelCase__			,					max_length=lowerCAmelCase__			,					padding='''max_length'''			,					)







       def snake_case__		(				self  :						Tuple	)       ->	Optional[Any]:

            '''simple docstring'''




            pass

@require_ftfy
@require_spacy
@require_tokenizers
class 							__lowerCAmelCase	(       __magic_name__					):






       """simple docstring"""
       pass



