





'''simple docstring'''




from __future__ import annotations

import unittest

import numpy as np

from transformers import OPTConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import tensorflow as tf

			from transformers import GPTaTokenizer, TFOPTForCausalLM, TFOPTModel




def 						__lowerCamelCase			(	UpperCAmelCase_					,				UpperCAmelCase_					,				UpperCAmelCase_=None					,				UpperCAmelCase_=None							)   ->List[Any]:
	if attention_mask is None:
		snake_case__	       =   tf.cast(tf.math.not_equal(UpperCAmelCase_					,				config.pad_token_id							)					,				tf.inta							)
	return {"input_ids": input_ids, "attention_mask": attention_mask}






@require_tf
class 				__snake_case     :
	__lowerCAmelCase       						=  OPTConfig
	__lowerCAmelCase       						=  {}
	__lowerCAmelCase       						=  '''gelu'''





	def __init__(		self      ,       UpperCamelCase_      ,       UpperCamelCase_=13      ,       UpperCamelCase_=7      ,       UpperCamelCase_=True      ,       UpperCamelCase_=False      ,       UpperCamelCase_=99      ,       UpperCamelCase_=16      ,       UpperCamelCase_=2      ,       UpperCamelCase_=4      ,       UpperCamelCase_=4      ,       UpperCamelCase_="gelu"      ,       UpperCamelCase_=0.1      ,       UpperCamelCase_=0.1      ,       UpperCamelCase_=20      ,       UpperCamelCase_=2      ,       UpperCamelCase_=1      ,       UpperCamelCase_=0      ,       UpperCamelCase_=16      ,       UpperCamelCase_=16      ,       )      ->		Optional[int]:
		snake_case__	       =   parent
		snake_case__	       =   batch_size
		snake_case__	       =   seq_length
		snake_case__	       =   is_training
		snake_case__	       =   use_labels
		snake_case__	       =   vocab_size
		snake_case__	       =   hidden_size
		snake_case__	       =   num_hidden_layers
		snake_case__	       =   num_attention_heads
		snake_case__	       =   intermediate_size
		snake_case__	       =   hidden_act
		snake_case__	       =   hidden_dropout_prob
		snake_case__	       =   attention_probs_dropout_prob
		snake_case__	       =   max_position_embeddings
		snake_case__	       =   eos_token_id
		snake_case__	       =   pad_token_id
		snake_case__	       =   bos_token_id
		snake_case__	       =   embed_dim
		snake_case__	       =   word_embed_proj_dim
		snake_case__	       =   False





	def   _snake_case   (		self		)      ->		str:
		snake_case__	       =   ids_tensor([self.batch_size, self.seq_length - 1]      ,       self.vocab_size		)
		snake_case__	       =   tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size		)      ,       1		)
		snake_case__	       =   tf.concat([input_ids, eos_tensor]      ,       axis=1		)

		snake_case__	       =   self.config_cls(
		    vocab_size=self.vocab_size      ,       hidden_size=self.hidden_size      ,       num_hidden_layers=self.num_hidden_layers      ,       num_attention_heads=self.num_attention_heads      ,       ffn_dim=self.intermediate_size      ,       dropout=self.hidden_dropout_prob      ,       attention_dropout=self.attention_probs_dropout_prob      ,       max_position_embeddings=self.max_position_embeddings      ,       eos_token_id=self.eos_token_id      ,       bos_token_id=self.bos_token_id      ,       pad_token_id=self.pad_token_id      ,       embed_dim=self.embed_dim      ,       word_embed_proj_dim=self.word_embed_proj_dim      ,       is_encoder_decoder=UpperCamelCase_      ,       **self.config_updates      ,       )
		snake_case__	       =   prepare_opt_inputs_dict(UpperCamelCase_      ,       UpperCamelCase_		)
		return config, inputs_dict





	def   _snake_case   (		self      ,       UpperCamelCase_      ,       UpperCamelCase_		)      ->		int:
		snake_case__	       =   TFOPTModel(config=UpperCamelCase_		)
		snake_case__	       =   inputs_dict['input_ids']

		snake_case__	       =   input_ids[:1, :]
		snake_case__	       =   inputs_dict['attention_mask'][:1, :]
		snake_case__	       =   1

		# first forward pass
		snake_case__	       =   model(UpperCamelCase_      ,       attention_mask=UpperCamelCase_      ,       use_cache=UpperCamelCase_		)

		snake_case__       ,  snake_case__	       =   outputs.to_tuple()

		# create hypothetical next token and extent to next_input_ids
		snake_case__	       =   ids_tensor((self.batch_size, 3)      ,       config.vocab_size		)
		snake_case__	       =   tf.cast(ids_tensor((self.batch_size, 3)      ,       2		)      ,       tf.inta		)

		# append to next input_ids and
		snake_case__	       =   tf.concat([input_ids, next_tokens]      ,       axis=-1		)
		snake_case__	       =   tf.concat([attention_mask, next_attn_mask]      ,       axis=-1		)

		snake_case__	       =   model(UpperCamelCase_      ,       attention_mask=UpperCamelCase_		)[0]
		snake_case__	       =   model(UpperCamelCase_      ,       attention_mask=UpperCamelCase_      ,       past_key_values=UpperCamelCase_		)[0]

		self.parent.assertEqual(next_tokens.shape[1]      ,       output_from_past.shape[1]		)

		# select random slice
		snake_case__	       =   int(ids_tensor((1,)      ,       output_from_past.shape[-1]		)		)
		snake_case__	       =   output_from_no_past[:, -3:, random_slice_idx]
		snake_case__	       =   output_from_past[:, :, random_slice_idx]

		# test that outputs are equal for slice
		tf.debugging.assert_near(UpperCamelCase_      ,       UpperCamelCase_      ,       rtol=1E-3		)






