


import json
import os
import re
import unicodedata
from json.encoder import INFINITY
from typing import Any, Dict, List, Optional, Tuple, Union

import numpy as np
import regex

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_flax_available, is_tf_available, is_torch_available, logging
from ...utils.generic import _is_jax, _is_numpy


A				:       str      =  logging.get_logger(__name__)

A				:       List[Any]      =  {
    """artists_file""": """artists.json""",
    """lyrics_file""": """lyrics.json""",
    """genres_file""": """genres.json""",
}

A				:       List[Any]      =  {
    """artists_file""": {
        """jukebox""": """https://huggingface.co/ArthurZ/jukebox/blob/main/artists.json""",
    },
    """genres_file""": {
        """jukebox""": """https://huggingface.co/ArthurZ/jukebox/blob/main/genres.json""",
    },
    """lyrics_file""": {
        """jukebox""": """https://huggingface.co/ArthurZ/jukebox/blob/main/lyrics.json""",
    },
}

A				:       Union[str, Any]      =  {
    """jukebox""": 512,
}






class      UpperCamelCase(	UpperCAmelCase__ ):
					snake_case_		:					List[Any]    					=						VOCAB_FILES_NAMES
					snake_case_		:					Optional[Any]    					=						PRETRAINED_VOCAB_FILES_MAP
					snake_case_		:					Any    					=						PRETRAINED_LYRIC_TOKENS_SIZES
					snake_case_		:					Optional[int]    					=						["""input_ids""", """attention_mask"""]


					def __init__(    self						:		int  ,     SCREAMING_SNAKE_CASE						:		Optional[int]  ,     SCREAMING_SNAKE_CASE						:		str  ,     SCREAMING_SNAKE_CASE						:		Tuple  ,     SCREAMING_SNAKE_CASE						:		Tuple=["v3", "v2", "v2"]  ,     SCREAMING_SNAKE_CASE						:		int=5_1_2  ,     SCREAMING_SNAKE_CASE						:		Dict=5  ,     SCREAMING_SNAKE_CASE						:		List[Any]="<|endoftext|>"  ,     **SCREAMING_SNAKE_CASE						:		Any  ,     )      ->						Tuple:




											'''simple docstring'''

											__snake_case				=		AddedToken(_a  ,     lstrip=_a  ,     rstrip=_a    ) if isinstance(_a  ,     _a    ) else unk_token
											super().__init__(
											    unk_token=_a  ,     n_genres=_a  ,     version=_a  ,     max_n_lyric_tokens=_a  ,     **_a  ,     )
											__snake_case				=		version
											__snake_case				=		max_n_lyric_tokens
											__snake_case				=		n_genres

											with open(_a  ,     encoding="utf-8"    ) as vocab_handle:
																	__snake_case				=		json.load(_a    )

											with open(_a  ,     encoding="utf-8"    ) as vocab_handle:
																	__snake_case				=		json.load(_a    )

											with open(_a  ,     encoding="utf-8"    ) as vocab_handle:
																	__snake_case				=		json.load(_a    )

											__snake_case				=		R"[^A-Za-z0-9.,:;!?\-'\"()\[\] \t\n]+"
											# In v2, we had a n_vocab=80 and in v3 we missed + and so n_vocab=79 of characters.
											if len(self.lyrics_encoder    ) == 7_9:
																	__snake_case				=		oov.replace(R"\-'"  ,     R"\-+'"    )

											__snake_case				=		regex.compile(_a    )
											__snake_case				=		{v: k for k, v in self.artists_encoder.items()}
											__snake_case				=		{v: k for k, v in self.genres_encoder.items()}
											__snake_case				=		{v: k for k, v in self.lyrics_encoder.items()}


					@property
					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[Any]    )      ->						Tuple:




											'''simple docstring'''

											return len(self.artists_encoder    ) + len(self.genres_encoder    ) + len(self.lyrics_encoder    )


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[Any]    )      ->						str:




											'''simple docstring'''

											return dict(self.artists_encoder  ,     self.genres_encoder  ,     self.lyrics_encoder    )


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[int]  ,     SCREAMING_SNAKE_CASE						:		Union[str, Any]  ,     SCREAMING_SNAKE_CASE						:		int  ,     SCREAMING_SNAKE_CASE						:		int    )      ->						str:




											'''simple docstring'''

											__snake_case				=		[self.artists_encoder.get(_a  ,     0    ) for artist in list_artists]
											for genres in range(len(_a    )    ):
																	__snake_case				=		[self.genres_encoder.get(_a  ,     0    ) for genre in list_genres[genres]]
																	__snake_case				=		list_genres[genres] + [-1] * (self.n_genres - len(list_genres[genres]    ))

											__snake_case				=		[[self.lyrics_encoder.get(_a  ,     0    ) for character in list_lyrics[0]], [], []]
											return artists_id, list_genres, lyric_ids


					def 				SCREAMING_SNAKE_CASE_	(    self						:		int  ,     SCREAMING_SNAKE_CASE						:		List[str]    )      ->						Optional[int]:




											'''simple docstring'''

											return list(_a    )


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Union[str, Any]  ,     SCREAMING_SNAKE_CASE						:		Tuple  ,     SCREAMING_SNAKE_CASE						:		Any  ,     SCREAMING_SNAKE_CASE						:		List[Any]  ,     **SCREAMING_SNAKE_CASE						:		List[Any]    )      ->						Optional[int]:




											'''simple docstring'''

											__snake_case		,       __snake_case		,       __snake_case				=		self.prepare_for_tokenization(_a  ,     _a  ,     _a    )
											__snake_case				=		self._tokenize(_a    )
											return artist, genre, lyrics


					def 				SCREAMING_SNAKE_CASE_	(    self						:		int  ,     SCREAMING_SNAKE_CASE						:		int  ,     SCREAMING_SNAKE_CASE						:		List[str]  ,     SCREAMING_SNAKE_CASE						:		List[str]  ,     SCREAMING_SNAKE_CASE						:		str = False    )      ->						List[Any]:




											'''simple docstring'''

											for idx in range(len(self.version    )    ):
																	if self.version[idx] == "v3":
																							__snake_case				=		artists[idx].lower()
																							__snake_case				=		[genres[idx].lower()]
																	else:
																							__snake_case				=		self._normalize(artists[idx]    ) + ".v2"
																							__snake_case				=		[
																							    self._normalize(_a    ) + ".v2" for genre in genres[idx].split("_"    )
																							]  # split is for the full dictionary with combined genres

											if self.version[0] == "v2":
																	__snake_case				=		regex.compile(R"[^A-Za-z0-9.,:;!?\-'\"()\[\] \t\n]+"    )
																	__snake_case				=		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,:;!?-+'\"()[] \t\n"
																	__snake_case				=		{vocab[index]: index + 1 for index in range(len(_a    )    )}
																	__snake_case				=		0
																	__snake_case				=		len(_a    ) + 1
																	__snake_case				=		self.vocab
																	__snake_case				=		{v: k for k, v in self.vocab.items()}
																	__snake_case				=		""
											else:
																	__snake_case				=		regex.compile(R"[^A-Za-z0-9.,:;!?\-+'\"()\[\] \t\n]+"    )

											__snake_case				=		self._run_strip_accents(_a    )
											__snake_case				=		lyrics.replace("\\"  ,     "\n"    )
											__snake_case				=		self.out_of_vocab.sub(""  ,     _a    ), [], []
											return artists, genres, lyrics


					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[str]  ,     SCREAMING_SNAKE_CASE						:		List[str]    )      ->						List[str]:




											'''simple docstring'''

											__snake_case				=		unicodedata.normalize("NFD"  ,     _a    )
											__snake_case				=		[]
											for char in text:
																	__snake_case				=		unicodedata.category(_a    )
																	if cat == "Mn":
																							continue
																	output.append(_a    )
											return "".join(_a    )


					def 				SCREAMING_SNAKE_CASE_	(    self						:		str  ,     SCREAMING_SNAKE_CASE						:		int    )      ->						Any:




											'''simple docstring'''

											__snake_case				=		(
											    [chr(_a    ) for i in range(ord("a"    )  ,     ord("z"    ) + 1    )]
											    + [chr(_a    ) for i in range(ord("A"    )  ,     ord("Z"    ) + 1    )]
											    + [chr(_a    ) for i in range(ord("0"    )  ,     ord("9"    ) + 1    )]
											    + ["."]
											)
											__snake_case				=		frozenset(_a    )
											__snake_case				=		re.compile(R"_+"    )
											__snake_case				=		"".join([c if c in accepted else "_" for c in text.lower()]    )
											__snake_case				=		pattern.sub("_"  ,     _a    ).strip("_"    )
											return text


					def 				SCREAMING_SNAKE_CASE_	(    self						:		int  ,     SCREAMING_SNAKE_CASE						:		Any    )      ->						Tuple:




											'''simple docstring'''

											return " ".join(_a    )


					def 				SCREAMING_SNAKE_CASE_	(    self						:		int  ,     SCREAMING_SNAKE_CASE						:		Optional[int]  ,     SCREAMING_SNAKE_CASE						:		Union[str, Any] = None  ,     SCREAMING_SNAKE_CASE						:		Union[str, Any] = False    )      ->						List[str]:




											'''simple docstring'''

											if not isinstance(_a  ,     _a    ):
																	__snake_case				=		TensorType(_a    )

											# Get a function reference for the correct framework
											if tensor_type == TensorType.TENSORFLOW:
																	if not is_tf_available():
																							raise ImportError(
																							    "Unable to convert output to TensorFlow tensors format, TensorFlow is not installed."    )
																	import tensorflow as tf

																	__snake_case				=		tf.constant
																	__snake_case				=		tf.is_tensor
											elif tensor_type == TensorType.PYTORCH:
																	if not is_torch_available():
																							raise ImportError("Unable to convert output to PyTorch tensors format, PyTorch is not installed."    )
																	import torch

																	__snake_case				=		torch.tensor
																	__snake_case				=		torch.is_tensor
											elif tensor_type == TensorType.JAX:
																	if not is_flax_available():
																							raise ImportError("Unable to convert output to JAX tensors format, JAX is not installed."    )
																	import jax.numpy as jnp  # noqa: F811

																	__snake_case				=		jnp.array
																	__snake_case				=		_is_jax
											else:
																	__snake_case				=		np.asarray
																	__snake_case				=		_is_numpy

											# Do the tensor conversion in batch

											try:
																	if prepend_batch_axis:
																							__snake_case				=		[inputs]

																	if not is_tensor(_a    ):
																							__snake_case				=		as_tensor(_a    )
											except:  # noqa E722
																	raise ValueError(
																	    "Unable to create tensor, you should probably activate truncation and/or padding "
																	    "with 'padding=True' 'truncation=True' to have batched tensors with the same length."    )

											return inputs


					def __call__(    self						:		Optional[int]  ,     SCREAMING_SNAKE_CASE						:		Union[str, Any]  ,     SCREAMING_SNAKE_CASE						:		Optional[Any]  ,     SCREAMING_SNAKE_CASE						:		int=""  ,     SCREAMING_SNAKE_CASE						:		Union[str, Any]="pt"    )      ->						Union[str, Any]:




											'''simple docstring'''

											__snake_case				=		[0, 0, 0]
											__snake_case				=		[artist] * len(self.version    )
											__snake_case				=		[genres] * len(self.version    )

											__snake_case		,       __snake_case		,       __snake_case				=		self.tokenize(_a  ,     _a  ,     _a    )
											__snake_case		,       __snake_case		,       __snake_case				=		self._convert_token_to_id(_a  ,     _a  ,     _a    )

											__snake_case				=		[-INFINITY] * len(full_tokens[-1]    )
											__snake_case				=		[
											    self.convert_to_tensors(
											        [input_ids + [artists_id[i]] + genres_ids[i] + full_tokens[i]]  ,     tensor_type=_a    )
											    for i in range(len(self.version    )    )
											]
											return BatchEncoding({"input_ids": input_ids, "attention_masks": attention_masks}    )


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[Any]  ,     SCREAMING_SNAKE_CASE						:		Tuple  ,     SCREAMING_SNAKE_CASE						:		Union[str, Any] = None    )      ->						Tuple:




											'''simple docstring'''

											if not os.path.isdir(_a    ):
																	logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''    )
																	return

											__snake_case				=		os.path.join(
											    _a  ,     (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["artists_file"]    )
											with open(_a  ,     "w"  ,     encoding="utf-8"    ) as f:
																	f.write(json.dumps(self.artists_encoder  ,     ensure_ascii=_a    )    )

											__snake_case				=		os.path.join(
											    _a  ,     (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["genres_file"]    )
											with open(_a  ,     "w"  ,     encoding="utf-8"    ) as f:
																	f.write(json.dumps(self.genres_encoder  ,     ensure_ascii=_a    )    )

											__snake_case				=		os.path.join(
											    _a  ,     (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["lyrics_file"]    )
											with open(_a  ,     "w"  ,     encoding="utf-8"    ) as f:
																	f.write(json.dumps(self.lyrics_encoder  ,     ensure_ascii=_a    )    )

											return (artists_file, genres_file, lyrics_file)






					def 				SCREAMING_SNAKE_CASE_	(    self						:		str  ,     SCREAMING_SNAKE_CASE						:		int  ,     SCREAMING_SNAKE_CASE						:		int  ,     SCREAMING_SNAKE_CASE						:		Optional[int]    )      ->						int:




											'''simple docstring'''

											__snake_case				=		self.artists_decoder.get(_a    )
											__snake_case				=		[self.genres_decoder.get(_a    ) for genre in genres_index]
											__snake_case				=		[self.lyrics_decoder.get(_a    ) for character in lyric_index]
											return artist, genres, lyrics











"""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        __magic_name__				:






				'''simple docstring'''





				def __init__(			self						,					_a						,					_a=13						,					_a=32						,					_a=2						,					_a=3						,					_a=16						,					_a=[1, 2, 1]						,					_a=[2, 2, 4]						,					_a=2						,					_a=2.0						,					_a=True						,					_a=0.0						,					_a=0.0						,					_a=0.1						,					_a="gelu"						,					_a=False						,					_a=True						,					_a=0.02						,					_a=1e-5						,					_a=True						,					_a=None						,					_a=True						,					_a=10						,					_a=8						,					_a=["stage1", "stage2", "stage3"]						,					_a=[1, 2, 3]						,					):





							"""simple docstring"""
							lowerCamelCase      =   parent
							lowerCamelCase      =   batch_size
							lowerCamelCase      =   image_size
							lowerCamelCase      =   patch_size
							lowerCamelCase      =   num_channels
							lowerCamelCase      =   embed_dim
							lowerCamelCase      =   depths
							lowerCamelCase      =   num_heads
							lowerCamelCase      =   window_size
							lowerCamelCase      =   mlp_ratio
							lowerCamelCase      =   qkv_bias
							lowerCamelCase      =   hidden_dropout_prob
							lowerCamelCase      =   attention_probs_dropout_prob
							lowerCamelCase      =   drop_path_rate
							lowerCamelCase      =   hidden_act
							lowerCamelCase      =   use_absolute_embeddings
							lowerCamelCase      =   patch_norm
							lowerCamelCase      =   layer_norm_eps
							lowerCamelCase      =   initializer_range
							lowerCamelCase      =   is_training
							lowerCamelCase      =   scope
							lowerCamelCase      =   use_labels
							lowerCamelCase      =   type_sequence_label_size
							lowerCamelCase      =   encoder_stride
							lowerCamelCase      =   out_features
							lowerCamelCase      =   out_indices





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

							lowerCamelCase      =   self.get_config()

							return config, pixel_values, labels





				def   _lowerCAmelCase							(			self					):





							"""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   _lowerCAmelCase							(			self						,					_a						,					_a						,					_a					):





							"""simple docstring"""
							lowerCamelCase      =   MaskFormerSwinModel(config=_a					)
							model.to(_a					)
							model.eval()
							lowerCamelCase      =   model(_a					)

							lowerCamelCase      =   ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths					) - 1))
							lowerCamelCase      =   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						,					_a						,					_a						,					_a					):





							"""simple docstring"""
							lowerCamelCase      =   MaskFormerSwinBackbone(config=_a					)
							model.to(_a					)
							model.eval()
							lowerCamelCase      =   model(_a					)

							# 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(_a					):
										lowerCamelCase      =   ["""stem"""]
										lowerCamelCase      =   MaskFormerSwinBackbone(config=_a					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   self.prepare_config_and_inputs()
							lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase      =   config_and_inputs
							lowerCamelCase      =   {"""pixel_values""": pixel_values}
							return config, inputs_dict






@require_torch
class        __magic_name__				(			UpperCAmelCase__		,   UpperCAmelCase__		,   unittest.TestCase							):






				'''simple docstring'''
				__UpperCamelCase         =		(
				    (
				        MaskFormerSwinModel,
				        MaskFormerSwinBackbone,
				    )
				    if is_torch_available()
				    else ()
				)
				__UpperCamelCase         =		{"feature-extraction": MaskFormerSwinModel} if is_torch_available() else {}
				__UpperCamelCase         =		False
				__UpperCamelCase         =		False
				__UpperCamelCase         =		False
				__UpperCamelCase         =		False
				__UpperCamelCase         =		False





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   MaskFormerSwinModelTester(self					)
							lowerCamelCase      =   ConfigTester(self						,					config_class=_a						,					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   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							pass





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





							"""simple docstring"""
							return





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_a					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_backbone(*_a					)





				@unittest.skip("""Swin does not use inputs_embeds"""					)
				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							pass





				@unittest.skip("""Swin does not support feedforward chunking"""					)
				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							pass





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase ,  lowerCamelCase      =   self.model_tester.prepare_config_and_inputs_for_common()

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





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase ,  lowerCamelCase      =   self.model_tester.prepare_config_and_inputs_for_common()

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

										lowerCamelCase      =   ["""pixel_values"""]
										self.assertListEqual(arg_names[:1]						,					_a					)





				@unittest.skip(reason="""MaskFormerSwin is only used as backbone and doesn't support output_attentions"""					)
				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							pass





				@unittest.skip(reason="""MaskFormerSwin is only used as an internal backbone"""					)
				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							pass





				def   _lowerCAmelCase							(			self						,					_a						,					_a						,					_a						,					_a					):





							"""simple docstring"""
							lowerCamelCase      =   model_class(_a					)
							model.to(_a					)
							model.eval()

							with torch.no_grad():
										lowerCamelCase      =   model(**self._prepare_for_class(_a						,					_a					)					)

							lowerCamelCase      =   outputs.hidden_states

							lowerCamelCase      =   getattr(
							    self.model_tester						,					"""expected_num_hidden_layers"""						,					len(self.model_tester.depths					) + 1					)
							self.assertEqual(len(_a					)						,					_a					)

							# Swin has a different seq_length
							lowerCamelCase      =   (
							    config.patch_size
							    if isinstance(config.patch_size						,					collections.abc.Iterable					)
							    else (config.patch_size, config.patch_size)
							)

							lowerCamelCase      =   (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   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase ,  lowerCamelCase      =   self.model_tester.prepare_config_and_inputs_for_common()

							lowerCamelCase      =   (
							    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:
										lowerCamelCase      =   True
										self.check_hidden_states_output(_a						,					_a						,					_a						,					_a					)

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

										self.check_hidden_states_output(_a						,					_a						,					_a						,					_a					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase ,  lowerCamelCase      =   self.model_tester.prepare_config_and_inputs_for_common()
							lowerCamelCase      =   3

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

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

							for model_class in self.all_model_classes:
										lowerCamelCase      =   True
										self.check_hidden_states_output(_a						,					_a						,					_a						,					(padded_height, padded_width)					)

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





				@unittest.skip(reason="""MaskFormerSwin doesn't have pretrained checkpoints"""					)
				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							pass





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





							"""simple docstring"""
							pass





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





							"""simple docstring"""
							pass





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase ,  lowerCamelCase      =   self.model_tester.prepare_config_and_inputs_for_common()

							def set_nan_tensor_to_zero(_a					):
										lowerCamelCase      =   0
										return t

							def check_equivalence(_a						,					_a						,					_a						,					_a={}					):
										with torch.no_grad():
													lowerCamelCase      =   model(**_a						,					return_dict=_a						,					**_a					)
													lowerCamelCase      =   model(**_a						,					return_dict=_a						,					**_a					).to_tuple()

													def recursive_check(_a						,					_a					):
																if isinstance(_a						,					(List, Tuple)					):
																			for tuple_iterable_value, dict_iterable_value in zip(_a						,					_a					):
																						recursive_check(_a						,					_a					)
																elif isinstance(_a						,					_a					):
																			for tuple_iterable_value, dict_iterable_value in zip(
																			    tuple_object.values()						,					dict_object.values()					):
																						recursive_check(_a						,					_a					)
																elif tuple_object is None:
																			return
																else:
																			self.assertTrue(
																			    torch.allclose(
																			        set_nan_tensor_to_zero(_a					)						,					set_nan_tensor_to_zero(_a					)						,					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(_a					).any()} and `inf`: {torch.isinf(_a					)}. Dict has'
																			        f' `nan`: {torch.isnan(_a					).any()} and `inf`: {torch.isinf(_a					)}.'
																			    )						,					)

													recursive_check(_a						,					_a					)

							for model_class in self.all_model_classes:
										lowerCamelCase      =   model_class(_a					)
										model.to(_a					)
										model.eval()

										lowerCamelCase      =   self._prepare_for_class(_a						,					_a					)
										lowerCamelCase      =   self._prepare_for_class(_a						,					_a					)
										check_equivalence(_a						,					_a						,					_a					)

										lowerCamelCase      =   self._prepare_for_class(_a						,					_a						,					return_labels=_a					)
										lowerCamelCase      =   self._prepare_for_class(_a						,					_a						,					return_labels=_a					)
										check_equivalence(_a						,					_a						,					_a					)

										lowerCamelCase      =   self._prepare_for_class(_a						,					_a					)
										lowerCamelCase      =   self._prepare_for_class(_a						,					_a					)
										check_equivalence(_a						,					_a						,					_a						,					{"""output_hidden_states""": True}					)

										lowerCamelCase      =   self._prepare_for_class(_a						,					_a						,					return_labels=_a					)
										lowerCamelCase      =   self._prepare_for_class(_a						,					_a						,					return_labels=_a					)
										check_equivalence(_a						,					_a						,					_a						,					{"""output_hidden_states""": True}					)






@require_torch
class        __magic_name__				(			unittest.TestCase		,   UpperCAmelCase__							):






				'''simple docstring'''
				__UpperCamelCase         =		(MaskFormerSwinBackbone,) if is_torch_available() else ()
				__UpperCamelCase         =		MaskFormerSwinConfig





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   MaskFormerSwinModelTester(self					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase ,  lowerCamelCase      =   self.model_tester.prepare_config_and_inputs_for_common()
							lowerCamelCase      =   inputs_dict["""pixel_values"""].shape[0]

							for backbone_class in self.all_model_classes:
										lowerCamelCase      =   backbone_class(_a					)
										backbone.to(_a					)
										backbone.eval()

										lowerCamelCase      =   backbone(**_a					)

										# Test default outputs and verify feature maps
										self.assertIsInstance(outputs.feature_maps						,					_a					)
										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
										lowerCamelCase      =   backbone(**_a						,					output_hidden_states=_a					)
										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)
																lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase      =   hidden_state.shape
																self.assertTrue((h_batch_size, h_n_channels)						,					(batch_size, n_channels)					)

            # Test output_attentions=True
										if self.has_attentions:
													lowerCamelCase      =   backbone(**_a						,					output_attentions=_a					)
													self.assertIsNotNone(outputs.attentions					)



