






"""simple docstring"""



import importlib
import os
import platform
from pathlib import Path

from packaging import version

from .utils.logging import get_logger


a  :						Optional[int]					       =							get_logger(__name__)

# Datasets
a  :						Tuple					       =							'''https://s3.amazonaws.com/datasets.huggingface.co/datasets/datasets'''
a  :						str					       =							'''https://cdn-datasets.huggingface.co/datasets/datasets'''
a  :						Any					       =							'''https://raw.githubusercontent.com/huggingface/datasets/{revision}/datasets/{path}/{name}'''

# Metrics
a  :						int					       =							'''https://s3.amazonaws.com/datasets.huggingface.co/datasets/metrics'''
a  :						Any					       =							'''https://cdn-datasets.huggingface.co/datasets/metric'''
a  :						Optional[int]					       =							'''https://raw.githubusercontent.com/huggingface/datasets/{revision}/metrics/{path}/{name}'''

# Hub
a  :						str					       =							os.environ.get('''HF_ENDPOINT''', '''https://huggingface.co''')
a  :						Any					       =							HF_ENDPOINT + '''/datasets/{repo_id}/resolve/{revision}/{path}'''
a  :						Tuple					       =							'''main'''

a  :						Any					       =							version.parse(platform.python_version())

if PY_VERSION < version.parse('''3.8'''):
 import importlib_metadata
else:
 import importlib.metadata as importlib_metadata

# General environment variables accepted values for booleans
a  :						Dict					       =							{'''1''', '''ON''', '''YES''', '''TRUE'''}
a  :						Optional[Any]					       =							ENV_VARS_TRUE_VALUES.union({'''AUTO'''})


# Imports
a  :						str					       =							version.parse(importlib_metadata.version('''dill'''))
a  :						Dict					       =							version.parse(importlib_metadata.version('''pandas'''))
a  :						List[Any]					       =							version.parse(importlib_metadata.version('''pyarrow'''))

a  :						Union[str, Any]					       =							os.environ.get('''USE_TF''', '''AUTO''').upper()
a  :						List[str]					       =							os.environ.get('''USE_TORCH''', '''AUTO''').upper()
a  :						Union[str, Any]					       =							os.environ.get('''USE_JAX''', '''AUTO''').upper()

a  :						Optional[Any]					       =							'''N/A'''
a  :						Optional[Any]					       =							False

if USE_TORCH in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TF not in ENV_VARS_TRUE_VALUES:
 a  :						List[Any]					       =							importlib.util.find_spec('''torch''') is not None
 if TORCH_AVAILABLE:
  try:
   a  :						str					       =							version.parse(importlib_metadata.version('''torch'''))
   logger.info(F'''PyTorch version {TORCH_VERSION} available.''')
  except importlib_metadata.PackageNotFoundError:
   pass
else:
 logger.info('''Disabling PyTorch because USE_TF is set''')

a  :						Tuple					       =							'''N/A'''
a  :						Optional[Any]					       =							False

if USE_TF in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TORCH not in ENV_VARS_TRUE_VALUES:
 a  :						Optional[Any]					       =							importlib.util.find_spec('''tensorflow''') is not None
 if TF_AVAILABLE:
  # For the metadata, we have to look for both tensorflow and tensorflow-cpu
  for package in [
      "tensorflow",
      "tensorflow-cpu",
      "tensorflow-gpu",
      "tf-nightly",
      "tf-nightly-cpu",
      "tf-nightly-gpu",
      "intel-tensorflow",
      "tensorflow-rocm",
      "tensorflow-macos",
  ]:
   try:
    a  :						List[Any]					       =							version.parse(importlib_metadata.version(package))
   except importlib_metadata.PackageNotFoundError:
    continue
   else:
    break
  else:
   a  :						Optional[int]					       =							False
 if TF_AVAILABLE:
  if TF_VERSION.major < 2:
   logger.info(F'''TensorFlow found but with version {TF_VERSION}. `datasets` requires version 2 minimum.''')
   a  :						Optional[Any]					       =							False
  else:
   logger.info(F'''TensorFlow version {TF_VERSION} available.''')
else:
 logger.info('''Disabling Tensorflow because USE_TORCH is set''')


a  :						List[str]					       =							'''N/A'''
a  :						List[str]					       =							False

