





import copy
import unittest

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

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
						    MODEL_FOR_QUESTION_ANSWERING_MAPPING,
						    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
						    MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
						    LayoutLMvaConfig,
						    LayoutLMvaForQuestionAnswering,
						    LayoutLMvaForSequenceClassification,
						    LayoutLMvaForTokenClassification,
						    LayoutLMvaModel,
						)
						from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
						from PIL import Image

						from transformers import LayoutLMvaImageProcessor




class 			_a						:






				def __init__(			self		:   Dict,			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   Union[str, Any]=2,			lowerCAmelCase__		:   List[Any]=3,			lowerCAmelCase__		:   List[str]=4,			lowerCAmelCase__		:   Optional[Any]=2,			lowerCAmelCase__		:   List[Any]=7,			lowerCAmelCase__		:   Union[str, Any]=True,			lowerCAmelCase__		:   Tuple=True,			lowerCAmelCase__		:   Union[str, Any]=True,			lowerCAmelCase__		:   List[Any]=True,			lowerCAmelCase__		:   int=9_9,			lowerCAmelCase__		:   str=3_6,			lowerCAmelCase__		:   int=3,			lowerCAmelCase__		:   str=4,			lowerCAmelCase__		:   Any=3_7,			lowerCAmelCase__		:   List[str]="gelu",			lowerCAmelCase__		:   str=0.1,			lowerCAmelCase__		:   str=0.1,			lowerCAmelCase__		:   Tuple=5_1_2,			lowerCAmelCase__		:   str=1_6,			lowerCAmelCase__		:   Any=2,			lowerCAmelCase__		:   Any=0.02,			lowerCAmelCase__		:   int=6,			lowerCAmelCase__		:   int=6,			lowerCAmelCase__		:   Any=3,			lowerCAmelCase__		:   List[Any]=4,			lowerCAmelCase__		:   Any=None,			lowerCAmelCase__		:   Optional[Any]=1_0_0_0,			)     ->				Any:




										'''simple docstring'''



										_UpperCamelCase      : Optional[Any]         =   parent
										_UpperCamelCase      : List[str]         =   batch_size
										_UpperCamelCase      : str         =   num_channels
										_UpperCamelCase      : str         =   image_size
										_UpperCamelCase      : Union[str, Any]         =   patch_size
										_UpperCamelCase      : str         =   text_seq_length
										_UpperCamelCase      : List[str]         =   is_training
										_UpperCamelCase      : int         =   use_input_mask
										_UpperCamelCase      : Any         =   use_token_type_ids
										_UpperCamelCase      : List[Any]         =   use_labels
										_UpperCamelCase      : Union[str, Any]         =   vocab_size
										_UpperCamelCase      : List[str]         =   hidden_size
										_UpperCamelCase      : str         =   num_hidden_layers
										_UpperCamelCase      : List[str]         =   num_attention_heads
										_UpperCamelCase      : str         =   intermediate_size
										_UpperCamelCase      : int         =   hidden_act
										_UpperCamelCase      : Optional[Any]         =   hidden_dropout_prob
										_UpperCamelCase      : int         =   attention_probs_dropout_prob
										_UpperCamelCase      : List[str]         =   max_position_embeddings
										_UpperCamelCase      : Dict         =   type_vocab_size
										_UpperCamelCase      : int         =   type_sequence_label_size
										_UpperCamelCase      : str         =   initializer_range
										_UpperCamelCase      : Tuple         =   coordinate_size
										_UpperCamelCase      : Tuple         =   shape_size
										_UpperCamelCase      : int         =   num_labels
										_UpperCamelCase      : Any         =   num_choices
										_UpperCamelCase      : Tuple         =   scope
										_UpperCamelCase      : List[str]         =   range_bbox

										# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
										_UpperCamelCase      : str         =   text_seq_length
										_UpperCamelCase      : Optional[Any]         =   (image_size // patch_size) ** 2 + 1
										_UpperCamelCase      : int         =   self.text_seq_length + self.image_seq_length






				def     snake_case     (			self		:   Dict							)     ->				Optional[int]:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   ids_tensor([self.batch_size, self.text_seq_length],			self.vocab_size							)

										_UpperCamelCase      : Union[str, Any]         =   ids_tensor([self.batch_size, self.text_seq_length, 4],			self.range_bbox							)
										# Ensure that bbox is legal
										for i in range(bbox.shape[0]							):
																for j in range(bbox.shape[1]							):
																						if bbox[i, j, 3] < bbox[i, j, 1]:
																												_UpperCamelCase      : Union[str, Any]         =   bbox[i, j, 3]
																												_UpperCamelCase      : List[Any]         =   bbox[i, j, 1]
																												_UpperCamelCase      : Optional[Any]         =   t
																						if bbox[i, j, 2] < bbox[i, j, 0]:
																												_UpperCamelCase      : List[Any]         =   bbox[i, j, 2]
																												_UpperCamelCase      : Tuple         =   bbox[i, j, 0]
																												_UpperCamelCase      : Optional[Any]         =   t

										_UpperCamelCase      : Any         =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

										_UpperCamelCase      : Any         =   None
										if self.use_input_mask:
																_UpperCamelCase      : Union[str, Any]         =   random_attention_mask([self.batch_size, self.text_seq_length]							)

										_UpperCamelCase      : str         =   None
										if self.use_token_type_ids:
																_UpperCamelCase      : Tuple         =   ids_tensor([self.batch_size, self.text_seq_length],			self.type_vocab_size							)

										_UpperCamelCase      : List[Any]         =   None
										_UpperCamelCase      : Tuple         =   None
										if self.use_labels:
																_UpperCamelCase      : List[str]         =   ids_tensor([self.batch_size],			self.type_sequence_label_size							)
																_UpperCamelCase      : Union[str, Any]         =   ids_tensor([self.batch_size, self.text_seq_length],			self.num_labels							)

										_UpperCamelCase      : Dict         =   LayoutLMvaConfig(
										    vocab_size=self.vocab_size,			hidden_size=self.hidden_size,			num_hidden_layers=self.num_hidden_layers,			num_attention_heads=self.num_attention_heads,			intermediate_size=self.intermediate_size,			hidden_act=self.hidden_act,			hidden_dropout_prob=self.hidden_dropout_prob,			attention_probs_dropout_prob=self.attention_probs_dropout_prob,			max_position_embeddings=self.max_position_embeddings,			type_vocab_size=self.type_vocab_size,			initializer_range=self.initializer_range,			coordinate_size=self.coordinate_size,			shape_size=self.shape_size,			input_size=self.image_size,			patch_size=self.patch_size,			)

										return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels






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




										'''simple docstring'''



										_UpperCamelCase      : str         =   LayoutLMvaModel(config=_SCREAMING_SNAKE_CASE							)
										model.to(_SCREAMING_SNAKE_CASE							)
										model.eval()

										# text + image
										_UpperCamelCase      : Any         =   model(_SCREAMING_SNAKE_CASE,			pixel_values=_SCREAMING_SNAKE_CASE							)
										_UpperCamelCase      : Any         =   model(
										    _SCREAMING_SNAKE_CASE,			bbox=_SCREAMING_SNAKE_CASE,			pixel_values=_SCREAMING_SNAKE_CASE,			attention_mask=_SCREAMING_SNAKE_CASE,			token_type_ids=_SCREAMING_SNAKE_CASE							)
										_UpperCamelCase      : Union[str, Any]         =   model(_SCREAMING_SNAKE_CASE,			bbox=_SCREAMING_SNAKE_CASE,			pixel_values=_SCREAMING_SNAKE_CASE,			token_type_ids=_SCREAMING_SNAKE_CASE							)
										_UpperCamelCase      : List[Any]         =   model(_SCREAMING_SNAKE_CASE,			bbox=_SCREAMING_SNAKE_CASE,			pixel_values=_SCREAMING_SNAKE_CASE							)

										self.parent.assertEqual(result.last_hidden_state.shape,			(self.batch_size, self.seq_length, self.hidden_size)							)

										# text only
										_UpperCamelCase      : int         =   model(_SCREAMING_SNAKE_CASE							)
										self.parent.assertEqual(
										    result.last_hidden_state.shape,			(self.batch_size, self.text_seq_length, self.hidden_size)							)

										# image only
										_UpperCamelCase      : List[Any]         =   model(pixel_values=_SCREAMING_SNAKE_CASE							)
										self.parent.assertEqual(
										    result.last_hidden_state.shape,			(self.batch_size, self.image_seq_length, self.hidden_size)							)






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




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   self.num_labels
										_UpperCamelCase      : Optional[int]         =   LayoutLMvaForSequenceClassification(_SCREAMING_SNAKE_CASE							)
										model.to(_SCREAMING_SNAKE_CASE							)
										model.eval()
										_UpperCamelCase      : str         =   model(
										    _SCREAMING_SNAKE_CASE,			bbox=_SCREAMING_SNAKE_CASE,			pixel_values=_SCREAMING_SNAKE_CASE,			attention_mask=_SCREAMING_SNAKE_CASE,			token_type_ids=_SCREAMING_SNAKE_CASE,			labels=_SCREAMING_SNAKE_CASE,			)
										self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.num_labels)							)






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




										'''simple docstring'''



										_UpperCamelCase      : Optional[Any]         =   self.num_labels
										_UpperCamelCase      : Dict         =   LayoutLMvaForTokenClassification(config=_SCREAMING_SNAKE_CASE							)
										model.to(_SCREAMING_SNAKE_CASE							)
										model.eval()
										_UpperCamelCase      : List[str]         =   model(
										    _SCREAMING_SNAKE_CASE,			bbox=_SCREAMING_SNAKE_CASE,			pixel_values=_SCREAMING_SNAKE_CASE,			attention_mask=_SCREAMING_SNAKE_CASE,			token_type_ids=_SCREAMING_SNAKE_CASE,			labels=_SCREAMING_SNAKE_CASE,			)
										self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.text_seq_length, self.num_labels)							)






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




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   LayoutLMvaForQuestionAnswering(config=_SCREAMING_SNAKE_CASE							)
										model.to(_SCREAMING_SNAKE_CASE							)
										model.eval()
										_UpperCamelCase      : int         =   model(
										    _SCREAMING_SNAKE_CASE,			bbox=_SCREAMING_SNAKE_CASE,			pixel_values=_SCREAMING_SNAKE_CASE,			attention_mask=_SCREAMING_SNAKE_CASE,			token_type_ids=_SCREAMING_SNAKE_CASE,			start_positions=_SCREAMING_SNAKE_CASE,			end_positions=_SCREAMING_SNAKE_CASE,			)
										self.parent.assertEqual(result.start_logits.shape,			(self.batch_size, self.seq_length)							)
										self.parent.assertEqual(result.end_logits.shape,			(self.batch_size, self.seq_length)							)






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




										'''simple docstring'''



										_UpperCamelCase      : Dict         =   self.prepare_config_and_inputs()
										(
										    _UpperCamelCase     
										)      : List[str]         =   config_and_inputs
										_UpperCamelCase      : Union[str, Any]         =   {
										    "input_ids": input_ids,
										    "bbox": bbox,
										    "pixel_values": pixel_values,
										    "token_type_ids": token_type_ids,
										    "attention_mask": input_mask,
										}
										return config, inputs_dict




