
'''simple docstring'''


import warnings
from dataclasses import dataclass, field
from typing import Optional, Tuple

from .training_args import TrainingArguments
from .utils import cached_property, is_tf_available, logging, requires_backends


__SCREAMING_SNAKE_CASE						=      logging.get_logger(__name__)

if is_tf_available():
	import tensorflow as tf




@dataclass
class     __snake_case			(    _a	):
		a_          =					"tf"
		a_          =					field(
		    default=_a					,			metadata={"help": "Name of TPU"}					,			)

		a_          =					field(
		    default=_a					,			metadata={"help": "Zone of TPU"}					,			)

		a_          =					field(
		    default=_a					,			metadata={"help": "Name of Cloud TPU-enabled project"}					,			)

		a_          =					field(
		    default=1.0					,			metadata={"help": "Power for the Polynomial decay LR scheduler."}					,			)

		a_          =					field(default=_a					,			metadata={"help": "Whether to activate the XLA compilation or not"}	)




		@cached_property
		def 	_snake_case	(  self					:str)					->			Tuple["tf.distribute.Strategy", int]:






								'''simple docstring'''






								requires_backends(self			,    ["""tf"""])
								logger.info("""Tensorflow: setting up strategy""")

								UpperCamelCase_     :	Tuple					     =     tf.config.list_physical_devices("""GPU""")

								# Set to float16 at first
								if self.fpaa:
														tf.keras.mixed_precision.set_global_policy("""mixed_float16""")

								if self.no_cuda:
														UpperCamelCase_     :	Tuple					     =     tf.distribute.OneDeviceStrategy(device="""/cpu:0""")
								else:
														try:
																				if self.tpu_name:
																										UpperCamelCase_     :	int					     =     tf.distribute.cluster_resolver.TPUClusterResolver(
																										    self.tpu_name			,    zone=self.tpu_zone			,    project=self.gcp_project)
																				else:
																										UpperCamelCase_     :	int					     =     tf.distribute.cluster_resolver.TPUClusterResolver()
														except ValueError:
																				if self.tpu_name:
																										raise RuntimeError(F"""Couldn't connect to TPU {self.tpu_name}!""")
																				else:
																										UpperCamelCase_     :	Any					     =     None

														if tpu:
																				# Set to bfloat16 in case of TPU
																				if self.fpaa:
																										tf.keras.mixed_precision.set_global_policy("""mixed_bfloat16""")

																				tf.config.experimental_connect_to_cluster(_SCREAMING_SNAKE_CASE)
																				tf.tpu.experimental.initialize_tpu_system(_SCREAMING_SNAKE_CASE)

																				UpperCamelCase_     :	Dict					     =     tf.distribute.TPUStrategy(_SCREAMING_SNAKE_CASE)

														elif len(_SCREAMING_SNAKE_CASE) == 0:
																				UpperCamelCase_     :	List[str]					     =     tf.distribute.OneDeviceStrategy(device="""/cpu:0""")
														elif len(_SCREAMING_SNAKE_CASE) == 1:
																				UpperCamelCase_     :	List[Any]					     =     tf.distribute.OneDeviceStrategy(device="""/gpu:0""")
														elif len(_SCREAMING_SNAKE_CASE) > 1:
																				# If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
																				UpperCamelCase_     :	Optional[Any]					     =     tf.distribute.MirroredStrategy()
														else:
																				raise ValueError("""Cannot find the proper strategy, please check your environment properties.""")

								return strategy




		@property
		def 	_snake_case	(  self					:str)					->			"tf.distribute.Strategy":






								'''simple docstring'''






								requires_backends(self			,    ["""tf"""])
								return self._setup_strategy




		@property
		def 	_snake_case	(  self					:Any)					->			int:






								'''simple docstring'''






								requires_backends(self			,    ["""tf"""])
								return self._setup_strategy.num_replicas_in_sync




		@property
		def 	_snake_case	(  self					:Dict)					->			List[Any]:






								'''simple docstring'''






								return False  # TF Logging is handled by Keras not the Trainer




		@property
		def 	_snake_case	(  self					:Tuple)					->			int:






								'''simple docstring'''






								if self.per_gpu_train_batch_size:
														logger.warning(
														    """Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future """
														    """version. Using `--per_device_train_batch_size` is preferred.""")
								UpperCamelCase_     :	Union[str, Any]					     =     self.per_gpu_train_batch_size or self.per_device_train_batch_size
								return per_device_batch_size * self.n_replicas




		@property
		def 	_snake_case	(  self					:Dict)					->			int:






								'''simple docstring'''






								if self.per_gpu_eval_batch_size:
														logger.warning(
														    """Using deprecated `--per_gpu_eval_batch_size` argument which will be removed in a future """
														    """version. Using `--per_device_eval_batch_size` is preferred.""")
								UpperCamelCase_     :	List[str]					     =     self.per_gpu_eval_batch_size or self.per_device_eval_batch_size
								return per_device_batch_size * self.n_replicas





		@property
		def 	_snake_case	(  self					:Any)					->			int:






								'''simple docstring'''






								requires_backends(self			,    ["""tf"""])
								warnings.warn(
								    """The n_gpu argument is deprecated and will be removed in a future version, use n_replicas instead."""			,    _SCREAMING_SNAKE_CASE			,    )
								return self._setup_strategy.num_replicas_in_sync






