




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		:	Optional[int]      ,						_A		:	int      ,						_A		:	List[str]=13      ,						_A		:	Dict=32      ,						_A		:	Any=3      ,						_A		:	Union[str, Any]=4      ,						_A		:	Optional[Any]=[10, 20, 30, 40]      ,						_A		:	List[str]=[2, 2, 3, 2]      ,						_A		:	str=True      ,						_A		:	Dict=True      ,						_A		:	Tuple=37      ,						_A		:	Optional[int]="gelu"      ,						_A		:	Tuple=10      ,						_A		:	int=0.02      ,						_A		:	List[Any]=["stage2", "stage3", "stage4"]      ,						_A		:	List[Any]=[2, 3, 4]      ,						_A		:	List[Any]=None      ,						)		->    List[Any]:






       """simple docstring"""







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






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






       """simple docstring"""







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

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

       lowercase					:   Optional[Any]								= self.get_config()
       return config, pixel_values, labels






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






       """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=_A      ,						initializer_range=self.initializer_range      ,						out_features=self.out_features      ,						out_indices=self.out_indices      ,						num_labels=self.num_labels      ,						)






  def 					__a			(  self		:	Dict      ,						_A		:	Any      ,						_A		:	List[str]      ,						_A		:	Optional[Any]      )		->    Optional[Any]:






       """simple docstring"""







       lowercase					:   Optional[int]								= ConvNextModel(config=_A      )
       model.to(_A      )
       model.eval()
       lowercase					:   Tuple								= 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 					__a			(  self		:	List[Any]      ,						_A		:	Tuple      ,						_A		:	int      ,						_A		:	int      )		->    int:






       """simple docstring"""







       lowercase					:   int								= ConvNextForImageClassification(_A      )
       model.to(_A      )
       model.eval()
       lowercase					:   List[str]								= model(_A      ,						labels=_A      )
       self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.num_labels)      )






  def 					__a			(  self		:	Union[str, Any]      ,						_A		:	Optional[int]      ,						_A		:	List[Any]      ,						_A		:	str      )		->    Union[str, Any]:






       """simple docstring"""







       lowercase					:   List[str]								= ConvNextBackbone(config=_A      )
       model.to(_A      )
       model.eval()
       lowercase					:   Union[str, Any]								= 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					:   Dict								= ConvNextBackbone(config=_A      )
       model.to(_A      )
       model.eval()
       lowercase					:   List[str]								= 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 					__a			(  self		:	Dict      )		->    List[Any]:






       """simple docstring"""







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


@require_torch
class 						_A					(  snake_case__			,   snake_case__			,   unittest.TestCase      ):
  _UpperCamelCase   :			List[str] =			(
      (
          ConvNextModel,
          ConvNextForImageClassification,
          ConvNextBackbone,
      )
      if is_torch_available()
      else ()
  )
  _UpperCamelCase   :			List[Any] =			(
      {"feature-extraction": ConvNextModel, "image-classification": ConvNextForImageClassification}
      if is_torch_available()
      else {}
  )

  _UpperCamelCase   :			str =			True
  _UpperCamelCase   :			List[Any] =			False
  _UpperCamelCase   :			str =			False
  _UpperCamelCase   :			Optional[int] =			False
  _UpperCamelCase   :			List[Any] =			False






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






       """simple docstring"""







       lowercase					:   int								= ConvNextModelTester(self      )
       lowercase					:   str								= ConfigTester(self      ,						config_class=_A      ,						has_text_modality=_A      ,						hidden_size=37      )






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






       """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 					__a			(  self		:	Tuple      )		->    Optional[Any]:






       """simple docstring"""







       return






  @unittest.skip(reason='''ConvNext does not use inputs_embeds'''      )
  def 					__a			(  self		:	str      )		->    Tuple:






       """simple docstring"""







       pass






  @unittest.skip(reason='''ConvNext does not support input and output embeddings'''      )
  def 					__a			(  self		:	Dict      )		->    Any:






       """simple docstring"""







       pass






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






       """simple docstring"""







       pass






  def 					__a			(  self		:	Any      )		->    Optional[int]:






       """simple docstring"""







       lowercase					:   Optional[int]								= self.model_tester.prepare_config_and_inputs_for_common()

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

            lowercase					:   Tuple								= ["pixel_values"]
            self.assertListEqual(arg_names[:1]      ,						_A      )






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






       """simple docstring"""







       lowercase					:   int								= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_A      )






  def 					__a			(  self		:	int      )		->    int:






       """simple docstring"""







       lowercase					:   Dict								= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_backbone(*_A      )






  def 					__a			(  self		:	int      )		->    int:






       """simple docstring"""







       def check_hidden_states_output(_A		:	Dict      ,						_A		:	Optional[Any]      ,						_A		:	str      ):
            lowercase					:   List[Any]								= model_class(_A      )
            model.to(_A      )
            model.eval()

            with torch.no_grad():
                 lowercase					:   Any								= model(**self._prepare_for_class(_A      ,						_A      )      )

            lowercase					:   List[Any]								= outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

            lowercase					:   List[str]								= self.model_tester.num_stages
            self.assertEqual(len(_A      )      ,						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]      ,						)

       lowercase					:   str								= self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            lowercase					:   List[str]								= True
            check_hidden_states_output(_A      ,						_A      ,						_A      )

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            lowercase					:   int								= True

            check_hidden_states_output(_A      ,						_A      ,						_A      )






  def 					__a			(  self		:	Dict      )		->    Dict:






       """simple docstring"""







       lowercase					:   Optional[Any]								= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_A      )






  @slow
  def 					__a			(  self		:	str      )		->    str:






       """simple docstring"""







       for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            lowercase					:   str								= ConvNextModel.from_pretrained(_A      )
            self.assertIsNotNone(_A      )






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





     '''simple docstring'''


     lowercase					:   List[str]								= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
     return image
