

"""simple docstring"""





import os
import unittest

from transformers import ElectraTokenizerFast
from transformers.models.electra.tokenization_electra import (
    VOCAB_FILES_NAMES,
    BasicTokenizer,
    ElectraTokenizer,
    WordpieceTokenizer,
    _is_control,
    _is_punctuation,
    _is_whitespace,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english

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

			"""simple docstring"""

			SCREAMING_SNAKE_CASE_      =    ElectraTokenizer
			SCREAMING_SNAKE_CASE_      =    ElectraTokenizerFast
			SCREAMING_SNAKE_CASE_      =    True
			SCREAMING_SNAKE_CASE_      =    True
			SCREAMING_SNAKE_CASE_      =    filter_non_english






			def  _lowerCAmelCase      (				self  :       Tuple    ):
								super().setUp()

								UpperCamelCase__		:     List[str]								=[
								    '''[UNK]''',
								    '''[CLS]''',
								    '''[SEP]''',
								    '''[PAD]''',
								    '''[MASK]''',
								    '''want''',
								    '''##want''',
								    '''##ed''',
								    '''wa''',
								    '''un''',
								    '''runn''',
								    '''##ing''',
								    ''',''',
								    '''low''',
								    '''lowest''',
								]
								UpperCamelCase__		:     Union[str, Any]								=os.path.join(self.tmpdirname    ,	VOCAB_FILES_NAMES['''vocab_file''']    )
								with open(self.vocab_file    ,	'''w'''    ,	encoding='''utf-8'''    ) as vocab_writer:
													vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]    )    )






			def  _lowerCAmelCase      (				self  :       Any    ,	lowercase_  :       Union[str, Any]    ):
								UpperCamelCase__		:     Tuple								='''UNwant\u00E9d,running'''
								UpperCamelCase__		:     Tuple								='''unwanted, running'''
								return input_text, output_text






			def  _lowerCAmelCase      (				self  :       Any    ):
								UpperCamelCase__		:     Any								=self.tokenizer_class(self.vocab_file    )

								UpperCamelCase__		:     List[str]								=tokenizer.tokenize('''UNwant\u00E9d,running'''    )
								self.assertListEqual(lowercase_    ,	['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing''']    )
								self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase_    )    ,	[9, 6, 7, 12, 10, 11]    )






			def  _lowerCAmelCase      (				self  :       str    ):
								if not self.test_rust_tokenizer:
													return

								UpperCamelCase__		:     List[Any]								=self.get_tokenizer()
								UpperCamelCase__		:     Tuple								=self.get_rust_tokenizer()

								UpperCamelCase__		:     Optional[int]								='''UNwant\u00E9d,running'''

								UpperCamelCase__		:     Any								=tokenizer.tokenize(lowercase_    )
								UpperCamelCase__		:     List[str]								=rust_tokenizer.tokenize(lowercase_    )
								self.assertListEqual(lowercase_    ,	lowercase_    )

								UpperCamelCase__		:     List[Any]								=tokenizer.encode(lowercase_    ,	add_special_tokens=lowercase_    )
								UpperCamelCase__		:     Optional[Any]								=rust_tokenizer.encode(lowercase_    ,	add_special_tokens=lowercase_    )
								self.assertListEqual(lowercase_    ,	lowercase_    )

								UpperCamelCase__		:     List[str]								=self.get_rust_tokenizer()
								UpperCamelCase__		:     Tuple								=tokenizer.encode(lowercase_    )
								UpperCamelCase__		:     Tuple								=rust_tokenizer.encode(lowercase_    )
								self.assertListEqual(lowercase_    ,	lowercase_    )

								# With lower casing
								UpperCamelCase__		:     int								=self.get_tokenizer(do_lower_case=lowercase_    )
								UpperCamelCase__		:     Optional[Any]								=self.get_rust_tokenizer(do_lower_case=lowercase_    )

								UpperCamelCase__		:     Optional[Any]								='''UNwant\u00E9d,running'''

								UpperCamelCase__		:     Tuple								=tokenizer.tokenize(lowercase_    )
								UpperCamelCase__		:     List[Any]								=rust_tokenizer.tokenize(lowercase_    )
								self.assertListEqual(lowercase_    ,	lowercase_    )

								UpperCamelCase__		:     List[str]								=tokenizer.encode(lowercase_    ,	add_special_tokens=lowercase_    )
								UpperCamelCase__		:     List[str]								=rust_tokenizer.encode(lowercase_    ,	add_special_tokens=lowercase_    )
								self.assertListEqual(lowercase_    ,	lowercase_    )

								UpperCamelCase__		:     Union[str, Any]								=self.get_rust_tokenizer()
								UpperCamelCase__		:     Optional[Any]								=tokenizer.encode(lowercase_    )
								UpperCamelCase__		:     Union[str, Any]								=rust_tokenizer.encode(lowercase_    )
								self.assertListEqual(lowercase_    ,	lowercase_    )






			def  _lowerCAmelCase      (				self  :       Any    ):
								UpperCamelCase__		:     List[str]								=BasicTokenizer()

								self.assertListEqual(tokenizer.tokenize('''ah\u535A\u63A8zz'''    )    ,	['''ah''', '''\u535A''', '''\u63A8''', '''zz''']    )






			def  _lowerCAmelCase      (				self  :       Optional[int]    ):
								UpperCamelCase__		:     Dict								=BasicTokenizer(do_lower_case=lowercase_    )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHeLLo!how  \n Are yoU?  '''    )    ,	['''hello''', '''!''', '''how''', '''are''', '''you''', '''?''']    )
								self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''    )    ,	['''hello''']    )






			def  _lowerCAmelCase      (				self  :       List[Any]    ):
								UpperCamelCase__		:     List[Any]								=BasicTokenizer(do_lower_case=lowercase_    ,	strip_accents=lowercase_    )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''    )    ,	['''hällo''', '''!''', '''how''', '''are''', '''you''', '''?''']    )
								self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''    )    ,	['''h\u00E9llo''']    )






			def  _lowerCAmelCase      (				self  :       int    ):
								UpperCamelCase__		:     str								=BasicTokenizer(do_lower_case=lowercase_    ,	strip_accents=lowercase_    )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''    )    ,	['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?''']    )
								self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''    )    ,	['''hello''']    )






			def  _lowerCAmelCase      (				self  :       str    ):
								UpperCamelCase__		:     str								=BasicTokenizer(do_lower_case=lowercase_    )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''    )    ,	['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?''']    )
								self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''    )    ,	['''hello''']    )






			def  _lowerCAmelCase      (				self  :       Optional[Any]    ):
								UpperCamelCase__		:     Optional[int]								=BasicTokenizer(do_lower_case=lowercase_    )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHeLLo!how  \n Are yoU?  '''    )    ,	['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''']    )






			def  _lowerCAmelCase      (				self  :       Optional[Any]    ):
								UpperCamelCase__		:     int								=BasicTokenizer(do_lower_case=lowercase_    ,	strip_accents=lowercase_    )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''    )    ,	['''HäLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''']    )






			def  _lowerCAmelCase      (				self  :       int    ):
								UpperCamelCase__		:     Optional[Any]								=BasicTokenizer(do_lower_case=lowercase_    ,	strip_accents=lowercase_    )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''    )    ,	['''HaLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''']    )






			def  _lowerCAmelCase      (				self  :       List[str]    ):
								UpperCamelCase__		:     Dict								=BasicTokenizer(do_lower_case=lowercase_    ,	never_split=['''[UNK]''']    )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHeLLo!how  \n Are yoU? [UNK]'''    )    ,	['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''', '''[UNK]''']    )






			def  _lowerCAmelCase      (				self  :       str    ):
								UpperCamelCase__		:     Dict								=['''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''']

								UpperCamelCase__		:     int								={}
								for i, token in enumerate(lowercase_    ):
													UpperCamelCase__		:     Dict								=i
								UpperCamelCase__		:     Union[str, Any]								=WordpieceTokenizer(vocab=lowercase_    ,	unk_token='''[UNK]'''    )

								self.assertListEqual(tokenizer.tokenize(''''''    )    ,	[]    )

								self.assertListEqual(tokenizer.tokenize('''unwanted running'''    )    ,	['''un''', '''##want''', '''##ed''', '''runn''', '''##ing''']    )

								self.assertListEqual(tokenizer.tokenize('''unwantedX running'''    )    ,	['''[UNK]''', '''runn''', '''##ing''']    )






			def  _lowerCAmelCase      (				self  :       Any    ):
								self.assertTrue(_is_whitespace(''' '''    )    )
								self.assertTrue(_is_whitespace('''\t'''    )    )
								self.assertTrue(_is_whitespace('''\r'''    )    )
								self.assertTrue(_is_whitespace('''\n'''    )    )
								self.assertTrue(_is_whitespace('''\u00A0'''    )    )

								self.assertFalse(_is_whitespace('''A'''    )    )
								self.assertFalse(_is_whitespace('''-'''    )    )






			def  _lowerCAmelCase      (				self  :       str    ):
								self.assertTrue(_is_control('''\u0005'''    )    )

								self.assertFalse(_is_control('''A'''    )    )
								self.assertFalse(_is_control(''' '''    )    )
								self.assertFalse(_is_control('''\t'''    )    )
								self.assertFalse(_is_control('''\r'''    )    )






			def  _lowerCAmelCase      (				self  :       Union[str, Any]    ):
								self.assertTrue(_is_punctuation('''-'''    )    )
								self.assertTrue(_is_punctuation('''$'''    )    )
								self.assertTrue(_is_punctuation('''`'''    )    )
								self.assertTrue(_is_punctuation('''.'''    )    )

								self.assertFalse(_is_punctuation('''A'''    )    )
								self.assertFalse(_is_punctuation(''' '''    )    )






			def  _lowerCAmelCase      (				self  :       List[Any]    ):
								UpperCamelCase__		:     Optional[int]								=self.get_tokenizer()
								UpperCamelCase__		:     Optional[int]								=self.get_rust_tokenizer()

								# Example taken from the issue https://github.com/huggingface/tokenizers/issues/340
								self.assertListEqual([tokenizer.tokenize(lowercase_    ) for t in ['''Test''', '''\xad''', '''test''']]    ,	[['''[UNK]'''], [], ['''[UNK]''']]    )

								self.assertListEqual(
								    [rust_tokenizer.tokenize(lowercase_    ) for t in ['''Test''', '''\xad''', '''test''']]    ,	[['''[UNK]'''], [], ['''[UNK]''']]    )






			@slow
			def  _lowerCAmelCase      (				self  :       List[Any]    ):
								UpperCamelCase__		:     Optional[Any]								=self.tokenizer_class.from_pretrained('''google/electra-base-discriminator'''    )

								UpperCamelCase__		:     Any								=tokenizer.encode('''sequence builders'''    ,	add_special_tokens=lowercase_    )
								UpperCamelCase__		:     Any								=tokenizer.encode('''multi-sequence build'''    ,	add_special_tokens=lowercase_    )

								UpperCamelCase__		:     str								=tokenizer.build_inputs_with_special_tokens(lowercase_    )
								UpperCamelCase__		:     int								=tokenizer.build_inputs_with_special_tokens(lowercase_    ,	lowercase_    )

								assert encoded_sentence == [101] + text + [102]
								assert encoded_pair == [101] + text + [102] + text_a + [102]






			def  _lowerCAmelCase      (				self  :       Any    ):
								for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
													with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''    ):
																		UpperCamelCase__		:     str								=self.rust_tokenizer_class.from_pretrained(lowercase_    ,	**lowercase_    )

																		UpperCamelCase__		:     Tuple								=f'''A, naïve {tokenizer_r.mask_token} AllenNLP sentence.'''
																		UpperCamelCase__		:     Optional[int]								=tokenizer_r.encode_plus(
																		    lowercase_    ,	return_attention_mask=lowercase_    ,	return_token_type_ids=lowercase_    ,	return_offsets_mapping=lowercase_    ,	add_special_tokens=lowercase_    ,	)

																		UpperCamelCase__		:     Any								=tokenizer_r.do_lower_case if hasattr(lowercase_    ,	'''do_lower_case'''    ) else False
																		UpperCamelCase__		:     Dict								=(
																		    [
																		        ((0, 0), tokenizer_r.cls_token),
																		        ((0, 1), '''A'''),
																		        ((1, 2), ''','''),
																		        ((3, 5), '''na'''),
																		        ((5, 6), '''##ï'''),
																		        ((6, 8), '''##ve'''),
																		        ((9, 15), tokenizer_r.mask_token),
																		        ((16, 21), '''Allen'''),
																		        ((21, 23), '''##NL'''),
																		        ((23, 24), '''##P'''),
																		        ((25, 33), '''sentence'''),
																		        ((33, 34), '''.'''),
																		        ((0, 0), tokenizer_r.sep_token),
																		    ]
																		    if not do_lower_case
																		    else [
																		        ((0, 0), tokenizer_r.cls_token),
																		        ((0, 1), '''a'''),
																		        ((1, 2), ''','''),
																		        ((3, 8), '''naive'''),
																		        ((9, 15), tokenizer_r.mask_token),
																		        ((16, 21), '''allen'''),
																		        ((21, 23), '''##nl'''),
																		        ((23, 24), '''##p'''),
																		        ((25, 33), '''sentence'''),
																		        ((33, 34), '''.'''),
																		        ((0, 0), tokenizer_r.sep_token),
																		    ]
																		)

																		self.assertEqual(
																		    [e[1] for e in expected_results]    ,	tokenizer_r.convert_ids_to_tokens(tokens['''input_ids''']    )    )
																		self.assertEqual([e[0] for e in expected_results]    ,	tokens['''offset_mapping''']    )






			def  _lowerCAmelCase      (				self  :       Dict    ):
								UpperCamelCase__		:     int								=['''的''', '''人''', '''有''']
								UpperCamelCase__		:     List[str]								=''''''.join(lowercase_    )
								for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
													with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''    ):
																		UpperCamelCase__		:     Dict								=True
																		UpperCamelCase__		:     Optional[Any]								=self.tokenizer_class.from_pretrained(lowercase_    ,	**lowercase_    )
																		UpperCamelCase__		:     List[str]								=self.rust_tokenizer_class.from_pretrained(lowercase_    ,	**lowercase_    )

																		UpperCamelCase__		:     Optional[int]								=tokenizer_p.encode(lowercase_    ,	add_special_tokens=lowercase_    )
																		UpperCamelCase__		:     Optional[Any]								=tokenizer_r.encode(lowercase_    ,	add_special_tokens=lowercase_    )

																		UpperCamelCase__		:     int								=tokenizer_r.convert_ids_to_tokens(lowercase_    )
																		UpperCamelCase__		:     Optional[int]								=tokenizer_p.convert_ids_to_tokens(lowercase_    )

																		# it is expected that each Chinese character is not preceded by "##"
																		self.assertListEqual(lowercase_    ,	lowercase_    )
																		self.assertListEqual(lowercase_    ,	lowercase_    )

																		UpperCamelCase__		:     Optional[int]								=False
																		UpperCamelCase__		:     Union[str, Any]								=self.rust_tokenizer_class.from_pretrained(lowercase_    ,	**lowercase_    )
																		UpperCamelCase__		:     List[Any]								=self.tokenizer_class.from_pretrained(lowercase_    ,	**lowercase_    )

																		UpperCamelCase__		:     Any								=tokenizer_r.encode(lowercase_    ,	add_special_tokens=lowercase_    )
																		UpperCamelCase__		:     int								=tokenizer_p.encode(lowercase_    ,	add_special_tokens=lowercase_    )

																		UpperCamelCase__		:     Optional[int]								=tokenizer_r.convert_ids_to_tokens(lowercase_    )
																		UpperCamelCase__		:     List[Any]								=tokenizer_p.convert_ids_to_tokens(lowercase_    )

																		# it is expected that only the first Chinese character is not preceded by "##".
																		UpperCamelCase__		:     Dict								=[
																		    f'''##{token}''' if idx != 0 else token for idx, token in enumerate(lowercase_    )
																		]
																		self.assertListEqual(lowercase_    ,	lowercase_    )
																		self.assertListEqual(lowercase_    ,	lowercase_    )





"""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    )    )


