






import unittest

from transformers import MPNetConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    MPNetForMaskedLM,
		    MPNetForMultipleChoice,
		    MPNetForQuestionAnswering,
		    MPNetForSequenceClassification,
		    MPNetForTokenClassification,
		    MPNetModel,
		)



class   lowerCamelCase      :







		'''simple docstring'''



		def __init__(							self				,		_UpperCamelCase				,		_UpperCamelCase=1_3				,		_UpperCamelCase=7				,		_UpperCamelCase=True				,		_UpperCamelCase=True				,		_UpperCamelCase=False				,		_UpperCamelCase=True				,		_UpperCamelCase=9_9				,		_UpperCamelCase=6_4				,		_UpperCamelCase=5				,		_UpperCamelCase=4				,		_UpperCamelCase=6_4				,		_UpperCamelCase="gelu"				,		_UpperCamelCase=0.1				,		_UpperCamelCase=0.1				,		_UpperCamelCase=5_1_2				,		_UpperCamelCase=1_6				,		_UpperCamelCase=2				,		_UpperCamelCase=0.02				,		_UpperCamelCase=3				,		_UpperCamelCase=4				,		_UpperCamelCase=None				,		)	->							Union[str, Any]:
							UpperCAmelCase_    :      Any            =						parent
							UpperCAmelCase_    :      Union[str, Any]            =						batch_size
							UpperCAmelCase_    :      List[Any]            =						seq_length
							UpperCAmelCase_    :      Optional[Any]            =						is_training
							UpperCAmelCase_    :      Any            =						use_input_mask
							UpperCAmelCase_    :      Optional[Any]            =						use_token_type_ids
							UpperCAmelCase_    :      Union[str, Any]            =						use_labels
							UpperCAmelCase_    :      List[str]            =						vocab_size
							UpperCAmelCase_    :      Dict            =						hidden_size
							UpperCAmelCase_    :      Tuple            =						num_hidden_layers
							UpperCAmelCase_    :      Optional[int]            =						num_attention_heads
							UpperCAmelCase_    :      Optional[Any]            =						intermediate_size
							UpperCAmelCase_    :      Optional[Any]            =						hidden_act
							UpperCAmelCase_    :      Union[str, Any]            =						hidden_dropout_prob
							UpperCAmelCase_    :      Optional[Any]            =						attention_probs_dropout_prob
							UpperCAmelCase_    :      Tuple            =						max_position_embeddings
							UpperCAmelCase_    :      int            =						type_vocab_size
							UpperCAmelCase_    :      int            =						type_sequence_label_size
							UpperCAmelCase_    :      Any            =						initializer_range
							UpperCAmelCase_    :      List[Any]            =						num_labels
							UpperCAmelCase_    :      List[Any]            =						num_choices
							UpperCAmelCase_    :      Any            =						scope



		def 					__UpperCAmelCase			(							self   )	->							List[str]:
							return MPNetConfig.from_pretrained('microsoft/mpnet-base'   )



		def 					__UpperCAmelCase			(							self   )	->							Optional[Any]:
							UpperCAmelCase_    :      Dict            =						ids_tensor([self.batch_size, self.seq_length]				,		self.vocab_size   )

							UpperCAmelCase_    :      Optional[Any]            =						None
							if self.use_input_mask:
												UpperCAmelCase_    :      Tuple            =						random_attention_mask([self.batch_size, self.seq_length]   )

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

							UpperCAmelCase_    :      Dict            =						self.get_config()
							return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



		def 					__UpperCAmelCase			(							self   )	->							Dict:
							return MPNetConfig(
							    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				,		initializer_range=self.initializer_range				,		)



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase   )	->							Optional[Any]:
							UpperCAmelCase_    :      Optional[int]            =						MPNetModel(config=_UpperCamelCase   )
							model.to(_UpperCamelCase   )
							model.eval()
							UpperCAmelCase_    :      List[Any]            =						model(_UpperCamelCase				,		_UpperCamelCase   )
							UpperCAmelCase_    :      str            =						model(_UpperCamelCase   )
							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 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase   )	->							Optional[Any]:
							UpperCAmelCase_    :      Any            =						MPNetForQuestionAnswering(config=_UpperCamelCase   )
							model.to(_UpperCamelCase   )
							model.eval()
							UpperCAmelCase_    :      Dict            =						model(
							    _UpperCamelCase				,		attention_mask=_UpperCamelCase				,		start_positions=_UpperCamelCase				,		end_positions=_UpperCamelCase				,		)
							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 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase   )	->							Dict:
							UpperCAmelCase_    :      Optional[int]            =						self.num_labels
							UpperCAmelCase_    :      Dict            =						MPNetForSequenceClassification(_UpperCamelCase   )
							model.to(_UpperCamelCase   )
							model.eval()
							UpperCAmelCase_    :      str            =						model(_UpperCamelCase				,		attention_mask=_UpperCamelCase				,		labels=_UpperCamelCase   )
							self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.num_labels)   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase   )	->							Optional[Any]:
							UpperCAmelCase_    :      Tuple            =						self.num_choices
							UpperCAmelCase_    :      Tuple            =						MPNetForMultipleChoice(config=_UpperCamelCase   )
							model.to(_UpperCamelCase   )
							model.eval()
							UpperCAmelCase_    :      Union[str, Any]            =						input_ids.unsqueeze(1   ).expand(-1				,		self.num_choices				,		-1   ).contiguous()
							UpperCAmelCase_    :      List[Any]            =						input_mask.unsqueeze(1   ).expand(-1				,		self.num_choices				,		-1   ).contiguous()
							UpperCAmelCase_    :      Dict            =						model(
							    _UpperCamelCase				,		attention_mask=_UpperCamelCase				,		labels=_UpperCamelCase				,		)
							self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.num_choices)   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase   )	->							Optional[Any]:
							UpperCAmelCase_    :      Tuple            =						self.num_labels
							UpperCAmelCase_    :      Union[str, Any]            =						MPNetForTokenClassification(config=_UpperCamelCase   )
							model.to(_UpperCamelCase   )
							model.eval()
							UpperCAmelCase_    :      Optional[int]            =						model(_UpperCamelCase				,		attention_mask=_UpperCamelCase				,		labels=_UpperCamelCase   )
							self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.seq_length, self.num_labels)   )



		def 					__UpperCAmelCase			(							self   )	->							Dict:
							UpperCAmelCase_    :      Union[str, Any]            =						self.prepare_config_and_inputs()
							((UpperCAmelCase_)       ,  (UpperCAmelCase_)       ,  (UpperCAmelCase_)       ,  (UpperCAmelCase_)       ,  (UpperCAmelCase_)       ,  (UpperCAmelCase_))    :      Optional[int]            =						config_and_inputs
							UpperCAmelCase_    :      int            =						{'input_ids': input_ids, 'attention_mask': input_mask}
							return config, inputs_dict





