






import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

		if is_vision_available():
				from transformers import MaskFormerImageProcessor

if is_vision_available():
		from PIL import Image







class    A_	:
							"""simple docstring"""




							def __init__(		self			:Tuple       ,							lowerCAmelCase__			:Optional[int]       ,							lowerCAmelCase__			:Any=2       ,							lowerCAmelCase__			:Optional[int]=True       ,							lowerCAmelCase__			:Union[str, Any]=False       ,							lowerCAmelCase__			:Optional[int]=10       ,							lowerCAmelCase__			:Tuple=3       ,							lowerCAmelCase__			:int=32 * 4       ,							lowerCAmelCase__			:Optional[Any]=32 * 6       ,							lowerCAmelCase__			:Tuple=4       ,							lowerCAmelCase__			:Union[str, Any]=32       ,							)				->  Any:


										'''simple docstring'''




										snake_case_				:					List[str]   =       parent
										snake_case_				:					str   =       batch_size
										snake_case_				:					List[str]   =       is_training
										snake_case_				:					Tuple   =       use_auxiliary_loss
										snake_case_				:					Optional[Any]   =       num_queries
										snake_case_				:					Optional[int]   =       num_channels
										snake_case_				:					Union[str, Any]   =       min_size
										snake_case_				:					Any   =       max_size
										snake_case_				:					Any   =       num_labels
										snake_case_				:					Union[str, Any]   =       mask_feature_size

							def 							_A	(		self			:Tuple				)				->  Dict:


										'''simple docstring'''




										snake_case_				:					Any   =       floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]				).to(
										    lowerCAmelCase__				)

										snake_case_				:					Dict   =       torch.ones([self.batch_size, self.min_size, self.max_size]       ,							device=lowerCAmelCase__				)

										snake_case_				:					int   =       (
										    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]       ,							device=lowerCAmelCase__				) > 0.5
										).float()
										snake_case_				:					List[Any]   =       (torch.rand((self.batch_size, self.num_labels)       ,							device=lowerCAmelCase__				) > 0.5).long()

										snake_case_				:					Optional[int]   =       self.get_config()
										return config, pixel_values, pixel_mask, mask_labels, class_labels

							def 							_A	(		self			:Tuple				)				->  Any:


										'''simple docstring'''




										return MaskFormerConfig.from_backbone_and_decoder_configs(
										    backbone_config=SwinConfig(
										        depths=[1, 1, 1, 1]       ,							)       ,							decoder_config=DetrConfig(
										        decoder_ffn_dim=128       ,							num_queries=self.num_queries       ,							decoder_attention_heads=2       ,							d_model=self.mask_feature_size       ,							)       ,							mask_feature_size=self.mask_feature_size       ,							fpn_feature_size=self.mask_feature_size       ,							num_channels=self.num_channels       ,							num_labels=self.num_labels       ,							)

							def 							_A	(		self			:Optional[int]				)				->  List[Any]:


										'''simple docstring'''




										snake_case_				:					Union[str, Any]   =       self.prepare_config_and_inputs()
										snake_case_				:					str   =       {'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
										return config, inputs_dict

							def 							_A	(		self			:Union[str, Any]       ,							lowerCAmelCase__			:Tuple       ,							lowerCAmelCase__			:List[Any]				)				->  List[str]:


										'''simple docstring'''




										snake_case_				:					Tuple   =       output.encoder_hidden_states
										snake_case_				:					str   =       output.pixel_decoder_hidden_states
										snake_case_				:					Tuple   =       output.transformer_decoder_hidden_states

										self.parent.assertTrue(len(lowerCAmelCase__				)       ,							len(config.backbone_config.depths				)				)
										self.parent.assertTrue(len(lowerCAmelCase__				)       ,							len(config.backbone_config.depths				)				)
										self.parent.assertTrue(len(lowerCAmelCase__				)       ,							config.decoder_config.decoder_layers				)

							def 							_A	(		self			:Any       ,							lowerCAmelCase__			:int       ,							lowerCAmelCase__			:str       ,							lowerCAmelCase__			:List[Any]       ,							lowerCAmelCase__			:Optional[int]=False				)				->  Optional[Any]:


										'''simple docstring'''




										with torch.no_grad():
													snake_case_				:					str   =       MaskFormerModel(config=lowerCAmelCase__				)
													model.to(lowerCAmelCase__				)
													model.eval()

													snake_case_				:					Dict   =       model(pixel_values=lowerCAmelCase__       ,							pixel_mask=lowerCAmelCase__				)
													snake_case_				:					int   =       model(lowerCAmelCase__       ,							output_hidden_states=lowerCAmelCase__				)
										# the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
										# encoder and pixel decoder
										self.parent.assertEqual(
										    output.transformer_decoder_last_hidden_state.shape       ,							(self.batch_size, self.num_queries, self.mask_feature_size)       ,							)
										# let's ensure the other two hidden state exists
										self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None				)
										self.parent.assertTrue(output.encoder_last_hidden_state is not None				)

										if output_hidden_states:
													self.check_output_hidden_state(lowerCAmelCase__       ,							lowerCAmelCase__				)






							def 							_A	(		self			:str       ,							lowerCAmelCase__			:Dict       ,							lowerCAmelCase__			:int       ,							lowerCAmelCase__			:Any       ,							lowerCAmelCase__			:Optional[Any]       ,							lowerCAmelCase__			:Tuple				)				->  List[Any]:


										'''simple docstring'''




										snake_case_				:					List[Any]   =       MaskFormerForInstanceSegmentation(config=lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()

										def comm_check_on_output(lowerCAmelCase__			:int				):
													# let's still check that all the required stuff is there
													self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None				)
													self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None				)
													self.parent.assertTrue(result.encoder_last_hidden_state is not None				)
													# okay, now we need to check the logits shape
													# due to the encoder compression, masks have a //4 spatial size
													self.parent.assertEqual(
													    result.masks_queries_logits.shape       ,							(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)       ,							)
													# + 1 for null class
													self.parent.assertEqual(
													    result.class_queries_logits.shape       ,							(self.batch_size, self.num_queries, self.num_labels + 1)				)

										with torch.no_grad():
													snake_case_				:					str   =       model(pixel_values=lowerCAmelCase__       ,							pixel_mask=lowerCAmelCase__				)
													snake_case_				:					Union[str, Any]   =       model(lowerCAmelCase__				)

													comm_check_on_output(lowerCAmelCase__				)

													snake_case_				:					List[str]   =       model(
													    pixel_values=lowerCAmelCase__       ,							pixel_mask=lowerCAmelCase__       ,							mask_labels=lowerCAmelCase__       ,							class_labels=lowerCAmelCase__				)

										comm_check_on_output(lowerCAmelCase__				)

										self.parent.assertTrue(result.loss is not None				)
										self.parent.assertEqual(result.loss.shape       ,							torch.Size([1]				)				)







