


import os
import unittest
from tempfile import TemporaryDirectory

import torch
import torch.nn as nn

from accelerate.utils import (
    OffloadedWeightsLoader,
    extract_submodules_state_dict,
    load_offloaded_weight,
    offload_state_dict,
    offload_weight,
)


class 	_a							(      nn.Module      ):

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





									"""simple docstring"""







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


							def 				lowerCamelCase_			(							self:     Tuple   ,						UpperCamelCase_:     List[str]							)      ->       str:





									"""simple docstring"""







									return self.lineara(self.batchnorm(self.lineara(UpperCamelCase_							)							)							)


class 	_a							(      unittest.TestCase      ):

							def 				lowerCamelCase_			(							self:     str							)      ->       int:





									"""simple docstring"""







									lowercase__						=				ModelForTest()
									with TemporaryDirectory() as tmp_dir:
											offload_state_dict(UpperCamelCase_   ,						model.state_dict()							)
											lowercase__						=				os.path.join(UpperCamelCase_   ,						'''index.json'''							)
											self.assertTrue(os.path.isfile(UpperCamelCase_							)							)
											# TODO: add tests on what is inside the index

											for key in ["linear1.weight", "linear1.bias", "linear2.weight", "linear2.bias"]:
													lowercase__						=				os.path.join(UpperCamelCase_   ,						f'{key}.dat'							)
													self.assertTrue(os.path.isfile(UpperCamelCase_							)							)
													# TODO: add tests on the fact weights are properly loaded

							def 				lowerCamelCase_			(							self:     Any							)      ->       List[str]:





									"""simple docstring"""







									lowercase__						=				[torch.floataa, torch.floataa, torch.bfloataa]

									for dtype in dtypes:
											lowercase__						=				torch.randn(2   ,						3   ,						dtype=UpperCamelCase_							)
											with TemporaryDirectory() as tmp_dir:
													lowercase__						=				offload_weight(UpperCamelCase_   ,						'''weight'''   ,						UpperCamelCase_   ,						{}							)
													lowercase__						=				os.path.join(UpperCamelCase_   ,						'''weight.dat'''							)
													self.assertTrue(os.path.isfile(UpperCamelCase_							)							)
													self.assertDictEqual(UpperCamelCase_   ,						{'''weight''': {'''shape''': [2, 3], '''dtype''': str(UpperCamelCase_							).split('''.'''							)[1]}}							)

													lowercase__						=				load_offloaded_weight(UpperCamelCase_   ,						index['''weight''']							)
													self.assertTrue(torch.equal(UpperCamelCase_   ,						UpperCamelCase_							)							)

							def 				lowerCamelCase_			(							self:     int							)      ->       Any:





									"""simple docstring"""







									lowercase__						=				ModelForTest()
									lowercase__						=				model.state_dict()
									lowercase__						=				{k: v for k, v in state_dict.items() if '''linear2''' not in k}
									lowercase__						=				{k: v for k, v in state_dict.items() if '''linear2''' in k}

									with TemporaryDirectory() as tmp_dir:
											offload_state_dict(UpperCamelCase_   ,						UpperCamelCase_							)
											lowercase__						=				OffloadedWeightsLoader(state_dict=UpperCamelCase_   ,						save_folder=UpperCamelCase_							)

											# Every key is there with the right value
											self.assertEqual(sorted(UpperCamelCase_							)   ,						sorted(state_dict.keys()							)							)
											for key, param in state_dict.items():
													self.assertTrue(torch.allclose(UpperCamelCase_   ,						weight_map[key]							)							)

									lowercase__						=				{k: v for k, v in state_dict.items() if '''weight''' in k}
									lowercase__						=				{k: v for k, v in state_dict.items() if '''weight''' not in k}

									with TemporaryDirectory() as tmp_dir:
											offload_state_dict(UpperCamelCase_   ,						UpperCamelCase_							)
											lowercase__						=				OffloadedWeightsLoader(state_dict=UpperCamelCase_   ,						save_folder=UpperCamelCase_							)

											# Every key is there with the right value
											self.assertEqual(sorted(UpperCamelCase_							)   ,						sorted(state_dict.keys()							)							)
											for key, param in state_dict.items():
													self.assertTrue(torch.allclose(UpperCamelCase_   ,						weight_map[key]							)							)

									with TemporaryDirectory() as tmp_dir:
											offload_state_dict(UpperCamelCase_   ,						UpperCamelCase_							)
											# Duplicates are removed
											lowercase__						=				OffloadedWeightsLoader(state_dict=UpperCamelCase_   ,						save_folder=UpperCamelCase_							)

											# Every key is there with the right value
											self.assertEqual(sorted(UpperCamelCase_							)   ,						sorted(state_dict.keys()							)							)
											for key, param in state_dict.items():
													self.assertTrue(torch.allclose(UpperCamelCase_   ,						weight_map[key]							)							)


							def 				lowerCamelCase_			(							self:     Dict							)      ->       Union[str, Any]:





									"""simple docstring"""







									lowercase__						=				{'''a.1''': 0, '''a.10''': 1, '''a.2''': 2}
									lowercase__						=				extract_submodules_state_dict(UpperCamelCase_   ,						['''a.1''', '''a.2''']							)
									self.assertDictEqual(UpperCamelCase_   ,						{'''a.1''': 0, '''a.2''': 2}							)

									lowercase__						=				{'''a.1.a''': 0, '''a.10.a''': 1, '''a.2.a''': 2}
									lowercase__						=				extract_submodules_state_dict(UpperCamelCase_   ,						['''a.1''', '''a.2''']							)
									self.assertDictEqual(UpperCamelCase_   ,						{'''a.1.a''': 0, '''a.2.a''': 2}							)






def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		return base * power(SCREAMING_SNAKE_CASE						,    (exponent - 1)					) if exponent else 1


if __name__ == "__main__":
			print('Raise base to the power of exponent using recursion...')
			lowerCAmelCase	=							int(input('Enter the base: ').strip())
			lowerCAmelCase	=							int(input('Enter the exponent: ').strip())
			lowerCAmelCase	=							power(base, abs(exponent))
			if exponent < 0:  # power() does not properly deal w/ negative exponents
						lowerCAmelCase	=							1 / result
			print(f"""{base} to the power of {exponent} is {result}""")


