
'''simple docstring'''





import unittest

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

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,
						    ElectraForCausalLM,
						    ElectraForMaskedLM,
						    ElectraForMultipleChoice,
						    ElectraForPreTraining,
						    ElectraForQuestionAnswering,
						    ElectraForSequenceClassification,
						    ElectraForTokenClassification,
						    ElectraModel,
						)
						from transformers.models.electra.modeling_electra import ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST

class     a :




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




				def 						__snake_case				(				self						:					List[Any]   ):
										snake_case							:  Tuple			=     ids_tensor([self.batch_size, self.seq_length],     self.vocab_size   )

										snake_case							:  Union[str, Any]			=     None
										if self.use_input_mask:
																snake_case							:  Dict			=     random_attention_mask([self.batch_size, self.seq_length]   )

										snake_case							:  int			=     None
										if self.use_token_type_ids:
																snake_case							:  Optional[int]			=     ids_tensor([self.batch_size, self.seq_length],     self.type_vocab_size   )

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

										snake_case							:  Union[str, Any]			=     self.get_config()

										return (
										    config,
										    input_ids,
										    token_type_ids,
										    input_mask,
										    sequence_labels,
										    token_labels,
										    choice_labels,
										    fake_token_labels,
										)




				def 						__snake_case				(				self						:					Union[str, Any]   ):
										return ElectraConfig(
										    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=SCREAMING_SNAKE_CASE_,     initializer_range=self.initializer_range,     )




				def 						__snake_case				(				self						:					Any   ):
										(
										    (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       
										)							:  Dict			=     self.prepare_config_and_inputs()

										snake_case							:  List[str]			=     True
										snake_case							:  Optional[Any]			=     floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
										snake_case							:  Optional[int]			=     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 						__snake_case				(				self						:					Tuple,     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					List[str],     SCREAMING_SNAKE_CASE_						:					Optional[int],     ):
										snake_case							:  Any			=     ElectraModel(config=SCREAMING_SNAKE_CASE_   )
										model.to(SCREAMING_SNAKE_CASE_   )
										model.eval()
										snake_case							:  Optional[int]			=     model(SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_   )
										snake_case							:  Dict			=     model(SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_   )
										snake_case							:  int			=     model(SCREAMING_SNAKE_CASE_   )
										self.parent.assertEqual(result.last_hidden_state.shape,     (self.batch_size, self.seq_length, self.hidden_size)   )




				def 						__snake_case				(				self						:					List[str],     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					Dict,     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					Optional[Any],     ):
										snake_case							:  List[Any]			=     True
										snake_case							:  int			=     ElectraModel(SCREAMING_SNAKE_CASE_   )
										model.to(SCREAMING_SNAKE_CASE_   )
										model.eval()
										snake_case							:  Union[str, Any]			=     model(
										    SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_,     encoder_hidden_states=SCREAMING_SNAKE_CASE_,     encoder_attention_mask=SCREAMING_SNAKE_CASE_,     )
										snake_case							:  Optional[int]			=     model(
										    SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_,     encoder_hidden_states=SCREAMING_SNAKE_CASE_,     )
										snake_case							:  List[Any]			=     model(SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_   )
										self.parent.assertEqual(result.last_hidden_state.shape,     (self.batch_size, self.seq_length, self.hidden_size)   )




				def 						__snake_case				(				self						:					int,     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					Dict,     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					Tuple,     ):
										snake_case							:  Dict			=     ElectraForMaskedLM(config=SCREAMING_SNAKE_CASE_   )
										model.to(SCREAMING_SNAKE_CASE_   )
										model.eval()
										snake_case							:  Union[str, Any]			=     model(SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_,     labels=SCREAMING_SNAKE_CASE_   )
										self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.seq_length, self.vocab_size)   )




				def 						__snake_case				(				self						:					Any,     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					Dict,     SCREAMING_SNAKE_CASE_						:					Optional[Any],     ):
										snake_case							:  int			=     ElectraForCausalLM(config=SCREAMING_SNAKE_CASE_   )
										model.to(SCREAMING_SNAKE_CASE_   )
										model.eval()
										snake_case							:  Optional[int]			=     model(SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_,     labels=SCREAMING_SNAKE_CASE_   )
										self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.seq_length, self.vocab_size)   )




				def 						__snake_case				(				self						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					List[str],     SCREAMING_SNAKE_CASE_						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					Dict,     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					Any,     ):
										snake_case							:  Dict			=     self.num_labels
										snake_case							:  Dict			=     ElectraForTokenClassification(config=SCREAMING_SNAKE_CASE_   )
										model.to(SCREAMING_SNAKE_CASE_   )
										model.eval()
										snake_case							:  str			=     model(SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_,     labels=SCREAMING_SNAKE_CASE_   )
										self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.seq_length, self.num_labels)   )




				def 						__snake_case				(				self						:					str,     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					Dict,     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					Optional[Any],     ):
										snake_case							:  Dict			=     self.num_labels
										snake_case							:  int			=     ElectraForPreTraining(config=SCREAMING_SNAKE_CASE_   )
										model.to(SCREAMING_SNAKE_CASE_   )
										model.eval()
										snake_case							:  List[Any]			=     model(SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_,     labels=SCREAMING_SNAKE_CASE_   )
										self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.seq_length)   )




				def 						__snake_case				(				self						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					List[str],     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					List[str],     ):
										snake_case							:  Tuple			=     self.num_labels
										snake_case							:  List[str]			=     ElectraForSequenceClassification(SCREAMING_SNAKE_CASE_   )
										model.to(SCREAMING_SNAKE_CASE_   )
										model.eval()
										snake_case							:  Optional[Any]			=     model(SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_,     labels=SCREAMING_SNAKE_CASE_   )
										self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.num_labels)   )




				def 						__snake_case				(				self						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					List[str],     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					List[str],     SCREAMING_SNAKE_CASE_						:					int,     ):
										snake_case							:  int			=     ElectraForQuestionAnswering(config=SCREAMING_SNAKE_CASE_   )
										model.to(SCREAMING_SNAKE_CASE_   )
										model.eval()
										snake_case							:  List[Any]			=     model(
										    SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_,     start_positions=SCREAMING_SNAKE_CASE_,     end_positions=SCREAMING_SNAKE_CASE_,     )
										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 						__snake_case				(				self						:					str,     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					Optional[int],     ):
										snake_case							:  str			=     self.num_choices
										snake_case							:  int			=     ElectraForMultipleChoice(config=SCREAMING_SNAKE_CASE_   )
										model.to(SCREAMING_SNAKE_CASE_   )
										model.eval()
										snake_case							:  List[str]			=     input_ids.unsqueeze(1   ).expand(-1,     self.num_choices,     -1   ).contiguous()
										snake_case							:  int			=     token_type_ids.unsqueeze(1   ).expand(-1,     self.num_choices,     -1   ).contiguous()
										snake_case							:  Tuple			=     input_mask.unsqueeze(1   ).expand(-1,     self.num_choices,     -1   ).contiguous()
										snake_case							:  str			=     model(
										    SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_,     token_type_ids=SCREAMING_SNAKE_CASE_,     labels=SCREAMING_SNAKE_CASE_,     )
										self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.num_choices)   )




				def 						__snake_case				(				self						:					Dict   ):
										snake_case							:  Union[str, Any]			=     self.prepare_config_and_inputs()
										(
										    (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       
										)							:  Union[str, Any]			=     config_and_inputs
										snake_case							:  Dict			=     {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
										return config, inputs_dict




@require_torch
class     a (					__magic_name__					,__magic_name__					,unittest.TestCase     ):
				_snake_case		     =			(
				    (
				        ElectraModel,
				        ElectraForPreTraining,
				        ElectraForMaskedLM,
				        ElectraForCausalLM,
				        ElectraForMultipleChoice,
				        ElectraForTokenClassification,
				        ElectraForSequenceClassification,
				        ElectraForQuestionAnswering,
				    )
				    if is_torch_available()
				    else ()
				)
				_snake_case		     =			(
				    {
				        '''feature-extraction''': ElectraModel,
				        '''fill-mask''': ElectraForMaskedLM,
				        '''question-answering''': ElectraForQuestionAnswering,
				        '''text-classification''': ElectraForSequenceClassification,
				        '''text-generation''': ElectraForCausalLM,
				        '''token-classification''': ElectraForTokenClassification,
				        '''zero-shot''': ElectraForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				_snake_case		     =			True




				def 						__snake_case				(				self						:					str,     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					int=False   ):
										snake_case							:  Dict			=     super()._prepare_for_class(SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     return_labels=SCREAMING_SNAKE_CASE_   )

										if return_labels:
																if model_class in get_values(SCREAMING_SNAKE_CASE_   ):
																						snake_case							:  int			=     torch.zeros(
																						    (self.model_tester.batch_size, self.model_tester.seq_length),     dtype=torch.long,     device=SCREAMING_SNAKE_CASE_   )
										return inputs_dict




				def 						__snake_case				(				self						:					Dict   ):
										snake_case							:  Dict			=     ElectraModelTester(self   )
										snake_case							:  int			=     ConfigTester(self,     config_class=SCREAMING_SNAKE_CASE_,     hidden_size=37   )




				def 						__snake_case				(				self						:					Dict   ):
										self.config_tester.run_common_tests()




				def 						__snake_case				(				self						:					Tuple   ):
										snake_case							:  Optional[int]			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_model(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					str   ):
										snake_case							:  int			=     self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_electra_model_as_decoder(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					List[str]   ):
										snake_case							:  Tuple			=     self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
																snake_case							:  List[Any]			=     type
																self.model_tester.create_and_check_electra_model(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					str   ):
										snake_case							:  Dict			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_masked_lm(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					Optional[int]   ):
										snake_case							:  Optional[Any]			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_token_classification(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					List[str]   ):
										snake_case							:  Any			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_pretraining(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					int   ):
										snake_case							:  List[str]			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_sequence_classification(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					Optional[int]   ):
										snake_case							:  str			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_question_answering(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					Any   ):
										snake_case							:  Any			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_multiple_choice(*SCREAMING_SNAKE_CASE_   )




				@slow
				def 						__snake_case				(				self						:					Tuple   ):
										for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																snake_case							:  Any			=     ElectraModel.from_pretrained(SCREAMING_SNAKE_CASE_   )
																self.assertIsNotNone(SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					Optional[int]   ):
										snake_case							:  Optional[int]			=     self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_electra_for_causal_lm(*SCREAMING_SNAKE_CASE_   )




@require_torch
class     a (					unittest.TestCase     ):




				@slow
				def 						__snake_case				(				self						:					Optional[int]   ):
										snake_case							:  Optional[int]			=     ElectraModel.from_pretrained('''google/electra-small-discriminator'''   )
										snake_case							:  Optional[int]			=     torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]   )
										snake_case							:  List[str]			=     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
										snake_case							:  str			=     model(SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_   )[0]
										snake_case							:  int			=     torch.Size((1, 11, 2_56)   )
										self.assertEqual(output.shape,     SCREAMING_SNAKE_CASE_   )
										snake_case							:  Union[str, Any]			=     torch.tensor(
										    [[[0.4471, 0.6821, -0.3265], [0.4627, 0.5255, -0.3668], [0.4532, 0.3313, -0.4344]]]   )

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


'''simple docstring'''

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 				lowercase    (    __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 __UpperCAmelCase							( self					:						Optional[int])   ->       Union[str, Any]:
         return 32


   @property
   def __UpperCAmelCase							( self					:						List[str])   ->       int:
         return 32


   @property
   def __UpperCAmelCase							( self					:						Optional[Any])   ->       Any:
         return self.time_input_dim


   @property
   def __UpperCAmelCase							( self					:						List[str])   ->       str:
         return self.time_input_dim * 4


   @property
   def __UpperCAmelCase							( self					:						List[str])   ->       str:
         return 100


   @property
   def __UpperCAmelCase							( self					:						Tuple)   ->       List[str]:
         lowercase_     			=   XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base")
         return tokenizer


   @property
   def __UpperCAmelCase							( self					:						Any)   ->       Optional[int]:
         torch.manual_seed(0)
         lowercase_     			=   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 ,		)

         lowercase_     			=   MultilingualCLIP(__lowerCAmelCase)
         lowercase_     			=   text_encoder.eval()

         return text_encoder


   @property
   def __UpperCAmelCase							( self					:						Tuple)   ->       Any:
         torch.manual_seed(0)

         lowercase_     			=   {
             "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,
         }

         lowercase_     			=   UNetaDConditionModel(**__lowerCAmelCase)
         return model


   @property
   def __UpperCAmelCase							( self					:						int)   ->       Optional[int]:
         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 __UpperCAmelCase							( self					:						Optional[Any])   ->       Tuple:
         torch.manual_seed(0)
         lowercase_     			=   VQModel(**self.dummy_movq_kwargs)
         return model


   def __UpperCAmelCase							( self					:						Tuple)   ->       Union[str, Any]:
         lowercase_     			=   self.dummy_text_encoder
         lowercase_     			=   self.dummy_tokenizer
         lowercase_     			=   self.dummy_unet
         lowercase_     			=   self.dummy_movq

         lowercase_     			=   DDIMScheduler(
             num_train_timesteps=1000 ,		beta_schedule="linear" ,		beta_start=0.0_0085 ,		beta_end=0.012 ,		clip_sample=__lowerCAmelCase ,		set_alpha_to_one=__lowerCAmelCase ,		steps_offset=1 ,		prediction_type="epsilon" ,		thresholding=__lowerCAmelCase ,		)

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


   def __UpperCAmelCase							( self					:						Union[str, Any] ,		__lowerCAmelCase					:						Optional[Any] ,		__lowerCAmelCase					:						List[str]=0)   ->       List[Any]:
         lowercase_     			=   floats_tensor((1, self.cross_attention_dim) ,		rng=random.Random(__lowerCAmelCase)).to(__lowerCAmelCase)
         lowercase_     			=   floats_tensor((1, self.cross_attention_dim) ,		rng=random.Random(seed + 1)).to(__lowerCAmelCase)
         if str(__lowerCAmelCase).startswith("mps"):
               lowercase_     			=   torch.manual_seed(__lowerCAmelCase)
         else:
               lowercase_     			=   torch.Generator(device=__lowerCAmelCase).manual_seed(__lowerCAmelCase)
         lowercase_     			=   {
             "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 __UpperCAmelCase							( self					:						Any)   ->       Optional[int]:
         lowercase_     			=   "cpu"

         lowercase_     			=   self.get_dummy_components()

         lowercase_     			=   self.pipeline_class(**__lowerCAmelCase)
         lowercase_     			=   pipe.to(__lowerCAmelCase)

         pipe.set_progress_bar_config(disable=__lowerCAmelCase)

         lowercase_     			=   pipe(**self.get_dummy_inputs(__lowerCAmelCase))
         lowercase_     			=   output.images

         lowercase_     			=   pipe(
             **self.get_dummy_inputs(__lowerCAmelCase) ,		return_dict=__lowerCAmelCase ,		)[0]

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

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

         lowercase_     			=   np.array(
             [0.32_8663, 1.0, 0.2321_6873, 1.0, 0.9271_7564, 0.463_9046, 0.9689_4777, 0.3171_3378, 0.629_3953])

         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 				lowercase    (    unittest.TestCase			):


   def __UpperCAmelCase							( self					:						str)   ->       Tuple:
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()


   def __UpperCAmelCase							( self					:						List[str])   ->       Tuple:
         lowercase_     			=   load_numpy(
             "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
             "/kandinsky/kandinsky_text2img_cat_fp16.npy")

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

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

         lowercase_     			=   "red cat, 4k photo"

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

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

         lowercase_     			=   output.images[0]

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

         assert_mean_pixel_difference(__lowerCAmelCase ,		__lowerCAmelCase)






