



from ..utils import DummyObject, requires_backends

class        _A			(      metaclass=snake_case_       ):
       UpperCamelCase__    :       Optional[Any]		   =		['torch', 'transformers', 'k_diffusion']



       def __init__(						self				:	Any	,						*__SCREAMING_SNAKE_CASE				:	str	,						**__SCREAMING_SNAKE_CASE				:	Any):


           '''simple docstring'''

           requires_backends(self	,						['''torch''', '''transformers''', '''k_diffusion'''])



       @classmethod
       def 						_lowerCamelCase     (						cls				:	Union[str, Any]	,						*__SCREAMING_SNAKE_CASE				:	int	,						**__SCREAMING_SNAKE_CASE				:	Tuple):


           '''simple docstring'''

           requires_backends(cls	,						['''torch''', '''transformers''', '''k_diffusion'''])





       @classmethod
       def 						_lowerCamelCase     (						cls				:	int	,						*__SCREAMING_SNAKE_CASE				:	str	,						**__SCREAMING_SNAKE_CASE				:	List[str]):


           '''simple docstring'''

           requires_backends(cls	,						['''torch''', '''transformers''', '''k_diffusion'''])













"""simple docstring"""


import argparse
import logging

import datasets
from tokenizers import Tokenizer, decoders, normalizers, pre_tokenizers, processors
from tokenizers.models import Unigram
from tokenizers.trainers import UnigramTrainer

from transformers import AlbertTokenizerFast


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




def 					a			(  )	->		Union[str, Any]:
 __A						:							int      =						argparse.ArgumentParser(description='Train a unigram tokenizer on the wikitext dataset.'		)
 parser.add_argument(
     '--dataset_name'  ,						type=UpperCamelCase_  ,						default='wikitext'  ,						help='Name of the training. Explore datasets at: hf.co/datasets.'  ,						)
 parser.add_argument(
     '--dataset_config'  ,						type=UpperCamelCase_  ,						default='wikitext-103-raw-v1'  ,						help='Configuration name of the dataset.'		)
 parser.add_argument(
     '--batch_size'  ,						type=UpperCamelCase_  ,						default=1_0_0_0  ,						help='Batch size during training.'  ,						)
 parser.add_argument(
     '--vocab_size'  ,						type=UpperCamelCase_  ,						default=1_0_0_4_8  ,						help='Size of the desired vocabulary.'  ,						)
 parser.add_argument(
     '--limit'  ,						default=UpperCamelCase_  ,						type=UpperCamelCase_  ,						help='Limit the number of shards (used for debugging).'  ,						)
 parser.add_argument(
     '--export_to_hub'  ,						action='store_true'  ,						)

 __A						:							List[Any]      =						parser.parse_args()
 return args




def 					a			(  UpperCamelCase_				:					Optional[Any]		)	->		Optional[Any]:
 __A						:							Union[str, Any]      =						datasets.load_dataset(args.dataset_name  ,						args.dataset_config  ,						split='train'		)

 if args.limit is not None:
  __A						:							Tuple      =						min(len(UpperCamelCase_		)  ,						args.limit		)
  __A						:							Optional[int]      =						dataset.select(range(UpperCamelCase_		)		)
  logger.info(F"Limiting the dataset to {args.limit} entries."		)

 def batch_iterator():
  for i in range(0  ,						len(UpperCamelCase_		)  ,						args.batch_size		):
   yield dataset[i : i + args.batch_size]["text"]

    # Prepare the tokenizer.
 __A						:							Any      =						Tokenizer(Unigram()		)
 __A						:							List[Any]      =						normalizers.Sequence([normalizers.Replace('``'  ,						'"'		), normalizers.Replace('\'\''  ,						'"'		)]		)
 __A						:							Optional[int]      =						pre_tokenizers.Metaspace()

 # Prepare the trainer.
 __A						:							int      =						UnigramTrainer(
     unk_token='<unk>'  ,						special_tokens=['[CLS]', '[SEP]', '<unk>', '<pad>', '[MASK]']  ,						vocab_size=args.vocab_size  ,						)

 logger.info('Training the tokenizer.'		)
 tokenizer.train_from_iterator(batch_iterator()  ,						trainer=UpperCamelCase_		)
 logger.info('Tokenizer training complete!'		)

 __A						:							Union[str, Any]      =						tokenizer.token_to_id('[CLS]'		)
 __A						:							str      =						tokenizer.token_to_id('[SEP]'		)
 __A						:							Tuple      =						processors.TemplateProcessing(
     single='[CLS]:0 $A:0 [SEP]:0'  ,						pair='[CLS]:0 $A:0 [SEP]:0 $B:1 [SEP]:1'  ,						special_tokens=[
         ('[CLS]', cls_token_id),
         ('[SEP]', sep_token_id),
     ]  ,						)
 __A						:							List[str]      =						decoders.Metaspace()

 if args.export_to_hub:
  logger.info('Exporting the trained tokenzier to Hub.'		)
  __A						:							Union[str, Any]      =						AlbertTokenizerFast(tokenizer_object=UpperCamelCase_		)
  new_tokenizer.push_to_hub('unigram-tokenizer-dataset'		)


if __name__ == "__main__":
   _UpperCamelCase :    Optional[Any]      =			parse_args()
   main(args)





