

import gc
import random
import tempfile
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.semantic_stable_diffusion import SemanticStableDiffusionPipeline as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()

class  a     (		unittest.TestCase ):


						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


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


						@property
						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


										_UpperCAmelCase				:						Dict       						=			1
										_UpperCAmelCase				:						Union[str, Any]       						=			3
										_UpperCAmelCase				:						Any       						=			(32, 32)

										_UpperCAmelCase				:						List[str]       						=			floats_tensor((batch_size, num_channels) + sizes  ,    rng=random.Random(0		)		).to(A_		)
										return image


						@property
						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


										torch.manual_seed(0		)
										_UpperCAmelCase				:						Dict       						=			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		):







										'''simple docstring'''


										torch.manual_seed(0		)
										_UpperCAmelCase				:						Optional[int]       						=			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		):







										'''simple docstring'''


										torch.manual_seed(0		)
										_UpperCAmelCase				:						Tuple       						=			CLIPTextConfig(
										    bos_token_id=0  ,    eos_token_id=2  ,    hidden_size=32  ,    intermediate_size=37  ,    layer_norm_eps=1e-05  ,    num_attention_heads=4  ,    num_hidden_layers=5  ,    pad_token_id=1  ,    vocab_size=1000  ,    )
										return CLIPTextModel(A_		)


						@property
						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


										def extract(*A_  ,    **A_		):

														class  a     :


																				def __init__(				self		):







																								'''simple docstring'''


																								_UpperCAmelCase				:						Dict       						=			torch.ones([0]		)





																				def 				_UpperCAmelCase (				self  ,    A_		):







																								'''simple docstring'''


																								self.pixel_values.to(A_		)
																								return self







														return Out()

										return extract


						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


										_UpperCAmelCase				:						Dict       						=			"cpu"  # ensure determinism for the device-dependent torch.Generator
										_UpperCAmelCase				:						int       						=			self.dummy_cond_unet
										_UpperCAmelCase				:						str       						=			DDIMScheduler(
										    beta_start=0.0_00_85  ,    beta_end=0.0_12  ,    beta_schedule="scaled_linear"  ,    clip_sample=A_  ,    set_alpha_to_one=A_  ,    )

										_UpperCAmelCase				:						int       						=			self.dummy_vae
										_UpperCAmelCase				:						Optional[int]       						=			self.dummy_text_encoder
										_UpperCAmelCase				:						int       						=			CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"		)

										# make sure here that pndm scheduler skips prk
										_UpperCAmelCase				:						Tuple       						=			StableDiffusionPipeline(
										    unet=A_  ,    scheduler=A_  ,    vae=A_  ,    text_encoder=A_  ,    tokenizer=A_  ,    safety_checker=A_  ,    feature_extractor=self.dummy_extractor  ,    )
										_UpperCAmelCase				:						str       						=			sd_pipe.to(A_		)
										sd_pipe.set_progress_bar_config(disable=A_		)

										_UpperCAmelCase				:						Dict       						=			"A painting of a squirrel eating a burger"

										_UpperCAmelCase				:						Optional[int]       						=			torch.Generator(device=A_		).manual_seed(0		)
										_UpperCAmelCase				:						List[str]       						=			sd_pipe([prompt]  ,    generator=A_  ,    guidance_scale=6.0  ,    num_inference_steps=2  ,    output_type="np"		)
										_UpperCAmelCase				:						Any       						=			output.images

										_UpperCAmelCase				:						Any       						=			torch.Generator(device=A_		).manual_seed(0		)
										_UpperCAmelCase				:						int       						=			sd_pipe(
										    [prompt]  ,    generator=A_  ,    guidance_scale=6.0  ,    num_inference_steps=2  ,    output_type="np"  ,    return_dict=A_  ,    )[0]

										_UpperCAmelCase				:						Dict       						=			image[0, -3:, -3:, -1]
										_UpperCAmelCase				:						Union[str, Any]       						=			image_from_tuple[0, -3:, -3:, -1]

										assert image.shape == (1, 64, 64, 3)
										_UpperCAmelCase				:						List[str]       						=			np.array([0.57_53, 0.61_14, 0.50_01, 0.50_34, 0.54_70, 0.47_29, 0.49_71, 0.48_67, 0.48_67]		)

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


						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


										_UpperCAmelCase				:						Optional[int]       						=			"cpu"  # ensure determinism for the device-dependent torch.Generator
										_UpperCAmelCase				:						Union[str, Any]       						=			self.dummy_cond_unet
										_UpperCAmelCase				:						Dict       						=			PNDMScheduler(skip_prk_steps=A_		)
										_UpperCAmelCase				:						List[Any]       						=			self.dummy_vae
										_UpperCAmelCase				:						List[Any]       						=			self.dummy_text_encoder
										_UpperCAmelCase				:						Optional[int]       						=			CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"		)

										# make sure here that pndm scheduler skips prk
										_UpperCAmelCase				:						Dict       						=			StableDiffusionPipeline(
										    unet=A_  ,    scheduler=A_  ,    vae=A_  ,    text_encoder=A_  ,    tokenizer=A_  ,    safety_checker=A_  ,    feature_extractor=self.dummy_extractor  ,    )
										_UpperCAmelCase				:						str       						=			sd_pipe.to(A_		)
										sd_pipe.set_progress_bar_config(disable=A_		)

										_UpperCAmelCase				:						int       						=			"A painting of a squirrel eating a burger"
										_UpperCAmelCase				:						Union[str, Any]       						=			torch.Generator(device=A_		).manual_seed(0		)
										_UpperCAmelCase				:						Dict       						=			sd_pipe([prompt]  ,    generator=A_  ,    guidance_scale=6.0  ,    num_inference_steps=2  ,    output_type="np"		)

										_UpperCAmelCase				:						str       						=			output.images

										_UpperCAmelCase				:						List[Any]       						=			torch.Generator(device=A_		).manual_seed(0		)
										_UpperCAmelCase				:						Union[str, Any]       						=			sd_pipe(
										    [prompt]  ,    generator=A_  ,    guidance_scale=6.0  ,    num_inference_steps=2  ,    output_type="np"  ,    return_dict=A_  ,    )[0]

										_UpperCAmelCase				:						int       						=			image[0, -3:, -3:, -1]
										_UpperCAmelCase				:						Union[str, Any]       						=			image_from_tuple[0, -3:, -3:, -1]

										assert image.shape == (1, 64, 64, 3)
										_UpperCAmelCase				:						Optional[int]       						=			np.array([0.51_22, 0.57_12, 0.48_25, 0.50_53, 0.56_46, 0.47_69, 0.51_79, 0.48_94, 0.49_94]		)

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


						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


										_UpperCAmelCase				:						List[str]       						=			StableDiffusionPipeline.from_pretrained(
										    "hf-internal-testing/tiny-stable-diffusion-lms-pipe"  ,    safety_checker=A_		)
										assert isinstance(A_  ,    A_		)
										assert isinstance(pipe.scheduler  ,    A_		)
										assert pipe.safety_checker is None

										_UpperCAmelCase				:						Optional[int]       						=			pipe("example prompt"  ,    num_inference_steps=2		).images[0]
										assert image is not None

										# check that there's no error when saving a pipeline with one of the models being None
										with tempfile.TemporaryDirectory() as tmpdirname:
														pipe.save_pretrained(A_		)
														_UpperCAmelCase				:						str       						=			StableDiffusionPipeline.from_pretrained(A_		)

										# sanity check that the pipeline still works
										assert pipe.safety_checker is None
										_UpperCAmelCase				:						List[Any]       						=			pipe("example prompt"  ,    num_inference_steps=2		).images[0]
										assert image is not None





						@unittest.skipIf(torch_device != "cuda"  ,    "This test requires a GPU"		)
						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


										_UpperCAmelCase				:						Optional[int]       						=			self.dummy_cond_unet
										_UpperCAmelCase				:						Any       						=			PNDMScheduler(skip_prk_steps=A_		)
										_UpperCAmelCase				:						Any       						=			self.dummy_vae
										_UpperCAmelCase				:						Any       						=			self.dummy_text_encoder
										_UpperCAmelCase				:						Union[str, Any]       						=			CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"		)

										# put models in fp16
										_UpperCAmelCase				:						int       						=			unet.half()
										_UpperCAmelCase				:						str       						=			vae.half()
										_UpperCAmelCase				:						Optional[Any]       						=			bert.half()

										# make sure here that pndm scheduler skips prk
										_UpperCAmelCase				:						Union[str, Any]       						=			StableDiffusionPipeline(
										    unet=A_  ,    scheduler=A_  ,    vae=A_  ,    text_encoder=A_  ,    tokenizer=A_  ,    safety_checker=A_  ,    feature_extractor=self.dummy_extractor  ,    )
										_UpperCAmelCase				:						Tuple       						=			sd_pipe.to(A_		)
										sd_pipe.set_progress_bar_config(disable=A_		)

										_UpperCAmelCase				:						Union[str, Any]       						=			"A painting of a squirrel eating a burger"
										_UpperCAmelCase				:						List[Any]       						=			sd_pipe([prompt]  ,    num_inference_steps=2  ,    output_type="np"		).images

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







