




"""simple docstring"""




import argparse
import os

# New Code #
import evaluate
import torch
from datasets import load_dataset
from torch.optim import AdamW
from torch.utils.data import DataLoader
from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed

from accelerate import Accelerator, DistributedType
from accelerate.utils import find_executable_batch_size


########################################################################
# This is a fully working simple example to use Accelerate,
# specifically showcasing how to ensure out-of-memory errors never
# interrupt training, and builds off the `nlp_example.py` script.
#
# This example trains a Bert base model on GLUE MRPC
# in any of the following settings (with the same script):
#   - single CPU or single GPU
#   - multi GPUS (using PyTorch distributed mode)
#   - (multi) TPUs
#   - fp16 (mixed-precision) or fp32 (normal precision)
#
# New additions from the base script can be found quickly by
# looking for the # New Code # tags
#
# To run it in each of these various modes, follow the instructions
# in the readme for examples:
# https://github.com/huggingface/accelerate/tree/main/examples
#
########################################################################


lowerCAmelCase							:			Optional[int]     							=							16
lowerCAmelCase							:			Any     							=							32



def     a__ (				snake_case__       , snake_case__ = 16				)						->					List[str]:
			lowerCamelCase			      =					AutoTokenizer.from_pretrained("""bert-base-cased"""				)
			lowerCamelCase			      =					load_dataset("""glue"""       , """mrpc"""				)

			def tokenize_function(snake_case__				):
						# max_length=None => use the model max length (it's actually the default)
						lowerCamelCase			      =					tokenizer(examples["""sentence1"""]       , examples["""sentence2"""]       , truncation=snake_case__       , max_length=snake_case__				)
						return outputs

			# Apply the method we just defined to all the examples in all the splits of the dataset
			# starting with the main process first:
			with accelerator.main_process_first():
						lowerCamelCase			      =					datasets.map(
						    snake_case__       , batched=snake_case__       , remove_columns=["""idx""", """sentence1""", """sentence2"""]       , )

			# We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the
			# transformers library
			lowerCamelCase			      =					tokenized_datasets.rename_column("""label"""       , """labels"""				)

			def collate_fn(snake_case__				):
						# On TPU it's best to pad everything to the same length or training will be very slow.
						lowerCamelCase			      =					1_28 if accelerator.distributed_type == DistributedType.TPU else None
						# When using mixed precision we want round multiples of 8/16
						if accelerator.mixed_precision == "fp8":
									lowerCamelCase			      =					16
						elif accelerator.mixed_precision != "no":
									lowerCamelCase			      =					8
						else:
									lowerCamelCase			      =					None

						return tokenizer.pad(
						    snake_case__       , padding="""longest"""       , max_length=snake_case__       , pad_to_multiple_of=snake_case__       , return_tensors="""pt"""       , )

			# Instantiate dataloaders.
			lowerCamelCase			      =					DataLoader(
			    tokenized_datasets["""train"""]       , shuffle=snake_case__       , collate_fn=snake_case__       , batch_size=snake_case__				)
			lowerCamelCase			      =					DataLoader(
			    tokenized_datasets["""validation"""]       , shuffle=snake_case__       , collate_fn=snake_case__       , batch_size=snake_case__				)

			return train_dataloader, eval_dataloader


# For testing only
if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1":
					from accelerate.test_utils.training import mocked_dataloaders

					lowerCAmelCase							:			Tuple     							=							mocked_dataloaders  # noqa: F811



