




import functools
import operator

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


__SCREAMING_SNAKE_CASE 						=logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE 						={
    """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""",
    # See all SEW-D models at https://huggingface.co/models?filter=sew-d
}






class  __magic_name__						(  __UpperCAmelCase):
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							Optional[int]	      =					"sew-d"


				def __init__(							self:   Union[str, Any]		,					_lowerCamelCase:   Dict=32		,					_lowerCamelCase:   List[Any]=7_68		,					_lowerCamelCase:   List[Any]=12		,					_lowerCamelCase:   Any=12		,					_lowerCamelCase:   Tuple=30_72		,					_lowerCamelCase:   Optional[int]=2		,					_lowerCamelCase:   Optional[Any]=5_12		,					_lowerCamelCase:   Tuple=2_56		,					_lowerCamelCase:   str=True		,					_lowerCamelCase:   List[str]=True		,					_lowerCamelCase:   Union[str, Any]=("p2c", "c2p")		,					_lowerCamelCase:   Dict="layer_norm"		,					_lowerCamelCase:   Optional[int]="gelu_python"		,					_lowerCamelCase:   List[Any]=0.1		,					_lowerCamelCase:   Any=0.1		,					_lowerCamelCase:   Optional[int]=0.1		,					_lowerCamelCase:   Union[str, Any]=0.0		,					_lowerCamelCase:   Optional[int]=0.1		,					_lowerCamelCase:   Union[str, Any]=0.02		,					_lowerCamelCase:   List[Any]=1E-7		,					_lowerCamelCase:   Dict=1E-5		,					_lowerCamelCase:   Any="group"		,					_lowerCamelCase:   List[str]="gelu"		,					_lowerCamelCase:   Optional[int]=(64, 1_28, 1_28, 1_28, 1_28, 2_56, 2_56, 2_56, 2_56, 5_12, 5_12, 5_12, 5_12)		,					_lowerCamelCase:   List[str]=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)		,					_lowerCamelCase:   int=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)		,					_lowerCamelCase:   str=False		,					_lowerCamelCase:   Dict=1_28		,					_lowerCamelCase:   Optional[Any]=16		,					_lowerCamelCase:   Optional[int]=True		,					_lowerCamelCase:   str=0.05		,					_lowerCamelCase:   Any=10		,					_lowerCamelCase:   Dict=2		,					_lowerCamelCase:   Optional[Any]=0.0		,					_lowerCamelCase:   str=10		,					_lowerCamelCase:   Union[str, Any]=0		,					_lowerCamelCase:   Any="mean"		,					_lowerCamelCase:   Union[str, Any]=False		,					_lowerCamelCase:   Dict=False		,					_lowerCamelCase:   List[str]=2_56		,					_lowerCamelCase:   int=0		,					_lowerCamelCase:   Optional[Any]=1		,					_lowerCamelCase:   int=2		,					**_lowerCamelCase:   Tuple		,					):
											super().__init__(**_lowerCamelCase		,					pad_token_id=_lowerCamelCase		,					bos_token_id=_lowerCamelCase		,					eos_token_id=_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    hidden_size
											SCREAMING_SNAKE_CASE_   	=    feat_extract_norm
											SCREAMING_SNAKE_CASE_   	=    feat_extract_activation
											SCREAMING_SNAKE_CASE_   	=    list(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    list(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    list(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    conv_bias
											SCREAMING_SNAKE_CASE_   	=    num_conv_pos_embeddings
											SCREAMING_SNAKE_CASE_   	=    num_conv_pos_embedding_groups
											SCREAMING_SNAKE_CASE_   	=    len(self.conv_dim		)
											SCREAMING_SNAKE_CASE_   	=    num_hidden_layers
											SCREAMING_SNAKE_CASE_   	=    intermediate_size
											SCREAMING_SNAKE_CASE_   	=    squeeze_factor
											SCREAMING_SNAKE_CASE_   	=    max_position_embeddings
											SCREAMING_SNAKE_CASE_   	=    position_buckets
											SCREAMING_SNAKE_CASE_   	=    share_att_key
											SCREAMING_SNAKE_CASE_   	=    relative_attention
											SCREAMING_SNAKE_CASE_   	=    norm_rel_ebd
											SCREAMING_SNAKE_CASE_   	=    list(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    hidden_act
											SCREAMING_SNAKE_CASE_   	=    num_attention_heads
											SCREAMING_SNAKE_CASE_   	=    hidden_dropout
											SCREAMING_SNAKE_CASE_   	=    attention_dropout
											SCREAMING_SNAKE_CASE_   	=    activation_dropout
											SCREAMING_SNAKE_CASE_   	=    feat_proj_dropout
											SCREAMING_SNAKE_CASE_   	=    final_dropout
											SCREAMING_SNAKE_CASE_   	=    layer_norm_eps
											SCREAMING_SNAKE_CASE_   	=    feature_layer_norm_eps
											SCREAMING_SNAKE_CASE_   	=    initializer_range
											SCREAMING_SNAKE_CASE_   	=    vocab_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)`,'''
																		    f"but is `len(config.conv_dim) = {len(self.conv_dim		)}`, `len(config.conv_stride)"
																		    f"= {len(self.conv_stride		)}`, `len(config.conv_kernel) = {len(self.conv_kernel		)}`."		)

											# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
											SCREAMING_SNAKE_CASE_   	=    apply_spec_augment
											SCREAMING_SNAKE_CASE_   	=    mask_time_prob
											SCREAMING_SNAKE_CASE_   	=    mask_time_length
											SCREAMING_SNAKE_CASE_   	=    mask_time_min_masks
											SCREAMING_SNAKE_CASE_   	=    mask_feature_prob
											SCREAMING_SNAKE_CASE_   	=    mask_feature_length
											SCREAMING_SNAKE_CASE_   	=    mask_feature_min_masks

											# ctc loss
											SCREAMING_SNAKE_CASE_   	=    ctc_loss_reduction
											SCREAMING_SNAKE_CASE_   	=    ctc_zero_infinity

											# sequence classification
											SCREAMING_SNAKE_CASE_   	=    use_weighted_layer_sum
											SCREAMING_SNAKE_CASE_   	=    classifier_proj_size





				@property
				def     _A							(							self:   Optional[Any]		):
											return functools.reduce(operator.mul		,					self.conv_stride		,					1		)








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


__SCREAMING_SNAKE_CASE 						=logging.get_logger(__name__)


__SCREAMING_SNAKE_CASE 						="""▁"""

__SCREAMING_SNAKE_CASE 						={"""vocab_file""": """spiece.model"""}

__SCREAMING_SNAKE_CASE 						={
    """vocab_file""": {
        """google/reformer-crime-and-punishment""": (
            """https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model"""
        )
    }
}

__SCREAMING_SNAKE_CASE 						={
    """google/reformer-crime-and-punishment""": 524_288,
}






class  __magic_name__						(  __UpperCAmelCase):
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							Any	      =					VOCAB_FILES_NAMES
				SCREAMING_SNAKE_CASE__					:							Union[str, Any]	      =					PRETRAINED_VOCAB_FILES_MAP
				SCREAMING_SNAKE_CASE__					:							Optional[int]	      =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				SCREAMING_SNAKE_CASE__					:							Union[str, Any]	      =					["input_ids", "attention_mask"]


				def __init__(							self:   List[Any]		,					_lowerCamelCase:   List[Any]		,					_lowerCamelCase:   List[Any]="</s>"		,					_lowerCamelCase:   str="<unk>"		,					_lowerCamelCase:   Union[str, Any]=[]		,					_lowerCamelCase:   Optional[Dict[str, Any]] = None		,					**_lowerCamelCase:   List[str]		,					):
											SCREAMING_SNAKE_CASE_   	=    {} if sp_model_kwargs is None else sp_model_kwargs

											super().__init__(
											    eos_token=_lowerCamelCase		,					unk_token=_lowerCamelCase		,					additional_special_tokens=_lowerCamelCase		,					sp_model_kwargs=self.sp_model_kwargs		,					**_lowerCamelCase		,					)

											SCREAMING_SNAKE_CASE_   	=    vocab_file
											SCREAMING_SNAKE_CASE_   	=    spm.SentencePieceProcessor(**self.sp_model_kwargs		)
											self.sp_model.Load(_lowerCamelCase		)


				@property
				def     _A							(							self:   Optional[int]		):
											return self.sp_model.get_piece_size()


				def     _A							(							self:   int		):
											SCREAMING_SNAKE_CASE_   	=    {self.convert_ids_to_tokens(_lowerCamelCase		): i for i in range(self.vocab_size		)}
											vocab.update(self.added_tokens_encoder		)
											return vocab


				def __getstate__(							self:   Tuple		):
											SCREAMING_SNAKE_CASE_   	=    self.__dict__.copy()
											SCREAMING_SNAKE_CASE_   	=    None
											return state


				def __setstate__(							self:   Dict		,					_lowerCamelCase:   Union[str, Any]		):
											SCREAMING_SNAKE_CASE_   	=    d

											# for backward compatibility
											if not hasattr(self		,					'''sp_model_kwargs'''		):
																		SCREAMING_SNAKE_CASE_   	=    {}

											SCREAMING_SNAKE_CASE_   	=    spm.SentencePieceProcessor(**self.sp_model_kwargs		)
											self.sp_model.Load(self.vocab_file		)


				def     _A							(							self:   Optional[Any]		,					_lowerCamelCase:   str		):
											return self.sp_model.encode(_lowerCamelCase		,					out_type=_lowerCamelCase		)


				def     _A							(							self:   int		,					_lowerCamelCase:   Optional[int]		):
											return self.sp_model.piece_to_id(_lowerCamelCase		)


				def     _A							(							self:   Union[str, Any]		,					_lowerCamelCase:   Tuple		):
											if index < self.sp_model.get_piece_size():
																		SCREAMING_SNAKE_CASE_   	=    self.sp_model.IdToPiece(_lowerCamelCase		)
											return token


				def     _A							(							self:   List[str]		,					_lowerCamelCase:   int		):
											SCREAMING_SNAKE_CASE_   	=    []
											SCREAMING_SNAKE_CASE_   	=    ''''''
											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(_lowerCamelCase		) + token
																									SCREAMING_SNAKE_CASE_   	=    []
																		else:
																									current_sub_tokens.append(_lowerCamelCase		)
											out_string += self.sp_model.decode(_lowerCamelCase		)
											return out_string.strip()





				def     _A							(							self:   Optional[int]		,					_lowerCamelCase:   str		,					_lowerCamelCase:   Optional[str] = None		):
											if not os.path.isdir(_lowerCamelCase		):
																		logger.error(f"Vocabulary path ({save_directory}) should be a directory"		)
																		return
											SCREAMING_SNAKE_CASE_   	=    os.path.join(
											    _lowerCamelCase		,					(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']		)

											if os.path.abspath(self.vocab_file		) != os.path.abspath(_lowerCamelCase		) and os.path.isfile(self.vocab_file		):
																		copyfile(self.vocab_file		,					_lowerCamelCase		)
											elif not os.path.isfile(self.vocab_file		):
																		with open(_lowerCamelCase		,					'''wb'''		) as fi:
																									SCREAMING_SNAKE_CASE_   	=    self.sp_model.serialized_model_proto()
																									fi.write(_lowerCamelCase		)

											return (out_vocab_file,)


