


import os
import time
from dataclasses import dataclass, field
from enum import Enum
from typing import Dict, List, Optional, Union

import torch
from filelock import FileLock
from torch.utils.data import Dataset

from ...models.auto.modeling_auto import MODEL_FOR_QUESTION_ANSWERING_MAPPING
from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging
from ..processors.squad import SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features


_A		       =						logging.get_logger(__name__)

_A		       =						list(MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys())
_A		       =						tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)






@dataclass
class     UpperCAmelCase__       :



    """simple docstring"""






    UpperCAmelCase__						:		str      =  field(
        default=A_   ,  metadata={"help": "Model type selected in the list: " + ", ".join(A_		)}		)
    UpperCAmelCase__						:		str      =  field(
        default=A_   ,  metadata={"help": "The input data dir. Should contain the .json files for the SQuAD task."}		)
    UpperCAmelCase__						:		int      =  field(
        default=1_2_8   ,  metadata={
            "help": (
                "The maximum total input sequence length after tokenization. Sequences longer "
                "than this will be truncated, sequences shorter will be padded."
            )
        }   ,  )
    UpperCAmelCase__						:		int      =  field(
        default=1_2_8   ,  metadata={"help": "When splitting up a long document into chunks, how much stride to take between chunks."}   ,  )
    UpperCAmelCase__						:		int      =  field(
        default=6_4   ,  metadata={
            "help": (
                "The maximum number of tokens for the question. Questions longer than this will "
                "be truncated to this length."
            )
        }   ,  )
    UpperCAmelCase__						:		int      =  field(
        default=3_0   ,  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."
            )
        }   ,  )
    UpperCAmelCase__						:		bool      =  field(
        default=A_   ,  metadata={"help": "Overwrite the cached training and evaluation sets"}		)
    UpperCAmelCase__						:		bool      =  field(
        default=A_   ,  metadata={"help": "If true, the SQuAD examples contain some that do not have an answer."}		)
    UpperCAmelCase__						:		float      =  field(
        default=0.0   ,  metadata={"help": "If null_score - best_non_null is greater than the threshold predict null."}		)
    UpperCAmelCase__						:		int      =  field(
        default=2_0   ,  metadata={"help": "If null_score - best_non_null is greater than the threshold predict null."}		)
    UpperCAmelCase__						:		int      =  field(
        default=0   ,  metadata={
            "help": (
                "language id of input for language-specific xlm models (see"
                " tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)"
            )
        }   ,  )
    UpperCAmelCase__						:		int      =  field(default=1   ,  metadata={"help": "multiple threads for converting example to features"}		)






class     UpperCAmelCase__       (					A_		):



    """simple docstring"""






    UpperCAmelCase__						:		List[str]      =  '''train'''
    UpperCAmelCase__						:		List[Any]      =  '''dev'''






