


import logging
import os
from typing import List, Tuple

import numpy as np
import psutil
import torch
import torch.distributed as dist

from transformers import RagRetriever


__magic_name__  	=     logging.getLogger(__name__)





class        lowerCAmelCase__ (					lowercase__  ):






						"""simple docstring"""




						def __init__(       self ,				a_ ,				a_ ,				a_ ,				a_=None      ):
							super().__init__(
							    SCREAMING_SNAKE_CASE_ ,				question_encoder_tokenizer=SCREAMING_SNAKE_CASE_ ,				generator_tokenizer=SCREAMING_SNAKE_CASE_ ,				index=SCREAMING_SNAKE_CASE_ ,				init_retrieval=SCREAMING_SNAKE_CASE_ ,				)
							lowerCamelCase_					:				List[Any]              =  None




						def 	_UpperCamelCase					(       self ,				a_      ):
							logger.info("initializing retrieval"      )

							# initializing a separate process group for retrieval as the default
							# nccl backend doesn't support gather/scatter operations while gloo
							# is too slow to replace nccl for the core gpu communication
							if dist.is_initialized():
								logger.info("dist initialized"      )
								# needs to be set manually
								lowerCamelCase_					:				Optional[int]              =  self._infer_socket_ifname()
								# avoid clash with the NCCL port
								lowerCamelCase_					:				Union[str, Any]              =  str(distributed_port + 1      )
								lowerCamelCase_					:				Dict              =  dist.new_group(ranks=SCREAMING_SNAKE_CASE_ ,				backend="gloo"      )

							# initialize retriever only on the main worker
							if not dist.is_initialized() or self._is_main():
								logger.info("dist not initialized / main"      )
								self.index.init_index()

							# all processes wait untill the retriever is initialized by the main process
							if dist.is_initialized():
								torch.distributed.barrier(group=self.process_group      )




						def 	_UpperCamelCase					(       self      ):
							return dist.get_rank(group=self.process_group      ) == 0




						def 	_UpperCamelCase					(       self ,				a_ ,				a_ ,				a_=torch.floataa      ):
							lowerCamelCase_					:				Optional[Any]              =  torch.empty(SCREAMING_SNAKE_CASE_ ,				dtype=SCREAMING_SNAKE_CASE_      )
							dist.scatter(SCREAMING_SNAKE_CASE_ ,				src=0 ,				scatter_list=SCREAMING_SNAKE_CASE_ ,				group=self.process_group      )
							return target_tensor




						def 	_UpperCamelCase					(       self      ):
							lowerCamelCase_					:				Dict              =  psutil.net_if_addrs()
							# a hacky way to deal with varying network interface names
							lowerCamelCase_					:				Optional[Any]              =  next((addr for addr in addrs if addr.startswith("e"      )) ,				SCREAMING_SNAKE_CASE_      )
							return ifname




						def 	_UpperCamelCase					(       self ,				a_ ,				a_      ):


							if not dist.is_initialized():
								lowerCamelCase_       ,lowerCamelCase_					:				Optional[Any]              =  self._main_retrieve(SCREAMING_SNAKE_CASE_ ,				SCREAMING_SNAKE_CASE_      )
								return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(SCREAMING_SNAKE_CASE_      )

							# distributed training
							lowerCamelCase_					:				Optional[int]              =  dist.get_world_size(group=self.process_group      )

							# gather logic
							lowerCamelCase_					:				Any              =  None
							if self._is_main():
								lowerCamelCase_					:				List[Any]              =  [torch.empty(question_hidden_states.shape ,				dtype=torch.floataa      ) for _ in range(SCREAMING_SNAKE_CASE_      )]
							dist.gather(torch.tensor(SCREAMING_SNAKE_CASE_      ) ,				dst=0 ,				gather_list=SCREAMING_SNAKE_CASE_ ,				group=self.process_group      )

							# scatter logic
							lowerCamelCase_					:				Any              =  question_hidden_states.shape[0]
							lowerCamelCase_					:				Union[str, Any]              =  []
							lowerCamelCase_					:				int              =  []
							if self._is_main():
								assert len(SCREAMING_SNAKE_CASE_      ) == world_size
								lowerCamelCase_       ,lowerCamelCase_					:				int              =  self._main_retrieve(torch.cat(SCREAMING_SNAKE_CASE_      ).numpy() ,				SCREAMING_SNAKE_CASE_      )
								lowerCamelCase_       ,lowerCamelCase_					:				Tuple              =  torch.tensor(SCREAMING_SNAKE_CASE_      ), torch.tensor(SCREAMING_SNAKE_CASE_      )
								lowerCamelCase_					:				Union[str, Any]              =  self._chunk_tensor(SCREAMING_SNAKE_CASE_ ,				SCREAMING_SNAKE_CASE_      )
								lowerCamelCase_					:				List[Any]              =  self._chunk_tensor(SCREAMING_SNAKE_CASE_ ,				SCREAMING_SNAKE_CASE_      )
							lowerCamelCase_					:				Dict              =  self._scattered(SCREAMING_SNAKE_CASE_ ,				[n_queries, n_docs] ,				target_type=torch.intaa      )
							lowerCamelCase_					:				Dict              =  self._scattered(SCREAMING_SNAKE_CASE_ ,				[n_queries, n_docs, question_hidden_states.shape[1]]      )

							return retrieved_doc_embeds.numpy(), doc_ids.numpy(), self.index.get_doc_dicts(SCREAMING_SNAKE_CASE_      )



