Ë
    T^(hÃ<  ã                   óÀ   — d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZmZ ddlmZ  ej"                  e«      Zdd	iZ G d
„ d«      Z G d„ de«      ZdgZy)z"Tokenization class for model MyT5.é    N)Údefaultdict)ÚDictÚListÚOptionalÚTupleÚUnioné   )Ú
AddedTokenÚPreTrainedTokenizer)ÚloggingÚ
vocab_filezbyte_maps.jsonc            	       óÚ   — e Zd ZdZdZdeeeeef   f   fd„Zdeeee	e
e   f   f   dedefd„Zdeeef   d	eeee	e
e   f   f   fd
„Zde
e   d	ede
e   f   fd„Zdde
e   d	e
e   fd„Zy)ÚByteRewriteraZ  
    Byte rewriter class for MyT5 tokenizer.
    This class is used to rewrite bytes using a hash tree. The hash tree is constructed from a set of rewriting rules.

    Args:
        rewriting_rules (`str` or `Dict[str, str]`):
            A path to a json file containing the rewriting rules or a dictionary containing the rewriting rules.

    z[LEAF]Úrewriting_rulesc                 óŠ  — t        |t        «      r+t        |d«      5 }t        j                  |«      }d d d «       n't        |t
        «      st        dt        |«      › �«      ‚| j                  |«      | _	        |j                  «       D ��ci c]  \  }}||“Œ
 }}}| j                  |«      | _        y # 1 sw Y   ŒYxY wc c}}w )NÚrzDrewriting_rules should be either a path to json file or a dict, got )Ú
isinstanceÚstrÚopenÚjsonÚloadÚdictÚ
ValueErrorÚtypeÚconstruct_hash_treeÚ	hash_treeÚitemsÚreverse_hash_tree)Úselfr   ÚfÚkÚvÚreverse_rewriting_ruless         úh/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/myt5/tokenization_myt5.pyÚ__init__zByteRewriter.__init__.   sº   € Ü�o¤sÔ+Ü�o sÓ+ð /¨qÜ"&§)¡)¨A£,�÷/ð /ä˜O¬TÔ2ÜØVÔW[Ð\kÓWlÐVmÐnóð ð ×1Ñ1°/ÓBˆŒØ4C×4IÑ4IÓ4K×"L©D¨A¨q 1 a¡4Ð"LÐÑ"LØ!%×!9Ñ!9Ð:QÓ!RˆÕ÷/ð /üó #Ms   �B3ÂB?Â3B<r   Úbyte_in_sequenceÚbyte_out_sequencec                 ó”   — |j                  d«      }|j                  d«      }|}|D ]  }||vri ||<   ||   }Œ ||| j                  <   y)zL
        Add a leaf with the output byte sequence to the hash tree.
        ú N)ÚsplitÚLEAF)r   r   r&   r'   Úbyte_in_listÚbyte_out_listÚtree_pointerÚbs           r$   Úadd_leafzByteRewriter.add_leaf;   sb   € ð (×-Ñ-¨cÓ2ˆØ)×/Ñ/°Ó4ˆà ˆØò 	+ˆAØ˜Ñ$Ø"$�˜Q‘Ø'¨™?‰Lð	+ð
 #0ˆ�T—Y‘YÒó    Úreturnc                 óÎ   — t        t        «      }d„ t        d«      D «       D ]  }|g||   | j                  <   Œ |j	                  «       D ]  \  }}| j                  |||«       Œ |S )zE
        Construct a hash tree for rewritten byte sequences.
        c              3   ó$   K  — | ]  }|d ›–— Œ
 y­w)Ú02xN© )Ú.0Úxs     r$   ú	<genexpr>z3ByteRewriter.construct_hash_tree.<locals>.<genexpr>O   s   è ø€ Ò1 �Q�s�G“*Ñ1ùs   ‚é   )r   r   Úranger+   r   r0   )r   r   r   r/   Úin_sequenceÚout_sequences         r$   r   z ByteRewriter.construct_hash_treeJ   ss   € ô  ¤Ó%ˆ	Ù1¤e¨C£jÔ1ò 	*ˆAØ'( cˆI�a‰L˜Ÿ™Ò#ð	*ð *9×)>Ñ)>Ó)@ò 	@Ñ%ˆK˜Ø�M‰M˜) [°,Õ?ð	@ð Ðr1   Úbyte_sequenceNc                 ó\   — | j                   }|D ]  }||v r||   }Œ y || j                     S )zW
        Search the hash tree and return the rewritten byte sequence if found.
        N)r   r+   )r   r>   r.   r/   s       r$   Úsearch_hash_treezByteRewriter.search_hash_treeW   sA   € ð —~‘~ˆØò 	ˆAØ�LÑ Ø+¨A™‘áð		ð ˜DŸI™IÑ&Ð&r1   Úin_bytesc                 óZ  — g }d}d}|t        |«      k  r–|s| j                  n| j                  }t        |t        |«      «      D ]?  }||   }||v r||   }n||k(  r|g}	|} n$ n"| j                  |v sŒ/|| j                     }	|}ŒA |j                  	«       |dz   }|t        |«      k  rŒ–|S )a6  
        Rewrite a sequence of bytes using the hash tree.

        Args:
            in_bytes (`List[str]`): A list of bytes to be rewritten.
            reverse (`bool`): If True, decoding is performed with the reverse hash tree.
        Returns:
            `List[str]`: The rewritten byte sequence.
        r   é   )Úlenr   r   r;   r+   Úextend)
