






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(	__SCREAMING_SNAKE_CASE							,     __SCREAMING_SNAKE_CASE							,     unittest.TestCase ):





       """simple docstring"""







       a    :		Optional[Any]									=IFPipeline
       a    :		str									=TEXT_TO_IMAGE_PARAMS - {"width", "height", "latents"}
       a    :		Union[str, Any]									=TEXT_TO_IMAGE_BATCH_PARAMS
       a    :		Optional[int]									=PipelineTesterMixin.required_optional_params - {"latents"}





       def       _a   ( self							):
         return self._get_dummy_components()





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase=0							):
         if str(_snake_case							).startswith('mps'							):
           UpperCamelCase_: int   				=    torch.manual_seed(_snake_case							)
         else:
           UpperCamelCase_: Dict   				=    torch.Generator(device=_snake_case							).manual_seed(_snake_case							)

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

         return inputs





       def       _a   ( self							):
         self._test_save_load_optional_components()





       @unittest.skipIf(torch_device != 'cuda'  ,      reason='float16 requires CUDA'							)
       def       _a   ( self							):
         # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
         super().test_save_load_floataa(expected_max_diff=1e-1							)





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





       def       _a   ( self							):
         self._test_save_load_local()





       def       _a   ( self							):
         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       _a   ( self							):
         self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3							)







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





       """simple docstring"""







       def       _a   ( self							):
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()





       def       _a   ( self							):
         # if

         UpperCamelCase_: List[Any]   				=    IFPipeline.from_pretrained('DeepFloyd/IF-I-XL-v1.0'  ,      variant='fp16'  ,      torch_dtype=torch.floataa							)

         UpperCamelCase_: Dict   				=    IFSuperResolutionPipeline.from_pretrained(
             'DeepFloyd/IF-II-L-v1.0'  ,      variant='fp16'  ,      torch_dtype=torch.floataa  ,      text_encoder=_snake_case  ,      tokenizer=_snake_case							)

         # pre compute text embeddings and remove T5 to save memory

         pipe_a.text_encoder.to('cuda'							)

         UpperCamelCase_: Union[str, Any]   				=    pipe_a.encode_prompt('anime turtle'  ,      device='cuda'							)

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

         UpperCamelCase_: Tuple   				=    None
         UpperCamelCase_: List[Any]   				=    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(_snake_case  ,      _snake_case  ,      _snake_case  ,      _snake_case							)

         pipe_a.remove_all_hooks()
         pipe_a.remove_all_hooks()

         # img2img

         UpperCamelCase_: Dict   				=    IFImgaImgPipeline(**pipe_a.components							)
         UpperCamelCase_: Any   				=    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(_snake_case  ,      _snake_case  ,      _snake_case  ,      _snake_case							)

         pipe_a.remove_all_hooks()
         pipe_a.remove_all_hooks()

         # inpainting

         UpperCamelCase_: str   				=    IFInpaintingPipeline(**pipe_a.components							)
         UpperCamelCase_: Dict   				=    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(_snake_case  ,      _snake_case  ,      _snake_case  ,      _snake_case							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         # pipeline 1

         _start_torch_memory_measurement()

         UpperCamelCase_: List[str]   				=    torch.Generator(device='cpu'							).manual_seed(0							)
         UpperCamelCase_: Any   				=    pipe_a(
             prompt_embeds=_snake_case  ,      negative_prompt_embeds=_snake_case  ,      num_inference_steps=2  ,      generator=_snake_case  ,      output_type='np'  ,      )

         UpperCamelCase_: List[str]   				=    output.images[0]

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

         UpperCamelCase_: Dict   				=    torch.cuda.max_memory_allocated()
         assert mem_bytes < 1_3 * 1_0**9

         UpperCamelCase_: List[str]   				=    load_numpy(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy'							)
         assert_mean_pixel_difference(_snake_case  ,      _snake_case							)

         # pipeline 2

         _start_torch_memory_measurement()

         UpperCamelCase_: Any   				=    torch.Generator(device='cpu'							).manual_seed(0							)

         UpperCamelCase_: Optional[Any]   				=    floats_tensor((1, 3, 6_4, 6_4)  ,      rng=random.Random(0							)							).to(_snake_case							)

         UpperCamelCase_: Dict   				=    pipe_a(
             prompt_embeds=_snake_case  ,      negative_prompt_embeds=_snake_case  ,      image=_snake_case  ,      generator=_snake_case  ,      num_inference_steps=2  ,      output_type='np'  ,      )

         UpperCamelCase_: int   				=    output.images[0]

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

         UpperCamelCase_: Tuple   				=    torch.cuda.max_memory_allocated()
         assert mem_bytes < 4 * 1_0**9

         UpperCamelCase_: Any   				=    load_numpy(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy'							)
         assert_mean_pixel_difference(_snake_case  ,      _snake_case							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         # pipeline 1

         _start_torch_memory_measurement()

         UpperCamelCase_: str   				=    floats_tensor((1, 3, 6_4, 6_4)  ,      rng=random.Random(0							)							).to(_snake_case							)

         UpperCamelCase_: str   				=    torch.Generator(device='cpu'							).manual_seed(0							)

         UpperCamelCase_: int   				=    pipe_a(
             prompt_embeds=_snake_case  ,      negative_prompt_embeds=_snake_case  ,      image=_snake_case  ,      num_inference_steps=2  ,      generator=_snake_case  ,      output_type='np'  ,      )

         UpperCamelCase_: List[str]   				=    output.images[0]

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

         UpperCamelCase_: Optional[Any]   				=    torch.cuda.max_memory_allocated()
         assert mem_bytes < 1_0 * 1_0**9

         UpperCamelCase_: Any   				=    load_numpy(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy'							)
         assert_mean_pixel_difference(_snake_case  ,      _snake_case							)

         # pipeline 2

         _start_torch_memory_measurement()

         UpperCamelCase_: List[str]   				=    torch.Generator(device='cpu'							).manual_seed(0							)

         UpperCamelCase_: Optional[int]   				=    floats_tensor((1, 3, 2_5_6, 2_5_6)  ,      rng=random.Random(0							)							).to(_snake_case							)
         UpperCamelCase_: List[str]   				=    floats_tensor((1, 3, 6_4, 6_4)  ,      rng=random.Random(0							)							).to(_snake_case							)

         UpperCamelCase_: Any   				=    pipe_a(
             prompt_embeds=_snake_case  ,      negative_prompt_embeds=_snake_case  ,      image=_snake_case  ,      original_image=_snake_case  ,      generator=_snake_case  ,      num_inference_steps=2  ,      output_type='np'  ,      )

         UpperCamelCase_: Optional[Any]   				=    output.images[0]

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

         UpperCamelCase_: Dict   				=    torch.cuda.max_memory_allocated()
         assert mem_bytes < 4 * 1_0**9

         UpperCamelCase_: List[str]   				=    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(_snake_case  ,      _snake_case							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         # pipeline 1

         _start_torch_memory_measurement()

         UpperCamelCase_: List[Any]   				=    floats_tensor((1, 3, 6_4, 6_4)  ,      rng=random.Random(0							)							).to(_snake_case							)
         UpperCamelCase_: Optional[Any]   				=    floats_tensor((1, 3, 6_4, 6_4)  ,      rng=random.Random(1							)							).to(_snake_case							)

         UpperCamelCase_: Dict   				=    torch.Generator(device='cpu'							).manual_seed(0							)
         UpperCamelCase_: Any   				=    pipe_a(
             prompt_embeds=_snake_case  ,      negative_prompt_embeds=_snake_case  ,      image=_snake_case  ,      mask_image=_snake_case  ,      num_inference_steps=2  ,      generator=_snake_case  ,      output_type='np'  ,      )

         UpperCamelCase_: Union[str, Any]   				=    output.images[0]

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

         UpperCamelCase_: str   				=    torch.cuda.max_memory_allocated()
         assert mem_bytes < 1_0 * 1_0**9

         UpperCamelCase_: List[str]   				=    load_numpy(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy'							)
         assert_mean_pixel_difference(_snake_case  ,      _snake_case							)

         # pipeline 2

         _start_torch_memory_measurement()

         UpperCamelCase_: Tuple   				=    torch.Generator(device='cpu'							).manual_seed(0							)

         UpperCamelCase_: Any   				=    floats_tensor((1, 3, 6_4, 6_4)  ,      rng=random.Random(0							)							).to(_snake_case							)
         UpperCamelCase_: str   				=    floats_tensor((1, 3, 2_5_6, 2_5_6)  ,      rng=random.Random(0							)							).to(_snake_case							)
         UpperCamelCase_: int   				=    floats_tensor((1, 3, 2_5_6, 2_5_6)  ,      rng=random.Random(1							)							).to(_snake_case							)

         UpperCamelCase_: List[str]   				=    pipe_a(
             prompt_embeds=_snake_case  ,      negative_prompt_embeds=_snake_case  ,      image=_snake_case  ,      mask_image=_snake_case  ,      original_image=_snake_case  ,      generator=_snake_case  ,      num_inference_steps=2  ,      output_type='np'  ,      )

         UpperCamelCase_: str   				=    output.images[0]

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

         UpperCamelCase_: Dict   				=    torch.cuda.max_memory_allocated()
         assert mem_bytes < 4 * 1_0**9

         UpperCamelCase_: Dict   				=    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(_snake_case  ,      _snake_case							)







def   snake_case ()	->    Optional[int]:
  torch.cuda.empty_cache()
  torch.cuda.reset_max_memory_allocated()
  torch.cuda.reset_peak_memory_stats()







import json
import os
import unittest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class     _lowerCAmelCase(	UpperCAmelCase_							,     unittest.TestCase ):





       """simple docstring"""







       a    :		Optional[Any]									=CLIPTokenizer
       a    :		List[Any]									=CLIPTokenizerFast
       a    :		str									=True
       a    :		List[Any]									={}
       a    :		str									=False





       def       _a   ( self							):
         super().setUp()

         # fmt: off
         UpperCamelCase_: str   				=    ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>']
         # fmt: on
         UpperCamelCase_: Any   				=    dict(zip(_lowerCamelCase  ,      range(len(_lowerCamelCase							)							)							)							)
         UpperCamelCase_: Optional[int]   				=    ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>']
         UpperCamelCase_: Any   				=    {'unk_token': '<unk>'}

         UpperCamelCase_: List[Any]   				=    os.path.join(self.tmpdirname  ,      VOCAB_FILES_NAMES['vocab_file']							)
         UpperCamelCase_: Optional[int]   				=    os.path.join(self.tmpdirname  ,      VOCAB_FILES_NAMES['merges_file']							)
         with open(self.vocab_file  ,      'w'  ,      encoding='utf-8'							) as fp:
           fp.write(json.dumps(_lowerCamelCase							) + '\n'							)
         with open(self.merges_file  ,      'w'  ,      encoding='utf-8'							) as fp:
           fp.write('\n'.join(_lowerCamelCase							)							)





       def       _a   ( self  ,      **_lowerCamelCase							):
         kwargs.update(self.special_tokens_map							)
         return CLIPTokenizer.from_pretrained(self.tmpdirname  ,      **_lowerCamelCase							)





       def       _a   ( self  ,      **_lowerCamelCase							):
         kwargs.update(self.special_tokens_map							)
         return CLIPTokenizerFast.from_pretrained(self.tmpdirname  ,      **_lowerCamelCase							)





       def       _a   ( self  ,      _lowerCamelCase							):
         UpperCamelCase_: Union[str, Any]   				=    'lower newer'
         UpperCamelCase_: Tuple   				=    'lower newer'
         return input_text, output_text





       def       _a   ( self							):
         UpperCamelCase_: Dict   				=    CLIPTokenizer(self.vocab_file  ,      self.merges_file  ,      **self.special_tokens_map							)
         UpperCamelCase_: Optional[int]   				=    'lower newer'
         UpperCamelCase_: Any   				=    ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>']
         UpperCamelCase_: Union[str, Any]   				=    tokenizer.tokenize(_lowerCamelCase							)
         self.assertListEqual(_lowerCamelCase  ,      _lowerCamelCase							)

         UpperCamelCase_: str   				=    tokens + [tokenizer.unk_token]
         UpperCamelCase_: Optional[Any]   				=    [1_0, 2, 1_6, 9, 3, 2, 1_6, 2_0]
         self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCamelCase							)  ,      _lowerCamelCase							)





       @require_ftfy
       def       _a   ( self							):
         for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
           with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''							):
             UpperCamelCase_: Optional[int]   				=    self.tokenizer_class.from_pretrained(_lowerCamelCase  ,      **_lowerCamelCase							)
             UpperCamelCase_: List[str]   				=    self.rust_tokenizer_class.from_pretrained(_lowerCamelCase  ,      **_lowerCamelCase							)

             UpperCamelCase_: Optional[int]   				=    'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.'
             UpperCamelCase_: Tuple   				=    tokenizer_s.tokenize(_lowerCamelCase							)
             UpperCamelCase_: Any   				=    tokenizer_r.tokenize(_lowerCamelCase							)

             self.assertListEqual(_lowerCamelCase  ,      _lowerCamelCase							)

             # Test that the tokenization is identical on an example containing a character (Latin Small Letter A
             # with Tilde) encoded in 2 different ways
             UpperCamelCase_: List[str]   				=    'xa\u0303y' + ' ' + 'x\xe3y'
             UpperCamelCase_: List[Any]   				=    tokenizer_s.tokenize(_lowerCamelCase							)
             UpperCamelCase_: int   				=    tokenizer_r.tokenize(_lowerCamelCase							)

             self.assertListEqual(_lowerCamelCase  ,      _lowerCamelCase							)

             # Test that the tokenization is identical on unicode of space type
             UpperCamelCase_: Dict   				=    [
                 '\u0009',  # (horizontal tab, '\t')
                 '\u000B',  # (vertical tab)
                 '\u000C',  # (form feed)
                 '\u0020',  # (space, ' ')
                 '\u200E',  # (left-to-right mark):w
                 '\u200F',  # (right-to-left mark)
             ]
             for unicode_seq in spaces_unicodes:
               UpperCamelCase_: int   				=    tokenizer_s.tokenize(_lowerCamelCase							)
               UpperCamelCase_: Dict   				=    tokenizer_r.tokenize(_lowerCamelCase							)

               self.assertListEqual(_lowerCamelCase  ,      _lowerCamelCase							)

             # Test that the tokenization is identical on unicode of line break type
             UpperCamelCase_: List[str]   				=    [
                 '\u000A',  # (line feed, '\n')
                 '\r\n',  # (carriage return and line feed, '\r\n')
                 '\u000D',  # (carriage return, '\r')
                 '\r',  # (carriage return, '\r')
                 '\u000D',  # (carriage return, '\r')
                 '\u2028',  # (line separator)
                 '\u2029',  # (paragraph separator)
                 # "\u0085", # (next line)
             ]

             # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms
             # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a
             # space (and thus into an empty list).

             for unicode_seq in line_break_unicodes:
               UpperCamelCase_: Optional[Any]   				=    tokenizer_s.tokenize(_lowerCamelCase							)
               UpperCamelCase_: str   				=    tokenizer_r.tokenize(_lowerCamelCase							)

               self.assertListEqual(_lowerCamelCase  ,      _lowerCamelCase							)





       def       _a   ( self							):
         # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space`
         for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
           with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''							):
             UpperCamelCase_: str   				=    'hello'  # `hello` is a token in the vocabulary of `pretrained_name`
             UpperCamelCase_: str   				=    f'''{text_of_1_token} {text_of_1_token}'''

             UpperCamelCase_: Optional[Any]   				=    self.rust_tokenizer_class.from_pretrained(
                 _lowerCamelCase  ,      use_fast=_lowerCamelCase  ,      )
             UpperCamelCase_: int   				=    tokenizer_r(_lowerCamelCase  ,      return_offsets_mapping=_lowerCamelCase  ,      add_special_tokens=_lowerCamelCase							)
             self.assertEqual(encoding.offset_mapping[0]  ,      (0, len(_lowerCamelCase							))							)
             self.assertEqual(
                 encoding.offset_mapping[1]  ,      (len(_lowerCamelCase							) + 1, len(_lowerCamelCase							) + 1 + len(_lowerCamelCase							))  ,      )

             UpperCamelCase_: List[str]   				=    f''' {text}'''

             UpperCamelCase_: str   				=    self.rust_tokenizer_class.from_pretrained(
                 _lowerCamelCase  ,      use_fast=_lowerCamelCase  ,      )
             UpperCamelCase_: Dict   				=    tokenizer_r(_lowerCamelCase  ,      return_offsets_mapping=_lowerCamelCase  ,      add_special_tokens=_lowerCamelCase							)
             self.assertEqual(encoding.offset_mapping[0]  ,      (1, 1 + len(_lowerCamelCase							))							)
             self.assertEqual(
                 encoding.offset_mapping[1]  ,      (1 + len(_lowerCamelCase							) + 1, 1 + len(_lowerCamelCase							) + 1 + len(_lowerCamelCase							))  ,      )





       def       _a   ( self							):
         # Test related to the breaking change introduced in transformers v4.17.0
         # We need to check that an error in raised when the user try to load a previous version of the tokenizer.
         with self.assertRaises(_lowerCamelCase							) as context:
           self.rust_tokenizer_class.from_pretrained('robot-test/old-clip-tokenizer'							)

         self.assertTrue(
             context.exception.args[0].startswith(
                 'The `backend_tokenizer` provided does not match the expected format.'							)							)





       @require_ftfy
       def       _a   ( self							):
         super().test_tokenization_python_rust_equals()





       def       _a   ( self							):
         # CLIP always lower cases letters
         pass