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






     """simple docstring"""







     def __init__(				self							:							Union[str, Any]     ,						_A							:							int     ,						_A							:							Dict=13     ,						_A							:							Optional[int]=32     ,						_A							:							str=3     ,						_A							:							Optional[Any]=4     ,						_A							:							Optional[int]=[10, 20, 30, 40]     ,						_A							:							Union[str, Any]=[2, 2, 3, 2]     ,						_A							:							List[Any]=True     ,						_A							:							Tuple=True     ,						_A							:							List[str]=37     ,						_A							:							Optional[Any]="gelu"     ,						_A							:							Union[str, Any]=10     ,						_A							:							int=0.02     ,						_A							:							int=["stage2", "stage3", "stage4"]     ,						_A							:							str=[2, 3, 4]     ,						_A							:							List[Any]=None     ,						):







       """simple docstring"""




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



     def  UpperCAmelCase__							(				self							:							int ):







       """simple docstring"""




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

       __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		None
       if self.use_labels:
         __SCREAMING_SNAKE_CASE      :	Tuple					=		ids_tensor([self.batch_size]     ,						self.num_labels )

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

       return config, pixel_values, labels



     def  UpperCAmelCase__							(				self							:							Any ):







       """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=_A     ,						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]     ,						_A							:							List[Any]     ,						_A							:							int     ,						_A							:							Any ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Tuple					=		ConvNextVaModel(config=_A )
       model.to(_A )
       model.eval()
       __SCREAMING_SNAKE_CASE      :	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  UpperCAmelCase__							(				self							:							Union[str, Any]     ,						_A							:							List[Any]     ,						_A							:							List[str]     ,						_A							:							Optional[Any] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		ConvNextVaForImageClassification(_A )
       model.to(_A )
       model.eval()
       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		model(_A     ,						labels=_A )
       self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.num_labels) )



     def  UpperCAmelCase__							(				self							:							Dict     ,						_A							:							str     ,						_A							:							Union[str, Any]     ,						_A							:							Dict ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Dict					=		ConvNextVaBackbone(config=_A )
       model.to(_A )
       model.eval()
       __SCREAMING_SNAKE_CASE      :	List[str]					=		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
       __SCREAMING_SNAKE_CASE      :	str					=		None
       __SCREAMING_SNAKE_CASE      :	List[Any]					=		ConvNextVaBackbone(config=_A )
       model.to(_A )
       model.eval()
       __SCREAMING_SNAKE_CASE      :	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  UpperCAmelCase__							(				self							:							Optional[int] ):







       """simple docstring"""




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




     def  UpperCAmelCase__							(				self							:							Union[str, Any] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		self.prepare_config_and_inputs()
       __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE      :	int					=		config_and_inputs
       __SCREAMING_SNAKE_CASE      :	List[str]					=		{'''pixel_values''': pixel_values, '''labels''': labels}
       return config, inputs_dict






@require_torch
class     __UpperCamelCase (							lowerCAmelCase__ ,						lowerCAmelCase__ ,						unittest.TestCase       ):






     """simple docstring"""







     lowerCAmelCase_							   =			(
         (
             ConvNextVaModel,
             ConvNextVaForImageClassification,
             ConvNextVaBackbone,
         )
         if is_torch_available()
         else ()
     )
     lowerCAmelCase_							   =			(
         {'''feature-extraction''': ConvNextVaModel, '''image-classification''': ConvNextVaForImageClassification}
         if is_torch_available()
         else {}
     )

     lowerCAmelCase_							   =			False
     lowerCAmelCase_							   =			False
     lowerCAmelCase_							   =			False
     lowerCAmelCase_							   =			False
     lowerCAmelCase_							   =			False



     def  UpperCAmelCase__							(				self							:							str ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		ConvNextVaModelTester(self )
       __SCREAMING_SNAKE_CASE      :	Tuple					=		ConfigTester(self     ,						config_class=_A     ,						has_text_modality=_A     ,						hidden_size=37 )



     def  UpperCAmelCase__							(				self							:							str ):







       """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  UpperCAmelCase__							(				self							:							Union[str, Any] ):







       """simple docstring"""




       return



     @unittest.skip(reason='''ConvNextV2 does not use inputs_embeds''' )
     def  UpperCAmelCase__							(				self							:							List[str] ):







       """simple docstring"""




       pass



     @unittest.skip(reason='''ConvNextV2 does not support input and output embeddings''' )
     def  UpperCAmelCase__							(				self							:							Optional[Any] ):







       """simple docstring"""




       pass



     @unittest.skip(reason='''ConvNextV2 does not use feedforward chunking''' )
     def  UpperCAmelCase__							(				self							:							Dict ):







       """simple docstring"""




       pass



     def  UpperCAmelCase__							(				self							:							Dict ):







       """simple docstring"""




       if not self.model_tester.is_training:
         return

       for model_class in self.all_model_classes:
         __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		self.model_tester.prepare_config_and_inputs_with_labels()
         __SCREAMING_SNAKE_CASE      :	int					=		True

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

         __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		model_class(_A )
         model.to(_A )
         model.train()
         __SCREAMING_SNAKE_CASE      :	List[str]					=		self._prepare_for_class(_A     ,						_A     ,						return_labels=_A )
         __SCREAMING_SNAKE_CASE      :	Tuple					=		model(**_A ).loss
         loss.backward()



     def  UpperCAmelCase__							(				self							:							Optional[int] ):







       """simple docstring"""




       if not self.model_tester.is_training:
         return

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

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

         __SCREAMING_SNAKE_CASE      :	str					=		model_class(_A )
         model.to(_A )
         model.gradient_checkpointing_enable()
         model.train()
         __SCREAMING_SNAKE_CASE      :	Any					=		self._prepare_for_class(_A     ,						_A     ,						return_labels=_A )
         __SCREAMING_SNAKE_CASE      :	Dict					=		model(**_A ).loss
         loss.backward()



     def  UpperCAmelCase__							(				self							:							int ):







       """simple docstring"""




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

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

         __SCREAMING_SNAKE_CASE      :	Tuple					=		['''pixel_values''']
         self.assertListEqual(arg_names[:1]     ,						_A )



     def  UpperCAmelCase__							(				self							:							int ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Dict					=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_A )



     def  UpperCAmelCase__							(				self							:							Any ):







       """simple docstring"""




       def check_hidden_states_output(_A							:							Tuple     ,						_A							:							Optional[int]     ,						_A							:							Dict ):
         __SCREAMING_SNAKE_CASE      :	int					=		model_class(_A )
         model.to(_A )
         model.eval()

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

         __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

       __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE      :	Tuple					=		self.model_tester.prepare_config_and_inputs_for_common()

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

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

         check_hidden_states_output(_A     ,						_A     ,						_A )



     def  UpperCAmelCase__							(				self							:							Optional[int] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	List[Any]					=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_A )




     @slow
     def  UpperCAmelCase__							(				self							:							Tuple ):







       """simple docstring"""




       for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __SCREAMING_SNAKE_CASE      :	List[str]					=		ConvNextVaModel.from_pretrained(_A )
         self.assertIsNotNone(_A )






