




import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
							from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor




class 		__A   :

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

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

						def      A__    (		self :Any			):
										'''simple docstring'''

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

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

										__magic_name__						:						int										=self.get_config()

										return config, pixel_values, labels

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

						def      A__    (		self :Optional[Any]     ,			__snake_case :Tuple     ,			__snake_case :List[Any]     ,			__snake_case :Optional[int]			):
										'''simple docstring'''

										__magic_name__						:						List[str]										=ConvNextVaModel(config=__snake_case			)
										model.to(__snake_case			)
										model.eval()
										__magic_name__						:						Dict										=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      A__    (		self :Dict     ,			__snake_case :List[str]     ,			__snake_case :int     ,			__snake_case :List[Any]			):
										'''simple docstring'''

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

						def      A__    (		self :str     ,			__snake_case :Tuple     ,			__snake_case :List[str]     ,			__snake_case :Tuple			):
										'''simple docstring'''

										__magic_name__						:						Union[str, Any]										=ConvNextVaBackbone(config=__snake_case			)
										model.to(__snake_case			)
										model.eval()
										__magic_name__						:						List[Any]										=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
										__magic_name__						:						int										=None
										__magic_name__						:						Tuple										=ConvNextVaBackbone(config=__snake_case			)
										model.to(__snake_case			)
										model.eval()
										__magic_name__						:						Union[str, Any]										=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      A__    (		self :List[str]			):
										'''simple docstring'''

										__magic_name__						:						Any										=self.prepare_config_and_inputs()
										__magic_name__					,			__magic_name__					,			__magic_name__						:						Tuple										=config_and_inputs
										__magic_name__						:						Union[str, Any]										={"""pixel_values""": pixel_values}
										return config, inputs_dict


						def      A__    (		self :Optional[Any]			):
										'''simple docstring'''

										__magic_name__						:						List[Any]										=self.prepare_config_and_inputs()
										__magic_name__					,			__magic_name__					,			__magic_name__						:						Any										=config_and_inputs
										__magic_name__						:						str										={"""pixel_values""": pixel_values, """labels""": labels}
										return config, inputs_dict




