"""simple docstring"""






from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


__lowerCAmelCase					: int        =logging.get_logger(__name__)

__lowerCAmelCase					: Optional[Any]        ={
    "EleutherAI/gpt-j-6B": "https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json",
    # See all GPT-J models at https://huggingface.co/models?filter=gpt_j
}





class 				_A		( _UpperCAmelCase			):

      '''simple docstring'''





      snake_case__    :     Optional[Any]													=			'gptj'
      snake_case__    :     Dict													=			{
          'max_position_embeddings': 'n_positions',
          'hidden_size': 'n_embd',
          'num_attention_heads': 'n_head',
          'num_hidden_layers': 'n_layer',
      }

      def __init__(		self	, __lowerCAmelCase=5_0400	, __lowerCAmelCase=2048	, __lowerCAmelCase=4096	, __lowerCAmelCase=28	, __lowerCAmelCase=16	, __lowerCAmelCase=64	, __lowerCAmelCase=None	, __lowerCAmelCase="gelu_new"	, __lowerCAmelCase=0.0	, __lowerCAmelCase=0.0	, __lowerCAmelCase=0.0	, __lowerCAmelCase=1E-5	, __lowerCAmelCase=0.0_2	, __lowerCAmelCase=True	, __lowerCAmelCase=5_0256	, __lowerCAmelCase=5_0256	, __lowerCAmelCase=False	, **__lowerCAmelCase	, ):







            """simple docstring"""



            lowercase														=			vocab_size
            lowercase														=			n_positions
            lowercase														=			n_embd
            lowercase														=			n_layer
            lowercase														=			n_head
            lowercase														=			n_inner
            lowercase														=			rotary_dim
            lowercase														=			activation_function
            lowercase														=			resid_pdrop
            lowercase														=			embd_pdrop
            lowercase														=			attn_pdrop
            lowercase														=			layer_norm_epsilon
            lowercase														=			initializer_range
            lowercase														=			use_cache

            lowercase														=			bos_token_id
            lowercase														=			eos_token_id

            super().__init__(
                bos_token_id=lowercase_	, eos_token_id=lowercase_	, tie_word_embeddings=lowercase_	, **lowercase_		)






class 				_A		( _UpperCAmelCase			):

      '''simple docstring'''





      def __init__(		self	, __lowerCAmelCase	, __lowerCAmelCase = "default"	, __lowerCAmelCase = None	, __lowerCAmelCase = False	, ):







            """simple docstring"""



            super().__init__(lowercase_	, task=lowercase_	, patching_specs=lowercase_	, use_past=lowercase_		)
            if not getattr(self._config	, """pad_token_id"""	, lowercase_		):
                  # TODO: how to do that better?
                  lowercase														=			0

      @property
      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}		)
            if self.use_past:
                  self.fill_with_past_key_values_(lowercase_	, direction="""inputs"""		)
                  lowercase														=			{0: """batch""", 1: """past_sequence + sequence"""}
            else:
                  lowercase														=			{0: """batch""", 1: """sequence"""}

            return common_inputs

      @property
      def        A__ (		self		):







            """simple docstring"""



            return self._config.n_layer

      @property
      def        A__ (		self		):







            """simple docstring"""



            return self._config.n_head

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = -1	, __lowerCAmelCase = -1	, __lowerCAmelCase = False	, __lowerCAmelCase = None	, ):







            """simple docstring"""



            lowercase														=			super(lowercase_	, self		).generate_dummy_inputs(
                lowercase_	, batch_size=lowercase_	, seq_length=lowercase_	, is_pair=lowercase_	, framework=lowercase_		)

            # We need to order the input in the way they appears in the forward()
            lowercase														=			OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}		)

            # Need to add the past_keys
            if self.use_past:
                  if not is_torch_available():
                        raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""		)
                  else:
                        import torch

                        lowercase														=			common_inputs["""input_ids"""].shape
                        # Not using the same length for past_key_values
                        lowercase														=			seqlen + 2
                        lowercase														=			(
                            batch,
                            self.num_attention_heads,
                            past_key_values_length,
                            self._config.hidden_size // self.num_attention_heads,
                        )
                        lowercase														=			[
                            (torch.zeros(lowercase_		), torch.zeros(lowercase_		)) for _ in range(self.num_layers		)
                        ]

            lowercase														=			common_inputs["""attention_mask"""]
            if self.use_past:
                  lowercase														=			ordered_inputs["""attention_mask"""].dtype
                  lowercase														=			torch.cat(
                      [ordered_inputs["""attention_mask"""], torch.ones(lowercase_	, lowercase_	, dtype=lowercase_		)]	, dim=1		)

            return ordered_inputs







      @property
      def        A__ (		self		):







            """simple docstring"""



            return 13




"""simple docstring"""






import os
from pathlib import Path
from unittest.mock import patch

import pytest
import zstandard as zstd

from datasets.download.download_config import DownloadConfig
from datasets.utils.file_utils import (
    OfflineModeIsEnabled,
    cached_path,
    fsspec_get,
    fsspec_head,
    ftp_get,
    ftp_head,
    get_from_cache,
    http_get,
    http_head,
)


__lowerCAmelCase					: List[Any]        ="""\
    Text data.
    Second line of data."""

__lowerCAmelCase					: Any        ="""file"""


@pytest.fixture(scope="""session"""	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :str	)				->					Union[str, Any]:
      '''simple docstring'''


      lowercase														=			tmp_path_factory.mktemp("""data"""	) / (FILE_PATH + """.zstd""")
      lowercase														=			bytes(lowerCAmelCase__      ,	"""utf-8"""	)
      with zstd.open(lowerCAmelCase__      ,	"""wb"""	) as f:
            f.write(lowerCAmelCase__	)
      return path


