#!/usr/bin/env python
# coding=utf-8
# Copyright 2020 The HuggingFace Team All and <NAME> rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Train tree structured models on the SICK task.
"""
# You can also adapt this script on your own task. Pointers for this are left as comments.

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

import datasets
from datasets import load_dataset, load_metric

import transformers
from transformers import (
    Trainer,
    EvalPrediction,
    HfArgumentParser,
    TrainingArguments,
    set_seed,
    default_data_collator,
)
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version
from transformers.utils.versions import require_version

from pytree import (
    NaryConfig, 
    NaryTree,
    ChildSumConfig,
    ChildSumTree,
    GloveTokenizer,
    Similarity,
    SimilarityConfig
)
from pytree.data import prepare_input_from_constituency_tree, prepare_input_from_dependency_tree
from pytree.data.utils import build_tree_ids_n_ary

from supar import Parser
import torch
import numpy as np
import math
from sklearn.metrics import mean_squared_error
from scipy.stats import pearsonr, spearmanr

# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
check_min_version("4.11.0")  # 4.12.0.dev0

require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/question-answering/requirements.txt")

logger = logging.getLogger(__name__)

class SickTrainer(Trainer):
    
    def create_optimizer(self):
        """
        Setup the optimizer.

        We provide a reasonable default that works well. If you want to use something else, you can pass a tuple in the
        Trainer's init through :obj:`optimizers`, or subclass and override this method in a subclass.
        """
        self.optimizer = torch.optim.Adagrad(self.model.parameters(), 
            lr=self.args.learning_rate, 
            weight_decay=self.args.weight_decay)
    
        # if self.sharded_ddp == ShardedDDPOption.SIMPLE:
        #     self.optimizer = OSS(
        #         params=optimizer_grouped_parameters,
        #         optim=optimizer_cls,
        #         **optimizer_kwargs,
        #     )
        # else:
        #     self.optimizer = optimizer_cls(optimizer_grouped_parameters, **optimizer_kwargs)

        # if is_sagemaker_mp_enabled():
        #     self.optimizer = smp.DistributedOptimizer(self.optimizer)

        return self.optimizer


@dataclass
class ModelArguments:
    """
    Arguments pertaining to which model/config/tokenizer we are going to fine-tune from.
    """

    model_name_or_path: Optional[str] = field(
        default=None, metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}
    )
    config_name: Optional[str] = field(
        default=None, metadata={"help": "Pretrained config name or path if not the same as model_name"}
    )
    tokenizer_name: Optional[str] = field(
        default=None, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}
    )
    cache_dir: Optional[str] = field(
        default=None,
        metadata={"help": "Path to directory to store the pretrained models downloaded from huggingface.co"},
    )
    model_revision: str = field(
        default="main",
        metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."},
    )
    use_auth_token: bool = field(
        default=False,
        metadata={
            "help": "Will use the token generated when running `transformers-cli login` (necessary to use this script "
            "with private models)."
        },
    )


@dataclass
class DataTrainingArguments:
    """
    Arguments pertaining to what data we are going to input our model for training and eval.
    """

    glove_file_path: str = field(
        default=None, metadata={"help": "The path to GloVe text file embeddings."}
    )
    dataset_name: Optional[str] = field(
        default=None, metadata={"help": "The name of the dataset to use (via the datasets library)."}
    )
    dataset_config_name: Optional[str] = field(
        default=None, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}
    )
    train_file: Optional[str] = field(default=None, metadata={"help": "The input training data file (a text file)."})
    validation_file: Optional[str] = field(
        default=None,
        metadata={"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."},
    )
    test_file: Optional[str] = field(
        default=None,
        metadata={"help": "An optional input test data file to evaluate the perplexity on (a text file)."},
    )
    overwrite_cache: bool = field(
        default=False, metadata={"help": "Overwrite the cached training and evaluation sets"}
    )
    preprocessing_num_workers: Optional[int] = field(
        default=None,
        metadata={"help": "The number of processes to use for the preprocessing."},
    )
    max_seq_length: int = field(
        default=384,
        metadata={
            "help": "The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be truncated, sequences shorter will be padded."
        },
    )
    pad_to_max_length: bool = field(
        default=True,
        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 (which can "
            "be faster on GPU but will be slower on TPU)."
        },
    )
    max_train_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of training examples to this "
            "value if set."
        },
    )
    max_eval_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of evaluation examples to this "
            "value if set."
        },
    )
    max_predict_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of prediction examples to this "
            "value if set."
        },
    )
    version_2_with_negative: bool = field(
        default=False, metadata={"help": "If true, some of the examples do not have an answer."}
    )
    null_score_diff_threshold: float = field(
        default=0.0,
        metadata={
            "help": "The threshold used to select the null answer: if the best answer has a score that is less than "
            "the score of the null answer minus this threshold, the null answer is selected for this example. "
            "Only useful when `version_2_with_negative=True`."
        },
    )
    doc_stride: int = field(
        default=128,
        metadata={"help": "When splitting up a long document into chunks, how much stride to take between chunks."},
    )
    n_best_size: int = field(
        default=20,
        metadata={"help": "The total number of n-best predictions to generate when looking for an answer."},
    )
    max_answer_length: int = field(
        default=30,
        metadata={
            "help": "The maximum length of an answer that can be generated. This is needed because the start "
            "and end predictions are not conditioned on one another."
        },
    )

    def __post_init__(self):
        if (
            self.dataset_name is None
            and self.train_file is None
            and self.validation_file is None
            and self.test_file is None
        ):
            raise ValueError("Need either a dataset name or a training/validation file/test_file.")
        else:
            if self.train_file is not None:
                extension = self.train_file.split(".")[-1]
                assert extension in ["csv", "json"], "`train_file` should be a csv or a json file."
            if self.validation_file is not None:
                extension = self.validation_file.split(".")[-1]
                assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file."
            if self.test_file is not None:
                extension = self.test_file.split(".")[-1]
                assert extension in ["csv", "json"], "`test_file` should be a csv or a json file."


def main():
    # 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.

    parser = 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.
        model_args, data_args, training_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]))
    else:
        model_args, data_args, training_args = parser.parse_args_into_dataclasses()

    # 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)],
    )

    log_level = training_args.get_process_log_level()
    logger.setLevel(log_level)
    datasets.utils.logging.set_verbosity(log_level)
    transformers.utils.logging.set_verbosity(log_level)
    transformers.utils.logging.enable_default_handler()
    transformers.utils.logging.enable_explicit_format()

    # 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.fp16}"
    )
    logger.info(f"Training/evaluation parameters {training_args}")

    # Detecting last checkpoint.
    last_checkpoint = None
    if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
        last_checkpoint = 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 and training_args.resume_from_checkpoint is 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."
            )

    # 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.
        raw_datasets = load_dataset(
            data_args.dataset_name, data_args.dataset_config_name, cache_dir=model_args.cache_dir, keep_in_memory=False,
        )
    else:
        data_files = {}
        if data_args.train_file is not None:
            data_files["train"] = data_args.train_file
            extension = data_args.train_file.split(".")[-1]

        if data_args.validation_file is not None:
            data_files["validation"] = data_args.validation_file
            extension = data_args.validation_file.split(".")[-1]
        if data_args.test_file is not None:
            data_files["test"] = data_args.test_file
            extension = data_args.test_file.split(".")[-1]
        raw_datasets = load_dataset(extension, data_files=data_files, field="data", cache_dir=model_args.cache_dir)
    # 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.
    con = Parser.load('crf-con-en')
    glove_tokenizer = GloveTokenizer(glove_file_path=data_args.glove_file_path, vocab_size=2000000)

    config = NaryConfig()
    encoder = NaryTree(config)
    encoder.embeddings.load_pretrained_embeddings(
        torch.tensor(glove_tokenizer.embeddings_arr, device=training_args.device))    
    config_similarity = SimilarityConfig()
    model = Similarity(encoder, config_similarity)

    # Preprocessing the datasets.
    # Preprocessing is slighlty different for training and evaluation.
    if training_args.do_train:
        column_names = raw_datasets["train"].column_names
    elif training_args.do_eval:
        column_names = raw_datasets["validation"].column_names
    else:
        column_names = raw_datasets["test"].column_names

    def map_label_to_target(label, num_classes, ):
        target = [0] * num_classes  # torch.zeros(1, num_classes, dtype=torch.float)
        ceil = int(math.ceil(label))
        floor = int(math.floor(label))
        if ceil == floor:
            target[floor - 1] = 1
        else:
            target[floor - 1] = ceil - label
            target[ceil - 1] = label - floor
        return target

    def prepare_train_features(examples):
        examples['input_ids_A'] = []
        examples['input_ids_B'] = []
        examples['head_idx_A'] = []
        examples['head_idx_B'] = []
        examples['head_idx_r_A'] = []
        examples['head_idx_r_B'] = []
        examples['head_idx_l_A'] = []
        examples['head_idx_l_B'] = []
        examples['labels'] = []
        
        for sent_A in examples['sentence_A']:
            con_tree_A = str(con.predict(sent_A.split(), verbose=False)[0])
            input_ids_A, head_idx_A, head_idx_r_A, head_idx_l_A = prepare_input_from_constituency_tree(con_tree_A)
            input_ids_A = glove_tokenizer.convert_tokens_to_ids(input_ids_A)
            examples['input_ids_A'].append(input_ids_A)
            examples['head_idx_A'].append(head_idx_A)
            examples['head_idx_r_A'].append(head_idx_r_A)
            examples['head_idx_l_A'].append(head_idx_l_A)
        
        for sent_B in examples['sentence_B']:
            con_tree_B = str(con.predict(sent_B.split(), verbose=False)[0])
            input_ids_B, head_idx_B, head_idx_r_B, head_idx_l_B = prepare_input_from_constituency_tree(con_tree_B)
            input_ids_B = glove_tokenizer.convert_tokens_to_ids(input_ids_B)
            examples['input_ids_B'].append(input_ids_B)
            examples['head_idx_B'].append(head_idx_B)
            examples['head_idx_r_B'].append(head_idx_r_B)
            examples['head_idx_l_B'].append(head_idx_l_B)

        for rel_score in examples['relatedness_score']:
            examples['labels'].append(map_label_to_target(rel_score, 5))

        return examples

    if training_args.do_train:
        if "train" not in raw_datasets:
            raise ValueError("--do_train requires a train dataset")
        train_dataset = raw_datasets["train"]
        if data_args.max_train_samples is not None:
            # We will select sample from whole data if argument is specified
            train_dataset = train_dataset.select(range(data_args.max_train_samples))
        # Create train feature from dataset
        with training_args.main_process_first(desc="train dataset map pre-processing"):
            train_dataset = train_dataset.map(
                prepare_train_features,
                batched=True,
                num_proc=data_args.preprocessing_num_workers,  # TODO if cuda is available, set to 0
                remove_columns=column_names,
                load_from_cache_file=not data_args.overwrite_cache,
                desc="Running parser on train dataset",
            )
        if data_args.max_train_samples is not None:
            # Number of samples might increase during Feature Creation, We select only specified max samples
            train_dataset = train_dataset.select(range(data_args.max_train_samples))

    # Validation preprocessing
    if training_args.do_eval:
        if "validation" not in raw_datasets:
            raise ValueError("--do_eval requires a validation dataset")
        eval_examples = raw_datasets["validation"]
        if data_args.max_eval_samples is not None:
            # We will select sample from whole data
            eval_examples = eval_examples.select(range(data_args.max_eval_samples))
        # Validation Feature Creation
        with training_args.main_process_first(desc="validation dataset map pre-processing"):
            eval_dataset = eval_examples.map(
                prepare_train_features,
                batched=True,
                num_proc=data_args.preprocessing_num_workers,
                remove_columns=None,  # column_names,
                load_from_cache_file=not data_args.overwrite_cache,
                desc="Running parser on validation dataset",
            )
        if data_args.max_eval_samples is not None:
            # During Feature creation dataset samples might increase, we will select required samples again
            eval_dataset = eval_dataset.select(range(data_args.max_eval_samples))

    if training_args.do_predict:
        if "test" not in raw_datasets:
            raise ValueError("--do_predict requires a test dataset")
        predict_examples = raw_datasets["test"]
        if data_args.max_predict_samples is not None:
            # We will select sample from whole data
            predict_examples = predict_examples.select(range(data_args.max_predict_samples))
        # Predict Feature Creation
        with training_args.main_process_first(desc="prediction dataset map pre-processing"):
            predict_dataset = predict_examples.map(
                prepare_train_features,
                batched=True,
                num_proc=data_args.preprocessing_num_workers,
                remove_columns=None,  # column_names,
                load_from_cache_file=not data_args.overwrite_cache,
                desc="Running parser on prediction dataset",
            )
        if data_args.max_predict_samples is not None:
            # During Feature creation dataset samples might increase, we will select required samples again
            predict_dataset = predict_dataset.select(range(data_args.max_predict_samples))

    def data_collator_with_padding(features, pad_ids=0, columns=None):
        batch = {}
        first = features[0]
        if columns is None:
            columns = ["head_idx_A", "head_idx_B", "input_ids_A", "input_ids_B", "head_idx_r_A", "head_idx_l_A", "head_idx_r_B", "head_idx_l_B"]
        feature_max_len = {k: max([len(f[k]) for f in features]) for k in first.keys() if k in columns or len(columns) == 0}
        for k, v in first.items():
            if k in columns or len(columns) == 0:
                feature_padded = [list([int(ff) for ff in f[k]]) + [0] * (feature_max_len[k] - len(f[k])) for f in features]
                batch[k] = feature_padded  # [f[k] for f in features]
        tree_ids_A, tree_ids_r_A, tree_ids_l_A = build_tree_ids_n_ary(batch['head_idx_A'], batch['head_idx_r_A'], batch['head_idx_l_A'])
        tree_ids_B, tree_ids_r_B, tree_ids_l_B = build_tree_ids_n_ary(batch['head_idx_B'], batch['head_idx_r_B'], batch['head_idx_l_B'])
        batch['input_ids_A'] = torch.tensor(batch['input_ids_A'])
        batch['input_ids_B'] = torch.tensor(batch['input_ids_B'])
        batch['tree_ids_A'] = torch.tensor(tree_ids_A)
        batch['tree_ids_B'] = torch.tensor(tree_ids_B)
        batch['tree_ids_r_A'] = torch.tensor(tree_ids_r_A)
        batch['tree_ids_r_B'] = torch.tensor(tree_ids_r_B)
        batch['tree_ids_l_A'] = torch.tensor(tree_ids_l_A)
        batch['tree_ids_l_B'] = torch.tensor(tree_ids_l_B)
        batch['labels'] = torch.tensor([f['labels'] for f in features])
        return batch

    data_collator = data_collator_with_padding

    def compute_metrics(eval_prediction):
        prediction = np.matmul(np.exp(eval_prediction.predictions), np.arange(1, 5 + 1))
        target = np.matmul(eval_prediction.label_ids, np.arange(1, 5 + 1))
        results_relatedness = {
            'pearson': pearsonr(prediction, target)[0] * 100,
            'spearman': spearmanr(prediction, target)[0] * 100,
            'mse': mean_squared_error(prediction, target) * 100
        }
        return results_relatedness
    
    # Initialize our Trainer
    trainer = SickTrainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset if training_args.do_train else None,
        eval_dataset=eval_dataset if training_args.do_eval else None,  # eval_dataset
        data_collator=data_collator,
        compute_metrics=compute_metrics,
        optimizers=("Adagrad", None),
    )

    # Training
    if training_args.do_train:
        checkpoint = None
        if training_args.resume_from_checkpoint is not None:
            checkpoint = training_args.resume_from_checkpoint
        elif last_checkpoint is not None:
            checkpoint = last_checkpoint
        train_result = trainer.train(resume_from_checkpoint=checkpoint)
        trainer.save_model()  # Saves the tokenizer too for easy upload

        metrics = train_result.metrics
        max_train_samples = (
            data_args.max_train_samples if data_args.max_train_samples is not None else len(train_dataset)
        )
        metrics["train_samples"] = min(max_train_samples, len(train_dataset))

        trainer.log_metrics("train", metrics)
        trainer.save_metrics("train", metrics)
        trainer.save_state()

    # Evaluation
    if training_args.do_eval:
        logger.info("*** Evaluate ***")
        metrics = trainer.evaluate()

        max_eval_samples = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(eval_dataset)
        metrics["eval_samples"] = min(max_eval_samples, len(eval_dataset))

        trainer.log_metrics("eval", metrics)
        trainer.save_metrics("eval", metrics)

    # Prediction
    if training_args.do_predict:
        logger.info("*** Predict ***")
        results = trainer.predict(predict_dataset)
        metrics = results.metrics

        max_predict_samples = (
            data_args.max_predict_samples if data_args.max_predict_samples is not None else len(predict_dataset)
        )
        metrics["predict_samples"] = min(max_predict_samples, len(predict_dataset))

        trainer.log_metrics("predict", metrics)
        trainer.save_metrics("predict", metrics)

    kwargs = {"finetuned_from": model_args.model_name_or_path, "tasks": "sick-relatedness"}
    if data_args.dataset_name is not None:
        kwargs["dataset_tags"] = data_args.dataset_name
        if data_args.dataset_config_name is not None:
            kwargs["dataset_args"] = data_args.dataset_config_name
            kwargs["dataset"] = f"{data_args.dataset_name} {data_args.dataset_config_name}"
        else:
            kwargs["dataset"] = data_args.dataset_name

    if training_args.push_to_hub:
        trainer.push_to_hub(**kwargs)
    else:
        trainer.create_model_card(**kwargs)


def _mp_fn(index):
    # For xla_spawn (TPUs)
    main()


if __name__ == "__main__":
    main()
#!/usr/bin/env python
# coding=utf-8
# Copyright 2020 The HuggingFace Team All and <NAME> rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Train tree structured models on the SICK task.
"""
# You can also adapt this script on your own task. Pointers for this are left as comments.

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

