



'''simple docstring'''

import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


class   a_  (_a							,			_a							,			unittest.TestCase    ):
       __lowerCAmelCase    :				Union[str, Any]    =      StableDiffusionXLPipeline
       __lowerCAmelCase    :				str    =      TEXT_TO_IMAGE_PARAMS
       __lowerCAmelCase    :				str    =      TEXT_TO_IMAGE_BATCH_PARAMS
       __lowerCAmelCase    :				List[Any]    =      TEXT_TO_IMAGE_IMAGE_PARAMS
       __lowerCAmelCase    :				Any    =      TEXT_TO_IMAGE_IMAGE_PARAMS




       def 							__UpperCamelCase       (     self  ):
         torch.manual_seed(0  )
         _lowerCAmelCase       :  Optional[Any]   				=					UNetaDConditionModel(
             block_out_channels=(3_2, 6_4)   ,   layers_per_block=2   ,   sample_size=3_2   ,   in_channels=4   ,   out_channels=4   ,   down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")   ,   up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")   ,   attention_head_dim=(2, 4)   ,   use_linear_projection=snake_case_   ,   addition_embed_type="""text_time"""   ,   addition_time_embed_dim=8   ,   transformer_layers_per_block=(1, 2)   ,   projection_class_embeddings_input_dim=8_0   ,   cross_attention_dim=6_4   ,   )
         _lowerCAmelCase       :  Dict   				=					EulerDiscreteScheduler(
             beta_start=0.0_0085   ,   beta_end=0.012   ,   steps_offset=1   ,   beta_schedule="""scaled_linear"""   ,   timestep_spacing="""leading"""   ,   )
         torch.manual_seed(0  )
         _lowerCAmelCase       :  Optional[Any]   				=					AutoencoderKL(
             block_out_channels=[3_2, 6_4]   ,   in_channels=3   ,   out_channels=3   ,   down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]   ,   up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]   ,   latent_channels=4   ,   sample_size=1_2_8   ,   )
         torch.manual_seed(0  )
         _lowerCAmelCase       :  Optional[int]   				=					CLIPTextConfig(
             bos_token_id=0   ,   eos_token_id=2   ,   hidden_size=3_2   ,   intermediate_size=3_7   ,   layer_norm_eps=1E-05   ,   num_attention_heads=4   ,   num_hidden_layers=5   ,   pad_token_id=1   ,   vocab_size=1_0_0_0   ,   hidden_act="""gelu"""   ,   projection_dim=3_2   ,   )
         _lowerCAmelCase       :  Optional[int]   				=					CLIPTextModel(snake_case_  )
         _lowerCAmelCase       :  List[str]   				=					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""   ,   local_files_only=snake_case_  )

         _lowerCAmelCase       :  Optional[int]   				=					CLIPTextModelWithProjection(snake_case_  )
         _lowerCAmelCase       :  Optional[int]   				=					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""   ,   local_files_only=snake_case_  )

         _lowerCAmelCase       :  List[Any]   				=					{
             """unet""": unet,
             """scheduler""": scheduler,
             """vae""": vae,
             """text_encoder""": text_encoder,
             """tokenizer""": tokenizer,
             """text_encoder_2""": text_encoder_a,
             """tokenizer_2""": tokenizer_a,
             # "safety_checker": None,
             # "feature_extractor": None,
         }
         return components




       def 							__UpperCamelCase       (     self   ,   snake_case_   ,   snake_case_=0  ):
         if str(snake_case_  ).startswith("""mps"""  ):
           _lowerCAmelCase       :  Tuple   				=					torch.manual_seed(snake_case_  )
         else:
           _lowerCAmelCase       :  Optional[int]   				=					torch.Generator(device=snake_case_  ).manual_seed(snake_case_  )
         _lowerCAmelCase       :  Any   				=					{
             """prompt""": """A painting of a squirrel eating a burger""",
             """generator""": generator,
             """num_inference_steps""": 2,
             """guidance_scale""": 5.0,
             """output_type""": """numpy""",
         }
         return inputs




       def 							__UpperCamelCase       (     self  ):
         _lowerCAmelCase       :  Any   				=					"""cpu"""  # ensure determinism for the device-dependent torch.Generator
         _lowerCAmelCase       :  Dict   				=					self.get_dummy_components()
         _lowerCAmelCase       :  Any   				=					StableDiffusionXLPipeline(**snake_case_  )
         _lowerCAmelCase       :  Optional[Any]   				=					sd_pipe.to(snake_case_  )
         sd_pipe.set_progress_bar_config(disable=snake_case_  )

         _lowerCAmelCase       :  Optional[Any]   				=					self.get_dummy_inputs(snake_case_  )
         _lowerCAmelCase       :  str   				=					sd_pipe(**snake_case_  ).images
         _lowerCAmelCase       :  str   				=					image[0, -3:, -3:, -1]

         assert image.shape == (1, 6_4, 6_4, 3)
         _lowerCAmelCase       :  int   				=					np.array([0.5873, 0.6128, 0.4797, 0.5122, 0.5674, 0.4639, 0.5227, 0.5149, 0.4747]  )

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




       def 							__UpperCamelCase       (     self  ):
         _lowerCAmelCase       :  Optional[int]   				=					self.get_dummy_components()
         _lowerCAmelCase       :  str   				=					StableDiffusionXLPipeline(**snake_case_  )
         _lowerCAmelCase       :  Any   				=					sd_pipe.to(snake_case_  )
         _lowerCAmelCase       :  List[Any]   				=					sd_pipe.to(snake_case_  )
         sd_pipe.set_progress_bar_config(disable=snake_case_  )

         # forward without prompt embeds
         _lowerCAmelCase       :  int   				=					self.get_dummy_inputs(snake_case_  )
         _lowerCAmelCase       :  Dict   				=					3 * ["""this is a negative prompt"""]
         _lowerCAmelCase       :  List[Any]   				=					negative_prompt
         _lowerCAmelCase       :  str   				=					3 * [inputs["""prompt"""]]

         _lowerCAmelCase       :  Optional[Any]   				=					sd_pipe(**snake_case_  )
         _lowerCAmelCase       :  List[Any]   				=					output.images[0, -3:, -3:, -1]

         # forward with prompt embeds
         _lowerCAmelCase       :  Optional[int]   				=					self.get_dummy_inputs(snake_case_  )
         _lowerCAmelCase       :  Any   				=					3 * ["""this is a negative prompt"""]
         _lowerCAmelCase       :  Union[str, Any]   				=					3 * [inputs.pop("""prompt"""  )]

         (
             _lowerCAmelCase					
         )       :  str   				=					sd_pipe.encode_prompt(snake_case_   ,   negative_prompt=snake_case_  )

         _lowerCAmelCase       :  Dict   				=					sd_pipe(
             **snake_case_   ,   prompt_embeds=snake_case_   ,   negative_prompt_embeds=snake_case_   ,   pooled_prompt_embeds=snake_case_   ,   negative_pooled_prompt_embeds=snake_case_   ,   )
         _lowerCAmelCase       :  List[Any]   				=					output.images[0, -3:, -3:, -1]

         # make sure that it's equal
         assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()  ).max() < 1E-4




       def 							__UpperCamelCase       (     self  ):
         super().test_attention_slicing_forward_pass(expected_max_diff=3E-3  )







       def 							__UpperCamelCase       (     self  ):
         super().test_inference_batch_single_identical(expected_max_diff=3E-3  )



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




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




       def 							__UpperCamelCase       (     self   ,   snake_case_   ,   snake_case_="cpu"   ,   snake_case_=torch.floataa   ,   snake_case_=0  ):
         _lowerCAmelCase       :  Dict   				=					torch.Generator(device=snake_case_  ).manual_seed(snake_case_  )
         _lowerCAmelCase       :  int   				=					np.random.RandomState(snake_case_  ).standard_normal((1, 4, 6_4, 6_4)  )
         _lowerCAmelCase       :  List[str]   				=					torch.from_numpy(snake_case_  ).to(device=snake_case_   ,   dtype=snake_case_  )
         _lowerCAmelCase       :  Any   				=					{
             """prompt""": """a photograph of an astronaut riding a horse""",
             """latents""": latents,
             """generator""": generator,
             """num_inference_steps""": 3,
             """guidance_scale""": 7.5,
             """output_type""": """numpy""",
         }
         return inputs







       def 							__UpperCamelCase       (     self  ):
         _lowerCAmelCase       :  str   				=					DiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-base"""  )
         pipe.to(snake_case_  )
         pipe.set_progress_bar_config(disable=snake_case_  )

         _lowerCAmelCase       :  List[str]   				=					self.get_inputs(snake_case_  )
         _lowerCAmelCase       :  str   				=					pipe(**snake_case_  ).images
         _lowerCAmelCase       :  List[str]   				=					image[0, -3:, -3:, -1].flatten()

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCAmelCase       :  Union[str, Any]   				=					np.array([0.4_9493, 0.4_7896, 0.4_0798, 0.5_4214, 0.5_3212, 0.4_8202, 0.4_7656, 0.4_6329, 0.4_8506]  )
         assert np.abs(image_slice - expected_slice  ).max() < 7E-3




'''simple docstring'''

import json
import os
import unittest

from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import (
    VOCAB_FILES_NAMES,
    GPTNeoXJapaneseTokenizer,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class   a_  (_a							,			unittest.TestCase    ):
       __lowerCAmelCase    :				List[Any]    =      GPTNeoXJapaneseTokenizer
       __lowerCAmelCase    :				Any    =      False
       __lowerCAmelCase    :				List[Any]    =      {"""do_clean_text""": False, """add_prefix_space""": False}




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

         _lowerCAmelCase       :  Any   				=					[
             """こん""",
             """こんに""",
             """にちは""",
             """ばんは""",
             """世界,㔺界""",
             """、""",
             """。""",
             """<BR>""",
             """<SP>""",
             """<TAB>""",
             """<URL>""",
             """<EMAIL>""",
             """<TEL>""",
             """<DATE>""",
             """<PRICE>""",
             """<BLOCK>""",
             """<KIGOU>""",
             """<U2000U2BFF>""",
             """<|emoji1|>""",
             """<unk>""",
             """<|startoftext|>""",
             """<|endoftext|>""",
         ]
         _lowerCAmelCase       :  Union[str, Any]   				=					{"""emoji""": {"""\ud83d\ude00""": """<|emoji1|>"""}, """emoji_inv""": {"""<|emoji1|>""": """\ud83d\ude00"""}}  # 😀
         _lowerCAmelCase       :  Any   				=					{"""unk_token""": """<unk>"""}

         _lowerCAmelCase       :  Optional[int]   				=					os.path.join(self.tmpdirname   ,   VOCAB_FILES_NAMES["""vocab_file"""]  )
         _lowerCAmelCase       :  Any   				=					os.path.join(self.tmpdirname   ,   VOCAB_FILES_NAMES["""emoji_file"""]  )
         with open(self.vocab_file   ,   """w"""   ,   encoding="""utf-8"""  ) as vocab_writer:
           vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]  )  )
         with open(self.emoji_file   ,   """w"""  ) as emoji_writer:
           emoji_writer.write(json.dumps(snake_case_  )  )




       def 							__UpperCamelCase       (     self   ,   **snake_case_  ):
         kwargs.update(self.special_tokens_map  )
         return GPTNeoXJapaneseTokenizer.from_pretrained(self.tmpdirname   ,   **snake_case_  )




       def 							__UpperCamelCase       (     self   ,   snake_case_  ):
         _lowerCAmelCase       :  Tuple   				=					"""こんにちは、世界。 \nこんばんは、㔺界。😀"""
         _lowerCAmelCase       :  str   				=					"""こんにちは、世界。 \nこんばんは、世界。😀"""
         return input_text, output_text




       def 							__UpperCamelCase       (     self   ,   snake_case_  ):
         _lowerCAmelCase				,					_lowerCAmelCase       :  str   				=					self.get_input_output_texts(snake_case_  )
         _lowerCAmelCase       :  Tuple   				=					tokenizer.encode(snake_case_   ,   add_special_tokens=snake_case_  )
         _lowerCAmelCase       :  Optional[Any]   				=					tokenizer.decode(snake_case_   ,   clean_up_tokenization_spaces=snake_case_  )
         return text, ids




       def 							__UpperCamelCase       (     self  ):
         pass  # TODO add if relevant




       def 							__UpperCamelCase       (     self  ):
         pass  # TODO add if relevant




       def 							__UpperCamelCase       (     self  ):
         pass  # TODO add if relevant




       def 							__UpperCamelCase       (     self  ):
         _lowerCAmelCase       :  List[Any]   				=					self.get_tokenizer()

         # Testing tokenization
         _lowerCAmelCase       :  Tuple   				=					"""こんにちは、世界。　こんばんは、㔺界。"""
         _lowerCAmelCase       :  Optional[Any]   				=					["""こん""", """にちは""", """、""", """世界""", """。""", """<SP>""", """こん""", """ばんは""", """、""", """㔺界""", """。"""]
         _lowerCAmelCase       :  Tuple   				=					tokenizer.tokenize(snake_case_  )
         self.assertListEqual(snake_case_   ,   snake_case_  )

         # Testing conversion to ids without special tokens
         _lowerCAmelCase       :  Optional[int]   				=					[0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6]
         _lowerCAmelCase       :  Optional[int]   				=					tokenizer.convert_tokens_to_ids(snake_case_  )
         self.assertListEqual(snake_case_   ,   snake_case_  )

         # Testing conversion to ids with special tokens
         _lowerCAmelCase       :  Optional[Any]   				=					tokens + [tokenizer.unk_token]
         _lowerCAmelCase       :  List[str]   				=					[0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 1_9]
         _lowerCAmelCase       :  Dict   				=					tokenizer.convert_tokens_to_ids(snake_case_  )
         self.assertListEqual(snake_case_   ,   snake_case_  )




       @slow
       def 							__UpperCamelCase       (     self  ):
         _lowerCAmelCase       :  int   				=					self.tokenizer_class.from_pretrained("""abeja/gpt-neox-japanese-2.7b"""  )

         _lowerCAmelCase       :  List[Any]   				=					tokenizer.encode("""ありがとう。"""   ,   add_special_tokens=snake_case_  )
         _lowerCAmelCase       :  List[str]   				=					tokenizer.encode("""どういたしまして。"""   ,   add_special_tokens=snake_case_  )

         _lowerCAmelCase       :  int   				=					tokenizer.build_inputs_with_special_tokens(snake_case_  )
         _lowerCAmelCase       :  Any   				=					tokenizer.build_inputs_with_special_tokens(snake_case_   ,   snake_case_  )

         assert encoded_sentence == ids_a
         assert encoded_pair == ids_a + ids_a




       def 							__UpperCamelCase       (     self  ):
         # Intentionally convert some words to accommodate character fluctuations unique to Japanese
         pass







       def 							__UpperCamelCase       (     self  ):
         # tokenizer has no padding token
         pass


