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




      def __init__(							self   ,       lowercase_   ,       lowercase_=1_3   ,       lowercase_=7   ,       lowercase_=True   ,       lowercase_=True   ,       lowercase_=True   ,       lowercase_=True   ,       lowercase_=9_9   ,       lowercase_=3_2   ,       lowercase_=2   ,       lowercase_=4   ,       lowercase_=3_7   ,       lowercase_="gelu"   ,       lowercase_=0.1   ,       lowercase_=0.1   ,       lowercase_=5_1_2   ,       lowercase_=1_6   ,       lowercase_=2   ,       lowercase_=0.0_2   ,       lowercase_=3   ,       lowercase_=4   ,       lowercase_=None   ,       )						->					Any:
          UpperCAmelCase									=					parent
          UpperCAmelCase									=					1_3
          UpperCAmelCase									=					7
          UpperCAmelCase									=					True
          UpperCAmelCase									=					True
          UpperCAmelCase									=					True
          UpperCAmelCase									=					True
          UpperCAmelCase									=					9_9
          UpperCAmelCase									=					3_8_4
          UpperCAmelCase									=					2
          UpperCAmelCase									=					4
          UpperCAmelCase									=					3_7
          UpperCAmelCase									=					'gelu'
          UpperCAmelCase									=					0.1
          UpperCAmelCase									=					0.1
          UpperCAmelCase									=					5_1_2
          UpperCAmelCase									=					1_6
          UpperCAmelCase									=					2
          UpperCAmelCase									=					0.0_2
          UpperCAmelCase									=					3
          UpperCAmelCase									=					4
          UpperCAmelCase									=					1_2_8
          UpperCAmelCase									=					2
          UpperCAmelCase									=					9
          UpperCAmelCase									=					1
          UpperCAmelCase									=					None




      def     a_      (							self							)						->					int:
          UpperCAmelCase									=					ids_tensor([self.batch_size, self.seq_length]   ,       self.vocab_size							)

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

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

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

          UpperCAmelCase									=					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     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_							)						->					Optional[int]:
          UpperCAmelCase									=					TFConvBertModel(config=lowercase_							)
          UpperCAmelCase									=					{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

          UpperCAmelCase									=					[input_ids, input_mask]
          UpperCAmelCase									=					model(lowercase_							)

          UpperCAmelCase									=					model(lowercase_							)

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




      def     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_							)						->					Dict:
          UpperCAmelCase									=					TFConvBertForMaskedLM(config=lowercase_							)
          UpperCAmelCase									=					{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }
          UpperCAmelCase									=					model(lowercase_							)
          self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.seq_length, self.vocab_size)							)




      def     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_							)						->					Optional[Any]:
          UpperCAmelCase									=					self.num_labels
          UpperCAmelCase									=					TFConvBertForSequenceClassification(config=lowercase_							)
          UpperCAmelCase									=					{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          UpperCAmelCase									=					model(lowercase_							)
          self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.num_labels)							)




      def     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_							)						->					str:
          UpperCAmelCase									=					self.num_choices
          UpperCAmelCase									=					TFConvBertForMultipleChoice(config=lowercase_							)
          UpperCAmelCase									=					tf.tile(tf.expand_dims(lowercase_   ,       1							)   ,       (1, self.num_choices, 1)							)
          UpperCAmelCase									=					tf.tile(tf.expand_dims(lowercase_   ,       1							)   ,       (1, self.num_choices, 1)							)
          UpperCAmelCase									=					tf.tile(tf.expand_dims(lowercase_   ,       1							)   ,       (1, self.num_choices, 1)							)
          UpperCAmelCase									=					{
              'input_ids': multiple_choice_inputs_ids,
              'attention_mask': multiple_choice_input_mask,
              'token_type_ids': multiple_choice_token_type_ids,
          }
          UpperCAmelCase									=					model(lowercase_							)
          self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.num_choices)							)




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




      def     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_							)						->					Optional[Any]:
          UpperCAmelCase									=					TFConvBertForQuestionAnswering(config=lowercase_							)
          UpperCAmelCase									=					{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          UpperCAmelCase									=					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     a_      (							self							)						->					Union[str, Any]:
          UpperCAmelCase									=					self.prepare_config_and_inputs()
          (
              (
              UpperCAmelCase
          )			,							(
              UpperCAmelCase
          )			,							(
              UpperCAmelCase
          )			,							(
              UpperCAmelCase
          )			,							(
              UpperCAmelCase
          )			,							(
              UpperCAmelCase
          )			,							(
              UpperCAmelCase
          )			,							
          )									=					config_and_inputs
          UpperCAmelCase									=					{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
          return config, inputs_dict

@require_tf
class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_     ,			SCREAMING_SNAKE_CASE_     ,			unittest.TestCase							):
      __SCREAMING_SNAKE_CASE   :							List[Any]							      =	(
          (
              TFConvBertModel,
              TFConvBertForMaskedLM,
              TFConvBertForQuestionAnswering,
              TFConvBertForSequenceClassification,
              TFConvBertForTokenClassification,
              TFConvBertForMultipleChoice,
          )
          if is_tf_available()
          else ()
      )
      __SCREAMING_SNAKE_CASE   :							Optional[int]							      =	(
          {
              "feature-extraction": TFConvBertModel,
              "fill-mask": TFConvBertForMaskedLM,
              "question-answering": TFConvBertForQuestionAnswering,
              "text-classification": TFConvBertForSequenceClassification,
              "token-classification": TFConvBertForTokenClassification,
              "zero-shot": TFConvBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      __SCREAMING_SNAKE_CASE   :							List[str]							      =	False
      __SCREAMING_SNAKE_CASE   :							Union[str, Any]							      =	False
      __SCREAMING_SNAKE_CASE   :							Optional[Any]							      =	False




      def     a_      (							self							)						->					Optional[Any]:
          UpperCAmelCase									=					TFConvBertModelTester(self							)
          UpperCAmelCase									=					ConfigTester(self   ,       config_class=lowercase_   ,       hidden_size=3_7							)




      def     a_      (							self							)						->					Union[str, Any]:
          self.config_tester.run_common_tests()




      def     a_      (							self							)						->					Any:
          UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowercase_							)




      def     a_      (							self							)						->					Tuple:
          UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*lowercase_							)




      def     a_      (							self							)						->					Tuple:
          UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*lowercase_							)




      def     a_      (							self							)						->					List[str]:
          UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*lowercase_							)




      def     a_      (							self							)						->					Tuple:
          UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*lowercase_							)




      def     a_      (							self							)						->					Dict:
          UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*lowercase_							)




      @slow
      def     a_      (							self							)						->					Optional[Any]:
          UpperCAmelCase			,							UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs_for_common()
          UpperCAmelCase									=					True
          UpperCAmelCase									=					True

          if hasattr(lowercase_   ,       'use_cache'							):
              UpperCAmelCase									=					True

          UpperCAmelCase									=					getattr(self.model_tester   ,       'encoder_seq_length'   ,       self.model_tester.seq_length							)
          UpperCAmelCase									=					getattr(self.model_tester   ,       'key_length'   ,       lowercase_							)

          for model_class in self.all_model_classes:
              UpperCAmelCase									=					self._prepare_for_class(lowercase_   ,       lowercase_							)
              UpperCAmelCase									=					model_class(lowercase_							)
              UpperCAmelCase									=					len(model(lowercase_							)							)

              with tempfile.TemporaryDirectory() as tmpdirname:
                  model.save_pretrained(lowercase_   ,       saved_model=lowercase_							)
                  UpperCAmelCase									=					os.path.join(lowercase_   ,       'saved_model'   ,       '1'							)
                  UpperCAmelCase									=					tf.keras.models.load_model(lowercase_							)
                  UpperCAmelCase									=					model(lowercase_							)

                  if self.is_encoder_decoder:
                      UpperCAmelCase									=					outputs['encoder_hidden_states']
                      UpperCAmelCase									=					outputs['encoder_attentions']
                  else:
                      UpperCAmelCase									=					outputs['hidden_states']
                      UpperCAmelCase									=					outputs['attentions']

                  self.assertEqual(len(lowercase_							)   ,       lowercase_							)

                  UpperCAmelCase									=					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     a_      (							self							)						->					Any:
          UpperCAmelCase									=					TFConvBertModel.from_pretrained('YituTech/conv-bert-base'							)
          self.assertIsNotNone(lowercase_							)




      def     a_      (							self							)						->					Optional[Any]:
          UpperCAmelCase			,							UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs_for_common()
          UpperCAmelCase									=					True
          UpperCAmelCase									=					getattr(self.model_tester   ,       'decoder_seq_length'   ,       self.model_tester.seq_length							)
          UpperCAmelCase									=					getattr(self.model_tester   ,       'encoder_seq_length'   ,       self.model_tester.seq_length							)
          UpperCAmelCase									=					getattr(self.model_tester   ,       'key_length'   ,       lowercase_							)
          UpperCAmelCase									=					getattr(self.model_tester   ,       'key_length'   ,       lowercase_							)

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

              if self.is_encoder_decoder:
                  UpperCAmelCase									=					model_class(lowercase_							)
                  UpperCAmelCase									=					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"]
              UpperCAmelCase									=					True
              UpperCAmelCase									=					model_class(lowercase_							)
              UpperCAmelCase									=					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
              UpperCAmelCase									=					True
              UpperCAmelCase									=					True
              UpperCAmelCase									=					model_class(lowercase_							)
              UpperCAmelCase									=					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      _UpperCAmelCase							(		unittest.TestCase							):




      @slow
      def     a_      (							self							)						->					Dict:
          UpperCAmelCase									=					TFConvBertModel.from_pretrained('YituTech/conv-bert-base'							)
          UpperCAmelCase									=					tf.constant([[0, 1, 2, 3, 4, 5]]							)
          UpperCAmelCase									=					model(lowercase_							)[0]

          UpperCAmelCase									=					[1, 6, 7_6_8]
          self.assertEqual(output.shape   ,       lowercase_							)

          UpperCAmelCase									=					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]   ,       lowercase_   ,       atol=1E-4							)





