






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

						"""simple docstring"""



						def __init__(self:					Optional[int] ,					__UpperCAmelCase:					List[str] ,					__UpperCAmelCase:					Union[str, Any]=13 ,					__UpperCAmelCase:					Tuple=3 ,					__UpperCAmelCase:					str=True ,					__UpperCAmelCase:					List[str]=True ,					__UpperCAmelCase:					List[str]=0.1 ,					__UpperCAmelCase:					Optional[int]=0.1 ,					__UpperCAmelCase:					Tuple=224 ,					__UpperCAmelCase:					List[Any]=1000 ,					__UpperCAmelCase:					List[Any]=[3, 3, 6, 4] ,					__UpperCAmelCase:					Tuple=[48, 56, 112, 220] ,					)    ->     Tuple:




											'''simple docstring'''



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



						def        UpperCAmelCase__					(self:					str   )    ->     str:




											'''simple docstring'''



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

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

											__a					:						Tuple      =					self.get_config()

											return config, pixel_values, labels



						def        UpperCAmelCase__					(self:					Any   )    ->     List[Any]:




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



						def        UpperCAmelCase__					(self:					Dict ,					__UpperCAmelCase:					List[str] ,					__UpperCAmelCase:					List[str] ,					__UpperCAmelCase:					int   )    ->     Optional[int]:




											'''simple docstring'''



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



						def        UpperCAmelCase__					(self:					int ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					Union[str, Any] ,					__UpperCAmelCase:					List[str]   )    ->     Optional[Any]:




											'''simple docstring'''



											__a					:						List[Any]      =					self.num_labels
											__a					:						Tuple      =					SwiftFormerForImageClassification(__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						List[str]      =					model(__UpperCAmelCase ,					labels=__UpperCAmelCase   )
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)   )

											__a					:						List[str]      =					SwiftFormerForImageClassification(__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()

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



						def        UpperCAmelCase__					(self:					Dict   )    ->     List[Any]:




											'''simple docstring'''



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




@require_torch
class 							snake_case_       (    __UpperCamelCase		,							__UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
						snake_case__        =      (
						    {"""feature-extraction""": SwiftFormerModel, """image-classification""": SwiftFormerForImageClassification}
						    if is_torch_available()
						    else {}
						)

						snake_case__        =      False
						snake_case__        =      False
						snake_case__        =      False
						snake_case__        =      False
						snake_case__        =      False



						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     List[str]:




											'''simple docstring'''



											__a					:						Any      =					SwiftFormerModelTester(self   )
											__a					:						List[str]      =					ConfigTester(
											    self ,					config_class=__UpperCAmelCase ,					has_text_modality=__UpperCAmelCase ,					hidden_size=37 ,					num_attention_heads=12 ,					num_hidden_layers=12 ,					)



						def        UpperCAmelCase__					(self:					Optional[int]   )    ->     Tuple:




											'''simple docstring'''



											self.config_tester.run_common_tests()



						@unittest.skip(reason="SwiftFormer does not use inputs_embeds"   )
						def        UpperCAmelCase__					(self:					List[str]   )    ->     int:




											'''simple docstring'''



											pass



						def        UpperCAmelCase__					(self:					List[str]   )    ->     Tuple:




											'''simple docstring'''



											__a      ,			__a					:						Union[str, Any]      =					self.model_tester.prepare_config_and_inputs_for_common()

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



						def        UpperCAmelCase__					(self:					Any   )    ->     Dict:




											'''simple docstring'''



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

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

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



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Any:




											'''simple docstring'''



											__a					:						Optional[int]      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Dict:




											'''simple docstring'''



											__a					:						Optional[Any]      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase   )



						@slow
						def        UpperCAmelCase__					(self:					Any   )    ->     List[Any]:




											'''simple docstring'''



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



						@unittest.skip(reason="SwiftFormer does not output attentions"   )
						def        UpperCAmelCase__					(self:					Tuple   )    ->     Union[str, Any]:




											'''simple docstring'''



											pass



						def        UpperCAmelCase__					(self:					Dict   )    ->     List[Any]:




											'''simple docstring'''



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

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

																__a					:						Union[str, Any]      =					outputs.hidden_states

																__a					:						Tuple      =					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),
																					        ]   ) ,					)

											__a      ,			__a					:						int      =					self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																__a					:						List[str]      =					True
																check_hidden_states_output(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase   )

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

																check_hidden_states_output(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Optional[Any]:




											'''simple docstring'''



											def _config_zero_init(__UpperCAmelCase:					Any   ):
																__a					:						Union[str, Any]      =					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-10   )
																					if isinstance(getattr(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase   ) ,					__UpperCAmelCase   ):
																										__a					:						List[str]      =					_config_zero_init(getattr(__UpperCAmelCase ,					__UpperCAmelCase   )   )
																										setattr(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase   )
																return configs_no_init

											__a      ,			__a					:						Any      =					self.model_tester.prepare_config_and_inputs_for_common()

											__a					:						Tuple      =					_config_zero_init(__UpperCAmelCase   )
											for model_class in self.all_model_classes:
																__a					:						Optional[int]      =					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        UpperCAmelCase__					(self:					Dict   )    ->     Union[str, Any]:




											'''simple docstring'''



											pass







def     a_   ()				->						str:



					"""simple docstring"""
					__a					:						Any      =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
					return image




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

						"""simple docstring"""



						@cached_property
						def        UpperCAmelCase__					(self:					List[Any]   )    ->     int:




											'''simple docstring'''



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



						@slow
						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Optional[int]:




											'''simple docstring'''



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

											__a					:						Optional[int]      =					self.default_image_processor
											__a					:						Optional[int]      =					prepare_img()
											__a					:						Optional[int]      =					image_processor(images=__UpperCAmelCase ,					return_tensors="pt"   ).to(__UpperCAmelCase   )

											# forward pass
											with torch.no_grad():
																__a					:						List[str]      =					model(**__UpperCAmelCase   )

											# verify the logits
											__a					:						List[Any]      =					torch.Size((1, 1000)   )
											self.assertEqual(outputs.logits.shape ,					__UpperCAmelCase   )

											__a					:						List[str]      =					torch.tensor([[-2.1703E00, 2.1107E00, -2.0811E00]]   ).to(__UpperCAmelCase   )
											self.assertTrue(torch.allclose(outputs.logits[0, :3] ,					__UpperCAmelCase ,					atol=1E-4   )   )













# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import sys

import transformers


UpperCAmelCase__      			=						'''3'''

print('''Python version:''', sys.version)
print('''transformers version:''', transformers.__version__)

try:
			import torch

			print('''Torch version:''', torch.__version__)
			print('''Cuda available:''', torch.cuda.is_available())
			print('''Cuda version:''', torch.version.cuda)
			print('''CuDNN version:''', torch.backends.cudnn.version())
			print('''Number of GPUs available:''', torch.cuda.device_count())
			print('''NCCL version:''', torch.cuda.nccl.version())
except ImportError:
			print('''Torch version:''', None)

try:
			import deepspeed

			print('''DeepSpeed version:''', deepspeed.__version__)
except ImportError:
			print('''DeepSpeed version:''', None)

try:
			import tensorflow as tf

			print('''TensorFlow version:''', tf.__version__)
			print('''TF GPUs available:''', bool(tf.config.list_physical_devices('''GPU''')))
			print('''Number of TF GPUs available:''', len(tf.config.list_physical_devices('''GPU''')))
except ImportError:
			print('''TensorFlow version:''', None)