"""simple docstring"""






import inspect
import tempfile
from collections import OrderedDict, UserDict
from collections.abc import MutableMapping
from contextlib import ExitStack, contextmanager
from dataclasses import fields
from enum import Enum
from typing import Any, ContextManager, List, Tuple

import numpy as np

from .import_utils import is_flax_available, is_tf_available, is_torch_available, is_torch_fx_proxy


if is_flax_available():
					import jax.numpy as jnp



class 			lowercase					( lowercase__		):






			def __get__(self  :	str      ,SCREAMING_SNAKE_CASE_  :	List[Any]      ,SCREAMING_SNAKE_CASE_  :	Optional[Any]=None			)       ->	int:





				"""simple docstring"""


				if obj is None:
					return self
				if self.fget is None:
					raise AttributeError('''unreadable attribute'''			)
				lowerCAmelCase												=  '''__cached_''' + self.fget.__name__
				lowerCAmelCase												=  getattr(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)
				if cached is None:
					lowerCAmelCase												=  self.fget(SCREAMING_SNAKE_CASE_			)
					setattr(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)
				return cached





def __magic_name__	(				_lowerCamelCase:		Union[str, Any]			)					->						List[str]:

	'''simple docstring'''




	lowerCAmelCase												=  val.lower()
	if val in {"y", "yes", "t", "true", "on", "1"}:
		return 1
	if val in {"n", "no", "f", "false", "off", "0"}:
		return 0
	raise ValueError(F"""invalid truth value {val!r}"""			)




def __magic_name__	(				_lowerCamelCase:		int			)					->						Union[str, Any]:

	'''simple docstring'''




	if is_torch_fx_proxy(_lowerCamelCase			):
		return True
	if is_torch_available():
		import torch

		if isinstance(_lowerCamelCase,					torch.Tensor			):
			return True
	if is_tf_available():
		import tensorflow as tf

		if isinstance(_lowerCamelCase,					tf.Tensor			):
			return True

	if is_flax_available():
		import jax.numpy as jnp
		from jax.core import Tracer

		if isinstance(_lowerCamelCase,					(jnp.ndarray, Tracer)			):
			return True

	return isinstance(_lowerCamelCase,					np.ndarray			)




