

'''simple docstring'''
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 							A__    :





  def __init__(		self      :	int							,					_a      :	Optional[int]							,					_a      :	Optional[Any]=13							,					_a      :	List[Any]=7							,					_a      :	List[Any]=True							,					_a      :	str=True							,					_a      :	Dict=False							,					_a      :	str=True							,					_a      :	List[str]=99							,					_a      :	str=64							,					_a      :	Union[str, Any]=5							,					_a      :	List[Any]=4							,					_a      :	Optional[Any]=64							,					_a      :	Tuple="gelu"							,					_a      :	List[str]=0.1							,					_a      :	str=0.1							,					_a      :	int=512							,					_a      :	Dict=16							,					_a      :	List[str]=2							,					_a      :	int=0.02							,					_a      :	Union[str, Any]=3							,					_a      :	str=4							,					_a      :	Tuple=None							,					)	->    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_input_mask
       _SCREAMING_SNAKE_CASE							    =use_token_type_ids
       _SCREAMING_SNAKE_CASE							    =use_labels
       _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							    =hidden_act
       _SCREAMING_SNAKE_CASE							    =hidden_dropout_prob
       _SCREAMING_SNAKE_CASE							    =attention_probs_dropout_prob
       _SCREAMING_SNAKE_CASE							    =max_position_embeddings
       _SCREAMING_SNAKE_CASE							    =type_vocab_size
       _SCREAMING_SNAKE_CASE							    =type_sequence_label_size
       _SCREAMING_SNAKE_CASE							    =initializer_range
       _SCREAMING_SNAKE_CASE							    =num_labels
       _SCREAMING_SNAKE_CASE							    =num_choices
       _SCREAMING_SNAKE_CASE							    =scope





  def 						A  (		self      :	str )	->    Tuple:





       '''simple docstring'''






       return MPNetConfig.from_pretrained('microsoft/mpnet-base' )





  def 						A  (		self      :	int )	->    int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size )

       _SCREAMING_SNAKE_CASE							    =None
       if self.use_input_mask:
            _SCREAMING_SNAKE_CASE							    =random_attention_mask([self.batch_size, self.seq_length] )

       _SCREAMING_SNAKE_CASE							    =None
       _SCREAMING_SNAKE_CASE							    =None
       _SCREAMING_SNAKE_CASE							    =None
       if self.use_labels:
            _SCREAMING_SNAKE_CASE							    =ids_tensor([self.batch_size]							,					self.type_sequence_label_size )
            _SCREAMING_SNAKE_CASE							    =ids_tensor([self.batch_size, self.seq_length]							,					self.num_labels )
            _SCREAMING_SNAKE_CASE							    =ids_tensor([self.batch_size]							,					self.num_choices )

       _SCREAMING_SNAKE_CASE							    =self.get_config()
       return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels





  def 						A  (		self      :	Dict )	->    Dict:





       '''simple docstring'''






       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 						A  (		self      :	Tuple							,					_a      :	Dict							,					_a      :	Dict							,					_a      :	Dict							,					_a      :	int							,					_a      :	Optional[int]							,					_a      :	Any )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =MPNetModel(config=_a )
       model.to(_a )
       model.eval()
       _SCREAMING_SNAKE_CASE							    =model(_a							,					_a )
       _SCREAMING_SNAKE_CASE							    =model(_a )
       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      :	Union[str, Any]							,					_a      :	List[Any]							,					_a      :	Dict							,					_a      :	List[Any]							,					_a      :	Any							,					_a      :	List[Any]							,					_a      :	Dict )	->    Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =MPNetForQuestionAnswering(config=_a )
       model.to(_a )
       model.eval()
       _SCREAMING_SNAKE_CASE							    =model(
           _a							,					attention_mask=_a							,					start_positions=_a							,					end_positions=_a							,					)
       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      :	Tuple							,					_a      :	Any							,					_a      :	List[Any]							,					_a      :	List[Any]							,					_a      :	Dict							,					_a      :	List[str]							,					_a      :	List[Any] )	->    List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.num_labels
       _SCREAMING_SNAKE_CASE							    =MPNetForSequenceClassification(_a )
       model.to(_a )
       model.eval()
       _SCREAMING_SNAKE_CASE							    =model(_a							,					attention_mask=_a							,					labels=_a )
       self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.num_labels) )





  def 						A  (		self      :	Any							,					_a      :	int							,					_a      :	Optional[Any]							,					_a      :	List[Any]							,					_a      :	Tuple							,					_a      :	Tuple							,					_a      :	int )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.num_choices
       _SCREAMING_SNAKE_CASE							    =MPNetForMultipleChoice(config=_a )
       model.to(_a )
       model.eval()
       _SCREAMING_SNAKE_CASE							    =input_ids.unsqueeze(1 ).expand(-1							,					self.num_choices							,					-1 ).contiguous()
       _SCREAMING_SNAKE_CASE							    =input_mask.unsqueeze(1 ).expand(-1							,					self.num_choices							,					-1 ).contiguous()
       _SCREAMING_SNAKE_CASE							    =model(
           _a							,					attention_mask=_a							,					labels=_a							,					)
       self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.num_choices) )





  def 						A  (		self      :	Any							,					_a      :	int							,					_a      :	Optional[Any]							,					_a      :	Optional[Any]							,					_a      :	str							,					_a      :	List[Any]							,					_a      :	Any )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.num_labels
       _SCREAMING_SNAKE_CASE							    =MPNetForTokenClassification(config=_a )
       model.to(_a )
       model.eval()
       _SCREAMING_SNAKE_CASE							    =model(_a							,					attention_mask=_a							,					labels=_a )
       self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.seq_length, self.num_labels) )





  def 						A  (		self      :	Any )	->    List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.prepare_config_and_inputs()
       ((_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE))							    =config_and_inputs
       _SCREAMING_SNAKE_CASE							    ={'input_ids': input_ids, 'attention_mask': input_mask}
       return config, inputs_dict







