





import unittest

import numpy as np
import timeout_decorator  # noqa

from transformers import OPTConfig, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor


if is_flax_available():
						import os

						# The slow tests are often failing with OOM error on GPU
						# This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed
						# but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html
						_SCREAMING_SNAKE_CASE   						=		"""platform"""

						import jax
						import jax.numpy as jnp

						from transformers import FlaxOPTForCausalLM, FlaxOPTModel, GPTaTokenizer


def 				SCREAMING_SNAKE_CASE__    (     __a ,			__a ,			__a=None ,			__a=None ):
	if attention_mask is None:
		snake_case_		:      str           =						np.where(input_ids != config.pad_token_id ,			1 ,			0 )
	return {
	    "input_ids": input_ids,
	    "attention_mask": attention_mask,
	}
@require_flax
class SCREAMING_SNAKE_CASE_    :






						def __init__(   self   :   List[str]		, _A   :   Optional[int]		, _A   :   Optional[Any]=13		, _A   :   Optional[Any]=7		, _A   :   List[Any]=True		, _A   :   Any=False		, _A   :   Dict=99		, _A   :   Any=16		, _A   :   Optional[int]=2		, _A   :   Tuple=4		, _A   :   Optional[int]=4		, _A   :   int="gelu"		, _A   :   Tuple=0.1		, _A   :   Optional[Any]=0.1		, _A   :   Optional[Any]=20		, _A   :   int=2		, _A   :   str=1		, _A   :   Any=0		, _A   :   Tuple=16		, _A   :   Optional[int]=16		, _A   :   Optional[Any]=0.0_2		, )				->     List[Any]:







							"""simple docstring"""


							snake_case_		:      List[str]           =						parent
							snake_case_		:      str           =						batch_size
							snake_case_		:      str           =						seq_length
							snake_case_		:      int           =						is_training
							snake_case_		:      Optional[Any]           =						use_labels
							snake_case_		:      Tuple           =						vocab_size
							snake_case_		:      Dict           =						hidden_size
							snake_case_		:      List[str]           =						num_hidden_layers
							snake_case_		:      Optional[int]           =						num_attention_heads
							snake_case_		:      Optional[int]           =						intermediate_size
							snake_case_		:      Optional[Any]           =						hidden_act
							snake_case_		:      List[str]           =						hidden_dropout_prob
							snake_case_		:      Optional[Any]           =						attention_probs_dropout_prob
							snake_case_		:      Tuple           =						max_position_embeddings
							snake_case_		:      int           =						eos_token_id
							snake_case_		:      Optional[Any]           =						pad_token_id
							snake_case_		:      str           =						bos_token_id
							snake_case_		:      Tuple           =						embed_dim
							snake_case_		:      Dict           =						word_embed_proj_dim
							snake_case_		:      Tuple           =						initializer_range
							snake_case_		:      List[str]           =						False






						def UpperCAmelCase_					(   self   :   str						)				->     Dict:







							"""simple docstring"""


							snake_case_		:      List[Any]           =						np.clip(ids_tensor([self.batch_size, self.seq_length - 1]		, self.vocab_size						)		, 3		, self.vocab_size						)
							snake_case_		:      Tuple           =						np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1)		, dtype=np.intaa						))		, -1						)

							snake_case_		:      Optional[Any]           =						OPTConfig(
							    vocab_size=self.vocab_size		, hidden_size=self.hidden_size		, num_hidden_layers=self.num_hidden_layers		, num_attention_heads=self.num_attention_heads		, ffn_dim=self.intermediate_size		, dropout=self.hidden_dropout_prob		, attention_dropout=self.attention_probs_dropout_prob		, max_position_embeddings=self.max_position_embeddings		, eos_token_id=self.eos_token_id		, bos_token_id=self.bos_token_id		, pad_token_id=self.pad_token_id		, embed_dim=self.embed_dim		, is_encoder_decoder=_A		, word_embed_proj_dim=self.word_embed_proj_dim		, initializer_range=self.initializer_range		, use_cache=_A		, )
							snake_case_		:      Dict           =						prepare_opt_inputs_dict(_A		, _A						)
							return config, inputs_dict






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







							"""simple docstring"""


							snake_case_    ,snake_case_		:      Optional[Any]           =						self.prepare_config_and_inputs()
							return config, inputs_dict






						def UpperCAmelCase_					(   self   :   Union[str, Any]		, _A   :   Tuple		, _A   :   Optional[Any]		, _A   :   Dict						)				->     Any:







							"""simple docstring"""


							snake_case_		:      Optional[int]           =						20
							snake_case_		:      Optional[Any]           =						model_class_name(_A						)

							snake_case_		:      Optional[int]           =						inputs_dict['input_ids']
							snake_case_		:      List[str]           =						inputs_dict['attention_mask']

							snake_case_		:      Optional[int]           =						model.init_cache(input_ids.shape[0]		, _A						)
							snake_case_		:      Tuple           =						jnp.ones((input_ids.shape[0], max_length)		, dtype='i4'						)

							snake_case_		:      Dict           =						jnp.broadcast_to(
							    jnp.arange(input_ids.shape[-1] - 1						)[None, :]		, (input_ids.shape[0], input_ids.shape[-1] - 1)		, )
							snake_case_		:      Optional[Any]           =						model(
							    input_ids[:, :-1]		, attention_mask=_A		, past_key_values=_A		, position_ids=_A		, )

							snake_case_		:      str           =						jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]		, dtype='i4'						)
							snake_case_		:      List[str]           =						model(
							    input_ids[:, -1:]		, attention_mask=_A		, past_key_values=outputs_cache.past_key_values		, position_ids=_A		, )

							snake_case_		:      Union[str, Any]           =						model(_A						)

							snake_case_		:      str           =						np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])						)						)
							self.parent.assertTrue(diff < 1E-3		, msg=F"""Max diff is {diff}"""						)






						def UpperCAmelCase_					(   self   :   List[Any]		, _A   :   int		, _A   :   List[str]		, _A   :   Tuple						)				->     List[Any]:







							"""simple docstring"""


							snake_case_		:      Tuple           =						20
							snake_case_		:      int           =						model_class_name(_A						)

							snake_case_    ,snake_case_		:      List[Any]           =						(
							    inputs_dict['input_ids'],
							    inputs_dict['attention_mask'],
							)

							snake_case_		:      List[str]           =						jnp.concatenate(
							    [
							        attention_mask,
							        jnp.zeros((attention_mask.shape[0], max_length - attention_mask.shape[1])						),
							    ]		, axis=-1		, )

							snake_case_		:      Union[str, Any]           =						model.init_cache(input_ids.shape[0]		, _A						)
							snake_case_		:      Union[str, Any]           =						jnp.broadcast_to(
							    jnp.arange(input_ids.shape[-1] - 1						)[None, :]		, (input_ids.shape[0], input_ids.shape[-1] - 1)		, )

							snake_case_		:      Tuple           =						model(
							    input_ids[:, :-1]		, attention_mask=_A		, past_key_values=_A		, position_ids=_A		, )
							snake_case_		:      Tuple           =						jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]		, dtype='i4'						)
							snake_case_		:      Union[str, Any]           =						model(
							    input_ids[:, -1:]		, past_key_values=outputs_cache.past_key_values		, attention_mask=_A		, position_ids=_A		, )

							snake_case_		:      Tuple           =						model(_A		, attention_mask=_A						)

							snake_case_		:      Any           =						np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])						)						)
							self.parent.assertTrue(diff < 1E-3		, msg=F"""Max diff is {diff}"""						)
