


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







     def __init__(						self :						int   ,   _lowercase :						Optional[Any]   ,   _lowercase :						Optional[Any]=13   ,   _lowercase :						List[str]=7   ,   _lowercase :						List[Any]=True   ,   _lowercase :						int=True   ,   _lowercase :						Dict=True   ,   _lowercase :						Union[str, Any]=True   ,   _lowercase :						Tuple=99   ,   _lowercase :						List[Any]=32   ,   _lowercase :						List[Any]=5   ,   _lowercase :						List[str]=4   ,   _lowercase :						List[Any]=37   ,   _lowercase :						Dict="gelu"   ,   _lowercase :						Any=0.1   ,   _lowercase :						Any=0.1   ,   _lowercase :						str=1_28   ,   _lowercase :						int=32   ,   _lowercase :						int=16   ,   _lowercase :						List[str]=2   ,   _lowercase :						Any=0.02   ,   _lowercase :						List[str]=3   ,   _lowercase :						Any=4   ,   _lowercase :						Tuple=None   ,   ):
           SCREAMING_SNAKE_CASE__       :  Tuple						     =		parent
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		batch_size
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		seq_length
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		is_training
           SCREAMING_SNAKE_CASE__       :  str						     =		use_input_mask
           SCREAMING_SNAKE_CASE__       :  int						     =		use_token_type_ids
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		use_labels
           SCREAMING_SNAKE_CASE__       :  str						     =		vocab_size
           SCREAMING_SNAKE_CASE__       :  str						     =		hidden_size
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		num_hidden_layers
           SCREAMING_SNAKE_CASE__       :  Any						     =		num_attention_heads
           SCREAMING_SNAKE_CASE__       :  int						     =		intermediate_size
           SCREAMING_SNAKE_CASE__       :  str						     =		hidden_act
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		hidden_dropout_prob
           SCREAMING_SNAKE_CASE__       :  Dict						     =		attention_probs_dropout_prob
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		max_position_embeddings
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		type_vocab_size
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		type_sequence_label_size
           SCREAMING_SNAKE_CASE__       :  Dict						     =		initializer_range
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		num_labels
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		num_choices
           SCREAMING_SNAKE_CASE__       :  Dict						     =		scope







     def 							lowercase__       (						self :						Optional[int]      ):
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		ids_tensor([self.batch_size, self.seq_length]   ,   self.vocab_size      )

           SCREAMING_SNAKE_CASE__       :  int						     =		None
           if self.use_input_mask:
                 SCREAMING_SNAKE_CASE__       :  Any						     =		random_attention_mask([self.batch_size, self.seq_length]      )

           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		None
           if self.use_token_type_ids:
                 SCREAMING_SNAKE_CASE__       :  Tuple						     =		ids_tensor([self.batch_size, self.seq_length]   ,   self.type_vocab_size      )

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

           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		self.get_config()

           return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







     def 							lowercase__       (						self :						Optional[Any]      ):
           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=__UpperCAmelCase   ,   initializer_range=self.initializer_range   ,   )







     def 							lowercase__       (						self :						Optional[Any]      ):
           (
               SCREAMING_SNAKE_CASE__			
           )       :  Optional[int]						     =		self.prepare_config_and_inputs()

           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		True
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
           SCREAMING_SNAKE_CASE__       :  List[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 							lowercase__       (						self :						Optional[int]   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						Tuple   ,   _lowercase :						Optional[int]   ,   _lowercase :						List[Any]   ,   _lowercase :						str      ):
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		NezhaModel(config=__UpperCAmelCase      )
           model.to(__UpperCAmelCase      )
           model.eval()
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		model(__UpperCAmelCase   ,   attention_mask=__UpperCAmelCase   ,   token_type_ids=__UpperCAmelCase      )
           SCREAMING_SNAKE_CASE__       :  str						     =		model(__UpperCAmelCase   ,   token_type_ids=__UpperCAmelCase      )
           SCREAMING_SNAKE_CASE__       :  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 							lowercase__       (						self :						List[str]   ,   _lowercase :						Optional[int]   ,   _lowercase :						int   ,   _lowercase :						Tuple   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						Tuple   ,   _lowercase :						int   ,   _lowercase :						Dict   ,   _lowercase :						Optional[Any]   ,   _lowercase :						str   ,   ):
           SCREAMING_SNAKE_CASE__       :  int						     =		True
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		NezhaModel(__UpperCAmelCase      )
           model.to(__UpperCAmelCase      )
           model.eval()
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		model(
               __UpperCAmelCase   ,   attention_mask=__UpperCAmelCase   ,   token_type_ids=__UpperCAmelCase   ,   encoder_hidden_states=__UpperCAmelCase   ,   encoder_attention_mask=__UpperCAmelCase   ,   )
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		model(
               __UpperCAmelCase   ,   attention_mask=__UpperCAmelCase   ,   token_type_ids=__UpperCAmelCase   ,   encoder_hidden_states=__UpperCAmelCase   ,   )
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		model(__UpperCAmelCase   ,   attention_mask=__UpperCAmelCase   ,   token_type_ids=__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 							lowercase__       (						self :						Union[str, Any]   ,   _lowercase :						Tuple   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						Optional[Any]   ,   _lowercase :						Optional[Any]   ,   _lowercase :						int   ,   _lowercase :						str   ,   _lowercase :						str      ):
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		NezhaForMaskedLM(config=__UpperCAmelCase      )
           model.to(__UpperCAmelCase      )
           model.eval()
           SCREAMING_SNAKE_CASE__       :  str						     =		model(__UpperCAmelCase   ,   attention_mask=__UpperCAmelCase   ,   token_type_ids=__UpperCAmelCase   ,   labels=__UpperCAmelCase      )
           self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.seq_length, self.vocab_size)      )







     def 							lowercase__       (						self :						Dict   ,   _lowercase :						int   ,   _lowercase :						Optional[int]   ,   _lowercase :						str   ,   _lowercase :						Optional[int]   ,   _lowercase :						str   ,   _lowercase :						Tuple   ,   _lowercase :						Tuple      ):
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		NezhaForNextSentencePrediction(config=__UpperCAmelCase      )
           model.to(__UpperCAmelCase      )
           model.eval()
           SCREAMING_SNAKE_CASE__       :  Dict						     =		model(
               __UpperCAmelCase   ,   attention_mask=__UpperCAmelCase   ,   token_type_ids=__UpperCAmelCase   ,   labels=__UpperCAmelCase   ,   )
           self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, 2)      )







     def 							lowercase__       (						self :						str   ,   _lowercase :						Optional[int]   ,   _lowercase :						Dict   ,   _lowercase :						Any   ,   _lowercase :						Optional[Any]   ,   _lowercase :						List[str]   ,   _lowercase :						int   ,   _lowercase :						Dict      ):
           SCREAMING_SNAKE_CASE__       :  Dict						     =		NezhaForPreTraining(config=__UpperCAmelCase      )
           model.to(__UpperCAmelCase      )
           model.eval()
           SCREAMING_SNAKE_CASE__       :  str						     =		model(
               __UpperCAmelCase   ,   attention_mask=__UpperCAmelCase   ,   token_type_ids=__UpperCAmelCase   ,   labels=__UpperCAmelCase   ,   next_sentence_label=__UpperCAmelCase   ,   )
           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 							lowercase__       (						self :						str   ,   _lowercase :						Tuple   ,   _lowercase :						Tuple   ,   _lowercase :						Optional[int]   ,   _lowercase :						str   ,   _lowercase :						Optional[Any]   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						List[Any]      ):
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		NezhaForQuestionAnswering(config=__UpperCAmelCase      )
           model.to(__UpperCAmelCase      )
           model.eval()
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		model(
               __UpperCAmelCase   ,   attention_mask=__UpperCAmelCase   ,   token_type_ids=__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 							lowercase__       (						self :						str   ,   _lowercase :						Optional[int]   ,   _lowercase :						str   ,   _lowercase :						str   ,   _lowercase :						Any   ,   _lowercase :						List[str]   ,   _lowercase :						Optional[Any]   ,   _lowercase :						str      ):
           SCREAMING_SNAKE_CASE__       :  Tuple						     =		self.num_labels
           SCREAMING_SNAKE_CASE__       :  str						     =		NezhaForSequenceClassification(__UpperCAmelCase      )
           model.to(__UpperCAmelCase      )
           model.eval()
           SCREAMING_SNAKE_CASE__       :  Dict						     =		model(__UpperCAmelCase   ,   attention_mask=__UpperCAmelCase   ,   token_type_ids=__UpperCAmelCase   ,   labels=__UpperCAmelCase      )
           self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.num_labels)      )







     def 							lowercase__       (						self :						Any   ,   _lowercase :						Dict   ,   _lowercase :						List[str]   ,   _lowercase :						int   ,   _lowercase :						int   ,   _lowercase :						int   ,   _lowercase :						Any   ,   _lowercase :						str      ):
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		self.num_labels
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		NezhaForTokenClassification(config=__UpperCAmelCase      )
           model.to(__UpperCAmelCase      )
           model.eval()
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		model(__UpperCAmelCase   ,   attention_mask=__UpperCAmelCase   ,   token_type_ids=__UpperCAmelCase   ,   labels=__UpperCAmelCase      )
           self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.seq_length, self.num_labels)      )







     def 							lowercase__       (						self :						Optional[Any]   ,   _lowercase :						int   ,   _lowercase :						str   ,   _lowercase :						Optional[int]   ,   _lowercase :						str   ,   _lowercase :						Optional[Any]   ,   _lowercase :						List[str]   ,   _lowercase :						Optional[Any]      ):
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		self.num_choices
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		NezhaForMultipleChoice(config=__UpperCAmelCase      )
           model.to(__UpperCAmelCase      )
           model.eval()
           SCREAMING_SNAKE_CASE__       :  Any						     =		input_ids.unsqueeze(1      ).expand(-1   ,   self.num_choices   ,   -1      ).contiguous()
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		token_type_ids.unsqueeze(1      ).expand(-1   ,   self.num_choices   ,   -1      ).contiguous()
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		input_mask.unsqueeze(1      ).expand(-1   ,   self.num_choices   ,   -1      ).contiguous()
           SCREAMING_SNAKE_CASE__       :  Any						     =		model(
               __UpperCAmelCase   ,   attention_mask=__UpperCAmelCase   ,   token_type_ids=__UpperCAmelCase   ,   labels=__UpperCAmelCase   ,   )
           self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.num_choices)      )







     def 							lowercase__       (						self :						List[str]      ):
           SCREAMING_SNAKE_CASE__       :  Tuple						     =		self.prepare_config_and_inputs()
           (
               SCREAMING_SNAKE_CASE__			
           )       :  int						     =		config_and_inputs
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
           return config, inputs_dict




