"""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    a_   (  __UpperCamelCase      ):




					def __get__(						self    :  Optional[Any]	,    snake_case__    :  Union[str, Any]	,    snake_case__    :  List[str]=None  ):
							# See docs.python.org/3/howto/descriptor.html#properties
							if obj is None:
									return self
							if self.fget is None:
									raise AttributeError("""unreadable attribute"""  )
							lowerCAmelCase__   = """__cached_""" + self.fget.__name__
							lowerCAmelCase__   = getattr(snake_case__	,    snake_case__	,    snake_case__  )
							if cached is None:
									lowerCAmelCase__   = self.fget(snake_case__  )
									setattr(snake_case__	,    snake_case__	,    snake_case__  )
							return cached







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




		return isinstance(lowerCAmelCase__      ,	np.ndarray  )







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




		return _is_numpy(lowerCAmelCase__  )







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




		import torch

		return isinstance(lowerCAmelCase__      ,	torch.Tensor  )







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




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







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




		import torch

		return isinstance(lowerCAmelCase__      ,	torch.device  )







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




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







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




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







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




		import tensorflow as tf

		return isinstance(lowerCAmelCase__      ,	tf.Tensor  )







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




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







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




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







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




		import jax.numpy as jnp  # noqa: F811

		return isinstance(lowerCAmelCase__      ,	jnp.ndarray  )







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""simple docstring"""




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







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""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    a_   (  __UpperCamelCase      ):




					def     _SCREAMING_SNAKE_CASE    (						self    :  Optional[int]  ):
							lowerCAmelCase__   = fields(self  )

							# Safety and consistency checks
							if not len(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(snake_case__  ):
									if isinstance(snake_case__	,    snake_case__  ):
											lowerCAmelCase__   = first_field.items()
											lowerCAmelCase__   = True
									else:
											try:
													lowerCAmelCase__   = iter(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(snake_case__  ):
													if (
													    not isinstance(snake_case__	,    (list, tuple)  )
													    or not len(snake_case__  ) == 2
													    or not isinstance(element[0]	,    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[int]	,    *snake_case__    :  Union[str, Any]	,    **snake_case__    :  Union[str, Any]  ):
							raise Exception(F"""You cannot use ``__delitem__`` on a {self.__class__.__name__} instance."""  )




					def     _SCREAMING_SNAKE_CASE    (						self    :  Optional[int]	,    *snake_case__    :  Tuple	,    **snake_case__    :  List[Any]  ):
							raise Exception(F"""You cannot use ``setdefault`` on a {self.__class__.__name__} instance."""  )




					def     _SCREAMING_SNAKE_CASE    (						self    :  Tuple	,    *snake_case__    :  List[Any]	,    **snake_case__    :  Optional[int]  ):
							raise Exception(F"""You cannot use ``pop`` on a {self.__class__.__name__} instance."""  )




					def     _SCREAMING_SNAKE_CASE    (						self    :  str	,    *snake_case__    :  Optional[int]	,    **snake_case__    :  Tuple  ):
							raise Exception(F"""You cannot use ``update`` on a {self.__class__.__name__} instance."""  )




					def __getitem__(						self    :  int	,    snake_case__    :  Optional[Any]  ):
							if isinstance(snake_case__	,    snake_case__  ):
									lowerCAmelCase__   = dict(self.items()  )
									return inner_dict[k]
							else:
									return self.to_tuple()[k]




					def __setattr__(						self    :  Optional[Any]	,    snake_case__    :  str	,    snake_case__    :  Union[str, Any]  ):
							if name in self.keys() and value is not None:
									# Don't call self.__setitem__ to avoid recursion errors
									super().__setitem__(snake_case__	,    snake_case__  )
							super().__setattr__(snake_case__	,    snake_case__  )




					def __setitem__(						self    :  Union[str, Any]	,    snake_case__    :  str	,    snake_case__    :  Tuple  ):
							# Will raise a KeyException if needed
							super().__setitem__(snake_case__	,    snake_case__  )
							# Don't call self.__setattr__ to avoid recursion errors
							super().__setattr__(snake_case__	,    snake_case__  )




					def     _SCREAMING_SNAKE_CASE    (						self    :  int  ):
							return tuple(self[k] for k in self.keys()  )

class    a_   (  __UpperCamelCase			,		__UpperCamelCase      ):




					@classmethod
					def     _SCREAMING_SNAKE_CASE    (						cls    :  List[Any]	,    snake_case__    :  Optional[int]  ):
							raise ValueError(
							    F"""{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys()  )}"""  )

class    a_   (  __UpperCamelCase      ):
					UpperCamelCase_			:						Tuple      =     'longest'
					UpperCamelCase_			:						Dict      =     'max_length'
					UpperCamelCase_			:						Optional[int]      =     'do_not_pad'

class    a_   (  __UpperCamelCase      ):
					UpperCamelCase_			:						Any      =     'pt'
					UpperCamelCase_			:						Union[str, Any]      =     'tf'
					UpperCamelCase_			:						int      =     'np'
					UpperCamelCase_			:						List[str]      =     'jax'

class    a_   :




					def __init__(						self    :  int	,    snake_case__    :  Dict  ):
							lowerCAmelCase__   = context_managers
							lowerCAmelCase__   = ExitStack()




					def __enter__(						self    :  Any  ):
							for context_manager in self.context_managers:
									self.stack.enter_context(snake_case__  )




					def __exit__(						self    :  Tuple	,    *snake_case__    :  List[Any]	,    **snake_case__    :  Optional[Any]  ):
							self.stack.__exit__(*snake_case__	,    **snake_case__  )







def 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__      ,	lowerCamelCase__ = ""      ,	lowerCamelCase__ = "."  ):





		"""simple docstring"""




		def _flatten_dict(lowerCamelCase__      ,	lowerCamelCase__=""      ,	lowerCamelCase__="."  ):
				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 				_UpperCAmelCase		(							lowerCamelCase__      ,	lowerCamelCase__ = False  ):





		"""simple docstring"""




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







def 				_UpperCAmelCase		(							lowerCamelCase__      ,	lowerCamelCase__=None  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__      ,	lowerCamelCase__  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__      ,	lowerCamelCase__=None  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__      ,	lowerCamelCase__  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__      ,	lowerCamelCase__  ):





		"""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 				_UpperCAmelCase		(							lowerCamelCase__  ):





		"""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}."""  )






"""simple docstring"""


import timeit

import numpy as np

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features.features import _ArrayXD






def  lowercase						(	lowerCAmelCase__						):
			def wrapper(*lowerCAmelCase__	,**lowerCAmelCase__						):
						lowerCamelCase_					=  timeit.default_timer()
						lowerCamelCase_					=  func(*lowerCAmelCase__	,**lowerCAmelCase__						)
						lowerCamelCase_					=  timeit.default_timer() - starttime
						return delta

			lowerCamelCase_					=  func.__name__

			return wrapper






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__=100	,lowerCAmelCase__=None						):
			lowerCamelCase_					=  []
			lowerCamelCase_					=  seq_shapes or {}
			for i in range(lowerCAmelCase__						):
						lowerCamelCase_					=  {}
						for col_id, (k, v) in enumerate(features.items()						):
									if isinstance(lowerCAmelCase__	,_ArrayXD						):
												lowerCamelCase_					=  np.random.rand(*v.shape						).astype(v.dtype						)
									elif isinstance(lowerCAmelCase__	,datasets.Value						):
												if v.dtype == "string":
															lowerCamelCase_					=  '''The small grey turtle was surprisingly fast when challenged.'''
												else:
															lowerCamelCase_					=  np.random.randint(10	,size=1						).astype(v.dtype						).item()
									elif isinstance(lowerCAmelCase__	,datasets.Sequence						):
												while isinstance(lowerCAmelCase__	,datasets.Sequence						):
															lowerCamelCase_					=  v.feature
												lowerCamelCase_					=  seq_shapes[k]
												lowerCamelCase_					=  np.random.rand(*lowerCAmelCase__						).astype(v.dtype						)
									lowerCamelCase_					=  data

						dummy_data.append((i, example)						)

			return dummy_data






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__	,lowerCAmelCase__=100	,lowerCAmelCase__=None						):
			lowerCamelCase_					=  generate_examples(lowerCAmelCase__	,num_examples=lowerCAmelCase__	,seq_shapes=lowerCAmelCase__						)

			with ArrowWriter(features=lowerCAmelCase__	,path=lowerCAmelCase__						) as writer:
						for key, record in dummy_data:
									lowerCamelCase_					=  features.encode_example(lowerCAmelCase__						)
									writer.write(lowerCAmelCase__						)

						lowerCamelCase_		, lowerCamelCase_					=  writer.finalize()

			if not num_final_examples == num_examples:
						raise ValueError(
						    f"Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}."						)

			lowerCamelCase_					=  datasets.Dataset.from_file(filename=lowerCAmelCase__	,info=datasets.DatasetInfo(features=lowerCAmelCase__						)						)

			return dataset