@require_torch
class 							A__    (       A__     ,   A__     ,   unittest.TestCase  ):
  A__		 =				(
      (
          MPNetForMaskedLM,
          MPNetForMultipleChoice,
          MPNetForQuestionAnswering,
          MPNetForSequenceClassification,
          MPNetForTokenClassification,
          MPNetModel,
      )
      if is_torch_available()
      else ()
  )
  A__		 =				(
      {
          'feature-extraction': MPNetModel,
          'fill-mask': MPNetForMaskedLM,
          'question-answering': MPNetForQuestionAnswering,
          'text-classification': MPNetForSequenceClassification,
          'token-classification': MPNetForTokenClassification,
          'zero-shot': MPNetForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  A__		 =				False
  A__		 =				True





  def 						A  (		self      :	int )	->    Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =MPNetModelTester(self )
       _SCREAMING_SNAKE_CASE							    =ConfigTester(self							,					config_class=_a							,					hidden_size=37 )





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





       '''simple docstring'''






       self.config_tester.run_common_tests()





  def 						A  (		self      :	Any )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_model(*_a )





  def 						A  (		self      :	List[Any] )	->    List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_sequence_classification(*_a )





  def 						A  (		self      :	Optional[int] )	->    str:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_multiple_choice(*_a )





  def 						A  (		self      :	Any )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_token_classification(*_a )





  def 						A  (		self      :	str )	->    str:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_question_answering(*_a )







@require_torch
class 							A__    (       unittest.TestCase  ):





  @slow
  def 						A  (		self      :	Optional[Any] )	->    List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =MPNetModel.from_pretrained('microsoft/mpnet-base' )
       _SCREAMING_SNAKE_CASE							    =torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] )
       _SCREAMING_SNAKE_CASE							    =model(_a )[0]
       _SCREAMING_SNAKE_CASE							    =torch.Size((1, 11, 768) )
       self.assertEqual(output.shape							,					_a )
       _SCREAMING_SNAKE_CASE							    =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]							,					_a							,					atol=1e-4 ) )






import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()


class  A		(  unittest.TestCase     ):



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								torch.manual_seed(0   )
								lowerCAmelCase_      			=  UNetaDModel(
								    block_out_channels=(32, 64),	layers_per_block=2,	sample_size=32,	in_channels=3,	out_channels=3,	down_block_types=('''DownBlock2D''', '''AttnDownBlock2D'''),	up_block_types=('''AttnUpBlock2D''', '''UpBlock2D'''),	)
								return model







		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								lowerCAmelCase_      			=  self.dummy_uncond_unet
								lowerCAmelCase_      			=  KarrasVeScheduler()

								lowerCAmelCase_      			=  KarrasVePipeline(unet=UpperCamelCase__,	scheduler=UpperCamelCase__   )
								pipe.to(UpperCamelCase__   )
								pipe.set_progress_bar_config(disable=UpperCamelCase__   )

								lowerCAmelCase_      			=  torch.manual_seed(0   )
								lowerCAmelCase_      			=  pipe(num_inference_steps=2,	generator=UpperCamelCase__,	output_type='''numpy'''   ).images

								lowerCAmelCase_      			=  torch.manual_seed(0   )
								lowerCAmelCase_      			=  pipe(num_inference_steps=2,	generator=UpperCamelCase__,	output_type='''numpy''',	return_dict=UpperCamelCase__   )[0]

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

								assert image.shape == (1, 32, 32, 3)
								lowerCAmelCase_      			=  np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]   )

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







@slow
@require_torch
class  A		(  unittest.TestCase     ):



		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								lowerCAmelCase_      			=  '''google/ncsnpp-celebahq-256'''
								lowerCAmelCase_      			=  UNetaDModel.from_pretrained(UpperCamelCase__   )
								lowerCAmelCase_      			=  KarrasVeScheduler()

								lowerCAmelCase_      			=  KarrasVePipeline(unet=UpperCamelCase__,	scheduler=UpperCamelCase__   )
								pipe.to(UpperCamelCase__   )
								pipe.set_progress_bar_config(disable=UpperCamelCase__   )

								lowerCAmelCase_      			=  torch.manual_seed(0   )
								lowerCAmelCase_      			=  pipe(num_inference_steps=20,	generator=UpperCamelCase__,	output_type='''numpy'''   ).images

								lowerCAmelCase_      			=  image[0, -3:, -3:, -1]
								assert image.shape == (1, 256, 256, 3)
								lowerCAmelCase_      			=  np.array([0.578, 0.5_811, 0.5_924, 0.5_809, 0.587, 0.5_886, 0.5_861, 0.5_802, 0.586]   )

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




