
import copy
import inspect
import unittest

from transformers import PretrainedConfig, SwiftFormerConfig
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 torch import nn

			from transformers import SwiftFormerForImageClassification, SwiftFormerModel
			from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import ViTImageProcessor





class 	_lowerCAmelCase					:




				def __init__(					self				,				_UpperCamelCase				,				_UpperCamelCase=13				,				_UpperCamelCase=3				,				_UpperCamelCase=True				,				_UpperCamelCase=True				,				_UpperCamelCase=0.1				,				_UpperCamelCase=0.1				,				_UpperCamelCase=224				,				_UpperCamelCase=1_000				,				_UpperCamelCase=[3, 3, 6, 4]				,				_UpperCamelCase=[48, 56, 112, 220]				,				)				->      int:
							lowerCAmelCase_						=	parent
							lowerCAmelCase_						=	batch_size
							lowerCAmelCase_						=	num_channels
							lowerCAmelCase_						=	is_training
							lowerCAmelCase_						=	use_labels
							lowerCAmelCase_						=	hidden_dropout_prob
							lowerCAmelCase_						=	attention_probs_dropout_prob
							lowerCAmelCase_						=	num_labels
							lowerCAmelCase_						=	image_size
							lowerCAmelCase_						=	layer_depths
							lowerCAmelCase_						=	embed_dims




				def      __a						(					self     )				->      Optional[int]:
							lowerCAmelCase_						=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

							lowerCAmelCase_						=	None
							if self.use_labels:
										lowerCAmelCase_						=	ids_tensor([self.batch_size]				,				self.num_labels     )

							lowerCAmelCase_						=	self.get_config()

							return config, pixel_values, labels




				def      __a						(					self     )				->      Any:
							return SwiftFormerConfig(
							    depths=self.layer_depths				,				embed_dims=self.embed_dims				,				mlp_ratio=4				,				downsamples=[True, True, True, True]				,				hidden_act="gelu"				,				num_labels=self.num_labels				,				down_patch_size=3				,				down_stride=2				,				down_pad=1				,				drop_rate=0.0				,				drop_path_rate=0.0				,				use_layer_scale=_UpperCamelCase				,				layer_scale_init_value=1e-5				,				)




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )				->      List[Any]:
							lowerCAmelCase_						=	SwiftFormerModel(config=_UpperCamelCase     )
							model.to(_UpperCamelCase     )
							model.eval()
							lowerCAmelCase_						=	model(_UpperCamelCase     )
							self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.embed_dims[-1], 7, 7)     )




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )				->      int:
							lowerCAmelCase_						=	self.num_labels
							lowerCAmelCase_						=	SwiftFormerForImageClassification(_UpperCamelCase     )
							model.to(_UpperCamelCase     )
							model.eval()
							lowerCAmelCase_						=	model(_UpperCamelCase				,				labels=_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels)     )

							lowerCAmelCase_						=	SwiftFormerForImageClassification(_UpperCamelCase     )
							model.to(_UpperCamelCase     )
							model.eval()

							lowerCAmelCase_						=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )
							lowerCAmelCase_						=	model(_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels)     )




				def      __a						(					self     )				->      Dict:
							((lowerCAmelCase_)	,  (lowerCAmelCase_)	,  (lowerCAmelCase_))						=	self.prepare_config_and_inputs()
							lowerCAmelCase_						=	{"pixel_values": pixel_values}
							return config, inputs_dict





