"""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    __snake_case       :



      def __init__(						self    ,			lowercase    ,			lowercase=13    ,			lowercase=7    ,			lowercase=True    ,			lowercase=True    ,			lowercase=True    ,			lowercase=True    ,			lowercase=99    ,			lowercase=32    ,			lowercase=2    ,			lowercase=4    ,			lowercase=37    ,			lowercase="gelu"    ,			lowercase=0.1    ,			lowercase=0.1    ,			lowercase=5_12    ,			lowercase=16    ,			lowercase=2    ,			lowercase=0.02    ,			lowercase=3    ,			lowercase=4    ,			lowercase=None    ,			)	->			List[Any]:





        '''simple docstring'''





        a__:     Any										=	parent
        a__:     Optional[int]										=	13
        a__:     List[str]										=	7
        a__:     Tuple										=	True
        a__:     int										=	True
        a__:     Any										=	True
        a__:     Dict										=	True
        a__:     Tuple										=	99
        a__:     Dict										=	3_84
        a__:     Optional[int]										=	2
        a__:     Dict										=	4
        a__:     str										=	37
        a__:     Union[str, Any]										=	'gelu'
        a__:     List[Any]										=	0.1
        a__:     Tuple										=	0.1
        a__:     Optional[int]										=	5_12
        a__:     Any										=	16
        a__:     Union[str, Any]										=	2
        a__:     List[Any]										=	0.02
        a__:     str										=	3
        a__:     int										=	4
        a__:     List[Any]										=	1_28
        a__:     Tuple										=	2
        a__:     Dict										=	9
        a__:     Any										=	1
        a__:     Dict										=	None



      def   lowerCamelCase_   (						self)	->			int:





        '''simple docstring'''





        a__:     Union[str, Any]										=	ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size)

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

        a__:     Optional[int]										=	None
        if self.use_token_type_ids:
          a__:     Union[str, Any]										=	ids_tensor([self.batch_size, self.seq_length]    ,			self.type_vocab_size)

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

        a__:     Union[str, Any]										=	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=lowercase    ,			)

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



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase)	->			List[Any]:





        '''simple docstring'''





        a__:     int										=	TFConvBertModel(config=lowercase)
        a__:     int										=	{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

        a__:     List[Any]										=	[input_ids, input_mask]
        a__:     str										=	model(lowercase)

        a__:     Tuple										=	model(lowercase)

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



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase)	->			Dict:





        '''simple docstring'''





        a__:     Union[str, Any]										=	TFConvBertForMaskedLM(config=lowercase)
        a__:     List[Any]										=	{
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }
        a__:     List[Any]										=	model(lowercase)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size))



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase)	->			List[Any]:





        '''simple docstring'''





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

        a__:     List[Any]										=	model(lowercase)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_labels))



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase)	->			Optional[int]:





        '''simple docstring'''





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



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase)	->			str:





        '''simple docstring'''





        a__:     Optional[Any]										=	self.num_labels
        a__:     Optional[int]										=	TFConvBertForTokenClassification(config=lowercase)
        a__:     Any										=	{
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }
        a__:     str										=	model(lowercase)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.num_labels))



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase    ,			lowercase)	->			str:





        '''simple docstring'''





        a__:     Tuple										=	TFConvBertForQuestionAnswering(config=lowercase)
        a__:     Dict										=	{
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }

        a__:     Tuple										=	model(lowercase)
        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)	->			Any:





        '''simple docstring'''





        a__:     Dict										=	self.prepare_config_and_inputs()
        (
            (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    (
            a__
        )							,    
        ):     List[str]										=	config_and_inputs
        a__:     int										=	{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
        return config, inputs_dict







@require_tf
class    __snake_case       (     __lowerCAmelCase       ,      __lowerCAmelCase       ,      unittest.TestCase				):
      a__        =		(
          (
              TFConvBertModel,
              TFConvBertForMaskedLM,
              TFConvBertForQuestionAnswering,
              TFConvBertForSequenceClassification,
              TFConvBertForTokenClassification,
              TFConvBertForMultipleChoice,
          )
          if is_tf_available()
          else ()
      )
      a__        =		(
          {
              """feature-extraction""": TFConvBertModel,
              """fill-mask""": TFConvBertForMaskedLM,
              """question-answering""": TFConvBertForQuestionAnswering,
              """text-classification""": TFConvBertForSequenceClassification,
              """token-classification""": TFConvBertForTokenClassification,
              """zero-shot""": TFConvBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      a__        =		False
      a__        =		False
      a__        =		False



      def   lowerCamelCase_   (						self)	->			Union[str, Any]:





        '''simple docstring'''





        a__:     List[str]										=	TFConvBertModelTester(self)
        a__:     Optional[Any]										=	ConfigTester(self    ,			config_class=lowercase    ,			hidden_size=37)



      def   lowerCamelCase_   (						self)	->			Union[str, Any]:





        '''simple docstring'''





        self.config_tester.run_common_tests()



      def   lowerCamelCase_   (						self)	->			Union[str, Any]:





        '''simple docstring'''





        a__:     Dict										=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase)



      def   lowerCamelCase_   (						self)	->			List[str]:





        '''simple docstring'''





        a__:     Optional[Any]										=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*lowercase)



      def   lowerCamelCase_   (						self)	->			Any:





        '''simple docstring'''





        a__:     int										=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*lowercase)



      def   lowerCamelCase_   (						self)	->			List[str]:





        '''simple docstring'''





        a__:     Any										=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*lowercase)



      def   lowerCamelCase_   (						self)	->			Union[str, Any]:





        '''simple docstring'''





        a__:     Any										=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*lowercase)



      def   lowerCamelCase_   (						self)	->			str:





        '''simple docstring'''





        a__:     int										=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*lowercase)



      @slow
      def   lowerCamelCase_   (						self)	->			List[str]:





        '''simple docstring'''





        a__							,    a__:     Any										=	self.model_tester.prepare_config_and_inputs_for_common()
        a__:     Tuple										=	True
        a__:     int										=	True

        if hasattr(lowercase    ,			'use_cache'):
          a__:     str										=	True

        a__:     List[str]										=	getattr(self.model_tester    ,			'encoder_seq_length'    ,			self.model_tester.seq_length)
        a__:     Any										=	getattr(self.model_tester    ,			'key_length'    ,			lowercase)

        for model_class in self.all_model_classes:
          a__:     Any										=	self._prepare_for_class(lowercase    ,			lowercase)
          a__:     Any										=	model_class(lowercase)
          a__:     Union[str, Any]										=	len(model(lowercase))

          with tempfile.TemporaryDirectory() as tmpdirname:
            model.save_pretrained(lowercase    ,			saved_model=lowercase)
            a__:     List[Any]										=	os.path.join(lowercase    ,			'saved_model'    ,			'1')
            a__:     Tuple										=	tf.keras.models.load_model(lowercase)
            a__:     Optional[Any]										=	model(lowercase)

            if self.is_encoder_decoder:
              a__:     Union[str, Any]										=	outputs['encoder_hidden_states']
              a__:     Tuple										=	outputs['encoder_attentions']
            else:
              a__:     int										=	outputs['hidden_states']
              a__:     str										=	outputs['attentions']

            self.assertEqual(len(lowercase)    ,			lowercase)

            a__:     List[str]										=	getattr(
                self.model_tester    ,			'expected_num_hidden_layers'    ,			self.model_tester.num_hidden_layers + 1)

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

            self.assertEqual(len(lowercase)    ,			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)	->			List[Any]:





        '''simple docstring'''





        a__:     List[Any]										=	TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
        self.assertIsNotNone(lowercase)



      def   lowerCamelCase_   (						self)	->			Optional[int]:





        '''simple docstring'''





        a__							,    a__:     int										=	self.model_tester.prepare_config_and_inputs_for_common()
        a__:     Tuple										=	True
        a__:     Optional[int]										=	getattr(self.model_tester    ,			'decoder_seq_length'    ,			self.model_tester.seq_length)
        a__:     List[Any]										=	getattr(self.model_tester    ,			'encoder_seq_length'    ,			self.model_tester.seq_length)
        a__:     Any										=	getattr(self.model_tester    ,			'key_length'    ,			lowercase)
        a__:     Optional[int]										=	getattr(self.model_tester    ,			'key_length'    ,			lowercase)

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

          if self.is_encoder_decoder:
            a__:     Dict										=	model_class(lowercase)
            a__:     Optional[Any]										=	model(self._prepare_for_class(lowercase    ,			lowercase))
            self.assertEqual(config.output_hidden_states    ,			lowercase)
            check_decoder_attentions_output(lowercase)

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

          # Check attention is always last and order is fine
          a__:     int										=	True
          a__:     List[str]										=	True
          a__:     Tuple										=	model_class(lowercase)
          a__:     int										=	model(self._prepare_for_class(lowercase    ,			lowercase))

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







@require_tf
class    __snake_case       (     unittest.TestCase				):



      @slow
      def   lowerCamelCase_   (						self)	->			Optional[int]:





        '''simple docstring'''





        a__:     int										=	TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
        a__:     Dict										=	tf.constant([[0, 1, 2, 3, 4, 5]])
        a__:     str										=	model(lowercase)[0]

        a__:     int										=	[1, 6, 7_68]
        self.assertEqual(output.shape    ,			lowercase)

        a__:     int										=	tf.constant(
            [
                [
                    [-0.03475493, -0.4686034, -0.30638832],
                    [0.22637248, -0.26988646, -0.7423424],
                    [0.10324868, -0.45013508, -0.58280784],
                ]
            ])
        tf.debugging.assert_near(output[:, :3, :3]    ,			lowercase    ,			atol=1e-4)



"""simple docstring"""
from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowercase__					       =	logging.get_logger(__name__)

lowercase__					       =	{
    'andreasmadsen/efficient_mlm_m0.40': (
        'https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json'
    ),
}




class    __snake_case       (     __lowerCAmelCase				):
      a__        =		"""roberta-prelayernorm"""



      def __init__(						self    ,			lowercase=5_02_65    ,			lowercase=7_68    ,			lowercase=12    ,			lowercase=12    ,			lowercase=30_72    ,			lowercase="gelu"    ,			lowercase=0.1    ,			lowercase=0.1    ,			lowercase=5_12    ,			lowercase=2    ,			lowercase=0.02    ,			lowercase=1e-12    ,			lowercase=1    ,			lowercase=0    ,			lowercase=2    ,			lowercase="absolute"    ,			lowercase=True    ,			lowercase=None    ,			**lowercase    ,			)	->			Any:





        '''simple docstring'''





        super().__init__(pad_token_id=lowercase    ,			bos_token_id=lowercase    ,			eos_token_id=lowercase    ,			**lowercase)

        a__:     Union[str, Any]										=	vocab_size
        a__:     str										=	hidden_size
        a__:     Tuple										=	num_hidden_layers
        a__:     List[str]										=	num_attention_heads
        a__:     Dict										=	hidden_act
        a__:     int										=	intermediate_size
        a__:     Tuple										=	hidden_dropout_prob
        a__:     str										=	attention_probs_dropout_prob
        a__:     Tuple										=	max_position_embeddings
        a__:     Tuple										=	type_vocab_size
        a__:     Optional[Any]										=	initializer_range
        a__:     Tuple										=	layer_norm_eps
        a__:     Optional[int]										=	position_embedding_type
        a__:     Any										=	use_cache
        a__:     Dict										=	classifier_dropout







class    __snake_case       (     __lowerCAmelCase				):



      @property
      def   lowerCamelCase_   (						self)	->			Mapping[str, Mapping[int, str]]:





        '''simple docstring'''





        if self.task == "multiple-choice":
          a__:     str										=	{0: 'batch', 1: 'choice', 2: 'sequence'}
        else:
          a__:     Union[str, Any]										=	{0: 'batch', 1: 'sequence'}
        return OrderedDict(
            [
                ('input_ids', dynamic_axis),
                ('attention_mask', dynamic_axis),
            ])


