


"""simple docstring"""






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 ):


  def 							_lowercase					(			self:						List[Any]    ):







     '''simple docstring'''







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


  @property
  def 							_lowercase					(			self:						Optional[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Union[str, Any]							=     1
     _lowerCamelCase   :				int							=     3
     _lowerCamelCase   :				Tuple							=     (32, 32)

     _lowerCamelCase   :				Optional[int]							=     floats_tensor((batch_size, num_channels) + sizes							,rng=random.Random(0    )    ).to(__lowerCAmelCase    )
     return image


  @property
  def 							_lowercase					(			self:						Optional[Any]    ):







     '''simple docstring'''







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







     '''simple docstring'''







     torch.manual_seed(0    )
     _lowerCamelCase   :				Dict							=     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 							_lowercase					(			self:						Optional[int]    ):







     '''simple docstring'''







     torch.manual_seed(0    )
     _lowerCamelCase   :				Any							=     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=5_006							,)
     return RobertaSeriesModelWithTransformation(__lowerCAmelCase    )


  @property
  def 							_lowercase					(			self:						Union[str, Any]    ):







     '''simple docstring'''







     def extract(*__lowerCAmelCase:						Tuple							,**__lowerCAmelCase:						Tuple    ):




        class 							A_      :


          def __init__(			self:						List[str]    ):







             '''simple docstring'''







             _lowerCamelCase   :				Any							=     torch.ones([0]    )






          def 							_lowercase					(			self:						Union[str, Any]							,__lowerCAmelCase:						List[Any]    ):







             '''simple docstring'''







             self.pixel_values.to(__lowerCAmelCase    )
             return self


        return Out()

     return extract


  def 							_lowercase					(			self:						Any    ):







     '''simple docstring'''







     _lowerCamelCase   :				str							=     "cpu"  # ensure determinism for the device-dependent torch.Generator
     _lowerCamelCase   :				Any							=     self.dummy_cond_unet
     _lowerCamelCase   :				int							=     PNDMScheduler(skip_prk_steps=__lowerCAmelCase    )
     _lowerCamelCase   :				Optional[Any]							=     self.dummy_vae
     _lowerCamelCase   :				List[str]							=     self.dummy_text_encoder
     _lowerCamelCase   :				Optional[Any]							=     XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"    )
     _lowerCamelCase   :				Union[str, Any]							=     77

     _lowerCamelCase   :				List[Any]							=     self.dummy_image.to(__lowerCAmelCase    )
     _lowerCamelCase   :				List[str]							=     init_image / 2 + 0.5

     # make sure here that pndm scheduler skips prk
     _lowerCamelCase   :				int							=     AltDiffusionImgaImgPipeline(
         unet=__lowerCAmelCase							,scheduler=__lowerCAmelCase							,vae=__lowerCAmelCase							,text_encoder=__lowerCAmelCase							,tokenizer=__lowerCAmelCase							,safety_checker=__lowerCAmelCase							,feature_extractor=self.dummy_extractor							,)
     _lowerCamelCase   :				List[str]							=     VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor							,do_normalize=__lowerCAmelCase    )
     _lowerCamelCase   :				Union[str, Any]							=     alt_pipe.to(__lowerCAmelCase    )
     alt_pipe.set_progress_bar_config(disable=__lowerCAmelCase    )

     _lowerCamelCase   :				Dict							=     "A painting of a squirrel eating a burger"
     _lowerCamelCase   :				List[str]							=     torch.Generator(device=__lowerCAmelCase    ).manual_seed(0    )
     _lowerCamelCase   :				int							=     alt_pipe(
         [prompt]							,generator=__lowerCAmelCase							,guidance_scale=6.0							,num_inference_steps=2							,output_type="np"							,image=__lowerCAmelCase							,)

     _lowerCamelCase   :				Dict							=     output.images

     _lowerCamelCase   :				Any							=     torch.Generator(device=__lowerCAmelCase    ).manual_seed(0    )
     _lowerCamelCase   :				Union[str, Any]							=     alt_pipe(
         [prompt]							,generator=__lowerCAmelCase							,guidance_scale=6.0							,num_inference_steps=2							,output_type="np"							,image=__lowerCAmelCase							,return_dict=__lowerCAmelCase							,)[0]

     _lowerCamelCase   :				List[Any]							=     image[0, -3:, -3:, -1]
     _lowerCamelCase   :				Tuple							=     image_from_tuple[0, -3:, -3:, -1]

     assert image.shape == (1, 32, 32, 3)
     _lowerCamelCase   :				List[str]							=     np.array([0.44_27, 0.37_31, 0.42_49, 0.49_41, 0.45_46, 0.41_48, 0.41_93, 0.46_66, 0.44_99]    )

     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 							_lowercase					(			self:						str    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     self.dummy_cond_unet
     _lowerCamelCase   :				str							=     PNDMScheduler(skip_prk_steps=__lowerCAmelCase    )
     _lowerCamelCase   :				int							=     self.dummy_vae
     _lowerCamelCase   :				int							=     self.dummy_text_encoder
     _lowerCamelCase   :				List[Any]							=     XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"    )
     _lowerCamelCase   :				Tuple							=     77

     _lowerCamelCase   :				Any							=     self.dummy_image.to(__lowerCAmelCase    )

     # put models in fp16
     _lowerCamelCase   :				List[str]							=     unet.half()
     _lowerCamelCase   :				Union[str, Any]							=     vae.half()
     _lowerCamelCase   :				Union[str, Any]							=     bert.half()

     # make sure here that pndm scheduler skips prk
     _lowerCamelCase   :				Optional[Any]							=     AltDiffusionImgaImgPipeline(
         unet=__lowerCAmelCase							,scheduler=__lowerCAmelCase							,vae=__lowerCAmelCase							,text_encoder=__lowerCAmelCase							,tokenizer=__lowerCAmelCase							,safety_checker=__lowerCAmelCase							,feature_extractor=self.dummy_extractor							,)
     _lowerCamelCase   :				int							=     VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor							,do_normalize=__lowerCAmelCase    )
     _lowerCamelCase   :				Optional[Any]							=     alt_pipe.to(__lowerCAmelCase    )
     alt_pipe.set_progress_bar_config(disable=__lowerCAmelCase    )

     _lowerCamelCase   :				int							=     "A painting of a squirrel eating a burger"
     _lowerCamelCase   :				List[Any]							=     torch.manual_seed(0    )
     _lowerCamelCase   :				Optional[int]							=     alt_pipe(
         [prompt]							,generator=__lowerCAmelCase							,num_inference_steps=2							,output_type="np"							,image=__lowerCAmelCase							,).images

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






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







     '''simple docstring'''







     _lowerCamelCase   :				Optional[Any]							=     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
     _lowerCamelCase   :				List[str]							=     init_image.resize((760, 504)    )

     _lowerCamelCase   :				Any							=     "BAAI/AltDiffusion"
     _lowerCamelCase   :				Optional[int]							=     AltDiffusionImgaImgPipeline.from_pretrained(
         __lowerCAmelCase							,safety_checker=__lowerCAmelCase							,)
     pipe.to(__lowerCAmelCase    )
     pipe.set_progress_bar_config(disable=__lowerCAmelCase    )
     pipe.enable_attention_slicing()

     _lowerCamelCase   :				Optional[Any]							=     "A fantasy landscape, trending on artstation"

     _lowerCamelCase   :				List[Any]							=     torch.manual_seed(0    )
     _lowerCamelCase   :				Union[str, Any]							=     pipe(
         prompt=__lowerCAmelCase							,image=__lowerCAmelCase							,strength=0.75							,guidance_scale=7.5							,generator=__lowerCAmelCase							,output_type="np"							,)
     _lowerCamelCase   :				Any							=     output.images[0]

     _lowerCamelCase   :				Optional[int]							=     image[255:258, 383:386, -1]

     assert image.shape == (504, 760, 3)
     _lowerCamelCase   :				List[Any]							=     np.array([0.93_58, 0.93_97, 0.95_99, 0.99_01, 1.00_00, 1.00_00, 0.98_82, 1.00_00, 1.00_00]    )

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




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


  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







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






  def 							_lowercase					(			self:						Union[str, Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[Any]							=     load_image(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
         "/img2img/sketch-mountains-input.jpg"    )
     _lowerCamelCase   :				Union[str, Any]							=     init_image.resize((768, 512)    )
     _lowerCamelCase   :				Dict							=     load_numpy(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy"    )

     _lowerCamelCase   :				Dict							=     "BAAI/AltDiffusion"
     _lowerCamelCase   :				str							=     AltDiffusionImgaImgPipeline.from_pretrained(
         __lowerCAmelCase							,safety_checker=__lowerCAmelCase							,)
     pipe.to(__lowerCAmelCase    )
     pipe.set_progress_bar_config(disable=__lowerCAmelCase    )
     pipe.enable_attention_slicing()

     _lowerCamelCase   :				Optional[int]							=     "A fantasy landscape, trending on artstation"

     _lowerCamelCase   :				Optional[Any]							=     torch.manual_seed(0    )
     _lowerCamelCase   :				List[str]							=     pipe(
         prompt=__lowerCAmelCase							,image=__lowerCAmelCase							,strength=0.75							,guidance_scale=7.5							,generator=__lowerCAmelCase							,output_type="np"							,)
     _lowerCamelCase   :				List[Any]							=     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



def       _lowerCAmelCase    (				lowerCAmelCase_				:int		,    lowerCAmelCase_				:int				)->int:
   '''simple docstring'''






   return int((input_a, input_a).count(0				) == 0				)



def       _lowerCAmelCase    (				)->None:
   '''simple docstring'''






   assert and_gate(0		,    0				) == 0
   assert and_gate(0		,    1				) == 0
   assert and_gate(1		,    0				) == 0
   assert and_gate(1		,    1				) == 1


if __name__ == "__main__":
   test_and_gate()
   print(and_gate(1, 0))
   print(and_gate(0, 0))
   print(and_gate(0, 1))
   print(and_gate(1, 1))




