

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 			_SCREAMING_SNAKE_CASE (   __snake_case					,       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      _snake_case		(       self					:			Any			):
       torch.manual_seed(0			)
       SCREAMING_SNAKE_CASE										=   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=__lowerCamelCase     , activation_fn="gelu-approximate"     , num_embeds_ada_norm=1000     , norm_type="ada_norm_zero"     , norm_elementwise_affine=__lowerCamelCase     , )
       SCREAMING_SNAKE_CASE										=   AutoencoderKL()
       SCREAMING_SNAKE_CASE										=   DDIMScheduler()
       SCREAMING_SNAKE_CASE										=   {"transformer": transformer.eval(), "vae": vae.eval(), "scheduler": scheduler}
       return components





   def      _snake_case		(       self					:			Tuple     , __lowerCamelCase					:			List[str]     , __lowerCamelCase					:			List[Any]=0			):
       if str(__lowerCamelCase			).startswith("mps"			):
           SCREAMING_SNAKE_CASE										=   torch.manual_seed(__lowerCamelCase			)
       else:
           SCREAMING_SNAKE_CASE										=   torch.Generator(device=__lowerCamelCase			).manual_seed(__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   {
           "class_labels": [1],
           "generator": generator,
           "num_inference_steps": 2,
           "output_type": "numpy",
       }
       return inputs





   def      _snake_case		(       self					:			List[Any]			):
       SCREAMING_SNAKE_CASE										=   "cpu"

       SCREAMING_SNAKE_CASE										=   self.get_dummy_components()
       SCREAMING_SNAKE_CASE										=   self.pipeline_class(**__lowerCamelCase			)
       pipe.to(__lowerCamelCase			)
       pipe.set_progress_bar_config(disable=__lowerCamelCase			)

       SCREAMING_SNAKE_CASE										=   self.get_dummy_inputs(__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   pipe(**__lowerCamelCase			).images
       SCREAMING_SNAKE_CASE										=   image[0, -3:, -3:, -1]

       self.assertEqual(image.shape     , (1, 16, 16, 3)			)
       SCREAMING_SNAKE_CASE										=   np.array([0.2_946, 0.6_601, 0.4_329, 0.3_296, 0.4_144, 0.5_319, 0.7_273, 0.5_013, 0.4_457]			)
       SCREAMING_SNAKE_CASE										=   np.abs(image_slice.flatten() - expected_slice			).max()
       self.assertLessEqual(__lowerCamelCase     , 1e-3			)





   def      _snake_case		(       self					:			Optional[Any]			):
       self._test_inference_batch_single_identical(relax_max_difference=__lowerCamelCase     , 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      _snake_case		(       self					:			str			):
       self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3			)





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







   '''simple docstring'''




   def      _snake_case		(       self					:			List[Any]			):
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()





   def      _snake_case		(       self					:			Dict			):
       SCREAMING_SNAKE_CASE										=   torch.manual_seed(0			)

       SCREAMING_SNAKE_CASE										=   DiTPipeline.from_pretrained("facebook/DiT-XL-2-256"			)
       pipe.to("cuda"			)

       SCREAMING_SNAKE_CASE										=   ["vase", "umbrella", "white shark", "white wolf"]
       SCREAMING_SNAKE_CASE										=   pipe.get_label_ids(__lowerCamelCase			)

       SCREAMING_SNAKE_CASE										=   pipe(__lowerCamelCase     , generator=__lowerCamelCase     , num_inference_steps=40     , output_type="np"			).images

       for word, image in zip(__lowerCamelCase     , __lowerCamelCase			):
           SCREAMING_SNAKE_CASE										=   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      _snake_case		(       self					:			Dict			):
       SCREAMING_SNAKE_CASE										=   DiTPipeline.from_pretrained("facebook/DiT-XL-2-512"			)
       SCREAMING_SNAKE_CASE										=   DPMSolverMultistepScheduler.from_config(pipe.scheduler.config			)
       pipe.to("cuda"			)

       SCREAMING_SNAKE_CASE										=   ["vase", "umbrella"]
       SCREAMING_SNAKE_CASE										=   pipe.get_label_ids(__lowerCamelCase			)

       SCREAMING_SNAKE_CASE										=   torch.manual_seed(0			)
       SCREAMING_SNAKE_CASE										=   pipe(__lowerCamelCase     , generator=__lowerCamelCase     , num_inference_steps=25     , output_type="np"			).images

       for word, image in zip(__lowerCamelCase     , __lowerCamelCase			):
           SCREAMING_SNAKE_CASE										=   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 __future__ import annotations






def       __UpperCamelCase						(		snake_case__ , snake_case__       ):
	if len(snake_case__       ) == 0:
		return False
	A_		:    Union[str, Any]						     = len(snake_case__       ) // 2
	if a_list[midpoint] == item:
		return True
	if item < a_list[midpoint]:
		return binary_search(a_list[:midpoint] , snake_case__       )
	else:
		return binary_search(a_list[midpoint + 1 :] , snake_case__       )


if __name__ == "__main__":
							_lowerCAmelCase       =				input("Enter numbers separated by comma:\n").strip()
							_lowerCAmelCase       =				[int(item.strip()) for item in user_input.split(",")]
							_lowerCAmelCase       =				int(input("Enter the number to be found in the list:\n").strip())
							_lowerCAmelCase       =				"" if binary_search(sequence, target) else "not "
							print(F'{target} was {not_str}found in {sequence}')

