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


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





												'''simple docstring'''





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


						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

												SCREAMING_SNAKE_CASE							:    Tuple							=							self.get_config()

												return config, pixel_values, labels


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


						def        __A       (       self	:				List[str]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				str						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							ConvNextVaModel(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Dict							=							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        __A       (       self	:				Any    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							ConvNextVaForImageClassification(UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Any							=							model(UpperCamelCase__    ,		labels=UpperCamelCase__						)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_labels)						)


						def        __A       (       self	:				Union[str, Any]    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							ConvNextVaBackbone(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    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
												SCREAMING_SNAKE_CASE							:    str							=							None
												SCREAMING_SNAKE_CASE							:    List[str]							=							ConvNextVaBackbone(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    List[str]							=							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        __A       (       self	:				List[Any]						):





												'''simple docstring'''





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



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





												'''simple docstring'''





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






@require_torch
class 					lowercase__						(   UpperCamelCase_	,							UpperCamelCase_	,							unittest.TestCase):
						UpperCamelCase_     =		(
						    (
						        ConvNextVaModel,
						        ConvNextVaForImageClassification,
						        ConvNextVaBackbone,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCamelCase_     =		(
						    {"""feature-extraction""": ConvNextVaModel, """image-classification""": ConvNextVaForImageClassification}
						    if is_torch_available()
						    else {}
						)

						UpperCamelCase_     =		False
						UpperCamelCase_     =		False
						UpperCamelCase_     =		False
						UpperCamelCase_     =		False
						UpperCamelCase_     =		False


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    str							=							ConvNextVaModelTester(self						)
												SCREAMING_SNAKE_CASE							:    str							=							ConfigTester(self    ,		config_class=UpperCamelCase__    ,		has_text_modality=UpperCamelCase__    ,		hidden_size=37						)


						def        __A       (       self	:				List[Any]						):





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





												'''simple docstring'''





												return


						@unittest.skip(reason='''ConvNextV2 does not use inputs_embeds'''						)
						def        __A       (       self	:				Tuple						):





												'''simple docstring'''





												pass


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





												'''simple docstring'''





												pass


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





												'''simple docstring'''





												pass


						def        __A       (       self	:				str						):





												'''simple docstring'''





												if not self.model_tester.is_training:
																		return

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

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

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


						def        __A       (       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							:    Tuple							=							self.model_tester.prepare_config_and_inputs_with_labels()
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							False
																		SCREAMING_SNAKE_CASE							:    Any							=							True

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

																		SCREAMING_SNAKE_CASE							:    List[Any]							=							model_class(UpperCamelCase__						)
																		model.to(UpperCamelCase__						)
																		model.gradient_checkpointing_enable()
																		model.train()
																		SCREAMING_SNAKE_CASE							:    Any							=							self._prepare_for_class(UpperCamelCase__    ,		UpperCamelCase__    ,		return_labels=UpperCamelCase__						)
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							model(**UpperCamelCase__						).loss
																		loss.backward()


						def        __A       (       self	:				int						):





												'''simple docstring'''





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

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

																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							['''pixel_values''']
																		self.assertListEqual(arg_names[:1]    ,		UpperCamelCase__						)


						def        __A       (       self	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*UpperCamelCase__						)


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												def check_hidden_states_output(UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				Optional[int]						):
																		SCREAMING_SNAKE_CASE							:    int							=							model_class(UpperCamelCase__						)
																		model.to(UpperCamelCase__						)
																		model.eval()

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

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

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

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

												for model_class in self.all_model_classes:
																		SCREAMING_SNAKE_CASE							:    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"]
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							True

																		check_hidden_states_output(UpperCamelCase__    ,		UpperCamelCase__    ,		UpperCamelCase__						)


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





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__						)



						@slow
						def        __A       (       self	:				Tuple						):





												'''simple docstring'''





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






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





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


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





												'''simple docstring'''





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



						@slow
						def        __A       (       self	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    str							=							ConvNextVaForImageClassification.from_pretrained('''facebook/convnextv2-tiny-1k-224'''						).to(UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    List[Any]							=							self.default_image_processor
												SCREAMING_SNAKE_CASE							:    Dict							=							prepare_img()
												SCREAMING_SNAKE_CASE							:    str							=							preprocessor(images=UpperCamelCase__    ,		return_tensors='''pt'''						).to(UpperCamelCase__						)

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

												# verify the logits
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							torch.Size((1, 1000)						)
												self.assertEqual(outputs.logits.shape    ,		UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							torch.tensor([0.9996, 0.1966, -0.4386]						).to(UpperCamelCase__						)
												self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,		UpperCamelCase__    ,		atol=1E-4						)						)






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


__UpperCamelCase		:       Tuple									=     {
    'configuration_swiftformer': [
        'SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'SwiftFormerConfig',
        'SwiftFormerOnnxConfig',
    ]
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__UpperCamelCase		:       Optional[Any]									=     [
					    'SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'SwiftFormerForImageClassification',
					    'SwiftFormerModel',
					    'SwiftFormerPreTrainedModel',
					]

if TYPE_CHECKING:
					from .configuration_swiftformer import (
					    SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    SwiftFormerConfig,
					    SwiftFormerOnnxConfig,
					)

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_swiftformer import (
										    SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    SwiftFormerForImageClassification,
										    SwiftFormerModel,
										    SwiftFormerPreTrainedModel,
										)

else:
					import sys

					__UpperCamelCase		:       List[Any]									=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





