
"""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      _UpperCAmelCase							:




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




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

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

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

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

          UpperCAmelCase									=					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     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       *lowercase_							)						->					Dict:
          UpperCAmelCase									=					OpenAIGPTModel(config=UpperCAmelCase__							)
          model.to(UpperCAmelCase__							)
          model.eval()

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

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




      def     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       *lowercase_							)						->					int:
          UpperCAmelCase									=					OpenAIGPTLMHeadModel(UpperCAmelCase__							)
          model.to(UpperCAmelCase__							)
          model.eval()

          UpperCAmelCase									=					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     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       *lowercase_							)						->					str:
          UpperCAmelCase									=					OpenAIGPTDoubleHeadsModel(UpperCAmelCase__							)
          model.to(UpperCAmelCase__							)
          model.eval()

          UpperCAmelCase									=					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     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       *lowercase_							)						->					Any:
          UpperCAmelCase									=					self.num_labels
          UpperCAmelCase									=					OpenAIGPTForSequenceClassification(UpperCAmelCase__							)
          model.to(UpperCAmelCase__							)
          model.eval()

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




      def     a_      (							self							)						->					Optional[int]:
          UpperCAmelCase									=					self.prepare_config_and_inputs()
          (
              UpperCAmelCase	
          )									=					config_and_inputs
          UpperCAmelCase									=					{
              '''input_ids''': input_ids,
              '''token_type_ids''': token_type_ids,
              '''head_mask''': head_mask,
          }

          return config, inputs_dict

@require_torch
class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_     ,			SCREAMING_SNAKE_CASE_     ,			SCREAMING_SNAKE_CASE_     ,			unittest.TestCase							):
      __SCREAMING_SNAKE_CASE   :							Any							      =	(
          (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
          if is_torch_available()
          else ()
      )
      __SCREAMING_SNAKE_CASE   :							List[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     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_   ,       lowercase_							)						->					int:
          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     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_=False							)						->					Union[str, Any]:
          UpperCAmelCase									=					super()._prepare_for_class(UpperCAmelCase__   ,       UpperCAmelCase__   ,       return_labels=UpperCAmelCase__							)

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




      def     a_      (							self							)						->					Union[str, Any]:
          UpperCAmelCase									=					OpenAIGPTModelTester(self							)
          UpperCAmelCase									=					ConfigTester(self   ,       config_class=UpperCAmelCase__   ,       n_embd=3_7							)




      def     a_      (							self							)						->					Optional[int]:
          self.config_tester.run_common_tests()




      def     a_      (							self							)						->					Tuple:
          UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase__							)




      def     a_      (							self							)						->					Any:
          UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(*UpperCAmelCase__							)




      def     a_      (							self							)						->					Optional[Any]:
          UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_double_lm_head_model(*UpperCAmelCase__							)




      def     a_      (							self							)						->					Union[str, Any]:
          UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCAmelCase__							)




      @slow
      def     a_      (							self							)						->					List[Any]:
          for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase									=					OpenAIGPTModel.from_pretrained(UpperCAmelCase__							)
              self.assertIsNotNone(UpperCAmelCase__							)

@require_torch
class      _UpperCAmelCase							(		unittest.TestCase							):




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

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





"""simple docstring"""

import os
from typing import Optional

import fsspec
from fsspec.archive import AbstractArchiveFileSystem
from fsspec.utils import DEFAULT_BLOCK_SIZE
class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_							):
      __SCREAMING_SNAKE_CASE   :							str							      =	""
      __SCREAMING_SNAKE_CASE   :							str							      =	(
          None  # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz
      )
      __SCREAMING_SNAKE_CASE   :							str							      =	None  # compression type in fsspec. ex: "gzip"
      __SCREAMING_SNAKE_CASE   :							str							      =	None  # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz




      def __init__(							self   ,       lowercase_ = ""   ,       lowercase_ = None   ,       lowercase_ = None   ,       **lowercase_							)						->					str:
          super().__init__(self   ,       **lowercase_							)
          # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode
          UpperCAmelCase									=					fsspec.open(
              lowercase_   ,       mode='rb'   ,       protocol=lowercase_   ,       compression=self.compression   ,       client_kwargs={
                  'requote_redirect_url': False,  # see https://github.com/huggingface/datasets/pull/5459
                  'trust_env': True,  # Enable reading proxy env variables.
                  **(target_options or {}).pop('client_kwargs'   ,       {}							),  # To avoid issues if it was already passed.
              }   ,       **(target_options or {})   ,       )
          UpperCAmelCase									=					os.path.basename(self.file.path.split('::'							)[0]							)
          UpperCAmelCase									=					(
              self.compressed_name[: self.compressed_name.rindex('.'							)]
              if '.' in self.compressed_name
              else self.compressed_name
          )
          UpperCAmelCase									=					None




      @classmethod
      def     a_      (							cls   ,       lowercase_							)						->					Union[str, Any]:
          # compressed file paths are always relative to the archive root
          return super()._strip_protocol(lowercase_							).lstrip('/'							)




      def     a_      (							self							)						->					int:
          if self.dir_cache is None:
              UpperCAmelCase									=					{**self.file.fs.info(self.file.path							), 'name': self.uncompressed_name}
              UpperCAmelCase									=					{f['name']: f}




      def     a_      (							self   ,       lowercase_							)						->					Any:
          return self.file.open().read()




      def     a_      (							self   ,       lowercase_   ,       lowercase_ = "rb"   ,       lowercase_=None   ,       lowercase_=True   ,       lowercase_=None   ,       **lowercase_   ,       )						->					Optional[Any]:
          UpperCAmelCase									=					self._strip_protocol(lowercase_							)
          if mode != "rb":
              raise ValueError(F"Tried to read with mode {mode} on file {self.file.path} opened with mode 'rb'"							)
          return self.file.open()

class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_							):
      __SCREAMING_SNAKE_CASE   :							List[Any]							      =	"bz2"
      __SCREAMING_SNAKE_CASE   :							Union[str, Any]							      =	"bz2"
      __SCREAMING_SNAKE_CASE   :							Union[str, Any]							      =	".bz2"

class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_							):
      __SCREAMING_SNAKE_CASE   :							List[str]							      =	"gzip"
      __SCREAMING_SNAKE_CASE   :							Any							      =	"gzip"
      __SCREAMING_SNAKE_CASE   :							List[str]							      =	".gz"

class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_							):
      __SCREAMING_SNAKE_CASE   :							List[str]							      =	"lz4"
      __SCREAMING_SNAKE_CASE   :							List[Any]							      =	"lz4"
      __SCREAMING_SNAKE_CASE   :							Tuple							      =	".lz4"

class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_							):
      __SCREAMING_SNAKE_CASE   :							List[str]							      =	"xz"
      __SCREAMING_SNAKE_CASE   :							Union[str, Any]							      =	"xz"
      __SCREAMING_SNAKE_CASE   :							List[Any]							      =	".xz"

class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_							):
      __SCREAMING_SNAKE_CASE   :							List[str]							      =	"zstd"
      __SCREAMING_SNAKE_CASE   :							int							      =	"zstd"
      __SCREAMING_SNAKE_CASE   :							Tuple							      =	".zst"




      def __init__(							self   ,       lowercase_   ,       lowercase_ = "rb"   ,       lowercase_ = None   ,       lowercase_ = None   ,       lowercase_ = DEFAULT_BLOCK_SIZE   ,       **lowercase_   ,       )						->					str:
          super().__init__(
              fo=lowercase_   ,       mode=lowercase_   ,       target_protocol=lowercase_   ,       target_options=lowercase_   ,       block_size=lowercase_   ,       **lowercase_   ,       )
          # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2:
          #
          # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open
          #     out.close = close
          # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only
          #
          # see https://github.com/intake/filesystem_spec/issues/725
          UpperCAmelCase									=					self.file.__enter__
          class      _UpperCAmelCase							:




                def __init__(							self   ,       lowercase_							)						->					List[Any]:
                    UpperCAmelCase									=					file_




                def __enter__(							self							)						->					List[str]:
                    self._file.__enter__()
                    return self




                def __exit__(							self   ,       *lowercase_   ,       **lowercase_							)						->					List[str]:
                    self._file.__exit__(*lowercase_   ,       **lowercase_							)




                def __iter__(							self							)						->					Optional[int]:
                    return iter(self._file							)




                def     a_      (							self							)						->					Tuple:
                    return next(self._file							)




                def __getattr__(							self   ,       lowercase_							)						->					Optional[int]:
                    return getattr(self._file   ,       lowercase_							)

          def fixed_enter(*lowercase_   ,       **lowercase_							):
              return WrappedFile(_enter(*lowercase_   ,       **lowercase_							)							)

          UpperCAmelCase									=					fixed_enter