@require_torch
class 			_a						(				__SCREAMING_SNAKE_CASE     ,    __SCREAMING_SNAKE_CASE     ,    unittest.TestCase    ):
				UpperCamelCase								=   False
				UpperCamelCase								=   False
				UpperCamelCase								=   False

				UpperCamelCase								=   (
				    (
				        LayoutLMvaModel,
				        LayoutLMvaForSequenceClassification,
				        LayoutLMvaForTokenClassification,
				        LayoutLMvaForQuestionAnswering,
				    )
				    if is_torch_available()
				    else ()
				)
				UpperCamelCase								=   (
				    {'''document-question-answering''': LayoutLMvaForQuestionAnswering, '''feature-extraction''': LayoutLMvaModel}
				    if is_torch_available()
				    else {}
				)






				def     snake_case     (			self		:   Any,			lowerCAmelCase__		:   Any,			lowerCAmelCase__		:   Union[str, Any],			lowerCAmelCase__		:   str,			lowerCAmelCase__		:   Any,			lowerCAmelCase__		:   List[str]							)     ->				int:




										'''simple docstring'''



										return True






				def     snake_case     (			self		:   Dict							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   LayoutLMvaModelTester(self							)
										_UpperCamelCase      : Union[str, Any]         =   ConfigTester(self,			config_class=_SCREAMING_SNAKE_CASE,			hidden_size=3_7							)






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




										'''simple docstring'''



										_UpperCamelCase      : int         =   copy.deepcopy(_SCREAMING_SNAKE_CASE							)
										if model_class in get_values(_SCREAMING_SNAKE_CASE							):
																_UpperCamelCase      : Optional[int]         =   {
																    k: v.unsqueeze(1							).expand(-1,			self.model_tester.num_choices,			-1							).contiguous()
																    if isinstance(_SCREAMING_SNAKE_CASE,			torch.Tensor							) and v.ndim > 1
																    else v
																    for k, v in inputs_dict.items()
																}
										if return_labels:
																if model_class in get_values(_SCREAMING_SNAKE_CASE							):
																						_UpperCamelCase      : List[Any]         =   torch.ones(self.model_tester.batch_size,			dtype=torch.long,			device=_SCREAMING_SNAKE_CASE							)
																elif model_class in get_values(_SCREAMING_SNAKE_CASE							):
																						_UpperCamelCase      : Optional[int]         =   torch.zeros(
																						    self.model_tester.batch_size,			dtype=torch.long,			device=_SCREAMING_SNAKE_CASE							)
																						_UpperCamelCase      : List[Any]         =   torch.zeros(
																						    self.model_tester.batch_size,			dtype=torch.long,			device=_SCREAMING_SNAKE_CASE							)
																elif model_class in [
																    *get_values(_SCREAMING_SNAKE_CASE							),
																]:
																						_UpperCamelCase      : int         =   torch.zeros(
																						    self.model_tester.batch_size,			dtype=torch.long,			device=_SCREAMING_SNAKE_CASE							)
																elif model_class in [
																    *get_values(_SCREAMING_SNAKE_CASE							),
																]:
																						_UpperCamelCase      : List[str]         =   torch.zeros(
																						    (self.model_tester.batch_size, self.model_tester.text_seq_length),			dtype=torch.long,			device=_SCREAMING_SNAKE_CASE,			)

										return inputs_dict






				def     snake_case     (			self		:   str							)     ->				Tuple:




										'''simple docstring'''



										self.config_tester.run_common_tests()






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




										'''simple docstring'''



										_UpperCamelCase      : Optional[int]         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE							)






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




										'''simple docstring'''



										_UpperCamelCase      : int         =   self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
																_UpperCamelCase      : int         =   type
																self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE							)






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




										'''simple docstring'''



										_UpperCamelCase      : str         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE							)






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




										'''simple docstring'''



										_UpperCamelCase      : Dict         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE							)






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




										'''simple docstring'''



										_UpperCamelCase      : int         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE							)






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




										'''simple docstring'''



										for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_UpperCamelCase      : Dict         =   LayoutLMvaModel.from_pretrained(_SCREAMING_SNAKE_CASE							)
																self.assertIsNotNone(_SCREAMING_SNAKE_CASE							)





