



import inspect
import unittest

from transformers import ResNetConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
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 torch import nn

 from transformers import ResNetBackbone, ResNetForImageClassification, ResNetModel
 from transformers.models.resnet.modeling_resnet import RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor




class  snake_case__						:






     """simple docstring"""


     def __init__(	self  :  Union[str, Any],			_snake_case  :  Union[str, Any],			_snake_case  :  Optional[Any]=3,			_snake_case  :  List[Any]=3_2,			_snake_case  :  str=3,			_snake_case  :  Union[str, Any]=1_0,			_snake_case  :  List[str]=[1_0, 2_0, 3_0, 4_0],			_snake_case  :  Any=[1, 1, 2, 1],			_snake_case  :  str=True,			_snake_case  :  Optional[Any]=True,			_snake_case  :  Optional[int]="relu",			_snake_case  :  List[str]=3,			_snake_case  :  int=None,			_snake_case  :  Union[str, Any]=["stage2", "stage3", "stage4"],			_snake_case  :  List[str]=[2, 3, 4],			)       ->Union[str, Any]:
            snake_case__							:     Optional[int]            =     parent
            snake_case__							:     Dict            =     batch_size
            snake_case__							:     Union[str, Any]            =     image_size
            snake_case__							:     Optional[Any]            =     num_channels
            snake_case__							:     Optional[int]            =     embeddings_size
            snake_case__							:     Any            =     hidden_sizes
            snake_case__							:     Optional[Any]            =     depths
            snake_case__							:     Dict            =     is_training
            snake_case__							:     Any            =     use_labels
            snake_case__							:     Dict            =     hidden_act
            snake_case__							:     int            =     num_labels
            snake_case__							:     Union[str, Any]            =     scope
            snake_case__							:     Any            =     len(_snake_case )
            snake_case__							:     List[str]            =     out_features
            snake_case__							:     Optional[int]            =     out_indices






     def        lowercase_       (	self  :  Tuple )       ->Tuple:
            snake_case__							:     Dict            =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

            snake_case__							:     Tuple            =     None
            if self.use_labels:
                   snake_case__							:     Union[str, Any]            =     ids_tensor([self.batch_size],			self.num_labels )

            snake_case__							:     Optional[Any]            =     self.get_config()

            return config, pixel_values, labels






     def        lowercase_       (	self  :  Union[str, Any] )       ->Optional[int]:
            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,			out_features=self.out_features,			out_indices=self.out_indices,			)






     def        lowercase_       (	self  :  Optional[int],			_snake_case  :  List[Any],			_snake_case  :  Optional[int],			_snake_case  :  str )       ->int:
            snake_case__							:     Dict            =     ResNetModel(config=_snake_case )
            model.to(_snake_case )
            model.eval()
            snake_case__							:     Tuple            =     model(_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 // 3_2, self.image_size // 3_2),			)






     def        lowercase_       (	self  :  Tuple,			_snake_case  :  Any,			_snake_case  :  Optional[Any],			_snake_case  :  Tuple )       ->List[str]:
            snake_case__							:     Dict            =     self.num_labels
            snake_case__							:     List[str]            =     ResNetForImageClassification(_snake_case )
            model.to(_snake_case )
            model.eval()
            snake_case__							:     List[str]            =     model(_snake_case,			labels=_snake_case )
            self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.num_labels) )






     def        lowercase_       (	self  :  Any,			_snake_case  :  List[Any],			_snake_case  :  Any,			_snake_case  :  Tuple )       ->int:
            snake_case__							:     List[str]            =     ResNetBackbone(config=_snake_case )
            model.to(_snake_case )
            model.eval()
            snake_case__							:     Tuple            =     model(_snake_case )

            # verify feature maps
            self.parent.assertEqual(len(result.feature_maps ),			len(config.out_features ) )
            self.parent.assertListEqual(list(result.feature_maps[0].shape ),			[self.batch_size, self.hidden_sizes[1], 4, 4] )

            # verify channels
            self.parent.assertEqual(len(model.channels ),			len(config.out_features ) )
            self.parent.assertListEqual(model.channels,			config.hidden_sizes[1:] )

            # verify backbone works with out_features=None
            snake_case__							:     Optional[Any]            =     None
            snake_case__							:     int            =     ResNetBackbone(config=_snake_case )
            model.to(_snake_case )
            model.eval()
            snake_case__							:     List[str]            =     model(_snake_case )

            # verify feature maps
            self.parent.assertEqual(len(result.feature_maps ),			1 )
            self.parent.assertListEqual(list(result.feature_maps[0].shape ),			[self.batch_size, self.hidden_sizes[-1], 1, 1] )

            # verify channels
            self.parent.assertEqual(len(model.channels ),			1 )
            self.parent.assertListEqual(model.channels,			[config.hidden_sizes[-1]] )






     def        lowercase_       (	self  :  Tuple )       ->Dict:
            snake_case__							:     str            =     self.prepare_config_and_inputs()
            snake_case__							:     List[Any]            =     config_and_inputs
            snake_case__							:     Any            =     {'pixel_values': pixel_values}
            return config, inputs_dict




