





'''simple docstring'''






A__    :       Optional[int]              =[
    9_99,
    8_00,
    7_99,
    6_00,
    5_99,
    5_00,
    4_00,
    3_99,
    3_77,
    3_55,
    3_33,
    3_11,
    2_88,
    2_66,
    2_44,
    2_22,
    2_00,
    1_99,
    1_77,
    1_55,
    1_33,
    1_11,
    88,
    66,
    44,
    22,
    0,
]

A__    :       Tuple              =[
    9_99,
    9_76,
    9_52,
    9_28,
    9_05,
    8_82,
    8_58,
    8_57,
    8_10,
    7_62,
    7_15,
    7_14,
    5_72,
    4_29,
    4_28,
    2_86,
    2_85,
    2_38,
    1_90,
    1_43,
    1_42,
    1_18,
    95,
    71,
    47,
    24,
    0,
]

A__    :       Any              =[
    9_99,
    9_88,
    9_77,
    9_66,
    9_55,
    9_44,
    9_33,
    9_22,
    9_11,
    9_00,
    8_99,
    8_79,
    8_59,
    8_40,
    8_20,
    8_00,
    7_99,
    7_66,
    7_33,
    7_00,
    6_99,
    6_50,
    6_00,
    5_99,
    5_00,
    4_99,
    4_00,
    3_99,
    3_50,
    3_00,
    2_99,
    2_66,
    2_33,
    2_00,
    1_99,
    1_79,
    1_59,
    1_40,
    1_20,
    1_00,
    99,
    88,
    77,
    66,
    55,
    44,
    33,
    22,
    11,
    0,
]

A__    :       List[Any]              =[
    9_99,
    9_95,
    9_92,
    9_89,
    9_85,
    9_81,
    9_78,
    9_75,
    9_71,
    9_67,
    9_64,
    9_61,
    9_57,
    9_56,
    9_51,
    9_47,
    9_42,
    9_37,
    9_33,
    9_28,
    9_23,
    9_19,
    9_14,
    9_13,
    9_08,
    9_03,
    8_97,
    8_92,
    8_87,
    8_81,
    8_76,
    8_71,
    8_70,
    8_64,
    8_58,
    8_52,
    8_46,
    8_40,
    8_34,
    8_28,
    8_27,
    8_20,
    8_13,
    8_06,
    7_99,
    7_92,
    7_85,
    7_84,
    7_77,
    7_70,
    7_63,
    7_56,
    7_49,
    7_42,
    7_41,
    7_33,
    7_24,
    7_16,
    7_07,
    6_99,
    6_98,
    6_88,
    6_77,
    6_66,
    6_56,
    6_55,
    6_45,
    6_34,
    6_23,
    6_13,
    6_12,
    5_98,
    5_84,
    5_70,
    5_69,
    5_55,
    5_41,
    5_27,
    5_26,
    5_05,
    4_84,
    4_83,
    4_62,
    4_40,
    4_39,
    3_96,
    3_95,
    3_52,
    3_51,
    3_08,
    3_07,
    2_64,
    2_63,
    2_20,
    2_19,
    1_76,
    1_32,
    88,
    44,
    0,
]

