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    A_ :

  def __init__(		self :      str     ,							__SCREAMING_SNAKE_CASE :      int     ,							__SCREAMING_SNAKE_CASE :      Union[str, Any]=13     ,							__SCREAMING_SNAKE_CASE :      List[Any]=32     ,							__SCREAMING_SNAKE_CASE :      Any=3     ,							__SCREAMING_SNAKE_CASE :      List[str]=4     ,							__SCREAMING_SNAKE_CASE :      Dict=[10, 20, 30, 40]     ,							__SCREAMING_SNAKE_CASE :      Any=[2, 2, 3, 2]     ,							__SCREAMING_SNAKE_CASE :      Optional[int]=True     ,							__SCREAMING_SNAKE_CASE :      Any=True     ,							__SCREAMING_SNAKE_CASE :      int=37     ,							__SCREAMING_SNAKE_CASE :      Dict="gelu"     ,							__SCREAMING_SNAKE_CASE :      Any=10     ,							__SCREAMING_SNAKE_CASE :      int=0.02     ,							__SCREAMING_SNAKE_CASE :      List[Any]=["stage2", "stage3", "stage4"]     ,							__SCREAMING_SNAKE_CASE :      Optional[Any]=[2, 3, 4]     ,							__SCREAMING_SNAKE_CASE :      List[Any]=None     ,							):
   __a       =  parent
   __a       =  batch_size
   __a       =  image_size
   __a       =  num_channels
   __a       =  num_stages
   __a       =  hidden_sizes
   __a       =  depths
   __a       =  is_training
   __a       =  use_labels
   __a       =  intermediate_size
   __a       =  hidden_act
   __a       =  num_labels
   __a       =  initializer_range
   __a       =  out_features
   __a       =  out_indices
   __a       =  scope

  def 		_UpperCAmelCase   (		self :      Union[str, Any]       ):
   __a       =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

   __a       =  self.get_config()

   return config, pixel_values, labels

  def 		_UpperCAmelCase   (		self :      int       ):
   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=__SCREAMING_SNAKE_CASE     ,							initializer_range=self.initializer_range     ,							out_features=self.out_features     ,							out_indices=self.out_indices     ,							num_labels=self.num_labels     ,							)

  def 		_UpperCAmelCase   (		self :      Union[str, Any]     ,							__SCREAMING_SNAKE_CASE :      List[str]     ,							__SCREAMING_SNAKE_CASE :      int     ,							__SCREAMING_SNAKE_CASE :      Any       ):
   __a       =  ConvNextVaModel(config=__SCREAMING_SNAKE_CASE       )
   model.to(__SCREAMING_SNAKE_CASE       )
   model.eval()
   __a       =  model(__SCREAMING_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 // 32, self.image_size // 32)     ,							)

  def 		_UpperCAmelCase   (		self :      List[Any]     ,							__SCREAMING_SNAKE_CASE :      Union[str, Any]     ,							__SCREAMING_SNAKE_CASE :      Tuple     ,							__SCREAMING_SNAKE_CASE :      Tuple       ):
   __a       =  ConvNextVaForImageClassification(__SCREAMING_SNAKE_CASE       )
   model.to(__SCREAMING_SNAKE_CASE       )
   model.eval()
   __a       =  model(__SCREAMING_SNAKE_CASE     ,							labels=__SCREAMING_SNAKE_CASE       )
   self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_labels)       )

  def 		_UpperCAmelCase   (		self :      List[str]     ,							__SCREAMING_SNAKE_CASE :      Optional[int]     ,							__SCREAMING_SNAKE_CASE :      Optional[int]     ,							__SCREAMING_SNAKE_CASE :      Dict       ):
   __a       =  ConvNextVaBackbone(config=__SCREAMING_SNAKE_CASE       )
   model.to(__SCREAMING_SNAKE_CASE       )
   model.eval()
   __a       =  model(__SCREAMING_SNAKE_CASE       )

   # 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
   __a       =  None
   __a       =  ConvNextVaBackbone(config=__SCREAMING_SNAKE_CASE       )
   model.to(__SCREAMING_SNAKE_CASE       )
   model.eval()
   __a       =  model(__SCREAMING_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 		_UpperCAmelCase   (		self :      Tuple       ):
   __a       =  self.prepare_config_and_inputs()
   __a       ,     __a       ,     __a       =  config_and_inputs
   __a       =  {"pixel_values": pixel_values}
   return config, inputs_dict

  def 		_UpperCAmelCase   (		self :      List[str]       ):
   __a       =  self.prepare_config_and_inputs()
   __a       ,     __a       ,     __a       =  config_and_inputs
   __a       =  {"pixel_values": pixel_values, "labels": labels}
   return config, inputs_dict
@require_torch
class    A_ (       a_			,       a_			,       unittest.TestCase ):
  _SCREAMING_SNAKE_CASE    				=			(
      (
          ConvNextVaModel,
          ConvNextVaForImageClassification,
          ConvNextVaBackbone,
      )
      if is_torch_available()
      else ()
  )
  _SCREAMING_SNAKE_CASE    				=			(
      {"""feature-extraction""": ConvNextVaModel, """image-classification""": ConvNextVaForImageClassification}
      if is_torch_available()
      else {}
  )

  _SCREAMING_SNAKE_CASE    				=			False
  _SCREAMING_SNAKE_CASE    				=			False
  _SCREAMING_SNAKE_CASE    				=			False
  _SCREAMING_SNAKE_CASE    				=			False
  _SCREAMING_SNAKE_CASE    				=			False

  def 		_UpperCAmelCase   (		self :      Union[str, Any]       ):
   __a       =  ConvNextVaModelTester(self       )
   __a       =  ConfigTester(self     ,							config_class=__SCREAMING_SNAKE_CASE     ,							has_text_modality=__SCREAMING_SNAKE_CASE     ,							hidden_size=37       )

  def 		_UpperCAmelCase   (		self :      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 		_UpperCAmelCase   (		self :      str       ):
   return

  @unittest.skip(reason="ConvNextV2 does not use inputs_embeds"       )
  def 		_UpperCAmelCase   (		self :      Any       ):
   pass

  @unittest.skip(reason="ConvNextV2 does not support input and output embeddings"       )
  def 		_UpperCAmelCase   (		self :      Optional[int]       ):
   pass

  @unittest.skip(reason="ConvNextV2 does not use feedforward chunking"       )
  def 		_UpperCAmelCase   (		self :      Any       ):
   pass

  def 		_UpperCAmelCase   (		self :      Union[str, Any]       ):
   if not self.model_tester.is_training:
    return

   for model_class in self.all_model_classes:
    __a       ,     __a       =  self.model_tester.prepare_config_and_inputs_with_labels()
    __a       =  True

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

    __a       =  model_class(__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.train()
    __a       =  self._prepare_for_class(__SCREAMING_SNAKE_CASE     ,							__SCREAMING_SNAKE_CASE     ,							return_labels=__SCREAMING_SNAKE_CASE       )
    __a       =  model(**__SCREAMING_SNAKE_CASE       ).loss
    loss.backward()

  def 		_UpperCAmelCase   (		self :      str       ):
   if not self.model_tester.is_training:
    return

   for model_class in self.all_model_classes:
    __a       ,     __a       =  self.model_tester.prepare_config_and_inputs_with_labels()
    __a       =  False
    __a       =  True

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

    __a       =  model_class(__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.gradient_checkpointing_enable()
    model.train()
    __a       =  self._prepare_for_class(__SCREAMING_SNAKE_CASE     ,							__SCREAMING_SNAKE_CASE     ,							return_labels=__SCREAMING_SNAKE_CASE       )
    __a       =  model(**__SCREAMING_SNAKE_CASE       ).loss
    loss.backward()

  def 		_UpperCAmelCase   (		self :      Dict       ):
   __a       ,     __a       =  self.model_tester.prepare_config_and_inputs_for_common()

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

    __a       =  ["pixel_values"]
    self.assertListEqual(arg_names[:1]     ,							__SCREAMING_SNAKE_CASE       )

  def 		_UpperCAmelCase   (		self :      Optional[Any]       ):
   __a       =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE       )

  def 		_UpperCAmelCase   (		self :      Tuple       ):
   def check_hidden_states_output(__SCREAMING_SNAKE_CASE :      Tuple     ,							__SCREAMING_SNAKE_CASE :      Union[str, Any]     ,							__SCREAMING_SNAKE_CASE :      int       ):
    __a       =  model_class(__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()

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

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

    __a       =  self.model_tester.num_stages
    self.assertEqual(len(__SCREAMING_SNAKE_CASE       )     ,							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]     ,							)

   __a       ,     __a       =  self.model_tester.prepare_config_and_inputs_for_common()

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

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

    check_hidden_states_output(__SCREAMING_SNAKE_CASE     ,							__SCREAMING_SNAKE_CASE     ,							__SCREAMING_SNAKE_CASE       )

  def 		_UpperCAmelCase   (		self :      List[Any]       ):
   __a       =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE       )

  @slow
  def 		_UpperCAmelCase   (		self :      Union[str, Any]       ):
   for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    __a       =  ConvNextVaModel.from_pretrained(__SCREAMING_SNAKE_CASE       )
    self.assertIsNotNone(__SCREAMING_SNAKE_CASE       )



def   __A		(			):



 """simple docstring"""


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






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

  @cached_property
  def 		_UpperCAmelCase   (		self :      Dict       ):
   return AutoImageProcessor.from_pretrained("facebook/convnextv2-tiny-1k-224"       ) if is_vision_available() else None

  @slow
  def 		_UpperCAmelCase   (		self :      Optional[int]       ):
   __a       =  ConvNextVaForImageClassification.from_pretrained("facebook/convnextv2-tiny-1k-224"       ).to(__SCREAMING_SNAKE_CASE       )

   __a       =  self.default_image_processor
   __a       =  prepare_img()
   __a       =  preprocessor(images=__SCREAMING_SNAKE_CASE     ,							return_tensors="pt"       ).to(__SCREAMING_SNAKE_CASE       )

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

   # verify the logits
   __a       =  torch.Size((1, 10_00)       )
   self.assertEqual(outputs.logits.shape     ,							__SCREAMING_SNAKE_CASE       )

   __a       =  torch.tensor([0.99_96, 0.19_66, -0.43_86]       ).to(__SCREAMING_SNAKE_CASE       )
   self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,							__SCREAMING_SNAKE_CASE     ,							atol=1E-4       )       )