@require_torch
class    A_	(UpperCamelCase_						,					UpperCamelCase_						,					unittest.TestCase							):
							"""simple docstring"""




							a__   		=	(MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
							a__   		=	(
							    {'''feature-extraction''': MaskFormerModel, '''image-segmentation''': MaskFormerForInstanceSegmentation}
							    if is_torch_available()
							    else {}
							)

							a__   		=	False
							a__   		=	False
							a__   		=	False
							a__   		=	False

							def 							_A	(		self			:List[str]				)				->  List[str]:


										'''simple docstring'''




										snake_case_				:					Tuple   =       MaskFormerModelTester(self				)
										snake_case_				:					Dict   =       ConfigTester(self       ,							config_class=lowerCAmelCase__       ,							has_text_modality=lowerCAmelCase__				)

							def 							_A	(		self			:Union[str, Any]				)				->  List[Any]:


										'''simple docstring'''




										self.config_tester.run_common_tests()

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


										'''simple docstring'''




										snake_case_				:					List[Any]   =       self.model_tester.prepare_config_and_inputs_for_common()
										self.model_tester.create_and_check_maskformer_model(lowerCAmelCase__       ,							**lowerCAmelCase__       ,							output_hidden_states=lowerCAmelCase__				)

							def 							_A	(		self			:str				)				->  List[Any]:


										'''simple docstring'''




										snake_case_				:					int   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*lowerCAmelCase__				)

							@unittest.skip(reason="MaskFormer does not use inputs_embeds"				)
							def 							_A	(		self			:Optional[int]				)				->  Optional[int]:


										'''simple docstring'''




										pass

							@unittest.skip(reason="MaskFormer does not have a get_input_embeddings method"				)
							def 							_A	(		self			:Optional[Any]				)				->  Union[str, Any]:


										'''simple docstring'''




										pass

							@unittest.skip(reason="MaskFormer is not a generative model"				)
							def 							_A	(		self			:Union[str, Any]				)				->  int:


										'''simple docstring'''




										pass

							@unittest.skip(reason="MaskFormer does not use token embeddings"				)
							def 							_A	(		self			:Optional[int]				)				->  Optional[Any]:


										'''simple docstring'''




										pass

							@require_torch_multi_gpu
							@unittest.skip(
							    reason="MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`"				)
							def 							_A	(		self			:Union[str, Any]				)				->  Dict:


										'''simple docstring'''




										pass

							@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."				)
							def 							_A	(		self			:Optional[Any]				)				->  Optional[int]:


										'''simple docstring'''




										pass

							def 							_A	(		self			:Dict				)				->  Optional[Any]:


										'''simple docstring'''




										snake_case_				:					Optional[int]   =       self.model_tester.prepare_config_and_inputs_for_common()

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

													snake_case_				:					int   =       ['''pixel_values''']
													self.assertListEqual(arg_names[:1]       ,							lowerCAmelCase__				)

							@slow
							def 							_A	(		self			:str				)				->  List[Any]:


										'''simple docstring'''




										for model_name in ["facebook/maskformer-swin-small-coco"]:
													snake_case_				:					Any   =       MaskFormerModel.from_pretrained(lowerCAmelCase__				)
													self.assertIsNotNone(lowerCAmelCase__				)

							def 							_A	(		self			:Optional[int]				)				->  Optional[Any]:


										'''simple docstring'''




										snake_case_				:					Tuple   =       (self.model_tester.min_size,) * 2
										snake_case_				:					int   =       {
										    '''pixel_values''': torch.randn((2, 3, *size)       ,							device=lowerCAmelCase__				),
										    '''mask_labels''': torch.randn((2, 10, *size)       ,							device=lowerCAmelCase__				),
										    '''class_labels''': torch.zeros(2       ,							10       ,							device=lowerCAmelCase__				).long(),
										}

										snake_case_				:					Any   =       MaskFormerForInstanceSegmentation(MaskFormerConfig()				).to(lowerCAmelCase__				)
										snake_case_				:					str   =       model(**lowerCAmelCase__				)
										self.assertTrue(outputs.loss is not None				)

							def 							_A	(		self			:Optional[int]				)				->  Optional[int]:


										'''simple docstring'''




										snake_case_				:					int   =       self.model_tester.prepare_config_and_inputs_for_common()
										self.model_tester.create_and_check_maskformer_model(lowerCAmelCase__       ,							**lowerCAmelCase__       ,							output_hidden_states=lowerCAmelCase__				)

							def 							_A	(		self			:Tuple				)				->  List[Any]:


										'''simple docstring'''




										snake_case_				:					Optional[int]   =       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													snake_case_				:					str   =       model_class(lowerCAmelCase__				).to(lowerCAmelCase__				)
													snake_case_				:					Dict   =       model(**lowerCAmelCase__       ,							output_attentions=lowerCAmelCase__				)
													self.assertTrue(outputs.attentions is not None				)

							def 							_A	(		self			:Tuple				)				->  List[str]:


										'''simple docstring'''




										if not self.model_tester.is_training:
													return
										# only MaskFormerForInstanceSegmentation has the loss
										snake_case_				:					Union[str, Any]   =       self.all_model_classes[1]
										snake_case_				:					Dict   =       self.model_tester.prepare_config_and_inputs()

										snake_case_				:					List[Any]   =       model_class(lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.train()

										snake_case_				:					Union[str, Any]   =       model(lowerCAmelCase__       ,							mask_labels=lowerCAmelCase__       ,							class_labels=lowerCAmelCase__				).loss
										loss.backward()






							def 							_A	(		self			:Tuple				)				->  Optional[int]:


										'''simple docstring'''




										snake_case_				:					Optional[int]   =       self.all_model_classes[1]
										snake_case_				:					Dict   =       self.model_tester.prepare_config_and_inputs()
										snake_case_				:					Dict   =       True
										snake_case_				:					Optional[int]   =       True

										snake_case_				:					Union[str, Any]   =       model_class(lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.train()

										snake_case_				:					int   =       model(lowerCAmelCase__       ,							mask_labels=lowerCAmelCase__       ,							class_labels=lowerCAmelCase__				)

										snake_case_				:					Optional[int]   =       outputs.encoder_hidden_states[0]
										encoder_hidden_states.retain_grad()

										snake_case_				:					List[str]   =       outputs.pixel_decoder_hidden_states[0]
										pixel_decoder_hidden_states.retain_grad()
										# we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
										snake_case_				:					List[str]   =       outputs.transformer_decoder_hidden_states[0]
										transformer_decoder_hidden_states.retain_grad()

										snake_case_				:					Any   =       outputs.attentions[0]
										attentions.retain_grad()

										outputs.loss.backward(retain_graph=lowerCAmelCase__				)

										self.assertIsNotNone(encoder_hidden_states.grad				)
										self.assertIsNotNone(pixel_decoder_hidden_states.grad				)
										self.assertIsNotNone(transformer_decoder_hidden_states.grad				)
										self.assertIsNotNone(attentions.grad				)
__lowerCamelCase   :					int   							=					1E-4

def 	__UpperCAmelCase       ( )->    List[Any]:



			"""simple docstring"""



			snake_case_				:					Tuple   =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
			return image







@require_vision
@slow
class    A_	(unittest.TestCase							):
							"""simple docstring"""




							@cached_property
							def 							_A	(		self			:Union[str, Any]				)				->  List[Any]:


										'''simple docstring'''




										return (
										    MaskFormerImageProcessor.from_pretrained("facebook/maskformer-swin-small-coco"				)
										    if is_vision_available()
										    else None
										)

							def 							_A	(		self			:List[str]				)				->  Tuple:


										'''simple docstring'''




										snake_case_				:					str   =       MaskFormerModel.from_pretrained("facebook/maskformer-swin-small-coco"				).to(lowerCAmelCase__				)
										snake_case_				:					Any   =       self.default_image_processor
										snake_case_				:					Optional[Any]   =       prepare_img()
										snake_case_				:					Dict   =       image_processor(lowerCAmelCase__       ,							return_tensors="pt"				).to(lowerCAmelCase__				)
										snake_case_				:					str   =       inputs['''pixel_values'''].shape
										# check size is divisible by 32
										self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0				)
										# check size
										self.assertEqual(lowerCAmelCase__       ,							(1, 3, 800, 1_088)				)

										with torch.no_grad():
													snake_case_				:					List[Any]   =       model(**lowerCAmelCase__				)

										snake_case_				:					List[Any]   =       torch.tensor(
										    [[-0.0_4_8_2, 0.9_2_2_8, 0.4_9_5_1], [-0.2_5_4_7, 0.8_0_1_7, 0.8_5_2_7], [-0.0_0_6_9, 0.3_3_8_5, -0.0_0_8_9]]				).to(lowerCAmelCase__				)
										self.assertTrue(
										    torch.allclose(
										        outputs.encoder_last_hidden_state[0, 0, :3, :3]       ,							lowerCAmelCase__       ,							atol=lowerCAmelCase__				)				)

										snake_case_				:					Any   =       torch.tensor(
										    [[-0.8_4_2_2, -0.8_4_3_4, -0.9_7_1_8], [-1.0_1_4_4, -0.5_5_6_5, -0.4_1_9_5], [-1.0_0_3_8, -0.4_4_8_4, -0.1_9_6_1]]				).to(lowerCAmelCase__				)
										self.assertTrue(
										    torch.allclose(
										        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]       ,							lowerCAmelCase__       ,							atol=lowerCAmelCase__				)				)

										snake_case_				:					Optional[Any]   =       torch.tensor(
										    [[0.2_8_5_2, -0.0_1_5_9, 0.9_7_3_5], [0.6_2_5_4, 0.1_8_5_8, 0.8_5_2_9], [-0.0_6_8_0, -0.4_1_1_6, 1.8_4_1_3]]				).to(lowerCAmelCase__				)
										self.assertTrue(
										    torch.allclose(
										        outputs.transformer_decoder_last_hidden_state[0, :3, :3]       ,							lowerCAmelCase__       ,							atol=lowerCAmelCase__				)				)

							def 							_A	(		self			:Optional[Any]				)				->  List[Any]:


										'''simple docstring'''




										snake_case_				:					List[Any]   =       (
										    MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco"				)
										    .to(lowerCAmelCase__				)
										    .eval()
										)
										snake_case_				:					List[str]   =       self.default_image_processor
										snake_case_				:					str   =       prepare_img()
										snake_case_				:					Any   =       image_processor(lowerCAmelCase__       ,							return_tensors="pt"				).to(lowerCAmelCase__				)
										snake_case_				:					List[str]   =       inputs['''pixel_values'''].shape
										# check size is divisible by 32
										self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0				)
										# check size
										self.assertEqual(lowerCAmelCase__       ,							(1, 3, 800, 1_088)				)

										with torch.no_grad():
													snake_case_				:					Tuple   =       model(**lowerCAmelCase__				)
										# masks_queries_logits
										snake_case_				:					Tuple   =       outputs.masks_queries_logits
										self.assertEqual(
										    masks_queries_logits.shape       ,							(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)       ,							)
										snake_case_				:					Any   =       [
										    [-1.3_7_3_7_1_2_4, -1.7_7_2_4_9_3_7, -1.9_3_6_4_2_3_3],
										    [-1.5_9_7_7_2_8_1, -1.9_8_6_7_9_3_9, -2.1_5_2_3_6_9_5],
										    [-1.5_7_9_5_3_9_8, -1.9_2_6_9_8_3_2, -2.0_9_3_9_4_2],
										]
										snake_case_				:					Any   =       torch.tensor(lowerCAmelCase__				).to(lowerCAmelCase__				)
										self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]       ,							lowerCAmelCase__       ,							atol=lowerCAmelCase__				)				)
										# class_queries_logits
										snake_case_				:					str   =       outputs.class_queries_logits
										self.assertEqual(
										    class_queries_logits.shape       ,							(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)				)
										snake_case_				:					List[str]   =       torch.tensor(
										    [
										        [1.6_5_1_2E0_0, -5.2_5_7_2E0_0, -3.3_5_1_9E0_0],
										        [3.6_1_6_9E-0_2, -5.9_0_2_5E0_0, -2.9_3_1_3E0_0],
										        [1.0_7_6_6E-0_4, -7.7_6_3_0E0_0, -5.1_2_6_3E0_0],
										    ]				).to(lowerCAmelCase__				)
										self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]       ,							lowerCAmelCase__       ,							atol=lowerCAmelCase__				)				)

							def 							_A	(		self			:Any				)				->  Optional[Any]:


										'''simple docstring'''




										snake_case_				:					Dict   =       (
										    MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-resnet101-coco-stuff"				)
										    .to(lowerCAmelCase__				)
										    .eval()
										)
										snake_case_				:					int   =       self.default_image_processor
										snake_case_				:					Optional[int]   =       prepare_img()
										snake_case_				:					str   =       image_processor(lowerCAmelCase__       ,							return_tensors="pt"				).to(lowerCAmelCase__				)
										snake_case_				:					List[Any]   =       inputs['''pixel_values'''].shape
										# check size is divisible by 32
										self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0				)
										# check size
										self.assertEqual(lowerCAmelCase__       ,							(1, 3, 800, 1_088)				)

										with torch.no_grad():
													snake_case_				:					int   =       model(**lowerCAmelCase__				)
										# masks_queries_logits
										snake_case_				:					int   =       outputs.masks_queries_logits
										self.assertEqual(
										    masks_queries_logits.shape       ,							(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)       ,							)
										snake_case_				:					str   =       [[-0.9_0_4_6, -2.6_3_6_6, -4.6_0_6_2], [-3.4_1_7_9, -5.7_8_9_0, -8.8_0_5_7], [-4.9_1_7_9, -7.6_5_6_0, -1_0.7_7_1_1]]
										snake_case_				:					Optional[int]   =       torch.tensor(lowerCAmelCase__				).to(lowerCAmelCase__				)
										self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]       ,							lowerCAmelCase__       ,							atol=lowerCAmelCase__				)				)
										# class_queries_logits
										snake_case_				:					Union[str, Any]   =       outputs.class_queries_logits
										self.assertEqual(
										    class_queries_logits.shape       ,							(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)				)
										snake_case_				:					List[str]   =       torch.tensor(
										    [[4.7_1_8_8, -3.2_5_8_5, -2.8_8_5_7], [6.6_8_7_1, -2.9_1_8_1, -1.2_4_8_7], [7.2_4_4_9, -2.2_7_6_4, -2.1_8_7_4]]				).to(lowerCAmelCase__				)
										self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]       ,							lowerCAmelCase__       ,							atol=lowerCAmelCase__				)				)






							def 							_A	(		self			:Optional[int]				)				->  str:


										'''simple docstring'''




										snake_case_				:					List[str]   =       (
										    MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco"				)
										    .to(lowerCAmelCase__				)
										    .eval()
										)
										snake_case_				:					Any   =       self.default_image_processor

										snake_case_				:					List[Any]   =       image_processor(
										    [np.zeros((3, 800, 1_333)				), np.zeros((3, 800, 1_333)				)]       ,							segmentation_maps=[np.zeros((384, 384)				).astype(np.floataa				), np.zeros((384, 384)				).astype(np.floataa				)]       ,							return_tensors="pt"       ,							)

										snake_case_				:					Dict   =       inputs['''pixel_values'''].to(lowerCAmelCase__				)
										snake_case_				:					int   =       [el.to(lowerCAmelCase__				) for el in inputs['''mask_labels''']]
										snake_case_				:					int   =       [el.to(lowerCAmelCase__				) for el in inputs['''class_labels''']]

										with torch.no_grad():
													snake_case_				:					List[str]   =       model(**lowerCAmelCase__				)

										self.assertTrue(outputs.loss is not None				)