def __magic_name__	(				_lowerCamelCase:		Optional[Any]			)					->						Tuple:

	'''simple docstring'''




	return isinstance(_lowerCamelCase,					np.ndarray			)




def __magic_name__	(				_lowerCamelCase:		str			)					->						List[Any]:

	'''simple docstring'''




	return _is_numpy(_lowerCamelCase			)




def __magic_name__	(				_lowerCamelCase:		Dict			)					->						Optional[int]:

	'''simple docstring'''




	import torch

	return isinstance(_lowerCamelCase,					torch.Tensor			)




def __magic_name__	(				_lowerCamelCase:		List[Any]			)					->						Union[str, Any]:

	'''simple docstring'''




	return False if not is_torch_available() else _is_torch(_lowerCamelCase			)




def __magic_name__	(				_lowerCamelCase:		Optional[Any]			)					->						List[Any]:

	'''simple docstring'''




	import torch

	return isinstance(_lowerCamelCase,					torch.device			)




def __magic_name__	(				_lowerCamelCase:		List[Any]			)					->						int:

	'''simple docstring'''




	return False if not is_torch_available() else _is_torch_device(_lowerCamelCase			)




def __magic_name__	(				_lowerCamelCase:		str			)					->						Dict:

	'''simple docstring'''




	import torch

	if isinstance(_lowerCamelCase,					_lowerCamelCase			):
		if hasattr(_lowerCamelCase,					_lowerCamelCase			):
			lowerCAmelCase												=  getattr(_lowerCamelCase,					_lowerCamelCase			)
		else:
			return False
	return isinstance(_lowerCamelCase,					torch.dtype			)




def __magic_name__	(				_lowerCamelCase:		Optional[Any]			)					->						Tuple:

	'''simple docstring'''




	return False if not is_torch_available() else _is_torch_dtype(_lowerCamelCase			)




def __magic_name__	(				_lowerCamelCase:		Tuple			)					->						Tuple:

	'''simple docstring'''




	import tensorflow as tf

	return isinstance(_lowerCamelCase,					tf.Tensor			)




def __magic_name__	(				_lowerCamelCase:		int			)					->						Tuple:

	'''simple docstring'''




	return False if not is_tf_available() else _is_tensorflow(_lowerCamelCase			)




def __magic_name__	(				_lowerCamelCase:		List[Any]			)					->						Union[str, Any]:

	'''simple docstring'''




	import tensorflow as tf

	# the `is_symbolic_tensor` predicate is only available starting with TF 2.14
	if hasattr(_lowerCamelCase,					'''is_symbolic_tensor'''			):
		return tf.is_symbolic_tensor(_lowerCamelCase			)
	return type(_lowerCamelCase			) == tf.Tensor




def __magic_name__	(				_lowerCamelCase:		Union[str, Any]			)					->						str:

	'''simple docstring'''




	return False if not is_tf_available() else _is_tf_symbolic_tensor(_lowerCamelCase			)




def __magic_name__	(				_lowerCamelCase:		List[Any]			)					->						List[Any]:

	'''simple docstring'''




	import jax.numpy as jnp  # noqa: F811

	return isinstance(_lowerCamelCase,					jnp.ndarray			)




def __magic_name__	(				_lowerCamelCase:		Optional[Any]			)					->						Optional[int]:

	'''simple docstring'''




	return False if not is_flax_available() else _is_jax(_lowerCamelCase			)




def __magic_name__	(				_lowerCamelCase:		List[str]			)					->						Any:

	'''simple docstring'''




	if isinstance(_lowerCamelCase,					(dict, UserDict)			):
		return {k: to_py_obj(_lowerCamelCase			) for k, v in obj.items()}
	elif isinstance(_lowerCamelCase,					(list, tuple)			):
		return [to_py_obj(_lowerCamelCase			) for o in obj]
	elif is_tf_tensor(_lowerCamelCase			):
		return obj.numpy().tolist()
	elif is_torch_tensor(_lowerCamelCase			):
		return obj.detach().cpu().tolist()
	elif is_jax_tensor(_lowerCamelCase			):
		return np.asarray(_lowerCamelCase			).tolist()
	elif isinstance(_lowerCamelCase,					(np.ndarray, np.number)			):  # tolist also works on 0d np arrays
		return obj.tolist()
	else:
		return obj




