
"""simple docstring"""




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

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


UpperCAmelCase__   :					List[str]												=				logging.get_logger(__name__)

UpperCAmelCase__   :					int												=				{'vocab_file': 'spiece.model'}

UpperCAmelCase__   :					List[str]												=				{
    'vocab_file': {
        'bert_for_seq_generation': (
            'https://huggingface.co/google/bert_for_seq_generation_L-24_bbc_encoder/resolve/main/spiece.model'
        ),
    }
}

UpperCAmelCase__   :					str												=				{'bert_for_seq_generation': 5_1_2}



class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		Any       =     VOCAB_FILES_NAMES
				__UpperCamelCase   :		Union[str, Any]       =     PRETRAINED_VOCAB_FILES_MAP
				__UpperCamelCase   :		Union[str, Any]       =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				__UpperCamelCase   :		List[int]       =     []
				__UpperCamelCase   :		int       =     ['''input_ids''', '''attention_mask''']



				def __init__(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__="<s>"     , SCREAMING_SNAKE_CASE__="</s>"     , SCREAMING_SNAKE_CASE__="<unk>"     , SCREAMING_SNAKE_CASE__="<pad>"     , SCREAMING_SNAKE_CASE__="<::::>"     , SCREAMING_SNAKE_CASE__ = None     , **SCREAMING_SNAKE_CASE__     , )      ->	None:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     {} if sp_model_kwargs is None else sp_model_kwargs

											# Add extra_ids to the special token list
											super().__init__(
											    bos_token=SCREAMING_SNAKE_CASE__     , eos_token=SCREAMING_SNAKE_CASE__     , unk_token=SCREAMING_SNAKE_CASE__     , pad_token=SCREAMING_SNAKE_CASE__     , sep_token=SCREAMING_SNAKE_CASE__     , sp_model_kwargs=self.sp_model_kwargs     , **SCREAMING_SNAKE_CASE__     , )

											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     vocab_file

											SCREAMING_SNAKE_CASE__		: List[str]     =     spm.SentencePieceProcessor(**self.sp_model_kwargs					)
											self.sp_model.Load(SCREAMING_SNAKE_CASE__					)



				@property
				def 			__magic_name__					(self					)      ->	str:



											"""simple docstring"""




											return self.sp_model.get_piece_size()



				def 			__magic_name__					(self					)      ->	Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     {self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE__					): i for i in range(self.vocab_size					)}
											vocab.update(self.added_tokens_encoder					)
											return vocab



				def __getstate__(self					)      ->	Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     self.__dict__.copy()
											SCREAMING_SNAKE_CASE__		: List[Any]     =     None
											return state



				def __setstate__(self     , SCREAMING_SNAKE_CASE__					)      ->	Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Dict     =     d

											# for backward compatibility
											if not hasattr(self     , """sp_model_kwargs"""					):
																		SCREAMING_SNAKE_CASE__		: List[Any]     =     {}

											SCREAMING_SNAKE_CASE__		: Optional[int]     =     spm.SentencePieceProcessor(**self.sp_model_kwargs					)
											self.sp_model.Load(self.vocab_file					)



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__					)      ->	List[str]:



											"""simple docstring"""




											return self.sp_model.encode(SCREAMING_SNAKE_CASE__     , out_type=SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__					)      ->	Optional[int]:



											"""simple docstring"""




											return self.sp_model.piece_to_id(SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__					)      ->	List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     self.sp_model.IdToPiece(SCREAMING_SNAKE_CASE__					)
											return token



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__					)      ->	Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     []
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     """"""
											for token in tokens:
																		# make sure that special tokens are not decoded using sentencepiece model
																		if token in self.all_special_tokens:
																									out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE__					) + token
																									SCREAMING_SNAKE_CASE__		: Tuple     =     []
																		else:
																									current_sub_tokens.append(SCREAMING_SNAKE_CASE__					)
											out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE__					)
											return out_string.strip()



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__ = None					)      ->	Tuple[str]:



											"""simple docstring"""




											if not os.path.isdir(SCREAMING_SNAKE_CASE__					):
																		logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''					)
																		return
											SCREAMING_SNAKE_CASE__		: List[str]     =     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__					) and os.path.isfile(self.vocab_file					):
																		copyfile(self.vocab_file     , SCREAMING_SNAKE_CASE__					)
											elif not os.path.isfile(self.vocab_file					):
																		with open(SCREAMING_SNAKE_CASE__     , """wb"""					) as fi:
																									SCREAMING_SNAKE_CASE__		: Dict     =     self.sp_model.serialized_model_proto()
																									fi.write(SCREAMING_SNAKE_CASE__					)

											return (out_vocab_file,)






"""simple docstring"""




import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase__   :					str												=				logging.get_logger(__name__)

