

'''simple docstring'''



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

import regex as re

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


if TYPE_CHECKING:
  from transformers.pipelines.conversational import Conversation

UpperCAmelCase					:			str       =	logging.get_logger(__name__)


UpperCAmelCase					:			Dict       =	{
    'vocab_file': 'vocab.json',
    'merges_file': 'merges.txt',
    'tokenizer_config_file': 'tokenizer_config.json',
}

UpperCAmelCase					:			Union[str, Any]       =	{
    'vocab_file': {'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'},
    'merges_file': {'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'},
    'tokenizer_config_file': {
        'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json'
    },
}

UpperCAmelCase					:			str       =	{'facebook/blenderbot-3B': 1_2_8}







@lru_cache()
# Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode
def   a__			(    ):




     """simple docstring"""


     __SCREAMING_SNAKE_CASE      = (
         list(range(ord("""!"""		) ,       ord("""~"""		) + 1		)		) + list(range(ord("""¡"""		) ,       ord("""¬"""		) + 1		)		) + list(range(ord("""®"""		) ,       ord("""ÿ"""		) + 1		)		)
     )
     __SCREAMING_SNAKE_CASE      = bs[:]
     __SCREAMING_SNAKE_CASE      = 0
     for b in range(2**8		):
          if b not in bs:
               bs.append(UpperCAmelCase_		)
               cs.append(2**8 + n		)
               n += 1
     __SCREAMING_SNAKE_CASE      = [chr(UpperCAmelCase_		) for n in cs]
     return dict(zip(UpperCAmelCase_ ,       UpperCAmelCase_		)		)







def   a__			(    a__		):




     """simple docstring"""


     __SCREAMING_SNAKE_CASE      = set()
     __SCREAMING_SNAKE_CASE      = word[0]
     for char in word[1:]:
          pairs.add((prev_char, char)		)
          __SCREAMING_SNAKE_CASE      = char
     return pairs







