



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



   def __init__(       self    :	Optional[int]				):



     """simple docstring"""
     super().__init__()
     _snake_case   	=      nn.Linear(3							,       4				)
     _snake_case   	=      nn.BatchNormad(4				)
     _snake_case   	=      nn.Linear(4							,       5				)





   def    __UpperCAmelCase    (       self    :	Tuple							,       __lowerCamelCase    :	Any				):



     """simple docstring"""
     return self.lineara(self.batchnorm(self.lineara(__UpperCAmelCase				)				)				)



class     UpperCAmelCase    (	unittest.TestCase  ):



   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



     """simple docstring"""
     _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    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     _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    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     _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    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     _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}				)













def     a_   (__A				,       __A				,       __A				,       __A      )				->						int:



					"""simple docstring"""
					__a      ,			__a					:						Any      =					len(__A      ), len(grid[0]      )
					if (
					    min(__A				,       __A      ) < 0
					    or row == row_length
					    or col == col_length
					    or (row, col) in visit
					    or grid[row][col] == 1
					):
										return 0
					if row == row_length - 1 and col == col_length - 1:
										return 1

					visit.add((row, col)      )

					__a					:						Dict      =					0
					count += depth_first_search(__A				,       row + 1				,       __A				,       __A      )
					count += depth_first_search(__A				,       row - 1				,       __A				,       __A      )
					count += depth_first_search(__A				,       __A				,       col + 1				,       __A      )
					count += depth_first_search(__A				,       __A				,       col - 1				,       __A      )

					visit.remove((row, col)      )
					return count


if __name__ == "__main__":
			import doctest

			doctest.testmod()





