'''simple docstring'''




import inspect
import unittest

from transformers import ConvNextConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
						import torch

						from transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel
						from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor





class     __magic_name__						:







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







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[int]    ):
										lowercase_					:			Tuple										=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

										lowercase_					:			Dict										=       self.get_config()
										return config, pixel_values, labels







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]    ):
										return ConvNextConfig(
										    num_channels=self.num_channels				,			hidden_sizes=self.hidden_sizes				,			depths=self.depths				,			num_stages=self.num_stages				,			hidden_act=self.hidden_act				,			is_decoder=lowercase_				,			initializer_range=self.initializer_range				,			out_features=self.out_features				,			out_indices=self.out_indices				,			num_labels=self.num_labels				,			)







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Dict				,			lowercase_				:  Optional[int]				,			lowercase_				:  Any				,			lowercase_				:  Dict    ):
										lowercase_					:			List[str]										=       ConvNextModel(config=lowercase_    )
										model.to(lowercase_    )
										model.eval()
										lowercase_					:			Any										=       model(lowercase_    )
										# 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 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[int]				,			lowercase_				:  int				,			lowercase_				:  int				,			lowercase_				:  Tuple    ):
										lowercase_					:			Dict										=       ConvNextForImageClassification(lowercase_    )
										model.to(lowercase_    )
										model.eval()
										lowercase_					:			Optional[Any]										=       model(lowercase_				,			labels=lowercase_    )
										self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.num_labels)    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]				,			lowercase_				:  Union[str, Any]				,			lowercase_				:  List[str]				,			lowercase_				:  List[Any]    ):
										lowercase_					:			Union[str, Any]										=       ConvNextBackbone(config=lowercase_    )
										model.to(lowercase_    )
										model.eval()
										lowercase_					:			Union[str, Any]										=       model(lowercase_    )

										# 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_					:			int										=       None
										lowercase_					:			Dict										=       ConvNextBackbone(config=lowercase_    )
										model.to(lowercase_    )
										model.eval()
										lowercase_					:			Any										=       model(lowercase_    )

										# 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 	SCREAMING_SNAKE_CASE_     (	self				:  int    ):
										lowercase_					:			List[str]										=       self.prepare_config_and_inputs()
										lowercase_						,						lowercase_						,						lowercase_					:			Tuple										=       config_and_inputs
										lowercase_					:			List[str]										=       {"""pixel_values""": pixel_values}
										return config, inputs_dict





