


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

 '''simple docstring'''

 def __init__(      self						, _UpperCAmelCase						, ):
       snake_case_			= parent
       snake_case_			= 13
       snake_case_			= 7
       snake_case_			= True
       snake_case_			= True
       snake_case_			= True
       snake_case_			= True
       snake_case_			= True
       snake_case_			= 99
       snake_case_			= 32
       snake_case_			= 2
       snake_case_			= 4
       snake_case_			= 37
       snake_case_			= '''gelu'''
       snake_case_			= 0.1
       snake_case_			= 0.1
       snake_case_			= 5_12
       snake_case_			= 16
       snake_case_			= 2
       snake_case_			= 0.02
       snake_case_			= 3
       snake_case_			= 4
       snake_case_			= None
       snake_case_			= self.vocab_size - 1



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= ids_tensor([self.batch_size, self.seq_length]						, self.vocab_size   )

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

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

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

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

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

       snake_case_			= 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						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, *_UpperCAmelCase   ):
       snake_case_			= TFOpenAIGPTModel(config=_SCREAMING_SNAKE_CASE   )
       snake_case_			= {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
       snake_case_			= model(_SCREAMING_SNAKE_CASE   )

       snake_case_			= [input_ids, input_mask]
       snake_case_			= model(_SCREAMING_SNAKE_CASE   )

       snake_case_			= model(_SCREAMING_SNAKE_CASE   )

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



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, *_UpperCAmelCase   ):
       snake_case_			= TFOpenAIGPTLMHeadModel(config=_SCREAMING_SNAKE_CASE   )
       snake_case_			= {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
       snake_case_			= model(_SCREAMING_SNAKE_CASE   )
       self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.seq_length, self.vocab_size)   )



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, *_UpperCAmelCase   ):
       snake_case_			= TFOpenAIGPTDoubleHeadsModel(config=_SCREAMING_SNAKE_CASE   )

       snake_case_			= tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE						, 1   )						, (1, self.num_choices, 1)   )
       snake_case_			= tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE						, 1   )						, (1, self.num_choices, 1)   )
       snake_case_			= tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE						, 1   )						, (1, self.num_choices, 1)   )

       snake_case_			= {
           '''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,
       }
       snake_case_			= model(_SCREAMING_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						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, *_UpperCAmelCase   ):
       snake_case_			= self.num_labels
       snake_case_			= ids_tensor([self.batch_size]						, self.type_sequence_label_size   )
       snake_case_			= {
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
           '''labels''': sequence_labels,
       }
       snake_case_			= TFOpenAIGPTForSequenceClassification(_SCREAMING_SNAKE_CASE   )
       snake_case_			= model(_SCREAMING_SNAKE_CASE   )
       self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.num_labels)   )





 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.prepare_config_and_inputs()

       (
           (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       
       )			= config_and_inputs

       snake_case_			= {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
       return config, inputs_dict







@require_tf
class lowerCAmelCase_		(					_UpperCamelCase   ,		_UpperCamelCase   ,		unittest.TestCase			):

 '''simple docstring'''

 __snake_case				=    (
     (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
     if is_tf_available()
     else ()
 )
 __snake_case				=    (
     (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 __snake_case				=    (
     {
         'feature-extraction': TFOpenAIGPTModel,
         'text-classification': TFOpenAIGPTForSequenceClassification,
         'text-generation': TFOpenAIGPTLMHeadModel,
         'zero-shot': TFOpenAIGPTForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 __snake_case				=    False
 __snake_case				=    False



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   ):
       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   ):
       snake_case_			= TFOpenAIGPTModelTester(self   )
       snake_case_			= ConfigTester(self						, config_class=_SCREAMING_SNAKE_CASE						, n_embd=37   )



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



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_model(*_SCREAMING_SNAKE_CASE   )



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_lm_head(*_SCREAMING_SNAKE_CASE   )



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_double_head(*_SCREAMING_SNAKE_CASE   )



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_SCREAMING_SNAKE_CASE   )





 @slow
 def 		UpperCamelCase__	(      self   ):
       for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             snake_case_			= TFOpenAIGPTModel.from_pretrained(_SCREAMING_SNAKE_CASE   )
             self.assertIsNotNone(_SCREAMING_SNAKE_CASE   )







@require_tf
class lowerCAmelCase_		(					unittest.TestCase			):

 '''simple docstring'''

 @slow
 def 		UpperCamelCase__	(      self   ):
       snake_case_			= TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''   )
       snake_case_			= tf.convert_to_tensor([[4_81, 47_35, 5_44]]						, dtype=tf.intaa   )  # the president is
       snake_case_			= [
           4_81,
           47_35,
           5_44,
           2_46,
           9_63,
           8_70,
           7_62,
           2_39,
           2_44,
           4_04_77,
           2_44,
           2_49,
           7_19,
           8_81,
           4_87,
           5_44,
           2_40,
           2_44,
           6_03,
           4_81,
       ]  # the president is a very good man. " \n " i\'m sure he is, " said the

       snake_case_			= model.generate(_SCREAMING_SNAKE_CASE						, do_sample=_SCREAMING_SNAKE_CASE   )
       self.assertListEqual(output_ids[0].numpy().tolist()						, _SCREAMING_SNAKE_CASE   )
'''simple docstring'''




def 	_UpperCAmelCase       (				_UpperCamelCase       :      list[list[int]],  _UpperCamelCase       :      list[list[int]]  )      -> list[list[int]]:
    A_      =  []
    A_      =  len(_UpperCamelCase  )
    for i in range(_UpperCamelCase  ):
        A_      =  []
        for j in range(_UpperCamelCase  ):
            A_      =  0
            for k in range(_UpperCamelCase  ):
                A_      =  val + matrix_a[i][k] * matrix_b[k][j]
            list_a.append(_UpperCamelCase  )
        matrix_c.append(_UpperCamelCase  )
    return matrix_c





def 	_UpperCAmelCase       (				_UpperCamelCase       :      int  )      -> list[list[int]]:
    return [[int(row == column  ) for column in range(_UpperCamelCase  )] for row in range(_UpperCamelCase  )]





def 	_UpperCAmelCase       (				_UpperCamelCase       :      int  )      -> int:
    if n <= 1:
        return n
    A_      =  identity(2  )
    A_      =  [[1, 1], [1, 0]]
    A_      =  n - 1
    while n > 0:
        if n % 2 == 1:
            A_      =  multiply(_UpperCamelCase,  _UpperCamelCase  )
        A_      =  multiply(_UpperCamelCase,  _UpperCamelCase  )
        A_      =  int(n / 2  )
    return res_matrix[0][0]





def 	_UpperCAmelCase       (				_UpperCamelCase       :      int  )      -> int:
    if n <= 1:
        return n
    A_      =  0
    A_      =  1
    for _ in range(2,  n + 1  ):
        A_  ,A_      =  fiba, fiba + fiba
    return fiba





def 	_UpperCAmelCase       (				)      -> None:
    for ordinal in "0th 1st 2nd 3rd 10th 100th 1000th".split():
        A_      =  int(''''''.join(c for c in ordinal if c in '''0123456789'''  )  )  # 1000th --> 1000
        print(
            F'''{ordinal} fibonacci number using matrix exponentiation is '''
            F'''{nth_fibonacci_matrix(_UpperCamelCase  )} and using bruteforce is '''
            F'''{nth_fibonacci_bruteforce(_UpperCamelCase  )}\n'''  )
    # from timeit import timeit
    # print(timeit("nth_fibonacci_matrix(1000000)",
    #              "from main import nth_fibonacci_matrix", number=5))
    # print(timeit("nth_fibonacci_bruteforce(1000000)",
    #              "from main import nth_fibonacci_bruteforce", number=5))
    # 2.3342058970001744
    # 57.256506615000035


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  main()






