



import json
import os
from collections.abc import Mapping
from typing import Dict, List, Optional, Union

import numpy as np
import torch

from .imports import is_safetensors_available

def 					lowerCAmelCase__  (							_SCREAMING_SNAKE_CASE    :     int			,       _SCREAMING_SNAKE_CASE    :     Tuple			,       _SCREAMING_SNAKE_CASE    :     int			,       _SCREAMING_SNAKE_CASE    :     List[str]=None      ):
				"""simple docstring"""







				__a					       =						None
				# Check the string instead of the dtype to be compatible with versions of PyTorch that don't have bfloat16.
				if str(weight.dtype      ) == "torch.bfloat16":
								# Need to reinterpret the underlined data as int16 since NumPy does not handle bfloat16s.
								__a					       =						weight.view(torch.intaa      )
								__a					       =						"""bfloat16"""
				__a					       =						weight.cpu().numpy()
				__a					       =						os.path.join(_SCREAMING_SNAKE_CASE			,       f"{weight_name}.dat"      )
				if index is not None:
								if dtype is None:
												__a					       =						str(array.dtype      )
								__a					       =						{"""dtype""": dtype, """shape""": list(array.shape      )}
				if array.ndim == 0:
								__a					       =						array[None]
				__a					       =						np.memmap(_SCREAMING_SNAKE_CASE			,       dtype=array.dtype			,       mode="""w+"""			,       shape=array.shape      )
				__a					       =						array[:]
				file_array.flush()
				return index

def 					lowerCAmelCase__  (							_SCREAMING_SNAKE_CASE    :     Union[str, Any]			,       _SCREAMING_SNAKE_CASE    :     List[str]      ):
				"""simple docstring"""







				__a					       =						tuple(weight_info["""shape"""]      )
				if shape == ():
								# NumPy memory-mapped arrays can't have 0 dims so it was saved as 1d tensor
								__a					       =						(1,)

				__a					       =						weight_info["""dtype"""]
				if dtype == "bfloat16":
								# NumPy does not support bfloat16 so this was saved as a int16
								__a					       =						"""int16"""

				__a					       =						np.memmap(_SCREAMING_SNAKE_CASE			,       dtype=_SCREAMING_SNAKE_CASE			,       shape=_SCREAMING_SNAKE_CASE			,       mode="""r"""      )

				if len(weight_info["""shape"""]      ) == 0:
								__a					       =						weight[0]
				__a					       =						torch.tensor(_SCREAMING_SNAKE_CASE      )
				if weight_info["dtype"] == "bfloat16":
								__a					       =						weight.view(torch.bfloataa      )

				return weight

def 					lowerCAmelCase__  (							_SCREAMING_SNAKE_CASE    :     List[str]			,       _SCREAMING_SNAKE_CASE    :     List[str]      ):
				"""simple docstring"""







				if index is None or len(_SCREAMING_SNAKE_CASE      ) == 0:
								# Nothing to save
								return

				__a					       =						os.path.join(_SCREAMING_SNAKE_CASE			,       """index.json"""      )
				if os.path.isfile(_SCREAMING_SNAKE_CASE      ):
								with open(_SCREAMING_SNAKE_CASE			,       """r"""			,       encoding="""utf-8"""      ) as f:
												__a					       =						json.load(_SCREAMING_SNAKE_CASE      )
				else:
								__a					       =						{}
				current_index.update(_SCREAMING_SNAKE_CASE      )

				with open(_SCREAMING_SNAKE_CASE			,       """w"""			,       encoding="""utf-8"""      ) as f:
								json.dump(_SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE			,       indent=2      )

def 					lowerCAmelCase__  (							_SCREAMING_SNAKE_CASE    :     Union[str, os.PathLike]			,       _SCREAMING_SNAKE_CASE    :     Dict[str, torch.Tensor]      ):
				"""simple docstring"""







				os.makedirs(_SCREAMING_SNAKE_CASE			,       exist_ok=_SCREAMING_SNAKE_CASE      )
				__a					       =						{}
				for name, parameter in state_dict.items():
								__a					       =						offload_weight(_SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE			,       index=_SCREAMING_SNAKE_CASE      )

				# Update index
				save_offload_index(_SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE      )