if USE_JAX in ENV_VARS_TRUE_AND_AUTO_VALUES:
 a  :						List[str]					       =							importlib.util.find_spec('''jax''') is not None and importlib.util.find_spec('''jaxlib''') is not None
 if JAX_AVAILABLE:
  try:
   a  :						Any					       =							version.parse(importlib_metadata.version('''jax'''))
   logger.info(F'''JAX version {JAX_VERSION} available.''')
  except importlib_metadata.PackageNotFoundError:
   pass
else:
 logger.info('''Disabling JAX because USE_JAX is set to False''')


a  :						Dict					       =							os.environ.get('''USE_BEAM''', '''AUTO''').upper()
a  :						str					       =							'''N/A'''
a  :						List[str]					       =							False
if USE_BEAM in ENV_VARS_TRUE_AND_AUTO_VALUES:
 try:
  a  :						Tuple					       =							version.parse(importlib_metadata.version('''apache_beam'''))
  a  :						str					       =							True
  logger.info(F'''Apache Beam version {BEAM_VERSION} available.''')
 except importlib_metadata.PackageNotFoundError:
  pass
else:
 logger.info('''Disabling Apache Beam because USE_BEAM is set to False''')


# Optional tools for data loading
a  :						int					       =							importlib.util.find_spec('''sqlalchemy''') is not None

# Optional tools for feature decoding
a  :						Optional[Any]					       =							importlib.util.find_spec('''PIL''') is not None
a  :						Tuple					       =							importlib.util.find_spec('''soundfile''') is not None and version.parse(
    importlib.import_module('''soundfile''').__libsndfile_version__
) >= version.parse('''1.0.31''')
a  :						Dict					       =							importlib.util.find_spec('''soundfile''') is not None and version.parse(
    importlib.import_module('''soundfile''').__libsndfile_version__
) >= version.parse('''1.1.0''')

# Optional compression tools
a  :						str					       =							importlib.util.find_spec('''rarfile''') is not None
a  :						Optional[int]					       =							importlib.util.find_spec('''zstandard''') is not None
a  :						Union[str, Any]					       =							importlib.util.find_spec('''lz4''') is not None
a  :						Any					       =							importlib.util.find_spec('''py7zr''') is not None


# Cache location
a  :						Union[str, Any]					       =							'''~/.cache'''
a  :						Union[str, Any]					       =							os.getenv('''XDG_CACHE_HOME''', DEFAULT_XDG_CACHE_HOME)
a  :						Dict					       =							os.path.join(XDG_CACHE_HOME, '''huggingface''')
a  :						List[Any]					       =							os.path.expanduser(os.getenv('''HF_HOME''', DEFAULT_HF_CACHE_HOME))

a  :						Any					       =							os.path.join(HF_CACHE_HOME, '''datasets''')
a  :						Union[str, Any]					       =							Path(os.getenv('''HF_DATASETS_CACHE''', DEFAULT_HF_DATASETS_CACHE))

a  :						List[str]					       =							os.path.join(HF_CACHE_HOME, '''metrics''')
a  :						Tuple					       =							Path(os.getenv('''HF_METRICS_CACHE''', DEFAULT_HF_METRICS_CACHE))

a  :						Union[str, Any]					       =							os.path.join(HF_CACHE_HOME, '''modules''')
a  :						Tuple					       =							Path(os.getenv('''HF_MODULES_CACHE''', DEFAULT_HF_MODULES_CACHE))

a  :						Union[str, Any]					       =							'''downloads'''
a  :						Tuple					       =							os.path.join(HF_DATASETS_CACHE, DOWNLOADED_DATASETS_DIR)
a  :						Any					       =							Path(os.getenv('''HF_DATASETS_DOWNLOADED_DATASETS_PATH''', DEFAULT_DOWNLOADED_DATASETS_PATH))

a  :						int					       =							'''extracted'''
a  :						Tuple					       =							os.path.join(DEFAULT_DOWNLOADED_DATASETS_PATH, EXTRACTED_DATASETS_DIR)
a  :						Optional[int]					       =							Path(os.getenv('''HF_DATASETS_EXTRACTED_DATASETS_PATH''', DEFAULT_EXTRACTED_DATASETS_PATH))

# Download count for the website
a  :						Optional[int]					       =							(
    os.environ.get('''HF_UPDATE_DOWNLOAD_COUNTS''', '''AUTO''').upper() in ENV_VARS_TRUE_AND_AUTO_VALUES
)

