

import json
import logging
import math
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

from datasets import Dataset, load_dataset

import transformers
from transformers import (
    CONFIG_MAPPING,
    MODEL_FOR_MASKED_LM_MAPPING,
    AutoConfig,
    AutoModelForMaskedLM,
    AutoTokenizer,
    DataCollatorForWholeWordMask,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.trainer_utils import get_last_checkpoint, is_main_process


SCREAMING_SNAKE_CASE_            = logging.getLogger(__name__)
SCREAMING_SNAKE_CASE_            = list(MODEL_FOR_MASKED_LM_MAPPING.keys())
SCREAMING_SNAKE_CASE_            = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)

@dataclass
class 			a					:
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={
						        "help": (
						            "The model checkpoint for weights initialization.Don't set if you want to train a model from scratch."
						        )
						    }							,	)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "If training from scratch, pass a model type from the list: " + ", ".join(UpperCAmelCase			)}							,	)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={
						        "help": (
						            "Override some existing default config settings when a model is trained from scratch. Example: "
						            "n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index"
						        )
						    }							,	)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "Pretrained config name or path if not the same as model_name"}			)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}			)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}							,	)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."}							,	)
						_lowercase        =     field(
						    default="main"							,	metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."}							,	)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={
						        "help": (
						            "Will use the token generated when running `huggingface-cli login` (necessary to use this script "
						            "with private models)."
						        )
						    }							,	)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None):
														raise ValueError(
														    "--config_overrides can't be used in combination with --config_name or --model_name_or_path"		)







@dataclass
class 			a					:
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "The name of the dataset to use (via the datasets library)."}			)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}			)
						_lowercase        =     field(default=UpperCAmelCase							,	metadata={"help": "The input training data file (a text file)."}			)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."}							,	)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "An optional input train ref data file for whole word masking in Chinese."}							,	)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "An optional input validation ref data file for whole word masking in Chinese."}							,	)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "Overwrite the cached training and evaluation sets"}			)
						_lowercase        =     field(
						    default=5							,	metadata={
						        "help": "The percentage of the train set used as validation set in case there's no validation split"
						    }							,	)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={
						        "help": (
						            "The maximum total input sequence length after tokenization. Sequences longer "
						            "than this will be truncated. Default to the max input length of the model."
						        )
						    }							,	)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={"help": "The number of processes to use for the preprocessing."}							,	)
						_lowercase        =     field(
						    default=0.15							,	metadata={"help": "Ratio of tokens to mask for masked language modeling loss"}			)
						_lowercase        =     field(
						    default=UpperCAmelCase							,	metadata={
						        "help": (
						            "Whether to pad all samples to `max_seq_length`. "
						            "If False, will pad the samples dynamically when batching to the maximum length in the batch."
						        )
						    }							,	)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										if self.train_file is not None:
														_UpperCAmelCase    :						List[str]							=      self.train_file.split("."		)[-1]
														assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file."
										if self.validation_file is not None:
														_UpperCAmelCase    :						List[Any]							=      self.validation_file.split("."		)[-1]
														assert extension in ["csv", "json", "txt"], "`validation_file` should be a csv, a json or a txt file."







def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Optional[Any]					, lowerCAmelCase:					Union[str, Any]	)				->    List[Any]:
				with open(lowerCAmelCase					, "r"					, encoding="utf-8"	) as f:
								_UpperCAmelCase    :						Tuple							=      [json.loads(lowerCAmelCase	) for line in f.read().splitlines() if (len(lowerCAmelCase	) > 0 and not line.isspace())]
				assert len(lowerCAmelCase	) == len(lowerCAmelCase	)

				_UpperCAmelCase    :						Optional[int]							=      {c: dataset[c] for c in dataset.column_names}
				_UpperCAmelCase    :						List[Any]							=      refs
				return Dataset.from_dict(lowerCAmelCase	)