'''simple docstring'''




import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()







class    A_	(unittest.TestCase							):
							"""simple docstring"""




							def 							_A	(		self			:Any				)				->  str:


										'''simple docstring'''




										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()

							@property
							def 							_A	(		self			:List[Any]				)				->  List[str]:


										'''simple docstring'''




										snake_case_				:					Any   =       1
										snake_case_				:					Dict   =       3
										snake_case_				:					Union[str, Any]   =       (32, 32)

										snake_case_				:					Optional[int]   =       floats_tensor((batch_size, num_channels) + sizes       ,							rng=random.Random(0				)				).to(lowerCAmelCase__				)
										return image

							@property
							def 							_A	(		self			:Optional[int]				)				->  Any:


										'''simple docstring'''




										torch.manual_seed(0				)
										snake_case_				:					List[str]   =       UNetaDConditionModel(
										    block_out_channels=(32, 64)       ,							layers_per_block=2       ,							sample_size=32       ,							in_channels=4       ,							out_channels=4       ,							down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")       ,							up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")       ,							cross_attention_dim=32       ,							)
										return model

							@property
							def 							_A	(		self			:Dict				)				->  Any:


										'''simple docstring'''




										torch.manual_seed(0				)
										snake_case_				:					Optional[Any]   =       AutoencoderKL(
										    block_out_channels=[32, 64]       ,							in_channels=3       ,							out_channels=3       ,							down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]       ,							up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]       ,							latent_channels=4       ,							)
										return model

							@property
							def 							_A	(		self			:Dict				)				->  Optional[int]:


										'''simple docstring'''




										torch.manual_seed(0				)
										snake_case_				:					str   =       RobertaSeriesConfig(
										    hidden_size=32       ,							project_dim=32       ,							intermediate_size=37       ,							layer_norm_eps=1E-0_5       ,							num_attention_heads=4       ,							num_hidden_layers=5       ,							pad_token_id=1       ,							vocab_size=5_006       ,							)
										return RobertaSeriesModelWithTransformation(lowerCAmelCase__				)

							@property
							def 							_A	(		self			:Any				)				->  str:


										'''simple docstring'''




										def extract(*lowerCAmelCase__			:Any       ,							**lowerCAmelCase__			:List[str]				):







													class    A_	:
																				"""simple docstring"""




																				def __init__(		self			:Optional[int]				)				->  List[str]:


																							'''simple docstring'''




																							snake_case_				:					str   =       torch.ones([0]				)






																				def 							_A	(		self			:int       ,							lowerCAmelCase__			:List[Any]				)				->  Tuple:


																							'''simple docstring'''




																							self.pixel_values.to(lowerCAmelCase__				)
																							return self
													return Out()

										return extract

							def 							_A	(		self			:int				)				->  Dict:


										'''simple docstring'''




										snake_case_				:					str   =       "cpu"  # ensure determinism for the device-dependent torch.Generator
										snake_case_				:					str   =       self.dummy_cond_unet
										snake_case_				:					Optional[int]   =       PNDMScheduler(skip_prk_steps=lowerCAmelCase__				)
										snake_case_				:					Dict   =       self.dummy_vae
										snake_case_				:					Dict   =       self.dummy_text_encoder
										snake_case_				:					Optional[int]   =       XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"				)
										snake_case_				:					str   =       77

										snake_case_				:					Any   =       self.dummy_image.to(lowerCAmelCase__				)
										snake_case_				:					Tuple   =       init_image / 2 + 0.5

										# make sure here that pndm scheduler skips prk
										snake_case_				:					Optional[Any]   =       AltDiffusionImgaImgPipeline(
										    unet=lowerCAmelCase__       ,							scheduler=lowerCAmelCase__       ,							vae=lowerCAmelCase__       ,							text_encoder=lowerCAmelCase__       ,							tokenizer=lowerCAmelCase__       ,							safety_checker=lowerCAmelCase__       ,							feature_extractor=self.dummy_extractor       ,							)
										snake_case_				:					Union[str, Any]   =       VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor       ,							do_normalize=lowerCAmelCase__				)
										snake_case_				:					Optional[Any]   =       alt_pipe.to(lowerCAmelCase__				)
										alt_pipe.set_progress_bar_config(disable=lowerCAmelCase__				)

										snake_case_				:					Dict   =       "A painting of a squirrel eating a burger"
										snake_case_				:					List[str]   =       torch.Generator(device=lowerCAmelCase__				).manual_seed(0				)
										snake_case_				:					Dict   =       alt_pipe(
										    [prompt]       ,							generator=lowerCAmelCase__       ,							guidance_scale=6.0       ,							num_inference_steps=2       ,							output_type="np"       ,							image=lowerCAmelCase__       ,							)

										snake_case_				:					Any   =       output.images

										snake_case_				:					List[str]   =       torch.Generator(device=lowerCAmelCase__				).manual_seed(0				)
										snake_case_				:					Optional[Any]   =       alt_pipe(
										    [prompt]       ,							generator=lowerCAmelCase__       ,							guidance_scale=6.0       ,							num_inference_steps=2       ,							output_type="np"       ,							image=lowerCAmelCase__       ,							return_dict=lowerCAmelCase__       ,							)[0]

										snake_case_				:					Tuple   =       image[0, -3:, -3:, -1]
										snake_case_				:					Dict   =       image_from_tuple[0, -3:, -3:, -1]

										assert image.shape == (1, 32, 32, 3)
										snake_case_				:					int   =       np.array([0.4_4_2_7, 0.3_7_3_1, 0.4_2_4_9, 0.4_9_4_1, 0.4_5_4_6, 0.4_1_4_8, 0.4_1_9_3, 0.4_6_6_6, 0.4_4_9_9]				)

										assert np.abs(image_slice.flatten() - expected_slice				).max() < 5E-3
										assert np.abs(image_from_tuple_slice.flatten() - expected_slice				).max() < 5E-3

							@unittest.skipIf(torch_device != "cuda"       ,							"This test requires a GPU"				)
							def 							_A	(		self			:int				)				->  List[str]:


										'''simple docstring'''




										snake_case_				:					Union[str, Any]   =       self.dummy_cond_unet
										snake_case_				:					Union[str, Any]   =       PNDMScheduler(skip_prk_steps=lowerCAmelCase__				)
										snake_case_				:					int   =       self.dummy_vae
										snake_case_				:					List[Any]   =       self.dummy_text_encoder
										snake_case_				:					int   =       XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"				)
										snake_case_				:					int   =       77

										snake_case_				:					Dict   =       self.dummy_image.to(lowerCAmelCase__				)

										# put models in fp16
										snake_case_				:					Optional[Any]   =       unet.half()
										snake_case_				:					Tuple   =       vae.half()
										snake_case_				:					List[str]   =       bert.half()

										# make sure here that pndm scheduler skips prk
										snake_case_				:					Optional[int]   =       AltDiffusionImgaImgPipeline(
										    unet=lowerCAmelCase__       ,							scheduler=lowerCAmelCase__       ,							vae=lowerCAmelCase__       ,							text_encoder=lowerCAmelCase__       ,							tokenizer=lowerCAmelCase__       ,							safety_checker=lowerCAmelCase__       ,							feature_extractor=self.dummy_extractor       ,							)
										snake_case_				:					List[str]   =       VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor       ,							do_normalize=lowerCAmelCase__				)
										snake_case_				:					Optional[Any]   =       alt_pipe.to(lowerCAmelCase__				)
										alt_pipe.set_progress_bar_config(disable=lowerCAmelCase__				)

										snake_case_				:					List[Any]   =       "A painting of a squirrel eating a burger"
										snake_case_				:					str   =       torch.manual_seed(0				)
										snake_case_				:					Any   =       alt_pipe(
										    [prompt]       ,							generator=lowerCAmelCase__       ,							num_inference_steps=2       ,							output_type="np"       ,							image=lowerCAmelCase__       ,							).images

										assert image.shape == (1, 32, 32, 3)






							@unittest.skipIf(torch_device != "cuda"       ,							"This test requires a GPU"				)
							def 							_A	(		self			:Optional[int]				)				->  Any:


										'''simple docstring'''




										snake_case_				:					Union[str, Any]   =       load_image(
										    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
										    "/img2img/sketch-mountains-input.jpg"				)
										# resize to resolution that is divisible by 8 but not 16 or 32
										snake_case_				:					str   =       init_image.resize((760, 504)				)

										snake_case_				:					Optional[Any]   =       "BAAI/AltDiffusion"
										snake_case_				:					int   =       AltDiffusionImgaImgPipeline.from_pretrained(
										    lowerCAmelCase__       ,							safety_checker=lowerCAmelCase__       ,							)
										pipe.to(lowerCAmelCase__				)
										pipe.set_progress_bar_config(disable=lowerCAmelCase__				)
										pipe.enable_attention_slicing()

										snake_case_				:					Tuple   =       "A fantasy landscape, trending on artstation"

										snake_case_				:					int   =       torch.manual_seed(0				)
										snake_case_				:					List[str]   =       pipe(
										    prompt=lowerCAmelCase__       ,							image=lowerCAmelCase__       ,							strength=0.7_5       ,							guidance_scale=7.5       ,							generator=lowerCAmelCase__       ,							output_type="np"       ,							)
										snake_case_				:					str   =       output.images[0]

										snake_case_				:					List[Any]   =       image[255:258, 383:386, -1]

										assert image.shape == (504, 760, 3)
										snake_case_				:					Tuple   =       np.array([0.9_3_5_8, 0.9_3_9_7, 0.9_5_9_9, 0.9_9_0_1, 1.0_0_0_0, 1.0_0_0_0, 0.9_8_8_2, 1.0_0_0_0, 1.0_0_0_0]				)

										assert np.abs(image_slice.flatten() - expected_slice				).max() < 1E-2