import datasets
from datasets import load_dataset, load_metric

import transformers
from transformers import (
    Trainer,
    EvalPrediction,
    HfArgumentParser,
    TrainingArguments,
    set_seed,
    default_data_collator,
)
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version
from transformers.utils.versions import require_version

from pytree import (
    NaryConfig, 
    NaryTree,
    ChildSumConfig,
    ChildSumTree,
    GloveTokenizer,
    Similarity,
    SimilarityConfig
)
from pytree.data import prepare_input_from_constituency_tree, prepare_input_from_dependency_tree
from pytree.data.utils import build_tree_ids_n_ary

from supar import Parser
import torch
import numpy as np
import math
from sklearn.metrics import mean_squared_error
from scipy.stats import pearsonr, spearmanr

# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
check_min_version("4.11.0")  # 4.12.0.dev0

require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/question-answering/requirements.txt")

logger = logging.getLogger(__name__)

class SickTrainer(Trainer):
    
    def create_optimizer(self):
        """
        Setup the optimizer.

        We provide a reasonable default that works well. If you want to use something else, you can pass a tuple in the
        Trainer's init through :obj:`optimizers`, or subclass and override this method in a subclass.
        """
        self.optimizer = torch.optim.Adagrad(self.model.parameters(), 
            lr=self.args.learning_rate, 
            weight_decay=self.args.weight_decay)
    
        # if self.sharded_ddp == ShardedDDPOption.SIMPLE:
        #     self.optimizer = OSS(
        #         params=optimizer_grouped_parameters,
        #         optim=optimizer_cls,
        #         **optimizer_kwargs,
        #     )
        # else:
        #     self.optimizer = optimizer_cls(optimizer_grouped_parameters, **optimizer_kwargs)

        # if is_sagemaker_mp_enabled():
        #     self.optimizer = smp.DistributedOptimizer(self.optimizer)

        return self.optimizer


