





"""simple docstring"""



import gc
import unittest

import numpy as np
import torch

from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel
from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS,
    CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()






class 		__snake_case (		lowerCAmelCase__							,							unittest.TestCase					):





	"""simple docstring"""


	_lowerCamelCase				=					DiTPipeline
	_lowerCamelCase				=					CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS
	_lowerCamelCase				=					PipelineTesterMixin.required_optional_params - {
	    "latents",
	    "num_images_per_prompt",
	    "callback",
	    "callback_steps",
	}
	_lowerCamelCase				=					CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS
	_lowerCamelCase				=					False

	def  UpperCamelCase__(   self  ):



					'''simple docstring'''




					torch.manual_seed(0  )
					__A   :						Optional[Any]									=					TransformeraDModel(
					    sample_size=16 ,						num_layers=2 ,						patch_size=4 ,						attention_head_dim=8 ,						num_attention_heads=2 ,						in_channels=4 ,						out_channels=8 ,						attention_bias=a__ ,						activation_fn='''gelu-approximate''' ,						num_embeds_ada_norm=1000 ,						norm_type='''ada_norm_zero''' ,						norm_elementwise_affine=a__ ,						)
					__A   :						List[Any]									=					AutoencoderKL()
					__A   :						Tuple									=					DDIMScheduler()
					__A   :						List[Any]									=					{'''transformer''': transformer.eval(), '''vae''': vae.eval(), '''scheduler''': scheduler}
					return components

	def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase=0  ):



					'''simple docstring'''




					if str(a__  ).startswith('''mps'''  ):
									__A   :						Optional[Any]									=					torch.manual_seed(a__  )
					else:
									__A   :						int									=					torch.Generator(device=a__  ).manual_seed(a__  )
					__A   :						Optional[int]									=					{
					    '''class_labels''': [1],
					    '''generator''': generator,
					    '''num_inference_steps''': 2,
					    '''output_type''': '''numpy''',
					}
					return inputs

	def  UpperCamelCase__(   self  ):



					'''simple docstring'''




					__A   :						str									=					'''cpu'''

					__A   :						List[str]									=					self.get_dummy_components()
					__A   :						int									=					self.pipeline_class(**a__  )
					pipe.to(a__  )
					pipe.set_progress_bar_config(disable=a__  )

					__A   :						int									=					self.get_dummy_inputs(a__  )
					__A   :						Dict									=					pipe(**a__  ).images
					__A   :						List[str]									=					image[0, -3:, -3:, -1]

					self.assertEqual(image.shape ,						(1, 16, 16, 3)  )
					__A   :						Dict									=					np.array([0.2_9_4_6, 0.6_6_0_1, 0.4_3_2_9, 0.3_2_9_6, 0.4_1_4_4, 0.5_3_1_9, 0.7_2_7_3, 0.5_0_1_3, 0.4_4_5_7]  )
					__A   :						List[str]									=					np.abs(image_slice.flatten() - expected_slice  ).max()
					self.assertLessEqual(a__ ,						1e-3  )

	def  UpperCamelCase__(   self  ):



					'''simple docstring'''




					self._test_inference_batch_single_identical(relax_max_difference=a__ ,						expected_max_diff=1e-3  )







	@unittest.skipIf(
	    torch_device != '''cuda''' or not is_xformers_available() ,						reason='''XFormers attention is only available with CUDA and `xformers` installed''' ,						)
	def  UpperCamelCase__(   self  ):



					'''simple docstring'''




					self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3  )






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





	"""simple docstring"""


	def  UpperCamelCase__(   self  ):



					'''simple docstring'''




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

	def  UpperCamelCase__(   self  ):



					'''simple docstring'''




					__A   :						Tuple									=					torch.manual_seed(0  )

					__A   :						int									=					DiTPipeline.from_pretrained('''facebook/DiT-XL-2-256'''  )
					pipe.to('''cuda'''  )

					__A   :						Dict									=					['''vase''', '''umbrella''', '''white shark''', '''white wolf''']
					__A   :						List[str]									=					pipe.get_label_ids(a__  )

					__A   :						str									=					pipe(a__ ,						generator=a__ ,						num_inference_steps=40 ,						output_type='''np'''  ).images

					for word, image in zip(a__ ,						a__  ):
									__A   :						Any									=					load_numpy(
									    F"""https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy"""  )
									assert np.abs((expected_image - image).max()  ) < 1e-2







	def  UpperCamelCase__(   self  ):



					'''simple docstring'''




					__A   :						str									=					DiTPipeline.from_pretrained('''facebook/DiT-XL-2-512'''  )
					__A   :						Optional[Any]									=					DPMSolverMultistepScheduler.from_config(pipe.scheduler.config  )
					pipe.to('''cuda'''  )

					__A   :						Tuple									=					['''vase''', '''umbrella''']
					__A   :						Dict									=					pipe.get_label_ids(a__  )

					__A   :						Dict									=					torch.manual_seed(0  )
					__A   :						Tuple									=					pipe(a__ ,						generator=a__ ,						num_inference_steps=25 ,						output_type='''np'''  ).images

					for word, image in zip(a__ ,						a__  ):
									__A   :						Dict									=					load_numpy(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
									    F"""/dit/{word}_512.npy"""  )

									assert np.abs((expected_image - image).max()  ) < 1e-1











"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


a_			=      {
    """configuration_bloom""": ["""BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BloomConfig""", """BloomOnnxConfig"""],
}
try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       a_			=      ["""BloomTokenizerFast"""]

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       a_			=      [
           """BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST""",
           """BloomForCausalLM""",
           """BloomModel""",
           """BloomPreTrainedModel""",
           """BloomForSequenceClassification""",
           """BloomForTokenClassification""",
           """BloomForQuestionAnswering""",
       ]

if TYPE_CHECKING:
       from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_bloom_fast import BloomTokenizerFast

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_bloom import (
                  BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST,
                  BloomForCausalLM,
                  BloomForQuestionAnswering,
                  BloomForSequenceClassification,
                  BloomForTokenClassification,
                  BloomModel,
                  BloomPreTrainedModel,
              )

else:
       import sys

       a_			=      _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




