

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



      """simple docstring"""







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

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

           SCREAMING_SNAKE_CASE      						=self.get_config()
           return config, pixel_values, labels
      def        _lowerCAmelCase     (      self     :							Any				):
           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=snake_case			,initializer_range=self.initializer_range			,out_features=self.out_features			,out_indices=self.out_indices			,num_labels=self.num_labels			,)
      def        _lowerCAmelCase     (      self     :							Tuple			,snake_case     :							str			,snake_case     :							Union[str, Any]			,snake_case     :							Dict				):
           SCREAMING_SNAKE_CASE      						=ConvNextModel(config=snake_case				)
           model.to(snake_case				)
           model.eval()
           SCREAMING_SNAKE_CASE      						=model(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        _lowerCAmelCase     (      self     :							int			,snake_case     :							Optional[int]			,snake_case     :							Union[str, Any]			,snake_case     :							List[Any]				):
           SCREAMING_SNAKE_CASE      						=ConvNextForImageClassification(snake_case				)
           model.to(snake_case				)
           model.eval()
           SCREAMING_SNAKE_CASE      						=model(snake_case			,labels=snake_case				)
           self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.num_labels)				)
      def        _lowerCAmelCase     (      self     :							Dict			,snake_case     :							List[Any]			,snake_case     :							Tuple			,snake_case     :							str				):
           SCREAMING_SNAKE_CASE      						=ConvNextBackbone(config=snake_case				)
           model.to(snake_case				)
           model.eval()
           SCREAMING_SNAKE_CASE      						=model(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
           SCREAMING_SNAKE_CASE      						=None
           SCREAMING_SNAKE_CASE      						=ConvNextBackbone(config=snake_case				)
           model.to(snake_case				)
           model.eval()
           SCREAMING_SNAKE_CASE      						=model(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        _lowerCAmelCase     (      self     :							Optional[Any]				):
           SCREAMING_SNAKE_CASE      						=self.prepare_config_and_inputs()
           SCREAMING_SNAKE_CASE						,  SCREAMING_SNAKE_CASE						,  SCREAMING_SNAKE_CASE      						=config_and_inputs
           SCREAMING_SNAKE_CASE      						={'pixel_values': pixel_values}
           return config, inputs_dict







@require_torch
class 		a_       (							A_ ,			A_ ,			unittest.TestCase     ):



      """simple docstring"""







      __UpperCAmelCase					=      (
          (
              ConvNextModel,
              ConvNextForImageClassification,
              ConvNextBackbone,
          )
          if is_torch_available()
          else ()
      )
      __UpperCAmelCase					=      (
          {'''feature-extraction''': ConvNextModel, '''image-classification''': ConvNextForImageClassification}
          if is_torch_available()
          else {}
      )

      __UpperCAmelCase					=      True
      __UpperCAmelCase					=      False
      __UpperCAmelCase					=      False
      __UpperCAmelCase					=      False
      __UpperCAmelCase					=      False
      def        _lowerCAmelCase     (      self     :							Dict				):
           SCREAMING_SNAKE_CASE      						=ConvNextModelTester(self				)
           SCREAMING_SNAKE_CASE      						=ConfigTester(self			,config_class=snake_case			,has_text_modality=snake_case			,hidden_size=37				)
      def        _lowerCAmelCase     (      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        _lowerCAmelCase     (      self     :							List[Any]				):
           return
      @unittest.skip(reason='ConvNext does not use inputs_embeds'				)
      def        _lowerCAmelCase     (      self     :							Tuple				):
           pass
      @unittest.skip(reason='ConvNext does not support input and output embeddings'				)
      def        _lowerCAmelCase     (      self     :							Tuple				):
           pass
      @unittest.skip(reason='ConvNext does not use feedforward chunking'				)
      def        _lowerCAmelCase     (      self     :							List[str]				):
           pass
      def        _lowerCAmelCase     (      self     :							Dict				):
           SCREAMING_SNAKE_CASE						,  SCREAMING_SNAKE_CASE      						=self.model_tester.prepare_config_and_inputs_for_common()

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

                SCREAMING_SNAKE_CASE      						=['pixel_values']
                self.assertListEqual(arg_names[:1]			,snake_case				)
      def        _lowerCAmelCase     (      self     :							str				):
           SCREAMING_SNAKE_CASE      						=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*snake_case				)
      def        _lowerCAmelCase     (      self     :							Dict				):
           SCREAMING_SNAKE_CASE      						=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_backbone(*snake_case				)
      def        _lowerCAmelCase     (      self     :							Dict				):
           def check_hidden_states_output(snake_case     :							List[Any]			,snake_case     :							Optional[Any]			,snake_case     :							str				):
                SCREAMING_SNAKE_CASE      						=model_class(snake_case				)
                model.to(snake_case				)
                model.eval()

                with torch.no_grad():
                     SCREAMING_SNAKE_CASE      						=model(**self._prepare_for_class(snake_case			,snake_case				)				)

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

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

           SCREAMING_SNAKE_CASE						,  SCREAMING_SNAKE_CASE      						=self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                SCREAMING_SNAKE_CASE      						=True
                check_hidden_states_output(snake_case			,snake_case			,snake_case				)

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

                check_hidden_states_output(snake_case			,snake_case			,snake_case				)
      def        _lowerCAmelCase     (      self     :							Union[str, Any]				):
           SCREAMING_SNAKE_CASE      						=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*snake_case				)


      @slow
      def        _lowerCAmelCase     (      self     :							Optional[int]				):
           for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                SCREAMING_SNAKE_CASE      						=ConvNextModel.from_pretrained(snake_case				)
                self.assertIsNotNone(snake_case				)







def 			snake_case__				(   ):





     """simple docstring"""







     SCREAMING_SNAKE_CASE      						=Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
     return image



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



      """simple docstring"""







      @cached_property
      def        _lowerCAmelCase     (      self     :							int				):
           return AutoImageProcessor.from_pretrained('facebook/convnext-tiny-224'				) if is_vision_available() else None


      @slow
      def        _lowerCAmelCase     (      self     :							Dict				):
           SCREAMING_SNAKE_CASE      						=ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'				).to(snake_case				)

           SCREAMING_SNAKE_CASE      						=self.default_image_processor
           SCREAMING_SNAKE_CASE      						=prepare_img()
           SCREAMING_SNAKE_CASE      						=image_processor(images=snake_case			,return_tensors='pt'				).to(snake_case				)

           # forward pass
           with torch.no_grad():
                SCREAMING_SNAKE_CASE      						=model(**snake_case				)

           # verify the logits
           SCREAMING_SNAKE_CASE      						=torch.Size((1, 1000)				)
           self.assertEqual(outputs.logits.shape			,snake_case				)

           SCREAMING_SNAKE_CASE      						=torch.tensor([-0.0_260, -0.4_739, 0.1_911]				).to(snake_case				)

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







@require_torch
class 		a_       (							unittest.TestCase ,			A_     ):



      """simple docstring"""







      __UpperCAmelCase					=      (ConvNextBackbone,) if is_torch_available() else ()
      __UpperCAmelCase					=      ConvNextConfig

      __UpperCAmelCase					=      False
      def        _lowerCAmelCase     (      self     :							int				):
           SCREAMING_SNAKE_CASE      						=ConvNextModelTester(self				)





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:
	lowerCAmelCase           =		None


lowerCAmelCase           =		logging.get_logger(__name__)

lowerCAmelCase           =		{'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''}

lowerCAmelCase           =		{
    '''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
lowerCAmelCase           =		{
    '''t5-small''': 5_1_2,
    '''t5-base''': 5_1_2,
    '''t5-large''': 5_1_2,
    '''t5-3b''': 5_1_2,
    '''t5-11b''': 5_1_2,
}





class       A    (   A_ ):
							UpperCamelCase_   :							Dict       =VOCAB_FILES_NAMES
							UpperCamelCase_   :							Dict       =PRETRAINED_VOCAB_FILES_MAP
							UpperCamelCase_   :							List[Any]       =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							UpperCamelCase_   :							str       =['''input_ids''', '''attention_mask''']
							UpperCamelCase_   :							List[str]       =TaTokenizer

							UpperCamelCase_   :							List[int]       =[]


							def __init__(self      ,				lowerCAmelCase=None      ,				lowerCAmelCase=None      ,				lowerCAmelCase="</s>"      ,				lowerCAmelCase="<unk>"      ,				lowerCAmelCase="<pad>"      ,				lowerCAmelCase=1_0_0      ,				lowerCAmelCase=None      ,				**lowerCAmelCase      ,				):
									# Add extra_ids to the special token list
									if extra_ids > 0 and additional_special_tokens is None:
											__lowercase=			[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=			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=			vocab_file
									__lowercase=			False if not self.vocab_file else True
									__lowercase=			extra_ids


							@staticmethod
							def 			_A       (lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase					):
									if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes:
											__lowercase=			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 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase = None					):
									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=			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 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase = None					):
									__lowercase=			token_ids_a + [self.eos_token_id]
									if token_ids_a is None:
											return self.prefix_tokens + token_ids_a
									else:
											__lowercase=			token_ids_a + [self.eos_token_id]
											return self.prefix_tokens + token_ids_a + token_ids_a


							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase = None					):
									__lowercase=			[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 			_A       (self					):
									return list(
									    set(filter(lambda lowerCAmelCase					: bool(re.search(r'<extra_id_\d+>'      ,				lowerCAmelCase					)					) is not None      ,				self.additional_special_tokens					)					)					)







							def 			_A       (self					):
									return [self.convert_tokens_to_ids(lowerCAmelCase					) for token in self.get_sentinel_tokens()]