def __magic_name__	(				_lowerCamelCase:		Optional[int]			)					->						Any:

	'''simple docstring'''




	if isinstance(_lowerCamelCase,					(dict, UserDict)			):
		return {k: to_numpy(_lowerCamelCase			) for k, v in obj.items()}
	elif isinstance(_lowerCamelCase,					(list, tuple)			):
		return np.array(_lowerCamelCase			)
	elif is_tf_tensor(_lowerCamelCase			):
		return obj.numpy()
	elif is_torch_tensor(_lowerCamelCase			):
		return obj.detach().cpu().numpy()
	elif is_jax_tensor(_lowerCamelCase			):
		return np.asarray(_lowerCamelCase			)
	else:
		return obj



class 			lowercase					( lowercase__		):






			def 					UpperCAmelCase							(self  :	Any			)       ->	List[Any]:





				"""simple docstring"""


				lowerCAmelCase												=  fields(self			)

				# Safety and consistency checks
				if not len(SCREAMING_SNAKE_CASE_			):
					raise ValueError(F"""{self.__class__.__name__} has no fields."""			)
				if not all(field.default is None for field in class_fields[1:]			):
					raise ValueError(F"""{self.__class__.__name__} should not have more than one required field."""			)

				lowerCAmelCase												=  getattr(self      ,class_fields[0].name			)
				lowerCAmelCase												=  all(getattr(self      ,field.name			) is None for field in class_fields[1:]			)

				if other_fields_are_none and not is_tensor(SCREAMING_SNAKE_CASE_			):
					if isinstance(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			):
						lowerCAmelCase												=  first_field.items()
						lowerCAmelCase												=  True
					else:
						try:
							lowerCAmelCase												=  iter(SCREAMING_SNAKE_CASE_			)
							lowerCAmelCase												=  True
						except TypeError:
							lowerCAmelCase												=  False

            # if we provided an iterator as first field and the iterator is a (key, value) iterator
            # set the associated fields
					if first_field_iterator:
						for idx, element in enumerate(SCREAMING_SNAKE_CASE_			):
							if (
							    not isinstance(SCREAMING_SNAKE_CASE_      ,(list, tuple)			)
							    or not len(SCREAMING_SNAKE_CASE_			) == 2
							    or not isinstance(element[0]      ,SCREAMING_SNAKE_CASE_			)
							):
								if idx == 0:
									# If we do not have an iterator of key/values, set it as attribute
									lowerCAmelCase												=  first_field
								else:
									# If we have a mixed iterator, raise an error
									raise ValueError(
									    F"""Cannot set key/value for {element}. It needs to be a tuple (key, value)."""			)
								break
							setattr(self      ,element[0]      ,element[1]			)
							if element[1] is not None:
								lowerCAmelCase												=  element[1]
					elif first_field is not None:
						lowerCAmelCase												=  first_field
				else:
					for field in class_fields:
						lowerCAmelCase												=  getattr(self      ,field.name			)
						if v is not None:
							lowerCAmelCase												=  v






			def __delitem__(self  :	Optional[Any]      ,*SCREAMING_SNAKE_CASE_  :	Tuple      ,**SCREAMING_SNAKE_CASE_  :	Dict			)       ->	Tuple:





				"""simple docstring"""


				raise Exception(F"""You cannot use ``__delitem__`` on a {self.__class__.__name__} instance."""			)






			def 					UpperCAmelCase							(self  :	List[Any]      ,*SCREAMING_SNAKE_CASE_  :	Any      ,**SCREAMING_SNAKE_CASE_  :	int			)       ->	List[Any]:





				"""simple docstring"""


				raise Exception(F"""You cannot use ``setdefault`` on a {self.__class__.__name__} instance."""			)






			def 					UpperCAmelCase							(self  :	Union[str, Any]      ,*SCREAMING_SNAKE_CASE_  :	List[Any]      ,**SCREAMING_SNAKE_CASE_  :	Dict			)       ->	Union[str, Any]:





				"""simple docstring"""


				raise Exception(F"""You cannot use ``pop`` on a {self.__class__.__name__} instance."""			)






			def 					UpperCAmelCase							(self  :	Any      ,*SCREAMING_SNAKE_CASE_  :	Optional[Any]      ,**SCREAMING_SNAKE_CASE_  :	Dict			)       ->	Any:





				"""simple docstring"""


				raise Exception(F"""You cannot use ``update`` on a {self.__class__.__name__} instance."""			)






			def __getitem__(self  :	int      ,SCREAMING_SNAKE_CASE_  :	Tuple			)       ->	Optional[int]:





				"""simple docstring"""


				if isinstance(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			):
					lowerCAmelCase												=  dict(self.items()			)
					return inner_dict[k]
				else:
					return self.to_tuple()[k]






			def __setattr__(self  :	int      ,SCREAMING_SNAKE_CASE_  :	Any      ,SCREAMING_SNAKE_CASE_  :	int			)       ->	List[str]:





				"""simple docstring"""


				if name in self.keys() and value is not None:
					# Don't call self.__setitem__ to avoid recursion errors
					super().__setitem__(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)
				super().__setattr__(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)






			def __setitem__(self  :	List[str]      ,SCREAMING_SNAKE_CASE_  :	str      ,SCREAMING_SNAKE_CASE_  :	Tuple			)       ->	List[Any]:





				"""simple docstring"""


				super().__setitem__(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)
				# Don't call self.__setattr__ to avoid recursion errors
				super().__setattr__(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	List[str]			)       ->	Tuple[Any]:





				"""simple docstring"""


				return tuple(self[k] for k in self.keys()			)





