





import math
import os
import unittest

from transformers import MegatronBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, 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 (
      MODEL_FOR_PRETRAINING_MAPPING,
      MegatronBertForCausalLM,
      MegatronBertForMaskedLM,
      MegatronBertForMultipleChoice,
      MegatronBertForNextSentencePrediction,
      MegatronBertForPreTraining,
      MegatronBertForQuestionAnswering,
      MegatronBertForSequenceClassification,
      MegatronBertForTokenClassification,
      MegatronBertModel,
  )






class     lowercase__       :

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__=13,		__magic_name__=7,		__magic_name__=True,		__magic_name__=True,		__magic_name__=True,		__magic_name__=True,		__magic_name__=99,		__magic_name__=64,		__magic_name__=32,		__magic_name__=5,		__magic_name__=4,		__magic_name__=37,		__magic_name__="gelu",		__magic_name__=0.1,		__magic_name__=0.1,		__magic_name__=512,		__magic_name__=16,		__magic_name__=2,		__magic_name__=0.02,		__magic_name__=3,		__magic_name__=4,		__magic_name__=None,		) ->      Optional[int]:






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




   def       UpperCamelCase__  (    self							) ->      Any:






       """simple docstring"""
       UpperCamelCase__							:				str          =      ids_tensor([self.batch_size, self.seq_length],		self.vocab_size							)

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

       UpperCamelCase__							:				List[Any]          =      None
       if self.use_token_type_ids:
           UpperCamelCase__							:				Tuple          =      ids_tensor([self.batch_size, self.seq_length],		self.type_vocab_size							)

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

       UpperCamelCase__							:				str          =      self.get_config()

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




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






       """simple docstring"""
       return MegatronBertConfig(
           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,		embedding_size=self.embedding_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=__magic_name__,		initializer_range=self.initializer_range,		)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				Optional[int]          =      MegatronBertModel(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:				Optional[int]          =      model(__magic_name__,		attention_mask=__magic_name__,		token_type_ids=__magic_name__							)
       UpperCamelCase__							:				Any          =      model(__magic_name__,		token_type_ids=__magic_name__							)
       UpperCamelCase__							:				Optional[int]          =      model(__magic_name__							)

       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,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      List[str]:






       """simple docstring"""
       UpperCamelCase__							:				List[str]          =      MegatronBertForMaskedLM(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:				List[Any]          =      model(__magic_name__,		attention_mask=__magic_name__,		token_type_ids=__magic_name__,		labels=__magic_name__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.vocab_size)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      str:






       """simple docstring"""
       UpperCamelCase__							:				Dict          =      MegatronBertForCausalLM(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:				List[str]          =      model(__magic_name__,		attention_mask=__magic_name__,		token_type_ids=__magic_name__,		labels=__magic_name__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.vocab_size)							)




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






       """simple docstring"""
       UpperCamelCase__							:				str          =      MegatronBertForNextSentencePrediction(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:				Dict          =      model(
           __magic_name__,		attention_mask=__magic_name__,		token_type_ids=__magic_name__,		labels=__magic_name__,		)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, 2)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      MegatronBertForPreTraining(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:				Optional[int]          =      model(
           __magic_name__,		attention_mask=__magic_name__,		token_type_ids=__magic_name__,		labels=__magic_name__,		next_sentence_label=__magic_name__,		)
       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       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				List[str]          =      MegatronBertForQuestionAnswering(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:				Optional[int]          =      model(
           __magic_name__,		attention_mask=__magic_name__,		token_type_ids=__magic_name__,		start_positions=__magic_name__,		end_positions=__magic_name__,		)
       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       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      self.num_labels
       UpperCamelCase__							:				Union[str, Any]          =      MegatronBertForSequenceClassification(__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:				List[Any]          =      model(__magic_name__,		attention_mask=__magic_name__,		token_type_ids=__magic_name__,		labels=__magic_name__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_labels)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Any:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      self.num_labels
       UpperCamelCase__							:				int          =      MegatronBertForTokenClassification(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:				Any          =      model(__magic_name__,		attention_mask=__magic_name__,		token_type_ids=__magic_name__,		labels=__magic_name__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.num_labels)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      self.num_choices
       UpperCamelCase__							:				Optional[int]          =      MegatronBertForMultipleChoice(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:				Tuple          =      input_ids.unsqueeze(1							).expand(-1,		self.num_choices,		-1							).contiguous()
       UpperCamelCase__							:				List[str]          =      token_type_ids.unsqueeze(1							).expand(-1,		self.num_choices,		-1							).contiguous()
       UpperCamelCase__							:				Any          =      input_mask.unsqueeze(1							).expand(-1,		self.num_choices,		-1							).contiguous()
       UpperCamelCase__							:				Dict          =      model(
           __magic_name__,		attention_mask=__magic_name__,		token_type_ids=__magic_name__,		labels=__magic_name__,		)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_choices)							)




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






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






@require_torch
class     lowercase__       (	__lowerCamelCase			,				__lowerCamelCase			,				unittest.TestCase				):

   '''simple docstring'''


   a						:							Optional[int]   		=		(
       (
           MegatronBertModel,
           MegatronBertForMaskedLM,
           MegatronBertForCausalLM,
           MegatronBertForMultipleChoice,
           MegatronBertForNextSentencePrediction,
           MegatronBertForPreTraining,
           MegatronBertForQuestionAnswering,
           MegatronBertForSequenceClassification,
           MegatronBertForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   a						:							Optional[Any]   		=		(
       {
           "feature-extraction": MegatronBertModel,
           "fill-mask": MegatronBertForMaskedLM,
           "question-answering": MegatronBertForQuestionAnswering,
           "text-classification": MegatronBertForSequenceClassification,
           "text-generation": MegatronBertForCausalLM,
           "token-classification": MegatronBertForTokenClassification,
           "zero-shot": MegatronBertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   a						:							Any   		=		True
   # test_resize_embeddings = False
   a						:							Union[str, Any]   		=		False




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__=False							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      super()._prepare_for_class(__magic_name__,		__magic_name__,		return_labels=__magic_name__							)

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




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






       """simple docstring"""
       UpperCamelCase__							:				List[str]          =      MegatronBertModelTester(self							)
       UpperCamelCase__							:				List[Any]          =      ConfigTester(self,		config_class=__magic_name__,		hidden_size=37							)




   def       UpperCamelCase__  (    self							) ->      List[str]:






       """simple docstring"""
       self.config_tester.run_common_tests()




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






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_model(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      List[str]:






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_masked_lm(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      str:






       """simple docstring"""
       UpperCamelCase__							:				Tuple          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				List[str]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_pretraining(*__magic_name__							)




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






       """simple docstring"""
       UpperCamelCase__							:				Dict          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_question_answering(*__magic_name__							)




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






       """simple docstring"""
       UpperCamelCase__							:				int          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_token_classification(*__magic_name__							)




def 		lowerCAmelCase_    (  __UpperCAmelCase:			Union[str, Any]      )      ->	str:
    return torch.tensor(
        __UpperCAmelCase		,   dtype=torch.long		,   device=__UpperCAmelCase		,   )


UpperCAmelCase_     							= 1e-4






@require_torch
@require_sentencepiece
@require_tokenizers
class     lowercase__       (	unittest.TestCase				):

   '''simple docstring'''


   @slow
   @unittest.skip('''Model is not available.'''							)
   def       UpperCamelCase__  (    self							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      '''nvidia/megatron-bert-uncased-345m'''
       if "MYDIR" in os.environ:
           UpperCamelCase__							:				List[str]          =      os.path.join(os.environ['''MYDIR'''],		__magic_name__							)
       UpperCamelCase__							:				List[str]          =      MegatronBertModel.from_pretrained(__magic_name__							)
       model.to(__magic_name__							)
       model.half()
       UpperCamelCase__							:				str          =      _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]							)
       with torch.no_grad():
           UpperCamelCase__							:				int          =      model(__magic_name__							)[0]
       UpperCamelCase__							:				int          =      torch.Size((1, 9, 1024)							)
       self.assertEqual(output.shape,		__magic_name__							)

       UpperCamelCase__							:				Optional[Any]          =      [-0.6040, -0.2517, -0.1025, 0.3420, -0.6758, -0.0017, -0.1089, -0.1990, 0.5728]
       for ii in range(3							):
           for jj in range(3							):
               UpperCamelCase__							:				List[Any]          =      output[0, ii, jj]
               UpperCamelCase__							:				int          =      expected[3 * ii + jj]
               UpperCamelCase__							:				int          =      '''ii={} jj={} a={} b={}'''.format(__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							)
               self.assertTrue(math.isclose(__magic_name__,		__magic_name__,		rel_tol=__magic_name__,		abs_tol=__magic_name__							),		msg=__magic_name__							)








from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
  import tensorflow as tf

  from transformers import (
      TFConvBertForMaskedLM,
      TFConvBertForMultipleChoice,
      TFConvBertForQuestionAnswering,
      TFConvBertForSequenceClassification,
      TFConvBertForTokenClassification,
      TFConvBertModel,
  )






class     lowercase__       :

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__=13,		__magic_name__=7,		__magic_name__=True,		__magic_name__=True,		__magic_name__=True,		__magic_name__=True,		__magic_name__=99,		__magic_name__=32,		__magic_name__=2,		__magic_name__=4,		__magic_name__=37,		__magic_name__="gelu",		__magic_name__=0.1,		__magic_name__=0.1,		__magic_name__=512,		__magic_name__=16,		__magic_name__=2,		__magic_name__=0.02,		__magic_name__=3,		__magic_name__=4,		__magic_name__=None,		) ->      List[str]:






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      parent
       UpperCamelCase__							:				int          =      13
       UpperCamelCase__							:				Optional[Any]          =      7
       UpperCamelCase__							:				Dict          =      True
       UpperCamelCase__							:				Dict          =      True
       UpperCamelCase__							:				str          =      True
       UpperCamelCase__							:				List[str]          =      True
       UpperCamelCase__							:				Tuple          =      99
       UpperCamelCase__							:				Any          =      384
       UpperCamelCase__							:				Tuple          =      2
       UpperCamelCase__							:				int          =      4
       UpperCamelCase__							:				Optional[int]          =      37
       UpperCamelCase__							:				str          =      '''gelu'''
       UpperCamelCase__							:				Any          =      0.1
       UpperCamelCase__							:				Dict          =      0.1
       UpperCamelCase__							:				Tuple          =      512
       UpperCamelCase__							:				int          =      16
       UpperCamelCase__							:				int          =      2
       UpperCamelCase__							:				Any          =      0.02
       UpperCamelCase__							:				Optional[Any]          =      3
       UpperCamelCase__							:				Any          =      4
       UpperCamelCase__							:				Any          =      128
       UpperCamelCase__							:				List[str]          =      2
       UpperCamelCase__							:				List[str]          =      9
       UpperCamelCase__							:				Optional[int]          =      1
       UpperCamelCase__							:				List[Any]          =      None




   def       UpperCamelCase__  (    self							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				Tuple          =      ids_tensor([self.batch_size, self.seq_length],		self.vocab_size							)

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

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

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

       UpperCamelCase__							:				Any          =      ConvBertConfig(
           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,		initializer_range=self.initializer_range,		return_dict=__magic_name__,		)

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




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				Tuple          =      TFConvBertModel(config=__magic_name__							)
       UpperCamelCase__							:				str          =      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

       UpperCamelCase__							:				Any          =      [input_ids, input_mask]
       UpperCamelCase__							:				int          =      model(__magic_name__							)

       UpperCamelCase__							:				int          =      model(__magic_name__							)

       self.parent.assertEqual(result.last_hidden_state.shape,		(self.batch_size, self.seq_length, self.hidden_size)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      int:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      TFConvBertForMaskedLM(config=__magic_name__							)
       UpperCamelCase__							:				List[str]          =      {
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }
       UpperCamelCase__							:				Tuple          =      model(__magic_name__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.vocab_size)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				str          =      self.num_labels
       UpperCamelCase__							:				Optional[int]          =      TFConvBertForSequenceClassification(config=__magic_name__							)
       UpperCamelCase__							:				str          =      {
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }

       UpperCamelCase__							:				Any          =      model(__magic_name__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_labels)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				int          =      self.num_choices
       UpperCamelCase__							:				str          =      TFConvBertForMultipleChoice(config=__magic_name__							)
       UpperCamelCase__							:				Optional[int]          =      tf.tile(tf.expand_dims(__magic_name__,		1							),		(1, self.num_choices, 1)							)
       UpperCamelCase__							:				Any          =      tf.tile(tf.expand_dims(__magic_name__,		1							),		(1, self.num_choices, 1)							)
       UpperCamelCase__							:				Tuple          =      tf.tile(tf.expand_dims(__magic_name__,		1							),		(1, self.num_choices, 1)							)
       UpperCamelCase__							:				int          =      {
           '''input_ids''': multiple_choice_inputs_ids,
           '''attention_mask''': multiple_choice_input_mask,
           '''token_type_ids''': multiple_choice_token_type_ids,
       }
       UpperCamelCase__							:				Tuple          =      model(__magic_name__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_choices)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      self.num_labels
       UpperCamelCase__							:				Any          =      TFConvBertForTokenClassification(config=__magic_name__							)
       UpperCamelCase__							:				str          =      {
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }
       UpperCamelCase__							:				Dict          =      model(__magic_name__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.num_labels)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      str:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      TFConvBertForQuestionAnswering(config=__magic_name__							)
       UpperCamelCase__							:				Optional[Any]          =      {
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }

       UpperCamelCase__							:				List[str]          =      model(__magic_name__							)
       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       UpperCamelCase__  (    self							) ->      str:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,
       )							:				Any          =      config_and_inputs
       UpperCamelCase__							:				List[str]          =      {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
       return config, inputs_dict






@require_tf
class     lowercase__       (	__lowerCamelCase			,				__lowerCamelCase			,				unittest.TestCase				):

   '''simple docstring'''


   a						:							Union[str, Any]   		=		(
       (
           TFConvBertModel,
           TFConvBertForMaskedLM,
           TFConvBertForQuestionAnswering,
           TFConvBertForSequenceClassification,
           TFConvBertForTokenClassification,
           TFConvBertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   a						:							int   		=		(
       {
           "feature-extraction": TFConvBertModel,
           "fill-mask": TFConvBertForMaskedLM,
           "question-answering": TFConvBertForQuestionAnswering,
           "text-classification": TFConvBertForSequenceClassification,
           "token-classification": TFConvBertForTokenClassification,
           "zero-shot": TFConvBertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   a						:							Optional[Any]   		=		False
   a						:							List[str]   		=		False
   a						:							List[Any]   		=		False




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






       """simple docstring"""
       UpperCamelCase__							:				List[str]          =      TFConvBertModelTester(self							)
       UpperCamelCase__							:				Optional[int]          =      ConfigTester(self,		config_class=__magic_name__,		hidden_size=37							)




   def       UpperCamelCase__  (    self							) ->      int:






       """simple docstring"""
       self.config_tester.run_common_tests()




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






       """simple docstring"""
       UpperCamelCase__							:				Any          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				str          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*__magic_name__							)




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






       """simple docstring"""
       UpperCamelCase__							:				str          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				Optional[int]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				str          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*__magic_name__							)




   @slow
   def       UpperCamelCase__  (    self							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__   ,UpperCamelCase__							:				Tuple          =      self.model_tester.prepare_config_and_inputs_for_common()
       UpperCamelCase__							:				Union[str, Any]          =      True
       UpperCamelCase__							:				Optional[int]          =      True

       if hasattr(__magic_name__,		'''use_cache'''							):
           UpperCamelCase__							:				Union[str, Any]          =      True

       UpperCamelCase__							:				Tuple          =      getattr(self.model_tester,		'''encoder_seq_length''',		self.model_tester.seq_length							)
       UpperCamelCase__							:				Any          =      getattr(self.model_tester,		'''key_length''',		__magic_name__							)

       for model_class in self.all_model_classes:
           UpperCamelCase__							:				Optional[int]          =      self._prepare_for_class(__magic_name__,		__magic_name__							)
           UpperCamelCase__							:				Any          =      model_class(__magic_name__							)
           UpperCamelCase__							:				List[str]          =      len(model(__magic_name__							)							)

           with tempfile.TemporaryDirectory() as tmpdirname:
               model.save_pretrained(__magic_name__,		saved_model=__magic_name__							)
               UpperCamelCase__							:				List[Any]          =      os.path.join(__magic_name__,		'''saved_model''',		'''1'''							)
               UpperCamelCase__							:				Dict          =      tf.keras.models.load_model(__magic_name__							)
               UpperCamelCase__							:				Optional[int]          =      model(__magic_name__							)

               if self.is_encoder_decoder:
                   UpperCamelCase__							:				Union[str, Any]          =      outputs['''encoder_hidden_states''']
                   UpperCamelCase__							:				Any          =      outputs['''encoder_attentions''']
               else:
                   UpperCamelCase__							:				Optional[Any]          =      outputs['''hidden_states''']
                   UpperCamelCase__							:				Dict          =      outputs['''attentions''']

               self.assertEqual(len(__magic_name__							),		__magic_name__							)

               UpperCamelCase__							:				Optional[Any]          =      getattr(
                   self.model_tester,		'''expected_num_hidden_layers''',		self.model_tester.num_hidden_layers + 1							)

               self.assertEqual(len(__magic_name__							),		__magic_name__							)
               self.assertListEqual(
                   list(output_hidden_states[0].shape[-2:]							),		[self.model_tester.seq_length, self.model_tester.hidden_size],		)

               self.assertEqual(len(__magic_name__							),		self.model_tester.num_hidden_layers							)
               self.assertListEqual(
                   list(output_attentions[0].shape[-3:]							),		[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length],		)




   @slow
   def       UpperCamelCase__  (    self							) ->      str:






       """simple docstring"""
       UpperCamelCase__							:				str          =      TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''							)
       self.assertIsNotNone(__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__   ,UpperCamelCase__							:				int          =      self.model_tester.prepare_config_and_inputs_for_common()
       UpperCamelCase__							:				str          =      True
       UpperCamelCase__							:				Union[str, Any]          =      getattr(self.model_tester,		'''decoder_seq_length''',		self.model_tester.seq_length							)
       UpperCamelCase__							:				str          =      getattr(self.model_tester,		'''encoder_seq_length''',		self.model_tester.seq_length							)
       UpperCamelCase__							:				List[str]          =      getattr(self.model_tester,		'''key_length''',		__magic_name__							)
       UpperCamelCase__							:				int          =      getattr(self.model_tester,		'''key_length''',		__magic_name__							)

       def check_decoder_attentions_output(__magic_name__							):
           UpperCamelCase__							:				List[str]          =      len(__magic_name__							)
           self.assertEqual(out_len % 2,		0							)
           UpperCamelCase__							:				Optional[Any]          =      outputs.decoder_attentions
           self.assertEqual(len(__magic_name__							),		self.model_tester.num_hidden_layers							)
           self.assertListEqual(
               list(decoder_attentions[0].shape[-3:]							),		[self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length],		)

       def check_encoder_attentions_output(__magic_name__							):
           UpperCamelCase__							:				int          =      [
               t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
           ]
           self.assertEqual(len(__magic_name__							),		self.model_tester.num_hidden_layers							)
           self.assertListEqual(
               list(attentions[0].shape[-3:]							),		[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length],		)

       for model_class in self.all_model_classes:
           UpperCamelCase__							:				Dict          =      True
           UpperCamelCase__							:				str          =      False
           UpperCamelCase__							:				Optional[Any]          =      model_class(__magic_name__							)
           UpperCamelCase__							:				Dict          =      model(self._prepare_for_class(__magic_name__,		__magic_name__							)							)
           UpperCamelCase__							:				int          =      len(__magic_name__							)
           self.assertEqual(config.output_hidden_states,		__magic_name__							)
           check_encoder_attentions_output(__magic_name__							)

           if self.is_encoder_decoder:
               UpperCamelCase__							:				str          =      model_class(__magic_name__							)
               UpperCamelCase__							:				List[str]          =      model(self._prepare_for_class(__magic_name__,		__magic_name__							)							)
               self.assertEqual(config.output_hidden_states,		__magic_name__							)
               check_decoder_attentions_output(__magic_name__							)

           # Check that output attentions can also be changed via the config
           del inputs_dict["output_attentions"]
           UpperCamelCase__							:				List[Any]          =      True
           UpperCamelCase__							:				Dict          =      model_class(__magic_name__							)
           UpperCamelCase__							:				Optional[Any]          =      model(self._prepare_for_class(__magic_name__,		__magic_name__							)							)
           self.assertEqual(config.output_hidden_states,		__magic_name__							)
           check_encoder_attentions_output(__magic_name__							)

           # Check attention is always last and order is fine
           UpperCamelCase__							:				List[Any]          =      True
           UpperCamelCase__							:				List[Any]          =      True
           UpperCamelCase__							:				Dict          =      model_class(__magic_name__							)
           UpperCamelCase__							:				List[Any]          =      model(self._prepare_for_class(__magic_name__,		__magic_name__							)							)

           self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1),		len(__magic_name__							)							)
           self.assertEqual(model.config.output_hidden_states,		__magic_name__							)
           check_encoder_attentions_output(__magic_name__							)






@require_tf
class     lowercase__       (	unittest.TestCase				):

   '''simple docstring'''


   @slow
   def       UpperCamelCase__  (    self							) ->      int:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''							)
       UpperCamelCase__							:				Tuple          =      tf.constant([[0, 1, 2, 3, 4, 5]]							)
       UpperCamelCase__							:				int          =      model(__magic_name__							)[0]

       UpperCamelCase__							:				Dict          =      [1, 6, 768]
       self.assertEqual(output.shape,		__magic_name__							)

       UpperCamelCase__							:				Union[str, Any]          =      tf.constant(
           [
               [
                   [-0.0347_5493, -0.468_6034, -0.3063_8832],
                   [0.2263_7248, -0.2698_8646, -0.742_3424],
                   [0.1032_4868, -0.4501_3508, -0.5828_0784],
               ]
           ]							)
       tf.debugging.assert_near(output[:, :3, :3],		__magic_name__,		atol=1E-4							)