A__    :       Any              =[
    9_99,
    9_97,
    9_95,
    9_92,
    9_90,
    9_88,
    9_86,
    9_84,
    9_81,
    9_79,
    9_77,
    9_75,
    9_72,
    9_70,
    9_68,
    9_66,
    9_64,
    9_61,
    9_59,
    9_57,
    9_56,
    9_54,
    9_51,
    9_49,
    9_46,
    9_44,
    9_41,
    9_39,
    9_36,
    9_34,
    9_31,
    9_29,
    9_26,
    9_24,
    9_21,
    9_19,
    9_16,
    9_14,
    9_13,
    9_10,
    9_07,
    9_05,
    9_02,
    8_99,
    8_96,
    8_93,
    8_91,
    8_88,
    8_85,
    8_82,
    8_79,
    8_77,
    8_74,
    8_71,
    8_70,
    8_67,
    8_64,
    8_61,
    8_58,
    8_55,
    8_52,
    8_49,
    8_46,
    8_43,
    8_40,
    8_37,
    8_34,
    8_31,
    8_28,
    8_27,
    8_24,
    8_21,
    8_17,
    8_14,
    8_11,
    8_08,
    8_04,
    8_01,
    7_98,
    7_95,
    7_91,
    7_88,
    7_85,
    7_84,
    7_80,
    7_77,
    7_74,
    7_70,
    7_66,
    7_63,
    7_60,
    7_56,
    7_52,
    7_49,
    7_46,
    7_42,
    7_41,
    7_37,
    7_33,
    7_30,
    7_26,
    7_22,
    7_18,
    7_14,
    7_10,
    7_07,
    7_03,
    6_99,
    6_98,
    6_94,
    6_90,
    6_85,
    6_81,
    6_77,
    6_73,
    6_69,
    6_64,
    6_60,
    6_56,
    6_55,
    6_50,
    6_46,
    6_41,
    6_36,
    6_32,
    6_27,
    6_22,
    6_18,
    6_13,
    6_12,
    6_07,
    6_02,
    5_96,
    5_91,
    5_86,
    5_80,
    5_75,
    5_70,
    5_69,
    5_63,
    5_57,
    5_51,
    5_45,
    5_39,
    5_33,
    5_27,
    5_26,
    5_19,
    5_12,
    5_05,
    4_98,
    4_91,
    4_84,
    4_83,
    4_74,
    4_66,
    4_57,
    4_49,
    4_40,
    4_39,
    4_28,
    4_18,
    4_07,
    3_96,
    3_95,
    3_81,
    3_66,
    3_52,
    3_51,
    3_30,
    3_08,
    3_07,
    2_86,
    2_64,
    2_63,
    2_42,
    2_20,
    2_19,
    1_76,
    1_75,
    1_32,
    1_31,
    88,
    44,
    0,
]

A__    :       List[str]              =[
    9_99,
    9_91,
    9_82,
    9_74,
    9_66,
    9_58,
    9_50,
    9_41,
    9_33,
    9_25,
    9_16,
    9_08,
    9_00,
    8_99,
    8_74,
    8_50,
    8_25,
    8_00,
    7_99,
    7_00,
    6_00,
    5_00,
    4_00,
    3_00,
    2_00,
    1_00,
    0,
]

A__    :       Union[str, Any]              =[
    9_99,
    9_92,
    9_85,
    9_78,
    9_71,
    9_64,
    9_57,
    9_49,
    9_42,
    9_35,
    9_28,
    9_21,
    9_14,
    9_07,
    9_00,
    8_99,
    8_79,
    8_59,
    8_40,
    8_20,
    8_00,
    7_99,
    7_66,
    7_33,
    7_00,
    6_99,
    6_50,
    6_00,
    5_99,
    5_00,
    4_99,
    4_00,
    3_99,
    3_00,
    2_99,
    2_00,
    1_99,
    1_00,
    99,
    0,
]