@require_torch
class 		__A   (      __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 :Dict			):
										'''simple docstring'''

										__magic_name__						:						List[str]										=ConvNextVaModelTester(self			)
										__magic_name__						:						Any										=ConfigTester(self     ,			config_class=__snake_case     ,			has_text_modality=__snake_case     ,			hidden_size=37			)

						def      A__    (		self :Union[str, 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 :List[Any]			):
										'''simple docstring'''

										pass

						@unittest.skip(reason="""ConvNextV2 does not support input and output embeddings"""			)
						def      A__    (		self :Dict			):
										'''simple docstring'''

										pass

						@unittest.skip(reason="""ConvNextV2 does not use feedforward chunking"""			)
						def      A__    (		self :Tuple			):
										'''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:
														__magic_name__					,			__magic_name__						:						str										=self.model_tester.prepare_config_and_inputs_with_labels()
														__magic_name__						:						Union[str, Any]										=True

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

														__magic_name__						:						Dict										=model_class(__snake_case			)
														model.to(__snake_case			)
														model.train()
														__magic_name__						:						Dict										=self._prepare_for_class(__snake_case     ,			__snake_case     ,			return_labels=__snake_case			)
														__magic_name__						:						Dict										=model(**__snake_case			).loss
														loss.backward()

						def      A__    (		self :List[Any]			):
										'''simple docstring'''

										if not self.model_tester.is_training:
														return

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

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

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

						def      A__    (		self :Union[str, Any]			):
										'''simple docstring'''

										__magic_name__					,			__magic_name__						:						List[Any]										=self.model_tester.prepare_config_and_inputs_for_common()

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

														__magic_name__						:						List[str]										=["""pixel_values"""]
														self.assertListEqual(arg_names[:1]     ,			__snake_case			)

						def      A__    (		self :List[str]			):
										'''simple docstring'''

										__magic_name__						:						Union[str, Any]										=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__snake_case			)

						def      A__    (		self :str			):
										'''simple docstring'''

										def check_hidden_states_output(__snake_case :List[Any]     ,			__snake_case :Optional[Any]     ,			__snake_case :Dict			):
														__magic_name__						:						Dict										=model_class(__snake_case			)
														model.to(__snake_case			)
														model.eval()

														with torch.no_grad():
																		__magic_name__						:						Dict										=model(**self._prepare_for_class(__snake_case     ,			__snake_case			)			)

														__magic_name__						:						Any										=outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

														__magic_name__						:						str										=self.model_tester.num_stages
														self.assertEqual(len(__snake_case			)     ,			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]     ,			)

										__magic_name__					,			__magic_name__						:						str										=self.model_tester.prepare_config_and_inputs_for_common()

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

														check_hidden_states_output(__snake_case     ,			__snake_case     ,			__snake_case			)

						def      A__    (		self :Any			):
										'''simple docstring'''

										__magic_name__						:						Any										=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__snake_case			)


						@slow
						def      A__    (		self :Dict			):
										'''simple docstring'''

										for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__magic_name__						:						Optional[Any]										=ConvNextVaModel.from_pretrained(__snake_case			)
														self.assertIsNotNone(__snake_case			)




def 		lowerCAmelCase_ (						):
				__magic_name__						:						Optional[Any]										=Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
				return image




@require_torch
@require_vision
class 		__A   (      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 :Union[str, Any]			):
										'''simple docstring'''

										__magic_name__						:						Dict										=ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224"""			).to(__snake_case			)

										__magic_name__						:						List[Any]										=self.default_image_processor
										__magic_name__						:						Optional[Any]										=prepare_img()
										__magic_name__						:						Tuple										=preprocessor(images=__snake_case     ,			return_tensors="""pt"""			).to(__snake_case			)

										# forward pass
										with torch.no_grad():
														__magic_name__						:						Dict										=model(**__snake_case			)

										# verify the logits
										__magic_name__						:						int										=torch.Size((1, 10_00)			)
										self.assertEqual(outputs.logits.shape     ,			__snake_case			)

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






import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
     from transformers import TvltImageProcessor

if is_speech_available():
     from transformers import TvltFeatureExtractor

from transformers import TvltProcessor




@require_torch
class        _SCREAMING_SNAKE_CASE	(   unittest.TestCase			):
      def A_      (      self			):
          snake_case__    =					"ZinengTang/tvlt-base"
          snake_case__    =					tempfile.mkdtemp()
      def A_      (      self		,						**lowerCamelCase			):
          return TvltImageProcessor.from_pretrained(self.checkpoint		,						**lowerCamelCase			)
      def A_      (      self		,						**lowerCamelCase			):
          return TvltFeatureExtractor.from_pretrained(self.checkpoint		,						**lowerCamelCase			)
      def A_      (      self			):
          shutil.rmtree(self.tmpdirname			)
      def A_      (      self			):
          snake_case__    =					self.get_image_processor()
          snake_case__    =					self.get_feature_extractor()

          snake_case__    =					TvltProcessor(image_processor=lowerCamelCase		,						feature_extractor=lowerCamelCase			)
          processor.save_pretrained(self.tmpdirname			)
          snake_case__    =					TvltProcessor.from_pretrained(self.tmpdirname			)

          self.assertIsInstance(processor.feature_extractor		,						lowerCamelCase			)
          self.assertIsInstance(processor.image_processor		,						lowerCamelCase			)
      def A_      (      self			):
          snake_case__    =					self.get_image_processor()
          snake_case__    =					self.get_feature_extractor()

          snake_case__    =					TvltProcessor(image_processor=lowerCamelCase		,						feature_extractor=lowerCamelCase			)

          snake_case__    =					np.ones([1_20_00]			)

          snake_case__    =					feature_extractor(lowerCamelCase		,						return_tensors="np"			)
          snake_case__    =					processor(audio=lowerCamelCase		,						return_tensors="np"			)

          for key in audio_dict.keys():
              self.assertAlmostEqual(audio_dict[key].sum()		,						input_processor[key].sum()		,						delta=1e-2			)
      def A_      (      self			):
          snake_case__    =					self.get_image_processor()
          snake_case__    =					self.get_feature_extractor()

          snake_case__    =					TvltProcessor(image_processor=lowerCamelCase		,						feature_extractor=lowerCamelCase			)

          snake_case__    =					np.ones([3, 2_24, 2_24]			)

          snake_case__    =					image_processor(lowerCamelCase		,						return_tensors="np"			)
          snake_case__    =					processor(images=lowerCamelCase		,						return_tensors="np"			)

          for key in image_dict.keys():
              self.assertAlmostEqual(image_dict[key].sum()		,						input_processor[key].sum()		,						delta=1e-2			)
      def A_      (      self			):
          snake_case__    =					self.get_image_processor()
          snake_case__    =					self.get_feature_extractor()

          snake_case__    =					TvltProcessor(image_processor=lowerCamelCase		,						feature_extractor=lowerCamelCase			)

          snake_case__    =					np.ones([1_20_00]			)
          snake_case__    =					np.ones([3, 2_24, 2_24]			)

          snake_case__    =					processor(audio=lowerCamelCase		,						images=lowerCamelCase			)

          self.assertListEqual(list(inputs.keys()			)		,						["audio_values", "audio_mask", "pixel_values", "pixel_mask"]			)

          # test if it raises when no input is passed
          with pytest.raises(lowerCamelCase			):
              processor()



      def A_      (      self			):
          snake_case__    =					self.get_image_processor()
          snake_case__    =					self.get_feature_extractor()

          snake_case__    =					TvltProcessor(image_processor=lowerCamelCase		,						feature_extractor=lowerCamelCase			)

          self.assertListEqual(
              processor.model_input_names		,						image_processor.model_input_names + feature_extractor.model_input_names		,						msg="`processor` and `image_processor`+`feature_extractor` model input names do not match"		,						)