UpperCAmelCase__   :					Dict												=				{
    'microsoft/unispeech-large-1500h-cv': (
        'https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json'
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}



class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		List[str]       =     '''unispeech'''



				def __init__(self     , SCREAMING_SNAKE_CASE__=32     , SCREAMING_SNAKE_CASE__=7_68     , SCREAMING_SNAKE_CASE__=12     , SCREAMING_SNAKE_CASE__=12     , SCREAMING_SNAKE_CASE__=30_72     , SCREAMING_SNAKE_CASE__="gelu"     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.02     , SCREAMING_SNAKE_CASE__=1E-5     , SCREAMING_SNAKE_CASE__="group"     , SCREAMING_SNAKE_CASE__="gelu"     , SCREAMING_SNAKE_CASE__=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)     , SCREAMING_SNAKE_CASE__=(5, 2, 2, 2, 2, 2, 2)     , SCREAMING_SNAKE_CASE__=(10, 3, 3, 3, 3, 2, 2)     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=1_28     , SCREAMING_SNAKE_CASE__=16     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=0.05     , SCREAMING_SNAKE_CASE__=10     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=10     , SCREAMING_SNAKE_CASE__=0     , SCREAMING_SNAKE_CASE__=3_20     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=1_00     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__="mean"     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=80     , SCREAMING_SNAKE_CASE__=0     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.5     , **SCREAMING_SNAKE_CASE__     , )      ->	List[Any]:



											"""simple docstring"""




											super().__init__(**SCREAMING_SNAKE_CASE__     , pad_token_id=SCREAMING_SNAKE_CASE__     , bos_token_id=SCREAMING_SNAKE_CASE__     , eos_token_id=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     hidden_size
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     feat_extract_norm
											SCREAMING_SNAKE_CASE__		: Any     =     feat_extract_activation
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     list(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: str     =     list(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Tuple     =     list(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     conv_bias
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     num_conv_pos_embeddings
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     num_conv_pos_embedding_groups
											SCREAMING_SNAKE_CASE__		: Dict     =     len(self.conv_dim					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     num_hidden_layers
											SCREAMING_SNAKE_CASE__		: str     =     intermediate_size
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     hidden_act
											SCREAMING_SNAKE_CASE__		: Any     =     num_attention_heads
											SCREAMING_SNAKE_CASE__		: Dict     =     hidden_dropout
											SCREAMING_SNAKE_CASE__		: List[str]     =     attention_dropout
											SCREAMING_SNAKE_CASE__		: Any     =     activation_dropout
											SCREAMING_SNAKE_CASE__		: int     =     feat_proj_dropout
											SCREAMING_SNAKE_CASE__		: List[Any]     =     final_dropout
											SCREAMING_SNAKE_CASE__		: str     =     layerdrop
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     layer_norm_eps
											SCREAMING_SNAKE_CASE__		: Dict     =     initializer_range
											SCREAMING_SNAKE_CASE__		: Dict     =     num_ctc_classes
											SCREAMING_SNAKE_CASE__		: List[Any]     =     vocab_size
											SCREAMING_SNAKE_CASE__		: str     =     do_stable_layer_norm
											SCREAMING_SNAKE_CASE__		: Dict     =     use_weighted_layer_sum
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     classifier_proj_size

											if (
											    (len(self.conv_stride					) != self.num_feat_extract_layers)
											    or (len(self.conv_kernel					) != self.num_feat_extract_layers)
											    or (len(self.conv_dim					) != self.num_feat_extract_layers)
											):
																		raise ValueError(
																		    """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
																		    """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
																		    F''' {len(self.conv_dim					)}`, `len(config.conv_stride) = {len(self.conv_stride					)}`,'''
																		    F''' `len(config.conv_kernel) = {len(self.conv_kernel					)}`.'''					)

											# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
											SCREAMING_SNAKE_CASE__		: Tuple     =     apply_spec_augment
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     mask_time_prob
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     mask_time_length
											SCREAMING_SNAKE_CASE__		: List[str]     =     mask_time_min_masks
											SCREAMING_SNAKE_CASE__		: Tuple     =     mask_feature_prob
											SCREAMING_SNAKE_CASE__		: Any     =     mask_feature_length
											SCREAMING_SNAKE_CASE__		: int     =     mask_feature_min_masks

											# parameters for pretraining with codevector quantized representations
											SCREAMING_SNAKE_CASE__		: Dict     =     num_codevectors_per_group
											SCREAMING_SNAKE_CASE__		: Any     =     num_codevector_groups
											SCREAMING_SNAKE_CASE__		: str     =     contrastive_logits_temperature
											SCREAMING_SNAKE_CASE__		: str     =     feat_quantizer_dropout
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     num_negatives
											SCREAMING_SNAKE_CASE__		: Tuple     =     codevector_dim
											SCREAMING_SNAKE_CASE__		: List[Any]     =     proj_codevector_dim
											SCREAMING_SNAKE_CASE__		: Tuple     =     diversity_loss_weight

											# ctc loss
											SCREAMING_SNAKE_CASE__		: int     =     ctc_loss_reduction
											SCREAMING_SNAKE_CASE__		: str     =     ctc_zero_infinity

											# pretraining loss
											SCREAMING_SNAKE_CASE__		: List[str]     =     replace_prob



				@property
				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




											return functools.reduce(operator.mul     , self.conv_stride     , 1					)




