





import inspect
import unittest

from transformers import ConvNextConfig
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 transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel
     from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor

class   A							:



      def __init__(						self:    List[str]		,	_lowerCAmelCase:    Tuple		,	_lowerCAmelCase:    Dict=13		,	_lowerCAmelCase:    Optional[Any]=32		,	_lowerCAmelCase:    Optional[int]=3		,	_lowerCAmelCase:    List[str]=4		,	_lowerCAmelCase:    Any=[10, 20, 30, 40]		,	_lowerCAmelCase:    Tuple=[2, 2, 3, 2]		,	_lowerCAmelCase:    Tuple=True		,	_lowerCAmelCase:    Any=True		,	_lowerCAmelCase:    Union[str, Any]=37		,	_lowerCAmelCase:    Optional[Any]="gelu"		,	_lowerCAmelCase:    Optional[Any]=10		,	_lowerCAmelCase:    Optional[Any]=0.02		,	_lowerCAmelCase:    List[str]=["stage2", "stage3", "stage4"]		,	_lowerCAmelCase:    List[Any]=[2, 3, 4]		,	_lowerCAmelCase:    Any=None		,	)    ->     Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_           =parent
         UpperCAmelCase_           =batch_size
         UpperCAmelCase_           =image_size
         UpperCAmelCase_           =num_channels
         UpperCAmelCase_           =num_stages
         UpperCAmelCase_           =hidden_sizes
         UpperCAmelCase_           =depths
         UpperCAmelCase_           =is_training
         UpperCAmelCase_           =use_labels
         UpperCAmelCase_           =intermediate_size
         UpperCAmelCase_           =hidden_act
         UpperCAmelCase_           =num_labels
         UpperCAmelCase_           =initializer_range
         UpperCAmelCase_           =out_features
         UpperCAmelCase_           =out_indices
         UpperCAmelCase_           =scope



      def 						lowerCAmelCase__     (						self:    Optional[int]							)    ->     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 						lowerCAmelCase__     (						self:    int							)    ->     List[str]:
         '''simple docstring'''

         return ConvNextConfig(
             num_channels=self.num_channels		,	hidden_sizes=self.hidden_sizes		,	depths=self.depths		,	num_stages=self.num_stages		,	hidden_act=self.hidden_act		,	is_decoder=_lowerCAmelCase		,	initializer_range=self.initializer_range		,	out_features=self.out_features		,	out_indices=self.out_indices		,	num_labels=self.num_labels		,	)



      def 						lowerCAmelCase__     (						self:    List[Any]		,	_lowerCAmelCase:    Dict		,	_lowerCAmelCase:    List[Any]		,	_lowerCAmelCase:    List[Any]							)    ->     str:
         '''simple docstring'''

         UpperCAmelCase_           =ConvNextModel(config=_lowerCAmelCase							)
         model.to(_lowerCAmelCase							)
         model.eval()
         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 						lowerCAmelCase__     (						self:    int		,	_lowerCAmelCase:    List[str]		,	_lowerCAmelCase:    List[Any]		,	_lowerCAmelCase:    Any							)    ->     int:
         '''simple docstring'''

         UpperCAmelCase_           =ConvNextForImageClassification(_lowerCAmelCase							)
         model.to(_lowerCAmelCase							)
         model.eval()
         UpperCAmelCase_           =model(_lowerCAmelCase		,	labels=_lowerCAmelCase							)
         self.parent.assertEqual(result.logits.shape		,	(self.batch_size, self.num_labels)							)



      def 						lowerCAmelCase__     (						self:    int		,	_lowerCAmelCase:    List[str]		,	_lowerCAmelCase:    Dict		,	_lowerCAmelCase:    List[Any]							)    ->     Tuple:
         '''simple docstring'''

         UpperCAmelCase_           =ConvNextBackbone(config=_lowerCAmelCase							)
         model.to(_lowerCAmelCase							)
         model.eval()
         UpperCAmelCase_           =model(_lowerCAmelCase							)

         # verify hidden states
         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
         UpperCAmelCase_           =None
         UpperCAmelCase_           =ConvNextBackbone(config=_lowerCAmelCase							)
         model.to(_lowerCAmelCase							)
         model.eval()
         UpperCAmelCase_           =model(_lowerCAmelCase							)

         # 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 						lowerCAmelCase__     (						self:    Optional[Any]							)    ->     int:
         '''simple docstring'''

         UpperCAmelCase_           =self.prepare_config_and_inputs()
         UpperCAmelCase_    ,							UpperCAmelCase_    ,							UpperCAmelCase_           =config_and_inputs
         UpperCAmelCase_           ={"pixel_values": pixel_values}
         return config, inputs_dict

