

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



      def 						UpperCamelCase_		(      self				:    Any		)   ->					Dict:
        # clean up the VRAM after each test
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()



      @property
      def 						UpperCamelCase_		(      self				:    Tuple		)   ->					Optional[int]:
        _snake_case						=							1
        _snake_case						=							3
        _snake_case						=							(32, 32)

        _snake_case						=							floats_tensor((batch_size, num_channels) + sizes      , rng=random.Random(0		)		).to(A__		)
        return image



      @property
      def 						UpperCamelCase_		(      self				:    Any		)   ->					List[str]:
        torch.manual_seed(0		)
        _snake_case						=							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 						UpperCamelCase_		(      self				:    Optional[Any]		)   ->					Dict:
        torch.manual_seed(0		)
        _snake_case						=							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 						UpperCamelCase_		(      self				:    Optional[int]		)   ->					Optional[int]:
        torch.manual_seed(0		)
        _snake_case						=							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=5006      , )
        return RobertaSeriesModelWithTransformation(A__		)



      @property
      def 						UpperCamelCase_		(      self				:    Tuple		)   ->					Tuple:
        def extract(*A__				:    int      , **A__				:    List[Any]		):



          class   lowercase_   :



                def __init__(      self				:    List[str]		)   ->					Union[str, Any]:
                  _snake_case						=							torch.ones([0]		)







                def 						UpperCamelCase_		(      self				:    List[Any]      , A__				:    Optional[int]		)   ->					int:
                  self.pixel_values.to(A__		)
                  return self

          return Out()

        return extract



      def 						UpperCamelCase_		(      self				:    Union[str, Any]		)   ->					Tuple:
        _snake_case						=							'''cpu'''  # ensure determinism for the device-dependent torch.Generator
        _snake_case						=							self.dummy_cond_unet
        _snake_case						=							PNDMScheduler(skip_prk_steps=A__		)
        _snake_case						=							self.dummy_vae
        _snake_case						=							self.dummy_text_encoder
        _snake_case						=							XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''		)
        _snake_case						=							77

        _snake_case						=							self.dummy_image.to(A__		)
        _snake_case						=							init_image / 2 + 0.5

        # make sure here that pndm scheduler skips prk
        _snake_case						=							AltDiffusionImgaImgPipeline(
            unet=A__      , scheduler=A__      , vae=A__      , text_encoder=A__      , tokenizer=A__      , safety_checker=A__      , feature_extractor=self.dummy_extractor      , )
        _snake_case						=							VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor      , do_normalize=A__		)
        _snake_case						=							alt_pipe.to(A__		)
        alt_pipe.set_progress_bar_config(disable=A__		)

        _snake_case						=							'''A painting of a squirrel eating a burger'''
        _snake_case						=							torch.Generator(device=A__		).manual_seed(0		)
        _snake_case						=							alt_pipe(
            [prompt]      , generator=A__      , guidance_scale=6.0      , num_inference_steps=2      , output_type='''np'''      , image=A__      , )

        _snake_case						=							output.images

        _snake_case						=							torch.Generator(device=A__		).manual_seed(0		)
        _snake_case						=							alt_pipe(
            [prompt]      , generator=A__      , guidance_scale=6.0      , num_inference_steps=2      , output_type='''np'''      , image=A__      , return_dict=A__      , )[0]

        _snake_case						=							image[0, -3:, -3:, -1]
        _snake_case						=							image_from_tuple[0, -3:, -3:, -1]

        assert image.shape == (1, 32, 32, 3)
        _snake_case						=							np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499]		)

        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 						UpperCamelCase_		(      self				:    Optional[Any]		)   ->					List[str]:
        _snake_case						=							self.dummy_cond_unet
        _snake_case						=							PNDMScheduler(skip_prk_steps=A__		)
        _snake_case						=							self.dummy_vae
        _snake_case						=							self.dummy_text_encoder
        _snake_case						=							XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''		)
        _snake_case						=							77

        _snake_case						=							self.dummy_image.to(A__		)

        # put models in fp16
        _snake_case						=							unet.half()
        _snake_case						=							vae.half()
        _snake_case						=							bert.half()

        # make sure here that pndm scheduler skips prk
        _snake_case						=							AltDiffusionImgaImgPipeline(
            unet=A__      , scheduler=A__      , vae=A__      , text_encoder=A__      , tokenizer=A__      , safety_checker=A__      , feature_extractor=self.dummy_extractor      , )
        _snake_case						=							VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor      , do_normalize=A__		)
        _snake_case						=							alt_pipe.to(A__		)
        alt_pipe.set_progress_bar_config(disable=A__		)

        _snake_case						=							'''A painting of a squirrel eating a burger'''
        _snake_case						=							torch.manual_seed(0		)
        _snake_case						=							alt_pipe(
            [prompt]      , generator=A__      , num_inference_steps=2      , output_type='''np'''      , image=A__      , ).images

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







      @unittest.skipIf(torch_device != '''cuda'''      , '''This test requires a GPU'''		)
      def 						UpperCamelCase_		(      self				:    str		)   ->					Dict:
        _snake_case						=							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
        _snake_case						=							init_image.resize((760, 504)		)

        _snake_case						=							'''BAAI/AltDiffusion'''
        _snake_case						=							AltDiffusionImgaImgPipeline.from_pretrained(
            A__      , safety_checker=A__      , )
        pipe.to(A__		)
        pipe.set_progress_bar_config(disable=A__		)
        pipe.enable_attention_slicing()

        _snake_case						=							'''A fantasy landscape, trending on artstation'''

        _snake_case						=							torch.manual_seed(0		)
        _snake_case						=							pipe(
            prompt=A__      , image=A__      , strength=0.75      , guidance_scale=7.5      , generator=A__      , output_type='''np'''      , )
        _snake_case						=							output.images[0]

        _snake_case						=							image[255:258, 383:386, -1]

        assert image.shape == (504, 760, 3)
        _snake_case						=							np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000]		)

        assert np.abs(image_slice.flatten() - expected_slice		).max() < 1e-2



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



      def 						UpperCamelCase_		(      self				:    List[str]		)   ->					List[str]:
        # clean up the VRAM after each test
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()







      def 						UpperCamelCase_		(      self				:    Any		)   ->					Any:
        _snake_case						=							load_image(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
            '''/img2img/sketch-mountains-input.jpg'''		)
        _snake_case						=							init_image.resize((768, 512)		)
        _snake_case						=							load_numpy(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy'''		)

        _snake_case						=							'''BAAI/AltDiffusion'''
        _snake_case						=							AltDiffusionImgaImgPipeline.from_pretrained(
            A__      , safety_checker=A__      , )
        pipe.to(A__		)
        pipe.set_progress_bar_config(disable=A__		)
        pipe.enable_attention_slicing()

        _snake_case						=							'''A fantasy landscape, trending on artstation'''

        _snake_case						=							torch.manual_seed(0		)
        _snake_case						=							pipe(
            prompt=A__      , image=A__      , strength=0.75      , guidance_scale=7.5      , generator=A__      , output_type='''np'''      , )
        _snake_case						=							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




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   lowercase_   (  __lowercase       ,							unittest.TestCase					):
      UpperCamelCase_      :		Union[str, Any]										=    CLIPTokenizer
      UpperCamelCase_      :		Optional[int]										=    CLIPTokenizerFast
      UpperCamelCase_      :		Dict										=    True
      UpperCamelCase_      :		Union[str, Any]										=    {}
      UpperCamelCase_      :		Optional[Any]										=    False



      def 						UpperCamelCase_		(      self				:    Union[str, Any]		)   ->					Dict:
        super().setUp()

        # fmt: off
        _snake_case						=							['''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
        _snake_case						=							dict(zip(A__      , range(len(A__		)		)		)		)
        _snake_case						=							['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''']
        _snake_case						=							{'''unk_token''': '''<unk>'''}

        _snake_case						=							os.path.join(self.tmpdirname      , VOCAB_FILES_NAMES['''vocab_file''']		)
        _snake_case						=							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(A__		) + '''\n'''		)
        with open(self.merges_file      , '''w'''      , encoding='''utf-8'''		) as fp:
          fp.write('''\n'''.join(A__		)		)



      def 						UpperCamelCase_		(      self				:    List[Any]      , **A__				:    int		)   ->					Union[str, Any]:
        kwargs.update(self.special_tokens_map		)
        return CLIPTokenizer.from_pretrained(self.tmpdirname      , **A__		)



      def 						UpperCamelCase_		(      self				:    Any      , **A__				:    Tuple		)   ->					Optional[Any]:
        kwargs.update(self.special_tokens_map		)
        return CLIPTokenizerFast.from_pretrained(self.tmpdirname      , **A__		)



      def 						UpperCamelCase_		(      self				:    Optional[Any]      , A__				:    str		)   ->					str:
        _snake_case						=							'''lower newer'''
        _snake_case						=							'''lower newer'''
        return input_text, output_text



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

        _snake_case						=							tokens + [tokenizer.unk_token]
        _snake_case						=							[10, 2, 16, 9, 3, 2, 16, 20]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(A__		)      , A__		)



      @require_ftfy
      def 						UpperCamelCase_		(      self				:    Any		)   ->					Optional[int]:
        for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
          with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""		):
            _snake_case						=							self.tokenizer_class.from_pretrained(A__      , **A__		)
            _snake_case						=							self.rust_tokenizer_class.from_pretrained(A__      , **A__		)

            _snake_case						=							'''A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.'''
            _snake_case						=							tokenizer_s.tokenize(A__		)
            _snake_case						=							tokenizer_r.tokenize(A__		)

            self.assertListEqual(A__      , A__		)

            # Test that the tokenization is identical on an example containing a character (Latin Small Letter A
            # with Tilde) encoded in 2 different ways
            _snake_case						=							'''xa\u0303y''' + ''' ''' + '''x\xe3y'''
            _snake_case						=							tokenizer_s.tokenize(A__		)
            _snake_case						=							tokenizer_r.tokenize(A__		)

            self.assertListEqual(A__      , A__		)

            # Test that the tokenization is identical on unicode of space type
            _snake_case						=							[
                '''\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:
              _snake_case						=							tokenizer_s.tokenize(A__		)
              _snake_case						=							tokenizer_r.tokenize(A__		)

              self.assertListEqual(A__      , A__		)

            # Test that the tokenization is identical on unicode of line break type
            _snake_case						=							[
                '''\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:
              _snake_case						=							tokenizer_s.tokenize(A__		)
              _snake_case						=							tokenizer_r.tokenize(A__		)

              self.assertListEqual(A__      , A__		)



      def 						UpperCamelCase_		(      self				:    List[Any]		)   ->					Optional[Any]:
        # 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})"""		):
            _snake_case						=							'''hello'''  # `hello` is a token in the vocabulary of `pretrained_name`
            _snake_case						=							f"""{text_of_1_token} {text_of_1_token}"""

            _snake_case						=							self.rust_tokenizer_class.from_pretrained(
                A__      , use_fast=A__      , )
            _snake_case						=							tokenizer_r(A__      , return_offsets_mapping=A__      , add_special_tokens=A__		)
            self.assertEqual(encoding.offset_mapping[0]      , (0, len(A__		))		)
            self.assertEqual(
                encoding.offset_mapping[1]      , (len(A__		) + 1, len(A__		) + 1 + len(A__		))      , )

            _snake_case						=							f""" {text}"""

            _snake_case						=							self.rust_tokenizer_class.from_pretrained(
                A__      , use_fast=A__      , )
            _snake_case						=							tokenizer_r(A__      , return_offsets_mapping=A__      , add_special_tokens=A__		)
            self.assertEqual(encoding.offset_mapping[0]      , (1, 1 + len(A__		))		)
            self.assertEqual(
                encoding.offset_mapping[1]      , (1 + len(A__		) + 1, 1 + len(A__		) + 1 + len(A__		))      , )



      def 						UpperCamelCase_		(      self				:    Union[str, Any]		)   ->					Optional[Any]:
        # 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(A__		) 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 						UpperCamelCase_		(      self				:    Dict		)   ->					Union[str, Any]:
        super().test_tokenization_python_rust_equals()







      def 						UpperCamelCase_		(      self				:    str		)   ->					Optional[int]:
        # CLIP always lower cases letters
        pass

