





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						:    Optional[Any]						,lowerCamelCase__						:    int						,lowerCamelCase__						:    Optional[int]=13						,lowerCamelCase__						:    Optional[Any]=7						,lowerCamelCase__						:    Tuple=True						,lowerCamelCase__						:    Optional[int]=True						,lowerCamelCase__						:    int=True						,lowerCamelCase__						:    List[str]=99						,lowerCamelCase__						:    Union[str, Any]=32						,lowerCamelCase__						:    Union[str, Any]=5						,lowerCamelCase__						:    Optional[int]=4						,lowerCamelCase__						:    Any=37						,lowerCamelCase__						:    Union[str, Any]="gelu"						,lowerCamelCase__						:    List[str]=0.1						,lowerCamelCase__						:    str=0.1						,lowerCamelCase__						:    List[Any]=512						,lowerCamelCase__						:    int=16						,lowerCamelCase__						:    Optional[Any]=2						,lowerCamelCase__						:    List[str]=0.02						,lowerCamelCase__						:    Any=3						,lowerCamelCase__						:    List[Any]=4						,lowerCamelCase__						:    Union[str, Any]=None						,)     ->		int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						parent
           SCREAMING_SNAKE_CASE		=						batch_size
           SCREAMING_SNAKE_CASE		=						seq_length
           SCREAMING_SNAKE_CASE		=						is_training
           SCREAMING_SNAKE_CASE		=						use_token_type_ids
           SCREAMING_SNAKE_CASE		=						use_labels
           SCREAMING_SNAKE_CASE		=						vocab_size
           SCREAMING_SNAKE_CASE		=						hidden_size
           SCREAMING_SNAKE_CASE		=						num_hidden_layers
           SCREAMING_SNAKE_CASE		=						num_attention_heads
           SCREAMING_SNAKE_CASE		=						intermediate_size
           SCREAMING_SNAKE_CASE		=						hidden_act
           SCREAMING_SNAKE_CASE		=						hidden_dropout_prob
           SCREAMING_SNAKE_CASE		=						attention_probs_dropout_prob
           SCREAMING_SNAKE_CASE		=						max_position_embeddings
           SCREAMING_SNAKE_CASE		=						type_vocab_size
           SCREAMING_SNAKE_CASE		=						type_sequence_label_size
           SCREAMING_SNAKE_CASE		=						initializer_range
           SCREAMING_SNAKE_CASE		=						num_labels
           SCREAMING_SNAKE_CASE		=						num_choices
           SCREAMING_SNAKE_CASE		=						scope
           SCREAMING_SNAKE_CASE		=						self.vocab_size - 1






     def 				SCREAMING_SNAKE_CASE__  (							self						:    int       )     ->		str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						ids_tensor([self.batch_size, self.seq_length]						,self.vocab_size       )

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

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

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

           SCREAMING_SNAKE_CASE		=						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 				SCREAMING_SNAKE_CASE__  (							self						:    str						,lowerCamelCase__						:    int						,lowerCamelCase__						:    int						,lowerCamelCase__						:    Optional[int]						,lowerCamelCase__						:    List[Any]						,*lowerCamelCase__						:    Tuple       )     ->		Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						OpenAIGPTModel(config=lowerCamelCase__       )
           model.to(lowerCamelCase__       )
           model.eval()

           SCREAMING_SNAKE_CASE		=						model(lowerCamelCase__						,token_type_ids=lowerCamelCase__						,head_mask=lowerCamelCase__       )
           SCREAMING_SNAKE_CASE		=						model(lowerCamelCase__						,token_type_ids=lowerCamelCase__       )
           SCREAMING_SNAKE_CASE		=						model(lowerCamelCase__       )

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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Any						,lowerCamelCase__						:    List[str]						,lowerCamelCase__						:    Optional[int]						,lowerCamelCase__						:    int						,lowerCamelCase__						:    str						,*lowerCamelCase__						:    Any       )     ->		List[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						OpenAIGPTLMHeadModel(lowerCamelCase__       )
           model.to(lowerCamelCase__       )
           model.eval()

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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Any						,lowerCamelCase__						:    Any						,lowerCamelCase__						:    Optional[Any]						,lowerCamelCase__						:    int						,lowerCamelCase__						:    str						,*lowerCamelCase__						:    str       )     ->		Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						OpenAIGPTDoubleHeadsModel(lowerCamelCase__       )
           model.to(lowerCamelCase__       )
           model.eval()

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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    List[str]						,lowerCamelCase__						:    List[Any]						,lowerCamelCase__						:    Optional[Any]						,lowerCamelCase__						:    int						,lowerCamelCase__						:    int						,*lowerCamelCase__						:    Any       )     ->		int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.num_labels
           SCREAMING_SNAKE_CASE		=						OpenAIGPTForSequenceClassification(lowerCamelCase__       )
           model.to(lowerCamelCase__       )
           model.eval()

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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    str       )     ->		Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.prepare_config_and_inputs()
           (
               (
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					
           )		=						config_and_inputs
           SCREAMING_SNAKE_CASE		=						{
               """input_ids""": input_ids,
               """token_type_ids""": token_type_ids,
               """head_mask""": head_mask,
           }

           return config, inputs_dict

@require_torch
class 		UpperCamelCase__ (      lowerCAmelCase_							,      lowerCAmelCase_							,      lowerCAmelCase_							,      unittest.TestCase   ):



     '''simple docstring'''

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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[Any]						,lowerCamelCase__						:    Any						,lowerCamelCase__						:    List[Any]						,lowerCamelCase__						:    Optional[Any]						,lowerCamelCase__						:    Tuple						,lowerCamelCase__						:    Optional[int]       )     ->		Any:




           '''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 				SCREAMING_SNAKE_CASE__  (							self						:    str						,lowerCamelCase__						:    Dict						,lowerCamelCase__						:    str						,lowerCamelCase__						:    Union[str, Any]=False       )     ->		Union[str, Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						super()._prepare_for_class(lowerCamelCase__						,lowerCamelCase__						,return_labels=lowerCamelCase__       )

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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Tuple       )     ->		int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						OpenAIGPTModelTester(self       )
           SCREAMING_SNAKE_CASE		=						ConfigTester(self						,config_class=lowerCamelCase__						,n_embd=37       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[int]       )     ->		int:




           '''simple docstring'''


           self.config_tester.run_common_tests()






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Dict       )     ->		Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*lowerCamelCase__       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[Any]       )     ->		str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_lm_head_model(*lowerCamelCase__       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Dict       )     ->		Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_double_lm_head_model(*lowerCamelCase__       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Dict       )     ->		Union[str, Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCamelCase__       )






     @slow
     def 				SCREAMING_SNAKE_CASE__  (							self						:    Dict       )     ->		Dict:




           '''simple docstring'''


           for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 SCREAMING_SNAKE_CASE		=						OpenAIGPTModel.from_pretrained(lowerCamelCase__       )
                 self.assertIsNotNone(lowerCamelCase__       )

@require_torch
class 		UpperCamelCase__ (      unittest.TestCase   ):



     '''simple docstring'''

     @slow
     def 				SCREAMING_SNAKE_CASE__  (							self						:    int       )     ->		int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""       )
           model.to(lowerCamelCase__       )
           SCREAMING_SNAKE_CASE		=						torch.tensor([[481, 4735, 544]]						,dtype=torch.long						,device=lowerCamelCase__       )  # the president is
           SCREAMING_SNAKE_CASE		=						[
               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

           SCREAMING_SNAKE_CASE		=						model.generate(lowerCamelCase__						,do_sample=lowerCamelCase__       )
           self.assertListEqual(output_ids[0].tolist()						,lowerCamelCase__       )









import importlib
import shutil
import threading
import warnings
from typing import List

import fsspec
import fsspec.asyn

from . import compression
from .hffilesystem import HfFileSystem


SCREAMING_SNAKE_CASE_						   =       importlib.util.find_spec("""s3fs""") is not None

if _has_safs:
      from .safilesystem import SaFileSystem  # noqa: F401

SCREAMING_SNAKE_CASE_						   =       [
    compression.BzaFileSystem,
    compression.GzipFileSystem,
    compression.LzaFileSystem,
    compression.XzFileSystem,
    compression.ZstdFileSystem,
]

# Register custom filesystems
for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]:
      if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class:
            warnings.warn(F'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''')
      fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True)