@nightly
@require_torch_gpu
class  a     (		unittest.TestCase ):


						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


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


						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


										_UpperCAmelCase				:						List[Any]       						=			"cuda"
										_UpperCAmelCase				:						str       						=			StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"		)
										_UpperCAmelCase				:						Union[str, Any]       						=			pipe.to(A_		)
										pipe.set_progress_bar_config(disable=A_		)

										_UpperCAmelCase				:						Union[str, Any]       						=			"a photo of a cat"
										_UpperCAmelCase				:						Any       						=			{
										    "editing_prompt": ["sunglasses"],
										    "reverse_editing_direction": [False],
										    "edit_warmup_steps": 10,
										    "edit_guidance_scale": 6,
										    "edit_threshold": 0.95,
										    "edit_momentum_scale": 0.5,
										    "edit_mom_beta": 0.6,
										}

										_UpperCAmelCase				:						Union[str, Any]       						=			3
										_UpperCAmelCase				:						Optional[Any]       						=			7

										# no sega enabled
										_UpperCAmelCase				:						Any       						=			torch.Generator(A_		)
										generator.manual_seed(A_		)
										_UpperCAmelCase				:						Optional[Any]       						=			pipe(
										    [prompt]  ,    generator=A_  ,    guidance_scale=A_  ,    num_inference_steps=50  ,    output_type="np"  ,    width=512  ,    height=512  ,    )

										_UpperCAmelCase				:						Optional[Any]       						=			output.images
										_UpperCAmelCase				:						Union[str, Any]       						=			image[0, -3:, -3:, -1]
										_UpperCAmelCase				:						Tuple       						=			[
										    0.34_67_31_13,
										    0.38_49_27_33,
										    0.37_59_73_52,
										    0.34_08_63_35,
										    0.35_65_07_48,
										    0.35_57_92_05,
										    0.3_38_47_63,
										    0.34_34_02_36,
										    0.3_57_32_71,
										]

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

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

										# with sega enabled
										# generator = torch.manual_seed(seed)
										generator.manual_seed(A_		)
										_UpperCAmelCase				:						Dict       						=			pipe(
										    [prompt]  ,    generator=A_  ,    guidance_scale=A_  ,    num_inference_steps=50  ,    output_type="np"  ,    width=512  ,    height=512  ,    **A_  ,    )

										_UpperCAmelCase				:						Tuple       						=			output.images
										_UpperCAmelCase				:						Optional[int]       						=			image[0, -3:, -3:, -1]
										_UpperCAmelCase				:						Any       						=			[
										    0.41_88_78_26,
										    0.37_72_87_66,
										    0.30_13_82_72,
										    0.41_41_63_35,
										    0.41_66_49_85,
										    0.36_28_33_92,
										    0.36_19_12_46,
										    0.43_36_44_65,
										    0.43_00_17_32,
										]

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

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


						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


										_UpperCAmelCase				:						List[str]       						=			"cuda"
										_UpperCAmelCase				:						Dict       						=			StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"		)
										_UpperCAmelCase				:						str       						=			pipe.to(A_		)
										pipe.set_progress_bar_config(disable=A_		)

										_UpperCAmelCase				:						Union[str, Any]       						=			"an image of a crowded boulevard, realistic, 4k"
										_UpperCAmelCase				:						Union[str, Any]       						=			{
										    "editing_prompt": "crowd, crowded, people",
										    "reverse_editing_direction": True,
										    "edit_warmup_steps": 10,
										    "edit_guidance_scale": 8.3,
										    "edit_threshold": 0.9,
										    "edit_momentum_scale": 0.5,
										    "edit_mom_beta": 0.6,
										}

										_UpperCAmelCase				:						List[str]       						=			9
										_UpperCAmelCase				:						List[str]       						=			7

										# no sega enabled
										_UpperCAmelCase				:						Tuple       						=			torch.Generator(A_		)
										generator.manual_seed(A_		)
										_UpperCAmelCase				:						Any       						=			pipe(
										    [prompt]  ,    generator=A_  ,    guidance_scale=A_  ,    num_inference_steps=50  ,    output_type="np"  ,    width=512  ,    height=512  ,    )

										_UpperCAmelCase				:						Optional[Any]       						=			output.images
										_UpperCAmelCase				:						List[Any]       						=			image[0, -3:, -3:, -1]
										_UpperCAmelCase				:						Dict       						=			[
										    0.43_49_79_98,
										    0.91_81_40_65,
										    0.7_54_07_39,
										    0.55_58_02_05,
										    0.8_46_72_65,
										    0.5_38_96_91,
										    0.62_57_45_06,
										    0.58_89_77_63,
										    0.50_92_67_57,
										]

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

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

										# with sega enabled
										# generator = torch.manual_seed(seed)
										generator.manual_seed(A_		)
										_UpperCAmelCase				:						int       						=			pipe(
										    [prompt]  ,    generator=A_  ,    guidance_scale=A_  ,    num_inference_steps=50  ,    output_type="np"  ,    width=512  ,    height=512  ,    **A_  ,    )

										_UpperCAmelCase				:						str       						=			output.images
										_UpperCAmelCase				:						List[Any]       						=			image[0, -3:, -3:, -1]
										_UpperCAmelCase				:						Optional[int]       						=			[
										    0.3_08_97_19,
										    0.30_50_01_44,
										    0.29_01_60_42,
										    0.30_63_09_64,
										    0.32_56_87,
										    0.29_41_92_25,
										    0.2_90_80_91,
										    0.28_72_35_98,
										    0.27_69_62_94,
										]

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

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


						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


										_UpperCAmelCase				:						str       						=			"cuda"
										_UpperCAmelCase				:						List[Any]       						=			StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"		)
										_UpperCAmelCase				:						Dict       						=			pipe.to(A_		)
										pipe.set_progress_bar_config(disable=A_		)

										_UpperCAmelCase				:						Optional[int]       						=			"a castle next to a river"
										_UpperCAmelCase				:						Dict       						=			{
										    "editing_prompt": ["boat on a river, boat", "monet, impression, sunrise"],
										    "reverse_editing_direction": False,
										    "edit_warmup_steps": [15, 18],
										    "edit_guidance_scale": 6,
										    "edit_threshold": [0.9, 0.8],
										    "edit_momentum_scale": 0.5,
										    "edit_mom_beta": 0.6,
										}

										_UpperCAmelCase				:						Optional[Any]       						=			48
										_UpperCAmelCase				:						Dict       						=			7

										# no sega enabled
										_UpperCAmelCase				:						Dict       						=			torch.Generator(A_		)
										generator.manual_seed(A_		)
										_UpperCAmelCase				:						List[str]       						=			pipe(
										    [prompt]  ,    generator=A_  ,    guidance_scale=A_  ,    num_inference_steps=50  ,    output_type="np"  ,    width=512  ,    height=512  ,    )

										_UpperCAmelCase				:						Dict       						=			output.images
										_UpperCAmelCase				:						Optional[Any]       						=			image[0, -3:, -3:, -1]
										_UpperCAmelCase				:						Tuple       						=			[
										    0.75_16_35_55,
										    0.76_03_71_45,
										    0.6_17_85,
										    0.9_18_96_73,
										    0.8_62_77_01,
										    0.85_18_96_94,
										    0.8_51_28_13,
										    0.87_01_20_76,
										    0.8_31_28_57,
										]

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

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

										# with sega enabled
										# generator = torch.manual_seed(seed)
										generator.manual_seed(A_		)
										_UpperCAmelCase				:						Dict       						=			pipe(
										    [prompt]  ,    generator=A_  ,    guidance_scale=A_  ,    num_inference_steps=50  ,    output_type="np"  ,    width=512  ,    height=512  ,    **A_  ,    )

										_UpperCAmelCase				:						Optional[int]       						=			output.images
										_UpperCAmelCase				:						str       						=			image[0, -3:, -3:, -1]
										_UpperCAmelCase				:						Dict       						=			[
										    0.73_55_33_65,
										    0.7_53_72_71,
										    0.74_34_19_05,
										    0.66_48_03_56,
										    0.6_47_29_25,
										    0.63_03_94_16,
										    0.64_81_29_05,
										    0.6_74_97_17,
										    0.6_51_71_02,
										]

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

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





						def 				_UpperCAmelCase (				self		):







										'''simple docstring'''


										_UpperCAmelCase				:						List[Any]       						=			"cuda"
										_UpperCAmelCase				:						Any       						=			StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"  ,    torch_dtype=torch.floataa		)
										_UpperCAmelCase				:						Union[str, Any]       						=			pipe.to(A_		)
										pipe.set_progress_bar_config(disable=A_		)

										_UpperCAmelCase				:						Union[str, Any]       						=			"a photo of a cat"
										_UpperCAmelCase				:						Optional[Any]       						=			{
										    "editing_prompt": ["sunglasses"],
										    "reverse_editing_direction": [False],
										    "edit_warmup_steps": 10,
										    "edit_guidance_scale": 6,
										    "edit_threshold": 0.95,
										    "edit_momentum_scale": 0.5,
										    "edit_mom_beta": 0.6,
										}

										_UpperCAmelCase				:						Dict       						=			3
										_UpperCAmelCase				:						Any       						=			7

										# no sega enabled
										_UpperCAmelCase				:						Optional[int]       						=			torch.Generator(A_		)
										generator.manual_seed(A_		)
										_UpperCAmelCase				:						Optional[Any]       						=			pipe(
										    [prompt]  ,    generator=A_  ,    guidance_scale=A_  ,    num_inference_steps=50  ,    output_type="np"  ,    width=512  ,    height=512  ,    )

										_UpperCAmelCase				:						Any       						=			output.images
										_UpperCAmelCase				:						Union[str, Any]       						=			image[0, -3:, -3:, -1]
										_UpperCAmelCase				:						Dict       						=			[
										    0.34_88_76_95,
										    0.3_87_69_53,
										    0.3_75,
										    0.34_42_38_28,
										    0.3_58_15_43,
										    0.35_71_77_73,
										    0.3_38_37_89,
										    0.34_57_03_12,
										    0.35_93_75,
										]

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

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

										# with sega enabled
										# generator = torch.manual_seed(seed)
										generator.manual_seed(A_		)
										_UpperCAmelCase				:						Dict       						=			pipe(
										    [prompt]  ,    generator=A_  ,    guidance_scale=A_  ,    num_inference_steps=50  ,    output_type="np"  ,    width=512  ,    height=512  ,    **A_  ,    )

										_UpperCAmelCase				:						Union[str, Any]       						=			output.images
										_UpperCAmelCase				:						str       						=			image[0, -3:, -3:, -1]
										_UpperCAmelCase				:						Any       						=			[
										    0.42_28_51_56,
										    0.36_91_40_62,
										    0.29_07_71_48,
										    0.42_04_10_16,
										    0.41_91_89_45,
										    0.35_49_80_47,
										    0.3_61_81_64,
										    0.4_42_38_28,
										    0.43_11_52_34,
										]

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

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








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       __a					:


     """simple docstring"""


     def __init__(	self			: int				,_UpperCamelCase			: str				,_UpperCamelCase			: Optional[int]=2				,_UpperCamelCase			: Tuple=8				,_UpperCamelCase			: Union[str, Any]=True				,_UpperCamelCase			: Tuple=False				,_UpperCamelCase			: Tuple=False				,_UpperCamelCase			: Optional[Any]=False				,_UpperCamelCase			: Union[str, Any]=False				,_UpperCamelCase			: Dict=9_9				,_UpperCamelCase			: int=3_2				,_UpperCamelCase			: Tuple=3				,_UpperCamelCase			: Dict=4				,_UpperCamelCase			: Optional[int]=3_7				,_UpperCamelCase			: int=3_2				,_UpperCamelCase			: Optional[int]=1_2_8				,_UpperCamelCase			: Optional[Any]=8				,_UpperCamelCase			: Optional[int]=8				,_UpperCamelCase			: Tuple=8				,_UpperCamelCase			: Optional[int]=1.0				,_UpperCamelCase			: List[Any]=True				,)		->   Optional[Any]:


         '''simple docstring'''





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




     def   __A				(	self			: List[Any]						)		->   Union[str, Any]:


         '''simple docstring'''





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

         SCREAMING_SNAKE_CASE__								=self.get_config()

         return (config, input_ids)




     def   __A				(	self			: Tuple						)		->   List[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=_UpperCamelCase				,init_std=self.init_std				,return_dict=self.return_dict				,)




     def   __A				(	self			: Any				,_UpperCamelCase			: Dict				,_UpperCamelCase			: List[Any]				,*_UpperCamelCase			: str						)		->   List[Any]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=CpmAntModel(config=_UpperCamelCase						)
         model.to(_UpperCamelCase						)
         model.eval()

         SCREAMING_SNAKE_CASE__								=model(**_UpperCamelCase						).last_hidden_state

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




     def   __A				(	self			: int				,_UpperCamelCase			: str				,_UpperCamelCase			: Tuple				,*_UpperCamelCase			: Dict						)		->   str:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=CpmAntForCausalLM(_UpperCamelCase						)
         model.to(_UpperCamelCase						)
         SCREAMING_SNAKE_CASE__								=input_ids["""input_ids"""].to(_UpperCamelCase						)
         model.eval()

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




     def   __A				(	self			: str						)		->   List[Any]:


         '''simple docstring'''





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






