


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__		():



     """simple docstring"""






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

class     A__   (       nn.Module      ):







    """simple docstring"""

    def __init__(			self       ):
         super().__init__()
         snake_case											=							nn.Linear(3     ,   4       )
         snake_case											=							nn.BatchNormad(4       )
         snake_case											=							nn.Linear(4     ,   5       )




    def 						a_     (			self     ,   __snake_case       ):
         return self.lineara(self.batchnorm(self.lineara(__snake_case       )       )       )






class     A__   (       unittest.TestCase      ):







    """simple docstring"""

    def 						a_     (			self       ):
         snake_case											=							[]

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

         mock_training_loop_function()
         self.assertListEqual(__snake_case     ,   [1_2_8, 6_4, 3_2, 1_6, 8]       )
    def 						a_     (			self       ):
         snake_case											=							[]

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

         snake_case					,    snake_case											=							mock_training_loop_function('''hello'''       )
         self.assertListEqual(__snake_case     ,   [1_2_8, 6_4, 3_2, 1_6, 8]       )
         self.assertListEqual([bs, arga]     ,   [8, '''hello''']       )
    def 						a_     (			self       ):
         @find_executable_batch_size(starting_batch_size=0       )
         def mock_training_loop_function(__snake_case       ):
              pass

         with self.assertRaises(__snake_case       ) as cm:
              mock_training_loop_function()
              self.assertIn('''No executable batch size found, reached zero.'''     ,   cm.exception.args[0]       )
    def 						a_     (			self       ):
         @find_executable_batch_size(starting_batch_size=1_6       )
         def mock_training_loop_function(__snake_case       ):
              if batch_size > 0:
                   raise_fake_out_of_memory()
              pass

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

         with self.assertRaises(__snake_case       ) as cm:
              mock_training_loop_function(1_2_8     ,   '''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       ):
         @find_executable_batch_size(starting_batch_size=1_6       )
         def mock_training_loop_function(__snake_case       ):
              raise ValueError('''Oops, we had an error!'''       )

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




    @require_cuda
    def 						a_     (			self       ):
         snake_case											=							torch.cuda.memory_allocated()
         snake_case											=							ModelForTest()
         model.cuda()
         self.assertGreater(torch.cuda.memory_allocated()     ,   __snake_case       )
         snake_case											=							release_memory(__snake_case       )
         self.assertEqual(torch.cuda.memory_allocated()     ,   __snake_case       )








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available


_SCREAMING_SNAKE_CASE     :					Optional[int]      =       {"configuration_speech_encoder_decoder": ["SpeechEncoderDecoderConfig"]}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _SCREAMING_SNAKE_CASE     :					Optional[int]      =       ["SpeechEncoderDecoderModel"]

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _SCREAMING_SNAKE_CASE     :					Dict      =       ["FlaxSpeechEncoderDecoderModel"]

if TYPE_CHECKING:
 from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel

else:
 import sys

 _SCREAMING_SNAKE_CASE     :					List[Any]      =       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




