
import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, LDMSuperResolutionPipeline, UNetaDModel, VQModel
from diffusers.utils import PIL_INTERPOLATION, floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch


enable_full_determinism()
class 			lowercase__(    unittest.TestCase							):



	"""simple docstring"""




	@property
	def 						_lowercase  (	self  :      List[str]	)	->					Any:
					lowercase_					  = 1
					lowercase_					  = 3
					lowercase_					  = (3_2, 3_2)

					lowercase_					  = floats_tensor((batch_size, num_channels) + sizes	,							rng=random.Random(0	)	).to(SCREAMING_SNAKE_CASE_	)
					return image
	@property
	def 						_lowercase  (	self  :      Optional[int]	)	->					int:
					torch.manual_seed(0	)
					lowercase_					  = UNetaDModel(
					    block_out_channels=(3_2, 6_4)	,							layers_per_block=2	,							sample_size=3_2	,							in_channels=6	,							out_channels=3	,							down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')	,							up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')	,							)
					return model
	@property
	def 						_lowercase  (	self  :      str	)	->					str:
					torch.manual_seed(0	)
					lowercase_					  = VQModel(
					    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=3	,							)
					return model
	def 						_lowercase  (	self  :      Tuple	)	->					int:
					lowercase_					  = '''cpu'''
					lowercase_					  = self.dummy_uncond_unet
					lowercase_					  = DDIMScheduler()
					lowercase_					  = self.dummy_vq_model

					lowercase_					  = LDMSuperResolutionPipeline(unet=SCREAMING_SNAKE_CASE_	,							vqvae=SCREAMING_SNAKE_CASE_	,							scheduler=SCREAMING_SNAKE_CASE_	)
					ldm.to(SCREAMING_SNAKE_CASE_	)
					ldm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

					lowercase_					  = self.dummy_image.to(SCREAMING_SNAKE_CASE_	)

					lowercase_					  = torch.Generator(device=SCREAMING_SNAKE_CASE_	).manual_seed(0	)
					lowercase_					  = ldm(image=SCREAMING_SNAKE_CASE_	,							generator=SCREAMING_SNAKE_CASE_	,							num_inference_steps=2	,							output_type='''numpy'''	).images

					lowercase_					  = image[0, -3:, -3:, -1]

					assert image.shape == (1, 6_4, 6_4, 3)
					lowercase_					  = np.array([0.86_78, 0.82_45, 0.63_81, 0.68_30, 0.43_85, 0.55_99, 0.46_41, 0.62_01, 0.51_50]	)

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




	@unittest.skipIf(torch_device != '''cuda'''	,							'''This test requires a GPU'''	)
	def 						_lowercase  (	self  :      Any	)	->					Optional[Any]:
					lowercase_					  = self.dummy_uncond_unet
					lowercase_					  = DDIMScheduler()
					lowercase_					  = self.dummy_vq_model

					# put models in fp16
					lowercase_					  = unet.half()
					lowercase_					  = vqvae.half()

					lowercase_					  = LDMSuperResolutionPipeline(unet=SCREAMING_SNAKE_CASE_	,							vqvae=SCREAMING_SNAKE_CASE_	,							scheduler=SCREAMING_SNAKE_CASE_	)
					ldm.to(SCREAMING_SNAKE_CASE_	)
					ldm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

					lowercase_					  = self.dummy_image.to(SCREAMING_SNAKE_CASE_	)

					lowercase_					  = ldm(SCREAMING_SNAKE_CASE_	,							num_inference_steps=2	,							output_type='''numpy'''	).images

					assert image.shape == (1, 6_4, 6_4, 3)

@slow
@require_torch
class 			lowercase__(    unittest.TestCase							):



	"""simple docstring"""




	def 						_lowercase  (	self  :      str	)	->					Union[str, Any]:
					lowercase_					  = load_image(
					    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
					    '''/vq_diffusion/teddy_bear_pool.png'''	)
					lowercase_					  = init_image.resize((6_4, 6_4)	,							resample=PIL_INTERPOLATION['''lanczos''']	)

					lowercase_					  = LDMSuperResolutionPipeline.from_pretrained('''duongna/ldm-super-resolution'''	,							device_map='''auto'''	)
					ldm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

					lowercase_					  = torch.manual_seed(0	)
					lowercase_					  = ldm(image=SCREAMING_SNAKE_CASE_	,							generator=SCREAMING_SNAKE_CASE_	,							num_inference_steps=2_0	,							output_type='''numpy'''	).images

					lowercase_					  = image[0, -3:, -3:, -1]

					assert image.shape == (1, 2_5_6, 2_5_6, 3)
					lowercase_					  = np.array([0.76_44, 0.76_79, 0.76_42, 0.76_33, 0.76_66, 0.75_60, 0.74_25, 0.72_57, 0.69_07]	)

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






import unittest

import numpy as np

from transformers import ElectraConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
						from transformers.models.electra.modeling_flax_electra import (
						    FlaxElectraForCausalLM,
						    FlaxElectraForMaskedLM,
						    FlaxElectraForMultipleChoice,
						    FlaxElectraForPreTraining,
						    FlaxElectraForQuestionAnswering,
						    FlaxElectraForSequenceClassification,
						    FlaxElectraForTokenClassification,
						    FlaxElectraModel,
						)
