import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizerFast

from diffusers import DDIMScheduler, KandinskyPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP
from diffusers.utils import floats_tensor, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()
class SCREAMING_SNAKE_CASE_		(			__lowerCAmelCase     ,    unittest.TestCase		):
   __lowerCAmelCase											=							KandinskyPipeline
   __lowerCAmelCase											=							[
       """prompt""",
       """image_embeds""",
       """negative_image_embeds""",
   ]
   __lowerCAmelCase											=							["""prompt""", """negative_prompt""", """image_embeds""", """negative_image_embeds"""]
   __lowerCAmelCase											=							[
       """generator""",
       """height""",
       """width""",
       """latents""",
       """guidance_scale""",
       """negative_prompt""",
       """num_inference_steps""",
       """return_dict""",
       """guidance_scale""",
       """num_images_per_prompt""",
       """output_type""",
       """return_dict""",
   ]
   __lowerCAmelCase											=							False







   @property
   def 	lowerCamelCase_  (    self	:	Optional[int]							):





    """simple docstring"""


    return 32







   @property
   def 	lowerCamelCase_  (    self	:	Optional[int]							):





    """simple docstring"""


    return 32







   @property
   def 	lowerCamelCase_  (    self	:	Any							):





    """simple docstring"""


    return self.time_input_dim







   @property
   def 	lowerCamelCase_  (    self	:	Dict							):





    """simple docstring"""


    return self.time_input_dim * 4







   @property
   def 	lowerCamelCase_  (    self	:	int							):





    """simple docstring"""


    return 100







   @property
   def 	lowerCamelCase_  (    self	:	Optional[int]							):





    """simple docstring"""


    UpperCamelCase      =			XLMRobertaTokenizerFast.from_pretrained("""YiYiXu/tiny-random-mclip-base"""							)
    return tokenizer







   @property
   def 	lowerCamelCase_  (    self	:	Dict							):





    """simple docstring"""


    torch.manual_seed(0							)
    UpperCamelCase      =			MCLIPConfig(
        numDims=self.cross_attention_dim      ,      transformerDimensions=self.text_embedder_hidden_size      ,      hidden_size=self.text_embedder_hidden_size      ,      intermediate_size=37      ,      num_attention_heads=4      ,      num_hidden_layers=5      ,      vocab_size=1005      ,      )

    UpperCamelCase      =			MultilingualCLIP(lowerCamelCase_							)
    UpperCamelCase      =			text_encoder.eval()

    return text_encoder







   @property
   def 	lowerCamelCase_  (    self	:	int							):





    """simple docstring"""


    torch.manual_seed(0							)

    UpperCamelCase      =			{
        """in_channels""": 4,
        # Out channels is double in channels because predicts mean and variance
        """out_channels""": 8,
        """addition_embed_type""": """text_image""",
        """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""),
        """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""),
        """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""",
        """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2),
        """layers_per_block""": 1,
        """encoder_hid_dim""": self.text_embedder_hidden_size,
        """encoder_hid_dim_type""": """text_image_proj""",
        """cross_attention_dim""": self.cross_attention_dim,
        """attention_head_dim""": 4,
        """resnet_time_scale_shift""": """scale_shift""",
        """class_embed_type""": None,
    }

    UpperCamelCase      =			UNetaDConditionModel(**lowerCamelCase_							)
    return model







   @property
   def 	lowerCamelCase_  (    self	:	List[str]							):





    """simple docstring"""


    return {
        "block_out_channels": [32, 64],
        "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
        "in_channels": 3,
        "latent_channels": 4,
        "layers_per_block": 1,
        "norm_num_groups": 8,
        "norm_type": "spatial",
        "num_vq_embeddings": 12,
        "out_channels": 3,
        "up_block_types": [
            "AttnUpDecoderBlock2D",
            "UpDecoderBlock2D",
        ],
        "vq_embed_dim": 4,
    }







   @property
   def 	lowerCamelCase_  (    self	:	Any							):





    """simple docstring"""


    torch.manual_seed(0							)
    UpperCamelCase      =			VQModel(**self.dummy_movq_kwargs							)
    return model







   def 	lowerCamelCase_  (    self	:	Optional[int]							):





    """simple docstring"""


    UpperCamelCase      =			self.dummy_text_encoder
    UpperCamelCase      =			self.dummy_tokenizer
    UpperCamelCase      =			self.dummy_unet
    UpperCamelCase      =			self.dummy_movq

    UpperCamelCase      =			DDIMScheduler(
        num_train_timesteps=1000      ,      beta_schedule="""linear"""      ,      beta_start=0.0_0_0_8_5      ,      beta_end=0.0_1_2      ,      clip_sample=lowerCamelCase_      ,      set_alpha_to_one=lowerCamelCase_      ,      steps_offset=1      ,      prediction_type="""epsilon"""      ,      thresholding=lowerCamelCase_      ,      )

    UpperCamelCase      =			{
        """text_encoder""": text_encoder,
        """tokenizer""": tokenizer,
        """unet""": unet,
        """scheduler""": scheduler,
        """movq""": movq,
    }
    return components







   def 	lowerCamelCase_  (    self	:	Union[str, Any]      ,      lowerCamelCase_	:	Optional[int]      ,      lowerCamelCase_	:	str=0							):





    """simple docstring"""


    UpperCamelCase      =			floats_tensor((1, self.cross_attention_dim)      ,      rng=random.Random(lowerCamelCase_							)							).to(lowerCamelCase_							)
    UpperCamelCase      =			floats_tensor((1, self.cross_attention_dim)      ,      rng=random.Random(seed + 1							)							).to(lowerCamelCase_							)
    if str(lowerCamelCase_							).startswith("""mps"""							):
     UpperCamelCase      =			torch.manual_seed(lowerCamelCase_							)
    else:
     UpperCamelCase      =			torch.Generator(device=lowerCamelCase_							).manual_seed(lowerCamelCase_							)
    UpperCamelCase      =			{
        """prompt""": """horse""",
        """image_embeds""": image_embeds,
        """negative_image_embeds""": negative_image_embeds,
        """generator""": generator,
        """height""": 64,
        """width""": 64,
        """guidance_scale""": 4.0,
        """num_inference_steps""": 2,
        """output_type""": """np""",
    }
    return inputs







   def 	lowerCamelCase_  (    self	:	Optional[int]							):





    """simple docstring"""


    UpperCamelCase      =			"""cpu"""

    UpperCamelCase      =			self.get_dummy_components()

    UpperCamelCase      =			self.pipeline_class(**lowerCamelCase_							)
    UpperCamelCase      =			pipe.to(lowerCamelCase_							)

    pipe.set_progress_bar_config(disable=lowerCamelCase_							)

    UpperCamelCase      =			pipe(**self.get_dummy_inputs(lowerCamelCase_							)							)
    UpperCamelCase      =			output.images

    UpperCamelCase      =			pipe(
        **self.get_dummy_inputs(lowerCamelCase_							)      ,      return_dict=lowerCamelCase_      ,      )[0]

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

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

    UpperCamelCase      =			np.array(
        [0.3_2_8_6_6_3, 1.0, 0.2_3_2_1_6_8_7_3, 1.0, 0.9_2_7_1_7_5_6_4, 0.4_6_3_9_0_4_6, 0.9_6_8_9_4_7_7_7, 0.3_1_7_1_3_3_7_8, 0.6_2_9_3_9_5_3]							)

    assert (
        np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2
    ), f""" expected_slice {expected_slice}, but got {image_slice.flatten()}"""
    assert (
        np.abs(image_from_tuple_slice.flatten() - expected_slice							).max() < 1E-2
    ), f""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"""





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







   def 	lowerCamelCase_  (    self	:	Tuple							):





    """simple docstring"""


    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()







   def 	lowerCamelCase_  (    self	:	Dict							):





    """simple docstring"""


    UpperCamelCase      =			load_numpy(
        """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
        """/kandinsky/kandinsky_text2img_cat_fp16.npy"""							)

    UpperCamelCase      =			KandinskyPriorPipeline.from_pretrained(
        """kandinsky-community/kandinsky-2-1-prior"""      ,      torch_dtype=torch.floataa							)
    pipe_prior.to(lowerCamelCase_							)

    UpperCamelCase      =			KandinskyPipeline.from_pretrained("""kandinsky-community/kandinsky-2-1"""      ,      torch_dtype=torch.floataa							)
    UpperCamelCase      =			pipeline.to(lowerCamelCase_							)
    pipeline.set_progress_bar_config(disable=lowerCamelCase_							)

    UpperCamelCase      =			"""red cat, 4k photo"""

    UpperCamelCase      =			torch.Generator(device="""cuda"""							).manual_seed(0							)
    UpperCamelCase     ,    UpperCamelCase      =			pipe_prior(
        lowerCamelCase_      ,      generator=lowerCamelCase_      ,      num_inference_steps=5      ,      negative_prompt=""""""      ,      ).to_tuple()

    UpperCamelCase      =			torch.Generator(device="""cuda"""							).manual_seed(0							)
    UpperCamelCase      =			pipeline(
        lowerCamelCase_      ,      image_embeds=lowerCamelCase_      ,      negative_image_embeds=lowerCamelCase_      ,      generator=lowerCamelCase_      ,      num_inference_steps=100      ,      output_type="""np"""      ,      )

    UpperCamelCase      =			output.images[0]

    assert image.shape == (512, 512, 3)

    assert_mean_pixel_difference(lowerCamelCase_      ,      lowerCamelCase_							)

import unittest

from transformers.testing_utils import is_torch_available, require_torch, tooslow

from ...generation.test_utils import torch_device
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         CpmAntConfig,
         CpmAntForCausalLM,
         CpmAntModel,
         CpmAntTokenizer,
     )
@require_torch
class SCREAMING_SNAKE_CASE_		:







   def __init__(    self	:	Optional[int]      ,      lowerCamelCase_	:	List[str]      ,      lowerCamelCase_	:	Tuple=2      ,      lowerCamelCase_	:	List[Any]=8      ,      lowerCamelCase_	:	Any=True      ,      lowerCamelCase_	:	int=False      ,      lowerCamelCase_	:	Optional[Any]=False      ,      lowerCamelCase_	:	Optional[int]=False      ,      lowerCamelCase_	:	Any=False      ,      lowerCamelCase_	:	List[Any]=99      ,      lowerCamelCase_	:	Optional[int]=32      ,      lowerCamelCase_	:	List[str]=3      ,      lowerCamelCase_	:	List[str]=4      ,      lowerCamelCase_	:	Tuple=37      ,      lowerCamelCase_	:	str=32      ,      lowerCamelCase_	:	Any=128      ,      lowerCamelCase_	:	List[str]=8      ,      lowerCamelCase_	:	Union[str, Any]=8      ,      lowerCamelCase_	:	Any=8      ,      lowerCamelCase_	:	Optional[int]=1.0      ,      lowerCamelCase_	:	int=True      ,      ):





    """simple docstring"""


    UpperCamelCase      =			parent
    UpperCamelCase      =			batch_size
    UpperCamelCase      =			seq_length
    UpperCamelCase      =			is_training
    UpperCamelCase      =			use_token_type_ids
    UpperCamelCase      =			use_input_mask
    UpperCamelCase      =			use_labels
    UpperCamelCase      =			use_mc_token_ids
    UpperCamelCase      =			vocab_size
    UpperCamelCase      =			hidden_size
    UpperCamelCase      =			num_hidden_layers
    UpperCamelCase      =			num_attention_heads
    UpperCamelCase      =			intermediate_size
    UpperCamelCase      =			num_buckets
    UpperCamelCase      =			max_distance
    UpperCamelCase      =			prompt_length
    UpperCamelCase      =			prompt_types
    UpperCamelCase      =			segment_types
    UpperCamelCase      =			init_std
    UpperCamelCase      =			return_dict







   def 	lowerCamelCase_  (    self	:	List[str]							):





    """simple docstring"""


    UpperCamelCase      =			{}
    UpperCamelCase      =			ids_tensor([self.batch_size, self.seq_length]      ,      self.vocab_size							).type(torch.intaa							)
    UpperCamelCase      =			False

    UpperCamelCase      =			self.get_config()

    return (config, input_ids)







   def 	lowerCamelCase_  (    self	:	Optional[Any]							):





    """simple docstring"""


    return CpmAntConfig(
        vocab_size=self.vocab_size      ,      hidden_size=self.hidden_size      ,      num_hidden_layers=self.num_hidden_layers      ,      num_attention_heads=self.num_attention_heads      ,      dim_ff=self.intermediate_size      ,      position_bias_num_buckets=self.num_buckets      ,      position_bias_max_distance=self.max_distance      ,      prompt_types=self.prompt_types      ,      prompt_length=self.prompt_length      ,      segment_types=self.segment_types      ,      use_cache=lowerCamelCase_      ,      init_std=self.init_std      ,      return_dict=self.return_dict      ,      )







   def 	lowerCamelCase_  (    self	:	Optional[int]      ,      lowerCamelCase_	:	List[str]      ,      lowerCamelCase_	:	List[str]      ,      *lowerCamelCase_	:	str							):





    """simple docstring"""


    UpperCamelCase      =			CpmAntModel(config=lowerCamelCase_							)
    model.to(lowerCamelCase_							)
    model.eval()

    UpperCamelCase      =			model(**lowerCamelCase_							).last_hidden_state

    self.parent.assertEqual(hidden_states.shape      ,      (self.batch_size, self.seq_length, config.hidden_size)							)







   def 	lowerCamelCase_  (    self	:	Any      ,      lowerCamelCase_	:	Dict      ,      lowerCamelCase_	:	Optional[Any]      ,      *lowerCamelCase_	:	str							):





    """simple docstring"""


    UpperCamelCase      =			CpmAntForCausalLM(lowerCamelCase_							)
    model.to(lowerCamelCase_							)
    UpperCamelCase      =			input_ids["""input_ids"""].to(lowerCamelCase_							)
    model.eval()

    UpperCamelCase      =			model(**lowerCamelCase_							)
    self.parent.assertEqual(
        model_output.logits.shape      ,      (self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)      ,      )







   def 	lowerCamelCase_  (    self	:	str							):





    """simple docstring"""


    UpperCamelCase     ,    UpperCamelCase      =			self.prepare_config_and_inputs()
    return config, inputs_dict





@require_torch
class SCREAMING_SNAKE_CASE_		(			__lowerCAmelCase     ,    __lowerCAmelCase     ,    unittest.TestCase		):
   __lowerCAmelCase											=							(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
   __lowerCAmelCase											=							(
       {"""feature-extraction""": CpmAntModel, """text-generation""": CpmAntForCausalLM} if is_torch_available() else {}
   )

   __lowerCAmelCase											=							False
   __lowerCAmelCase											=							False
   __lowerCAmelCase											=							False
   __lowerCAmelCase											=							False
   __lowerCAmelCase											=							False







   def 	lowerCamelCase_  (    self	:	List[Any]							):





    """simple docstring"""


    UpperCamelCase      =			CpmAntModelTester(self							)
    UpperCamelCase      =			ConfigTester(self      ,      config_class=lowerCamelCase_							)







   def 	lowerCamelCase_  (    self	:	str							):





    """simple docstring"""


    self.config_tester.create_and_test_config_common_properties()
    self.config_tester.create_and_test_config_to_json_string()
    self.config_tester.create_and_test_config_to_json_file()
    self.config_tester.create_and_test_config_from_and_save_pretrained()
    self.config_tester.check_config_can_be_init_without_params()
    self.config_tester.check_config_arguments_init()







   def 	lowerCamelCase_  (    self	:	int							):





    """simple docstring"""


    unittest.skip("""CPMAnt doesn't support input_embeds."""							)(self.test_inputs_embeds							)







   def 	lowerCamelCase_  (    self	:	Tuple							):





    """simple docstring"""


    unittest.skip(
        """CPMAnt doesn't support retain grad in hidden_states or attentions, because prompt management will peel off the output.hidden_states from graph.\
                 So is attentions. We strongly recommand you use loss to tune model."""							)(self.test_retain_grad_hidden_states_attentions							)







   def 	lowerCamelCase_  (    self	:	Optional[int]							):





    """simple docstring"""


    UpperCamelCase     ,    UpperCamelCase      =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_cpmant_model(lowerCamelCase_      ,      lowerCamelCase_							)







   def 	lowerCamelCase_  (    self	:	str							):





    """simple docstring"""


    UpperCamelCase     ,    UpperCamelCase      =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_lm_head_model(lowerCamelCase_      ,      lowerCamelCase_							)





@require_torch
class SCREAMING_SNAKE_CASE_		(			unittest.TestCase		):







   @tooslow
   def 	lowerCamelCase_  (    self	:	List[str]							):





    """simple docstring"""


    UpperCamelCase      =			"""今天天气真好！"""
    UpperCamelCase      =			"""openbmb/cpm-ant-10b"""
    UpperCamelCase      =			CpmAntModel.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			tokenizer(lowerCamelCase_      ,      return_tensors="""pt"""							)
    UpperCamelCase      =			model(**lowerCamelCase_							).last_hidden_state

    UpperCamelCase      =			torch.tensor(
        [[[6.1_7_0_8, 5.9_2_4_4, 1.0_8_3_5], [6.5_2_0_7, 6.2_8_9_3, -1_1.3_3_2_4], [-1.0_1_0_7, -0.0_5_7_6, -5.9_5_7_7]]]      ,      )
    self.assertTrue(torch.allclose(hidden_states[:, :3, :3]      ,      lowerCamelCase_      ,      atol=1E-2							)							)





@require_torch
class SCREAMING_SNAKE_CASE_		(			unittest.TestCase		):







   @tooslow
   def 	lowerCamelCase_  (    self	:	str							):





    """simple docstring"""


    UpperCamelCase      =			"""今天天气真好！"""
    UpperCamelCase      =			"""openbmb/cpm-ant-10b"""
    UpperCamelCase      =			CpmAntForCausalLM.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			tokenizer(lowerCamelCase_      ,      return_tensors="""pt"""							)
    UpperCamelCase      =			model(**lowerCamelCase_							).logits

    UpperCamelCase      =			torch.tensor(
        [[[-6.4_2_6_7, -6.4_0_8_3, -6.3_9_5_8], [-5.8_8_0_2, -5.9_4_4_7, -5.7_8_1_1], [-5.3_8_9_6, -5.4_8_2_0, -5.4_2_9_5]]]      ,      )
    self.assertTrue(torch.allclose(hidden_states[:, :3, :3]      ,      lowerCamelCase_      ,      atol=1E-2							)							)







   @tooslow
   def 	lowerCamelCase_  (    self	:	Dict							):





    """simple docstring"""


    UpperCamelCase      =			"""openbmb/cpm-ant-10b"""
    UpperCamelCase      =			CpmAntForCausalLM.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			"""今天天气不错，"""
    UpperCamelCase      =			"""今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的"""
    UpperCamelCase      =			tokenizer(lowerCamelCase_      ,      return_tensors="""pt"""							)
    UpperCamelCase      =			model.generate(**lowerCamelCase_							)
    UpperCamelCase      =			tokenizer.batch_decode(lowerCamelCase_							)
    self.assertEqual(lowerCamelCase_      ,      lowerCamelCase_							)







   @tooslow
   def 	lowerCamelCase_  (    self	:	int							):





    """simple docstring"""


    UpperCamelCase      =			"""openbmb/cpm-ant-10b"""
    UpperCamelCase      =			CpmAntForCausalLM.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			["""今天天气不错，""", """新年快乐，万事如意！"""]
    UpperCamelCase      =			[
        """今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的""",
        """新年快乐，万事如意！在这辞旧迎新的美好时刻，我谨代表《农村新技术》杂志社全体同仁，向一直以来关心、支持《农村新技术》杂志发展的各级领导、各界朋友和广大读者致以最诚挚的""",
    ]
    UpperCamelCase      =			tokenizer(lowerCamelCase_      ,      return_tensors="""pt"""      ,      padding=lowerCamelCase_							)
    UpperCamelCase      =			model.generate(**lowerCamelCase_							)
    UpperCamelCase      =			tokenizer.batch_decode(lowerCamelCase_							)
    self.assertEqual(lowerCamelCase_      ,      lowerCamelCase_							)