@require_torch
class 							lowercase  (						__UpperCamelCase    ,       __UpperCamelCase    ,       __UpperCamelCase    ,       unittest.TestCase			):
     lowerCamelCase			:			Any      = (
         (
             NezhaModel,
             NezhaForMaskedLM,
             NezhaForMultipleChoice,
             NezhaForNextSentencePrediction,
             NezhaForPreTraining,
             NezhaForQuestionAnswering,
             NezhaForSequenceClassification,
             NezhaForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     lowerCamelCase			:			Any      = (
         {
             '''feature-extraction''': NezhaModel,
             '''fill-mask''': NezhaForMaskedLM,
             '''question-answering''': NezhaForQuestionAnswering,
             '''text-classification''': NezhaForSequenceClassification,
             '''token-classification''': NezhaForTokenClassification,
             '''zero-shot''': NezhaForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     lowerCamelCase			:			int      = True







     def 							lowercase__       (						self :						str   ,   _lowercase :						Dict   ,   _lowercase :						int   ,   _lowercase :						Union[str, Any]=False      ):
           SCREAMING_SNAKE_CASE__       :  Any						     =		super()._prepare_for_class(__UpperCAmelCase   ,   __UpperCAmelCase   ,   return_labels=__UpperCAmelCase      )

           if return_labels:
                 if model_class in get_values(__UpperCAmelCase      ):
                       SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		torch.zeros(
                           (self.model_tester.batch_size, self.model_tester.seq_length)   ,   dtype=torch.long   ,   device=__UpperCAmelCase      )
                       SCREAMING_SNAKE_CASE__       :  List[str]						     =		torch.zeros(
                           self.model_tester.batch_size   ,   dtype=torch.long   ,   device=__UpperCAmelCase      )
           return inputs_dict







     def 							lowercase__       (						self :						Optional[Any]      ):
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		NezhaModelTester(self      )
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		ConfigTester(self   ,   config_class=__UpperCAmelCase   ,   hidden_size=37      )







     def 							lowercase__       (						self :						Dict      ):
           self.config_tester.run_common_tests()







     def 							lowercase__       (						self :						Dict      ):
           SCREAMING_SNAKE_CASE__       :  str						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__UpperCAmelCase      )







     def 							lowercase__       (						self :						Optional[int]      ):
           SCREAMING_SNAKE_CASE__       :  Dict						     =		self.model_tester.prepare_config_and_inputs_for_decoder()
           self.model_tester.create_and_check_model_as_decoder(*__UpperCAmelCase      )







     def 							lowercase__       (						self :						str      ):
           (
               SCREAMING_SNAKE_CASE__			
           )       :  Any						     =		self.model_tester.prepare_config_and_inputs_for_decoder()

           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		None

           self.model_tester.create_and_check_model_as_decoder(
               __UpperCAmelCase   ,   __UpperCAmelCase   ,   __UpperCAmelCase   ,   __UpperCAmelCase   ,   __UpperCAmelCase   ,   __UpperCAmelCase   ,   __UpperCAmelCase   ,   __UpperCAmelCase   ,   __UpperCAmelCase   ,   )







     def 							lowercase__       (						self :						Optional[Any]      ):
           SCREAMING_SNAKE_CASE__       :  Any						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase      )







     def 							lowercase__       (						self :						Tuple      ):
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*__UpperCAmelCase      )







     def 							lowercase__       (						self :						Tuple      ):
           SCREAMING_SNAKE_CASE__       :  Dict						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_next_sequence_prediction(*__UpperCAmelCase      )







     def 							lowercase__       (						self :						Tuple      ):
           SCREAMING_SNAKE_CASE__       :  Tuple						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_pretraining(*__UpperCAmelCase      )







     def 							lowercase__       (						self :						Dict      ):
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase      )







     def 							lowercase__       (						self :						List[str]      ):
           SCREAMING_SNAKE_CASE__       :  Any						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase      )







     def 							lowercase__       (						self :						Dict      ):
           SCREAMING_SNAKE_CASE__       :  Any						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase      )







     @slow
     def 							lowercase__       (						self :						List[str]      ):



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







     @slow
     @require_torch_gpu
     def 							lowercase__       (						self :						Optional[Any]      ):
           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__       :  Optional[Any]						     =		True
                 SCREAMING_SNAKE_CASE__       :  Any						     =		model_class(config=__UpperCAmelCase      )

                 SCREAMING_SNAKE_CASE__       :  Dict						     =		self._prepare_for_class(__UpperCAmelCase   ,   __UpperCAmelCase      )
                 SCREAMING_SNAKE_CASE__       :  int						     =		torch.jit.trace(
                     __UpperCAmelCase   ,   (inputs_dict['''input_ids'''].to('''cpu'''      ), inputs_dict['''attention_mask'''].to('''cpu'''      ))      )

                 with tempfile.TemporaryDirectory() as tmp:
                       torch.jit.save(__UpperCAmelCase   ,   os.path.join(__UpperCAmelCase   ,   '''bert.pt'''      )      )
                       SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		torch.jit.load(os.path.join(__UpperCAmelCase   ,   '''bert.pt'''      )   ,   map_location=__UpperCAmelCase      )
                       loaded(inputs_dict['''input_ids'''].to(__UpperCAmelCase      )   ,   inputs_dict['''attention_mask'''].to(__UpperCAmelCase      )      )




