



'''simple docstring'''




import gc
import random
import unittest

import torch

from diffusers import (
    IFImgaImgPipeline,
    IFImgaImgSuperResolutionPipeline,
    IFInpaintingPipeline,
    IFInpaintingSuperResolutionPipeline,
    IFPipeline,
    IFSuperResolutionPipeline,
)
from diffusers.models.attention_processor import AttnAddedKVProcessor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference
from . import IFPipelineTesterMixin



@skip_mps
class    lowerCamelCase__				(			__lowerCAmelCase	,    __lowerCAmelCase	,    unittest.TestCase						):






      """simple docstring"""
      UpperCamelCase__			 =     IFPipeline
      UpperCamelCase__			 =     TEXT_TO_IMAGE_PARAMS - {'''width''', '''height''', '''latents'''}
      UpperCamelCase__			 =     TEXT_TO_IMAGE_BATCH_PARAMS
      UpperCamelCase__			 =     PipelineTesterMixin.required_optional_params - {'''latents'''}




      def lowerCAmelCase_					(       self							:     int   ):
             return self._get_dummy_components()




      def lowerCAmelCase_					(       self							:     List[Any]			,a__							:     List[Any]			,a__							:     Any=0   ):




             if str(__SCREAMING_SNAKE_CASE   ).startswith("mps"   ):
                    a__								= torch.manual_seed(__SCREAMING_SNAKE_CASE   )
             else:
                    a__								= torch.Generator(device=__SCREAMING_SNAKE_CASE   ).manual_seed(__SCREAMING_SNAKE_CASE   )

             a__								= {
                 "prompt": "A painting of a squirrel eating a burger",
                 "generator": generator,
                 "num_inference_steps": 2,
                 "output_type": "numpy",
             }

             return inputs




      def lowerCAmelCase_					(       self							:     Dict   ):
             self._test_save_load_optional_components()




      @unittest.skipIf(torch_device != "cuda"			,reason="float16 requires CUDA"   )
      def lowerCAmelCase_					(       self							:     Any   ):
             super().test_save_load_floataa(expected_max_diff=1e-1   )




      def lowerCAmelCase_					(       self							:     Tuple   ):
             self._test_attention_slicing_forward_pass(expected_max_diff=1e-2   )




      def lowerCAmelCase_					(       self							:     Dict   ):
             self._test_save_load_local()




      def lowerCAmelCase_					(       self							:     List[str]   ):
             self._test_inference_batch_single_identical(
                 expected_max_diff=1e-2			,)




      @unittest.skipIf(
          torch_device != "cuda" or not is_xformers_available()			,reason="XFormers attention is only available with CUDA and `xformers` installed"			,)
      def lowerCAmelCase_					(       self							:     Optional[Any]   ):
             self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3   )



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






      """simple docstring"""




      def lowerCAmelCase_					(       self							:     int   ):
             super().tearDown()
             gc.collect()
             torch.cuda.empty_cache()




      def lowerCAmelCase_					(       self							:     str   ):
             a__								= IFPipeline.from_pretrained("DeepFloyd/IF-I-XL-v1.0"			,variant="fp16"			,torch_dtype=torch.floataa   )

             a__								= IFSuperResolutionPipeline.from_pretrained(
                 "DeepFloyd/IF-II-L-v1.0"			,variant="fp16"			,torch_dtype=torch.floataa			,text_encoder=__SCREAMING_SNAKE_CASE			,tokenizer=__SCREAMING_SNAKE_CASE   )

             # pre compute text embeddings and remove T5 to save memory

             pipe_a.text_encoder.to("cuda"   )

             a__						,					a__								= pipe_a.encode_prompt("anime turtle"			,device="cuda"   )

             del pipe_a.tokenizer
             del pipe_a.text_encoder
             gc.collect()

             a__								= None
             a__								= None

             pipe_a.enable_model_cpu_offload()
             pipe_a.enable_model_cpu_offload()

             pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )
             pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )

             self._test_if(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE   )

             pipe_a.remove_all_hooks()
             pipe_a.remove_all_hooks()

             # img2img

             a__								= IFImgaImgPipeline(**pipe_a.components   )
             a__								= IFImgaImgSuperResolutionPipeline(**pipe_a.components   )

             pipe_a.enable_model_cpu_offload()
             pipe_a.enable_model_cpu_offload()

             pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )
             pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )

             self._test_if_imgaimg(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE   )

             pipe_a.remove_all_hooks()
             pipe_a.remove_all_hooks()

             # inpainting

             a__								= IFInpaintingPipeline(**pipe_a.components   )
             a__								= IFInpaintingSuperResolutionPipeline(**pipe_a.components   )

             pipe_a.enable_model_cpu_offload()
             pipe_a.enable_model_cpu_offload()

             pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )
             pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )

             self._test_if_inpainting(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE   )




      def lowerCAmelCase_					(       self							:     int			,a__							:     List[Any]			,a__							:     Any			,a__							:     str			,a__							:     Dict   ):
             _start_torch_memory_measurement()

             a__								= torch.Generator(device="cpu"   ).manual_seed(0   )
             a__								= pipe_a(
                 prompt_embeds=__SCREAMING_SNAKE_CASE			,negative_prompt_embeds=__SCREAMING_SNAKE_CASE			,num_inference_steps=2			,generator=__SCREAMING_SNAKE_CASE			,output_type="np"			,)

             a__								= output.images[0]

             assert image.shape == (64, 64, 3)

             a__								= torch.cuda.max_memory_allocated()
             assert mem_bytes < 13 * 10**9

             a__								= load_numpy(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy"   )
             assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE   )

             # pipeline 2

             _start_torch_memory_measurement()

             a__								= torch.Generator(device="cpu"   ).manual_seed(0   )

             a__								= floats_tensor((1, 3, 64, 64)			,rng=random.Random(0   )   ).to(__SCREAMING_SNAKE_CASE   )

             a__								= pipe_a(
                 prompt_embeds=__SCREAMING_SNAKE_CASE			,negative_prompt_embeds=__SCREAMING_SNAKE_CASE			,image=__SCREAMING_SNAKE_CASE			,generator=__SCREAMING_SNAKE_CASE			,num_inference_steps=2			,output_type="np"			,)

             a__								= output.images[0]

             assert image.shape == (2_56, 2_56, 3)

             a__								= torch.cuda.max_memory_allocated()
             assert mem_bytes < 4 * 10**9

             a__								= load_numpy(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy"   )
             assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE   )




      def lowerCAmelCase_					(       self							:     Dict			,a__							:     Union[str, Any]			,a__							:     str			,a__							:     List[str]			,a__							:     str   ):
             _start_torch_memory_measurement()

             a__								= floats_tensor((1, 3, 64, 64)			,rng=random.Random(0   )   ).to(__SCREAMING_SNAKE_CASE   )

             a__								= torch.Generator(device="cpu"   ).manual_seed(0   )

             a__								= pipe_a(
                 prompt_embeds=__SCREAMING_SNAKE_CASE			,negative_prompt_embeds=__SCREAMING_SNAKE_CASE			,image=__SCREAMING_SNAKE_CASE			,num_inference_steps=2			,generator=__SCREAMING_SNAKE_CASE			,output_type="np"			,)

             a__								= output.images[0]

             assert image.shape == (64, 64, 3)

             a__								= torch.cuda.max_memory_allocated()
             assert mem_bytes < 10 * 10**9

             a__								= load_numpy(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy"   )
             assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE   )

             # pipeline 2

             _start_torch_memory_measurement()

             a__								= torch.Generator(device="cpu"   ).manual_seed(0   )

             a__								= floats_tensor((1, 3, 2_56, 2_56)			,rng=random.Random(0   )   ).to(__SCREAMING_SNAKE_CASE   )
             a__								= floats_tensor((1, 3, 64, 64)			,rng=random.Random(0   )   ).to(__SCREAMING_SNAKE_CASE   )

             a__								= pipe_a(
                 prompt_embeds=__SCREAMING_SNAKE_CASE			,negative_prompt_embeds=__SCREAMING_SNAKE_CASE			,image=__SCREAMING_SNAKE_CASE			,original_image=__SCREAMING_SNAKE_CASE			,generator=__SCREAMING_SNAKE_CASE			,num_inference_steps=2			,output_type="np"			,)

             a__								= output.images[0]

             assert image.shape == (2_56, 2_56, 3)

             a__								= torch.cuda.max_memory_allocated()
             assert mem_bytes < 4 * 10**9

             a__								= load_numpy(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy"   )
             assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE   )




      def lowerCAmelCase_					(       self							:     int			,a__							:     List[str]			,a__							:     List[str]			,a__							:     Optional[int]			,a__							:     Optional[Any]   ):
             _start_torch_memory_measurement()

             a__								= floats_tensor((1, 3, 64, 64)			,rng=random.Random(0   )   ).to(__SCREAMING_SNAKE_CASE   )
             a__								= floats_tensor((1, 3, 64, 64)			,rng=random.Random(1   )   ).to(__SCREAMING_SNAKE_CASE   )

             a__								= torch.Generator(device="cpu"   ).manual_seed(0   )
             a__								= pipe_a(
                 prompt_embeds=__SCREAMING_SNAKE_CASE			,negative_prompt_embeds=__SCREAMING_SNAKE_CASE			,image=__SCREAMING_SNAKE_CASE			,mask_image=__SCREAMING_SNAKE_CASE			,num_inference_steps=2			,generator=__SCREAMING_SNAKE_CASE			,output_type="np"			,)

             a__								= output.images[0]

             assert image.shape == (64, 64, 3)

             a__								= torch.cuda.max_memory_allocated()
             assert mem_bytes < 10 * 10**9

             a__								= load_numpy(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy"   )
             assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE   )

             # pipeline 2

             _start_torch_memory_measurement()

             a__								= torch.Generator(device="cpu"   ).manual_seed(0   )

             a__								= floats_tensor((1, 3, 64, 64)			,rng=random.Random(0   )   ).to(__SCREAMING_SNAKE_CASE   )
             a__								= floats_tensor((1, 3, 2_56, 2_56)			,rng=random.Random(0   )   ).to(__SCREAMING_SNAKE_CASE   )
             a__								= floats_tensor((1, 3, 2_56, 2_56)			,rng=random.Random(1   )   ).to(__SCREAMING_SNAKE_CASE   )

             a__								= pipe_a(
                 prompt_embeds=__SCREAMING_SNAKE_CASE			,negative_prompt_embeds=__SCREAMING_SNAKE_CASE			,image=__SCREAMING_SNAKE_CASE			,mask_image=__SCREAMING_SNAKE_CASE			,original_image=__SCREAMING_SNAKE_CASE			,generator=__SCREAMING_SNAKE_CASE			,num_inference_steps=2			,output_type="np"			,)

             a__								= output.images[0]

             assert image.shape == (2_56, 2_56, 3)

             a__								= torch.cuda.max_memory_allocated()
             assert mem_bytes < 4 * 10**9

             a__								= load_numpy(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy"   )
             assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE   )






