

"""simple docstring"""





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
       OpenAIGPTConfig,
       OpenAIGPTDoubleHeadsModel,
       OpenAIGPTForSequenceClassification,
       OpenAIGPTLMHeadModel,
       OpenAIGPTModel,
   )




class  _lowerCAmelCase  :







 """simple docstring"""







 def __init__(      self							:      Union[str, Any], UpperCAmelCase__							:      Tuple, UpperCAmelCase__							:      Union[str, Any]=1_3, UpperCAmelCase__							:      Any=7, UpperCAmelCase__							:      Any=True, UpperCAmelCase__							:      int=True, UpperCAmelCase__							:      Union[str, Any]=True, UpperCAmelCase__							:      Any=9_9, UpperCAmelCase__							:      Optional[Any]=3_2, UpperCAmelCase__							:      Tuple=5, UpperCAmelCase__							:      Tuple=4, UpperCAmelCase__							:      List[str]=3_7, UpperCAmelCase__							:      Optional[Any]="gelu", UpperCAmelCase__							:      List[Any]=0.1, UpperCAmelCase__							:      Union[str, Any]=0.1, UpperCAmelCase__							:      Dict=5_1_2, UpperCAmelCase__							:      List[str]=1_6, UpperCAmelCase__							:      Dict=2, UpperCAmelCase__							:      Optional[int]=0.02, UpperCAmelCase__							:      Union[str, Any]=3, UpperCAmelCase__							:      Optional[int]=4, UpperCAmelCase__							:      List[Any]=None, ):
    __lowercase								=						parent
    __lowercase								=						batch_size
    __lowercase								=						seq_length
    __lowercase								=						is_training
    __lowercase								=						use_token_type_ids
    __lowercase								=						use_labels
    __lowercase								=						vocab_size
    __lowercase								=						hidden_size
    __lowercase								=						num_hidden_layers
    __lowercase								=						num_attention_heads
    __lowercase								=						intermediate_size
    __lowercase								=						hidden_act
    __lowercase								=						hidden_dropout_prob
    __lowercase								=						attention_probs_dropout_prob
    __lowercase								=						max_position_embeddings
    __lowercase								=						type_vocab_size
    __lowercase								=						type_sequence_label_size
    __lowercase								=						initializer_range
    __lowercase								=						num_labels
    __lowercase								=						num_choices
    __lowercase								=						scope
    __lowercase								=						self.vocab_size - 1


 def  _lowercase			(      self							:      Any     ):
    __lowercase								=						ids_tensor([self.batch_size, self.seq_length], self.vocab_size     )

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

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

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

    __lowercase								=						ids_tensor([self.num_hidden_layers, self.num_attention_heads], 2     )

    return (
        config,
        input_ids,
        head_mask,
        token_type_ids,
        sequence_labels,
        token_labels,
        choice_labels,
    )


 def  _lowercase			(      self							:      List[Any], UpperCAmelCase__							:      Optional[Any], UpperCAmelCase__							:      List[str], UpperCAmelCase__							:      Tuple, UpperCAmelCase__							:      List[Any], *UpperCAmelCase__							:      List[Any]     ):
    __lowercase								=						OpenAIGPTModel(config=UpperCAmelCase__     )
    model.to(UpperCAmelCase__     )
    model.eval()

    __lowercase								=						model(UpperCAmelCase__, token_type_ids=UpperCAmelCase__, head_mask=UpperCAmelCase__     )
    __lowercase								=						model(UpperCAmelCase__, token_type_ids=UpperCAmelCase__     )
    __lowercase								=						model(UpperCAmelCase__     )

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


 def  _lowercase			(      self							:      Any, UpperCAmelCase__							:      Any, UpperCAmelCase__							:      List[Any], UpperCAmelCase__							:      Any, UpperCAmelCase__							:      int, *UpperCAmelCase__							:      Dict     ):
    __lowercase								=						OpenAIGPTLMHeadModel(UpperCAmelCase__     )
    model.to(UpperCAmelCase__     )
    model.eval()

    __lowercase								=						model(UpperCAmelCase__, token_type_ids=UpperCAmelCase__, labels=UpperCAmelCase__     )
    self.parent.assertEqual(result.loss.shape, ()     )
    self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size)     )


 def  _lowercase			(      self							:      Optional[Any], UpperCAmelCase__							:      Optional[int], UpperCAmelCase__							:      Tuple, UpperCAmelCase__							:      List[str], UpperCAmelCase__							:      Dict, *UpperCAmelCase__							:      Optional[int]     ):
    __lowercase								=						OpenAIGPTDoubleHeadsModel(UpperCAmelCase__     )
    model.to(UpperCAmelCase__     )
    model.eval()

    __lowercase								=						model(UpperCAmelCase__, token_type_ids=UpperCAmelCase__, labels=UpperCAmelCase__     )
    self.parent.assertEqual(result.loss.shape, ()     )
    self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size)     )


 def  _lowercase			(      self							:      Any, UpperCAmelCase__							:      Optional[Any], UpperCAmelCase__							:      Union[str, Any], UpperCAmelCase__							:      Optional[int], UpperCAmelCase__							:      Tuple, *UpperCAmelCase__							:      Optional[Any]     ):
    __lowercase								=						self.num_labels
    __lowercase								=						OpenAIGPTForSequenceClassification(UpperCAmelCase__     )
    model.to(UpperCAmelCase__     )
    model.eval()

    __lowercase								=						ids_tensor([self.batch_size], self.type_sequence_label_size     )
    __lowercase								=						model(UpperCAmelCase__, token_type_ids=UpperCAmelCase__, labels=UpperCAmelCase__     )
    self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_labels)     )



 def  _lowercase			(      self							:      int     ):
    __lowercase								=						self.prepare_config_and_inputs()
    (
        (
        __lowercase
    )		,(
        __lowercase
    )		,(
        __lowercase
    )		,(
        __lowercase
    )		,(
        __lowercase
    )		,(
        __lowercase
    )		,(
        __lowercase
    )		,
    )								=						config_and_inputs
    __lowercase								=						{
        "input_ids": input_ids,
        "token_type_ids": token_type_ids,
        "head_mask": head_mask,
    }

    return config, inputs_dict