@require_flax
class SCREAMING_SNAKE_CASE_    (       snake_case_							,					unittest.TestCase							,					snake_case_       ):
						__magic_name__:      List[str]			   =							(FlaxOPTModel, FlaxOPTForCausalLM) if is_flax_available() else ()
						__magic_name__:      List[str]			   =							() if is_flax_available() else ()






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







							"""simple docstring"""


							snake_case_		:      str           =						FlaxOPTModelTester(self						)






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







							"""simple docstring"""


							snake_case_    ,snake_case_		:      Union[str, Any]           =						self.model_tester.prepare_config_and_inputs()
							for model_class in self.all_model_classes:
								self.model_tester.check_use_cache_forward(_A		, _A		, _A						)






						def UpperCAmelCase_					(   self   :   Optional[Any]						)				->     Optional[Any]:







							"""simple docstring"""


							snake_case_    ,snake_case_		:      List[str]           =						self.model_tester.prepare_config_and_inputs()
							for model_class in self.all_model_classes:
								self.model_tester.check_use_cache_forward_with_attn_mask(_A		, _A		, _A						)






						@slow
						def UpperCAmelCase_					(   self   :   int						)				->     Union[str, Any]:







							"""simple docstring"""


							for model_class_name in self.all_model_classes:
								snake_case_		:      Optional[Any]           =						model_class_name.from_pretrained('facebook/opt-125m'						)
								snake_case_		:      Dict           =						np.ones((1, 1)						) * model.config.eos_token_id
								snake_case_		:      Any           =						model(_A						)
								self.assertIsNotNone(_A						)
