




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







 '''simple docstring'''





 def __init__(				self:		List[str]		):




   '''simple docstring'''



   super().__init__()
   _snake_case							:		Optional[Any]								=  nn.Linear(3,      4		)
   _snake_case							:		List[Any]								=  nn.BatchNormad(4		)
   _snake_case							:		Any								=  nn.Linear(4,      5		)


 def  UpperCamelCase_   (				self:		str,      a_:		Optional[Any]		):




   '''simple docstring'''



   return self.lineara(self.batchnorm(self.lineara(a_		)		)		)







class 					lowercase(     unittest.TestCase ):







 '''simple docstring'''





 def  UpperCamelCase_   (				self:		int		):




   '''simple docstring'''



   _snake_case							:		Union[str, Any]								=  ModelForTest()
   with TemporaryDirectory() as tmp_dir:
     offload_state_dict(a_,      model.state_dict()		)
     _snake_case							:		Dict								=  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"]:
       _snake_case							:		Tuple								=  os.path.join(a_,      f"{key}.dat"		)
       self.assertTrue(os.path.isfile(a_		)		)
       # TODO: add tests on the fact weights are properly loaded


 def  UpperCamelCase_   (				self:		Tuple		):




   '''simple docstring'''



   _snake_case							:		List[Any]								=  [torch.floataa, torch.floataa, torch.bfloataa]

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

       _snake_case							:		int								=  load_offloaded_weight(a_,      index["""weight"""]		)
       self.assertTrue(torch.equal(a_,      a_		)		)


 def  UpperCamelCase_   (				self:		Tuple		):




   '''simple docstring'''



   _snake_case							:		int								=  ModelForTest()
   _snake_case							:		int								=  model.state_dict()
   _snake_case							:		Any								=  {k: v for k, v in state_dict.items() if """linear2""" not in k}
   _snake_case							:		Optional[int]								=  {k: v for k, v in state_dict.items() if """linear2""" in k}

   with TemporaryDirectory() as tmp_dir:
     offload_state_dict(a_,      a_		)
     _snake_case							:		Optional[int]								=  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]		)		)

   _snake_case							:		Tuple								=  {k: v for k, v in state_dict.items() if """weight""" in k}
   _snake_case							:		List[str]								=  {k: v for k, v in state_dict.items() if """weight""" not in k}

   with TemporaryDirectory() as tmp_dir:
     offload_state_dict(a_,      a_		)
     _snake_case							:		Dict								=  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
     _snake_case							:		int								=  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  UpperCamelCase_   (				self:		List[str]		):




   '''simple docstring'''



   _snake_case							:		int								=  {"""a.1""": 0, """a.10""": 1, """a.2""": 2}
   _snake_case							:		Tuple								=  extract_submodules_state_dict(a_,      ["""a.1""", """a.2"""]		)
   self.assertDictEqual(a_,      {"""a.1""": 0, """a.2""": 2}		)

   _snake_case							:		Optional[int]								=  {"""a.1.a""": 0, """a.10.a""": 1, """a.2.a""": 2}
   _snake_case							:		Tuple								=  extract_submodules_state_dict(a_,      ["""a.1""", """a.2"""]		)
   self.assertDictEqual(a_,      {"""a.1.a""": 0, """a.2.a""": 2}		)








"""simple docstring"""


from ..utils import DummyObject, requires_backends






class 					lowercase(     metaclass=__a ):







 '''simple docstring'''





 lowercase__ 					=					["note_seq"]


 def __init__(				self:		Dict,      *a_:		Union[str, Any],      **a_:		List[str]		):




   '''simple docstring'''



   requires_backends(self,      ["""note_seq"""]		)


 @classmethod
 def  UpperCamelCase_   (				cls:		Optional[int],      *a_:		Any,      **a_:		Optional[Any]		):




   '''simple docstring'''



   requires_backends(cls,      ["""note_seq"""]		)


 @classmethod
 def  UpperCamelCase_   (				cls:		Tuple,      *a_:		Optional[Any],      **a_:		List[str]		):




   '''simple docstring'''



   requires_backends(cls,      ["""note_seq"""]		)


