"""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      ):


    def __init__(    self		:     Optional[int]    ):
      super().__init__()
      _UpperCAmelCase         =					nn.Linear(3      , 4    )
      _UpperCAmelCase         =					nn.BatchNormad(4    )
      _UpperCAmelCase         =					nn.Linear(4      , 5    )


    def    lowerCAmelCase_     (    self		:     Union[str, Any]      , __lowerCAmelCase		:     str    ):
      return self.lineara(self.batchnorm(self.lineara(__lowerCAmelCase    )    )    )



class 				a (						unittest.TestCase      ):


    def    lowerCAmelCase_     (    self		:     str    ):
      _UpperCAmelCase         =					ModelForTest()
      with TemporaryDirectory() as tmp_dir:
        offload_state_dict(__lowerCAmelCase      , model.state_dict()    )
        _UpperCAmelCase         =					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         =					os.path.join(__lowerCAmelCase      , f'''{key}.dat'''    )
          self.assertTrue(os.path.isfile(__lowerCAmelCase    )    )
          # TODO: add tests on the fact weights are properly loaded


    def    lowerCAmelCase_     (    self		:     Optional[Any]    ):
      _UpperCAmelCase         =					[torch.floataa, torch.floataa, torch.bfloataa]

      for dtype in dtypes:
        _UpperCAmelCase         =					torch.randn(2      , 3      , dtype=__lowerCAmelCase    )
        with TemporaryDirectory() as tmp_dir:
          _UpperCAmelCase         =					offload_weight(__lowerCAmelCase      , """weight"""      , __lowerCAmelCase      , {}    )
          _UpperCAmelCase         =					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         =					load_offloaded_weight(__lowerCAmelCase      , index["""weight"""]    )
          self.assertTrue(torch.equal(__lowerCAmelCase      , __lowerCAmelCase    )    )


    def    lowerCAmelCase_     (    self		:     Any    ):
      _UpperCAmelCase         =					ModelForTest()
      _UpperCAmelCase         =					model.state_dict()
      _UpperCAmelCase         =					{k: v for k, v in state_dict.items() if """linear2""" not in k}
      _UpperCAmelCase         =					{k: v for k, v in state_dict.items() if """linear2""" in k}

      with TemporaryDirectory() as tmp_dir:
        offload_state_dict(__lowerCAmelCase      , __lowerCAmelCase    )
        _UpperCAmelCase         =					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         =					{k: v for k, v in state_dict.items() if """weight""" in k}
      _UpperCAmelCase         =					{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         =					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         =					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    lowerCAmelCase_     (    self		:     Optional[Any]    ):
      _UpperCAmelCase         =					{"""a.1""": 0, """a.10""": 1, """a.2""": 2}
      _UpperCAmelCase         =					extract_submodules_state_dict(__lowerCAmelCase      , ["""a.1""", """a.2"""]    )
      self.assertDictEqual(__lowerCAmelCase      , {"""a.1""": 0, """a.2""": 2}    )

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

"""simple docstring"""


import math






def 			__UpperCAmelCase    (	lowercase						):







  """simple docstring"""
  _UpperCAmelCase         =					[]
  _UpperCAmelCase         =					2
  _UpperCAmelCase         =					int(math.sqrt(lowercase						)						)  # Size of every segment
  _UpperCAmelCase         =					[True] * (end + 1)
  _UpperCAmelCase         =					[]

  while start <= end:
    if temp[start] is True:
      in_prime.append(lowercase						)
      for i in range(start * start      ,end + 1      ,lowercase						):
        _UpperCAmelCase         =					False
    start += 1
  prime += in_prime

  _UpperCAmelCase         =					end + 1
  _UpperCAmelCase         =					min(2 * end      ,lowercase						)

  while low <= n:
    _UpperCAmelCase         =					[True] * (high - low + 1)
    for each in in_prime:
      _UpperCAmelCase         =					math.floor(low / each						) * each
      if t < low:
        t += each

      for j in range(lowercase      ,high + 1      ,lowercase						):
        _UpperCAmelCase         =					False

    for j in range(len(lowercase						)						):
      if temp[j] is True:
        prime.append(j + low						)

    _UpperCAmelCase         =					high + 1
    _UpperCAmelCase         =					min(high + end      ,lowercase						)

  return prime

print(sieve(1_0**6))