@require_tf
class 				__snake_case     (					__magic_name__       ,       __magic_name__       ,       unittest.TestCase							):
	__lowerCAmelCase       						=  (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
	__lowerCAmelCase       						=  (TFOPTForCausalLM,) if is_tf_available() else ()
	__lowerCAmelCase       						=  (
	    {'''feature-extraction''': TFOPTModel, '''text-generation''': TFOPTForCausalLM} if is_tf_available() else {}
	)
	__lowerCAmelCase       						=  False
	__lowerCAmelCase       						=  False
	__lowerCAmelCase       						=  False
	__lowerCAmelCase       						=  10





	def   _snake_case   (		self		)      ->		Any:
		snake_case__	       =   TFOPTModelTester(self		)
		snake_case__	       =   ConfigTester(self      ,       config_class=UpperCamelCase_		)





	def   _snake_case   (		self		)      ->		Tuple:
		self.config_tester.run_common_tests()





	def   _snake_case   (		self		)      ->		Dict:
		snake_case__	       =   self.model_tester.prepare_config_and_inputs_for_common()
		self.model_tester.check_decoder_model_past_large_inputs(*UpperCamelCase_		)





	def   _snake_case   (		self		)      ->		int:
		snake_case__       ,  snake_case__	       =   self.model_tester.prepare_config_and_inputs_for_common()

		def _get_word_embedding_weight(UpperCamelCase_      ,       UpperCamelCase_		):
			if hasattr(UpperCamelCase_      ,       'weight'		):
				return embedding_layer.weight
			else:
				# Here we build the word embeddings weights if not exists.
				# And then we retry to get the attribute once built.
				model.build()
				if hasattr(UpperCamelCase_      ,       'weight'		):
					return embedding_layer.weight
				else:
					return None

		for model_class in self.all_model_classes:
			for size in [config.vocab_size - 10, config.vocab_size + 10]:
				# build the embeddings
				snake_case__	       =   model_class(config=UpperCamelCase_		)
				snake_case__	       =   _get_word_embedding_weight(UpperCamelCase_      ,       model.get_input_embeddings()		)
				snake_case__	       =   _get_word_embedding_weight(UpperCamelCase_      ,       model.get_output_embeddings()		)

				# reshape the embeddings
				model.resize_token_embeddings(UpperCamelCase_		)
				snake_case__	       =   _get_word_embedding_weight(UpperCamelCase_      ,       model.get_input_embeddings()		)
				snake_case__	       =   _get_word_embedding_weight(UpperCamelCase_      ,       model.get_output_embeddings()		)

				# check that the resized embeddings size matches the desired size.
				snake_case__	       =   size if size is not None else config.vocab_size

				self.assertEqual(new_input_embeddings.shape[0]      ,       UpperCamelCase_		)

				# check that weights remain the same after resizing
				snake_case__	       =   True
				for pa, pa in zip(old_input_embeddings.value()      ,       new_input_embeddings.value()		):
					if tf.math.reduce_sum(tf.math.abs(pa - pa		)		) > 0:
						snake_case__	       =   False
				self.assertTrue(UpperCamelCase_		)

				if old_output_embeddings is not None and new_output_embeddings is not None:
					self.assertEqual(new_output_embeddings.shape[0]      ,       UpperCamelCase_		)

					snake_case__	       =   True
					for pa, pa in zip(old_output_embeddings.value()      ,       new_output_embeddings.value()		):
						if tf.math.reduce_sum(tf.math.abs(pa - pa		)		) > 0:
							snake_case__	       =   False
					self.assertTrue(UpperCamelCase_		)





def 						__lowerCamelCase			(	UpperCAmelCase_							)   ->Tuple:
	return tf.constant(UpperCAmelCase_					,				dtype=tf.intaa							)






@require_tf
class 				__snake_case     (					unittest.TestCase							):
	__lowerCAmelCase       						=  99





	def   _snake_case   (		self		)      ->		Optional[Any]:
		snake_case__	       =   tf.ones((4, 1)      ,       dtype=tf.intaa		) * 2
		snake_case__	       =   tf.concat([ids_tensor((4, 6)      ,       self.vocab_size - 3		) + 3, eos_column_vector]      ,       axis=1		)
		snake_case__	       =   input_ids.shape[0]
		snake_case__	       =   OPTConfig(
		    vocab_size=self.vocab_size      ,       hidden_size=24      ,       num_hidden_layers=2      ,       num_attention_heads=2      ,       ffn_dim=32      ,       max_position_embeddings=48      ,       eos_token_id=2      ,       pad_token_id=1      ,       bos_token_id=0      ,       )
		return config, input_ids, batch_size






@require_sentencepiece
@require_tf
class 				__snake_case     (					unittest.TestCase							):





	@slow
	def   _snake_case   (		self		)      ->		List[str]:
		snake_case__	       =   TFOPTModel.from_pretrained('facebook/opt-350m'		)
		snake_case__	       =   _long_tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]		)
		snake_case__	       =   tf.not_equal(UpperCamelCase_      ,       model.config.pad_token_id		)
		with tf.GradientTape():
			snake_case__	       =   model(input_ids=UpperCamelCase_      ,       attention_mask=UpperCamelCase_		).last_hidden_state
		snake_case__	       =   (1, 11, 512)
		self.assertEqual(output.shape      ,       UpperCamelCase_		)
		snake_case__	       =   tf.constant(
		    [[-0.2_8_7_3, -1.9_2_1_8, -0.3_0_3_3], [-1.2_7_1_0, -0.1_3_3_8, -0.1_9_0_2], [0.4_0_9_5, 0.1_2_1_4, -1.3_1_2_1]]		)
		self.assertTrue(np.allclose(output[:, :3, :3]      ,       UpperCamelCase_      ,       atol=4E-3		)		)

		snake_case__	       =   tf.function(UpperCamelCase_      ,       jit_compile=UpperCamelCase_		)
		snake_case__	       =   xla_generate(UpperCamelCase_      ,       UpperCamelCase_		)[0]
		self.assertTrue(np.allclose(output[:, :3, :3]      ,       UpperCamelCase_      ,       atol=4E-2		)		)