def     a__ (				snake_case__       , snake_case__				)						->					Dict:
			# For testing only
			if os.environ.get("""TESTING_MOCKED_DATALOADERS"""       , snake_case__				) == "1":
						lowerCamelCase			      =					2
			# Initialize accelerator
			lowerCamelCase			      =					Accelerator(cpu=args.cpu       , mixed_precision=args.mixed_precision				)
			# Sample hyper-parameters for learning rate, batch size, seed and a few other HPs
			lowerCamelCase			      =					config["""lr"""]
			lowerCamelCase			      =					int(config["""num_epochs"""]				)
			lowerCamelCase			      =					int(config["""seed"""]				)
			lowerCamelCase			      =					int(config["""batch_size"""]				)

			lowerCamelCase			      =					evaluate.load("""glue"""       , """mrpc"""				)

			# New Code #
			# We now can define an inner training loop function. It should take a batch size as the only parameter,
			# and build the dataloaders in there.
			# It also gets our decorator
			@find_executable_batch_size(starting_batch_size=snake_case__				)
			def inner_training_loop(snake_case__				):
						# And now just move everything below under this function
						# We need to bring in the Accelerator object from earlier
						nonlocal accelerator
						# And reset all of its attributes that could hold onto any memory:
						accelerator.free_memory()

						# Then we can declare the model, optimizer, and everything else:
						set_seed(snake_case__				)

						# Instantiate the model (we build the model here so that the seed also control new weights initialization)
						lowerCamelCase			      =					AutoModelForSequenceClassification.from_pretrained("""bert-base-cased"""       , return_dict=snake_case__				)

						# We could avoid this line since the accelerator is set with `device_placement=True` (default value).
						# Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer
						# creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that).
						lowerCamelCase			      =					model.to(accelerator.device				)

						# Instantiate optimizer
						lowerCamelCase			      =					AdamW(params=model.parameters()       , lr=snake_case__				)
						lowerCamelCase      ,   lowerCamelCase			      =					get_dataloaders(snake_case__       , snake_case__				)

						# Instantiate scheduler
						lowerCamelCase			      =					get_linear_schedule_with_warmup(
						    optimizer=snake_case__       , num_warmup_steps=1_00       , num_training_steps=(len(snake_case__				) * num_epochs)       , )

						# Prepare everything
						# There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the
						# prepare method.
						lowerCamelCase      ,   lowerCamelCase      ,   lowerCamelCase      ,   lowerCamelCase      ,   lowerCamelCase			      =					accelerator.prepare(
						    snake_case__       , snake_case__       , snake_case__       , snake_case__       , snake_case__				)

						# Now we train the model
						for epoch in range(snake_case__				):
									model.train()
									for step, batch in enumerate(snake_case__				):
												# We could avoid this line since we set the accelerator with `device_placement=True`.
												batch.to(accelerator.device				)
												lowerCamelCase			      =					model(**snake_case__				)
												lowerCamelCase			      =					outputs.loss
												accelerator.backward(snake_case__				)
												optimizer.step()
												lr_scheduler.step()
												optimizer.zero_grad()

									model.eval()
									for step, batch in enumerate(snake_case__				):
												# We could avoid this line since we set the accelerator with `device_placement=True`.
												batch.to(accelerator.device				)
												with torch.no_grad():
															lowerCamelCase			      =					model(**snake_case__				)
												lowerCamelCase			      =					outputs.logits.argmax(dim=-1				)
												lowerCamelCase      ,   lowerCamelCase			      =					accelerator.gather_for_metrics((predictions, batch["""labels"""])				)
												metric.add_batch(
												    predictions=snake_case__       , references=snake_case__       , )

									lowerCamelCase			      =					metric.compute()
									# Use accelerator.print to print only on the main process.
									accelerator.print(F'epoch {epoch}:'       , snake_case__				)

    # New Code #
    # And call it at the end with no arguments
    # Note: You could also refactor this outside of your training loop function
			inner_training_loop()