def 		_lowerCAmelCase				():

       """simple docstring"""

       torch.cuda.empty_cache()
       torch.cuda.reset_max_memory_allocated()
       torch.cuda.reset_peak_memory_stats()






'''simple docstring'''



import unittest

import numpy as np

from transformers import RoFormerConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
     import jax.numpy as jnp

     from transformers.models.roformer.modeling_flax_roformer import (
         FlaxRoFormerForMaskedLM,
         FlaxRoFormerForMultipleChoice,
         FlaxRoFormerForQuestionAnswering,
         FlaxRoFormerForSequenceClassification,
         FlaxRoFormerForTokenClassification,
         FlaxRoFormerModel,
     )





class      lowerCAmelCase (			unittest.TestCase):


       def __init__( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=13     ,	__SCREAMING_SNAKE_CASE=7     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=99     ,	__SCREAMING_SNAKE_CASE=32     ,	__SCREAMING_SNAKE_CASE=5     ,	__SCREAMING_SNAKE_CASE=4     ,	__SCREAMING_SNAKE_CASE=37     ,	__SCREAMING_SNAKE_CASE="gelu"     ,	__SCREAMING_SNAKE_CASE=0.1     ,	__SCREAMING_SNAKE_CASE=0.1     ,	__SCREAMING_SNAKE_CASE=512     ,	__SCREAMING_SNAKE_CASE=16     ,	__SCREAMING_SNAKE_CASE=2     ,	__SCREAMING_SNAKE_CASE=0.02     ,	__SCREAMING_SNAKE_CASE=4     ,	)   ->	Any:



            '''simple docstring'''




            __snake_case            =	parent
            __snake_case            =	batch_size
            __snake_case            =	seq_length
            __snake_case            =	is_training
            __snake_case            =	use_attention_mask
            __snake_case            =	use_token_type_ids
            __snake_case            =	use_labels
            __snake_case            =	vocab_size
            __snake_case            =	hidden_size
            __snake_case            =	num_hidden_layers
            __snake_case            =	num_attention_heads
            __snake_case            =	intermediate_size
            __snake_case            =	hidden_act
            __snake_case            =	hidden_dropout_prob
            __snake_case            =	attention_probs_dropout_prob
            __snake_case            =	max_position_embeddings
            __snake_case            =	type_vocab_size
            __snake_case            =	type_sequence_label_size
            __snake_case            =	initializer_range
            __snake_case            =	num_choices


       def    lowerCAmelCase   ( self	)   ->	Union[str, Any]:



            '''simple docstring'''




            __snake_case            =	ids_tensor([self.batch_size, self.seq_length]     ,	self.vocab_size	)

            __snake_case            =	None
            if self.use_attention_mask:
                 __snake_case            =	random_attention_mask([self.batch_size, self.seq_length]	)

            __snake_case            =	None
            if self.use_token_type_ids:
                 __snake_case            =	ids_tensor([self.batch_size, self.seq_length]     ,	self.type_vocab_size	)

            __snake_case            =	RoFormerConfig(
                vocab_size=self.vocab_size     ,	hidden_size=self.hidden_size     ,	num_hidden_layers=self.num_hidden_layers     ,	num_attention_heads=self.num_attention_heads     ,	intermediate_size=self.intermediate_size     ,	hidden_act=self.hidden_act     ,	hidden_dropout_prob=self.hidden_dropout_prob     ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,	max_position_embeddings=self.max_position_embeddings     ,	type_vocab_size=self.type_vocab_size     ,	is_decoder=__SCREAMING_SNAKE_CASE     ,	initializer_range=self.initializer_range     ,	)

            return config, input_ids, token_type_ids, attention_mask




       def    lowerCAmelCase   ( self	)   ->	int:



            '''simple docstring'''




            __snake_case            =	self.prepare_config_and_inputs()
            __snake_case       ,     __snake_case       ,     __snake_case       ,     __snake_case            =	config_and_inputs
            __snake_case            =	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
            return config, inputs_dict