def 	a_    (							):
						_UpperCamelCase      : Tuple         =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
						return image






@require_torch
class 			_a						(				unittest.TestCase    ):






				@cached_property
				def     snake_case     (			self		:   Optional[int]							)     ->				Union[str, Any]:




										'''simple docstring'''



										return LayoutLMvaImageProcessor(apply_ocr=_SCREAMING_SNAKE_CASE							) if is_vision_available() else None






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




										'''simple docstring'''



										_UpperCamelCase      : Dict         =   LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''							).to(_SCREAMING_SNAKE_CASE							)

										_UpperCamelCase      : List[Any]         =   self.default_image_processor
										_UpperCamelCase      : Dict         =   prepare_img()
										_UpperCamelCase      : int         =   image_processor(images=_SCREAMING_SNAKE_CASE,			return_tensors='''pt'''							).pixel_values.to(_SCREAMING_SNAKE_CASE							)

										_UpperCamelCase      : List[Any]         =   torch.tensor([[1, 2]]							)
										_UpperCamelCase      : int         =   torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]							).unsqueeze(0							)

										# forward pass
										_UpperCamelCase      : Optional[Any]         =   model(
										    input_ids=input_ids.to(_SCREAMING_SNAKE_CASE							),			bbox=bbox.to(_SCREAMING_SNAKE_CASE							),			pixel_values=pixel_values.to(_SCREAMING_SNAKE_CASE							),			)

										# verify the logits
										_UpperCamelCase      : Union[str, Any]         =   torch.Size((1, 1_9_9, 7_6_8)							)
										self.assertEqual(outputs.last_hidden_state.shape,			_SCREAMING_SNAKE_CASE							)

										_UpperCamelCase      : Dict         =   torch.tensor(
										    [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]]							).to(_SCREAMING_SNAKE_CASE							)

										self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3],			_SCREAMING_SNAKE_CASE,			atol=1e-4							)							)










