






import json
import os
from functools import lru_cache
from typing import List, Optional, Tuple

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


A_			:Union[str, Any]	    =		logging.get_logger(__name__)


A_			:Any	    =		{'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt'''}

# See all MVP models at https://huggingface.co/models?filter=mvp
A_			:Union[str, Any]	    =		{
    '''vocab_file''': {
        '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/vocab.json''',
    },
    '''added_tokens.json''': {
        '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/added_tokens.json''',
    },
    '''merges_file''': {
        '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/merges.txt''',
    },
}

A_			:List[str]	    =		{
    '''RUCAIBox/mvp''': 1024,
}






@lru_cache()
def 				A	(			)	->		Tuple:
					__UpperCamelCase :   List[Any]									=(
					    list(range(ord('!'   )				,ord('~'   ) + 1   )   ) + list(range(ord('¡'   )				,ord('¬'   ) + 1   )   ) + list(range(ord('®'   )				,ord('ÿ'   ) + 1   )   )
					)
					__UpperCamelCase :   Optional[int]									=bs[:]
					__UpperCamelCase :   List[Any]									=0
					for b in range(2**8   ):
										if b not in bs:
															bs.append(a_   )
															cs.append(2**8 + n   )
															n += 1
					__UpperCamelCase :   Union[str, Any]									=[chr(a_   ) for n in cs]
					return dict(zip(a_				,a_   )   )






def 				A	(			a_   )	->		Union[str, Any]:
					__UpperCamelCase :   int									=set()
					__UpperCamelCase :   Any									=word[0]
					for char in word[1:]:
										pairs.add((prev_char, char)   )
										__UpperCamelCase :   Union[str, Any]									=char
					return pairs