r   rA   ÚreverseÚ	out_bytesÚb_startÚb_endr.   Újr/   Úcur_leafs
             r$   Úrewrite_byteszByteRewriter.rewrite_bytesd   sÍ   € ð ˆ	ØˆØˆàœ˜H›Ò%Ù18˜4Ÿ>š>¸d×>TÑ>TˆLÜ˜7¤C¨£MÓ2ò �Ø˜Q‘K�Ø˜Ñ$Ø#/°¡?‘LØ˜'’\Ø !˜s�HØ�EÙáØ—9‘9 Ò,Ø+¨D¯I©IÑ6�HØ‘Eðð ×Ñ˜XÔ&Ø˜a‘iˆGð! œ˜H›Ó%ð$ Ðr1   )F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   r   r   r   r%   r   r   r0   r   r@   rL   r6   r1   r$   r   r   !   sØ   „ ñð €DðS¨¨c°4¸¸S¸±>Ð.AÑ(Bó Sð0 $ s¨E°$¸¸S¹	°/Ñ,BÐ'BÑ"Cð 0ÐWZð 0Ðoró 0ð°4¸¸S¸±>ð ÀdÈ3ÐPUÐVZÐ\`ÐadÑ\eÐVeÑPfÐKfÑFgó ð'¨d°3©ið '¸EÀ$ÈÈSÉ	À/Ñ<Ró 'ñ  d¨3¡ið  À4ÈÁ9ô  r1   r   c            
       óŠ  ‡ — e Zd ZdZddgZeZ	 	 	 	 	 d	 dˆ fd„Zed„ «       Z	d„ Z
	 ddee   d	eee      d
edee   fˆ fd„Zdee   dee   fd„Z	 ddee   d	eee      dee   fd„Z	 ddee   d	eee      dee   fd„Zdedee   fd„Zd„ Zd„ Zdee   dee   fd„Zdee   dee   fd„Zd„ Zddedee   dee   fd„Zˆ xZS )ÚMyT5Tokenizeraè  
    Construct a MyT5 tokenizer.

    This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
    this superclass for more information regarding those methods.

    Args:
        vocab_file (`str`): The file containing the byte rewriting rules.
        eos_token (`str`, *optional*, defaults to `"</s>"`):
            The end of sequence token.

        unk_token (`str`, *optional*, defaults to `"<unk>"`):
            The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
            token instead.
        pad_token (`str`, *optional*, defaults to `"<pad>"`):
            The token used for padding, for example when batching sequences of different lengths.
        extra_ids (`int`, *optional*, defaults to 125):
            Add a number of extra ids added to the end of the vocabulary for use as sentinels. These tokens are
            accessible as "<extra_id_{%d}>" where "{%d}" is a number between 0 and extra_ids-1. Extra tokens are
            indexed from the end of the vocabulary up to beginning ("<extra_id_0>" is the last token in the vocabulary
            like in ByT5 preprocessing see
            [here](https://github.com/google-research/text-to-text-transfer-transformer/blob/9fd7b14a769417be33bc6c850f9598764913c833/t5/data/preprocessors.py#L2117)).
        additional_special_tokens (`List[str]`, *optional*):
            Additional special tokens used by the tokenizer.
    Ú	input_idsÚattention_maskr2   c           	      óì  •— |dkD  r|€t        |«      D �cg c]  }d|› d�‘Œ
 }}nK|dkD  rF|�Dt        |«      dkD  r6t        t        t        d„ |«      «      «      }	|	|k7  rt	        d|› d|› d�«      ‚t        |t        «      rt        |dd¬	«      n|}t        |t        «      rt        |dd¬	«      n|}t        |t        «      rt        |dd¬	«      n|}|||d
