






'''simple docstring'''


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




 def __init__(		self     ,       _lowerCAmelCase     ,       )      ->							int:
     _lowerCAmelCase		     = parent
     _lowerCAmelCase		     = 13
     _lowerCAmelCase		     = 7
     _lowerCAmelCase		     = True
     _lowerCAmelCase		     = True
     _lowerCAmelCase		     = True
     _lowerCAmelCase		     = True
     _lowerCAmelCase		     = True
     _lowerCAmelCase		     = 99
     _lowerCAmelCase		     = 32
     _lowerCAmelCase		     = 2
     _lowerCAmelCase		     = 4
     _lowerCAmelCase		     = 37
     _lowerCAmelCase		     = "gelu"
     _lowerCAmelCase		     = 0.1
     _lowerCAmelCase		     = 0.1
     _lowerCAmelCase		     = 512
     _lowerCAmelCase		     = 16
     _lowerCAmelCase		     = 2
     _lowerCAmelCase		     = 0.02
     _lowerCAmelCase		     = 3
     _lowerCAmelCase		     = 4
     _lowerCAmelCase		     = None
     _lowerCAmelCase		     = self.vocab_size - 1




 def 							_snake_case				(		self						)      ->							int:
     _lowerCAmelCase		     = ids_tensor([self.batch_size, self.seq_length]     ,       self.vocab_size						)

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

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

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

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

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

     _lowerCAmelCase		     = 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     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       *_lowerCAmelCase						)      ->							str:
     _lowerCAmelCase		     = TFOpenAIGPTModel(config=_lowerCAmelCase						)
     _lowerCAmelCase		     = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
     _lowerCAmelCase		     = model(_lowerCAmelCase						)

     _lowerCAmelCase		     = [input_ids, input_mask]
     _lowerCAmelCase		     = model(_lowerCAmelCase						)

     _lowerCAmelCase		     = model(_lowerCAmelCase						)

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




 def 							_snake_case				(		self     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       *_lowerCAmelCase						)      ->							Dict:
     _lowerCAmelCase		     = TFOpenAIGPTLMHeadModel(config=_lowerCAmelCase						)
     _lowerCAmelCase		     = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
     _lowerCAmelCase		     = model(_lowerCAmelCase						)
     self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.seq_length, self.vocab_size)						)




 def 							_snake_case				(		self     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       *_lowerCAmelCase						)      ->							Tuple:
     _lowerCAmelCase		     = TFOpenAIGPTDoubleHeadsModel(config=_lowerCAmelCase						)

     _lowerCAmelCase		     = tf.tile(tf.expand_dims(_lowerCAmelCase     ,       1						)     ,       (1, self.num_choices, 1)						)
     _lowerCAmelCase		     = tf.tile(tf.expand_dims(_lowerCAmelCase     ,       1						)     ,       (1, self.num_choices, 1)						)
     _lowerCAmelCase		     = tf.tile(tf.expand_dims(_lowerCAmelCase     ,       1						)     ,       (1, self.num_choices, 1)						)

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





 def 							_snake_case				(		self						)      ->							Optional[Any]:
     _lowerCAmelCase		     = self.prepare_config_and_inputs()

     (
         (
         _lowerCAmelCase
     )     ,     (
         _lowerCAmelCase
     )     ,     (
         _lowerCAmelCase
     )     ,     (
         _lowerCAmelCase
     )     ,     (
         _lowerCAmelCase
     )     ,     (
         _lowerCAmelCase
     )     ,     (
         _lowerCAmelCase
     )     ,     (
         _lowerCAmelCase
     )     ,     (
         _lowerCAmelCase
     )     ,     
     )		     = config_and_inputs

     _lowerCAmelCase		     = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict







@require_tf
class 				lowerCAmelCase_						(						lowercase_  ,lowercase_  ,unittest.TestCase						):
 __lowerCamelCase  :							Any    					=   (
     (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
     if is_tf_available()
     else ()
 )
 __lowerCamelCase  :							str    					=   (
     (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 __lowerCamelCase  :							Dict    					=   (
     {
         """feature-extraction""": TFOpenAIGPTModel,
         """text-classification""": TFOpenAIGPTForSequenceClassification,
         """text-generation""": TFOpenAIGPTLMHeadModel,
         """zero-shot""": TFOpenAIGPTForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 __lowerCamelCase  :							str    					=   False
 __lowerCamelCase  :							Dict    					=   False




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







     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						)      ->							Union[str, Any]:
     _lowerCAmelCase		     = TFOpenAIGPTModelTester(self						)
     _lowerCAmelCase		     = ConfigTester(self     ,       config_class=_lowerCAmelCase     ,       n_embd=37						)




 def 							_snake_case				(		self						)      ->							Tuple:
     self.config_tester.run_common_tests()




 def 							_snake_case				(		self						)      ->							List[Any]:
     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_model(*_lowerCAmelCase						)




 def 							_snake_case				(		self						)      ->							List[Any]:
     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_lm_head(*_lowerCAmelCase						)




 def 							_snake_case				(		self						)      ->							Union[str, Any]:
     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_double_head(*_lowerCAmelCase						)




 def 							_snake_case				(		self						)      ->							Optional[int]:
     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCAmelCase						)





 @slow
 def 							_snake_case				(		self						)      ->							Optional[Any]:







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







@require_tf
class 				lowerCAmelCase_						(						unittest.TestCase						):




 @slow
 def 							_snake_case				(		self						)      ->							List[Any]:
     _lowerCAmelCase		     = TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"						)
     _lowerCAmelCase		     = tf.convert_to_tensor([[481, 4735, 544]]     ,       dtype=tf.intaa						)  # the president is
     _lowerCAmelCase		     = [
         481,
         4735,
         544,
         246,
         963,
         870,
         762,
         239,
         244,
         40477,
         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

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









'''simple docstring'''


def 		__a(SCREAMING_SNAKE_CASE_					:   List[Any]						,    SCREAMING_SNAKE_CASE_					:   str      ):



    '''simple docstring'''





    if n < 0:
        return 0
    # table[i] represents the number of ways to get to amount i
    _lowerCAmelCase		     = [0] * (n + 1)

    # There is exactly 1 way to get to zero(You pick no coins).
    _lowerCAmelCase		     = 1

    # Pick all coins one by one and update table[] values
    # after the index greater than or equal to the value of the
    # picked coin
    for coin_val in s:
        for j in range(SCREAMING_SNAKE_CASE_						,    n + 1      ):
            table[j] += table[j - coin_val]

    return table[n]


if __name__ == "__main__":
       import doctest

       doctest.testmod()