@require_torch
class  _lowerCAmelCase  (     lowercase					,lowercase					,lowercase					,unittest.TestCase       ):







 """simple docstring"""







 __UpperCAmelCase     : int       		=     (
     (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
     if is_torch_available()
     else ()
 )
 __UpperCAmelCase     : Optional[int]       		=     (
     (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 __UpperCAmelCase     : List[Any]       		=     (
     {
         "feature-extraction": OpenAIGPTModel,
         "text-classification": OpenAIGPTForSequenceClassification,
         "text-generation": OpenAIGPTLMHeadModel,
         "zero-shot": OpenAIGPTForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )


 def  _lowercase			(      self							:      Any, UpperCAmelCase__							:      List[str], UpperCAmelCase__							:      Optional[int], UpperCAmelCase__							:      List[Any], UpperCAmelCase__							:      Union[str, Any], UpperCAmelCase__							:      List[str]     ):
    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  _lowercase			(      self							:      Dict, UpperCAmelCase__							:      Optional[Any], UpperCAmelCase__							:      Tuple, UpperCAmelCase__							:      List[str]=False     ):
    __lowercase								=						super()._prepare_for_class(UpperCAmelCase__, UpperCAmelCase__, return_labels=UpperCAmelCase__     )

    if return_labels:
       if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
          __lowercase								=						torch.zeros(
              (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length), dtype=torch.long, device=UpperCAmelCase__, )
          __lowercase								=						inputs_dict["labels"]
          __lowercase								=						inputs_dict["labels"]
          __lowercase								=						torch.zeros(
              (self.model_tester.batch_size, self.model_tester.num_choices), dtype=torch.long, device=UpperCAmelCase__, )
          __lowercase								=						torch.zeros(
              self.model_tester.batch_size, dtype=torch.long, device=UpperCAmelCase__     )
    return inputs_dict


 def  _lowercase			(      self							:      List[str]     ):
    __lowercase								=						OpenAIGPTModelTester(self     )
    __lowercase								=						ConfigTester(self, config_class=UpperCAmelCase__, n_embd=3_7     )


 def  _lowercase			(      self							:      int     ):
    self.config_tester.run_common_tests()


 def  _lowercase			(      self							:      str     ):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase__     )


 def  _lowercase			(      self							:      Any     ):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_lm_head_model(*UpperCAmelCase__     )


 def  _lowercase			(      self							:      List[str]     ):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_double_lm_head_model(*UpperCAmelCase__     )


 def  _lowercase			(      self							:      Optional[int]     ):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCAmelCase__     )



 @slow
 def  _lowercase			(      self							:      Optional[Any]     ):
    for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowercase								=						OpenAIGPTModel.from_pretrained(UpperCAmelCase__     )
       self.assertIsNotNone(UpperCAmelCase__     )







@require_torch
class  _lowerCAmelCase  (     unittest.TestCase       ):







 """simple docstring"""







 @slow
 def  _lowercase			(      self							:      int     ):
    __lowercase								=						OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"     )
    model.to(UpperCAmelCase__     )
    __lowercase								=						torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]], dtype=torch.long, device=UpperCAmelCase__     )  # the president is
    __lowercase								=						[
        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

    __lowercase								=						model.generate(UpperCAmelCase__, do_sample=UpperCAmelCase__     )
    self.assertListEqual(output_ids[0].tolist(), UpperCAmelCase__     )







"""simple docstring"""





import json
import os
import tempfile

import datasets
from utils import generate_example_dataset, get_duration


_a							=							5_00_00
_a							=							50_00

_a				,						_a							=							os.path.split(__file__)
_a							=							os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json'))




@get_duration
def   _A  (  UpperCamelCase_					:	datasets.Dataset,      UpperCamelCase_					:	List[str])     ->						List[str]:
   '''simple docstring'''


   for i in range(UpperCamelCase_):
      __lowercase								=						dataset[i]




@get_duration
def   _A  (  UpperCamelCase_					:	datasets.Dataset,      UpperCamelCase_					:	List[Any],      UpperCamelCase_					:	int)     ->						Dict:
   '''simple docstring'''


   for i in range(0,      len(UpperCamelCase_),      UpperCamelCase_):
      __lowercase								=						dataset[i : i + batch_size]




@get_duration
def   _A  (  UpperCamelCase_					:	datasets.Dataset,      UpperCamelCase_					:	Any,      UpperCamelCase_					:	Optional[int])     ->						List[str]:
   '''simple docstring'''


   with dataset.formatted_as(type=UpperCamelCase_):
      for i in range(UpperCamelCase_):
         __lowercase								=						dataset[i]




@get_duration
def   _A  (  UpperCamelCase_					:	datasets.Dataset,      UpperCamelCase_					:	str,      UpperCamelCase_					:	Union[str, Any],      UpperCamelCase_					:	Union[str, Any])     ->						Dict:
   '''simple docstring'''


   with dataset.formatted_as(type=UpperCamelCase_):
      for i in range(0,      UpperCamelCase_,      UpperCamelCase_):
         __lowercase								=						dataset[i : i + batch_size]




def   _A  (  )     ->						List[str]:
   '''simple docstring'''


   __lowercase								=						{"num examples": SPEED_TEST_N_EXAMPLES}
   __lowercase								=						[
       (read, {"length": SMALL_TEST}),
       (read, {"length": SPEED_TEST_N_EXAMPLES}),
       (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10}),
       (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 100}),
       (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 1000}),
       (read_formatted, {"type": "numpy", "length": SMALL_TEST}),
       (read_formatted, {"type": "pandas", "length": SMALL_TEST}),
       (read_formatted, {"type": "torch", "length": SMALL_TEST}),
       (read_formatted, {"type": "tensorflow", "length": SMALL_TEST}),
       (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10}),
       (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 1000}),
   ]

   __lowercase								=						[
       (read, {"length": SMALL_TEST}),
       (read, {"length": SPEED_TEST_N_EXAMPLES}),
       (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10}),
       (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 100}),
       (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 1000}),
       (read_formatted, {"type": "numpy", "length": SMALL_TEST}),
       (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10}),
       (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 1000}),
   ]
   with tempfile.TemporaryDirectory() as tmp_dir:
      print("generating dataset")
      __lowercase								=						datasets.Features(
          {"list": datasets.Sequence(datasets.Value("float32")), "numbers": datasets.Value("float32")})
      __lowercase								=						generate_example_dataset(
          os.path.join(UpperCamelCase_,      "dataset.arrow"),      UpperCamelCase_,      num_examples=UpperCamelCase_,      seq_shapes={"list": (100,)},      )
      print("first set of iterations")
      for func, kwargs in functions:
         print(func.__name__,      str(UpperCamelCase_))
         __lowercase								=						func(UpperCamelCase_,      **UpperCamelCase_)

      print("shuffling dataset")
      __lowercase								=						dataset.shuffle()
      print("Second set of iterations (after shuffling")
      for func, kwargs in functions_shuffled:
         print("shuffled ",      func.__name__,      str(UpperCamelCase_))
         __lowercase								=						func(
             UpperCamelCase_,      **UpperCamelCase_)

   with open(UpperCamelCase_,      "wb") as f:
      f.write(json.dumps(UpperCamelCase_).encode("utf-8"))


if __name__ == "__main__":  # useful to run the profiler
   benchmark_iterating()




