

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

  def __init__(					self):
      super().__init__()
      SCREAMING_SNAKE_CASE_									=						nn.Linear(3						,		4)
      SCREAMING_SNAKE_CASE_									=						nn.BatchNormad(4)
      SCREAMING_SNAKE_CASE_									=						nn.Linear(4						,		5)


  def 						lowerCAmelCase__     (					self						,		_A):
      return self.lineara(self.batchnorm(self.lineara(_A)))






class       __snake_case  (    unittest.TestCase       ):

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						ModelForTest()
      with TemporaryDirectory() as tmp_dir:
          offload_state_dict(_A						,		model.state_dict())
          SCREAMING_SNAKE_CASE_									=						os.path.join(_A						,		'index.json')
          self.assertTrue(os.path.isfile(_A))
          # TODO: add tests on what is inside the index

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

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						[torch.floataa, torch.floataa, torch.bfloataa]

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

              SCREAMING_SNAKE_CASE_									=						load_offloaded_weight(_A						,		index['weight'])
              self.assertTrue(torch.equal(_A						,		_A))

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						ModelForTest()
      SCREAMING_SNAKE_CASE_									=						model.state_dict()
      SCREAMING_SNAKE_CASE_									=						{k: v for k, v in state_dict.items() if 'linear2' not in k}
      SCREAMING_SNAKE_CASE_									=						{k: v for k, v in state_dict.items() if 'linear2' in k}

      with TemporaryDirectory() as tmp_dir:
          offload_state_dict(_A						,		_A)
          SCREAMING_SNAKE_CASE_									=						OffloadedWeightsLoader(state_dict=_A						,		save_folder=_A)

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

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

      with TemporaryDirectory() as tmp_dir:
          offload_state_dict(_A						,		_A)
          SCREAMING_SNAKE_CASE_									=						OffloadedWeightsLoader(state_dict=_A						,		save_folder=_A)

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

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

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


  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						{'a.1': 0, 'a.10': 1, 'a.2': 2}
      SCREAMING_SNAKE_CASE_									=						extract_submodules_state_dict(_A						,		['a.1', 'a.2'])
      self.assertDictEqual(_A						,		{'a.1': 0, 'a.2': 2})

      SCREAMING_SNAKE_CASE_									=						{'a.1.a': 0, 'a.10.a': 1, 'a.2.a': 2}
      SCREAMING_SNAKE_CASE_									=						extract_submodules_state_dict(_A						,		['a.1', 'a.2'])
      self.assertDictEqual(_A						,		{'a.1.a': 0, 'a.2.a': 2})





def 					_UpperCAmelCase       (  ):





    """simple docstring"""


    for n in range(1 ,   1_000_000       ):
        yield n * (n + 1) // 2


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Tuple       ):





    """simple docstring"""


    SCREAMING_SNAKE_CASE_									=						1
    SCREAMING_SNAKE_CASE_									=						2
    while i * i <= n:
        SCREAMING_SNAKE_CASE_									=						0
        while n % i == 0:
            n //= i
            multiplicity += 1
        divisors_count *= multiplicity + 1
        i += 1
    if n > 1:
        divisors_count *= 2
    return divisors_count


def 					_UpperCAmelCase       (  ):





    """simple docstring"""


    return next(i for i in triangle_number_generator() if count_divisors(_SCREAMING_SNAKE_CASE       ) > 500       )


if __name__ == "__main__":
       print(solution())


