
'''simple docstring'''




import math
import os
import re
import sys
import unittest
from pathlib import Path
from typing import Tuple
from unittest.mock import patch

from parameterized import parameterized

from transformers.testing_utils import (
    CaptureStderr,
    ExtendSysPath,
    TestCasePlus,
    execute_subprocess_async,
    get_gpu_count,
    get_torch_dist_unique_port,
    require_apex,
    require_bitsandbytes,
    require_fairscale,
    require_torch,
    require_torch_gpu,
    require_torch_multi_gpu,
    require_torch_non_multi_gpu,
    slow,
)
from transformers.trainer_callback import TrainerState
from transformers.trainer_utils import set_seed


lowercase			:				Dict    		=			os.path.abspath(os.path.dirname(__file__))
with ExtendSysPath(f'''{bindir}/../../examples/pytorch/translation'''):
      from run_translation import main  # noqa


set_seed(42)
lowercase			:				Dict    		=			'sshleifer/student_marian_en_ro_6_1'
lowercase			:				Optional[int]    		=			'sshleifer/tiny-mbart'





@require_torch
class  A				(							__snake_case						):





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE=False						,			SCREAMING_SNAKE_CASE=None						,			SCREAMING_SNAKE_CASE=True						,			SCREAMING_SNAKE_CASE=True						,			SCREAMING_SNAKE_CASE=True						,			SCREAMING_SNAKE_CASE=True						,			) ->				str:






         """simple docstring"""






         A       :   str					=						self.run_trainer(
             eval_steps=1						,			max_len=12						,			model_name=SCREAMING_SNAKE_CASE						,			num_train_epochs=1						,			distributed=SCREAMING_SNAKE_CASE						,			extra_args_str=SCREAMING_SNAKE_CASE						,			predict_with_generate=SCREAMING_SNAKE_CASE						,			do_train=SCREAMING_SNAKE_CASE						,			do_eval=SCREAMING_SNAKE_CASE						,			do_predict=SCREAMING_SNAKE_CASE						,			)
         A       :   List[Any]					=						TrainerState.load_from_json(os.path.join(SCREAMING_SNAKE_CASE						,			'''trainer_state.json'''	)	).log_history

         if not do_eval:
             return

         A       :   Dict					=						[log for log in logs if '''eval_loss''' in log.keys()]

         A       :   List[str]					=						eval_metrics[0]
         if predict_with_generate:
             assert "eval_bleu" in first_step_stats

             A       :   Tuple					=						eval_metrics[-1]
             assert isinstance(last_step_stats['''eval_bleu''']						,			SCREAMING_SNAKE_CASE	)
             assert not math.isnan(float(last_step_stats['''eval_loss''']	)	), "eval_loss must not be `nan`"





     @require_torch_non_multi_gpu
     def 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






         self.run_seqaseq_quick()





     @require_torch_multi_gpu
     def 				__lowerCAmelCase							(							self	) ->				List[str]:






         """simple docstring"""






         self.run_seqaseq_quick(distributed=SCREAMING_SNAKE_CASE	)





     @require_torch_multi_gpu
     def 				__lowerCAmelCase							(							self	) ->				List[Any]:






         """simple docstring"""






         self.run_seqaseq_quick(distributed=SCREAMING_SNAKE_CASE	)





     @unittest.skip('''Requires an update of the env running those tests'''	)
     @require_torch_multi_gpu
     @require_fairscale
     def 				__lowerCAmelCase							(							self	) ->				Optional[Any]:






         """simple docstring"""






         self.run_seqaseq_quick(distributed=SCREAMING_SNAKE_CASE						,			extra_args_str='''--sharded_ddp simple'''	)





     @unittest.skip('''Requires an update of the env running those tests'''	)
     @require_torch_multi_gpu
     @require_fairscale
     def 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






         self.run_seqaseq_quick(distributed=SCREAMING_SNAKE_CASE						,			extra_args_str='''--sharded_ddp simple --fp16'''	)





     @unittest.skip('''Requires an update of the env running those tests'''	)
     @require_torch_multi_gpu
     @require_fairscale
     def 				__lowerCAmelCase							(							self	) ->				int:






         """simple docstring"""






         self.run_seqaseq_quick(distributed=SCREAMING_SNAKE_CASE						,			extra_args_str='''--sharded_ddp zero_dp_2'''						,			predict_with_generate=SCREAMING_SNAKE_CASE	)





     @unittest.skip('''Requires an update of the env running those tests'''	)
     @require_torch_multi_gpu
     @require_fairscale
     def 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






         self.run_seqaseq_quick(
             distributed=SCREAMING_SNAKE_CASE						,			extra_args_str='''--sharded_ddp zero_dp_2 --fp16'''						,			predict_with_generate=SCREAMING_SNAKE_CASE	)





     @require_apex
     @require_torch_gpu
     def 				__lowerCAmelCase							(							self	) ->				int:






         """simple docstring"""






         self.run_seqaseq_quick(distributed=SCREAMING_SNAKE_CASE						,			extra_args_str='''--fp16 --fp16_backend=apex'''	)
         # test 2nd time - was getting eval_loss': nan'
         # to reproduce the problem set distributed=False
         self.run_seqaseq_quick(distributed=SCREAMING_SNAKE_CASE						,			extra_args_str='''--fp16 --fp16_backend=apex'''	)





     @parameterized.expand(['''base''', '''low''', '''high''', '''mixed''']	)
     @require_torch_multi_gpu
     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE	) ->				str:






         """simple docstring"""






         A       :   Any					=						{
             # test with the default log_level - should be info and thus log info once
             '''base''': {'''extra_args_str''': '''''', '''n_matches''': 1},
             # test with low log_level and log_level_replica - should be noisy on all processes
             # now the info string should appear twice on 2 processes
             '''low''': {'''extra_args_str''': '''--log_level debug --log_level_replica debug''', '''n_matches''': 2},
             # test with high log_level and low log_level_replica
             # now the info string should appear once only on the replica
             '''high''': {'''extra_args_str''': '''--log_level error --log_level_replica debug''', '''n_matches''': 1},
             # test with high log_level and log_level_replica - should be quiet on all processes
             '''mixed''': {'''extra_args_str''': '''--log_level error --log_level_replica error''', '''n_matches''': 0},
         }

         A       :   Any					=						experiments[experiment_id]
         A       :   Any					=						{'''distributed''': True, '''predict_with_generate''': False, '''do_eval''': False, '''do_predict''': False}
         A       :   Union[str, Any]					=						'''Running training'''
         with CaptureStderr() as cl:
             self.run_seqaseq_quick(**SCREAMING_SNAKE_CASE						,			extra_args_str=data['''extra_args_str''']	)
         A       :   int					=						len(re.findall(SCREAMING_SNAKE_CASE						,			cl.err	)	)
         self.assertEqual(SCREAMING_SNAKE_CASE						,			data['''n_matches''']	)





     @slow
     def 				__lowerCAmelCase							(							self	) ->				Optional[Any]:






         """simple docstring"""






         A       :   Tuple					=						self.run_trainer(
             eval_steps=2						,			max_len=128						,			model_name=SCREAMING_SNAKE_CASE						,			learning_rate=3e-4						,			num_train_epochs=10						,			distributed=SCREAMING_SNAKE_CASE						,			)

         # Check metrics
         A       :   Union[str, Any]					=						TrainerState.load_from_json(os.path.join(SCREAMING_SNAKE_CASE						,			'''trainer_state.json'''	)	).log_history
         A       :   Union[str, Any]					=						[log for log in logs if '''eval_loss''' in log.keys()]
         A       :   List[str]					=						eval_metrics[0]
         A       :   List[Any]					=						eval_metrics[-1]

         assert first_step_stats["eval_loss"] > last_step_stats["eval_loss"], "model learned nothing"
         assert isinstance(last_step_stats['''eval_bleu''']						,			SCREAMING_SNAKE_CASE	)

         # test if do_predict saves generations and metrics
         A       :   int					=						os.listdir(SCREAMING_SNAKE_CASE	)
         A       :   Optional[int]					=						{os.path.basename(SCREAMING_SNAKE_CASE	) for p in contents}
         assert "generated_predictions.txt" in contents
         assert "predict_results.json" in contents





     @slow
     @require_bitsandbytes
     def 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






         from transformers.training_args import OptimizerNames

         def train_and_return_metrics(SCREAMING_SNAKE_CASE	) -> Tuple[int, float]:
             A       :   Optional[int]					=						'''--skip_memory_metrics 0'''

             A       :   List[Any]					=						self.run_trainer(
                 max_len=128						,			model_name=SCREAMING_SNAKE_CASE						,			learning_rate=3e-4						,			num_train_epochs=1						,			optim=SCREAMING_SNAKE_CASE						,			distributed=SCREAMING_SNAKE_CASE						,			extra_args_str=SCREAMING_SNAKE_CASE						,			do_eval=SCREAMING_SNAKE_CASE						,			do_predict=SCREAMING_SNAKE_CASE						,			n_gpus_to_use=1						,			)

             # Check metrics
             A       :   str					=						TrainerState.load_from_json(Path(SCREAMING_SNAKE_CASE						,			'''trainer_state.json'''	)	).log_history
             A       :   Union[str, Any]					=						int(logs[0]['''train_mem_gpu_peaked_delta'''] / 2**20	)
             A       :   int					=						int(logs[0]['''train_mem_gpu_alloc_delta'''] / 2**20	)

             A       :   List[Any]					=						logs[0]['''train_loss''']
             return gpu_peak_mem_mb, gpu_alloc_mem_mb, loss

         A,  A,  A       :   List[Any]					=						train_and_return_metrics(OptimizerNames.ADAMW_TORCH.value	)
         A,  A,  A       :   Optional[Any]					=						train_and_return_metrics(OptimizerNames.ADAMW_BNB.value	)

         A       :   Dict					=						gpu_alloc_mem_orig - gpu_alloc_mem_bnb

         A       :   Union[str, Any]					=						gpu_peak_mem_orig + gpu_alloc_mem_orig
         A       :   Optional[Any]					=						gpu_peak_mem_bnb + gpu_alloc_mem_bnb
         A       :   str					=						gpu_total_mem_orig - gpu_total_mem_bnb

         # sshleifer/student_marian_en_ro_6_1 has 54M parameter, 29M of which is `nn.Embedding` which
         # doesn't get quantized and remains in fp32. Therefore we only have 25M parameters quantized
         # in 2 bytes and the diff in optim memory usage is derived as so:
         #
         # - normal 25*8=~200MB (8 bytes per param)
         # - bnb    25*2= ~50MB (2 bytes per param)
         #
         # Thus we should expect ~150MB total memory saved.
         #
         # Peak memory should be the same - the total should be different by about that same margin
         #
         # After leaving a small margin to accommodate for differences between gpus let's check
         # that we have at least 120MB in savings
         A       :   List[str]					=						120

         # uncomment the following if this test starts failing - requires py38 for a new print feature
         # gpu_peak_mem_diff = gpu_peak_mem_orig - gpu_peak_mem_bnb
         # print(f"{gpu_alloc_mem_orig=}MB {gpu_peak_mem_orig=}MB {gpu_alloc_mem_orig+gpu_peak_mem_orig=}MB")
         # print(f" {gpu_alloc_mem_bnb=}MB  {gpu_peak_mem_bnb=}MB  {gpu_alloc_mem_bnb+gpu_peak_mem_bnb=}MB")
         # print(f"{gpu_alloc_mem_diff=}MB")
         # print(f"{gpu_peak_mem_diff=}MB")
         # print(f"{gpu_total_mem_orig=}MB, {gpu_total_mem_bnb=}MB")
         # print(f"{gpu_total_mem_diff=}MB, {gpu_total_mem_diff=}MB")

         self.assertGreater(
             SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			'''should use ~150MB less alloc gpu memory with BNB, compared to without it for this model but got'''
             F' a difference of {gpu_alloc_mem_diff}MB, with gpu_alloc_mem_orig={gpu_alloc_mem_orig}MB and'
             F' gpu_alloc_mem_bnb={gpu_alloc_mem_bnb}MB'						,			)

         self.assertGreater(
             SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			'''should use ~150MB less total gpu memory with BNB, compared to without it for this model but got'''
             F' a difference of {gpu_total_mem_diff}MB, with gpu_total_mem_orig={gpu_total_mem_orig}MB and'
             F' gpu_total_mem_bnb={gpu_total_mem_bnb}MB'						,			)

         self.assertEqual(
             SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			F'loss should be the same, but got loss_orig={loss_orig}, loss_bnb={loss_bnb}'	)





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE = 3e-3						,			SCREAMING_SNAKE_CASE = "adafactor"						,			SCREAMING_SNAKE_CASE = False						,			SCREAMING_SNAKE_CASE = None						,			SCREAMING_SNAKE_CASE = 0						,			SCREAMING_SNAKE_CASE = True						,			SCREAMING_SNAKE_CASE = True						,			SCREAMING_SNAKE_CASE = True						,			SCREAMING_SNAKE_CASE = True						,			SCREAMING_SNAKE_CASE = None						,			) ->				Tuple:






         """simple docstring"""






         A       :   Tuple					=						self.test_file_dir / '''../fixtures/tests_samples/wmt_en_ro'''
         A       :   Dict					=						self.get_auto_remove_tmp_dir()
         A       :   int					=						F'\n            --model_name_or_path {model_name}\n            --train_file {data_dir}/train.json\n            --validation_file {data_dir}/val.json\n            --test_file {data_dir}/test.json\n            --output_dir {output_dir}\n            --overwrite_output_dir\n            --max_train_samples 8\n            --max_source_length {max_len}\n            --max_target_length {max_len}\n            --do_train\n            --num_train_epochs {str(SCREAMING_SNAKE_CASE	)}\n            --per_device_train_batch_size 4\n            --learning_rate {learning_rate}\n            --warmup_steps 8\n            --logging_steps 0\n            --logging_strategy no\n            --save_steps {str(SCREAMING_SNAKE_CASE	)}\n            --group_by_length\n            --label_smoothing_factor 0.1\n            --target_lang ro_RO\n            --source_lang en_XX\n        '.split()

         A       :   Any					=						F'\n            --do_eval\n            --per_device_eval_batch_size 4\n            --max_eval_samples 8\n            --val_max_target_length {max_len}\n            --evaluation_strategy steps\n            --eval_steps {str(SCREAMING_SNAKE_CASE	)}\n        '.split()

         A       :   Optional[Any]					=						'''
            --do_predict
        '''.split()

         A       :   Union[str, Any]					=						[]
         if do_train:
             args += args_train

         if do_eval:
             args += args_eval

         if do_predict:
             args += args_predict

         if predict_with_generate:
             args += "--predict_with_generate".split()

         if do_train:
             if optim == "adafactor":
                 args += "--adafactor".split()
             else:
                 args += F'--optim {optim}'.split()

         if extra_args_str is not None:
             args += extra_args_str.split()

         if distributed:
             if n_gpus_to_use is None:
                 A       :   Dict					=						get_gpu_count()
             A       :   Tuple					=						get_torch_dist_unique_port()
             A       :   str					=						F'\n                -m torch.distributed.run\n                --nproc_per_node={n_gpus_to_use}\n                --master_port={master_port}\n                {self.examples_dir_str}/pytorch/translation/run_translation.py\n            '.split()
             A       :   str					=						[sys.executable] + distributed_args + args
             # keep for quick debug
             # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die
             execute_subprocess_async(SCREAMING_SNAKE_CASE						,			env=self.get_env()	)
         else:
             A       :   List[str]					=						['''run_translation.py'''] + args
             with patch.object(SCREAMING_SNAKE_CASE						,			'''argv'''						,			SCREAMING_SNAKE_CASE	):
                 main()

         return output_dir












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

					'''simple docstring'''

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


												"""simple docstring"""

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



					def 					lowerCAmelCase (     self :		List[Any]							)	->     List[Any]:


												"""simple docstring"""

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

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

												__lowercase					:							Union[str, Any]			=   self.get_config()

												return config, pixel_values, labels



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


												"""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 					lowerCAmelCase (     self :		int						,   __a :		Tuple						,   __a :		Dict						,   __a :		List[str]							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							str			=   ConvNextVaModel(config=__a							)
												model.to(__a							)
												model.eval()
												__lowercase					:							Union[str, Any]			=   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 					lowerCAmelCase (     self :		Any						,   __a :		Tuple						,   __a :		Any						,   __a :		Union[str, Any]							)	->     Optional[int]:


												"""simple docstring"""

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



					def 					lowerCAmelCase (     self :		int						,   __a :		str						,   __a :		Any						,   __a :		Optional[int]							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							Union[str, Any]			=   ConvNextVaBackbone(config=__a							)
												model.to(__a							)
												model.eval()
												__lowercase					:							Dict			=   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					:							str			=   ConvNextVaBackbone(config=__a							)
												model.to(__a							)
												model.eval()
												__lowercase					:							Optional[Any]			=   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 					lowerCAmelCase (     self :		str							)	->     List[str]:


												"""simple docstring"""

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



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


												"""simple docstring"""

												__lowercase					:							Dict			=   self.prepare_config_and_inputs()
												__lowercase	,	__lowercase	,	__lowercase					:							Optional[Any]			=   config_and_inputs
												__lowercase					:							int			=   {"""pixel_values""": pixel_values, """labels""": labels}
												return config, inputs_dict







@require_torch
class 						lowerCAmelCase   (       __a					,				__a					,				unittest.TestCase	):

					'''simple docstring'''

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

					_A  :	str     =					False
					_A  :	Dict     =					False
					_A  :	str     =					False
					_A  :	str     =					False
					_A  :	Dict     =					False



					def 					lowerCAmelCase (     self :		Optional[Any]							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   ConvNextVaModelTester(self							)
												__lowercase					:							Optional[int]			=   ConfigTester(self						,   config_class=__a						,   has_text_modality=__a						,   hidden_size=37							)



					def 					lowerCAmelCase (     self :		int							)	->     Optional[int]:


												"""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 :		int							)	->     str:


												"""simple docstring"""

												return



					@unittest.skip(reason="""ConvNextV2 does not use inputs_embeds"""							)
					def 					lowerCAmelCase (     self :		int							)	->     Any:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""ConvNextV2 does not support input and output embeddings"""							)
					def 					lowerCAmelCase (     self :		Any							)	->     Union[str, Any]:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""ConvNextV2 does not use feedforward chunking"""							)
					def 					lowerCAmelCase (     self :		Any							)	->     Optional[int]:


												"""simple docstring"""

												pass



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


												"""simple docstring"""

												if not self.model_tester.is_training:
																			return

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

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

																			__lowercase					:							Union[str, Any]			=   model_class(__a							)
																			model.to(__a							)
																			model.train()
																			__lowercase					:							Tuple			=   self._prepare_for_class(__a						,   __a						,   return_labels=__a							)
																			__lowercase					:							Dict			=   model(**__a							).loss
																			loss.backward()



					def 					lowerCAmelCase (     self :		Optional[int]							)	->     int:


												"""simple docstring"""

												if not self.model_tester.is_training:
																			return

												for model_class in self.all_model_classes:
																			__lowercase	,	__lowercase					:							Tuple			=   self.model_tester.prepare_config_and_inputs_with_labels()
																			__lowercase					:							Dict			=   False
																			__lowercase					:							Optional[Any]			=   True

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

																			__lowercase					:							List[Any]			=   model_class(__a							)
																			model.to(__a							)
																			model.gradient_checkpointing_enable()
																			model.train()
																			__lowercase					:							List[str]			=   self._prepare_for_class(__a						,   __a						,   return_labels=__a							)
																			__lowercase					:							int			=   model(**__a							).loss
																			loss.backward()



					def 					lowerCAmelCase (     self :		Tuple							)	->     Union[str, Any]:


												"""simple docstring"""

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

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

																			__lowercase					:							Optional[Any]			=   ["""pixel_values"""]
																			self.assertListEqual(arg_names[:1]						,   __a							)



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


												"""simple docstring"""

												__lowercase					:							List[Any]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__a							)



					def 					lowerCAmelCase (     self :		Dict							)	->     List[str]:


												"""simple docstring"""

												def check_hidden_states_output(__a :		Any						,   __a :		Tuple						,   __a :		Tuple							):
																			__lowercase					:							Optional[Any]			=   model_class(__a							)
																			model.to(__a							)
																			model.eval()

																			with torch.no_grad():
																										__lowercase					:							int			=   model(**self._prepare_for_class(__a						,   __a							)							)

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

																			__lowercase					:							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]						,   )

												__lowercase	,	__lowercase					:							Optional[Any]			=   self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																			__lowercase					:							int			=   True
																			check_hidden_states_output(__a						,   __a						,   __a							)

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

																			check_hidden_states_output(__a						,   __a						,   __a							)



					def 					lowerCAmelCase (     self :		Dict							)	->     Union[str, Any]:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__a							)



					@slow
					def 					lowerCAmelCase (     self :		Dict							)	->     Tuple:


												"""simple docstring"""

												for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			__lowercase					:							Optional[int]			=   ConvNextVaModel.from_pretrained(__a							)
																			self.assertIsNotNone(__a							)







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



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

					'''simple docstring'''

					@cached_property
					def 					lowerCAmelCase (     self :		Any							)	->     List[str]:


												"""simple docstring"""

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



					@slow
					def 					lowerCAmelCase (     self :		Dict							)	->     str:


												"""simple docstring"""

												__lowercase					:							Optional[Any]			=   ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224"""							).to(__a							)

												__lowercase					:							Dict			=   self.default_image_processor
												__lowercase					:							Any			=   prepare_img()
												__lowercase					:							int			=   preprocessor(images=__a						,   return_tensors="""pt"""							).to(__a							)

												# forward pass
												with torch.no_grad():
																			__lowercase					:							int			=   model(**__a							)

												# verify the logits
												__lowercase					:							List[str]			=   torch.Size((1, 1000)							)
												self.assertEqual(outputs.logits.shape						,   __a							)

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