@require_torch
class     __magic_name__						(    _UpperCAmelCase,     _UpperCAmelCase,     unittest.TestCase):
			UpperCamelCase__				=       (
			    (
			        ConvNextModel,
			        ConvNextForImageClassification,
			        ConvNextBackbone,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase__				=       (
			    {'''feature-extraction''': ConvNextModel, '''image-classification''': ConvNextForImageClassification}
			    if is_torch_available()
			    else {}
			)

			UpperCamelCase__				=       True
			UpperCamelCase__				=       False
			UpperCamelCase__				=       False
			UpperCamelCase__				=       False
			UpperCamelCase__				=       False







			def 	SCREAMING_SNAKE_CASE_     (	self				:  List[Any]    ):
										lowercase_					:			Optional[Any]										=       ConvNextModelTester(self    )
										lowercase_					:			int										=       ConfigTester(self				,			config_class=lowercase_				,			has_text_modality=lowercase_				,			hidden_size=37    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Union[str, Any]    ):
										self.create_and_test_config_common_properties()
										self.config_tester.create_and_test_config_to_json_string()
										self.config_tester.create_and_test_config_to_json_file()
										self.config_tester.create_and_test_config_from_and_save_pretrained()
										self.config_tester.create_and_test_config_with_num_labels()
										self.config_tester.check_config_can_be_init_without_params()
										self.config_tester.check_config_arguments_init()







			def 	SCREAMING_SNAKE_CASE_     (	self				:  int    ):
										return







			@unittest.skip(reason="""ConvNext does not use inputs_embeds"""    )
			def 	SCREAMING_SNAKE_CASE_     (	self				:  int    ):
										pass







			@unittest.skip(reason="""ConvNext does not support input and output embeddings"""    )
			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]    ):
										pass







			@unittest.skip(reason="""ConvNext does not use feedforward chunking"""    )
			def 	SCREAMING_SNAKE_CASE_     (	self				:  Tuple    ):
										pass







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[int]    ):
										lowercase_						,						lowercase_					:			Optional[Any]										=       self.model_tester.prepare_config_and_inputs_for_common()

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

																	lowercase_					:			Tuple										=       ["""pixel_values"""]
																	self.assertListEqual(arg_names[:1]				,			lowercase_    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  str    ):
										lowercase_					:			Union[str, Any]										=       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowercase_    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  int    ):
										lowercase_					:			Dict										=       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_backbone(*lowercase_    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  List[Any]    ):
										def check_hidden_states_output(lowercase_				:  Optional[Any]				,			lowercase_				:  Any				,			lowercase_				:  Any    ):
																	lowercase_					:			Any										=       model_class(lowercase_    )
																	model.to(lowercase_    )
																	model.eval()

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

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

																	lowercase_					:			Union[str, Any]										=       self.model_tester.num_stages
																	self.assertEqual(len(lowercase_    )				,			expected_num_stages + 1    )

																	# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
																	self.assertListEqual(
																	    list(hidden_states[0].shape[-2:]    )				,			[self.model_tester.image_size // 4, self.model_tester.image_size // 4]				,			)

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

										for model_class in self.all_model_classes:
																	lowercase_					:			Optional[Any]										=       True
																	check_hidden_states_output(lowercase_				,			lowercase_				,			lowercase_    )

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

																	check_hidden_states_output(lowercase_				,			lowercase_				,			lowercase_    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  str    ):
										lowercase_					:			Any										=       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowercase_    )







			@slow
			def 	SCREAMING_SNAKE_CASE_     (	self				:  Union[str, Any]    ):
										for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_					:			Optional[int]										=       ConvNextModel.from_pretrained(lowercase_    )
																	self.assertIsNotNone(lowercase_    )




def 							lowerCamelCase	(      )			->      Dict:
							lowercase_					:			List[str]										=       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
							return image





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







			@cached_property
			def 	SCREAMING_SNAKE_CASE_     (	self				:  Tuple    ):
										return AutoImageProcessor.from_pretrained("""facebook/convnext-tiny-224"""    ) if is_vision_available() else None







			@slow
			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]    ):
										lowercase_					:			Dict										=       ConvNextForImageClassification.from_pretrained("""facebook/convnext-tiny-224"""    ).to(lowercase_    )

										lowercase_					:			Dict										=       self.default_image_processor
										lowercase_					:			int										=       prepare_img()
										lowercase_					:			Union[str, Any]										=       image_processor(images=lowercase_				,			return_tensors="""pt"""    ).to(lowercase_    )

										# forward pass
										with torch.no_grad():
																	lowercase_					:			str										=       model(**lowercase_    )

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

										lowercase_					:			Optional[Any]										=       torch.tensor([-0.02_60, -0.47_39, 0.19_11]    ).to(lowercase_    )

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





@require_torch
class     __magic_name__						(    unittest.TestCase,     _UpperCAmelCase):
			UpperCamelCase__				=       (ConvNextBackbone,) if is_torch_available() else ()
			UpperCamelCase__				=       ConvNextConfig

			UpperCamelCase__				=       False







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Dict    ):
										lowercase_					:			List[str]										=       ConvNextModelTester(self    )






'''simple docstring'''




import unittest

from transformers import load_tool
from transformers.utils import is_torch_available


if is_torch_available():
						import torch

from transformers.testing_utils import require_torch

from .test_tools_common import ToolTesterMixin





@require_torch
class     __magic_name__						(    unittest.TestCase,     _UpperCAmelCase):







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]    ):
										lowercase_					:			List[str]										=       load_tool("""text-to-speech"""    )
										self.tool.setup()







			def 	SCREAMING_SNAKE_CASE_     (	self				:  List[Any]    ):
										# SpeechT5 isn't deterministic
										torch.manual_seed(0    )
										lowercase_					:			List[str]										=       self.tool("""hey"""    )
										lowercase_					:			Optional[int]										=       result.to_raw()
										self.assertTrue(
										    torch.allclose(
										        resulting_tensor[:3]				,			torch.tensor([-0.0_00_59_66_66_88_32_11_58_29, -0.0_00_36_57_64_01_90_79_50_64, -0.00_01_34_39_50_27_99_88_34_85]    )				,			)    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  List[Any]    ):
										# SpeechT5 isn't deterministic
										torch.manual_seed(0    )
										lowercase_					:			Union[str, Any]										=       self.tool("""hey"""    )
										lowercase_					:			int										=       result.to_raw()
										self.assertTrue(
										    torch.allclose(
										        resulting_tensor[:3]				,			torch.tensor([-0.0_00_59_66_66_88_32_11_58_29, -0.0_00_36_57_64_01_90_79_50_64, -0.00_01_34_39_50_27_99_88_34_85]    )				,			)    )





