ó
    óó�j  ã                   ó,   • S r SSKrSSKr " S S5      rg)z>Tokenizer for semantic tokens.

Author
 * Pooneh Mousavi 2024
é    Nc                   ó:   • \ rS rSrSrS rS rS/S/S/4S jrS	rg)
ÚDiscreteSSLTokenizeré   aù  This class is tokenizer for DiscreteSSL models that apply post-processing on the semnatic tokens extracted from DiscreteSSL model.
It makes the token ids of each layer to be unique by adding the token IDs of each layer by layer_num*sunmber_of _cluster.
It applies deduplication for each layer independently if the field is set to true for the layer and padded all items with zero.
It applies subwording for each layer independently if the sentence piece tokenizer is set to for the layer and padded all items with zero.
If subwording is not applied, all token IDs are incremented by one to avoid conflict between pad_id(0) and cluster with centroid zero.

Arguments
---------
num_clusters: List[int]
    determine the number of clusters of the  kmeans models. It could be varying for each layer.

Example
-------
>>> import torch
>>> inputs = torch.randint(0, 1000, (3, 6, 2))
>>> ssl_layer_num = [7, 23]
>>> deduplicate = [False, True]
>>> bpe_tokenizers = [None, None]
>>> num_clusters = [1000, 2000]
>>> tokenizer = DiscreteSSLTokenizer(num_clusters=num_clusters)
>>> tokens = tokenizer.encode(
...     inputs,
...     SSL_layers=ssl_layer_num,
...     deduplicates=deduplicate,
...     bpe_tokenizers=bpe_tokenizers,
... )
>>> print(tokens.shape)
torch.Size([3, 6, 2])
c                 ó   • Xl         g )N©Únum_clusters)Úselfr   s     Ú»C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\speechbrain/tokenizers/discrete_SSL_tokenizer.pyÚ__init__ÚDiscreteSSLTokenizer.__init__*   s   € Ø(Õó    c                 ó    • / nU H@  nUR                  SR                  U Vs/ s H  n[        US-   5      PM     sn5      5        MB     U$ s  snf )a  Convert token ID to char to be used for training sentencepiece tokenizer.

Arguments
---------
tokens : torch.Tensor
    A (Batch x Seq ) tensor of audio tokens

Returns
-------
processed_tokens : list
    A (Batch x Seq) list of corresponding char for each token ID.
Ú éa   )ÚappendÚjoinÚchr)r	   ÚtokensÚtokens_charÚrowÚtokens        r
   ÚtextifyÚDiscreteSSLTokenizer.textify-   sN   € ð ˆãˆCØ×Ñ˜sŸx™xÉÓ(LÊ¸u¬¨e°r©\Ö):ÉÑ(LÓMÖNñ àÐùò )Ms   ¢Aé   FNc                 óˆ  • UR                   S   [        U5      :X  d!   SUR                    S[        U5       S35       e/ n[        U5       GHy  u  pg/ nU(       a   USS2SS2U4   R                  5        V	s/ s H<  n	U	[        R
                  " U	[        R                  S9R                  [        5         PM>     n
n	U
 V	s/ s H   o™R                  5       R                  5       PM"     nn	UR                  U5        OUR                  USS2SS2U4   5        XF   bp  U R                  U5      nUR                  U V	s/ s H?  n	[        R                  " XF   R                  U	5      5      X&   U R                   U   -  -   PMA     sn	5        GM?  UR                  U V	s/ s H  n	X’U   U R                   U   -  -   S-   PM     sn	5        GM|     [        R"                  " [        R$                  " [        R&                  R(                  R*                  R-                  USS	9UR                   S
   5      SS9$ s  sn	f s  sn	f s  sn	f s  sn	f )a8  Takes an input tokenized wavform and return its corresponding processed tokens.

Arguments
---------
input : torch.Tensor
    A (Batch x Seq x num_SSL_layers) tensor of audio tokens.
SSL_layers: List[int] (default: [7]):
    determine which layers of SSL should be used to extract information.
deduplicates: List[boolean] (default: [False]):
    determine to apply deduplication(remove duplicate subsequent tokens) on the tokens extracted for the corresponding layer.
bpe_tokenizers: List[int] (default: [None]):
    determine to apply subwording on the tokens extracted for the corresponding layer if the sentencePiece tokenizer is trained for that layer.

Returns
-------
processed_tokens : torch.Tensor
    A (Batch x Seq x num_SSL_layers) tensor of audio tokens after applying deduplication and subwording if necessary.
é   zinput shape:z7 has conflicts with the length of provided SSL_layers: zJ. The second dimension of input should be the same  as number of layers!!!N)Úprependé   T)Úbatch_firstr   )Údim)ÚshapeÚlenÚ	enumerateÚcpuÚnpÚdiffÚnanÚastypeÚboolÚcloneÚdetachÚextendr   ÚtorchÚ
LongTensorÚencode_as_idsr   ÚstackÚsplitÚnnÚutilsÚrnnÚpad_sequence)r	   ÚinputÚ
SSL_layersÚdeduplicatesÚbpe_tokenizersÚ	token_idsÚiÚ	duplicater   r   Úunique_token_idsÚlayer_token_idsÚ
token_chars                r
   ÚencodeÚDiscreteSSLTokenizer.encode@   s8  € ð* �{‰{˜1‰~¤ Z£Ó0ð 	
Ø˜5Ÿ;™;˜-Ð'^Ô_bÐcmÓ_nÐ^oð  pzð  {ó	
Ð0ð ˆ	Ü% l×3‰LˆAØˆFÞð  %¢Qª¨1 W™~×1Ñ1Ô3ó$â3˜ð œŸš ¬R¯V©VÑ4×;Ñ;¼DÓAÔBÙ3ð !ð $ñ
 5Eó#Ú4D¨S—I‘I“K×&Ñ&Ö(Ñ4Dð  ð #ð —‘˜oÕ.ð —‘˜e¢A¢q¨! G™nÔ-àÑ Ñ,Ø!Ÿ\™\¨&Ó1�
Ø× Ñ ñ $.óò $.˜Cô ×(Ò(¨Ñ):×)HÑ)HÈÓ)MÓNØ$™-¨$×*;Ñ*;¸AÑ*>Ñ>ô?á#-ñ÷ð × Ñ ñ $*óâ#)˜Cð ¨™m¨d×.?Ñ.?ÀÑ.BÑBÑBÀQÔFÙ#)ñ÷ñ3 4ô@ �{Š{Ü�KŠKÜ—‘—‘×"Ñ"×/Ñ/°	ÀtÐ/ÐLØ—‘˜A‘óð ñ
ð 	
ùò;$ùò#ùòùòs   Á6AH0Â?'H5Ä:AH:
Æ$H?
r   )	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   r@   Ú__static_attributes__© r   r
   r   r      s%   † ñò<)òð( "# °5°'È4È&÷?
r   r   )rF   Únumpyr%   r-   r   rH   r   r
   Ú<module>rJ      s   ðñó Û ÷t
ò t
r   