def 							a__					(							):





  """simple docstring"""
  __SCREAMING_SNAKE_CASE      :	Dict					=		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
  return image





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






     """simple docstring"""







     @cached_property
     def  UpperCAmelCase__							(				self							:							Optional[int] ):







       """simple docstring"""




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




     @slow
     def  UpperCAmelCase__							(				self							:							List[Any] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	List[Any]					=		ConvNextVaForImageClassification.from_pretrained('''facebook/convnextv2-tiny-1k-224''' ).to(_A )

       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		self.default_image_processor
       __SCREAMING_SNAKE_CASE      :	List[str]					=		prepare_img()
       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		preprocessor(images=_A     ,						return_tensors='''pt''' ).to(_A )

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

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

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







'''simple docstring'''

import json
import os
import tempfile

import datasets
from utils import generate_example_dataset, get_duration


__lowerCAmelCase				:      int						      =5_0000
__lowerCAmelCase				:      Union[str, Any]						      =5000

__lowerCAmelCase      ,	__lowerCAmelCase				:      Tuple						      =os.path.split(__file__)
__lowerCAmelCase				:      int						      =os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json"))





@get_duration
def 		UpperCamelCase							(				_lowerCamelCase   :   datasets.Dataset		,			_lowerCamelCase   :   int			):
    for i in range(_lowerCamelCase			):
        A__							=     dataset[i]





@get_duration
def 		UpperCamelCase							(				_lowerCamelCase   :   datasets.Dataset		,			_lowerCamelCase   :   Optional[int]		,			_lowerCamelCase   :   Any			):
    for i in range(0		,			len(_lowerCamelCase			)		,			_lowerCamelCase			):
        A__							=     dataset[i : i + batch_size]





@get_duration
def 		UpperCamelCase							(				_lowerCamelCase   :   datasets.Dataset		,			_lowerCamelCase   :   Optional[Any]		,			_lowerCamelCase   :   List[str]			):
    with dataset.formatted_as(type=_lowerCamelCase			):
        for i in range(_lowerCamelCase			):
            A__							=     dataset[i]





@get_duration
def 		UpperCamelCase							(				_lowerCamelCase   :   datasets.Dataset		,			_lowerCamelCase   :   Optional[int]		,			_lowerCamelCase   :   int		,			_lowerCamelCase   :   Dict			):
    with dataset.formatted_as(type=_lowerCamelCase			):
        for i in range(0		,			_lowerCamelCase		,			_lowerCamelCase			):
            A__							=     dataset[i : i + batch_size]





def 		UpperCamelCase							(				):
    A__							=     {"num examples": SPEED_TEST_N_EXAMPLES}
    A__							=     [
        (read, {"length": SMALL_TEST}),
        (read, {"length": SPEED_TEST_N_EXAMPLES}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 1_00}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10_00}),
        (read_formatted, {"type": "numpy", "length": SMALL_TEST}),
        (read_formatted, {"type": "pandas", "length": SMALL_TEST}),
        (read_formatted, {"type": "torch", "length": SMALL_TEST}),
        (read_formatted, {"type": "tensorflow", "length": SMALL_TEST}),
        (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10}),
        (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10_00}),
    ]

    A__							=     [
        (read, {"length": SMALL_TEST}),
        (read, {"length": SPEED_TEST_N_EXAMPLES}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 1_00}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10_00}),
        (read_formatted, {"type": "numpy", "length": SMALL_TEST}),
        (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10}),
        (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10_00}),
    ]
    with tempfile.TemporaryDirectory() as tmp_dir:
        print("generating dataset"			)
        A__							=     datasets.Features(
            {"list": datasets.Sequence(datasets.Value("float32"			)			), "numbers": datasets.Value("float32"			)}			)
        A__							=     generate_example_dataset(
            os.path.join(_lowerCamelCase		,			"dataset.arrow"			)		,			_lowerCamelCase		,			num_examples=_lowerCamelCase		,			seq_shapes={"list": (1_00,)}		,			)
        print("first set of iterations"			)
        for func, kwargs in functions:
            print(func.__name__		,			str(_lowerCamelCase			)			)
            A__							=     func(_lowerCamelCase		,			**_lowerCamelCase			)

        print("shuffling dataset"			)
        A__							=     dataset.shuffle()
        print("Second set of iterations (after shuffling"			)
        for func, kwargs in functions_shuffled:
            print("shuffled "		,			func.__name__		,			str(_lowerCamelCase			)			)
            A__							=     func(
                _lowerCamelCase		,			**_lowerCamelCase			)

    with open(_lowerCamelCase		,			"wb"			) as f:
        f.write(json.dumps(_lowerCamelCase			).encode("utf-8"			)			)


if __name__ == "__main__":  # useful to run the profiler
  benchmark_iterating()



