


'''simple docstring'''



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




	def __init__(       self						,    __a						,    __a=13						,    __a=3						,    __a=True						,    __a=True						,    __a=0.1						,    __a=0.1						,    __a=224						,    __a=1000						,    __a=[3, 3, 6, 4]						,    __a=[48, 56, 112, 220]						,    ):

						'''simple docstring'''





						__a      :	Optional[int]    =  parent
						__a      :	Dict    =  batch_size
						__a      :	Dict    =  num_channels
						__a      :	str    =  is_training
						__a      :	Optional[int]    =  use_labels
						__a      :	Union[str, Any]    =  hidden_dropout_prob
						__a      :	Dict    =  attention_probs_dropout_prob
						__a      :	Optional[Any]    =  num_labels
						__a      :	Tuple    =  image_size
						__a      :	Optional[Any]    =  layer_depths
						__a      :	Dict    =  embed_dims




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





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

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

						__a      :	List[str]    =  self.get_config()

						return config, pixel_values, labels




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						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=__a						,    layer_scale_init_value=1E-5						,    )




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	int    =  SwiftFormerModel(config=__a							)
						model.to(__a							)
						model.eval()
						__a      :	Optional[Any]    =  model(__a							)
						self.parent.assertEqual(result.last_hidden_state.shape						,    (self.batch_size, self.embed_dims[-1], 7, 7)							)




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Tuple    =  self.num_labels
						__a      :	Optional[int]    =  SwiftFormerForImageClassification(__a							)
						model.to(__a							)
						model.eval()
						__a      :	int    =  model(__a						,    labels=__a							)
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_labels)							)

						__a      :	int    =  SwiftFormerForImageClassification(__a							)
						model.to(__a							)
						model.eval()

						__a      :	Union[str, Any]    =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)
						__a      :	Optional[Any]    =  model(__a							)
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_labels)							)





	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						(__a)      :	Tuple    =  self.prepare_config_and_inputs()
						__a      :	Dict    =  {'pixel_values': pixel_values}
						return config, inputs_dict





