


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




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






      def        lowerCamelCase       (					self						:     Any )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		str		=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

             _lowerCamelCase       :		Tuple		=None
             if self.use_labels:
                    _lowerCamelCase       :		List[str]		=ids_tensor([self.batch_size]			,   self.num_labels )

             _lowerCamelCase       :		List[str]		=self.get_config()

             return config, pixel_values, labels






      def        lowerCamelCase       (					self						:     List[Any] )					->						Tuple:
             """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=lowercase_			,   initializer_range=self.initializer_range			,   out_features=self.out_features			,   out_indices=self.out_indices			,   num_labels=self.num_labels			,   )






      def        lowerCamelCase       (					self						:     Tuple			,   lowercase_						:     Union[str, Any]			,   lowercase_						:     Optional[Any]			,   lowercase_						:     int )					->						int:
             """simple docstring"""




             _lowerCamelCase       :		Tuple		=ConvNextVaModel(config=lowercase_ )
             model.to(lowercase_ )
             model.eval()
             _lowerCamelCase       :		Optional[Any]		=model(lowercase_ )
             # 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						:     str			,   lowercase_						:     int			,   lowercase_						:     List[str]			,   lowercase_						:     int )					->						List[Any]:
             """simple docstring"""




             _lowerCamelCase       :		List[Any]		=ConvNextVaForImageClassification(lowercase_ )
             model.to(lowercase_ )
             model.eval()
             _lowerCamelCase       :		str		=model(lowercase_			,   labels=lowercase_ )
             self.parent.assertEqual(result.logits.shape			,   (self.batch_size, self.num_labels) )






      def        lowerCamelCase       (					self						:     List[Any]			,   lowercase_						:     List[str]			,   lowercase_						:     Dict			,   lowercase_						:     Any )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		List[str]		=ConvNextVaBackbone(config=lowercase_ )
             model.to(lowercase_ )
             model.eval()
             _lowerCamelCase       :		Optional[Any]		=model(lowercase_ )

             # 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
             _lowerCamelCase       :		Tuple		=None
             _lowerCamelCase       :		str		=ConvNextVaBackbone(config=lowercase_ )
             model.to(lowercase_ )
             model.eval()
             _lowerCamelCase       :		Any		=model(lowercase_ )

             # 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						:     int )					->						Optional[Any]:
             """simple docstring"""




             _lowerCamelCase       :		Optional[Any]		=self.prepare_config_and_inputs()
             _lowerCamelCase	,     _lowerCamelCase	,     _lowerCamelCase       :		Dict		=config_and_inputs
             _lowerCamelCase       :		List[str]		={'pixel_values': pixel_values}
             return config, inputs_dict






      def        lowerCamelCase       (					self						:     int )					->						str:
             """simple docstring"""




             _lowerCamelCase       :		Optional[Any]		=self.prepare_config_and_inputs()
             _lowerCamelCase	,     _lowerCamelCase	,     _lowerCamelCase       :		Dict		=config_and_inputs
             _lowerCamelCase       :		List[Any]		={'pixel_values': pixel_values, 'labels': labels}
             return config, inputs_dict




@require_torch
class A  (   UpperCamelCase_    ,							UpperCamelCase_    ,							unittest.TestCase     ):
      UpperCamelCase__							:  Union[str, Any]      =(
          (
              ConvNextVaModel,
              ConvNextVaForImageClassification,
              ConvNextVaBackbone,
          )
          if is_torch_available()
          else ()
      )
      UpperCamelCase__							:  Optional[int]      =(
          {'feature-extraction': ConvNextVaModel, 'image-classification': ConvNextVaForImageClassification}
          if is_torch_available()
          else {}
      )

      UpperCamelCase__							:  Union[str, Any]      =False
      UpperCamelCase__							:  Union[str, Any]      =False
      UpperCamelCase__							:  List[Any]      =False
      UpperCamelCase__							:  Optional[Any]      =False
      UpperCamelCase__							:  Optional[Any]      =False






      def        lowerCamelCase       (					self						:     Dict )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		Any		=ConvNextVaModelTester(self )
             _lowerCamelCase       :		List[Any]		=ConfigTester(self			,   config_class=lowercase_			,   has_text_modality=lowercase_			,   hidden_size=37 )






      def        lowerCamelCase       (					self						:     Optional[int] )					->						Tuple:
             """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						:     Any )					->						Any:
             """simple docstring"""




             return






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




             pass






      @unittest.skip(reason='ConvNextV2 does not support input and output embeddings' )
      def        lowerCamelCase       (					self						:     int )					->						List[str]:
             """simple docstring"""




             pass






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




             pass






      def        lowerCamelCase       (					self						:     str )					->						List[Any]:
             """simple docstring"""




             if not self.model_tester.is_training:
                    return

             for model_class in self.all_model_classes:
                    _lowerCamelCase	,     _lowerCamelCase       :		int		=self.model_tester.prepare_config_and_inputs_with_labels()
                    _lowerCamelCase       :		Any		=True

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

                    _lowerCamelCase       :		Optional[Any]		=model_class(lowercase_ )
                    model.to(lowercase_ )
                    model.train()
                    _lowerCamelCase       :		str		=self._prepare_for_class(lowercase_			,   lowercase_			,   return_labels=lowercase_ )
                    _lowerCamelCase       :		str		=model(**lowercase_ ).loss
                    loss.backward()






      def        lowerCamelCase       (					self						:     Any )					->						str:
             """simple docstring"""




             if not self.model_tester.is_training:
                    return

             for model_class in self.all_model_classes:
                    _lowerCamelCase	,     _lowerCamelCase       :		Optional[int]		=self.model_tester.prepare_config_and_inputs_with_labels()
                    _lowerCamelCase       :		str		=False
                    _lowerCamelCase       :		Tuple		=True

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

                    _lowerCamelCase       :		Dict		=model_class(lowercase_ )
                    model.to(lowercase_ )
                    model.gradient_checkpointing_enable()
                    model.train()
                    _lowerCamelCase       :		Optional[int]		=self._prepare_for_class(lowercase_			,   lowercase_			,   return_labels=lowercase_ )
                    _lowerCamelCase       :		Any		=model(**lowercase_ ).loss
                    loss.backward()






      def        lowerCamelCase       (					self						:     str )					->						Tuple:
             """simple docstring"""




             _lowerCamelCase	,     _lowerCamelCase       :		int		=self.model_tester.prepare_config_and_inputs_for_common()

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

                    _lowerCamelCase       :		List[Any]		=['pixel_values']
                    self.assertListEqual(arg_names[:1]			,   lowercase_ )






      def        lowerCamelCase       (					self						:     List[str] )					->						List[Any]:
             """simple docstring"""




             _lowerCamelCase       :		List[Any]		=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*lowercase_ )






      def        lowerCamelCase       (					self						:     List[Any] )					->						str:
             """simple docstring"""




             def check_hidden_states_output(lowercase_						:     str			,   lowercase_						:     Optional[Any]			,   lowercase_						:     Union[str, Any] ):
                    _lowerCamelCase       :		List[str]		=model_class(lowercase_ )
                    model.to(lowercase_ )
                    model.eval()

                    with torch.no_grad():
                           _lowerCamelCase       :		Union[str, Any]		=model(**self._prepare_for_class(lowercase_			,   lowercase_ ) )

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

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

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

             for model_class in self.all_model_classes:
                    _lowerCamelCase       :		Optional[int]		=True
                    check_hidden_states_output(lowercase_			,   lowercase_			,   lowercase_ )

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

                    check_hidden_states_output(lowercase_			,   lowercase_			,   lowercase_ )






      def        lowerCamelCase       (					self						:     Any )					->						int:
             """simple docstring"""




             _lowerCamelCase       :		Union[str, Any]		=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*lowercase_ )






      @slow
      def        lowerCamelCase       (					self						:     Union[str, Any] )					->						List[Any]:
             """simple docstring"""




             for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _lowerCamelCase       :		Tuple		=ConvNextVaModel.from_pretrained(lowercase_ )
                    self.assertIsNotNone(lowercase_ )