'''simple docstring'''


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

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


if is_sentencepiece_available():
	from .tokenization_xglm import XGLMTokenizer
else:
	__SCREAMING_SNAKE_CASE						=      None


__SCREAMING_SNAKE_CASE						=      logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE						=      {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"}

__SCREAMING_SNAKE_CASE						=      {
    "vocab_file": {
        "facebook/xglm-564M": "https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model",
    },
    "tokenizer_file": {
        "facebook/xglm-564M": "https://huggingface.co/facebook/xglm-564M/resolve/main/tokenizer.json",
    },
}

__SCREAMING_SNAKE_CASE						=      {
    "facebook/xglm-564M": 2048,
}




class     __snake_case			(    _a	):
		a_          =					VOCAB_FILES_NAMES
		a_          =					PRETRAINED_VOCAB_FILES_MAP
		a_          =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		a_          =					["input_ids", "attention_mask"]
		a_          =					XGLMTokenizer




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






								'''simple docstring'''






								UpperCamelCase_     :	Tuple					     =     7
								UpperCamelCase_     :	int					     =     [F"""<madeupword{i}>""" for i in range(self.num_madeup_words)]

								UpperCamelCase_     :	Optional[Any]					     =     kwargs.get("""additional_special_tokens"""			,    [])
								kwargs["additional_special_tokens"] += [
								    word for word in madeup_words if word not in kwargs["additional_special_tokens"]
								]

								super().__init__(
								    _SCREAMING_SNAKE_CASE			,    tokenizer_file=_SCREAMING_SNAKE_CASE			,    bos_token=_SCREAMING_SNAKE_CASE			,    eos_token=_SCREAMING_SNAKE_CASE			,    sep_token=_SCREAMING_SNAKE_CASE			,    cls_token=_SCREAMING_SNAKE_CASE			,    unk_token=_SCREAMING_SNAKE_CASE			,    pad_token=_SCREAMING_SNAKE_CASE			,    **_SCREAMING_SNAKE_CASE			,    )

								UpperCamelCase_     :	int					     =     vocab_file
								UpperCamelCase_     :	Any					     =     False if not self.vocab_file else True




		def 	_snake_case	(  self					:Dict			,    _SCREAMING_SNAKE_CASE					:List[int]			,    _SCREAMING_SNAKE_CASE					:Optional[List[int]] = None)					->			List[int]:






								'''simple docstring'''






								if token_ids_a is None:
														return [self.sep_token_id] + token_ids_a
								UpperCamelCase_     :	str					     =     [self.sep_token_id]
								return sep + token_ids_a + sep + sep + token_ids_a




		def 	_snake_case	(  self					:int			,    _SCREAMING_SNAKE_CASE					:List[int]			,    _SCREAMING_SNAKE_CASE					:Optional[List[int]] = None)					->			List[int]:






								'''simple docstring'''






								UpperCamelCase_     :	Optional[Any]					     =     [self.sep_token_id]

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





		def 	_snake_case	(  self					:List[Any]			,    _SCREAMING_SNAKE_CASE					:str			,    _SCREAMING_SNAKE_CASE					:Optional[str] = None)					->			Tuple[str]:






								'''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(_SCREAMING_SNAKE_CASE):
														logger.error(F"""Vocabulary path ({save_directory}) should be a directory.""")
														return
								UpperCamelCase_     :	Dict					     =     os.path.join(
								    _SCREAMING_SNAKE_CASE			,    (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""])

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

								return (out_vocab_file,)




