
"""simple docstring"""


import unittest

from transformers import XLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    XLMForMultipleChoice,
						    XLMForQuestionAnswering,
						    XLMForQuestionAnsweringSimple,
						    XLMForSequenceClassification,
						    XLMForTokenClassification,
						    XLMModel,
						    XLMWithLMHeadModel,
						)
						from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST




class  __lowerCamelCase  :




				def __init__(				self							,						UpperCAmelCase							,						UpperCAmelCase=13							,						UpperCAmelCase=7							,						UpperCAmelCase=True							,						UpperCAmelCase=True							,						UpperCAmelCase=True							,						UpperCAmelCase=True							,						UpperCAmelCase=True							,						UpperCAmelCase=False							,						UpperCAmelCase=False							,						UpperCAmelCase=False							,						UpperCAmelCase=2							,						UpperCAmelCase=99							,						UpperCAmelCase=0							,						UpperCAmelCase=32							,						UpperCAmelCase=5							,						UpperCAmelCase=4							,						UpperCAmelCase=0.1							,						UpperCAmelCase=0.1							,						UpperCAmelCase=512							,						UpperCAmelCase=2							,						UpperCAmelCase=0.0_2							,						UpperCAmelCase=2							,						UpperCAmelCase=4							,						UpperCAmelCase="last"							,						UpperCAmelCase=True							,						UpperCAmelCase=None							,						UpperCAmelCase=0							,						):
							lowerCamelCase_					=  parent
							lowerCamelCase_					=  batch_size
							lowerCamelCase_					=  seq_length
							lowerCamelCase_					=  is_training
							lowerCamelCase_					=  use_input_lengths
							lowerCamelCase_					=  use_token_type_ids
							lowerCamelCase_					=  use_labels
							lowerCamelCase_					=  gelu_activation
							lowerCamelCase_					=  sinusoidal_embeddings
							lowerCamelCase_					=  causal
							lowerCamelCase_					=  asm
							lowerCamelCase_					=  n_langs
							lowerCamelCase_					=  vocab_size
							lowerCamelCase_					=  n_special
							lowerCamelCase_					=  hidden_size
							lowerCamelCase_					=  num_hidden_layers
							lowerCamelCase_					=  num_attention_heads
							lowerCamelCase_					=  hidden_dropout_prob
							lowerCamelCase_					=  attention_probs_dropout_prob
							lowerCamelCase_					=  max_position_embeddings
							lowerCamelCase_					=  type_sequence_label_size
							lowerCamelCase_					=  initializer_range
							lowerCamelCase_					=  num_labels
							lowerCamelCase_					=  num_choices
							lowerCamelCase_					=  summary_type
							lowerCamelCase_					=  use_proj
							lowerCamelCase_					=  scope
							lowerCamelCase_					=  bos_token_id




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  ids_tensor([self.batch_size, self.seq_length]							,						self.vocab_size   )
							lowerCamelCase_					=  random_attention_mask([self.batch_size, self.seq_length]   )

							lowerCamelCase_					=  None
							if self.use_input_lengths:
										lowerCamelCase_					=  (
										    ids_tensor([self.batch_size]							,						vocab_size=2   ) + self.seq_length - 2
										)  # small variation of seq_length

							lowerCamelCase_					=  None
							if self.use_token_type_ids:
										lowerCamelCase_					=  ids_tensor([self.batch_size, self.seq_length]							,						self.n_langs   )

							lowerCamelCase_					=  None
							lowerCamelCase_					=  None
							lowerCamelCase_					=  None
							if self.use_labels:
										lowerCamelCase_					=  ids_tensor([self.batch_size]							,						self.type_sequence_label_size   )
										lowerCamelCase_					=  ids_tensor([self.batch_size, self.seq_length]							,						self.num_labels   )
										lowerCamelCase_					=  ids_tensor([self.batch_size]							,						2   ).float()
										lowerCamelCase_					=  ids_tensor([self.batch_size]							,						self.num_choices   )

							lowerCamelCase_					=  self.get_config()

							return (
							    config,
							    input_ids,
							    token_type_ids,
							    input_lengths,
							    sequence_labels,
							    token_labels,
							    is_impossible_labels,
							    choice_labels,
							    input_mask,
							)




				def        UpperCAmelCase__						(				self   ):
							return XLMConfig(
							    vocab_size=self.vocab_size							,						n_special=self.n_special							,						emb_dim=self.hidden_size							,						n_layers=self.num_hidden_layers							,						n_heads=self.num_attention_heads							,						dropout=self.hidden_dropout_prob							,						attention_dropout=self.attention_probs_dropout_prob							,						gelu_activation=self.gelu_activation							,						sinusoidal_embeddings=self.sinusoidal_embeddings							,						asm=self.asm							,						causal=self.causal							,						n_langs=self.n_langs							,						max_position_embeddings=self.max_position_embeddings							,						initializer_range=self.initializer_range							,						summary_type=self.summary_type							,						use_proj=self.use_proj							,						num_labels=self.num_labels							,						bos_token_id=self.bos_token_id							,						)




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						):
							lowerCamelCase_					=  XLMModel(config=__lowerCAmelCase   )
							model.to(__lowerCAmelCase   )
							model.eval()
							lowerCamelCase_					=  model(__lowerCAmelCase							,						lengths=__lowerCAmelCase							,						langs=__lowerCAmelCase   )
							lowerCamelCase_					=  model(__lowerCAmelCase							,						langs=__lowerCAmelCase   )
							lowerCamelCase_					=  model(__lowerCAmelCase   )
							self.parent.assertEqual(result.last_hidden_state.shape							,						(self.batch_size, self.seq_length, self.hidden_size)   )




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						):
							lowerCamelCase_					=  XLMWithLMHeadModel(__lowerCAmelCase   )
							model.to(__lowerCAmelCase   )
							model.eval()

							lowerCamelCase_					=  model(__lowerCAmelCase							,						token_type_ids=__lowerCAmelCase							,						labels=__lowerCAmelCase   )
							self.parent.assertEqual(result.loss.shape							,						()   )
							self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.seq_length, self.vocab_size)   )




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						):
							lowerCamelCase_					=  XLMForQuestionAnsweringSimple(__lowerCAmelCase   )
							model.to(__lowerCAmelCase   )
							model.eval()

							lowerCamelCase_					=  model(__lowerCAmelCase   )

							lowerCamelCase_					=  model(__lowerCAmelCase							,						start_positions=__lowerCAmelCase							,						end_positions=__lowerCAmelCase   )
							lowerCamelCase_					=  outputs
							self.parent.assertEqual(result.start_logits.shape							,						(self.batch_size, self.seq_length)   )
							self.parent.assertEqual(result.end_logits.shape							,						(self.batch_size, self.seq_length)   )




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						):
							lowerCamelCase_					=  XLMForQuestionAnswering(__lowerCAmelCase   )
							model.to(__lowerCAmelCase   )
							model.eval()

							lowerCamelCase_					=  model(__lowerCAmelCase   )

							lowerCamelCase_					=  model(
							    __lowerCAmelCase							,						start_positions=__lowerCAmelCase							,						end_positions=__lowerCAmelCase							,						cls_index=__lowerCAmelCase							,						is_impossible=__lowerCAmelCase							,						p_mask=__lowerCAmelCase							,						)

							lowerCamelCase_					=  model(
							    __lowerCAmelCase							,						start_positions=__lowerCAmelCase							,						end_positions=__lowerCAmelCase							,						cls_index=__lowerCAmelCase							,						is_impossible=__lowerCAmelCase							,						)

							((lowerCamelCase_     )		,      )					=  result_with_labels.to_tuple()

							lowerCamelCase_					=  model(__lowerCAmelCase							,						start_positions=__lowerCAmelCase							,						end_positions=__lowerCAmelCase   )

							((lowerCamelCase_     )		,      )					=  result_with_labels.to_tuple()

							self.parent.assertEqual(result_with_labels.loss.shape							,						()   )
							self.parent.assertEqual(result.start_top_log_probs.shape							,						(self.batch_size, model.config.start_n_top)   )
							self.parent.assertEqual(result.start_top_index.shape							,						(self.batch_size, model.config.start_n_top)   )
							self.parent.assertEqual(
							    result.end_top_log_probs.shape							,						(self.batch_size, model.config.start_n_top * model.config.end_n_top)   )
							self.parent.assertEqual(
							    result.end_top_index.shape							,						(self.batch_size, model.config.start_n_top * model.config.end_n_top)   )
							self.parent.assertEqual(result.cls_logits.shape							,						(self.batch_size,)   )




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						):
							lowerCamelCase_					=  XLMForSequenceClassification(__lowerCAmelCase   )
							model.to(__lowerCAmelCase   )
							model.eval()

							lowerCamelCase_					=  model(__lowerCAmelCase   )
							lowerCamelCase_					=  model(__lowerCAmelCase							,						labels=__lowerCAmelCase   )
							self.parent.assertEqual(result.loss.shape							,						()   )
							self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.type_sequence_label_size)   )




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						):
							lowerCamelCase_					=  self.num_labels
							lowerCamelCase_					=  XLMForTokenClassification(__lowerCAmelCase   )
							model.to(__lowerCAmelCase   )
							model.eval()

							lowerCamelCase_					=  model(__lowerCAmelCase							,						attention_mask=__lowerCAmelCase							,						labels=__lowerCAmelCase   )
							self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.seq_length, self.num_labels)   )




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						):
							lowerCamelCase_					=  self.num_choices
							lowerCamelCase_					=  XLMForMultipleChoice(config=__lowerCAmelCase   )
							model.to(__lowerCAmelCase   )
							model.eval()
							lowerCamelCase_					=  input_ids.unsqueeze(1   ).expand(-1							,						self.num_choices							,						-1   ).contiguous()
							lowerCamelCase_					=  token_type_ids.unsqueeze(1   ).expand(-1							,						self.num_choices							,						-1   ).contiguous()
							lowerCamelCase_					=  input_mask.unsqueeze(1   ).expand(-1							,						self.num_choices							,						-1   ).contiguous()
							lowerCamelCase_					=  model(
							    __lowerCAmelCase							,						attention_mask=__lowerCAmelCase							,						token_type_ids=__lowerCAmelCase							,						labels=__lowerCAmelCase							,						)
							self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.num_choices)   )





				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.prepare_config_and_inputs()
							(
							    (
							    lowerCamelCase_     
							)		, (
							    lowerCamelCase_     
							)		, (
							    lowerCamelCase_     
							)		, (
							    lowerCamelCase_     
							)		, (
							    lowerCamelCase_     
							)		, (
							    lowerCamelCase_     
							)		, (
							    lowerCamelCase_     
							)		, (
							    lowerCamelCase_     
							)		, (
							    lowerCamelCase_     
							)		,      
							)					=  config_and_inputs
							lowerCamelCase_					=  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths}
							return config, inputs_dict




