
"""simple docstring"""


import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD







class 				lowerCAmelCase_				(      nn.Module    ):



						'''simple docstring'''


						_lowerCamelCase:    int
						_lowerCamelCase:    int
						_lowerCamelCase:    float      =    0.0
						_lowerCamelCase:    int      =    1
						_lowerCamelCase:    int      =    1
						_lowerCamelCase:    bool      =    True
						_lowerCamelCase:    bool      =    False
						_lowerCamelCase:    bool      =    False
						_lowerCamelCase:    bool      =    False
						_lowerCamelCase:    jnp.dtype      =    jnp.floataa


						def 						_SCREAMING_SNAKE_CASE      ( self     :      int      )     ->   Any:
							A						   =   []
							A						   =   []

							for i in range(self.num_layers      ):
								A						   =   self.in_channels if i == 0 else self.out_channels

								A						   =   FlaxResnetBlockaD(
								    in_channels=A_  ,out_channels=self.out_channels  ,dropout_prob=self.dropout  ,dtype=self.dtype  ,)
								resnets.append(A_      )

								A						   =   FlaxTransformeraDModel(
								    in_channels=self.out_channels  ,n_heads=self.num_attention_heads  ,d_head=self.out_channels // self.num_attention_heads  ,depth=1  ,use_linear_projection=self.use_linear_projection  ,only_cross_attention=self.only_cross_attention  ,use_memory_efficient_attention=self.use_memory_efficient_attention  ,dtype=self.dtype  ,)
								attentions.append(A_      )

							A						   =   resnets
							A						   =   attentions

							if self.add_downsample:
								A						   =   FlaxDownsampleaD(self.out_channels  ,dtype=self.dtype      )




						def __call__( self     :      int  ,A_     :      Optional[int]  ,A_     :      Any  ,A_     :      int  ,A_     :      Dict=True      )     ->   str:
							A						   =   ()

							for resnet, attn in zip(self.resnets  ,self.attentions      ):
								A						   =   resnet(A_  ,A_  ,deterministic=A_      )
								A						   =   attn(A_  ,A_  ,deterministic=A_      )
								output_states += (hidden_states,)

							if self.add_downsample:
								A						   =   self.downsamplers_a(A_      )
								output_states += (hidden_states,)

							return hidden_states, output_states







class 				lowerCAmelCase_				(      nn.Module    ):



						'''simple docstring'''


						_lowerCamelCase:    int
						_lowerCamelCase:    int
						_lowerCamelCase:    float      =    0.0
						_lowerCamelCase:    int      =    1
						_lowerCamelCase:    bool      =    True
						_lowerCamelCase:    jnp.dtype      =    jnp.floataa


						def 						_SCREAMING_SNAKE_CASE      ( self     :      List[str]      )     ->   str:
							A						   =   []

							for i in range(self.num_layers      ):
								A						   =   self.in_channels if i == 0 else self.out_channels

								A						   =   FlaxResnetBlockaD(
								    in_channels=A_  ,out_channels=self.out_channels  ,dropout_prob=self.dropout  ,dtype=self.dtype  ,)
								resnets.append(A_      )
							A						   =   resnets

							if self.add_downsample:
								A						   =   FlaxDownsampleaD(self.out_channels  ,dtype=self.dtype      )




						def __call__( self     :      str  ,A_     :      Any  ,A_     :      Tuple  ,A_     :      List[str]=True      )     ->   List[str]:
							A						   =   ()

							for resnet in self.resnets:
								A						   =   resnet(A_  ,A_  ,deterministic=A_      )
								output_states += (hidden_states,)

							if self.add_downsample:
								A						   =   self.downsamplers_a(A_      )
								output_states += (hidden_states,)

							return hidden_states, output_states







class 				lowerCAmelCase_				(      nn.Module    ):



						'''simple docstring'''


						_lowerCamelCase:    int
						_lowerCamelCase:    int
						_lowerCamelCase:    int
						_lowerCamelCase:    float      =    0.0
						_lowerCamelCase:    int      =    1
						_lowerCamelCase:    int      =    1
						_lowerCamelCase:    bool      =    True
						_lowerCamelCase:    bool      =    False
						_lowerCamelCase:    bool      =    False
						_lowerCamelCase:    bool      =    False
						_lowerCamelCase:    jnp.dtype      =    jnp.floataa


						def 						_SCREAMING_SNAKE_CASE      ( self     :      List[Any]      )     ->   str:
							A						   =   []
							A						   =   []

							for i in range(self.num_layers      ):
								A						   =   self.in_channels if (i == self.num_layers - 1) else self.out_channels
								A						   =   self.prev_output_channel if i == 0 else self.out_channels

								A						   =   FlaxResnetBlockaD(
								    in_channels=resnet_in_channels + res_skip_channels  ,out_channels=self.out_channels  ,dropout_prob=self.dropout  ,dtype=self.dtype  ,)
								resnets.append(A_      )

								A						   =   FlaxTransformeraDModel(
								    in_channels=self.out_channels  ,n_heads=self.num_attention_heads  ,d_head=self.out_channels // self.num_attention_heads  ,depth=1  ,use_linear_projection=self.use_linear_projection  ,only_cross_attention=self.only_cross_attention  ,use_memory_efficient_attention=self.use_memory_efficient_attention  ,dtype=self.dtype  ,)
								attentions.append(A_      )

							A						   =   resnets
							A						   =   attentions

							if self.add_upsample:
								A						   =   FlaxUpsampleaD(self.out_channels  ,dtype=self.dtype      )




						def __call__( self     :      List[str]  ,A_     :      Tuple  ,A_     :      Union[str, Any]  ,A_     :      Tuple  ,A_     :      Tuple  ,A_     :      str=True      )     ->   int:
							for resnet, attn in zip(self.resnets  ,self.attentions      ):
								# pop res hidden states
								A						   =   res_hidden_states_tuple[-1]
								A						   =   res_hidden_states_tuple[:-1]
								A						   =   jnp.concatenate((hidden_states, res_hidden_states)  ,axis=-1      )

								A						   =   resnet(A_  ,A_  ,deterministic=A_      )
								A						   =   attn(A_  ,A_  ,deterministic=A_      )

							if self.add_upsample:
								A						   =   self.upsamplers_a(A_      )

							return hidden_states







class 				lowerCAmelCase_				(      nn.Module    ):



						'''simple docstring'''


						_lowerCamelCase:    int
						_lowerCamelCase:    int
						_lowerCamelCase:    int
						_lowerCamelCase:    float      =    0.0
						_lowerCamelCase:    int      =    1
						_lowerCamelCase:    bool      =    True
						_lowerCamelCase:    jnp.dtype      =    jnp.floataa


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Any      )     ->   List[str]:
							A						   =   []

							for i in range(self.num_layers      ):
								A						   =   self.in_channels if (i == self.num_layers - 1) else self.out_channels
								A						   =   self.prev_output_channel if i == 0 else self.out_channels

								A						   =   FlaxResnetBlockaD(
								    in_channels=resnet_in_channels + res_skip_channels  ,out_channels=self.out_channels  ,dropout_prob=self.dropout  ,dtype=self.dtype  ,)
								resnets.append(A_      )

							A						   =   resnets

							if self.add_upsample:
								A						   =   FlaxUpsampleaD(self.out_channels  ,dtype=self.dtype      )




						def __call__( self     :      Union[str, Any]  ,A_     :      Tuple  ,A_     :      Any  ,A_     :      Optional[int]  ,A_     :      int=True      )     ->   List[str]:
							for resnet in self.resnets:
								# pop res hidden states
								A						   =   res_hidden_states_tuple[-1]
								A						   =   res_hidden_states_tuple[:-1]
								A						   =   jnp.concatenate((hidden_states, res_hidden_states)  ,axis=-1      )

								A						   =   resnet(A_  ,A_  ,deterministic=A_      )

							if self.add_upsample:
								A						   =   self.upsamplers_a(A_      )

							return hidden_states







class 				lowerCAmelCase_				(      nn.Module    ):



						'''simple docstring'''


						_lowerCamelCase:    int
						_lowerCamelCase:    float      =    0.0
						_lowerCamelCase:    int      =    1
						_lowerCamelCase:    int      =    1
						_lowerCamelCase:    bool      =    False
						_lowerCamelCase:    bool      =    False
						_lowerCamelCase:    jnp.dtype      =    jnp.floataa


						def 						_SCREAMING_SNAKE_CASE      ( self     :      int      )     ->   Optional[int]:
							# there is always at least one resnet
							A						   =   [
							    FlaxResnetBlockaD(
							        in_channels=self.in_channels  ,out_channels=self.in_channels  ,dropout_prob=self.dropout  ,dtype=self.dtype  ,)
							]

							A						   =   []

							for _ in range(self.num_layers      ):
								A						   =   FlaxTransformeraDModel(
								    in_channels=self.in_channels  ,n_heads=self.num_attention_heads  ,d_head=self.in_channels // self.num_attention_heads  ,depth=1  ,use_linear_projection=self.use_linear_projection  ,use_memory_efficient_attention=self.use_memory_efficient_attention  ,dtype=self.dtype  ,)
								attentions.append(A_      )

								A						   =   FlaxResnetBlockaD(
								    in_channels=self.in_channels  ,out_channels=self.in_channels  ,dropout_prob=self.dropout  ,dtype=self.dtype  ,)
								resnets.append(A_      )

							A						   =   resnets
							A						   =   attentions




						def __call__( self     :      Tuple  ,A_     :      List[Any]  ,A_     :      List[str]  ,A_     :      Union[str, Any]  ,A_     :      List[str]=True      )     ->   Tuple:
							A						   =   self.resnets[0](A_  ,A_      )
							for attn, resnet in zip(self.attentions  ,self.resnets[1:]      ):
								A						   =   attn(A_  ,A_  ,deterministic=A_      )
								A						   =   resnet(A_  ,A_  ,deterministic=A_      )

							return hidden_states

"""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


_lowercase							   =				logging.get_logger(__name__)

_lowercase							   =				{'''vocab_file''': '''spm_char.model'''}

_lowercase							   =				{
    '''vocab_file''': {
        '''microsoft/speecht5_asr''': '''https://huggingface.co/microsoft/speecht5_asr/resolve/main/spm_char.model''',
        '''microsoft/speecht5_tts''': '''https://huggingface.co/microsoft/speecht5_tts/resolve/main/spm_char.model''',
        '''microsoft/speecht5_vc''': '''https://huggingface.co/microsoft/speecht5_vc/resolve/main/spm_char.model''',
    }
}

_lowercase							   =				{
    '''microsoft/speecht5_asr''': 10_24,
    '''microsoft/speecht5_tts''': 10_24,
    '''microsoft/speecht5_vc''': 10_24,
}







class 				lowerCAmelCase_				(      _lowercase    ):



						'''simple docstring'''


						_lowerCamelCase:    Optional[Any]      =    VOCAB_FILES_NAMES
						_lowerCamelCase:    List[Any]      =    PRETRAINED_VOCAB_FILES_MAP
						_lowerCamelCase:    str      =    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						_lowerCamelCase:    Tuple      =    ['''input_ids''', '''attention_mask''']


						def __init__( self     :      List[str]  ,A_     :      int  ,A_     :      List[str]="<s>"  ,A_     :      Optional[Any]="</s>"  ,A_     :      Optional[Any]="<unk>"  ,A_     :      str="<pad>"  ,A_     :      Optional[Dict[str, Any]] = None  ,**A_     :      List[str]  ,)     ->   None:
							A						   =   {} if sp_model_kwargs is None else sp_model_kwargs

							super().__init__(
							    bos_token=A_  ,eos_token=A_  ,unk_token=A_  ,pad_token=A_  ,sp_model_kwargs=self.sp_model_kwargs  ,**A_  ,)

							A						   =   vocab_file

							A						   =   spm.SentencePieceProcessor(**self.sp_model_kwargs      )
							self.sp_model.Load(A_      )


						@property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      Any      )     ->   str:
							return self.sp_model.get_piece_size()


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[int]      )     ->   Optional[Any]:
							A						   =   {self.convert_ids_to_tokens(A_      ): i for i in range(self.vocab_size      )}
							vocab.update(self.added_tokens_encoder      )
							return vocab


						def __getstate__( self     :      str      )     ->   Any:
							A						   =   self.__dict__.copy()
							A						   =   None
							return state


						def __setstate__( self     :      Optional[int]  ,A_     :      str      )     ->   Tuple:
							A						   =   d

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

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


						def 						_SCREAMING_SNAKE_CASE      ( self     :      str  ,A_     :      str      )     ->   List[str]:
							return self.sp_model.encode(A_  ,out_type=A_      )


						def 						_SCREAMING_SNAKE_CASE      ( self     :      List[str]  ,A_     :      Union[str, Any]      )     ->   Union[str, Any]:
							return self.sp_model.piece_to_id(A_      )


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[Any]  ,A_     :      Dict      )     ->   List[Any]:
							A						   =   self.sp_model.IdToPiece(A_      )
							return token


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Tuple  ,A_     :      Optional[Any]      )     ->   List[str]:
							A						   =   []
							A						   =   ''
							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(A_      ) + token
									A						   =   []
								else:
									current_sub_tokens.append(A_      )
							out_string += self.sp_model.decode(A_      )
							return out_string.strip()


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Dict  ,A_     :      Dict  ,A_     :      Optional[int]=None      )     ->   List[int]:
							if token_ids_a is None:
								return token_ids_a + [self.eos_token_id]
							# We don't expect to process pairs, but leave the pair logic for API consistency
							return token_ids_a + token_ids_a + [self.eos_token_id]


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[Any]  ,A_     :      List[int]  ,A_     :      Optional[List[int]] = None  ,A_     :      bool = False      )     ->   List[int]:
							if already_has_special_tokens:
								return super().get_special_tokens_mask(
								    token_ids_a=A_  ,token_ids_a=A_  ,already_has_special_tokens=A_      )

							A						   =   [1]
							if token_ids_a is None:
								return ([0] * len(A_      )) + suffix_ones
							return ([0] * len(A_      )) + ([0] * len(A_      )) + suffix_ones




						def 						_SCREAMING_SNAKE_CASE      ( self     :      str  ,A_     :      str  ,A_     :      Optional[str] = None      )     ->   Tuple[str]:
							if not os.path.isdir(A_      ):
								logger.error(F'Vocabulary path ({save_directory}) should be a directory'      )
								return
							A						   =   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_      ) and os.path.isfile(self.vocab_file      ):
								copyfile(self.vocab_file  ,A_      )
							elif not os.path.isfile(self.vocab_file      ):
								with open(A_  ,'wb'      ) as fi:
									A						   =   self.sp_model.serialized_model_proto()
									fi.write(A_      )

							return (out_vocab_file,)