

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    UpperCamelCase						(			self				)     ->  List[str]:
							'''simple docstring'''






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


			@property
			def    UpperCamelCase						(			self				)     ->  List[str]:
							'''simple docstring'''






							A__						=	1
							A__						=	3
							A__						=	(32, 32)

							A__						=	floats_tensor((batch_size, num_channels) + sizes					,					rng=random.Random(0				)				).to(lowercase				)
							return image


			@property
			def    UpperCamelCase						(			self				)     ->  str:
							'''simple docstring'''






							torch.manual_seed(0				)
							A__						=	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    UpperCamelCase						(			self				)     ->  List[str]:
							'''simple docstring'''






							torch.manual_seed(0				)
							A__						=	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    UpperCamelCase						(			self				)     ->  Tuple:
							'''simple docstring'''






							torch.manual_seed(0				)
							A__						=	RobertaSeriesConfig(
							    hidden_size=32					,					project_dim=32					,					intermediate_size=37					,					layer_norm_eps=1e-05					,					num_attention_heads=4					,					num_hidden_layers=5					,					pad_token_id=1					,					vocab_size=5006					,					)
							return RobertaSeriesModelWithTransformation(lowercase				)


			@property
			def    UpperCamelCase						(			self				)     ->  Optional[Any]:
							'''simple docstring'''






							def extract(*lowercase					,					**lowercase				):

											class     a__  :
														"""simple docstring"""







														def __init__(			self				)     ->  List[Any]:
																		'''simple docstring'''






																		A__						=	torch.ones([0]				)


														def    UpperCamelCase						(			self					,					lowercase				)     ->  Optional[int]:
																		'''simple docstring'''






																		self.pixel_values.to(lowercase				)
																		return self

											return Out()

							return extract


			def    UpperCamelCase						(			self				)     ->  Optional[Any]:
							'''simple docstring'''






							A__						=	"cpu"  # ensure determinism for the device-dependent torch.Generator
							A__						=	self.dummy_cond_unet
							A__						=	PNDMScheduler(skip_prk_steps=lowercase				)
							A__						=	self.dummy_vae
							A__						=	self.dummy_text_encoder
							A__						=	XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"				)
							A__						=	77

							A__						=	self.dummy_image.to(lowercase				)
							A__						=	init_image / 2 + 0.5

							# make sure here that pndm scheduler skips prk
							A__						=	AltDiffusionImgaImgPipeline(
							    unet=lowercase					,					scheduler=lowercase					,					vae=lowercase					,					text_encoder=lowercase					,					tokenizer=lowercase					,					safety_checker=lowercase					,					feature_extractor=self.dummy_extractor					,					)
							A__						=	VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor					,					do_normalize=lowercase				)
							A__						=	alt_pipe.to(lowercase				)
							alt_pipe.set_progress_bar_config(disable=lowercase				)

							A__						=	"A painting of a squirrel eating a burger"
							A__						=	torch.Generator(device=lowercase				).manual_seed(0				)
							A__						=	alt_pipe(
							    [prompt]					,					generator=lowercase					,					guidance_scale=6.0					,					num_inference_steps=2					,					output_type="np"					,					image=lowercase					,					)

							A__						=	output.images

							A__						=	torch.Generator(device=lowercase				).manual_seed(0				)
							A__						=	alt_pipe(
							    [prompt]					,					generator=lowercase					,					guidance_scale=6.0					,					num_inference_steps=2					,					output_type="np"					,					image=lowercase					,					return_dict=lowercase					,					)[0]

							A__						=	image[0, -3:, -3:, -1]
							A__						=	image_from_tuple[0, -3:, -3:, -1]

							assert image.shape == (1, 32, 32, 3)
							A__						=	np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499]				)

							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    UpperCamelCase						(			self				)     ->  Optional[int]:
							'''simple docstring'''






							A__						=	self.dummy_cond_unet
							A__						=	PNDMScheduler(skip_prk_steps=lowercase				)
							A__						=	self.dummy_vae
							A__						=	self.dummy_text_encoder
							A__						=	XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"				)
							A__						=	77

							A__						=	self.dummy_image.to(lowercase				)

							# put models in fp16
							A__						=	unet.half()
							A__						=	vae.half()
							A__						=	bert.half()

							# make sure here that pndm scheduler skips prk
							A__						=	AltDiffusionImgaImgPipeline(
							    unet=lowercase					,					scheduler=lowercase					,					vae=lowercase					,					text_encoder=lowercase					,					tokenizer=lowercase					,					safety_checker=lowercase					,					feature_extractor=self.dummy_extractor					,					)
							A__						=	VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor					,					do_normalize=lowercase				)
							A__						=	alt_pipe.to(lowercase				)
							alt_pipe.set_progress_bar_config(disable=lowercase				)

							A__						=	"A painting of a squirrel eating a burger"
							A__						=	torch.manual_seed(0				)
							A__						=	alt_pipe(
							    [prompt]					,					generator=lowercase					,					num_inference_steps=2					,					output_type="np"					,					image=lowercase					,					).images

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


			@unittest.skipIf(torch_device != "cuda"					,					"This test requires a GPU"				)
			def    UpperCamelCase						(			self				)     ->  Dict:
							'''simple docstring'''






							A__						=	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
							A__						=	init_image.resize((760, 504)				)

							A__						=	"BAAI/AltDiffusion"
							A__						=	AltDiffusionImgaImgPipeline.from_pretrained(
							    lowercase					,					safety_checker=lowercase					,					)
							pipe.to(lowercase				)
							pipe.set_progress_bar_config(disable=lowercase				)
							pipe.enable_attention_slicing()

							A__						=	"A fantasy landscape, trending on artstation"

							A__						=	torch.manual_seed(0				)
							A__						=	pipe(
							    prompt=lowercase					,					image=lowercase					,					strength=0.75					,					guidance_scale=7.5					,					generator=lowercase					,					output_type="np"					,					)
							A__						=	output.images[0]

							A__						=	image[255:258, 383:386, -1]

							assert image.shape == (504, 760, 3)
							A__						=	np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000]				)

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

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







			def    UpperCamelCase						(			self				)     ->  int:
							'''simple docstring'''






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


			def    UpperCamelCase						(			self				)     ->  List[str]:
							'''simple docstring'''






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

							A__						=	"BAAI/AltDiffusion"
							A__						=	AltDiffusionImgaImgPipeline.from_pretrained(
							    lowercase					,					safety_checker=lowercase					,					)
							pipe.to(lowercase				)
							pipe.set_progress_bar_config(disable=lowercase				)
							pipe.enable_attention_slicing()

							A__						=	"A fantasy landscape, trending on artstation"

							A__						=	torch.manual_seed(0				)
							A__						=	pipe(
							    prompt=lowercase					,					image=lowercase					,					strength=0.75					,					guidance_scale=7.5					,					generator=lowercase					,					output_type="np"					,					)
							A__						=	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


