



'''simple docstring'''





import gc
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNetaDConditionModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_numpy,
    nightly,
    require_torch_gpu,
    slow,
    torch_device,
)

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


enable_full_determinism()







class     UpperCAmelCase_       (       _a		,		unittest.TestCase	):
    """simple docstring"""







    lowercase					=		LDMTextToImagePipeline
    lowercase					=		TEXT_TO_IMAGE_PARAMS - {
        "negative_prompt",
        "negative_prompt_embeds",
        "cross_attention_kwargs",
        "prompt_embeds",
    }
    lowercase					=		PipelineTesterMixin.required_optional_params - {
        "num_images_per_prompt",
        "callback",
        "callback_steps",
    }
    lowercase					=		TEXT_TO_IMAGE_BATCH_PARAMS
    lowercase					=		False




    def lowerCamelCase       (    self					:     List[Any]						):
          torch.manual_seed(0						)
          snake_case__  :    List[str]    						=      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 ,     )
          snake_case__  :    List[str]    						=      DDIMScheduler(
              beta_start=0.00085 ,     beta_end=0.012 ,     beta_schedule="""scaled_linear""" ,     clip_sample=snake_case_ ,     set_alpha_to_one=snake_case_ ,     )
          torch.manual_seed(0						)
          snake_case__  :    Optional[Any]    						=      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 ,     )
          torch.manual_seed(0						)
          snake_case__  :    Optional[int]    						=      CLIPTextConfig(
              bos_token_id=0 ,     eos_token_id=2 ,     hidden_size=32 ,     intermediate_size=37 ,     layer_norm_eps=1E-0_5 ,     num_attention_heads=4 ,     num_hidden_layers=5 ,     pad_token_id=1 ,     vocab_size=1_000 ,     )
          snake_case__  :    Optional[Any]    						=      CLIPTextModel(snake_case_						)
          snake_case__  :    List[Any]    						=      CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""						)

          snake_case__  :    Any    						=      {
              """unet""": unet,
              """scheduler""": scheduler,
              """vqvae""": vae,
              """bert""": text_encoder,
              """tokenizer""": tokenizer,
          }
          return components




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




    def lowerCamelCase       (    self					:     int						):
          snake_case__  :    Dict    						=      """cpu"""  # ensure determinism for the device-dependent torch.Generator

          snake_case__  :    List[Any]    						=      self.get_dummy_components()
          snake_case__  :    Any    						=      LDMTextToImagePipeline(**snake_case_						)
          pipe.to(snake_case_						)
          pipe.set_progress_bar_config(disable=snake_case_						)

          snake_case__  :    Tuple    						=      self.get_dummy_inputs(snake_case_						)
          snake_case__  :    Union[str, Any]    						=      pipe(**snake_case_						).images
          snake_case__  :    Dict    						=      image[0, -3:, -3:, -1]

          assert image.shape == (1, 16, 16, 3)
          snake_case__  :    Dict    						=      np.array([0.6101, 0.6156, 0.5622, 0.4895, 0.6661, 0.3804, 0.5748, 0.6136, 0.5014]						)

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







