






"""simple docstring"""
import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_flax_cross_test,
    require_flax,
    require_torch,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import is_flax_available, is_torch_available, is_vision_available

from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_flax_bert import FlaxBertModelTester
from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester
from ..vit.test_modeling_flax_vit import FlaxViTModelTester


if is_flax_available():
			from transformers import (
			    FlaxBertModel,
			    FlaxCLIPVisionModel,
			    FlaxVisionTextDualEncoderModel,
			    FlaxViTModel,
			    VisionTextDualEncoderConfig,
			    VisionTextDualEncoderProcessor,
			)
			from transformers.modeling_flax_pytorch_utils import (
			    convert_pytorch_state_dict_to_flax,
			    load_flax_weights_in_pytorch_model,
			)


if is_torch_available():
			import torch

			from transformers import VisionTextDualEncoderModel

if is_vision_available():
			from PIL import Image

def 							snake_case (  A__		):
			if isinstance(A__      ,collections.abc.Iterable		):
						return x
			return (x, x)







@require_flax
class      UpperCamelCase_			:





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[int]      ,      lowerCAmelCase_   :					str      ,      lowerCAmelCase_   :					Union[str, Any]		)		->     Optional[Any]:
				pass





	def 	_SCREAMING_SNAKE_CASE ( self   :					Dict		)		->     List[str]:
				pass





	def 	_SCREAMING_SNAKE_CASE ( self   :					str		)		->     Dict:
				pass





	def 	_SCREAMING_SNAKE_CASE ( self   :					Any      ,      lowerCAmelCase_   :					np.ndarray      ,      lowerCAmelCase_   :					np.ndarray      ,      lowerCAmelCase_   :					float		)		->     Dict:
				UpperCAmelCase_	:					Tuple     	=   np.abs((a - b)		).max()
				self.assertLessEqual(lowerCAmelCase_      ,      lowerCAmelCase_      ,      f"""Difference between torch and flax is {diff} (>= {tol})."""		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					int      ,      lowerCAmelCase_   :					Union[str, Any]      ,      lowerCAmelCase_   :					Optional[int]      ,      lowerCAmelCase_   :					List[str]      ,      lowerCAmelCase_   :					str      ,      lowerCAmelCase_   :					int=None      ,      **lowerCAmelCase_   :					str		)		->     List[Any]:
				UpperCAmelCase_	:					Tuple     	=   VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase_      ,      lowerCAmelCase_		)

				UpperCAmelCase_	:					List[Any]     	=   FlaxVisionTextDualEncoderModel(lowerCAmelCase_		)

				UpperCAmelCase_	:					List[Any]     	=   model(input_ids=lowerCAmelCase_      ,      pixel_values=lowerCAmelCase_      ,      attention_mask=lowerCAmelCase_		)

				self.assertEqual(output["text_embeds"].shape      ,      (input_ids.shape[0], config.projection_dim)		)
				self.assertEqual(output["image_embeds"].shape      ,      (pixel_values.shape[0], config.projection_dim)		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					str      ,      lowerCAmelCase_   :					int      ,      lowerCAmelCase_   :					str      ,      lowerCAmelCase_   :					Tuple      ,      lowerCAmelCase_   :					Union[str, Any]      ,      lowerCAmelCase_   :					Optional[int]=None      ,      **lowerCAmelCase_   :					List[str]		)		->     Dict:
				UpperCAmelCase_	,      UpperCAmelCase_	:					Dict     	=   self.get_vision_text_model(lowerCAmelCase_      ,      lowerCAmelCase_		)
				UpperCAmelCase_	:					Optional[Any]     	=   {"vision_model": vision_model, "text_model": text_model}
				UpperCAmelCase_	:					str     	=   FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase_		)

				UpperCAmelCase_	:					Tuple     	=   model(input_ids=lowerCAmelCase_      ,      pixel_values=lowerCAmelCase_      ,      attention_mask=lowerCAmelCase_		)

				self.assertEqual(output["text_embeds"].shape      ,      (input_ids.shape[0], model.config.projection_dim)		)
				self.assertEqual(output["image_embeds"].shape      ,      (pixel_values.shape[0], model.config.projection_dim)		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[int]      ,      lowerCAmelCase_   :					Optional[Any]      ,      lowerCAmelCase_   :					Dict      ,      lowerCAmelCase_   :					Tuple      ,      lowerCAmelCase_   :					Optional[Any]      ,      lowerCAmelCase_   :					str=None      ,      **lowerCAmelCase_   :					Optional[int]		)		->     Tuple:
				UpperCAmelCase_	,      UpperCAmelCase_	:					str     	=   self.get_vision_text_model(lowerCAmelCase_      ,      lowerCAmelCase_		)
				UpperCAmelCase_	:					Tuple     	=   {"vision_model": vision_model, "text_model": text_model}
				UpperCAmelCase_	:					Optional[int]     	=   FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase_		)

				UpperCAmelCase_	:					List[str]     	=   model(input_ids=lowerCAmelCase_      ,      pixel_values=lowerCAmelCase_      ,      attention_mask=lowerCAmelCase_		)
				UpperCAmelCase_	:					List[str]     	=   output[0]

				with tempfile.TemporaryDirectory() as tmpdirname:
							model.save_pretrained(lowerCAmelCase_		)
							UpperCAmelCase_	:					Optional[int]     	=   FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase_		)

							UpperCAmelCase_	:					Dict     	=   model(input_ids=lowerCAmelCase_      ,      pixel_values=lowerCAmelCase_      ,      attention_mask=lowerCAmelCase_		)
							UpperCAmelCase_	:					Union[str, Any]     	=   after_output[0]
							UpperCAmelCase_	:					Any     	=   np.amax(np.abs(out_a - out_a		)		)
							self.assertLessEqual(lowerCAmelCase_      ,      1e-3		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					str      ,      lowerCAmelCase_   :					List[str]      ,      lowerCAmelCase_   :					Dict      ,      lowerCAmelCase_   :					Union[str, Any]      ,      lowerCAmelCase_   :					Tuple      ,      lowerCAmelCase_   :					str=None      ,      **lowerCAmelCase_   :					Union[str, Any]		)		->     Dict:
				UpperCAmelCase_	,      UpperCAmelCase_	:					Optional[int]     	=   self.get_vision_text_model(lowerCAmelCase_      ,      lowerCAmelCase_		)
				UpperCAmelCase_	:					List[str]     	=   {"vision_model": vision_model, "text_model": text_model}
				UpperCAmelCase_	:					Any     	=   FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase_		)

				UpperCAmelCase_	:					List[Any]     	=   model(
				    input_ids=lowerCAmelCase_      ,      pixel_values=lowerCAmelCase_      ,      attention_mask=lowerCAmelCase_      ,      output_attentions=lowerCAmelCase_		)

				UpperCAmelCase_	:					List[str]     	=   output.vision_model_output.attentions
				self.assertEqual(len(lowerCAmelCase_		)      ,      vision_config.num_hidden_layers		)

				# in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
				UpperCAmelCase_	:					str     	=   to_atuple(vision_model.config.image_size		)
				UpperCAmelCase_	:					Optional[Any]     	=   to_atuple(vision_model.config.patch_size		)
				UpperCAmelCase_	:					Optional[Any]     	=   (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
				UpperCAmelCase_	:					Dict     	=   num_patches + 1
				self.assertEqual(vision_attentions[0].shape[-3:]      ,      (vision_config.num_attention_heads, seq_len, seq_len)		)

				UpperCAmelCase_	:					List[str]     	=   output.text_model_output.attentions
				self.assertEqual(len(lowerCAmelCase_		)      ,      text_config.num_hidden_layers		)

				self.assertEqual(
				    text_attentions[0].shape[-3:]      ,      (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])      ,      )





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[Any]      ,      lowerCAmelCase_   :					List[Any]      ,      lowerCAmelCase_   :					List[Any]      ,      lowerCAmelCase_   :					Dict		)		->     str:
				pt_model.to(lowerCAmelCase_		)
				pt_model.eval()

				# prepare inputs
				UpperCAmelCase_	:					Dict     	=   inputs_dict
				UpperCAmelCase_	:					List[str]     	=   {k: torch.tensor(v.tolist()		) for k, v in flax_inputs.items()}

				with torch.no_grad():
							UpperCAmelCase_	:					int     	=   pt_model(**lowerCAmelCase_		).to_tuple()

				UpperCAmelCase_	:					int     	=   fx_model(**lowerCAmelCase_		).to_tuple()
				self.assertEqual(len(lowerCAmelCase_		)      ,      len(lowerCAmelCase_		)      ,      "Output lengths differ between Flax and PyTorch"		)
				for fx_output, pt_output in zip(fx_outputs[:4]      ,      pt_outputs[:4]		):
							self.assert_almost_equals(lowerCAmelCase_      ,      pt_output.numpy()      ,      4e-2		)

				# PT -> Flax
				with tempfile.TemporaryDirectory() as tmpdirname:
							pt_model.save_pretrained(lowerCAmelCase_		)
							UpperCAmelCase_	:					List[Any]     	=   FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase_      ,      from_pt=lowerCAmelCase_		)

				UpperCAmelCase_	:					Optional[Any]     	=   fx_model_loaded(**lowerCAmelCase_		).to_tuple()
				self.assertEqual(len(lowerCAmelCase_		)      ,      len(lowerCAmelCase_		)      ,      "Output lengths differ between Flax and PyTorch"		)
				for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4]      ,      pt_outputs[:4]		):
							self.assert_almost_equals(lowerCAmelCase_      ,      pt_output.numpy()      ,      4e-2		)

				# Flax -> PT
				with tempfile.TemporaryDirectory() as tmpdirname:
							fx_model.save_pretrained(lowerCAmelCase_		)
							UpperCAmelCase_	:					Optional[Any]     	=   VisionTextDualEncoderModel.from_pretrained(lowerCAmelCase_      ,      from_flax=lowerCAmelCase_		)

				pt_model_loaded.to(lowerCAmelCase_		)
				pt_model_loaded.eval()

				with torch.no_grad():
							UpperCAmelCase_	:					Tuple     	=   pt_model_loaded(**lowerCAmelCase_		).to_tuple()

				self.assertEqual(len(lowerCAmelCase_		)      ,      len(lowerCAmelCase_		)      ,      "Output lengths differ between Flax and PyTorch"		)
				for fx_output, pt_output_loaded in zip(fx_outputs[:4]      ,      pt_outputs_loaded[:4]		):
							self.assert_almost_equals(lowerCAmelCase_      ,      pt_output_loaded.numpy()      ,      4e-2		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Tuple      ,      lowerCAmelCase_   :					Optional[Any]      ,      lowerCAmelCase_   :					List[Any]      ,      lowerCAmelCase_   :					Any		)		->     Any:
				UpperCAmelCase_	:					Union[str, Any]     	=   VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase_      ,      lowerCAmelCase_		)

				UpperCAmelCase_	:					List[Any]     	=   VisionTextDualEncoderModel(lowerCAmelCase_		)
				UpperCAmelCase_	:					Dict     	=   FlaxVisionTextDualEncoderModel(lowerCAmelCase_		)

				UpperCAmelCase_	:					Any     	=   convert_pytorch_state_dict_to_flax(pt_model.state_dict()      ,      lowerCAmelCase_		)
				UpperCAmelCase_	:					Dict     	=   fx_state

				self.check_pt_flax_equivalence(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lowerCAmelCase_		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					List[Any]      ,      lowerCAmelCase_   :					List[str]      ,      lowerCAmelCase_   :					Any      ,      lowerCAmelCase_   :					Optional[int]		)		->     Tuple:
				UpperCAmelCase_	:					str     	=   VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase_      ,      lowerCAmelCase_		)

				UpperCAmelCase_	:					List[Any]     	=   VisionTextDualEncoderModel(lowerCAmelCase_		)
				UpperCAmelCase_	:					Dict     	=   FlaxVisionTextDualEncoderModel(lowerCAmelCase_		)

				UpperCAmelCase_	:					List[Any]     	=   load_flax_weights_in_pytorch_model(lowerCAmelCase_      ,      fx_model.params		)

				self.check_pt_flax_equivalence(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lowerCAmelCase_		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Union[str, Any]		)		->     Any:
				UpperCAmelCase_	:					str     	=   self.prepare_config_and_inputs()
				self.check_model_from_pretrained_configs(**lowerCAmelCase_		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[int]		)		->     Optional[Any]:
				UpperCAmelCase_	:					Union[str, Any]     	=   self.prepare_config_and_inputs()
				self.check_vision_text_dual_encoder_from_pretrained(**lowerCAmelCase_		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Union[str, Any]		)		->     List[str]:
				UpperCAmelCase_	:					Optional[Any]     	=   self.prepare_config_and_inputs()
				self.check_save_load(**lowerCAmelCase_		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[Any]		)		->     int:
				UpperCAmelCase_	:					str     	=   self.prepare_config_and_inputs()
				self.check_vision_text_output_attention(**lowerCAmelCase_		)





	@is_pt_flax_cross_test
	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[int]		)		->     str:
				UpperCAmelCase_	:					int     	=   self.prepare_config_and_inputs()
				UpperCAmelCase_	:					int     	=   config_inputs_dict.pop("vision_config"		)
				UpperCAmelCase_	:					int     	=   config_inputs_dict.pop("text_config"		)

				UpperCAmelCase_	:					Optional[Any]     	=   config_inputs_dict

				self.check_equivalence_pt_to_flax(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lowerCAmelCase_		)
				self.check_equivalence_flax_to_pt(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lowerCAmelCase_		)





	@slow
	def 	_SCREAMING_SNAKE_CASE ( self   :					Dict		)		->     Optional[Any]:
				UpperCAmelCase_	,      UpperCAmelCase_	:					int     	=   self.get_pretrained_model_and_inputs()

				UpperCAmelCase_	:					List[Any]     	=   model_a(**lowerCAmelCase_		)
				UpperCAmelCase_	:					int     	=   outputs[0]

				with tempfile.TemporaryDirectory() as tmp_dirname:
							model_a.save_pretrained(lowerCAmelCase_		)
							UpperCAmelCase_	:					List[str]     	=   FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase_		)

							UpperCAmelCase_	:					Any     	=   model_a(**lowerCAmelCase_		)
							UpperCAmelCase_	:					Optional[int]     	=   after_outputs[0]
							UpperCAmelCase_	:					int     	=   np.amax(np.abs(out_a - out_a		)		)
							self.assertLessEqual(lowerCAmelCase_      ,      1e-5		)







