

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__				:







  """simple docstring"""




  def __init__(	self			:       int , UpperCAmelCase__			:       Optional[int] , UpperCAmelCase__			:       Optional[Any]=1_3 , UpperCAmelCase__			:       List[Any]=7 , UpperCAmelCase__			:       List[Any]=True , UpperCAmelCase__			:       str=True , UpperCAmelCase__			:       Dict=False , UpperCAmelCase__			:       str=True , UpperCAmelCase__			:       List[str]=9_9 , UpperCAmelCase__			:       str=6_4 , UpperCAmelCase__			:       Union[str, Any]=5 , UpperCAmelCase__			:       List[Any]=4 , UpperCAmelCase__			:       Optional[Any]=6_4 , UpperCAmelCase__			:       Tuple="gelu" , UpperCAmelCase__			:       List[str]=0.1 , UpperCAmelCase__			:       str=0.1 , UpperCAmelCase__			:       int=5_1_2 , UpperCAmelCase__			:       Dict=1_6 , UpperCAmelCase__			:       List[str]=2 , UpperCAmelCase__			:       int=0.02 , UpperCAmelCase__			:       Union[str, Any]=3 , UpperCAmelCase__			:       str=4 , UpperCAmelCase__			:       Tuple=None , )    ->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					List[Any]        =      parent
        SCREAMING_SNAKE_CASE       :					Tuple        =      batch_size
        SCREAMING_SNAKE_CASE       :					List[Any]        =      seq_length
        SCREAMING_SNAKE_CASE       :					Optional[Any]        =      is_training
        SCREAMING_SNAKE_CASE       :					Tuple        =      use_input_mask
        SCREAMING_SNAKE_CASE       :					List[Any]        =      use_token_type_ids
        SCREAMING_SNAKE_CASE       :					Any        =      use_labels
        SCREAMING_SNAKE_CASE       :					int        =      vocab_size
        SCREAMING_SNAKE_CASE       :					int        =      hidden_size
        SCREAMING_SNAKE_CASE       :					Tuple        =      num_hidden_layers
        SCREAMING_SNAKE_CASE       :					Any        =      num_attention_heads
        SCREAMING_SNAKE_CASE       :					Dict        =      intermediate_size
        SCREAMING_SNAKE_CASE       :					List[str]        =      hidden_act
        SCREAMING_SNAKE_CASE       :					List[str]        =      hidden_dropout_prob
        SCREAMING_SNAKE_CASE       :					Union[str, Any]        =      attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE       :					int        =      max_position_embeddings
        SCREAMING_SNAKE_CASE       :					List[str]        =      type_vocab_size
        SCREAMING_SNAKE_CASE       :					Optional[int]        =      type_sequence_label_size
        SCREAMING_SNAKE_CASE       :					Dict        =      initializer_range
        SCREAMING_SNAKE_CASE       :					List[Any]        =      num_labels
        SCREAMING_SNAKE_CASE       :					Tuple        =      num_choices
        SCREAMING_SNAKE_CASE       :					Tuple        =      scope


  def        _lowercase					(	self			:       str						)    ->Tuple:
        """simple docstring"""







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


  def        _lowercase					(	self			:       int						)    ->int:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					Tuple        =      ids_tensor([self.batch_size, self.seq_length] , self.vocab_size						)

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

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

        SCREAMING_SNAKE_CASE       :					List[str]        =      self.get_config()
        return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels


  def        _lowercase					(	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        _lowercase					(	self			:       Tuple , UpperCAmelCase__			:       Dict , UpperCAmelCase__			:       Dict , UpperCAmelCase__			:       Dict , UpperCAmelCase__			:       int , UpperCAmelCase__			:       Optional[int] , UpperCAmelCase__			:       Any						)    ->Tuple:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					Union[str, Any]        =      MPNetModel(config=_a						)
        model.to(_a						)
        model.eval()
        SCREAMING_SNAKE_CASE       :					int        =      model(_a , _a						)
        SCREAMING_SNAKE_CASE       :					List[Any]        =      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        _lowercase					(	self			:       Union[str, Any] , UpperCAmelCase__			:       List[Any] , UpperCAmelCase__			:       Dict , UpperCAmelCase__			:       List[Any] , UpperCAmelCase__			:       Any , UpperCAmelCase__			:       List[Any] , UpperCAmelCase__			:       Dict						)    ->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					List[str]        =      MPNetForQuestionAnswering(config=_a						)
        model.to(_a						)
        model.eval()
        SCREAMING_SNAKE_CASE       :					Union[str, Any]        =      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        _lowercase					(	self			:       Tuple , UpperCAmelCase__			:       Any , UpperCAmelCase__			:       List[Any] , UpperCAmelCase__			:       List[Any] , UpperCAmelCase__			:       Dict , UpperCAmelCase__			:       List[str] , UpperCAmelCase__			:       List[Any]						)    ->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					List[str]        =      self.num_labels
        SCREAMING_SNAKE_CASE       :					List[str]        =      MPNetForSequenceClassification(_a						)
        model.to(_a						)
        model.eval()
        SCREAMING_SNAKE_CASE       :					Optional[Any]        =      model(_a , attention_mask=_a , labels=_a						)
        self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)						)


  def        _lowercase					(	self			:       Any , UpperCAmelCase__			:       int , UpperCAmelCase__			:       Optional[Any] , UpperCAmelCase__			:       List[Any] , UpperCAmelCase__			:       Tuple , UpperCAmelCase__			:       Tuple , UpperCAmelCase__			:       int						)    ->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					Optional[int]        =      self.num_choices
        SCREAMING_SNAKE_CASE       :					Tuple        =      MPNetForMultipleChoice(config=_a						)
        model.to(_a						)
        model.eval()
        SCREAMING_SNAKE_CASE       :					Optional[int]        =      input_ids.unsqueeze(1						).expand(-1 , self.num_choices , -1						).contiguous()
        SCREAMING_SNAKE_CASE       :					Tuple        =      input_mask.unsqueeze(1						).expand(-1 , self.num_choices , -1						).contiguous()
        SCREAMING_SNAKE_CASE       :					Dict        =      model(
            _a , attention_mask=_a , labels=_a , )
        self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices)						)


  def        _lowercase					(	self			:       Any , UpperCAmelCase__			:       int , UpperCAmelCase__			:       Optional[Any] , UpperCAmelCase__			:       Optional[Any] , UpperCAmelCase__			:       str , UpperCAmelCase__			:       List[Any] , UpperCAmelCase__			:       Any						)    ->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					Any        =      self.num_labels
        SCREAMING_SNAKE_CASE       :					List[str]        =      MPNetForTokenClassification(config=_a						)
        model.to(_a						)
        model.eval()
        SCREAMING_SNAKE_CASE       :					Optional[int]        =      model(_a , attention_mask=_a , labels=_a						)
        self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)						)




  def        _lowercase					(	self			:       Any						)    ->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					str        =      self.prepare_config_and_inputs()
        ((SCREAMING_SNAKE_CASE)   ,		(SCREAMING_SNAKE_CASE)   ,		(SCREAMING_SNAKE_CASE)   ,		(SCREAMING_SNAKE_CASE)   ,		(SCREAMING_SNAKE_CASE)   ,		(SCREAMING_SNAKE_CASE))       :					Tuple        =      config_and_inputs
        SCREAMING_SNAKE_CASE       :					Tuple        =      {"""input_ids""": input_ids, """attention_mask""": input_mask}
        return config, inputs_dict