@slow
@require_torch_gpu
class     UpperCAmelCase_       (       unittest.TestCase	):
    """simple docstring"""







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




    def lowerCamelCase       (    self					:     Tuple ,     snake_case_					:     List[Any] ,     snake_case_					:     List[Any]=torch.floataa ,     snake_case_					:     Tuple=0						):
          snake_case__  :    Any    						=      torch.manual_seed(snake_case_						)
          snake_case__  :    str    						=      np.random.RandomState(snake_case_						).standard_normal((1, 4, 32, 32)						)
          snake_case__  :    int    						=      torch.from_numpy(snake_case_						).to(device=snake_case_ ,     dtype=snake_case_						)
          snake_case__  :    List[Any]    						=      {
              """prompt""": """A painting of a squirrel eating a burger""",
              """latents""": latents,
              """generator""": generator,
              """num_inference_steps""": 3,
              """guidance_scale""": 6.0,
              """output_type""": """numpy""",
          }
          return inputs




    def lowerCamelCase       (    self					:     Tuple						):
          snake_case__  :    Dict    						=      LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256"""						).to(snake_case_						)
          pipe.set_progress_bar_config(disable=snake_case_						)

          snake_case__  :    int    						=      self.get_inputs(snake_case_						)
          snake_case__  :    Optional[Any]    						=      pipe(**snake_case_						).images
          snake_case__  :    Tuple    						=      image[0, -3:, -3:, -1].flatten()

          assert image.shape == (1, 256, 256, 3)
          snake_case__  :    List[Any]    						=      np.array([0.51825, 0.52850, 0.52543, 0.54258, 0.52304, 0.52569, 0.54363, 0.55276, 0.56878]						)
          snake_case__  :    Any    						=      np.abs(expected_slice - image_slice						).max()
          assert max_diff < 1E-3







@nightly
@require_torch_gpu
class     UpperCAmelCase_       (       unittest.TestCase	):
    """simple docstring"""







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




    def lowerCamelCase       (    self					:     Union[str, Any] ,     snake_case_					:     Optional[int] ,     snake_case_					:     Tuple=torch.floataa ,     snake_case_					:     List[str]=0						):
          snake_case__  :    Tuple    						=      torch.manual_seed(snake_case_						)
          snake_case__  :    Union[str, Any]    						=      np.random.RandomState(snake_case_						).standard_normal((1, 4, 32, 32)						)
          snake_case__  :    List[str]    						=      torch.from_numpy(snake_case_						).to(device=snake_case_ ,     dtype=snake_case_						)
          snake_case__  :    Optional[int]    						=      {
              """prompt""": """A painting of a squirrel eating a burger""",
              """latents""": latents,
              """generator""": generator,
              """num_inference_steps""": 50,
              """guidance_scale""": 6.0,
              """output_type""": """numpy""",
          }
          return inputs




    def lowerCamelCase       (    self					:     List[Any]						):
          snake_case__  :    Any    						=      LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256"""						).to(snake_case_						)
          pipe.set_progress_bar_config(disable=snake_case_						)

          snake_case__  :    Tuple    						=      self.get_inputs(snake_case_						)
          snake_case__  :    Tuple    						=      pipe(**snake_case_						).images[0]

          snake_case__  :    int    						=      load_numpy(
              """https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy"""						)
          snake_case__  :    List[str]    						=      np.abs(expected_image - image						).max()
          assert max_diff < 1E-3





'''simple docstring'''





import json
import os
import unittest

