
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 			UpperCamelCase__		(     unittest.TestCase ):







	'''simple docstring'''





	@property
	def 	__snake_case					(      self		):
					A__	:						Dict								=				1
					A__	:						int								=				3
					A__	:						str								=				(32, 32)

					A__	:						List[str]								=				floats_tensor((batch_size, num_channels) + sizes					,							rng=random.Random(0		)		).to(_SCREAMING_SNAKE_CASE		)
					return image





	@property
	def 	__snake_case					(      self		):
					torch.manual_seed(0		)
					A__	:						Tuple								=				UNetaDModel(
					    block_out_channels=(32, 64)					,							layers_per_block=2					,							sample_size=32					,							in_channels=6					,							out_channels=3					,							down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')					,							up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')					,							)
					return model





	@property
	def 	__snake_case					(      self		):
					torch.manual_seed(0		)
					A__	:						str								=				VQModel(
					    block_out_channels=[32, 64]					,							in_channels=3					,							out_channels=3					,							down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']					,							up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']					,							latent_channels=3					,							)
					return model





	def 	__snake_case					(      self		):
					A__	:						Any								=				'''cpu'''
					A__	:						Any								=				self.dummy_uncond_unet
					A__	:						str								=				DDIMScheduler()
					A__	:						Optional[Any]								=				self.dummy_vq_model

					A__	:						Tuple								=				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		)

					A__	:						List[str]								=				self.dummy_image.to(_SCREAMING_SNAKE_CASE		)

					A__	:						int								=				torch.Generator(device=_SCREAMING_SNAKE_CASE		).manual_seed(0		)
					A__	:						Tuple								=				ldm(image=_SCREAMING_SNAKE_CASE					,							generator=_SCREAMING_SNAKE_CASE					,							num_inference_steps=2					,							output_type='''numpy'''		).images

					A__	:						List[str]								=				image[0, -3:, -3:, -1]

					assert image.shape == (1, 64, 64, 3)
					A__	:						Optional[int]								=				np.array([0.8_6_7_8, 0.8_2_4_5, 0.6_3_8_1, 0.6_8_3_0, 0.4_3_8_5, 0.5_5_9_9, 0.4_6_4_1, 0.6_2_0_1, 0.5_1_5_0]		)

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





	@unittest.skipIf(torch_device != '''cuda'''					,							'''This test requires a GPU'''		)
	def 	__snake_case					(      self		):
					A__	:						List[str]								=				self.dummy_uncond_unet
					A__	:						Union[str, Any]								=				DDIMScheduler()
					A__	:						int								=				self.dummy_vq_model

					# put models in fp16
					A__	:						str								=				unet.half()
					A__	:						str								=				vqvae.half()

					A__	:						Optional[int]								=				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		)

					A__	:						Dict								=				self.dummy_image.to(_SCREAMING_SNAKE_CASE		)

					A__	:						Tuple								=				ldm(_SCREAMING_SNAKE_CASE					,							num_inference_steps=2					,							output_type='''numpy'''		).images

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







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







	'''simple docstring'''





	def 	__snake_case					(      self		):
					A__	:						List[Any]								=				load_image(
					    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
					    '''/vq_diffusion/teddy_bear_pool.png'''		)
					A__	:						int								=				init_image.resize((64, 64)					,							resample=PIL_INTERPOLATION['''lanczos''']		)

					A__	:						int								=				LDMSuperResolutionPipeline.from_pretrained('''duongna/ldm-super-resolution'''					,							device_map='''auto'''		)
					ldm.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

					A__	:						int								=				torch.manual_seed(0		)
					A__	:						str								=				ldm(image=_SCREAMING_SNAKE_CASE					,							generator=_SCREAMING_SNAKE_CASE					,							num_inference_steps=20					,							output_type='''numpy'''		).images

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

					assert image.shape == (1, 256, 256, 3)
					A__	:						Any								=				np.array([0.7_6_4_4, 0.7_6_7_9, 0.7_6_4_2, 0.7_6_3_3, 0.7_6_6_6, 0.7_5_6_0, 0.7_4_2_5, 0.7_2_5_7, 0.6_9_0_7]		)

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







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