class 			lowercase					( lowercase__   ,lowercase__		):






			@classmethod
			def 					UpperCAmelCase							(cls  :	int      ,SCREAMING_SNAKE_CASE_  :	Any			)       ->	Dict:





				"""simple docstring"""


				raise ValueError(
				    F"""{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys()			)}"""			)





class 			lowercase					( lowercase__		):
			lowercase        =     '''longest'''
			lowercase        =     '''max_length'''
			lowercase        =     '''do_not_pad'''





class 			lowercase					( lowercase__		):
			lowercase        =     '''pt'''
			lowercase        =     '''tf'''
			lowercase        =     '''np'''
			lowercase        =     '''jax'''





class 			lowercase					:






			def __init__(self  :	Optional[Any]      ,SCREAMING_SNAKE_CASE_  :	List[ContextManager]			)       ->	Optional[Any]:





				"""simple docstring"""


				lowerCAmelCase												=  context_managers
				lowerCAmelCase												=  ExitStack()






			def __enter__(self  :	int			)       ->	Dict:





				"""simple docstring"""


				for context_manager in self.context_managers:
					self.stack.enter_context(SCREAMING_SNAKE_CASE_			)






			def __exit__(self  :	Tuple      ,*SCREAMING_SNAKE_CASE_  :	Tuple      ,**SCREAMING_SNAKE_CASE_  :	int			)       ->	str:





				"""simple docstring"""


				self.stack.__exit__(*SCREAMING_SNAKE_CASE_      ,**SCREAMING_SNAKE_CASE_			)