@require_sentencepiece
@require_flax
class SCREAMING_SNAKE_CASE_    (       unittest.TestCase       ):






						@slow
						def UpperCAmelCase_					(   self   :   Tuple						)				->     Optional[int]:







							"""simple docstring"""


							snake_case_		:      Optional[Any]           =						FlaxOPTModel.from_pretrained('facebook/opt-350m'						)
							snake_case_		:      Union[str, Any]           =						jnp.array([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]						)
							snake_case_		:      Tuple           =						model(input_ids=_A						).last_hidden_state
							snake_case_		:      Tuple           =						(1, 11, 512)
							self.assertEqual(output.shape		, _A						)
							snake_case_		:      Union[str, Any]           =						jnp.array(
							    [[-0.2_8_6_7, -1.9_2_5_6, -0.3_0_6_2], [-1.2_7_1_1, -0.1_3_3_7, -0.1_8_9_7], [0.4_1_0_9, 0.1_1_8_7, -1.3_1_4_2]]						)
							self.assertTrue(jnp.allclose(output[:, :3, :3]		, _A		, atol=4E-2						)						)
@require_flax
@slow
class SCREAMING_SNAKE_CASE_    (       unittest.TestCase       ):






						def UpperCAmelCase_					(   self   :   List[str]						)				->     str:







							"""simple docstring"""


							super().setUp()
							snake_case_		:      Optional[int]           =						'facebook/opt-350m'






						def UpperCAmelCase_					(   self   :   Optional[Any]						)				->     Tuple:







							"""simple docstring"""


							snake_case_		:      List[Any]           =						FlaxOPTForCausalLM.from_pretrained(self.path_model						)
							snake_case_		:      Dict           =						GPTaTokenizer.from_pretrained(self.path_model						)

							snake_case_		:      int           =						[
							    'Today is a beautiful day and I want to',
							    'In the city of',
							    'Paris is the capital of France and',
							    'Computers and mobile phones have taken',
							]
							# verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
							snake_case_		:      Union[str, Any]           =						tokenizer(_A		, return_tensors='jax'		, padding=_A		, add_special_tokens=_A						)
							snake_case_		:      List[str]           =						model(inputs.input_ids		, attention_mask=inputs.attention_mask						)[0].mean(axis=-1						)
							snake_case_		:      Union[str, Any]           =						jnp.array(
							    [
							        [1.3_8_5_1, -1_3.8_9_2_3, -1_0.5_2_2_9, -1_0.7_5_3_3, -0.2_3_0_9, -1_0.2_3_8_4, -0.5_3_6_5, -9.0_9_4_7, -5.1_6_7_0],
							        [-4.7_0_7_3, -1_0.6_2_7_6, -3.9_4_1_5, -2_1.5_2_4_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2],
							        [0.6_2_4_7, -3.4_2_2_9, -8.9_1_7_9, -1.4_2_9_7, -1_4.1_6_5_0, 1.4_1_4_6, -9.0_2_1_8, -0.2_7_0_3, -0.2_7_0_3],
							        [6.4_7_8_3, -1.9_9_1_3, -1_0.7_9_2_6, -2.3_3_3_6, 1.5_0_9_2, -0.9_9_7_4, -6.8_2_1_3, 1.3_4_7_7, 1.3_4_7_7],
							    ]						)
							self.assertTrue(jnp.allclose(_A		, _A		, atol=4E-2						)						)

							snake_case_		:      Any           =						jax.jit(_A						)
							snake_case_		:      Tuple           =						model(inputs.input_ids		, attention_mask=inputs.attention_mask						)[0].mean(axis=-1						)
							self.assertTrue(jnp.allclose(_A		, _A		, atol=4E-2						)						)