class 		lowerCAmelCase__  ( a   ):



 """simple docstring"""



 lowerCAmelCase__       = VOCAB_FILES_NAMES
 lowerCAmelCase__       = PRETRAINED_VOCAB_FILES_MAP
 lowerCAmelCase__       = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 lowerCAmelCase__       = ["input_ids", "attention_mask"]
 def __init__(						self   :			List[Any]   ,				__SCREAMING_SNAKE_CASE   :			Any   ,				__SCREAMING_SNAKE_CASE   :			Tuple   ,				__SCREAMING_SNAKE_CASE   :			str="replace"   ,				__SCREAMING_SNAKE_CASE   :			Optional[Any]="<s>"   ,				__SCREAMING_SNAKE_CASE   :			int="</s>"   ,				__SCREAMING_SNAKE_CASE   :			List[Any]="</s>"   ,				__SCREAMING_SNAKE_CASE   :			str="<s>"   ,				__SCREAMING_SNAKE_CASE   :			List[Any]="<unk>"   ,				__SCREAMING_SNAKE_CASE   :			Union[str, Any]="<pad>"   ,				__SCREAMING_SNAKE_CASE   :			Tuple="<mask>"   ,				__SCREAMING_SNAKE_CASE   :			Tuple=False   ,				**__SCREAMING_SNAKE_CASE   :			str   ,				)    ->					Optional[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = AddedToken(lowerCamelCase__   ,				lstrip=lowerCamelCase__   ,				rstrip=lowerCamelCase__     ) if isinstance(lowerCamelCase__   ,				lowerCamelCase__     ) else bos_token
      __SCREAMING_SNAKE_CASE      = AddedToken(lowerCamelCase__   ,				lstrip=lowerCamelCase__   ,				rstrip=lowerCamelCase__     ) if isinstance(lowerCamelCase__   ,				lowerCamelCase__     ) else eos_token
      __SCREAMING_SNAKE_CASE      = AddedToken(lowerCamelCase__   ,				lstrip=lowerCamelCase__   ,				rstrip=lowerCamelCase__     ) if isinstance(lowerCamelCase__   ,				lowerCamelCase__     ) else sep_token
      __SCREAMING_SNAKE_CASE      = AddedToken(lowerCamelCase__   ,				lstrip=lowerCamelCase__   ,				rstrip=lowerCamelCase__     ) if isinstance(lowerCamelCase__   ,				lowerCamelCase__     ) else cls_token
      __SCREAMING_SNAKE_CASE      = AddedToken(lowerCamelCase__   ,				lstrip=lowerCamelCase__   ,				rstrip=lowerCamelCase__     ) if isinstance(lowerCamelCase__   ,				lowerCamelCase__     ) else unk_token
      __SCREAMING_SNAKE_CASE      = 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
      __SCREAMING_SNAKE_CASE      = 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:
           __SCREAMING_SNAKE_CASE      = json.load(lowerCamelCase__     )
      __SCREAMING_SNAKE_CASE      = {v: k for k, v in self.encoder.items()}
      __SCREAMING_SNAKE_CASE      = errors  # how to handle errors in decoding
      __SCREAMING_SNAKE_CASE      = bytes_to_unicode()
      __SCREAMING_SNAKE_CASE      = {v: k for k, v in self.byte_encoder.items()}
      with open(lowerCamelCase__   ,				encoding="""utf-8"""     ) as merges_handle:
           __SCREAMING_SNAKE_CASE      = merges_handle.read().split("""\n"""     )[1:-1]
      __SCREAMING_SNAKE_CASE      = [tuple(merge.split()     ) for merge in bpe_merges]
      __SCREAMING_SNAKE_CASE      = dict(zip(lowerCamelCase__   ,				range(len(lowerCamelCase__     )     )     )     )
      __SCREAMING_SNAKE_CASE      = {}
      __SCREAMING_SNAKE_CASE      = add_prefix_space

      # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
      __SCREAMING_SNAKE_CASE      = re.compile(r"""\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""     )
 @property
 # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.vocab_size with Roberta->Blenderbot, RoBERTa->Blenderbot
 def  UpperCAmelCase__						(						self   :			Optional[Any]     )    ->					List[Any]:


      """simple docstring"""




      return len(self.encoder     )
 def  UpperCAmelCase__						(						self   :			Union[str, Any]     )    ->					List[Any]:


      """simple docstring"""




      return dict(self.encoder   ,				**self.added_tokens_encoder     )
 def  UpperCAmelCase__						(						self   :			int   ,				__SCREAMING_SNAKE_CASE   :			Optional[int]     )    ->					Tuple:


      """simple docstring"""




      if token in self.cache:
           return self.cache[token]
      __SCREAMING_SNAKE_CASE      = tuple(lowerCamelCase__     )
      __SCREAMING_SNAKE_CASE      = get_pairs(lowerCamelCase__     )

      if not pairs:
           return token

      while True:
           __SCREAMING_SNAKE_CASE      = min(lowerCamelCase__   ,				key=lambda __SCREAMING_SNAKE_CASE     : self.bpe_ranks.get(lowerCamelCase__   ,				float("""inf"""     )     )     )
           if bigram not in self.bpe_ranks:
                break
           __SCREAMING_SNAKE_CASE      = bigram
           __SCREAMING_SNAKE_CASE      = []
           __SCREAMING_SNAKE_CASE      = 0
           while i < len(lowerCamelCase__     ):
                try:
                     __SCREAMING_SNAKE_CASE      = word.index(lowerCamelCase__   ,				lowerCamelCase__     )
                except ValueError:
                     new_word.extend(word[i:]     )
                     break
                else:
                     new_word.extend(word[i:j]     )
                     __SCREAMING_SNAKE_CASE      = 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
           __SCREAMING_SNAKE_CASE      = tuple(lowerCamelCase__     )
           __SCREAMING_SNAKE_CASE      = new_word
           if len(lowerCamelCase__     ) == 1:
                break
           else:
                __SCREAMING_SNAKE_CASE      = get_pairs(lowerCamelCase__     )
      __SCREAMING_SNAKE_CASE      = ' '.join(lowerCamelCase__     )
      __SCREAMING_SNAKE_CASE      = word
      return word
 def  UpperCAmelCase__						(						self   :			Optional[int]   ,				__SCREAMING_SNAKE_CASE   :			Optional[Any]     )    ->					Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = []
      for token in re.findall(self.pat   ,				lowerCamelCase__     ):
           __SCREAMING_SNAKE_CASE      = ''.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  UpperCAmelCase__						(						self   :			Optional[Any]   ,				__SCREAMING_SNAKE_CASE   :			List[str]     )    ->					str:


      """simple docstring"""




      return self.encoder.get(lowerCamelCase__   ,				self.encoder.get(self.unk_token     )     )
 def  UpperCAmelCase__						(						self   :			List[Any]   ,				__SCREAMING_SNAKE_CASE   :			Optional[Any]     )    ->					str:


      """simple docstring"""




      return self.decoder.get(lowerCamelCase__     )
 def  UpperCAmelCase__						(						self   :			Union[str, Any]   ,				__SCREAMING_SNAKE_CASE   :			int     )    ->					str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = ''.join(lowerCamelCase__     )
      __SCREAMING_SNAKE_CASE      = bytearray([self.byte_decoder[c] for c in text]     ).decode("""utf-8"""   ,				errors=self.errors     )
      return text
 def  UpperCAmelCase__						(						self   :			str   ,				__SCREAMING_SNAKE_CASE   :			str   ,				__SCREAMING_SNAKE_CASE   :			Optional[str] = None     )    ->					str:


      """simple docstring"""




      if not os.path.isdir(lowerCamelCase__     ):
           logger.error(f'Vocabulary path ({save_directory}) should be a directory'     )
           return
      __SCREAMING_SNAKE_CASE      = os.path.join(
          lowerCamelCase__   ,				(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]     )
      __SCREAMING_SNAKE_CASE      = 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"""     )

      __SCREAMING_SNAKE_CASE      = 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 __SCREAMING_SNAKE_CASE     : 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!"""     )
                     __SCREAMING_SNAKE_CASE      = token_index
                writer.write(""" """.join(lowerCamelCase__     ) + """\n"""     )
                index += 1

      return vocab_file, merge_file
 def  UpperCAmelCase__						(						self   :			List[Any]   ,				__SCREAMING_SNAKE_CASE   :			List[int]   ,				__SCREAMING_SNAKE_CASE   :			Optional[List[int]] = None   ,				__SCREAMING_SNAKE_CASE   :			bool = False     )    ->					Any:


      """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  UpperCAmelCase__						(						self   :			Dict   ,				__SCREAMING_SNAKE_CASE   :			List[int]   ,				__SCREAMING_SNAKE_CASE   :			Optional[List[int]] = None     )    ->					Tuple:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = [self.sep_token_id]
      __SCREAMING_SNAKE_CASE      = [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  UpperCAmelCase__						(						self   :			str   ,				__SCREAMING_SNAKE_CASE   :			Dict   ,				__SCREAMING_SNAKE_CASE   :			Dict=False   ,				**__SCREAMING_SNAKE_CASE   :			int     )    ->					Tuple:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = 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()):
           __SCREAMING_SNAKE_CASE      = ' ' + text
      return (text, kwargs)
 def  UpperCAmelCase__						(						self   :			Optional[Any]   ,				__SCREAMING_SNAKE_CASE   :			List[int]   ,				__SCREAMING_SNAKE_CASE   :			Optional[List[int]] = None     )    ->					Optional[Any]:


      """simple docstring"""




      return token_ids_a + [self.eos_token_id]


 def  UpperCAmelCase__						(						self   :			int   ,				__SCREAMING_SNAKE_CASE   :			"Conversation"     )    ->					Optional[int]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = []
      for is_user, text in conversation.iter_texts():
           if is_user:
                # We need to space prefix as it's being done within blenderbot
                inputs.append(""" """ + text     )
           else:
                # Generated responses should contain them already.
                inputs.append(lowerCamelCase__     )

      __SCREAMING_SNAKE_CASE      = '  '.join(lowerCamelCase__     )
      __SCREAMING_SNAKE_CASE      = self.encode(lowerCamelCase__     )
      if len(lowerCamelCase__     ) > self.model_max_length:
           __SCREAMING_SNAKE_CASE      = input_ids[-self.model_max_length :]
           logger.warning(f'Trimmed input from conversation as it was longer than {self.model_max_length} tokens.'     )
      return input_ids











'''simple docstring'''
import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch
					from torch import nn

					from transformers import MaskFormerSwinBackbone
					from transformers.models.maskformer import MaskFormerSwinModel






class 				lowercase__      :





	def __init__(					self   :							Tuple					,lowerCamelCase__   :							str					,lowerCamelCase__   :							str=13					,lowerCamelCase__   :							List[Any]=32					,lowerCamelCase__   :							Optional[int]=2					,lowerCamelCase__   :							str=3					,lowerCamelCase__   :							Optional[int]=16					,lowerCamelCase__   :							Union[str, Any]=[1, 2, 1]					,lowerCamelCase__   :							Optional[int]=[2, 2, 4]					,lowerCamelCase__   :							int=2					,lowerCamelCase__   :							Any=2.0					,lowerCamelCase__   :							Dict=True					,lowerCamelCase__   :							List[Any]=0.0					,lowerCamelCase__   :							List[str]=0.0					,lowerCamelCase__   :							Union[str, Any]=0.1					,lowerCamelCase__   :							List[Any]="gelu"					,lowerCamelCase__   :							int=False					,lowerCamelCase__   :							Any=True					,lowerCamelCase__   :							List[Any]=0.0_2					,lowerCamelCase__   :							Dict=1E-5					,lowerCamelCase__   :							Optional[int]=True					,lowerCamelCase__   :							int=None					,lowerCamelCase__   :							int=True					,lowerCamelCase__   :							List[Any]=10					,lowerCamelCase__   :							int=8					,lowerCamelCase__   :							Any=["stage1", "stage2", "stage3"]					,lowerCamelCase__   :							List[str]=[1, 2, 3]					,):







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





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

							_UpperCamelCase   :						Dict												=  self.get_config()

							return config, pixel_values, labels





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							return MaskFormerSwinConfig(
							    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					,out_features=self.out_features					,out_indices=self.out_indices					,)





	def       UpperCamelCase_    (					self   :							Optional[int]					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							Optional[Any]					,lowerCamelCase__   :							Any					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  MaskFormerSwinModel(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Dict												=  model(lowerCamelCase__					)

							_UpperCamelCase   :						Tuple												=  ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths					) - 1))
							_UpperCamelCase   :						Optional[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       UpperCamelCase_    (					self   :							int					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							Optional[Any]					,lowerCamelCase__   :							Union[str, Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  MaskFormerSwinBackbone(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						List[Any]												=  model(lowerCamelCase__					)

							# verify feature maps
							self.parent.assertEqual(len(result.feature_maps					)					,len(config.out_features					)					)
							self.parent.assertListEqual(list(result.feature_maps[0].shape					)					,[13, 16, 16, 16]					)

							# verify channels
							self.parent.assertEqual(len(model.channels					)					,len(config.out_features					)					)
							self.parent.assertListEqual(model.channels					,[16, 32, 64]					)

							# verify ValueError
							with self.parent.assertRaises(lowerCamelCase__					):
													_UpperCamelCase   :						Union[str, Any]												=  ['stem']
													_UpperCamelCase   :						int												=  MaskFormerSwinBackbone(config=lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  self.prepare_config_and_inputs()
							_UpperCamelCase							,     _UpperCamelCase							,     _UpperCamelCase   :						str												=  config_and_inputs
							_UpperCamelCase   :						int												=  {'pixel_values': pixel_values}
							return config, inputs_dict






@require_torch
class 				lowercase__      (				lowercase		,   lowercase		,   unittest.TestCase  ):
	lowercase__    							=     (
	    (
	        MaskFormerSwinModel,
	        MaskFormerSwinBackbone,
	    )
	    if is_torch_available()
	    else ()
	)
	lowercase__    							=     {"""feature-extraction""": MaskFormerSwinModel} if is_torch_available() else {}
	lowercase__    							=     False
	lowercase__    							=     False
	lowercase__    							=     False
	lowercase__    							=     False
	lowercase__    							=     False





	def       UpperCamelCase_    (					self   :							Tuple					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  MaskFormerSwinModelTester(self					)
							_UpperCamelCase   :						Union[str, Any]												=  ConfigTester(self					,config_class=lowerCamelCase__					,embed_dim=37					)





	@require_torch_multi_gpu
	@unittest.skip(
	    reason=(
	        '`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with'
	        ' `nn.DataParallel`'
	    )					)
	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''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       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							return





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						str												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_backbone(*lowerCamelCase__					)





	@unittest.skip('Swin does not use inputs_embeds'					)
	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							pass





	@unittest.skip('Swin does not support feedforward chunking'					)
	def       UpperCamelCase_    (					self   :							Tuple					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						Dict												=  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCamelCase   :						Any												=  model_class(lowerCamelCase__					)
													self.assertIsInstance(model.get_input_embeddings()					,(nn.Module)					)
													_UpperCamelCase   :						str												=  model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowerCamelCase__					,nn.Linear					)					)





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						Optional[Any]												=  self.model_tester.prepare_config_and_inputs_for_common()

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

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





	@unittest.skip(reason='MaskFormerSwin is only used as backbone and doesn\'t support output_attentions'					)
	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							pass





	@unittest.skip(reason='MaskFormerSwin is only used as an internal backbone'					)
	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							Optional[Any]					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							List[Any]					,lowerCamelCase__   :							Tuple					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  model_class(lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()

							with torch.no_grad():
													_UpperCamelCase   :						List[Any]												=  model(**self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					)					)

							_UpperCamelCase   :						List[Any]												=  outputs.hidden_states

							_UpperCamelCase   :						Any												=  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   :						Tuple												=  (
							    config.patch_size
							    if isinstance(config.patch_size					,collections.abc.Iterable					)
							    else (config.patch_size, config.patch_size)
							)

							_UpperCamelCase   :						Optional[int]												=  (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]					,)





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						Optional[int]												=  self.model_tester.prepare_config_and_inputs_for_common()

							_UpperCamelCase   :						str												=  (
							    self.model_tester.image_size
							    if isinstance(self.model_tester.image_size					,collections.abc.Iterable					)
							    else (self.model_tester.image_size, self.model_tester.image_size)
							)

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

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





	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						Union[str, Any]												=  self.model_tester.prepare_config_and_inputs_for_common()
							_UpperCamelCase   :						List[Any]												=  3

							_UpperCamelCase   :						str												=  (
							    self.model_tester.image_size
							    if isinstance(self.model_tester.image_size					,collections.abc.Iterable					)
							    else (self.model_tester.image_size, self.model_tester.image_size)
							)
							_UpperCamelCase   :						Tuple												=  (
							    config.patch_size
							    if isinstance(config.patch_size					,collections.abc.Iterable					)
							    else (config.patch_size, config.patch_size)
							)

							_UpperCamelCase   :						List[Any]												=  image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
							_UpperCamelCase   :						Optional[Any]												=  image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

							for model_class in self.all_model_classes:
													_UpperCamelCase   :						Dict												=  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   :						Tuple												=  True
													self.check_hidden_states_output(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					,(padded_height, padded_width)					)





	@unittest.skip(reason='MaskFormerSwin doesn\'t have pretrained checkpoints'					)
	def       UpperCamelCase_    (					self   :							Tuple					):







							'''simple docstring'''
							pass





	@unittest.skip(reason='This will be fixed once MaskFormerSwin is replaced by native Swin'					)
	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							pass





	@unittest.skip(reason='This will be fixed once MaskFormerSwin is replaced by native Swin'					)
	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						str												=  self.model_tester.prepare_config_and_inputs_for_common()

							def set_nan_tensor_to_zero(lowerCamelCase__   :							str					):
													_UpperCamelCase   :						List[str]												=  0
													return t

							def check_equivalence(lowerCamelCase__   :							Optional[Any]					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							Optional[int]={}					):
													with torch.no_grad():
																			_UpperCamelCase   :						List[str]												=  model(**lowerCamelCase__					,return_dict=lowerCamelCase__					,**lowerCamelCase__					)
																			_UpperCamelCase   :						Optional[Any]												=  model(**lowerCamelCase__					,return_dict=lowerCamelCase__					,**lowerCamelCase__					).to_tuple()

																			def recursive_check(lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							int					):
																									if isinstance(lowerCamelCase__					,(List, Tuple)					):
																															for tuple_iterable_value, dict_iterable_value in zip(lowerCamelCase__					,lowerCamelCase__					):
																																					recursive_check(lowerCamelCase__					,lowerCamelCase__					)
																									elif isinstance(lowerCamelCase__					,lowerCamelCase__					):
																															for tuple_iterable_value, dict_iterable_value in zip(
																															    tuple_object.values()					,dict_object.values()					):
																																					recursive_check(lowerCamelCase__					,lowerCamelCase__					)
																									elif tuple_object is None:
																															return
																									else:
																															self.assertTrue(
																															    torch.allclose(
																															        set_nan_tensor_to_zero(lowerCamelCase__					)					,set_nan_tensor_to_zero(lowerCamelCase__					)					,atol=1E-5					)					,msg=(
																															        'Tuple and dict output are not equal. Difference:'
																															        F' {torch.max(torch.abs(tuple_object - dict_object					)					)}. Tuple has `nan`:'
																															        F' {torch.isnan(lowerCamelCase__					).any()} and `inf`: {torch.isinf(lowerCamelCase__					)}. Dict has'
																															        F' `nan`: {torch.isnan(lowerCamelCase__					).any()} and `inf`: {torch.isinf(lowerCamelCase__					)}.'
																															    )					,)

																			recursive_check(lowerCamelCase__					,lowerCamelCase__					)

							for model_class in self.all_model_classes:
													_UpperCamelCase   :						List[str]												=  model_class(lowerCamelCase__					)
													model.to(lowerCamelCase__					)
													model.eval()

													_UpperCamelCase   :						Optional[Any]												=  self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					)
													_UpperCamelCase   :						Dict												=  self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					)
													check_equivalence(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					)

													_UpperCamelCase   :						int												=  self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					,return_labels=lowerCamelCase__					)
													_UpperCamelCase   :						Optional[Any]												=  self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					,return_labels=lowerCamelCase__					)
													check_equivalence(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					)

													_UpperCamelCase   :						Optional[Any]												=  self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					)
													_UpperCamelCase   :						Any												=  self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					)
													check_equivalence(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					,{'output_hidden_states': True}					)

													_UpperCamelCase   :						Union[str, Any]												=  self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					,return_labels=lowerCamelCase__					)
													_UpperCamelCase   :						str												=  self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					,return_labels=lowerCamelCase__					)
													check_equivalence(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					,{'output_hidden_states': True}					)