from ... import PretrainedConfig


SCREAMING_SNAKE_CASE :	Any							     =		{
    """sijunhe/nezha-cn-base""": """https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json""",
}
class    A_ (       a_ ):
  _SCREAMING_SNAKE_CASE    				=			NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP
  _SCREAMING_SNAKE_CASE    				=			"""nezha"""

  def __init__(		self :      Any     ,							__SCREAMING_SNAKE_CASE :      List[str]=2_11_28     ,							__SCREAMING_SNAKE_CASE :      Dict=7_68     ,							__SCREAMING_SNAKE_CASE :      List[Any]=12     ,							__SCREAMING_SNAKE_CASE :      str=12     ,							__SCREAMING_SNAKE_CASE :      Optional[Any]=30_72     ,							__SCREAMING_SNAKE_CASE :      Any="gelu"     ,							__SCREAMING_SNAKE_CASE :      int=0.1     ,							__SCREAMING_SNAKE_CASE :      List[str]=0.1     ,							__SCREAMING_SNAKE_CASE :      int=5_12     ,							__SCREAMING_SNAKE_CASE :      Union[str, Any]=64     ,							__SCREAMING_SNAKE_CASE :      List[Any]=2     ,							__SCREAMING_SNAKE_CASE :      int=0.02     ,							__SCREAMING_SNAKE_CASE :      Optional[Any]=1E-12     ,							__SCREAMING_SNAKE_CASE :      int=0.1     ,							__SCREAMING_SNAKE_CASE :      Any=0     ,							__SCREAMING_SNAKE_CASE :      Any=2     ,							__SCREAMING_SNAKE_CASE :      Optional[Any]=3     ,							__SCREAMING_SNAKE_CASE :      List[Any]=True     ,							**__SCREAMING_SNAKE_CASE :      List[str]     ,							):
   super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE     ,							bos_token_id=__SCREAMING_SNAKE_CASE     ,							eos_token_id=__SCREAMING_SNAKE_CASE     ,							**__SCREAMING_SNAKE_CASE       )

   __a       =  vocab_size
   __a       =  hidden_size
   __a       =  num_hidden_layers
   __a       =  num_attention_heads
   __a       =  hidden_act
   __a       =  intermediate_size
   __a       =  hidden_dropout_prob
   __a       =  attention_probs_dropout_prob
   __a       =  max_position_embeddings
   __a       =  max_relative_position
   __a       =  type_vocab_size
   __a       =  initializer_range
   __a       =  layer_norm_eps
   __a       =  classifier_dropout
   __a       =  use_cache


