



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  lowerCamelCase__	:
     """simple docstring"""



     def __init__(self					,      __a					,      __a=13					,      __a=7					,      __a=True					,      __a=True					,      __a=True					,      __a=True					,      __a=99					,      __a=32					,      __a=2					,      __a=4					,      __a=37					,      __a="gelu"					,      __a=0.1					,      __a=0.1					,      __a=5_12					,      __a=16					,      __a=2					,      __a=0.02					,      __a=3					,      __a=4					,      __a=None					,      ):






           '''simple docstring'''



           lowerCamelCase             =		parent
           lowerCamelCase             =		13
           lowerCamelCase             =		7
           lowerCamelCase             =		True
           lowerCamelCase             =		True
           lowerCamelCase             =		True
           lowerCamelCase             =		True
           lowerCamelCase             =		99
           lowerCamelCase             =		3_84
           lowerCamelCase             =		2
           lowerCamelCase             =		4
           lowerCamelCase             =		37
           lowerCamelCase             =		"gelu"
           lowerCamelCase             =		0.1
           lowerCamelCase             =		0.1
           lowerCamelCase             =		5_12
           lowerCamelCase             =		16
           lowerCamelCase             =		2
           lowerCamelCase             =		0.02
           lowerCamelCase             =		3
           lowerCamelCase             =		4
           lowerCamelCase             =		1_28
           lowerCamelCase             =		2
           lowerCamelCase             =		9
           lowerCamelCase             =		1
           lowerCamelCase             =		None







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size     )

           lowerCamelCase             =		None
           if self.use_input_mask:
                 lowerCamelCase             =		random_attention_mask([self.batch_size, self.seq_length]     )

           lowerCamelCase             =		None
           if self.use_token_type_ids:
                 lowerCamelCase             =		ids_tensor([self.batch_size, self.seq_length]					,      self.type_vocab_size     )

           lowerCamelCase             =		None
           lowerCamelCase             =		None
           lowerCamelCase             =		None
           if self.use_labels:
                 lowerCamelCase             =		ids_tensor([self.batch_size]					,      self.type_sequence_label_size     )
                 lowerCamelCase             =		ids_tensor([self.batch_size, self.seq_length]					,      self.num_labels     )
                 lowerCamelCase             =		ids_tensor([self.batch_size]					,      self.num_choices     )

           lowerCamelCase             =		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=__a					,      )

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







     def     _a   (self					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		TFConvBertModel(config=__a     )
           lowerCamelCase             =		{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

           lowerCamelCase             =		[input_ids, input_mask]
           lowerCamelCase             =		model(__a     )

           lowerCamelCase             =		model(__a     )

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







     def     _a   (self					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		TFConvBertForMaskedLM(config=__a     )
           lowerCamelCase             =		{
               "input_ids": input_ids,
               "attention_mask": input_mask,
               "token_type_ids": token_type_ids,
           }
           lowerCamelCase             =		model(__a     )
           self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.vocab_size)     )







     def     _a   (self					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.num_labels
           lowerCamelCase             =		TFConvBertForSequenceClassification(config=__a     )
           lowerCamelCase             =		{
               "input_ids": input_ids,
               "attention_mask": input_mask,
               "token_type_ids": token_type_ids,
           }

           lowerCamelCase             =		model(__a     )
           self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)     )







     def     _a   (self					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.num_choices
           lowerCamelCase             =		TFConvBertForMultipleChoice(config=__a     )
           lowerCamelCase             =		tf.tile(tf.expand_dims(__a					,      1     )					,      (1, self.num_choices, 1)     )
           lowerCamelCase             =		tf.tile(tf.expand_dims(__a					,      1     )					,      (1, self.num_choices, 1)     )
           lowerCamelCase             =		tf.tile(tf.expand_dims(__a					,      1     )					,      (1, self.num_choices, 1)     )
           lowerCamelCase             =		{
               "input_ids": multiple_choice_inputs_ids,
               "attention_mask": multiple_choice_input_mask,
               "token_type_ids": multiple_choice_token_type_ids,
           }
           lowerCamelCase             =		model(__a     )
           self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_choices)     )







     def     _a   (self					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.num_labels
           lowerCamelCase             =		TFConvBertForTokenClassification(config=__a     )
           lowerCamelCase             =		{
               "input_ids": input_ids,
               "attention_mask": input_mask,
               "token_type_ids": token_type_ids,
           }
           lowerCamelCase             =		model(__a     )
           self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.num_labels)     )







     def     _a   (self					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		TFConvBertForQuestionAnswering(config=__a     )
           lowerCamelCase             =		{
               "input_ids": input_ids,
               "attention_mask": input_mask,
               "token_type_ids": token_type_ids,
           }

           lowerCamelCase             =		model(__a     )
           self.parent.assertEqual(result.start_logits.shape					,      (self.batch_size, self.seq_length)     )
           self.parent.assertEqual(result.end_logits.shape					,      (self.batch_size, self.seq_length)     )







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.prepare_config_and_inputs()
           (
               (
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				
           )             =		config_and_inputs
           lowerCamelCase             =		{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
           return config, inputs_dict





@require_tf
class  lowerCamelCase__	(    UpperCAmelCase_							,     UpperCAmelCase_							,     unittest.TestCase):
     """simple docstring"""



     _A      =      (
         (
             TFConvBertModel,
             TFConvBertForMaskedLM,
             TFConvBertForQuestionAnswering,
             TFConvBertForSequenceClassification,
             TFConvBertForTokenClassification,
             TFConvBertForMultipleChoice,
         )
         if is_tf_available()
         else ()
     )
     _A      =      (
         {
             'feature-extraction': TFConvBertModel,
             'fill-mask': TFConvBertForMaskedLM,
             'question-answering': TFConvBertForQuestionAnswering,
             'text-classification': TFConvBertForSequenceClassification,
             'token-classification': TFConvBertForTokenClassification,
             'zero-shot': TFConvBertForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     _A      =      False
     _A      =      False
     _A      =      False







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		TFConvBertModelTester(self     )
           lowerCamelCase             =		ConfigTester(self					,      config_class=__a					,      hidden_size=37     )







     def     _a   (self     ):






           '''simple docstring'''



           self.config_tester.run_common_tests()







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__a     )







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*__a     )







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*__a     )







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*__a     )







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*__a     )







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*__a     )







     @slow
     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase      ,				lowerCamelCase             =		self.model_tester.prepare_config_and_inputs_for_common()
           lowerCamelCase             =		True
           lowerCamelCase             =		True

           if hasattr(__a					,      "use_cache"     ):
                 lowerCamelCase             =		True

           lowerCamelCase             =		getattr(self.model_tester					,      "encoder_seq_length"					,      self.model_tester.seq_length     )
           lowerCamelCase             =		getattr(self.model_tester					,      "key_length"					,      __a     )

           for model_class in self.all_model_classes:
                 lowerCamelCase             =		self._prepare_for_class(__a					,      __a     )
                 lowerCamelCase             =		model_class(__a     )
                 lowerCamelCase             =		len(model(__a     )     )

                 with tempfile.TemporaryDirectory() as tmpdirname:
                       model.save_pretrained(__a					,      saved_model=__a     )
                       lowerCamelCase             =		os.path.join(__a					,      "saved_model"					,      "1"     )
                       lowerCamelCase             =		tf.keras.models.load_model(__a     )
                       lowerCamelCase             =		model(__a     )

                       if self.is_encoder_decoder:
                             lowerCamelCase             =		outputs["encoder_hidden_states"]
                             lowerCamelCase             =		outputs["encoder_attentions"]
                       else:
                             lowerCamelCase             =		outputs["hidden_states"]
                             lowerCamelCase             =		outputs["attentions"]

                       self.assertEqual(len(__a     )					,      __a     )

                       lowerCamelCase             =		getattr(
                           self.model_tester					,      "expected_num_hidden_layers"					,      self.model_tester.num_hidden_layers + 1     )

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

                       self.assertEqual(len(__a     )					,      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     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		TFConvBertModel.from_pretrained("YituTech/conv-bert-base"     )
           self.assertIsNotNone(__a     )







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase      ,				lowerCamelCase             =		self.model_tester.prepare_config_and_inputs_for_common()
           lowerCamelCase             =		True
           lowerCamelCase             =		getattr(self.model_tester					,      "decoder_seq_length"					,      self.model_tester.seq_length     )
           lowerCamelCase             =		getattr(self.model_tester					,      "encoder_seq_length"					,      self.model_tester.seq_length     )
           lowerCamelCase             =		getattr(self.model_tester					,      "key_length"					,      __a     )
           lowerCamelCase             =		getattr(self.model_tester					,      "key_length"					,      __a     )

           def check_decoder_attentions_output(__a     ):
                 lowerCamelCase             =		len(__a     )
                 self.assertEqual(out_len % 2					,      0     )
                 lowerCamelCase             =		outputs.decoder_attentions
                 self.assertEqual(len(__a     )					,      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(__a     ):
                 lowerCamelCase             =		[
                     t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
                 ]
                 self.assertEqual(len(__a     )					,      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:
                 lowerCamelCase             =		True
                 lowerCamelCase             =		False
                 lowerCamelCase             =		model_class(__a     )
                 lowerCamelCase             =		model(self._prepare_for_class(__a					,      __a     )     )
                 lowerCamelCase             =		len(__a     )
                 self.assertEqual(config.output_hidden_states					,      __a     )
                 check_encoder_attentions_output(__a     )

                 if self.is_encoder_decoder:
                       lowerCamelCase             =		model_class(__a     )
                       lowerCamelCase             =		model(self._prepare_for_class(__a					,      __a     )     )
                       self.assertEqual(config.output_hidden_states					,      __a     )
                       check_decoder_attentions_output(__a     )

                 # Check that output attentions can also be changed via the config
                 del inputs_dict["output_attentions"]
                 lowerCamelCase             =		True
                 lowerCamelCase             =		model_class(__a     )
                 lowerCamelCase             =		model(self._prepare_for_class(__a					,      __a     )     )
                 self.assertEqual(config.output_hidden_states					,      __a     )
                 check_encoder_attentions_output(__a     )

                 # Check attention is always last and order is fine
                 lowerCamelCase             =		True
                 lowerCamelCase             =		True
                 lowerCamelCase             =		model_class(__a     )
                 lowerCamelCase             =		model(self._prepare_for_class(__a					,      __a     )     )

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





@require_tf
class  lowerCamelCase__	(    unittest.TestCase):
     """simple docstring"""



     @slow
     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		TFConvBertModel.from_pretrained("YituTech/conv-bert-base"     )
           lowerCamelCase             =		tf.constant([[0, 1, 2, 3, 4, 5]]     )
           lowerCamelCase             =		model(__a     )[0]

           lowerCamelCase             =		[1, 6, 7_68]
           self.assertEqual(output.shape					,      __a     )

           lowerCamelCase             =		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]					,      __a					,      atol=1E-4     )




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


a_    :  Tuple						  =		{'configuration_unispeech': ['UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP', 'UniSpeechConfig']}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     a_    :  Union[str, Any]						  =		[
         'UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST',
         'UniSpeechForCTC',
         'UniSpeechForPreTraining',
         'UniSpeechForSequenceClassification',
         'UniSpeechModel',
         'UniSpeechPreTrainedModel',
     ]

if TYPE_CHECKING:
     from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_unispeech import (
              UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST,
              UniSpeechForCTC,
              UniSpeechForPreTraining,
              UniSpeechForSequenceClassification,
              UniSpeechModel,
              UniSpeechPreTrainedModel,
          )

else:
     import sys

     a_    :  Optional[Any]						  =		_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)