

'''simple docstring'''





import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    PNDMScheduler,
    StableDiffusionLDMaDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import nightly, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS


enable_full_determinism()







class        a__       (   unittest.TestCase       ):







  """simple docstring"""




  UpperCAmelCase__      : List[Any]			       =StableDiffusionLDMaDPipeline
  UpperCAmelCase__      : str			       =TEXT_TO_IMAGE_PARAMS
  UpperCAmelCase__      : Dict			       =TEXT_TO_IMAGE_BATCH_PARAMS
  UpperCAmelCase__      : Any			       =TEXT_TO_IMAGE_IMAGE_PARAMS


  def  _lowercase      (			self    : int		)					->str:
        """simple docstring"""







        torch.manual_seed(0		)
        SCREAMING_SNAKE_CASE :      Any       =   UNetaDConditionModel(
            block_out_channels=(3_2, 6_4)				,				layers_per_block=2				,				sample_size=3_2				,				in_channels=4				,				out_channels=4				,				down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")				,				up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")				,				cross_attention_dim=3_2				,				)
        SCREAMING_SNAKE_CASE :      Any       =   DDIMScheduler(
            beta_start=0.0_00_85				,				beta_end=0.0_12				,				beta_schedule="""scaled_linear"""				,				clip_sample=UpperCAmelCase__				,				set_alpha_to_one=UpperCAmelCase__				,				)
        torch.manual_seed(0		)
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   AutoencoderKL(
            block_out_channels=[3_2, 6_4]				,				in_channels=6				,				out_channels=6				,				down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]				,				up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]				,				latent_channels=4				,				)
        torch.manual_seed(0		)
        SCREAMING_SNAKE_CASE :      Dict       =   CLIPTextConfig(
            bos_token_id=0				,				eos_token_id=2				,				hidden_size=3_2				,				intermediate_size=3_7				,				layer_norm_eps=1e-05				,				num_attention_heads=4				,				num_hidden_layers=5				,				pad_token_id=1				,				vocab_size=1_0_0_0				,				)
        SCREAMING_SNAKE_CASE :      Dict       =   CLIPTextModel(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Tuple       =   CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""		)

        SCREAMING_SNAKE_CASE :      int       =   {
            """unet""": unet,
            """scheduler""": scheduler,
            """vae""": vae,
            """text_encoder""": text_encoder,
            """tokenizer""": tokenizer,
            """safety_checker""": None,
            """feature_extractor""": None,
        }
        return components


  def  _lowercase      (			self    : Any				,				UpperCAmelCase__    : List[Any]				,				UpperCAmelCase__    : List[str]=0		)					->Dict:
        """simple docstring"""







        if str(UpperCAmelCase__		).startswith("""mps"""		):
              SCREAMING_SNAKE_CASE :      Optional[int]       =   torch.manual_seed(UpperCAmelCase__		)
        else:
              SCREAMING_SNAKE_CASE :      Any       =   torch.Generator(device=UpperCAmelCase__		).manual_seed(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      str       =   {
            """prompt""": """A painting of a squirrel eating a burger""",
            """generator""": generator,
            """num_inference_steps""": 2,
            """guidance_scale""": 6.0,
            """output_type""": """numpy""",
        }
        return inputs


  def  _lowercase      (			self    : List[str]		)					->Dict:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[int]       =   """cpu"""  # ensure determinism for the device-dependent torch.Generator

        SCREAMING_SNAKE_CASE :      int       =   self.get_dummy_components()
        SCREAMING_SNAKE_CASE :      Dict       =   StableDiffusionLDMaDPipeline(**UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Any       =   ldmad_pipe.to(UpperCAmelCase__		)
        ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__		)

        SCREAMING_SNAKE_CASE :      Optional[Any]       =   self.get_dummy_inputs(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Optional[int]       =   ldmad_pipe(**UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Dict       =   output.rgb, output.depth

        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   rgb[0, -3:, -3:, -1]
        SCREAMING_SNAKE_CASE :      int       =   depth[0, -3:, -1]

        assert rgb.shape == (1, 6_4, 6_4, 3)
        assert depth.shape == (1, 6_4, 6_4)

        SCREAMING_SNAKE_CASE :      Tuple       =   np.array(
            [0.37_33_81_76, 0.7_02_47, 0.74_20_31_93, 0.51_64_36_04, 0.58_25_67_93, 0.60_93_21_36, 0.4_18_10_95, 0.48_35_58_77, 0.46_53_52_62]		)
        SCREAMING_SNAKE_CASE :      Tuple       =   np.array([1_03.4_67_27, 85.81_20_04, 87.84_92_36]		)

        assert np.abs(image_slice_rgb.flatten() - expected_slice_rgb		).max() < 1e-2
        assert np.abs(image_slice_depth.flatten() - expected_slice_depth		).max() < 1e-2


  def  _lowercase      (			self    : Tuple		)					->Dict:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[int]       =   self.get_dummy_components()
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   StableDiffusionLDMaDPipeline(**UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Dict       =   ldmad_pipe.to(UpperCAmelCase__		)
        ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__		)

        SCREAMING_SNAKE_CASE :      List[str]       =   self.get_dummy_inputs(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Tuple       =   3 * [inputs["""prompt"""]]

        # forward
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   ldmad_pipe(**UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      str       =   output.rgb, output.depth
        SCREAMING_SNAKE_CASE :      str       =   rgb_slice_a[0, -3:, -3:, -1]
        SCREAMING_SNAKE_CASE :      List[str]       =   depth_slice_a[0, -3:, -1]

        SCREAMING_SNAKE_CASE :      List[str]       =   self.get_dummy_inputs(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Tuple       =   3 * [inputs.pop("""prompt"""		)]

        SCREAMING_SNAKE_CASE :      List[str]       =   ldmad_pipe.tokenizer(
            UpperCAmelCase__				,				padding="""max_length"""				,				max_length=ldmad_pipe.tokenizer.model_max_length				,				truncation=UpperCAmelCase__				,				return_tensors="""pt"""				,				)
        SCREAMING_SNAKE_CASE :      List[Any]       =   text_inputs["""input_ids"""].to(UpperCAmelCase__		)

        SCREAMING_SNAKE_CASE :      Dict       =   ldmad_pipe.text_encoder(UpperCAmelCase__		)[0]

        SCREAMING_SNAKE_CASE :      str       =   prompt_embeds

        # forward
        SCREAMING_SNAKE_CASE :      str       =   ldmad_pipe(**UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Dict       =   output.rgb, output.depth
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   rgb_slice_a[0, -3:, -3:, -1]
        SCREAMING_SNAKE_CASE :      Any       =   depth_slice_a[0, -3:, -1]

        assert np.abs(rgb_slice_a.flatten() - rgb_slice_a.flatten()		).max() < 1e-4
        assert np.abs(depth_slice_a.flatten() - depth_slice_a.flatten()		).max() < 1e-4




  def  _lowercase      (			self    : Union[str, Any]		)					->int:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   """cpu"""  # ensure determinism for the device-dependent torch.Generator
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   self.get_dummy_components()
        SCREAMING_SNAKE_CASE :      Any       =   PNDMScheduler(skip_prk_steps=UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      str       =   StableDiffusionLDMaDPipeline(**UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Dict       =   ldmad_pipe.to(UpperCAmelCase__		)
        ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__		)

        SCREAMING_SNAKE_CASE :      int       =   self.get_dummy_inputs(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   """french fries"""
        SCREAMING_SNAKE_CASE :      List[Any]       =   ldmad_pipe(**UpperCAmelCase__				,				negative_prompt=UpperCAmelCase__		)

        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Any       =   output.rgb, output.depth
        SCREAMING_SNAKE_CASE :      Tuple       =   rgb[0, -3:, -3:, -1]
        SCREAMING_SNAKE_CASE :      Any       =   depth[0, -3:, -1]

        assert rgb.shape == (1, 6_4, 6_4, 3)
        assert depth.shape == (1, 6_4, 6_4)

        SCREAMING_SNAKE_CASE :      List[Any]       =   np.array(
            [0.3_70_44, 0.71_81_15_03, 0.7_22_32_51, 0.48_60_36_75, 0.5_63_83_91, 0.6_36_49_48, 0.42_83_37_04, 0.4_90_13_15, 0.47_92_62_17]		)
        SCREAMING_SNAKE_CASE :      int       =   np.array([1_07.8_47_38, 84.6_28_02, 89.96_21_35]		)
        assert np.abs(rgb_slice.flatten() - expected_slice_rgb		).max() < 1e-2
        assert np.abs(depth_slice.flatten() - expected_slice_depth		).max() < 1e-2







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







  """simple docstring"""




  def  _lowercase      (			self    : Tuple		)					->Optional[Any]:
        """simple docstring"""







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


  def  _lowercase      (			self    : List[str]				,				UpperCAmelCase__    : List[Any]				,				UpperCAmelCase__    : List[str]="cpu"				,				UpperCAmelCase__    : Union[str, Any]=torch.floataa				,				UpperCAmelCase__    : List[str]=0		)					->Union[str, Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[Any]       =   torch.Generator(device=UpperCAmelCase__		).manual_seed(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      List[Any]       =   np.random.RandomState(UpperCAmelCase__		).standard_normal((1, 4, 6_4, 6_4)		)
        SCREAMING_SNAKE_CASE :      str       =   torch.from_numpy(UpperCAmelCase__		).to(device=UpperCAmelCase__				,				dtype=UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      int       =   {
            """prompt""": """a photograph of an astronaut riding a horse""",
            """latents""": latents,
            """generator""": generator,
            """num_inference_steps""": 3,
            """guidance_scale""": 7.5,
            """output_type""": """numpy""",
        }
        return inputs




  def  _lowercase      (			self    : List[Any]		)					->str:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Dict       =   StableDiffusionLDMaDPipeline.from_pretrained("""Intel/ldm3d"""		)
        SCREAMING_SNAKE_CASE :      List[str]       =   ldmad_pipe.to(UpperCAmelCase__		)
        ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__		)

        SCREAMING_SNAKE_CASE :      List[str]       =   self.get_inputs(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      int       =   ldmad_pipe(**UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Optional[Any]       =   output.rgb, output.depth
        SCREAMING_SNAKE_CASE :      Tuple       =   rgb[0, -3:, -3:, -1].flatten()
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   rgb[0, -3:, -1].flatten()

        assert rgb.shape == (1, 5_1_2, 5_1_2, 3)
        assert depth.shape == (1, 5_1_2, 5_1_2)

        SCREAMING_SNAKE_CASE :      Optional[Any]       =   np.array(
            [0.53_80_54_65, 0.56_70_73_05, 0.5_48_65_15, 0.57_01_22_36, 0.5_81_45_11, 0.56_25_34_87, 0.54_84_30_14, 0.55_09_22_63, 0.6_45_97_06]		)
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   np.array(
            [0.9_26_37_81, 0.6_67_86_72, 0.5_48_65_15, 0.92_20_21_45, 0.67_83_11_35, 0.56_25_34_87, 0.9_24_16_94, 0.7_55_14_78, 0.6_45_97_06]		)
        assert np.abs(rgb_slice - expected_slice_rgb		).max() < 3e-3
        assert np.abs(depth_slice - expected_slice_depth		).max() < 3e-3







@nightly
@require_torch_gpu
class        a__       (   unittest.TestCase       ):







  """simple docstring"""




  def  _lowercase      (			self    : int		)					->int:
        """simple docstring"""







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


  def  _lowercase      (			self    : List[str]				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : int="cpu"				,				UpperCAmelCase__    : str=torch.floataa				,				UpperCAmelCase__    : Optional[Any]=0		)					->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[Any]       =   torch.Generator(device=UpperCAmelCase__		).manual_seed(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Any       =   np.random.RandomState(UpperCAmelCase__		).standard_normal((1, 4, 6_4, 6_4)		)
        SCREAMING_SNAKE_CASE :      Any       =   torch.from_numpy(UpperCAmelCase__		).to(device=UpperCAmelCase__				,				dtype=UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Dict       =   {
            """prompt""": """a photograph of an astronaut riding a horse""",
            """latents""": latents,
            """generator""": generator,
            """num_inference_steps""": 5_0,
            """guidance_scale""": 7.5,
            """output_type""": """numpy""",
        }
        return inputs


  def  _lowercase      (			self    : List[str]		)					->Optional[int]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Any       =   StableDiffusionLDMaDPipeline.from_pretrained("""Intel/ldm3d"""		).to(UpperCAmelCase__		)
        ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__		)

        SCREAMING_SNAKE_CASE :      str       =   self.get_inputs(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      str       =   ldmad_pipe(**UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      str       =   output.rgb, output.depth

        SCREAMING_SNAKE_CASE :      Optional[int]       =   0.49_55_86
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   0.33_79_55_15
        SCREAMING_SNAKE_CASE :      int       =   1_12.4_85_18
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   98.48_97_46
        assert np.abs(expected_rgb_mean - rgb.mean()		) < 1e-3
        assert np.abs(expected_rgb_std - rgb.std()		) < 1e-3
        assert np.abs(expected_depth_mean - depth.mean()		) < 1e-3
        assert np.abs(expected_depth_std - depth.std()		) < 1e-3




  def  _lowercase      (			self    : Optional[int]		)					->str:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Dict       =   StableDiffusionLDMaDPipeline.from_pretrained("""Intel/ldm3d-4c"""		).to(UpperCAmelCase__		)
        ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__		)

        SCREAMING_SNAKE_CASE :      Optional[Any]       =   self.get_inputs(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Any       =   ldmad_pipe(**UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      List[Any]       =   output.rgb, output.depth

        SCREAMING_SNAKE_CASE :      str       =   0.4_19_41_27
        SCREAMING_SNAKE_CASE :      Any       =   0.35_37_55_86
        SCREAMING_SNAKE_CASE :      Dict       =   0.5_63_85_02
        SCREAMING_SNAKE_CASE :      List[str]       =   0.34_68_61_03

        assert rgb.shape == (1, 5_1_2, 5_1_2, 3)
        assert depth.shape == (1, 5_1_2, 5_1_2, 1)
        assert np.abs(expected_rgb_mean - rgb.mean()		) < 1e-3
        assert np.abs(expected_rgb_std - rgb.std()		) < 1e-3
        assert np.abs(expected_depth_mean - depth.mean()		) < 1e-3
        assert np.abs(expected_depth_std - depth.std()		) < 1e-3







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


UpperCAmelCase__ :     str				    =    {"""processing_layoutxlm""": ["""LayoutXLMProcessor"""]}

try:
  if not is_sentencepiece_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__ :     Any				    =    ["""LayoutXLMTokenizer"""]

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__ :     List[str]				    =    ["""LayoutXLMTokenizerFast"""]

if TYPE_CHECKING:
  from .processing_layoutxlm import LayoutXLMProcessor

  try:
    if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_layoutxlm import LayoutXLMTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast

else:
  import sys

  UpperCAmelCase__ :     List[str]				    =    _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




