
"""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 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				def __get__(self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=None    )					->		Optional[int]:



											"""simple docstring"""




											if obj is None:
																		return self
											if self.fget is None:
																		raise AttributeError("""unreadable attribute"""    )
											SCREAMING_SNAKE_CASE__						:       Tuple           =   """__cached_""" + self.fget.__name__
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   getattr(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )
											if cached is None:
																		SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.fget(SCREAMING_SNAKE_CASE__    )
																		setattr(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )
											return cached






def lowercase_  (   _snake_case						):
							SCREAMING_SNAKE_CASE__						:       Dict           =   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 lowercase_  (   _snake_case						):
							if is_torch_fx_proxy(_snake_case						):
														return True
							if is_torch_available():
														import torch

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

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

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

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

							return isinstance(_snake_case   ,np.ndarray						)

def lowercase_  (   _snake_case						):
							return isinstance(_snake_case   ,np.ndarray						)

def lowercase_  (   _snake_case						):
							return _is_numpy(_snake_case						)

def lowercase_  (   _snake_case						):
							import torch

							return isinstance(_snake_case   ,torch.Tensor						)

def lowercase_  (   _snake_case						):
							return False if not is_torch_available() else _is_torch(_snake_case						)

def lowercase_  (   _snake_case						):
							import torch

							return isinstance(_snake_case   ,torch.device						)

def lowercase_  (   _snake_case						):
							return False if not is_torch_available() else _is_torch_device(_snake_case						)

def lowercase_  (   _snake_case						):
							import torch

							if isinstance(_snake_case   ,_snake_case						):
														if hasattr(_snake_case   ,_snake_case						):
																					SCREAMING_SNAKE_CASE__						:       Tuple           =   getattr(_snake_case   ,_snake_case						)
														else:
																					return False
							return isinstance(_snake_case   ,torch.dtype						)

def lowercase_  (   _snake_case						):
							return False if not is_torch_available() else _is_torch_dtype(_snake_case						)

def lowercase_  (   _snake_case						):
							import tensorflow as tf

							return isinstance(_snake_case   ,tf.Tensor						)

def lowercase_  (   _snake_case						):
							return False if not is_tf_available() else _is_tensorflow(_snake_case						)

def lowercase_  (   _snake_case						):
							import tensorflow as tf

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

def lowercase_  (   _snake_case						):
							return False if not is_tf_available() else _is_tf_symbolic_tensor(_snake_case						)

def lowercase_  (   _snake_case						):
							import jax.numpy as jnp  # noqa: F811

							return isinstance(_snake_case   ,jnp.ndarray						)

def lowercase_  (   _snake_case						):
							return False if not is_flax_available() else _is_jax(_snake_case						)

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

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



class 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[Any]           =   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.'''    )

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   getattr(self   ,       class_fields[0].name    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   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__    ):
																									SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   first_field.items()
																									SCREAMING_SNAKE_CASE__						:       Any           =   True
																		else:
																									try:
																																SCREAMING_SNAKE_CASE__						:       List[Any]           =   iter(SCREAMING_SNAKE_CASE__    )
																																SCREAMING_SNAKE_CASE__						:       Optional[int]           =   True
																									except TypeError:
																																SCREAMING_SNAKE_CASE__						:       List[str]           =   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
																																														SCREAMING_SNAKE_CASE__						:       Dict           =   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:
																																							SCREAMING_SNAKE_CASE__						:       Optional[int]           =   element[1]
																		elif first_field is not None:
																									SCREAMING_SNAKE_CASE__						:       Any           =   first_field
											else:
																		for field in class_fields:
																									SCREAMING_SNAKE_CASE__						:       Tuple           =   getattr(self   ,       field.name    )
																									if v is not None:
																																SCREAMING_SNAKE_CASE__						:       Dict           =   v



				def __delitem__(self   ,       *SCREAMING_SNAKE_CASE__   ,       **SCREAMING_SNAKE_CASE__    )					->		Optional[Any]:



											"""simple docstring"""




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



				def 	__magic_name__    (self   ,       *SCREAMING_SNAKE_CASE__   ,       **SCREAMING_SNAKE_CASE__    )					->		List[Any]:



											"""simple docstring"""




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



				def 	__magic_name__    (self   ,       *SCREAMING_SNAKE_CASE__   ,       **SCREAMING_SNAKE_CASE__    )					->		str:



											"""simple docstring"""




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



				def 	__magic_name__    (self   ,       *SCREAMING_SNAKE_CASE__   ,       **SCREAMING_SNAKE_CASE__    )					->		Optional[Any]:



											"""simple docstring"""




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



				def __getitem__(self   ,       SCREAMING_SNAKE_CASE__    )					->		Optional[Any]:



											"""simple docstring"""




											if isinstance(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    ):
																		SCREAMING_SNAKE_CASE__						:       List[Any]           =   dict(self.items()    )
																		return inner_dict[k]
											else:
																		return self.to_tuple()[k]



				def __setattr__(self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Any:



											"""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   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Union[str, 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 	__magic_name__    (self    )					->		Tuple[Any]:



											"""simple docstring"""




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






class 	lowerCAmelCase_						(a__ ,					a__  ):


				"""simple docstring"""



				@classmethod
				def 	__magic_name__    (cls   ,       SCREAMING_SNAKE_CASE__    )					->		Optional[int]:



											"""simple docstring"""




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






class 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				__UpperCamelCase      :						Dict					       =   '''longest'''
				__UpperCamelCase      :						List[Any]					       =   '''max_length'''
				__UpperCamelCase      :						List[str]					       =   '''do_not_pad'''






class 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				__UpperCamelCase      :						str					       =   '''pt'''
				__UpperCamelCase      :						str					       =   '''tf'''
				__UpperCamelCase      :						List[str]					       =   '''np'''
				__UpperCamelCase      :						Optional[Any]					       =   '''jax'''






class 	lowerCAmelCase_						:


				"""simple docstring"""



				def __init__(self   ,       SCREAMING_SNAKE_CASE__    )					->		Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   context_managers
											SCREAMING_SNAKE_CASE__						:       int           =   ExitStack()



				def __enter__(self    )					->		Any:



											"""simple docstring"""




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



				def __exit__(self   ,       *SCREAMING_SNAKE_CASE__   ,       **SCREAMING_SNAKE_CASE__    )					->		List[str]:



											"""simple docstring"""




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






def lowercase_  (   _snake_case						):
							SCREAMING_SNAKE_CASE__						:       Dict           =   infer_framework(_snake_case						)
							if framework == "tf":
														SCREAMING_SNAKE_CASE__						:       Tuple           =   inspect.signature(model_class.call						)  # TensorFlow models
							elif framework == "pt":
														SCREAMING_SNAKE_CASE__						:       Dict           =   inspect.signature(model_class.forward						)  # PyTorch models
							else:
														SCREAMING_SNAKE_CASE__						:       str           =   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 lowercase_  (   _snake_case						):
							SCREAMING_SNAKE_CASE__						:       str           =   model_class.__name__
							SCREAMING_SNAKE_CASE__						:       List[str]           =   infer_framework(_snake_case						)
							if framework == "tf":
														SCREAMING_SNAKE_CASE__						:       Optional[int]           =   inspect.signature(model_class.call						)  # TensorFlow models
							elif framework == "pt":
														SCREAMING_SNAKE_CASE__						:       Tuple           =   inspect.signature(model_class.forward						)  # PyTorch models
							else:
														SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   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 lowercase_  (   _snake_case   ,_snake_case = ""   ,_snake_case = "."						):

							def _flatten_dict(_snake_case   ,_snake_case=""   ,_snake_case="."						):
														for k, v in d.items():
																					SCREAMING_SNAKE_CASE__						:       str           =   str(_snake_case						) + delimiter + str(_snake_case						) if parent_key else k
																					if v and isinstance(_snake_case   ,_snake_case						):
																												yield from flatten_dict(_snake_case   ,_snake_case   ,delimiter=_snake_case						).items()
																					else:
																												yield key, v

							return dict(_flatten_dict(_snake_case   ,_snake_case   ,_snake_case						)						)

@contextmanager
def lowercase_  (   _snake_case   ,_snake_case = False						):
							if use_temp_dir:
														with tempfile.TemporaryDirectory() as tmp_dir:
																					yield tmp_dir
							else:
														yield working_dir

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

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

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

														return tf.reshape(_snake_case   ,_snake_case						)
							elif is_jax_tensor(_snake_case						):
														return jnp.reshape(_snake_case   ,_snake_case						)
							else:
														raise ValueError(f'''Type not supported for reshape: {type(_snake_case						)}.'''						)

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

														return tf.squeeze(_snake_case   ,axis=_snake_case						)
							elif is_jax_tensor(_snake_case						):
														return jnp.squeeze(_snake_case   ,axis=_snake_case						)
							else:
														raise ValueError(f'''Type not supported for squeeze: {type(_snake_case						)}.'''						)

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

														return tf.expand_dims(_snake_case   ,axis=_snake_case						)
							elif is_jax_tensor(_snake_case						):
														return jnp.expand_dims(_snake_case   ,axis=_snake_case						)
							else:
														raise ValueError(f'''Type not supported for expand_dims: {type(_snake_case						)}.'''						)

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

														return tf.size(_snake_case						)
							elif is_jax_tensor(_snake_case						):
														return array.size
							else:
														raise ValueError(f'''Type not supported for expand_dims: {type(_snake_case						)}.'''						)

def lowercase_  (   _snake_case   ,_snake_case						):
							for key, value in auto_map.items():
														if isinstance(_snake_case   ,(tuple, list)						):
																					SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   [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:
																					SCREAMING_SNAKE_CASE__						:       Any           =   f'''{repo_id}--{value}'''

							return auto_map

def lowercase_  (   _snake_case						):
							for base_class in inspect.getmro(_snake_case						):
														SCREAMING_SNAKE_CASE__						:       Any           =   base_class.__module__
														SCREAMING_SNAKE_CASE__						:       Any           =   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 unittest

from transformers import is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow


if is_flax_available():
	import optax
	from flax.training.common_utils import onehot

	from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration
	from transformers.models.ta.modeling_flax_ta import shift_tokens_right



@require_torch
@require_sentencepiece
@require_tokenizers
@require_flax
class 			A__   (  unittest.TestCase							):




	'''simple docstring'''





	@slow
	def 							_SCREAMING_SNAKE_CASE   (     self:		Union[str, Any])				->       str:

				"""simple docstring"""

				__lowerCAmelCase      :  Optional[Any]												=       FlaxMTaForConditionalGeneration.from_pretrained("google/mt5-small")
				__lowerCAmelCase      :  Optional[Any]												=       AutoTokenizer.from_pretrained("google/mt5-small")

				__lowerCAmelCase      :  Tuple												=       tokenizer("Hello there"       ,			return_tensors="np").input_ids
				__lowerCAmelCase      :  Dict												=       tokenizer("Hi I am"       ,			return_tensors="np").input_ids

				__lowerCAmelCase      :  str												=       shift_tokens_right(_SCREAMING_SNAKE_CASE       ,			model.config.pad_token_id       ,			model.config.decoder_start_token_id)

				__lowerCAmelCase      :  Optional[int]												=       model(_SCREAMING_SNAKE_CASE       ,			decoder_input_ids=_SCREAMING_SNAKE_CASE).logits
				__lowerCAmelCase      :  int												=       optax.softmax_cross_entropy(_SCREAMING_SNAKE_CASE       ,			onehot(_SCREAMING_SNAKE_CASE       ,			logits.shape[-1])).mean()

				__lowerCAmelCase      :  List[str]												=       -(labels.shape[-1] * loss.item())

				__lowerCAmelCase      :  str												=       -84.9127
				self.assertTrue(abs(mtf_score - EXPECTED_SCORE) < 1e-4)