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


__magic_name__     					=       """\
    Text data.
    Second line of data."""

__magic_name__     					=       """file"""





@pytest.fixture(scope='session'       )
def 	_lowerCamelCase			(			UpperCAmelCase__       )				->  Dict:


   '''simple docstring'''


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





@pytest.fixture
def 	_lowerCamelCase			(			UpperCAmelCase__       )				->  str:


   '''simple docstring'''


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





@pytest.mark.parametrize('compression_format',['gzip', 'xz', 'zstd']       )
def 	_lowerCamelCase			(			UpperCAmelCase__,UpperCAmelCase__,UpperCAmelCase__,UpperCAmelCase__,UpperCAmelCase__,UpperCAmelCase__       )				->  Any:


   '''simple docstring'''


   a__  					=		{"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_path}
   a__  					=		input_paths[compression_format]
   a__  					=		tmp_path / """cache"""
   a__  					=		DownloadConfig(cache_dir=lowercase_,extract_compressed_file=lowercase_       )
   a__  					=		cached_path(lowercase_,download_config=lowercase_       )
   with open(lowercase_       ) as f:
      a__  					=		f.read()
   with open(lowercase_       ) as f:
      a__  					=		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 	_lowerCamelCase			(			UpperCAmelCase__,UpperCAmelCase__,UpperCAmelCase__,UpperCAmelCase__,UpperCAmelCase__       )				->  int:


   '''simple docstring'''


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

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





def 	_lowerCamelCase			(			UpperCAmelCase__       )				->  Any:


   '''simple docstring'''


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





def 	_lowerCamelCase			(			UpperCAmelCase__       )				->  List[Any]:


   '''simple docstring'''


   a__  					=		str(tmp_path.resolve() / '__missing_file__.txt'       )
   with pytest.raises(lowercase_       ):
      cached_path(lowercase_       )
   # relative path
   a__  					=		"""./__missing_file__.txt"""
   with pytest.raises(lowercase_       ):
      cached_path(lowercase_       )





def 	_lowerCamelCase			(			UpperCAmelCase__       )				->  Any:


   '''simple docstring'''


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





@patch('datasets.config.HF_DATASETS_OFFLINE',lowercase_       )
def 	_lowerCamelCase			(			)				->  Optional[Any]:


   '''simple docstring'''


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





@patch('datasets.config.HF_DATASETS_OFFLINE',lowercase_       )
def 	_lowerCamelCase			(			UpperCAmelCase__       )				->  List[Any]:


   '''simple docstring'''


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





@patch('datasets.config.HF_DATASETS_OFFLINE',lowercase_       )
def 	_lowerCamelCase			(			UpperCAmelCase__       )				->  Optional[int]:


   '''simple docstring'''


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





@patch('datasets.config.HF_DATASETS_OFFLINE',lowercase_       )
def 	_lowerCamelCase			(			UpperCAmelCase__       )				->  str:


   '''simple docstring'''


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




import unittest

import torch
from torch import nn

from accelerate.test_utils import require_cuda
from accelerate.utils.memory import find_executable_batch_size, release_memory

def 		UpperCamelCase						(   )		->						List[Any]:


  '''simple docstring'''






  raise RuntimeError("""CUDA out of memory.""" )



class    _snake_case (						nn.Module				):


 def __init__(       self):


   '''simple docstring'''




   super().__init__()
   lowercase__	: Optional[Any]     		=   nn.Linear(3       ,	4)
   lowercase__	: Union[str, Any]     		=   nn.BatchNormad(4)
   lowercase__	: str     		=   nn.Linear(4       ,	5)



 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   return self.lineara(self.batchnorm(self.lineara(SCREAMING_SNAKE_CASE_)))



class    _snake_case (						unittest.TestCase				):


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: List[str]     		=   []

   @find_executable_batch_size(starting_batch_size=1_28)
   def mock_training_loop_function(SCREAMING_SNAKE_CASE_):
     nonlocal batch_sizes
     batch_sizes.append(SCREAMING_SNAKE_CASE_)
     if batch_size != 8:
       raise_fake_out_of_memory()

   mock_training_loop_function()
   self.assertListEqual(SCREAMING_SNAKE_CASE_       ,	[1_28, 64, 32, 16, 8])


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: int     		=   []

   @find_executable_batch_size(starting_batch_size=1_28)
   def mock_training_loop_function(SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_):
     nonlocal batch_sizes
     batch_sizes.append(SCREAMING_SNAKE_CASE_)
     if batch_size != 8:
       raise_fake_out_of_memory()
     return batch_size, arga

   lowercase__		,     lowercase__	: int     		=   mock_training_loop_function("""hello""")
   self.assertListEqual(SCREAMING_SNAKE_CASE_       ,	[1_28, 64, 32, 16, 8])
   self.assertListEqual([bs, arga]       ,	[8, """hello"""])


 def   lowercase__  (       self):


   '''simple docstring'''




   @find_executable_batch_size(starting_batch_size=0)
   def mock_training_loop_function(SCREAMING_SNAKE_CASE_):
     pass

   with self.assertRaises(SCREAMING_SNAKE_CASE_) as cm:
     mock_training_loop_function()
     self.assertIn("""No executable batch size found, reached zero."""       ,	cm.exception.args[0])


 def   lowercase__  (       self):


   '''simple docstring'''




   @find_executable_batch_size(starting_batch_size=16)
   def mock_training_loop_function(SCREAMING_SNAKE_CASE_):
     if batch_size > 0:
       raise_fake_out_of_memory()
     pass

   with self.assertRaises(SCREAMING_SNAKE_CASE_) as cm:
     mock_training_loop_function()
     self.assertIn("""No executable batch size found, reached zero."""       ,	cm.exception.args[0])


 def   lowercase__  (       self):


   '''simple docstring'''




   @find_executable_batch_size(starting_batch_size=1_28)
   def mock_training_loop_function(SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_):
     if batch_size != 8:
       raise raise_fake_out_of_memory()

   with self.assertRaises(SCREAMING_SNAKE_CASE_) as cm:
     mock_training_loop_function(1_28       ,	"""hello"""       ,	"""world""")
     self.assertIn("""Batch size was passed into `f`"""       ,	cm.exception.args[0])
     self.assertIn("""`f(arg1='hello', arg2='world')"""       ,	cm.exception.args[0])


 def   lowercase__  (       self):


   '''simple docstring'''




   @find_executable_batch_size(starting_batch_size=16)
   def mock_training_loop_function(SCREAMING_SNAKE_CASE_):
     raise ValueError("""Oops, we had an error!""")

   with self.assertRaises(SCREAMING_SNAKE_CASE_) as cm:
     mock_training_loop_function()
     self.assertIn("""Oops, we had an error!"""       ,	cm.exception.args[0])



 @require_cuda
 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: str     		=   torch.cuda.memory_allocated()
   lowercase__	: str     		=   ModelForTest()
   model.cuda()
   self.assertGreater(torch.cuda.memory_allocated()       ,	SCREAMING_SNAKE_CASE_)
   lowercase__	: Tuple     		=   release_memory(SCREAMING_SNAKE_CASE_)
   self.assertEqual(torch.cuda.memory_allocated()       ,	SCREAMING_SNAKE_CASE_)






