'''simple docstring'''

import torch
from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.bert.modeling_bert import (
    BERT_INPUTS_DOCSTRING,
    BERT_START_DOCSTRING,
    BertEmbeddings,
    BertLayer,
    BertPooler,
    BertPreTrainedModel,
)
def 			__UpperCamelCase       (   UpperCAmelCase     ):
 lowercase__				:    Tuple  =					torch.exp(lowerCAmelCase_     )
 lowercase__				:    int  =					torch.sum(lowerCAmelCase_		,     dim=1     )  # sum of exp(x_i)
 lowercase__				:    Any  =					torch.sum(x * exp_x		,     dim=1     )  # sum of x_i * exp(x_i)
 return torch.log(lowerCAmelCase_     ) - B / A



class 			UpperCAmelCase  (						nn.Module					):





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase )  ->  int:
        super().__init__()
        lowercase__				:    List[Any]  =					config.output_attentions
        lowercase__				:    Tuple  =					config.output_hidden_states
        lowercase__				:    Union[str, Any]  =					nn.ModuleList([BertLayer(__a ) for _ in range(config.num_hidden_layers )] )
        lowercase__				:    Dict  =					nn.ModuleList([BertHighway(__a ) for _ in range(config.num_hidden_layers )] )

        lowercase__				:    List[str]  =					[-1 for _ in range(config.num_hidden_layers )]

       def _lowerCAmelCase(						self	,   __lowerCAmelCase )  ->  Optional[int]:

        if (type(__a ) is float) or (type(__a ) is int):
         for i in range(len(self.early_exit_entropy ) ):
          lowercase__				:    Optional[Any]  =					x
        else:
         lowercase__				:    List[str]  =					x

       def _lowerCAmelCase(						self	,   __lowerCAmelCase )  ->  Optional[int]:
        lowercase__				:    Union[str, Any]  =					pooler.state_dict()
        for highway in self.highway:
         for name, param in highway.pooler.state_dict().items():
          param.copy_(loaded_model[name] )


       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   )  ->  Optional[Any]:
        lowercase__				:    List[Any]  =					()
        lowercase__				:    Union[str, Any]  =					()
        lowercase__				:    int  =					()
        for i, layer_module in enumerate(self.layer ):
         if self.output_hidden_states:
          lowercase__				:    Optional[int]  =					all_hidden_states + (hidden_states,)

         lowercase__				:    Union[str, Any]  =					layer_module(
             __a	,   __a	,   head_mask[i]	,   __a	,   __a )
         lowercase__				:    Any  =					layer_outputs[0]

         if self.output_attentions:
          lowercase__				:    Optional[int]  =					all_attentions + (layer_outputs[1],)

         lowercase__				:    Optional[int]  =					(hidden_states,)
         if self.output_hidden_states:
          lowercase__				:    str  =					current_outputs + (all_hidden_states,)
         if self.output_attentions:
          lowercase__				:    Dict  =					current_outputs + (all_attentions,)

         lowercase__				:    List[Any]  =					self.highway[i](__a )
         # logits, pooled_output

         if not self.training:
          lowercase__				:    Tuple  =					highway_exit[0]
          lowercase__				:    List[Any]  =					entropy(__a )
          lowercase__				:    Union[str, Any]  =					highway_exit + (highway_entropy,)  # logits, hidden_states(?), entropy
          lowercase__				:    int  =					all_highway_exits + (highway_exit,)

          if highway_entropy < self.early_exit_entropy[i]:
           lowercase__				:    Optional[Any]  =					(highway_logits,) + current_outputs[1:] + (all_highway_exits,)
           raise HighwayException(__a	,   i + 1 )
         else:
          lowercase__				:    Optional[int]  =					all_highway_exits + (highway_exit,)

        # Add last layer
        if self.output_hidden_states:
         lowercase__				:    Any  =					all_hidden_states + (hidden_states,)

        lowercase__				:    Union[str, Any]  =					(hidden_states,)
        if self.output_hidden_states:
         lowercase__				:    Any  =					outputs + (all_hidden_states,)
        if self.output_attentions:
         lowercase__				:    Optional[Any]  =					outputs + (all_attentions,)

        lowercase__				:    List[Any]  =					outputs + (all_highway_exits,)
        return outputs  # last-layer hidden state, (all hidden states), (all attentions), all highway exits



