



import json
import os
from typing import TYPE_CHECKING, List, Optional, Tuple

import regex as re

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


if TYPE_CHECKING:
						from transformers.pipelines.conversational import Conversation

lowerCamelCase__     =	logging.get_logger(__name__)

lowerCamelCase__     =	{"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""}

lowerCamelCase__     =	{
    """vocab_file""": {
        """microsoft/deberta-base""": """https://huggingface.co/microsoft/deberta-base/resolve/main/vocab.json""",
        """microsoft/deberta-large""": """https://huggingface.co/microsoft/deberta-large/resolve/main/vocab.json""",
        """microsoft/deberta-xlarge""": """https://huggingface.co/microsoft/deberta-xlarge/resolve/main/vocab.json""",
        """microsoft/deberta-base-mnli""": """https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/vocab.json""",
        """microsoft/deberta-large-mnli""": """https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/vocab.json""",
        """microsoft/deberta-xlarge-mnli""": (
            """https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/vocab.json"""
        ),
    },
    """merges_file""": {
        """microsoft/deberta-base""": """https://huggingface.co/microsoft/deberta-base/resolve/main/merges.txt""",
        """microsoft/deberta-large""": """https://huggingface.co/microsoft/deberta-large/resolve/main/merges.txt""",
        """microsoft/deberta-xlarge""": """https://huggingface.co/microsoft/deberta-xlarge/resolve/main/merges.txt""",
        """microsoft/deberta-base-mnli""": """https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/merges.txt""",
        """microsoft/deberta-large-mnli""": """https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/merges.txt""",
        """microsoft/deberta-xlarge-mnli""": (
            """https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/merges.txt"""
        ),
    },
}

lowerCamelCase__     =	{
    """microsoft/deberta-base""": 512,
    """microsoft/deberta-large""": 512,
    """microsoft/deberta-xlarge""": 512,
    """microsoft/deberta-base-mnli""": 512,
    """microsoft/deberta-large-mnli""": 512,
    """microsoft/deberta-xlarge-mnli""": 512,
}

lowerCamelCase__     =	{
    """microsoft/deberta-base""": {"""do_lower_case""": False},
    """microsoft/deberta-large""": {"""do_lower_case""": False},
}

def 					lowerCAmelCase__  (							):
				"""simple docstring"""







				__a					       =						(
				    list(range(ord("""!"""      )			,       ord("""~"""      ) + 1      )      ) + list(range(ord("""¡"""      )			,       ord("""¬"""      ) + 1      )      ) + list(range(ord("""®"""      )			,       ord("""ÿ"""      ) + 1      )      )
				)
				__a					       =						bs[:]
				__a					       =						0
				for b in range(2**8      ):
								if b not in bs:
												bs.append(_SCREAMING_SNAKE_CASE      )
												cs.append(2**8 + n      )
												n += 1
				__a					       =						[chr(_SCREAMING_SNAKE_CASE      ) for n in cs]
				return dict(zip(_SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE      )      )

def 					lowerCAmelCase__  (							_SCREAMING_SNAKE_CASE    :     str      ):
				"""simple docstring"""







				__a					       =						set()
				__a					       =						word[0]
				for char in word[1:]:
								pairs.add((prev_char, char)      )
								__a					       =						char
				return pairs






class 				SCREAMING_SNAKE_CASE     (							lowerCamelCase__   ):
							__lowerCamelCase		:		Dict      =VOCAB_FILES_NAMES
							__lowerCamelCase		:		Any      =PRETRAINED_VOCAB_FILES_MAP
							__lowerCamelCase		:		int      =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							__lowerCamelCase		:		Optional[int]      =['input_ids', 'attention_mask', 'token_type_ids']





							def __init__(				self						:						Tuple ,			__lowercase						:						int ,			__lowercase						:						Dict ,			__lowercase						:						Optional[Any]="replace" ,			__lowercase						:						Any="[CLS]" ,			__lowercase						:						Tuple="[SEP]" ,			__lowercase						:						List[Any]="[SEP]" ,			__lowercase						:						Optional[int]="[CLS]" ,			__lowercase						:						Union[str, Any]="[UNK]" ,			__lowercase						:						Optional[Any]="[PAD]" ,			__lowercase						:						int="[MASK]" ,			__lowercase						:						Optional[int]=False ,			__lowercase						:						Optional[int]=False ,			**__lowercase						:						Any ,			):

											'''simple docstring'''
											__a					       =						AddedToken(__lowercase ,			lstrip=__lowercase ,			rstrip=__lowercase		) if isinstance(__lowercase ,			__lowercase		) else bos_token
											__a					       =						AddedToken(__lowercase ,			lstrip=__lowercase ,			rstrip=__lowercase		) if isinstance(__lowercase ,			__lowercase		) else eos_token
											__a					       =						AddedToken(__lowercase ,			lstrip=__lowercase ,			rstrip=__lowercase		) if isinstance(__lowercase ,			__lowercase		) else sep_token
											__a					       =						AddedToken(__lowercase ,			lstrip=__lowercase ,			rstrip=__lowercase		) if isinstance(__lowercase ,			__lowercase		) else cls_token
											__a					       =						AddedToken(__lowercase ,			lstrip=__lowercase ,			rstrip=__lowercase		) if isinstance(__lowercase ,			__lowercase		) else unk_token
											__a					       =						AddedToken(__lowercase ,			lstrip=__lowercase ,			rstrip=__lowercase		) if isinstance(__lowercase ,			__lowercase		) else pad_token

											# Mask token behave like a normal word, i.e. include the space before it
											__a					       =						AddedToken(__lowercase ,			lstrip=__lowercase ,			rstrip=__lowercase		) if isinstance(__lowercase ,			__lowercase		) else mask_token

											super().__init__(
											    errors=__lowercase ,			bos_token=__lowercase ,			eos_token=__lowercase ,			unk_token=__lowercase ,			sep_token=__lowercase ,			cls_token=__lowercase ,			pad_token=__lowercase ,			mask_token=__lowercase ,			add_prefix_space=__lowercase ,			add_bos_token=__lowercase ,			**__lowercase ,			)
											__a					       =						add_bos_token

											with open(__lowercase ,			encoding="""utf-8"""		) as vocab_handle:
															__a					       =						json.load(__lowercase		)
											__a					       =						{v: k for k, v in self.encoder.items()}
											__a					       =						errors  # how to handle errors in decoding
											__a					       =						bytes_to_unicode()
											__a					       =						{v: k for k, v in self.byte_encoder.items()}
											with open(__lowercase ,			encoding="""utf-8"""		) as merges_handle:
															__a					       =						merges_handle.read().split("""\n"""		)[1:-1]
											__a					       =						[tuple(merge.split()		) for merge in bpe_merges]
											__a					       =						dict(zip(__lowercase ,			range(len(__lowercase		)		)		)		)
											__a					       =						{}
											__a					       =						add_prefix_space

											# Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
											__a					       =						re.compile(r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""		)





							@property
							# Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.vocab_size
							def   UpperCamelCase_   (				self						:						str		):

											'''simple docstring'''
											return len(self.encoder		)





							def   UpperCamelCase_   (				self						:						str		):

											'''simple docstring'''
											return dict(self.encoder ,			**self.added_tokens_encoder		)





							def   UpperCamelCase_   (				self						:						Union[str, Any] ,			__lowercase						:						str		):

											'''simple docstring'''
											if token in self.cache:
															return self.cache[token]
											__a					       =						tuple(__lowercase		)
											__a					       =						get_pairs(__lowercase		)

											if not pairs:
															return token

											while True:
															__a					       =						min(__lowercase ,			key=lambda __lowercase		: self.bpe_ranks.get(__lowercase ,			float("""inf"""		)		)		)
															if bigram not in self.bpe_ranks:
																			break
															__a       ,   __a					       =						bigram
															__a					       =						[]
															__a					       =						0
															while i < len(__lowercase		):
																			try:
																							__a					       =						word.index(__lowercase ,			__lowercase		)
																			except ValueError:
																							new_word.extend(word[i:]		)
																							break
																			else:
																							new_word.extend(word[i:j]		)
																							__a					       =						j

																			if word[i] == first and i < len(__lowercase		) - 1 and word[i + 1] == second:
																							new_word.append(first + second		)
																							i += 2
																			else:
																							new_word.append(word[i]		)
																							i += 1
															__a					       =						tuple(__lowercase		)
															__a					       =						new_word
															if len(__lowercase		) == 1:
																			break
															else:
																			__a					       =						get_pairs(__lowercase		)
											__a					       =						""" """.join(__lowercase		)
											__a					       =						word
											return word





							def   UpperCamelCase_   (				self						:						Any ,			__lowercase						:						List[int] ,			__lowercase						:						Optional[List[int]] = None		):

											'''simple docstring'''
											if token_ids_a is None:
															return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
											__a					       =						[self.cls_token_id]
											__a					       =						[self.sep_token_id]
											return cls + token_ids_a + sep + token_ids_a + sep





							def   UpperCamelCase_   (				self						:						Union[str, Any] ,			__lowercase						:						List[int] ,			__lowercase						:						Optional[List[int]] = None ,			__lowercase						:						bool = False		):

											'''simple docstring'''
											if already_has_special_tokens:
															return super().get_special_tokens_mask(
															    token_ids_a=__lowercase ,			token_ids_a=__lowercase ,			already_has_special_tokens=__lowercase		)

											if token_ids_a is None:
															return [1] + ([0] * len(__lowercase		)) + [1]
											return [1] + ([0] * len(__lowercase		)) + [1] + ([0] * len(__lowercase		)) + [1]





							def   UpperCamelCase_   (				self						:						List[Any] ,			__lowercase						:						List[int] ,			__lowercase						:						Optional[List[int]] = None		):

											'''simple docstring'''
											__a					       =						[self.sep_token_id]
											__a					       =						[self.cls_token_id]

											if token_ids_a is None:
															return len(cls + token_ids_a + sep		) * [0]
											return len(cls + token_ids_a + sep		) * [0] + len(token_ids_a + sep		) * [1]





							def   UpperCamelCase_   (				self						:						Optional[Any] ,			__lowercase						:						Optional[int]		):

											'''simple docstring'''
											__a					       =						[]
											for token in re.findall(self.pat ,			__lowercase		):
															__a					       =						"""""".join(
															    self.byte_encoder[b] for b in token.encode("""utf-8"""		)		)  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
															bpe_tokens.extend(bpe_token for bpe_token in self.bpe(__lowercase		).split(""" """		)		)
											return bpe_tokens





							def   UpperCamelCase_   (				self						:						List[Any] ,			__lowercase						:						str		):

											'''simple docstring'''
											return self.encoder.get(__lowercase ,			self.encoder.get(self.unk_token		)		)





							def   UpperCamelCase_   (				self						:						List[Any] ,			__lowercase						:						Union[str, Any]		):

											'''simple docstring'''
											return self.decoder.get(__lowercase		)





							def   UpperCamelCase_   (				self						:						Optional[int] ,			__lowercase						:						List[str]		):

											'''simple docstring'''
											__a					       =						"""""".join(__lowercase		)
											__a					       =						bytearray([self.byte_decoder[c] for c in text]		).decode("""utf-8""" ,			errors=self.errors		)
											return text





							def   UpperCamelCase_   (				self						:						List[Any] ,			__lowercase						:						str ,			__lowercase						:						Optional[str] = None		):

											'''simple docstring'''
											if not os.path.isdir(__lowercase		):
															logger.error(F"Vocabulary path ({save_directory}) should be a directory"		)
															return
											__a					       =						os.path.join(
											    __lowercase ,			(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]		)
											__a					       =						os.path.join(
											    __lowercase ,			(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""]		)

											with open(__lowercase ,			"""w""" ,			encoding="""utf-8"""		) as f:
															f.write(json.dumps(self.encoder ,			indent=2 ,			sort_keys=__lowercase ,			ensure_ascii=__lowercase		) + """\n"""		)

											__a					       =						0
											with open(__lowercase ,			"""w""" ,			encoding="""utf-8"""		) as writer:
															writer.write("""#version: 0.2\n"""		)
															for bpe_tokens, token_index in sorted(self.bpe_ranks.items() ,			key=lambda __lowercase		: kv[1]		):
																			if index != token_index:
																							logger.warning(
																							    F"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."
																							    """ Please check that the tokenizer is not corrupted!"""		)
																							__a					       =						token_index
																			writer.write(""" """.join(__lowercase		) + """\n"""		)
																			index += 1

											return vocab_file, merge_file





							def   UpperCamelCase_   (				self						:						str ,			__lowercase						:						Any ,			__lowercase						:						int=False ,			**__lowercase						:						Optional[int]		):

											'''simple docstring'''
											__a					       =						kwargs.pop("""add_prefix_space""" ,			self.add_prefix_space		)
											if (is_split_into_words or add_prefix_space) and (len(__lowercase		) > 0 and not text[0].isspace()):
															__a					       =						""" """ + text
											return (text, kwargs)







							def   UpperCamelCase_   (				self						:						int ,			__lowercase						:						"Conversation"		):

											'''simple docstring'''
											__a					       =						[]
											for is_user, text in conversation.iter_texts():
															input_ids.extend(self.encode(__lowercase ,			add_special_tokens=__lowercase		) + [self.eos_token_id]		)
											if len(__lowercase		) > self.model_max_length:
															__a					       =						input_ids[-self.model_max_length :]
											return input_ids





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

import transformers
from transformers import (
    AutoConfig,
    AutoModelForQuestionAnswering,
    AutoTokenizer,
    DataCollatorWithPadding,
    HfArgumentParser,
    SquadDataset,
    Trainer,
    TrainingArguments,
)
from transformers import SquadDataTrainingArguments as DataTrainingArguments
from transformers.trainer_utils import is_main_process


lowerCamelCase__     =	logging.getLogger(__name__)






@dataclass
class 				SCREAMING_SNAKE_CASE     :
							__lowerCamelCase		:		str      =field(
							    metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}   )
							__lowerCamelCase		:		Optional[str]      =field(
							    default=lowerCamelCase__     , metadata={'help': 'Pretrained config name or path if not the same as model_name'}   )
							__lowerCamelCase		:		Optional[str]      =field(
							    default=lowerCamelCase__     , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'}   )
							__lowerCamelCase		:		bool      =field(default=lowerCamelCase__     , metadata={'help': 'Set this flag to use fast tokenization.'}   )
							# If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
							# or just modify its tokenizer_config.json.
							__lowerCamelCase		:		Optional[str]      =field(
							    default=lowerCamelCase__     , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}     , )