def __magic_name__	(				_lowerCamelCase:		str			)					->						List[Any]:

	'''simple docstring'''




	lowerCAmelCase												=  infer_framework(_lowerCamelCase			)
	if framework == "tf":
		lowerCAmelCase												=  inspect.signature(model_class.call			)  # TensorFlow models
	elif framework == "pt":
		lowerCAmelCase												=  inspect.signature(model_class.forward			)  # PyTorch models
	else:
		lowerCAmelCase												=  inspect.signature(model_class.__call__			)  # Flax models

	for p in signature.parameters:
		if p == "return_loss" and signature.parameters[p].default is True:
			return True

	return False




def __magic_name__	(				_lowerCamelCase:		Any			)					->						List[str]:

	'''simple docstring'''




	lowerCAmelCase												=  model_class.__name__
	lowerCAmelCase												=  infer_framework(_lowerCamelCase			)
	if framework == "tf":
		lowerCAmelCase												=  inspect.signature(model_class.call			)  # TensorFlow models
	elif framework == "pt":
		lowerCAmelCase												=  inspect.signature(model_class.forward			)  # PyTorch models
	else:
		lowerCAmelCase												=  inspect.signature(model_class.__call__			)  # Flax models

	if "QuestionAnswering" in model_name:
		return [p for p in signature.parameters if "label" in p or p in ("start_positions", "end_positions")]
	else:
		return [p for p in signature.parameters if "label" in p]




def __magic_name__	(				_lowerCamelCase:		MutableMapping,					_lowerCamelCase:		str = "",					_lowerCamelCase:		str = "."			)					->						Optional[int]:

	'''simple docstring'''




	def _flatten_dict(_lowerCamelCase:		Any,					_lowerCamelCase:		Optional[int]="",					_lowerCamelCase:		Union[str, Any]="."			):
		for k, v in d.items():
			lowerCAmelCase												=  str(_lowerCamelCase			) + delimiter + str(_lowerCamelCase			) if parent_key else k
			if v and isinstance(_lowerCamelCase,					_lowerCamelCase			):
				yield from flatten_dict(_lowerCamelCase,					_lowerCamelCase,					delimiter=_lowerCamelCase			).items()
			else:
				yield key, v

	return dict(_flatten_dict(_lowerCamelCase,					_lowerCamelCase,					_lowerCamelCase			)			)




@contextmanager
def __magic_name__	(				_lowerCamelCase:		Union[str, Any],					_lowerCamelCase:		bool = False			)					->						Tuple:

	'''simple docstring'''




	if use_temp_dir:
		with tempfile.TemporaryDirectory() as tmp_dir:
			yield tmp_dir
	else:
		yield working_dir




def __magic_name__	(				_lowerCamelCase:		Optional[int],					_lowerCamelCase:		int=None			)					->						List[Any]:

	'''simple docstring'''




	if is_numpy_array(_lowerCamelCase			):
		return np.transpose(_lowerCamelCase,					axes=_lowerCamelCase			)
	elif is_torch_tensor(_lowerCamelCase			):
		return array.T if axes is None else array.permute(*_lowerCamelCase			)
	elif is_tf_tensor(_lowerCamelCase			):
		import tensorflow as tf

		return tf.transpose(_lowerCamelCase,					perm=_lowerCamelCase			)
	elif is_jax_tensor(_lowerCamelCase			):
		return jnp.transpose(_lowerCamelCase,					axes=_lowerCamelCase			)
	else:
		raise ValueError(F"""Type not supported for transpose: {type(_lowerCamelCase			)}."""			)




def __magic_name__	(				_lowerCamelCase:		List[Any],					_lowerCamelCase:		Any			)					->						List[str]:

	'''simple docstring'''




	if is_numpy_array(_lowerCamelCase			):
		return np.reshape(_lowerCamelCase,					_lowerCamelCase			)
	elif is_torch_tensor(_lowerCamelCase			):
		return array.reshape(*_lowerCamelCase			)
	elif is_tf_tensor(_lowerCamelCase			):
		import tensorflow as tf

		return tf.reshape(_lowerCamelCase,					_lowerCamelCase			)
	elif is_jax_tensor(_lowerCamelCase			):
		return jnp.reshape(_lowerCamelCase,					_lowerCamelCase			)
	else:
		raise ValueError(F"""Type not supported for reshape: {type(_lowerCamelCase			)}."""			)