@dataclass
class ModelArguments:
    """
    Arguments pertaining to which model/config/tokenizer we are going to fine-tune from.
    """

    model_name_or_path: Optional[str] = field(
        default=None, metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}
    )
    config_name: Optional[str] = field(
        default=None, metadata={"help": "Pretrained config name or path if not the same as model_name"}
    )
    tokenizer_name: Optional[str] = field(
        default=None, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}
    )
    cache_dir: Optional[str] = field(
        default=None,
        metadata={"help": "Path to directory to store the pretrained models downloaded from huggingface.co"},
    )
    model_revision: str = field(
        default="main",
        metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."},
    )
    use_auth_token: bool = field(
        default=False,
        metadata={
            "help": "Will use the token generated when running `transformers-cli login` (necessary to use this script "
            "with private models)."
        },
    )


@dataclass
class DataTrainingArguments:
    """
    Arguments pertaining to what data we are going to input our model for training and eval.
    """

    glove_file_path: str = field(
        default=None, metadata={"help": "The path to GloVe text file embeddings."}
    )
    dataset_name: Optional[str] = field(
        default=None, metadata={"help": "The name of the dataset to use (via the datasets library)."}
    )
    dataset_config_name: Optional[str] = field(
        default=None, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}
    )
    train_file: Optional[str] = field(default=None, metadata={"help": "The input training data file (a text file)."})
    validation_file: Optional[str] = field(
        default=None,
        metadata={"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."},
    )
    test_file: Optional[str] = field(
        default=None,
        metadata={"help": "An optional input test data file to evaluate the perplexity on (a text file)."},
    )
    overwrite_cache: bool = field(
        default=False, metadata={"help": "Overwrite the cached training and evaluation sets"}
    )
    preprocessing_num_workers: Optional[int] = field(
        default=None,
        metadata={"help": "The number of processes to use for the preprocessing."},
    )
    max_seq_length: int = field(
        default=384,
        metadata={
            "help": "The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be truncated, sequences shorter will be padded."
        },
    )
    pad_to_max_length: bool = field(
        default=True,
        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 (which can "
            "be faster on GPU but will be slower on TPU)."
        },
    )
    max_train_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of training examples to this "
            "value if set."
        },
    )
    max_eval_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of evaluation examples to this "
            "value if set."
        },
    )
    max_predict_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of prediction examples to this "
            "value if set."
        },
    )
    version_2_with_negative: bool = field(
        default=False, metadata={"help": "If true, some of the examples do not have an answer."}
    )
    null_score_diff_threshold: float = field(
        default=0.0,
        metadata={
            "help": "The threshold used to select the null answer: if the best answer has a score that is less than "
            "the score of the null answer minus this threshold, the null answer is selected for this example. "
            "Only useful when `version_2_with_negative=True`."
        },
    )
    doc_stride: int = field(
        default=128,
        metadata={"help": "When splitting up a long document into chunks, how much stride to take between chunks."},
    )
    n_best_size: int = field(
        default=20,
        metadata={"help": "The total number of n-best predictions to generate when looking for an answer."},
    )
    max_answer_length: int = field(
        default=30,
        metadata={
            "help": "The maximum length of an answer that can be generated. This is needed because the start "
            "and end predictions are not conditioned on one another."
        },
    )

    def __post_init__(self):
        if (
            self.dataset_name is None
            and self.train_file is None
            and self.validation_file is None
            and self.test_file is None
        ):
            raise ValueError("Need either a dataset name or a training/validation file/test_file.")
        else:
            if self.train_file is not None:
                extension = self.train_file.split(".")[-1]
                assert extension in ["csv", "json"], "`train_file` should be a csv or a json file."
            if self.validation_file is not None:
                extension = self.validation_file.split(".")[-1]
                assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file."
            if self.test_file is not None:
                extension = self.test_file.split(".")[-1]
                assert extension in ["csv", "json"], "`test_file` should be a csv or a json file."


