


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

			'''simple docstring'''




			raise RuntimeError("CUDA out of memory."   )
class 						_UpperCamelCase						(     nn.Module	):
							'''simple docstring'''







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



							def     snake_case       (  self					,     __a			):
										return self.lineara(self.batchnorm(self.lineara(__a			)			)			)
class 						_UpperCamelCase						(     unittest.TestCase	):
							'''simple docstring'''







							def     snake_case       (  self			):
										__lowerCAmelCase       					=    []

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

										mock_training_loop_function()
										self.assertListEqual(__a					,     [1_28, 64, 32, 16, 8]			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    []

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

										__lowerCAmelCase					,			__lowerCAmelCase       					=    mock_training_loop_function("hello"			)
										self.assertListEqual(__a					,     [1_28, 64, 32, 16, 8]			)
										self.assertListEqual([bs, arga]					,     [8, "hello"]			)

							def     snake_case       (  self			):
										@find_executable_batch_size(starting_batch_size=0			)
										def mock_training_loop_function(__a			):
													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     snake_case       (  self			):
										@find_executable_batch_size(starting_batch_size=16			)
										def mock_training_loop_function(__a			):
													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     snake_case       (  self			):
										@find_executable_batch_size(starting_batch_size=1_28			)
										def mock_training_loop_function(__a					,     __a					,     __a			):
													if batch_size != 8:
																raise raise_fake_out_of_memory()

										with self.assertRaises(__a			) 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     snake_case       (  self			):
										@find_executable_batch_size(starting_batch_size=16			)
										def mock_training_loop_function(__a			):
													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     snake_case       (  self			):
										__lowerCAmelCase       					=    torch.cuda.memory_allocated()
										__lowerCAmelCase       					=    ModelForTest()
										model.cuda()
										self.assertGreater(torch.cuda.memory_allocated()					,     __a			)
										__lowerCAmelCase       					=    release_memory(__a			)
										self.assertEqual(torch.cuda.memory_allocated()					,     __a			)








"""simple docstring"""


A  :     List[Any]    					=    [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
A  :     Optional[int]    					=    [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
A  :     Optional[int]    					=    {
    0: "Sunday",
    1: "Monday",
    2: "Tuesday",
    3: "Wednesday",
    4: "Thursday",
    5: "Friday",
    6: "Saturday",
}


def _lowerCamelCase			(	_UpperCamelCase					,				_UpperCamelCase					,				_UpperCamelCase   ):

			'''simple docstring'''




			assert len(str(_UpperCamelCase   )   ) > 2, "year should be in YYYY format"
			assert 1 <= month <= 12, "month should be between 1 to 12"
			assert 1 <= day <= 31, "day should be between 1 to 31"

			# Doomsday algorithm:
			__lowerCAmelCase       					=    year // 100
			__lowerCAmelCase       					=    (5 * (century % 4) + 2) % 7
			__lowerCAmelCase       					=    year % 100
			__lowerCAmelCase       					=    centurian % 12
			__lowerCAmelCase       					=    (
			    (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor
			) % 7
			__lowerCAmelCase       					=    (
			    DOOMSDAY_NOT_LEAP[month - 1]
			    if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0)
			    else DOOMSDAY_LEAP[month - 1]
			)
			__lowerCAmelCase       					=    (dooms_day + day - day_anchor) % 7
			return WEEK_DAY_NAMES[week_day]


if __name__ == "__main__":
	import doctest

	doctest.testmod()