def __magic_name__	(				_lowerCamelCase:		int,					_lowerCamelCase:		Tuple=None			)					->						List[str]:

	'''simple docstring'''




	if is_numpy_array(_lowerCamelCase			):
		return np.squeeze(_lowerCamelCase,					axis=_lowerCamelCase			)
	elif is_torch_tensor(_lowerCamelCase			):
		return array.squeeze() if axis is None else array.squeeze(dim=_lowerCamelCase			)
	elif is_tf_tensor(_lowerCamelCase			):
		import tensorflow as tf

		return tf.squeeze(_lowerCamelCase,					axis=_lowerCamelCase			)
	elif is_jax_tensor(_lowerCamelCase			):
		return jnp.squeeze(_lowerCamelCase,					axis=_lowerCamelCase			)
	else:
		raise ValueError(F"""Type not supported for squeeze: {type(_lowerCamelCase			)}."""			)




def __magic_name__	(				_lowerCamelCase:		List[str],					_lowerCamelCase:		int			)					->						List[Any]:

	'''simple docstring'''




	if is_numpy_array(_lowerCamelCase			):
		return np.expand_dims(_lowerCamelCase,					_lowerCamelCase			)
	elif is_torch_tensor(_lowerCamelCase			):
		return array.unsqueeze(dim=_lowerCamelCase			)
	elif is_tf_tensor(_lowerCamelCase			):
		import tensorflow as tf

		return tf.expand_dims(_lowerCamelCase,					axis=_lowerCamelCase			)
	elif is_jax_tensor(_lowerCamelCase			):
		return jnp.expand_dims(_lowerCamelCase,					axis=_lowerCamelCase			)
	else:
		raise ValueError(F"""Type not supported for expand_dims: {type(_lowerCamelCase			)}."""			)




def __magic_name__	(				_lowerCamelCase:		Any			)					->						Dict:

	'''simple docstring'''




	if is_numpy_array(_lowerCamelCase			):
		return np.size(_lowerCamelCase			)
	elif is_torch_tensor(_lowerCamelCase			):
		return array.numel()
	elif is_tf_tensor(_lowerCamelCase			):
		import tensorflow as tf

		return tf.size(_lowerCamelCase			)
	elif is_jax_tensor(_lowerCamelCase			):
		return array.size
	else:
		raise ValueError(F"""Type not supported for expand_dims: {type(_lowerCamelCase			)}."""			)




def __magic_name__	(				_lowerCamelCase:		Optional[Any],					_lowerCamelCase:		Optional[int]			)					->						Optional[int]:

	'''simple docstring'''




	for key, value in auto_map.items():
		if isinstance(_lowerCamelCase,					(tuple, list)			):
			lowerCAmelCase												=  [F"""{repo_id}--{v}""" if (v is not None and '''--''' not in v) else v for v in value]
		elif value is not None and "--" not in value:
			lowerCAmelCase												=  F"""{repo_id}--{value}"""

	return auto_map




def __magic_name__	(				_lowerCamelCase:		str			)					->						Any:

	'''simple docstring'''




	for base_class in inspect.getmro(_lowerCamelCase			):
		lowerCAmelCase												=  base_class.__module__
		lowerCAmelCase												=  base_class.__name__
		if module.startswith('''tensorflow'''			) or module.startswith('''keras'''			) or name == "TFPreTrainedModel":
			return "tf"
		elif module.startswith('''torch'''			) or name == "PreTrainedModel":
			return "pt"
		elif module.startswith('''flax'''			) or module.startswith('''jax'''			) or name == "FlaxPreTrainedModel":
			return "flax"
	else:
		raise TypeError(F"""Could not infer framework from class {model_class}."""			)