def main():
    # 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.

    parser = 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.
        model_args, data_args, training_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]))
    else:
        model_args, data_args, training_args = parser.parse_args_into_dataclasses()

    # 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)],
    )

    log_level = training_args.get_process_log_level()
    logger.setLevel(log_level)
    datasets.utils.logging.set_verbosity(log_level)
    transformers.utils.logging.set_verbosity(log_level)
    transformers.utils.logging.enable_default_handler()
    transformers.utils.logging.enable_explicit_format()

    # 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.fp16}"
    )
    logger.info(f"Training/evaluation parameters {training_args}")

    # Detecting last checkpoint.
    last_checkpoint = None
    if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
        last_checkpoint = 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 and training_args.resume_from_checkpoint is 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."
            )

    # 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.
        raw_datasets = load_dataset(
            data_args.dataset_name, data_args.dataset_config_name, cache_dir=model_args.cache_dir, keep_in_memory=False,
        )
    else:
        data_files = {}
        if data_args.train_file is not None:
            data_files["train"] = data_args.train_file
            extension = data_args.train_file.split(".")[-1]

        if data_args.validation_file is not None:
            data_files["validation"] = data_args.validation_file
            extension = data_args.validation_file.split(".")[-1]
        if data_args.test_file is not None:
            data_files["test"] = data_args.test_file
            extension = data_args.test_file.split(".")[-1]
        raw_datasets = load_dataset(extension, data_files=data_files, field="data", cache_dir=model_args.cache_dir)
    # 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.
    con = Parser.load('crf-con-en')
    glove_tokenizer = GloveTokenizer(glove_file_path=data_args.glove_file_path, vocab_size=2000000)

    config = NaryConfig()
    encoder = NaryTree(config)
    encoder.embeddings.load_pretrained_embeddings(
        torch.tensor(glove_tokenizer.embeddings_arr, device=training_args.device))    
    config_similarity = SimilarityConfig()
    model = Similarity(encoder, config_similarity)

    # Preprocessing the datasets.
    # Preprocessing is slighlty different for training and evaluation.
    if training_args.do_train:
        column_names = raw_datasets["train"].column_names
    elif training_args.do_eval:
        column_names = raw_datasets["validation"].column_names
    else:
        column_names = raw_datasets["test"].column_names

    def map_label_to_target(label, num_classes, ):
        target = [0] * num_classes  # torch.zeros(1, num_classes, dtype=torch.float)
        ceil = int(math.ceil(label))
        floor = int(math.floor(label))
        if ceil == floor:
            target[floor - 1] = 1
        else:
            target[floor - 1] = ceil - label
            target[ceil - 1] = label - floor
        return target

    def prepare_train_features(examples):
        examples['input_ids_A'] = []
        examples['input_ids_B'] = []
        examples['head_idx_A'] = []
        examples['head_idx_B'] = []
        examples['head_idx_r_A'] = []
        examples['head_idx_r_B'] = []
        examples['head_idx_l_A'] = []
        examples['head_idx_l_B'] = []
        examples['labels'] = []
        
        for sent_A in examples['sentence_A']:
            con_tree_A = str(con.predict(sent_A.split(), verbose=False)[0])
            input_ids_A, head_idx_A, head_idx_r_A, head_idx_l_A = prepare_input_from_constituency_tree(con_tree_A)
            input_ids_A = glove_tokenizer.convert_tokens_to_ids(input_ids_A)
            examples['input_ids_A'].append(input_ids_A)
            examples['head_idx_A'].append(head_idx_A)
            examples['head_idx_r_A'].append(head_idx_r_A)
            examples['head_idx_l_A'].append(head_idx_l_A)
        
        for sent_B in examples['sentence_B']:
            con_tree_B = str(con.predict(sent_B.split(), verbose=False)[0])
            input_ids_B, head_idx_B, head_idx_r_B, head_idx_l_B = prepare_input_from_constituency_tree(con_tree_B)
            input_ids_B = glove_tokenizer.convert_tokens_to_ids(input_ids_B)
            examples['input_ids_B'].append(input_ids_B)
            examples['head_idx_B'].append(head_idx_B)
            examples['head_idx_r_B'].append(head_idx_r_B)
            examples['head_idx_l_B'].append(head_idx_l_B)

        for rel_score in examples['relatedness_score']:
            examples['labels'].append(map_label_to_target(rel_score, 5))

        return examples

    if training_args.do_train:
        if "train" not in raw_datasets:
            raise ValueError("--do_train requires a train dataset")
        train_dataset = raw_datasets["train"]
        if data_args.max_train_samples is not None:
            # We will select sample from whole data if argument is specified
            train_dataset = train_dataset.select(range(data_args.max_train_samples))
        # Create train feature from dataset
        with training_args.main_process_first(desc="train dataset map pre-processing"):
            train_dataset = train_dataset.map(
                prepare_train_features,
                batched=True,
                num_proc=data_args.preprocessing_num_workers,  # TODO if cuda is available, set to 0
                remove_columns=column_names,
                load_from_cache_file=not data_args.overwrite_cache,
                desc="Running parser on train dataset",
            )
        if data_args.max_train_samples is not None:
            # Number of samples might increase during Feature Creation, We select only specified max samples
            train_dataset = train_dataset.select(range(data_args.max_train_samples))

    # Validation preprocessing
    if training_args.do_eval:
        if "validation" not in raw_datasets:
            raise ValueError("--do_eval requires a validation dataset")
        eval_examples = raw_datasets["validation"]
        if data_args.max_eval_samples is not None:
            # We will select sample from whole data
            eval_examples = eval_examples.select(range(data_args.max_eval_samples))
        # Validation Feature Creation
        with training_args.main_process_first(desc="validation dataset map pre-processing"):
            eval_dataset = eval_examples.map(
                prepare_train_features,
                batched=True,
                num_proc=data_args.preprocessing_num_workers,
                remove_columns=None,  # column_names,
                load_from_cache_file=not data_args.overwrite_cache,
                desc="Running parser on validation dataset",
            )
        if data_args.max_eval_samples is not None:
            # During Feature creation dataset samples might increase, we will select required samples again
            eval_dataset = eval_dataset.select(range(data_args.max_eval_samples))

    if training_args.do_predict:
        if "test" not in raw_datasets:
            raise ValueError("--do_predict requires a test dataset")
        predict_examples = raw_datasets["test"]
        if data_args.max_predict_samples is not None:
            # We will select sample from whole data
            predict_examples = predict_examples.select(range(data_args.max_predict_samples))
        # Predict Feature Creation
        with training_args.main_process_first(desc="prediction dataset map pre-processing"):
            predict_dataset = predict_examples.map(
                prepare_train_features,
                batched=True,
                num_proc=data_args.preprocessing_num_workers,
                remove_columns=None,  # column_names,
                load_from_cache_file=not data_args.overwrite_cache,
                desc="Running parser on prediction dataset",
            )
        if data_args.max_predict_samples is not None:
            # During Feature creation dataset samples might increase, we will select required samples again
            predict_dataset = predict_dataset.select(range(data_args.max_predict_samples))

    def data_collator_with_padding(features, pad_ids=0, columns=None):
        batch = {}
        first = features[0]
        if columns is None:
            columns = ["head_idx_A", "head_idx_B", "input_ids_A", "input_ids_B", "head_idx_r_A", "head_idx_l_A", "head_idx_r_B", "head_idx_l_B"]
        feature_max_len = {k: max([len(f[k]) for f in features]) for k in first.keys() if k in columns or len(columns) == 0}
        for k, v in first.items():
            if k in columns or len(columns) == 0:
                feature_padded = [list([int(ff) for ff in f[k]]) + [0] * (feature_max_len[k] - len(f[k])) for f in features]
                batch[k] = feature_padded  # [f[k] for f in features]
        tree_ids_A, tree_ids_r_A, tree_ids_l_A = build_tree_ids_n_ary(batch['head_idx_A'], batch['head_idx_r_A'], batch['head_idx_l_A'])
        tree_ids_B, tree_ids_r_B, tree_ids_l_B = build_tree_ids_n_ary(batch['head_idx_B'], batch['head_idx_r_B'], batch['head_idx_l_B'])
        batch['input_ids_A'] = torch.tensor(batch['input_ids_A'])
        batch['input_ids_B'] = torch.tensor(batch['input_ids_B'])
        batch['tree_ids_A'] = torch.tensor(tree_ids_A)
        batch['tree_ids_B'] = torch.tensor(tree_ids_B)
        batch['tree_ids_r_A'] = torch.tensor(tree_ids_r_A)
        batch['tree_ids_r_B'] = torch.tensor(tree_ids_r_B)
        batch['tree_ids_l_A'] = torch.tensor(tree_ids_l_A)
        batch['tree_ids_l_B'] = torch.tensor(tree_ids_l_B)
        batch['labels'] = torch.tensor([f['labels'] for f in features])
        return batch

    data_collator = data_collator_with_padding

    def compute_metrics(eval_prediction):
        prediction = np.matmul(np.exp(eval_prediction.predictions), np.arange(1, 5 + 1))
        target = np.matmul(eval_prediction.label_ids, np.arange(1, 5 + 1))
        results_relatedness = {
            'pearson': pearsonr(prediction, target)[0] * 100,
            'spearman': spearmanr(prediction, target)[0] * 100,
            'mse': mean_squared_error(prediction, target) * 100
        }
        return results_relatedness
    
    # Initialize our Trainer
    trainer = SickTrainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset if training_args.do_train else None,
        eval_dataset=eval_dataset if training_args.do_eval else None,  # eval_dataset
        data_collator=data_collator,
        compute_metrics=compute_metrics,
        optimizers=("Adagrad", None),
    )

    # Training
    if training_args.do_train:
        checkpoint = None
        if training_args.resume_from_checkpoint is not None:
            checkpoint = training_args.resume_from_checkpoint
        elif last_checkpoint is not None:
            checkpoint = last_checkpoint
        train_result = trainer.train(resume_from_checkpoint=checkpoint)
        trainer.save_model()  # Saves the tokenizer too for easy upload

        metrics = train_result.metrics
        max_train_samples = (
            data_args.max_train_samples if data_args.max_train_samples is not None else len(train_dataset)
        )
        metrics["train_samples"] = min(max_train_samples, len(train_dataset))

        trainer.log_metrics("train", metrics)
        trainer.save_metrics("train", metrics)
        trainer.save_state()

    # Evaluation
    if training_args.do_eval:
        logger.info("*** Evaluate ***")
        metrics = trainer.evaluate()

        max_eval_samples = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(eval_dataset)
        metrics["eval_samples"] = min(max_eval_samples, len(eval_dataset))

        trainer.log_metrics("eval", metrics)
        trainer.save_metrics("eval", metrics)

    # Prediction
    if training_args.do_predict:
        logger.info("*** Predict ***")
        results = trainer.predict(predict_dataset)
        metrics = results.metrics

        max_predict_samples = (
            data_args.max_predict_samples if data_args.max_predict_samples is not None else len(predict_dataset)
        )
        metrics["predict_samples"] = min(max_predict_samples, len(predict_dataset))

        trainer.log_metrics("predict", metrics)
        trainer.save_metrics("predict", metrics)

    kwargs = {"finetuned_from": model_args.model_name_or_path, "tasks": "sick-relatedness"}
    if data_args.dataset_name is not None:
        kwargs["dataset_tags"] = data_args.dataset_name
        if data_args.dataset_config_name is not None:
            kwargs["dataset_args"] = data_args.dataset_config_name
            kwargs["dataset"] = f"{data_args.dataset_name} {data_args.dataset_config_name}"
        else:
            kwargs["dataset"] = data_args.dataset_name

    if training_args.push_to_hub:
        trainer.push_to_hub(**kwargs)
    else:
        trainer.create_model_card(**kwargs)


