






'''simple docstring'''







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


_A			:    int					  =logging.getLogger(__name__)





def 				SCREAMING_SNAKE_CASE_ (UpperCamelCase       ,    UpperCamelCase   )   -> int:
							return (preds == labels).mean()

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



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





def 				SCREAMING_SNAKE_CASE_ ()   -> List[Any]:
							lowerCamelCase__      :			List[Any] 						= HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)   )
							lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__      :			List[str] 						= 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"""       ,    lowercase__   )

							# Set seed
							set_seed(training_args.seed   )

							try:
														lowerCamelCase__      :			Union[str, Any] 						= processors[data_args.task_name]()
														lowerCamelCase__      :			Tuple 						= processor.get_labels()
														lowerCamelCase__      :			int 						= len(lowercase__   )
							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.

							lowerCamelCase__      :			List[Any] 						= AutoConfig.from_pretrained(
							    model_args.config_name if model_args.config_name else model_args.model_name_or_path       ,    num_labels=lowercase__       ,    finetuning_task=data_args.task_name       ,    cache_dir=model_args.cache_dir       ,    )
							lowerCamelCase__      :			Optional[int] 						= 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       ,    )
							lowerCamelCase__      :			List[str] 						= AutoModelForMultipleChoice.from_pretrained(
							    model_args.model_name_or_path       ,    from_tf=bool(""".ckpt""" in model_args.model_name_or_path   )       ,    config=lowercase__       ,    cache_dir=model_args.cache_dir       ,    )

							# Get datasets
							lowerCamelCase__      :			Any 						= (
							    MultipleChoiceDataset(
							        data_dir=data_args.data_dir       ,    tokenizer=lowercase__       ,    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
							)
							lowerCamelCase__      :			Any 						= (
							    MultipleChoiceDataset(
							        data_dir=data_args.data_dir       ,    tokenizer=lowercase__       ,    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(UpperCamelCase   ) -> Dict:
														lowerCamelCase__      :			str 						= np.argmax(p.predictions       ,    axis=1   )
														return {"acc": simple_accuracy(lowercase__       ,    p.label_ids   )}

							# Data collator
							lowerCamelCase__      :			Tuple 						= DataCollatorWithPadding(lowercase__       ,    pad_to_multiple_of=8   ) if training_args.fpaa else None

							# Initialize our Trainer
							lowerCamelCase__      :			List[Any] 						= Trainer(
							    model=lowercase__       ,    args=lowercase__       ,    train_dataset=lowercase__       ,    eval_dataset=lowercase__       ,    compute_metrics=lowercase__       ,    data_collator=lowercase__       ,    )

							# 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
							lowerCamelCase__      :			Optional[Any] 						= {}
							if training_args.do_eval:
														logger.info("""*** Evaluate ***"""   )

														lowerCamelCase__      :			str 						= trainer.evaluate()

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

																												results.update(lowercase__   )

							return results





def 				SCREAMING_SNAKE_CASE_ (UpperCamelCase   )   -> Any:
							main()


if __name__ == "__main__":
			main()









'''simple docstring'''







from ...configuration_utils import PretrainedConfig
from ...utils import logging


_A			:    Any					  =logging.get_logger(__name__)

_A			:    Dict					  ={
    '''microsoft/trocr-base-handwritten''': (
        '''https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json'''
    ),
    # See all TrOCR models at https://huggingface.co/models?filter=trocr
}

class _lowercase			(       _lowercase	):
							a          = """trocr"""
							a          = ["""past_key_values"""]
							a          = {
							    """num_attention_heads""": """decoder_attention_heads""",
							    """hidden_size""": """d_model""",
							    """num_hidden_layers""": """decoder_layers""",
							}




							def __init__(							self:       Optional[Any]    ,							UpperCamelCase__:       int=50_265    ,							UpperCamelCase__:       int=1_024    ,							UpperCamelCase__:       Optional[Any]=12    ,							UpperCamelCase__:       Dict=16    ,							UpperCamelCase__:       int=4_096    ,							UpperCamelCase__:       Tuple="gelu"    ,							UpperCamelCase__:       int=512    ,							UpperCamelCase__:       Dict=0.1    ,							UpperCamelCase__:       Tuple=0.0    ,							UpperCamelCase__:       str=0.0    ,							UpperCamelCase__:       Any=2    ,							UpperCamelCase__:       Dict=0.02    ,							UpperCamelCase__:       Optional[Any]=0.0    ,							UpperCamelCase__:       str=True    ,							UpperCamelCase__:       Tuple=False    ,							UpperCamelCase__:       Union[str, Any]=True    ,							UpperCamelCase__:       Tuple=True    ,							UpperCamelCase__:       Dict=1    ,							UpperCamelCase__:       List[str]=0    ,							UpperCamelCase__:       Union[str, Any]=2    ,							**UpperCamelCase__:       str    ,							):
														lowerCamelCase__      :			Any 						= vocab_size
														lowerCamelCase__      :			Tuple 						= d_model
														lowerCamelCase__      :			Any 						= decoder_layers
														lowerCamelCase__      :			Dict 						= decoder_attention_heads
														lowerCamelCase__      :			str 						= decoder_ffn_dim
														lowerCamelCase__      :			Tuple 						= activation_function
														lowerCamelCase__      :			Dict 						= max_position_embeddings
														lowerCamelCase__      :			int 						= dropout
														lowerCamelCase__      :			int 						= attention_dropout
														lowerCamelCase__      :			List[Any] 						= activation_dropout
														lowerCamelCase__      :			Union[str, Any] 						= init_std
														lowerCamelCase__      :			Optional[int] 						= decoder_layerdrop
														lowerCamelCase__      :			Dict 						= use_cache
														lowerCamelCase__      :			Any 						= scale_embedding
														lowerCamelCase__      :			Optional[int] 						= use_learned_position_embeddings
														lowerCamelCase__      :			List[str] 						= layernorm_embedding

														super().__init__(
														    pad_token_id=UpperCamelCase__    ,							bos_token_id=UpperCamelCase__    ,							eos_token_id=UpperCamelCase__    ,							decoder_start_token_id=UpperCamelCase__    ,							**UpperCamelCase__    ,							)