@require_torch
class       __a					( __lowerCamelCase							,     __lowerCamelCase							,     unittest.TestCase     ):


     """simple docstring"""


     _A				:    List[Any]									=  (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
     _A				:    Any									=  (
         {"feature-extraction": CpmAntModel, "text-generation": CpmAntForCausalLM} if is_torch_available() else {}
     )

     _A				:    Optional[int]									=  False
     _A				:    Optional[int]									=  False
     _A				:    Optional[Any]									=  False
     _A				:    Any									=  False
     _A				:    int									=  False




     def   __A				(	self			: int						)		->   List[Any]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=CpmAntModelTester(self						)
         SCREAMING_SNAKE_CASE__								=ConfigTester(self				,config_class=_UpperCamelCase						)




     def   __A				(	self			: Optional[int]						)		->   Dict:


         '''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   __A				(	self			: Tuple						)		->   List[Any]:


         '''simple docstring'''





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




     def   __A				(	self			: str						)		->   Dict:


         '''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   __A				(	self			: Any						)		->   List[str]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__			,		SCREAMING_SNAKE_CASE__								=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_cpmant_model(_UpperCamelCase				,_UpperCamelCase						)




     def   __A				(	self			: str						)		->   Tuple:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__			,		SCREAMING_SNAKE_CASE__								=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_lm_head_model(_UpperCamelCase				,_UpperCamelCase						)






@require_torch
class       __a					( unittest.TestCase     ):


     """simple docstring"""


     @tooslow
     def   __A				(	self			: Optional[Any]						)		->   Tuple:


         '''simple docstring'''





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

         SCREAMING_SNAKE_CASE__								=torch.tensor(
             [[[6.1708, 5.9244, 1.0835], [6.5207, 6.2893, -11.3324], [-1.0107, -0.0576, -5.9577]]]				,)
         self.assertTrue(torch.allclose(hidden_states[:, :3, :3]				,_UpperCamelCase				,atol=1e-2						)						)






@require_torch
class       __a					( unittest.TestCase     ):


     """simple docstring"""


     @tooslow
     def   __A				(	self			: List[Any]						)		->   Optional[Any]:


         '''simple docstring'''





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

         SCREAMING_SNAKE_CASE__								=torch.tensor(
             [[[-6.4267, -6.4083, -6.3958], [-5.8802, -5.9447, -5.7811], [-5.3896, -5.4820, -5.4295]]]				,)
         self.assertTrue(torch.allclose(hidden_states[:, :3, :3]				,_UpperCamelCase				,atol=1e-2						)						)




     @tooslow
     def   __A				(	self			: Any						)		->   Any:


         '''simple docstring'''





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




     @tooslow
     def   __A				(	self			: Optional[Any]						)		->   Union[str, Any]:


         '''simple docstring'''





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






