

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     lowerCAmelCase__							(		)	->					Optional[Any]:




				'''simple docstring'''





				raise RuntimeError("CUDA out of memory."					)

class     a__  (    nn.Module	):
			"""simple docstring"""







			def __init__(			self				)     ->  Dict:
							'''simple docstring'''






							super().__init__()
							A__						=	nn.Linear(3					,					4				)
							A__						=	nn.BatchNormad(4				)
							A__						=	nn.Linear(4					,					5				)


			def    UpperCamelCase						(			self					,					lowercase				)     ->  int:
							'''simple docstring'''






							return self.lineara(self.batchnorm(self.lineara(lowercase				)				)				)

class     a__  (    unittest.TestCase	):
			"""simple docstring"""







			def    UpperCamelCase						(			self				)     ->  Optional[int]:
							'''simple docstring'''






							A__						=	[]

							@find_executable_batch_size(starting_batch_size=128				)
							def mock_training_loop_function(lowercase				):
											nonlocal batch_sizes
											batch_sizes.append(lowercase				)
											if batch_size != 8:
															raise_fake_out_of_memory()

							mock_training_loop_function()
							self.assertListEqual(lowercase					,					[128, 64, 32, 16, 8]				)


			def    UpperCamelCase						(			self				)     ->  Union[str, Any]:
							'''simple docstring'''






							A__						=	[]

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

							A__			, A__						=	mock_training_loop_function("hello"				)
							self.assertListEqual(lowercase					,					[128, 64, 32, 16, 8]				)
							self.assertListEqual([bs, arga]					,					[8, "hello"]				)


			def    UpperCamelCase						(			self				)     ->  Optional[Any]:
							'''simple docstring'''






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

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


			def    UpperCamelCase						(			self				)     ->  List[str]:
							'''simple docstring'''






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

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


			def    UpperCamelCase						(			self				)     ->  List[str]:
							'''simple docstring'''






							@find_executable_batch_size(starting_batch_size=128				)
							def mock_training_loop_function(lowercase					,					lowercase					,					lowercase				):
											if batch_size != 8:
															raise raise_fake_out_of_memory()

							with self.assertRaises(lowercase				) 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    UpperCamelCase						(			self				)     ->  Dict:
							'''simple docstring'''






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

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


			@require_cuda
			def    UpperCamelCase						(			self				)     ->  Tuple:
							'''simple docstring'''






							A__						=	torch.cuda.memory_allocated()
							A__						=	ModelForTest()
							model.cuda()
							self.assertGreater(torch.cuda.memory_allocated()					,					lowercase				)
							A__						=	release_memory(lowercase				)
							self.assertEqual(torch.cuda.memory_allocated()					,					lowercase				)






def 		UpperCamelCase	(   snake_case__       :    int       ,     snake_case__       :    int	)	->     int:
   return int(input_a == input_a == 0	)
def 		UpperCamelCase	(   )	->     None:
   print('Truth Table of NOR Gate:'	)
   print('|   Input 1   |  Input 2  | Output |'	)
   print(F"""|      0      |    0      |  {nor_gate(0       ,     0	)}     |"""	)
   print(F"""|      0      |    1      |  {nor_gate(0       ,     1	)}     |"""	)
   print(F"""|      1      |    0      |  {nor_gate(1       ,     0	)}     |"""	)
   print(F"""|      1      |    1      |  {nor_gate(1       ,     1	)}     |"""	)


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    main()