@require_torch
class   lowerCamelCase      (_snake_case ,  _snake_case ,  unittest.TestCase			):







		'''simple docstring'''



		_snake_case    :    Optional[Any]       						=  (
		    (
		        MPNetForMaskedLM,
		        MPNetForMultipleChoice,
		        MPNetForQuestionAnswering,
		        MPNetForSequenceClassification,
		        MPNetForTokenClassification,
		        MPNetModel,
		    )
		    if is_torch_available()
		    else ()
		)
		_snake_case    :    Optional[Any]       						=  (
		    {
		        '''feature-extraction''': MPNetModel,
		        '''fill-mask''': MPNetForMaskedLM,
		        '''question-answering''': MPNetForQuestionAnswering,
		        '''text-classification''': MPNetForSequenceClassification,
		        '''token-classification''': MPNetForTokenClassification,
		        '''zero-shot''': MPNetForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		_snake_case    :    Union[str, Any]       						=  False
		_snake_case    :    Dict       						=  True



		def 					__UpperCAmelCase			(							self   )	->							List[str]:
							UpperCAmelCase_    :      Union[str, Any]            =						MPNetModelTester(self   )
							UpperCAmelCase_    :      Optional[int]            =						ConfigTester(self				,		config_class=_UpperCamelCase				,		hidden_size=3_7   )



		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							self.config_tester.run_common_tests()



		def 					__UpperCAmelCase			(							self   )	->							Any:
							UpperCAmelCase_    :      Dict            =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_model(*_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self   )	->							str:
							UpperCAmelCase_    :      List[str]            =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_sequence_classification(*_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self   )	->							Optional[int]:
							UpperCAmelCase_    :      Optional[int]            =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_multiple_choice(*_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self   )	->							Tuple:
							UpperCAmelCase_    :      int            =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_token_classification(*_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							UpperCAmelCase_    :      int            =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_question_answering(*_UpperCamelCase   )





@require_torch
class   lowerCamelCase      (unittest.TestCase			):







		'''simple docstring'''



		@slow
		def 					__UpperCAmelCase			(							self   )	->							Optional[int]:
							UpperCAmelCase_    :      Optional[int]            =						MPNetModel.from_pretrained('microsoft/mpnet-base'   )
							UpperCAmelCase_    :      Optional[Any]            =						torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]   )
							UpperCAmelCase_    :      Union[str, Any]            =						model(_UpperCamelCase   )[0]
							UpperCAmelCase_    :      int            =						torch.Size((1, 1_1, 7_6_8)   )
							self.assertEqual(output.shape				,		_UpperCamelCase   )
							UpperCAmelCase_    :      Tuple            =						torch.tensor(
							    [[[-0.05_50, 0.19_43, -0.07_40], [-0.05_62, 0.22_11, -0.05_79], [-0.04_37, 0.33_37, -0.06_41]]]   )
							# compare the actual values for a slice.
							self.assertTrue(torch.allclose(output[:, :3, :3]				,		_UpperCamelCase				,		atol=1E-4   )   )














import random
import unittest

import torch

from diffusers import IFImgaImgSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin



@skip_mps
class   lowerCamelCase      (_snake_case ,  _snake_case ,  unittest.TestCase			):







		'''simple docstring'''



		_snake_case    :    Union[str, Any]       						=  IFImgaImgSuperResolutionPipeline
		_snake_case    :    Union[str, Any]       						=  TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''width''', '''height'''}
		_snake_case    :    List[Any]       						=  TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({'''original_image'''}			)
		_snake_case    :    List[str]       						=  PipelineTesterMixin.required_optional_params - {'''latents'''}



		def 					__UpperCAmelCase			(							self   )	->							Optional[Any]:
							return self._get_superresolution_dummy_components()



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase=0   )	->							Any:
							if str(_UpperCamelCase   ).startswith('mps'   ):
												UpperCAmelCase_    :      List[Any]            =						torch.manual_seed(_UpperCamelCase   )
							else:
												UpperCAmelCase_    :      int            =						torch.Generator(device=_UpperCamelCase   ).manual_seed(_UpperCamelCase   )

							UpperCAmelCase_    :      List[Any]            =						floats_tensor((1, 3, 3_2, 3_2)				,		rng=random.Random(_UpperCamelCase   )   ).to(_UpperCamelCase   )
							UpperCAmelCase_    :      Dict            =						floats_tensor((1, 3, 1_6, 1_6)				,		rng=random.Random(_UpperCamelCase   )   ).to(_UpperCamelCase   )

							UpperCAmelCase_    :      Tuple            =						{
							    'prompt': 'A painting of a squirrel eating a burger',
							    'image': image,
							    'original_image': original_image,
							    'generator': generator,
							    'num_inference_steps': 2,
							    'output_type': 'numpy',
							}

							return inputs



		@unittest.skipIf(
		    torch_device != 'cuda' or not is_xformers_available()				,		reason='XFormers attention is only available with CUDA and `xformers` installed'				,		)
		def 					__UpperCAmelCase			(							self   )	->							Any:
							self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3   )



		def 					__UpperCAmelCase			(							self   )	->							Dict:
							self._test_save_load_optional_components()



		@unittest.skipIf(torch_device != 'cuda'				,		reason='float16 requires CUDA'   )
		def 					__UpperCAmelCase			(							self   )	->							str:
							# Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
							super().test_save_load_floataa(expected_max_diff=1E-1   )



		def 					__UpperCAmelCase			(							self   )	->							List[Any]:
							self._test_attention_slicing_forward_pass(expected_max_diff=1E-2   )



		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							self._test_save_load_local()



		def 					__UpperCAmelCase			(							self   )	->							Dict:
							self._test_inference_batch_single_identical(
							    expected_max_diff=1E-2				,		)