@require_flax
@slow
class SCREAMING_SNAKE_CASE_    (       unittest.TestCase       ):






						@property
						def UpperCAmelCase_					(   self   :   List[str]						)				->     int:







							"""simple docstring"""


							return [
							    "Today is a beautiful day and I want",
							    "In the city of",
							    "Paris is the capital of France and",
							    "Computers and mobile phones have taken",
							]






						def UpperCAmelCase_					(   self   :   str						)				->     Dict:







							"""simple docstring"""


							snake_case_		:      List[str]           =						'facebook/opt-125m'

							snake_case_		:      Tuple           =						[
							    'Today is a beautiful day and I want to',
							    'In the city of New York, the city',
							    'Paris is the capital of France and the capital',
							    'Computers and mobile phones have taken over the',
							]

							snake_case_		:      Optional[int]           =						[]

							snake_case_		:      Tuple           =						FlaxOPTForCausalLM.from_pretrained(_A						)
							snake_case_		:      int           =						GPTaTokenizer.from_pretrained(_A						)

							for prompt in self.prompts:
								snake_case_		:      Any           =						tokenizer(_A		, return_tensors='jax'						).input_ids

								snake_case_		:      List[str]           =						model.generate(_A		, max_length=10						)
								snake_case_		:      str           =						generated_ids[0]

								snake_case_		:      Dict           =						tokenizer.batch_decode(_A		, skip_special_tokens=_A						)
								predicted_outputs += generated_string

							self.assertListEqual(_A		, _A						)






						def UpperCAmelCase_					(   self   :   Union[str, Any]						)				->     int:







							"""simple docstring"""


							snake_case_		:      int           =						'facebook/opt-350m'

							snake_case_		:      Dict           =						[
							    'Today is a beautiful day and I want to',
							    'In the city of San Francisco, the city',
							    'Paris is the capital of France and the capital',
							    'Computers and mobile phones have taken over the',
							]

							snake_case_		:      str           =						[]
							snake_case_		:      List[str]           =						FlaxOPTForCausalLM.from_pretrained(_A						)
							snake_case_		:      Tuple           =						GPTaTokenizer.from_pretrained(_A						)

							for prompt in self.prompts:
								snake_case_		:      Union[str, Any]           =						tokenizer(_A		, return_tensors='jax'						).input_ids

								snake_case_		:      Dict           =						model.generate(_A		, max_length=10						)
								snake_case_		:      str           =						generated_ids[0]

								snake_case_		:      int           =						tokenizer.batch_decode(_A		, skip_special_tokens=_A						)
								predicted_outputs += generated_string

							self.assertListEqual(_A		, _A						)






						def UpperCAmelCase_					(   self   :   int						)				->     Optional[int]:







							"""simple docstring"""


							snake_case_		:      Union[str, Any]           =						'facebook/opt-125m'
							snake_case_		:      Union[str, Any]           =						[
							    'Today is a beautiful day and I want to thank',
							    'In the city of Rome Canaver Canaver Canaver Canaver',
							]
							snake_case_		:      int           =						FlaxOPTForCausalLM.from_pretrained(_A						)
							snake_case_		:      List[Any]           =						GPTaTokenizer.from_pretrained(_A						)
							snake_case_		:      Optional[int]           =						tokenizer(
							    [
							        'Today is a beautiful day and I want to',
							        'In the city of',
							    ]		, return_tensors='jax'		, padding=_A		, )

							snake_case_		:      Any           =						jax.jit(model.generate						)

							snake_case_		:      int           =						jit_generate(inputs['input_ids']		, attention_mask=inputs['attention_mask']						).sequences

							snake_case_		:      Dict           =						tokenizer.batch_decode(_A		, skip_special_tokens=_A						)

							self.assertIsNotNone(_A		, _A						)






						def UpperCAmelCase_					(   self   :   int						)				->     Optional[int]:







							"""simple docstring"""


							snake_case_		:      str           =						'facebook/opt-350m'

							snake_case_		:      Any           =						GPTaTokenizer.from_pretrained(_A						)
							snake_case_		:      List[str]           =						FlaxOPTForCausalLM.from_pretrained(_A						)

							snake_case_		:      int           =						'left'

							# use different length sentences to test batching
							snake_case_		:      str           =						[
							    'Hello, my dog is a little',
							    'Today, I',
							]

							snake_case_		:      Optional[int]           =						tokenizer(_A		, return_tensors='jax'		, padding=_A						)
							snake_case_		:      Dict           =						inputs['input_ids']

							snake_case_		:      List[str]           =						model.generate(input_ids=_A		, attention_mask=inputs['attention_mask']		, trace=_A						)

							snake_case_		:      Optional[int]           =						tokenizer(sentences[0]		, return_tensors='jax'						).input_ids
							snake_case_		:      Optional[int]           =						model.generate(input_ids=_A						)

							snake_case_		:      Tuple           =						inputs_non_padded.shape[-1] - inputs['attention_mask'][-1].sum()
							snake_case_		:      List[str]           =						tokenizer(sentences[1]		, return_tensors='jax'						).input_ids
							snake_case_		:      Optional[int]           =						model.generate(input_ids=_A		, max_length=model.config.max_length - num_paddings						)

							snake_case_		:      int           =						tokenizer.batch_decode(outputs[0]		, skip_special_tokens=_A						)
							snake_case_		:      Union[str, Any]           =						tokenizer.decode(output_non_padded[0][0]		, skip_special_tokens=_A						)
							snake_case_		:      Dict           =						tokenizer.decode(output_padded[0][0]		, skip_special_tokens=_A						)

							snake_case_		:      Union[str, Any]           =						[
							    'Hello, my dog is a little bit of a dork.\nI\'m a little bit',
							    'Today, I was in the middle of a conversation with a friend about the',
							]
							self.assertListEqual(_A		, _A						)
							self.assertListEqual(_A		, [non_padded_sentence, padded_sentence]						)