from ...configuration_utils import PretrainedConfig
from ...utils import logging


a_    					=       logging.get_logger(__name__)

a_    					=       {
    # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert
}





class 		_lowercase						(					snake_case_       ):
 lowercase				     =   'megatron-bert'

 def __init__( self	:							List[str]   ,      snake_case	:							Tuple=2_9_0_5_6   ,      snake_case	:							Dict=1_0_2_4   ,      snake_case	:							Dict=2_4   ,      snake_case	:							Union[str, Any]=1_6   ,      snake_case	:							Optional[int]=4_0_9_6   ,      snake_case	:							Optional[int]="gelu"   ,      snake_case	:							Any=0.1   ,      snake_case	:							Tuple=0.1   ,      snake_case	:							Optional[int]=5_1_2   ,      snake_case	:							List[Any]=2   ,      snake_case	:							Tuple=0.02   ,      snake_case	:							Optional[Any]=1e-12   ,      snake_case	:							str=0   ,      snake_case	:							Optional[int]="absolute"   ,      snake_case	:							Union[str, Any]=True   ,      **snake_case	:							Any   ,      )   ->						List[Any]:

  """simple docstring"""





  super().__init__(pad_token_id=snake_case   ,      **snake_case			)

  UpperCamelCase_					:						Optional[Any] =					vocab_size
  UpperCamelCase_					:						Any =					hidden_size
  UpperCamelCase_					:						Union[str, Any] =					num_hidden_layers
  UpperCamelCase_					:						List[Any] =					num_attention_heads
  UpperCamelCase_					:						str =					hidden_act
  UpperCamelCase_					:						List[str] =					intermediate_size
  UpperCamelCase_					:						List[Any] =					hidden_dropout_prob
  UpperCamelCase_					:						Any =					attention_probs_dropout_prob
  UpperCamelCase_					:						Tuple =					max_position_embeddings
  UpperCamelCase_					:						Dict =					type_vocab_size
  UpperCamelCase_					:						Optional[int] =					initializer_range
  UpperCamelCase_					:						Optional[Any] =					layer_norm_eps
  UpperCamelCase_					:						Dict =					position_embedding_type
  UpperCamelCase_					:						List[str] =					use_cache



