

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



'''simple docstring'''
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionImageVariationPipeline
from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device


lowerCamelCase :      Optional[int]					 =     False







class 							A__    (       unittest.TestCase  ):
  pass







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





  def 						A  (		self      :	Tuple )	->    Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =VersatileDiffusionImageVariationPipeline.from_pretrained('shi-labs/versatile-diffusion' )
       pipe.to(_a )
       pipe.set_progress_bar_config(disable=_a )

       _SCREAMING_SNAKE_CASE							    =load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' )
       _SCREAMING_SNAKE_CASE							    =torch.manual_seed(0 )
       _SCREAMING_SNAKE_CASE							    =pipe(
           image=_a							,					generator=_a							,					guidance_scale=7.5							,					num_inference_steps=50							,					output_type='numpy'							,					).images

       _SCREAMING_SNAKE_CASE							    =image[0, 253:256, 253:256, -1]

       assert image.shape == (1, 512, 512, 3)
       _SCREAMING_SNAKE_CASE							    =np.array([0.04_41, 0.04_69, 0.05_07, 0.05_75, 0.06_32, 0.06_50, 0.08_65, 0.09_09, 0.09_45] )

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