"""simple docstring"""





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 :

			"""simple docstring"""

			def __init__(				self  :       Any    ,	lowercase_  :       Union[str, Any]    ,	lowercase_  :       Optional[Any]=13    ,	lowercase_  :       List[str]=32    ,	lowercase_  :       int=2    ,	lowercase_  :       List[Any]=3    ,	lowercase_  :       List[Any]=16    ,	lowercase_  :       Union[str, Any]=[1, 2, 1]    ,	lowercase_  :       Optional[Any]=[2, 2, 4]    ,	lowercase_  :       List[str]=2    ,	lowercase_  :       List[Any]=2.0    ,	lowercase_  :       Optional[Any]=True    ,	lowercase_  :       Any=0.0    ,	lowercase_  :       Optional[int]=0.0    ,	lowercase_  :       str=0.1    ,	lowercase_  :       List[str]="gelu"    ,	lowercase_  :       int=False    ,	lowercase_  :       List[Any]=True    ,	lowercase_  :       Union[str, Any]=0.0_2    ,	lowercase_  :       Union[str, Any]=1e-5    ,	lowercase_  :       Dict=True    ,	lowercase_  :       int=None    ,	lowercase_  :       Dict=True    ,	lowercase_  :       Union[str, Any]=10    ,	lowercase_  :       int=8    ,	):
								UpperCamelCase__		:     Optional[Any]								=parent
								UpperCamelCase__		:     Any								=batch_size
								UpperCamelCase__		:     Tuple								=image_size
								UpperCamelCase__		:     str								=patch_size
								UpperCamelCase__		:     Tuple								=num_channels
								UpperCamelCase__		:     List[Any]								=embed_dim
								UpperCamelCase__		:     Optional[int]								=depths
								UpperCamelCase__		:     Optional[int]								=num_heads
								UpperCamelCase__		:     Dict								=window_size
								UpperCamelCase__		:     Dict								=mlp_ratio
								UpperCamelCase__		:     Optional[Any]								=qkv_bias
								UpperCamelCase__		:     int								=hidden_dropout_prob
								UpperCamelCase__		:     Optional[Any]								=attention_probs_dropout_prob
								UpperCamelCase__		:     Tuple								=drop_path_rate
								UpperCamelCase__		:     Dict								=hidden_act
								UpperCamelCase__		:     Union[str, Any]								=use_absolute_embeddings
								UpperCamelCase__		:     str								=patch_norm
								UpperCamelCase__		:     Optional[int]								=layer_norm_eps
								UpperCamelCase__		:     Optional[int]								=initializer_range
								UpperCamelCase__		:     List[Any]								=is_training
								UpperCamelCase__		:     Optional[int]								=scope
								UpperCamelCase__		:     Any								=use_labels
								UpperCamelCase__		:     Optional[Any]								=type_sequence_label_size
								UpperCamelCase__		:     Optional[int]								=encoder_stride






			def  _lowerCAmelCase      (				self  :       int    ):
								UpperCamelCase__		:     List[Any]								=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

								UpperCamelCase__		:     List[Any]								=None
								if self.use_labels:
													UpperCamelCase__		:     Optional[Any]								=ids_tensor([self.batch_size]    ,	self.type_sequence_label_size    )

								UpperCamelCase__		:     List[Any]								=self.get_config()

								return config, pixel_values, labels






			def  _lowerCAmelCase      (				self  :       Dict    ):
								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  :       List[str]    ,	lowercase_  :       str    ,	lowercase_  :       str    ,	lowercase_  :       Optional[Any]    ):
								UpperCamelCase__		:     Union[str, Any]								=TFSwinModel(config=lowercase_    )
								UpperCamelCase__		:     List[Any]								=model(lowercase_    )

								UpperCamelCase__		:     Optional[int]								=((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths    ) - 1))
								UpperCamelCase__		:     Any								=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  :       Any    ,	lowercase_  :       int    ,	lowercase_  :       str    ,	lowercase_  :       Tuple    ):
								UpperCamelCase__		:     str								=TFSwinForMaskedImageModeling(config=lowercase_    )
								UpperCamelCase__		:     List[Any]								=model(lowercase_    )
								self.parent.assertEqual(
								    result.logits.shape    ,	(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

								# test greyscale images
								UpperCamelCase__		:     Optional[Any]								=1
								UpperCamelCase__		:     Dict								=TFSwinForMaskedImageModeling(lowercase_    )

								UpperCamelCase__		:     Tuple								=floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
								UpperCamelCase__		:     Any								=model(lowercase_    )
								self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, 1, self.image_size, self.image_size)    )






			def  _lowerCAmelCase      (				self  :       Optional[Any]    ,	lowercase_  :       List[Any]    ,	lowercase_  :       List[Any]    ,	lowercase_  :       int    ):
								UpperCamelCase__		:     Any								=self.type_sequence_label_size
								UpperCamelCase__		:     Union[str, Any]								=TFSwinForImageClassification(lowercase_    )
								UpperCamelCase__		:     Optional[int]								=model(lowercase_    ,	labels=lowercase_    )
								self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.type_sequence_label_size)    )

								# test greyscale images
								UpperCamelCase__		:     Dict								=1
								UpperCamelCase__		:     Union[str, Any]								=TFSwinForImageClassification(lowercase_    )
								UpperCamelCase__		:     Tuple								=floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
								UpperCamelCase__		:     List[Any]								=model(lowercase_    )
								self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.type_sequence_label_size)    )






			def  _lowerCAmelCase      (				self  :       Tuple    ):
								UpperCamelCase__		:     int								=self.prepare_config_and_inputs()
								UpperCamelCase__	,      UpperCamelCase__	,      UpperCamelCase__		:     List[Any]								=config_and_inputs
								UpperCamelCase__		:     Tuple								={'''pixel_values''': pixel_values}
								return config, inputs_dict