def 							__SCREAMING_SNAKE_CASE							(			)				->    Any:
				# See all possible arguments in src/transformers/training_args.py
				# or by passing the --help flag to this script.
				# We now keep distinct sets of args, for a cleaner separation of concerns.

				_UpperCAmelCase    :						List[Any]							=      HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)	)
				if len(sys.argv	) == 2 and sys.argv[1].endswith(".json"	):
								# If we pass only one argument to the script and it's the path to a json file,
								# let's parse it to get our arguments.
								_UpperCAmelCase						,							_UpperCAmelCase						,							_UpperCAmelCase    :						Union[str, Any]							=      parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]	)	)
				else:
								_UpperCAmelCase						,							_UpperCAmelCase						,							_UpperCAmelCase    :						Optional[Any]							=      parser.parse_args_into_dataclasses()

				# Detecting last checkpoint.
				_UpperCAmelCase    :						str							=      None
				if os.path.isdir(training_args.output_dir	) and training_args.do_train and not training_args.overwrite_output_dir:
								_UpperCAmelCase    :						Union[str, Any]							=      get_last_checkpoint(training_args.output_dir	)
								if last_checkpoint is None and len(os.listdir(training_args.output_dir	)	) > 0:
												raise ValueError(
												    F'Output directory ({training_args.output_dir}) already exists and is not empty. '
												    "Use --overwrite_output_dir to overcome."	)
								elif last_checkpoint is not None:
												logger.info(
												    F'Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change '
												    "the `--output_dir` or add `--overwrite_output_dir` to train from scratch."	)

    # Setup logging
				logging.basicConfig(
				    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"					, datefmt="%m/%d/%Y %H:%M:%S"					, handlers=[logging.StreamHandler(sys.stdout	)]					, )
				logger.setLevel(logging.INFO if is_main_process(training_args.local_rank	) else logging.WARN	)

				# Log on each process the small summary:
				logger.warning(
				    F'Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}'
				    + F'distributed training: {bool(training_args.local_rank != -1	)}, 16-bits training: {training_args.fpaa}'	)
				# Set the verbosity to info of the Transformers logger (on main process only):
				if is_main_process(training_args.local_rank	):
								transformers.utils.logging.set_verbosity_info()
								transformers.utils.logging.enable_default_handler()
								transformers.utils.logging.enable_explicit_format()
				logger.info("Training/evaluation parameters %s"					, lowerCAmelCase	)

				# Set seed before initializing model.
				set_seed(training_args.seed	)

				# Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below)
				# or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/
				# (the dataset will be downloaded automatically from the datasets Hub).
				#
				# For CSV/JSON files, this script will use the column called 'text' or the first column if no column called
				# 'text' is found. You can easily tweak this behavior (see below).
				#
				# In distributed training, the load_dataset function guarantee that only one local process can concurrently
				# download the dataset.
				if data_args.dataset_name is not None:
								# Downloading and loading a dataset from the hub.
								_UpperCAmelCase    :						List[str]							=      load_dataset(data_args.dataset_name					, data_args.dataset_config_name	)
								if "validation" not in datasets.keys():
												_UpperCAmelCase    :						int							=      load_dataset(
												    data_args.dataset_name					, data_args.dataset_config_name					, split=F'train[:{data_args.validation_split_percentage}%]'					, )
												_UpperCAmelCase    :						List[str]							=      load_dataset(
												    data_args.dataset_name					, data_args.dataset_config_name					, split=F'train[{data_args.validation_split_percentage}%:]'					, )
				else:
								_UpperCAmelCase    :						Tuple							=      {}
								if data_args.train_file is not None:
												_UpperCAmelCase    :						Dict							=      data_args.train_file
								if data_args.validation_file is not None:
												_UpperCAmelCase    :						List[Any]							=      data_args.validation_file
								_UpperCAmelCase    :						str							=      data_args.train_file.split("."	)[-1]
								if extension == "txt":
												_UpperCAmelCase    :						Union[str, Any]							=      "text"
								_UpperCAmelCase    :						List[str]							=      load_dataset(lowerCAmelCase					, data_files=lowerCAmelCase	)
				# See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at
				# https://huggingface.co/docs/datasets/loading_datasets.html.

				# Load pretrained model and tokenizer
				#
				# Distributed training:
				# The .from_pretrained methods guarantee that only one local process can concurrently
				# download model & vocab.
				_UpperCAmelCase    :						Any							=      {
				    "cache_dir": model_args.cache_dir,
				    "revision": model_args.model_revision,
				    "use_auth_token": True if model_args.use_auth_token else None,
				}
				if model_args.config_name:
								_UpperCAmelCase    :						int							=      AutoConfig.from_pretrained(model_args.config_name					, **lowerCAmelCase	)
				elif model_args.model_name_or_path:
								_UpperCAmelCase    :						Any							=      AutoConfig.from_pretrained(model_args.model_name_or_path					, **lowerCAmelCase	)
				else:
								_UpperCAmelCase    :						List[Any]							=      CONFIG_MAPPING[model_args.model_type]()
								logger.warning("You are instantiating a new config instance from scratch."	)
								if model_args.config_overrides is not None:
												logger.info(F'Overriding config: {model_args.config_overrides}'	)
												config.update_from_string(model_args.config_overrides	)
												logger.info(F'New config: {config}'	)

				_UpperCAmelCase    :						Optional[Any]							=      {
				    "cache_dir": model_args.cache_dir,
				    "use_fast": model_args.use_fast_tokenizer,
				    "revision": model_args.model_revision,
				    "use_auth_token": True if model_args.use_auth_token else None,
				}
				if model_args.tokenizer_name:
								_UpperCAmelCase    :						int							=      AutoTokenizer.from_pretrained(model_args.tokenizer_name					, **lowerCAmelCase	)
				elif model_args.model_name_or_path:
								_UpperCAmelCase    :						str							=      AutoTokenizer.from_pretrained(model_args.model_name_or_path					, **lowerCAmelCase	)
				else:
								raise ValueError(
								    "You are instantiating a new tokenizer from scratch. This is not supported by this script."
								    "You can do it from another script, save it, and load it from here, using --tokenizer_name."	)

				if model_args.model_name_or_path:
								_UpperCAmelCase    :						List[Any]							=      AutoModelForMaskedLM.from_pretrained(
								    model_args.model_name_or_path					, from_tf=bool(".ckpt" in model_args.model_name_or_path	)					, config=lowerCAmelCase					, cache_dir=model_args.cache_dir					, revision=model_args.model_revision					, use_auth_token=True if model_args.use_auth_token else None					, )
				else:
								logger.info("Training new model from scratch"	)
								_UpperCAmelCase    :						List[Any]							=      AutoModelForMaskedLM.from_config(lowerCAmelCase	)

				model.resize_token_embeddings(len(lowerCAmelCase	)	)

				# Preprocessing the datasets.
				# First we tokenize all the texts.
				if training_args.do_train:
								_UpperCAmelCase    :						Optional[Any]							=      datasets["train"].column_names
				else:
								_UpperCAmelCase    :						str							=      datasets["validation"].column_names
				_UpperCAmelCase    :						Tuple							=      "text" if "text" in column_names else column_names[0]

				_UpperCAmelCase    :						Union[str, Any]							=      "max_length" if data_args.pad_to_max_length else False

				def tokenize_function(lowerCAmelCase:					Tuple	):
								# Remove empty lines
								_UpperCAmelCase    :						Optional[Any]							=      [line for line in examples["text"] if len(lowerCAmelCase	) > 0 and not line.isspace()]
								return tokenizer(examples["text"]					, padding=lowerCAmelCase					, truncation=lowerCAmelCase					, max_length=data_args.max_seq_length	)

				_UpperCAmelCase    :						Union[str, Any]							=      datasets.map(
				    lowerCAmelCase					, batched=lowerCAmelCase					, num_proc=data_args.preprocessing_num_workers					, remove_columns=[text_column_name]					, load_from_cache_file=not data_args.overwrite_cache					, )

				# Add the chinese references if provided
				if data_args.train_ref_file is not None:
								_UpperCAmelCase    :						Optional[Any]							=      add_chinese_references(tokenized_datasets["train"]					, data_args.train_ref_file	)
				if data_args.validation_ref_file is not None:
								_UpperCAmelCase    :						Optional[Any]							=      add_chinese_references(
								    tokenized_datasets["validation"]					, data_args.validation_ref_file	)
				# If we have ref files, need to avoid it removed by trainer
				_UpperCAmelCase    :						Optional[Any]							=      data_args.train_ref_file or data_args.validation_ref_file
				if has_ref:
								_UpperCAmelCase    :						Dict							=      False

				# Data collator
				# This one will take care of randomly masking the tokens.
				_UpperCAmelCase    :						Optional[Any]							=      DataCollatorForWholeWordMask(tokenizer=lowerCAmelCase					, mlm_probability=data_args.mlm_probability	)

				# Initialize our Trainer
				_UpperCAmelCase    :						Tuple							=      Trainer(
				    model=lowerCAmelCase					, args=lowerCAmelCase					, train_dataset=tokenized_datasets["train"] if training_args.do_train else None					, eval_dataset=tokenized_datasets["validation"] if training_args.do_eval else None					, tokenizer=lowerCAmelCase					, data_collator=lowerCAmelCase					, )

				# Training
				if training_args.do_train:
								if last_checkpoint is not None:
												_UpperCAmelCase    :						List[Any]							=      last_checkpoint
								elif model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path	):
												_UpperCAmelCase    :						str							=      model_args.model_name_or_path
								else:
												_UpperCAmelCase    :						Optional[Any]							=      None
								_UpperCAmelCase    :						List[str]							=      trainer.train(resume_from_checkpoint=lowerCAmelCase	)
								trainer.save_model()  # Saves the tokenizer too for easy upload

								_UpperCAmelCase    :						str							=      os.path.join(training_args.output_dir					, "train_results.txt"	)
								if trainer.is_world_process_zero():
												with open(lowerCAmelCase					, "w"	) as writer:
																logger.info("***** Train results *****"	)
																for key, value in sorted(train_result.metrics.items()	):
																				logger.info(F'  {key} = {value}'	)
																				writer.write(F'{key} = {value}\n'	)

            # Need to save the state, since Trainer.save_model saves only the tokenizer with the model
												trainer.state.save_to_json(os.path.join(training_args.output_dir					, "trainer_state.json"	)	)

    # Evaluation
				_UpperCAmelCase    :						Union[str, Any]							=      {}
				if training_args.do_eval:
								logger.info("*** Evaluate ***"	)

								_UpperCAmelCase    :						Any							=      trainer.evaluate()

								_UpperCAmelCase    :						int							=      math.exp(eval_output["eval_loss"]	)
								_UpperCAmelCase    :						str							=      perplexity

								_UpperCAmelCase    :						Tuple							=      os.path.join(training_args.output_dir					, "eval_results_mlm_wwm.txt"	)
								if trainer.is_world_process_zero():
												with open(lowerCAmelCase					, "w"	) as writer:
																logger.info("***** Eval results *****"	)
																for key, value in sorted(results.items()	):
																				logger.info(F'  {key} = {value}'	)
																				writer.write(F'{key} = {value}\n'	)

				return results

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Tuple	)				->    Any:
				# For xla_spawn (TPUs)
				main()