@require_torch
class  __lowerCamelCase  ( lowerCAmelCase						,	lowerCAmelCase						,	lowerCAmelCase						,	unittest.TestCase			):
				a__:      Tuple          =    (
				    (
				        XLMModel,
				        XLMWithLMHeadModel,
				        XLMForQuestionAnswering,
				        XLMForSequenceClassification,
				        XLMForQuestionAnsweringSimple,
				        XLMForTokenClassification,
				        XLMForMultipleChoice,
				    )
				    if is_torch_available()
				    else ()
				)
				a__:      int          =    (
				    (XLMWithLMHeadModel,) if is_torch_available() else ()
				)  # TODO (PVP): Check other models whether language generation is also applicable
				a__:      Any          =    (
				    {
				        '''feature-extraction''': XLMModel,
				        '''fill-mask''': XLMWithLMHeadModel,
				        '''question-answering''': XLMForQuestionAnsweringSimple,
				        '''text-classification''': XLMForSequenceClassification,
				        '''text-generation''': XLMWithLMHeadModel,
				        '''token-classification''': XLMForTokenClassification,
				        '''zero-shot''': XLMForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase   ):






							if (
							    pipeline_test_casse_name == "QAPipelineTests"
							    and tokenizer_name is not None
							    and not tokenizer_name.endswith('''Fast'''   )
							):
										# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
										# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
										# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
										return True

							return False




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase=False   ):
							lowerCamelCase_					=  super()._prepare_for_class(__lowerCAmelCase							,						__lowerCAmelCase							,						return_labels=__lowerCAmelCase   )

							if return_labels:
										if model_class.__name__ == "XLMForQuestionAnswering":
													lowerCamelCase_					=  torch.zeros(
													    self.model_tester.batch_size							,						dtype=torch.long							,						device=__lowerCAmelCase   )
													lowerCamelCase_					=  torch.zeros(
													    self.model_tester.batch_size							,						dtype=torch.long							,						device=__lowerCAmelCase   )

							return inputs_dict




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  XLMModelTester(self   )
							lowerCamelCase_					=  ConfigTester(self							,						config_class=__lowerCAmelCase							,						emb_dim=37   )




				def        UpperCAmelCase__						(				self   ):
							self.config_tester.run_common_tests()




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_model(*__lowerCAmelCase   )




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_lm_head(*__lowerCAmelCase   )




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_simple_qa(*__lowerCAmelCase   )




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_qa(*__lowerCAmelCase   )




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_sequence_classif(*__lowerCAmelCase   )




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_token_classif(*__lowerCAmelCase   )




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_for_multiple_choice(*__lowerCAmelCase   )




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase=False							,						UpperCAmelCase=1   ):
							self.assertIsInstance(__lowerCAmelCase							,						__lowerCAmelCase   )
							self.assertListEqual(
							    [isinstance(__lowerCAmelCase							,						__lowerCAmelCase   ) for iter_attentions in attentions]							,						[True] * len(__lowerCAmelCase   )   )
							self.assertEqual(len(__lowerCAmelCase   )							,						(max_length - min_length) * num_beam_groups   )

							for idx, iter_attentions in enumerate(__lowerCAmelCase   ):
										# adds PAD dummy token
										lowerCamelCase_					=  min_length + idx + 1
										lowerCamelCase_					=  min_length + idx + 1

										lowerCamelCase_					=  (
										    batch_size * num_beam_groups,
										    config.num_attention_heads,
										    tgt_len,
										    src_len,
										)
										# check attn size
										self.assertListEqual(
										    [layer_attention.shape for layer_attention in iter_attentions]							,						[expected_shape] * len(__lowerCAmelCase   )   )




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase=False							,						UpperCAmelCase=1   ):
							self.assertIsInstance(__lowerCAmelCase							,						__lowerCAmelCase   )
							self.assertListEqual(
							    [isinstance(__lowerCAmelCase							,						__lowerCAmelCase   ) for iter_hidden_states in hidden_states]							,						[True] * len(__lowerCAmelCase   )							,						)
							self.assertEqual(len(__lowerCAmelCase   )							,						(max_length - min_length) * num_beam_groups   )

							for idx, iter_hidden_states in enumerate(__lowerCAmelCase   ):
										# adds PAD dummy token
										lowerCamelCase_					=  min_length + idx + 1
										lowerCamelCase_					=  (batch_size * num_beam_groups, seq_len, config.hidden_size)
										# check hidden size
										self.assertListEqual(
										    [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states]							,						[expected_shape] * len(__lowerCAmelCase   )							,						)
							pass





				@slow
				def        UpperCAmelCase__						(				self   ):






							for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCamelCase_					=  XLMModel.from_pretrained(__lowerCAmelCase   )
										self.assertIsNotNone(__lowerCAmelCase   )