@require_tf
class      __a (   snake_case__,     snake_case__,     unittest.TestCase     ):

			"""simple docstring"""

			SCREAMING_SNAKE_CASE_      =    (
			    (
			        TFSwinModel,
			        TFSwinForImageClassification,
			        TFSwinForMaskedImageModeling,
			    )
			    if is_tf_available()
			    else ()
			)
			SCREAMING_SNAKE_CASE_      =    (
			    {'feature-extraction': TFSwinModel, 'image-classification': TFSwinForImageClassification}
			    if is_tf_available()
			    else {}
			)

			SCREAMING_SNAKE_CASE_      =    False
			SCREAMING_SNAKE_CASE_      =    False
			SCREAMING_SNAKE_CASE_      =    False
			SCREAMING_SNAKE_CASE_      =    False






			def  _lowerCAmelCase      (				self  :       Dict    ):
								UpperCamelCase__		:     int								=TFSwinModelTester(self    )
								UpperCamelCase__		:     Optional[Any]								=ConfigTester(self    ,	config_class=lowercase_    ,	embed_dim=37    )






			def  _lowerCAmelCase      (				self  :       Dict    ):
								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  :       str    ):
								return






			def  _lowerCAmelCase      (				self  :       Any    ):
								UpperCamelCase__		:     Tuple								=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowercase_    )






			def  _lowerCAmelCase      (				self  :       List[str]    ):
								UpperCamelCase__		:     List[Any]								=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*lowercase_    )






			def  _lowerCAmelCase      (				self  :       List[str]    ):
								UpperCamelCase__		:     Tuple								=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  :       int    ):
								pass






			def  _lowerCAmelCase      (				self  :       List[Any]    ):
								UpperCamelCase__	,      UpperCamelCase__		:     Tuple								=self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													UpperCamelCase__		:     Optional[int]								=model_class(lowercase_    )
													self.assertIsInstance(model.get_input_embeddings()    ,	tf.keras.layers.Layer    )
													UpperCamelCase__		:     List[Any]								=model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowercase_    ,	tf.keras.layers.Dense    )    )






			def  _lowerCAmelCase      (				self  :       int    ):
								UpperCamelCase__	,      UpperCamelCase__		:     Optional[Any]								=self.model_tester.prepare_config_and_inputs_for_common()

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

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






			def  _lowerCAmelCase      (				self  :       Optional[Any]    ):
								UpperCamelCase__	,      UpperCamelCase__		:     int								=self.model_tester.prepare_config_and_inputs_for_common()
								UpperCamelCase__		:     Dict								=True

								for model_class in self.all_model_classes:
													UpperCamelCase__		:     Any								=True
													UpperCamelCase__		:     List[str]								=False
													UpperCamelCase__		:     Union[str, Any]								=True
													UpperCamelCase__		:     int								=model_class(lowercase_    )
													UpperCamelCase__		:     str								=model(**self._prepare_for_class(lowercase_    ,	lowercase_    )    )
													UpperCamelCase__		:     str								=outputs.attentions
													UpperCamelCase__		:     List[str]								=len(self.model_tester.depths    )
													self.assertEqual(len(lowercase_    )    ,	lowercase_    )

													# check that output_attentions also work using config
													del inputs_dict["output_attentions"]
													UpperCamelCase__		:     Tuple								=True
													UpperCamelCase__		:     str								=config.window_size**2
													UpperCamelCase__		:     List[Any]								=model_class(lowercase_    )
													UpperCamelCase__		:     Union[str, Any]								=model(**self._prepare_for_class(lowercase_    ,	lowercase_    )    )
													UpperCamelCase__		:     Union[str, Any]								=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]    ,	)
													UpperCamelCase__		:     Any								=len(lowercase_    )

													# Check attention is always last and order is fine
													UpperCamelCase__		:     List[Any]								=True
													UpperCamelCase__		:     List[str]								=True
													UpperCamelCase__		:     Optional[Any]								=model_class(lowercase_    )
													UpperCamelCase__		:     Optional[int]								=model(**self._prepare_for_class(lowercase_    ,	lowercase_    )    )

													if hasattr(self.model_tester    ,	'''num_hidden_states_types'''    ):
																		UpperCamelCase__		:     str								=self.model_tester.num_hidden_states_types
													else:
																		# also another +1 for reshaped_hidden_states
																		UpperCamelCase__		:     Any								=2
													self.assertEqual(out_len + added_hidden_states    ,	len(lowercase_    )    )

													UpperCamelCase__		:     Any								=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  :       List[str]    ,	lowercase_  :       Union[str, Any]    ,	lowercase_  :       Dict    ,	lowercase_  :       str    ,	lowercase_  :       List[str]    ):
								UpperCamelCase__		:     Any								=model_class(lowercase_    )
								UpperCamelCase__		:     Union[str, Any]								=model(**self._prepare_for_class(lowercase_    ,	lowercase_    )    )
								UpperCamelCase__		:     str								=outputs.hidden_states

								UpperCamelCase__		:     Dict								=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
								UpperCamelCase__		:     List[Any]								=to_atuple(config.patch_size    )

								UpperCamelCase__		:     List[str]								=(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]    ,	)

								UpperCamelCase__		:     Optional[Any]								=outputs.reshaped_hidden_states
								self.assertEqual(len(lowercase_    )    ,	lowercase_    )

								UpperCamelCase__	,      UpperCamelCase__	,      UpperCamelCase__	,      UpperCamelCase__		:     int								=reshaped_hidden_states[0].shape

								UpperCamelCase__		:     Optional[int]								=tf.reshape(reshaped_hidden_states[0]    ,	(batch_size, num_channels, height * width)    )
								UpperCamelCase__		:     Optional[int]								=tf.transpose(lowercase_    ,	(0, 2, 1)    )

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






			def  _lowerCAmelCase      (				self  :       List[str]    ):
								UpperCamelCase__	,      UpperCamelCase__		:     Dict								=self.model_tester.prepare_config_and_inputs_for_common()

								UpperCamelCase__		:     Union[str, Any]								=to_atuple(self.model_tester.image_size    )

								for model_class in self.all_model_classes:
													UpperCamelCase__		:     Union[str, Any]								=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"]
													UpperCamelCase__		:     List[str]								=True

													self.check_hidden_states_output(lowercase_    ,	lowercase_    ,	lowercase_    ,	lowercase_    )






			def  _lowerCAmelCase      (				self  :       List[Any]    ):
								UpperCamelCase__	,      UpperCamelCase__		:     Optional[Any]								=self.model_tester.prepare_config_and_inputs_for_common()
								UpperCamelCase__		:     List[str]								=3

								UpperCamelCase__		:     Optional[int]								=to_atuple(self.model_tester.image_size    )
								UpperCamelCase__		:     str								=to_atuple(config.patch_size    )

								UpperCamelCase__		:     List[Any]								=image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
								UpperCamelCase__		:     int								=image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

								for model_class in self.all_model_classes:
													UpperCamelCase__		:     List[str]								=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"]
													UpperCamelCase__		:     Optional[int]								=True
													self.check_hidden_states_output(lowercase_    ,	lowercase_    ,	lowercase_    ,	(padded_height, padded_width)    )






			@slow
			def  _lowerCAmelCase      (				self  :       List[Any]    ):
								for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													UpperCamelCase__		:     str								=TFSwinModel.from_pretrained(lowercase_    )
													self.assertIsNotNone(lowercase_    )



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

			"""simple docstring"""

			@cached_property
			def  _lowerCAmelCase      (				self  :       Optional[int]    ):
								return (
								    AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''    )
								    if is_vision_available()
								    else None
								)






			@slow
			def  _lowerCAmelCase      (				self  :       Any    ):
								UpperCamelCase__		:     List[str]								=TFSwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''    )
								UpperCamelCase__		:     Optional[Any]								=self.default_image_processor

								UpperCamelCase__		:     str								=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
								UpperCamelCase__		:     int								=image_processor(images=lowercase_    ,	return_tensors='''tf'''    )

								# forward pass
								UpperCamelCase__		:     str								=model(lowercase_    )

								# verify the logits
								UpperCamelCase__		:     Optional[int]								=tf.TensorShape((1, 1000)    )
								self.assertEqual(outputs.logits.shape    ,	lowercase_    )
								UpperCamelCase__		:     Tuple								=tf.constant([-0.0_9_4_8, -0.6_4_5_4, -0.0_9_2_1]    )
								self.assertTrue(np.allclose(outputs.logits[0, :3]    ,	lowercase_    ,	atol=1e-4    )    )


