





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       (a__				:int				,     a__				:Dict    ):






							"""simple docstring"""





							assert isinstance(a__				,     a__    )
							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       (a__				:Any				,     a__				:int				,     a__				:str    ):






							"""simple docstring"""





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





@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       (a__				:str				,     a__				:Tuple				,     a__				:str    ):






							"""simple docstring"""





							UpperCamelCase__			       =     tmp_path / """cache"""
							UpperCamelCase__			       =     {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							UpperCamelCase__			       =     features.copy() if features else default_expected_features
							UpperCamelCase__			       =     (
							    Features({feature: Value(a__    ) for feature, dtype in features.items()}    ) if features is not None else None
							)
							UpperCamelCase__			       =     ParquetDatasetReader(a__				,     features=a__				,     cache_dir=a__    ).read()
							_check_parquet_dataset(a__				,     a__    )





@pytest.mark.parametrize("""split"""				,     [None, NamedSplit("""train"""    ), """train""", """test"""]    )
def       _UpperCamelCase       (a__				:List[str]				,     a__				:Any				,     a__				:int    ):






							"""simple docstring"""





							UpperCamelCase__			       =     tmp_path / """cache"""
							UpperCamelCase__			       =     {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							UpperCamelCase__			       =     ParquetDatasetReader(a__				,     cache_dir=a__				,     split=a__    ).read()
							_check_parquet_dataset(a__				,     a__    )
							assert dataset.split == split if split else "train"





@pytest.mark.parametrize("""path_type"""				,     [str, list]    )
def       _UpperCamelCase       (a__				:Tuple				,     a__				:str				,     a__				:Tuple    ):






							"""simple docstring"""





							if issubclass(a__				,     a__    ):
														UpperCamelCase__			       =     parquet_path
							elif issubclass(a__				,     a__    ):
														UpperCamelCase__			       =     [parquet_path]
							UpperCamelCase__			       =     tmp_path / """cache"""
							UpperCamelCase__			       =     {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							UpperCamelCase__			       =     ParquetDatasetReader(a__				,     cache_dir=a__    ).read()
							_check_parquet_dataset(a__				,     a__    )





def       _UpperCamelCase       (a__				:List[Any]				,     a__				:Union[str, Any]				,     a__				:List[Any]=("train",)    ):






							"""simple docstring"""





							assert isinstance(a__				,     a__    )
							for split in splits:
														UpperCamelCase__			       =     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       (a__				:List[Any]				,     a__				:List[Any]				,     a__				:Any    ):






							"""simple docstring"""





							UpperCamelCase__			       =     tmp_path / """cache"""
							UpperCamelCase__			       =     {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
														UpperCamelCase__			       =     ParquetDatasetReader(
														    {"""train""": parquet_path}				,     cache_dir=a__				,     keep_in_memory=a__    ).read()
							_check_parquet_datasetdict(a__				,     a__    )





@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       (a__				:Optional[int]				,     a__				:Any				,     a__				:int    ):






							"""simple docstring"""





							UpperCamelCase__			       =     tmp_path / """cache"""
							UpperCamelCase__			       =     {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							UpperCamelCase__			       =     features.copy() if features else default_expected_features
							UpperCamelCase__			       =     (
							    Features({feature: Value(a__    ) for feature, dtype in features.items()}    ) if features is not None else None
							)
							UpperCamelCase__			       =     ParquetDatasetReader({"""train""": parquet_path}				,     features=a__				,     cache_dir=a__    ).read()
							_check_parquet_datasetdict(a__				,     a__    )





@pytest.mark.parametrize("""split"""				,     [None, NamedSplit("""train"""    ), """train""", """test"""]    )
def       _UpperCamelCase       (a__				:Optional[Any]				,     a__				:Dict				,     a__				:List[str]    ):






							"""simple docstring"""





							if split:
														UpperCamelCase__			       =     {split: parquet_path}
							else:
														UpperCamelCase__			       =     """train"""
														UpperCamelCase__			       =     {"""train""": parquet_path, """test""": parquet_path}
							UpperCamelCase__			       =     tmp_path / """cache"""
							UpperCamelCase__			       =     {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							UpperCamelCase__			       =     ParquetDatasetReader(a__				,     cache_dir=a__    ).read()
							_check_parquet_datasetdict(a__				,     a__				,     splits=list(path.keys()    )    )
							assert all(dataset[split].split == split for split in path.keys()    )





def       _UpperCamelCase       (a__				:Dict				,     a__				:Tuple    ):






							"""simple docstring"""





							UpperCamelCase__			       =     ParquetDatasetWriter(a__				,     tmp_path / """foo.parquet"""    )
							assert writer.write() > 0
							UpperCamelCase__			       =     pq.ParquetFile(tmp_path / """foo.parquet"""    )
							UpperCamelCase__			       =     pf.read()
							assert dataset.data.table == output_table





def       _UpperCamelCase       (a__				:List[Any]				,     a__				:int    ):






							"""simple docstring"""





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

							UpperCamelCase__			       =     Dataset.from_parquet(str(tmp_path / """foo.parquet"""    )    )
							assert dataset.features == reloaded_dataset.features

							UpperCamelCase__			       =     ParquetDatasetReader(str(tmp_path / """foo.parquet"""    )				,     streaming=a__    ).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       (a__				:Optional[Any]				,     a__				:Optional[Any]    ):






							"""simple docstring"""





							assert get_writer_batch_size(a__    ) == expected











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()




