



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


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

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

		lowerCAmelCase_      :					Tuple     =			np.memmap(__UpperCamelCase					,dtype=__UpperCamelCase					,shape=__UpperCamelCase					,mode='''r'''			)

		if len(weight_info['''shape''']			) == 0:
				lowerCAmelCase_      :					Any     =			weight[0]
		lowerCAmelCase_      :					List[Any]     =			torch.tensor(__UpperCamelCase			)
		if weight_info["dtype"] == "bfloat16":
				lowerCAmelCase_      :					Optional[Any]     =			weight.view(torch.bfloataa			)

		return weight


def       UpperCamelCase(  __UpperCamelCase							:		Dict					,__UpperCamelCase							:		Optional[int]			):
		if index is None or len(__UpperCamelCase			) == 0:
				# Nothing to save
				return

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

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


def       UpperCamelCase(  __UpperCamelCase							:		Union[str, os.PathLike]					,__UpperCamelCase							:		Dict[str, torch.Tensor]			):
		os.makedirs(__UpperCamelCase					,exist_ok=__UpperCamelCase			)
		lowerCAmelCase_      :					Union[str, Any]     =			{}
		for name, parameter in state_dict.items():
				lowerCAmelCase_      :					Any     =			offload_weight(__UpperCamelCase					,__UpperCamelCase					,__UpperCamelCase					,index=__UpperCamelCase			)

		# Update index
		save_offload_index(__UpperCamelCase					,__UpperCamelCase			)



class     __snake_case						(					UpperCamelCase_						):




							def __init__(	self					:	Any    ,    A_					:	Mapping    ,    A_					:	str):
									lowerCAmelCase_      :					Tuple     =			dataset
									lowerCAmelCase_      :					Any     =			prefix




							def __getitem__(	self					:	Optional[Any]    ,    A_					:	List[Any]):
									return self.dataset[F"""{self.prefix}{key}"""]




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







							def __len__(	self					:	Any):
									return len(self.dataset)





class     __snake_case						(					UpperCamelCase_						):




							def __init__(	self					:	Optional[int]    ,    A_					:	Dict[str, torch.Tensor] = None    ,    A_					:	Optional[Union[str, os.PathLike]] = None    ,    A_					:	Mapping = None    ,    A_					:	List[Any]=None    ,    ):
									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_      :					List[str]     =			{} if state_dict is None else state_dict
									lowerCAmelCase_      :					Tuple     =			save_folder
									if index is None and save_folder is not None:
											with open(os.path.join(A_    ,    '''index.json''')) as f:
													lowerCAmelCase_      :					List[str]     =			json.load(A_)
									lowerCAmelCase_      :					Optional[Any]     =			{} if index is None else index
									lowerCAmelCase_      :					int     =			list(self.state_dict.keys())
									self.all_keys.extend([key for key in self.index if key not in self.all_keys])
									lowerCAmelCase_      :					List[Any]     =			device




							def __getitem__(	self					:	Dict    ,    A_					:	str):
									# State dict gets priority
									if key in self.state_dict:
											return self.state_dict[key]
									lowerCAmelCase_      :					Union[str, Any]     =			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_      :					List[Any]     =			'''cpu''' if self.device is None else self.device
											with safe_open(weight_info['''safetensors_file''']    ,    framework='''pt'''    ,    device=A_) as f:
													lowerCAmelCase_      :					int     =			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

									lowerCAmelCase_      :					Dict     =			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					:	Dict):
									return len(self.all_keys)





def       UpperCamelCase(  __UpperCamelCase							:		Dict[str, torch.Tensor]					,__UpperCamelCase							:		List[str]			):
		lowerCAmelCase_      :					Optional[Any]     =			{}
		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 ...configuration_utils import PretrainedConfig
from ...utils import logging


A__  :  Tuple       = logging.get_logger(__name__)

A__  :  Tuple       = {
    '''facebook/opt-125m''': '''https://huggingface.co/facebook/opt-125m/blob/main/config.json''',
    '''facebook/opt-350m''': '''https://huggingface.co/facebook/opt-350m/blob/main/config.json''',
    '''facebook/opt-1.3b''': '''https://huggingface.co/facebook/opt-1.3b/blob/main/config.json''',
    '''facebook/opt-2.7b''': '''https://huggingface.co/facebook/opt-2.7b/blob/main/config.json''',
    '''facebook/opt-6.7b''': '''https://huggingface.co/facebook/opt-6.7b/blob/main/config.json''',
    '''facebook/opt-13b''': '''https://huggingface.co/facebook/opt-13b/blob/main/config.json''',
}



class     __snake_case						(					UpperCamelCase_						):
							_a      				=			'''opt'''
							_a      				=			['''past_key_values''']




							def __init__(	self					:	Optional[int]    ,    A_					:	Any=5_0_2_7_2    ,    A_					:	Tuple=7_6_8    ,    A_					:	Optional[Any]=1_2    ,    A_					:	str=3_0_7_2    ,    A_					:	Dict=2_0_4_8    ,    A_					:	List[Any]=True    ,    A_					:	Optional[int]=False    ,    A_					:	Optional[int]=None    ,    A_					:	Dict=0.1    ,    A_					:	str=0.0    ,    A_					:	Union[str, Any]=1_2    ,    A_					:	List[str]="relu"    ,    A_					:	Dict=0.0    ,    A_					:	Union[str, Any]=0.02    ,    A_					:	Optional[Any]=True    ,    A_					:	Optional[Any]=1    ,    A_					:	List[Any]=2    ,    A_					:	Dict=2    ,    A_					:	Optional[int]=True    ,    A_					:	Optional[int]=True    ,    **A_					:	List[Any]    ,    ):
									super().__init__(
									    pad_token_id=A_    ,    bos_token_id=A_    ,    eos_token_id=A_    ,    **A_    ,    )
									lowerCAmelCase_      :					List[Any]     =			vocab_size
									lowerCAmelCase_      :					Union[str, Any]     =			max_position_embeddings
									lowerCAmelCase_      :					Optional[int]     =			num_attention_heads
									lowerCAmelCase_      :					int     =			word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
									lowerCAmelCase_      :					List[Any]     =			ffn_dim
									lowerCAmelCase_      :					Any     =			hidden_size
									lowerCAmelCase_      :					Union[str, Any]     =			num_hidden_layers
									lowerCAmelCase_      :					str     =			dropout
									lowerCAmelCase_      :					List[str]     =			attention_dropout
									lowerCAmelCase_      :					Any     =			activation_function
									lowerCAmelCase_      :					Union[str, Any]     =			init_std
									lowerCAmelCase_      :					int     =			layerdrop
									lowerCAmelCase_      :					Optional[int]     =			use_cache
									lowerCAmelCase_      :					int     =			do_layer_norm_before
									# We keep these variables at `True` for backward compatibility.
									lowerCAmelCase_      :					Optional[Any]     =			enable_bias
									lowerCAmelCase_      :					Optional[int]     =			layer_norm_elementwise_affine

									# Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
									# with checkpoints that have been fine-tuned before transformers v4.20.1
									# see https://github.com/facebookresearch/metaseq/pull/164
									lowerCAmelCase_      :					Optional[Any]     =			_remove_final_layer_norm