@add_start_docstrings(
    "The Bert Model transformer with early exiting (DeeBERT). "    ,						__a    ,						)
class 			UpperCAmelCase  (						__a					):





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase )  ->  Dict:
        super().__init__(__a )
        lowercase__				:    int  =					config

        lowercase__				:    Tuple  =					BertEmbeddings(__a )
        lowercase__				:    Dict  =					DeeBertEncoder(__a )
        lowercase__				:    List[Any]  =					BertPooler(__a )

        self.init_weights()

       def _lowerCAmelCase(						self )  ->  Union[str, Any]:
        self.encoder.init_highway_pooler(self.pooler )

       def _lowerCAmelCase(						self )  ->  Optional[Any]:
        return self.embeddings.word_embeddings

       def _lowerCAmelCase(						self	,   __lowerCAmelCase )  ->  Optional[int]:
        lowercase__				:    Tuple  =					value

       def _lowerCAmelCase(						self	,   __lowerCAmelCase )  ->  Optional[int]:

        for layer, heads in heads_to_prune.items():
         self.encoder.layer[layer].attention.prune_heads(__a )


       @add_start_docstrings_to_model_forward(__a )
       def _lowerCAmelCase(						self	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   )  ->  Dict:

        if input_ids is not None and inputs_embeds is not None:
         raise ValueError('''You cannot specify both input_ids and inputs_embeds at the same time''' )
        elif input_ids is not None:
         lowercase__				:    Optional[int]  =					input_ids.size()
        elif inputs_embeds is not None:
         lowercase__				:    Dict  =					inputs_embeds.size()[:-1]
        else:
         raise ValueError('''You have to specify either input_ids or inputs_embeds''' )

        lowercase__				:    Dict  =					input_ids.device if input_ids is not None else inputs_embeds.device

        if attention_mask is None:
         lowercase__				:    Any  =					torch.ones(__a	,   device=__a )
        if encoder_attention_mask is None:
         lowercase__				:    Any  =					torch.ones(__a	,   device=__a )
        if token_type_ids is None:
         lowercase__				:    Union[str, Any]  =					torch.zeros(__a	,   dtype=torch.long	,   device=__a )

        # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length]
        # ourselves in which case we just need to make it broadcastable to all heads.
        lowercase__				:    torch.Tensor  =					self.get_extended_attention_mask(__a	,   __a	,   __a )

        # If a 2D ou 3D attention mask is provided for the cross-attention
        # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length]
        if encoder_attention_mask.dim() == 3:
         lowercase__				:    Union[str, Any]  =					encoder_attention_mask[:, None, :, :]
        if encoder_attention_mask.dim() == 2:
         lowercase__				:    str  =					encoder_attention_mask[:, None, None, :]

        lowercase__				:    Optional[Any]  =					encoder_extended_attention_mask.to(
            dtype=next(self.parameters() ).dtype )  # fp16 compatibility
        lowercase__				:    Any  =					(1.0 - encoder_extended_attention_mask) * -1_0_0_0_0.0

        # Prepare head mask if needed
        # 1.0 in head_mask indicate we keep the head
        # attention_probs has shape bsz x n_heads x N x N
        # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads]
        # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length]
        lowercase__				:    Tuple  =					self.get_head_mask(__a	,   self.config.num_hidden_layers )

        lowercase__				:    Union[str, Any]  =					self.embeddings(
            input_ids=__a	,   position_ids=__a	,   token_type_ids=__a	,   inputs_embeds=__a )
        lowercase__				:    Union[str, Any]  =					self.encoder(
            __a	,   attention_mask=__a	,   head_mask=__a	,   encoder_hidden_states=__a	,   encoder_attention_mask=__a	,   )
        lowercase__				:    Union[str, Any]  =					encoder_outputs[0]
        lowercase__				:    List[Any]  =					self.pooler(__a )

        lowercase__				:    Optional[Any]  =					(
            sequence_output,
            pooled_output,
        ) + encoder_outputs[
            1:
        ]  # add hidden_states and attentions if they are here
        return outputs  # sequence_output, pooled_output, (hidden_states), (attentions), highway exits