"""simple docstring"""




import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer

from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




class 			_a						(				_lowerCAmelCase     ,    _lowerCAmelCase     ,    _lowerCAmelCase     ,    unittest.TestCase    ):
				UpperCamelCase								=   AltDiffusionPipeline
				UpperCamelCase								=   TEXT_TO_IMAGE_PARAMS
				UpperCamelCase								=   TEXT_TO_IMAGE_BATCH_PARAMS
				UpperCamelCase								=   TEXT_TO_IMAGE_IMAGE_PARAMS
				UpperCamelCase								=   TEXT_TO_IMAGE_IMAGE_PARAMS






				def     snake_case     (			self		:   int							)     ->				int:




										'''simple docstring'''



										torch.manual_seed(0							)
										_UpperCamelCase      : Dict         =   UNetaDConditionModel(
										    block_out_channels=(3_2, 6_4),			layers_per_block=2,			sample_size=3_2,			in_channels=4,			out_channels=4,			down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D'''),			up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D'''),			cross_attention_dim=3_2,			)
										_UpperCamelCase      : Union[str, Any]         =   DDIMScheduler(
										    beta_start=0.00_085,			beta_end=0.012,			beta_schedule='''scaled_linear''',			clip_sample=lowerCAmelCase__,			set_alpha_to_one=lowerCAmelCase__,			)
										torch.manual_seed(0							)
										_UpperCamelCase      : List[str]         =   AutoencoderKL(
										    block_out_channels=[3_2, 6_4],			in_channels=3,			out_channels=3,			down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''],			up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''],			latent_channels=4,			)

										# TODO: address the non-deterministic text encoder (fails for save-load tests)
										# torch.manual_seed(0)
										# text_encoder_config = RobertaSeriesConfig(
										#     hidden_size=32,
										#     project_dim=32,
										#     intermediate_size=37,
										#     layer_norm_eps=1e-05,
										#     num_attention_heads=4,
										#     num_hidden_layers=5,
										#     vocab_size=5002,
										# )
										# text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config)

										torch.manual_seed(0							)
										_UpperCamelCase      : str         =   CLIPTextConfig(
										    bos_token_id=0,			eos_token_id=2,			hidden_size=3_2,			projection_dim=3_2,			intermediate_size=3_7,			layer_norm_eps=1e-0_5,			num_attention_heads=4,			num_hidden_layers=5,			pad_token_id=1,			vocab_size=5_0_0_2,			)
										_UpperCamelCase      : List[Any]         =   CLIPTextModel(lowerCAmelCase__							)

										_UpperCamelCase      : Dict         =   XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''							)
										_UpperCamelCase      : str         =   7_7

										_UpperCamelCase      : int         =   {
										    '''unet''': unet,
										    '''scheduler''': scheduler,
										    '''vae''': vae,
										    '''text_encoder''': text_encoder,
										    '''tokenizer''': tokenizer,
										    '''safety_checker''': None,
										    '''feature_extractor''': None,
										}
										return components






				def     snake_case     (			self		:   Dict,			lowerCAmelCase__		:   Any,			lowerCAmelCase__		:   int=0							)     ->				Optional[int]:




										'''simple docstring'''



										if str(lowerCAmelCase__							).startswith('''mps'''							):
																_UpperCamelCase      : Any         =   torch.manual_seed(lowerCAmelCase__							)
										else:
																_UpperCamelCase      : Dict         =   torch.Generator(device=lowerCAmelCase__							).manual_seed(lowerCAmelCase__							)
										_UpperCamelCase      : str         =   {
										    '''prompt''': '''A painting of a squirrel eating a burger''',
										    '''generator''': generator,
										    '''num_inference_steps''': 2,
										    '''guidance_scale''': 6.0,
										    '''output_type''': '''numpy''',
										}
										return inputs






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




										'''simple docstring'''



										super().test_attention_slicing_forward_pass(expected_max_diff=3e-3							)






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




										'''simple docstring'''



										super().test_inference_batch_single_identical(expected_max_diff=3e-3							)






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




										'''simple docstring'''



										_UpperCamelCase      : Any         =   '''cpu'''  # ensure determinism for the device-dependent torch.Generator

										_UpperCamelCase      : int         =   self.get_dummy_components()
										torch.manual_seed(0							)
										_UpperCamelCase      : Any         =   RobertaSeriesConfig(
										    hidden_size=3_2,			project_dim=3_2,			intermediate_size=3_7,			layer_norm_eps=1e-0_5,			num_attention_heads=4,			num_hidden_layers=5,			vocab_size=5_0_0_2,			)
										# TODO: remove after fixing the non-deterministic text encoder
										_UpperCamelCase      : Tuple         =   RobertaSeriesModelWithTransformation(lowerCAmelCase__							)
										_UpperCamelCase      : str         =   text_encoder

										_UpperCamelCase      : List[Any]         =   AltDiffusionPipeline(**lowerCAmelCase__							)
										_UpperCamelCase      : List[Any]         =   alt_pipe.to(lowerCAmelCase__							)
										alt_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

										_UpperCamelCase      : Optional[int]         =   self.get_dummy_inputs(lowerCAmelCase__							)
										_UpperCamelCase      : Optional[Any]         =   '''A photo of an astronaut'''
										_UpperCamelCase      : Any         =   alt_pipe(**lowerCAmelCase__							)
										_UpperCamelCase      : Any         =   output.images
										_UpperCamelCase      : Union[str, Any]         =   image[0, -3:, -3:, -1]

										assert image.shape == (1, 6_4, 6_4, 3)
										_UpperCamelCase      : List[Any]         =   np.array(
										    [0.5_748_162, 0.60_447_145, 0.48_821_217, 0.50_100_636, 0.5_431_185, 0.45_763_683, 0.49_657_696, 0.48_132_733, 0.47_573_093]							)

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






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




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   '''cpu'''  # ensure determinism for the device-dependent torch.Generator

										_UpperCamelCase      : Optional[Any]         =   self.get_dummy_components()
										_UpperCamelCase      : str         =   PNDMScheduler(skip_prk_steps=lowerCAmelCase__							)
										torch.manual_seed(0							)
										_UpperCamelCase      : int         =   RobertaSeriesConfig(
										    hidden_size=3_2,			project_dim=3_2,			intermediate_size=3_7,			layer_norm_eps=1e-0_5,			num_attention_heads=4,			num_hidden_layers=5,			vocab_size=5_0_0_2,			)
										# TODO: remove after fixing the non-deterministic text encoder
										_UpperCamelCase      : Tuple         =   RobertaSeriesModelWithTransformation(lowerCAmelCase__							)
										_UpperCamelCase      : int         =   text_encoder
										_UpperCamelCase      : str         =   AltDiffusionPipeline(**lowerCAmelCase__							)
										_UpperCamelCase      : List[str]         =   alt_pipe.to(lowerCAmelCase__							)
										alt_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

										_UpperCamelCase      : Union[str, Any]         =   self.get_dummy_inputs(lowerCAmelCase__							)
										_UpperCamelCase      : Optional[int]         =   alt_pipe(**lowerCAmelCase__							)
										_UpperCamelCase      : List[str]         =   output.images
										_UpperCamelCase      : Optional[int]         =   image[0, -3:, -3:, -1]

										assert image.shape == (1, 6_4, 6_4, 3)
										_UpperCamelCase      : List[Any]         =   np.array(
										    [0.51_605_093, 0.5_707_241, 0.47_365_507, 0.50_578_886, 0.5_633_877, 0.4_642_503, 0.5_182_081, 0.48_763_484, 0.49_084_237]							)

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