@pytest.fixture
def     UpperCAmelCase__ (       lowerCAmelCase__  :Tuple	)				->					Dict:
      '''simple docstring'''


      with open(os.path.join(tmpfs.local_root_dir      ,	lowerCAmelCase__	)      ,	"""w"""	) as f:
            f.write(lowerCAmelCase__	)
      return FILE_PATH


@pytest.mark.parametrize("""compression_format"""      ,	["""gzip""", """xz""", """zstd"""]	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :Any      ,	lowerCAmelCase__  :Any      ,	lowerCAmelCase__  :List[Any]      ,	lowerCAmelCase__  :Optional[Any]      ,	lowerCAmelCase__  :Any      ,	lowerCAmelCase__  :int	)				->					Union[str, Any]:
      '''simple docstring'''


      lowercase														=			{"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_path}
      lowercase														=			input_paths[compression_format]
      lowercase														=			tmp_path / """cache"""
      lowercase														=			DownloadConfig(cache_dir=lowerCAmelCase__      ,	extract_compressed_file=lowerCAmelCase__	)
      lowercase														=			cached_path(lowerCAmelCase__      ,	download_config=lowerCAmelCase__	)
      with open(lowerCAmelCase__	) as f:
            lowercase														=			f.read()
      with open(lowerCAmelCase__	) as f:
            lowercase														=			f.read()
      assert extracted_file_content == expected_file_content


@pytest.mark.parametrize("""default_extracted"""      ,	[True, False]	)
@pytest.mark.parametrize("""default_cache_dir"""      ,	[True, False]	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :Dict      ,	lowerCAmelCase__  :Tuple      ,	lowerCAmelCase__  :Tuple      ,	lowerCAmelCase__  :Union[str, Any]      ,	lowerCAmelCase__  :List[str]	)				->					Any:
      '''simple docstring'''


      lowercase														=			"""custom_cache"""
      lowercase														=			"""custom_extracted_dir"""
      lowercase														=			tmp_path / """custom_extracted_path"""
      if default_extracted:
            lowercase														=			("""downloads""" if default_cache_dir else custom_cache_dir, """extracted""")
      else:
            monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_DIR"""      ,	lowerCAmelCase__	)
            monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_PATH"""      ,	str(lowerCAmelCase__	)	)
            lowercase														=			custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

      lowercase														=			xz_file
      lowercase														=			(
          DownloadConfig(extract_compressed_file=lowerCAmelCase__	)
          if default_cache_dir
          else DownloadConfig(cache_dir=tmp_path / custom_cache_dir      ,	extract_compressed_file=lowerCAmelCase__	)
      )
      lowercase														=			cached_path(lowerCAmelCase__      ,	download_config=lowerCAmelCase__	)
      assert Path(lowerCAmelCase__	).parent.parts[-2:] == expected


def     UpperCAmelCase__ (       lowerCAmelCase__  :List[str]	)				->					Tuple:
      '''simple docstring'''


      lowercase														=			str(Path(lowerCAmelCase__	).resolve()	)
      assert cached_path(lowerCAmelCase__	) == text_file
      # relative path
      lowercase														=			str(Path(lowerCAmelCase__	).resolve().relative_to(Path(os.getcwd()	)	)	)
      assert cached_path(lowerCAmelCase__	) == text_file


def     UpperCAmelCase__ (       lowerCAmelCase__  :Any	)				->					List[Any]:
      '''simple docstring'''


      lowercase														=			str(tmp_path.resolve() / """__missing_file__.txt"""	)
      with pytest.raises(lowerCAmelCase__	):
            cached_path(lowerCAmelCase__	)
      # relative path
      lowercase														=			"""./__missing_file__.txt"""
      with pytest.raises(lowerCAmelCase__	):
            cached_path(lowerCAmelCase__	)


def     UpperCAmelCase__ (       lowerCAmelCase__  :Any	)				->					Union[str, Any]:
      '''simple docstring'''


      lowercase														=			get_from_cache(f'tmp://{tmpfs_file}'	)
      with open(lowerCAmelCase__	) as f:
            lowercase														=			f.read()
      assert output_file_content == FILE_CONTENT


@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,	lowerCAmelCase__	)
def     UpperCAmelCase__ (       )				->					str:
      '''simple docstring'''


      with pytest.raises(lowerCAmelCase__	):
            cached_path("""https://huggingface.co"""	)


@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,	lowerCAmelCase__	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :Tuple	)				->					Any:
      '''simple docstring'''


      lowercase														=			tmp_path_factory.mktemp("""data"""	) / """file.html"""
      with pytest.raises(lowerCAmelCase__	):
            http_get("""https://huggingface.co"""      ,	temp_file=lowerCAmelCase__	)
      with pytest.raises(lowerCAmelCase__	):
            http_head("""https://huggingface.co"""	)


@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,	lowerCAmelCase__	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :str	)				->					Any:
      '''simple docstring'''


      lowercase														=			tmp_path_factory.mktemp("""data"""	) / """file.html"""
      with pytest.raises(lowerCAmelCase__	):
            ftp_get("""ftp://huggingface.co"""      ,	temp_file=lowerCAmelCase__	)
      with pytest.raises(lowerCAmelCase__	):
            ftp_head("""ftp://huggingface.co"""	)






@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,	lowerCAmelCase__	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :Any	)				->					Dict:
      '''simple docstring'''


      lowercase														=			tmp_path_factory.mktemp("""data"""	) / """file.html"""
      with pytest.raises(lowerCAmelCase__	):
            fsspec_get("""s3://huggingface.co"""      ,	temp_file=lowerCAmelCase__	)
      with pytest.raises(lowerCAmelCase__	):
            fsspec_head("""s3://huggingface.co"""	)