@require_torch
class 				lowercase__      (				unittest.TestCase		,   lowercase  ):
	lowercase__    							=     (MaskFormerSwinBackbone,) if is_torch_available() else ()
	lowercase__    							=     MaskFormerSwinConfig





	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							_UpperCamelCase   :						Union[str, Any]												=  MaskFormerSwinModelTester(self					)





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						List[str]												=  self.model_tester.prepare_config_and_inputs_for_common()
							_UpperCamelCase   :						str												=  inputs_dict['pixel_values'].shape[0]

							for backbone_class in self.all_model_classes:
													_UpperCamelCase   :						List[Any]												=  backbone_class(lowerCamelCase__					)
													backbone.to(lowerCamelCase__					)
													backbone.eval()

													_UpperCamelCase   :						Optional[Any]												=  backbone(**lowerCamelCase__					)

													# Test default outputs and verify feature maps
													self.assertIsInstance(outputs.feature_maps					,lowerCamelCase__					)
													self.assertTrue(len(outputs.feature_maps					) == len(backbone.channels					)					)
													for feature_map, n_channels in zip(outputs.feature_maps					,backbone.channels					):
																			self.assertTrue(feature_map.shape[:2]					,(batch_size, n_channels)					)
													self.assertIsNone(outputs.hidden_states					)
													self.assertIsNone(outputs.attentions					)

													# Test output_hidden_states=True
													_UpperCamelCase   :						List[str]												=  backbone(**lowerCamelCase__					,output_hidden_states=lowerCamelCase__					)
													self.assertIsNotNone(outputs.hidden_states					)
													self.assertTrue(len(outputs.hidden_states					)					,len(backbone.stage_names					)					)
													# We skip the stem layer
													for hidden_states, n_channels in zip(outputs.hidden_states[1:]					,backbone.channels					):
																			for hidden_state in hidden_states:
																									# Hidden states are in the format (batch_size, (height * width), n_channels)
																									_UpperCamelCase							,     _UpperCamelCase							,     _UpperCamelCase   :						Optional[Any]												=  hidden_state.shape
																									self.assertTrue((h_batch_size, h_n_channels)					,(batch_size, n_channels)					)

            # Test output_attentions=True
													if self.has_attentions:
																			_UpperCamelCase   :						int												=  backbone(**lowerCamelCase__					,output_attentions=lowerCamelCase__					)
																			self.assertIsNotNone(outputs.attentions					)


