





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      lowerCAmelCase							(      __UpperCamelCase,						unittest.TestCase    ):
 UpperCAmelCase__									=	DiTPipeline
 UpperCAmelCase__									=	CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS
 UpperCAmelCase__									=	PipelineTesterMixin.required_optional_params - {
     """latents""",
     """num_images_per_prompt""",
     """callback""",
     """callback_steps""",
 }
 UpperCAmelCase__									=	CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS
 UpperCAmelCase__									=	False
 def   A_			(   self						:   str			)    ->			List[Any]:
     torch.manual_seed(0			)
     lowerCamelCase__					:   int      				=	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=UpperCAmelCase    ,  activation_fn='gelu-approximate'    ,  num_embeds_ada_norm=1000    ,  norm_type='ada_norm_zero'    ,  norm_elementwise_affine=UpperCAmelCase    ,  )
     lowerCamelCase__					:   str      				=	AutoencoderKL()
     lowerCamelCase__					:   List[str]      				=	DDIMScheduler()
     lowerCamelCase__					:   Optional[int]      				=	{'transformer': transformer.eval(), 'vae': vae.eval(), 'scheduler': scheduler}
     return components
 def   A_			(   self						:   List[str]    ,  UpperCAmelCase						:   List[str]    ,  UpperCAmelCase						:   Optional[Any]=0			)    ->			int:
     if str(UpperCAmelCase			).startswith('mps'			):
         lowerCamelCase__					:   List[Any]      				=	torch.manual_seed(UpperCAmelCase			)
     else:
         lowerCamelCase__					:   Any      				=	torch.Generator(device=UpperCAmelCase			).manual_seed(UpperCAmelCase			)
     lowerCamelCase__					:   Any      				=	{
         'class_labels': [1],
         'generator': generator,
         'num_inference_steps': 2,
         'output_type': 'numpy',
     }
     return inputs
 def   A_			(   self						:   Any			)    ->			str:
     lowerCamelCase__					:   Tuple      				=	'cpu'

     lowerCamelCase__					:   Optional[int]      				=	self.get_dummy_components()
     lowerCamelCase__					:   Any      				=	self.pipeline_class(**UpperCAmelCase			)
     pipe.to(UpperCAmelCase			)
     pipe.set_progress_bar_config(disable=UpperCAmelCase			)

     lowerCamelCase__					:   List[Any]      				=	self.get_dummy_inputs(UpperCAmelCase			)
     lowerCamelCase__					:   str      				=	pipe(**UpperCAmelCase			).images
     lowerCamelCase__					:   Any      				=	image[0, -3:, -3:, -1]

     self.assertEqual(image.shape    ,  (1, 16, 16, 3)			)
     lowerCamelCase__					:   Union[str, Any]      				=	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]			)
     lowerCamelCase__					:   Optional[Any]      				=	np.abs(image_slice.flatten() - expected_slice			).max()
     self.assertLessEqual(UpperCAmelCase    ,  1e-3			)
 def   A_			(   self						:   Optional[Any]			)    ->			Tuple:
     self._test_inference_batch_single_identical(relax_max_difference=UpperCAmelCase    ,  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   A_			(   self						:   List[str]			)    ->			List[str]:
     self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3			)