class 			UpperCAmelCase  (						__a					):





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase	,   __lowerCAmelCase )  ->  Any:
        lowercase__				:    Dict  =					message
        lowercase__				:    Optional[int]  =					exit_layer  # start from 1!



class 			UpperCAmelCase  (						nn.Module					):





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase )  ->  Dict:
        super().__init__()
        lowercase__				:    List[str]  =					BertPooler(__a )
        lowercase__				:    Optional[Any]  =					nn.Dropout(config.hidden_dropout_prob )
        lowercase__				:    int  =					nn.Linear(config.hidden_size	,   config.num_labels )


       def _lowerCAmelCase(						self	,   __lowerCAmelCase )  ->  Optional[int]:
        lowercase__				:    Tuple  =					encoder_outputs[0]
        lowercase__				:    List[Any]  =					self.pooler(__a )
        # "return" pooler_output

        # BertModel
        lowercase__				:    List[Any]  =					(pooler_input, pooler_output) + encoder_outputs[1:]
        # "return" bmodel_output

        # Dropout and classification
        lowercase__				:    Optional[int]  =					bmodel_output[1]

        lowercase__				:    Union[str, Any]  =					self.dropout(__a )
        lowercase__				:    Any  =					self.classifier(__a )

        return logits, pooled_output



@add_start_docstrings(
    "Bert Model (with early exiting - DeeBERT) with a classifier on top,\n    also takes care of multi-layer training. "    ,						__a    ,						)