@require_torch
class 				a__				(      A__		,	A__		,	unittest.TestCase				):







  """simple docstring"""




  UpperCAmelCase__					:       int					=(
      (
          MPNetForMaskedLM,
          MPNetForMultipleChoice,
          MPNetForQuestionAnswering,
          MPNetForSequenceClassification,
          MPNetForTokenClassification,
          MPNetModel,
      )
      if is_torch_available()
      else ()
  )
  UpperCAmelCase__					:       Tuple					=(
      {
          """feature-extraction""": MPNetModel,
          """fill-mask""": MPNetForMaskedLM,
          """question-answering""": MPNetForQuestionAnswering,
          """text-classification""": MPNetForSequenceClassification,
          """token-classification""": MPNetForTokenClassification,
          """zero-shot""": MPNetForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  UpperCAmelCase__					:       List[Any]					=False
  UpperCAmelCase__					:       List[Any]					=True


  def        _lowercase					(	self			:       int						)    ->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					Any        =      MPNetModelTester(self						)
        SCREAMING_SNAKE_CASE       :					Union[str, Any]        =      ConfigTester(self , config_class=_a , hidden_size=3_7						)


  def        _lowercase					(	self			:       List[Any]						)    ->int:
        """simple docstring"""







        self.config_tester.run_common_tests()


  def        _lowercase					(	self			:       Any						)    ->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					int        =      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_model(*_a						)


  def        _lowercase					(	self			:       List[Any]						)    ->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					Tuple        =      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_sequence_classification(*_a						)


  def        _lowercase					(	self			:       Optional[int]						)    ->str:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					Dict        =      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_multiple_choice(*_a						)


  def        _lowercase					(	self			:       Any						)    ->Tuple:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					int        =      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_token_classification(*_a						)




  def        _lowercase					(	self			:       str						)    ->str:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					Union[str, Any]        =      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_question_answering(*_a						)







@require_torch
class 				a__				(      unittest.TestCase				):







  """simple docstring"""




  @slow
  def        _lowercase					(	self			:       Optional[Any]						)    ->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       :					List[Any]        =      MPNetModel.from_pretrained("""microsoft/mpnet-base"""						)
        SCREAMING_SNAKE_CASE       :					Optional[int]        =      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]]						)
        SCREAMING_SNAKE_CASE       :					Dict        =      model(_a						)[0]
        SCREAMING_SNAKE_CASE       :					str        =      torch.Size((1, 1_1, 7_6_8)						)
        self.assertEqual(output.shape , _a						)
        SCREAMING_SNAKE_CASE       :					int        =      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 importlib

