





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

     def __init__(   self,							A,							):





            """simple docstring"""

            _UpperCamelCase														=    parent
            _UpperCamelCase														=    13
            _UpperCamelCase														=    7
            _UpperCamelCase														=    True
            _UpperCamelCase														=    True
            _UpperCamelCase														=    True
            _UpperCamelCase														=    True
            _UpperCamelCase														=    True
            _UpperCamelCase														=    99
            _UpperCamelCase														=    32
            _UpperCamelCase														=    2
            _UpperCamelCase														=    4
            _UpperCamelCase														=    37
            _UpperCamelCase														=    '''gelu'''
            _UpperCamelCase														=    0.1
            _UpperCamelCase														=    0.1
            _UpperCamelCase														=    512
            _UpperCamelCase														=    16
            _UpperCamelCase														=    2
            _UpperCamelCase														=    0.02
            _UpperCamelCase														=    3
            _UpperCamelCase														=    4
            _UpperCamelCase														=    None
            _UpperCamelCase														=    self.vocab_size - 1

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _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
            if self.use_mc_token_ids:
                   _UpperCamelCase														=    ids_tensor([self.batch_size, self.num_choices],							self.seq_length   )

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

            _UpperCamelCase														=    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,							A,							A,							A,							A,							A,							*A   ):





            """simple docstring"""

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

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

            _UpperCamelCase														=    model(A_   )

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

     def 						_UpperCamelCase  (   self,							A,							A,							A,							A,							A,							*A   ):





            """simple docstring"""

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

     def 						_UpperCamelCase  (   self,							A,							A,							A,							A,							A,							A,							*A   ):





            """simple docstring"""

            _UpperCamelCase														=    TFOpenAIGPTDoubleHeadsModel(config=A_   )

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

            _UpperCamelCase														=    {
                '''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,
            }
            _UpperCamelCase														=    model(A_   )
            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,							A,							A,							A,							A,							A,							*A   ):





            """simple docstring"""

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



     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    self.prepare_config_and_inputs()

            (
                (
                _UpperCamelCase 
            )							,		(
                _UpperCamelCase 
            )							,		(
                _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 			__A(    A_    ,      A_    ,      unittest.TestCase			):
     __A							=							(
         (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
         if is_tf_available()
         else ()
     )
     __A							=							(
         (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     __A							=							(
         {
             "feature-extraction": TFOpenAIGPTModel,
             "text-classification": TFOpenAIGPTForSequenceClassification,
             "text-generation": TFOpenAIGPTLMHeadModel,
             "zero-shot": TFOpenAIGPTForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     __A							=							False
     __A							=							False

     def 						_UpperCamelCase  (   self,							A,							A,							A,							A,							A   ):





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

            _UpperCamelCase														=    TFOpenAIGPTModelTester(self   )
            _UpperCamelCase														=    ConfigTester(self,							config_class=A_,							n_embd=37   )

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            self.config_tester.run_common_tests()

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

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

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

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

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

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

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

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



     @slow
     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

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






@require_tf
class 			__A(    unittest.TestCase			):

     @slow
     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''   )
            _UpperCamelCase														=    tf.convert_to_tensor([[481, 4735, 544]],							dtype=tf.intaa   )  # the president is
            _UpperCamelCase														=    [
                481,
                4735,
                544,
                246,
                963,
                870,
                762,
                239,
                244,
                4_0477,
                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

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










def 		_UpperCAmelCase							(     SCREAMING_SNAKE_CASE__     :   list     ,	SCREAMING_SNAKE_CASE__     :   int     ,	SCREAMING_SNAKE_CASE__     :   int     ,	SCREAMING_SNAKE_CASE__     :   int							):
     __UpperCamelCase       				=int((lower_bound + upper_bound) // 2							)
     if array[r] == value:
          return r
     if lower_bound >= upper_bound:
          return -1
     if array[r] < value:
          return binary_search(SCREAMING_SNAKE_CASE__     ,	r + 1     ,	SCREAMING_SNAKE_CASE__     ,	SCREAMING_SNAKE_CASE__							)
     else:
          return binary_search(SCREAMING_SNAKE_CASE__     ,	SCREAMING_SNAKE_CASE__     ,	r - 1     ,	SCREAMING_SNAKE_CASE__							)

def 		_UpperCAmelCase							(     SCREAMING_SNAKE_CASE__     :   int     ,	SCREAMING_SNAKE_CASE__     :   list							):
     __UpperCamelCase       				=0
     if matrix[index][0] == value:
          return [index, 0]
     while index < len(SCREAMING_SNAKE_CASE__							) and matrix[index][0] < value:
          __UpperCamelCase       				=binary_search(matrix[index]     ,	0     ,	len(matrix[index]							) - 1     ,	SCREAMING_SNAKE_CASE__							)
          if r != -1:
               return [index, r]
          index += 1
     return [-1, -1]


if __name__ == "__main__":
   import doctest

   doctest.testmod()





