



'''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   a_   (		)    ->  List[Any]:
      raise RuntimeError('CUDA out of memory.'				)



class  snake_case__						(    nn.Module				):
 def __init__(   self						:							List[str]  )      ->   Optional[int]:







       '''simple docstring'''
       super().__init__()
       __snake_case :   Optional[int]													=    nn.Linear(3      ,     4  )
       __snake_case :   List[str]													=    nn.BatchNormad(4  )
       __snake_case :   Optional[Any]													=    nn.Linear(4      ,     5  )





 def A_   (   self						:							Any      ,     __a						:							int  )      ->   Optional[Any]:







       '''simple docstring'''
       return self.lineara(self.batchnorm(self.lineara(__a  )  )  )



class  snake_case__						(    unittest.TestCase				):
 def A_   (   self						:							Union[str, Any]  )      ->   int:







       '''simple docstring'''
       __snake_case :   Optional[int]													=    []

       @find_executable_batch_size(starting_batch_size=128  )
       def mock_training_loop_function(__a						:							Union[str, Any]  ):
             nonlocal batch_sizes
             batch_sizes.append(__a  )
             if batch_size != 8:
                   raise_fake_out_of_memory()

       mock_training_loop_function()
       self.assertListEqual(__a      ,     [128, 64, 32, 16, 8]  )
 def A_   (   self						:							Union[str, Any]  )      ->   List[str]:







       '''simple docstring'''
       __snake_case :   Any													=    []

       @find_executable_batch_size(starting_batch_size=128  )
       def mock_training_loop_function(__a						:							int      ,     __a						:							int  ):
             nonlocal batch_sizes
             batch_sizes.append(__a  )
             if batch_size != 8:
                   raise_fake_out_of_memory()
             return batch_size, arga

       __snake_case							,     __snake_case :   Union[str, Any]													=    mock_training_loop_function('hello'  )
       self.assertListEqual(__a      ,     [128, 64, 32, 16, 8]  )
       self.assertListEqual([bs, arga]      ,     [8, 'hello']  )
 def A_   (   self						:							Any  )      ->   Union[str, Any]:







       '''simple docstring'''
       @find_executable_batch_size(starting_batch_size=0  )
       def mock_training_loop_function(__a						:							Dict  ):
             pass

       with self.assertRaises(__a  ) as cm:
             mock_training_loop_function()
             self.assertIn('No executable batch size found, reached zero.'      ,     cm.exception.args[0]  )
 def A_   (   self						:							Union[str, Any]  )      ->   str:







       '''simple docstring'''
       @find_executable_batch_size(starting_batch_size=16  )
       def mock_training_loop_function(__a						:							Any  ):
             if batch_size > 0:
                   raise_fake_out_of_memory()
             pass

       with self.assertRaises(__a  ) as cm:
             mock_training_loop_function()
             self.assertIn('No executable batch size found, reached zero.'      ,     cm.exception.args[0]  )
 def A_   (   self						:							int  )      ->   Union[str, Any]:







       '''simple docstring'''
       @find_executable_batch_size(starting_batch_size=128  )
       def mock_training_loop_function(__a						:							Optional[Any]      ,     __a						:							Tuple      ,     __a						:							Optional[Any]  ):
             if batch_size != 8:
                   raise raise_fake_out_of_memory()

       with self.assertRaises(__a  ) as cm:
             mock_training_loop_function(128      ,     '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 A_   (   self						:							Tuple  )      ->   Any:







       '''simple docstring'''
       @find_executable_batch_size(starting_batch_size=16  )
       def mock_training_loop_function(__a						:							List[Any]  ):
             raise ValueError('Oops, we had an error!'  )

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





 @require_cuda
 def A_   (   self						:							int  )      ->   Union[str, Any]:







       '''simple docstring'''
       __snake_case :   Union[str, Any]													=    torch.cuda.memory_allocated()
       __snake_case :   List[str]													=    ModelForTest()
       model.cuda()
       self.assertGreater(torch.cuda.memory_allocated()      ,     __a  )
       __snake_case :   Optional[Any]													=    release_memory(__a  )
       self.assertEqual(torch.cuda.memory_allocated()      ,     __a  )








'''simple docstring'''





import json
import logging
import os
import sys
from time import time
from unittest.mock import patch

from transformers.testing_utils import TestCasePlus, require_torch_tpu


logging.basicConfig(level=logging.DEBUG)

A__    :   List[str]           =					logging.getLogger()





def   a_   (		_UpperCAmelCase    :      int				)    ->  Optional[int]:
      __snake_case :   List[str]													=    {}
      __snake_case :   List[str]													=    os.path.join(_UpperCAmelCase ,'all_results.json'				)
      if os.path.exists(_UpperCAmelCase				):
            with open(_UpperCAmelCase ,'r'				) as f:
                  __snake_case :   Dict													=    json.load(_UpperCAmelCase				)
      else:
            raise ValueError(f'''can\'t find {path}'''				)
      return results


A__    :   Dict           =					logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)





@require_torch_tpu
class  snake_case__						(    SCREAMING_SNAKE_CASE_				):
 def A_   (   self						:							Optional[Any]  )      ->   List[str]:







       '''simple docstring'''
       import xla_spawn

       __snake_case :   int													=    self.get_auto_remove_tmp_dir()
       __snake_case :   Tuple													=    f'''
            ./examples/pytorch/text-classification/run_glue.py
            --num_cores=8
            ./examples/pytorch/text-classification/run_glue.py
            --model_name_or_path distilbert-base-uncased
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv
            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv
            --do_train
            --do_eval
            --debug tpu_metrics_debug
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --learning_rate=1e-4
            --max_steps=10
            --warmup_steps=2
            --seed=42
            --max_seq_length=128
            '''.split()

       with patch.object(__a      ,     'argv'      ,     __a  ):
             __snake_case :   Tuple													=    time()
             xla_spawn.main()
             __snake_case :   Optional[int]													=    time()

             __snake_case :   List[Any]													=    get_results(__a  )
             self.assertGreaterEqual(result['eval_accuracy']      ,     0.7_5  )

             # Assert that the script takes less than 500 seconds to make sure it doesn't hang.
             self.assertLess(end - start      ,     500  )





 def A_   (   self						:							List[str]  )      ->   Optional[int]:







       '''simple docstring'''
       import xla_spawn

       __snake_case :   Optional[Any]													=    '\n            ./tests/test_trainer_tpu.py\n            --num_cores=8\n            ./tests/test_trainer_tpu.py\n            '.split()
       with patch.object(__a      ,     'argv'      ,     __a  ):
             xla_spawn.main()



