




"""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   :     Union[str, Any]):


						"""simple docstring"""




						super().__init__()
						__UpperCAmelCase						:			Optional[int]												=       nn.Linear(3   ,	4)
						__UpperCAmelCase						:			str												=       nn.BatchNormad(4)
						__UpperCAmelCase						:			int												=       nn.Linear(4   ,	5)


			def    a_  (   self   :     str   ,	UpperCamelCase_   :     List[str]):


						"""simple docstring"""




						return self.lineara(self.batchnorm(self.lineara(UpperCamelCase_)))






class   a__					(						unittest.TestCase ):


			def    a_  (   self   :     Dict):


						"""simple docstring"""




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


			def    a_  (   self   :     Any):


						"""simple docstring"""




						__UpperCAmelCase						:			int												=       [torch.floataa, torch.floataa, torch.bfloataa]

						for dtype in dtypes:
									__UpperCAmelCase						:			List[Any]												=       torch.randn(2   ,	3   ,	dtype=UpperCamelCase_)
									with TemporaryDirectory() as tmp_dir:
												__UpperCAmelCase						:			Tuple												=       offload_weight(UpperCamelCase_   ,	"weight"   ,	UpperCamelCase_   ,	{})
												__UpperCAmelCase						:			Dict												=       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    a_  (   self   :     List[str]):


						"""simple docstring"""




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

						with TemporaryDirectory() as tmp_dir:
									offload_state_dict(UpperCamelCase_   ,	UpperCamelCase_)
									__UpperCAmelCase						:			List[str]												=       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						:			Optional[int]												=       {k: v for k, v in state_dict.items() if "weight" in k}
						__UpperCAmelCase						:			Optional[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						:			Optional[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						:			str												=       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    a_  (   self   :     Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			Any												=       {"a.1": 0, "a.10": 1, "a.2": 2}
						__UpperCAmelCase						:			Union[str, Any]												=       extract_submodules_state_dict(UpperCamelCase_   ,	["a.1", "a.2"])
						self.assertDictEqual(UpperCamelCase_   ,	{"a.1": 0, "a.2": 2})

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




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__lowerCamelCase				=			{
    """configuration_chinese_clip""": [
        """CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """ChineseCLIPConfig""",
        """ChineseCLIPOnnxConfig""",
        """ChineseCLIPTextConfig""",
        """ChineseCLIPVisionConfig""",
    ],
    """processing_chinese_clip""": ["""ChineseCLIPProcessor"""],
}

try:
   if not is_vision_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase				=			["""ChineseCLIPFeatureExtractor"""]
   __lowerCamelCase				=			["""ChineseCLIPImageProcessor"""]

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase				=			[
       """CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """ChineseCLIPModel""",
       """ChineseCLIPPreTrainedModel""",
       """ChineseCLIPTextModel""",
       """ChineseCLIPVisionModel""",
   ]

if TYPE_CHECKING:
   from .configuration_chinese_clip import (
       CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
       ChineseCLIPConfig,
       ChineseCLIPOnnxConfig,
       ChineseCLIPTextConfig,
       ChineseCLIPVisionConfig,
   )
   from .processing_chinese_clip import ChineseCLIPProcessor

   try:
      if not is_vision_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_chinese_clip import (
          CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
          ChineseCLIPModel,
          ChineseCLIPPreTrainedModel,
          ChineseCLIPTextModel,
          ChineseCLIPVisionModel,
      )

else:
   import sys

   __lowerCamelCase				=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





