



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


_lowercase:				List[str]       =	logging.getLogger(__name__)



@dataclass
class 			_lowercase							:





	"""simple docstring"""
	__A					=							field(
	    metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}			)
	__A					=							field(
	    default=lowerCAmelCase,   metadata={"help": "Pretrained config name or path if not the same as model_name"}			)
	__A					=							field(
	    default=lowerCAmelCase,   metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}			)
	__A					=							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.
	__A					=							field(
	    default=lowerCAmelCase,   metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"},   )






def 					a(   )	-> Optional[Any]:
					"""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"				,  A )

					# 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=A				,  )
					a        =     AutoModelForQuestionAnswering.from_pretrained(
					    model_args.model_name_or_path				,  from_tf=bool(".ckpt" in model_args.model_name_or_path )				,  config=A				,  cache_dir=model_args.cache_dir				,  )

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

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

					# Initialize our Trainer
					a        =     Trainer(
					    model=A				,  args=A				,  train_dataset=A				,  eval_dataset=A				,  data_collator=A				,  )

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






def 					a(   A :					Any )	-> Dict:
					"""simple docstring"""





					main()


if __name__ == "__main__":
				main()





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device


if is_torch_available():
				import torch

				from transformers import XLMProphetNetForConditionalGeneration, XLMProphetNetTokenizer



@require_torch
class 			_lowercase							(     unittest.TestCase			):





	"""simple docstring"""




	@slow
	def   UpperCamelCase_ (self	):
						"""simple docstring"""

						a        =     XLMProphetNetForConditionalGeneration.from_pretrained("microsoft/xprophetnet-large-wiki100-cased"	)
						model.to(lowerCamelCase_	)

						# encoder-decoder outputs
						a        =     torch.tensor([[17, 96208, 103471, 2]]	).to(lowerCamelCase_	)
						a        =     torch.tensor(
						    [[2, 250, 9953, 34, 69489, 1620, 32, 118424, 624, 210, 105, 2913, 1032, 351]]	).to(lowerCamelCase_	)
						a        =     model(
						    input_ids=lowerCamelCase_	, attention_mask=lowerCamelCase_	, encoder_outputs=lowerCamelCase_	, decoder_input_ids=lowerCamelCase_	)
						a        =     output[0]
						a        =     torch.Size((1, 14, 250012)	)
						self.assertEqual(output_predited_logis.shape	, lowerCamelCase_	)
						a        =     torch.tensor(
						    [[[-6.3986, -8.2391, 12.5189], [-6.3289, -8.0864, 12.6211], [-6.2418, -8.0445, 12.7968]]]	).to(lowerCamelCase_	)
						self.assertTrue(torch.allclose(output_predited_logis[:, :3, :3]	, lowerCamelCase_	, atol=1E-4	)	)

						# encoder outputs
						a        =     model.prophetnet.encoder(lowerCamelCase_	)[0]
						a        =     torch.tensor(
						    [[[-1.4260, -0.7628, 0.8453], [-1.4719, -0.1391, 0.7807], [-1.7678, 0.0114, 0.4646]]]	).to(lowerCamelCase_	)
						a        =     torch.Size((1, 4, 1024)	)
						self.assertEqual(encoder_outputs.shape	, lowerCamelCase_	)
						self.assertTrue(torch.allclose(encoder_outputs[:, :3, :3]	, lowerCamelCase_	, atol=1E-4	)	)

						# decoder outputs
						a        =     model.prophetnet.decoder(
						    lowerCamelCase_	, encoder_hidden_states=lowerCamelCase_	, )
						a        =     decoder_outputs[1].view(1	, model.config.ngram	, 14	, -1	)
						a        =     model.lm_head(lowerCamelCase_	)
						a        =     predicting_streams_logits[:, 0]
						self.assertTrue(torch.allclose(next_first_stream_logits[:, :3, :3]	, lowerCamelCase_	, atol=1E-4	)	)




	@slow
	def   UpperCamelCase_ (self	):
						"""simple docstring"""

						a        =     XLMProphetNetForConditionalGeneration.from_pretrained(
						    "microsoft/xprophetnet-large-wiki100-cased-xglue-ntg"	)
						model.to(lowerCamelCase_	)

						a        =     torch.tensor([[17, 96208, 103471, 2]]	).to(lowerCamelCase_	)
						a        =     torch.tensor(
						    [[2, 250, 9953, 34, 69489, 1620, 32, 118424, 624, 210, 105, 2913, 1032, 351]]	).to(lowerCamelCase_	)
						a        =     model(
						    input_ids=lowerCamelCase_	, attention_mask=lowerCamelCase_	, encoder_outputs=lowerCamelCase_	, decoder_input_ids=lowerCamelCase_	)
						a        =     output[0]
						a        =     torch.Size((1, 14, 250012)	)
						self.assertEqual(output_predited_logis.shape	, lowerCamelCase_	)
						# compare the actual values for a slice.
						a        =     torch.tensor(
						    [[[-9.2253, -9.7173, -6.3529], [-7.6701, -9.0145, -1.9382], [-8.0195, -7.0004, -0.1523]]]	).to(lowerCamelCase_	)

						self.assertTrue(torch.allclose(output_predited_logis[:, :3, :3]	, lowerCamelCase_	, atol=1E-4	)	)




	@slow
	def   UpperCamelCase_ (self	):
						"""simple docstring"""

						a        =     XLMProphetNetForConditionalGeneration.from_pretrained(
						    "microsoft/xprophetnet-large-wiki100-cased-xglue-ntg"	)
						model.to(lowerCamelCase_	)
						a        =     512

						a        =     XLMProphetNetTokenizer.from_pretrained("microsoft/xprophetnet-large-wiki100-cased-xglue-ntg"	)

						a        =     (
						    "Microsoft Corporation intends to officially end free support for the Windows 7 operating system after"
						    " January 14, 2020, according to the official portal of the organization. From that day, users of this"
						    " system will not be able to receive security updates, which could make their computers vulnerable to"
						    " cyber attacks."
						)
						a        =     (
						    "орпорация Microsoft намерена официально прекратить бесплатную поддержку операционной системы Windows 7"
						    " после 14 января 2020 года, сообщается на официальном портале организации . С указанного дня пользователи"
						    " этой системы не смогут получать обновления безопасности, из-за чего их компьютеры могут стать уязвимыми"
						    " к кибератакам."
						)
						a        =     (
						    "根据该组织的官方门户网站，微软公司打算在2020年1月14日之后正式终止对Windows 7操作系统的免费支持。从那时起，该系统的用户将无法接收安全更新，这可能会使他们的计算机容易受到网络攻击。"
						)

						a        =     tokenizer(
						    [EN_SENTENCE, RU_SENTENCE, ZH_SENTENCE]	, padding=lowerCamelCase_	, max_length=255	, return_tensors="pt"	).input_ids
						a        =     input_ids.to(lowerCamelCase_	)

						a        =     model.generate(
						    lowerCamelCase_	, num_beams=10	, length_penalty=1.0	, no_repeat_ngram_size=3	, early_stopping=lowerCamelCase_	)
						a        =     [tokenizer.decode(lowerCamelCase_	, skip_special_tokens=lowerCamelCase_	) for g in summary_ids]
						a        =     "Microsoft to end Windows 7 free support after January 14, 2020"
						a        =     "Microsoft намерена прекратить бесплатную поддержку Windows 7 после 14 января 2020 года"
						a        =     "微软打算终止对Windows 7操作系统的免费支持"
						self.assertListEqual(
						    [EXPECTED_TITLE_EN, EXPECTED_TITLE_RU, EXPECTED_TITLE_ZH]	, lowerCamelCase_	, )

						a        =     model.generate(
						    lowerCamelCase_	, num_beams=1	, length_penalty=1.0	, no_repeat_ngram_size=3	, early_stopping=lowerCamelCase_	)
						a        =     [
						    tokenizer.convert_ids_to_tokens(lowerCamelCase_	, skip_special_tokens=lowerCamelCase_	) for g in summary_ids_beama
						]
						a        =     "▁Microsoft ▁to ▁end ▁free ▁support ▁for ▁Windows ▁7".split(" "	)
						a        =     "▁Microsoft ▁намерен а ▁прекрати ть ▁бес плат ную ▁поддержку ▁Windows ▁7 ▁после ▁14 ▁января ▁2020 ▁года".split(
						    " "	)
						a        =     "微软 公司 打算 终止 对 Windows ▁7 操作 系统的 免费 支持".split(" "	)
						self.assertListEqual(
						    [EXPECTED_TITLE_EN_BEAM1_TOK, EXPECTED_TITLE_RU_BEAM1_TOK, EXPECTED_TITLE_ZH_BEAM1_TOK]	, lowerCamelCase_	, )