class 			UpperCAmelCase  (						__a					):





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase )  ->  Optional[int]:
        super().__init__(__a )
        lowercase__				:    List[str]  =					config.num_labels
        lowercase__				:    List[Any]  =					config.num_hidden_layers

        lowercase__				:    List[str]  =					DeeBertModel(__a )
        lowercase__				:    Optional[int]  =					nn.Dropout(config.hidden_dropout_prob )
        lowercase__				:    Tuple  =					nn.Linear(config.hidden_size	,   self.config.num_labels )

        self.init_weights()


       @add_start_docstrings_to_model_forward(__a )
       def _lowerCAmelCase(						self	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase=-1	,   __lowerCAmelCase=False	,   )  ->  Optional[int]:
        lowercase__				:    Any  =					self.num_layers
        try:
         lowercase__				:    Dict  =					self.bert(
             __a	,   attention_mask=__a	,   token_type_ids=__a	,   position_ids=__a	,   head_mask=__a	,   inputs_embeds=__a	,   )
         # sequence_output, pooled_output, (hidden_states), (attentions), highway exits

         lowercase__				:    Tuple  =					outputs[1]

         lowercase__				:    str  =					self.dropout(__a )
         lowercase__				:    Any  =					self.classifier(__a )
         lowercase__				:    List[str]  =					(logits,) + outputs[2:]  # add hidden states and attention if they are here
        except HighwayException as e:
         lowercase__				:    List[str]  =					e.message
         lowercase__				:    Tuple  =					e.exit_layer
         lowercase__				:    Dict  =					outputs[0]

        if not self.training:
         lowercase__				:    Union[str, Any]  =					entropy(__a )
         lowercase__				:    Optional[int]  =					[]
         lowercase__				:    Union[str, Any]  =					[]
        if labels is not None:
         if self.num_labels == 1:
          #  We are doing regression
          lowercase__				:    Dict  =					MSELoss()
          lowercase__				:    Optional[Any]  =					loss_fct(logits.view(-1 )	,   labels.view(-1 ) )
         else:
          lowercase__				:    List[str]  =					CrossEntropyLoss()
          lowercase__				:    Any  =					loss_fct(logits.view(-1	,   self.num_labels )	,   labels.view(-1 ) )

         # work with highway exits
         lowercase__				:    Optional[Any]  =					[]
         for highway_exit in outputs[-1]:
          lowercase__				:    List[str]  =					highway_exit[0]
          if not self.training:
           highway_logits_all.append(__a )
           highway_entropy.append(highway_exit[2] )
          if self.num_labels == 1:
           #  We are doing regression
           lowercase__				:    Optional[Any]  =					MSELoss()
           lowercase__				:    int  =					loss_fct(highway_logits.view(-1 )	,   labels.view(-1 ) )
          else:
           lowercase__				:    Dict  =					CrossEntropyLoss()
           lowercase__				:    List[Any]  =					loss_fct(highway_logits.view(-1	,   self.num_labels )	,   labels.view(-1 ) )
          highway_losses.append(__a )

         if train_highway:
          lowercase__				:    Union[str, Any]  =					(sum(highway_losses[:-1] ),) + outputs
          # exclude the final highway, of course
         else:
          lowercase__				:    List[Any]  =					(loss,) + outputs
        if not self.training:
         lowercase__				:    str  =					outputs + ((original_entropy, highway_entropy), exit_layer)
         if output_layer >= 0:
          lowercase__				:    str  =					(
              (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:]
          )  # use the highway of the last layer

        return outputs  # (loss), logits, (hidden_states), (attentions), (highway_exits)









import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
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_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 ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
	from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor

