



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





	def __init__(					self   :							Optional[int]					):







							'''simple docstring'''
							super().__init__()
							_UpperCamelCase   :						str												=  nn.Linear(3					,4					)
							_UpperCamelCase   :						Optional[int]												=  nn.BatchNormad(4					)
							_UpperCamelCase   :						Dict												=  nn.Linear(4					,5					)





	def       UpperCamelCase_    (					self   :							Any					,lowerCamelCase__   :							Dict					):







							'''simple docstring'''
							return self.lineara(self.batchnorm(self.lineara(lowerCamelCase__					)					)					)






class 				lowercase__      (				unittest.TestCase  ):





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  ModelForTest()
							with TemporaryDirectory() as tmp_dir:
													offload_state_dict(lowerCamelCase__					,model.state_dict()					)
													_UpperCamelCase   :						int												=  os.path.join(lowerCamelCase__					,'index.json'					)
													self.assertTrue(os.path.isfile(lowerCamelCase__					)					)
													# TODO: add tests on what is inside the index

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





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  [torch.floataa, torch.floataa, torch.bfloataa]

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

																			_UpperCamelCase   :						int												=  load_offloaded_weight(lowerCamelCase__					,index['weight']					)
																			self.assertTrue(torch.equal(lowerCamelCase__					,lowerCamelCase__					)					)





	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							_UpperCamelCase   :						str												=  ModelForTest()
							_UpperCamelCase   :						List[Any]												=  model.state_dict()
							_UpperCamelCase   :						Optional[Any]												=  {k: v for k, v in state_dict.items() if 'linear2' not in k}
							_UpperCamelCase   :						str												=  {k: v for k, v in state_dict.items() if 'linear2' in k}

							with TemporaryDirectory() as tmp_dir:
													offload_state_dict(lowerCamelCase__					,lowerCamelCase__					)
													_UpperCamelCase   :						Optional[Any]												=  OffloadedWeightsLoader(state_dict=lowerCamelCase__					,save_folder=lowerCamelCase__					)

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

							_UpperCamelCase   :						Dict												=  {k: v for k, v in state_dict.items() if 'weight' in k}
							_UpperCamelCase   :						List[str]												=  {k: v for k, v in state_dict.items() if 'weight' not in k}

							with TemporaryDirectory() as tmp_dir:
													offload_state_dict(lowerCamelCase__					,lowerCamelCase__					)
													_UpperCamelCase   :						List[Any]												=  OffloadedWeightsLoader(state_dict=lowerCamelCase__					,save_folder=lowerCamelCase__					)

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

							with TemporaryDirectory() as tmp_dir:
													offload_state_dict(lowerCamelCase__					,lowerCamelCase__					)
													# Duplicates are removed
													_UpperCamelCase   :						int												=  OffloadedWeightsLoader(state_dict=lowerCamelCase__					,save_folder=lowerCamelCase__					)

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





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  {'a.1': 0, 'a.10': 1, 'a.2': 2}
							_UpperCamelCase   :						Dict												=  extract_submodules_state_dict(lowerCamelCase__					,['a.1', 'a.2']					)
							self.assertDictEqual(lowerCamelCase__					,{'a.1': 0, 'a.2': 2}					)

							_UpperCamelCase   :						List[Any]												=  {'a.1.a': 0, 'a.10.a': 1, 'a.2.a': 2}
							_UpperCamelCase   :						Optional[int]												=  extract_submodules_state_dict(lowerCamelCase__					,['a.1', 'a.2']					)
							self.assertDictEqual(lowerCamelCase__					,{'a.1.a': 0, 'a.2.a': 2}					)







'''simple docstring'''
def A__       (	UpperCAmelCase_    ,    UpperCAmelCase_				):
						return int((input_a, input_a).count(0				) != 0				)






def A__       (	):
						assert nand_gate(0    ,    0				) == 1
						assert nand_gate(0    ,    1				) == 1
						assert nand_gate(1    ,    0				) == 1
						assert nand_gate(1    ,    1				) == 0


if __name__ == "__main__":
					print(nand_gate(0, 0))
					print(nand_gate(0, 1))
					print(nand_gate(1, 0))
					print(nand_gate(1, 1))


