





'''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]    )							-> Tuple:
            super().__init__()
            _lowerCAmelCase					=				nn.Linear(3     ,     4    )
            _lowerCAmelCase					=				nn.BatchNormad(4    )
            _lowerCAmelCase					=				nn.Linear(4     ,     5    )




      def 							lowercase__       (     self     :					List[Any]     ,     __snake_case     :					List[Any]    )							-> str:
            return self.lineara(self.batchnorm(self.lineara(__snake_case    )    )    )




class 					UpperCAmelCase	(     unittest.TestCase  ):




      def 							lowercase__       (     self     :					Any    )							-> Any:
            _lowerCAmelCase					=				ModelForTest()
            with TemporaryDirectory() as tmp_dir:
                  offload_state_dict(__snake_case     ,     model.state_dict()    )
                  _lowerCAmelCase					=				os.path.join(__snake_case     ,     """index.json"""    )
                  self.assertTrue(os.path.isfile(__snake_case    )    )
                  # TODO: add tests on what is inside the index

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




      def 							lowercase__       (     self     :					List[str]    )							-> Optional[Any]:
            _lowerCAmelCase					=				[torch.floataa, torch.floataa, torch.bfloataa]

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

                        _lowerCAmelCase					=				load_offloaded_weight(__snake_case     ,     index["""weight"""]    )
                        self.assertTrue(torch.equal(__snake_case     ,     __snake_case    )    )




      def 							lowercase__       (     self     :					Any    )							-> Dict:
            _lowerCAmelCase					=				ModelForTest()
            _lowerCAmelCase					=				model.state_dict()
            _lowerCAmelCase					=				{k: v for k, v in state_dict.items() if """linear2""" not in k}
            _lowerCAmelCase					=				{k: v for k, v in state_dict.items() if """linear2""" in k}

            with TemporaryDirectory() as tmp_dir:
                  offload_state_dict(__snake_case     ,     __snake_case    )
                  _lowerCAmelCase					=				OffloadedWeightsLoader(state_dict=__snake_case     ,     save_folder=__snake_case    )

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

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

            with TemporaryDirectory() as tmp_dir:
                  offload_state_dict(__snake_case     ,     __snake_case    )
                  _lowerCAmelCase					=				OffloadedWeightsLoader(state_dict=__snake_case     ,     save_folder=__snake_case    )

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

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

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




      def 							lowercase__       (     self     :					Union[str, Any]    )							-> Optional[Any]:
            _lowerCAmelCase					=				{"""a.1""": 0, """a.10""": 1, """a.2""": 2}
            _lowerCAmelCase					=				extract_submodules_state_dict(__snake_case     ,     ["""a.1""", """a.2"""]    )
            self.assertDictEqual(__snake_case     ,     {"""a.1""": 0, """a.2""": 2}    )

            _lowerCAmelCase					=				{"""a.1.a""": 0, """a.10.a""": 1, """a.2.a""": 2}
            _lowerCAmelCase					=				extract_submodules_state_dict(__snake_case     ,     ["""a.1""", """a.2"""]    )
            self.assertDictEqual(__snake_case     ,     {"""a.1.a""": 0, """a.2.a""": 2}    )













'''simple docstring'''






def   UpperCamelCase__ (						lowerCAmelCase    ):





      """simple docstring"""


      if divisor % 5 == 0 or divisor % 2 == 0:
            return 0
      _lowerCAmelCase					=				1
      _lowerCAmelCase					=				1
      while repunit:
            _lowerCAmelCase					=				(10 * repunit + 1) % divisor
            repunit_index += 1
      return repunit_index
def   UpperCamelCase__ (						lowerCAmelCase = 1_00_00_00    ):





      """simple docstring"""


      _lowerCAmelCase					=				limit - 1
      if divisor % 2 == 0:
            divisor += 1
      while least_divisible_repunit(lowerCAmelCase    ) <= limit:
            divisor += 2
      return divisor


if __name__ == "__main__":
      print(F"""{solution() = }""")