def 	a_      (    ):
       '''simple docstring'''





       _lowerCamelCase       :		Union[str, Any]		=Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
       return image




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






      @cached_property
      def        lowerCamelCase       (					self						:     Optional[Any] )					->						Tuple:
             """simple docstring"""




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






      @slow
      def        lowerCamelCase       (					self						:     Optional[int] )					->						Optional[int]:
             """simple docstring"""




             _lowerCamelCase       :		Optional[int]		=ConvNextVaForImageClassification.from_pretrained('facebook/convnextv2-tiny-1k-224' ).to(lowercase_ )

             _lowerCamelCase       :		str		=self.default_image_processor
             _lowerCamelCase       :		Tuple		=prepare_img()
             _lowerCamelCase       :		str		=preprocessor(images=lowercase_			,   return_tensors='pt' ).to(lowercase_ )

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

             # verify the logits
             _lowerCamelCase       :		Optional[Any]		=torch.Size((1, 1000) )
             self.assertEqual(outputs.logits.shape			,   lowercase_ )

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






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


if is_sentencepiece_available():
 from ..ta.tokenization_ta import TaTokenizer
else:
 from ...utils.dummy_sentencepiece_objects import TaTokenizer

lowerCamelCase											=       TaTokenizer

if is_tokenizers_available():
 from ..ta.tokenization_ta_fast import TaTokenizerFast
else:
 from ...utils.dummy_tokenizers_objects import TaTokenizerFast

lowerCamelCase											=       TaTokenizerFast

lowerCamelCase											=       {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase											=       [
     'MT5EncoderModel',
     'MT5ForConditionalGeneration',
     'MT5ForQuestionAnswering',
     'MT5Model',
     'MT5PreTrainedModel',
     'MT5Stack',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase											=       ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model']

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase											=       ['FlaxMT5EncoderModel', 'FlaxMT5ForConditionalGeneration', 'FlaxMT5Model']


if TYPE_CHECKING:
 from .configuration_mta import MTaConfig, MTaOnnxConfig

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_mta import (
      MTaEncoderModel,
      MTaForConditionalGeneration,
      MTaForQuestionAnswering,
      MTaModel,
      MTaPreTrainedModel,
      MTaStack,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel

else:
 import sys

 lowerCamelCase											=       _LazyModule(
     __name__,
     globals()['__file__'],
     _import_structure,
     extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast},
     module_spec=__spec__,
 )


