


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


     """simple docstring"""


     def __init__(	self  ,       __A  ,       __A=13  ,       __A=7  ,       __A=True  ,       __A=True  ,       __A=True  ,       __A=99  ,       __A=32  ,       __A=5  ,       __A=4  ,       __A=37  ,       __A="gelu"  ,       __A=0.1  ,       __A=0.1  ,       __A=512  ,       __A=16  ,       __A=2  ,       __A=0.02  ,       __A=3  ,       __A=4  ,       __A=None  ,       ):
        __a					=  parent
        __a					=  batch_size
        __a					=  seq_length
        __a					=  is_training
        __a					=  use_token_type_ids
        __a					=  use_labels
        __a					=  vocab_size
        __a					=  hidden_size
        __a					=  num_hidden_layers
        __a					=  num_attention_heads
        __a					=  intermediate_size
        __a					=  hidden_act
        __a					=  hidden_dropout_prob
        __a					=  attention_probs_dropout_prob
        __a					=  max_position_embeddings
        __a					=  type_vocab_size
        __a					=  type_sequence_label_size
        __a					=  initializer_range
        __a					=  num_labels
        __a					=  num_choices
        __a					=  scope
        __a					=  self.vocab_size - 1


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

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

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

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

        __a					=  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 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       *__A    ):
        __a					=  OpenAIGPTModel(config=__A    )
        model.to(__A    )
        model.eval()

        __a					=  model(__A  ,       token_type_ids=__A  ,       head_mask=__A    )
        __a					=  model(__A  ,       token_type_ids=__A    )
        __a					=  model(__A    )

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


     def 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       *__A    ):
        __a					=  OpenAIGPTLMHeadModel(__A    )
        model.to(__A    )
        model.eval()

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


     def 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       *__A    ):
        __a					=  OpenAIGPTDoubleHeadsModel(__A    )
        model.to(__A    )
        model.eval()

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


     def 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       *__A    ):
        __a					=  self.num_labels
        __a					=  OpenAIGPTForSequenceClassification(__A    )
        model.to(__A    )
        model.eval()

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


     def 	snake_case_  (	self    ):
        __a					=  self.prepare_config_and_inputs()
        (
            (
            __a
        )	,    (
            __a
        )	,    (
            __a
        )	,    (
            __a
        )	,    (
            __a
        )	,    (
            __a
        )	,    (
            __a
        )	,    
        )					=  config_and_inputs
        __a					=  {
            """input_ids""": input_ids,
            """token_type_ids""": token_type_ids,
            """head_mask""": head_mask,
        }

        return config, inputs_dict







@require_torch
class     __UpperCAmelCase							(       __A ,					__A ,					__A ,					unittest.TestCase							):


     """simple docstring"""
     _lowerCamelCase   =      (
         (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
         if is_torch_available()
         else ()
     )
     _lowerCamelCase   =      (
         (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     _lowerCamelCase   =      (
         {
             """feature-extraction""": OpenAIGPTModel,
             """text-classification""": OpenAIGPTForSequenceClassification,
             """text-generation""": OpenAIGPTLMHeadModel,
             """zero-shot""": OpenAIGPTForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )


     def 	snake_case_  (	self  ,       __A  ,       __A  ,       __A  ,       __A  ,       __A    ):
        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  ,       __A  ,       __A  ,       __A=False    ):
        __a					=  super()._prepare_for_class(__A  ,       __A  ,       return_labels=__A    )

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


     def 	snake_case_  (	self    ):
        __a					=  OpenAIGPTModelTester(self    )
        __a					=  ConfigTester(self  ,       config_class=__A  ,       n_embd=37    )


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


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_double_lm_head_model(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__A    )


     @slow
     def 	snake_case_  (	self    ):
        for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __a					=  OpenAIGPTModel.from_pretrained(__A    )
           self.assertIsNotNone(__A    )







@require_torch
class     __UpperCAmelCase							(       unittest.TestCase							):


     """simple docstring"""


     @slow
     def 	snake_case_  (	self    ):
        __a					=  OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""    )
        model.to(__A    )
        __a					=  torch.tensor([[481, 4735, 544]]  ,       dtype=torch.long  ,       device=__A    )  # the president is
        __a					=  [
            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

        __a					=  model.generate(__A  ,       do_sample=__A    )
        self.assertListEqual(output_ids[0].tolist()  ,       __A    )






_UpperCAmelCase						:  Any									=			6_5521





def    __lowerCamelCase      (					UpperCamelCase__ ):







							'''simple docstring'''






							snake_case_     							=       1
							snake_case_     							=       0
							for plain_chr in plain_text:
														snake_case_     							=       (a + ord(UpperCamelCase__ )) % MOD_ADLER
														snake_case_     							=       (b + a) % MOD_ADLER
							return (b << 16) | a