from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import (
    VOCAB_FILES_NAMES,
    GPTSanJapaneseTokenizer,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin







@require_tokenizers
class     UpperCAmelCase_       (       _a		,		unittest.TestCase	):
    """simple docstring"""







    lowercase					=		GPTSanJapaneseTokenizer
    lowercase					=		False
    lowercase					=		{"do_clean_text": False, "add_prefix_space": False}




    def lowerCamelCase       (    self					:     str						):
          super().setUp()

          # fmt: off
          snake_case__  :    Optional[Any]    						=      ["""こん""", """こんに""", """にちは""", """ばんは""", """世界,㔺界""", """、""", """。""", """<BR>""", """<SP>""", """<TAB>""", """<URL>""", """<EMAIL>""", """<TEL>""", """<DATE>""", """<PRICE>""", """<BLOCK>""", """<KIGOU>""", """<U2000U2BFF>""", """<|emoji1|>""", """<unk>""", """<|bagoftoken|>""", """<|endoftext|>"""]
          # fmt: on
          snake_case__  :    int    						=      {"""emoji""": {"""\ud83d\ude00""": """<|emoji1|>"""}, """emoji_inv""": {"""<|emoji1|>""": """\ud83d\ude00"""}}  # 😀
          snake_case__  :    List[Any]    						=      {"""unk_token""": """<unk>"""}

          snake_case__  :    Optional[Any]    						=      os.path.join(self.tmpdirname ,     VOCAB_FILES_NAMES["""vocab_file"""]						)
          snake_case__  :    Dict    						=      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 lowerCamelCase       (    self					:     Any ,     **snake_case_					:     Union[str, Any]						):
          kwargs.update(self.special_tokens_map						)
          return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname ,     **snake_case_						)




    def lowerCamelCase       (    self					:     Any ,     snake_case_					:     str						):
          snake_case__  :    Union[str, Any]    						=      """こんにちは、世界。 \nこんばんは、㔺界。😀"""
          snake_case__  :    List[str]    						=      """こんにちは、世界。 \nこんばんは、世界。😀"""
          return input_text, output_text




    def lowerCamelCase       (    self					:     Any ,     snake_case_					:     Dict						):
          snake_case__						,				snake_case__  :    int    						=      self.get_input_output_texts(snake_case_						)
          snake_case__  :    int    						=      tokenizer.encode(snake_case_ ,     add_special_tokens=snake_case_						)
          snake_case__  :    List[str]    						=      tokenizer.decode(snake_case_ ,     clean_up_tokenization_spaces=snake_case_						)
          return text, ids




    def lowerCamelCase       (    self					:     Optional[Any]						):
          pass  # TODO add if relevant




    def lowerCamelCase       (    self					:     Union[str, Any]						):
          pass  # TODO add if relevant




    def lowerCamelCase       (    self					:     List[str]						):
          pass  # TODO add if relevant




    def lowerCamelCase       (    self					:     Dict						):
          snake_case__  :    Optional[Any]    						=      self.get_tokenizer()

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

          # Testing conversion to ids without special tokens
          snake_case__  :    Union[str, Any]    						=      [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6]
          snake_case__  :    List[Any]    						=      tokenizer.convert_tokens_to_ids(snake_case_						)
          self.assertListEqual(snake_case_ ,     snake_case_						)

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




    def lowerCamelCase       (    self					:     Optional[Any]						):
          snake_case__  :    Union[str, Any]    						=      self.get_tokenizer()

          # Testing tokenization
          snake_case__  :    Union[str, Any]    						=      """こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。"""
          snake_case__  :    Optional[int]    						=      """こんにちは、、、、世界。こんばんは、、、、世界。"""
          snake_case__  :    Any    						=      tokenizer.encode(snake_case_						)
          snake_case__  :    int    						=      tokenizer.decode(snake_case_						)
          self.assertEqual(snake_case_ ,     snake_case_						)




    @slow
    def lowerCamelCase       (    self					:     Union[str, Any]						):
          snake_case__  :    Optional[Any]    						=      self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""						)

          # Testing tokenization
          snake_case__  :    Tuple    						=      """こんにちは、世界。"""
          snake_case__  :    Optional[Any]    						=      """こんばんは、㔺界。😀"""
          snake_case__  :    List[str]    						=      """こんにちは、世界。こんばんは、世界。😀"""
          snake_case__  :    Dict    						=      tokenizer.encode(prefix_text + input_text						)
          snake_case__  :    Dict    						=      tokenizer.encode("""""" ,     prefix_text=prefix_text + input_text						)
          snake_case__  :    int    						=      tokenizer.encode(snake_case_ ,     prefix_text=snake_case_						)
          snake_case__  :    Optional[Any]    						=      tokenizer.decode(snake_case_						)
          snake_case__  :    Union[str, Any]    						=      tokenizer.decode(snake_case_						)
          snake_case__  :    str    						=      tokenizer.decode(snake_case_						)
          self.assertEqual(snake_case_ ,     snake_case_						)
          self.assertEqual(snake_case_ ,     snake_case_						)
          self.assertEqual(snake_case_ ,     snake_case_						)




    @slow
    def lowerCamelCase       (    self					:     Union[str, Any]						):
          snake_case__  :    Optional[Any]    						=      self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""						)

          # Testing tokenization
          snake_case__  :    Dict    						=      """こんにちは、世界。"""
          snake_case__  :    Optional[int]    						=      """こんばんは、㔺界。😀"""

          snake_case__  :    Any    						=      len(tokenizer.encode(snake_case_						)						) - 2
          snake_case__  :    Optional[int]    						=      len(tokenizer.encode(snake_case_						)						) - 2

          snake_case__  :    List[str]    						=      [1] + [0] * (len_prefix + len_text + 1)
          snake_case__  :    Optional[int]    						=      [1] * (len_prefix + len_text + 1) + [0]
          snake_case__  :    int    						=      [1] + [1] * (len_prefix) + [0] * (len_text + 1)

          snake_case__  :    Any    						=      tokenizer(prefix_text + input_text						).token_type_ids
          snake_case__  :    str    						=      tokenizer("""""" ,     prefix_text=prefix_text + input_text						).token_type_ids
          snake_case__  :    Optional[Any]    						=      tokenizer(snake_case_ ,     prefix_text=snake_case_						).token_type_ids
          self.assertListEqual(snake_case_ ,     snake_case_						)
          self.assertListEqual(snake_case_ ,     snake_case_						)
          self.assertListEqual(snake_case_ ,     snake_case_						)




    @slow
    def lowerCamelCase       (    self					:     Optional[int]						):
          snake_case__  :    Optional[Any]    						=      self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""						)

          snake_case__  :    Union[str, Any]    						=      tokenizer.encode("""あンいワ"""						)
          snake_case__  :    int    						=      tokenizer.encode("""""" ,     prefix_text="""あンいワ"""						)
          snake_case__  :    Dict    						=      tokenizer.encode("""いワ""" ,     prefix_text="""あン"""						)

          self.assertEqual(tokenizer.decode(snake_case_						) ,     tokenizer.decode(snake_case_						)						)
          self.assertEqual(tokenizer.decode(snake_case_						) ,     tokenizer.decode(snake_case_						)						)
          self.assertNotEqual(snake_case_ ,     snake_case_						)
          self.assertNotEqual(snake_case_ ,     snake_case_						)
          self.assertEqual(x_token_a[1] ,     x_token_a[-1]						)  # SEG token
          self.assertEqual(x_token_a[1] ,     x_token_a[3]						)  # SEG token




    @slow
    def lowerCamelCase       (    self					:     Any						):
          snake_case__  :    Optional[int]    						=      self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""						)

          snake_case__  :    int    						=      [["""武田信玄""", """は、"""], ["""織田信長""", """の配下の、"""]]
          snake_case__  :    Optional[Any]    						=      tokenizer(snake_case_ ,     padding=snake_case_						)
          snake_case__  :    Tuple    						=      tokenizer.batch_encode_plus(snake_case_ ,     padding=snake_case_						)

          # fmt: off
          snake_case__  :    Optional[Any]    						=      [[35_993, 8_640, 25_948, 35_998, 30_647, 35_675, 35_999, 35_999], [35_993, 10_382, 9_868, 35_998, 30_646, 9_459, 30_646, 35_675]]
          snake_case__  :    Optional[Any]    						=      [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]]
          snake_case__  :    Optional[Any]    						=      [[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]]
          # fmt: on
          self.assertListEqual(x_token.input_ids ,     snake_case_						)
          self.assertListEqual(x_token.token_type_ids ,     snake_case_						)
          self.assertListEqual(x_token.attention_mask ,     snake_case_						)
          self.assertListEqual(x_token_a.input_ids ,     snake_case_						)
          self.assertListEqual(x_token_a.token_type_ids ,     snake_case_						)
          self.assertListEqual(x_token_a.attention_mask ,     snake_case_						)




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




    def lowerCamelCase       (    self					:     List[str]						):
          # tokenizer has no padding token
          pass
