


"""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 							A_      (       unittest.TestCase ):


  def 							_lowercase					(			self:						List[Any]    ):







     '''simple docstring'''







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


  @property
  def 							_lowercase					(			self:						Optional[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Union[str, Any]							=     1
     _lowerCamelCase   :				int							=     3
     _lowerCamelCase   :				Tuple							=     (32, 32)

     _lowerCamelCase   :				Optional[int]							=     floats_tensor((batch_size, num_channels) + sizes							,rng=random.Random(0    )    ).to(__lowerCAmelCase    )
     return image


  @property
  def 							_lowercase					(			self:						Optional[Any]    ):







     '''simple docstring'''







     torch.manual_seed(0    )
     _lowerCamelCase   :				str							=     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:						List[Any]    ):







     '''simple docstring'''







     torch.manual_seed(0    )
     _lowerCamelCase   :				Dict							=     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:						Optional[int]    ):







     '''simple docstring'''







     torch.manual_seed(0    )
     _lowerCamelCase   :				Any							=     RobertaSeriesConfig(
         hidden_size=32							,project_dim=32							,intermediate_size=37							,layer_norm_eps=1e-05							,num_attention_heads=4							,num_hidden_layers=5							,pad_token_id=1							,vocab_size=5_006							,)
     return RobertaSeriesModelWithTransformation(__lowerCAmelCase    )


  @property
  def 							_lowercase					(			self:						Union[str, Any]    ):







     '''simple docstring'''







     def extract(*__lowerCAmelCase:						Tuple							,**__lowerCAmelCase:						Tuple    ):




        class 							A_      :


          def __init__(			self:						List[str]    ):







             '''simple docstring'''







             _lowerCamelCase   :				Any							=     torch.ones([0]    )






          def 							_lowercase					(			self:						Union[str, Any]							,__lowerCAmelCase:						List[Any]    ):







             '''simple docstring'''







             self.pixel_values.to(__lowerCAmelCase    )
             return self


        return Out()

     return extract


  def 							_lowercase					(			self:						Any    ):







     '''simple docstring'''







     _lowerCamelCase   :				str							=     "cpu"  # ensure determinism for the device-dependent torch.Generator
     _lowerCamelCase   :				Any							=     self.dummy_cond_unet
     _lowerCamelCase   :				int							=     PNDMScheduler(skip_prk_steps=__lowerCAmelCase    )
     _lowerCamelCase   :				Optional[Any]							=     self.dummy_vae
     _lowerCamelCase   :				List[str]							=     self.dummy_text_encoder
     _lowerCamelCase   :				Optional[Any]							=     XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"    )
     _lowerCamelCase   :				Union[str, Any]							=     77

     _lowerCamelCase   :				List[Any]							=     self.dummy_image.to(__lowerCAmelCase    )
     _lowerCamelCase   :				List[str]							=     init_image / 2 + 0.5

     # make sure here that pndm scheduler skips prk
     _lowerCamelCase   :				int							=     AltDiffusionImgaImgPipeline(
         unet=__lowerCAmelCase							,scheduler=__lowerCAmelCase							,vae=__lowerCAmelCase							,text_encoder=__lowerCAmelCase							,tokenizer=__lowerCAmelCase							,safety_checker=__lowerCAmelCase							,feature_extractor=self.dummy_extractor							,)
     _lowerCamelCase   :				List[str]							=     VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor							,do_normalize=__lowerCAmelCase    )
     _lowerCamelCase   :				Union[str, Any]							=     alt_pipe.to(__lowerCAmelCase    )
     alt_pipe.set_progress_bar_config(disable=__lowerCAmelCase    )

     _lowerCamelCase   :				Dict							=     "A painting of a squirrel eating a burger"
     _lowerCamelCase   :				List[str]							=     torch.Generator(device=__lowerCAmelCase    ).manual_seed(0    )
     _lowerCamelCase   :				int							=     alt_pipe(
         [prompt]							,generator=__lowerCAmelCase							,guidance_scale=6.0							,num_inference_steps=2							,output_type="np"							,image=__lowerCAmelCase							,)

     _lowerCamelCase   :				Dict							=     output.images

     _lowerCamelCase   :				Any							=     torch.Generator(device=__lowerCAmelCase    ).manual_seed(0    )
     _lowerCamelCase   :				Union[str, Any]							=     alt_pipe(
         [prompt]							,generator=__lowerCAmelCase							,guidance_scale=6.0							,num_inference_steps=2							,output_type="np"							,image=__lowerCAmelCase							,return_dict=__lowerCAmelCase							,)[0]

     _lowerCamelCase   :				List[Any]							=     image[0, -3:, -3:, -1]
     _lowerCamelCase   :				Tuple							=     image_from_tuple[0, -3:, -3:, -1]

     assert image.shape == (1, 32, 32, 3)
     _lowerCamelCase   :				List[str]							=     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:						str    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     self.dummy_cond_unet
     _lowerCamelCase   :				str							=     PNDMScheduler(skip_prk_steps=__lowerCAmelCase    )
     _lowerCamelCase   :				int							=     self.dummy_vae
     _lowerCamelCase   :				int							=     self.dummy_text_encoder
     _lowerCamelCase   :				List[Any]							=     XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"    )
     _lowerCamelCase   :				Tuple							=     77

     _lowerCamelCase   :				Any							=     self.dummy_image.to(__lowerCAmelCase    )

     # put models in fp16
     _lowerCamelCase   :				List[str]							=     unet.half()
     _lowerCamelCase   :				Union[str, Any]							=     vae.half()
     _lowerCamelCase   :				Union[str, Any]							=     bert.half()

     # make sure here that pndm scheduler skips prk
     _lowerCamelCase   :				Optional[Any]							=     AltDiffusionImgaImgPipeline(
         unet=__lowerCAmelCase							,scheduler=__lowerCAmelCase							,vae=__lowerCAmelCase							,text_encoder=__lowerCAmelCase							,tokenizer=__lowerCAmelCase							,safety_checker=__lowerCAmelCase							,feature_extractor=self.dummy_extractor							,)
     _lowerCamelCase   :				int							=     VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor							,do_normalize=__lowerCAmelCase    )
     _lowerCamelCase   :				Optional[Any]							=     alt_pipe.to(__lowerCAmelCase    )
     alt_pipe.set_progress_bar_config(disable=__lowerCAmelCase    )

     _lowerCamelCase   :				int							=     "A painting of a squirrel eating a burger"
     _lowerCamelCase   :				List[Any]							=     torch.manual_seed(0    )
     _lowerCamelCase   :				Optional[int]							=     alt_pipe(
         [prompt]							,generator=__lowerCAmelCase							,num_inference_steps=2							,output_type="np"							,image=__lowerCAmelCase							,).images

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






  @unittest.skipIf(torch_device != "cuda"							,"This test requires a GPU"    )
  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[Any]							=     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   :				List[str]							=     init_image.resize((760, 504)    )

     _lowerCamelCase   :				Any							=     "BAAI/AltDiffusion"
     _lowerCamelCase   :				Optional[int]							=     AltDiffusionImgaImgPipeline.from_pretrained(
         __lowerCAmelCase							,safety_checker=__lowerCAmelCase							,)
     pipe.to(__lowerCAmelCase    )
     pipe.set_progress_bar_config(disable=__lowerCAmelCase    )
     pipe.enable_attention_slicing()

     _lowerCamelCase   :				Optional[Any]							=     "A fantasy landscape, trending on artstation"

     _lowerCamelCase   :				List[Any]							=     torch.manual_seed(0    )
     _lowerCamelCase   :				Union[str, Any]							=     pipe(
         prompt=__lowerCAmelCase							,image=__lowerCAmelCase							,strength=0.75							,guidance_scale=7.5							,generator=__lowerCAmelCase							,output_type="np"							,)
     _lowerCamelCase   :				Any							=     output.images[0]

     _lowerCamelCase   :				Optional[int]							=     image[255:258, 383:386, -1]

     assert image.shape == (504, 760, 3)
     _lowerCamelCase   :				List[Any]							=     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 							A_      (       unittest.TestCase ):


  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







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






  def 							_lowercase					(			self:						Union[str, Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[Any]							=     load_image(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
         "/img2img/sketch-mountains-input.jpg"    )
     _lowerCamelCase   :				Union[str, Any]							=     init_image.resize((768, 512)    )
     _lowerCamelCase   :				Dict							=     load_numpy(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy"    )

     _lowerCamelCase   :				Dict							=     "BAAI/AltDiffusion"
     _lowerCamelCase   :				str							=     AltDiffusionImgaImgPipeline.from_pretrained(
         __lowerCAmelCase							,safety_checker=__lowerCAmelCase							,)
     pipe.to(__lowerCAmelCase    )
     pipe.set_progress_bar_config(disable=__lowerCAmelCase    )
     pipe.enable_attention_slicing()

     _lowerCamelCase   :				Optional[int]							=     "A fantasy landscape, trending on artstation"

     _lowerCamelCase   :				Optional[Any]							=     torch.manual_seed(0    )
     _lowerCamelCase   :				List[str]							=     pipe(
         prompt=__lowerCAmelCase							,image=__lowerCAmelCase							,strength=0.75							,guidance_scale=7.5							,generator=__lowerCAmelCase							,output_type="np"							,)
     _lowerCamelCase   :				List[Any]							=     output.images[0]

     assert image.shape == (512, 768, 3)
     # img2img is flaky across GPUs even in fp32, so using MAE here
     assert np.abs(expected_image - image    ).max() < 1e-2

'''simple docstring'''





import json
import os
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import ImageGPTImageProcessor






class 							UpperCAmelCase_							(    unittest.TestCase				):

 '''simple docstring'''







 def __init__(					self		:  Optional[int]			,				snake_case__		:  int			,				snake_case__		:  Optional[Any]=7			,				snake_case__		:  Tuple=3			,				snake_case__		:  Optional[Any]=18			,				snake_case__		:  Dict=30			,				snake_case__		:  Optional[int]=4_00			,				snake_case__		:  str=True			,				snake_case__		:  str=None			,				snake_case__		:  Optional[int]=True			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							size if size is not None else {"height": 18, "width": 18}
   UpperCAmelCase__      :					List[str]				       =							parent
   UpperCAmelCase__      :					Union[str, Any]				       =							batch_size
   UpperCAmelCase__      :					List[str]				       =							num_channels
   UpperCAmelCase__      :					List[Any]				       =							image_size
   UpperCAmelCase__      :					Tuple				       =							min_resolution
   UpperCAmelCase__      :					Optional[int]				       =							max_resolution
   UpperCAmelCase__      :					Any				       =							do_resize
   UpperCAmelCase__      :					Any				       =							size
   UpperCAmelCase__      :					int				       =							do_normalize







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   return {
       # here we create 2 clusters for the sake of simplicity
       "clusters": np.asarray(
           [
               [0.8866443634033203, 0.6618829369544983, 0.3891746401786804],
               [-0.6042559146881104, -0.02295008860528469, 0.5423797369003296],
           ] ),
       "do_resize": self.do_resize,
       "size": self.size,
       "do_normalize": self.do_normalize,
   }






@require_torch
@require_vision
class 							UpperCAmelCase_							(    A					,				unittest.TestCase				):

 '''simple docstring'''
 lowercase_					:    Any				=							ImageGPTImageProcessor if is_vision_available() else None







 def 							UpperCamelCase  (					self		:  Dict ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							ImageGPTImageProcessingTester(self )







 @property
 def 							UpperCamelCase  (					self		:  List[Any] ):



   '''simple docstring'''




   return self.image_processor_tester.prepare_image_processor_dict()







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							self.image_processing_class(**self.image_processor_dict )
   self.assertTrue(hasattr(snake_case__			,				"clusters" ) )
   self.assertTrue(hasattr(snake_case__			,				"do_resize" ) )
   self.assertTrue(hasattr(snake_case__			,				"size" ) )
   self.assertTrue(hasattr(snake_case__			,				"do_normalize" ) )







 def 							UpperCamelCase  (					self		:  Tuple ):



   '''simple docstring'''




   UpperCAmelCase__      :					str				       =							self.image_processing_class.from_dict(self.image_processor_dict )
   self.assertEqual(image_processor.size			,				{"height": 18, "width": 18} )

   UpperCAmelCase__      :					Union[str, Any]				       =							self.image_processing_class.from_dict(self.image_processor_dict			,				size=42 )
   self.assertEqual(image_processor.size			,				{"height": 42, "width": 42} )







 def 							UpperCamelCase  (					self		:  Any ):



   '''simple docstring'''




   UpperCAmelCase__      :					int				       =							self.image_processing_class(**self.image_processor_dict )
   UpperCAmelCase__      :					List[str]				       =							json.loads(image_processor.to_json_string() )
   for key, value in self.image_processor_dict.items():
     if key == "clusters":
       self.assertTrue(np.array_equal(snake_case__			,				obj[key] ) )
     else:
       self.assertEqual(obj[key]			,				snake_case__ )







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Dict				       =							self.image_processing_class(**self.image_processor_dict )

   with tempfile.TemporaryDirectory() as tmpdirname:
     UpperCAmelCase__      :					Union[str, Any]				       =							os.path.join(snake_case__			,				"image_processor.json" )
     image_processor_first.to_json_file(snake_case__ )
     UpperCAmelCase__      :					Any				       =							self.image_processing_class.from_json_file(snake_case__ ).to_dict()

   UpperCAmelCase__      :					Union[str, Any]				       =							image_processor_first.to_dict()
   for key, value in image_processor_first.items():
     if key == "clusters":
       self.assertTrue(np.array_equal(snake_case__			,				image_processor_second[key] ) )
     else:
       self.assertEqual(image_processor_first[key]			,				snake_case__ )







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					int				       =							self.image_processing_class(**self.image_processor_dict )

   with tempfile.TemporaryDirectory() as tmpdirname:
     image_processor_first.save_pretrained(snake_case__ )
     UpperCAmelCase__      :					List[str]				       =							self.image_processing_class.from_pretrained(snake_case__ ).to_dict()

   UpperCAmelCase__      :					Tuple				       =							image_processor_first.to_dict()
   for key, value in image_processor_first.items():
     if key == "clusters":
       self.assertTrue(np.array_equal(snake_case__			,				image_processor_second[key] ) )
     else:
       self.assertEqual(image_processor_first[key]			,				snake_case__ )







 @unittest.skip("ImageGPT requires clusters at initialization" )
 def 							UpperCamelCase  (					self		:  List[Any] ):



   '''simple docstring'''




   pass







def snake_case_     (					):
  UpperCAmelCase__      :					List[str]				       =							load_dataset("hf-internal-testing/fixtures_image_utils"   ,       split="test"		)

  UpperCAmelCase__      :					Tuple				       =							Image.open(dataset[4]["file"]		)
  UpperCAmelCase__      :					List[Any]				       =							Image.open(dataset[5]["file"]		)

  UpperCAmelCase__      :					Optional[Any]				       =							[imagea, imagea]

  return images






@require_vision
@require_torch
class 							UpperCAmelCase_							(    unittest.TestCase				):

 '''simple docstring'''







 @slow
 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   UpperCAmelCase__      :					Dict				       =							ImageGPTImageProcessor.from_pretrained("openai/imagegpt-small" )

   UpperCAmelCase__      :					Union[str, Any]				       =							prepare_images()

   # test non-batched
   UpperCAmelCase__      :					Optional[Any]				       =							image_processing(images[0]			,				return_tensors="pt" )

   self.assertIsInstance(encoding.input_ids			,				torch.LongTensor )
   self.assertEqual(encoding.input_ids.shape			,				(1, 10_24) )

   UpperCAmelCase__      :					Union[str, Any]				       =							[3_06, 1_91, 1_91]
   self.assertEqual(encoding.input_ids[0, :3].tolist()			,				snake_case__ )

   # test batched
   UpperCAmelCase__      :					List[Any]				       =							image_processing(snake_case__			,				return_tensors="pt" )

   self.assertIsInstance(encoding.input_ids			,				torch.LongTensor )
   self.assertEqual(encoding.input_ids.shape			,				(2, 10_24) )

   UpperCAmelCase__      :					List[str]				       =							[3_03, 13, 13]
   self.assertEqual(encoding.input_ids[1, -3:].tolist()			,				snake_case__ )