@require_tf
@slow
class 				__snake_case     (					unittest.TestCase							):





	def   _snake_case   (		self		)      ->		List[str]:
		super().setUp()
		snake_case__	       =   'facebook/opt-350m'





	def   _snake_case   (		self		)      ->		List[str]:
		snake_case__	       =   TFOPTForCausalLM.from_pretrained(self.path_model		)
		snake_case__	       =   GPTaTokenizer.from_pretrained(self.path_model		)

		snake_case__	       =   [
		    'Today is a beautiful day and I want to',
		    'In the city of',
		    'Paris is the capital of France and',
		    'Computers and mobile phones have taken',
		]
		# verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
		snake_case__	       =   tokenizer(UpperCamelCase_      ,       return_tensors='tf'      ,       padding=UpperCamelCase_      ,       add_special_tokens=UpperCamelCase_		)
		snake_case__	       =   tf.math.reduce_mean(model(inputs.input_ids      ,       attention_mask=inputs.attention_mask		)[0]      ,       axis=-1		)
		snake_case__	       =   tf.constant(
		    [
		        [1.3_8_5_1, -1_3.8_9_2_3, -1_0.5_2_2_9, -1_0.7_5_3_3, -0.2_3_0_9, -1_0.2_3_8_4, -0.5_3_6_5, -9.0_9_4_7, -5.1_6_7_0],
		        [-4.7_0_7_3, -1_0.6_2_7_6, -3.9_4_1_5, -2_1.5_2_4_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2],
		        [0.6_2_4_7, -3.4_2_2_9, -8.9_1_7_9, -1.4_2_9_7, -1_4.1_6_5_0, 1.4_1_4_6, -9.0_2_1_8, -0.2_7_0_3, -0.2_7_0_3],
		        [6.4_7_8_3, -1.9_9_1_3, -1_0.7_9_2_6, -2.3_3_3_6, 1.5_0_9_2, -0.9_9_7_4, -6.8_2_1_3, 1.3_4_7_7, 1.3_4_7_7],
		    ]		)
		self.assertTrue(np.allclose(UpperCamelCase_      ,       UpperCamelCase_      ,       atol=1E-4		)		)

		snake_case__	       =   tf.function(UpperCamelCase_      ,       jit_compile=UpperCamelCase_		)
		snake_case__	       =   tf.math.reduce_mean(xla_generate(inputs.input_ids      ,       attention_mask=inputs.attention_mask		)[0]      ,       axis=-1		)
		self.assertTrue(np.allclose(UpperCamelCase_      ,       UpperCamelCase_      ,       atol=1E-4		)		)