œ| _        t        | j                  «      | _	        d| _
        t        j                  t        |d«      «      | _        t        | j                  d   «      | _        t        | j                  d   «      | _        t%        ‰
| �L  d|||d|dœ|¤Ž y c c}w )Nr   z
<extra_id_ú>c                 ó.   — t        dt        | «      v «      S )NÚextra_id)Úboolr   )r8   s    r$   ú<lambda>z(MyT5Tokenizer.__init__.<locals>.<lambda>´   s   € ´D¸ÄsÈ1ÃvÐ9MÓ4N€ r1   zBoth extra_ids (z!) and additional_special_tokens (zm) are provided to MyT5Tokenizer. In this case the additional_special_tokens must include the extra_ids tokensT)ÚlstripÚrstrip)r   rC   é   r:   r   Údecompose_mapÚ	merge_map)Ú	eos_tokenÚ	unk_tokenÚ	pad_tokenÚ	extra_idsÚadditional_special_tokensr6   )r;   rD   ÚsetÚfilterr   r   r   r
   Ú_added_tokens_decoderÚoffsetÚ_utf_vocab_sizer   r   r   Ú	byte_mapsr   Údecompose_rewriterÚmerge_rewriterÚsuperr%   )r   r   r`   ra   rb   rc   rd   ÚkwargsÚiÚextra_tokensÚ	__class__s             €r$   r%   zMyT5Tokenizer.__init__¥   sŽ  ø€ ð �qŠ=Ð6Ð>ÜDIÈ)ÓDTÖ(U¸q¨:°a°S¸Ò):Ð(UÐ%Ñ(UØ˜Š]Ð8ÐDÌÐMfÓIgÐjkÒIkäœs¤6Ñ*NÐPiÓ#jÓkÓlˆLØ˜yÒ(Ü Ø& y kÐ1RÐSlÐRmð n(ð (óð ô HRÐR[Ô]`ÔGa”J˜y°¸dÕCÐgpˆ	ÜGQÐR[Ô]`ÔGa”J˜y°¸dÕCÐgpˆ	ÜGQÐR[Ô]`ÔGa”J˜y°¸dÕCÐgpˆ	à)2°yÀYÑ%OˆÔ"Ü˜$×4Ñ4Ó5ˆŒØ#ˆÔô Ÿ™¤4¨
