


"""simple docstring"""







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  :







    """simple docstring"""




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






    def 	__snake_case							(					self				:    List[str]):
      a						:		Optional[int]      							=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

      a						:		Optional[Any]      							=			self.get_config()

      return config, pixel_values, labels






    def 	__snake_case							(					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=__UpperCAmelCase				, initializer_range=self.initializer_range				, out_features=self.out_features				, out_indices=self.out_indices				, num_labels=self.num_labels				, )






    def 	__snake_case							(					self				:    List[Any]				, __UpperCAmelCase				:    str				, __UpperCAmelCase				:    Any				, __UpperCAmelCase				:    Dict):
      a						:		int      							=			ConvNextVaModel(config=__UpperCAmelCase)
      model.to(__UpperCAmelCase)
      model.eval()
      a						:		List[str]      							=			model(__UpperCAmelCase)
      # 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 	__snake_case							(					self				:    int				, __UpperCAmelCase				:    Tuple				, __UpperCAmelCase				:    int				, __UpperCAmelCase				:    Dict):
      a						:		Optional[Any]      							=			ConvNextVaForImageClassification(__UpperCAmelCase)
      model.to(__UpperCAmelCase)
      model.eval()
      a						:		List[str]      							=			model(__UpperCAmelCase				, labels=__UpperCAmelCase)
      self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.num_labels))






    def 	__snake_case							(					self				:    List[str]				, __UpperCAmelCase				:    Optional[int]				, __UpperCAmelCase				:    List[Any]				, __UpperCAmelCase				:    List[str]):
      a						:		Optional[int]      							=			ConvNextVaBackbone(config=__UpperCAmelCase)
      model.to(__UpperCAmelCase)
      model.eval()
      a						:		List[Any]      							=			model(__UpperCAmelCase)

      # 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						:		Any      							=			None
      a						:		Optional[int]      							=			ConvNextVaBackbone(config=__UpperCAmelCase)
      model.to(__UpperCAmelCase)
      model.eval()
      a						:		Optional[Any]      							=			model(__UpperCAmelCase)

      # 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 	__snake_case							(					self				:    Union[str, Any]):
      a						:		str      							=			self.prepare_config_and_inputs()
      a							,		a							,		a						:		Any      							=			config_and_inputs
      a						:		List[Any]      							=			{"pixel_values": pixel_values}
      return config, inputs_dict






    def 	__snake_case							(					self				:    Union[str, Any]):
      a						:		int      							=			self.prepare_config_and_inputs()
      a							,		a							,		a						:		Optional[int]      							=			config_and_inputs
      a						:		Optional[int]      							=			{"pixel_values": pixel_values, "labels": labels}
      return config, inputs_dict






