




'''simple docstring'''


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






      @property
      def        lowercase_	(   self      :     List[str]					):


           '''simple docstring'''

           a_    :       Optional[Any]    	=     1
           a_    :       Union[str, Any]    	=     3
           a_    :       Any    	=     (32, 32)

           a_    :       Tuple    	=     floats_tensor((batch_size, num_channels) + sizes	,	rng=random.Random(0					)					).to(A_					)
           return image






      @property
      def        lowercase_	(   self      :     str					):


           '''simple docstring'''

           torch.manual_seed(0					)
           a_    :       List[str]    	=     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        lowercase_	(   self      :     Any					):


           '''simple docstring'''

           torch.manual_seed(0					)
           a_    :       int    	=     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        lowercase_	(   self      :     Tuple					):


           '''simple docstring'''

           a_    :       List[str]    	=     """cpu"""
           a_    :       Dict    	=     self.dummy_uncond_unet
           a_    :       Dict    	=     DDIMScheduler()
           a_    :       Dict    	=     self.dummy_vq_model

           a_    :       Union[str, Any]    	=     LDMSuperResolutionPipeline(unet=A_	,	vqvae=A_	,	scheduler=A_					)
           ldm.to(A_					)
           ldm.set_progress_bar_config(disable=A_					)

           a_    :       Tuple    	=     self.dummy_image.to(A_					)

           a_    :       int    	=     torch.Generator(device=A_					).manual_seed(0					)
           a_    :       str    	=     ldm(image=A_	,	generator=A_	,	num_inference_steps=2	,	output_type="""numpy"""					).images

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

           assert image.shape == (1, 64, 64, 3)
           a_    :       List[str]    	=     np.array([0.8678, 0.8245, 0.6381, 0.6830, 0.4385, 0.5599, 0.4641, 0.6201, 0.5150]					)

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






      @unittest.skipIf(torch_device != """cuda"""	,	"""This test requires a GPU"""					)
      def        lowercase_	(   self      :     List[str]					):


           '''simple docstring'''

           a_    :       int    	=     self.dummy_uncond_unet
           a_    :       Optional[Any]    	=     DDIMScheduler()
           a_    :       Union[str, Any]    	=     self.dummy_vq_model

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

           a_    :       Any    	=     LDMSuperResolutionPipeline(unet=A_	,	vqvae=A_	,	scheduler=A_					)
           ldm.to(A_					)
           ldm.set_progress_bar_config(disable=A_					)

           a_    :       Any    	=     self.dummy_image.to(A_					)

           a_    :       int    	=     ldm(A_	,	num_inference_steps=2	,	output_type="""numpy"""					).images

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







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






      def        lowercase_	(   self      :     Optional[Any]					):


           '''simple docstring'''

           a_    :       Any    	=     load_image(
               """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
               """/vq_diffusion/teddy_bear_pool.png"""					)
           a_    :       Tuple    	=     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=A_					)

           a_    :       int    	=     torch.manual_seed(0					)
           a_    :       List[str]    	=     ldm(image=A_	,	generator=A_	,	num_inference_steps=20	,	output_type="""numpy"""					).images

           a_    :       Tuple    	=     image[0, -3:, -3:, -1]

           assert image.shape == (1, 256, 256, 3)
           a_    :       Union[str, Any]    	=     np.array([0.7644, 0.7679, 0.7642, 0.7633, 0.7666, 0.7560, 0.7425, 0.7257, 0.6907]					)

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






'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCAmelCase_			:     int							    =     {
    'configuration_dpr': ['DPR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DPRConfig'],
    'tokenization_dpr': [
        'DPRContextEncoderTokenizer',
        'DPRQuestionEncoderTokenizer',
        'DPRReaderOutput',
        'DPRReaderTokenizer',
    ],
}


try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase_			:     Union[str, Any]							    =     [
     'DPRContextEncoderTokenizerFast',
     'DPRQuestionEncoderTokenizerFast',
     'DPRReaderTokenizerFast',
 ]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase_			:     List[str]							    =     [
     'DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
     'DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
     'DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST',
     'DPRContextEncoder',
     'DPRPretrainedContextEncoder',
     'DPRPreTrainedModel',
     'DPRPretrainedQuestionEncoder',
     'DPRPretrainedReader',
     'DPRQuestionEncoder',
     'DPRReader',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase_			:     Dict							    =     [
     'TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TFDPRContextEncoder',
     'TFDPRPretrainedContextEncoder',
     'TFDPRPretrainedQuestionEncoder',
     'TFDPRPretrainedReader',
     'TFDPRQuestionEncoder',
     'TFDPRReader',
 ]


if TYPE_CHECKING:
 from .configuration_dpr import DPR_PRETRAINED_CONFIG_ARCHIVE_MAP, DPRConfig
 from .tokenization_dpr import (
     DPRContextEncoderTokenizer,
     DPRQuestionEncoderTokenizer,
     DPRReaderOutput,
     DPRReaderTokenizer,
 )

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_dpr_fast import (
      DPRContextEncoderTokenizerFast,
      DPRQuestionEncoderTokenizerFast,
      DPRReaderTokenizerFast,
  )

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_dpr import (
      DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
      DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
      DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
      DPRContextEncoder,
      DPRPretrainedContextEncoder,
      DPRPreTrainedModel,
      DPRPretrainedQuestionEncoder,
      DPRPretrainedReader,
      DPRQuestionEncoder,
      DPRReader,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_dpr import (
      TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
      TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
      TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFDPRContextEncoder,
      TFDPRPretrainedContextEncoder,
      TFDPRPretrainedQuestionEncoder,
      TFDPRPretrainedReader,
      TFDPRQuestionEncoder,
      TFDPRReader,
  )

else:
 import sys

 lowerCAmelCase_			:     int							    =     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
