




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


    """simple docstring"""







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

class 				snake_case_			(					nn.Module			):







   """simple docstring"""




   def __init__(		self)				->   Any:
       super().__init__()
       UpperCamelCase              =	nn.Linear(3       ,					4)
       UpperCamelCase              =	nn.BatchNormad(4)
       UpperCamelCase              =	nn.Linear(4       ,					5)







   def   UpperCAmelCase__			(		self       ,					lowerCamelCase_)				->   Union[str, Any]:
       return self.lineara(self.batchnorm(self.lineara(lowerCamelCase_)))

class 				snake_case_			(					unittest.TestCase			):







   """simple docstring"""




   def   UpperCAmelCase__			(		self)				->   List[Any]:
       UpperCamelCase              =	[]

       @find_executable_batch_size(starting_batch_size=1_2_8)
       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_2_8, 6_4, 3_2, 1_6, 8])







   def   UpperCAmelCase__			(		self)				->   Optional[Any]:
       UpperCamelCase              =	[]

       @find_executable_batch_size(starting_batch_size=1_2_8)
       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              =	mock_training_loop_function('''hello''')
       self.assertListEqual(lowerCamelCase_       ,					[1_2_8, 6_4, 3_2, 1_6, 8])
       self.assertListEqual([bs, arga]       ,					[8, '''hello'''])







   def   UpperCAmelCase__			(		self)				->   Tuple:
       @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)				->   List[Any]:
       @find_executable_batch_size(starting_batch_size=1_6)
       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)				->   Union[str, Any]:
       @find_executable_batch_size(starting_batch_size=1_2_8)
       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_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   UpperCAmelCase__			(		self)				->   Dict:
       @find_executable_batch_size(starting_batch_size=1_6)
       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)				->   Optional[int]:
       UpperCamelCase              =	torch.cuda.memory_allocated()
       UpperCamelCase              =	ModelForTest()
       model.cuda()
       self.assertGreater(torch.cuda.memory_allocated()       ,					lowerCamelCase_)
       UpperCamelCase              =	release_memory(lowerCamelCase_)
       self.assertEqual(torch.cuda.memory_allocated()       ,					lowerCamelCase_)


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_tf_available,
    is_torch_available,
)


A									=  {
    'configuration_speech_to_text': ['SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Speech2TextConfig'],
    'processing_speech_to_text': ['Speech2TextProcessor'],
}

try:
		if not is_sentencepiece_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A									=  ['Speech2TextTokenizer']

try:
		if not is_speech_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A									=  ['Speech2TextFeatureExtractor']

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A									=  [
		    'TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'TFSpeech2TextForConditionalGeneration',
		    'TFSpeech2TextModel',
		    'TFSpeech2TextPreTrainedModel',
		]

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A									=  [
		    'SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'Speech2TextForConditionalGeneration',
		    'Speech2TextModel',
		    'Speech2TextPreTrainedModel',
		]


if TYPE_CHECKING:
		from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig
		from .processing_speech_to_text import SpeechaTextProcessor

		try:
				if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_speech_to_text import SpeechaTextTokenizer

		try:
				if not is_speech_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_speech_to_text import (
				    TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFSpeechaTextForConditionalGeneration,
				    TFSpeechaTextModel,
				    TFSpeechaTextPreTrainedModel,
				)

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_speech_to_text import (
				    SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    SpeechaTextForConditionalGeneration,
				    SpeechaTextModel,
				    SpeechaTextPreTrainedModel,
				)

else:
		import sys

		A									=  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
