





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        __SCREAMING_SNAKE_CASE	(				__lowercase:		Dict  ,				__lowercase:		int  ,				__lowercase:		Optional[int]  ,				__lowercase:		List[Any]=None):



  '''simple docstring'''






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


def        __SCREAMING_SNAKE_CASE	(				__lowercase:		Union[str, Any]  ,				__lowercase:		List[str]):



  '''simple docstring'''






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

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

  __lowerCAmelCase								=					np.memmap(__snake_case  ,				dtype=__snake_case  ,				shape=__snake_case  ,				mode='''r''')

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

  return weight


def        __SCREAMING_SNAKE_CASE	(				__lowercase:		Tuple  ,				__lowercase:		Tuple):



  '''simple docstring'''






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

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

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


def        __SCREAMING_SNAKE_CASE	(				__lowercase:		int  ,				__lowercase:		Tuple):



  '''simple docstring'''






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

  # Update index
  save_offload_index(__snake_case  ,				__snake_case)

class 	__snake_case					(      _a ):






    '''simple docstring'''





    def __init__( self    :		Tuple      ,    snake_case    :		Optional[Any]      ,    snake_case    :		List[Any]				):

      """simple docstring"""





      __lowerCAmelCase								=					dataset
      __lowerCAmelCase								=					prefix





    def __getitem__( self    :		Optional[Any]      ,    snake_case    :		Dict				):

      """simple docstring"""





      return self.dataset[f'''{self.prefix}{key}''']





    def __iter__( self    :		str				):

      """simple docstring"""





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





    def __len__( self    :		Union[str, Any]				):

      """simple docstring"""





      return len(self.dataset				)


class 	__snake_case					(      _a ):






    '''simple docstring'''





    def __init__( self    :		int      ,    snake_case    :		List[Any] = None      ,    snake_case    :		int = None      ,    snake_case    :		Union[str, Any] = None      ,    snake_case    :		List[str]=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.'''				)

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





    def __getitem__( self    :		Union[str, Any]      ,    snake_case    :		List[str]				):

      """simple docstring"""





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

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

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

      __lowerCAmelCase								=					os.path.join(self.save_folder      ,    f'''{key}.dat'''				)
      return load_offloaded_weight(snake_case      ,    snake_case				)





    def __iter__( self    :		Tuple				):

      """simple docstring"""





      return iter(self.all_keys				)





    def __len__( self    :		Tuple				):

      """simple docstring"""





      return len(self.all_keys				)


def        __SCREAMING_SNAKE_CASE	(				__lowercase:		Tuple  ,				__lowercase:		Any):



  '''simple docstring'''






  __lowerCAmelCase								=					{}
  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


UpperCAmelCase__        =				{
    '''configuration_flaubert''': ['''FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FlaubertConfig''', '''FlaubertOnnxConfig'''],
    '''tokenization_flaubert''': ['''FlaubertTokenizer'''],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__        =				[
      '''FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''FlaubertForMultipleChoice''',
      '''FlaubertForQuestionAnswering''',
      '''FlaubertForQuestionAnsweringSimple''',
      '''FlaubertForSequenceClassification''',
      '''FlaubertForTokenClassification''',
      '''FlaubertModel''',
      '''FlaubertWithLMHeadModel''',
      '''FlaubertPreTrainedModel''',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__        =				[
      '''TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''TFFlaubertForMultipleChoice''',
      '''TFFlaubertForQuestionAnsweringSimple''',
      '''TFFlaubertForSequenceClassification''',
      '''TFFlaubertForTokenClassification''',
      '''TFFlaubertModel''',
      '''TFFlaubertPreTrainedModel''',
      '''TFFlaubertWithLMHeadModel''',
  ]


if TYPE_CHECKING:
  from .configuration_flaubert import FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, FlaubertConfig, FlaubertOnnxConfig
  from .tokenization_flaubert import FlaubertTokenizer

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flaubert import (
        FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        FlaubertForMultipleChoice,
        FlaubertForQuestionAnswering,
        FlaubertForQuestionAnsweringSimple,
        FlaubertForSequenceClassification,
        FlaubertForTokenClassification,
        FlaubertModel,
        FlaubertPreTrainedModel,
        FlaubertWithLMHeadModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_flaubert import (
        TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFFlaubertForMultipleChoice,
        TFFlaubertForQuestionAnsweringSimple,
        TFFlaubertForSequenceClassification,
        TFFlaubertForTokenClassification,
        TFFlaubertModel,
        TFFlaubertPreTrainedModel,
        TFFlaubertWithLMHeadModel,
    )

else:
  import sys

  UpperCAmelCase__        =				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