class 						lowerCAmelCase   :

					'''simple docstring'''

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


												"""simple docstring"""

												__lowercase					:							List[str]			=   parent
												__lowercase					:							str			=   batch_size
												__lowercase					:							List[str]			=   image_size
												__lowercase					:							int			=   num_channels
												__lowercase					:							Optional[int]			=   num_stages
												__lowercase					:							str			=   hidden_sizes
												__lowercase					:							Dict			=   depths
												__lowercase					:							List[str]			=   is_training
												__lowercase					:							Optional[Any]			=   use_labels
												__lowercase					:							Optional[Any]			=   intermediate_size
												__lowercase					:							Tuple			=   hidden_act
												__lowercase					:							List[Any]			=   num_labels
												__lowercase					:							Dict			=   initializer_range
												__lowercase					:							List[str]			=   out_features
												__lowercase					:							str			=   out_indices
												__lowercase					:							Optional[Any]			=   scope



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


												"""simple docstring"""

												__lowercase					:							List[Any]			=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

												__lowercase					:							Optional[int]			=   None
												if self.use_labels:
																			__lowercase					:							Optional[Any]			=   ids_tensor([self.batch_size]						,   self.num_labels							)

												__lowercase					:							Union[str, Any]			=   self.get_config()

												return config, pixel_values, labels



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


												"""simple docstring"""

												return ConvNextVaConfig(
												    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=__a						,   initializer_range=self.initializer_range						,   out_features=self.out_features						,   out_indices=self.out_indices						,   num_labels=self.num_labels						,   )



					def 					lowerCAmelCase (     self :		int						,   __a :		Tuple						,   __a :		Dict						,   __a :		List[str]							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							str			=   ConvNextVaModel(config=__a							)
												model.to(__a							)
												model.eval()
												__lowercase					:							Union[str, Any]			=   model(__a							)
												# 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 :		Any						,   __a :		Tuple						,   __a :		Any						,   __a :		Union[str, Any]							)	->     Optional[int]:


												"""simple docstring"""

												__lowercase					:							int			=   ConvNextVaForImageClassification(__a							)
												model.to(__a							)
												model.eval()
												__lowercase					:							Union[str, Any]			=   model(__a						,   labels=__a							)
												self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.num_labels)							)



					def 					lowerCAmelCase (     self :		int						,   __a :		str						,   __a :		Any						,   __a :		Optional[int]							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							Union[str, Any]			=   ConvNextVaBackbone(config=__a							)
												model.to(__a							)
												model.eval()
												__lowercase					:							Dict			=   model(__a							)

												# 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
												__lowercase					:							List[str]			=   None
												__lowercase					:							str			=   ConvNextVaBackbone(config=__a							)
												model.to(__a							)
												model.eval()
												__lowercase					:							Optional[Any]			=   model(__a							)

												# 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 :		str							)	->     List[str]:


												"""simple docstring"""

												__lowercase					:							Any			=   self.prepare_config_and_inputs()
												__lowercase	,	__lowercase	,	__lowercase					:							Optional[int]			=   config_and_inputs
												__lowercase					:							List[Any]			=   {"""pixel_values""": pixel_values}
												return config, inputs_dict



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


												"""simple docstring"""

												__lowercase					:							Dict			=   self.prepare_config_and_inputs()
												__lowercase	,	__lowercase	,	__lowercase					:							Optional[Any]			=   config_and_inputs
												__lowercase					:							int			=   {"""pixel_values""": pixel_values, """labels""": labels}
												return config, inputs_dict







@require_torch
class 						lowerCAmelCase   (       __a					,				__a					,				unittest.TestCase	):

					'''simple docstring'''

					_A  :	Any     =					(
					    (
					        ConvNextVaModel,
					        ConvNextVaForImageClassification,
					        ConvNextVaBackbone,
					    )
					    if is_torch_available()
					    else ()
					)
					_A  :	Any     =					(
					    {'''feature-extraction''': ConvNextVaModel, '''image-classification''': ConvNextVaForImageClassification}
					    if is_torch_available()
					    else {}
					)

					_A  :	str     =					False
					_A  :	Dict     =					False
					_A  :	str     =					False
					_A  :	str     =					False
					_A  :	Dict     =					False



					def 					lowerCAmelCase (     self :		Optional[Any]							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   ConvNextVaModelTester(self							)
												__lowercase					:							Optional[int]			=   ConfigTester(self						,   config_class=__a						,   has_text_modality=__a						,   hidden_size=37							)



					def 					lowerCAmelCase (     self :		int							)	->     Optional[int]:


												"""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="""ConvNextV2 does not use inputs_embeds"""							)
					def 					lowerCAmelCase (     self :		int							)	->     Any:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""ConvNextV2 does not support input and output embeddings"""							)
					def 					lowerCAmelCase (     self :		Any							)	->     Union[str, Any]:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""ConvNextV2 does not use feedforward chunking"""							)
					def 					lowerCAmelCase (     self :		Any							)	->     Optional[int]:


												"""simple docstring"""

												pass



					def 					lowerCAmelCase (     self :		Union[str, Any]							)	->     List[str]:


												"""simple docstring"""

												if not self.model_tester.is_training:
																			return

												for model_class in self.all_model_classes:
																			__lowercase	,	__lowercase					:							Union[str, Any]			=   self.model_tester.prepare_config_and_inputs_with_labels()
																			__lowercase					:							str			=   True

																			if model_class.__name__ in [
																			    *get_values(__a							),
																			    *get_values(__a							),
																			]:
																										continue

																			__lowercase					:							Union[str, Any]			=   model_class(__a							)
																			model.to(__a							)
																			model.train()
																			__lowercase					:							Tuple			=   self._prepare_for_class(__a						,   __a						,   return_labels=__a							)
																			__lowercase					:							Dict			=   model(**__a							).loss
																			loss.backward()



					def 					lowerCAmelCase (     self :		Optional[int]							)	->     int:


												"""simple docstring"""

												if not self.model_tester.is_training:
																			return

												for model_class in self.all_model_classes:
																			__lowercase	,	__lowercase					:							Tuple			=   self.model_tester.prepare_config_and_inputs_with_labels()
																			__lowercase					:							Dict			=   False
																			__lowercase					:							Optional[Any]			=   True

																			if (
																			    model_class.__name__
																			    in [*get_values(__a							), *get_values(__a							)]
																			    or not model_class.supports_gradient_checkpointing
																			):
																										continue

																			__lowercase					:							List[Any]			=   model_class(__a							)
																			model.to(__a							)
																			model.gradient_checkpointing_enable()
																			model.train()
																			__lowercase					:							List[str]			=   self._prepare_for_class(__a						,   __a						,   return_labels=__a							)
																			__lowercase					:							int			=   model(**__a							).loss
																			loss.backward()



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


												"""simple docstring"""

												__lowercase	,	__lowercase					:							List[str]			=   self.model_tester.prepare_config_and_inputs_for_common()

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

																			__lowercase					:							Optional[Any]			=   ["""pixel_values"""]
																			self.assertListEqual(arg_names[:1]						,   __a							)



					def 					lowerCAmelCase (     self :		List[str]							)	->     Union[str, Any]:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__a							)



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


												"""simple docstring"""

												def check_hidden_states_output(__a :		Any						,   __a :		Tuple						,   __a :		Tuple							):
																			__lowercase					:							Optional[Any]			=   model_class(__a							)
																			model.to(__a							)
																			model.eval()

																			with torch.no_grad():
																										__lowercase					:							int			=   model(**self._prepare_for_class(__a						,   __a							)							)

																			__lowercase					:							Optional[int]			=   outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																			__lowercase					:							Any			=   self.model_tester.num_stages
																			self.assertEqual(len(__a							)						,   expected_num_stages + 1							)

																			# ConvNextV2'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]						,   )

												__lowercase	,	__lowercase					:							Optional[Any]			=   self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																			__lowercase					:							int			=   True
																			check_hidden_states_output(__a						,   __a						,   __a							)

																			# check that output_hidden_states also work using config
																			del inputs_dict["output_hidden_states"]
																			__lowercase					:							Optional[Any]			=   True

																			check_hidden_states_output(__a						,   __a						,   __a							)



					def 					lowerCAmelCase (     self :		Dict							)	->     Union[str, Any]:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__a							)



					@slow
					def 					lowerCAmelCase (     self :		Dict							)	->     Tuple:


												"""simple docstring"""

												for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			__lowercase					:							Optional[int]			=   ConvNextVaModel.from_pretrained(__a							)
																			self.assertIsNotNone(__a							)







def 					snake_case_						(   ):
							__lowercase					:							int			=   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
							return image



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

					'''simple docstring'''

					@cached_property
					def 					lowerCAmelCase (     self :		Any							)	->     List[str]:


												"""simple docstring"""

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



					@slow
					def 					lowerCAmelCase (     self :		Dict							)	->     str:


												"""simple docstring"""

												__lowercase					:							Optional[Any]			=   ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224"""							).to(__a							)

												__lowercase					:							Dict			=   self.default_image_processor
												__lowercase					:							Any			=   prepare_img()
												__lowercase					:							int			=   preprocessor(images=__a						,   return_tensors="""pt"""							).to(__a							)

												# forward pass
												with torch.no_grad():
																			__lowercase					:							int			=   model(**__a							)

												# verify the logits
												__lowercase					:							List[str]			=   torch.Size((1, 1000)							)
												self.assertEqual(outputs.logits.shape						,   __a							)

												__lowercase					:							Tuple			=   torch.tensor([0.9996, 0.1966, -0.4386]							).to(__a							)
												self.assertTrue(torch.allclose(outputs.logits[0, :3]						,   __a						,   atol=1E-4							)							)