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				:						Tuple				,	lowerCamelCase				:						List[Any]				,	)       ->							Optional[int]:
        _UpperCamelCase   :				Optional[int]       =     parent
        _UpperCamelCase   :				List[Any]       =     1_3
        _UpperCamelCase   :				int       =     7
        _UpperCamelCase   :				List[str]       =     True
        _UpperCamelCase   :				Dict       =     True
        _UpperCamelCase   :				str       =     True
        _UpperCamelCase   :				List[str]       =     True
        _UpperCamelCase   :				List[str]       =     True
        _UpperCamelCase   :				List[str]       =     9_9
        _UpperCamelCase   :				Tuple       =     3_2
        _UpperCamelCase   :				List[str]       =     2
        _UpperCamelCase   :				Union[str, Any]       =     4
        _UpperCamelCase   :				Dict       =     3_7
        _UpperCamelCase   :				Union[str, Any]       =     'gelu'
        _UpperCamelCase   :				List[str]       =     0.1
        _UpperCamelCase   :				List[Any]       =     0.1
        _UpperCamelCase   :				Dict       =     5_1_2
        _UpperCamelCase   :				List[str]       =     1_6
        _UpperCamelCase   :				Any       =     2
        _UpperCamelCase   :				str       =     0.0_2
        _UpperCamelCase   :				List[str]       =     3
        _UpperCamelCase   :				Optional[int]       =     4
        _UpperCamelCase   :				Optional[int]       =     None
        _UpperCamelCase   :				List[str]       =     self.vocab_size - 1

 def     __snake_case    (	self				:						Tuple     )       ->							List[str]:
        _UpperCamelCase   :				Union[str, Any]       =     ids_tensor([self.batch_size, self.seq_length]				,	self.vocab_size     )

        _UpperCamelCase   :				Dict       =     None
        if self.use_input_mask:
               _UpperCamelCase   :				Optional[Any]       =     random_attention_mask([self.batch_size, self.seq_length]     )

        _UpperCamelCase   :				Optional[int]       =     None
        if self.use_token_type_ids:
               _UpperCamelCase   :				Any       =     ids_tensor([self.batch_size, self.seq_length]				,	self.type_vocab_size     )

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

        _UpperCamelCase   :				List[Any]       =     None
        _UpperCamelCase   :				Optional[int]       =     None
        _UpperCamelCase   :				Optional[int]       =     None
        if self.use_labels:
               _UpperCamelCase   :				Union[str, Any]       =     ids_tensor([self.batch_size]				,	self.type_sequence_label_size     )
               _UpperCamelCase   :				Dict       =     ids_tensor([self.batch_size, self.seq_length]				,	self.num_labels     )
               _UpperCamelCase   :				Union[str, Any]       =     ids_tensor([self.batch_size]				,	self.num_choices     )

        _UpperCamelCase   :				Dict       =     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   :				Optional[int]       =     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     __snake_case    (	self				:						Any				,	lowerCamelCase				:						Dict				,	lowerCamelCase				:						List[str]				,	lowerCamelCase				:						Dict				,	lowerCamelCase				:						Optional[int]				,	lowerCamelCase				:						Dict				,	*lowerCamelCase				:						Any     )       ->							Union[str, Any]:
        _UpperCamelCase   :				Any       =     TFOpenAIGPTModel(config=lowerCamelCase     )
        _UpperCamelCase   :				Optional[Any]       =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        _UpperCamelCase   :				Union[str, Any]       =     model(lowerCamelCase     )

        _UpperCamelCase   :				Dict       =     [input_ids, input_mask]
        _UpperCamelCase   :				List[str]       =     model(lowerCamelCase     )

        _UpperCamelCase   :				List[Any]       =     model(lowerCamelCase     )

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

 def     __snake_case    (	self				:						Any				,	lowerCamelCase				:						int				,	lowerCamelCase				:						int				,	lowerCamelCase				:						Union[str, Any]				,	lowerCamelCase				:						List[Any]				,	lowerCamelCase				:						str				,	*lowerCamelCase				:						int     )       ->							Union[str, Any]:
        _UpperCamelCase   :				Optional[int]       =     TFOpenAIGPTLMHeadModel(config=lowerCamelCase     )
        _UpperCamelCase   :				List[str]       =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        _UpperCamelCase   :				List[str]       =     model(lowerCamelCase     )
        self.parent.assertEqual(result.logits.shape				,	(self.batch_size, self.seq_length, self.vocab_size)     )

 def     __snake_case    (	self				:						Dict				,	lowerCamelCase				:						Optional[int]				,	lowerCamelCase				:						Optional[int]				,	lowerCamelCase				:						str				,	lowerCamelCase				:						Union[str, Any]				,	lowerCamelCase				:						Tuple				,	lowerCamelCase				:						str				,	*lowerCamelCase				:						Any     )       ->							Tuple:
        _UpperCamelCase   :				List[str]       =     TFOpenAIGPTDoubleHeadsModel(config=lowerCamelCase     )

        _UpperCamelCase   :				Any       =     tf.tile(tf.expand_dims(lowerCamelCase				,	1     )				,	(1, self.num_choices, 1)     )
        _UpperCamelCase   :				int       =     tf.tile(tf.expand_dims(lowerCamelCase				,	1     )				,	(1, self.num_choices, 1)     )
        _UpperCamelCase   :				str       =     tf.tile(tf.expand_dims(lowerCamelCase				,	1     )				,	(1, self.num_choices, 1)     )

        _UpperCamelCase   :				Optional[Any]       =     {
            '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   :				Union[str, Any]       =     model(lowerCamelCase     )
        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     __snake_case    (	self				:						Tuple				,	lowerCamelCase				:						int				,	lowerCamelCase				:						List[Any]				,	lowerCamelCase				:						List[str]				,	lowerCamelCase				:						str				,	lowerCamelCase				:						Any				,	*lowerCamelCase				:						str     )       ->							List[str]:
        _UpperCamelCase   :				List[Any]       =     self.num_labels
        _UpperCamelCase   :				Tuple       =     ids_tensor([self.batch_size]				,	self.type_sequence_label_size     )
        _UpperCamelCase   :				Dict       =     {
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
            'labels': sequence_labels,
        }
        _UpperCamelCase   :				Tuple       =     TFOpenAIGPTForSequenceClassification(lowerCamelCase     )
        _UpperCamelCase   :				List[str]       =     model(lowerCamelCase     )
        self.parent.assertEqual(result.logits.shape				,	(self.batch_size, self.num_labels)     )

 def     __snake_case    (	self				:						Dict     )       ->							List[str]:
        _UpperCamelCase   :				str       =     self.prepare_config_and_inputs()

        (
            (
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					
        )   :				Union[str, Any]       =     config_and_inputs

        _UpperCamelCase   :				Optional[Any]       =     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
        return config, inputs_dict







@require_tf
class       __lowerCAmelCase     (				a_,		a_,		unittest.TestCase  ):



 '''simple docstring'''
 _lowerCamelCase  :		Tuple 			=       (
     (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
     if is_tf_available()
     else ()
 )
 _lowerCamelCase  :		Dict 			=       (
     (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 _lowerCamelCase  :		Union[str, Any] 			=       (
     {
         'feature-extraction': TFOpenAIGPTModel,
         'text-classification': TFOpenAIGPTForSequenceClassification,
         'text-generation': TFOpenAIGPTLMHeadModel,
         'zero-shot': TFOpenAIGPTForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 _lowerCamelCase  :		List[Any] 			=       False
 _lowerCamelCase  :		Any 			=       False

 def     __snake_case    (	self				:						Tuple				,	lowerCamelCase				:						Any				,	lowerCamelCase				:						Union[str, Any]				,	lowerCamelCase				:						List[str]				,	lowerCamelCase				:						Any				,	lowerCamelCase				:						List[Any]     )       ->							Tuple:
        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     __snake_case    (	self				:						Optional[int]     )       ->							Optional[Any]:
        _UpperCamelCase   :				int       =     TFOpenAIGPTModelTester(self     )
        _UpperCamelCase   :				str       =     ConfigTester(self				,	config_class=lowerCamelCase				,	n_embd=3_7     )

 def     __snake_case    (	self				:						str     )       ->							Tuple:
        self.config_tester.run_common_tests()

 def     __snake_case    (	self				:						Union[str, Any]     )       ->							Union[str, Any]:
        _UpperCamelCase   :				Dict       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*lowerCamelCase     )

 def     __snake_case    (	self				:						Union[str, Any]     )       ->							Tuple:
        _UpperCamelCase   :				Union[str, Any]       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_lm_head(*lowerCamelCase     )

 def     __snake_case    (	self				:						List[str]     )       ->							List[Any]:
        _UpperCamelCase   :				Dict       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_double_head(*lowerCamelCase     )

 def     __snake_case    (	self				:						Optional[int]     )       ->							Dict:
        _UpperCamelCase   :				Union[str, Any]       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCamelCase     )

 @slow
 def     __snake_case    (	self				:						Dict     )       ->							Any:
        for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _UpperCamelCase   :				str       =     TFOpenAIGPTModel.from_pretrained(lowerCamelCase     )
               self.assertIsNotNone(lowerCamelCase     )







@require_tf
class       __lowerCAmelCase     (				unittest.TestCase  ):



 '''simple docstring'''

 @slow
 def     __snake_case    (	self				:						str     )       ->							Any:
        _UpperCamelCase   :				Dict       =     TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'     )
        _UpperCamelCase   :				List[str]       =     tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]				,	dtype=tf.intaa     )  # the president is
        _UpperCamelCase   :				Dict       =     [
            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

        _UpperCamelCase   :				Union[str, Any]       =     model.generate(lowerCamelCase				,	do_sample=lowerCamelCase     )
        self.assertListEqual(output_ids[0].numpy().tolist()				,	lowerCamelCase     )




from __future__ import annotations


def SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int						,  lowercase_:		int ):
       assert n > 1 and a > 0 and greatest_common_divisor(lowercase_						,  lowercase_ ) == 1
       ((_UpperCamelCase)  ,					(_UpperCamelCase)  ,					(_UpperCamelCase))   :				Optional[int]       =     extended_gcd(lowercase_						,  lowercase_ )  # Implemented below
       _UpperCamelCase   :				str       =     (b * s) % n
       return x


def SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int ):
       ((_UpperCamelCase)  ,					(_UpperCamelCase))   :				Union[str, Any]       =     extended_euclid(lowercase_						,  lowercase_ )  # Implemented below
       if b < 0:
              _UpperCamelCase   :				Union[str, Any]       =     (b % n + n) % n
       return b


def SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int						,  lowercase_:		int ):
       _UpperCamelCase   :				Union[str, Any]       =     invert_modulo(lowercase_						,  lowercase_ )
       _UpperCamelCase   :				str       =     (b * s) % n
       return x


def SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int ):
       assert a >= 0 and b >= 0

       if b == 0:
              _UpperCamelCase  ,					_UpperCamelCase  ,					_UpperCamelCase   :				Any       =     a, 1, 0
       else:
              ((_UpperCamelCase)  ,					(_UpperCamelCase)  ,					(_UpperCamelCase))   :				Dict       =     extended_gcd(lowercase_						,  a % b )
              _UpperCamelCase   :				List[str]       =     q
              _UpperCamelCase   :				Any       =     p - q * (a // b)

       assert a % d == 0 and b % d == 0
       assert d == a * x + b * y

       return (d, x, y)


def SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int ):
       if b == 0:
              return (1, 0)
       ((_UpperCamelCase)  ,					(_UpperCamelCase))   :				Optional[Any]       =     extended_euclid(lowercase_						,  a % b )
       _UpperCamelCase   :				List[Any]       =     a // b
       return (y, x - k * y)


def SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int ):
       if a < b:
              _UpperCamelCase  ,					_UpperCamelCase   :				Optional[int]       =     b, a

       while a % b != 0:
              _UpperCamelCase  ,					_UpperCamelCase   :				Union[str, Any]       =     b, a % b

       return b


if __name__ == "__main__":
 from doctest import testmod

 testmod(name='''modular_division''', verbose=True)
 testmod(name='''modular_division2''', verbose=True)
 testmod(name='''invert_modulo''', verbose=True)
 testmod(name='''extended_gcd''', verbose=True)
 testmod(name='''extended_euclid''', verbose=True)
 testmod(name='''greatest_common_divisor''', verbose=True)