@require_torch
class   A							(						__lowercase		,   __lowercase		,   unittest.TestCase    ):
      _snake_case				  =(
          (
              ConvNextModel,
              ConvNextForImageClassification,
              ConvNextBackbone,
          )
          if is_torch_available()
          else ()
      )
      _snake_case				  =(
          {'''feature-extraction''': ConvNextModel, '''image-classification''': ConvNextForImageClassification}
          if is_torch_available()
          else {}
      )

      _snake_case				  =True
      _snake_case				  =False
      _snake_case				  =False
      _snake_case				  =False
      _snake_case				  =False



      def 						lowerCAmelCase__     (						self:    Dict							)    ->     Dict:
         '''simple docstring'''

         UpperCAmelCase_           =ConvNextModelTester(self							)
         UpperCAmelCase_           =ConfigTester(self		,	config_class=_lowerCAmelCase		,	has_text_modality=_lowerCAmelCase		,	hidden_size=37							)



      def 						lowerCAmelCase__     (						self:    str							)    ->     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 						lowerCAmelCase__     (						self:    int							)    ->     str:
         '''simple docstring'''

         return



      @unittest.skip(reason="ConvNext does not use inputs_embeds"							)
      def 						lowerCAmelCase__     (						self:    Union[str, Any]							)    ->     List[str]:
         '''simple docstring'''

         pass



      @unittest.skip(reason="ConvNext does not support input and output embeddings"							)
      def 						lowerCAmelCase__     (						self:    Dict							)    ->     Optional[Any]:
         '''simple docstring'''

         pass



      @unittest.skip(reason="ConvNext does not use feedforward chunking"							)
      def 						lowerCAmelCase__     (						self:    List[Any]							)    ->     Any:
         '''simple docstring'''

         pass



      def 						lowerCAmelCase__     (						self:    Union[str, Any]							)    ->     List[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.forward							)
            # 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 						lowerCAmelCase__     (						self:    Any							)    ->     Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_           =self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCAmelCase							)



      def 						lowerCAmelCase__     (						self:    List[Any]							)    ->     int:
         '''simple docstring'''

         UpperCAmelCase_           =self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_backbone(*_lowerCAmelCase							)



      def 						lowerCAmelCase__     (						self:    List[Any]							)    ->     Any:
         '''simple docstring'''

         def check_hidden_states_output(_lowerCAmelCase:    List[Any]		,	_lowerCAmelCase:    Optional[Any]		,	_lowerCAmelCase:    int							):
            UpperCAmelCase_           =model_class(_lowerCAmelCase							)
            model.to(_lowerCAmelCase							)
            model.eval()

            with torch.no_grad():
               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							)

            # ConvNext'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()

         for model_class in self.all_model_classes:
            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 						lowerCAmelCase__     (						self:    str							)    ->     Tuple:
         '''simple docstring'''

         UpperCAmelCase_           =self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase							)






      @slow
      def 						lowerCAmelCase__     (						self:    str							)    ->     List[str]:
         '''simple docstring'''

         for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCAmelCase_           =ConvNextModel.from_pretrained(_lowerCAmelCase							)
            self.assertIsNotNone(_lowerCAmelCase							)




def      a__			(       ):






   '''simple docstring'''






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

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



      @cached_property
      def 						lowerCAmelCase__     (						self:    Tuple							)    ->     List[Any]:
         '''simple docstring'''

         return AutoImageProcessor.from_pretrained("facebook/convnext-tiny-224"							) if is_vision_available() else None






      @slow
      def 						lowerCAmelCase__     (						self:    List[Any]							)    ->     List[Any]:
         '''simple docstring'''

         UpperCAmelCase_           =ConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"							).to(_lowerCAmelCase							)

         UpperCAmelCase_           =self.default_image_processor
         UpperCAmelCase_           =prepare_img()
         UpperCAmelCase_           =image_processor(images=_lowerCAmelCase		,	return_tensors="pt"							).to(_lowerCAmelCase							)

         # forward pass
         with torch.no_grad():
            UpperCAmelCase_           =model(**_lowerCAmelCase							)

         # verify the logits
         UpperCAmelCase_           =torch.Size((1, 1000)							)
         self.assertEqual(outputs.logits.shape		,	_lowerCAmelCase							)

         UpperCAmelCase_           =torch.tensor([-0.02_60, -0.47_39, 0.19_11]							).to(_lowerCAmelCase							)

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