def _mp_fn(index):
    # For xla_spawn (TPUs)
    main()


if __name__ == "__main__":
    main()
#!/usr/bin/env python # coding=utf-8 # Copyright 2020 The HuggingFace Team All and <NAME> rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #     http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. Train tree structured models on the SICK task. # You can also adapt this script on your own task. Pointers for this are left as comments. # Will error if the minimal version of Transformers is not installed. Remove at your own risks. # 4.12.0.dev0 Setup the optimizer.          We provide a reasonable default that works well. If you want to use something else, you can pass a tuple in the         Trainer's init through :obj:`optimizers`, or subclass and override this method in a subclass. # if self.sharded_ddp == ShardedDDPOption.SIMPLE: #     self.optimizer = OSS( #         params=optimizer_grouped_parameters, #         optim=optimizer_cls, #         **optimizer_kwargs, #     ) # else: #     self.optimizer = optimizer_cls(optimizer_grouped_parameters, **optimizer_kwargs) # if is_sagemaker_mp_enabled(): #     self.optimizer = smp.DistributedOptimizer(self.optimizer) Arguments pertaining to which model/config/tokenizer we are going to fine-tune from. Arguments pertaining to what data we are going to input our model for training and eval. # 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. # 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. # Setup logging # Log on each process the small summary: # Detecting last checkpoint. # Set seed before initializing model. # 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. # Downloading and loading a dataset from the hub. # 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. # Preprocessing the datasets. # Preprocessing is slighlty different for training and evaluation. # torch.zeros(1, num_classes, dtype=torch.float) # We will select sample from whole data if argument is specified # Create train feature from dataset # TODO if cuda is available, set to 0 # Number of samples might increase during Feature Creation, We select only specified max samples # Validation preprocessing # We will select sample from whole data # Validation Feature Creation # column_names, # During Feature creation dataset samples might increase, we will select required samples again # We will select sample from whole data # Predict Feature Creation # column_names, # During Feature creation dataset samples might increase, we will select required samples again # [f[k] for f in features] # Initialize our Trainer # eval_dataset # Training # Saves the tokenizer too for easy upload # Evaluation # Prediction # For xla_spawn (TPUs)