@slow
@require_torch_gpu
class    A_	(unittest.TestCase							):
							"""simple docstring"""




							def 							_A	(		self			:Optional[Any]				)				->  Optional[int]:


										'''simple docstring'''




										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()






							def 							_A	(		self			:str				)				->  Any:


										'''simple docstring'''




										snake_case_				:					Optional[Any]   =       load_image(
										    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
										    "/img2img/sketch-mountains-input.jpg"				)
										snake_case_				:					List[Any]   =       init_image.resize((768, 512)				)
										snake_case_				:					Tuple   =       load_numpy(
										    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy"				)

										snake_case_				:					Any   =       "BAAI/AltDiffusion"
										snake_case_				:					List[str]   =       AltDiffusionImgaImgPipeline.from_pretrained(
										    lowerCAmelCase__       ,							safety_checker=lowerCAmelCase__       ,							)
										pipe.to(lowerCAmelCase__				)
										pipe.set_progress_bar_config(disable=lowerCAmelCase__				)
										pipe.enable_attention_slicing()

										snake_case_				:					Tuple   =       "A fantasy landscape, trending on artstation"

										snake_case_				:					Tuple   =       torch.manual_seed(0				)
										snake_case_				:					List[Any]   =       pipe(
										    prompt=lowerCAmelCase__       ,							image=lowerCAmelCase__       ,							strength=0.7_5       ,							guidance_scale=7.5       ,							generator=lowerCAmelCase__       ,							output_type="np"       ,							)
										snake_case_				:					Optional[int]   =       output.images[0]

										assert image.shape == (512, 768, 3)
										# img2img is flaky across GPUs even in fp32, so using MAE here
										assert np.abs(expected_image - image				).max() < 1E-2