class     UpperCAmelCase__       (					A_		):



    """simple docstring"""






    UpperCAmelCase__						:		SquadDataTrainingArguments
    UpperCAmelCase__						:		List[SquadFeatures]
    UpperCAmelCase__						:		Split
    UpperCAmelCase__						:		bool






    def __init__(			self					,   A_					,   A_					,   A_ = None					,   A_ = Split.train					,   A_ = False					,   A_ = None					,   A_ = "pt"					,   )  ->       Union[str, Any]:
         __UpperCamelCase    							=args
         __UpperCamelCase    							=is_language_sensitive
         __UpperCamelCase    							=SquadVaProcessor() if args.version_2_with_negative else SquadVaProcessor()
         if isinstance(__lowerCAmelCase					,   __lowerCAmelCase    ):
              try:
                   __UpperCamelCase    							=Split[mode]
              except KeyError:
                   raise KeyError('mode is not a valid split name'    )
         __UpperCamelCase    							=mode
         # Load data features from cache or dataset file
         __UpperCamelCase    							='v2' if args.version_2_with_negative else 'v1'
         __UpperCamelCase    							=os.path.join(
             cache_dir if cache_dir is not None else args.data_dir					,   f'cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{version_tag}'					,   )

         # Make sure only the first process in distributed training processes the dataset,
         # and the others will use the cache.
         __UpperCamelCase    							=cached_features_file + '.lock'
         with FileLock(__lowerCAmelCase    ):
              if os.path.exists(__lowerCAmelCase    ) and not args.overwrite_cache:
                   __UpperCamelCase    							=time.time()
                   __UpperCamelCase    							=torch.load(__lowerCAmelCase    )

                   # Legacy cache files have only features, while new cache files
                   # will have dataset and examples also.
                   __UpperCamelCase    							=self.old_features['features']
                   __UpperCamelCase    							=self.old_features.get('dataset'					,   __lowerCAmelCase    )
                   __UpperCamelCase    							=self.old_features.get('examples'					,   __lowerCAmelCase    )
                   logger.info(
                       f'Loading features from cached file {cached_features_file} [took %.3f s]'					,   time.time() - start    )

                   if self.dataset is None or self.examples is None:
                        logger.warning(
                            f'Deleting cached file {cached_features_file} will allow dataset and examples to be cached in'
                            ' future run'    )
              else:
                   if mode == Split.dev:
                        __UpperCamelCase    							=self.processor.get_dev_examples(args.data_dir    )
                   else:
                        __UpperCamelCase    							=self.processor.get_train_examples(args.data_dir    )

                   __UpperCamelCase							,      __UpperCamelCase    							=squad_convert_examples_to_features(
                       examples=self.examples					,   tokenizer=__lowerCAmelCase					,   max_seq_length=args.max_seq_length					,   doc_stride=args.doc_stride					,   max_query_length=args.max_query_length					,   is_training=mode == Split.train					,   threads=args.threads					,   return_dataset=__lowerCAmelCase					,   )

                   __UpperCamelCase    							=time.time()
                   torch.save(
                       {'features': self.features, 'dataset': self.dataset, 'examples': self.examples}					,   __lowerCAmelCase					,   )
                   # ^ This seems to take a lot of time so I want to investigate why and how we can improve.
                   logger.info(
                       f'Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]'    )






    def __len__(			self    )  ->       Any:
         return len(self.features    )






    def __getitem__(			self					,   A_    )  ->       Dict[str, torch.Tensor]:
         __UpperCamelCase    							=self.features[i]

         __UpperCamelCase    							=torch.tensor(feature.input_ids					,   dtype=torch.long    )
         __UpperCamelCase    							=torch.tensor(feature.attention_mask					,   dtype=torch.long    )
         __UpperCamelCase    							=torch.tensor(feature.token_type_ids					,   dtype=torch.long    )
         __UpperCamelCase    							=torch.tensor(feature.cls_index					,   dtype=torch.long    )
         __UpperCamelCase    							=torch.tensor(feature.p_mask					,   dtype=torch.float    )
         __UpperCamelCase    							=torch.tensor(feature.is_impossible					,   dtype=torch.float    )

         __UpperCamelCase    							={
             'input_ids': input_ids,
             'attention_mask': attention_mask,
             'token_type_ids': token_type_ids,
         }

         if self.args.model_type in ["xlm", "roberta", "distilbert", "camembert"]:
              del inputs["token_type_ids"]

         if self.args.model_type in ["xlnet", "xlm"]:
              inputs.update({'cls_index': cls_index, 'p_mask': p_mask}    )
              if self.args.version_2_with_negative:
                   inputs.update({'is_impossible': is_impossible}    )
              if self.is_language_sensitive:
                   inputs.update({'langs': (torch.ones(input_ids.shape					,   dtype=torch.intaa    ) * self.args.lang_id)}    )

         if self.mode == Split.train:
              __UpperCamelCase    							=torch.tensor(feature.start_position					,   dtype=torch.long    )
              __UpperCamelCase    							=torch.tensor(feature.end_position					,   dtype=torch.long    )
              inputs.update({'start_positions': start_positions, 'end_positions': end_positions}    )

         return inputs












import json
import os
from pathlib import Path
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple, Union

import sentencepiece

from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer
from ...utils import logging


A    :					Optional[Any] =      logging.get_logger(__name__)

A    :					str =      '''▁'''

A    :					Any =      {
    '''vocab_file''': '''vocab.json''',
    '''spm_file''': '''sentencepiece.bpe.model''',
    '''tokenizer_config_file''': '''tokenizer_config.json''',
}

