

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

   '''simple docstring'''




   def __init__(				self,    lowercase_,    lowercase_=13,    lowercase_=7,    lowercase_=True,    lowercase_=True,    lowercase_=True,    lowercase_=True,    lowercase_=99,    lowercase_=32,    lowercase_=2,    lowercase_=4,    lowercase_=37,    lowercase_="gelu",    lowercase_=0.1,    lowercase_=0.1,    lowercase_=512,    lowercase_=16,    lowercase_=2,    lowercase_=0.02,    lowercase_=3,    lowercase_=4,    lowercase_=None,    )							->    int:






     """simple docstring"""






     a__  =parent
     a__  =13
     a__  =7
     a__  =True
     a__  =True
     a__  =True
     a__  =True
     a__  =99
     a__  =384
     a__  =2
     a__  =4
     a__  =37
     a__  ='''gelu'''
     a__  =0.1
     a__  =0.1
     a__  =512
     a__  =16
     a__  =2
     a__  =0.02
     a__  =3
     a__  =4
     a__  =128
     a__  =2
     a__  =9
     a__  =1
     a__  =None



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






     """simple docstring"""






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

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

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

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

     a__  =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=lowercase_,    )

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



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_       )							->    Tuple:






     """simple docstring"""






     a__  =TFConvBertModel(config=lowercase_       )
     a__  ={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

     a__  =[input_ids, input_mask]
     a__  =model(lowercase_       )

     a__  =model(lowercase_       )

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



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






     """simple docstring"""






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



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_       )							->    Optional[int]:






     """simple docstring"""






     a__  =self.num_labels
     a__  =TFConvBertForSequenceClassification(config=lowercase_       )
     a__  ={
         '''input_ids''': input_ids,
         '''attention_mask''': input_mask,
         '''token_type_ids''': token_type_ids,
     }

     a__  =model(lowercase_       )
     self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.num_labels)       )



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_       )							->    List[Any]:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_       )							->    int:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_       )							->    str:






     """simple docstring"""






     a__  =TFConvBertForQuestionAnswering(config=lowercase_       )
     a__  ={
         '''input_ids''': input_ids,
         '''attention_mask''': input_mask,
         '''token_type_ids''': token_type_ids,
     }

     a__  =model(lowercase_       )
     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       )							->    int:






     """simple docstring"""






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







@require_tf
class 						__magic_name__			(							lowerCamelCase__		,       lowerCamelCase__		,       unittest.TestCase							):

   '''simple docstring'''




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



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






     """simple docstring"""






     a__  =TFConvBertModelTester(self       )
     a__  =ConfigTester(self,    config_class=lowercase_,    hidden_size=37       )



   def  _UpperCAmelCase (				self       )							->    str:






     """simple docstring"""






     self.config_tester.run_common_tests()



   def  _UpperCAmelCase (				self       )							->    Dict:






     """simple docstring"""






     a__  =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_       )



   def  _UpperCAmelCase (				self       )							->    str:






     """simple docstring"""






     a__  =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*lowercase_       )



   def  _UpperCAmelCase (				self       )							->    Tuple:






     """simple docstring"""






     a__  =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*lowercase_       )



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






     """simple docstring"""






     a__  =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*lowercase_       )



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






     """simple docstring"""






     a__  =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*lowercase_       )



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






     """simple docstring"""






     a__  =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*lowercase_       )



   @slow
   def  _UpperCAmelCase (				self       )							->    str:






     """simple docstring"""






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

     if hasattr(lowercase_,    '''use_cache'''       ):
       a__  =True

     a__  =getattr(self.model_tester,    '''encoder_seq_length''',    self.model_tester.seq_length       )
     a__  =getattr(self.model_tester,    '''key_length''',    lowercase_       )

     for model_class in self.all_model_classes:
       a__  =self._prepare_for_class(lowercase_,    lowercase_       )
       a__  =model_class(lowercase_       )
       a__  =len(model(lowercase_       )       )

       with tempfile.TemporaryDirectory() as tmpdirname:
         model.save_pretrained(lowercase_,    saved_model=lowercase_       )
         a__  =os.path.join(lowercase_,    '''saved_model''',    '''1'''       )
         a__  =tf.keras.models.load_model(lowercase_       )
         a__  =model(lowercase_       )

         if self.is_encoder_decoder:
           a__  =outputs['''encoder_hidden_states''']
           a__  =outputs['''encoder_attentions''']
         else:
           a__  =outputs['''hidden_states''']
           a__  =outputs['''attentions''']

         self.assertEqual(len(lowercase_       ),    lowercase_       )

         a__  =getattr(
             self.model_tester,    '''expected_num_hidden_layers''',    self.model_tester.num_hidden_layers + 1       )

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

         self.assertEqual(len(lowercase_       ),    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       )							->    List[Any]:






     """simple docstring"""






     a__  =TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''       )
     self.assertIsNotNone(lowercase_       )



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






     """simple docstring"""






     a__,      a__  =self.model_tester.prepare_config_and_inputs_for_common()
     a__  =True
     a__  =getattr(self.model_tester,    '''decoder_seq_length''',    self.model_tester.seq_length       )
     a__  =getattr(self.model_tester,    '''encoder_seq_length''',    self.model_tester.seq_length       )
     a__  =getattr(self.model_tester,    '''key_length''',    lowercase_       )
     a__  =getattr(self.model_tester,    '''key_length''',    lowercase_       )

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

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

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

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

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







@require_tf
class 						__magic_name__			(							unittest.TestCase							):

   '''simple docstring'''




   @slow
   def  _UpperCAmelCase (				self       )							->    Tuple:






     """simple docstring"""






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

     a__  =[1, 6, 768]
     self.assertEqual(output.shape,    lowercase_       )

     a__  =tf.constant(
         [
             [
                 [-0.03475493, -0.4686034, -0.30638832],
                 [0.22637248, -0.26988646, -0.7423424],
                 [0.10324868, -0.45013508, -0.58280784],
             ]
         ]       )
     tf.debugging.assert_near(output[:, :3, :3],    lowercase_,    atol=1E-4       )



from __future__ import annotations




def 			UpperCAmelCase__				(			_A      : float	,      _A      : float	,      _A      : float	,      ):





  '''simple docstring'''







  if (electron_conc, hole_conc, intrinsic_conc).count(0							) != 1:
    raise ValueError('''You cannot supply more or less than 2 values'''							)
  elif electron_conc < 0:
    raise ValueError('''Electron concentration cannot be negative in a semiconductor'''							)
  elif hole_conc < 0:
    raise ValueError('''Hole concentration cannot be negative in a semiconductor'''							)
  elif intrinsic_conc < 0:
    raise ValueError(
        '''Intrinsic concentration cannot be negative in a semiconductor'''							)
  elif electron_conc == 0:
    return (
        "electron_conc",
        intrinsic_conc**2 / hole_conc,
    )
  elif hole_conc == 0:
    return (
        "hole_conc",
        intrinsic_conc**2 / electron_conc,
    )
  elif intrinsic_conc == 0:
    return (
        "intrinsic_conc",
        (electron_conc * hole_conc) ** 0.5,
    )
  else:
    return (-1, -1)


if __name__ == "__main__":
       import doctest

       doctest.testmod()
