



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 :       Optional[int]  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Dict=13  ,     __magic_name__ :       Tuple=7  ,     __magic_name__ :       int=True  ,     __magic_name__ :       Optional[Any]=True  ,     __magic_name__ :       Optional[int]=True  ,     __magic_name__ :       Optional[Any]=True  ,     __magic_name__ :       Any=99  ,     __magic_name__ :       str=32  ,     __magic_name__ :       List[Any]=2  ,     __magic_name__ :       Optional[Any]=4  ,     __magic_name__ :       Optional[Any]=37  ,     __magic_name__ :       Tuple="gelu"  ,     __magic_name__ :       Any=0.1  ,     __magic_name__ :       Union[str, Any]=0.1  ,     __magic_name__ :       Any=512  ,     __magic_name__ :       str=16  ,     __magic_name__ :       Optional[int]=2  ,     __magic_name__ :       List[str]=0.02  ,     __magic_name__ :       Union[str, Any]=3  ,     __magic_name__ :       Tuple=4  ,     __magic_name__ :       Union[str, Any]=None  ,     )  ->	List[Any]:
        SCREAMING_SNAKE_CASE_          =  parent
        SCREAMING_SNAKE_CASE_          =  13
        SCREAMING_SNAKE_CASE_          =  7
        SCREAMING_SNAKE_CASE_          =  True
        SCREAMING_SNAKE_CASE_          =  True
        SCREAMING_SNAKE_CASE_          =  True
        SCREAMING_SNAKE_CASE_          =  True
        SCREAMING_SNAKE_CASE_          =  99
        SCREAMING_SNAKE_CASE_          =  384
        SCREAMING_SNAKE_CASE_          =  2
        SCREAMING_SNAKE_CASE_          =  4
        SCREAMING_SNAKE_CASE_          =  37
        SCREAMING_SNAKE_CASE_          =  "gelu"
        SCREAMING_SNAKE_CASE_          =  0.1
        SCREAMING_SNAKE_CASE_          =  0.1
        SCREAMING_SNAKE_CASE_          =  512
        SCREAMING_SNAKE_CASE_          =  16
        SCREAMING_SNAKE_CASE_          =  2
        SCREAMING_SNAKE_CASE_          =  0.02
        SCREAMING_SNAKE_CASE_          =  3
        SCREAMING_SNAKE_CASE_          =  4
        SCREAMING_SNAKE_CASE_          =  128
        SCREAMING_SNAKE_CASE_          =  2
        SCREAMING_SNAKE_CASE_          =  9
        SCREAMING_SNAKE_CASE_          =  1
        SCREAMING_SNAKE_CASE_          =  None







  def    __A     (							self :       Union[str, Any]   )  ->	Dict:
        SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size   )

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

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

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

        SCREAMING_SNAKE_CASE_          =  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    __A     (							self :       str  ,     __magic_name__ :       int  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       str  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       int  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Any   )  ->	str:
        SCREAMING_SNAKE_CASE_          =  TFConvBertModel(config=__magic_name__   )
        SCREAMING_SNAKE_CASE_          =  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

        SCREAMING_SNAKE_CASE_          =  [input_ids, input_mask]
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__   )

        SCREAMING_SNAKE_CASE_          =  model(__magic_name__   )

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







  def    __A     (							self :       Union[str, Any]  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       Tuple  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Dict   )  ->	Tuple:
        SCREAMING_SNAKE_CASE_          =  TFConvBertForMaskedLM(config=__magic_name__   )
        SCREAMING_SNAKE_CASE_          =  {
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__   )
        self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)   )







  def    __A     (							self :       Optional[Any]  ,     __magic_name__ :       Tuple  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       Any  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Optional[Any]   )  ->	Optional[int]:
        SCREAMING_SNAKE_CASE_          =  self.num_labels
        SCREAMING_SNAKE_CASE_          =  TFConvBertForSequenceClassification(config=__magic_name__   )
        SCREAMING_SNAKE_CASE_          =  {
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }

        SCREAMING_SNAKE_CASE_          =  model(__magic_name__   )
        self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.num_labels)   )







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







  def    __A     (							self :       List[str]  ,     __magic_name__ :       str  ,     __magic_name__ :       Dict  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       str  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       int  ,     __magic_name__ :       Union[str, Any]   )  ->	Union[str, Any]:
        SCREAMING_SNAKE_CASE_          =  self.num_labels
        SCREAMING_SNAKE_CASE_          =  TFConvBertForTokenClassification(config=__magic_name__   )
        SCREAMING_SNAKE_CASE_          =  {
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__   )
        self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.num_labels)   )







  def    __A     (							self :       List[str]  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       str  ,     __magic_name__ :       str  ,     __magic_name__ :       str  ,     __magic_name__ :       int  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       List[Any]   )  ->	List[Any]:
        SCREAMING_SNAKE_CASE_          =  TFConvBertForQuestionAnswering(config=__magic_name__   )
        SCREAMING_SNAKE_CASE_          =  {
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }

        SCREAMING_SNAKE_CASE_          =  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    __A     (							self :       Tuple   )  ->	int:
        SCREAMING_SNAKE_CASE_          =  self.prepare_config_and_inputs()
        (
            (
            SCREAMING_SNAKE_CASE_ 
        )     , (
            SCREAMING_SNAKE_CASE_ 
        )     , (
            SCREAMING_SNAKE_CASE_ 
        )     , (
            SCREAMING_SNAKE_CASE_ 
        )     , (
            SCREAMING_SNAKE_CASE_ 
        )     , (
            SCREAMING_SNAKE_CASE_ 
        )     , (
            SCREAMING_SNAKE_CASE_ 
        )     ,  
        )          =  config_and_inputs
        SCREAMING_SNAKE_CASE_          =  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
        return config, inputs_dict