@require_torch
class  snake_case__						(	lowerCAmelCase_    ,					lowerCAmelCase_    ,					unittest.TestCase					):






     """simple docstring"""


     _SCREAMING_SNAKE_CASE            =     (
         (
             ResNetModel,
             ResNetForImageClassification,
             ResNetBackbone,
         )
         if is_torch_available()
         else ()
     )
     _SCREAMING_SNAKE_CASE            =     (
         {"""feature-extraction""": ResNetModel, """image-classification""": ResNetForImageClassification}
         if is_torch_available()
         else {}
     )

     _SCREAMING_SNAKE_CASE            =     True
     _SCREAMING_SNAKE_CASE            =     False
     _SCREAMING_SNAKE_CASE            =     False
     _SCREAMING_SNAKE_CASE            =     False
     _SCREAMING_SNAKE_CASE            =     False






     def        lowercase_       (	self  :  int )       ->Optional[Any]:
            snake_case__							:     Optional[int]            =     ResNetModelTester(self )
            snake_case__							:     str            =     ConfigTester(self,			config_class=_snake_case,			has_text_modality=_snake_case )






     def        lowercase_       (	self  :  str )       ->Optional[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        lowercase_       (	self  :  Any )       ->Union[str, Any]:
            return






     @unittest.skip(reason='ResNet does not use inputs_embeds' )
     def        lowercase_       (	self  :  Optional[Any] )       ->Any:
            pass






     @unittest.skip(reason='ResNet does not support input and output embeddings' )
     def        lowercase_       (	self  :  List[Any] )       ->Tuple:
            pass






     def        lowercase_       (	self  :  str )       ->Dict:
            snake_case__							:     int            =     self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   snake_case__							:     Optional[int]            =     model_class(_snake_case )
                   snake_case__							:     Dict            =     inspect.signature(model.forward )
                   # signature.parameters is an OrderedDict => so arg_names order is deterministic
                   snake_case__							:     str            =     [*signature.parameters.keys()]

                   snake_case__							:     Union[str, Any]            =     ['pixel_values']
                   self.assertListEqual(arg_names[:1],			_snake_case )






     def        lowercase_       (	self  :  Dict )       ->Optional[int]:
            snake_case__							:     int            =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*_snake_case )






     def        lowercase_       (	self  :  Dict )       ->Union[str, Any]:
            snake_case__							:     int            =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_backbone(*_snake_case )






     def        lowercase_       (	self  :  Optional[Any] )       ->Union[str, Any]:
            snake_case__							:     Tuple            =     self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   snake_case__							:     int            =     model_class(config=_snake_case )
                   for name, module in model.named_modules():
                          if isinstance(_snake_case,			(nn.BatchNormad, nn.GroupNorm) ):
                                 self.assertTrue(
                                     torch.all(module.weight == 1 ),			msg=F'''Parameter {name} of model {model_class} seems not properly initialized''',			)
                                 self.assertTrue(
                                     torch.all(module.bias == 0 ),			msg=F'''Parameter {name} of model {model_class} seems not properly initialized''',			)






     def        lowercase_       (	self  :  Any )       ->int:
            def check_hidden_states_output(_snake_case  :  Optional[Any],			_snake_case  :  int,			_snake_case  :  List[str] ):
                   snake_case__							:     Optional[Any]            =     model_class(_snake_case )
                   model.to(_snake_case )
                   model.eval()

                   with torch.no_grad():
                          snake_case__							:     Any            =     model(**self._prepare_for_class(_snake_case,			_snake_case ) )

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

                   snake_case__							:     Optional[Any]            =     self.model_tester.num_stages
                   self.assertEqual(len(_snake_case ),			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],			)

            snake_case__							:     Any            =     self.model_tester.prepare_config_and_inputs_for_common()
            snake_case__							:     Union[str, Any]            =     ['basic', 'bottleneck']
            for model_class in self.all_model_classes:
                   for layer_type in layers_type:
                          snake_case__							:     str            =     layer_type
                          snake_case__							:     List[Any]            =     True
                          check_hidden_states_output(_snake_case,			_snake_case,			_snake_case )

                          # check that output_hidden_states also work using config
                          del inputs_dict["output_hidden_states"]
                          snake_case__							:     List[str]            =     True

                          check_hidden_states_output(_snake_case,			_snake_case,			_snake_case )






     @unittest.skip(reason='ResNet does not use feedforward chunking' )
     def        lowercase_       (	self  :  Union[str, Any] )       ->Tuple:
            pass






     def        lowercase_       (	self  :  Dict )       ->int:
            snake_case__							:     Any            =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*_snake_case )






     @slow
     def        lowercase_       (	self  :  str )       ->Optional[int]:
            for model_name in RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   snake_case__							:     Optional[int]            =     ResNetModel.from_pretrained(_snake_case )
                   self.assertIsNotNone(_snake_case )


