






'''simple docstring'''




import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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 transformers import (
		    MODEL_MAPPING,
		    SegformerForImageClassification,
		    SegformerForSemanticSegmentation,
		    SegformerModel,
		)
		from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import SegformerImageProcessor







class    A_	(a_							):
							"""simple docstring"""




							def 							_A	(		self			:Any				)				->  int:


										'''simple docstring'''




										snake_case_				:					Optional[Any]   =       self.config_class(**self.inputs_dict				)
										self.parent.assertTrue(hasattr(lowerCAmelCase__       ,							"hidden_sizes"				)				)
										self.parent.assertTrue(hasattr(lowerCAmelCase__       ,							"num_attention_heads"				)				)
										self.parent.assertTrue(hasattr(lowerCAmelCase__       ,							"num_encoder_blocks"				)				)







class    A_	:
							"""simple docstring"""




							def __init__(		self			:Union[str, Any]       ,							lowerCAmelCase__			:List[str]       ,							lowerCAmelCase__			:int=13       ,							lowerCAmelCase__			:int=64       ,							lowerCAmelCase__			:Optional[Any]=3       ,							lowerCAmelCase__			:List[Any]=4       ,							lowerCAmelCase__			:Dict=[2, 2, 2, 2]       ,							lowerCAmelCase__			:Any=[8, 4, 2, 1]       ,							lowerCAmelCase__			:Dict=[16, 32, 64, 128]       ,							lowerCAmelCase__			:Union[str, Any]=[1, 4, 8, 16]       ,							lowerCAmelCase__			:Dict=[1, 2, 4, 8]       ,							lowerCAmelCase__			:Tuple=True       ,							lowerCAmelCase__			:Optional[Any]=True       ,							lowerCAmelCase__			:Any="gelu"       ,							lowerCAmelCase__			:List[str]=0.1       ,							lowerCAmelCase__			:Optional[int]=0.1       ,							lowerCAmelCase__			:Dict=0.0_2       ,							lowerCAmelCase__			:str=3       ,							lowerCAmelCase__			:Optional[int]=None       ,							)				->  Union[str, Any]:


										'''simple docstring'''




										snake_case_				:					Optional[int]   =       parent
										snake_case_				:					List[Any]   =       batch_size
										snake_case_				:					List[Any]   =       image_size
										snake_case_				:					Optional[Any]   =       num_channels
										snake_case_				:					List[str]   =       num_encoder_blocks
										snake_case_				:					Optional[int]   =       sr_ratios
										snake_case_				:					Dict   =       depths
										snake_case_				:					List[str]   =       hidden_sizes
										snake_case_				:					Dict   =       downsampling_rates
										snake_case_				:					Optional[int]   =       num_attention_heads
										snake_case_				:					Optional[Any]   =       is_training
										snake_case_				:					str   =       use_labels
										snake_case_				:					List[str]   =       hidden_act
										snake_case_				:					str   =       hidden_dropout_prob
										snake_case_				:					str   =       attention_probs_dropout_prob
										snake_case_				:					int   =       initializer_range
										snake_case_				:					Union[str, Any]   =       num_labels
										snake_case_				:					Dict   =       scope

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


										'''simple docstring'''




										snake_case_				:					Optional[int]   =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

										snake_case_				:					Optional[int]   =       None
										if self.use_labels:
													snake_case_				:					Dict   =       ids_tensor([self.batch_size, self.image_size, self.image_size]       ,							self.num_labels				)

										snake_case_				:					Optional[Any]   =       self.get_config()
										return config, pixel_values, labels

							def 							_A	(		self			:int				)				->  int:


										'''simple docstring'''




										return SegformerConfig(
										    image_size=self.image_size       ,							num_channels=self.num_channels       ,							num_encoder_blocks=self.num_encoder_blocks       ,							depths=self.depths       ,							hidden_sizes=self.hidden_sizes       ,							num_attention_heads=self.num_attention_heads       ,							hidden_act=self.hidden_act       ,							hidden_dropout_prob=self.hidden_dropout_prob       ,							attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,							initializer_range=self.initializer_range       ,							)

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


										'''simple docstring'''




										snake_case_				:					Any   =       SegformerModel(config=lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()
										snake_case_				:					Tuple   =       model(lowerCAmelCase__				)
										snake_case_				:					Union[str, Any]   =       self.image_size // (self.downsampling_rates[-1] * 2)
										self.parent.assertEqual(
										    result.last_hidden_state.shape       ,							(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)				)

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


										'''simple docstring'''




										snake_case_				:					str   =       self.num_labels
										snake_case_				:					int   =       SegformerForSemanticSegmentation(lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()
										snake_case_				:					str   =       model(lowerCAmelCase__				)
										self.parent.assertEqual(
										    result.logits.shape       ,							(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)				)
										snake_case_				:					str   =       model(lowerCAmelCase__       ,							labels=lowerCAmelCase__				)
										self.parent.assertEqual(
										    result.logits.shape       ,							(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)				)
										self.parent.assertGreater(result.loss       ,							0.0				)

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


										'''simple docstring'''




										snake_case_				:					Dict   =       1
										snake_case_				:					Optional[int]   =       SegformerForSemanticSegmentation(config=lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()
										snake_case_				:					Optional[Any]   =       torch.randint(0       ,							1       ,							(self.batch_size, self.image_size, self.image_size)				).to(lowerCAmelCase__				)
										snake_case_				:					List[str]   =       model(lowerCAmelCase__       ,							labels=lowerCAmelCase__				)
										self.parent.assertGreater(result.loss       ,							0.0				)






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


										'''simple docstring'''




										snake_case_				:					List[str]   =       self.prepare_config_and_inputs()
										snake_case_,	snake_case_,	snake_case_				:					Optional[int]   =       config_and_inputs
										snake_case_				:					Tuple   =       {"pixel_values": pixel_values}
										return config, inputs_dict







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




							a__   		=	(
							    (
							        SegformerModel,
							        SegformerForSemanticSegmentation,
							        SegformerForImageClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							a__   		=	(
							    {
							        '''feature-extraction''': SegformerModel,
							        '''image-classification''': SegformerForImageClassification,
							        '''image-segmentation''': SegformerForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							a__   		=	True
							a__   		=	False
							a__   		=	False
							a__   		=	False

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


										'''simple docstring'''




										snake_case_				:					Union[str, Any]   =       SegformerModelTester(self				)
										snake_case_				:					Union[str, Any]   =       SegformerConfigTester(self       ,							config_class=lowerCAmelCase__				)

							def 							_A	(		self			:Tuple				)				->  int:


										'''simple docstring'''




										self.config_tester.run_common_tests()

							def 							_A	(		self			:str				)				->  str:


										'''simple docstring'''




										snake_case_				:					List[Any]   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__				)

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


										'''simple docstring'''




										snake_case_				:					Any   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_binary_image_segmentation(*lowerCAmelCase__				)

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


										'''simple docstring'''




										snake_case_				:					Optional[int]   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_segmentation(*lowerCAmelCase__				)

							@unittest.skip("SegFormer does not use inputs_embeds"				)
							def 							_A	(		self			:Union[str, Any]				)				->  Optional[int]:


										'''simple docstring'''




										pass

							@unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods"				)
							def 							_A	(		self			:Optional[int]				)				->  Tuple:


										'''simple docstring'''




										pass

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


										'''simple docstring'''




										snake_case_,	snake_case_				:					Union[str, Any]   =       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[Any]   =       inspect.signature(model.forward				)
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													snake_case_				:					int   =       [*signature.parameters.keys()]

													snake_case_				:					List[Any]   =       ["pixel_values"]
													self.assertListEqual(arg_names[:1]       ,							lowerCAmelCase__				)

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


										'''simple docstring'''




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

										for model_class in self.all_model_classes:
													snake_case_				:					Dict   =       True
													snake_case_				:					List[Any]   =       False
													snake_case_				:					Optional[int]   =       True
													snake_case_				:					Optional[int]   =       model_class(lowerCAmelCase__				)
													model.to(lowerCAmelCase__				)
													model.eval()
													with torch.no_grad():
																snake_case_				:					Tuple   =       model(**self._prepare_for_class(lowerCAmelCase__       ,							lowerCAmelCase__				)				)
													snake_case_				:					Dict   =       outputs.attentions

													snake_case_				:					List[str]   =       sum(self.model_tester.depths				)
													self.assertEqual(len(lowerCAmelCase__				)       ,							lowerCAmelCase__				)

													# check that output_attentions also work using config
													del inputs_dict["output_attentions"]
													snake_case_				:					Any   =       True
													snake_case_				:					Union[str, Any]   =       model_class(lowerCAmelCase__				)
													model.to(lowerCAmelCase__				)
													model.eval()
													with torch.no_grad():
																snake_case_				:					str   =       model(**self._prepare_for_class(lowerCAmelCase__       ,							lowerCAmelCase__				)				)
													snake_case_				:					List[str]   =       outputs.attentions

													self.assertEqual(len(lowerCAmelCase__				)       ,							lowerCAmelCase__				)

													# verify the first attentions (first block, first layer)
													snake_case_				:					List[str]   =       (self.model_tester.image_size // 4) ** 2
													snake_case_				:					int   =       (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
													self.assertListEqual(
													    list(attentions[0].shape[-3:]				)       ,							[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]       ,							)

													# verify the last attentions (last block, last layer)
													snake_case_				:					int   =       (self.model_tester.image_size // 32) ** 2
													snake_case_				:					int   =       (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
													self.assertListEqual(
													    list(attentions[-1].shape[-3:]				)       ,							[self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]       ,							)
													snake_case_				:					Union[str, Any]   =       len(lowerCAmelCase__				)

													# Check attention is always last and order is fine
													snake_case_				:					Dict   =       True
													snake_case_				:					List[Any]   =       True
													snake_case_				:					int   =       model_class(lowerCAmelCase__				)
													model.to(lowerCAmelCase__				)
													model.eval()
													with torch.no_grad():
																snake_case_				:					Tuple   =       model(**self._prepare_for_class(lowerCAmelCase__       ,							lowerCAmelCase__				)				)

													self.assertEqual(out_len + 1       ,							len(lowerCAmelCase__				)				)

													snake_case_				:					Optional[Any]   =       outputs.attentions

													self.assertEqual(len(lowerCAmelCase__				)       ,							lowerCAmelCase__				)
													# verify the first attentions (first block, first layer)
													snake_case_				:					Dict   =       (self.model_tester.image_size // 4) ** 2
													snake_case_				:					List[Any]   =       (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
													self.assertListEqual(
													    list(self_attentions[0].shape[-3:]				)       ,							[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]       ,							)

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


										'''simple docstring'''




										def check_hidden_states_output(lowerCAmelCase__			:Optional[int]       ,							lowerCAmelCase__			:Any       ,							lowerCAmelCase__			:Union[str, Any]				):
													snake_case_				:					str   =       model_class(lowerCAmelCase__				)
													model.to(lowerCAmelCase__				)
													model.eval()

													with torch.no_grad():
																snake_case_				:					Any   =       model(**self._prepare_for_class(lowerCAmelCase__       ,							lowerCAmelCase__				)				)

													snake_case_				:					int   =       outputs.hidden_states

													snake_case_				:					Optional[int]   =       self.model_tester.num_encoder_blocks
													self.assertEqual(len(lowerCAmelCase__				)       ,							lowerCAmelCase__				)

													# verify the first hidden states (first block)
													self.assertListEqual(
													    list(hidden_states[0].shape[-3:]				)       ,							[
													        self.model_tester.hidden_sizes[0],
													        self.model_tester.image_size // 4,
													        self.model_tester.image_size // 4,
													    ]       ,							)

										snake_case_,	snake_case_				:					List[Any]   =       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													snake_case_				:					Any   =       True
													check_hidden_states_output(lowerCAmelCase__       ,							lowerCAmelCase__       ,							lowerCAmelCase__				)

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													snake_case_				:					Tuple   =       True

													check_hidden_states_output(lowerCAmelCase__       ,							lowerCAmelCase__       ,							lowerCAmelCase__				)

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


										'''simple docstring'''




										if not self.model_tester.is_training:
													return

										snake_case_,	snake_case_				:					str   =       self.model_tester.prepare_config_and_inputs_for_common()
										snake_case_				:					Any   =       True

										for model_class in self.all_model_classes:
													if model_class in get_values(lowerCAmelCase__				):
																continue

													snake_case_				:					Optional[int]   =       model_class(lowerCAmelCase__				)
													model.to(lowerCAmelCase__				)
													model.train()
													snake_case_				:					Optional[Any]   =       self._prepare_for_class(lowerCAmelCase__       ,							lowerCAmelCase__       ,							return_labels=lowerCAmelCase__				)
													snake_case_				:					Optional[Any]   =       model(**lowerCAmelCase__				).loss
													loss.backward()

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


										'''simple docstring'''




										pass






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


										'''simple docstring'''




										for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													snake_case_				:					Optional[int]   =       SegformerModel.from_pretrained(lowerCAmelCase__				)
													self.assertIsNotNone(lowerCAmelCase__				)

def 	__UpperCAmelCase       ( )->    int:



			"""simple docstring"""



			snake_case_				:					List[Any]   =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
			return image







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




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


										'''simple docstring'''




										snake_case_				:					List[str]   =       SegformerImageProcessor(
										    image_scale=(512, 512)       ,							keep_ratio=lowerCAmelCase__       ,							align=lowerCAmelCase__       ,							do_random_crop=lowerCAmelCase__				)
										snake_case_				:					Tuple   =       SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"				).to(
										    lowerCAmelCase__				)

										snake_case_				:					int   =       prepare_img()
										snake_case_				:					str   =       image_processor(images=lowerCAmelCase__       ,							return_tensors="pt"				)
										snake_case_				:					int   =       encoded_inputs.pixel_values.to(lowerCAmelCase__				)

										with torch.no_grad():
													snake_case_				:					Tuple   =       model(lowerCAmelCase__				)

										snake_case_				:					Tuple   =       torch.Size((1, model.config.num_labels, 128, 128)				)
										self.assertEqual(outputs.logits.shape       ,							lowerCAmelCase__				)

										snake_case_				:					List[Any]   =       torch.tensor(
										    [
										        [[-4.6_3_1_0, -5.5_2_3_2, -6.2_3_5_6], [-5.1_9_2_1, -6.1_4_4_4, -6.5_9_9_6], [-5.4_4_2_4, -6.2_7_9_0, -6.7_5_7_4]],
										        [[-1_2.1_3_9_1, -1_3.3_1_2_2, -1_3.9_5_5_4], [-1_2.8_7_3_2, -1_3.9_3_5_2, -1_4.3_5_6_3], [-1_2.9_4_3_8, -1_3.8_2_2_6, -1_4.2_5_1_3]],
										        [[-1_2.5_1_3_4, -1_3.4_6_8_6, -1_4.4_9_1_5], [-1_2.8_6_6_9, -1_4.4_3_4_3, -1_4.7_7_5_8], [-1_3.2_5_2_3, -1_4.5_8_1_9, -1_5.0_6_9_4]],
										    ]				).to(lowerCAmelCase__				)
										self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]       ,							lowerCAmelCase__       ,							atol=1E-4				)				)

							@slow
							def 							_A	(		self			:int				)				->  Dict:


										'''simple docstring'''




										snake_case_				:					Union[str, Any]   =       SegformerImageProcessor(
										    image_scale=(512, 512)       ,							keep_ratio=lowerCAmelCase__       ,							align=lowerCAmelCase__       ,							do_random_crop=lowerCAmelCase__				)
										snake_case_				:					List[Any]   =       SegformerForSemanticSegmentation.from_pretrained(
										    "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"				).to(lowerCAmelCase__				)

										snake_case_				:					Any   =       prepare_img()
										snake_case_				:					str   =       image_processor(images=lowerCAmelCase__       ,							return_tensors="pt"				)
										snake_case_				:					List[Any]   =       encoded_inputs.pixel_values.to(lowerCAmelCase__				)

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

										snake_case_				:					List[str]   =       torch.Size((1, model.config.num_labels, 128, 128)				)
										self.assertEqual(outputs.logits.shape       ,							lowerCAmelCase__				)

										snake_case_				:					List[Any]   =       torch.tensor(
										    [
										        [[-1_3.5_7_4_8, -1_3.9_1_1_1, -1_2.6_5_0_0], [-1_4.3_5_0_0, -1_5.3_6_8_3, -1_4.2_3_2_8], [-1_4.7_5_3_2, -1_6.0_4_2_4, -1_5.6_0_8_7]],
										        [[-1_7.1_6_5_1, -1_5.8_7_2_5, -1_2.9_6_5_3], [-1_7.2_5_8_0, -1_7.3_7_1_8, -1_4.8_2_2_3], [-1_6.6_0_5_8, -1_6.8_7_8_3, -1_6.7_4_5_2]],
										        [[-3.6_4_5_6, -3.0_2_0_9, -1.4_2_0_3], [-3.0_7_9_7, -3.1_9_5_9, -2.0_0_0_0], [-1.8_7_5_7, -1.9_2_1_7, -1.6_9_9_7]],
										    ]				).to(lowerCAmelCase__				)
										self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]       ,							lowerCAmelCase__       ,							atol=1E-1				)				)






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


										'''simple docstring'''




										snake_case_				:					Optional[int]   =       SegformerImageProcessor(
										    image_scale=(512, 512)       ,							keep_ratio=lowerCAmelCase__       ,							align=lowerCAmelCase__       ,							do_random_crop=lowerCAmelCase__				)
										snake_case_				:					List[str]   =       SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"				).to(
										    lowerCAmelCase__				)

										snake_case_				:					Tuple   =       prepare_img()
										snake_case_				:					str   =       image_processor(images=lowerCAmelCase__       ,							return_tensors="pt"				)
										snake_case_				:					Optional[int]   =       encoded_inputs.pixel_values.to(lowerCAmelCase__				)

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

										snake_case_				:					Tuple   =       outputs.logits.detach().cpu()

										snake_case_				:					Optional[int]   =       image_processor.post_process_semantic_segmentation(outputs=lowerCAmelCase__       ,							target_sizes=[(500, 300)]				)
										snake_case_				:					Optional[Any]   =       torch.Size((500, 300)				)
										self.assertEqual(segmentation[0].shape       ,							lowerCAmelCase__				)

										snake_case_				:					Dict   =       image_processor.post_process_semantic_segmentation(outputs=lowerCAmelCase__				)
										snake_case_				:					Optional[Any]   =       torch.Size((128, 128)				)
										self.assertEqual(segmentation[0].shape       ,							lowerCAmelCase__				)












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




