

import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
       OpenAIGPTConfig,
       OpenAIGPTDoubleHeadsModel,
       OpenAIGPTForSequenceClassification,
       OpenAIGPTLMHeadModel,
       OpenAIGPTModel,
   )



class       __snake_case  :

  def __init__(					self						,		_A						,		_A=13						,		_A=7						,		_A=True						,		_A=True						,		_A=True						,		_A=99						,		_A=32						,		_A=5						,		_A=4						,		_A=37						,		_A="gelu"						,		_A=0.1						,		_A=0.1						,		_A=512						,		_A=16						,		_A=2						,		_A=0.0_2						,		_A=3						,		_A=4						,		_A=None						,		):
      SCREAMING_SNAKE_CASE_									=						parent
      SCREAMING_SNAKE_CASE_									=						batch_size
      SCREAMING_SNAKE_CASE_									=						seq_length
      SCREAMING_SNAKE_CASE_									=						is_training
      SCREAMING_SNAKE_CASE_									=						use_token_type_ids
      SCREAMING_SNAKE_CASE_									=						use_labels
      SCREAMING_SNAKE_CASE_									=						vocab_size
      SCREAMING_SNAKE_CASE_									=						hidden_size
      SCREAMING_SNAKE_CASE_									=						num_hidden_layers
      SCREAMING_SNAKE_CASE_									=						num_attention_heads
      SCREAMING_SNAKE_CASE_									=						intermediate_size
      SCREAMING_SNAKE_CASE_									=						hidden_act
      SCREAMING_SNAKE_CASE_									=						hidden_dropout_prob
      SCREAMING_SNAKE_CASE_									=						attention_probs_dropout_prob
      SCREAMING_SNAKE_CASE_									=						max_position_embeddings
      SCREAMING_SNAKE_CASE_									=						type_vocab_size
      SCREAMING_SNAKE_CASE_									=						type_sequence_label_size
      SCREAMING_SNAKE_CASE_									=						initializer_range
      SCREAMING_SNAKE_CASE_									=						num_labels
      SCREAMING_SNAKE_CASE_									=						num_choices
      SCREAMING_SNAKE_CASE_									=						scope
      SCREAMING_SNAKE_CASE_									=						self.vocab_size - 1

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						ids_tensor([self.batch_size, self.seq_length]						,		self.vocab_size)

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

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

      SCREAMING_SNAKE_CASE_									=						OpenAIGPTConfig(
          vocab_size=self.vocab_size						,		n_embd=self.hidden_size						,		n_layer=self.num_hidden_layers						,		n_head=self.num_attention_heads						,		n_positions=self.max_position_embeddings						,		pad_token_id=self.pad_token_id						,		)

      SCREAMING_SNAKE_CASE_									=						ids_tensor([self.num_hidden_layers, self.num_attention_heads]						,		2)

      return (
          config,
          input_ids,
          head_mask,
          token_type_ids,
          sequence_labels,
          token_labels,
          choice_labels,
      )

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		*_A):
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTModel(config=_A)
      model.to(_A)
      model.eval()

      SCREAMING_SNAKE_CASE_									=						model(_A						,		token_type_ids=_A						,		head_mask=_A)
      SCREAMING_SNAKE_CASE_									=						model(_A						,		token_type_ids=_A)
      SCREAMING_SNAKE_CASE_									=						model(_A)

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

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		*_A):
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTLMHeadModel(_A)
      model.to(_A)
      model.eval()

      SCREAMING_SNAKE_CASE_									=						model(_A						,		token_type_ids=_A						,		labels=_A)
      self.parent.assertEqual(result.loss.shape						,		())
      self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.seq_length, self.vocab_size))

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		*_A):
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTDoubleHeadsModel(_A)
      model.to(_A)
      model.eval()

      SCREAMING_SNAKE_CASE_									=						model(_A						,		token_type_ids=_A						,		labels=_A)
      self.parent.assertEqual(result.loss.shape						,		())
      self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.seq_length, self.vocab_size))

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		*_A):
      SCREAMING_SNAKE_CASE_									=						self.num_labels
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTForSequenceClassification(_A)
      model.to(_A)
      model.eval()

      SCREAMING_SNAKE_CASE_									=						ids_tensor([self.batch_size]						,		self.type_sequence_label_size)
      SCREAMING_SNAKE_CASE_									=						model(_A						,		token_type_ids=_A						,		labels=_A)
      self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_labels))


  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.prepare_config_and_inputs()
      (
          SCREAMING_SNAKE_CASE_
      )									=						config_and_inputs
      SCREAMING_SNAKE_CASE_									=						{
          """input_ids""": input_ids,
          """token_type_ids""": token_type_ids,
          """head_mask""": head_mask,
      }

      return config, inputs_dict






