


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:
       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=5_12,   lowerCamelCase=16,   lowerCamelCase=2,   lowerCamelCase=0.0_2,   lowerCamelCase=3,   lowerCamelCase=4,   lowerCamelCase=None,   ) ->     Optional[int]:




           """simple docstring"""

           _lowercase :							Tuple   	=	parent
           _lowercase :							List[Any]   	=	13
           _lowercase :							Union[str, Any]   	=	7
           _lowercase :							List[Any]   	=	True
           _lowercase :							List[Any]   	=	True
           _lowercase :							List[str]   	=	True
           _lowercase :							Optional[int]   	=	True
           _lowercase :							Optional[Any]   	=	99
           _lowercase :							List[str]   	=	3_84
           _lowercase :							Tuple   	=	2
           _lowercase :							int   	=	4
           _lowercase :							Optional[int]   	=	37
           _lowercase :							Any   	=	'gelu'
           _lowercase :							Tuple   	=	0.1
           _lowercase :							Optional[int]   	=	0.1
           _lowercase :							List[Any]   	=	5_12
           _lowercase :							Any   	=	16
           _lowercase :							int   	=	2
           _lowercase :							Optional[int]   	=	0.0_2
           _lowercase :							List[Any]   	=	3
           _lowercase :							int   	=	4
           _lowercase :							Dict   	=	1_28
           _lowercase :							str   	=	2
           _lowercase :							List[Any]   	=	9
           _lowercase :							int   	=	1
           _lowercase :							Optional[int]   	=	None
       def     UpperCamelCase  (      self) ->     int:




           """simple docstring"""

           _lowercase :							List[Any]   	=	ids_tensor([self.batch_size, self.seq_length],   self.vocab_size)

           _lowercase :							Optional[Any]   	=	None
           if self.use_input_mask:
               _lowercase :							Optional[int]   	=	random_attention_mask([self.batch_size, self.seq_length])

           _lowercase :							List[Any]   	=	None
           if self.use_token_type_ids:
               _lowercase :							Tuple   	=	ids_tensor([self.batch_size, self.seq_length],   self.type_vocab_size)

           _lowercase :							Any   	=	None
           _lowercase :							str   	=	None
           _lowercase :							str   	=	None
           if self.use_labels:
               _lowercase :							Tuple   	=	ids_tensor([self.batch_size],   self.type_sequence_label_size)
               _lowercase :							Tuple   	=	ids_tensor([self.batch_size, self.seq_length],   self.num_labels)
               _lowercase :							Union[str, Any]   	=	ids_tensor([self.batch_size],   self.num_choices)

           _lowercase :							Tuple   	=	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     UpperCamelCase  (      self,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase) ->     Tuple:




           """simple docstring"""

           _lowercase :							Any   	=	TFConvBertModel(config=lowerCamelCase)
           _lowercase :							str   	=	{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

           _lowercase :							int   	=	[input_ids, input_mask]
           _lowercase :							List[Any]   	=	model(lowerCamelCase)

           _lowercase :							Optional[int]   	=	model(lowerCamelCase)

           self.parent.assertEqual(result.last_hidden_state.shape,   (self.batch_size, self.seq_length, self.hidden_size))
       def     UpperCamelCase  (      self,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase) ->     Optional[Any]:




           """simple docstring"""

           _lowercase :							Union[str, Any]   	=	TFConvBertForMaskedLM(config=lowerCamelCase)
           _lowercase :							int   	=	{
               'input_ids': input_ids,
               'attention_mask': input_mask,
               'token_type_ids': token_type_ids,
           }
           _lowercase :							Dict   	=	model(lowerCamelCase)
           self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.seq_length, self.vocab_size))
       def     UpperCamelCase  (      self,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase) ->     Optional[Any]:




           """simple docstring"""

           _lowercase :							str   	=	self.num_labels
           _lowercase :							List[Any]   	=	TFConvBertForSequenceClassification(config=lowerCamelCase)
           _lowercase :							str   	=	{
               'input_ids': input_ids,
               'attention_mask': input_mask,
               'token_type_ids': token_type_ids,
           }

           _lowercase :							List[str]   	=	model(lowerCamelCase)
           self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.num_labels))
       def     UpperCamelCase  (      self,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase) ->     Tuple:




           """simple docstring"""

           _lowercase :							int   	=	self.num_choices
           _lowercase :							Any   	=	TFConvBertForMultipleChoice(config=lowerCamelCase)
           _lowercase :							Optional[Any]   	=	tf.tile(tf.expand_dims(lowerCamelCase,   1),   (1, self.num_choices, 1))
           _lowercase :							int   	=	tf.tile(tf.expand_dims(lowerCamelCase,   1),   (1, self.num_choices, 1))
           _lowercase :							int   	=	tf.tile(tf.expand_dims(lowerCamelCase,   1),   (1, self.num_choices, 1))
           _lowercase :							int   	=	{
               'input_ids': multiple_choice_inputs_ids,
               'attention_mask': multiple_choice_input_mask,
               'token_type_ids': multiple_choice_token_type_ids,
           }
           _lowercase :							Optional[Any]   	=	model(lowerCamelCase)
           self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.num_choices))
       def     UpperCamelCase  (      self,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase) ->     Tuple:




           """simple docstring"""

           _lowercase :							Any   	=	self.num_labels
           _lowercase :							List[str]   	=	TFConvBertForTokenClassification(config=lowerCamelCase)
           _lowercase :							Dict   	=	{
               'input_ids': input_ids,
               'attention_mask': input_mask,
               'token_type_ids': token_type_ids,
           }
           _lowercase :							List[str]   	=	model(lowerCamelCase)
           self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.seq_length, self.num_labels))
       def     UpperCamelCase  (      self,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase) ->     List[str]:




           """simple docstring"""

           _lowercase :							Optional[Any]   	=	TFConvBertForQuestionAnswering(config=lowerCamelCase)
           _lowercase :							str   	=	{
               'input_ids': input_ids,
               'attention_mask': input_mask,
               'token_type_ids': token_type_ids,
           }

           _lowercase :							List[str]   	=	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     UpperCamelCase  (      self) ->     str:




           """simple docstring"""

           _lowercase :							Optional[int]   	=	self.prepare_config_and_inputs()
           (
               (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       
           ) :							str   	=	config_and_inputs
           _lowercase :							Optional[Any]   	=	{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
           return config, inputs_dict






@require_tf
class _lowerCamelCase(		_a,				_a,				unittest.TestCase     ):
       lowercase_				:			Union[str, Any]									=    (
           (
               TFConvBertModel,
               TFConvBertForMaskedLM,
               TFConvBertForQuestionAnswering,
               TFConvBertForSequenceClassification,
               TFConvBertForTokenClassification,
               TFConvBertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       lowercase_				:			str									=    (
           {
               """feature-extraction""": TFConvBertModel,
               """fill-mask""": TFConvBertForMaskedLM,
               """question-answering""": TFConvBertForQuestionAnswering,
               """text-classification""": TFConvBertForSequenceClassification,
               """token-classification""": TFConvBertForTokenClassification,
               """zero-shot""": TFConvBertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       lowercase_				:			Tuple									=    False
       lowercase_				:			Any									=    False
       lowercase_				:			List[str]									=    False
       def     UpperCamelCase  (      self) ->     List[Any]:




           """simple docstring"""

           _lowercase :							List[str]   	=	TFConvBertModelTester(self)
           _lowercase :							Union[str, Any]   	=	ConfigTester(self,   config_class=lowerCamelCase,   hidden_size=37)
       def     UpperCamelCase  (      self) ->     List[str]:




           """simple docstring"""

           self.config_tester.run_common_tests()
       def     UpperCamelCase  (      self) ->     Tuple:




           """simple docstring"""

           _lowercase :							Optional[Any]   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Union[str, Any]:




           """simple docstring"""

           _lowercase :							Optional[Any]   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Dict:




           """simple docstring"""

           _lowercase :							Any   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Optional[int]:




           """simple docstring"""

           _lowercase :							Optional[Any]   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     int:




           """simple docstring"""

           _lowercase :							List[Any]   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Any:




           """simple docstring"""

           _lowercase :							Optional[int]   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*lowerCamelCase)
       @slow
       def     UpperCamelCase  (      self) ->     List[Any]:




           """simple docstring"""

           _lowercase	,       _lowercase :							Optional[int]   	=	self.model_tester.prepare_config_and_inputs_for_common()
           _lowercase :							List[str]   	=	True
           _lowercase :							Tuple   	=	True

           if hasattr(lowerCamelCase,   'use_cache'):
               _lowercase :							Optional[Any]   	=	True

           _lowercase :							List[Any]   	=	getattr(self.model_tester,   'encoder_seq_length',   self.model_tester.seq_length)
           _lowercase :							Optional[int]   	=	getattr(self.model_tester,   'key_length',   lowerCamelCase)

           for model_class in self.all_model_classes:
               _lowercase :							Any   	=	self._prepare_for_class(lowerCamelCase,   lowerCamelCase)
               _lowercase :							Tuple   	=	model_class(lowerCamelCase)
               _lowercase :							Tuple   	=	len(model(lowerCamelCase))

               with tempfile.TemporaryDirectory() as tmpdirname:
                   model.save_pretrained(lowerCamelCase,   saved_model=lowerCamelCase)
                   _lowercase :							Tuple   	=	os.path.join(lowerCamelCase,   'saved_model',   '1')
                   _lowercase :							str   	=	tf.keras.models.load_model(lowerCamelCase)
                   _lowercase :							Dict   	=	model(lowerCamelCase)

                   if self.is_encoder_decoder:
                       _lowercase :							List[str]   	=	outputs['encoder_hidden_states']
                       _lowercase :							Optional[int]   	=	outputs['encoder_attentions']
                   else:
                       _lowercase :							Any   	=	outputs['hidden_states']
                       _lowercase :							Dict   	=	outputs['attentions']

                   self.assertEqual(len(lowerCamelCase),   lowerCamelCase)

                   _lowercase :							Tuple   	=	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     UpperCamelCase  (      self) ->     int:




           """simple docstring"""

           _lowercase :							str   	=	TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
           self.assertIsNotNone(lowerCamelCase)




       def     UpperCamelCase  (      self) ->     List[Any]:




           """simple docstring"""

           _lowercase	,       _lowercase :							Any   	=	self.model_tester.prepare_config_and_inputs_for_common()
           _lowercase :							int   	=	True
           _lowercase :							Union[str, Any]   	=	getattr(self.model_tester,   'decoder_seq_length',   self.model_tester.seq_length)
           _lowercase :							Union[str, Any]   	=	getattr(self.model_tester,   'encoder_seq_length',   self.model_tester.seq_length)
           _lowercase :							Tuple   	=	getattr(self.model_tester,   'key_length',   lowerCamelCase)
           _lowercase :							int   	=	getattr(self.model_tester,   'key_length',   lowerCamelCase)

           def check_decoder_attentions_output(lowerCamelCase):
               _lowercase :							Union[str, Any]   	=	len(lowerCamelCase)
               self.assertEqual(out_len % 2,   0)
               _lowercase :							Optional[Any]   	=	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 :							List[str]   	=	[
                   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 :							Dict   	=	True
               _lowercase :							Any   	=	False
               _lowercase :							Optional[int]   	=	model_class(lowerCamelCase)
               _lowercase :							Tuple   	=	model(self._prepare_for_class(lowerCamelCase,   lowerCamelCase))
               _lowercase :							List[str]   	=	len(lowerCamelCase)
               self.assertEqual(config.output_hidden_states,   lowerCamelCase)
               check_encoder_attentions_output(lowerCamelCase)

               if self.is_encoder_decoder:
                   _lowercase :							str   	=	model_class(lowerCamelCase)
                   _lowercase :							List[Any]   	=	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 :							int   	=	True
               _lowercase :							Union[str, Any]   	=	model_class(lowerCamelCase)
               _lowercase :							Union[str, Any]   	=	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 :							Dict   	=	True
               _lowercase :							int   	=	True
               _lowercase :							List[str]   	=	model_class(lowerCamelCase)
               _lowercase :							Union[str, Any]   	=	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 _lowerCamelCase(		unittest.TestCase     ):
       @slow
       def     UpperCamelCase  (      self) ->     List[Any]:




           """simple docstring"""

           _lowercase :							Dict   	=	TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
           _lowercase :							Any   	=	tf.constant([[0, 1, 2, 3, 4, 5]])
           _lowercase :							Any   	=	model(lowerCamelCase)[0]

           _lowercase :							List[Any]   	=	[1, 6, 7_68]
           self.assertEqual(output.shape,   lowerCamelCase)

           _lowercase :							Tuple   	=	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)






import pytest


SCREAMING_SNAKE_CASE :   Optional[Any]							      =				"__dummy_dataset1__"

SCREAMING_SNAKE_CASE :   int							      =				"\nimport json\nimport os\n\nimport datasets\n\n\nREPO_URL = \"https://huggingface.co/datasets/albertvillanova/tests-raw-jsonl/resolve/main/\"\nURLS = {\"train\": REPO_URL + \"wikiann-bn-train.jsonl\", \"validation\": REPO_URL + \"wikiann-bn-validation.jsonl\"}\n\n\nclass __DummyDataset1__(datasets.GeneratorBasedBuilder):\n\n    def _info(self):\n        features = datasets.Features(\n            {\n                \"tokens\": datasets.Sequence(datasets.Value(\"string\")),\n                \"ner_tags\": datasets.Sequence(\n                    datasets.features.ClassLabel(\n                        names=[\n                            \"O\",\n                            \"B-PER\",\n                            \"I-PER\",\n                            \"B-ORG\",\n                            \"I-ORG\",\n                            \"B-LOC\",\n                            \"I-LOC\",\n                        ]\n                    )\n                ),\n                \"langs\": datasets.Sequence(datasets.Value(\"string\")),\n                \"spans\": datasets.Sequence(datasets.Value(\"string\")),\n            }\n        )\n        return datasets.DatasetInfo(features=features)\n\n    def _split_generators(self, dl_manager):\n        dl_path = dl_manager.download(URLS)\n        return [\n            datasets.SplitGenerator(datasets.Split.TRAIN, gen_kwargs={\"filepath\": dl_path[\"train\"]}),\n            datasets.SplitGenerator(datasets.Split.VALIDATION, gen_kwargs={\"filepath\": dl_path[\"validation\"]}),\n        ]\n\n    def _generate_examples(self, filepath):\n        with open(filepath, \"r\", encoding=\"utf-8\") as f:\n            for i, line in enumerate(f):\n                yield i, json.loads(line)\n"



@pytest.fixture
def        UpperCamelCase_(   )					->		Dict:
    return DATASET_LOADING_SCRIPT_NAME



@pytest.fixture
def        UpperCamelCase_(   )					->		List[Any]:
    return DATASET_LOADING_SCRIPT_CODE



@pytest.fixture
def        UpperCamelCase_(   lowerCamelCase_							,				lowerCamelCase_							,				lowerCamelCase_				)					->		int:
    _lowercase :							List[str]   	=	dataset_loading_script_name
    _lowercase :							Union[str, Any]   	=	tmp_path / 'datasets' / script_name
    script_dir.mkdir(parents=lowerCamelCase_				)
    _lowercase :							Optional[int]   	=	script_dir / F'''{script_name}.py'''
    with open(lowerCamelCase_							,				'w'				) as f:
        f.write(lowerCamelCase_				)
    return str(lowerCamelCase_				)


