




"""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	:  Dict   ,				__lowerCAmelCase	:  List[Any]   ,				__lowerCAmelCase	:  Any=13   ,				__lowerCAmelCase	:  List[Any]=7   ,				__lowerCAmelCase	:  Optional[int]=True   ,				__lowerCAmelCase	:  Union[str, Any]=True   ,				__lowerCAmelCase	:  Dict=True   ,				__lowerCAmelCase	:  Optional[Any]=True   ,				__lowerCAmelCase	:  Union[str, Any]=99   ,				__lowerCAmelCase	:  int=32   ,				__lowerCAmelCase	:  Dict=2   ,				__lowerCAmelCase	:  List[str]=4   ,				__lowerCAmelCase	:  Dict=37   ,				__lowerCAmelCase	:  List[Any]="gelu"   ,				__lowerCAmelCase	:  Union[str, Any]=0.1   ,				__lowerCAmelCase	:  Optional[int]=0.1   ,				__lowerCAmelCase	:  List[Any]=512   ,				__lowerCAmelCase	:  Union[str, Any]=16   ,				__lowerCAmelCase	:  Optional[int]=2   ,				__lowerCAmelCase	:  Union[str, Any]=0.02   ,				__lowerCAmelCase	:  Tuple=3   ,				__lowerCAmelCase	:  Optional[Any]=4   ,				__lowerCAmelCase	:  str=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	:  Dict					):
      _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	:  Dict   ,				__lowerCAmelCase	:  str   ,				__lowerCAmelCase	:  List[Any]   ,				__lowerCAmelCase	:  List[str]   ,				__lowerCAmelCase	:  List[Any]   ,				__lowerCAmelCase	:  str   ,				__lowerCAmelCase	:  Optional[Any]					):
      _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	:  Tuple   ,				__lowerCAmelCase	:  Dict   ,				__lowerCAmelCase	:  str   ,				__lowerCAmelCase	:  Dict   ,				__lowerCAmelCase	:  str   ,				__lowerCAmelCase	:  str   ,				__lowerCAmelCase	:  Dict   ,				__lowerCAmelCase	:  int					):
      _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	:  int   ,				__lowerCAmelCase	:  Union[str, Any]   ,				__lowerCAmelCase	:  str   ,				__lowerCAmelCase	:  Tuple   ,				__lowerCAmelCase	:  Any   ,				__lowerCAmelCase	:  List[Any]   ,				__lowerCAmelCase	:  int   ,				__lowerCAmelCase	:  Union[str, 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	:  Tuple   ,				__lowerCAmelCase	:  List[str]   ,				__lowerCAmelCase	:  Union[str, Any]   ,				__lowerCAmelCase	:  List[str]   ,				__lowerCAmelCase	:  int   ,				__lowerCAmelCase	:  Dict   ,				__lowerCAmelCase	:  Tuple					):
      _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	:  Optional[Any]   ,				__lowerCAmelCase	:  Optional[int]   ,				__lowerCAmelCase	:  Tuple   ,				__lowerCAmelCase	:  str   ,				__lowerCAmelCase	:  Tuple   ,				__lowerCAmelCase	:  str   ,				__lowerCAmelCase	:  Optional[int]   ,				__lowerCAmelCase	:  Dict					):
      _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	:  Any   ,				__lowerCAmelCase	:  Union[str, Any]   ,				__lowerCAmelCase	:  List[Any]   ,				__lowerCAmelCase	:  Dict   ,				__lowerCAmelCase	:  Tuple   ,				__lowerCAmelCase	:  Dict   ,				__lowerCAmelCase	:  int   ,				__lowerCAmelCase	:  Union[str, 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	:  List[str]					):
      _UpperCAmelCase						 =      self.prepare_config_and_inputs()
      (
          _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				(		lowercase__	,							lowercase__	,							unittest.TestCase   ):
    _snake_case     :  str     							=							(
        (
            TFConvBertModel,
            TFConvBertForMaskedLM,
            TFConvBertForQuestionAnswering,
            TFConvBertForSequenceClassification,
            TFConvBertForTokenClassification,
            TFConvBertForMultipleChoice,
        )
        if is_tf_available()
        else ()
    )
    _snake_case     :  Optional[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     :  List[str]     							=							False
    _snake_case     :  Tuple     							=							False
    _snake_case     :  str     							=							False


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


    def      lowerCAmelCase_  (					self	:  Optional[int]					):
      self.config_tester.run_common_tests()


    def      lowerCAmelCase_  (					self	:  Dict					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__lowerCamelCase					)


    def      lowerCAmelCase_  (					self	:  Dict					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*__lowerCamelCase					)


    def      lowerCAmelCase_  (					self	:  str					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*__lowerCamelCase					)


    def      lowerCAmelCase_  (					self	:  List[str]					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase					)


    def      lowerCAmelCase_  (					self	:  Optional[int]					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase					)


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


    @slow
    def      lowerCAmelCase_  (					self	:  str					):
      _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	:  Union[str, Any]					):
      _UpperCAmelCase						 =      TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""					)
      self.assertIsNotNone(__lowerCamelCase					)


    def      lowerCAmelCase_  (					self	:  Union[str, Any]					):
      _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	:  Optional[int]					):
        _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	:  int					):
        _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	:  Optional[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 __future__ import annotations






def     __UpperCAmelCase					(			lowercase		):







  """simple docstring"""
  return [ord(lowercase		) - 96 for elem in plain]






def     __UpperCAmelCase					(			lowercase		):







  """simple docstring"""
  return "".join(chr(elem + 96		) for elem in encoded		)






def     __UpperCAmelCase					(			):







  """simple docstring"""
  _UpperCAmelCase						 =      encode(input("""-> """		).strip().lower()		)
  print("""Encoded: """   ,lowercase		)
  print("""Decoded:"""   ,decode(lowercase		)		)


if __name__ == "__main__":
    main()
