"""simple docstring"""


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


    def __init__(       self						:	Union[str, Any] ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	Tuple=13 ,					__lowerCAmelCase						:	List[Any]=7 ,					__lowerCAmelCase						:	Union[str, Any]=True ,					__lowerCAmelCase						:	Tuple=True ,					__lowerCAmelCase						:	Optional[int]=True ,					__lowerCAmelCase						:	int=True ,					__lowerCAmelCase						:	Optional[Any]=99 ,					__lowerCAmelCase						:	Dict=32 ,					__lowerCAmelCase						:	int=2 ,					__lowerCAmelCase						:	Optional[Any]=4 ,					__lowerCAmelCase						:	Optional[int]=37 ,					__lowerCAmelCase						:	List[str]="gelu" ,					__lowerCAmelCase						:	Union[str, Any]=0.1 ,					__lowerCAmelCase						:	Tuple=0.1 ,					__lowerCAmelCase						:	Dict=512 ,					__lowerCAmelCase						:	Union[str, Any]=16 ,					__lowerCAmelCase						:	List[str]=2 ,					__lowerCAmelCase						:	Dict=0.02 ,					__lowerCAmelCase						:	str=3 ,					__lowerCAmelCase						:	Tuple=4 ,					__lowerCAmelCase						:	Any=None ,					):
      _UpperCAmelCase    			=   parent
      _UpperCAmelCase    			=   13
      _UpperCAmelCase    			=   7
      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   99
      _UpperCAmelCase    			=   384
      _UpperCAmelCase    			=   2
      _UpperCAmelCase    			=   4
      _UpperCAmelCase    			=   37
      _UpperCAmelCase    			=   """gelu"""
      _UpperCAmelCase    			=   0.1
      _UpperCAmelCase    			=   0.1
      _UpperCAmelCase    			=   512
      _UpperCAmelCase    			=   16
      _UpperCAmelCase    			=   2
      _UpperCAmelCase    			=   0.02
      _UpperCAmelCase    			=   3
      _UpperCAmelCase    			=   4
      _UpperCAmelCase    			=   128
      _UpperCAmelCase    			=   2
      _UpperCAmelCase    			=   9
      _UpperCAmelCase    			=   1
      _UpperCAmelCase    			=   None


    def   lowerCAmelCase_	(       self						:	Optional[int]       ):
      _UpperCAmelCase    			=   ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size       )

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

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

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

      _UpperCAmelCase    			=   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=__lowerCAmelCase ,					)

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


    def   lowerCAmelCase_	(       self						:	Dict ,					__lowerCAmelCase						:	Optional[int] ,					__lowerCAmelCase						:	Any ,					__lowerCAmelCase						:	int ,					__lowerCAmelCase						:	int ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	Any ,					__lowerCAmelCase						:	List[str]       ):
      _UpperCAmelCase    			=   TFConvBertModel(config=__lowerCAmelCase       )
      _UpperCAmelCase    			=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

      _UpperCAmelCase    			=   [input_ids, input_mask]
      _UpperCAmelCase    			=   model(__lowerCAmelCase       )

      _UpperCAmelCase    			=   model(__lowerCAmelCase       )

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


    def   lowerCAmelCase_	(       self						:	List[Any] ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	Tuple ,					__lowerCAmelCase						:	Union[str, Any] ,					__lowerCAmelCase						:	str ,					__lowerCAmelCase						:	Dict ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	Union[str, Any]       ):
      _UpperCAmelCase    			=   TFConvBertForMaskedLM(config=__lowerCAmelCase       )
      _UpperCAmelCase    			=   {
          """input_ids""": input_ids,
          """attention_mask""": input_mask,
          """token_type_ids""": token_type_ids,
      }
      _UpperCAmelCase    			=   model(__lowerCAmelCase       )
      self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.vocab_size)       )


    def   lowerCAmelCase_	(       self						:	Union[str, Any] ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	Union[str, Any] ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	List[Any]       ):
      _UpperCAmelCase    			=   self.num_labels
      _UpperCAmelCase    			=   TFConvBertForSequenceClassification(config=__lowerCAmelCase       )
      _UpperCAmelCase    			=   {
          """input_ids""": input_ids,
          """attention_mask""": input_mask,
          """token_type_ids""": token_type_ids,
      }

      _UpperCAmelCase    			=   model(__lowerCAmelCase       )
      self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)       )


    def   lowerCAmelCase_	(       self						:	List[str] ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	str ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	Dict       ):
      _UpperCAmelCase    			=   self.num_choices
      _UpperCAmelCase    			=   TFConvBertForMultipleChoice(config=__lowerCAmelCase       )
      _UpperCAmelCase    			=   tf.tile(tf.expand_dims(__lowerCAmelCase ,					1       ) ,					(1, self.num_choices, 1)       )
      _UpperCAmelCase    			=   tf.tile(tf.expand_dims(__lowerCAmelCase ,					1       ) ,					(1, self.num_choices, 1)       )
      _UpperCAmelCase    			=   tf.tile(tf.expand_dims(__lowerCAmelCase ,					1       ) ,					(1, self.num_choices, 1)       )
      _UpperCAmelCase    			=   {
          """input_ids""": multiple_choice_inputs_ids,
          """attention_mask""": multiple_choice_input_mask,
          """token_type_ids""": multiple_choice_token_type_ids,
      }
      _UpperCAmelCase    			=   model(__lowerCAmelCase       )
      self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_choices)       )


    def   lowerCAmelCase_	(       self						:	str ,					__lowerCAmelCase						:	Union[str, Any] ,					__lowerCAmelCase						:	Any ,					__lowerCAmelCase						:	str ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	Optional[int] ,					__lowerCAmelCase						:	Dict ,					__lowerCAmelCase						:	List[Any]       ):
      _UpperCAmelCase    			=   self.num_labels
      _UpperCAmelCase    			=   TFConvBertForTokenClassification(config=__lowerCAmelCase       )
      _UpperCAmelCase    			=   {
          """input_ids""": input_ids,
          """attention_mask""": input_mask,
          """token_type_ids""": token_type_ids,
      }
      _UpperCAmelCase    			=   model(__lowerCAmelCase       )
      self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.num_labels)       )


    def   lowerCAmelCase_	(       self						:	Optional[int] ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	str ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	Dict ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	List[Any]       ):
      _UpperCAmelCase    			=   TFConvBertForQuestionAnswering(config=__lowerCAmelCase       )
      _UpperCAmelCase    			=   {
          """input_ids""": input_ids,
          """attention_mask""": input_mask,
          """token_type_ids""": token_type_ids,
      }

      _UpperCAmelCase    			=   model(__lowerCAmelCase       )
      self.parent.assertEqual(result.start_logits.shape ,					(self.batch_size, self.seq_length)       )
      self.parent.assertEqual(result.end_logits.shape ,					(self.batch_size, self.seq_length)       )


    def   lowerCAmelCase_	(       self						:	Tuple       ):
      _UpperCAmelCase    			=   self.prepare_config_and_inputs()
      (
          (
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					
      )    			=   config_and_inputs
      _UpperCAmelCase    			=   {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
      return config, inputs_dict



@require_tf
class 							a					(		lowerCAmelCase_						,				lowerCAmelCase_						,				unittest.TestCase ):
    _snake_case  :	str  				=				(
        (
            TFConvBertModel,
            TFConvBertForMaskedLM,
            TFConvBertForQuestionAnswering,
            TFConvBertForSequenceClassification,
            TFConvBertForTokenClassification,
            TFConvBertForMultipleChoice,
        )
        if is_tf_available()
        else ()
    )
    _snake_case  :	List[Any]  				=				(
        {
            'feature-extraction': TFConvBertModel,
            'fill-mask': TFConvBertForMaskedLM,
            'question-answering': TFConvBertForQuestionAnswering,
            'text-classification': TFConvBertForSequenceClassification,
            'token-classification': TFConvBertForTokenClassification,
            'zero-shot': TFConvBertForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    _snake_case  :	Any  				=				False
    _snake_case  :	Optional[Any]  				=				False
    _snake_case  :	int  				=				False


    def   lowerCAmelCase_	(       self						:	List[str]       ):
      _UpperCAmelCase    			=   TFConvBertModelTester(self       )
      _UpperCAmelCase    			=   ConfigTester(self ,					config_class=__lowerCAmelCase ,					hidden_size=37       )


    def   lowerCAmelCase_	(       self						:	List[Any]       ):
      self.config_tester.run_common_tests()


    def   lowerCAmelCase_	(       self						:	List[Any]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	Any       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	Optional[Any]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	int       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	Any       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase       )


    @slow
    def   lowerCAmelCase_	(       self						:	Optional[Any]       ):
      _UpperCAmelCase			,					_UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs_for_common()
      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   True

      if hasattr(__lowerCAmelCase ,					"""use_cache"""       ):
        _UpperCAmelCase    			=   True

      _UpperCAmelCase    			=   getattr(self.model_tester ,					"""encoder_seq_length""" ,					self.model_tester.seq_length       )
      _UpperCAmelCase    			=   getattr(self.model_tester ,					"""key_length""" ,					__lowerCAmelCase       )

      for model_class in self.all_model_classes:
        _UpperCAmelCase    			=   self._prepare_for_class(__lowerCAmelCase ,					__lowerCAmelCase       )
        _UpperCAmelCase    			=   model_class(__lowerCAmelCase       )
        _UpperCAmelCase    			=   len(model(__lowerCAmelCase       )       )

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

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

          self.assertEqual(len(__lowerCAmelCase       ) ,					__lowerCAmelCase       )

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

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

          self.assertEqual(len(__lowerCAmelCase       ) ,					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   lowerCAmelCase_	(       self						:	str       ):
      _UpperCAmelCase    			=   TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""       )
      self.assertIsNotNone(__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	Any       ):
      _UpperCAmelCase			,					_UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs_for_common()
      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   getattr(self.model_tester ,					"""decoder_seq_length""" ,					self.model_tester.seq_length       )
      _UpperCAmelCase    			=   getattr(self.model_tester ,					"""encoder_seq_length""" ,					self.model_tester.seq_length       )
      _UpperCAmelCase    			=   getattr(self.model_tester ,					"""key_length""" ,					__lowerCAmelCase       )
      _UpperCAmelCase    			=   getattr(self.model_tester ,					"""key_length""" ,					__lowerCAmelCase       )

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

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

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

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

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



@require_tf
class 							a					(		unittest.TestCase ):


    @slow
    def   lowerCAmelCase_	(       self						:	Any       ):
      _UpperCAmelCase    			=   TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""       )
      _UpperCAmelCase    			=   tf.constant([[0, 1, 2, 3, 4, 5]]       )
      _UpperCAmelCase    			=   model(__lowerCAmelCase       )[0]

      _UpperCAmelCase    			=   [1, 6, 768]
      self.assertEqual(output.shape ,					__lowerCAmelCase       )

      _UpperCAmelCase    			=   tf.constant(
          [
              [
                  [-0.03_475_493, -0.4_686_034, -0.30_638_832],
                  [0.22_637_248, -0.26_988_646, -0.7_423_424],
                  [0.10_324_868, -0.45_013_508, -0.58_280_784],
              ]
          ]       )
      tf.debugging.assert_near(output[:, :3, :3] ,					__lowerCAmelCase ,					atol=1e-4       )

"""simple docstring"""


from typing import TYPE_CHECKING

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


UpperCAmelCase__			=    {
    """configuration_gpt_bigcode""": ["""GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTBigCodeConfig"""],
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCAmelCase__			=    [
        """GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """GPTBigCodeForSequenceClassification""",
        """GPTBigCodeForTokenClassification""",
        """GPTBigCodeForCausalLM""",
        """GPTBigCodeModel""",
        """GPTBigCodePreTrainedModel""",
    ]

if TYPE_CHECKING:
    from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_gpt_bigcode import (
            GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST,
            GPTBigCodeForCausalLM,
            GPTBigCodeForSequenceClassification,
            GPTBigCodeForTokenClassification,
            GPTBigCodeModel,
            GPTBigCodePreTrainedModel,
        )


else:
    import sys

    UpperCAmelCase__			=    _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
