





from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, 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.models.openai.modeling_tf_openai import (
        TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFOpenAIGPTDoubleHeadsModel,
        TFOpenAIGPTForSequenceClassification,
        TFOpenAIGPTLMHeadModel,
        TFOpenAIGPTModel,
    )





class UpperCAmelCase_							:


  def __init__(       self   ,__snake_case   ,):



       """simple docstring"""





       A_					      =					parent
       A_					      =					1_3
       A_					      =					7
       A_					      =					True
       A_					      =					True
       A_					      =					True
       A_					      =					True
       A_					      =					True
       A_					      =					9_9
       A_					      =					3_2
       A_					      =					2
       A_					      =					4
       A_					      =					3_7
       A_					      =					'''gelu'''
       A_					      =					0.1
       A_					      =					0.1
       A_					      =					5_1_2
       A_					      =					1_6
       A_					      =					2
       A_					      =					0.02
       A_					      =					3
       A_					      =					4
       A_					      =					None
       A_					      =					self.vocab_size - 1


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





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

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

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

       A_					      =					None
       if self.use_mc_token_ids:
            A_					      =					ids_tensor([self.batch_size, self.num_choices]   ,self.seq_length   )

       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,
           input_mask,
           head_mask,
           token_type_ids,
           mc_token_ids,
           sequence_labels,
           token_labels,
           choice_labels,
       )


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ,*__snake_case   ):



       """simple docstring"""





       A_					      =					TFOpenAIGPTModel(config=__snake_case   )
       A_					      =					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
       A_					      =					model(__snake_case   )

       A_					      =					[input_ids, input_mask]
       A_					      =					model(__snake_case   )

       A_					      =					model(__snake_case   )

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


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ,*__snake_case   ):



       """simple docstring"""





       A_					      =					TFOpenAIGPTLMHeadModel(config=__snake_case   )
       A_					      =					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
       A_					      =					model(__snake_case   )
       self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.seq_length, self.vocab_size)   )


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ,*__snake_case   ):



       """simple docstring"""





       A_					      =					TFOpenAIGPTDoubleHeadsModel(config=__snake_case   )

       A_					      =					tf.tile(tf.expand_dims(__snake_case   ,1   )   ,(1, self.num_choices, 1)   )
       A_					      =					tf.tile(tf.expand_dims(__snake_case   ,1   )   ,(1, self.num_choices, 1)   )
       A_					      =					tf.tile(tf.expand_dims(__snake_case   ,1   )   ,(1, self.num_choices, 1)   )

       A_					      =					{
           '''input_ids''': multiple_choice_inputs_ids,
           '''mc_token_ids''': mc_token_ids,
           '''attention_mask''': multiple_choice_input_mask,
           '''token_type_ids''': multiple_choice_token_type_ids,
       }
       A_					      =					model(__snake_case   )
       self.parent.assertEqual(
           result.logits.shape   ,(self.batch_size, self.num_choices, self.seq_length, self.vocab_size)   )
       self.parent.assertEqual(result.mc_logits.shape   ,(self.batch_size, self.num_choices)   )


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ,*__snake_case   ):



       """simple docstring"""





       A_					      =					self.num_labels
       A_					      =					ids_tensor([self.batch_size]   ,self.type_sequence_label_size   )
       A_					      =					{
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
           '''labels''': sequence_labels,
       }
       A_					      =					TFOpenAIGPTForSequenceClassification(__snake_case   )
       A_					      =					model(__snake_case   )
       self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.num_labels)   )




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.prepare_config_and_inputs()

       (
           (
           A_						
       )      ,						(
           A_						
       )      ,						(
           A_						
       )      ,						(
           A_						
       )      ,						(
           A_						
       )      ,						(
           A_						
       )      ,						(
           A_						
       )      ,						(
           A_						
       )      ,						(
           A_						
       )      ,												
       )					      =					config_and_inputs

       A_					      =					{'''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   ):
  __lowerCAmelCase      :	int  	=      (
      (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
      if is_tf_available()
      else ()
  )
  __lowerCAmelCase      :	Tuple  	=      (
      (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
  )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
  __lowerCAmelCase      :	Optional[int]  	=      (
      {
          """feature-extraction""": TFOpenAIGPTModel,
          """text-classification""": TFOpenAIGPTForSequenceClassification,
          """text-generation""": TFOpenAIGPTLMHeadModel,
          """zero-shot""": TFOpenAIGPTForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  __lowerCAmelCase      :	Any  	=      False
  __lowerCAmelCase      :	Any  	=      False


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ):



       """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 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					TFOpenAIGPTModelTester(self   )
       A_					      =					ConfigTester(self   ,config_class=__snake_case   ,n_embd=3_7   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       self.config_tester.run_common_tests()


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_model(*__snake_case   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_lm_head(*__snake_case   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_double_head(*__snake_case   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__snake_case   )




  @slow
  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A_					      =					TFOpenAIGPTModel.from_pretrained(__snake_case   )
            self.assertIsNotNone(__snake_case   )





@require_tf
class UpperCAmelCase_							(   unittest.TestCase   ):


  @slow
  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''   )
       A_					      =					tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]   ,dtype=tf.intaa   )  # the president is
       A_					      =					[
           4_8_1,
           4_7_3_5,
           5_4_4,
           2_4_6,
           9_6_3,
           8_7_0,
           7_6_2,
           2_3_9,
           2_4_4,
           4_0_4_7_7,
           2_4_4,
           2_4_9,
           7_1_9,
           8_8_1,
           4_8_7,
           5_4_4,
           2_4_0,
           2_4_4,
           6_0_3,
           4_8_1,
       ]  # the president is a very good man. " \n " i\'m sure he is, " said the

       A_					      =					model.generate(__snake_case   ,do_sample=__snake_case   )
       self.assertListEqual(output_ids[0].numpy().tolist()   ,__snake_case   )










import itertools
from pathlib import Path

def     UpperCAmelCase_						(						_UpperCAmelCase       :list[str]							)  ->							int:
     '''simple docstring'''


     A_					      =					[tuple(_UpperCAmelCase							) for login in logins]

     A_					      =					{char for login in split_logins for char in login}

     for permutation in itertools.permutations(_UpperCAmelCase							):
          A_					      =					True
          for login in logins:
               if not (
                   permutation.index(login[0]							)
                   < permutation.index(login[1]							)
                   < permutation.index(login[2]							)
               ):
                    A_					      =					False
                    break

          if satisfied:
               return int(''''''.join(_UpperCAmelCase							)							)

     raise Exception('''Unable to find the secret passcode'''							)

def     UpperCAmelCase_						(						_UpperCAmelCase       :str = "keylog.txt"							)  ->							int:
     '''simple docstring'''


     A_					      =					Path(_UpperCAmelCase							).parent.joinpath(_UpperCAmelCase							).read_text().splitlines()

     return find_secret_passcode(_UpperCAmelCase							)


if __name__ == "__main__":
      print(f'''{solution() = }''')



