




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_      			=       logging.getLogger(__name__)


def       UpperCAmelCase__ (snake_case__							:      Any	,     snake_case__							:      List[Any]					):
  """simple docstring"""

  return (preds == labels).mean()






@dataclass
class       lowercase:







 '''simple docstring'''





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







@dataclass
class       lowercase:







 '''simple docstring'''





 lowercase__				      =      field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys()				)}				)
 lowercase__				      =      field(metadata={"help": "Should contain the data files for the task."}				)
 lowercase__				      =      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."
         )
     } ,    )
 lowercase__				      =      field(
     default=__a ,    metadata={"help": "Overwrite the cached training and evaluation sets"}				)







def       UpperCAmelCase__ ():
  """simple docstring"""

  _snake_case							:	Dict			=						HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)					)
  _snake_case							:	List[Any]			=						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"""	,     snake_case__					)

  # Set seed
  set_seed(training_args.seed					)

  try:
    _snake_case							:	int			=						processors[data_args.task_name]()
    _snake_case							:	Any			=						processor.get_labels()
    _snake_case							:	int			=						len(snake_case__					)
  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.

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

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

  # Data collator
  _snake_case							:	Any			=						DataCollatorWithPadding(snake_case__	,     pad_to_multiple_of=8					) if training_args.fpaa else None

  # Initialize our Trainer
  _snake_case							:	int			=						Trainer(
      model=snake_case__	,     args=snake_case__	,     train_dataset=snake_case__	,     eval_dataset=snake_case__	,     compute_metrics=snake_case__	,     data_collator=snake_case__	,     )

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

    _snake_case							:	Optional[int]			=						trainer.evaluate()

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

        results.update(snake_case__					)

  return results


def       UpperCAmelCase__ (snake_case__							:      List[Any]					):
  """simple docstring"""

  main()


if __name__ == "__main__":
     main()








"""simple docstring"""


import pyarrow.parquet as pq
import pytest

from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config
from datasets.features.image import Image
from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases


def       UpperCAmelCase__ (snake_case__							:      Optional[Any]	,     snake_case__							:      Dict					):
  """simple docstring"""

  assert isinstance(snake_case__	,     snake_case__					)
  assert dataset.num_rows == 4
  assert dataset.num_columns == 3
  assert dataset.column_names == ["col_1", "col_2", "col_3"]
  for feature, expected_dtype in expected_features.items():
    assert dataset.features[feature].dtype == expected_dtype


@pytest.mark.parametrize("""keep_in_memory"""	,     [False, True]					)
def       UpperCAmelCase__ (snake_case__							:      str	,     snake_case__							:      Optional[int]	,     snake_case__							:      Dict					):
  """simple docstring"""

  _snake_case							:	str			=						tmp_path / """cache"""
  _snake_case							:	int			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
    _snake_case							:	str			=						ParquetDatasetReader(snake_case__	,     cache_dir=snake_case__	,     keep_in_memory=snake_case__					).read()
  _check_parquet_dataset(snake_case__	,     snake_case__					)


@pytest.mark.parametrize(
    """features"""	,     [
        None,
        {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""},
        {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""},
        {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""},
        {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""},
    ]	,     )
def       UpperCAmelCase__ (snake_case__							:      Dict	,     snake_case__							:      int	,     snake_case__							:      List[Any]					):
  """simple docstring"""

  _snake_case							:	str			=						tmp_path / """cache"""
  _snake_case							:	List[Any]			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  _snake_case							:	List[Any]			=						features.copy() if features else default_expected_features
  _snake_case							:	List[Any]			=						(
      Features({feature: Value(snake_case__					) for feature, dtype in features.items()}					) if features is not None else None
  )
  _snake_case							:	Optional[Any]			=						ParquetDatasetReader(snake_case__	,     features=snake_case__	,     cache_dir=snake_case__					).read()
  _check_parquet_dataset(snake_case__	,     snake_case__					)


@pytest.mark.parametrize("""split"""	,     [None, NamedSplit("""train"""					), """train""", """test"""]					)
def       UpperCAmelCase__ (snake_case__							:      Optional[int]	,     snake_case__							:      int	,     snake_case__							:      int					):
  """simple docstring"""

  _snake_case							:	List[str]			=						tmp_path / """cache"""
  _snake_case							:	Any			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  _snake_case							:	int			=						ParquetDatasetReader(snake_case__	,     cache_dir=snake_case__	,     split=snake_case__					).read()
  _check_parquet_dataset(snake_case__	,     snake_case__					)
  assert dataset.split == split if split else "train"


@pytest.mark.parametrize("""path_type"""	,     [str, list]					)
def       UpperCAmelCase__ (snake_case__							:      Dict	,     snake_case__							:      str	,     snake_case__							:      str					):
  """simple docstring"""

  if issubclass(snake_case__	,     snake_case__					):
    _snake_case							:	Optional[Any]			=						parquet_path
  elif issubclass(snake_case__	,     snake_case__					):
    _snake_case							:	int			=						[parquet_path]
  _snake_case							:	Union[str, Any]			=						tmp_path / """cache"""
  _snake_case							:	Tuple			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  _snake_case							:	List[str]			=						ParquetDatasetReader(snake_case__	,     cache_dir=snake_case__					).read()
  _check_parquet_dataset(snake_case__	,     snake_case__					)


def       UpperCAmelCase__ (snake_case__							:      str	,     snake_case__							:      Optional[int]	,     snake_case__							:      str=("train",)					):
  """simple docstring"""

  assert isinstance(snake_case__	,     snake_case__					)
  for split in splits:
    _snake_case							:	int			=						dataset_dict[split]
    assert dataset.num_rows == 4
    assert dataset.num_columns == 3
    assert dataset.column_names == ["col_1", "col_2", "col_3"]
    for feature, expected_dtype in expected_features.items():
      assert dataset.features[feature].dtype == expected_dtype


@pytest.mark.parametrize("""keep_in_memory"""	,     [False, True]					)
def       UpperCAmelCase__ (snake_case__							:      str	,     snake_case__							:      str	,     snake_case__							:      List[Any]					):
  """simple docstring"""

  _snake_case							:	Tuple			=						tmp_path / """cache"""
  _snake_case							:	Optional[int]			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
    _snake_case							:	Tuple			=						ParquetDatasetReader(
        {"""train""": parquet_path}	,     cache_dir=snake_case__	,     keep_in_memory=snake_case__					).read()
  _check_parquet_datasetdict(snake_case__	,     snake_case__					)


@pytest.mark.parametrize(
    """features"""	,     [
        None,
        {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""},
        {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""},
        {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""},
        {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""},
    ]	,     )
def       UpperCAmelCase__ (snake_case__							:      List[str]	,     snake_case__							:      Tuple	,     snake_case__							:      List[Any]					):
  """simple docstring"""

  _snake_case							:	Optional[int]			=						tmp_path / """cache"""
  _snake_case							:	Dict			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  _snake_case							:	Optional[Any]			=						features.copy() if features else default_expected_features
  _snake_case							:	Dict			=						(
      Features({feature: Value(snake_case__					) for feature, dtype in features.items()}					) if features is not None else None
  )
  _snake_case							:	Optional[int]			=						ParquetDatasetReader({"""train""": parquet_path}	,     features=snake_case__	,     cache_dir=snake_case__					).read()
  _check_parquet_datasetdict(snake_case__	,     snake_case__					)


@pytest.mark.parametrize("""split"""	,     [None, NamedSplit("""train"""					), """train""", """test"""]					)
def       UpperCAmelCase__ (snake_case__							:      List[str]	,     snake_case__							:      Optional[Any]	,     snake_case__							:      Tuple					):
  """simple docstring"""

  if split:
    _snake_case							:	int			=						{split: parquet_path}
  else:
    _snake_case							:	Optional[Any]			=						"""train"""
    _snake_case							:	int			=						{"""train""": parquet_path, """test""": parquet_path}
  _snake_case							:	Dict			=						tmp_path / """cache"""
  _snake_case							:	Any			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  _snake_case							:	Union[str, Any]			=						ParquetDatasetReader(snake_case__	,     cache_dir=snake_case__					).read()
  _check_parquet_datasetdict(snake_case__	,     snake_case__	,     splits=list(path.keys()					)					)
  assert all(dataset[split].split == split for split in path.keys()					)


def       UpperCAmelCase__ (snake_case__							:      Tuple	,     snake_case__							:      Tuple					):
  """simple docstring"""

  _snake_case							:	List[Any]			=						ParquetDatasetWriter(snake_case__	,     tmp_path / """foo.parquet"""					)
  assert writer.write() > 0
  _snake_case							:	str			=						pq.ParquetFile(tmp_path / """foo.parquet"""					)
  _snake_case							:	int			=						pf.read()
  assert dataset.data.table == output_table


def       UpperCAmelCase__ (snake_case__							:      int	,     snake_case__							:      int					):
  """simple docstring"""

  _snake_case							:	Optional[Any]			=						str(shared_datadir / """test_image_rgb.jpg"""					)
  _snake_case							:	Tuple			=						{"""image""": [image_path]}
  _snake_case							:	Optional[int]			=						Features({"""image""": Image()}					)
  _snake_case							:	int			=						Dataset.from_dict(snake_case__	,     features=snake_case__					)
  _snake_case							:	Optional[Any]			=						ParquetDatasetWriter(snake_case__	,     tmp_path / """foo.parquet"""					)
  assert writer.write() > 0

  _snake_case							:	List[str]			=						Dataset.from_parquet(str(tmp_path / """foo.parquet"""					)					)
  assert dataset.features == reloaded_dataset.features

  _snake_case							:	Optional[Any]			=						ParquetDatasetReader(str(tmp_path / """foo.parquet"""					)	,     streaming=snake_case__					).read()
  assert dataset.features == reloaded_iterable_dataset.features






@pytest.mark.parametrize(
    """feature, expected"""	,     [
        (Features({"""foo""": Value("""int32"""					)}					), None),
        (Features({"""image""": Image(), """foo""": Value("""int32"""					)}					), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS),
        (Features({"""nested""": Sequence(Audio()					)}					), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS),
    ]	,     )
def       UpperCAmelCase__ (snake_case__							:      List[Any]	,     snake_case__							:      str					):
  """simple docstring"""

  assert get_writer_batch_size(snake_case__					) == expected