@require_torch
class       __snake_case  (    A_     , A_     , A_     , unittest.TestCase       ):
  __lowerCAmelCase     :	Dict						 =  (
      (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
      if is_torch_available()
      else ()
  )
  __lowerCAmelCase     :	Dict						 =  (
      (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
  )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
  __lowerCAmelCase     :	List[Any]						 =  (
      {
          '''feature-extraction''': OpenAIGPTModel,
          '''text-classification''': OpenAIGPTForSequenceClassification,
          '''text-generation''': OpenAIGPTLMHeadModel,
          '''zero-shot''': OpenAIGPTForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		_A):
      if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
          # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
          # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
          # tiny config could not be created.
          return True

      return False

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A=False):
      SCREAMING_SNAKE_CASE_									=						super()._prepare_for_class(_A						,		_A						,		return_labels=_A)

      if return_labels:
          if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
              SCREAMING_SNAKE_CASE_									=						torch.zeros(
                  (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)						,		dtype=torch.long						,		device=_A						,		)
              SCREAMING_SNAKE_CASE_									=						inputs_dict["""labels"""]
              SCREAMING_SNAKE_CASE_									=						inputs_dict["""labels"""]
              SCREAMING_SNAKE_CASE_									=						torch.zeros(
                  (self.model_tester.batch_size, self.model_tester.num_choices)						,		dtype=torch.long						,		device=_A						,		)
              SCREAMING_SNAKE_CASE_									=						torch.zeros(
                  self.model_tester.batch_size						,		dtype=torch.long						,		device=_A)
      return inputs_dict

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTModelTester(self)
      SCREAMING_SNAKE_CASE_									=						ConfigTester(self						,		config_class=_A						,		n_embd=37)

  def 						lowerCAmelCase__     (					self):
      self.config_tester.run_common_tests()

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*_A)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_lm_head_model(*_A)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_double_lm_head_model(*_A)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_A)


  @slow
  def 						lowerCAmelCase__     (					self):
      for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          SCREAMING_SNAKE_CASE_									=						OpenAIGPTModel.from_pretrained(_A)
          self.assertIsNotNone(_A)






@require_torch
class       __snake_case  (    unittest.TestCase       ):

  @slow
  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTLMHeadModel.from_pretrained('openai-gpt')
      model.to(_A)
      SCREAMING_SNAKE_CASE_									=						torch.tensor([[481, 4735, 544]]						,		dtype=torch.long						,		device=_A)  # the president is
      SCREAMING_SNAKE_CASE_									=						[
          481,
          4735,
          544,
          246,
          963,
          870,
          762,
          239,
          244,
          40477,
          244,
          249,
          719,
          881,
          487,
          544,
          240,
          244,
          603,
          481,
      ]  # the president is a very good man. " \n " i\'m sure he is, " said the

      SCREAMING_SNAKE_CASE_									=						model.generate(_A						,		do_sample=_A)
      self.assertListEqual(output_ids[0].tolist()						,		_A)





from multiprocessing import Lock, Pipe, Process

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


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		str ,   _SCREAMING_SNAKE_CASE					:		Union[str, Any] ,   _SCREAMING_SNAKE_CASE					:		Dict ,   _SCREAMING_SNAKE_CASE					:		List[Any] ,   _SCREAMING_SNAKE_CASE					:		Optional[int] ,   _SCREAMING_SNAKE_CASE					:		Optional[int] ,   _SCREAMING_SNAKE_CASE					:		Any       ):





    """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(_SCREAMING_SNAKE_CASE       )
            process_lock.release()

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

            # take the lower value since you are on the left
            SCREAMING_SNAKE_CASE_									=						min(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       )
        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(_SCREAMING_SNAKE_CASE       )
            process_lock.release()

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

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


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Union[str, Any]       ):





    """simple docstring"""


    SCREAMING_SNAKE_CASE_									=						[]
    SCREAMING_SNAKE_CASE_									=						[]
    # 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
    SCREAMING_SNAKE_CASE_									=						Pipe()
    SCREAMING_SNAKE_CASE_									=						Pipe()
    process_array_.append(
        Process(
            target=_SCREAMING_SNAKE_CASE ,   args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) ,   )       )
    SCREAMING_SNAKE_CASE_									=						temp_rs
    SCREAMING_SNAKE_CASE_									=						temp_rr

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

    process_array_.append(
        Process(
            target=_SCREAMING_SNAKE_CASE ,   args=(
                len(_SCREAMING_SNAKE_CASE       ) - 1,
                arr[len(_SCREAMING_SNAKE_CASE       ) - 1],
                temp_ls,
                None,
                temp_lr,
                None,
                result_pipe[len(_SCREAMING_SNAKE_CASE       ) - 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(_SCREAMING_SNAKE_CASE       )       ):
        SCREAMING_SNAKE_CASE_									=						result_pipe[p][0].recv()
        process_array_[p].join()
    return arr


def 					_UpperCAmelCase       (  ):





    """simple docstring"""


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


if __name__ == "__main__":
       main()