°CÓ#8Ó9ˆŒä".¨t¯~©~¸oÑ/NÓ"OˆÔÜ*¨4¯>©>¸+Ñ+FÓGˆÔä‰Ñð 	
ØØØØØ&?ñ	
ð ó	
ùò3 )Vs   –E1c                 ó   — | j                   S ©N)ri   )r   s    r$   Ú
vocab_sizezMyT5Tokenizer.vocab_sizeÓ   s   € à×#Ñ#Ð#r1   c                 óÄ   — t        | j                  | j                  z   «      D �ci c]  }| j                  |«      |“Œ }}|j	                  | j
                  «       |S c c}w rs   )r;   rt   rh   Úconvert_ids_to_tokensÚupdateÚadded_tokens_encoder)r   ro   Úvocabs      r$   Ú	get_vocabzMyT5Tokenizer.get_vocabØ   sW   € Ü;@ÀÇÁÐSW×S^ÑS^ÑA^Ó;_Ö`°a�×+Ñ+¨AÓ.°Ñ1Ð`ˆÐ`Ø�‰�T×.Ñ.Ô/Øˆùò as   ¥AÚtoken_ids_0Útoken_ids_1Úalready_has_special_tokensc                 ó¤   •— |rt         ‰| �  ||d¬«      S |€dgt        |«      z  dgz   S dgt        |«      z  dgz   dgt        |«      z  z   dgz   S )aÄ  
        Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
        special tokens using the tokenizer `prepare_for_model` method.

        Args:
            token_ids_0 (`List[int]`):
                List of IDs.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.
            already_has_special_tokens (`bool`, *optional*, defaults to `False`):
                Whether or not the token list is already formatted with special tokens for the model.

        Returns:
            `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
        T)r{   r|   r}   r   rC   )rm   Úget_special_tokens_maskrD   )r   r{   r|   r}   rq   s       €r$   r   z%MyT5Tokenizer.get_special_tokens_maskÞ   sy   ø€ ñ$ &Ü‘7Ñ2Ø'°[Ð]að 3ó ð ð
 ÐØ�Cœ#˜kÓ*Ñ*¨q¨cÑ1Ð1Ø�”c˜+Ó&Ñ&¨1¨#Ñ-°!°´s¸;Ó7GÑ1GÑHÈAÈ3ÑNÐNr1   Ú	token_idsc                 ó¬   — t        |«      dkD  r7|d   | j                  k(  r%t        j                  d| j                  › d�«       |S || j                  gz   S )z.Do not add eos again if user already added it.r   éÿÿÿÿzThis sequence already has zQ. In future versions this behavior may lead to duplicated eos tokens being added.)rD   Úeos_token_idÚwarningsÚwarnr`   )r   r€   s     r$   Ú_add_eos_if_not_presentz%MyT5Tokenizer._add_eos_if_not_presentú   s]   € äˆy‹>˜AÒ )¨B¡-°4×3DÑ3DÒ"DÜ�M‰MØ,¨T¯^©^Ð,<ð =+ð +ôð Ðà × 1Ñ 1Ð2Ñ2Ð2r1   c                 ót   — | j                   g}|€t        ||z   «      dgz  S t        ||z   |z   |z   «      dgz  S )aÉ  
        Create a mask from the two sequences passed to be used in a sequence-pair classification task. MyT5 does not
        make use of token type ids, therefore a list of zeros is returned.

        Args:
            token_ids_0 (`List[int]`):
                List of IDs.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `List[int]`: List of zeros.
        r   )rƒ   rD   )r   r{   r|   Úeoss       r$   Ú$create_token_type_ids_from_sequencesz2MyT5Tokenizer.create_token_type_ids_from_sequences  sP   € ð  × Ñ Ð!ˆàÐÜ�{ SÑ(Ó)¨Q¨CÑ/Ð/Ü�; Ñ$ {Ñ2°SÑ8Ó9¸Q¸CÑ?Ð?r1   c                 óX   — | j                  |«      }|€|S | j                  |«      }||z   S )a‚  
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
        adding special tokens. A sequence has the following format:

        - single sequence: `X </s>`
        - pair of sequences: `A </s> B </s>`

        Args:
            token_ids_0 (`List[int]`):
                List of IDs to which the special tokens will be added.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
        )r†   )r   r{   r|   s      r$   Ú build_inputs_with_special_tokensz.MyT5Tokenizer.build_inputs_with_special_tokens  s;   € ð& ×2Ñ2°;Ó?ˆØÐØÐà×6Ñ6°{ÓCˆKØ Ñ,Ð,r1   Útextc                 ór   — |j                  d«      D �cg c]  }|d›‘Œ }}| j                  |«      }|S c c}w )z‡Take as input a string and return a list of strings (tokens) for words/sub-words.
        Represents tokens in two character hex formatúutf-8r5   )ÚencodeÚmorphological_encode)r   rŒ   rn   ro   Útokenss        r$   Ú	_tokenizezMyT5Tokenizer._tokenize6  s@   € ð '+§k¡k°'Ó&:Ö; �Q�s�G‘*Ð;ˆÐ;Ø×*Ñ*¨6Ó2ˆØˆùò <s   ”4c                 ó\   — t        |«      dk7  rd}|S t        |d«      | j                  z   }|S )z0Converts a token (str) in an id using the vocab.r]   Né   )rD   Úintrh   )r   ÚtokenÚtoken_ids      r$   Ú_convert_token_to_idz"MyT5Tokenizer._convert_token_to_id>  s6   € ô ˆu‹:˜Š?ØˆHð ˆô ˜5 "“~¨¯©Ñ3ˆHàˆr1   c                 ó(   — || j                   z
  d›}|S )z=Converts an index (integer) in a token (str) using the vocab.r5   )rh   )r   Úindexr–   s      r$   Ú_convert_id_to_tokenz"MyT5Tokenizer._convert_id_to_tokenH  s   € à˜4Ÿ;™;Ñ& sÐ+ˆØˆr1   Úindicesc                 óz   — | j                   j                  |d¬«      }| j                  j                  |d¬«      }|S )NF©rF   )rk   rL   rl   ©r   rœ   s     r$   r�   z"MyT5Tokenizer.morphological_encodeM  s=   € à×)Ñ)×7Ñ7¸ÈÐ7ÓOˆØ×%Ñ%×3Ñ3°GÀUÐ3ÓKˆØˆr1   c                 óz   — | j                   j                  |d¬«      }| j                  j                  |d¬«      }|S )NTrž   )rl   rL   rk   rŸ   s     r$   Úmorphological_decodez"MyT5Tokenizer.morphological_decodeS  s=   € à×%Ñ%×3Ñ3°GÀTÐ3ÓJˆØ×)Ñ)×7Ñ7¸ÈÐ7ÓNˆØˆr1   c                 óø  — d}g }|D ]`  }|| j                   v r|j                  | j                   |   «       Œ0|| j                  v r|j                  |«       ŒP|j                  |«       Œb | j                  |«      }t	        | j                   j                  «       «      t	        | j                  «      z  }|D ].  }||v r|t        |d«      z  }Œ|t        j                  |«      z  }Œ0 |j                  dd¬«      }|S )z:Converts a sequence of tokens (string) in a single string.r1   rŽ   Úignore)Úerrors)	Úadded_tokens_decoderÚappendrx   r¡   re   ÚvaluesÚbytesÚfromhexÚdecode)r   r‘   ÚbstringÚ