_SCREAMING_SNAKE_CASE	:		str   =   logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE	:		Any   =   {
    '''facebook/opt-125m''': '''https://huggingface.co/facebook/opt-125m/blob/main/config.json''',
    '''facebook/opt-350m''': '''https://huggingface.co/facebook/opt-350m/blob/main/config.json''',
    '''facebook/opt-1.3b''': '''https://huggingface.co/facebook/opt-1.3b/blob/main/config.json''',
    '''facebook/opt-2.7b''': '''https://huggingface.co/facebook/opt-2.7b/blob/main/config.json''',
    '''facebook/opt-6.7b''': '''https://huggingface.co/facebook/opt-6.7b/blob/main/config.json''',
    '''facebook/opt-13b''': '''https://huggingface.co/facebook/opt-13b/blob/main/config.json''',
}






class      _SCREAMING_SNAKE_CASE							(			UpperCAmelCase				):



   '''simple docstring'''



   __UpperCAmelCase	:							Tuple    =    "opt"
   __UpperCAmelCase	:							Any    =    ["past_key_values"]







   def __init__(	self ,				_SCREAMING_SNAKE_CASE=50272 ,				_SCREAMING_SNAKE_CASE=768 ,				_SCREAMING_SNAKE_CASE=12 ,				_SCREAMING_SNAKE_CASE=3072 ,				_SCREAMING_SNAKE_CASE=2048 ,				_SCREAMING_SNAKE_CASE=True ,				_SCREAMING_SNAKE_CASE=False ,				_SCREAMING_SNAKE_CASE=None ,				_SCREAMING_SNAKE_CASE=0.1 ,				_SCREAMING_SNAKE_CASE=0.0 ,				_SCREAMING_SNAKE_CASE=12 ,				_SCREAMING_SNAKE_CASE="relu" ,				_SCREAMING_SNAKE_CASE=0.0 ,				_SCREAMING_SNAKE_CASE=0.02 ,				_SCREAMING_SNAKE_CASE=True ,				_SCREAMING_SNAKE_CASE=1 ,				_SCREAMING_SNAKE_CASE=2 ,				_SCREAMING_SNAKE_CASE=2 ,				_SCREAMING_SNAKE_CASE=True ,				_SCREAMING_SNAKE_CASE=True ,				**_SCREAMING_SNAKE_CASE ,				):


      '''simple docstring'''

      super().__init__(
          pad_token_id=_SCREAMING_SNAKE_CASE ,				bos_token_id=_SCREAMING_SNAKE_CASE ,				eos_token_id=_SCREAMING_SNAKE_CASE ,				**_SCREAMING_SNAKE_CASE ,				)
      __lowerCamelCase    		=					vocab_size
      __lowerCamelCase    		=					max_position_embeddings
      __lowerCamelCase    		=					num_attention_heads
      __lowerCamelCase    		=					word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
      __lowerCamelCase    		=					ffn_dim
      __lowerCamelCase    		=					hidden_size
      __lowerCamelCase    		=					num_hidden_layers
      __lowerCamelCase    		=					dropout
      __lowerCamelCase    		=					attention_dropout
      __lowerCamelCase    		=					activation_function
      __lowerCamelCase    		=					init_std
      __lowerCamelCase    		=					layerdrop
      __lowerCamelCase    		=					use_cache
      __lowerCamelCase    		=					do_layer_norm_before
      # We keep these variables at `True` for backward compatibility.
      __lowerCamelCase    		=					enable_bias
      __lowerCamelCase    		=					layer_norm_elementwise_affine

      # Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
      # with checkpoints that have been fine-tuned before transformers v4.20.1
      # see https://github.com/facebookresearch/metaseq/pull/164
      __lowerCamelCase    		=					_remove_final_layer_norm