"""simple docstring"""

from multiprocessing import Lock, Pipe, Process

# lock used to ensure that two processes do not access a pipe at the same time
SCREAMING_SNAKE_CASE_    					=						Lock()




def     lowercase__						(					lowerCAmelCase				:					Dict ,				lowerCAmelCase				:					Dict ,				lowerCAmelCase				:					Union[str, Any] ,				lowerCAmelCase				:					Optional[Any] ,				lowerCAmelCase				:					str ,				lowerCAmelCase				:					Optional[int] ,				lowerCAmelCase				:					Optional[Any] ) ->      Optional[int]:






    """simple docstring"""




    global process_lock

    # we perform n swaps since after n swaps we know we are sorted
    # we *could* stop early if we are sorted already, but it takes as long to
    # find out we are sorted as it does to sort the list with this algorithm
    for i in range(0 ,				10 ):
        if (i + position) % 2 == 0 and r_send is not None:
            # send your value to your right neighbor
            process_lock.acquire()
            r_send[1].send(lowerCAmelCase )
            process_lock.release()

            # receive your right neighbor's value
            process_lock.acquire()
            UpperCAmelCase									=					rr_cv[0].recv()
            process_lock.release()

            # take the lower value since you are on the left
            UpperCAmelCase									=					min(lowerCAmelCase ,				lowerCAmelCase )
        elif (i + position) % 2 != 0 and l_send is not None:
            # send your value to your left neighbor
            process_lock.acquire()
            l_send[1].send(lowerCAmelCase )
            process_lock.release()

            # receive your left neighbor's value
            process_lock.acquire()
            UpperCAmelCase									=					lr_cv[0].recv()
            process_lock.release()

            # take the higher value since you are on the right
            UpperCAmelCase									=					max(lowerCAmelCase ,				lowerCAmelCase )
    # after all swaps are performed, send the values back to main
    result_pipe[1].send(lowerCAmelCase )