A__    :       Any              =[
    9_99,
    9_96,
    9_92,
    9_89,
    9_85,
    9_82,
    9_79,
    9_75,
    9_72,
    9_68,
    9_65,
    9_61,
    9_58,
    9_55,
    9_51,
    9_48,
    9_44,
    9_41,
    9_38,
    9_34,
    9_31,
    9_27,
    9_24,
    9_20,
    9_17,
    9_14,
    9_10,
    9_07,
    9_03,
    9_00,
    8_99,
    8_91,
    8_84,
    8_76,
    8_69,
    8_61,
    8_53,
    8_46,
    8_38,
    8_30,
    8_23,
    8_15,
    8_08,
    8_00,
    7_99,
    7_88,
    7_77,
    7_66,
    7_55,
    7_44,
    7_33,
    7_22,
    7_11,
    7_00,
    6_99,
    6_88,
    6_77,
    6_66,
    6_55,
    6_44,
    6_33,
    6_22,
    6_11,
    6_00,
    5_99,
    5_85,
    5_71,
    5_57,
    5_42,
    5_28,
    5_14,
    5_00,
    4_99,
    4_85,
    4_71,
    4_57,
    4_42,
    4_28,
    4_14,
    4_00,
    3_99,
    3_79,
    3_59,
    3_40,
    3_20,
    3_00,
    2_99,
    2_79,
    2_59,
    2_40,
    2_20,
    2_00,
    1_99,
    1_66,
    1_33,
    1_00,
    99,
    66,
    33,
    0,
]













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




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




      @property
      def     lowercase__   (      self      :			List[Any]  )    ->				List[str]:
            _lowerCAmelCase												=							1
            _lowerCAmelCase												=							3
            _lowerCAmelCase												=							(32, 32)

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




      @property
      def     lowercase__   (      self      :			int  )    ->				Union[str, Any]:
            torch.manual_seed(0  )
            _lowerCAmelCase												=							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      :			Optional[int]  )    ->				List[str]:
            torch.manual_seed(0  )
            _lowerCAmelCase												=							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      :			Dict  )    ->				Optional[Any]:
            torch.manual_seed(0  )
            _lowerCAmelCase												=							RobertaSeriesConfig(
                hidden_size=32      ,      project_dim=32      ,      intermediate_size=37      ,      layer_norm_eps=1E-0_5      ,      num_attention_heads=4      ,      num_hidden_layers=5      ,      pad_token_id=1      ,      vocab_size=50_06      ,      )
            return RobertaSeriesModelWithTransformation(__snake_case  )




      @property
      def     lowercase__   (      self      :			Union[str, Any]  )    ->				str:
            def extract(*__snake_case      :			List[Any]      ,      **__snake_case      :			Any  ):




                  class 			UpperCAmelCase					:




                        def __init__(      self      :			Any  )    ->				Any:
                              _lowerCAmelCase												=							torch.ones([0]  )




                        def     lowercase__   (      self      :			Optional[Any]      ,      __snake_case      :			Tuple  )    ->				Dict:
                              self.pixel_values.to(__snake_case  )
                              return self




                  return Out()

            return extract




      def     lowercase__   (      self      :			List[str]  )    ->				Optional[int]:
            _lowerCAmelCase												=							"""cpu"""  # ensure determinism for the device-dependent torch.Generator
            _lowerCAmelCase												=							self.dummy_cond_unet
            _lowerCAmelCase												=							PNDMScheduler(skip_prk_steps=__snake_case  )
            _lowerCAmelCase												=							self.dummy_vae
            _lowerCAmelCase												=							self.dummy_text_encoder
            _lowerCAmelCase												=							XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta"""  )
            _lowerCAmelCase												=							77

            _lowerCAmelCase												=							self.dummy_image.to(__snake_case  )
            _lowerCAmelCase												=							init_image / 2 + 0.5

            # make sure here that pndm scheduler skips prk
            _lowerCAmelCase												=							AltDiffusionImgaImgPipeline(
                unet=__snake_case      ,      scheduler=__snake_case      ,      vae=__snake_case      ,      text_encoder=__snake_case      ,      tokenizer=__snake_case      ,      safety_checker=__snake_case      ,      feature_extractor=self.dummy_extractor      ,      )
            _lowerCAmelCase												=							VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor      ,      do_normalize=__snake_case  )
            _lowerCAmelCase												=							alt_pipe.to(__snake_case  )
            alt_pipe.set_progress_bar_config(disable=__snake_case  )

            _lowerCAmelCase												=							"""A painting of a squirrel eating a burger"""
            _lowerCAmelCase												=							torch.Generator(device=__snake_case  ).manual_seed(0  )
            _lowerCAmelCase												=							alt_pipe(
                [prompt]      ,      generator=__snake_case      ,      guidance_scale=6.0      ,      num_inference_steps=2      ,      output_type="""np"""      ,      image=__snake_case      ,      )

            _lowerCAmelCase												=							output.images

            _lowerCAmelCase												=							torch.Generator(device=__snake_case  ).manual_seed(0  )
            _lowerCAmelCase												=							alt_pipe(
                [prompt]      ,      generator=__snake_case      ,      guidance_scale=6.0      ,      num_inference_steps=2      ,      output_type="""np"""      ,      image=__snake_case      ,      return_dict=__snake_case      ,      )[0]

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

            assert image.shape == (1, 32, 32, 3)
            _lowerCAmelCase												=							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      :			Tuple  )    ->				str:
            _lowerCAmelCase												=							self.dummy_cond_unet
            _lowerCAmelCase												=							PNDMScheduler(skip_prk_steps=__snake_case  )
            _lowerCAmelCase												=							self.dummy_vae
            _lowerCAmelCase												=							self.dummy_text_encoder
            _lowerCAmelCase												=							XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta"""  )
            _lowerCAmelCase												=							77

            _lowerCAmelCase												=							self.dummy_image.to(__snake_case  )

            # put models in fp16
            _lowerCAmelCase												=							unet.half()
            _lowerCAmelCase												=							vae.half()
            _lowerCAmelCase												=							bert.half()

            # make sure here that pndm scheduler skips prk
            _lowerCAmelCase												=							AltDiffusionImgaImgPipeline(
                unet=__snake_case      ,      scheduler=__snake_case      ,      vae=__snake_case      ,      text_encoder=__snake_case      ,      tokenizer=__snake_case      ,      safety_checker=__snake_case      ,      feature_extractor=self.dummy_extractor      ,      )
            _lowerCAmelCase												=							VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor      ,      do_normalize=__snake_case  )
            _lowerCAmelCase												=							alt_pipe.to(__snake_case  )
            alt_pipe.set_progress_bar_config(disable=__snake_case  )

            _lowerCAmelCase												=							"""A painting of a squirrel eating a burger"""
            _lowerCAmelCase												=							torch.manual_seed(0  )
            _lowerCAmelCase												=							alt_pipe(
                [prompt]      ,      generator=__snake_case      ,      num_inference_steps=2      ,      output_type="""np"""      ,      image=__snake_case      ,      ).images

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




      @unittest.skipIf(torch_device != """cuda"""      ,      """This test requires a GPU"""  )
      def     lowercase__   (      self      :			Optional[Any]  )    ->				Optional[Any]:
            _lowerCAmelCase												=							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												=							init_image.resize((7_60, 5_04)  )

            _lowerCAmelCase												=							"""BAAI/AltDiffusion"""
            _lowerCAmelCase												=							AltDiffusionImgaImgPipeline.from_pretrained(
                __snake_case      ,      safety_checker=__snake_case      ,      )
            pipe.to(__snake_case  )
            pipe.set_progress_bar_config(disable=__snake_case  )
            pipe.enable_attention_slicing()

            _lowerCAmelCase												=							"""A fantasy landscape, trending on artstation"""

            _lowerCAmelCase												=							torch.manual_seed(0  )
            _lowerCAmelCase												=							pipe(
                prompt=__snake_case      ,      image=__snake_case      ,      strength=0.75      ,      guidance_scale=7.5      ,      generator=__snake_case      ,      output_type="""np"""      ,      )
            _lowerCAmelCase												=							output.images[0]

            _lowerCAmelCase												=							image[2_55:2_58, 3_83:3_86, -1]

            assert image.shape == (5_04, 7_60, 3)
            _lowerCAmelCase												=							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 			UpperCAmelCase					(      unittest.TestCase       ):




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




      def     lowercase__   (      self      :			Dict  )    ->				Tuple:
            _lowerCAmelCase												=							load_image(
                """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
                """/img2img/sketch-mountains-input.jpg"""  )
            _lowerCAmelCase												=							init_image.resize((7_68, 5_12)  )
            _lowerCAmelCase												=							load_numpy(
                """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy"""  )

            _lowerCAmelCase												=							"""BAAI/AltDiffusion"""
            _lowerCAmelCase												=							AltDiffusionImgaImgPipeline.from_pretrained(
                __snake_case      ,      safety_checker=__snake_case      ,      )
            pipe.to(__snake_case  )
            pipe.set_progress_bar_config(disable=__snake_case  )
            pipe.enable_attention_slicing()

            _lowerCAmelCase												=							"""A fantasy landscape, trending on artstation"""

            _lowerCAmelCase												=							torch.manual_seed(0  )
            _lowerCAmelCase												=							pipe(
                prompt=__snake_case      ,      image=__snake_case      ,      strength=0.75      ,      guidance_scale=7.5      ,      generator=__snake_case      ,      output_type="""np"""      ,      )
            _lowerCAmelCase												=							output.images[0]

            assert image.shape == (5_12, 7_68, 3)
            # img2img is flaky across GPUs even in fp32, so using MAE here
            assert np.abs(expected_image - image  ).max() < 1E-2