# Batch size constants. For more info, see:
# https://github.com/apache/arrow/blob/master/docs/source/cpp/arrays.rst#size-limitations-and-recommendations)
a  :						List[Any]					       =							1000

# Size of the preloaded record batch in `Dataset.__iter__`
a  :						Dict					       =							10

# Pickling tables works only for small tables (<4GiB)
# For big tables, we write them on disk instead
a  :						Dict					       =							4 << 30

# Max shard size in bytes (e.g. to shard parquet datasets in push_to_hub or download_and_prepare)
a  :						str					       =							'''500MB'''

# Parquet configuration
a  :						Tuple					       =							100
a  :						Union[str, Any]					       =							100
a  :						List[Any]					       =							100

# Offline mode
a  :						Any					       =							os.environ.get('''HF_DATASETS_OFFLINE''', '''AUTO''').upper() in ENV_VARS_TRUE_VALUES

# In-memory
a  :						Tuple					       =							0  # Disabled
a  :						List[Any]					       =							float(os.environ.get('''HF_DATASETS_IN_MEMORY_MAX_SIZE''', DEFAULT_IN_MEMORY_MAX_SIZE))

# File names
a  :						List[Any]					       =							'''dataset.arrow'''
a  :						Dict					       =							'''indices.arrow'''
a  :						str					       =							'''state.json'''
a  :						List[Any]					       =							'''dataset_info.json'''
a  :						List[Any]					       =							'''dataset_infos.json'''
a  :						Any					       =							'''LICENSE'''
a  :						Optional[int]					       =							'''metric_info.json'''
a  :						Tuple					       =							'''dataset_dict.json'''

a  :						Dict					       =							'''datasets_modules'''

a  :						Tuple					       =							255

# Streaming
a  :						Optional[int]					       =							20
a  :						Dict					       =							5

# Datasets without script
a  :						List[Any]					       =							200
a  :						Union[str, Any]					       =							10
a  :						List[str]					       =							200

# Progress bars
a  :						Any					       =							0.05  # 20 progress updates per sec











"""simple docstring"""



import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
 import torch

 from transformers import GPTaLMHeadModel


@require_torch
@require_sentencepiece
@require_tokenizers
class 	_UpperCamelCase ( unittest.TestCase							):



    '''simple docstring'''


    @slow
    @unittest.skip("""Model is not available."""			)
    def 		A__      (      self			):
     UpperCAmelCase__ 						=				"""nvidia/megatron-gpt2-345m/"""
     if "MYDIR" in os.environ:
      UpperCAmelCase__ 						=				os.path.join(os.environ["""MYDIR"""]   ,			__lowercase			)
     UpperCAmelCase__ 						=				GPTaLMHeadModel.from_pretrained(__lowercase			)
     model.to(__lowercase			)
     model.half()

     UpperCAmelCase__ 						=				torch.tensor(
         [[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]   ,			device=__lowercase   ,			dtype=torch.long   ,			)

     with torch.no_grad():
      UpperCAmelCase__ 						=				model(__lowercase			).logits

     UpperCAmelCase__ 						=				torch.Size((1, 9, 50257)			)
     self.assertEqual(output.shape   ,			__lowercase			)

     UpperCAmelCase__ 						=				torch.tensor(
         [
             4.9_414,
             -0.2_920,
             -1.2_148,
             -4.0_273,
             -0.5_161,
             -5.2_109,
             -1.2_412,
             -1.8_301,
             -1.7_734,
             -4.7_148,
             -0.2_317,
             -1.0_811,
             -2.1_777,
             0.4_141,
             -3.7_969,
             -4.0_586,
             -2.5_332,
             -3.3_809,
             4.3_867,
         ]   ,			device=__lowercase   ,			dtype=torch.half   ,			)

     for i in range(19			):
      UpperCAmelCase__						,	UpperCAmelCase__ 						=				8 * i // 17, 2792 * i  # along the diagonal
      UpperCAmelCase__						,	UpperCAmelCase__ 						=				output[0, r, c], expected_diag[i]
      UpperCAmelCase__ 						=				F'''row={r} col={c} computed={computed} expected={expected}'''
      self.assertAlmostEqual(__lowercase   ,			__lowercase   ,			delta=1e-4   ,			msg=__lowercase			)



