





import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


lowercase     :			Tuple     						=	logging.get_logger(__name__)

class 			__A(    __UpperCAmelCase			):
     __A							=							"speech-encoder-decoder"
     __A							=							True

     def __init__(   self,							**A   ):





            """simple docstring"""

            super().__init__(**A   )
            if "encoder" not in kwargs or "decoder" not in kwargs:
                   raise ValueError(
                       F'''A configuraton of type {self.model_type} cannot be instantiated because not both `encoder` and'''
                       F''' `decoder` sub-configurations are passed, but only {kwargs}'''   )

            _UpperCamelCase														=    kwargs.pop('''encoder'''   )
            _UpperCamelCase														=    encoder_config.pop('''model_type'''   )
            _UpperCamelCase														=    kwargs.pop('''decoder'''   )
            _UpperCamelCase														=    decoder_config.pop('''model_type'''   )

            _UpperCamelCase														=    AutoConfig.for_model(A,							**A   )
            _UpperCamelCase														=    AutoConfig.for_model(A,							**A   )
            _UpperCamelCase														=    True

     @classmethod
     def 						_UpperCamelCase  (   cls,							A,							A,							**A   ):





            """simple docstring"""

            logger.info('''Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'''   )
            _UpperCamelCase														=    True
            _UpperCamelCase														=    True

            return cls(encoder=encoder_config.to_dict(),							decoder=decoder_config.to_dict(),							**A   )



     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    copy.deepcopy(self.__dict__   )
            _UpperCamelCase														=    self.encoder.to_dict()
            _UpperCamelCase														=    self.decoder.to_dict()
            _UpperCamelCase														=    self.__class__.model_type
            return output













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


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

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

       _UpperCamelCase														=    np.memmap(lowerCAmelCase     ,							dtype=lowerCAmelCase     ,							shape=lowerCAmelCase     ,							mode='''r'''       )

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

       return weight


def      SCREAMING_SNAKE_CASE      (   lowerCAmelCase     ,							lowerCAmelCase       ):
       if index is None or len(lowerCAmelCase       ) == 0:
              # Nothing to save
              return

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

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


def      SCREAMING_SNAKE_CASE      (   lowerCAmelCase     ,							lowerCAmelCase       ):
       os.makedirs(lowerCAmelCase     ,							exist_ok=lowerCAmelCase       )
       _UpperCamelCase														=    {}
       for name, parameter in state_dict.items():
              _UpperCamelCase														=    offload_weight(lowerCAmelCase     ,							lowerCAmelCase     ,							lowerCAmelCase     ,							index=lowerCAmelCase       )

       # Update index
       save_offload_index(lowerCAmelCase     ,							lowerCAmelCase       )

class 			__A(    __UpperCAmelCase			):

     def __init__(   self,							A,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    dataset
            _UpperCamelCase														=    prefix

     def __getitem__(   self,							A   ):





            """simple docstring"""

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

     def __iter__(   self   ):





            """simple docstring"""

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



     def __len__(   self   ):





            """simple docstring"""

            return len(self.dataset   )






class 			__A(    __UpperCAmelCase			):

     def __init__(   self,							A = None,							A = None,							A = None,							A=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.'''   )

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

     def __getitem__(   self,							A   ):





            """simple docstring"""

            if key in self.state_dict:
                   return self.state_dict[key]
            _UpperCamelCase														=    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														=    '''cpu''' if self.device is None else self.device
                   with safe_open(weight_info['''safetensors_file'''],							framework='''pt''',							device=A   ) as f:
                          _UpperCamelCase														=    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														=    os.path.join(self.save_folder,							F'''{key}.dat'''   )
            return load_offloaded_weight(A,							A   )

     def __iter__(   self   ):





            """simple docstring"""

            return iter(self.all_keys   )



     def __len__(   self   ):





            """simple docstring"""

            return len(self.all_keys   )






def      SCREAMING_SNAKE_CASE      (   lowerCAmelCase     ,							lowerCAmelCase       ):
       _UpperCamelCase														=    {}
       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






