



'''simple docstring'''





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 __A			:



 '''simple docstring'''







 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.02 ,   A=3 ,   A=4 ,   A=None ,   )    ->     Dict:


      """simple docstring"""



      _a      =  parent
      _a      =  batch_size
      _a      =  seq_length
      _a      =  is_training
      _a      =  use_token_type_ids
      _a      =  use_labels
      _a      =  vocab_size
      _a      =  hidden_size
      _a      =  num_hidden_layers
      _a      =  num_attention_heads
      _a      =  intermediate_size
      _a      =  hidden_act
      _a      =  hidden_dropout_prob
      _a      =  attention_probs_dropout_prob
      _a      =  max_position_embeddings
      _a      =  type_vocab_size
      _a      =  type_sequence_label_size
      _a      =  initializer_range
      _a      =  num_labels
      _a      =  num_choices
      _a      =  scope
      _a      =  self.vocab_size - 1



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


      """simple docstring"""



      _a      =  ids_tensor([self.batch_size, self.seq_length] ,   self.vocab_size       )

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

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

      _a      =  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 ,   )

      _a      =  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 					a__  (self ,   A ,   A ,   A ,   A ,   *A       )    ->     int:


      """simple docstring"""



      _a      =  OpenAIGPTModel(config=A       )
      model.to(A       )
      model.eval()

      _a      =  model(A ,   token_type_ids=A ,   head_mask=A       )
      _a      =  model(A ,   token_type_ids=A       )
      _a      =  model(A       )

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



 def 					a__  (self ,   A ,   A ,   A ,   A ,   *A       )    ->     Dict:


      """simple docstring"""



      _a      =  OpenAIGPTLMHeadModel(A       )
      model.to(A       )
      model.eval()

      _a      =  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 					a__  (self ,   A ,   A ,   A ,   A ,   *A       )    ->     List[str]:


      """simple docstring"""



      _a      =  OpenAIGPTDoubleHeadsModel(A       )
      model.to(A       )
      model.eval()

      _a      =  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 					a__  (self ,   A ,   A ,   A ,   A ,   *A       )    ->     Tuple:


      """simple docstring"""



      _a      =  self.num_labels
      _a      =  OpenAIGPTForSequenceClassification(A       )
      model.to(A       )
      model.eval()

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



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


      """simple docstring"""



      _a      =  self.prepare_config_and_inputs()
      (
          (
          _a
      )  ,    (
          _a
      )  ,    (
          _a
      )  ,    (
          _a
      )  ,    (
          _a
      )  ,    (
          _a
      )  ,    (
          _a
      )  ,    
      )      =  config_and_inputs
      _a      =  {
          '''input_ids''': input_ids,
          '''token_type_ids''': token_type_ids,
          '''head_mask''': head_mask,
      }

      return config, inputs_dict