class 							__A	(     a					):





				"""simple docstring"""







				UpperCamelCase__					: Any     					=VOCAB_FILES_NAMES
				UpperCamelCase__					: Dict     					=PRETRAINED_VOCAB_FILES_MAP
				UpperCamelCase__					: Tuple     					=PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				UpperCamelCase__					: str     					=["""input_ids""", """attention_mask"""]


				def __init__(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__="replace"				,    lowerCamelCase__="<s>"				,    lowerCamelCase__="</s>"				,    lowerCamelCase__="</s>"				,    lowerCamelCase__="<s>"				,    lowerCamelCase__="<unk>"				,    lowerCamelCase__="<pad>"				,    lowerCamelCase__="<mask>"				,    lowerCamelCase__=False				,    **lowerCamelCase__				,    ):




									"""simple docstring"""

									__UpperCamelCase :   Optional[int]									=AddedToken(lowerCamelCase__				,    lstrip=lowerCamelCase__				,    rstrip=lowerCamelCase__       ) if isinstance(lowerCamelCase__				,    lowerCamelCase__       ) else bos_token
									__UpperCamelCase :   Union[str, Any]									=AddedToken(lowerCamelCase__				,    lstrip=lowerCamelCase__				,    rstrip=lowerCamelCase__       ) if isinstance(lowerCamelCase__				,    lowerCamelCase__       ) else eos_token
									__UpperCamelCase :   Optional[Any]									=AddedToken(lowerCamelCase__				,    lstrip=lowerCamelCase__				,    rstrip=lowerCamelCase__       ) if isinstance(lowerCamelCase__				,    lowerCamelCase__       ) else sep_token
									__UpperCamelCase :   int									=AddedToken(lowerCamelCase__				,    lstrip=lowerCamelCase__				,    rstrip=lowerCamelCase__       ) if isinstance(lowerCamelCase__				,    lowerCamelCase__       ) else cls_token
									__UpperCamelCase :   str									=AddedToken(lowerCamelCase__				,    lstrip=lowerCamelCase__				,    rstrip=lowerCamelCase__       ) if isinstance(lowerCamelCase__				,    lowerCamelCase__       ) else unk_token
									__UpperCamelCase :   Optional[Any]									=AddedToken(lowerCamelCase__				,    lstrip=lowerCamelCase__				,    rstrip=lowerCamelCase__       ) if isinstance(lowerCamelCase__				,    lowerCamelCase__       ) else pad_token

									# Mask token behave like a normal word, i.e. include the space before it
									__UpperCamelCase :   Optional[int]									=AddedToken(lowerCamelCase__				,    lstrip=lowerCamelCase__				,    rstrip=lowerCamelCase__       ) if isinstance(lowerCamelCase__				,    lowerCamelCase__       ) else mask_token

									super().__init__(
									    errors=lowerCamelCase__				,    bos_token=lowerCamelCase__				,    eos_token=lowerCamelCase__				,    unk_token=lowerCamelCase__				,    sep_token=lowerCamelCase__				,    cls_token=lowerCamelCase__				,    pad_token=lowerCamelCase__				,    mask_token=lowerCamelCase__				,    add_prefix_space=lowerCamelCase__				,    **lowerCamelCase__				,    )
									with open(lowerCamelCase__				,    encoding='utf-8'       ) as vocab_handle:
														__UpperCamelCase :   Dict									=json.load(lowerCamelCase__       )
									__UpperCamelCase :   List[str]									={v: k for k, v in self.encoder.items()}
									__UpperCamelCase :   Optional[int]									=errors  # how to handle errors in decoding
									__UpperCamelCase :   Optional[int]									=bytes_to_unicode()
									__UpperCamelCase :   str									={v: k for k, v in self.byte_encoder.items()}
									with open(lowerCamelCase__				,    encoding='utf-8'       ) as merges_handle:
														__UpperCamelCase :   Tuple									=merges_handle.read().split('\n'       )[1:-1]
									__UpperCamelCase :   str									=[tuple(merge.split()       ) for merge in bpe_merges]
									__UpperCamelCase :   Optional[int]									=dict(zip(lowerCamelCase__				,    range(len(lowerCamelCase__       )       )       )       )
									__UpperCamelCase :   int									={}
									__UpperCamelCase :   str									=add_prefix_space

									# Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
									__UpperCamelCase :   Optional[Any]									=re.compile(R'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+'       )


				@property
				def  __lowercase				(					self       ):




									"""simple docstring"""

									return len(self.encoder       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									return dict(self.encoder				,    **self.added_tokens_encoder       )


				def  __lowercase				(					self				,    lowerCamelCase__       ):




									"""simple docstring"""

									if token in self.cache:
														return self.cache[token]
									__UpperCamelCase :   List[str]									=tuple(lowerCamelCase__       )
									__UpperCamelCase :   Optional[int]									=get_pairs(lowerCamelCase__       )

									if not pairs:
														return token

									while True:
														__UpperCamelCase :   Union[str, Any]									=min(lowerCamelCase__				,    key=lambda lowerCamelCase__       : self.bpe_ranks.get(lowerCamelCase__				,    float('inf'       )       )       )
														if bigram not in self.bpe_ranks:
																			break
														__UpperCamelCase						,							__UpperCamelCase :   Dict									=bigram
														__UpperCamelCase :   Dict									=[]
														__UpperCamelCase :   int									=0
														while i < len(lowerCamelCase__       ):
																			try:
																								__UpperCamelCase :   List[Any]									=word.index(lowerCamelCase__				,    lowerCamelCase__       )
																			except ValueError:
																								new_word.extend(word[i:]       )
																								break
																			else:
																								new_word.extend(word[i:j]       )
																								__UpperCamelCase :   Tuple									=j

																			if word[i] == first and i < len(lowerCamelCase__       ) - 1 and word[i + 1] == second:
																								new_word.append(first + second       )
																								i += 2
																			else:
																								new_word.append(word[i]       )
																								i += 1
														__UpperCamelCase :   str									=tuple(lowerCamelCase__       )
														__UpperCamelCase :   List[Any]									=new_word
														if len(lowerCamelCase__       ) == 1:
																			break
														else:
																			__UpperCamelCase :   Union[str, Any]									=get_pairs(lowerCamelCase__       )
									__UpperCamelCase :   Tuple									=' '.join(lowerCamelCase__       )
									__UpperCamelCase :   str									=word
									return word


				def  __lowercase				(					self				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   str									=[]
									for token in re.findall(self.pat				,    lowerCamelCase__       ):
														__UpperCamelCase :   Dict									=''.join(
														    self.byte_encoder[b] for b in token.encode('utf-8'       )       )  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
														bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowerCamelCase__       ).split(' '       )       )
									return bpe_tokens


				def  __lowercase				(					self				,    lowerCamelCase__       ):




									"""simple docstring"""

									return self.encoder.get(lowerCamelCase__				,    self.encoder.get(self.unk_token       )       )


				def  __lowercase				(					self				,    lowerCamelCase__       ):




									"""simple docstring"""

									return self.decoder.get(lowerCamelCase__       )


				def  __lowercase				(					self				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   List[str]									=''.join(lowerCamelCase__       )
									__UpperCamelCase :   Union[str, Any]									=bytearray([self.byte_decoder[c] for c in text]       ).decode('utf-8'				,    errors=self.errors       )
									return text


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__ = None       ):




									"""simple docstring"""

									if not os.path.isdir(lowerCamelCase__       ):
														logger.error(f'Vocabulary path ({save_directory}) should be a directory'       )
														return
									__UpperCamelCase :   str									=os.path.join(
									    lowerCamelCase__				,    (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']       )
									__UpperCamelCase :   Tuple									=os.path.join(
									    lowerCamelCase__				,    (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file']       )

									with open(lowerCamelCase__				,    'w'				,    encoding='utf-8'       ) as f:
														f.write(json.dumps(self.encoder				,    indent=2				,    sort_keys=lowerCamelCase__				,    ensure_ascii=lowerCamelCase__       ) + '\n'       )

									__UpperCamelCase :   Dict									=0
									with open(lowerCamelCase__				,    'w'				,    encoding='utf-8'       ) as writer:
														writer.write('#version: 0.2\n'       )
														for bpe_tokens, token_index in sorted(self.bpe_ranks.items()				,    key=lambda lowerCamelCase__       : kv[1]       ):
																			if index != token_index:
																								logger.warning(
																								    f'Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.'
																								    ' Please check that the tokenizer is not corrupted!'       )
																								__UpperCamelCase :   Tuple									=token_index
																			writer.write(' '.join(lowerCamelCase__       ) + '\n'       )
																			index += 1

									return vocab_file, merge_file


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__ = None       ):




									"""simple docstring"""

									if token_ids_a is None:
														return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
									__UpperCamelCase :   Dict									=[self.cls_token_id]
									__UpperCamelCase :   Optional[int]									=[self.sep_token_id]
									return cls + token_ids_a + sep + sep + token_ids_a + sep


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__ = None				,    lowerCamelCase__ = False       ):




									"""simple docstring"""

									if already_has_special_tokens:
														return super().get_special_tokens_mask(
														    token_ids_a=lowerCamelCase__				,    token_ids_a=lowerCamelCase__				,    already_has_special_tokens=lowerCamelCase__       )

									if token_ids_a is None:
														return [1] + ([0] * len(lowerCamelCase__       )) + [1]
									return [1] + ([0] * len(lowerCamelCase__       )) + [1, 1] + ([0] * len(lowerCamelCase__       )) + [1]


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__ = None       ):




									"""simple docstring"""

									__UpperCamelCase :   Tuple									=[self.sep_token_id]
									__UpperCamelCase :   Union[str, Any]									=[self.cls_token_id]

									if token_ids_a is None:
														return len(cls + token_ids_a + sep       ) * [0]
									return len(cls + token_ids_a + sep + sep + token_ids_a + sep       ) * [0]







				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__=False				,    **lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   List[str]									=kwargs.pop('add_prefix_space'				,    self.add_prefix_space       )
									if (is_split_into_words or add_prefix_space) and (len(lowerCamelCase__       ) > 0 and not text[0].isspace()):
														__UpperCamelCase :   Dict									=' ' + text
									return (text, kwargs)









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				,    lowerCamelCase__				,    lowerCamelCase__=13				,    lowerCamelCase__=32				,    lowerCamelCase__=2				,    lowerCamelCase__=3				,    lowerCamelCase__=16				,    lowerCamelCase__=[1, 2, 1]				,    lowerCamelCase__=[2, 2, 4]				,    lowerCamelCase__=2				,    lowerCamelCase__=2.0				,    lowerCamelCase__=True				,    lowerCamelCase__=0.0				,    lowerCamelCase__=0.0				,    lowerCamelCase__=0.1				,    lowerCamelCase__="gelu"				,    lowerCamelCase__=False				,    lowerCamelCase__=True				,    lowerCamelCase__=0.02				,    lowerCamelCase__=1E-5				,    lowerCamelCase__=True				,    lowerCamelCase__=None				,    lowerCamelCase__=True				,    lowerCamelCase__=10				,    lowerCamelCase__=8				,    ):




									"""simple docstring"""

									__UpperCamelCase :   List[Any]									=parent
									__UpperCamelCase :   Tuple									=batch_size
									__UpperCamelCase :   str									=image_size
									__UpperCamelCase :   int									=patch_size
									__UpperCamelCase :   Dict									=num_channels
									__UpperCamelCase :   Optional[int]									=embed_dim
									__UpperCamelCase :   Dict									=depths
									__UpperCamelCase :   Tuple									=num_heads
									__UpperCamelCase :   List[str]									=window_size
									__UpperCamelCase :   Optional[int]									=mlp_ratio
									__UpperCamelCase :   Dict									=qkv_bias
									__UpperCamelCase :   Dict									=hidden_dropout_prob
									__UpperCamelCase :   Optional[int]									=attention_probs_dropout_prob
									__UpperCamelCase :   Optional[Any]									=drop_path_rate
									__UpperCamelCase :   Optional[int]									=hidden_act
									__UpperCamelCase :   Optional[Any]									=use_absolute_embeddings
									__UpperCamelCase :   Dict									=patch_norm
									__UpperCamelCase :   str									=layer_norm_eps
									__UpperCamelCase :   Dict									=initializer_range
									__UpperCamelCase :   Optional[int]									=is_training
									__UpperCamelCase :   Any									=scope
									__UpperCamelCase :   int									=use_labels
									__UpperCamelCase :   Union[str, Any]									=type_sequence_label_size
									__UpperCamelCase :   int									=encoder_stride


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Any									=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

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

									return config, pixel_values, labels


				def  __lowercase				(					self       ):




									"""simple docstring"""

									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  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   List[Any]									=TFSwinModel(config=lowerCamelCase__       )
									__UpperCamelCase :   Optional[int]									=model(lowerCamelCase__       )

									__UpperCamelCase :   Tuple									=((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
									__UpperCamelCase :   Dict									=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  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   Dict									=TFSwinForMaskedImageModeling(config=lowerCamelCase__       )
									__UpperCamelCase :   List[str]									=model(lowerCamelCase__       )
									self.parent.assertEqual(
									    result.logits.shape				,    (self.batch_size, self.num_channels, self.image_size, self.image_size)       )

									# test greyscale images
									__UpperCamelCase :   Dict									=1
									__UpperCamelCase :   Union[str, Any]									=TFSwinForMaskedImageModeling(lowerCamelCase__       )

									__UpperCamelCase :   Optional[Any]									=floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
									__UpperCamelCase :   List[Any]									=model(lowerCamelCase__       )
									self.parent.assertEqual(result.logits.shape				,    (self.batch_size, 1, self.image_size, self.image_size)       )


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   str									=self.type_sequence_label_size
									__UpperCamelCase :   Union[str, Any]									=TFSwinForImageClassification(lowerCamelCase__       )
									__UpperCamelCase :   str									=model(lowerCamelCase__				,    labels=lowerCamelCase__       )
									self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.type_sequence_label_size)       )

									# test greyscale images
									__UpperCamelCase :   Optional[Any]									=1
									__UpperCamelCase :   int									=TFSwinForImageClassification(lowerCamelCase__       )
									__UpperCamelCase :   Union[str, Any]									=floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
									__UpperCamelCase :   List[str]									=model(lowerCamelCase__       )
									self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.type_sequence_label_size)       )







				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Any									=self.prepare_config_and_inputs()
									__UpperCamelCase						,							__UpperCamelCase						,							__UpperCamelCase :   str									=config_and_inputs
									__UpperCamelCase :   Any									={'pixel_values': pixel_values}
									return config, inputs_dict




