"""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		,       __lowerCAmelCase		,       __lowerCAmelCase=13		,       __lowerCAmelCase=7		,       __lowerCAmelCase=True		,       __lowerCAmelCase=True		,       __lowerCAmelCase=True		,       __lowerCAmelCase=True		,       __lowerCAmelCase=99		,       __lowerCAmelCase=32		,       __lowerCAmelCase=2		,       __lowerCAmelCase=4		,       __lowerCAmelCase=37		,       __lowerCAmelCase="gelu"		,       __lowerCAmelCase=0.1		,       __lowerCAmelCase=0.1		,       __lowerCAmelCase=512		,       __lowerCAmelCase=16		,       __lowerCAmelCase=2		,       __lowerCAmelCase=0.0_2		,       __lowerCAmelCase=3		,       __lowerCAmelCase=4		,       __lowerCAmelCase=None		,       ):







            """simple docstring"""



            lowercase														=	parent
            lowercase														=	13
            lowercase														=	7
            lowercase														=	True
            lowercase														=	True
            lowercase														=	True
            lowercase														=	True
            lowercase														=	99
            lowercase														=	384
            lowercase														=	2
            lowercase														=	4
            lowercase														=	37
            lowercase														=	"""gelu"""
            lowercase														=	0.1
            lowercase														=	0.1
            lowercase														=	512
            lowercase														=	16
            lowercase														=	2
            lowercase														=	0.0_2
            lowercase														=	3
            lowercase														=	4
            lowercase														=	128
            lowercase														=	2
            lowercase														=	9
            lowercase														=	1
            lowercase														=	None

      def 				A__       (						self						):







            """simple docstring"""



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

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

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

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

            lowercase														=	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 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



            lowercase														=	TFConvBertModel(config=__lowerCAmelCase						)
            lowercase														=	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

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

            lowercase														=	model(__lowerCAmelCase						)

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

      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



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

      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



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

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

      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



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

      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



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

      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



            lowercase														=	TFConvBertForQuestionAnswering(config=__lowerCAmelCase						)
            lowercase														=	{
                """input_ids""": input_ids,
                """attention_mask""": input_mask,
                """token_type_ids""": token_type_ids,
            }

            lowercase														=	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 				A__       (						self						):







            """simple docstring"""



            lowercase														=	self.prepare_config_and_inputs()
            (
                (
                lowercase
            )							,							(
                lowercase
            )							,							(
                lowercase
            )							,							(
                lowercase
            )							,							(
                lowercase
            )							,							(
                lowercase
            )							,							(
                lowercase
            )							,							
            )														=	config_and_inputs
            lowercase														=	{"""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__			:   Union[str, Any]							  =	(
          (
              TFConvBertModel,
              TFConvBertForMaskedLM,
              TFConvBertForQuestionAnswering,
              TFConvBertForSequenceClassification,
              TFConvBertForTokenClassification,
              TFConvBertForMultipleChoice,
          )
          if is_tf_available()
          else ()
      )
      snake_case__			:   Union[str, 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__			:   str							  =	False
      snake_case__			:   Optional[Any]							  =	False
      snake_case__			:   Optional[int]							  =	False

      def 				A__       (						self						):







            """simple docstring"""



            lowercase														=	TFConvBertModelTester(self						)
            lowercase														=	ConfigTester(self		,       config_class=__lowerCAmelCase		,       hidden_size=37						)

      def 				A__       (						self						):







            """simple docstring"""



            self.config_tester.run_common_tests()

      def 				A__       (						self						):







            """simple docstring"""



            lowercase														=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__lowerCAmelCase						)

      def 				A__       (						self						):







            """simple docstring"""



            lowercase														=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase						)

      def 				A__       (						self						):







            """simple docstring"""



            lowercase														=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_multiple_choice(*__lowerCAmelCase						)

      def 				A__       (						self						):







            """simple docstring"""



            lowercase														=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase						)

      def 				A__       (						self						):







            """simple docstring"""



            lowercase														=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*__lowerCAmelCase						)

      def 				A__       (						self						):







            """simple docstring"""



            lowercase														=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase						)

      @slow
      def 				A__       (						self						):







            """simple docstring"""



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

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

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

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

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

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

                        self.assertEqual(len(__lowerCAmelCase						)		,       __lowerCAmelCase						)

                        lowercase														=	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 				A__       (						self						):







            """simple docstring"""



            lowercase														=	TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""						)
            self.assertIsNotNone(__lowerCAmelCase						)







      def 				A__       (						self						):







            """simple docstring"""



            lowercase							,							lowercase														=	self.model_tester.prepare_config_and_inputs_for_common()
            lowercase														=	True
            lowercase														=	getattr(self.model_tester		,       """decoder_seq_length"""		,       self.model_tester.seq_length						)
            lowercase														=	getattr(self.model_tester		,       """encoder_seq_length"""		,       self.model_tester.seq_length						)
            lowercase														=	getattr(self.model_tester		,       """key_length"""		,       __lowerCAmelCase						)
            lowercase														=	getattr(self.model_tester		,       """key_length"""		,       __lowerCAmelCase						)

            def check_decoder_attentions_output(__lowerCAmelCase						):
                  lowercase														=	len(__lowerCAmelCase						)
                  self.assertEqual(out_len % 2		,       0						)
                  lowercase														=	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						):
                  lowercase														=	[
                      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:
                  lowercase														=	True
                  lowercase														=	False
                  lowercase														=	model_class(__lowerCAmelCase						)
                  lowercase														=	model(self._prepare_for_class(__lowerCAmelCase		,       __lowerCAmelCase						)						)
                  lowercase														=	len(__lowerCAmelCase						)
                  self.assertEqual(config.output_hidden_states		,       __lowerCAmelCase						)
                  check_encoder_attentions_output(__lowerCAmelCase						)

                  if self.is_encoder_decoder:
                        lowercase														=	model_class(__lowerCAmelCase						)
                        lowercase														=	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"]
                  lowercase														=	True
                  lowercase														=	model_class(__lowerCAmelCase						)
                  lowercase														=	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
                  lowercase														=	True
                  lowercase														=	True
                  lowercase														=	model_class(__lowerCAmelCase						)
                  lowercase														=	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 				A__       (						self						):







            """simple docstring"""



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

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

            lowercase														=	tf.constant(
                [
                    [
                        [-0.0_3_4_7_5_4_9_3, -0.4_6_8_6_0_3_4, -0.3_0_6_3_8_8_3_2],
                        [0.2_2_6_3_7_2_4_8, -0.2_6_9_8_8_6_4_6, -0.7_4_2_3_4_2_4],
                        [0.1_0_3_2_4_8_6_8, -0.4_5_0_1_3_5_0_8, -0.5_8_2_8_0_7_8_4],
                    ]
                ]						)
            tf.debugging.assert_near(output[:, :3, :3]		,       __lowerCAmelCase		,       atol=1E-4						)




"""simple docstring"""






from queue import Queue
from typing import TYPE_CHECKING, Optional


if TYPE_CHECKING:
       from ..models.auto import AutoTokenizer





class  _A						:

      def 				A__       (						self		,       __lowerCAmelCase						):







            """simple docstring"""



            raise NotImplementedError()







      def 				A__       (						self						):







            """simple docstring"""



            raise NotImplementedError()






class  _A						(       lowerCAmelCase	):

      def __init__(						self		,       __lowerCAmelCase		,       __lowerCAmelCase = False		,       **__lowerCAmelCase						):







            """simple docstring"""



            lowercase														=	tokenizer
            lowercase														=	skip_prompt
            lowercase														=	decode_kwargs

            # variables used in the streaming process
            lowercase														=	[]
            lowercase														=	0
            lowercase														=	True

      def 				A__       (						self		,       __lowerCAmelCase						):







            """simple docstring"""



            if len(value.shape						) > 1 and value.shape[0] > 1:
                  raise ValueError("""TextStreamer only supports batch size 1"""						)
            elif len(value.shape						) > 1:
                  lowercase														=	value[0]

            if self.skip_prompt and self.next_tokens_are_prompt:
                  lowercase														=	False
                  return

            # Add the new token to the cache and decodes the entire thing.
            self.token_cache.extend(value.tolist()						)
            lowercase														=	self.tokenizer.decode(self.token_cache		,       **self.decode_kwargs						)

            # After the symbol for a new line, we flush the cache.
            if text.endswith("""\n"""						):
                  lowercase														=	text[self.print_len :]
                  lowercase														=	[]
                  lowercase														=	0
            # If the last token is a CJK character, we print the characters.
            elif len(__lowerCAmelCase						) > 0 and self._is_chinese_char(ord(text[-1]						)						):
                  lowercase														=	text[self.print_len :]
                  self.print_len += len(__lowerCAmelCase						)
            # Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words,
            # which may change with the subsequent token -- there are probably smarter ways to do this!)
            else:
                  lowercase														=	text[self.print_len : text.rfind(""" """						) + 1]
                  self.print_len += len(__lowerCAmelCase						)

            self.on_finalized_text(__lowerCAmelCase						)

      def 				A__       (						self						):







            """simple docstring"""



            if len(self.token_cache						) > 0:
                  lowercase														=	self.tokenizer.decode(self.token_cache		,       **self.decode_kwargs						)
                  lowercase														=	text[self.print_len :]
                  lowercase														=	[]
                  lowercase														=	0
            else:
                  lowercase														=	""""""

            lowercase														=	True
            self.on_finalized_text(__lowerCAmelCase		,       stream_end=__lowerCAmelCase						)

      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase = False						):







            """simple docstring"""



            print(__lowerCAmelCase		,       flush=__lowerCAmelCase		,       end="""""" if not stream_end else None						)







      def 				A__       (						self		,       __lowerCAmelCase						):







            """simple docstring"""



            if (
                (cp >= 0X4_e00 and cp <= 0X9_fff)
                or (cp >= 0X3_400 and cp <= 0X4_dbf)  #
                or (cp >= 0X20_000 and cp <= 0X2a_6df)  #
                or (cp >= 0X2a_700 and cp <= 0X2b_73f)  #
                or (cp >= 0X2b_740 and cp <= 0X2b_81f)  #
                or (cp >= 0X2b_820 and cp <= 0X2c_eaf)  #
                or (cp >= 0Xf_900 and cp <= 0Xf_aff)
                or (cp >= 0X2f_800 and cp <= 0X2f_a1f)  #
            ):  #
                  return True

            return False






class  _A						(       lowerCAmelCase	):

      def __init__(						self		,       __lowerCAmelCase		,       __lowerCAmelCase = False		,       __lowerCAmelCase = None		,       **__lowerCAmelCase						):







            """simple docstring"""



            super().__init__(__lowerCAmelCase		,       __lowerCAmelCase		,       **__lowerCAmelCase						)
            lowercase														=	Queue()
            lowercase														=	None
            lowercase														=	timeout

      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase = False						):







            """simple docstring"""



            self.text_queue.put(__lowerCAmelCase		,       timeout=self.timeout						)
            if stream_end:
                  self.text_queue.put(self.stop_signal		,       timeout=self.timeout						)

      def __iter__(						self						):







            """simple docstring"""



            return self







      def 				A__       (						self						):







            """simple docstring"""



            lowercase														=	self.text_queue.get(timeout=self.timeout						)
            if value == self.stop_signal:
                  raise StopIteration()
            else:
                  return value