@slow
@require_torch_gpu
class 			_a						(				unittest.TestCase    ):






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




										'''simple docstring'''



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






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




										'''simple docstring'''



										_UpperCamelCase      : str         =   AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''',			safety_checker=lowerCAmelCase__							)
										_UpperCamelCase      : List[Any]         =   alt_pipe.to(lowerCAmelCase__							)
										alt_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

										_UpperCamelCase      : int         =   '''A painting of a squirrel eating a burger'''
										_UpperCamelCase      : int         =   torch.manual_seed(0							)
										_UpperCamelCase      : Dict         =   alt_pipe([prompt],			generator=lowerCAmelCase__,			guidance_scale=6.0,			num_inference_steps=2_0,			output_type='''np'''							)

										_UpperCamelCase      : Optional[int]         =   output.images

										_UpperCamelCase      : Optional[int]         =   image[0, -3:, -3:, -1]

										assert image.shape == (1, 5_1_2, 5_1_2, 3)
										_UpperCamelCase      : List[str]         =   np.array([0.1_010, 0.0_800, 0.0_794, 0.0_885, 0.0_843, 0.0_762, 0.0_769, 0.0_729, 0.0_586]							)

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






				def     snake_case     (			self		:   str							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   DDIMScheduler.from_pretrained('''BAAI/AltDiffusion''',			subfolder='''scheduler'''							)

										_UpperCamelCase      : Dict         =   AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''',			scheduler=lowerCAmelCase__,			safety_checker=lowerCAmelCase__							)
										_UpperCamelCase      : Dict         =   alt_pipe.to(lowerCAmelCase__							)
										alt_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

										_UpperCamelCase      : Optional[Any]         =   '''A painting of a squirrel eating a burger'''
										_UpperCamelCase      : Optional[Any]         =   torch.manual_seed(0							)

										_UpperCamelCase      : Union[str, Any]         =   alt_pipe([prompt],			generator=lowerCAmelCase__,			num_inference_steps=2,			output_type='''numpy'''							)
										_UpperCamelCase      : Tuple         =   output.images

										_UpperCamelCase      : Dict         =   image[0, -3:, -3:, -1]

										assert image.shape == (1, 5_1_2, 5_1_2, 3)
										_UpperCamelCase      : str         =   np.array([0.4_019, 0.4_052, 0.3_810, 0.4_119, 0.3_916, 0.3_982, 0.4_651, 0.4_195, 0.5_323]							)

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