@require_torch_gpu
@slow
class      lowerCAmelCase							(      unittest.TestCase    ):
 def   A_			(   self						:   Tuple			)    ->			Tuple:
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()
 def   A_			(   self						:   Optional[Any]			)    ->			str:
     lowerCamelCase__					:   Any      				=	torch.manual_seed(0			)

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

     lowerCamelCase__					:   Dict      				=	['vase', 'umbrella', 'white shark', 'white wolf']
     lowerCamelCase__					:   Union[str, Any]      				=	pipe.get_label_ids(UpperCAmelCase			)

     lowerCamelCase__					:   Optional[Any]      				=	pipe(UpperCAmelCase    ,  generator=UpperCAmelCase    ,  num_inference_steps=40    ,  output_type='np'			).images

     for word, image in zip(UpperCAmelCase    ,  UpperCAmelCase			):
         lowerCamelCase__					:   Tuple      				=	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   A_			(   self						:   Optional[Any]			)    ->			List[str]:
     lowerCamelCase__					:   int      				=	DiTPipeline.from_pretrained('facebook/DiT-XL-2-512'			)
     lowerCamelCase__					:   str      				=	DPMSolverMultistepScheduler.from_config(pipe.scheduler.config			)
     pipe.to('cuda'			)

     lowerCamelCase__					:   Tuple      				=	['vase', 'umbrella']
     lowerCamelCase__					:   List[str]      				=	pipe.get_label_ids(UpperCAmelCase			)

     lowerCamelCase__					:   int      				=	torch.manual_seed(0			)
     lowerCamelCase__					:   Any      				=	pipe(UpperCAmelCase    ,  generator=UpperCAmelCase    ,  num_inference_steps=25    ,  output_type='np'			).images

     for word, image in zip(UpperCAmelCase    ,  UpperCAmelCase			):
         lowerCamelCase__					:   List[Any]      				=	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












import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
    import torch

    from transformers.activations import gelu_new, gelu_python, get_activation

@require_torch
class      lowerCAmelCase							(      unittest.TestCase    ):
 def   A_			(   self						:   Tuple			)    ->			int:
     lowerCamelCase__					:   Optional[int]      				=	torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]			)
     lowerCamelCase__					:   Dict      				=	get_activation('gelu'			)
     self.assertTrue(torch.allclose(gelu_python(UpperCAmelCase			)    ,  torch_builtin(UpperCAmelCase			)			)			)
     self.assertFalse(torch.allclose(gelu_python(UpperCAmelCase			)    ,  gelu_new(UpperCAmelCase			)			)			)
 def   A_			(   self						:   Dict			)    ->			str:
     lowerCamelCase__					:   Optional[int]      				=	torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]			)
     lowerCamelCase__					:   str      				=	get_activation('gelu'			)
     lowerCamelCase__					:   Tuple      				=	get_activation('gelu_10'			)

     lowerCamelCase__					:   Tuple      				=	torch_builtin(UpperCAmelCase			)
     lowerCamelCase__					:   List[str]      				=	geluaa(UpperCAmelCase			)

     lowerCamelCase__					:   Tuple      				=	torch.where(y_gelu_aa < 1_0.0    ,  1    ,  0			)

     self.assertTrue(torch.max(UpperCAmelCase			).item() == 1_0.0			)
     self.assertTrue(torch.allclose(y_gelu * clipped_mask    ,  y_gelu_aa * clipped_mask			)			)
 def   A_			(   self						:   str			)    ->			List[str]:
     get_activation('gelu'			)
     get_activation('gelu_10'			)
     get_activation('gelu_fast'			)
     get_activation('gelu_new'			)
     get_activation('gelu_python'			)
     get_activation('gelu_pytorch_tanh'			)
     get_activation('linear'			)
     get_activation('mish'			)
     get_activation('quick_gelu'			)
     get_activation('relu'			)
     get_activation('sigmoid'			)
     get_activation('silu'			)
     get_activation('swish'			)
     get_activation('tanh'			)
     with self.assertRaises(UpperCAmelCase			):
         get_activation('bogus'			)
     with self.assertRaises(UpperCAmelCase			):
         get_activation(UpperCAmelCase			)







 def   A_			(   self						:   Any			)    ->			Optional[Any]:
     lowerCamelCase__					:   Optional[Any]      				=	get_activation('gelu'			)
     lowerCamelCase__					:   Union[str, Any]      				=	1
     lowerCamelCase__					:   Optional[Any]      				=	get_activation('gelu'			)
     self.assertEqual(acta.a    ,  1			)
     with self.assertRaises(UpperCAmelCase			):
         lowerCamelCase__					:   str      				=	acta.a