def 	lowercase_							():
       snake_case__							:     List[str]            =     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
       return image




@require_torch
@require_vision
class  snake_case__						(	unittest.TestCase					):






     """simple docstring"""


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






     @slow
     def        lowercase_       (	self  :  List[str] )       ->List[str]:
            snake_case__							:     Tuple            =     ResNetForImageClassification.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_snake_case )

            snake_case__							:     Tuple            =     self.default_image_processor
            snake_case__							:     Union[str, Any]            =     prepare_img()
            snake_case__							:     Tuple            =     image_processor(images=_snake_case,			return_tensors='pt' ).to(_snake_case )

            # forward pass
            with torch.no_grad():
                   snake_case__							:     List[Any]            =     model(**_snake_case )

            # verify the logits
            snake_case__							:     Union[str, Any]            =     torch.Size((1, 1_0_0_0) )
            self.assertEqual(outputs.logits.shape,			_snake_case )

            snake_case__							:     Optional[Any]            =     torch.tensor([-1_1.1_0_6_9, -9.7_8_7_7, -8.3_7_7_7] ).to(_snake_case )

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




@require_torch
class  snake_case__						(	lowerCAmelCase_    ,					unittest.TestCase					):






     """simple docstring"""


     _SCREAMING_SNAKE_CASE            =     (ResNetBackbone,) if is_torch_available() else ()
     _SCREAMING_SNAKE_CASE            =     False
     _SCREAMING_SNAKE_CASE            =     ResNetConfig






     def        lowercase_       (	self  :  List[str] )       ->Optional[Any]:
            snake_case__							:     Dict            =     ResNetModelTester(self )











import os
import unittest

from transformers.models.bertweet.tokenization_bertweet import VOCAB_FILES_NAMES, BertweetTokenizer

from ...test_tokenization_common import TokenizerTesterMixin




class  snake_case__						(	lowerCAmelCase_    ,					unittest.TestCase					):






     """simple docstring"""


     _SCREAMING_SNAKE_CASE            =     BertweetTokenizer
     _SCREAMING_SNAKE_CASE            =     False






     def        lowercase_       (	self  :  List[str] )       ->Union[str, Any]:
            super().setUp()

            # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
            snake_case__							:     List[Any]            =     ['I', 'm', 'V@@', 'R@@', 'r', 'e@@']
            snake_case__							:     Tuple            =     dict(zip(_snake_case,			range(len(_snake_case ) ) ) )
            snake_case__							:     int            =     ['#version: 0.2', 'a m</w>']
            snake_case__							:     Dict            =     {'unk_token': '<unk>'}

            snake_case__							:     Tuple            =     os.path.join(self.tmpdirname,			VOCAB_FILES_NAMES['vocab_file'] )
            snake_case__							:     Tuple            =     os.path.join(self.tmpdirname,			VOCAB_FILES_NAMES['merges_file'] )
            with open(self.vocab_file,			'w',			encoding='utf-8' ) as fp:
                   for token in vocab_tokens:
                          fp.write(F'''{token} {vocab_tokens[token]}\n''' )
            with open(self.merges_file,			'w',			encoding='utf-8' ) as fp:
                   fp.write('\n'.join(_snake_case ) )






     def        lowercase_       (	self  :  List[Any],			**_snake_case  :  List[Any] )       ->Optional[Any]:
            kwargs.update(self.special_tokens_map )
            return BertweetTokenizer.from_pretrained(self.tmpdirname,			**_snake_case )






     def        lowercase_       (	self  :  Optional[Any],			_snake_case  :  Tuple )       ->Union[str, Any]:
            snake_case__							:     List[str]            =     'I am VinAI Research'
            snake_case__							:     str            =     'I <unk> m V<unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>'
            return input_text, output_text






     def        lowercase_       (	self  :  str )       ->int:
            snake_case__							:     Dict            =     BertweetTokenizer(self.vocab_file,			self.merges_file,			**self.special_tokens_map )
            snake_case__							:     List[str]            =     'I am VinAI Research'
            snake_case__							:     str            =     'I a@@ m V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h'.split()
            snake_case__							:     Optional[int]            =     tokenizer.tokenize(_snake_case )
            self.assertListEqual(_snake_case,			_snake_case )

            snake_case__							:     Tuple            =     tokens + [tokenizer.unk_token]

            snake_case__							:     Dict            =     [4, 3, 5, 6, 3, 3, 3, 4, 7, 9, 3, 9, 3, 3, 3, 3, 3]
            self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case ),			_snake_case )






