




'''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   _a							:






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






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

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

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

          _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   ,)

          _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 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,*_SCREAMING_SNAKE_CASE       )				->     List[Any]:
          _snake_case				= OpenAIGPTModel(config=__a       )
          model.to(__a       )
          model.eval()

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

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






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,*_SCREAMING_SNAKE_CASE       )				->     Union[str, Any]:
          _snake_case				= OpenAIGPTLMHeadModel(__a       )
          model.to(__a       )
          model.eval()

          _snake_case				= 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 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,*_SCREAMING_SNAKE_CASE       )				->     Any:
          _snake_case				= OpenAIGPTDoubleHeadsModel(__a       )
          model.to(__a       )
          model.eval()

          _snake_case				= 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 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,*_SCREAMING_SNAKE_CASE       )				->     Optional[int]:
          _snake_case				= self.num_labels
          _snake_case				= OpenAIGPTForSequenceClassification(__a       )
          model.to(__a       )
          model.eval()

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






     def 							_lowercase      (					self       )				->     Optional[Any]:
          _snake_case				= self.prepare_config_and_inputs()
          (
              _snake_case		
          )				= config_and_inputs
          _snake_case				= {
              """input_ids""": input_ids,
              """token_type_ids""": token_type_ids,
              """head_mask""": head_mask,
          }

          return config, inputs_dict







@require_torch
class   _a							(		a__   ,   a__   ,   a__   ,   unittest.TestCase				):
     SCREAMING_SNAKE_CASE_				:       Union[str, Any]       							=		(
         (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
         if is_torch_available()
         else ()
     )
     SCREAMING_SNAKE_CASE_				:       str       							=		(
         (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     SCREAMING_SNAKE_CASE_				:       List[str]       							=		(
         {
             """feature-extraction""": OpenAIGPTModel,
             """text-classification""": OpenAIGPTForSequenceClassification,
             """text-generation""": OpenAIGPTLMHeadModel,
             """zero-shot""": OpenAIGPTForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )				->     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 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE=False       )				->     Any:
          _snake_case				= super()._prepare_for_class(__a   ,__a   ,return_labels=__a       )

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






     def 							_lowercase      (					self       )				->     int:
          _snake_case				= OpenAIGPTModelTester(self       )
          _snake_case				= ConfigTester(self   ,config_class=__a   ,n_embd=37       )






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






     def 							_lowercase      (					self       )				->     Optional[int]:
          _snake_case				= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_model(*__a       )






     def 							_lowercase      (					self       )				->     int:
          _snake_case				= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(*__a       )






     def 							_lowercase      (					self       )				->     Any:
          _snake_case				= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_double_lm_head_model(*__a       )






     def 							_lowercase      (					self       )				->     Optional[Any]:
          _snake_case				= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__a       )






     @slow
     def 							_lowercase      (					self       )				->     Optional[Any]:
          for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _snake_case				= OpenAIGPTModel.from_pretrained(__a       )
               self.assertIsNotNone(__a       )







@require_torch
class   _a							(		unittest.TestCase				):






     @slow
     def 							_lowercase      (					self       )				->     Optional[int]:
          _snake_case				= OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"       )
          model.to(__a       )
          _snake_case				= torch.tensor([[481, 4_735, 544]]   ,dtype=torch.long   ,device=__a       )  # the president is
          _snake_case				= [
              481,
              4_735,
              544,
              246,
              963,
              870,
              762,
              239,
              244,
              40_477,
              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

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








'''simple docstring'''







import os
import time

import pytest

from datasets.utils.filelock import FileLock, Timeout



def        lowerCamelCase__				(      __lowercase				):
     snake_case			:      List[Any]        =			FileLock(str(tmpdir / """foo.lock"""				)				)
     snake_case			:      str        =			FileLock(str(tmpdir / """foo.lock"""				)				)
     snake_case			:      Any        =			0.01
     with locka.acquire():
          with pytest.raises(__lowercase				):
               snake_case			:      Union[str, Any]        =			time.time()
               locka.acquire(__lowercase				)
               assert time.time() - _start > timeout







def        lowerCamelCase__				(      __lowercase				):
     snake_case			:      List[Any]        =			"""a""" * 1_000 + """.lock"""
     snake_case			:      List[str]        =			FileLock(str(tmpdir / filename				)				)
     assert locka._lock_file.endswith(""".lock"""				)
     assert not locka._lock_file.endswith(__lowercase				)
     assert len(os.path.basename(locka._lock_file				)				) <= 255
     snake_case			:      Any        =			FileLock(tmpdir / filename				)
     with locka.acquire():
          with pytest.raises(__lowercase				):
               locka.acquire(0				)