def     lowercase__						(					lowerCAmelCase				:					Union[str, Any] ) ->      List[str]:






    """simple docstring"""




    UpperCAmelCase									=					[]
    UpperCAmelCase									=					[]
    # initialize the list of pipes where the values will be retrieved
    for _ in arr:
        result_pipe.append(Pipe() )
    # creates the processes
    # the first and last process only have one neighbor so they are made outside
    # of the loop
    UpperCAmelCase									=					Pipe()
    UpperCAmelCase									=					Pipe()
    process_array_.append(
        Process(
            target=lowerCAmelCase ,				args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) ,				) )
    UpperCAmelCase									=					temp_rs
    UpperCAmelCase									=					temp_rr

    for i in range(1 ,				len(lowerCAmelCase ) - 1 ):
        UpperCAmelCase									=					Pipe()
        UpperCAmelCase									=					Pipe()
        process_array_.append(
            Process(
                target=lowerCAmelCase ,				args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) ,				) )
        UpperCAmelCase									=					temp_rs
        UpperCAmelCase									=					temp_rr

    process_array_.append(
        Process(
            target=lowerCAmelCase ,				args=(
                len(lowerCAmelCase ) - 1,
                arr[len(lowerCAmelCase ) - 1],
                temp_ls,
                None,
                temp_lr,
                None,
                result_pipe[len(lowerCAmelCase ) - 1],
            ) ,				) )

    # start the processes
    for p in process_array_:
        p.start()

    # wait for the processes to end and write their values to the list
    for p in range(0 ,				len(lowerCAmelCase ) ):
        UpperCAmelCase									=					result_pipe[p][0].recv()
        process_array_[p].join()
    return arr




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






    """simple docstring"""




    UpperCAmelCase									=					list(range(10 ,				0 ,				-1 ) )
    print('Initial List' )
    print(*lowerCAmelCase )
    UpperCAmelCase									=					odd_even_transposition(lowerCAmelCase )
    print('Sorted List\n' )
    print(*lowerCAmelCase )


if __name__ == "__main__":
   main()