@require_torch
class 							lowercase  (						unittest.TestCase			):







     @slow
     def 							lowercase__       (						self :						int      ):
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		NezhaModel.from_pretrained('''sijunhe/nezha-cn-base'''      )
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		torch.tensor([[0, 1, 2, 3, 4, 5]]      )
           SCREAMING_SNAKE_CASE__       :  str						     =		torch.tensor([[0, 1, 1, 1, 1, 1]]      )
           with torch.no_grad():
                 SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		model(__UpperCAmelCase   ,   attention_mask=__UpperCAmelCase      )[0]
           SCREAMING_SNAKE_CASE__       :  str						     =		torch.Size((1, 6, 7_68)      )
           self.assertEqual(output.shape   ,   __UpperCAmelCase      )
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		torch.tensor([[[0.0685, 0.2441, 0.1102], [0.0600, 0.1906, 0.1349], [0.0221, 0.0819, 0.0586]]]      )

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







     @slow
     def 							lowercase__       (						self :						Any      ):
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		NezhaForMaskedLM.from_pretrained('''sijunhe/nezha-cn-base'''      )
           SCREAMING_SNAKE_CASE__       :  Any						     =		torch.tensor([[0, 1, 2, 3, 4, 5]]      )
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		torch.tensor([[1, 1, 1, 1, 1, 1]]      )
           with torch.no_grad():
                 SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		model(__UpperCAmelCase   ,   attention_mask=__UpperCAmelCase      )[0]
           SCREAMING_SNAKE_CASE__       :  int						     =		torch.Size((1, 6, 2_11_28)      )
           self.assertEqual(output.shape   ,   __UpperCAmelCase      )
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		torch.tensor(
               [[-2.7939, -1.7902, -2.2189], [-2.8585, -1.8908, -2.3723], [-2.6499, -1.7750, -2.2558]]      )

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