@require_torch
class   A							(						unittest.TestCase		,   __lowercase    ):
      _snake_case				  =(ConvNextBackbone,) if is_torch_available() else ()
      _snake_case				  =ConvNextConfig

      _snake_case				  =False



      def 						lowerCAmelCase__     (						self:    List[Any]							)    ->     Dict:
         '''simple docstring'''

         UpperCAmelCase_           =ConvNextModelTester(self							)










'''simple docstring'''


from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase_      :  str				     =  logging.get_logger(__name__)

lowerCAmelCase_      :  Any				     =  {
    'transfo-xl-wt103': 'https://huggingface.co/transfo-xl-wt103/resolve/main/config.json',
}







class      SCREAMING_SNAKE_CASE    (						snake_case_				):
      __magic_name__  :       str   							=					'''transfo-xl'''
      __magic_name__  :       List[str]   							=					['''mems''']
      __magic_name__  :       Dict   							=					{
          '''n_token''': '''vocab_size''',
          '''hidden_size''': '''d_model''',
          '''num_attention_heads''': '''n_head''',
          '''num_hidden_layers''': '''n_layer''',
      }






      def __init__( self   :  Union[str, Any]						,						lowercase__   :  List[Any]=26_7735						,						lowercase__   :  Optional[Any]=[2_0000, 4_0000, 20_0000]						,						lowercase__   :  Optional[Any]=1024						,						lowercase__   :  str=1024						,						lowercase__   :  Any=16						,						lowercase__   :  int=64						,						lowercase__   :  str=4096						,						lowercase__   :  Union[str, Any]=4						,						lowercase__   :  List[Any]=False						,						lowercase__   :  List[Any]=18						,						lowercase__   :  str=1600						,						lowercase__   :  str=1000						,						lowercase__   :  Any=True						,						lowercase__   :  Optional[Any]=True						,						lowercase__   :  Union[str, Any]=0						,						lowercase__   :  str=-1						,						lowercase__   :  int=True						,						lowercase__   :  str=0.1						,						lowercase__   :  Optional[Any]=0.0						,						lowercase__   :  Tuple=True						,						lowercase__   :  Optional[int]="normal"						,						lowercase__   :  str=0.01						,						lowercase__   :  List[str]=0.01						,						lowercase__   :  Union[str, Any]=0.02						,						lowercase__   :  str=1e-5						,						lowercase__   :  Any=0						,						**lowercase__   :  List[str]						,						):


           '''simple docstring'''

           a_     :							Optional[Any]     						=  vocab_size
           a_     :							Optional[int]     						=  []
           self.cutoffs.extend(lowercase__       )
           if proj_share_all_but_first:
                a_     :							Any     						=  [False] + [True] * len(self.cutoffs       )
           else:
                a_     :							Tuple     						=  [False] + [False] * len(self.cutoffs       )
           a_     :							Tuple     						=  d_model
           a_     :							Optional[int]     						=  d_embed
           a_     :							List[Any]     						=  d_head
           a_     :							List[str]     						=  d_inner
           a_     :							Tuple     						=  div_val
           a_     :							Dict     						=  pre_lnorm
           a_     :							Optional[Any]     						=  n_layer
           a_     :							Dict     						=  n_head
           a_     :							Any     						=  mem_len
           a_     :							Union[str, Any]     						=  same_length
           a_     :							Dict     						=  attn_type
           a_     :							List[str]     						=  clamp_len
           a_     :							str     						=  sample_softmax
           a_     :							Any     						=  adaptive
           a_     :							List[Any]     						=  dropout
           a_     :							str     						=  dropatt
           a_     :							Dict     						=  untie_r
           a_     :							Tuple     						=  init
           a_     :							Optional[int]     						=  init_range
           a_     :							List[Any]     						=  proj_init_std
           a_     :							Optional[int]     						=  init_std
           a_     :							int     						=  layer_norm_epsilon
           super().__init__(eos_token_id=lowercase__						,						**lowercase__       )






      @property
      def 							lowercase_						( self   :  int       ):


           '''simple docstring'''

           logger.info(F"The model {self.model_type} is one of the few models that has no sequence length limit."       )
           return -1






      @max_position_embeddings.setter
      def 							lowercase_						( self   :  Optional[int]						,						lowercase__   :  Tuple       ):


           '''simple docstring'''

           raise NotImplementedError(
               F"The model {self.model_type} is one of the few models that has no sequence length limit."       )