A    :					List[Any] =      {
    '''vocab_file''': {
        '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''',
        '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''',
    },
    '''spm_file''': {
        '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''',
        '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''',
    },
    '''tokenizer_config_file''': {
        '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''',
        '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''',
    },
}

A    :					Tuple =      {
    '''facebook/m2m100_418M''': 1_0_2_4,
}

# fmt: off
A    :					Optional[int] =      {
    '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''],
    '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de''']
}


class   A			(SCREAMING_SNAKE_CASE    ):
			'''simple docstring'''




			__lowerCamelCase	:       Union[str, Any]							=    VOCAB_FILES_NAMES
			__lowerCamelCase	:       List[str]							=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			__lowerCamelCase	:       Optional[int]							=    PRETRAINED_VOCAB_FILES_MAP
			__lowerCamelCase	:       Dict							=    ['''input_ids''', '''attention_mask''']

			__lowerCamelCase	:       List[int]							=    []
			__lowerCamelCase	:       List[int]							=    []






			def __init__(			self   :    List[Any]							,	__lowerCAmelCase   :    List[Any]							,	__lowerCAmelCase   :    Dict							,	__lowerCAmelCase   :    Union[str, Any]=None							,	__lowerCAmelCase   :    str=None							,	__lowerCAmelCase   :    List[Any]="<s>"							,	__lowerCAmelCase   :    List[Any]="</s>"							,	__lowerCAmelCase   :    Optional[int]="</s>"							,	__lowerCAmelCase   :    Optional[Any]="<pad>"							,	__lowerCAmelCase   :    Any="<unk>"							,	__lowerCAmelCase   :    Any="m2m100"							,	__lowerCAmelCase   :    Optional[Dict[str, Any]] = None							,	__lowerCAmelCase   :    Dict=8							,	**__lowerCAmelCase   :    Tuple							,	)	->	None:







					"""simple docstring"""






					A__        =   {} if sp_model_kwargs is None else sp_model_kwargs

					A__        =   language_codes
					A__        =   FAIRSEQ_LANGUAGE_CODES[language_codes]
					A__        =   {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code}

					A__        =   kwargs.get("""additional_special_tokens"""							,	[]   )
					kwargs["additional_special_tokens"] += [
					    self.get_lang_token(__lowerCAmelCase   )
					    for lang_code in fairseq_language_code
					    if self.get_lang_token(__lowerCAmelCase   ) not in kwargs["additional_special_tokens"]
					]

					super().__init__(
					    src_lang=__lowerCAmelCase							,	tgt_lang=__lowerCAmelCase							,	bos_token=__lowerCAmelCase							,	eos_token=__lowerCAmelCase							,	sep_token=__lowerCAmelCase							,	unk_token=__lowerCAmelCase							,	pad_token=__lowerCAmelCase							,	language_codes=__lowerCAmelCase							,	sp_model_kwargs=self.sp_model_kwargs							,	num_madeup_words=__lowerCAmelCase							,	**__lowerCAmelCase							,	)

					A__        =   vocab_file
					A__        =   load_json(__lowerCAmelCase   )
					A__        =   {v: k for k, v in self.encoder.items()}
					A__        =   spm_file
					A__        =   load_spm(__lowerCAmelCase							,	self.sp_model_kwargs   )

					A__        =   len(self.encoder   )

					A__        =   {
					    self.get_lang_token(__lowerCAmelCase   ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase   )
					}
					A__        =   {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase   )}
					A__        =   {v: k for k, v in self.lang_token_to_id.items()}

					A__        =   src_lang if src_lang is not None else """en"""
					A__        =   tgt_lang
					A__        =   self.get_lang_id(self._src_lang   )
					self.set_src_lang_special_tokens(self._src_lang   )

					A__        =   num_madeup_words






			@property
			def    a_       (			self   :    Optional[int]   )	->	int:







					"""simple docstring"""






					return len(self.encoder   ) + len(self.lang_token_to_id   )






			@property
			def    a_       (			self   :    Optional[Any]   )	->	str:







					"""simple docstring"""






					return self._src_lang






			@src_lang.setter
			def    a_       (			self   :    List[Any]							,	__lowerCAmelCase   :    str   )	->	None:







					"""simple docstring"""






					A__        =   new_src_lang
					self.set_src_lang_special_tokens(self._src_lang   )






			def    a_       (			self   :    Optional[int]							,	__lowerCAmelCase   :    str   )	->	List[str]:







					"""simple docstring"""






					return self.sp_model.encode(__lowerCAmelCase							,	out_type=__lowerCAmelCase   )






			def    a_       (			self   :    Optional[Any]							,	__lowerCAmelCase   :    Dict   )	->	Optional[Any]:







					"""simple docstring"""






					if token in self.lang_token_to_id:
							return self.lang_token_to_id[token]
					return self.encoder.get(__lowerCAmelCase							,	self.encoder[self.unk_token]   )






			def    a_       (			self   :    Optional[int]							,	__lowerCAmelCase   :    int   )	->	str:







					"""simple docstring"""






					if index in self.id_to_lang_token:
							return self.id_to_lang_token[index]
					return self.decoder.get(__lowerCAmelCase							,	self.unk_token   )






			def    a_       (			self   :    Optional[int]							,	__lowerCAmelCase   :    Dict   )	->	str:







					"""simple docstring"""






					A__        =   []
					A__        =   """"""
					for token in tokens:
							# make sure that special tokens are not decoded using sentencepiece model
							if token in self.all_special_tokens:
									out_string += self.sp_model.decode(__lowerCAmelCase   ) + token
									A__        =   []
							else:
									current_sub_tokens.append(__lowerCAmelCase   )
					out_string += self.sp_model.decode(__lowerCAmelCase   )
					return out_string.strip()






			def    a_       (			self   :    List[str]							,	__lowerCAmelCase   :    List[int]							,	__lowerCAmelCase   :    Optional[List[int]] = None							,	__lowerCAmelCase   :    bool = False   )	->	List[int]:







					"""simple docstring"""






					if already_has_special_tokens:
							return super().get_special_tokens_mask(
							    token_ids_a=__lowerCAmelCase							,	token_ids_a=__lowerCAmelCase							,	already_has_special_tokens=__lowerCAmelCase   )

					A__        =   [1] * len(self.prefix_tokens   )
					A__        =   [1] * len(self.suffix_tokens   )
					if token_ids_a is None:
							return prefix_ones + ([0] * len(__lowerCAmelCase   )) + suffix_ones
					return prefix_ones + ([0] * len(__lowerCAmelCase   )) + ([0] * len(__lowerCAmelCase   )) + suffix_ones






			def    a_       (			self   :    Tuple							,	__lowerCAmelCase   :    List[int]							,	__lowerCAmelCase   :    Optional[List[int]] = None   )	->	List[int]:







					"""simple docstring"""






					if token_ids_a is None:
							return self.prefix_tokens + token_ids_a + self.suffix_tokens
					# We don't expect to process pairs, but leave the pair logic for API consistency
					return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens






			def    a_       (			self   :    int   )	->	Dict:







					"""simple docstring"""






					A__        =   {self.convert_ids_to_tokens(__lowerCAmelCase   ): i for i in range(self.vocab_size   )}
					vocab.update(self.added_tokens_encoder   )
					return vocab






			def __getstate__(			self   :    Union[str, Any]   )	->	Dict:







					"""simple docstring"""






					A__        =   self.__dict__.copy()
					A__        =   None
					return state






			def __setstate__(			self   :    str							,	__lowerCAmelCase   :    Dict   )	->	None:







					"""simple docstring"""






					A__        =   d

					# for backward compatibility
					if not hasattr(self							,	"""sp_model_kwargs"""   ):
							A__        =   {}

					A__        =   load_spm(self.spm_file							,	self.sp_model_kwargs   )






			def    a_       (			self   :    List[str]							,	__lowerCAmelCase   :    str							,	__lowerCAmelCase   :    Optional[str] = None   )	->	Tuple[str]:







					"""simple docstring"""






					A__        =   Path(__lowerCAmelCase   )
					if not save_dir.is_dir():
							raise OSError(f'{save_directory} should be a directory'   )
					A__        =   save_dir / (
					    (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""]
					)
					A__        =   save_dir / (
					    (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""]
					)

					save_json(self.encoder							,	__lowerCAmelCase   )

					if os.path.abspath(self.spm_file   ) != os.path.abspath(__lowerCAmelCase   ) and os.path.isfile(self.spm_file   ):
							copyfile(self.spm_file							,	__lowerCAmelCase   )
					elif not os.path.isfile(self.spm_file   ):
							with open(__lowerCAmelCase							,	"""wb"""   ) as fi:
									A__        =   self.sp_model.serialized_model_proto()
									fi.write(__lowerCAmelCase   )

					return (str(__lowerCAmelCase   ), str(__lowerCAmelCase   ))






			def    a_       (			self   :    str							,	__lowerCAmelCase   :    List[str]							,	__lowerCAmelCase   :    str = "en"							,	__lowerCAmelCase   :    Optional[List[str]] = None							,	__lowerCAmelCase   :    str = "ro"							,	**__lowerCAmelCase   :    List[Any]							,	)	->	BatchEncoding:







					"""simple docstring"""






					A__        =   src_lang
					A__        =   tgt_lang
					self.set_src_lang_special_tokens(self.src_lang   )
					return super().prepare_seqaseq_batch(__lowerCAmelCase							,	__lowerCAmelCase							,	**__lowerCAmelCase   )






			def    a_       (			self   :    Optional[int]							,	__lowerCAmelCase   :    Any							,	__lowerCAmelCase   :    Optional[str]							,	__lowerCAmelCase   :    Optional[str]							,	**__lowerCAmelCase   :    Tuple   )	->	Tuple:







					"""simple docstring"""






					if src_lang is None or tgt_lang is None:
							raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model"""   )
					A__        =   src_lang
					A__        =   self(__lowerCAmelCase							,	add_special_tokens=__lowerCAmelCase							,	**__lowerCAmelCase   )
					A__        =   self.get_lang_id(__lowerCAmelCase   )
					A__        =   tgt_lang_id
					return inputs






			def    a_       (			self   :    Dict   )	->	int:







					"""simple docstring"""






					self.set_src_lang_special_tokens(self.src_lang   )






			def    a_       (			self   :    Union[str, Any]   )	->	Union[str, Any]:







					"""simple docstring"""






					self.set_tgt_lang_special_tokens(self.tgt_lang   )






			def    a_       (			self   :    str							,	__lowerCAmelCase   :    str   )	->	None:







					"""simple docstring"""






					A__        =   self.get_lang_token(__lowerCAmelCase   )
					A__        =   self.lang_token_to_id[lang_token]
					A__        =   [self.cur_lang_id]
					A__        =   [self.eos_token_id]






			def    a_       (			self   :    Tuple							,	__lowerCAmelCase   :    str   )	->	None:







					"""simple docstring"""






					A__        =   self.get_lang_token(__lowerCAmelCase   )
					A__        =   self.lang_token_to_id[lang_token]
					A__        =   [self.cur_lang_id]
					A__        =   [self.eos_token_id]






			def    a_       (			self   :    Union[str, Any]							,	__lowerCAmelCase   :    str   )	->	str:







					"""simple docstring"""






					return self.lang_code_to_token[lang]







			def    a_       (			self   :    Union[str, Any]							,	__lowerCAmelCase   :    str   )	->	int:







					"""simple docstring"""






					A__        =   self.get_lang_token(__lowerCAmelCase   )
					return self.lang_token_to_id[lang_token]







def    __lowerCamelCase					(							__a   :str    ,     __a   :Dict[str, Any]    )		->				sentencepiece.SentencePieceProcessor:






		"""simple docstring"""

		A__        =   sentencepiece.SentencePieceProcessor(**__a    )
		spm.Load(str(__a    )    )
		return spm







def    __lowerCamelCase					(							__a   :str    )		->				Union[Dict, List]:






		"""simple docstring"""

		with open(__a    ,     """r"""    ) as f:
				return json.load(__a    )







def    __lowerCamelCase					(							__a   :List[Any]    ,     __a   :str    )		->				None:






		"""simple docstring"""

		with open(__a    ,     """w"""    ) as f:
				json.dump(__a    ,     __a    ,     indent=2    )