@require_torch
class 	_lowerCAmelCase					(	__a					,      __a					,      unittest.TestCase       ):
				_lowercase					=(SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
				_lowercase					=(
				    {'''feature-extraction''': SwiftFormerModel, '''image-classification''': SwiftFormerForImageClassification}
				    if is_torch_available()
				    else {}
				)

				_lowercase					=False
				_lowercase					=False
				_lowercase					=False
				_lowercase					=False
				_lowercase					=False




				def      __a						(					self     )				->      Optional[Any]:
							lowerCAmelCase_						=	SwiftFormerModelTester(self     )
							lowerCAmelCase_						=	ConfigTester(
							    self				,				config_class=_UpperCamelCase				,				has_text_modality=_UpperCamelCase				,				hidden_size=37				,				num_attention_heads=12				,				num_hidden_layers=12				,				)




				def      __a						(					self     )				->      Tuple:
							self.config_tester.run_common_tests()




				@unittest.skip(reason="SwiftFormer does not use inputs_embeds"     )
				def      __a						(					self     )				->      Any:
							pass




				def      __a						(					self     )				->      Union[str, Any]:
							lowerCAmelCase_	,  lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										lowerCAmelCase_						=	model_class(_UpperCamelCase     )
										lowerCAmelCase_						=	model.get_output_embeddings()
										self.assertTrue(x is None or isinstance(_UpperCamelCase				,				nn.Linear     )     )




				def      __a						(					self     )				->      str:
							lowerCAmelCase_	,  lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs_for_common()

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

										lowerCAmelCase_						=	["pixel_values"]
										self.assertListEqual(arg_names[:1]				,				_UpperCamelCase     )




				def      __a						(					self     )				->      Tuple:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_UpperCamelCase     )




				def      __a						(					self     )				->      Any:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase     )




				@slow
				def      __a						(					self     )				->      List[str]:
							for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase_						=	SwiftFormerModel.from_pretrained(_UpperCamelCase     )
										self.assertIsNotNone(_UpperCamelCase     )




				@unittest.skip(reason="SwiftFormer does not output attentions"     )
				def      __a						(					self     )				->      Optional[Any]:
							pass




				def      __a						(					self     )				->      List[Any]:
							def check_hidden_states_output(_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     ):
										lowerCAmelCase_						=	model_class(_UpperCamelCase     )
										model.to(_UpperCamelCase     )
										model.eval()

										with torch.no_grad():
													lowerCAmelCase_						=	model(**self._prepare_for_class(_UpperCamelCase				,				_UpperCamelCase     )     )

										lowerCAmelCase_						=	outputs.hidden_states

										lowerCAmelCase_						=	8
										self.assertEqual(len(_UpperCamelCase     )				,				_UpperCamelCase     )  # TODO

										# SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width)
										# with the width and height being successively divided by 2, after every 2 blocks
										for i in range(len(_UpperCamelCase     )     ):
													self.assertEqual(
													    hidden_states[i].shape				,				torch.Size(
													        [
													            self.model_tester.batch_size,
													            self.model_tester.embed_dims[i // 2],
													            (self.model_tester.image_size // 4) // 2 ** (i // 2),
													            (self.model_tester.image_size // 4) // 2 ** (i // 2),
													        ]     )				,				)

							lowerCAmelCase_	,  lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										lowerCAmelCase_						=	True
										check_hidden_states_output(_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )

										# check that output_hidden_states also work using config
										del inputs_dict["output_hidden_states"]
										lowerCAmelCase_						=	True

										check_hidden_states_output(_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )




				def      __a						(					self     )				->      List[str]:
							def _config_zero_init(_UpperCamelCase     ):
										lowerCAmelCase_						=	copy.deepcopy(_UpperCamelCase     )
										for key in configs_no_init.__dict__.keys():
													if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key:
																setattr(_UpperCamelCase				,				_UpperCamelCase				,				1e-1_0     )
													if isinstance(getattr(_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )				,				_UpperCamelCase     ):
																lowerCAmelCase_						=	_config_zero_init(getattr(_UpperCamelCase				,				_UpperCamelCase     )     )
																setattr(_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )
										return configs_no_init

							lowerCAmelCase_	,  lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs_for_common()

							lowerCAmelCase_						=	_config_zero_init(_UpperCamelCase     )
							for model_class in self.all_model_classes:
										lowerCAmelCase_						=	model_class(config=_UpperCamelCase     )
										for name, param in model.named_parameters():
													if param.requires_grad:
																self.assertIn(
																    ((param.data.mean() * 1e9) / 1e9).round().item()				,				[0.0, 1.0]				,				msg=f"""Parameter {name} of model {model_class} seems not properly initialized"""				,				)




				@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."     )
				def      __a						(					self     )				->      Dict:
							pass






def    lowerCamelCase__				(				):




			"""simple docstring"""


			lowerCAmelCase_						=	Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
			return image





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




				@cached_property
				def      __a						(					self     )				->      str:
							return ViTImageProcessor.from_pretrained("MBZUAI/swiftformer-xs"     ) if is_vision_available() else None




				@slow
				def      __a						(					self     )				->      Optional[int]:
							lowerCAmelCase_						=	SwiftFormerForImageClassification.from_pretrained("MBZUAI/swiftformer-xs"     ).to(_UpperCamelCase     )

							lowerCAmelCase_						=	self.default_image_processor
							lowerCAmelCase_						=	prepare_img()
							lowerCAmelCase_						=	image_processor(images=_UpperCamelCase				,				return_tensors="pt"     ).to(_UpperCamelCase     )

							# forward pass
							with torch.no_grad():
										lowerCAmelCase_						=	model(**_UpperCamelCase     )

							# verify the logits
							lowerCAmelCase_						=	torch.Size((1, 1_000)     )
							self.assertEqual(outputs.logits.shape				,				_UpperCamelCase     )

							lowerCAmelCase_						=	torch.tensor([[-2.1_7_0_3e0_0, 2.1_1_0_7e0_0, -2.0_8_1_1e0_0]]     ).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_sentencepiece_available,
    is_speech_available,
    is_tf_available,
    is_torch_available,
)


_A   =     {
    "configuration_speech_to_text": ["SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2TextConfig"],
    "processing_speech_to_text": ["Speech2TextProcessor"],
}

try:
			if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_A   =     ["Speech2TextTokenizer"]

try:
			if not is_speech_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_A   =     ["Speech2TextFeatureExtractor"]

try:
			if not is_tf_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_A   =     [
			    "TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
			    "TFSpeech2TextForConditionalGeneration",
			    "TFSpeech2TextModel",
			    "TFSpeech2TextPreTrainedModel",
			]

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_A   =     [
			    "SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
			    "Speech2TextForConditionalGeneration",
			    "Speech2TextModel",
			    "Speech2TextPreTrainedModel",
			]


if TYPE_CHECKING:
			from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig
			from .processing_speech_to_text import SpeechaTextProcessor

			try:
						if not is_sentencepiece_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .tokenization_speech_to_text import SpeechaTextTokenizer

			try:
						if not is_speech_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor

			try:
						if not is_tf_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_speech_to_text import (
						    TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFSpeechaTextForConditionalGeneration,
						    TFSpeechaTextModel,
						    TFSpeechaTextPreTrainedModel,
						)

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_speech_to_text import (
						    SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    SpeechaTextForConditionalGeneration,
						    SpeechaTextModel,
						    SpeechaTextPreTrainedModel,
						)

else:
			import sys

			_A   =     _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



