



"""simple docstring"""
import os
import tempfile
import unittest

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

from ...generation.test_utils import GenerationTesterMixin
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,
       NezhaForMaskedLM,
       NezhaForMultipleChoice,
       NezhaForNextSentencePrediction,
       NezhaForPreTraining,
       NezhaForQuestionAnswering,
       NezhaForSequenceClassification,
       NezhaForTokenClassification,
       NezhaModel,
   )
   from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST
class 					a	:



  def __init__(						self :					Dict       ,		lowerCAmelCase :					Optional[Any]       ,		lowerCAmelCase :					Tuple=13       ,		lowerCAmelCase :					str=7       ,		lowerCAmelCase :					Any=True       ,		lowerCAmelCase :					List[Any]=True       ,		lowerCAmelCase :					Union[str, Any]=True       ,		lowerCAmelCase :					List[str]=True       ,		lowerCAmelCase :					Optional[Any]=99       ,		lowerCAmelCase :					Union[str, Any]=32       ,		lowerCAmelCase :					Optional[int]=5       ,		lowerCAmelCase :					Tuple=4       ,		lowerCAmelCase :					List[Any]=37       ,		lowerCAmelCase :					Tuple="gelu"       ,		lowerCAmelCase :					int=0.1       ,		lowerCAmelCase :					Union[str, Any]=0.1       ,		lowerCAmelCase :					Union[str, Any]=128       ,		lowerCAmelCase :					int=32       ,		lowerCAmelCase :					int=16       ,		lowerCAmelCase :					List[Any]=2       ,		lowerCAmelCase :					Dict=0.0_2       ,		lowerCAmelCase :					str=3       ,		lowerCAmelCase :					Optional[Any]=4       ,		lowerCAmelCase :					Optional[int]=None       ,		)     ->					Union[str, Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[Any]     			=parent
     SCREAMING_SNAKE_CASE_: Dict     			=batch_size
     SCREAMING_SNAKE_CASE_: List[str]     			=seq_length
     SCREAMING_SNAKE_CASE_: Dict     			=is_training
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=use_input_mask
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=use_token_type_ids
     SCREAMING_SNAKE_CASE_: int     			=use_labels
     SCREAMING_SNAKE_CASE_: List[str]     			=vocab_size
     SCREAMING_SNAKE_CASE_: Tuple     			=hidden_size
     SCREAMING_SNAKE_CASE_: Tuple     			=num_hidden_layers
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=num_attention_heads
     SCREAMING_SNAKE_CASE_: List[Any]     			=intermediate_size
     SCREAMING_SNAKE_CASE_: Tuple     			=hidden_act
     SCREAMING_SNAKE_CASE_: Dict     			=hidden_dropout_prob
     SCREAMING_SNAKE_CASE_: int     			=attention_probs_dropout_prob
     SCREAMING_SNAKE_CASE_: int     			=max_position_embeddings
     SCREAMING_SNAKE_CASE_: int     			=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_: List[str]     			=num_choices
     SCREAMING_SNAKE_CASE_: Tuple     			=scope



  def 	lowerCamelCase__       (						self :					Dict		)     ->					Optional[int]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=ids_tensor([self.batch_size, self.seq_length]       ,		self.vocab_size		)

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

     SCREAMING_SNAKE_CASE_: List[str]     			=None
     if self.use_token_type_ids:
        SCREAMING_SNAKE_CASE_: int     			=ids_tensor([self.batch_size, self.seq_length]       ,		self.type_vocab_size		)

     SCREAMING_SNAKE_CASE_: List[str]     			=None
     SCREAMING_SNAKE_CASE_: List[str]     			=None
     SCREAMING_SNAKE_CASE_: Dict     			=None
     if self.use_labels:
        SCREAMING_SNAKE_CASE_: Optional[int]     			=ids_tensor([self.batch_size]       ,		self.type_sequence_label_size		)
        SCREAMING_SNAKE_CASE_: Optional[Any]     			=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, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



  def 	lowerCamelCase__       (						self :					List[str]		)     ->					Tuple:


     '''simple docstring'''




     return NezhaConfig(
         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=_snake_case       ,		initializer_range=self.initializer_range       ,		)



  def 	lowerCamelCase__       (						self :					Union[str, Any]		)     ->					Tuple:


     '''simple docstring'''




     (
         (
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		 
     ): List[Any]     			=self.prepare_config_and_inputs()

     SCREAMING_SNAKE_CASE_: Tuple     			=True
     SCREAMING_SNAKE_CASE_: int     			=floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=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 	lowerCamelCase__       (						self :					str       ,		lowerCAmelCase :					Tuple       ,		lowerCAmelCase :					List[str]       ,		lowerCAmelCase :					Any       ,		lowerCAmelCase :					Optional[int]       ,		lowerCAmelCase :					int       ,		lowerCAmelCase :					List[str]       ,		lowerCAmelCase :					List[Any]		)     ->					List[str]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[str]     			=NezhaModel(config=_snake_case		)
     model.to(_snake_case		)
     model.eval()
     SCREAMING_SNAKE_CASE_: Tuple     			=model(_snake_case       ,		attention_mask=_snake_case       ,		token_type_ids=_snake_case		)
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=model(_snake_case       ,		token_type_ids=_snake_case		)
     SCREAMING_SNAKE_CASE_: Any     			=model(_snake_case		)
     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 	lowerCamelCase__       (						self :					str       ,		lowerCAmelCase :					List[Any]       ,		lowerCAmelCase :					Optional[int]       ,		lowerCAmelCase :					List[Any]       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					int       ,		lowerCAmelCase :					Union[str, Any]       ,		lowerCAmelCase :					Optional[int]       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					int       ,		)     ->					List[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Dict     			=True
     SCREAMING_SNAKE_CASE_: List[str]     			=NezhaModel(_snake_case		)
     model.to(_snake_case		)
     model.eval()
     SCREAMING_SNAKE_CASE_: Any     			=model(
         _snake_case       ,		attention_mask=_snake_case       ,		token_type_ids=_snake_case       ,		encoder_hidden_states=_snake_case       ,		encoder_attention_mask=_snake_case       ,		)
     SCREAMING_SNAKE_CASE_: Dict     			=model(
         _snake_case       ,		attention_mask=_snake_case       ,		token_type_ids=_snake_case       ,		encoder_hidden_states=_snake_case       ,		)
     SCREAMING_SNAKE_CASE_: Any     			=model(_snake_case       ,		attention_mask=_snake_case       ,		token_type_ids=_snake_case		)
     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 	lowerCamelCase__       (						self :					str       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					int       ,		lowerCAmelCase :					str       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					Any       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					List[Any]		)     ->					List[str]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Any     			=NezhaForMaskedLM(config=_snake_case		)
     model.to(_snake_case		)
     model.eval()
     SCREAMING_SNAKE_CASE_: int     			=model(_snake_case       ,		attention_mask=_snake_case       ,		token_type_ids=_snake_case       ,		labels=_snake_case		)
     self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.seq_length, self.vocab_size)		)



  def 	lowerCamelCase__       (						self :					Any       ,		lowerCAmelCase :					Optional[int]       ,		lowerCAmelCase :					List[str]       ,		lowerCAmelCase :					Optional[int]       ,		lowerCAmelCase :					Tuple       ,		lowerCAmelCase :					int       ,		lowerCAmelCase :					str       ,		lowerCAmelCase :					str		)     ->					Union[str, Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: str     			=NezhaForNextSentencePrediction(config=_snake_case		)
     model.to(_snake_case		)
     model.eval()
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=model(
         _snake_case       ,		attention_mask=_snake_case       ,		token_type_ids=_snake_case       ,		labels=_snake_case       ,		)
     self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, 2)		)



  def 	lowerCamelCase__       (						self :					Optional[Any]       ,		lowerCAmelCase :					Optional[int]       ,		lowerCAmelCase :					List[str]       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					List[Any]       ,		lowerCAmelCase :					Tuple       ,		lowerCAmelCase :					Dict		)     ->					int:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			=NezhaForPreTraining(config=_snake_case		)
     model.to(_snake_case		)
     model.eval()
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=model(
         _snake_case       ,		attention_mask=_snake_case       ,		token_type_ids=_snake_case       ,		labels=_snake_case       ,		next_sentence_label=_snake_case       ,		)
     self.parent.assertEqual(result.prediction_logits.shape       ,		(self.batch_size, self.seq_length, self.vocab_size)		)
     self.parent.assertEqual(result.seq_relationship_logits.shape       ,		(self.batch_size, 2)		)



  def 	lowerCamelCase__       (						self :					Union[str, Any]       ,		lowerCAmelCase :					int       ,		lowerCAmelCase :					Union[str, Any]       ,		lowerCAmelCase :					Any       ,		lowerCAmelCase :					Any       ,		lowerCAmelCase :					Any       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					Union[str, Any]		)     ->					str:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[int]     			=NezhaForQuestionAnswering(config=_snake_case		)
     model.to(_snake_case		)
     model.eval()
     SCREAMING_SNAKE_CASE_: Dict     			=model(
         _snake_case       ,		attention_mask=_snake_case       ,		token_type_ids=_snake_case       ,		start_positions=_snake_case       ,		end_positions=_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 	lowerCamelCase__       (						self :					Dict       ,		lowerCAmelCase :					Union[str, Any]       ,		lowerCAmelCase :					Union[str, Any]       ,		lowerCAmelCase :					str       ,		lowerCAmelCase :					Union[str, Any]       ,		lowerCAmelCase :					str       ,		lowerCAmelCase :					List[Any]       ,		lowerCAmelCase :					Union[str, Any]		)     ->					List[str]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[Any]     			=self.num_labels
     SCREAMING_SNAKE_CASE_: str     			=NezhaForSequenceClassification(_snake_case		)
     model.to(_snake_case		)
     model.eval()
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=model(_snake_case       ,		attention_mask=_snake_case       ,		token_type_ids=_snake_case       ,		labels=_snake_case		)
     self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.num_labels)		)



  def 	lowerCamelCase__       (						self :					List[str]       ,		lowerCAmelCase :					Optional[int]       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					Union[str, Any]       ,		lowerCAmelCase :					List[Any]       ,		lowerCAmelCase :					Tuple       ,		lowerCAmelCase :					List[str]       ,		lowerCAmelCase :					Optional[int]		)     ->					Optional[int]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[str]     			=self.num_labels
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=NezhaForTokenClassification(config=_snake_case		)
     model.to(_snake_case		)
     model.eval()
     SCREAMING_SNAKE_CASE_: List[Any]     			=model(_snake_case       ,		attention_mask=_snake_case       ,		token_type_ids=_snake_case       ,		labels=_snake_case		)
     self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.seq_length, self.num_labels)		)



  def 	lowerCamelCase__       (						self :					int       ,		lowerCAmelCase :					int       ,		lowerCAmelCase :					List[Any]       ,		lowerCAmelCase :					Tuple       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					Tuple       ,		lowerCAmelCase :					List[str]       ,		lowerCAmelCase :					int		)     ->					Tuple:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[str]     			=self.num_choices
     SCREAMING_SNAKE_CASE_: str     			=NezhaForMultipleChoice(config=_snake_case		)
     model.to(_snake_case		)
     model.eval()
     SCREAMING_SNAKE_CASE_: Tuple     			=input_ids.unsqueeze(1		).expand(-1       ,		self.num_choices       ,		-1		).contiguous()
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=token_type_ids.unsqueeze(1		).expand(-1       ,		self.num_choices       ,		-1		).contiguous()
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=input_mask.unsqueeze(1		).expand(-1       ,		self.num_choices       ,		-1		).contiguous()
     SCREAMING_SNAKE_CASE_: Dict     			=model(
         _snake_case       ,		attention_mask=_snake_case       ,		token_type_ids=_snake_case       ,		labels=_snake_case       ,		)
     self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.num_choices)		)





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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[int]     			=self.prepare_config_and_inputs()
     (
         (
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		 
     ): Dict     			=config_and_inputs
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			={"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
     return config, inputs_dict







@require_torch
class 					a	(  UpperCAmelCase_	,      UpperCAmelCase_	,      UpperCAmelCase_	,      unittest.TestCase       ):
  UpperCamelCase	:				str     						=					(
      (
          NezhaModel,
          NezhaForMaskedLM,
          NezhaForMultipleChoice,
          NezhaForNextSentencePrediction,
          NezhaForPreTraining,
          NezhaForQuestionAnswering,
          NezhaForSequenceClassification,
          NezhaForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  UpperCamelCase	:				str     						=					(
      {
          "feature-extraction": NezhaModel,
          "fill-mask": NezhaForMaskedLM,
          "question-answering": NezhaForQuestionAnswering,
          "text-classification": NezhaForSequenceClassification,
          "token-classification": NezhaForTokenClassification,
          "zero-shot": NezhaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  UpperCamelCase	:				int     						=					True



  def 	lowerCamelCase__       (						self :					Tuple       ,		lowerCAmelCase :					Optional[int]       ,		lowerCAmelCase :					Optional[int]       ,		lowerCAmelCase :					str=False		)     ->					int:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			=super()._prepare_for_class(_snake_case       ,		_snake_case       ,		return_labels=_snake_case		)

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



  def 	lowerCamelCase__       (						self :					Any		)     ->					Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Dict     			=NezhaModelTester(self		)
     SCREAMING_SNAKE_CASE_: Any     			=ConfigTester(self       ,		config_class=_snake_case       ,		hidden_size=37		)



  def 	lowerCamelCase__       (						self :					str		)     ->					List[Any]:


     '''simple docstring'''




     self.config_tester.run_common_tests()



  def 	lowerCamelCase__       (						self :					Optional[Any]		)     ->					Any:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_snake_case		)



  def 	lowerCamelCase__       (						self :					List[Any]		)     ->					str:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(*_snake_case		)



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


     '''simple docstring'''




     (
         (
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		(
         SCREAMING_SNAKE_CASE_ 
     )			,		 
     ): Tuple     			=self.model_tester.prepare_config_and_inputs_for_decoder()

     SCREAMING_SNAKE_CASE_: Any     			=None

     self.model_tester.create_and_check_model_as_decoder(
         _snake_case       ,		_snake_case       ,		_snake_case       ,		_snake_case       ,		_snake_case       ,		_snake_case       ,		_snake_case       ,		_snake_case       ,		_snake_case       ,		)



  def 	lowerCamelCase__       (						self :					Any		)     ->					int:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[Any]     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*_snake_case		)



  def 	lowerCamelCase__       (						self :					Union[str, Any]		)     ->					Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*_snake_case		)



  def 	lowerCamelCase__       (						self :					Tuple		)     ->					Optional[int]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Tuple     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_next_sequence_prediction(*_snake_case		)



  def 	lowerCamelCase__       (						self :					Union[str, Any]		)     ->					Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[str]     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*_snake_case		)



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: str     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*_snake_case		)



  def 	lowerCamelCase__       (						self :					int		)     ->					List[str]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Tuple     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*_snake_case		)



  def 	lowerCamelCase__       (						self :					Union[str, Any]		)     ->					Optional[int]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*_snake_case		)



  @slow
  def 	lowerCamelCase__       (						self :					str		)     ->					Tuple:


     '''simple docstring'''




     for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        SCREAMING_SNAKE_CASE_: Optional[int]     			=NezhaModel.from_pretrained(_snake_case		)
        self.assertIsNotNone(_snake_case		)





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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Dict     			=self.model_tester.prepare_config_and_inputs_for_common()
     for model_class in self.all_model_classes:
        # NezhaForMultipleChoice behaves incorrectly in JIT environments.
        if model_class == NezhaForMultipleChoice:
           return

        SCREAMING_SNAKE_CASE_: List[Any]     			=True
        SCREAMING_SNAKE_CASE_: int     			=model_class(config=_snake_case		)

        SCREAMING_SNAKE_CASE_: Union[str, Any]     			=self._prepare_for_class(_snake_case       ,		_snake_case		)
        SCREAMING_SNAKE_CASE_: int     			=torch.jit.trace(
            _snake_case       ,		(inputs_dict["""input_ids"""].to("""cpu"""		), inputs_dict["""attention_mask"""].to("""cpu"""		))		)

        with tempfile.TemporaryDirectory() as tmp:
           torch.jit.save(_snake_case       ,		os.path.join(_snake_case       ,		"""bert.pt"""		)		)
           SCREAMING_SNAKE_CASE_: str     			=torch.jit.load(os.path.join(_snake_case       ,		"""bert.pt"""		)       ,		map_location=_snake_case		)
           loaded(inputs_dict["""input_ids"""].to(_snake_case		)       ,		inputs_dict["""attention_mask"""].to(_snake_case		)		)