class 			lowercase__(    unittest.TestCase							):



	"""simple docstring"""




	def __init__(	self  :      str	,							SCREAMING_SNAKE_CASE_  :      str	,							SCREAMING_SNAKE_CASE_  :      Optional[int]=1_3	,							SCREAMING_SNAKE_CASE_  :      int=7	,							SCREAMING_SNAKE_CASE_  :      Tuple=True	,							SCREAMING_SNAKE_CASE_  :      List[Any]=True	,							SCREAMING_SNAKE_CASE_  :      List[Any]=True	,							SCREAMING_SNAKE_CASE_  :      Dict=True	,							SCREAMING_SNAKE_CASE_  :      Optional[Any]=9_9	,							SCREAMING_SNAKE_CASE_  :      Any=2_4	,							SCREAMING_SNAKE_CASE_  :      int=3_2	,							SCREAMING_SNAKE_CASE_  :      Any=5	,							SCREAMING_SNAKE_CASE_  :      List[str]=4	,							SCREAMING_SNAKE_CASE_  :      Optional[int]=3_7	,							SCREAMING_SNAKE_CASE_  :      Optional[int]="gelu"	,							SCREAMING_SNAKE_CASE_  :      Any=0.1	,							SCREAMING_SNAKE_CASE_  :      List[Any]=0.1	,							SCREAMING_SNAKE_CASE_  :      Dict=5_1_2	,							SCREAMING_SNAKE_CASE_  :      Optional[int]=1_6	,							SCREAMING_SNAKE_CASE_  :      List[Any]=2	,							SCREAMING_SNAKE_CASE_  :      List[str]=0.02	,							SCREAMING_SNAKE_CASE_  :      List[str]=4	,							)	->					Optional[int]:
					lowercase_					  = parent
					lowercase_					  = batch_size
					lowercase_					  = seq_length
					lowercase_					  = is_training
					lowercase_					  = use_attention_mask
					lowercase_					  = use_token_type_ids
					lowercase_					  = use_labels
					lowercase_					  = vocab_size
					lowercase_					  = hidden_size
					lowercase_					  = embedding_size
					lowercase_					  = num_hidden_layers
					lowercase_					  = num_attention_heads
					lowercase_					  = intermediate_size
					lowercase_					  = hidden_act
					lowercase_					  = hidden_dropout_prob
					lowercase_					  = attention_probs_dropout_prob
					lowercase_					  = max_position_embeddings
					lowercase_					  = type_vocab_size
					lowercase_					  = type_sequence_label_size
					lowercase_					  = initializer_range
					lowercase_					  = num_choices
	def 						_lowercase  (	self  :      Optional[int]	)	->					Tuple:
					lowercase_					  = ids_tensor([self.batch_size, self.seq_length]	,							self.vocab_size	)

					lowercase_					  = None
					if self.use_attention_mask:
									lowercase_					  = random_attention_mask([self.batch_size, self.seq_length]	)

					lowercase_					  = None
					if self.use_token_type_ids:
									lowercase_					  = ids_tensor([self.batch_size, self.seq_length]	,							self.type_vocab_size	)

					lowercase_					  = ElectraConfig(
					    vocab_size=self.vocab_size	,							hidden_size=self.hidden_size	,							embedding_size=self.embedding_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	,							)

					return config, input_ids, token_type_ids, attention_mask




	def 						_lowercase  (	self  :      Any	)	->					str:
					lowercase_					  = self.prepare_config_and_inputs()
					lowercase_  ,     lowercase_  ,     lowercase_  ,     lowercase_					  = config_and_inputs
					lowercase_					  = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
					return config, inputs_dict

@require_flax
class 			lowercase__(    UpperCAmelCase			,						unittest.TestCase							):



	"""simple docstring"""




	a      :List[str]   					=     True

	a      :List[Any]   					=     (
	    (
	        FlaxElectraModel,
	        FlaxElectraForCausalLM,
	        FlaxElectraForMaskedLM,
	        FlaxElectraForPreTraining,
	        FlaxElectraForTokenClassification,
	        FlaxElectraForQuestionAnswering,
	        FlaxElectraForMultipleChoice,
	        FlaxElectraForSequenceClassification,
	    )
	    if is_flax_available()
	    else ()
	)
	def 						_lowercase  (	self  :      List[str]	)	->					Optional[Any]:
					lowercase_					  = FlaxElectraModelTester(self	)




	@slow
	def 						_lowercase  (	self  :      List[str]	)	->					int:
					for model_class_name in self.all_model_classes:
									if model_class_name == FlaxElectraForMaskedLM:
													lowercase_					  = model_class_name.from_pretrained('''google/electra-small-generator'''	)
									else:
													lowercase_					  = model_class_name.from_pretrained('''google/electra-small-discriminator'''	)
									lowercase_					  = model(np.ones((1, 1)	)	)
									self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)




