






'''simple docstring'''

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 							snake_case					(     )       ->				str:





      """simple docstring"""

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




class     A    (      nn.Module						):






 """simple docstring"""





 def __init__(							self ):
       super().__init__()
       UpperCamelCase_				:   Any					    =							nn.Linear(3			,     4 )
       UpperCamelCase_				:   Optional[int]					    =							nn.BatchNormad(4 )
       UpperCamelCase_				:   Tuple					    =							nn.Linear(4			,     5 )





 def   _UpperCAmelCase       (							self			,     __lowerCAmelCase ):
       return self.lineara(self.batchnorm(self.lineara(__lowerCAmelCase ) ) )






class     A    (      unittest.TestCase						):






 """simple docstring"""





 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   List[str]					    =							[]

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

       mock_training_loop_function()
       self.assertListEqual(__lowerCAmelCase			,     [1_28, 64, 32, 16, 8] )
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   str					    =							[]

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

       UpperCamelCase_    ,			UpperCamelCase_				:   List[Any]					    =							mock_training_loop_function("""hello""" )
       self.assertListEqual(__lowerCAmelCase			,     [1_28, 64, 32, 16, 8] )
       self.assertListEqual([bs, arga]			,     [8, """hello"""] )
 def   _UpperCAmelCase       (							self ):
       @find_executable_batch_size(starting_batch_size=0 )
       def mock_training_loop_function(__lowerCAmelCase ):
             pass

       with self.assertRaises(__lowerCAmelCase ) as cm:
             mock_training_loop_function()
             self.assertIn("""No executable batch size found, reached zero."""			,     cm.exception.args[0] )
 def   _UpperCAmelCase       (							self ):
       @find_executable_batch_size(starting_batch_size=16 )
       def mock_training_loop_function(__lowerCAmelCase ):
             if batch_size > 0:
                   raise_fake_out_of_memory()
             pass

       with self.assertRaises(__lowerCAmelCase ) as cm:
             mock_training_loop_function()
             self.assertIn("""No executable batch size found, reached zero."""			,     cm.exception.args[0] )
 def   _UpperCAmelCase       (							self ):
       @find_executable_batch_size(starting_batch_size=1_28 )
       def mock_training_loop_function(__lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase ):
             if batch_size != 8:
                   raise raise_fake_out_of_memory()

       with self.assertRaises(__lowerCAmelCase ) 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   _UpperCAmelCase       (							self ):
       @find_executable_batch_size(starting_batch_size=16 )
       def mock_training_loop_function(__lowerCAmelCase ):
             raise ValueError("""Oops, we had an error!""" )

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





 @require_cuda
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   List[Any]					    =							torch.cuda.memory_allocated()
       UpperCamelCase_				:   Any					    =							ModelForTest()
       model.cuda()
       self.assertGreater(torch.cuda.memory_allocated()			,     __lowerCAmelCase )
       UpperCamelCase_				:   Union[str, Any]					    =							release_memory(__lowerCAmelCase )
       self.assertEqual(torch.cuda.memory_allocated()			,     __lowerCAmelCase )














'''simple docstring'''

import pytest

import datasets


# Import fixture modules as plugins
UpperCamelCase							=["tests.fixtures.files", "tests.fixtures.hub", "tests.fixtures.fsspec"]



def 							snake_case					(     a_							: List[str]    ,   a_							: Optional[Any]	)       ->				Union[str, Any]:





      """simple docstring"""

      for item in items:
            if any(marker in item.keywords for marker in ["""integration""", """unit"""]	):
                  continue
            item.add_marker(pytest.mark.unit	)



def 							snake_case					(     a_							: Optional[int]	)       ->				Optional[Any]:





      """simple docstring"""

      config.addinivalue_line("""markers"""    ,   """torchaudio_latest: mark test to run with torchaudio>=0.12"""	)



@pytest.fixture(autouse=a_	)
def 							snake_case					(     a_							: Any    ,   a_							: Optional[int]	)       ->				Tuple:





      """simple docstring"""

      UpperCamelCase_				:   Optional[Any]					    =							tmp_path_factory.getbasetemp() / """cache"""
      UpperCamelCase_				:   str					    =							test_hf_cache_home / """datasets"""
      UpperCamelCase_				:   Any					    =							test_hf_cache_home / """metrics"""
      UpperCamelCase_				:   List[Any]					    =							test_hf_cache_home / """modules"""
      monkeypatch.setattr("""datasets.config.HF_DATASETS_CACHE"""    ,   str(a_	)	)
      monkeypatch.setattr("""datasets.config.HF_METRICS_CACHE"""    ,   str(a_	)	)
      monkeypatch.setattr("""datasets.config.HF_MODULES_CACHE"""    ,   str(a_	)	)
      UpperCamelCase_				:   List[Any]					    =							test_hf_datasets_cache / """downloads"""
      monkeypatch.setattr("""datasets.config.DOWNLOADED_DATASETS_PATH"""    ,   str(a_	)	)
      UpperCamelCase_				:   Any					    =							test_hf_datasets_cache / """downloads""" / """extracted"""
      monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_PATH"""    ,   str(a_	)	)



@pytest.fixture(autouse=a_    ,   scope="""session"""	)
def 							snake_case					(     )       ->				Any:





      """simple docstring"""

      datasets.disable_progress_bar()



@pytest.fixture(autouse=a_	)
def 							snake_case					(     a_							: Optional[int]	)       ->				Union[str, Any]:





      """simple docstring"""

      monkeypatch.setattr("""datasets.config.HF_UPDATE_DOWNLOAD_COUNTS"""    ,   a_	)



@pytest.fixture
def 							snake_case					(     a_							: int	)       ->				Union[str, Any]:





      """simple docstring"""

      monkeypatch.setattr("""sqlalchemy.util.deprecations.SILENCE_UBER_WARNING"""    ,   a_	)