out_tokensr–   Ú_added_tokensÚstrings          r$   Úconvert_tokens_to_stringz&MyT5Tokenizer.convert_tokens_to_stringY  sû   € àˆàˆ
Øò 	)ˆEØ˜×1Ñ1Ñ1Ø×!Ñ! $×";Ñ";¸EÑ"BÕCØ˜$×3Ñ3Ñ3Ø×!Ñ! %Õ(à×!Ñ! %Õ(ð	)ð ×.Ñ.¨zÓ:ˆ
Ü˜D×5Ñ5×<Ñ<Ó>Ó?Ä#Àd×F_ÑF_ÓB`Ñ`ˆØò 	0ˆEØ˜Ñ%Øœ5 ¨Ó0Ñ0‘àœ5Ÿ=™=¨Ó/Ñ/‘ð		0ð
 —‘ °�Ó9ˆØˆr1   Úsave_directoryÚfilename_prefixc                 ón  — t         j                  j                  |«      r2t         j                  j                  ||r|dz   ndt        d   z   «      }n|r|dz   nd|z   }t        |dd¬«      5 }|j                  t        j                  | j                  dd¬	«      «       d d d «       |fS # 1 sw Y   |fS xY w)
Nú-Ú r   ÚwrŽ   )Úencodingr]   F)ÚindentÚensure_ascii)
ÚosÚpathÚisdirÚjoinÚVOCAB_FILES_NAMESr   Úwriter   Údumpsrj   )r   r°   r±   r   Úwriters        r$   Úsave_vocabularyzMyT5Tokenizer.save_vocabularyp  s¢   € Ü�7‰7�=‰=˜Ô(ÜŸ™Ÿ™Ø¹/ °3Ò!6ÈrÔUfÐgsÑUtÑ tó‰Jñ 4C˜/¨CÒ/ÈÈnÑ\ˆJÜ�*˜c¨GÔ4ð 	S¸Ø�L‰LœŸ™ D§N¡N¸1È5ÔQÔR÷	Sàˆ}Ð÷	Sàˆ}Ðús   Á,2B)Â)B4)z</s>z<unk>z<pad>é}   N)r2   N)NFrs   )rM   rN   rO   rP   Úmodel_input_namesr½   Úvocab_files_namesr%   Úpropertyrt   rz   r   r•   r   rY   r   r†   r‰   r‹   r   r’   r˜   r›   r�   r¡   r¯   r   rÁ   Ú__classcell__)rq   s   @r$   rR   rR   ‡   s¢  ø„ ñð4 %Ð&6Ð7ÐØ)Ðð
 ØØØØ"&ð,
ð 
õ,
ð\ ñ$ó ð$òð sxñOØ ™9ðOØ3;¸DÀ¹IÑ3FðOØkoðOà	ˆc‰õOð8	3°°c±ð 	3¸tÀC¹yó 	3ð JNñ@Ø ™9ð@Ø3;¸DÀ¹IÑ3Fð@à	ˆc‰ó@ð0 JNñ-Ø ™9ð-Ø3;¸DÀ¹IÑ3Fð-à	ˆc‰ó-ð4˜cð °°S±	ó òòð
¨D°©Ið ¸$¸s¹)ó ð¨D°©Ið ¸$¸s¹)ó òñ.	¨cð 	ÀHÈSÁMð 	Ð]bÐcfÑ]g÷ 	r1   rR   )rP   r   r¹   r„   Úcollectionsr   Útypingr   r   r   r   r   Útokenization_utilsr
   r   Úutilsr   Ú
get_loggerrM   Úloggerr½   r   rR   Ú__all__r6   r1   r$   ú<module>rÎ      sk   ðñ )ã Û 	Û Ý #ß 5Õ 5ç AÝ ð 
ˆ×	Ñ	˜HÓ	%€ð "Ð#3Ð4Ð ÷cñ côLrÐ'ô rðj Ð
�r1   