


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


     """simple docstring"""






     def __init__(       self  :	Union[str, Any]    )		->							Any:
          super().__init__()
          __UpperCAmelCase			:				Tuple													=    nn.Linear(3							,				4    )
          __UpperCAmelCase			:				List[Any]													=    nn.BatchNormad(4    )
          __UpperCAmelCase			:				Dict													=    nn.Linear(4							,				5    )






     def 				lowerCamelCase__				(       self  :	Tuple							,				snake_case  :	List[Any]    )		->							Any:
          return self.lineara(self.batchnorm(self.lineara(UpperCamelCase__    )    )    )



class 			a       (	unittest.TestCase ):


     """simple docstring"""






     def 				lowerCamelCase__				(       self  :	Any    )		->							Dict:
          __UpperCAmelCase			:				Dict													=    ModelForTest()
          with TemporaryDirectory() as tmp_dir:
               offload_state_dict(UpperCamelCase__							,				model.state_dict()    )
               __UpperCAmelCase			:				List[Any]													=    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"]:
                    __UpperCAmelCase			:				Optional[Any]													=    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  :	List[str]    )		->							Tuple:
          __UpperCAmelCase			:				int													=    [torch.floataa, torch.floataa, torch.bfloataa]

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

                    __UpperCAmelCase			:				Optional[Any]													=    load_offloaded_weight(UpperCamelCase__							,				index['''weight''']    )
                    self.assertTrue(torch.equal(UpperCamelCase__							,				UpperCamelCase__    )    )

     def 				lowerCamelCase__				(       self  :	Optional[int]    )		->							List[str]:
          __UpperCAmelCase			:				Tuple													=    ModelForTest()
          __UpperCAmelCase			:				Any													=    model.state_dict()
          __UpperCAmelCase			:				Tuple													=    {k: v for k, v in state_dict.items() if '''linear2''' not in k}
          __UpperCAmelCase			:				Tuple													=    {k: v for k, v in state_dict.items() if '''linear2''' in k}

          with TemporaryDirectory() as tmp_dir:
               offload_state_dict(UpperCamelCase__							,				UpperCamelCase__    )
               __UpperCAmelCase			:				Any													=    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]    )    )

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

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

          __UpperCAmelCase			:				Dict													=    {'''a.1.a''': 0, '''a.10.a''': 1, '''a.2.a''': 2}
          __UpperCAmelCase			:				Optional[int]													=    extract_submodules_state_dict(UpperCamelCase__							,				['''a.1''', '''a.2''']    )
          self.assertDictEqual(UpperCamelCase__							,				{'''a.1.a''': 0, '''a.2.a''': 2}    )



'''simple docstring'''



def 			_a			(		_lowercase      :			int = 50000000						):





     '''simple docstring'''






     __UpperCAmelCase			:				List[str]													=    set()
     __UpperCAmelCase			:				Tuple													=    int((limit - 24) ** (1 / 2)						)

     __UpperCAmelCase			:				Tuple													=    set(range(3    ,      prime_square_limit + 1    ,      2						)						)
     primes.add(2						)
     for p in range(3    ,      prime_square_limit + 1    ,      2						):
          if p not in primes:
               continue
          primes.difference_update(set(range(p * p    ,      prime_square_limit + 1    ,      _lowercase						)						)						)

     for primea in primes:
          __UpperCAmelCase			:				Any													=    primea * primea
          for primea in primes:
               __UpperCAmelCase			:				Any													=    primea * primea * primea
               if square + cube >= limit - 16:
                    break
               for primea in primes:
                    __UpperCAmelCase			:				Optional[Any]													=    primea * primea * primea * primea
                    __UpperCAmelCase			:				Union[str, Any]													=    square + cube + tetr
                    if total >= limit:
                         break
                    ret.add(_lowercase						)

     return len(_lowercase						)


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