@require_flax
class      UpperCamelCase_			(__A							,		unittest.TestCase	):





	def 	_SCREAMING_SNAKE_CASE ( self   :					Dict		)		->     List[Any]:
				UpperCAmelCase_	:					Optional[Any]     	=   FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
				    "hf-internal-testing/tiny-random-vit"      ,      "hf-internal-testing/tiny-bert"      ,      vision_from_pt=lowerCAmelCase_      ,      text_from_pt=lowerCAmelCase_      ,      )
				UpperCAmelCase_	:					List[Any]     	=   13
				UpperCAmelCase_	:					Optional[Any]     	=   floats_tensor(
				    [
				        batch_size,
				        model.config.vision_config.num_channels,
				        model.config.vision_config.image_size,
				        model.config.vision_config.image_size,
				    ]		)
				UpperCAmelCase_	:					Tuple     	=   ids_tensor([batch_size, 4]      ,      model.config.text_config.vocab_size		)
				UpperCAmelCase_	:					Optional[Any]     	=   random_attention_mask([batch_size, 4]		)
				UpperCAmelCase_	:					str     	=   {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

				return model, inputs





	def 	_SCREAMING_SNAKE_CASE ( self   :					Any      ,      lowerCAmelCase_   :					List[str]      ,      lowerCAmelCase_   :					Optional[int]		)		->     List[str]:
				UpperCAmelCase_	:					List[str]     	=   FlaxViTModel(lowerCAmelCase_		)
				UpperCAmelCase_	:					Any     	=   FlaxBertModel(lowerCAmelCase_		)
				return vision_model, text_model





	def 	_SCREAMING_SNAKE_CASE ( self   :					int		)		->     List[Any]:
				UpperCAmelCase_	:					Union[str, Any]     	=   FlaxViTModelTester(self		)
				UpperCAmelCase_	:					Optional[int]     	=   FlaxBertModelTester(self		)
				UpperCAmelCase_	:					Any     	=   vit_model_tester.prepare_config_and_inputs()
				UpperCAmelCase_	:					Optional[Any]     	=   bert_model_tester.prepare_config_and_inputs()

				UpperCAmelCase_	,      UpperCAmelCase_	:					Any     	=   vision_config_and_inputs

				UpperCAmelCase_	,      UpperCAmelCase_	,      UpperCAmelCase_	,      UpperCAmelCase_	:					int     	=   text_config_and_inputs

				# make sure that cross attention layers are added
				return {
				    "text_config": text_config,
				    "vision_config": vision_config,
				    "pixel_values": pixel_values,
				    "attention_mask": attention_mask,
				    "input_ids": input_ids,
				    "token_type_ids": token_type_ids,
				}







@require_torch
class      UpperCamelCase_			(__A							,		unittest.TestCase	):





	def 	_SCREAMING_SNAKE_CASE ( self   :					str		)		->     Union[str, Any]:
				UpperCAmelCase_	:					Union[str, Any]     	=   FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
				    "hf-internal-testing/tiny-random-clip"      ,      "hf-internal-testing/tiny-bert"      ,      vision_from_pt=lowerCAmelCase_      ,      text_from_pt=lowerCAmelCase_      ,      )
				UpperCAmelCase_	:					Any     	=   13
				UpperCAmelCase_	:					Union[str, Any]     	=   floats_tensor(
				    [
				        batch_size,
				        model.config.vision_config.num_channels,
				        model.config.vision_config.image_size,
				        model.config.vision_config.image_size,
				    ]		)
				UpperCAmelCase_	:					Optional[int]     	=   ids_tensor([batch_size, 4]      ,      model.config.text_config.vocab_size		)
				UpperCAmelCase_	:					int     	=   random_attention_mask([batch_size, 4]		)
				UpperCAmelCase_	:					int     	=   {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

				return model, inputs





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[Any]      ,      lowerCAmelCase_   :					List[Any]      ,      lowerCAmelCase_   :					str		)		->     str:
				UpperCAmelCase_	:					Tuple     	=   FlaxCLIPVisionModel(lowerCAmelCase_		)
				UpperCAmelCase_	:					Optional[Any]     	=   FlaxBertModel(lowerCAmelCase_		)
				return vision_model, text_model





	def 	_SCREAMING_SNAKE_CASE ( self   :					Tuple		)		->     int:
				UpperCAmelCase_	:					List[Any]     	=   FlaxCLIPVisionModelTester(self		)
				UpperCAmelCase_	:					Union[str, Any]     	=   FlaxBertModelTester(self		)
				UpperCAmelCase_	:					List[str]     	=   clip_model_tester.prepare_config_and_inputs()
				UpperCAmelCase_	:					Union[str, Any]     	=   bert_model_tester.prepare_config_and_inputs()

				UpperCAmelCase_	,      UpperCAmelCase_	:					Any     	=   vision_config_and_inputs

				UpperCAmelCase_	,      UpperCAmelCase_	,      UpperCAmelCase_	,      UpperCAmelCase_	:					Dict     	=   text_config_and_inputs

				# make sure that cross attention layers are added
				return {
				    "text_config": text_config,
				    "vision_config": vision_config,
				    "pixel_values": pixel_values,
				    "attention_mask": attention_mask,
				    "input_ids": input_ids,
				    "token_type_ids": token_type_ids,
				}







