


"""simple docstring"""

import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import require_keras_nlp, require_tf, slow


if is_tf_available():
       import tensorflow as tf

if is_keras_nlp_available():
       from transformers.models.gpta import TFGPTaTokenizer


UpperCAmelCase__					=["gpt2"]
UpperCAmelCase__					="gpt2"

if is_tf_available():





       class  lowerCamelCase__    (		tf.Module     ):
             def __init__(		self						:						Tuple   ,							A_						:						Dict				):






              '''simple docstring'''


              super().__init__()
              __lowercase						=						tokenizer
              __lowercase						=						AutoConfig.from_pretrained(A_				)
              __lowercase						=						TFGPTaLMHeadModel.from_config(A_				)






             @tf.function(input_signature=(tf.TensorSpec((None,)   ,							tf.string   ,							name="""text"""				),)				)
             def SCREAMING_SNAKE_CASE_						(		self						:						Dict   ,							A_						:						Any				):






              '''simple docstring'''


              __lowercase						=						self.tokenizer(A_				)
              __lowercase						=						tokenized["""input_ids"""].to_tensor()

              __lowercase						=						tf.cast(input_ids_dense > 0   ,							tf.intaa				)
              # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN])

              __lowercase						=						self.model(input_ids=A_   ,							attention_mask=A_				)["""logits"""]

              return outputs





@require_tf
@require_keras_nlp
class  lowerCamelCase__    (		unittest.TestCase     ):
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       super().setUp()

       __lowercase						=						[GPTaTokenizer.from_pretrained(A_				) for checkpoint in (TOKENIZER_CHECKPOINTS)]
       __lowercase						=						[TFGPTaTokenizer.from_pretrained(A_				) for checkpoint in TOKENIZER_CHECKPOINTS]
       assert len(self.tokenizers				) == len(self.tf_tokenizers				)

       __lowercase						=						[
           """This is a straightforward English test sentence.""",
           """This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.""",
           """Now we're going to add some Chinese: 一 二 三 一二三""",
           """And some much more rare Chinese: 齉 堃 齉堃""",
           """Je vais aussi écrire en français pour tester les accents""",
           """Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ""",
       ]
       __lowercase						=						list(zip(self.test_sentences   ,							self.test_sentences[::-1]				)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       for tokenizer, tf_tokenizer in zip(self.tokenizers   ,							self.tf_tokenizers				):
        for test_inputs in self.test_sentences:
         __lowercase						=						tokenizer([test_inputs]   ,							return_tensors="""tf"""				)
         __lowercase						=						tf_tokenizer([test_inputs]				)

         for key in python_outputs.keys():
          # convert them to numpy to avoid messing with ragged tensors
          __lowercase						=						python_outputs[key].numpy()
          __lowercase						=						tf_outputs[key].numpy()

          self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape				)				)
          self.assertTrue(tf.reduce_all(tf.cast(A_   ,							tf.intaa				) == tf_outputs_values				)				)
      @slow
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[Any]				):






       '''simple docstring'''


       for tf_tokenizer in self.tf_tokenizers:
        __lowercase						=						tf.function(A_				)
        for test_inputs in self.test_sentences:
         __lowercase						=						tf.constant(A_				)
         __lowercase						=						compiled_tokenizer(A_				)
         __lowercase						=						tf_tokenizer(A_				)

         for key in eager_outputs.keys():
          self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]				)				)
      @slow
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       for tf_tokenizer in self.tf_tokenizers:
        __lowercase						=						ModelToSave(tokenizer=A_				)
        __lowercase						=						tf.convert_to_tensor([self.test_sentences[0]]				)
        __lowercase						=						model.serving(A_				)  # Build model with some sample inputs
        with TemporaryDirectory() as tempdir:
         __lowercase						=						Path(A_				) / """saved.model"""
         tf.saved_model.save(A_   ,							A_   ,							signatures={"""serving_default""": model.serving}				)
         __lowercase						=						tf.saved_model.load(A_				)
        __lowercase						=						loaded_model.signatures["""serving_default"""](A_				)["""output_0"""]
        # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
        self.assertTrue(tf.reduce_all(out == loaded_output				)				)
      @slow
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       for tf_tokenizer in self.tf_tokenizers:
        __lowercase						=						tf.convert_to_tensor([self.test_sentences[0]]				)
        __lowercase						=						tf_tokenizer(A_				)  # Build model with some sample inputs

        __lowercase						=						tf_tokenizer.get_config()
        __lowercase						=						TFGPTaTokenizer.from_config(A_				)
        __lowercase						=						model_from_config(A_				)

        for key in from_config_output.keys():
         self.assertTrue(tf.reduce_all(from_config_output[key] == out[key]				)				)






      @slow
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       for tf_tokenizer in self.tf_tokenizers:
        # for the test to run
        __lowercase						=						1_2_3_1_2_3

        for max_length in [3, 5, 1_0_2_4]:
         __lowercase						=						tf.convert_to_tensor([self.test_sentences[0]]				)
         __lowercase						=						tf_tokenizer(A_   ,							max_length=A_				)

         __lowercase						=						out["""input_ids"""].numpy().shape[1]

         assert out_length == max_length




