






from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import SwinConfig
from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers.models.swin.modeling_tf_swin import (
					    TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TFSwinForImageClassification,
					    TFSwinForMaskedImageModeling,
					    TFSwinModel,
					)


if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor





class        a__      :






		def __init__(      self							, lowercase__							, lowercase__=13							, lowercase__=32							, lowercase__=2							, lowercase__=3							, lowercase__=16							, lowercase__=[1, 2, 1]							, lowercase__=[2, 2, 4]							, lowercase__=2							, lowercase__=2.0							, lowercase__=True							, lowercase__=0.0							, lowercase__=0.0							, lowercase__=0.1							, lowercase__="gelu"							, lowercase__=False							, lowercase__=True							, lowercase__=0.02							, lowercase__=1e-5							, lowercase__=True							, lowercase__=None							, lowercase__=True							, lowercase__=10							, lowercase__=8							, )				->				None:
				__A    =							parent
				__A    =							batch_size
				__A    =							image_size
				__A    =							patch_size
				__A    =							num_channels
				__A    =							embed_dim
				__A    =							depths
				__A    =							num_heads
				__A    =							window_size
				__A    =							mlp_ratio
				__A    =							qkv_bias
				__A    =							hidden_dropout_prob
				__A    =							attention_probs_dropout_prob
				__A    =							drop_path_rate
				__A    =							hidden_act
				__A    =							use_absolute_embeddings
				__A    =							patch_norm
				__A    =							layer_norm_eps
				__A    =							initializer_range
				__A    =							is_training
				__A    =							scope
				__A    =							use_labels
				__A    =							type_sequence_label_size
				__A    =							encoder_stride






		def 					_lowerCamelCase					(      self						)				->				Union[str, Any]:
				__A    =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

				__A    =							None
				if self.use_labels:
						__A    =							ids_tensor([self.batch_size]							, self.type_sequence_label_size						)

				__A    =							self.get_config()

				return config, pixel_values, labels






		def 					_lowerCamelCase					(      self						)				->				Optional[Any]:
				return SwinConfig(
				    image_size=self.image_size							, patch_size=self.patch_size							, num_channels=self.num_channels							, embed_dim=self.embed_dim							, depths=self.depths							, num_heads=self.num_heads							, window_size=self.window_size							, mlp_ratio=self.mlp_ratio							, qkv_bias=self.qkv_bias							, hidden_dropout_prob=self.hidden_dropout_prob							, attention_probs_dropout_prob=self.attention_probs_dropout_prob							, drop_path_rate=self.drop_path_rate							, hidden_act=self.hidden_act							, use_absolute_embeddings=self.use_absolute_embeddings							, path_norm=self.patch_norm							, layer_norm_eps=self.layer_norm_eps							, initializer_range=self.initializer_range							, encoder_stride=self.encoder_stride							, )






		def 					_lowerCamelCase					(      self							, lowercase__							, lowercase__							, lowercase__						)				->				Tuple:
				__A    =							TFSwinModel(config=lowercase__						)
				__A    =							model(lowercase__						)

				__A    =							((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
				__A    =							int(config.embed_dim * 2 ** (len(config.depths						) - 1)						)

				self.parent.assertEqual(result.last_hidden_state.shape							, (self.batch_size, expected_seq_len, expected_dim)						)






		def 					_lowerCamelCase					(      self							, lowercase__							, lowercase__							, lowercase__						)				->				List[str]:
				__A    =							TFSwinForMaskedImageModeling(config=lowercase__						)
				__A    =							model(lowercase__						)
				self.parent.assertEqual(
				    result.logits.shape							, (self.batch_size, self.num_channels, self.image_size, self.image_size)						)

				# test greyscale images
				__A    =							1
				__A    =							TFSwinForMaskedImageModeling(lowercase__						)

				__A    =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
				__A    =							model(lowercase__						)
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, 1, self.image_size, self.image_size)						)






		def 					_lowerCamelCase					(      self							, lowercase__							, lowercase__							, lowercase__						)				->				List[str]:
				__A    =							self.type_sequence_label_size
				__A    =							TFSwinForImageClassification(lowercase__						)
				__A    =							model(lowercase__							, labels=lowercase__						)
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.type_sequence_label_size)						)

				# test greyscale images
				__A    =							1
				__A    =							TFSwinForImageClassification(lowercase__						)
				__A    =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
				__A    =							model(lowercase__						)
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.type_sequence_label_size)						)






		def 					_lowerCamelCase					(      self						)				->				List[Any]:
				__A    =							self.prepare_config_and_inputs()
				__A ,   __A ,   __A    =							config_and_inputs
				__A    =							{"pixel_values": pixel_values}
				return config, inputs_dict