from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
			import tensorflow as tf

			from transformers import TFBlipTextModel
			from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST




class 							snake_case_       :

						"""simple docstring"""



						def __init__(self:					Optional[Any] ,					__UpperCAmelCase:					List[str] ,					__UpperCAmelCase:					int=12 ,					__UpperCAmelCase:					Dict=7 ,					__UpperCAmelCase:					str=True ,					__UpperCAmelCase:					Tuple=True ,					__UpperCAmelCase:					Tuple=True ,					__UpperCAmelCase:					Optional[int]=99 ,					__UpperCAmelCase:					str=32 ,					__UpperCAmelCase:					Any=32 ,					__UpperCAmelCase:					int=2 ,					__UpperCAmelCase:					List[str]=4 ,					__UpperCAmelCase:					Tuple=37 ,					__UpperCAmelCase:					Optional[int]=0.1 ,					__UpperCAmelCase:					List[Any]=0.1 ,					__UpperCAmelCase:					Dict=512 ,					__UpperCAmelCase:					Union[str, Any]=0.02 ,					__UpperCAmelCase:					List[Any]=0 ,					__UpperCAmelCase:					str=None ,					)    ->     Tuple:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					parent
											__a					:						Dict      =					batch_size
											__a					:						Optional[Any]      =					seq_length
											__a					:						int      =					is_training
											__a					:						Union[str, Any]      =					use_input_mask
											__a					:						str      =					use_labels
											__a					:						Tuple      =					vocab_size
											__a					:						Dict      =					hidden_size
											__a					:						Optional[int]      =					projection_dim
											__a					:						Tuple      =					num_hidden_layers
											__a					:						Dict      =					num_attention_heads
											__a					:						Any      =					intermediate_size
											__a					:						Optional[int]      =					dropout
											__a					:						Dict      =					attention_dropout
											__a					:						List[Any]      =					max_position_embeddings
											__a					:						Any      =					initializer_range
											__a					:						Tuple      =					scope
											__a					:						Dict      =					bos_token_id



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




											'''simple docstring'''



											__a					:						Optional[Any]      =					ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size   )

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

											if input_mask is not None:
																__a					:						Tuple      =					input_mask.numpy()
																__a      ,			__a					:						int      =					input_mask.shape
																__a					:						Dict      =					np.random.randint(1 ,					seq_length - 1 ,					size=(batch_size,)   )
																for batch_idx, start_index in enumerate(__UpperCAmelCase   ):
																					__a					:						Any      =					1
																					__a					:						int      =					0

											__a					:						Union[str, Any]      =					self.get_config()

											return config, input_ids, tf.convert_to_tensor(__UpperCAmelCase   )



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




											'''simple docstring'''



											return BlipTextConfig(
											    vocab_size=self.vocab_size ,					hidden_size=self.hidden_size ,					projection_dim=self.projection_dim ,					num_hidden_layers=self.num_hidden_layers ,					num_attention_heads=self.num_attention_heads ,					intermediate_size=self.intermediate_size ,					dropout=self.dropout ,					attention_dropout=self.attention_dropout ,					max_position_embeddings=self.max_position_embeddings ,					initializer_range=self.initializer_range ,					bos_token_id=self.bos_token_id ,					)



						def        UpperCAmelCase__					(self:					List[str] ,					__UpperCAmelCase:					Any ,					__UpperCAmelCase:					Dict ,					__UpperCAmelCase:					str   )    ->     List[str]:




											'''simple docstring'''



											__a					:						int      =					TFBlipTextModel(config=__UpperCAmelCase   )
											__a					:						List[str]      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					training=__UpperCAmelCase   )
											__a					:						Any      =					model(__UpperCAmelCase ,					training=__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:					List[str]   )    ->     str:




											'''simple docstring'''



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