@require_torch
class __A			(  A   ,   A   ,   A   ,   unittest.TestCase						):



 '''simple docstring'''







 __lowerCamelCase    :       int          =       (
     (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
     if is_torch_available()
     else ()
 )
 __lowerCamelCase    :       Optional[Any]          =       (
     (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 __lowerCamelCase    :       Optional[Any]          =       (
     {
         'feature-extraction': OpenAIGPTModel,
         'text-classification': OpenAIGPTForSequenceClassification,
         'text-generation': OpenAIGPTLMHeadModel,
         'zero-shot': OpenAIGPTForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )



 def 					a__  (self ,   A ,   A ,   A ,   A ,   A       )    ->     Tuple:


      """simple docstring"""



      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 					a__  (self ,   A ,   A ,   A=False       )    ->     Optional[int]:


      """simple docstring"""



      _a      =  super()._prepare_for_class(A ,   A ,   return_labels=A       )

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



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


      """simple docstring"""



      _a      =  OpenAIGPTModelTester(self       )
      _a      =  ConfigTester(self ,   config_class=A ,   n_embd=37       )



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


      """simple docstring"""



      self.config_tester.run_common_tests()



 def 					a__  (self       )    ->     Dict:


      """simple docstring"""



      _a      =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*A       )



 def 					a__  (self       )    ->     Tuple:


      """simple docstring"""



      _a      =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_lm_head_model(*A       )



 def 					a__  (self       )    ->     Optional[Any]:


      """simple docstring"""



      _a      =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_double_lm_head_model(*A       )



 def 					a__  (self       )    ->     Optional[Any]:


      """simple docstring"""



      _a      =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*A       )



 @slow
 def 					a__  (self       )    ->     Dict:


      """simple docstring"""



      for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _a      =  OpenAIGPTModel.from_pretrained(A       )
           self.assertIsNotNone(A       )






@require_torch
class __A			(  unittest.TestCase						):



 '''simple docstring'''







 @slow
 def 					a__  (self       )    ->     Tuple:


      """simple docstring"""



      _a      =  OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''       )
      model.to(A       )
      _a      =  torch.tensor([[481, 4_735, 544]] ,   dtype=torch.long ,   device=A       )  # the president is
      _a      =  [
          481,
          4_735,
          544,
          246,
          963,
          870,
          762,
          239,
          244,
          40_477,
          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

      _a      =  model.generate(A ,   do_sample=A       )
      self.assertListEqual(output_ids[0].tolist() ,   A       )








'''simple docstring'''





from collections.abc import Generator
from math import sin




def lowerCAmelCase  (__A):



     """simple docstring"""



     if len(__A) != 32:
          raise ValueError('''Input must be of length 32''')

     _a      =  b''''''
     for i in [3, 2, 1, 0]:
          little_endian += string_aa[8 * i : 8 * i + 8]
     return little_endian




def lowerCAmelCase  (__A):



     """simple docstring"""



     if i < 0:
          raise ValueError('''Input must be non-negative''')

     _a      =  format(__A  ,	'''08x''')[-8:]
     _a      =  b''''''
     for i in [3, 2, 1, 0]:
          little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode('''utf-8''')
     return little_endian_hex




def lowerCAmelCase  (__A):



     """simple docstring"""



     _a      =  b''''''
     for char in message:
          bit_string += format(__A  ,	'''08b''').encode('''utf-8''')
     _a      =  format(len(__A)  ,	'''064b''').encode('''utf-8''')

     # Pad bit_string to a multiple of 512 chars
     bit_string += b"1"
     while len(__A) % 512 != 448:
          bit_string += b"0"
     bit_string += to_little_endian(start_len[32:]) + to_little_endian(start_len[:32])

     return bit_string




def lowerCAmelCase  (__A):



     """simple docstring"""



     if len(__A) % 512 != 0:
          raise ValueError('''Input must have length that\'s a multiple of 512''')

     for pos in range(0  ,	len(__A)  ,	512):
          _a      =  bit_string[pos : pos + 512]
          _a      =  []
          for i in range(0  ,	512  ,	32):
               block_words.append(int(to_little_endian(block[i : i + 32])  ,	2))
          yield block_words




def lowerCAmelCase  (__A):



     """simple docstring"""



     if i < 0:
          raise ValueError('''Input must be non-negative''')

     _a      =  format(__A  ,	'''032b''')
     _a      =  ''''''
     for c in i_str:
          new_str += "1" if c == "0" else "0"
     return int(__A  ,	2)




def lowerCAmelCase  (__A  ,	__A):



     """simple docstring"""



     return (a + b) % 2**32




def lowerCAmelCase  (__A  ,	__A):



     """simple docstring"""



     if i < 0:
          raise ValueError('''Input must be non-negative''')
     if shift < 0:
          raise ValueError('''Shift must be non-negative''')
     return ((i << shift) ^ (i >> (32 - shift))) % 2**32




def lowerCAmelCase  (__A):



     """simple docstring"""



     _a      =  preprocess(__A)

     _a      =  [int(2**32 * abs(sin(i + 1))) for i in range(64)]

     # Starting states
     _a      =  0x67_452_301
     _a      =  0xEF_CDA_B89
     _a      =  0x98_BAD_CFE
     _a      =  0x10_325_476

     _a      =  [
         7,
         12,
         17,
         22,
         7,
         12,
         17,
         22,
         7,
         12,
         17,
         22,
         7,
         12,
         17,
         22,
         5,
         9,
         14,
         20,
         5,
         9,
         14,
         20,
         5,
         9,
         14,
         20,
         5,
         9,
         14,
         20,
         4,
         11,
         16,
         23,
         4,
         11,
         16,
         23,
         4,
         11,
         16,
         23,
         4,
         11,
         16,
         23,
         6,
         10,
         15,
         21,
         6,
         10,
         15,
         21,
         6,
         10,
         15,
         21,
         6,
         10,
         15,
         21,
     ]

     # Process bit string in chunks, each with 16 32-char words
     for block_words in get_block_words(__A):
          _a      =  aa
          _a      =  ba
          _a      =  ca
          _a      =  da

          # Hash current chunk
          for i in range(64):
               if i <= 15:
                    # f = (b & c) | (not_32(b) & d)     # Alternate definition for f
                    _a      =  d ^ (b & (c ^ d))
                    _a      =  i
               elif i <= 31:
                    # f = (d & b) | (not_32(d) & c)     # Alternate definition for f
                    _a      =  c ^ (d & (b ^ c))
                    _a      =  (5 * i + 1) % 16
               elif i <= 47:
                    _a      =  b ^ c ^ d
                    _a      =  (3 * i + 5) % 16
               else:
                    _a      =  c ^ (b | not_aa(__A))
                    _a      =  (7 * i) % 16
               _a      =  (f + a + added_consts[i] + block_words[g]) % 2**32
               _a      =  d
               _a      =  c
               _a      =  b
               _a      =  sum_aa(__A  ,	left_rotate_aa(__A  ,	shift_amounts[i]))

          # Add hashed chunk to running total
          _a      =  sum_aa(__A  ,	__A)
          _a      =  sum_aa(__A  ,	__A)
          _a      =  sum_aa(__A  ,	__A)
          _a      =  sum_aa(__A  ,	__A)

     _a      =  reformat_hex(__A) + reformat_hex(__A) + reformat_hex(__A) + reformat_hex(__A)
     return digest


if __name__ == "__main__":
  import doctest

  doctest.testmod()



