



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


lowercase  							=					"\\n    Text data.\n    Second line of data."

lowercase  							=					"file"




@pytest.fixture(scope='session'       )
def 	UpperCAmelCase     (		A	:						str       ):

  '''simple docstring'''


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




@pytest.fixture
def 	UpperCAmelCase     (		A	:						Optional[int]       ):

  '''simple docstring'''


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




@pytest.mark.parametrize('compression_format'     ,				['gzip', 'xz', 'zstd']       )
def 	UpperCAmelCase     (		A	:						str     ,				A	:						Tuple     ,				A	:						Dict     ,				A	:						Optional[Any]     ,				A	:						Tuple     ,				A	:						Union[str, Any]       ):

  '''simple docstring'''


  _UpperCAmelCase							 =		{'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_path}
  _UpperCAmelCase							 =		input_paths[compression_format]
  _UpperCAmelCase							 =		tmp_path / '''cache'''
  _UpperCAmelCase							 =		DownloadConfig(cache_dir=a_     ,				extract_compressed_file=a_       )
  _UpperCAmelCase							 =		cached_path(a_     ,				download_config=a_       )
  with open(a_       ) as f:
    _UpperCAmelCase							 =		f.read()
  with open(a_       ) as f:
    _UpperCAmelCase							 =		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     (		A	:						List[Any]     ,				A	:						Optional[int]     ,				A	:						List[Any]     ,				A	:						Optional[int]     ,				A	:						Tuple       ):

  '''simple docstring'''


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

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




def 	UpperCAmelCase     (		A	:						Any       ):

  '''simple docstring'''


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




def 	UpperCAmelCase     (		A	:						Optional[int]       ):

  '''simple docstring'''


  _UpperCAmelCase							 =		str(tmp_path.resolve() / '__missing_file__.txt'       )
  with pytest.raises(a_       ):
    cached_path(a_       )
  # relative path
  _UpperCAmelCase							 =		'''./__missing_file__.txt'''
  with pytest.raises(a_       ):
    cached_path(a_       )




def 	UpperCAmelCase     (		A	:						Optional[int]       ):

  '''simple docstring'''


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




@patch('datasets.config.HF_DATASETS_OFFLINE'     ,				a_       )
def 	UpperCAmelCase     (		):

  '''simple docstring'''


  with pytest.raises(a_       ):
    cached_path('https://huggingface.co'       )




@patch('datasets.config.HF_DATASETS_OFFLINE'     ,				a_       )
def 	UpperCAmelCase     (		A	:						Dict       ):

  '''simple docstring'''


  _UpperCAmelCase							 =		tmp_path_factory.mktemp('data'       ) / '''file.html'''
  with pytest.raises(a_       ):
    http_get('https://huggingface.co'     ,				temp_file=a_       )
  with pytest.raises(a_       ):
    http_head('https://huggingface.co'       )




@patch('datasets.config.HF_DATASETS_OFFLINE'     ,				a_       )
def 	UpperCAmelCase     (		A	:						List[str]       ):

  '''simple docstring'''


  _UpperCAmelCase							 =		tmp_path_factory.mktemp('data'       ) / '''file.html'''
  with pytest.raises(a_       ):
    ftp_get('ftp://huggingface.co'     ,				temp_file=a_       )
  with pytest.raises(a_       ):
    ftp_head('ftp://huggingface.co'       )






@patch('datasets.config.HF_DATASETS_OFFLINE'     ,				a_       )
def 	UpperCAmelCase     (		A	:						str       ):

  '''simple docstring'''


  _UpperCAmelCase							 =		tmp_path_factory.mktemp('data'       ) / '''file.html'''
  with pytest.raises(a_       ):
    fsspec_get('s3://huggingface.co'     ,				temp_file=a_       )
  with pytest.raises(a_       ):
    fsspec_head('s3://huggingface.co'       )







"""simple docstring"""





import unittest

import numpy as np

from transformers.testing_utils import require_flax, require_tf, require_torch
from transformers.utils import (
    expand_dims,
    flatten_dict,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    reshape,
    squeeze,
    transpose,
)


if is_flax_available():
					import jax.numpy as jnp

if is_tf_available():
					import tensorflow as tf

if is_torch_available():
					import torch




class 			lowercase__			(      unittest.TestCase    ):
							'''simple docstring'''







							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		{
									    'task_specific_params': {
									        'summarization': {'length_penalty': 1.0, 'max_length': 128, 'min_length': 12, 'num_beams': 4},
									        'summarization_cnn': {'length_penalty': 2.0, 'max_length': 142, 'min_length': 56, 'num_beams': 4},
									        'summarization_xsum': {'length_penalty': 1.0, 'max_length': 62, 'min_length': 11, 'num_beams': 6},
									    }
									}
									_UpperCAmelCase							 =		{
									    'task_specific_params.summarization.length_penalty': 1.0,
									    'task_specific_params.summarization.max_length': 128,
									    'task_specific_params.summarization.min_length': 12,
									    'task_specific_params.summarization.num_beams': 4,
									    'task_specific_params.summarization_cnn.length_penalty': 2.0,
									    'task_specific_params.summarization_cnn.max_length': 142,
									    'task_specific_params.summarization_cnn.min_length': 56,
									    'task_specific_params.summarization_cnn.num_beams': 4,
									    'task_specific_params.summarization_xsum.length_penalty': 1.0,
									    'task_specific_params.summarization_xsum.max_length': 62,
									    'task_specific_params.summarization_xsum.min_length': 11,
									    'task_specific_params.summarization_xsum.num_beams': 6,
									}

									self.assertEqual(flatten_dict(snake_case						)	,       snake_case						)






							def        lowerCamelCase_   (  self						)			->							List[str]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       x.transpose()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       x.transpose((1, 2, 0)						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							List[str]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       transpose(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       transpose(snake_case	,       axes=(1, 2, 0)						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       transpose(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       transpose(snake_case	,       axes=(1, 2, 0)						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       np.asarray(transpose(snake_case						)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       np.asarray(transpose(snake_case	,       axes=(1, 2, 0)						)						)						)						)






							def        lowerCamelCase_   (  self						)			->							List[str]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       np.reshape(snake_case	,       (4, 3)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       np.reshape(snake_case	,       (12, 5)						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Optional[Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       reshape(snake_case	,       (4, 3)						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       reshape(snake_case	,       (12, 5)						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       reshape(snake_case	,       (4, 3)						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       reshape(snake_case	,       (12, 5)						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							Tuple:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       np.asarray(reshape(snake_case	,       (4, 3)						)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       np.asarray(reshape(snake_case	,       (12, 5)						)						)						)						)






							def        lowerCamelCase_   (  self						)			->							str:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       np.squeeze(snake_case						)						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       np.squeeze(snake_case	,       axis=2						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       squeeze(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       squeeze(snake_case	,       axis=2						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Optional[int]:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       squeeze(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       squeeze(snake_case	,       axis=2						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							str:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       np.asarray(squeeze(snake_case						)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       np.asarray(squeeze(snake_case	,       axis=2						)						)						)						)






							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       np.expand_dims(snake_case	,       axis=1						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       expand_dims(snake_case	,       axis=1						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							int:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       expand_dims(snake_case	,       axis=1						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							str:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       np.asarray(expand_dims(snake_case	,       axis=1						)						)						)						)