def 					lowerCAmelCase__  (							):
				"""simple docstring"""







				__a					       =						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.
								__a       ,   __a       ,   __a					       =						parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]      )      )
				else:
								__a       ,   __a       ,   __a					       =						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"""			,       _SCREAMING_SNAKE_CASE      )

				# Prepare Question-Answering task
				# Load pretrained model and tokenizer
				#
				# Distributed training:
				# The .from_pretrained methods guarantee that only one local process can concurrently
				# download model & vocab.

				__a					       =						AutoConfig.from_pretrained(
				    model_args.config_name if model_args.config_name else model_args.model_name_or_path			,       cache_dir=model_args.cache_dir			,       )
				__a					       =						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			,       use_fast=_SCREAMING_SNAKE_CASE			,       )
				__a					       =						AutoModelForQuestionAnswering.from_pretrained(
				    model_args.model_name_or_path			,       from_tf=bool(""".ckpt""" in model_args.model_name_or_path      )			,       config=_SCREAMING_SNAKE_CASE			,       cache_dir=model_args.cache_dir			,       )

				# Get datasets
				__a					       =						hasattr(model.config			,       """lang2id"""      )
				__a					       =						(
				    SquadDataset(
				        _SCREAMING_SNAKE_CASE			,       tokenizer=_SCREAMING_SNAKE_CASE			,       is_language_sensitive=_SCREAMING_SNAKE_CASE			,       cache_dir=model_args.cache_dir      )
				    if training_args.do_train
				    else None
				)
				__a					       =						(
				    SquadDataset(
				        _SCREAMING_SNAKE_CASE			,       tokenizer=_SCREAMING_SNAKE_CASE			,       mode="""dev"""			,       is_language_sensitive=_SCREAMING_SNAKE_CASE			,       cache_dir=model_args.cache_dir			,       )
				    if training_args.do_eval
				    else None
				)

				# Data collator
				__a					       =						DataCollatorWithPadding(_SCREAMING_SNAKE_CASE			,       pad_to_multiple_of=8      ) if training_args.fpaa else None

				# Initialize our Trainer
				__a					       =						Trainer(
				    model=_SCREAMING_SNAKE_CASE			,       args=_SCREAMING_SNAKE_CASE			,       train_dataset=_SCREAMING_SNAKE_CASE			,       eval_dataset=_SCREAMING_SNAKE_CASE			,       data_collator=_SCREAMING_SNAKE_CASE			,       )

				# Training
				if training_args.do_train:
								trainer.train(
								    model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path      ) else None      )
								trainer.save_model()
								# For convenience, we also re-save the tokenizer to the same directory,
								# so that you can share your model easily on huggingface.co/models =)
								if trainer.is_world_master():
												tokenizer.save_pretrained(training_args.output_dir      )

def 					lowerCAmelCase__  (							_SCREAMING_SNAKE_CASE    :     Optional[Any]      ):
				"""simple docstring"""







				main()


if __name__ == "__main__":
						main()