@require_torch
class 						_A  (  _a    ,_a    ,unittest.TestCase  ):







    """simple docstring"""




    UpperCAmelCase       :							Tuple	=       (
        (
            ConvNextVaModel,
            ConvNextVaForImageClassification,
            ConvNextVaBackbone,
        )
        if is_torch_available()
        else ()
    )
    UpperCAmelCase       :							Optional[int]	=       (
        {"""feature-extraction""": ConvNextVaModel, """image-classification""": ConvNextVaForImageClassification}
        if is_torch_available()
        else {}
    )

    UpperCAmelCase       :							Optional[int]	=       False
    UpperCAmelCase       :							str	=       False
    UpperCAmelCase       :							Optional[int]	=       False
    UpperCAmelCase       :							List[str]	=       False
    UpperCAmelCase       :							Optional[Any]	=       False






    def 	__snake_case							(					self				:    List[str]):
      a						:		Tuple      							=			ConvNextVaModelTester(self)
      a						:		str      							=			ConfigTester(self				, config_class=__UpperCAmelCase				, has_text_modality=__UpperCAmelCase				, hidden_size=37)






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






    @unittest.skip(reason="ConvNextV2 does not use inputs_embeds")
    def 	__snake_case							(					self				:    Optional[int]):
      pass






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






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






    def 	__snake_case							(					self				:    Optional[int]):
      if not self.model_tester.is_training:
        return

      for model_class in self.all_model_classes:
        a							,		a						:		Dict      							=			self.model_tester.prepare_config_and_inputs_with_labels()
        a						:		Tuple      							=			True

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

        a						:		int      							=			model_class(__UpperCAmelCase)
        model.to(__UpperCAmelCase)
        model.train()
        a						:		Dict      							=			self._prepare_for_class(__UpperCAmelCase				, __UpperCAmelCase				, return_labels=__UpperCAmelCase)
        a						:		Optional[int]      							=			model(**__UpperCAmelCase).loss
        loss.backward()






    def 	__snake_case							(					self				:    Dict):
      if not self.model_tester.is_training:
        return

      for model_class in self.all_model_classes:
        a							,		a						:		Optional[Any]      							=			self.model_tester.prepare_config_and_inputs_with_labels()
        a						:		Optional[int]      							=			False
        a						:		Optional[Any]      							=			True

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

        a						:		Tuple      							=			model_class(__UpperCAmelCase)
        model.to(__UpperCAmelCase)
        model.gradient_checkpointing_enable()
        model.train()
        a						:		Union[str, Any]      							=			self._prepare_for_class(__UpperCAmelCase				, __UpperCAmelCase				, return_labels=__UpperCAmelCase)
        a						:		Optional[int]      							=			model(**__UpperCAmelCase).loss
        loss.backward()






    def 	__snake_case							(					self				:    Dict):
      a							,		a						:		Tuple      							=			self.model_tester.prepare_config_and_inputs_for_common()

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

        a						:		Tuple      							=			["pixel_values"]
        self.assertListEqual(arg_names[:1]				, __UpperCAmelCase)






    def 	__snake_case							(					self				:    Dict):
      a						:		Any      							=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__UpperCAmelCase)






    def 	__snake_case							(					self				:    Optional[Any]):
      def check_hidden_states_output(__UpperCAmelCase				:    Union[str, Any]				, __UpperCAmelCase				:    Union[str, Any]				, __UpperCAmelCase				:    Optional[int]):
        a						:		Optional[int]      							=			model_class(__UpperCAmelCase)
        model.to(__UpperCAmelCase)
        model.eval()

        with torch.no_grad():
          a						:		Optional[Any]      							=			model(**self._prepare_for_class(__UpperCAmelCase				, __UpperCAmelCase))

        a						:		Tuple      							=			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

        a						:		List[Any]      							=			self.model_tester.num_stages
        self.assertEqual(len(__UpperCAmelCase)				, 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						:		Union[str, Any]      							=			self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        a						:		List[Any]      							=			True
        check_hidden_states_output(__UpperCAmelCase				, __UpperCAmelCase				, __UpperCAmelCase)

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

        check_hidden_states_output(__UpperCAmelCase				, __UpperCAmelCase				, __UpperCAmelCase)






    def 	__snake_case							(					self				:    Optional[int]):
      a						:		Union[str, Any]      							=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase)






    @slow
    def 	__snake_case							(					self				:    Optional[Any]):
      for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a						:		Tuple      							=			ConvNextVaModel.from_pretrained(__UpperCAmelCase)
        self.assertIsNotNone(__UpperCAmelCase)






def      lowercase			(				)->      List[str]:
  '''simple docstring'''







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







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







    """simple docstring"""




    @cached_property
    def 	__snake_case							(					self				:    Union[str, Any]):
      return AutoImageProcessor.from_pretrained("facebook/convnextv2-tiny-1k-224") if is_vision_available() else None






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

      a						:		Any      							=			self.default_image_processor
      a						:		Union[str, Any]      							=			prepare_img()
      a						:		List[str]      							=			preprocessor(images=__UpperCAmelCase				, return_tensors="pt").to(__UpperCAmelCase)

      # forward pass
      with torch.no_grad():
        a						:		Any      							=			model(**__UpperCAmelCase)

      # verify the logits
      a						:		str      							=			torch.Size((1, 1000))
      self.assertEqual(outputs.logits.shape				, __UpperCAmelCase)

      a						:		Union[str, Any]      							=			torch.tensor([0.9_996, 0.1_966, -0.4_386]).to(__UpperCAmelCase)
      self.assertTrue(torch.allclose(outputs.logits[0, :3]				, __UpperCAmelCase				, atol=1e-4))







'''simple docstring'''

import os
import re
import warnings
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
  from .tokenization_ta import TaTokenizer
else:
  __a: Tuple          =   None


__a: Tuple          =   logging.get_logger(__name__)

__a: Optional[Any]          =   {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""}

__a: Optional[Any]          =   {
    """vocab_file""": {
        """t5-small""": """https://huggingface.co/t5-small/resolve/main/spiece.model""",
        """t5-base""": """https://huggingface.co/t5-base/resolve/main/spiece.model""",
        """t5-large""": """https://huggingface.co/t5-large/resolve/main/spiece.model""",
        """t5-3b""": """https://huggingface.co/t5-3b/resolve/main/spiece.model""",
        """t5-11b""": """https://huggingface.co/t5-11b/resolve/main/spiece.model""",
    },
    """tokenizer_file""": {
        """t5-small""": """https://huggingface.co/t5-small/resolve/main/tokenizer.json""",
        """t5-base""": """https://huggingface.co/t5-base/resolve/main/tokenizer.json""",
        """t5-large""": """https://huggingface.co/t5-large/resolve/main/tokenizer.json""",
        """t5-3b""": """https://huggingface.co/t5-3b/resolve/main/tokenizer.json""",
        """t5-11b""": """https://huggingface.co/t5-11b/resolve/main/tokenizer.json""",
    },
}


# TODO(PVP) - this should be removed in Transformers v5
__a: Tuple          =   {
    """t5-small""": 5_12,
    """t5-base""": 5_12,
    """t5-large""": 5_12,
    """t5-3b""": 5_12,
    """t5-11b""": 5_12,
}