@require_tf
@slow
class 				__snake_case     (					unittest.TestCase							):





	@property
	def   _snake_case   (		self		)      ->		str:
		return [
		    "Today is a beautiful day and I want",
		    "In the city of",
		    "Paris is the capital of France and",
		    "Computers and mobile phones have taken",
		]





	def   _snake_case   (		self		)      ->		Any:
		snake_case__	       =   'facebook/opt-125m'

		snake_case__	       =   [
		    'Today is a beautiful day and I want to',
		    'In the city of New York, the city',
		    'Paris is the capital of France and the capital',
		    'Computers and mobile phones have taken over the',
		]

		snake_case__	       =   []
		snake_case__	       =   GPTaTokenizer.from_pretrained(UpperCamelCase_		)
		snake_case__	       =   TFOPTForCausalLM.from_pretrained(UpperCamelCase_		)

		for prompt in self.prompts:
			snake_case__	       =   tokenizer(UpperCamelCase_      ,       return_tensors='tf'		).input_ids

			snake_case__	       =   model.generate(UpperCamelCase_      ,       max_length=10		)

			snake_case__	       =   tokenizer.batch_decode(UpperCamelCase_      ,       skip_special_tokens=UpperCamelCase_		)
			predicted_outputs += generated_string

		self.assertListEqual(UpperCamelCase_      ,       UpperCamelCase_		)





	def   _snake_case   (		self		)      ->		List[str]:
		snake_case__	       =   'facebook/opt-350m'

		snake_case__	       =   GPTaTokenizer.from_pretrained(UpperCamelCase_		)
		snake_case__	       =   TFOPTForCausalLM.from_pretrained(UpperCamelCase_		)

		snake_case__	       =   'left'

		# use different length sentences to test batching
		snake_case__	       =   [
		    'Hello, my dog is a little',
		    'Today, I',
		]

		snake_case__	       =   tokenizer(UpperCamelCase_      ,       return_tensors='tf'      ,       padding=UpperCamelCase_		)
		snake_case__	       =   inputs['input_ids']

		snake_case__	       =   model.generate(input_ids=UpperCamelCase_      ,       attention_mask=inputs['attention_mask']		)

		snake_case__	       =   tokenizer(sentences[0]      ,       return_tensors='tf'		).input_ids
		snake_case__	       =   model.generate(input_ids=UpperCamelCase_		)

		snake_case__	       =   inputs_non_padded.shape[-1] - tf.math.reduce_sum(
		    tf.cast(inputs['attention_mask'][-1]      ,       tf.intaa		)		)
		snake_case__	       =   tokenizer(sentences[1]      ,       return_tensors='tf'		).input_ids
		snake_case__	       =   model.generate(input_ids=UpperCamelCase_      ,       max_length=model.config.max_length - num_paddings		)

		snake_case__	       =   tokenizer.batch_decode(UpperCamelCase_      ,       skip_special_tokens=UpperCamelCase_		)
		snake_case__	       =   tokenizer.decode(output_non_padded[0]      ,       skip_special_tokens=UpperCamelCase_		)
		snake_case__	       =   tokenizer.decode(output_padded[0]      ,       skip_special_tokens=UpperCamelCase_		)

		snake_case__	       =   [
		    'Hello, my dog is a little bit of a dork.\nI\'m a little bit',
		    'Today, I was in the middle of a conversation with a friend about the',
		]
		self.assertListEqual(UpperCamelCase_      ,       UpperCamelCase_		)
		self.assertListEqual(UpperCamelCase_      ,       [non_padded_sentence, padded_sentence]		)





	def   _snake_case   (		self		)      ->		Optional[int]:
		snake_case__	       =   'facebook/opt-350m'

		snake_case__	       =   [
		    'Today is a beautiful day and I want to',
		    'In the city of San Francisco, the city',
		    'Paris is the capital of France and the capital',
		    'Computers and mobile phones have taken over the',
		]

		snake_case__	       =   []
		snake_case__	       =   GPTaTokenizer.from_pretrained(UpperCamelCase_		)
		snake_case__	       =   TFOPTForCausalLM.from_pretrained(UpperCamelCase_		)

		for prompt in self.prompts:
			snake_case__	       =   tokenizer(UpperCamelCase_      ,       return_tensors='tf'		).input_ids

			snake_case__	       =   model.generate(UpperCamelCase_      ,       max_length=10		)

			snake_case__	       =   tokenizer.batch_decode(UpperCamelCase_      ,       skip_special_tokens=UpperCamelCase_		)
			predicted_outputs += generated_string

		self.assertListEqual(UpperCamelCase_      ,       UpperCamelCase_		)










