





'''simple docstring'''




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 				__snake_case     (					nn.Module							):





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





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		List[str]:
		return self.lineara(self.batchnorm(self.lineara(UpperCamelCase_		)		)		)






class 				__snake_case     (					unittest.TestCase							):





	def   _snake_case   (		self		)      ->		Optional[int]:
		snake_case__	       =   ModelForTest()
		with TemporaryDirectory() as tmp_dir:
			offload_state_dict(UpperCamelCase_      ,       model.state_dict()		)
			snake_case__	       =   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"]:
				snake_case__	       =   os.path.join(UpperCamelCase_      ,       F'''{key}.dat'''		)
				self.assertTrue(os.path.isfile(UpperCamelCase_		)		)
				# TODO: add tests on the fact weights are properly loaded





	def   _snake_case   (		self		)      ->		str:
		snake_case__	       =   [torch.floataa, torch.floataa, torch.bfloataa]

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

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





	def   _snake_case   (		self		)      ->		Any:
		snake_case__	       =   ModelForTest()
		snake_case__	       =   model.state_dict()
		snake_case__	       =   {k: v for k, v in state_dict.items() if 'linear2' not in k}
		snake_case__	       =   {k: v for k, v in state_dict.items() if 'linear2' in k}

		with TemporaryDirectory() as tmp_dir:
			offload_state_dict(UpperCamelCase_      ,       UpperCamelCase_		)
			snake_case__	       =   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]		)		)

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

		with TemporaryDirectory() as tmp_dir:
			offload_state_dict(UpperCamelCase_      ,       UpperCamelCase_		)
			snake_case__	       =   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
			snake_case__	       =   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   _snake_case   (		self		)      ->		Any:
		snake_case__	       =   {'a.1': 0, 'a.10': 1, 'a.2': 2}
		snake_case__	       =   extract_submodules_state_dict(UpperCamelCase_      ,       ['a.1', 'a.2']		)
		self.assertDictEqual(UpperCamelCase_      ,       {'a.1': 0, 'a.2': 2}		)

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










'''simple docstring'''




import enum
import shutil
import sys


a__      ,       a__					:			Any          =     shutil.get_terminal_size()

a__					:			Optional[int]          =     {'''UP''': '''A''', '''DOWN''': '''B''', '''RIGHT''': '''C''', '''LEFT''': '''D'''}






class 				__snake_case     (					enum.Enum							):
	__lowerCAmelCase       						=  0
	__lowerCAmelCase       						=  1





def 						__lowerCamelCase			(	UpperCAmelCase_					,				UpperCAmelCase_=""							)   ->Optional[Any]:
	sys.stdout.write(str(UpperCAmelCase_							) + end							)
	sys.stdout.flush()




def 						__lowerCamelCase			(	UpperCAmelCase_					,				UpperCAmelCase_					,				UpperCAmelCase_=""							)   ->List[str]:
	forceWrite(f'''\u001b[{color}m{content}\u001b[0m'''					,				UpperCAmelCase_							)




def 						__lowerCamelCase			(	)   ->Optional[Any]:
	forceWrite('\r'							)




def 						__lowerCamelCase			(	UpperCAmelCase_					,				UpperCAmelCase_							)   ->Any:
	forceWrite(f'''\033[{num_lines}{CURSOR_TO_CHAR[direction.upper()]}'''							)




def 						__lowerCamelCase			(	)   ->str:
	forceWrite(' ' * TERMINAL_WIDTH							)
	reset_cursor()







def 						__lowerCamelCase			(	)   ->Tuple:
	reset_cursor()
	forceWrite('-' * TERMINAL_WIDTH							)