if __name__ == "__main__":
					main()





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  ,   A_  ,   A_=None		):







										'''simple docstring'''


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







def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Optional[Any]	)				->    int:
				_UpperCAmelCase    :						str							=      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 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Any	)				->    int:
				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 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Any	)				->    Dict:
				return isinstance(lowerCAmelCase					, np.ndarray	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Optional[Any]	)				->    Any:
				return _is_numpy(lowerCAmelCase	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Tuple	)				->    Optional[int]:
				import torch

				return isinstance(lowerCAmelCase					, torch.Tensor	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Any	)				->    Optional[int]:
				return False if not is_torch_available() else _is_torch(lowerCAmelCase	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					List[Any]	)				->    List[Any]:
				import torch

				return isinstance(lowerCAmelCase					, torch.device	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					int	)				->    Tuple:
				return False if not is_torch_available() else _is_torch_device(lowerCAmelCase	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					List[str]	)				->    Tuple:
				import torch

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

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Any	)				->    int:
				return False if not is_torch_available() else _is_torch_dtype(lowerCAmelCase	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					List[Any]	)				->    Optional[Any]:
				import tensorflow as tf

				return isinstance(lowerCAmelCase					, tf.Tensor	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					str	)				->    Optional[Any]:
				return False if not is_tf_available() else _is_tensorflow(lowerCAmelCase	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Optional[int]	)				->    Any:
				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 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Tuple	)				->    Optional[Any]:
				return False if not is_tf_available() else _is_tf_symbolic_tensor(lowerCAmelCase	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Optional[Any]	)				->    List[str]:
				import jax.numpy as jnp  # noqa: F811

				return isinstance(lowerCAmelCase					, jnp.ndarray	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					List[Any]	)				->    str:
				return False if not is_flax_available() else _is_jax(lowerCAmelCase	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					str	)				->    Tuple:
				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 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Optional[Any]	)				->    List[Any]:
				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     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[Any]							=      fields(self		)

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

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

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


						def __delitem__(    self  ,   *A_  ,   **A_		):







										'''simple docstring'''


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


						def     _UpperCAmelCase			(    self  ,   *A_  ,   **A_		):







										'''simple docstring'''


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


						def     _UpperCAmelCase			(    self  ,   *A_  ,   **A_		):







										'''simple docstring'''


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


						def     _UpperCAmelCase			(    self  ,   *A_  ,   **A_		):







										'''simple docstring'''


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


						def __getitem__(    self  ,   A_		):







										'''simple docstring'''


										if isinstance(A_  ,   A_		):
														_UpperCAmelCase    :						Optional[int]							=      dict(self.items()		)
														return inner_dict[k]
										else:
														return self.to_tuple()[k]


						def __setattr__(    self  ,   A_  ,   A_		):







										'''simple docstring'''


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


						def __setitem__(    self  ,   A_  ,   A_		):







										'''simple docstring'''


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





						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


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