def    __lowercase   (      _SCREAMING_SNAKE_CASE	) ->   str:


      '''simple docstring'''


      if "://" in dataset_path:
            SCREAMING_SNAKE_CASE		=						dataset_path.split("""://"""	)[1]
      return dataset_path



def    __lowercase   (      _SCREAMING_SNAKE_CASE	) ->   bool:


      '''simple docstring'''


      if fs is not None and fs.protocol != "file":
            return True
      else:
            return False



def    __lowercase   (      _SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE	) ->   Dict:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						not is_remote_filesystem(_SCREAMING_SNAKE_CASE	)
      if is_local:
            # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory
            shutil.move(fs._strip_protocol(_SCREAMING_SNAKE_CASE	) ,						fs._strip_protocol(_SCREAMING_SNAKE_CASE	)	)
      else:
            fs.mv(_SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE ,						recursive=_SCREAMING_SNAKE_CASE	)



def    __lowercase   (      ) ->   None:


      '''simple docstring'''


      if hasattr(fsspec.asyn ,						"""reset_lock"""	):
            # for future fsspec>2022.05.0
            fsspec.asyn.reset_lock()
      else:
            SCREAMING_SNAKE_CASE		=						None
            SCREAMING_SNAKE_CASE		=						None
            SCREAMING_SNAKE_CASE		=						threading.Lock()


