





import warnings

from transformers import AutoTokenizer
from transformers.utils import is_torch_available
from transformers.utils.generic import ExplicitEnum

from ...processing_utils import ProcessorMixin


if is_torch_available():
							import torch

class     __SCREAMING_SNAKE_CASE   (       lowerCamelCase__    ):
							snake_case       : Any         =    """char"""
							snake_case       : Union[str, Any]         =    """bpe"""
							snake_case       : Tuple         =    """wp"""






UpperCamelCase__     =	(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)

class     __SCREAMING_SNAKE_CASE   (       lowerCamelCase__    ):
							snake_case       : Dict         =    ["""image_processor""", """char_tokenizer"""]
							snake_case       : Any         =    """ViTImageProcessor"""
							snake_case       : List[Any]         =    """MgpstrTokenizer"""






							def __init__(  self					,	__lowerCAmelCase=None					,	__lowerCAmelCase=None					,	**__lowerCAmelCase					):
														UpperCamelCase__			       =     None
														if "feature_extractor" in kwargs:
																					warnings.warn(
																					    """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
																					    """ instead."""					,	__lowerCAmelCase					,	)
																					UpperCamelCase__			       =     kwargs.pop("""feature_extractor"""					)

														UpperCamelCase__			       =     image_processor if image_processor is not None else feature_extractor
														if image_processor is None:
																					raise ValueError("""You need to specify an `image_processor`."""					)
														if tokenizer is None:
																					raise ValueError("""You need to specify a `tokenizer`."""					)

														UpperCamelCase__			       =     tokenizer
														UpperCamelCase__			       =     AutoTokenizer.from_pretrained("""gpt2"""					)
														UpperCamelCase__			       =     AutoTokenizer.from_pretrained("""bert-base-uncased"""					)

														super().__init__(__lowerCAmelCase					,	__lowerCAmelCase					)






							def __call__(  self					,	__lowerCAmelCase=None					,	__lowerCAmelCase=None					,	__lowerCAmelCase=None					,	**__lowerCAmelCase					):




														if images is None and text is None:
																					raise ValueError("""You need to specify either an `images` or `text` input to process."""					)

														if images is not None:
																					UpperCamelCase__			       =     self.image_processor(__lowerCAmelCase					,	return_tensors=__lowerCAmelCase					,	**__lowerCAmelCase					)
														if text is not None:
																					UpperCamelCase__			       =     self.char_tokenizer(__lowerCAmelCase					,	return_tensors=__lowerCAmelCase					,	**__lowerCAmelCase					)

														if text is None:
																					return inputs
														elif images is None:
																					return encodings
														else:
																					UpperCamelCase__			       =     encodings["input_ids"]
																					return inputs






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					):
														UpperCamelCase__			       =     sequences
														UpperCamelCase__			       =     char_preds.size(0					)

														UpperCamelCase__			       =     self._decode_helper(__lowerCAmelCase					,	"""char"""					)
														UpperCamelCase__			       =     self._decode_helper(__lowerCAmelCase					,	"""bpe"""					)
														UpperCamelCase__			       =     self._decode_helper(__lowerCAmelCase					,	"""wp"""					)

														UpperCamelCase__			       =     []
														UpperCamelCase__			       =     []
														for i in range(__lowerCAmelCase					):
																					UpperCamelCase__			       =     [char_scores[i], bpe_scores[i], wp_scores[i]]
																					UpperCamelCase__			       =     [char_strs[i], bpe_strs[i], wp_strs[i]]
																					UpperCamelCase__			       =     scores.index(max(__lowerCAmelCase					)					)
																					final_strs.append(strs[max_score_index]					)
																					final_scores.append(scores[max_score_index]					)

														UpperCamelCase__			       =     {}
														UpperCamelCase__			       =     final_strs
														UpperCamelCase__			       =     final_scores
														UpperCamelCase__			       =     char_strs
														UpperCamelCase__			       =     bpe_strs
														UpperCamelCase__			       =     wp_strs
														return out






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase					):




														if format == DecodeType.CHARACTER:
																					UpperCamelCase__			       =     self.char_decode
																					UpperCamelCase__			       =     1
																					UpperCamelCase__			       =     "[s]"
														elif format == DecodeType.BPE:
																					UpperCamelCase__			       =     self.bpe_decode
																					UpperCamelCase__			       =     2
																					UpperCamelCase__			       =     "#"
														elif format == DecodeType.WORDPIECE:
																					UpperCamelCase__			       =     self.wp_decode
																					UpperCamelCase__			       =     102
																					UpperCamelCase__			       =     "[SEP]"
														else:
																					raise ValueError(f"""Format {format} is not supported."""					)

														UpperCamelCase__			       =     [], []
														UpperCamelCase__			       =     pred_logits.size(0					)
														UpperCamelCase__			       =     pred_logits.size(1					)
														UpperCamelCase__			       =     pred_logits.topk(1					,	dim=-1					,	largest=__lowerCAmelCase					,	sorted=__lowerCAmelCase					)
														UpperCamelCase__			       =     preds_index.view(-1					,	__lowerCAmelCase					)[:, 1:]
														UpperCamelCase__			       =     decoder(__lowerCAmelCase					)
														UpperCamelCase__			       =     torch.nn.functional.softmax(__lowerCAmelCase					,	dim=2					).max(dim=2					)
														UpperCamelCase__			       =     preds_max_prob[:, 1:]

														for index in range(__lowerCAmelCase					):
																					UpperCamelCase__			       =     preds_str[index].find(__lowerCAmelCase					)
																					UpperCamelCase__			       =     preds_str[index][:pred_eos]
																					UpperCamelCase__			       =     preds_index[index].cpu().tolist()
																					UpperCamelCase__			       =     pred_index.index(__lowerCAmelCase					) if eos_token in pred_index else -1
																					UpperCamelCase__			       =     preds_max_prob[index][: pred_eos_index + 1]
																					UpperCamelCase__			       =     pred_max_prob.cumprod(dim=0					)[-1] if pred_max_prob.nelement() != 0 else 0.0
																					dec_strs.append(__lowerCAmelCase					)
																					conf_scores.append(__lowerCAmelCase					)

														return dec_strs, conf_scores






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					):
														UpperCamelCase__			       =     [seq.replace(""" """					,	""""""					) for seq in self.char_tokenizer.batch_decode(__lowerCAmelCase					)]
														return decode_strs






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					):
														return self.bpe_tokenizer.batch_decode(__lowerCAmelCase					)







							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					):
														UpperCamelCase__			       =     [seq.replace(""" """					,	""""""					) for seq in self.wp_tokenizer.batch_decode(__lowerCAmelCase					)]
														return decode_strs











import logging
import os
from dataclasses import dataclass, field
from typing import Dict, Optional

import numpy as np
from utils_multiple_choice import MultipleChoiceDataset, Split, processors

import transformers
from transformers import (
    AutoConfig,
    AutoModelForMultipleChoice,
    AutoTokenizer,
    DataCollatorWithPadding,
    EvalPrediction,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.trainer_utils import is_main_process


UpperCamelCase__     =	logging.getLogger(__name__)





def       _UpperCamelCase       (a__				:Union[str, Any]				,     a__				:Optional[Any]    ):






							"""simple docstring"""





							return (preds == labels).mean()

@dataclass
class     __SCREAMING_SNAKE_CASE   :
							snake_case       : str         =    field(
							    metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}    )
							snake_case       : Optional[str]         =    field(
							    default=_a				,						metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}    )
							snake_case       : Optional[str]         =    field(
							    default=_a				,						metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}    )
							snake_case       : Optional[str]         =    field(
							    default=_a				,						metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}				,						)






@dataclass
class     __SCREAMING_SNAKE_CASE   :
							snake_case       : str         =    field(metadata={"""help""": """The name of the task to train on: """ + """, """.join(processors.keys()    )}    )
							snake_case       : str         =    field(metadata={"""help""": """Should contain the data files for the task."""}    )
							snake_case       : int         =    field(
							    default=1_28				,						metadata={
							        """help""": (
							            """The maximum total input sequence length after tokenization. Sequences longer """
							            """than this will be truncated, sequences shorter will be padded."""
							        )
							    }				,						)
							snake_case       : bool         =    field(
							    default=_a				,						metadata={"""help""": """Overwrite the cached training and evaluation sets"""}    )






def       _UpperCamelCase       ():






							"""simple docstring"""





							UpperCamelCase__			       =     HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)    )
							UpperCamelCase__       ,       UpperCamelCase__       ,       UpperCamelCase__			       =     parser.parse_args_into_dataclasses()

							if (
							    os.path.exists(training_args.output_dir    )
							    and os.listdir(training_args.output_dir    )
							    and training_args.do_train
							    and not training_args.overwrite_output_dir
							):
														raise ValueError(
														    f"""Output directory ({training_args.output_dir}) already exists and is not empty. Use"""
														    """ --overwrite_output_dir to overcome."""    )

							# Setup logging
							logging.basicConfig(
							    format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s"""				,     datefmt="""%m/%d/%Y %H:%M:%S"""				,     level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN				,     )
							logger.warning(
							    """Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s"""				,     training_args.local_rank				,     training_args.device				,     training_args.n_gpu				,     bool(training_args.local_rank != -1    )				,     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"""				,     a__    )

							# Set seed
							set_seed(training_args.seed    )

							try:
														UpperCamelCase__			       =     processors[data_args.task_name]()
														UpperCamelCase__			       =     processor.get_labels()
														UpperCamelCase__			       =     len(a__    )
							except KeyError:
														raise ValueError("""Task not found: %s""" % (data_args.task_name)    )

							# Load pretrained model and tokenizer
							#
							# Distributed training:
							# The .from_pretrained methods guarantee that only one local process can concurrently
							# download model & vocab.

							UpperCamelCase__			       =     AutoConfig.from_pretrained(
							    model_args.config_name if model_args.config_name else model_args.model_name_or_path				,     num_labels=a__				,     finetuning_task=data_args.task_name				,     cache_dir=model_args.cache_dir				,     )
							UpperCamelCase__			       =     AutoTokenizer.from_pretrained(
							    model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path				,     cache_dir=model_args.cache_dir				,     )
							UpperCamelCase__			       =     AutoModelForMultipleChoice.from_pretrained(
							    model_args.model_name_or_path				,     from_tf=bool(""".ckpt""" in model_args.model_name_or_path    )				,     config=a__				,     cache_dir=model_args.cache_dir				,     )

							# Get datasets
							UpperCamelCase__			       =     (
							    MultipleChoiceDataset(
							        data_dir=data_args.data_dir				,     tokenizer=a__				,     task=data_args.task_name				,     max_seq_length=data_args.max_seq_length				,     overwrite_cache=data_args.overwrite_cache				,     mode=Split.train				,     )
							    if training_args.do_train
							    else None
							)
							UpperCamelCase__			       =     (
							    MultipleChoiceDataset(
							        data_dir=data_args.data_dir				,     tokenizer=a__				,     task=data_args.task_name				,     max_seq_length=data_args.max_seq_length				,     overwrite_cache=data_args.overwrite_cache				,     mode=Split.dev				,     )
							    if training_args.do_eval
							    else None
							)

							def compute_metrics(a__				:EvalPrediction    ) -> Dict:
														UpperCamelCase__			       =     np.argmax(p.predictions				,     axis=1    )
														return {"acc": simple_accuracy(a__				,     p.label_ids    )}

							# Data collator
							UpperCamelCase__			       =     DataCollatorWithPadding(a__				,     pad_to_multiple_of=8    ) if training_args.fpaa else None

							# Initialize our Trainer
							UpperCamelCase__			       =     Trainer(
							    model=a__				,     args=a__				,     train_dataset=a__				,     eval_dataset=a__				,     compute_metrics=a__				,     data_collator=a__				,     )

							# Training
							if training_args.do_train:
														trainer.train(
														    model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path    ) else None    )
														trainer.save_model()
														# For convenience, we also re-save the tokenizer to the same directory,
														# so that you can share your model easily on huggingface.co/models =)
														if trainer.is_world_master():
																					tokenizer.save_pretrained(training_args.output_dir    )

    # Evaluation
							UpperCamelCase__			       =     {}
							if training_args.do_eval:
														logger.info("""*** Evaluate ***"""    )

														UpperCamelCase__			       =     trainer.evaluate()

														UpperCamelCase__			       =     os.path.join(training_args.output_dir				,     """eval_results.txt"""    )
														if trainer.is_world_master():
																					with open(a__				,     """w"""    ) as writer:
																												logger.info("""***** Eval results *****"""    )
																												for key, value in result.items():
																																			logger.info("""  %s = %s"""				,     a__				,     a__    )
																																			writer.write("""%s = %s\n""" % (key, value)    )

																												results.update(a__    )

							return results





def       _UpperCamelCase       (a__				:Union[str, Any]    ):






							"""simple docstring"""





							main()


if __name__ == "__main__":
					main()




