
import collections
import types

import numpy as np

from ..utils import (
    add_end_docstrings,
    is_tensorflow_probability_available,
    is_tf_available,
    is_torch_available,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, Dataset, Pipeline, PipelineException


if is_torch_available():
				import torch

				from ..models.auto.modeling_auto import (
				    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
				    MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING,
				)

if is_tf_available() and is_tensorflow_probability_available():
				import tensorflow as tf
				import tensorflow_probability as tfp

				from ..models.auto.modeling_tf_auto import (
				    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
				    TF_MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING,
				)






class     SCREAMING_SNAKE_CASE__      (							SCREAMING_SNAKE_CASE_      ):

							"""simple docstring"""






							def __call__(					self       ,		SCREAMING_SNAKE_CASE_=None       ,		SCREAMING_SNAKE_CASE_=None       ,		**SCREAMING_SNAKE_CASE_     )->				Any:





													'''simple docstring'''






													requires_backends(self       ,		'''pandas'''     )
													import pandas as pd

													if table is None:
																			raise ValueError('''Keyword argument `table` cannot be None.'''     )
													elif query is None:
																			if isinstance(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     ) and table.get('''query'''     ) is not None and table.get('''table'''     ) is not None:
																									__UpperCamelCase       					=     [table]
																			elif isinstance(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     ) and len(SCREAMING_SNAKE_CASE_     ) > 0:
																									if not all(isinstance(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     ) for d in table     ):
																															raise ValueError(
																															    F"Keyword argument `table` should be a list of dict, but is {(type(SCREAMING_SNAKE_CASE_     ) for d in table)}"     )

																									if table[0].get('''query'''     ) is not None and table[0].get('''table'''     ) is not None:
																															__UpperCamelCase       					=     table
																									else:
																															raise ValueError(
																															    '''If keyword argument `table` is a list of dictionaries, each dictionary should have a `table`'''
																															    F" and `query` key, but only dictionary has keys {table[0].keys()} `table` and `query` keys."     )
																			elif Dataset is not None and isinstance(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     ) or isinstance(SCREAMING_SNAKE_CASE_       ,		types.GeneratorType     ):
																									return table
																			else:
																									raise ValueError(
																									    '''Invalid input. Keyword argument `table` should be either of type `dict` or `list`, but '''
																									    F"is {type(SCREAMING_SNAKE_CASE_     )})"     )
													else:
																			__UpperCamelCase       					=     [{'''table''': table, '''query''': query}]

													for tqa_pipeline_input in tqa_pipeline_inputs:
																			if not isinstance(tqa_pipeline_input['''table''']       ,		pd.DataFrame     ):
																									if tqa_pipeline_input["table"] is None:
																															raise ValueError('''Table cannot be None.'''     )

																									__UpperCamelCase       					=     pd.DataFrame(tqa_pipeline_input['''table''']     )

													return tqa_pipeline_inputs






@add_end_docstrings(SCREAMING_SNAKE_CASE_      )
class     SCREAMING_SNAKE_CASE__      (							SCREAMING_SNAKE_CASE_      ):

							"""simple docstring"""






							_snake_case       						=					'table,query'



							def __init__(					self       ,		SCREAMING_SNAKE_CASE_=TableQuestionAnsweringArgumentHandler()       ,		*SCREAMING_SNAKE_CASE_       ,		**SCREAMING_SNAKE_CASE_     )->				Any:





													'''simple docstring'''






													super().__init__(*SCREAMING_SNAKE_CASE_       ,		**SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     args_parser

													self.check_model_type(
													    dict(
													        TF_MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.items()
													        + TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.items()     )
													    if self.framework == '''tf'''
													    else dict(
													        MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.items() + MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.items()     )     )

													__UpperCamelCase       					=     bool(getattr(self.model.config       ,		'''aggregation_labels'''       ,		SCREAMING_SNAKE_CASE_     )     ) and bool(
													    getattr(self.model.config       ,		'''num_aggregation_labels'''       ,		SCREAMING_SNAKE_CASE_     )     )
													__UpperCamelCase       					=     '''tapas''' if hasattr(self.model.config       ,		'''aggregation_labels'''     ) else None



							def       A__						(					self       ,		**SCREAMING_SNAKE_CASE_     )->				Union[str, Any]:





													'''simple docstring'''






													return self.model(**SCREAMING_SNAKE_CASE_     )



							def       A__						(					self       ,		**SCREAMING_SNAKE_CASE_     )->				List[str]:





													'''simple docstring'''






													if self.framework == "pt":
																			__UpperCamelCase       					=     []
																			__UpperCamelCase       					=     []
																			__UpperCamelCase       					=     None
																			__UpperCamelCase       					=     inputs['''input_ids'''].shape[0]

																			__UpperCamelCase       					=     inputs['''input_ids'''].to(self.device     )
																			__UpperCamelCase       					=     inputs['''attention_mask'''].to(self.device     )
																			__UpperCamelCase       					=     inputs['''token_type_ids'''].to(self.device     )
																			__UpperCamelCase       					=     None

																			for index in range(SCREAMING_SNAKE_CASE_     ):
																									# If sequences have already been processed, the token type IDs will be created according to the previous
																									# answer.
																									if prev_answers is not None:
																															__UpperCamelCase       					=     token_type_ids_example[:, 3]  # shape (seq_len,)
																															__UpperCamelCase       					=     np.zeros_like(prev_labels_example.cpu().numpy()     )  # shape (seq_len,)

																															__UpperCamelCase       					=     token_type_ids[index]  # shape (seq_len, 7)
																															for i in range(model_labels.shape[0]     ):
																																					__UpperCamelCase       					=     token_type_ids_example[:, 0].tolist()[i]
																																					__UpperCamelCase       					=     token_type_ids_example[:, 1].tolist()[i] - 1
																																					__UpperCamelCase       					=     token_type_ids_example[:, 2].tolist()[i] - 1

																																					if row_id >= 0 and col_id >= 0 and segment_id == 1:
																																											__UpperCamelCase       					=     int(prev_answers[(col_id, row_id)]     )

																															__UpperCamelCase       					=     torch.from_numpy(SCREAMING_SNAKE_CASE_     ).type(torch.long     ).to(self.device     )

																									__UpperCamelCase       					=     input_ids[index]
																									__UpperCamelCase       					=     attention_mask[index]  # shape (seq_len,)
																									__UpperCamelCase       					=     token_type_ids[index]  # shape (seq_len, 7)
																									__UpperCamelCase       					=     self.model(
																									    input_ids=input_ids_example.unsqueeze(0     )       ,		attention_mask=attention_mask_example.unsqueeze(0     )       ,		token_type_ids=token_type_ids_example.unsqueeze(0     )       ,		)
																									__UpperCamelCase       					=     outputs.logits

																									if self.aggregate:
																															all_aggregations.append(outputs.logits_aggregation     )

																									all_logits.append(SCREAMING_SNAKE_CASE_     )

																									__UpperCamelCase       					=     torch.distributions.Bernoulli(logits=SCREAMING_SNAKE_CASE_     )
																									__UpperCamelCase       					=     dist_per_token.probs * attention_mask_example.type(torch.floataa     ).to(
																									    dist_per_token.probs.device     )

																									__UpperCamelCase       					=     collections.defaultdict(SCREAMING_SNAKE_CASE_     )
																									for i, p in enumerate(probabilities.squeeze().tolist()     ):
																															__UpperCamelCase       					=     token_type_ids_example[:, 0].tolist()[i]
																															__UpperCamelCase       					=     token_type_ids_example[:, 1].tolist()[i] - 1
																															__UpperCamelCase       					=     token_type_ids_example[:, 2].tolist()[i] - 1
																															if col >= 0 and row >= 0 and segment_id == 1:
																																					coords_to_probs[(col, row)].append(SCREAMING_SNAKE_CASE_     )

																									__UpperCamelCase       					=     {key: np.array(coords_to_probs[key]     ).mean() > 0.5 for key in coords_to_probs}

																			__UpperCamelCase       					=     torch.cat(tuple(SCREAMING_SNAKE_CASE_     )       ,		0     )

																			return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(SCREAMING_SNAKE_CASE_     )       ,		0     ))
													else:
																			__UpperCamelCase       					=     []
																			__UpperCamelCase       					=     []
																			__UpperCamelCase       					=     None
																			__UpperCamelCase       					=     inputs['''input_ids'''].shape[0]

																			__UpperCamelCase       					=     inputs['''input_ids''']
																			__UpperCamelCase       					=     inputs['''attention_mask''']
																			__UpperCamelCase       					=     inputs['''token_type_ids'''].numpy()
																			__UpperCamelCase       					=     None

																			for index in range(SCREAMING_SNAKE_CASE_     ):
																									# If sequences have already been processed, the token type IDs will be created according to the previous
																									# answer.
																									if prev_answers is not None:
																															__UpperCamelCase       					=     token_type_ids_example[:, 3]  # shape (seq_len,)
																															__UpperCamelCase       					=     np.zeros_like(SCREAMING_SNAKE_CASE_       ,		dtype=np.intaa     )  # shape (seq_len,)

																															__UpperCamelCase       					=     token_type_ids[index]  # shape (seq_len, 7)
																															for i in range(model_labels.shape[0]     ):
																																					__UpperCamelCase       					=     token_type_ids_example[:, 0].tolist()[i]
																																					__UpperCamelCase       					=     token_type_ids_example[:, 1].tolist()[i] - 1
																																					__UpperCamelCase       					=     token_type_ids_example[:, 2].tolist()[i] - 1

																																					if row_id >= 0 and col_id >= 0 and segment_id == 1:
																																											__UpperCamelCase       					=     int(prev_answers[(col_id, row_id)]     )

																															__UpperCamelCase       					=     model_labels

																									__UpperCamelCase       					=     input_ids[index]
																									__UpperCamelCase       					=     attention_mask[index]  # shape (seq_len,)
																									__UpperCamelCase       					=     token_type_ids[index]  # shape (seq_len, 7)
																									__UpperCamelCase       					=     self.model(
																									    input_ids=np.expand_dims(SCREAMING_SNAKE_CASE_       ,		axis=0     )       ,		attention_mask=np.expand_dims(SCREAMING_SNAKE_CASE_       ,		axis=0     )       ,		token_type_ids=np.expand_dims(SCREAMING_SNAKE_CASE_       ,		axis=0     )       ,		)
																									__UpperCamelCase       					=     outputs.logits

																									if self.aggregate:
																															all_aggregations.append(outputs.logits_aggregation     )

																									all_logits.append(SCREAMING_SNAKE_CASE_     )

																									__UpperCamelCase       					=     tfp.distributions.Bernoulli(logits=SCREAMING_SNAKE_CASE_     )
																									__UpperCamelCase       					=     dist_per_token.probs_parameter() * tf.cast(SCREAMING_SNAKE_CASE_       ,		tf.floataa     )

																									__UpperCamelCase       					=     collections.defaultdict(SCREAMING_SNAKE_CASE_     )
																									__UpperCamelCase       					=     token_type_ids_example
																									for i, p in enumerate(tf.squeeze(SCREAMING_SNAKE_CASE_     ).numpy().tolist()     ):
																															__UpperCamelCase       					=     token_type_ids_example[:, 0].tolist()[i]
																															__UpperCamelCase       					=     token_type_ids_example[:, 1].tolist()[i] - 1
																															__UpperCamelCase       					=     token_type_ids_example[:, 2].tolist()[i] - 1
																															if col >= 0 and row >= 0 and segment_id == 1:
																																					coords_to_probs[(col, row)].append(SCREAMING_SNAKE_CASE_     )

																									__UpperCamelCase       					=     {key: np.array(coords_to_probs[key]     ).mean() > 0.5 for key in coords_to_probs}

																			__UpperCamelCase       					=     tf.concat(tuple(SCREAMING_SNAKE_CASE_     )       ,		0     )

																			return (logits_batch,) if not self.aggregate else (logits_batch, tf.concat(tuple(SCREAMING_SNAKE_CASE_     )       ,		0     ))



							def __call__(					self       ,		*SCREAMING_SNAKE_CASE_       ,		**SCREAMING_SNAKE_CASE_     )->				str:





													'''simple docstring'''






													__UpperCamelCase       					=     self._args_parser(*SCREAMING_SNAKE_CASE_       ,		**SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     super().__call__(SCREAMING_SNAKE_CASE_       ,		**SCREAMING_SNAKE_CASE_     )
													if len(SCREAMING_SNAKE_CASE_     ) == 1:
																			return results[0]
													return results



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_=None       ,		SCREAMING_SNAKE_CASE_=None       ,		SCREAMING_SNAKE_CASE_=None       ,		**SCREAMING_SNAKE_CASE_     )->				Optional[int]:





													'''simple docstring'''






													__UpperCamelCase       					=     {}
													if padding is not None:
																			__UpperCamelCase       					=     padding
													if truncation is not None:
																			__UpperCamelCase       					=     truncation

													__UpperCamelCase       					=     {}
													if sequential is not None:
																			__UpperCamelCase       					=     sequential
													return preprocess_params, forward_params, {}



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_=None       ,		SCREAMING_SNAKE_CASE_=True       ,		SCREAMING_SNAKE_CASE_=None     )->				str:





													'''simple docstring'''






													if truncation is None:
																			if self.type == "tapas":
																									__UpperCamelCase       					=     '''drop_rows_to_fit'''
																			else:
																									__UpperCamelCase       					=     '''do_not_truncate'''

													__UpperCamelCase					,							__UpperCamelCase       					=     pipeline_input['''table'''], pipeline_input['''query''']
													if table.empty:
																			raise ValueError('''table is empty'''     )
													if query is None or query == "":
																			raise ValueError('''query is empty'''     )
													__UpperCamelCase       					=     self.tokenizer(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		return_tensors=self.framework       ,		truncation=SCREAMING_SNAKE_CASE_       ,		padding=SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     table
													return inputs



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_=False     )->				Optional[Any]:





													'''simple docstring'''






													__UpperCamelCase       					=     model_inputs.pop('''table'''     )

													if self.type == "tapas":
																			if sequential:
																									__UpperCamelCase       					=     self.sequential_inference(**SCREAMING_SNAKE_CASE_     )
																			else:
																									__UpperCamelCase       					=     self.batch_inference(**SCREAMING_SNAKE_CASE_     )
													else:
																			__UpperCamelCase       					=     self.model.generate(**SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     {'''model_inputs''': model_inputs, '''table''': table, '''outputs''': outputs}
													return model_outputs



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_     )->				Tuple:





													'''simple docstring'''






													__UpperCamelCase       					=     model_outputs['''model_inputs''']
													__UpperCamelCase       					=     model_outputs['''table''']
													__UpperCamelCase       					=     model_outputs['''outputs''']
													if self.type == "tapas":
																			if self.aggregate:
																									__UpperCamelCase					,							__UpperCamelCase       					=     outputs[:2]
																									__UpperCamelCase       					=     self.tokenizer.convert_logits_to_predictions(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )
																									__UpperCamelCase					,							__UpperCamelCase       					=     predictions
																									__UpperCamelCase       					=     {i: self.model.config.aggregation_labels[pred] for i, pred in enumerate(SCREAMING_SNAKE_CASE_     )}

																									__UpperCamelCase       					=     self.model.config.no_aggregation_label_index
																									__UpperCamelCase       					=     {
																									    i: aggregators[i] + ''' > ''' for i, pred in enumerate(SCREAMING_SNAKE_CASE_     ) if pred != no_agg_label_index
																									}
																			else:
																									__UpperCamelCase       					=     outputs[0]
																									__UpperCamelCase       					=     self.tokenizer.convert_logits_to_predictions(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )
																									__UpperCamelCase       					=     predictions[0]
																									__UpperCamelCase       					=     {}
																									__UpperCamelCase       					=     {}
																			__UpperCamelCase       					=     []
																			for index, coordinates in enumerate(SCREAMING_SNAKE_CASE_     ):
																									__UpperCamelCase       					=     [table.iat[coordinate] for coordinate in coordinates]
																									__UpperCamelCase       					=     aggregators.get(SCREAMING_SNAKE_CASE_       ,		''''''     )
																									__UpperCamelCase       					=     aggregators_prefix.get(SCREAMING_SNAKE_CASE_       ,		''''''     )
																									__UpperCamelCase       					=     {
																									    '''answer''': aggregator_prefix + ''', '''.join(SCREAMING_SNAKE_CASE_     ),
																									    '''coordinates''': coordinates,
																									    '''cells''': [table.iat[coordinate] for coordinate in coordinates],
																									}
																									if aggregator:
																															__UpperCamelCase       					=     aggregator

																									answers.append(SCREAMING_SNAKE_CASE_     )
																			if len(SCREAMING_SNAKE_CASE_     ) == 0:
																									raise PipelineException('''Empty answer'''     )
													else:
																			__UpperCamelCase       					=     [{'''answer''': answer} for answer in self.tokenizer.batch_decode(SCREAMING_SNAKE_CASE_       ,		skip_special_tokens=SCREAMING_SNAKE_CASE_     )]

													return answers if len(SCREAMING_SNAKE_CASE_     ) > 1 else answers[0]






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 		A_	(							snake_case							:				Union[str, Any]	, snake_case							:				str	, snake_case							:				Dict	, snake_case							:				Optional[Any]=None	) ->      Optional[Any]:



						'''simple docstring'''

						__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(snake_case	, 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(snake_case	, dtype=array.dtype	, mode='''w+'''	, shape=array.shape	)
						__UpperCamelCase       					=     array[:]
						file_array.flush()
						return index




def 		A_	(							snake_case							:				Tuple	, snake_case							:				Union[str, Any]	) ->      Dict:



						'''simple docstring'''

						__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(snake_case	, dtype=snake_case	, shape=snake_case	, mode='''r'''	)

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

						return weight




def 		A_	(							snake_case							:				Dict	, snake_case							:				int	) ->      str:



						'''simple docstring'''

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

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

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




def 		A_	(							snake_case							:				Union[str, os.PathLike]	, snake_case							:				Dict[str, torch.Tensor]	) ->      Dict:



						'''simple docstring'''

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

						# Update index
						save_offload_index(snake_case	, snake_case	)






class     SCREAMING_SNAKE_CASE__      (							SCREAMING_SNAKE_CASE_      ):

							"""simple docstring"""






							def __init__(					self       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )->				str:





													'''simple docstring'''






													__UpperCamelCase       					=     dataset
													__UpperCamelCase       					=     prefix



							def __getitem__(					self       ,		SCREAMING_SNAKE_CASE_     )->				Union[str, Any]:





													'''simple docstring'''






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



							def __iter__(					self     )->				Union[str, 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__      (							SCREAMING_SNAKE_CASE_      ):

							"""simple docstring"""






							def __init__(					self       ,		SCREAMING_SNAKE_CASE_ = None       ,		SCREAMING_SNAKE_CASE_ = None       ,		SCREAMING_SNAKE_CASE_ = None       ,		SCREAMING_SNAKE_CASE_=None       ,		)->				Tuple:





													'''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(SCREAMING_SNAKE_CASE_       ,		'''index.json'''     )     ) as f:
																									__UpperCamelCase       					=     json.load(SCREAMING_SNAKE_CASE_     )
													__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       ,		SCREAMING_SNAKE_CASE_     )->				Tuple:





													'''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=SCREAMING_SNAKE_CASE_     ) as f:
																									__UpperCamelCase       					=     f.get_tensor(weight_info.get('''weight_name'''       ,		SCREAMING_SNAKE_CASE_     )     )

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

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



							def __iter__(					self     )->				Dict:





													'''simple docstring'''






													return iter(self.all_keys     )



							def __len__(					self     )->				Dict:





													'''simple docstring'''






													return len(self.all_keys     )





def 		A_	(							snake_case							:				Dict[str, torch.Tensor]	, snake_case							:				List[str]	) ->      Tuple:



						'''simple docstring'''

						__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