@require_torch
@require_vision
class 						_A					(  unittest.TestCase      ):






  @cached_property
  def 					__a			(  self		:	Optional[Any]      )		->    List[Any]:






       """simple docstring"""







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






  @slow
  def 					__a			(  self		:	int      )		->    int:






       """simple docstring"""







       lowercase					:   int								= ConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''      ).to(_A      )

       lowercase					:   List[str]								= self.default_image_processor
       lowercase					:   Tuple								= prepare_img()
       lowercase					:   List[Any]								= image_processor(images=_A      ,						return_tensors='''pt'''      ).to(_A      )

       # forward pass
       with torch.no_grad():
            lowercase					:   List[str]								= model(**_A      )

       # verify the logits
       lowercase					:   Union[str, Any]								= torch.Size((1, 1_000)      )
       self.assertEqual(outputs.logits.shape      ,						_A      )

       lowercase					:   Any								= torch.tensor([-0.0_260, -0.4_739, 0.1_911]      ).to(_A      )

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


@require_torch
class 						_A					(  unittest.TestCase			,   snake_case__      ):
  _UpperCamelCase   :			Optional[Any] =			(ConvNextBackbone,) if is_torch_available() else ()
  _UpperCamelCase   :			int =			ConvNextConfig

  _UpperCamelCase   :			Any =			False






  def 					__a			(  self		:	Optional[int]      )		->    Any:






       """simple docstring"""







       lowercase					:   Optional[Any]								= ConvNextModelTester(self      )
'''simple docstring'''




from typing import TYPE_CHECKING

from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available
from ...utils import OptionalDependencyNotAvailable


__SCREAMING_SNAKE_CASE       :     Optional[int]     =  {"""configuration_gpt_neox""": ["""GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXConfig"""]}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __SCREAMING_SNAKE_CASE       :     Tuple     =  ["""GPTNeoXTokenizerFast"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __SCREAMING_SNAKE_CASE       :     Dict     =  [
      """GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """GPTNeoXForCausalLM""",
      """GPTNeoXForQuestionAnswering""",
      """GPTNeoXForSequenceClassification""",
      """GPTNeoXForTokenClassification""",
      """GPTNeoXLayer""",
      """GPTNeoXModel""",
      """GPTNeoXPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_gpt_neox import (
        GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST,
        GPTNeoXForCausalLM,
        GPTNeoXForQuestionAnswering,
        GPTNeoXForSequenceClassification,
        GPTNeoXForTokenClassification,
        GPTNeoXLayer,
        GPTNeoXModel,
        GPTNeoXPreTrainedModel,
    )


else:
  import sys

  __SCREAMING_SNAKE_CASE       :     Any     =  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

