





"""simple docstring"""






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 			__lowercase     :







					'''simple docstring'''




					def __init__(  self		,     _UpperCAmelCase		,     _UpperCAmelCase=2		,     _UpperCAmelCase=True		,     _UpperCAmelCase=False		,     _UpperCAmelCase=10		,     _UpperCAmelCase=3		,     _UpperCAmelCase=32 * 4		,     _UpperCAmelCase=32 * 6		,     _UpperCAmelCase=4		,     _UpperCAmelCase=32		,     ):
									__a	:  Dict         =					parent
									__a	:  Optional[Any]         =					batch_size
									__a	:  str         =					is_training
									__a	:  Optional[int]         =					use_auxiliary_loss
									__a	:  Optional[Any]         =					num_queries
									__a	:  Optional[Any]         =					num_channels
									__a	:  List[str]         =					min_size
									__a	:  List[str]         =					max_size
									__a	:  Tuple         =					num_labels
									__a	:  Optional[Any]         =					mask_feature_size







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]       ).to(
									    _SCREAMING_SNAKE_CASE       )

									__a	:  Tuple         =					torch.ones([self.batch_size, self.min_size, self.max_size]		,     device=_SCREAMING_SNAKE_CASE       )

									__a	:  Any         =					(
									    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]		,     device=_SCREAMING_SNAKE_CASE       ) > 0.5
									).float()
									__a	:  Tuple         =					(torch.rand((self.batch_size, self.num_labels)		,     device=_SCREAMING_SNAKE_CASE       ) > 0.5).long()

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







					def 	_lowerCamelCase						(  self       ):
									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 	_lowerCamelCase						(  self       ):
									__a    ,  __a    ,  __a    ,  __a    ,  __a	:  int         =					self.prepare_config_and_inputs()
									__a	:  List[str]         =					{'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
									return config, inputs_dict







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Any         =					output.encoder_hidden_states
									__a	:  Any         =					output.pixel_decoder_hidden_states
									__a	:  Optional[Any]         =					output.transformer_decoder_hidden_states

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







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase=False       ):
									with torch.no_grad():
													__a	:  Any         =					MaskFormerModel(config=_SCREAMING_SNAKE_CASE       )
													model.to(_SCREAMING_SNAKE_CASE       )
													model.eval()

													__a	:  Any         =					model(pixel_values=_SCREAMING_SNAKE_CASE		,     pixel_mask=_SCREAMING_SNAKE_CASE       )
													__a	:  str         =					model(_SCREAMING_SNAKE_CASE		,     output_hidden_states=_SCREAMING_SNAKE_CASE       )
									# 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(_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  List[Any]         =					MaskFormerForInstanceSegmentation(config=_SCREAMING_SNAKE_CASE       )
									model.to(_SCREAMING_SNAKE_CASE       )
									model.eval()

									def comm_check_on_output(_UpperCAmelCase       ):
													# 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():
													__a	:  int         =					model(pixel_values=_SCREAMING_SNAKE_CASE		,     pixel_mask=_SCREAMING_SNAKE_CASE       )
													__a	:  Any         =					model(_SCREAMING_SNAKE_CASE       )

													comm_check_on_output(_SCREAMING_SNAKE_CASE       )

													__a	:  Tuple         =					model(
													    pixel_values=_SCREAMING_SNAKE_CASE		,     pixel_mask=_SCREAMING_SNAKE_CASE		,     mask_labels=_SCREAMING_SNAKE_CASE		,     class_labels=_SCREAMING_SNAKE_CASE       )

									comm_check_on_output(_SCREAMING_SNAKE_CASE       )

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