@require_tf
class 							__A	(     a ,	a ,	unittest.TestCase					):





				"""simple docstring"""







				UpperCamelCase__					: Tuple     					=(
				    (
				        TFSwinModel,
				        TFSwinForImageClassification,
				        TFSwinForMaskedImageModeling,
				    )
				    if is_tf_available()
				    else ()
				)
				UpperCamelCase__					: Optional[Any]     					=(
				    {"""feature-extraction""": TFSwinModel, """image-classification""": TFSwinForImageClassification}
				    if is_tf_available()
				    else {}
				)

				UpperCamelCase__					: Any     					=False
				UpperCamelCase__					: Tuple     					=False
				UpperCamelCase__					: Tuple     					=False
				UpperCamelCase__					: Tuple     					=False


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Dict									=TFSwinModelTester(self       )
									__UpperCamelCase :   int									=ConfigTester(self				,    config_class=lowerCamelCase__				,    embed_dim=37       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									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  __lowercase				(					self       ):




									"""simple docstring"""

									return


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Union[str, Any]									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   List[str]									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   List[Any]									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__       )


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




									"""simple docstring"""

									pass


				def  __lowercase				(					self       ):




									"""simple docstring"""

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

									for model_class in self.all_model_classes:
														__UpperCamelCase :   Union[str, Any]									=model_class(lowerCamelCase__       )
														self.assertIsInstance(model.get_input_embeddings()				,    tf.keras.layers.Layer       )
														__UpperCamelCase :   List[str]									=model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(lowerCamelCase__				,    tf.keras.layers.Dense       )       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase						,							__UpperCamelCase :   Optional[Any]									=self.model_tester.prepare_config_and_inputs_for_common()

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

														__UpperCamelCase :   Any									=['pixel_values']
														self.assertListEqual(arg_names[:1]				,    lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase						,							__UpperCamelCase :   Any									=self.model_tester.prepare_config_and_inputs_for_common()
									__UpperCamelCase :   Any									=True

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

														# check that output_attentions also work using config
														del inputs_dict["output_attentions"]
														__UpperCamelCase :   List[str]									=True
														__UpperCamelCase :   Tuple									=config.window_size**2
														__UpperCamelCase :   Optional[int]									=model_class(lowerCamelCase__       )
														__UpperCamelCase :   Any									=model(**self._prepare_for_class(lowerCamelCase__				,    lowerCamelCase__       )       )
														__UpperCamelCase :   Optional[int]									=outputs.attentions
														self.assertEqual(len(lowerCamelCase__       )				,    lowerCamelCase__       )

														self.assertListEqual(
														    list(attentions[0].shape[-3:]       )				,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]				,    )
														__UpperCamelCase :   Union[str, Any]									=len(lowerCamelCase__       )

														# Check attention is always last and order is fine
														__UpperCamelCase :   Dict									=True
														__UpperCamelCase :   Tuple									=True
														__UpperCamelCase :   Union[str, Any]									=model_class(lowerCamelCase__       )
														__UpperCamelCase :   int									=model(**self._prepare_for_class(lowerCamelCase__				,    lowerCamelCase__       )       )

														if hasattr(self.model_tester				,    'num_hidden_states_types'       ):
																			__UpperCamelCase :   Union[str, Any]									=self.model_tester.num_hidden_states_types
														else:
																			# also another +1 for reshaped_hidden_states
																			__UpperCamelCase :   Union[str, Any]									=2
														self.assertEqual(out_len + added_hidden_states				,    len(lowerCamelCase__       )       )

														__UpperCamelCase :   int									=outputs.attentions

														self.assertEqual(len(lowerCamelCase__       )				,    lowerCamelCase__       )

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


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   List[Any]									=model_class(lowerCamelCase__       )
									__UpperCamelCase :   Optional[Any]									=model(**self._prepare_for_class(lowerCamelCase__				,    lowerCamelCase__       )       )
									__UpperCamelCase :   Optional[int]									=outputs.hidden_states

									__UpperCamelCase :   Dict									=getattr(
									    self.model_tester				,    'expected_num_hidden_layers'				,    len(self.model_tester.depths       ) + 1       )
									self.assertEqual(len(lowerCamelCase__       )				,    lowerCamelCase__       )

									# Swin has a different seq_length
									__UpperCamelCase :   Union[str, Any]									=to_atuple(config.patch_size       )

									__UpperCamelCase :   Dict									=(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 :   List[Any]									=outputs.reshaped_hidden_states
									self.assertEqual(len(lowerCamelCase__       )				,    lowerCamelCase__       )

									__UpperCamelCase						,							__UpperCamelCase						,							__UpperCamelCase						,							__UpperCamelCase :   List[str]									=reshaped_hidden_states[0].shape

									__UpperCamelCase :   Dict									=tf.reshape(reshaped_hidden_states[0]				,    (batch_size, num_channels, height * width)       )
									__UpperCamelCase :   Any									=tf.transpose(lowerCamelCase__				,    (0, 2, 1)       )

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


				def  __lowercase				(					self       ):




									"""simple docstring"""

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

									__UpperCamelCase :   Tuple									=to_atuple(self.model_tester.image_size       )

									for model_class in self.all_model_classes:
														__UpperCamelCase :   Dict									=True
														self.check_hidden_states_output(lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__       )

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														__UpperCamelCase :   int									=True

														self.check_hidden_states_output(lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

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

									__UpperCamelCase :   int									=to_atuple(self.model_tester.image_size       )
									__UpperCamelCase :   Optional[Any]									=to_atuple(config.patch_size       )

									__UpperCamelCase :   Optional[int]									=image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
									__UpperCamelCase :   Dict									=image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

									for model_class in self.all_model_classes:
														__UpperCamelCase :   Any									=True
														self.check_hidden_states_output(lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    (padded_height, padded_width)       )

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														__UpperCamelCase :   int									=True
														self.check_hidden_states_output(lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    (padded_height, padded_width)       )







				@slow
				def  __lowercase				(					self       ):




									"""simple docstring"""

									for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__UpperCamelCase :   Optional[int]									=TFSwinModel.from_pretrained(lowerCamelCase__       )
														self.assertIsNotNone(lowerCamelCase__       )




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





				"""simple docstring"""







				@cached_property
				def  __lowercase				(					self       ):




									"""simple docstring"""

									return (
									    AutoImageProcessor.from_pretrained('microsoft/swin-tiny-patch4-window7-224'       )
									    if is_vision_available()
									    else None
									)







				@slow
				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   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 :   List[str]									=image_processor(images=lowerCamelCase__				,    return_tensors='tf'       )

									# forward pass
									__UpperCamelCase :   Tuple									=model(lowerCamelCase__       )

									# verify the logits
									__UpperCamelCase :   Optional[int]									=tf.TensorShape((1, 1000)       )
									self.assertEqual(outputs.logits.shape				,    lowerCamelCase__       )
									__UpperCamelCase :   Optional[int]									=tf.constant([-0.0_948, -0.6_454, -0.0_921]       )
									self.assertTrue(np.allclose(outputs.logits[0, :3]				,    lowerCamelCase__				,    atol=1E-4       )       )