class 			UpperCAmelCase  (						a__					):





       '''simple docstring'''



       SCREAMING_SNAKE_CASE							=    VOCAB_FILES_NAMES
       SCREAMING_SNAKE_CASE							=    PRETRAINED_VOCAB_FILES_MAP
       SCREAMING_SNAKE_CASE							=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       SCREAMING_SNAKE_CASE							=    ["input_ids", "attention_mask"]
       SCREAMING_SNAKE_CASE							=    TaTokenizer

       SCREAMING_SNAKE_CASE							=    []

       def __init__(						self	,   __lowerCAmelCase=None	,   __lowerCAmelCase=None	,   __lowerCAmelCase="</s>"	,   __lowerCAmelCase="<unk>"	,   __lowerCAmelCase="<pad>"	,   __lowerCAmelCase=100	,   __lowerCAmelCase=None	,   **__lowerCAmelCase	,   )  ->  Union[str, Any]:
        # Add extra_ids to the special token list
        if extra_ids > 0 and additional_special_tokens is None:
         lowercase__				:    Union[str, Any]  =					[F"""<extra_id_{i}>""" for i in range(__lowerCAmelCase )]
        elif extra_ids > 0 and additional_special_tokens is not None:
         # Check that we have the right number of extra special tokens
         lowercase__				:    Dict  =					len(set(filter(lambda __lowerCAmelCase : bool('''extra_id_''' in str(__lowerCAmelCase ) )	,   __lowerCAmelCase ) ) )
         if extra_tokens != extra_ids:
          raise ValueError(
              F"""Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are"""
              ''' provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids'''
              ''' tokens''' )

        super().__init__(
            __lowerCAmelCase	,   tokenizer_file=__lowerCAmelCase	,   eos_token=__lowerCAmelCase	,   unk_token=__lowerCAmelCase	,   pad_token=__lowerCAmelCase	,   extra_ids=__lowerCAmelCase	,   additional_special_tokens=__lowerCAmelCase	,   **__lowerCAmelCase	,   )

        lowercase__				:    Union[str, Any]  =					vocab_file
        lowercase__				:    Optional[int]  =					False if not self.vocab_file else True
        lowercase__				:    Any  =					extra_ids

       @staticmethod
       def _lowerCAmelCase(						__lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )  ->  Union[str, Any]:
        if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes:
         lowercase__				:    Any  =					TaTokenizerFast.max_model_input_sizes[pretrained_model_name_or_path]
         if init_max_model_length is not None and init_max_model_length != max_model_length:
          return init_max_model_length
         elif init_max_model_length is None:
          warnings.warn(
              '''This tokenizer was incorrectly instantiated with a model max length of'''
              F""" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this"""
              ''' behavior is kept to avoid breaking backwards compatibility when padding/encoding with'''
              ''' `truncation is True`.\n- Be aware that you SHOULD NOT rely on'''
              F""" {pretrained_model_name_or_path} automatically truncating your input to"""
              F""" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences"""
              F""" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with"""
              ''' `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please'''
              ''' instantiate this tokenizer with `model_max_length` set to your preferred value.'''	,   __lowerCAmelCase	,   )

        return max_model_length

       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase = None )  ->  Tuple[str]:
        if not self.can_save_slow_tokenizer:
         raise ValueError(
             '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '''
             '''tokenizer.''' )

        if not os.path.isdir(__lowerCAmelCase ):
         logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" )
         return
        lowercase__				:    List[Any]  =					os.path.join(
            __lowerCAmelCase	,   (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] )

        if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ):
         copyfile(self.vocab_file	,   __lowerCAmelCase )
         logger.info(F"""Copy vocab file to {out_vocab_file}""" )

        return (out_vocab_file,)

       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase = None )  ->  List[int]:
        lowercase__				:    Any  =					token_ids_a + [self.eos_token_id]
        if token_ids_a is None:
         return self.prefix_tokens + token_ids_a
        else:
         lowercase__				:    Dict  =					token_ids_a + [self.eos_token_id]
         return self.prefix_tokens + token_ids_a + token_ids_a

       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase = None )  ->  List[int]:
        lowercase__				:    Optional[int]  =					[self.eos_token_id]

        if token_ids_a is None:
         return len(token_ids_a + eos ) * [0]
        return len(token_ids_a + eos + token_ids_a + eos ) * [0]

       def _lowerCAmelCase(						self )  ->  List[Any]:
        return list(
            set(filter(lambda __lowerCAmelCase : bool(re.search(r'''<extra_id_\d+>'''	,   __lowerCAmelCase ) ) is not None	,   self.additional_special_tokens ) ) )


       def _lowerCAmelCase(						self )  ->  Tuple:
        return [self.convert_tokens_to_ids(__lowerCAmelCase ) for token in self.get_sentinel_tokens()]

