




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




     '''simple docstring'''


     def __init__(    self						,	__a						,	__a=13						,	__a=7						,	__a=True						,	__a=True						,	__a=True						,	__a=True						,	__a=99						,	__a=32						,	__a=2						,	__a=4						,	__a=37						,	__a="gelu"						,	__a=0.1						,	__a=0.1						,	__a=512						,	__a=16						,	__a=2						,	__a=0.02						,	__a=3						,	__a=4						,	__a=None						,	):
        __lowerCamelCase					:	List[Any]	       =				parent
        __lowerCamelCase					:	List[str]	       =				13
        __lowerCamelCase					:	List[str]	       =				7
        __lowerCamelCase					:	Optional[int]	       =				True
        __lowerCamelCase					:	Dict	       =				True
        __lowerCamelCase					:	int	       =				True
        __lowerCamelCase					:	Dict	       =				True
        __lowerCamelCase					:	Union[str, Any]	       =				99
        __lowerCamelCase					:	Union[str, Any]	       =				384
        __lowerCamelCase					:	Union[str, Any]	       =				2
        __lowerCamelCase					:	Optional[int]	       =				4
        __lowerCamelCase					:	List[str]	       =				37
        __lowerCamelCase					:	List[Any]	       =				'gelu'
        __lowerCamelCase					:	str	       =				0.1
        __lowerCamelCase					:	Optional[Any]	       =				0.1
        __lowerCamelCase					:	Union[str, Any]	       =				512
        __lowerCamelCase					:	Dict	       =				16
        __lowerCamelCase					:	int	       =				2
        __lowerCamelCase					:	List[Any]	       =				0.02
        __lowerCamelCase					:	List[Any]	       =				3
        __lowerCamelCase					:	Optional[Any]	       =				4
        __lowerCamelCase					:	str	       =				128
        __lowerCamelCase					:	int	       =				2
        __lowerCamelCase					:	Any	       =				9
        __lowerCamelCase					:	List[str]	       =				1
        __lowerCamelCase					:	Optional[int]	       =				None
     def     snake_case_				(    self     ):
        __lowerCamelCase					:	Optional[int]	       =				ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size     )

        __lowerCamelCase					:	List[Any]	       =				None
        if self.use_input_mask:
           __lowerCamelCase					:	Union[str, Any]	       =				random_attention_mask([self.batch_size, self.seq_length]     )

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

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

        __lowerCamelCase					:	List[str]	       =				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=__a						,	)

        return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
     def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
        __lowerCamelCase					:	Tuple	       =				TFConvBertModel(config=__a     )
        __lowerCamelCase					:	Any	       =				{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

        __lowerCamelCase					:	Dict	       =				[input_ids, input_mask]
        __lowerCamelCase					:	Any	       =				model(__a     )

        __lowerCamelCase					:	List[str]	       =				model(__a     )

        self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)     )
     def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
        __lowerCamelCase					:	List[str]	       =				TFConvBertForMaskedLM(config=__a     )
        __lowerCamelCase					:	Dict	       =				{
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }
        __lowerCamelCase					:	Dict	       =				model(__a     )
        self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.vocab_size)     )
     def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
        __lowerCamelCase					:	Tuple	       =				self.num_labels
        __lowerCamelCase					:	Optional[int]	       =				TFConvBertForSequenceClassification(config=__a     )
        __lowerCamelCase					:	Optional[int]	       =				{
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }

        __lowerCamelCase					:	Optional[Any]	       =				model(__a     )
        self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_labels)     )
     def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
        __lowerCamelCase					:	Dict	       =				self.num_choices
        __lowerCamelCase					:	Union[str, Any]	       =				TFConvBertForMultipleChoice(config=__a     )
        __lowerCamelCase					:	Any	       =				tf.tile(tf.expand_dims(__a						,	1     )						,	(1, self.num_choices, 1)     )
        __lowerCamelCase					:	Dict	       =				tf.tile(tf.expand_dims(__a						,	1     )						,	(1, self.num_choices, 1)     )
        __lowerCamelCase					:	Dict	       =				tf.tile(tf.expand_dims(__a						,	1     )						,	(1, self.num_choices, 1)     )
        __lowerCamelCase					:	Tuple	       =				{
            'input_ids': multiple_choice_inputs_ids,
            'attention_mask': multiple_choice_input_mask,
            'token_type_ids': multiple_choice_token_type_ids,
        }
        __lowerCamelCase					:	int	       =				model(__a     )
        self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_choices)     )
     def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
        __lowerCamelCase					:	Any	       =				self.num_labels
        __lowerCamelCase					:	Any	       =				TFConvBertForTokenClassification(config=__a     )
        __lowerCamelCase					:	List[Any]	       =				{
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }
        __lowerCamelCase					:	Optional[int]	       =				model(__a     )
        self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.num_labels)     )
     def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
        __lowerCamelCase					:	Optional[Any]	       =				TFConvBertForQuestionAnswering(config=__a     )
        __lowerCamelCase					:	List[Any]	       =				{
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }

        __lowerCamelCase					:	List[Any]	       =				model(__a     )
        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     snake_case_				(    self     ):
        __lowerCamelCase					:	Dict	       =				self.prepare_config_and_inputs()
        (
            (
            __lowerCamelCase		
        )       ,      (
            __lowerCamelCase		
        )       ,      (
            __lowerCamelCase		
        )       ,      (
            __lowerCamelCase		
        )       ,      (
            __lowerCamelCase		
        )       ,      (
            __lowerCamelCase		
        )       ,      (
            __lowerCamelCase		
        )       ,      		
        )					:	Tuple	       =				config_and_inputs
        __lowerCamelCase					:	Any	       =				{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
        return config, inputs_dict



@require_tf
class __lowercase(      __UpperCAmelCase	,	__UpperCAmelCase	,	unittest.TestCase				):




     '''simple docstring'''


     __a						:						Dict     		= (
         (
             TFConvBertModel,
             TFConvBertForMaskedLM,
             TFConvBertForQuestionAnswering,
             TFConvBertForSequenceClassification,
             TFConvBertForTokenClassification,
             TFConvBertForMultipleChoice,
         )
         if is_tf_available()
         else ()
     )
     __a						:						Any     		= (
         {
             'feature-extraction': TFConvBertModel,
             'fill-mask': TFConvBertForMaskedLM,
             'question-answering': TFConvBertForQuestionAnswering,
             'text-classification': TFConvBertForSequenceClassification,
             'token-classification': TFConvBertForTokenClassification,
             'zero-shot': TFConvBertForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     __a						:						Any     		= False
     __a						:						Optional[int]     		= False
     __a						:						int     		= False
     def     snake_case_				(    self     ):
        __lowerCamelCase					:	List[Any]	       =				TFConvBertModelTester(self     )
        __lowerCamelCase					:	Any	       =				ConfigTester(self						,	config_class=__a						,	hidden_size=37     )
     def     snake_case_				(    self     ):
        self.config_tester.run_common_tests()
     def     snake_case_				(    self     ):
        __lowerCamelCase					:	Union[str, Any]	       =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__a     )
     def     snake_case_				(    self     ):
        __lowerCamelCase					:	Dict	       =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*__a     )
     def     snake_case_				(    self     ):
        __lowerCamelCase					:	Optional[Any]	       =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*__a     )
     def     snake_case_				(    self     ):
        __lowerCamelCase					:	List[str]	       =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*__a     )
     def     snake_case_				(    self     ):
        __lowerCamelCase					:	str	       =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*__a     )
     def     snake_case_				(    self     ):
        __lowerCamelCase					:	Optional[int]	       =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*__a     )
     @slow
     def     snake_case_				(    self     ):
        __lowerCamelCase       ,      __lowerCamelCase					:	Optional[Any]	       =				self.model_tester.prepare_config_and_inputs_for_common()
        __lowerCamelCase					:	Tuple	       =				True
        __lowerCamelCase					:	int	       =				True

        if hasattr(__a						,	'use_cache'     ):
           __lowerCamelCase					:	int	       =				True

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

        for model_class in self.all_model_classes:
           __lowerCamelCase					:	int	       =				self._prepare_for_class(__a						,	__a     )
           __lowerCamelCase					:	List[Any]	       =				model_class(__a     )
           __lowerCamelCase					:	Any	       =				len(model(__a     )     )

           with tempfile.TemporaryDirectory() as tmpdirname:
              model.save_pretrained(__a						,	saved_model=__a     )
              __lowerCamelCase					:	Optional[Any]	       =				os.path.join(__a						,	'saved_model'						,	'1'     )
              __lowerCamelCase					:	Optional[int]	       =				tf.keras.models.load_model(__a     )
              __lowerCamelCase					:	Dict	       =				model(__a     )

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

              self.assertEqual(len(__a     )						,	__a     )

              __lowerCamelCase					:	Optional[Any]	       =				getattr(
                  self.model_tester						,	'expected_num_hidden_layers'						,	self.model_tester.num_hidden_layers + 1     )

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

              self.assertEqual(len(__a     )						,	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     snake_case_				(    self     ):
        __lowerCamelCase					:	Tuple	       =				TFConvBertModel.from_pretrained('YituTech/conv-bert-base'     )
        self.assertIsNotNone(__a     )





     def     snake_case_				(    self     ):
        __lowerCamelCase       ,      __lowerCamelCase					:	Optional[int]	       =				self.model_tester.prepare_config_and_inputs_for_common()
        __lowerCamelCase					:	Optional[Any]	       =				True
        __lowerCamelCase					:	Dict	       =				getattr(self.model_tester						,	'decoder_seq_length'						,	self.model_tester.seq_length     )
        __lowerCamelCase					:	Tuple	       =				getattr(self.model_tester						,	'encoder_seq_length'						,	self.model_tester.seq_length     )
        __lowerCamelCase					:	Union[str, Any]	       =				getattr(self.model_tester						,	'key_length'						,	__a     )
        __lowerCamelCase					:	List[Any]	       =				getattr(self.model_tester						,	'key_length'						,	__a     )

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

           if self.is_encoder_decoder:
              __lowerCamelCase					:	List[Any]	       =				model_class(__a     )
              __lowerCamelCase					:	Union[str, Any]	       =				model(self._prepare_for_class(__a						,	__a     )     )
              self.assertEqual(config.output_hidden_states						,	__a     )
              check_decoder_attentions_output(__a     )

           # Check that output attentions can also be changed via the config
           del inputs_dict["output_attentions"]
           __lowerCamelCase					:	Optional[int]	       =				True
           __lowerCamelCase					:	List[Any]	       =				model_class(__a     )
           __lowerCamelCase					:	Union[str, Any]	       =				model(self._prepare_for_class(__a						,	__a     )     )
           self.assertEqual(config.output_hidden_states						,	__a     )
           check_encoder_attentions_output(__a     )

           # Check attention is always last and order is fine
           __lowerCamelCase					:	Dict	       =				True
           __lowerCamelCase					:	Dict	       =				True
           __lowerCamelCase					:	Union[str, Any]	       =				model_class(__a     )
           __lowerCamelCase					:	Tuple	       =				model(self._prepare_for_class(__a						,	__a     )     )

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



@require_tf
class __lowercase(      unittest.TestCase				):




     '''simple docstring'''


     @slow
     def     snake_case_				(    self     ):
        __lowerCamelCase					:	Union[str, Any]	       =				TFConvBertModel.from_pretrained('YituTech/conv-bert-base'     )
        __lowerCamelCase					:	List[str]	       =				tf.constant([[0, 1, 2, 3, 4, 5]]     )
        __lowerCamelCase					:	Tuple	       =				model(__a     )[0]

        __lowerCamelCase					:	List[str]	       =				[1, 6, 768]
        self.assertEqual(output.shape						,	__a     )

        __lowerCamelCase					:	List[Any]	       =				tf.constant(
            [
                [
                    [-0.03_475_493, -0.4_686_034, -0.30_638_832],
                    [0.22_637_248, -0.26_988_646, -0.7_423_424],
                    [0.10_324_868, -0.45_013_508, -0.58_280_784],
                ]
            ]     )
        tf.debugging.assert_near(output[:, :3, :3]						,	__a						,	atol=1E-4     )









"""simple docstring"""
import random





def 						UpperCAmelCase				(			A__:    Union[str, Any]							,				A__:    List[str]							,				A__:    Union[str, Any]     )  ->     int:
			__lowerCamelCase					:	Optional[Any]	       =				a[left_index]
			__lowerCamelCase					:	int	       =				left_index + 1
			for j in range(left_index + 1							,				A__     ):
						if a[j] < pivot:
									__lowerCamelCase       ,      __lowerCamelCase					:	Optional[int]	       =				a[i], a[j]
									i += 1
			__lowerCamelCase       ,      __lowerCamelCase					:	str	       =				a[i - 1], a[left_index]
			return i - 1





def 						UpperCAmelCase				(			A__:    List[Any]							,				A__:    Tuple							,				A__:    Tuple     )  ->     Dict:
			if left < right:
						__lowerCamelCase					:	Optional[int]	       =				random.randint(A__							,				right - 1     )
						__lowerCamelCase       ,      __lowerCamelCase					:	int	       =				(
						    a[left],
						    a[pivot],
						)  # switches the pivot with the left most bound
						__lowerCamelCase					:	Union[str, Any]	       =				partition(A__							,				A__							,				A__     )
						quick_sort_random(
						    A__							,				A__							,				A__     )  # recursive quicksort to the left of the pivot point
						quick_sort_random(
						    A__							,				pivot_index + 1							,				A__     )  # recursive quicksort to the right of the pivot point





def 						UpperCAmelCase				(			)  ->     int:
			__lowerCamelCase					:	Dict	       =				input('Enter numbers separated by a comma:\n'     ).strip()
			__lowerCamelCase					:	int	       =				[int(A__     ) for item in user_input.split(','     )]

			quick_sort_random(A__							,				0							,				len(A__     )     )

			print(A__     )


if __name__ == "__main__":
					main()