@require_tf
class   lowerCamelCase					(SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      unittest.TestCase					):
  """simple docstring"""







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







  def    __A     (							self :       int   )  ->	Tuple:
        SCREAMING_SNAKE_CASE_          =  TFConvBertModelTester(self   )
        SCREAMING_SNAKE_CASE_          =  ConfigTester(self  ,     config_class=__magic_name__  ,     hidden_size=37   )







  def    __A     (							self :       List[str]   )  ->	Optional[int]:
        self.config_tester.run_common_tests()







  def    __A     (							self :       Union[str, Any]   )  ->	str:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__magic_name__   )







  def    __A     (							self :       Any   )  ->	Tuple:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*__magic_name__   )







  def    __A     (							self :       Optional[Any]   )  ->	Union[str, Any]:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*__magic_name__   )







  def    __A     (							self :       str   )  ->	List[Any]:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*__magic_name__   )







  def    __A     (							self :       Union[str, Any]   )  ->	Union[str, Any]:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*__magic_name__   )







  def    __A     (							self :       List[Any]   )  ->	Dict:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*__magic_name__   )







  @slow
  def    __A     (							self :       Tuple   )  ->	List[Any]:
        SCREAMING_SNAKE_CASE_     , SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs_for_common()
        SCREAMING_SNAKE_CASE_          =  True
        SCREAMING_SNAKE_CASE_          =  True

        if hasattr(__magic_name__  ,     "use_cache"   ):
              SCREAMING_SNAKE_CASE_          =  True

        SCREAMING_SNAKE_CASE_          =  getattr(self.model_tester  ,     "encoder_seq_length"  ,     self.model_tester.seq_length   )
        SCREAMING_SNAKE_CASE_          =  getattr(self.model_tester  ,     "key_length"  ,     __magic_name__   )

        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE_          =  self._prepare_for_class(__magic_name__  ,     __magic_name__   )
              SCREAMING_SNAKE_CASE_          =  model_class(__magic_name__   )
              SCREAMING_SNAKE_CASE_          =  len(model(__magic_name__   )   )

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

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

                    self.assertEqual(len(__magic_name__   )  ,     __magic_name__   )

                    SCREAMING_SNAKE_CASE_          =  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    __A     (							self :       List[str]   )  ->	str:
        SCREAMING_SNAKE_CASE_          =  TFConvBertModel.from_pretrained("YituTech/conv-bert-base"   )
        self.assertIsNotNone(__magic_name__   )







  def    __A     (							self :       Tuple   )  ->	Any:
        SCREAMING_SNAKE_CASE_     , SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs_for_common()
        SCREAMING_SNAKE_CASE_          =  True
        SCREAMING_SNAKE_CASE_          =  getattr(self.model_tester  ,     "decoder_seq_length"  ,     self.model_tester.seq_length   )
        SCREAMING_SNAKE_CASE_          =  getattr(self.model_tester  ,     "encoder_seq_length"  ,     self.model_tester.seq_length   )
        SCREAMING_SNAKE_CASE_          =  getattr(self.model_tester  ,     "key_length"  ,     __magic_name__   )
        SCREAMING_SNAKE_CASE_          =  getattr(self.model_tester  ,     "key_length"  ,     __magic_name__   )

        def check_decoder_attentions_output(__magic_name__ :       List[str]   ):
              SCREAMING_SNAKE_CASE_          =  len(__magic_name__   )
              self.assertEqual(out_len % 2  ,     0   )
              SCREAMING_SNAKE_CASE_          =  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__ :       List[Any]   ):
              SCREAMING_SNAKE_CASE_          =  [
                  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:
              SCREAMING_SNAKE_CASE_          =  True
              SCREAMING_SNAKE_CASE_          =  False
              SCREAMING_SNAKE_CASE_          =  model_class(__magic_name__   )
              SCREAMING_SNAKE_CASE_          =  model(self._prepare_for_class(__magic_name__  ,     __magic_name__   )   )
              SCREAMING_SNAKE_CASE_          =  len(__magic_name__   )
              self.assertEqual(config.output_hidden_states  ,     __magic_name__   )
              check_encoder_attentions_output(__magic_name__   )

              if self.is_encoder_decoder:
                    SCREAMING_SNAKE_CASE_          =  model_class(__magic_name__   )
                    SCREAMING_SNAKE_CASE_          =  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"]
              SCREAMING_SNAKE_CASE_          =  True
              SCREAMING_SNAKE_CASE_          =  model_class(__magic_name__   )
              SCREAMING_SNAKE_CASE_          =  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
              SCREAMING_SNAKE_CASE_          =  True
              SCREAMING_SNAKE_CASE_          =  True
              SCREAMING_SNAKE_CASE_          =  model_class(__magic_name__   )
              SCREAMING_SNAKE_CASE_          =  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   lowerCamelCase					(unittest.TestCase					):
  """simple docstring"""







  @slow
  def    __A     (							self :       Optional[Any]   )  ->	int:
        SCREAMING_SNAKE_CASE_          =  TFConvBertModel.from_pretrained("YituTech/conv-bert-base"   )
        SCREAMING_SNAKE_CASE_          =  tf.constant([[0, 1, 2, 3, 4, 5]]   )
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__   )[0]

        SCREAMING_SNAKE_CASE_          =  [1, 6, 768]
        self.assertEqual(output.shape  ,     __magic_name__   )

        SCREAMING_SNAKE_CASE_          =  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   )



from __future__ import annotations


def      a__ (		__UpperCamelCase				):

      # preprocessing the first row
      for i in range(1     ,      len(matrix[0]				)				):
            matrix[0][i] += matrix[0][i - 1]

      # preprocessing the first column
      for i in range(1     ,      len(__UpperCamelCase				)				):
            matrix[i][0] += matrix[i - 1][0]

      # updating the path cost for current position
      for i in range(1     ,      len(__UpperCamelCase				)				):
            for j in range(1     ,      len(matrix[0]				)				):
                  matrix[i][j] += min(matrix[i - 1][j]     ,      matrix[i][j - 1]				)

      return matrix[-1][-1]


if __name__ == "__main__":
      import doctest

      doctest.testmod()