@require_torch
class 							__UpperCamelCase    (		lowerCAmelCase_		,				lowerCAmelCase_		,				unittest.TestCase ):
	A_           =   (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
	A_           =   (
	    {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification}
	    if is_torch_available()
	    else {}
	)

	A_           =   False
	A_           =   False
	A_           =   False
	A_           =   False
	A_           =   False




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[Any]    =  SwiftFormerModelTester(self							)
						__a      :	Dict    =  ConfigTester(
						    self						,    config_class=__a						,    has_text_modality=__a						,    hidden_size=37						,    num_attention_heads=12						,    num_hidden_layers=12						,    )




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						self.config_tester.run_common_tests()




	@unittest.skip(reason='SwiftFormer does not use inputs_embeds'							)
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						pass




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[int]    =  self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											__a      :	Optional[Any]    =  model_class(__a							)
											__a      :	int    =  model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(__a						,    nn.Linear							)							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Dict    =  self.model_tester.prepare_config_and_inputs_for_common()

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

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




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Tuple    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	int    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__a							)




	@slow
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__a      :	int    =  SwiftFormerModel.from_pretrained(__a							)
											self.assertIsNotNone(__a							)




	@unittest.skip(reason='SwiftFormer does not output attentions'							)
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						pass




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





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

											with torch.no_grad():
																__a      :	Union[str, Any]    =  model(**self._prepare_for_class(__a						,    __a							)							)

											__a      :	Any    =  outputs.hidden_states

											__a      :	Tuple    =  8
											self.assertEqual(len(__a							)						,    __a							)  # 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(__a							)							):
																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),
																        ]							)						,    )

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

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

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											__a      :	Union[str, Any]    =  True

											check_hidden_states_output(__a						,    __a						,    __a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						def _config_zero_init(__a							):
											__a      :	List[Any]    =  copy.deepcopy(__a							)
											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(__a						,    __a						,    1E-1_0							)
																if isinstance(getattr(__a						,    __a						,    __a							)						,    __a							):
																					__a      :	List[Any]    =  _config_zero_init(getattr(__a						,    __a							)							)
																					setattr(__a						,    __a						,    __a							)
											return configs_no_init

						__a      :	Dict    =  self.model_tester.prepare_config_and_inputs_for_common()

						__a      :	Optional[Any]    =  _config_zero_init(__a							)
						for model_class in self.all_model_classes:
											__a      :	Tuple    =  model_class(config=__a							)
											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 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						pass






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





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




	@cached_property
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						return ViTImageProcessor.from_pretrained('MBZUAI/swiftformer-xs'							) if is_vision_available() else None





	@slow
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Tuple    =  SwiftFormerForImageClassification.from_pretrained('MBZUAI/swiftformer-xs'							).to(__a							)

						__a      :	Optional[Any]    =  self.default_image_processor
						__a      :	Tuple    =  prepare_img()
						__a      :	int    =  image_processor(images=__a						,    return_tensors='pt'							).to(__a							)

						# forward pass
						with torch.no_grad():
											__a      :	Optional[Any]    =  model(**__a							)

						# verify the logits
						__a      :	Tuple    =  torch.Size((1, 1000)							)
						self.assertEqual(outputs.logits.shape						,    __a							)

						__a      :	int    =  torch.tensor([[-2.1_7_0_3E0_0, 2.1_1_0_7E0_0, -2.0_8_1_1E0_0]]							).to(__a							)
						self.assertTrue(torch.allclose(outputs.logits[0, :3]						,    __a						,    atol=1E-4							)							)







'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__lowercase     :						Tuple	=     {
    'configuration_distilbert': [
        'DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'DistilBertConfig',
        'DistilBertOnnxConfig',
    ],
    'tokenization_distilbert': ['DistilBertTokenizer'],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowercase     :						str	=     ['DistilBertTokenizerFast']

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowercase     :						Any	=     [
	    'DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'DistilBertForMaskedLM',
	    'DistilBertForMultipleChoice',
	    'DistilBertForQuestionAnswering',
	    'DistilBertForSequenceClassification',
	    'DistilBertForTokenClassification',
	    'DistilBertModel',
	    'DistilBertPreTrainedModel',
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowercase     :						List[str]	=     [
	    'TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'TFDistilBertForMaskedLM',
	    'TFDistilBertForMultipleChoice',
	    'TFDistilBertForQuestionAnswering',
	    'TFDistilBertForSequenceClassification',
	    'TFDistilBertForTokenClassification',
	    'TFDistilBertMainLayer',
	    'TFDistilBertModel',
	    'TFDistilBertPreTrainedModel',
	]

try:
	if not is_flax_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowercase     :						List[str]	=     [
	    'FlaxDistilBertForMaskedLM',
	    'FlaxDistilBertForMultipleChoice',
	    'FlaxDistilBertForQuestionAnswering',
	    'FlaxDistilBertForSequenceClassification',
	    'FlaxDistilBertForTokenClassification',
	    'FlaxDistilBertModel',
	    'FlaxDistilBertPreTrainedModel',
	]


if TYPE_CHECKING:
	from .configuration_distilbert import (
	    DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    DistilBertConfig,
	    DistilBertOnnxConfig,
	)
	from .tokenization_distilbert import DistilBertTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_distilbert_fast import DistilBertTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_distilbert import (
		    DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    DistilBertForMaskedLM,
		    DistilBertForMultipleChoice,
		    DistilBertForQuestionAnswering,
		    DistilBertForSequenceClassification,
		    DistilBertForTokenClassification,
		    DistilBertModel,
		    DistilBertPreTrainedModel,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_distilbert import (
		    TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFDistilBertForMaskedLM,
		    TFDistilBertForMultipleChoice,
		    TFDistilBertForQuestionAnswering,
		    TFDistilBertForSequenceClassification,
		    TFDistilBertForTokenClassification,
		    TFDistilBertMainLayer,
		    TFDistilBertModel,
		    TFDistilBertPreTrainedModel,
		)

	try:
		if not is_flax_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_flax_distilbert import (
		    FlaxDistilBertForMaskedLM,
		    FlaxDistilBertForMultipleChoice,
		    FlaxDistilBertForQuestionAnswering,
		    FlaxDistilBertForSequenceClassification,
		    FlaxDistilBertForTokenClassification,
		    FlaxDistilBertModel,
		    FlaxDistilBertPreTrainedModel,
		)

else:
	import sys

	__lowercase     :						int	=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