import torch
import yaml
from omegaconf import OmegaConf
from taming.models.vqgan import VQModel


def        _lowerCAmelCase					(						_UpperCamelCase						:     Dict							, _UpperCamelCase						:     Any=False				)      ->			Optional[Any]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =OmegaConf.load(_UpperCamelCase				)
     if display:
          print(yaml.dump(OmegaConf.to_container(_UpperCamelCase				)				)				)
     return config


def        _lowerCAmelCase					(						_UpperCamelCase						:     Tuple							, _UpperCamelCase						:     Optional[Any]=None							, _UpperCamelCase						:     Union[str, Any]=None				)      ->			Optional[Any]:
     """simple docstring"""




     if conf_path is None:
          _SCREAMING_SNAKE_CASE							    ='./model_checkpoints/vqgan_only.yaml'
     _SCREAMING_SNAKE_CASE							    =load_config(_UpperCamelCase							, display=_UpperCamelCase				)
     _SCREAMING_SNAKE_CASE							    =VQModel(**config.model.params				)
     if ckpt_path is None:
          _SCREAMING_SNAKE_CASE							    ='./model_checkpoints/vqgan_only.pt'
     _SCREAMING_SNAKE_CASE							    =torch.load(_UpperCamelCase							, map_location=_UpperCamelCase				)
     if ".ckpt" in ckpt_path:
          _SCREAMING_SNAKE_CASE							    =sd['state_dict']
     model.load_state_dict(_UpperCamelCase							, strict=_UpperCamelCase				)
     model.to(_UpperCamelCase				)
     del sd
     return model


def        _lowerCAmelCase					(						_UpperCamelCase						:     Tuple							, _UpperCamelCase						:     int				)      ->			Dict:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE							    =model.encode(_UpperCamelCase				)
     print(f"VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}"				)
     _SCREAMING_SNAKE_CASE							    =model.decode(_UpperCamelCase				)
     return xrec


def        _lowerCAmelCase					(						_UpperCamelCase						:     Dict							, _UpperCamelCase						:     List[str]=False				)      ->			int:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE							    =string.rsplit('.'							, 1				)
     if reload:
          _SCREAMING_SNAKE_CASE							    =importlib.import_module(_UpperCamelCase				)
          importlib.reload(_UpperCamelCase				)
     return getattr(importlib.import_module(_UpperCamelCase							, package=_UpperCamelCase				)							, cls				)


def        _lowerCAmelCase					(						_UpperCamelCase						:     str				)      ->			List[str]:
     """simple docstring"""




     if "target" not in config:
          raise KeyError('Expected key `target` to instantiate.'				)
     return get_obj_from_str(config['target']				)(**config.get('params'							, {}				)				)


def        _lowerCAmelCase					(						_UpperCamelCase						:     Tuple							, _UpperCamelCase						:     List[Any]							, _UpperCamelCase						:     List[str]=True							, _UpperCamelCase						:     int=True				)      ->			Union[str, Any]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =instantiate_from_config(_UpperCamelCase				)
     if sd is not None:
          model.load_state_dict(_UpperCamelCase				)
     if gpu:
          model.cuda()
     if eval_mode:
          model.eval()
     return {"model": model}







def        _lowerCAmelCase					(						_UpperCamelCase						:     Union[str, Any]							, _UpperCamelCase						:     Optional[Any]							, _UpperCamelCase						:     Union[str, Any]							, _UpperCamelCase						:     Optional[int]				)      ->			List[Any]:
     """simple docstring"""




     if ckpt:
          _SCREAMING_SNAKE_CASE							    =torch.load(_UpperCamelCase							, map_location='cpu'				)
          _SCREAMING_SNAKE_CASE							    =pl_sd['global_step']
          print(f"loaded model from global step {global_step}."				)
     else:
          _SCREAMING_SNAKE_CASE							    ={'state_dict': None}
          _SCREAMING_SNAKE_CASE							    =None
     _SCREAMING_SNAKE_CASE							    =load_model_from_config(config.model							, pl_sd['state_dict']							, gpu=_UpperCamelCase							, eval_mode=_UpperCamelCase				)['model']
     return model, global_step