'''simple docstring'''




import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
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 				__snake_case     (					unittest.TestCase							):





	def   _snake_case   (		self		)      ->		Dict:
		# clean up the VRAM after each test
		super().tearDown()
		gc.collect()
		torch.cuda.empty_cache()





	@property
	def   _snake_case   (		self		)      ->		str:
		snake_case__	       =   1
		snake_case__	       =   3
		snake_case__	       =   (32, 32)

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





	@property
	def   _snake_case   (		self		)      ->		Union[str, Any]:
		torch.manual_seed(0		)
		snake_case__	       =   UNetaDConditionModel(
		    block_out_channels=(32, 32, 64)      ,       layers_per_block=2      ,       sample_size=32      ,       in_channels=7      ,       out_channels=4      ,       down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D', 'CrossAttnDownBlock2D')      ,       up_block_types=('CrossAttnUpBlock2D', 'CrossAttnUpBlock2D', 'UpBlock2D')      ,       cross_attention_dim=32      ,       attention_head_dim=8      ,       use_linear_projection=UpperCamelCase_      ,       only_cross_attention=(True, True, False)      ,       num_class_embeds=100      ,       )
		return model





	@property
	def   _snake_case   (		self		)      ->		Optional[Any]:
		torch.manual_seed(0		)
		snake_case__	       =   AutoencoderKL(
		    block_out_channels=[32, 32, 64]      ,       in_channels=3      ,       out_channels=3      ,       down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D']      ,       up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D', 'UpDecoderBlock2D']      ,       latent_channels=4      ,       )
		return model





	@property
	def   _snake_case   (		self		)      ->		List[str]:
		torch.manual_seed(0		)
		snake_case__	       =   CLIPTextConfig(
		    bos_token_id=0      ,       eos_token_id=2      ,       hidden_size=32      ,       intermediate_size=37      ,       layer_norm_eps=1E-0_5      ,       num_attention_heads=4      ,       num_hidden_layers=5      ,       pad_token_id=1      ,       vocab_size=1000      ,       hidden_act='gelu'      ,       projection_dim=512      ,       )
		return CLIPTextModel(UpperCamelCase_		)





	def   _snake_case   (		self		)      ->		Union[str, Any]:
		snake_case__	       =   'cpu'  # ensure determinism for the device-dependent torch.Generator
		snake_case__	       =   self.dummy_cond_unet_upscale
		snake_case__	       =   DDPMScheduler()
		snake_case__	       =   DDIMScheduler(prediction_type='v_prediction'		)
		snake_case__	       =   self.dummy_vae
		snake_case__	       =   self.dummy_text_encoder
		snake_case__	       =   CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'		)

		snake_case__	       =   self.dummy_image.cpu().permute(0      ,       2      ,       3      ,       1		)[0]
		snake_case__	       =   Image.fromarray(np.uinta(UpperCamelCase_		)		).convert('RGB'		).resize((64, 64)		)

		# make sure here that pndm scheduler skips prk
		snake_case__	       =   StableDiffusionUpscalePipeline(
		    unet=UpperCamelCase_      ,       low_res_scheduler=UpperCamelCase_      ,       scheduler=UpperCamelCase_      ,       vae=UpperCamelCase_      ,       text_encoder=UpperCamelCase_      ,       tokenizer=UpperCamelCase_      ,       max_noise_level=350      ,       )
		snake_case__	       =   sd_pipe.to(UpperCamelCase_		)
		sd_pipe.set_progress_bar_config(disable=UpperCamelCase_		)

		snake_case__	       =   'A painting of a squirrel eating a burger'
		snake_case__	       =   torch.Generator(device=UpperCamelCase_		).manual_seed(0		)
		snake_case__	       =   sd_pipe(
		    [prompt]      ,       image=UpperCamelCase_      ,       generator=UpperCamelCase_      ,       guidance_scale=6.0      ,       noise_level=20      ,       num_inference_steps=2      ,       output_type='np'      ,       )

		snake_case__	       =   output.images

		snake_case__	       =   torch.Generator(device=UpperCamelCase_		).manual_seed(0		)
		snake_case__	       =   sd_pipe(
		    [prompt]      ,       image=UpperCamelCase_      ,       generator=UpperCamelCase_      ,       guidance_scale=6.0      ,       noise_level=20      ,       num_inference_steps=2      ,       output_type='np'      ,       return_dict=UpperCamelCase_      ,       )[0]

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

		snake_case__	       =   low_res_image.size[0] * 4
		assert image.shape == (1, expected_height_width, expected_height_width, 3)
		snake_case__	       =   np.array([0.3_1_1_3, 0.3_9_1_0, 0.4_2_7_2, 0.4_8_5_9, 0.5_0_6_1, 0.4_6_5_2, 0.5_3_6_2, 0.5_7_1_5, 0.5_6_6_1]		)

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





	def   _snake_case   (		self		)      ->		List[str]:
		snake_case__	       =   'cpu'  # ensure determinism for the device-dependent torch.Generator
		snake_case__	       =   self.dummy_cond_unet_upscale
		snake_case__	       =   DDPMScheduler()
		snake_case__	       =   DDIMScheduler(prediction_type='v_prediction'		)
		snake_case__	       =   self.dummy_vae
		snake_case__	       =   self.dummy_text_encoder
		snake_case__	       =   CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'		)

		snake_case__	       =   self.dummy_image.cpu().permute(0      ,       2      ,       3      ,       1		)[0]
		snake_case__	       =   Image.fromarray(np.uinta(UpperCamelCase_		)		).convert('RGB'		).resize((64, 64)		)

		# make sure here that pndm scheduler skips prk
		snake_case__	       =   StableDiffusionUpscalePipeline(
		    unet=UpperCamelCase_      ,       low_res_scheduler=UpperCamelCase_      ,       scheduler=UpperCamelCase_      ,       vae=UpperCamelCase_      ,       text_encoder=UpperCamelCase_      ,       tokenizer=UpperCamelCase_      ,       max_noise_level=350      ,       )
		snake_case__	       =   sd_pipe.to(UpperCamelCase_		)
		sd_pipe.set_progress_bar_config(disable=UpperCamelCase_		)

		snake_case__	       =   'A painting of a squirrel eating a burger'
		snake_case__	       =   sd_pipe(
		    2 * [prompt]      ,       image=2 * [low_res_image]      ,       guidance_scale=6.0      ,       noise_level=20      ,       num_inference_steps=2      ,       output_type='np'      ,       )
		snake_case__	       =   output.images
		assert image.shape[0] == 2

		snake_case__	       =   torch.Generator(device=UpperCamelCase_		).manual_seed(0		)
		snake_case__	       =   sd_pipe(
		    [prompt]      ,       image=UpperCamelCase_      ,       generator=UpperCamelCase_      ,       num_images_per_prompt=2      ,       guidance_scale=6.0      ,       noise_level=20      ,       num_inference_steps=2      ,       output_type='np'      ,       )
		snake_case__	       =   output.images
		assert image.shape[0] == 2





	@unittest.skipIf(torch_device != 'cuda'      ,       'This test requires a GPU'		)
	def   _snake_case   (		self		)      ->		str:
		snake_case__	       =   self.dummy_cond_unet_upscale
		snake_case__	       =   DDPMScheduler()
		snake_case__	       =   DDIMScheduler(prediction_type='v_prediction'		)
		snake_case__	       =   self.dummy_vae
		snake_case__	       =   self.dummy_text_encoder
		snake_case__	       =   CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'		)

		snake_case__	       =   self.dummy_image.cpu().permute(0      ,       2      ,       3      ,       1		)[0]
		snake_case__	       =   Image.fromarray(np.uinta(UpperCamelCase_		)		).convert('RGB'		).resize((64, 64)		)

		# put models in fp16, except vae as it overflows in fp16
		snake_case__	       =   unet.half()
		snake_case__	       =   text_encoder.half()

		# make sure here that pndm scheduler skips prk
		snake_case__	       =   StableDiffusionUpscalePipeline(
		    unet=UpperCamelCase_      ,       low_res_scheduler=UpperCamelCase_      ,       scheduler=UpperCamelCase_      ,       vae=UpperCamelCase_      ,       text_encoder=UpperCamelCase_      ,       tokenizer=UpperCamelCase_      ,       max_noise_level=350      ,       )
		snake_case__	       =   sd_pipe.to(UpperCamelCase_		)
		sd_pipe.set_progress_bar_config(disable=UpperCamelCase_		)

		snake_case__	       =   'A painting of a squirrel eating a burger'
		snake_case__	       =   torch.manual_seed(0		)
		snake_case__	       =   sd_pipe(
		    [prompt]      ,       image=UpperCamelCase_      ,       generator=UpperCamelCase_      ,       num_inference_steps=2      ,       output_type='np'      ,       ).images

		snake_case__	       =   low_res_image.size[0] * 4
		assert image.shape == (1, expected_height_width, expected_height_width, 3)






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





	def   _snake_case   (		self		)      ->		List[str]:
		# clean up the VRAM after each test
		super().tearDown()
		gc.collect()
		torch.cuda.empty_cache()





	def   _snake_case   (		self		)      ->		Optional[int]:
		snake_case__	       =   load_image(
		    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
		    '/sd2-upscale/low_res_cat.png'		)
		snake_case__	       =   load_numpy(
		    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'
		    '/upsampled_cat.npy'		)

		snake_case__	       =   'stabilityai/stable-diffusion-x4-upscaler'
		snake_case__	       =   StableDiffusionUpscalePipeline.from_pretrained(UpperCamelCase_		)
		pipe.to(UpperCamelCase_		)
		pipe.set_progress_bar_config(disable=UpperCamelCase_		)
		pipe.enable_attention_slicing()

		snake_case__	       =   'a cat sitting on a park bench'

		snake_case__	       =   torch.manual_seed(0		)
		snake_case__	       =   pipe(
		    prompt=UpperCamelCase_      ,       image=UpperCamelCase_      ,       generator=UpperCamelCase_      ,       output_type='np'      ,       )
		snake_case__	       =   output.images[0]

		assert image.shape == (512, 512, 3)
		assert np.abs(expected_image - image		).max() < 1E-3





	def   _snake_case   (		self		)      ->		List[Any]:
		snake_case__	       =   load_image(
		    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
		    '/sd2-upscale/low_res_cat.png'		)
		snake_case__	       =   load_numpy(
		    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'
		    '/upsampled_cat_fp16.npy'		)

		snake_case__	       =   'stabilityai/stable-diffusion-x4-upscaler'
		snake_case__	       =   StableDiffusionUpscalePipeline.from_pretrained(
		    UpperCamelCase_      ,       torch_dtype=torch.floataa      ,       )
		pipe.to(UpperCamelCase_		)
		pipe.set_progress_bar_config(disable=UpperCamelCase_		)
		pipe.enable_attention_slicing()

		snake_case__	       =   'a cat sitting on a park bench'

		snake_case__	       =   torch.manual_seed(0		)
		snake_case__	       =   pipe(
		    prompt=UpperCamelCase_      ,       image=UpperCamelCase_      ,       generator=UpperCamelCase_      ,       output_type='np'      ,       )
		snake_case__	       =   output.images[0]

		assert image.shape == (512, 512, 3)
		assert np.abs(expected_image - image		).max() < 5E-1





	def   _snake_case   (		self		)      ->		int:
		torch.cuda.empty_cache()
		torch.cuda.reset_max_memory_allocated()
		torch.cuda.reset_peak_memory_stats()

		snake_case__	       =   load_image(
		    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
		    '/sd2-upscale/low_res_cat.png'		)

		snake_case__	       =   'stabilityai/stable-diffusion-x4-upscaler'
		snake_case__	       =   StableDiffusionUpscalePipeline.from_pretrained(
		    UpperCamelCase_      ,       torch_dtype=torch.floataa      ,       )
		pipe.to(UpperCamelCase_		)
		pipe.set_progress_bar_config(disable=UpperCamelCase_		)
		pipe.enable_attention_slicing(1		)
		pipe.enable_sequential_cpu_offload()

		snake_case__	       =   'a cat sitting on a park bench'

		snake_case__	       =   torch.manual_seed(0		)
		snake_case__	       =   pipe(
		    prompt=UpperCamelCase_      ,       image=UpperCamelCase_      ,       generator=UpperCamelCase_      ,       num_inference_steps=5      ,       output_type='np'      ,       )

		snake_case__	       =   torch.cuda.max_memory_allocated()
		# make sure that less than 2.9 GB is allocated
		assert mem_bytes < 2.9 * 10**9