@require_torch
class 			__lowercase     (     lowerCAmelCase__				, lowerCAmelCase__				, unittest.TestCase							):







					'''simple docstring'''




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

					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					False







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[Any]         =					MaskFormerModelTester(self       )
									__a	:  Optional[Any]         =					ConfigTester(self		,     config_class=_SCREAMING_SNAKE_CASE		,     has_text_modality=_SCREAMING_SNAKE_CASE       )







					def 	_lowerCamelCase						(  self       ):
									self.config_tester.run_common_tests()







					def 	_lowerCamelCase						(  self       ):
									__a    ,  __a	:  List[str]         =					self.model_tester.prepare_config_and_inputs_for_common()
									self.model_tester.create_and_check_maskformer_model(_SCREAMING_SNAKE_CASE		,     **_SCREAMING_SNAKE_CASE		,     output_hidden_states=_SCREAMING_SNAKE_CASE       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[int]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*_SCREAMING_SNAKE_CASE       )







					@unittest.skip(reason='''MaskFormer does not use inputs_embeds'''       )
					def 	_lowerCamelCase						(  self       ):
									pass







					@unittest.skip(reason='''MaskFormer does not have a get_input_embeddings method'''       )
					def 	_lowerCamelCase						(  self       ):
									pass







					@unittest.skip(reason='''MaskFormer is not a generative model'''       )
					def 	_lowerCamelCase						(  self       ):
									pass







					@unittest.skip(reason='''MaskFormer does not use token embeddings'''       )
					def 	_lowerCamelCase						(  self       ):
									pass







					@require_torch_multi_gpu
					@unittest.skip(
					    reason='''MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''       )
					def 	_lowerCamelCase						(  self       ):
									pass







					@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''       )
					def 	_lowerCamelCase						(  self       ):
									pass







					def 	_lowerCamelCase						(  self       ):
									__a    ,  __a	:  Optional[int]         =					self.model_tester.prepare_config_and_inputs_for_common()

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

													__a	:  Any         =					['''pixel_values''']
													self.assertListEqual(arg_names[:1]		,     _SCREAMING_SNAKE_CASE       )







					@slow
					def 	_lowerCamelCase						(  self       ):
									for model_name in ["facebook/maskformer-swin-small-coco"]:
													__a	:  Tuple         =					MaskFormerModel.from_pretrained(_SCREAMING_SNAKE_CASE       )
													self.assertIsNotNone(_SCREAMING_SNAKE_CASE       )







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

									__a	:  str         =					MaskFormerForInstanceSegmentation(MaskFormerConfig()       ).to(_SCREAMING_SNAKE_CASE       )
									__a	:  str         =					model(**_SCREAMING_SNAKE_CASE       )
									self.assertTrue(outputs.loss is not None       )







					def 	_lowerCamelCase						(  self       ):
									__a    ,  __a	:  List[str]         =					self.model_tester.prepare_config_and_inputs_for_common()
									self.model_tester.create_and_check_maskformer_model(_SCREAMING_SNAKE_CASE		,     **_SCREAMING_SNAKE_CASE		,     output_hidden_states=_SCREAMING_SNAKE_CASE       )







					def 	_lowerCamelCase						(  self       ):
									__a    ,  __a	:  Optional[Any]         =					self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
													__a	:  Tuple         =					model_class(_SCREAMING_SNAKE_CASE       ).to(_SCREAMING_SNAKE_CASE       )
													__a	:  Union[str, Any]         =					model(**_SCREAMING_SNAKE_CASE		,     output_attentions=_SCREAMING_SNAKE_CASE       )
													self.assertTrue(outputs.attentions is not None       )







					def 	_lowerCamelCase						(  self       ):
									if not self.model_tester.is_training:
													return
									# only MaskFormerForInstanceSegmentation has the loss
									__a	:  str         =					self.all_model_classes[1]
									__a    ,  __a    ,  __a    ,  __a    ,  __a	:  List[Any]         =					self.model_tester.prepare_config_and_inputs()

									__a	:  List[str]         =					model_class(_SCREAMING_SNAKE_CASE       )
									model.to(_SCREAMING_SNAKE_CASE       )
									model.train()

									__a	:  Optional[int]         =					model(_SCREAMING_SNAKE_CASE		,     mask_labels=_SCREAMING_SNAKE_CASE		,     class_labels=_SCREAMING_SNAKE_CASE       ).loss
									loss.backward()







					def 	_lowerCamelCase						(  self       ):
									# only MaskFormerForInstanceSegmentation has the loss
									__a	:  Optional[int]         =					self.all_model_classes[1]
									__a    ,  __a    ,  __a    ,  __a    ,  __a	:  List[str]         =					self.model_tester.prepare_config_and_inputs()
									__a	:  List[Any]         =					True
									__a	:  List[str]         =					True

									__a	:  Union[str, Any]         =					model_class(_SCREAMING_SNAKE_CASE       )
									model.to(_SCREAMING_SNAKE_CASE       )
									model.train()

									__a	:  int         =					model(_SCREAMING_SNAKE_CASE		,     mask_labels=_SCREAMING_SNAKE_CASE		,     class_labels=_SCREAMING_SNAKE_CASE       )

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

									__a	:  Optional[int]         =					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
									__a	:  Any         =					outputs.transformer_decoder_hidden_states[0]
									transformer_decoder_hidden_states.retain_grad()

									__a	:  Union[str, Any]         =					outputs.attentions[0]
									attentions.retain_grad()

									outputs.loss.backward(retain_graph=_SCREAMING_SNAKE_CASE       )

									self.assertIsNotNone(encoder_hidden_states.grad       )
									self.assertIsNotNone(pixel_decoder_hidden_states.grad       )
									self.assertIsNotNone(transformer_decoder_hidden_states.grad       )
									self.assertIsNotNone(attentions.grad       )






A	     =					1e-4

def       __A (     )						->						Optional[int]:
				__a	:  List[str]         =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
				return image






@require_vision
@slow
class 			__lowercase     (     unittest.TestCase							):







					'''simple docstring'''




					@cached_property
					def 	_lowerCamelCase						(  self       ):
									return (
									    MaskFormerImageProcessor.from_pretrained('''facebook/maskformer-swin-small-coco'''       )
									    if is_vision_available()
									    else None
									)







					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco'''       ).to(_SCREAMING_SNAKE_CASE       )
									__a	:  Union[str, Any]         =					self.default_image_processor
									__a	:  Any         =					prepare_img()
									__a	:  int         =					image_processor(_SCREAMING_SNAKE_CASE		,     return_tensors='''pt'''       ).to(_SCREAMING_SNAKE_CASE       )
									__a	:  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(_SCREAMING_SNAKE_CASE		,     (1, 3, 800, 1088)       )

									with torch.no_grad():
													__a	:  List[str]         =					model(**_SCREAMING_SNAKE_CASE       )

									__a	:  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(_SCREAMING_SNAKE_CASE       )
									self.assertTrue(
									    torch.allclose(
									        outputs.encoder_last_hidden_state[0, 0, :3, :3]		,     _SCREAMING_SNAKE_CASE		,     atol=_SCREAMING_SNAKE_CASE       )       )

									__a	:  str         =					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(_SCREAMING_SNAKE_CASE       )
									self.assertTrue(
									    torch.allclose(
									        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]		,     _SCREAMING_SNAKE_CASE		,     atol=_SCREAMING_SNAKE_CASE       )       )

									__a	:  Dict         =					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(_SCREAMING_SNAKE_CASE       )
									self.assertTrue(
									    torch.allclose(
									        outputs.transformer_decoder_last_hidden_state[0, :3, :3]		,     _SCREAMING_SNAKE_CASE		,     atol=_SCREAMING_SNAKE_CASE       )       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					(
									    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''       )
									    .to(_SCREAMING_SNAKE_CASE       )
									    .eval()
									)
									__a	:  Any         =					self.default_image_processor
									__a	:  Dict         =					prepare_img()
									__a	:  Tuple         =					image_processor(_SCREAMING_SNAKE_CASE		,     return_tensors='''pt'''       ).to(_SCREAMING_SNAKE_CASE       )
									__a	:  Union[str, 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(_SCREAMING_SNAKE_CASE		,     (1, 3, 800, 1088)       )

									with torch.no_grad():
													__a	:  List[Any]         =					model(**_SCREAMING_SNAKE_CASE       )
									# masks_queries_logits
									__a	:  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)		,     )
									__a	:  List[str]         =					[
									    [-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],
									]
									__a	:  int         =					torch.tensor(_SCREAMING_SNAKE_CASE       ).to(_SCREAMING_SNAKE_CASE       )
									self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]		,     _SCREAMING_SNAKE_CASE		,     atol=_SCREAMING_SNAKE_CASE       )       )
									# class_queries_logits
									__a	:  Tuple         =					outputs.class_queries_logits
									self.assertEqual(
									    class_queries_logits.shape		,     (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)       )
									__a	:  Any         =					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(_SCREAMING_SNAKE_CASE       )
									self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]		,     _SCREAMING_SNAKE_CASE		,     atol=_SCREAMING_SNAKE_CASE       )       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[int]         =					(
									    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff'''       )
									    .to(_SCREAMING_SNAKE_CASE       )
									    .eval()
									)
									__a	:  Optional[int]         =					self.default_image_processor
									__a	:  List[str]         =					prepare_img()
									__a	:  Tuple         =					image_processor(_SCREAMING_SNAKE_CASE		,     return_tensors='''pt'''       ).to(_SCREAMING_SNAKE_CASE       )
									__a	:  Dict         =					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(_SCREAMING_SNAKE_CASE		,     (1, 3, 800, 1088)       )

									with torch.no_grad():
													__a	:  int         =					model(**_SCREAMING_SNAKE_CASE       )
									# masks_queries_logits
									__a	:  str         =					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)		,     )
									__a	:  Optional[Any]         =					[[-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, -10.7711]]
									__a	:  str         =					torch.tensor(_SCREAMING_SNAKE_CASE       ).to(_SCREAMING_SNAKE_CASE       )
									self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]		,     _SCREAMING_SNAKE_CASE		,     atol=_SCREAMING_SNAKE_CASE       )       )
									# class_queries_logits
									__a	:  Any         =					outputs.class_queries_logits
									self.assertEqual(
									    class_queries_logits.shape		,     (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)       )
									__a	:  Union[str, Any]         =					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(_SCREAMING_SNAKE_CASE       )
									self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]		,     _SCREAMING_SNAKE_CASE		,     atol=_SCREAMING_SNAKE_CASE       )       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					(
									    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''       )
									    .to(_SCREAMING_SNAKE_CASE       )
									    .eval()
									)
									__a	:  str         =					self.default_image_processor

									__a	:  List[str]         =					image_processor(
									    [np.zeros((3, 800, 1333)       ), np.zeros((3, 800, 1333)       )]		,     segmentation_maps=[np.zeros((384, 384)       ).astype(np.floataa       ), np.zeros((384, 384)       ).astype(np.floataa       )]		,     return_tensors='''pt'''		,     )

									__a	:  int         =					inputs['''pixel_values'''].to(_SCREAMING_SNAKE_CASE       )
									__a	:  List[str]         =					[el.to(_SCREAMING_SNAKE_CASE       ) for el in inputs['''mask_labels''']]
									__a	:  Optional[Any]         =					[el.to(_SCREAMING_SNAKE_CASE       ) for el in inputs['''class_labels''']]

									with torch.no_grad():
													__a	:  int         =					model(**_SCREAMING_SNAKE_CASE       )

									self.assertTrue(outputs.loss is not None       )






"""simple docstring"""






import random
import unittest

import numpy as np

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionImgaImgPipeline,
    PNDMScheduler,
)
from diffusers.utils import floats_tensor
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
		import onnxruntime as ort




class 			__lowercase     (     _UpperCamelCase				, unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					'''hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline'''







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase=0       ):
									__a	:  Tuple         =					floats_tensor((1, 3, 128, 128)		,     rng=random.Random(_UpperCAmelCase       )       )
									__a	:  Any         =					np.random.RandomState(_UpperCAmelCase       )
									__a	:  Any         =					{
									    '''prompt''': '''A painting of a squirrel eating a burger''',
									    '''image''': image,
									    '''generator''': generator,
									    '''num_inference_steps''': 3,
									    '''strength''': 0.7_5,
									    '''guidance_scale''': 7.5,
									    '''output_type''': '''numpy''',
									}
									return inputs







					def 	_lowerCamelCase						(  self       ):
									__a	:  int         =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,     provider='''CPUExecutionProvider'''       )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

									__a	:  Dict         =					self.get_dummy_inputs()
									__a	:  Any         =					pipe(**_UpperCAmelCase       ).images
									__a	:  Optional[int]         =					image[0, -3:, -3:, -1].flatten()

									assert image.shape == (1, 128, 128, 3)
									__a	:  List[Any]         =					np.array([0.6_9_6_4_3, 0.5_8_4_8_4, 0.5_0_3_1_4, 0.5_8_7_6_0, 0.5_5_3_6_8, 0.5_9_6_4_3, 0.5_1_5_2_9, 0.4_1_2_1_7, 0.4_9_0_8_7]       )
									assert np.abs(image_slice - expected_slice       ).max() < 1e-1







					def 	_lowerCamelCase						(  self       ):
									__a	:  int         =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,     provider='''CPUExecutionProvider'''       )
									__a	:  Tuple         =					PNDMScheduler.from_config(pipe.scheduler.config		,     skip_prk_steps=_UpperCAmelCase       )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

									__a	:  Optional[int]         =					self.get_dummy_inputs()
									__a	:  Optional[Any]         =					pipe(**_UpperCAmelCase       ).images
									__a	:  Dict         =					image[0, -3:, -3:, -1]

									assert image.shape == (1, 128, 128, 3)
									__a	:  Optional[int]         =					np.array([0.6_1_7_3_7, 0.5_4_6_4_2, 0.5_3_1_8_3, 0.5_4_4_6_5, 0.5_2_7_4_2, 0.6_0_5_2_5, 0.4_9_9_6_9, 0.4_0_6_5_5, 0.4_8_1_5_4]       )

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







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,     provider='''CPUExecutionProvider'''       )
									__a	:  Any         =					LMSDiscreteScheduler.from_config(pipe.scheduler.config       )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

									# warmup pass to apply optimizations
									__a	:  Any         =					pipe(**self.get_dummy_inputs()       )

									__a	:  List[str]         =					self.get_dummy_inputs()
									__a	:  Tuple         =					pipe(**_UpperCAmelCase       ).images
									__a	:  Tuple         =					image[0, -3:, -3:, -1]

									assert image.shape == (1, 128, 128, 3)
									__a	:  int         =					np.array([0.5_2_7_6_1, 0.5_9_9_7_7, 0.4_9_0_3_3, 0.4_9_6_1_9, 0.5_4_2_8_2, 0.5_0_3_1_1, 0.4_7_6_0_0, 0.4_0_9_1_8, 0.4_5_2_0_3]       )

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







					def 	_lowerCamelCase						(  self       ):
									__a	:  int         =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,     provider='''CPUExecutionProvider'''       )
									__a	:  Any         =					EulerDiscreteScheduler.from_config(pipe.scheduler.config       )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

									__a	:  List[Any]         =					self.get_dummy_inputs()
									__a	:  Any         =					pipe(**_UpperCAmelCase       ).images
									__a	:  List[Any]         =					image[0, -3:, -3:, -1]

									assert image.shape == (1, 128, 128, 3)
									__a	:  Optional[Any]         =					np.array([0.5_2_9_1_1, 0.6_0_0_0_4, 0.4_9_2_2_9, 0.4_9_8_0_5, 0.5_4_5_0_2, 0.5_0_6_8_0, 0.4_7_7_7_7, 0.4_1_0_2_8, 0.4_5_3_0_4]       )

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







					def 	_lowerCamelCase						(  self       ):
									__a	:  int         =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,     provider='''CPUExecutionProvider'''       )
									__a	:  Any         =					EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config       )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

									__a	:  Union[str, Any]         =					self.get_dummy_inputs()
									__a	:  str         =					pipe(**_UpperCAmelCase       ).images
									__a	:  Union[str, Any]         =					image[0, -3:, -3:, -1]

									assert image.shape == (1, 128, 128, 3)
									__a	:  Optional[int]         =					np.array([0.5_2_9_1_1, 0.6_0_0_0_4, 0.4_9_2_2_9, 0.4_9_8_0_5, 0.5_4_5_0_2, 0.5_0_6_8_0, 0.4_7_7_7_7, 0.4_1_0_2_8, 0.4_5_3_0_4]       )

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







					def 	_lowerCamelCase						(  self       ):
									__a	:  int         =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,     provider='''CPUExecutionProvider'''       )
									__a	:  str         =					DPMSolverMultistepScheduler.from_config(pipe.scheduler.config       )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

									__a	:  Optional[int]         =					self.get_dummy_inputs()
									__a	:  Optional[Any]         =					pipe(**_UpperCAmelCase       ).images
									__a	:  Union[str, Any]         =					image[0, -3:, -3:, -1]

									assert image.shape == (1, 128, 128, 3)
									__a	:  Optional[Any]         =					np.array([0.6_5_3_3_1, 0.5_8_2_7_7, 0.4_8_2_0_4, 0.5_6_0_5_9, 0.5_3_6_6_5, 0.5_6_2_3_5, 0.5_0_9_6_9, 0.4_0_0_0_9, 0.4_6_5_5_2]       )

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






@nightly
@require_onnxruntime
@require_torch_gpu
class 			__lowercase     (     unittest.TestCase							):







					'''simple docstring'''




					@property
					def 	_lowerCamelCase						(  self       ):
									return (
									    "CUDAExecutionProvider",
									    {
									        "gpu_mem_limit": "15000000000",  # 15GB
									        "arena_extend_strategy": "kSameAsRequested",
									    },
									)







					@property
					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					ort.SessionOptions()
									__a	:  Any         =					False
									return options







					def 	_lowerCamelCase						(  self       ):
									__a	:  str         =					load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
									    '''/img2img/sketch-mountains-input.jpg'''       )
									__a	:  Tuple         =					init_image.resize((768, 512)       )
									# using the PNDM scheduler by default
									__a	:  List[str]         =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(
									    '''CompVis/stable-diffusion-v1-4'''		,     revision='''onnx'''		,     safety_checker=_UpperCAmelCase		,     feature_extractor=_UpperCAmelCase		,     provider=self.gpu_provider		,     sess_options=self.gpu_options		,     )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

									__a	:  Tuple         =					'''A fantasy landscape, trending on artstation'''

									__a	:  Tuple         =					np.random.RandomState(0       )
									__a	:  int         =					pipe(
									    prompt=_UpperCAmelCase		,     image=_UpperCAmelCase		,     strength=0.7_5		,     guidance_scale=7.5		,     num_inference_steps=10		,     generator=_UpperCAmelCase		,     output_type='''np'''		,     )
									__a	:  List[Any]         =					output.images
									__a	:  int         =					images[0, 255:258, 383:386, -1]

									assert images.shape == (1, 512, 768, 3)
									__a	:  Any         =					np.array([0.4_9_0_9, 0.5_0_5_9, 0.5_3_7_2, 0.4_6_2_3, 0.4_8_7_6, 0.5_0_4_9, 0.4_8_2_0, 0.4_9_5_6, 0.5_0_1_9]       )
									# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
									    '''/img2img/sketch-mountains-input.jpg'''       )
									__a	:  Tuple         =					init_image.resize((768, 512)       )
									__a	:  str         =					LMSDiscreteScheduler.from_pretrained(
									    '''runwayml/stable-diffusion-v1-5'''		,     subfolder='''scheduler'''		,     revision='''onnx'''       )
									__a	:  Optional[int]         =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(
									    '''runwayml/stable-diffusion-v1-5'''		,     revision='''onnx'''		,     scheduler=_UpperCAmelCase		,     safety_checker=_UpperCAmelCase		,     feature_extractor=_UpperCAmelCase		,     provider=self.gpu_provider		,     sess_options=self.gpu_options		,     )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

									__a	:  List[str]         =					'''A fantasy landscape, trending on artstation'''

									__a	:  str         =					np.random.RandomState(0       )
									__a	:  str         =					pipe(
									    prompt=_UpperCAmelCase		,     image=_UpperCAmelCase		,     strength=0.7_5		,     guidance_scale=7.5		,     num_inference_steps=20		,     generator=_UpperCAmelCase		,     output_type='''np'''		,     )
									__a	:  Dict         =					output.images
									__a	:  List[Any]         =					images[0, 255:258, 383:386, -1]

									assert images.shape == (1, 512, 768, 3)
									__a	:  Dict         =					np.array([0.8_0_4_3, 0.9_2_6, 0.9_5_8_1, 0.8_1_1_9, 0.8_9_5_4, 0.9_1_3, 0.7_2_0_9, 0.7_4_6_3, 0.7_4_3_1]       )
									# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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