



'''simple docstring'''




import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image

from diffusers import (
    DDIMScheduler,
    KandinskyVaaInpaintPipeline,
    KandinskyVaaPriorPipeline,
    UNetaDConditionModel,
    VQModel,
)
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

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()







class      _lowerCAmelCase							(      __UpperCAmelCase				,      unittest.TestCase			):
	__SCREAMING_SNAKE_CASE       :		Tuple		      = KandinskyVaaInpaintPipeline
	__SCREAMING_SNAKE_CASE       :		int		      = ['image_embeds', 'negative_image_embeds', 'image', 'mask_image']
	__SCREAMING_SNAKE_CASE       :		str		      = [
	    'image_embeds',
	    'negative_image_embeds',
	    'image',
	    'mask_image',
	]
	__SCREAMING_SNAKE_CASE       :		Optional[Any]		      = [
	    'generator',
	    'height',
	    'width',
	    'latents',
	    'guidance_scale',
	    'num_inference_steps',
	    'return_dict',
	    'guidance_scale',
	    'num_images_per_prompt',
	    'output_type',
	    'return_dict',
	]
	__SCREAMING_SNAKE_CASE       :		List[str]		      = False


	@property
	def    _a     (self							):
				return 32


	@property
	def    _a     (self							):
				return 32


	@property
	def    _a     (self							):
				return self.time_input_dim


	@property
	def    _a     (self							):
				return self.time_input_dim * 4


	@property
	def    _a     (self							):
				return 100


	@property
	def    _a     (self							):
				torch.manual_seed(0							)

				A_     :						str     =					{
				    """in_channels""": 9,
				    # Out channels is double in channels because predicts mean and variance
				    """out_channels""": 8,
				    """addition_embed_type""": """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""": """image_proj""",
				    """cross_attention_dim""": self.cross_attention_dim,
				    """attention_head_dim""": 4,
				    """resnet_time_scale_shift""": """scale_shift""",
				    """class_embed_type""": None,
				}

				A_     :						str     =					UNetaDConditionModel(**lowercase							)
				return model


	@property
	def    _a     (self							):
				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    _a     (self							):
				torch.manual_seed(0							)
				A_     :						int     =					VQModel(**self.dummy_movq_kwargs							)
				return model


	def    _a     (self							):
				A_     :						Optional[Any]     =					self.dummy_unet
				A_     :						Dict     =					self.dummy_movq

				A_     :						List[Any]     =					DDIMScheduler(
				    num_train_timesteps=1000	,				beta_schedule="""linear"""	,				beta_start=0.0_00_85	,				beta_end=0.0_12	,				clip_sample=lowercase	,				set_alpha_to_one=lowercase	,				steps_offset=1	,				prediction_type="""epsilon"""	,				thresholding=lowercase	,				)

				A_     :						Any     =					{
				    """unet""": unet,
				    """scheduler""": scheduler,
				    """movq""": movq,
				}

				return components


	def    _a     (self	,				lowercase	,				lowercase=0							):
				A_     :						Optional[Any]     =					floats_tensor((1, self.text_embedder_hidden_size)	,				rng=random.Random(lowercase							)							).to(lowercase							)
				A_     :						List[Any]     =					floats_tensor((1, self.text_embedder_hidden_size)	,				rng=random.Random(seed + 1							)							).to(
				    lowercase							)
				# create init_image
				A_     :						Any     =					floats_tensor((1, 3, 64, 64)	,				rng=random.Random(lowercase							)							).to(lowercase							)
				A_     :						Union[str, Any]     =					image.cpu().permute(0	,				2	,				3	,				1							)[0]
				A_     :						Tuple     =					Image.fromarray(np.uinta(lowercase							)							).convert("""RGB"""							).resize((256, 256)							)
				# create mask
				A_     :						List[str]     =					np.ones((64, 64)	,				dtype=np.floataa							)
				A_     :						Any     =					0

				if str(lowercase							).startswith("""mps"""							):
							A_     :						int     =					torch.manual_seed(lowercase							)
				else:
							A_     :						Dict     =					torch.Generator(device=lowercase							).manual_seed(lowercase							)
				A_     :						Any     =					{
				    """image""": init_image,
				    """mask_image""": mask,
				    """image_embeds""": image_embeds,
				    """negative_image_embeds""": negative_image_embeds,
				    """generator""": generator,
				    """height""": 64,
				    """width""": 64,
				    """num_inference_steps""": 2,
				    """guidance_scale""": 4.0,
				    """output_type""": """np""",
				}
				return inputs


	def    _a     (self							):
				A_     :						List[str]     =					"""cpu"""

				A_     :						str     =					self.get_dummy_components()

				A_     :						Any     =					self.pipeline_class(**lowercase							)
				A_     :						List[Any]     =					pipe.to(lowercase							)

				pipe.set_progress_bar_config(disable=lowercase							)

				A_     :						Union[str, Any]     =					pipe(**self.get_dummy_inputs(lowercase							)							)
				A_     :						List[str]     =					output.images

				A_     :						Union[str, Any]     =					pipe(
				    **self.get_dummy_inputs(lowercase							)	,				return_dict=lowercase	,				)[0]

				A_     :						List[str]     =					image[0, -3:, -3:, -1]
				A_     :						Any     =					image_from_tuple[0, -3:, -3:, -1]

				print(F'image.shape {image.shape}'							)

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

				A_     :						Optional[int]     =					np.array(
				    [0.50_77_59_03, 0.49_52_71_95, 0.48_82_45_43, 0.50_19_22_37, 0.48_64_49_06, 0.49_37_38_14, 0.4_78_05_98, 0.47_23_48_27, 0.48_32_78_48]							)

				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()}'


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







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


	def    _a     (self							):
				# clean up the VRAM after each test
				super().tearDown()
				gc.collect()
				torch.cuda.empty_cache()


	def    _a     (self							):
				A_     :						Optional[Any]     =					load_numpy(
				    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
				    """/kandinskyv22/kandinskyv22_inpaint_cat_with_hat_fp16.npy"""							)

				A_     :						Optional[Any]     =					load_image(
				    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png"""							)
				A_     :						Dict     =					np.ones((768, 768)	,				dtype=np.floataa							)
				A_     :						Any     =					0

				A_     :						str     =					"""a hat"""

				A_     :						Union[str, Any]     =					KandinskyVaaPriorPipeline.from_pretrained(
				    """kandinsky-community/kandinsky-2-2-prior"""	,				torch_dtype=torch.floataa							)
				pipe_prior.to(lowercase							)

				A_     :						Union[str, Any]     =					KandinskyVaaInpaintPipeline.from_pretrained(
				    """kandinsky-community/kandinsky-2-2-decoder-inpaint"""	,				torch_dtype=torch.floataa							)
				A_     :						str     =					pipeline.to(lowercase							)
				pipeline.set_progress_bar_config(disable=lowercase							)

				A_     :						int     =					torch.Generator(device="""cpu"""							).manual_seed(0							)
				A_     :						Dict     =					pipe_prior(
				    lowercase	,				generator=lowercase	,				num_inference_steps=5	,				negative_prompt=""""""	,				).to_tuple()

				A_     :						List[Any]     =					pipeline(
				    image=lowercase	,				mask_image=lowercase	,				image_embeds=lowercase	,				negative_image_embeds=lowercase	,				generator=lowercase	,				num_inference_steps=100	,				height=768	,				width=768	,				output_type="""np"""	,				)

				A_     :						Tuple     =					output.images[0]

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

				assert_mean_pixel_difference(lowercase	,				lowercase							)




'''simple docstring'''




import os
import tempfile
import unittest

from transformers import NezhaConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    MODEL_FOR_PRETRAINING_MAPPING,
						    NezhaForMaskedLM,
						    NezhaForMultipleChoice,
						    NezhaForNextSentencePrediction,
						    NezhaForPreTraining,
						    NezhaForQuestionAnswering,
						    NezhaForSequenceClassification,
						    NezhaForTokenClassification,
						    NezhaModel,
						)
						from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST







class      _lowerCAmelCase							:


	def __init__(self	,				lowercase	,				lowercase=13	,				lowercase=7	,				lowercase=True	,				lowercase=True	,				lowercase=True	,				lowercase=True	,				lowercase=99	,				lowercase=32	,				lowercase=5	,				lowercase=4	,				lowercase=37	,				lowercase="gelu"	,				lowercase=0.1	,				lowercase=0.1	,				lowercase=128	,				lowercase=32	,				lowercase=16	,				lowercase=2	,				lowercase=0.02	,				lowercase=3	,				lowercase=4	,				lowercase=None	,				):
				A_     :						Union[str, Any]     =					parent
				A_     :						Optional[int]     =					batch_size
				A_     :						Any     =					seq_length
				A_     :						int     =					is_training
				A_     :						List[str]     =					use_input_mask
				A_     :						Any     =					use_token_type_ids
				A_     :						List[Any]     =					use_labels
				A_     :						Dict     =					vocab_size
				A_     :						Optional[int]     =					hidden_size
				A_     :						int     =					num_hidden_layers
				A_     :						List[str]     =					num_attention_heads
				A_     :						Dict     =					intermediate_size
				A_     :						List[str]     =					hidden_act
				A_     :						List[str]     =					hidden_dropout_prob
				A_     :						Union[str, Any]     =					attention_probs_dropout_prob
				A_     :						Optional[Any]     =					max_position_embeddings
				A_     :						Optional[Any]     =					type_vocab_size
				A_     :						List[Any]     =					type_sequence_label_size
				A_     :						Tuple     =					initializer_range
				A_     :						List[Any]     =					num_labels
				A_     :						str     =					num_choices
				A_     :						Tuple     =					scope


	def    _a     (self							):
				A_     :						List[str]     =					ids_tensor([self.batch_size, self.seq_length]	,				self.vocab_size							)

				A_     :						Tuple     =					None
				if self.use_input_mask:
							A_     :						str     =					random_attention_mask([self.batch_size, self.seq_length]							)

				A_     :						Any     =					None
				if self.use_token_type_ids:
							A_     :						str     =					ids_tensor([self.batch_size, self.seq_length]	,				self.type_vocab_size							)

				A_     :						Dict     =					None
				A_     :						Any     =					None
				A_     :						List[Any]     =					None
				if self.use_labels:
							A_     :						int     =					ids_tensor([self.batch_size]	,				self.type_sequence_label_size							)
							A_     :						int     =					ids_tensor([self.batch_size, self.seq_length]	,				self.num_labels							)
							A_     :						int     =					ids_tensor([self.batch_size]	,				self.num_choices							)

				A_     :						Any     =					self.get_config()

				return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


	def    _a     (self							):
				return NezhaConfig(
				    vocab_size=self.vocab_size	,				hidden_size=self.hidden_size	,				num_hidden_layers=self.num_hidden_layers	,				num_attention_heads=self.num_attention_heads	,				intermediate_size=self.intermediate_size	,				hidden_act=self.hidden_act	,				hidden_dropout_prob=self.hidden_dropout_prob	,				attention_probs_dropout_prob=self.attention_probs_dropout_prob	,				max_position_embeddings=self.max_position_embeddings	,				type_vocab_size=self.type_vocab_size	,				is_decoder=lowercase	,				initializer_range=self.initializer_range	,				)


	def    _a     (self							):
				(
				    (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   
				)     :						Union[str, Any]     =					self.prepare_config_and_inputs()

				A_     :						Union[str, Any]     =					True
				A_     :						List[Any]     =					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
				A_     :						Optional[Any]     =					ids_tensor([self.batch_size, self.seq_length]	,				vocab_size=2							)

				return (
				    config,
				    input_ids,
				    token_type_ids,
				    input_mask,
				    sequence_labels,
				    token_labels,
				    choice_labels,
				    encoder_hidden_states,
				    encoder_attention_mask,
				)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						Union[str, Any]     =					NezhaModel(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						int     =					model(lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase							)
				A_     :						Optional[Any]     =					model(lowercase	,				token_type_ids=lowercase							)
				A_     :						str     =					model(lowercase							)
				self.parent.assertEqual(result.last_hidden_state.shape	,				(self.batch_size, self.seq_length, self.hidden_size)							)
				self.parent.assertEqual(result.pooler_output.shape	,				(self.batch_size, self.hidden_size)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				):
				A_     :						Optional[int]     =					True
				A_     :						Optional[Any]     =					NezhaModel(lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						Optional[int]     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				encoder_hidden_states=lowercase	,				encoder_attention_mask=lowercase	,				)
				A_     :						str     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				encoder_hidden_states=lowercase	,				)
				A_     :						Tuple     =					model(lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase							)
				self.parent.assertEqual(result.last_hidden_state.shape	,				(self.batch_size, self.seq_length, self.hidden_size)							)
				self.parent.assertEqual(result.pooler_output.shape	,				(self.batch_size, self.hidden_size)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						Optional[Any]     =					NezhaForMaskedLM(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						List[str]     =					model(lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase							)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.seq_length, self.vocab_size)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						Tuple     =					NezhaForNextSentencePrediction(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						Union[str, Any]     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase	,				)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, 2)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						int     =					NezhaForPreTraining(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						str     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase	,				next_sentence_label=lowercase	,				)
				self.parent.assertEqual(result.prediction_logits.shape	,				(self.batch_size, self.seq_length, self.vocab_size)							)
				self.parent.assertEqual(result.seq_relationship_logits.shape	,				(self.batch_size, 2)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						Any     =					NezhaForQuestionAnswering(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						Optional[int]     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				start_positions=lowercase	,				end_positions=lowercase	,				)
				self.parent.assertEqual(result.start_logits.shape	,				(self.batch_size, self.seq_length)							)
				self.parent.assertEqual(result.end_logits.shape	,				(self.batch_size, self.seq_length)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						Optional[Any]     =					self.num_labels
				A_     :						int     =					NezhaForSequenceClassification(lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						List[Any]     =					model(lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase							)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_labels)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						List[str]     =					self.num_labels
				A_     :						Optional[int]     =					NezhaForTokenClassification(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						int     =					model(lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase							)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.seq_length, self.num_labels)							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						int     =					self.num_choices
				A_     :						int     =					NezhaForMultipleChoice(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						int     =					input_ids.unsqueeze(1							).expand(-1	,				self.num_choices	,				-1							).contiguous()
				A_     :						str     =					token_type_ids.unsqueeze(1							).expand(-1	,				self.num_choices	,				-1							).contiguous()
				A_     :						List[str]     =					input_mask.unsqueeze(1							).expand(-1	,				self.num_choices	,				-1							).contiguous()
				A_     :						Optional[int]     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase	,				)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_choices)							)


	def    _a     (self							):
				A_     :						Tuple     =					self.prepare_config_and_inputs()
				(
				    (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   
				)     :						int     =					config_and_inputs
				A_     :						Any     =					{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
				return config, inputs_dict







@require_torch
class      _lowerCAmelCase							(      __UpperCAmelCase				,      __UpperCAmelCase				,      __UpperCAmelCase				,      unittest.TestCase			):
	__SCREAMING_SNAKE_CASE       :		Optional[Any]		      = (
	    (
	        NezhaModel,
	        NezhaForMaskedLM,
	        NezhaForMultipleChoice,
	        NezhaForNextSentencePrediction,
	        NezhaForPreTraining,
	        NezhaForQuestionAnswering,
	        NezhaForSequenceClassification,
	        NezhaForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	__SCREAMING_SNAKE_CASE       :		str		      = (
	    {
	        'feature-extraction': NezhaModel,
	        'fill-mask': NezhaForMaskedLM,
	        'question-answering': NezhaForQuestionAnswering,
	        'text-classification': NezhaForSequenceClassification,
	        'token-classification': NezhaForTokenClassification,
	        'zero-shot': NezhaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	__SCREAMING_SNAKE_CASE       :		List[Any]		      = True


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase=False							):
				A_     :						Optional[Any]     =					super()._prepare_for_class(lowercase	,				lowercase	,				return_labels=lowercase							)

				if return_labels:
							if model_class in get_values(lowercase							):
										A_     :						Optional[Any]     =					torch.zeros(
										    (self.model_tester.batch_size, self.model_tester.seq_length)	,				dtype=torch.long	,				device=lowercase							)
										A_     :						Tuple     =					torch.zeros(
										    self.model_tester.batch_size	,				dtype=torch.long	,				device=lowercase							)
				return inputs_dict


	def    _a     (self							):
				A_     :						Optional[int]     =					NezhaModelTester(self							)
				A_     :						Any     =					ConfigTester(self	,				config_class=lowercase	,				hidden_size=37							)


	def    _a     (self							):
				self.config_tester.run_common_tests()


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowercase							)


	def    _a     (self							):
				A_     :						str     =					self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_model_as_decoder(*lowercase							)


	def    _a     (self							):
				# This regression test was failing with PyTorch < 1.3
				(
				    (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   
				)     :						List[Any]     =					self.model_tester.prepare_config_and_inputs_for_decoder()

				A_     :						str     =					None

				self.model_tester.create_and_check_model_as_decoder(
				    lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				)


	def    _a     (self							):
				A_     :						str     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_lm(*lowercase							)


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_multiple_choice(*lowercase							)


	def    _a     (self							):
				A_     :						Union[str, Any]     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_next_sequence_prediction(*lowercase							)


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_pretraining(*lowercase							)


	def    _a     (self							):
				A_     :						Tuple     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*lowercase							)


	def    _a     (self							):
				A_     :						List[str]     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*lowercase							)


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*lowercase							)


	@slow
	def    _a     (self							):
				for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							A_     :						Any     =					NezhaModel.from_pretrained(lowercase							)
							self.assertIsNotNone(lowercase							)


	@slow
	@require_torch_gpu
	def    _a     (self							):
				A_,   A_     :						Tuple     =					self.model_tester.prepare_config_and_inputs_for_common()
				for model_class in self.all_model_classes:
							# NezhaForMultipleChoice behaves incorrectly in JIT environments.
							if model_class == NezhaForMultipleChoice:
										return

							A_     :						Optional[int]     =					True
							A_     :						str     =					model_class(config=lowercase							)

							A_     :						str     =					self._prepare_for_class(lowercase	,				lowercase							)
							A_     :						Tuple     =					torch.jit.trace(
							    lowercase	,				(inputs_dict["""input_ids"""].to("""cpu"""							), inputs_dict["""attention_mask"""].to("""cpu"""							))							)

							with tempfile.TemporaryDirectory() as tmp:
										torch.jit.save(lowercase	,				os.path.join(lowercase	,				"""bert.pt"""							)							)
										A_     :						List[str]     =					torch.jit.load(os.path.join(lowercase	,				"""bert.pt"""							)	,				map_location=lowercase							)
										loaded(inputs_dict["""input_ids"""].to(lowercase							)	,				inputs_dict["""attention_mask"""].to(lowercase							)							)







@require_torch
class      _lowerCAmelCase							(      unittest.TestCase			):


	@slow
	def    _a     (self							):
				A_     :						Dict     =					NezhaModel.from_pretrained("""sijunhe/nezha-cn-base"""							)
				A_     :						List[Any]     =					torch.tensor([[0, 1, 2, 3, 4, 5]]							)
				A_     :						List[str]     =					torch.tensor([[0, 1, 1, 1, 1, 1]]							)
				with torch.no_grad():
							A_     :						Optional[int]     =					model(lowercase	,				attention_mask=lowercase							)[0]
				A_     :						Optional[int]     =					torch.Size((1, 6, 768)							)
				self.assertEqual(output.shape	,				lowercase							)
				A_     :						List[Any]     =					torch.tensor([[[0.06_85, 0.24_41, 0.11_02], [0.06_00, 0.19_06, 0.13_49], [0.02_21, 0.08_19, 0.05_86]]]							)

				self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,				lowercase	,				atol=1E-4							)							)


	@slow
	def    _a     (self							):
				A_     :						str     =					NezhaForMaskedLM.from_pretrained("""sijunhe/nezha-cn-base"""							)
				A_     :						Union[str, Any]     =					torch.tensor([[0, 1, 2, 3, 4, 5]]							)
				A_     :						str     =					torch.tensor([[1, 1, 1, 1, 1, 1]]							)
				with torch.no_grad():
							A_     :						Tuple     =					model(lowercase	,				attention_mask=lowercase							)[0]
				A_     :						str     =					torch.Size((1, 6, 21128)							)
				self.assertEqual(output.shape	,				lowercase							)
				A_     :						List[Any]     =					torch.tensor(
				    [[-2.79_39, -1.79_02, -2.21_89], [-2.85_85, -1.89_08, -2.37_23], [-2.64_99, -1.77_50, -2.25_58]]							)

				self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,				lowercase	,				atol=1E-4							)							)