class 			a					(		UpperCAmelCase							,	UpperCAmelCase			):


						@classmethod
						def     _UpperCAmelCase			(    cls  ,   A_		):







										'''simple docstring'''


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







class 			a					(		UpperCAmelCase			):
						_lowercase        =     "longest"
						_lowercase        =     "max_length"
						_lowercase        =     "do_not_pad"







class 			a					(		UpperCAmelCase			):
						_lowercase        =     "pt"
						_lowercase        =     "tf"
						_lowercase        =     "np"
						_lowercase        =     "jax"







class 			a					:


						def __init__(    self  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						Tuple							=      context_managers
										_UpperCAmelCase    :						Dict							=      ExitStack()


						def __enter__(    self		):







										'''simple docstring'''


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





						def __exit__(    self  ,   *A_  ,   **A_		):







										'''simple docstring'''


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







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

				def _flatten_dict(lowerCAmelCase:					int					, lowerCAmelCase:					Tuple=""					, lowerCAmelCase:					List[str]="."	):
								for k, v in d.items():
												_UpperCAmelCase    :						Optional[int]							=      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 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Union[str, Any]					, lowerCAmelCase:					bool = False	)				->    List[Any]:
				if use_temp_dir:
								with tempfile.TemporaryDirectory() as tmp_dir:
												yield tmp_dir
				else:
								yield working_dir

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Dict					, lowerCAmelCase:					Tuple=None	)				->    List[str]:
				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 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Optional[Any]					, lowerCAmelCase:					Any	)				->    int:
				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 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					str					, lowerCAmelCase:					Union[str, Any]=None	)				->    Union[str, Any]:
				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 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Dict					, lowerCAmelCase:					List[str]	)				->    Union[str, Any]:
				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 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					List[Any]	)				->    int:
				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 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					List[str]					, lowerCAmelCase:					List[Any]	)				->    List[Any]:
				for key, value in auto_map.items():
								if isinstance(lowerCAmelCase					, (tuple, list)	):
												_UpperCAmelCase    :						List[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:
												_UpperCAmelCase    :						Tuple							=      F'{repo_id}--{value}'

				return auto_map





def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Any	)				->    List[Any]:
				for base_class in inspect.getmro(lowerCAmelCase	):
								_UpperCAmelCase    :						int							=      base_class.__module__
								_UpperCAmelCase    :						Dict							=      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}.'	)