@require_flax
@require_vision
class      UpperCamelCase_			(unittest.TestCase	):





	@slow
	def 	_SCREAMING_SNAKE_CASE ( self   :					List[str]		)		->     Union[str, Any]:
				UpperCAmelCase_	:					Any     	=   FlaxVisionTextDualEncoderModel.from_pretrained("clip-italian/clip-italian"      ,      logit_scale_init_value=1.0		)
				UpperCAmelCase_	:					Tuple     	=   VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian"		)

				UpperCAmelCase_	:					Optional[Any]     	=   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
				UpperCAmelCase_	:					int     	=   processor(
				    text=["una foto di un gatto", "una foto di un cane"]      ,      images=lowerCAmelCase_      ,      padding=lowerCAmelCase_      ,      return_tensors="np"		)

				UpperCAmelCase_	:					Optional[int]     	=   model(**lowerCAmelCase_		)

				# verify the logits
				self.assertEqual(outputs.logits_per_image.shape      ,      (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])		)
				self.assertEqual(
				    outputs.logits_per_text.shape      ,      (inputs.input_ids.shape[0], inputs.pixel_values.shape[0])      ,      )

				UpperCAmelCase_	:					List[str]     	=   np.array([[1.2_2_8_4_7_2_7, 0.3_1_0_4_1_2_2]]		)

				self.assertTrue(np.allclose(outputs.logits_per_image      ,      lowerCAmelCase_      ,      atol=1e-3		)		)








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