"""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__    (		_a      , _a      , unittest.TestCase     ):
      a			:  Any           =		IFPipeline
      a			:  str           =		TEXT_TO_IMAGE_PARAMS - {"""width""", """height""", """latents"""}
      a			:  Dict           =		TEXT_TO_IMAGE_BATCH_PARAMS
      a			:  int           =		PipelineTesterMixin.required_optional_params - {"""latents"""}
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       return self._get_dummy_components()
      def SCREAMING_SNAKE_CASE_						(		self						:						Any   ,							A_						:						int   ,							A_						:						Dict=0				):






       '''simple docstring'''


       if str(A_				).startswith("""mps"""				):
        __lowercase						=						torch.manual_seed(A_				)
       else:
        __lowercase						=						torch.Generator(device=A_				).manual_seed(A_				)

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

       return inputs
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       self._test_save_load_optional_components()
      @unittest.skipIf(torch_device != """cuda"""   ,							reason="""float16 requires CUDA"""				)
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       super().test_save_load_floataa(expected_max_diff=1e-1				)
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       self._test_attention_slicing_forward_pass(expected_max_diff=1e-2				)
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       self._test_save_load_local()
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       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 SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3				)






@slow
@require_torch_gpu
class  lowerCamelCase__    (		unittest.TestCase     ):
      def SCREAMING_SNAKE_CASE_						(		self						:						str				):






       '''simple docstring'''


       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       __lowercase						=						IFPipeline.from_pretrained("""DeepFloyd/IF-I-XL-v1.0"""   ,							variant="""fp16"""   ,							torch_dtype=torch.floataa				)

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

       # pre compute text embeddings and remove T5 to save memory

       pipe_a.text_encoder.to("""cuda"""				)

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

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

       __lowercase						=						None
       __lowercase						=						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(A_   ,							A_   ,							A_   ,							A_				)

       pipe_a.remove_all_hooks()
       pipe_a.remove_all_hooks()

       # img2img

       __lowercase						=						IFImgaImgPipeline(**pipe_a.components				)
       __lowercase						=						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(A_   ,							A_   ,							A_   ,							A_				)

       pipe_a.remove_all_hooks()
       pipe_a.remove_all_hooks()

       # inpainting

       __lowercase						=						IFInpaintingPipeline(**pipe_a.components				)
       __lowercase						=						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(A_   ,							A_   ,							A_   ,							A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]   ,							A_						:						Any   ,							A_						:						int   ,							A_						:						str   ,							A_						:						Dict				):






       '''simple docstring'''


       _start_torch_memory_measurement()

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

       __lowercase						=						output.images[0]

       assert image.shape == (6_4, 6_4, 3)

       __lowercase						=						torch.cuda.max_memory_allocated()
       assert mem_bytes < 1_3 * 1_0**9

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

       # pipeline 2

       _start_torch_memory_measurement()

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

       __lowercase						=						floats_tensor((1, 3, 6_4, 6_4)   ,							rng=random.Random(0				)				).to(A_				)

       __lowercase						=						pipe_a(
           prompt_embeds=A_   ,							negative_prompt_embeds=A_   ,							image=A_   ,							generator=A_   ,							num_inference_steps=2   ,							output_type="""np"""   ,							)

       __lowercase						=						output.images[0]

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

       __lowercase						=						torch.cuda.max_memory_allocated()
       assert mem_bytes < 4 * 1_0**9

       __lowercase						=						load_numpy(
           """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy"""				)
       assert_mean_pixel_difference(A_   ,							A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[Any]   ,							A_						:						Tuple   ,							A_						:						List[Any]   ,							A_						:						List[Any]   ,							A_						:						Any				):






       '''simple docstring'''


       _start_torch_memory_measurement()

       __lowercase						=						floats_tensor((1, 3, 6_4, 6_4)   ,							rng=random.Random(0				)				).to(A_				)

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

       __lowercase						=						pipe_a(
           prompt_embeds=A_   ,							negative_prompt_embeds=A_   ,							image=A_   ,							num_inference_steps=2   ,							generator=A_   ,							output_type="""np"""   ,							)

       __lowercase						=						output.images[0]

       assert image.shape == (6_4, 6_4, 3)

       __lowercase						=						torch.cuda.max_memory_allocated()
       assert mem_bytes < 1_0 * 1_0**9

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

       # pipeline 2

       _start_torch_memory_measurement()

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

       __lowercase						=						floats_tensor((1, 3, 2_5_6, 2_5_6)   ,							rng=random.Random(0				)				).to(A_				)
       __lowercase						=						floats_tensor((1, 3, 6_4, 6_4)   ,							rng=random.Random(0				)				).to(A_				)

       __lowercase						=						pipe_a(
           prompt_embeds=A_   ,							negative_prompt_embeds=A_   ,							image=A_   ,							original_image=A_   ,							generator=A_   ,							num_inference_steps=2   ,							output_type="""np"""   ,							)

       __lowercase						=						output.images[0]

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

       __lowercase						=						torch.cuda.max_memory_allocated()
       assert mem_bytes < 4 * 1_0**9

       __lowercase						=						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(A_   ,							A_				)






      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]   ,							A_						:						List[Any]   ,							A_						:						str   ,							A_						:						List[Any]   ,							A_						:						List[Any]				):






       '''simple docstring'''


       _start_torch_memory_measurement()

       __lowercase						=						floats_tensor((1, 3, 6_4, 6_4)   ,							rng=random.Random(0				)				).to(A_				)
       __lowercase						=						floats_tensor((1, 3, 6_4, 6_4)   ,							rng=random.Random(1				)				).to(A_				)

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

       __lowercase						=						output.images[0]

       assert image.shape == (6_4, 6_4, 3)

       __lowercase						=						torch.cuda.max_memory_allocated()
       assert mem_bytes < 1_0 * 1_0**9

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

       # pipeline 2

       _start_torch_memory_measurement()

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

       __lowercase						=						floats_tensor((1, 3, 6_4, 6_4)   ,							rng=random.Random(0				)				).to(A_				)
       __lowercase						=						floats_tensor((1, 3, 2_5_6, 2_5_6)   ,							rng=random.Random(0				)				).to(A_				)
       __lowercase						=						floats_tensor((1, 3, 2_5_6, 2_5_6)   ,							rng=random.Random(1				)				).to(A_				)

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

       __lowercase						=						output.images[0]

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

       __lowercase						=						torch.cuda.max_memory_allocated()
       assert mem_bytes < 4 * 1_0**9

       __lowercase						=						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(A_   ,							A_				)






def lowerCAmelCase_   (					):






 """simple docstring"""







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