






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







			def __init__(     self:  Optional[Any]					):







									'''simple docstring'''




									super().__init__()
									_SCREAMING_SNAKE_CASE     				=  nn.Linear(3				,     4					)
									_SCREAMING_SNAKE_CASE     				=  nn.BatchNormad(4					)
									_SCREAMING_SNAKE_CASE     				=  nn.Linear(4				,     5					)







			def 							UpperCamelCase				(     self:  Optional[int]				,     UpperCAmelCase_:  Optional[Any]					):







									'''simple docstring'''




									return self.lineara(self.batchnorm(self.lineara(UpperCAmelCase_					)					)					)



class 				__UpperCAmelCase		(unittest.TestCase   ):







			def 							UpperCamelCase				(     self:  str					):







									'''simple docstring'''




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







			def 							UpperCamelCase				(     self:  Optional[Any]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  [torch.floataa, torch.floataa, torch.bfloataa]

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

																					_SCREAMING_SNAKE_CASE     				=  load_offloaded_weight(UpperCAmelCase_				,     index["""weight"""]					)
																					self.assertTrue(torch.equal(UpperCAmelCase_				,     UpperCAmelCase_					)					)







			def 							UpperCamelCase				(     self:  Any					):







									'''simple docstring'''




									_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(UpperCAmelCase_				,     UpperCAmelCase_					)
															_SCREAMING_SNAKE_CASE     				=  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]					)					)

									_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(UpperCAmelCase_				,     UpperCAmelCase_					)
															_SCREAMING_SNAKE_CASE     				=  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
															_SCREAMING_SNAKE_CASE     				=  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 							UpperCamelCase				(     self:  Any					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  {"""a.1""": 0, """a.10""": 1, """a.2""": 2}
									_SCREAMING_SNAKE_CASE     				=  extract_submodules_state_dict(UpperCAmelCase_				,     ["""a.1""", """a.2"""]					)
									self.assertDictEqual(UpperCAmelCase_				,     {"""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(UpperCAmelCase_				,     ["""a.1""", """a.2"""]					)
									self.assertDictEqual(UpperCAmelCase_				,     {"""a.1.a""": 0, """a.2.a""": 2}					)









from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCamelCase			    =     logging.get_logger(__name__)

UpperCamelCase			    =     {
    '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''',
    '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
}



class 				__UpperCAmelCase		(_UpperCAmelCase   ):
			__snake_case    :				Any									=							"mobilenet_v1"







			def __init__(     self:  List[str]				,     UpperCAmelCase_:  List[Any]=3				,     UpperCAmelCase_:  List[str]=224				,     UpperCAmelCase_:  Optional[Any]=1.0				,     UpperCAmelCase_:  Dict=8				,     UpperCAmelCase_:  List[str]="relu6"				,     UpperCAmelCase_:  List[Any]=True				,     UpperCAmelCase_:  Dict=0.9_99				,     UpperCAmelCase_:  Union[str, Any]=0.02				,     UpperCAmelCase_:  Tuple=0.0_01				,     **UpperCAmelCase_:  List[str]				,     ):







									'''simple docstring'''




									super().__init__(**UpperCAmelCase_					)

									if depth_multiplier <= 0:
															raise ValueError("""depth_multiplier must be greater than zero."""					)

									_SCREAMING_SNAKE_CASE     				=  num_channels
									_SCREAMING_SNAKE_CASE     				=  image_size
									_SCREAMING_SNAKE_CASE     				=  depth_multiplier
									_SCREAMING_SNAKE_CASE     				=  min_depth
									_SCREAMING_SNAKE_CASE     				=  hidden_act
									_SCREAMING_SNAKE_CASE     				=  tf_padding
									_SCREAMING_SNAKE_CASE     				=  classifier_dropout_prob
									_SCREAMING_SNAKE_CASE     				=  initializer_range
									_SCREAMING_SNAKE_CASE     				=  layer_norm_eps



class 				__UpperCAmelCase		(_UpperCAmelCase   ):
			__snake_case    :				List[Any]									=							version.parse("1.11"   )







			@property
			def 							UpperCamelCase				(     self:  Tuple					):







									'''simple docstring'''




									return OrderedDict([("""pixel_values""", {0: """batch"""})]					)







			@property
			def 							UpperCamelCase				(     self:  Dict					):







									'''simple docstring'''




									if self.task == "image-classification":
															return OrderedDict([("""logits""", {0: """batch"""})]					)
									else:
															return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})]					)







			@property
			def 							UpperCamelCase				(     self:  int					):







									'''simple docstring'''




									return 1E-4