@require_torch
class  __lowerCamelCase  ( unittest.TestCase			):




				@slow
				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  XLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048'''   )
							model.to(__lowerCAmelCase   )
							lowerCamelCase_					=  torch.tensor([[14, 447]]							,						dtype=torch.long							,						device=__lowerCAmelCase   )  # the president
							lowerCamelCase_					=  [
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							]  # the president the president the president the president the president the president the president the president the president the president
							# TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
							lowerCamelCase_					=  model.generate(__lowerCAmelCase							,						do_sample=__lowerCAmelCase   )
							self.assertListEqual(output_ids[0].cpu().numpy().tolist()							,						__lowerCAmelCase   )










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     (SCREAMING_SNAKE_CASE       ):
			'''simple docstring'''




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







					"""simple docstring"""






					if obj is None:
							return self
					if self.fget is None:
							raise AttributeError("""unreadable attribute"""  )
					A__     =  """__cached_""" + self.fget.__name__
					A__     =  getattr(__lowerCAmelCase				,      __lowerCAmelCase				,      __lowerCAmelCase  )
					if cached is None:
							A__     =  self.fget(__lowerCAmelCase  )
							setattr(__lowerCAmelCase				,      __lowerCAmelCase				,      __lowerCAmelCase  )
					return cached







def   __lowerCamelCase   (				__a  :Dict       )						->							Optional[int]:






		"""simple docstring"""

		A__     =  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   __lowerCamelCase   (				__a  :Any       )						->							Any:






		"""simple docstring"""

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

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

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

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

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

		return isinstance(__a       ,	np.ndarray       )







def   __lowerCamelCase   (				__a  :Optional[Any]       )						->							Union[str, Any]:






		"""simple docstring"""

		return isinstance(__a       ,	np.ndarray       )







def   __lowerCamelCase   (				__a  :int       )						->							List[Any]:






		"""simple docstring"""

		return _is_numpy(__a       )







def   __lowerCamelCase   (				__a  :Dict       )						->							Union[str, Any]:






		"""simple docstring"""

		import torch

		return isinstance(__a       ,	torch.Tensor       )







def   __lowerCamelCase   (				__a  :Dict       )						->							Any:






		"""simple docstring"""

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







def   __lowerCamelCase   (				__a  :List[str]       )						->							Any:






		"""simple docstring"""

		import torch

		return isinstance(__a       ,	torch.device       )







def   __lowerCamelCase   (				__a  :Any       )						->							int:






		"""simple docstring"""

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







def   __lowerCamelCase   (				__a  :str       )						->							List[str]:






		"""simple docstring"""

		import torch

		if isinstance(__a       ,	__a       ):
				if hasattr(__a       ,	__a       ):
						A__     =  getattr(__a       ,	__a       )
				else:
						return False
		return isinstance(__a       ,	torch.dtype       )







def   __lowerCamelCase   (				__a  :str       )						->							List[str]:






		"""simple docstring"""

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







def   __lowerCamelCase   (				__a  :Any       )						->							Optional[int]:






		"""simple docstring"""

		import tensorflow as tf

		return isinstance(__a       ,	tf.Tensor       )







def   __lowerCamelCase   (				__a  :int       )						->							Optional[int]:






		"""simple docstring"""

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







def   __lowerCamelCase   (				__a  :Tuple       )						->							Any:






		"""simple docstring"""

		import tensorflow as tf

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







def   __lowerCamelCase   (				__a  :Optional[Any]       )						->							List[Any]:






		"""simple docstring"""

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







def   __lowerCamelCase   (				__a  :Any       )						->							int:






		"""simple docstring"""

		import jax.numpy as jnp  # noqa: F811

		return isinstance(__a       ,	jnp.ndarray       )







def   __lowerCamelCase   (				__a  :Union[str, Any]       )						->							int:






		"""simple docstring"""

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







def   __lowerCamelCase   (				__a  :str       )						->							Dict:






		"""simple docstring"""

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







def   __lowerCamelCase   (				__a  :Tuple       )						->							int:






		"""simple docstring"""

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


class     A     (SCREAMING_SNAKE_CASE       ):
			'''simple docstring'''




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







					"""simple docstring"""






					A__     =  fields(self  )

					# Safety and consistency checks
					if not len(__lowerCAmelCase  ):
							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.'  )

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

					if other_fields_are_none and not is_tensor(__lowerCAmelCase  ):
							if isinstance(__lowerCAmelCase				,      __lowerCAmelCase  ):
									A__     =  first_field.items()
									A__     =  True
							else:
									try:
											A__     =  iter(__lowerCAmelCase  )
											A__     =  True
									except TypeError:
											A__     =  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(__lowerCAmelCase  ):
											if (
											    not isinstance(__lowerCAmelCase				,      (list, tuple)  )
											    or not len(__lowerCAmelCase  ) == 2
											    or not isinstance(element[0]				,      __lowerCAmelCase  )
											):
													if idx == 0:
															# If we do not have an iterator of key/values, set it as attribute
															A__     =  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:
													A__     =  element[1]
							elif first_field is not None:
									A__     =  first_field
					else:
							for field in class_fields:
									A__     =  getattr(self				,      field.name  )
									if v is not None:
											A__     =  v






			def __delitem__(     self  :       int				,      *__lowerCAmelCase  :       Tuple				,      **__lowerCAmelCase  :       List[str]  )      ->      Optional[int]:







					"""simple docstring"""






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






			def   a_			(     self  :       List[Any]				,      *__lowerCAmelCase  :       List[str]				,      **__lowerCAmelCase  :       List[Any]  )      ->      List[str]:







					"""simple docstring"""






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






			def   a_			(     self  :       Dict				,      *__lowerCAmelCase  :       Tuple				,      **__lowerCAmelCase  :       Union[str, Any]  )      ->      Optional[Any]:







					"""simple docstring"""






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






			def   a_			(     self  :       Tuple				,      *__lowerCAmelCase  :       int				,      **__lowerCAmelCase  :       List[str]  )      ->      Optional[Any]:







					"""simple docstring"""






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






			def __getitem__(     self  :       Tuple				,      __lowerCAmelCase  :       Tuple  )      ->      int:







					"""simple docstring"""






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






			def __setattr__(     self  :       Optional[Any]				,      __lowerCAmelCase  :       List[str]				,      __lowerCAmelCase  :       Tuple  )      ->      Dict:







					"""simple docstring"""






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






			def __setitem__(     self  :       Any				,      __lowerCAmelCase  :       int				,      __lowerCAmelCase  :       Dict  )      ->      List[Any]:







					"""simple docstring"""






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







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







					"""simple docstring"""






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


class     A     (SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE       ):
			'''simple docstring'''




			@classmethod
			def   a_			(     cls  :       str				,      __lowerCAmelCase  :       Optional[Any]  )      ->      Any:







					"""simple docstring"""






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


class     A     (SCREAMING_SNAKE_CASE       ):
			'''simple docstring'''




			__lowerCamelCase      :			Optional[int]							     =       '''longest'''
			__lowerCamelCase      :			str							     =       '''max_length'''
			__lowerCamelCase      :			List[str]							     =       '''do_not_pad'''


class     A     (SCREAMING_SNAKE_CASE       ):
			'''simple docstring'''




			__lowerCamelCase      :			str							     =       '''pt'''
			__lowerCamelCase      :			int							     =       '''tf'''
			__lowerCamelCase      :			Dict							     =       '''np'''
			__lowerCamelCase      :			Union[str, Any]							     =       '''jax'''


class     A     :
			'''simple docstring'''




			def __init__(     self  :       List[str]				,      __lowerCAmelCase  :       List[ContextManager]  )      ->      Any:







					"""simple docstring"""






					A__     =  context_managers
					A__     =  ExitStack()






			def __enter__(     self  :       Dict  )      ->      int:







					"""simple docstring"""






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







			def __exit__(     self  :       Any				,      *__lowerCAmelCase  :       Dict				,      **__lowerCAmelCase  :       Dict  )      ->      Any:







					"""simple docstring"""






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







def   __lowerCamelCase   (				__a  :List[str]       )						->							Dict:






		"""simple docstring"""

		A__     =  infer_framework(__a       )
		if framework == "tf":
				A__     =  inspect.signature(model_class.call       )  # TensorFlow models
		elif framework == "pt":
				A__     =  inspect.signature(model_class.forward       )  # PyTorch models
		else:
				A__     =  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   __lowerCamelCase   (				__a  :Optional[int]       )						->							str:






		"""simple docstring"""

		A__     =  model_class.__name__
		A__     =  infer_framework(__a       )
		if framework == "tf":
				A__     =  inspect.signature(model_class.call       )  # TensorFlow models
		elif framework == "pt":
				A__     =  inspect.signature(model_class.forward       )  # PyTorch models
		else:
				A__     =  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   __lowerCamelCase   (				__a  :MutableMapping       ,	__a  :str = ""       ,	__a  :str = "."       )						->							Optional[Any]:






		"""simple docstring"""

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

		return dict(_flatten_dict(__a       ,	__a       ,	__a       )       )







@contextmanager
def   __lowerCamelCase   (				__a  :Dict       ,	__a  :bool = False       )						->							List[Any]:






		"""simple docstring"""

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







def   __lowerCamelCase   (				__a  :Optional[Any]       ,	__a  :Optional[Any]=None       )						->							List[str]:






		"""simple docstring"""

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

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







def   __lowerCamelCase   (				__a  :Optional[Any]       ,	__a  :Optional[Any]       )						->							List[str]:






		"""simple docstring"""

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

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







def   __lowerCamelCase   (				__a  :Optional[Any]       ,	__a  :List[Any]=None       )						->							Union[str, Any]:






		"""simple docstring"""

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

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







def   __lowerCamelCase   (				__a  :Tuple       ,	__a  :Any       )						->							Any:






		"""simple docstring"""

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

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







def   __lowerCamelCase   (				__a  :Optional[int]       )						->							Dict:






		"""simple docstring"""

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

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







def   __lowerCamelCase   (				__a  :Union[str, Any]       ,	__a  :List[Any]       )						->							Optional[int]:






		"""simple docstring"""

		for key, value in auto_map.items():
				if isinstance(__a       ,	(tuple, list)       ):
						A__     =  [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:
						A__     =  F'{repo_id}--{value}'

		return auto_map







def   __lowerCamelCase   (				__a  :List[str]       )						->							Union[str, Any]:






		"""simple docstring"""

		for base_class in inspect.getmro(__a       ):
				A__     =  base_class.__module__
				A__     =  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}.'       )