@require_tf
class 							snake_case_       (    __UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      (TFBlipTextModel,) if is_tf_available() else ()
						snake_case__        =      False
						snake_case__        =      False
						snake_case__        =      False



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




											'''simple docstring'''



											__a					:						str      =					BlipTextModelTester(self   )
											__a					:						str      =					ConfigTester(self ,					config_class=__UpperCAmelCase ,					hidden_size=37   )



						def        UpperCAmelCase__					(self:					str   )    ->     str:




											'''simple docstring'''



											self.config_tester.run_common_tests()



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




											'''simple docstring'''



											__a					:						int      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Dict:




											'''simple docstring'''



											pass



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




											'''simple docstring'''



											pass



						@unittest.skip(reason="Blip does not use inputs_embeds"   )
						def        UpperCAmelCase__					(self:					Any   )    ->     Dict:




											'''simple docstring'''



											pass



						@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"   )
						def        UpperCAmelCase__					(self:					List[Any]   )    ->     List[str]:




											'''simple docstring'''



											pass



						@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"   )
						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     int:




											'''simple docstring'''



											pass



						@slow
						def        UpperCAmelCase__					(self:					Optional[Any]   )    ->     Tuple:




											'''simple docstring'''



											for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__a					:						List[str]      =					TFBlipTextModel.from_pretrained(__UpperCAmelCase   )
																self.assertIsNotNone(__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Dict ,					__UpperCAmelCase:					Optional[Any]=True   )    ->     Any:




											'''simple docstring'''



											super().test_pt_tf_model_equivalence(allow_missing_keys=__UpperCAmelCase   )