@require_torch
class 					a	(  unittest.TestCase       ):



  @slow
  def 	lowerCamelCase__       (						self :					str		)     ->					List[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[str]     			=NezhaModel.from_pretrained("""sijunhe/nezha-cn-base"""		)
     SCREAMING_SNAKE_CASE_: int     			=torch.tensor([[0, 1, 2, 3, 4, 5]]		)
     SCREAMING_SNAKE_CASE_: List[Any]     			=torch.tensor([[0, 1, 1, 1, 1, 1]]		)
     with torch.no_grad():
        SCREAMING_SNAKE_CASE_: str     			=model(_snake_case       ,		attention_mask=_snake_case		)[0]
     SCREAMING_SNAKE_CASE_: List[str]     			=torch.Size((1, 6, 768)		)
     self.assertEqual(output.shape       ,		_snake_case		)
     SCREAMING_SNAKE_CASE_: List[str]     			=torch.tensor([[[0.0_6_8_5, 0.2_4_4_1, 0.1_1_0_2], [0.0_6_0_0, 0.1_9_0_6, 0.1_3_4_9], [0.0_2_2_1, 0.0_8_1_9, 0.0_5_8_6]]]		)

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





  @slow
  def 	lowerCamelCase__       (						self :					Tuple		)     ->					Union[str, Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[int]     			=NezhaForMaskedLM.from_pretrained("""sijunhe/nezha-cn-base"""		)
     SCREAMING_SNAKE_CASE_: str     			=torch.tensor([[0, 1, 2, 3, 4, 5]]		)
     SCREAMING_SNAKE_CASE_: str     			=torch.tensor([[1, 1, 1, 1, 1, 1]]		)
     with torch.no_grad():
        SCREAMING_SNAKE_CASE_: List[Any]     			=model(_snake_case       ,		attention_mask=_snake_case		)[0]
     SCREAMING_SNAKE_CASE_: List[Any]     			=torch.Size((1, 6, 2_1128)		)
     self.assertEqual(output.shape       ,		_snake_case		)
     SCREAMING_SNAKE_CASE_: Tuple     			=torch.tensor(
         [[-2.7_9_3_9, -1.7_9_0_2, -2.2_1_8_9], [-2.8_5_8_5, -1.8_9_0_8, -2.3_7_2_3], [-2.6_4_9_9, -1.7_7_5_0, -2.2_5_5_8]]		)

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








"""simple docstring"""
import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
   import jax
   import jax.numpy as jnp
   from flax.jax_utils import replicate
   from flax.training.common_utils import shard
@slow
@require_flax
class 					a	(  unittest.TestCase       ):



  def 	lowerCamelCase__       (						self :					Dict		)     ->					str:


     '''simple docstring'''




     super().tearDown()
     gc.collect()



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Dict     			=FlaxStableDiffusionPipeline.from_pretrained(
         """stabilityai/stable-diffusion-2"""       ,		revision="""bf16"""       ,		dtype=jnp.bfloataa       ,		)

     SCREAMING_SNAKE_CASE_: Union[str, Any]     			="""A painting of a squirrel eating a burger"""
     SCREAMING_SNAKE_CASE_: Any     			=jax.device_count()
     SCREAMING_SNAKE_CASE_: Dict     			=num_samples * [prompt]
     SCREAMING_SNAKE_CASE_: List[Any]     			=sd_pipe.prepare_inputs(lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=replicate(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=shard(lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: Dict     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=jax.random.split(lowerCAmelCase       ,		jax.device_count()		)

     SCREAMING_SNAKE_CASE_: Tuple     			=sd_pipe(lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		num_inference_steps=25       ,		jit=lowerCAmelCase		)[0]
     assert images.shape == (jax.device_count(), 1, 768, 768, 3)

     SCREAMING_SNAKE_CASE_: Dict     			=images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]		)
     SCREAMING_SNAKE_CASE_: Dict     			=images[0, 253:256, 253:256, -1]

     SCREAMING_SNAKE_CASE_: Optional[int]     			=jnp.asarray(jax.device_get(image_slice.flatten()		)		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=jnp.array([0.4_2_3_8, 0.4_4_1_4, 0.4_3_9_5, 0.4_4_5_3, 0.4_6_2_9, 0.4_5_9_0, 0.4_5_3_1, 0.4_5_5_0_8, 0.4_5_1_2]		)
     print(f'''output_slice: {output_slice}'''		)
     assert jnp.abs(output_slice - expected_slice		).max() < 1E-2





  def 	lowerCamelCase__       (						self :					List[str]		)     ->					Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			="""stabilityai/stable-diffusion-2"""
     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Optional[int]     			=FlaxDPMSolverMultistepScheduler.from_pretrained(lowerCAmelCase       ,		subfolder="""scheduler"""		)
     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Optional[int]     			=FlaxStableDiffusionPipeline.from_pretrained(
         lowerCAmelCase       ,		scheduler=lowerCAmelCase       ,		revision="""bf16"""       ,		dtype=jnp.bfloataa       ,		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=scheduler_params

     SCREAMING_SNAKE_CASE_: Tuple     			="""A painting of a squirrel eating a burger"""
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=jax.device_count()
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=num_samples * [prompt]
     SCREAMING_SNAKE_CASE_: List[Any]     			=sd_pipe.prepare_inputs(lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: Tuple     			=replicate(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=shard(lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: Any     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: Any     			=jax.random.split(lowerCAmelCase       ,		jax.device_count()		)

     SCREAMING_SNAKE_CASE_: Tuple     			=sd_pipe(lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		num_inference_steps=25       ,		jit=lowerCAmelCase		)[0]
     assert images.shape == (jax.device_count(), 1, 768, 768, 3)

     SCREAMING_SNAKE_CASE_: str     			=images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]		)
     SCREAMING_SNAKE_CASE_: Any     			=images[0, 253:256, 253:256, -1]

     SCREAMING_SNAKE_CASE_: Optional[Any]     			=jnp.asarray(jax.device_get(image_slice.flatten()		)		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=jnp.array([0.4_3_3_6, 0.4_2_9_6_9, 0.4_4_5_3, 0.4_1_9_9, 0.4_2_9_7, 0.4_5_3_1, 0.4_4_3_4, 0.4_4_3_4, 0.4_2_9_7]		)
     print(f'''output_slice: {output_slice}'''		)
     assert jnp.abs(output_slice - expected_slice		).max() < 1E-2