def     a__ (				)						->					List[str]:
			lowerCamelCase			      =					argparse.ArgumentParser(description="""Simple example of training script."""				)
			parser.add_argument(
			    """--mixed_precision"""       , type=snake_case__       , default=snake_case__       , choices=["""no""", """fp16""", """bf16""", """fp8"""]       , help="""Whether to use mixed precision. Choose"""
			    """between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10."""
			    """and an Nvidia Ampere GPU."""       , )
			parser.add_argument("""--cpu"""       , action="""store_true"""       , help="""If passed, will train on the CPU."""				)
			lowerCamelCase			      =					parser.parse_args()
			lowerCamelCase			      =					{"""lr""": 2E-5, """num_epochs""": 3, """seed""": 42, """batch_size""": 16}
			training_function(snake_case__       , snake_case__				)


if __name__ == "__main__":
					main()









"""simple docstring"""




import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
					from .tokenization_xlnet import XLNetTokenizer
else:
					lowerCAmelCase							:			int     							=							None


lowerCAmelCase							:			Tuple     							=							logging.get_logger(__name__)

lowerCAmelCase							:			Tuple     							=							{"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""}

lowerCAmelCase							:			Union[str, Any]     							=							{
    """vocab_file""": {
        """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model""",
        """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model""",
    },
    """tokenizer_file""": {
        """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json""",
        """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json""",
    },
}

lowerCAmelCase							:			Optional[int]     							=							{
    """xlnet-base-cased""": None,
    """xlnet-large-cased""": None,
}

lowerCAmelCase							:			Union[str, Any]     							=							"""▁"""

# Segments (not really needed)
lowerCAmelCase							:			str     							=							0
lowerCAmelCase							:			Optional[int]     							=							1
lowerCAmelCase							:			Tuple     							=							2
lowerCAmelCase							:			Optional[Any]     							=							3
lowerCAmelCase							:			List[Any]     							=							4






class     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''
				__UpperCamelCase			  =						VOCAB_FILES_NAMES
				__UpperCamelCase			  =						PRETRAINED_VOCAB_FILES_MAP
				__UpperCamelCase			  =						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				__UpperCamelCase			  =						"left"
				__UpperCamelCase			  =						XLNetTokenizer





				def __init__(       self		, _a=None		, _a=None		, _a=False		, _a=True		, _a=False		, _a="<s>"		, _a="</s>"		, _a="<unk>"		, _a="<sep>"		, _a="<pad>"		, _a="<cls>"		, _a="<mask>"		, _a=["<eop>", "<eod>"]		, **_a		, ):





							"""simple docstring"""
							# Mask token behave like a normal word, i.e. include the space before it
							lowerCamelCase			      =					AddedToken(_a		, lstrip=_a		, rstrip=_a ) if isinstance(_a		, _a ) else mask_token

							super().__init__(
							    vocab_file=_a		, tokenizer_file=_a		, do_lower_case=_a		, remove_space=_a		, keep_accents=_a		, bos_token=_a		, eos_token=_a		, unk_token=_a		, sep_token=_a		, pad_token=_a		, cls_token=_a		, mask_token=_a		, additional_special_tokens=_a		, **_a		, )

							lowerCamelCase			      =					3
							lowerCamelCase			      =					do_lower_case
							lowerCamelCase			      =					remove_space
							lowerCamelCase			      =					keep_accents
							lowerCamelCase			      =					vocab_file
							lowerCamelCase			      =					False if not self.vocab_file else True





				def    _lowerCAmelCase				(       self		, _a		, _a = None ):





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





				def    _lowerCAmelCase				(       self		, _a		, _a = None ):





							"""simple docstring"""
							lowerCamelCase			      =					[self.sep_token_id]
							lowerCamelCase			      =					[2]

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





				def    _lowerCAmelCase				(       self		, _a		, _a = None ):





							"""simple docstring"""
							if not self.can_save_slow_tokenizer:
										raise ValueError(
										    """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """
										    """tokenizer.""" )

							if not os.path.isdir(_a ):
										logger.error(f'Vocabulary path ({save_directory}) should be a directory' )
										return
							lowerCamelCase			      =					os.path.join(
							    _a		, (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] )

							if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ):
										copyfile(self.vocab_file		, _a )

							return (out_vocab_file,)



