




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__   (			_lowercase ,						_lowercase ,						_lowercase ,						_lowercase=None      ):

     '''simple docstring'''


     UpperCAmelCase_			:							Union[str, Any]   =			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.
          UpperCAmelCase_			:							Optional[Any]   =			weight.view(torch.intaa      )
          UpperCAmelCase_			:							int   =			'''bfloat16'''
     UpperCAmelCase_			:							Optional[Any]   =			weight.cpu().numpy()
     UpperCAmelCase_			:							Union[str, Any]   =			os.path.join(__lowercase ,						f'''{weight_name}.dat'''      )
     if index is not None:
          if dtype is None:
               UpperCAmelCase_			:							int   =			str(array.dtype      )
          UpperCAmelCase_			:							Tuple   =			{'''dtype''': dtype, '''shape''': list(array.shape      )}
     if array.ndim == 0:
          UpperCAmelCase_			:							Optional[int]   =			array[None]
     UpperCAmelCase_			:							Union[str, Any]   =			np.memmap(__lowercase ,						dtype=array.dtype ,						mode='''w+''' ,						shape=array.shape      )
     UpperCAmelCase_			:							Union[str, Any]   =			array[:]
     file_array.flush()
     return index







def lowerCamelCase__   (			_lowercase ,						_lowercase      ):

     '''simple docstring'''


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

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

     UpperCAmelCase_			:							Union[str, Any]   =			np.memmap(__lowercase ,						dtype=__lowercase ,						shape=__lowercase ,						mode='''r'''      )

     if len(weight_info['''shape''']      ) == 0:
          UpperCAmelCase_			:							Dict   =			weight[0]
     UpperCAmelCase_			:							str   =			torch.tensor(__lowercase      )
     if weight_info["dtype"] == "bfloat16":
          UpperCAmelCase_			:							str   =			weight.view(torch.bfloataa      )

     return weight







def lowerCamelCase__   (			_lowercase ,						_lowercase      ):

     '''simple docstring'''


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

     UpperCAmelCase_			:							Union[str, Any]   =			os.path.join(__lowercase ,						'''index.json'''      )
     if os.path.isfile(__lowercase      ):
          with open(__lowercase ,						'''r''' ,						encoding='''utf-8'''      ) as f:
               UpperCAmelCase_			:							Tuple   =			json.load(__lowercase      )
     else:
          UpperCAmelCase_			:							int   =			{}
     current_index.update(__lowercase      )

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







def lowerCamelCase__   (			_lowercase ,						_lowercase      ):

     '''simple docstring'''


     os.makedirs(__lowercase ,						exist_ok=__lowercase      )
     UpperCAmelCase_			:							Any   =			{}
     for name, parameter in state_dict.items():
          UpperCAmelCase_			:							Dict   =			offload_weight(__lowercase ,						__lowercase ,						__lowercase ,						index=__lowercase      )

     # Update index
     save_offload_index(__lowercase ,						__lowercase      )

class   __a(     _UpperCamelCase   ):






    """simple docstring"""





    def __init__(      self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE  ) ->		str:
         UpperCAmelCase_			:							int   =			dataset
         UpperCAmelCase_			:							Tuple   =			prefix





    def __getitem__(      self   ,_SCREAMING_SNAKE_CASE  ) ->		Union[str, Any]:
         return self.dataset[f'''{self.prefix}{key}''']





    def __iter__(      self  ) ->		List[str]:
         return iter([key for key in self.dataset if key.startswith(self.prefix  )]  )





    def __len__(      self  ) ->		Optional[int]:
         return len(self.dataset  )


class   __a(     _UpperCamelCase   ):






    """simple docstring"""





    def __init__(      self   ,_SCREAMING_SNAKE_CASE = None   ,_SCREAMING_SNAKE_CASE = None   ,_SCREAMING_SNAKE_CASE = None   ,_SCREAMING_SNAKE_CASE=None   ,) ->		Optional[int]:







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

         UpperCAmelCase_			:							Any   =			{} if state_dict is None else state_dict
         UpperCAmelCase_			:							Union[str, Any]   =			save_folder
         if index is None and save_folder is not None:
              with open(os.path.join(a__   ,'''index.json'''  )  ) as f:
                   UpperCAmelCase_			:							List[str]   =			json.load(a__  )
         UpperCAmelCase_			:							List[Any]   =			{} if index is None else index
         UpperCAmelCase_			:							Union[str, Any]   =			list(self.state_dict.keys()  )
         self.all_keys.extend([key for key in self.index if key not in self.all_keys]  )
         UpperCAmelCase_			:							str   =			device





    def __getitem__(      self   ,_SCREAMING_SNAKE_CASE  ) ->		Dict:







         if key in self.state_dict:
              return self.state_dict[key]
         UpperCAmelCase_			:							Tuple   =			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

              UpperCAmelCase_			:							Any   =			'''cpu''' if self.device is None else self.device
              with safe_open(weight_info['''safetensors_file''']   ,framework='''pt'''   ,device=a__  ) as f:
                   UpperCAmelCase_			:							Dict   =			f.get_tensor(weight_info.get('''weight_name'''   ,a__  )  )

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

         UpperCAmelCase_			:							List[Any]   =			os.path.join(self.save_folder   ,f'''{key}.dat'''  )
         return load_offloaded_weight(a__   ,a__  )





    def __iter__(      self  ) ->		Union[str, Any]:
         return iter(self.all_keys  )





    def __len__(      self  ) ->		Union[str, Any]:
         return len(self.all_keys  )







def lowerCamelCase__   (			_lowercase ,						_lowercase      ):

     '''simple docstring'''


     UpperCAmelCase_			:							int   =			{}
     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



"""simple docstring"""


import warnings

from ...utils import logging
from .image_processing_mobilenet_va import MobileNetVaImageProcessor


a_											=			logging.get_logger(__name__)
class snake_case					(     _UpperCamelCase):







		def __init__(    self					:				str			,      *a__					:				Union[str, Any]			,      **a__					:				Optional[Any]						)				->  None:





						'''simple docstring'''







						warnings.warn(
						    "The class MobileNetV2FeatureExtractor is deprecated and will be removed in version 5 of Transformers."
						    " Please use MobileNetV2ImageProcessor instead."			,      a__			,      )
						super().__init__(*a__			,      **a__						)