class 				SCREAMING_SNAKE_CASE     (							lowerCamelCase__   ):





							def __init__(				self						:						Optional[int] ,			__lowercase						:						Mapping ,			__lowercase						:						str		):

											'''simple docstring'''
											__a					       =						dataset
											__a					       =						prefix





							def __getitem__(				self						:						Optional[int] ,			__lowercase						:						Optional[int]		):

											'''simple docstring'''
											return self.dataset[F"{self.prefix}{key}"]





							def __iter__(				self						:						Any		):

											'''simple docstring'''
											return iter([key for key in self.dataset if key.startswith(self.prefix		)]		)







							def __len__(				self						:						str		):

											'''simple docstring'''
											return len(self.dataset		)






class 				SCREAMING_SNAKE_CASE     (							lowerCamelCase__   ):





							def __init__(				self						:						Any ,			__lowercase						:						Dict[str, torch.Tensor] = None ,			__lowercase						:						Optional[Union[str, os.PathLike]] = None ,			__lowercase						:						Mapping = None ,			__lowercase						:						Dict=None ,			):

											'''simple docstring'''
											if state_dict is None and save_folder is None:
															raise ValueError("""Need either a `state_dict` or a `save_folder` containing offloaded weights."""		)

											__a					       =						{} if state_dict is None else state_dict
											__a					       =						save_folder
											if index is None and save_folder is not None:
															with open(os.path.join(__lowercase ,			"""index.json"""		)		) as f:
																			__a					       =						json.load(__lowercase		)
											__a					       =						{} if index is None else index
											__a					       =						list(self.state_dict.keys()		)
											self.all_keys.extend([key for key in self.index if key not in self.all_keys]		)
											__a					       =						device





							def __getitem__(				self						:						Optional[int] ,			__lowercase						:						str		):

											'''simple docstring'''
											# State dict gets priority
											if key in self.state_dict:
															return self.state_dict[key]
											__a					       =						self.index[key]
											if weight_info.get("""safetensors_file"""		) is not None:
															if not is_safetensors_available():
																			raise ImportError("""These offloaded weights require the use of safetensors: `pip install safetensors`."""		)

															from safetensors import safe_open

															__a					       =						"""cpu""" if self.device is None else self.device
															with safe_open(weight_info["""safetensors_file"""] ,			framework="""pt""" ,			device=__lowercase		) as f:
																			__a					       =						f.get_tensor(weight_info.get("""weight_name""" ,			__lowercase		)		)

															if "dtype" in weight_info:
																			return tensor.to(getattr(__lowercase ,			weight_info["""dtype"""]		)		)
															else:
																			return tensor

											__a					       =						os.path.join(self.save_folder ,			F"{key}.dat"		)
											return load_offloaded_weight(__lowercase ,			__lowercase		)





							def __iter__(				self						:						List[Any]		):

											'''simple docstring'''
											return iter(self.all_keys		)







							def __len__(				self						:						str		):

											'''simple docstring'''
											return len(self.all_keys		)



def 					lowerCAmelCase__  (							_SCREAMING_SNAKE_CASE    :     Dict[str, torch.Tensor]			,       _SCREAMING_SNAKE_CASE    :     List[str]      ):
				"""simple docstring"""







				__a					       =						{}
				for module_name in submodule_names:
								# We want to catch module_name parameter (module_name.xxx) or potentially module_name, but not any of the
								# submodules that could being like module_name (transformers.h.1 and transformers.h.10 for instance)
								result.update(
								    {
								        key: param
								        for key, param in state_dict.items()
								        if key == module_name or key.startswith(module_name + """."""      )
								    }      )
				return result





from typing import TYPE_CHECKING

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


lowerCamelCase__     =	{"""configuration_hubert""": ["""HUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """HubertConfig"""]}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__     =	[
						    """HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
						    """HubertForCTC""",
						    """HubertForSequenceClassification""",
						    """HubertModel""",
						    """HubertPreTrainedModel""",
						]


try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__     =	[
						    """TF_HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
						    """TFHubertForCTC""",
						    """TFHubertModel""",
						    """TFHubertPreTrainedModel""",
						]

if TYPE_CHECKING:
						from .configuration_hubert import HUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, HubertConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_hubert import (
												    HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    HubertForCTC,
												    HubertForSequenceClassification,
												    HubertModel,
												    HubertPreTrainedModel,
												)

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_hubert import (
												    TF_HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFHubertForCTC,
												    TFHubertModel,
												    TFHubertPreTrainedModel,
												)


else:
						import sys

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