@require_flax
class      lowerCAmelCase (			__lowerCAmelCase					, unittest.TestCase):
       __lowercase	:   Tuple										=      True

       __lowercase	:   Optional[int]										=      (
           (
               FlaxRoFormerModel,
               FlaxRoFormerForMaskedLM,
               FlaxRoFormerForSequenceClassification,
               FlaxRoFormerForTokenClassification,
               FlaxRoFormerForMultipleChoice,
               FlaxRoFormerForQuestionAnswering,
           )
           if is_flax_available()
           else ()
       )


       def    lowerCAmelCase   ( self	)   ->	int:



            '''simple docstring'''




            __snake_case            =	FlaxRoFormerModelTester(self	)




       @slow
       def    lowerCAmelCase   ( self	)   ->	List[Any]:



            '''simple docstring'''




            for model_class_name in self.all_model_classes:
                 __snake_case            =	model_class_name.from_pretrained('''junnyu/roformer_chinese_small'''     ,	from_pt=__SCREAMING_SNAKE_CASE	)
                 __snake_case            =	model(np.ones((1, 1)	)	)
                 self.assertIsNotNone(__SCREAMING_SNAKE_CASE	)





@require_flax
class      lowerCAmelCase (			unittest.TestCase):


       @slow
       def    lowerCAmelCase   ( self	)   ->	Tuple:



            '''simple docstring'''




            __snake_case            =	FlaxRoFormerForMaskedLM.from_pretrained('''junnyu/roformer_chinese_base'''	)
            __snake_case            =	jnp.array([[0, 1, 2, 3, 4, 5]]	)
            __snake_case            =	model(__SCREAMING_SNAKE_CASE	)[0]

            __snake_case            =	5_0000

            __snake_case            =	(1, 6, vocab_size)
            self.assertEqual(output.shape     ,	__SCREAMING_SNAKE_CASE	)

            __snake_case            =	jnp.array(
                [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]]	)

            self.assertTrue(jnp.allclose(output[:, :3, :3]     ,	__SCREAMING_SNAKE_CASE     ,	atol=1E-4	)	)