@require_tf
class        a__      (	lowerCAmelCase__	,       lowerCAmelCase__	,       unittest.TestCase		):
		UpperCAmelCase_							:       Tuple 					= (
		    (
		        TFSwinModel,
		        TFSwinForImageClassification,
		        TFSwinForMaskedImageModeling,
		    )
		    if is_tf_available()
		    else ()
		)
		UpperCAmelCase_							:       Tuple 					= (
		    {'feature-extraction': TFSwinModel, 'image-classification': TFSwinForImageClassification}
		    if is_tf_available()
		    else {}
		)

		UpperCAmelCase_							:       int 					= False
		UpperCAmelCase_							:       str 					= False
		UpperCAmelCase_							:       Any 					= False
		UpperCAmelCase_							:       Optional[Any] 					= False






		def 					_lowerCamelCase					(      self						)				->				Tuple:
				__A    =							TFSwinModelTester(self						)
				__A    =							ConfigTester(self							, config_class=lowercase__							, embed_dim=37						)






		def 					_lowerCamelCase					(      self						)				->				Optional[int]:
				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 					_lowerCamelCase					(      self						)				->				Any:
				return






		def 					_lowerCamelCase					(      self						)				->				List[Any]:
				__A    =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowercase__						)






		def 					_lowerCamelCase					(      self						)				->				List[Any]:
				__A    =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_image_modeling(*lowercase__						)






		def 					_lowerCamelCase					(      self						)				->				Tuple:
				__A    =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*lowercase__						)






		@unittest.skip(reason="Swin does not use inputs_embeds"						)
		def 					_lowerCamelCase					(      self						)				->				Union[str, Any]:
				pass






		def 					_lowerCamelCase					(      self						)				->				List[Any]:
				__A ,   __A    =							self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
						__A    =							model_class(lowercase__						)
						self.assertIsInstance(model.get_input_embeddings()							, tf.keras.layers.Layer						)
						__A    =							model.get_output_embeddings()
						self.assertTrue(x is None or isinstance(lowercase__							, tf.keras.layers.Dense						)						)






		def 					_lowerCamelCase					(      self						)				->				Any:
				__A ,   __A    =							self.model_tester.prepare_config_and_inputs_for_common()

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

						__A    =							["pixel_values"]
						self.assertListEqual(arg_names[:1]							, lowercase__						)






		def 					_lowerCamelCase					(      self						)				->				Any:
				__A ,   __A    =							self.model_tester.prepare_config_and_inputs_for_common()
				__A    =							True

				for model_class in self.all_model_classes:
						__A    =							True
						__A    =							False
						__A    =							True
						__A    =							model_class(lowercase__						)
						__A    =							model(**self._prepare_for_class(lowercase__							, lowercase__						)						)
						__A    =							outputs.attentions
						__A    =							len(self.model_tester.depths						)
						self.assertEqual(len(lowercase__						)							, lowercase__						)

						# check that output_attentions also work using config
						del inputs_dict["output_attentions"]
						__A    =							True
						__A    =							config.window_size**2
						__A    =							model_class(lowercase__						)
						__A    =							model(**self._prepare_for_class(lowercase__							, lowercase__						)						)
						__A    =							outputs.attentions
						self.assertEqual(len(lowercase__						)							, lowercase__						)

						self.assertListEqual(
						    list(attentions[0].shape[-3:]						)							, [self.model_tester.num_heads[0], window_size_squared, window_size_squared]							, )
						__A    =							len(lowercase__						)

						# Check attention is always last and order is fine
						__A    =							True
						__A    =							True
						__A    =							model_class(lowercase__						)
						__A    =							model(**self._prepare_for_class(lowercase__							, lowercase__						)						)

						if hasattr(self.model_tester							, "num_hidden_states_types"						):
								__A    =							self.model_tester.num_hidden_states_types
						else:
								# also another +1 for reshaped_hidden_states
								__A    =							2
						self.assertEqual(out_len + added_hidden_states							, len(lowercase__						)						)

						__A    =							outputs.attentions

						self.assertEqual(len(lowercase__						)							, lowercase__						)

						self.assertListEqual(
						    list(self_attentions[0].shape[-3:]						)							, [self.model_tester.num_heads[0], window_size_squared, window_size_squared]							, )






		def 					_lowerCamelCase					(      self							, lowercase__							, lowercase__							, lowercase__							, lowercase__						)				->				Optional[int]:
				__A    =							model_class(lowercase__						)
				__A    =							model(**self._prepare_for_class(lowercase__							, lowercase__						)						)
				__A    =							outputs.hidden_states

				__A    =							getattr(
				    self.model_tester							, "expected_num_hidden_layers"							, len(self.model_tester.depths						) + 1						)
				self.assertEqual(len(lowercase__						)							, lowercase__						)

				# Swin has a different seq_length
				__A    =							to_atuple(config.patch_size						)

				__A    =							(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

				self.assertListEqual(
				    list(hidden_states[0].shape[-2:]						)							, [num_patches, self.model_tester.embed_dim]							, )

				__A    =							outputs.reshaped_hidden_states
				self.assertEqual(len(lowercase__						)							, lowercase__						)

				__A ,   __A ,   __A ,   __A    =							reshaped_hidden_states[0].shape

				__A    =							tf.reshape(reshaped_hidden_states[0]							, (batch_size, num_channels, height * width)						)
				__A    =							tf.transpose(lowercase__							, (0, 2, 1)						)

				self.assertListEqual(
				    list(reshaped_hidden_states.shape[-2:]						)							, [num_patches, self.model_tester.embed_dim]							, )






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

				__A    =							to_atuple(self.model_tester.image_size						)

				for model_class in self.all_model_classes:
						__A    =							True
						self.check_hidden_states_output(lowercase__							, lowercase__							, lowercase__							, lowercase__						)

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

						self.check_hidden_states_output(lowercase__							, lowercase__							, lowercase__							, lowercase__						)






		def 					_lowerCamelCase					(      self						)				->				Any:
				__A ,   __A    =							self.model_tester.prepare_config_and_inputs_for_common()
				__A    =							3

				__A    =							to_atuple(self.model_tester.image_size						)
				__A    =							to_atuple(config.patch_size						)

				__A    =							image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
				__A    =							image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

				for model_class in self.all_model_classes:
						__A    =							True
						self.check_hidden_states_output(lowercase__							, lowercase__							, lowercase__							, (padded_height, padded_width)						)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						__A    =							True
						self.check_hidden_states_output(lowercase__							, lowercase__							, lowercase__							, (padded_height, padded_width)						)






		@slow
		def 					_lowerCamelCase					(      self						)				->				str:
				for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__A    =							TFSwinModel.from_pretrained(lowercase__						)
						self.assertIsNotNone(lowercase__						)





@require_vision
@require_tf
class        a__      (	unittest.TestCase		):






		@cached_property
		def 					_lowerCamelCase					(      self						)				->				int:
				return (
				    AutoImageProcessor.from_pretrained("microsoft/swin-tiny-patch4-window7-224"						)
				    if is_vision_available()
				    else None
				)






		@slow
		def 					_lowerCamelCase					(      self						)				->				List[Any]:
				__A    =							TFSwinForImageClassification.from_pretrained("microsoft/swin-tiny-patch4-window7-224"						)
				__A    =							self.default_image_processor

				__A    =							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
				__A    =							image_processor(images=lowercase__							, return_tensors="tf"						)

				# forward pass
				__A    =							model(lowercase__						)

				# verify the logits
				__A    =							tf.TensorShape((1, 1000)						)
				self.assertEqual(outputs.logits.shape							, lowercase__						)
				__A    =							tf.constant([-0.0948, -0.6454, -0.0921]						)
				self.assertTrue(np.allclose(outputs.logits[0, :3]							, lowercase__							, atol=1e-4						)						)






import unittest

from transformers import BlenderbotTokenizer, BlenderbotTokenizerFast
from transformers.utils import cached_property


class 	a__       (       unittest.TestCase       ):


					"""simple docstring"""



					@cached_property
					def 				__UpperCAmelCase		(		self     :Optional[Any]    ):
										return BlenderbotTokenizer.from_pretrained('facebook/blenderbot-3B'    )

					@cached_property
					def 				__UpperCAmelCase		(		self     :Any    ):
										return BlenderbotTokenizerFast.from_pretrained('facebook/blenderbot-3B'    )

					def 				__UpperCAmelCase		(		self     :str    ):
										lowercase						   =       self.tokenizer_ab
										lowercase						   =       ' I am a small frog.'
										lowercase						   =       tok([src_text]      ,  padding=lowercase__      ,  truncation=lowercase__    )['input_ids']
										lowercase						   =       tok.batch_decode(lowercase__      ,  skip_special_tokens=lowercase__      ,  clean_up_tokenization_spaces=lowercase__    )[0]
										assert src_text == decoded

					def 				__UpperCAmelCase		(		self     :Union[str, Any]    ):
										lowercase						   =       self.rust_tokenizer_ab
										lowercase						   =       ' I am a small frog.'
										lowercase						   =       tok([src_text]      ,  padding=lowercase__      ,  truncation=lowercase__    )['input_ids']
										lowercase						   =       tok.batch_decode(lowercase__      ,  skip_special_tokens=lowercase__      ,  clean_up_tokenization_spaces=lowercase__    )[0]
										assert src_text == decoded

					def 				__UpperCAmelCase		(		self     :Any    ):
										assert self.tokenizer_ab.add_prefix_space
										assert self.tokenizer_ab([' Sam', 'Sam']    ).input_ids == [[5502, 2], [5502, 2]]



					def 				__UpperCAmelCase		(		self     :Union[str, Any]    ):
										assert self.rust_tokenizer_ab.add_prefix_space
										assert self.rust_tokenizer_ab([' Sam', 'Sam']    ).input_ids == [[5502, 2], [5502, 2]]






