a
    dO                     @   s   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	 ddl
ZddlmZmZmZmZmZ ddlmZ eeZdd	d
Zddiddid
ZddiZdi iZe dd Zdd Zdd Zdd ZG dd deZ G dd deZ!dS )zTokenization classes for CLIP.    N)	lru_cache)ListOptionalTuple   )
AddedTokenPreTrainedTokenizer_is_control_is_punctuation_is_whitespace)loggingz
vocab.jsonz
merges.txt)
vocab_filemerges_filezopenai/clip-vit-base-patch32zKhttps://huggingface.co/openai/clip-vit-base-patch32/resolve/main/vocab.jsonzKhttps://huggingface.co/openai/clip-vit-base-patch32/resolve/main/merges.txtM   c                  C   s   t ttdtdd t ttdtdd  t ttdtdd  } | dd }d	}td
D ],}|| vrf| | |d
|  |d7 }qfdd |D }tt| |S )a8  
    Returns list of utf-8 byte and a mapping to unicode strings. We specifically avoids mapping to whitespace/control
    characters the bpe code barfs on.

    The reversible bpe codes work on unicode strings. This means you need a large # of unicode characters in your vocab
    if you want to avoid UNKs. When you're at something like a 10B token dataset you end up needing around 5K for
    decent coverage. This is a significant percentage of your normal, say, 32K bpe vocab. To avoid that, we want lookup
    tables between utf-8 bytes and unicode strings.
    !~      ¡   ¬   ®   ÿNr      c                 S   s   g | ]}t |qS  )chr).0nr   r   s/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/transformers/models/clip/tokenization_clip.py
<listcomp>L       z$bytes_to_unicode.<locals>.<listcomp>)listrangeordappenddictzip)bscsr   br   r   r   bytes_to_unicode7   s    L

r(   c                 C   s6   t  }| d }| dd D ]}|||f |}q|S )z
    Return set of symbol pairs in a word.

    Word is represented as tuple of symbols (symbols being variable-length strings).
    r   r   N)setadd)wordpairsZ	prev_charcharr   r   r   	get_pairsP   s    r.   c                 C   s   t dd| } |  } | S )Nz\s+ )resubstrip)textr   r   r   whitespace_clean^   s    r4   c                 C   s   |   } | sg S |  }|S )z@Runs basic whitespace cleaning and splitting on a piece of text.)r2   split)r3   tokensr   r   r   whitespace_tokenizee   s
    r7   c                   @   sN   e Zd ZdZdddZdddZdd	 Zdd
dZdd Zdd Z	dd Z
dS )BasicTokenizera  
    Constructs a BasicTokenizer that will run basic tokenization (punctuation splitting, lower casing, etc.).

    Args:
        do_lower_case (`bool`, *optional*, defaults to `True`):
            Whether or not to lowercase the input when tokenizing.
        never_split (`Iterable`, *optional*):
            Collection of tokens which will never be split during tokenization. Only has an effect when
            `do_basic_tokenize=True`
        tokenize_chinese_chars (`bool`, *optional*, defaults to `True`):
            Whether or not to tokenize Chinese characters.

            This should likely be deactivated for Japanese (see this
            [issue](https://github.com/huggingface/transformers/issues/328)).
        strip_accents (`bool`, *optional*):
            Whether or not to strip all accents. If this option is not specified, then it will be determined by the
            value for `lowercase` (as in the original BERT).
    TNc                 C   s,   |d u rg }|| _ t|| _|| _|| _d S N)do_lower_caser)   never_splittokenize_chinese_charsstrip_accents)selfr:   r;   r<   r=   r   r   r   __init__   s    
zBasicTokenizer.__init__c                 C   s   |r| j t|n| j }| |}| jr4| |}t|}g }|D ]R}||vr| jrt| }| j	dur| 
|}n| j	r| 
|}|| || qDtd|}|S )a  
        Basic Tokenization of a piece of text. Split on "white spaces" only, for sub-word tokenization, see
        WordPieceTokenizer.

        Args:
            never_split (`List[str]`, *optional*)
                Kept for backward compatibility purposes. Now implemented directly at the base class level (see
                [`PreTrainedTokenizer.tokenize`]) List of token not to split.
        Fr/   )r;   unionr)   _clean_textr<   _tokenize_chinese_charsr7   r:   lowerr=   _run_strip_accentsextend_run_split_on_puncjoin)r>   r3   r;   Zorig_tokensZsplit_tokenstokenZoutput_tokensr   r   r   tokenize   s"    



zBasicTokenizer.tokenizec                 C   sB   t d|}g }|D ]"}t |}|dkr,q|| qd|S )z$Strips accents from a piece of text.ZNFDZMn )unicodedata	normalizecategoryr"   rG   )r>   r3   outputr-   catr   r   r   rD      s    
z!BasicTokenizer._run_strip_accentsc                 C   s   |dur||v r|gS t |}d}d}g }|t|k r|| }t|rX||g d}n |rf|g  d}|d | |d7 }q*dd |D S )	z&Splits punctuation on a piece of text.Nr   TFr   c                 S   s   g | ]}d  |qS )rJ   )rG   )r   xr   r   r   r      r   z5BasicTokenizer._run_split_on_punc.<locals>.<listcomp>)r   lenr
   r"   )r>   r3   r;   charsiZstart_new_wordrN   r-   r   r   r   rF      s"    

z!BasicTokenizer._run_split_on_puncc                 C   sT   g }|D ]@}t |}| |r>|d || |d q|| qd|S )z)Adds whitespace around any CJK character.r/   rJ   )r!   _is_chinese_charr"   rG   r>   r3   rN   r-   cpr   r   r   rB      s    


z&BasicTokenizer._tokenize_chinese_charsc                 C   s   |dkr|dks|dkr |dks|dkr0|dks|dkr@|dks|d	krP|d
ks|dkr`|dks|dkrp|dks|dkr|dkrdS dS )z6Checks whether CP is the codepoint of a CJK character.i N  i  i 4  iM  i   iߦ i  i? i@ i i  i i   i  i  i TFr   )r>   rW   r   r   r   rU      sD    
zBasicTokenizer._is_chinese_charc                 C   sX   g }|D ]D}t |}|dks|dkst|r.qt|rB|d q|| qd|S )zBPerforms invalid character removal and whitespace cleanup on text.r   i  r/   rJ   )r!   r	   r   r"   rG   rV   r   r   r   rA      s    zBasicTokenizer._clean_text)TNTN)N)N)__name__
__module____qualname____doc__r?   rI   rD   rF   rB   rU   rA   r   r   r   r   r8   o   s   

%
r8   c                       s   e Zd ZdZeZeZeZ	ddgZ
d$ fdd	Zed	d
 Zdd Zd%ee eee  ee dddZd&ee eee  eee d fddZd'ee eee  ee dddZdd Zdd Zdd Zdd Zdd  Zd(eee ee d!d"d#Z  ZS ))CLIPTokenizera<  
    Construct a CLIP tokenizer. Based on byte-level Byte-Pair-Encoding.

    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`):
            Path to the vocabulary file.
        merges_file (`str`):
            Path to the merges file.
        errors (`str`, *optional*, defaults to `"replace"`):
            Paradigm to follow when decoding bytes to UTF-8. See
            [bytes.decode](https://docs.python.org/3/library/stdtypes.html#bytes.decode) for more information.
        unk_token (`str`, *optional*, defaults to `<|endoftext|>`):
            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.
        bos_token (`str`, *optional*, defaults to `<|startoftext|>`):
            The beginning of sequence token.
        eos_token (`str`, *optional*, defaults to `<|endoftext|>`):
            The end of sequence token.
    Z	input_idsZattention_maskreplace<|endoftext|><|startoftext|>c                    s  t |trt|dddn|}t |tr4t|dddn|}t |trPt|dddn|}t jf |||||d| zdd l}	|	j| _W n. ty   t	d t
dd| _d | _Y n0 t|dd	}
t|
| _W d    n1 s0    Y  d
d | j D | _|| _t | _dd | j D | _t|dd	*}|  ddd }W d    n1 sf0    Y  dd |D }tt|tt|| _ddd| _t dtj!| _"d S )NF)lstriprstrip)errors	unk_token	bos_token	eos_token	pad_tokenr   zKftfy or spacy is not installed using custom BasicTokenizer instead of ftfy.T)r:   utf-8encodingc                 S   s   i | ]\}}||qS r   r   r   kvr   r   r   
<dictcomp>D  r   z*CLIPTokenizer.__init__.<locals>.<dictcomp>c                 S   s   i | ]\}}||qS r   r   rj   r   r   r   rm   G  r   
r   i  c                 S   s   g | ]}t | qS r   )tupler5   )r   merger   r   r   r   J  r   z*CLIPTokenizer.__init__.<locals>.<listcomp>r_   r^   )r_   r^   z[<\|startoftext\|>|<\|endoftext\|>|'s|'t|'re|'ve|'m|'ll|'d|[\p{L}]+|[\p{N}]|[^\s\p{L}\p{N}]+)#
isinstancestrr   superr?   ftfyfix_textImportErrorloggerinfor8   nlpopenjsonloadencoderitemsdecoderrb   r(   byte_encoderbyte_decoderreadr2   r5   r#   r$   r    rR   	bpe_rankscacher0   compile
IGNORECASEpat)r>   r   r   rb   rc   rd   re   rf   kwargsrt   Zvocab_handleZmerges_handleZ
bpe_merges	__class__r   r   r?   !  sD    	
*:zCLIPTokenizer.__init__c                 C   s
   t | jS r9   )rR   r}   r>   r   r   r   
vocab_sizeS  s    zCLIPTokenizer.vocab_sizec                 C   s   t | jfi | jS r9   )r#   r}   Zadded_tokens_encoderr   r   r   r   	get_vocabW  s    zCLIPTokenizer.get_vocabN)token_ids_0token_ids_1returnc                 C   s<   | j g}| jg}|du r$|| | S || | | | | S )a  
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
        adding special tokens. A CLIP sequence has the following format:

        - single sequence: `<|startoftext|> X <|endoftext|>`

        Pairs of sequences are not the expected use case, but they will be handled without a separator.

        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.
        N)bos_token_ideos_token_idr>   r   r   rd   re   r   r   r    build_inputs_with_special_tokensZ  s
    z.CLIPTokenizer.build_inputs_with_special_tokensF)r   r   already_has_special_tokensr   c                    sl   |rt  j||ddS |du r8dgdgt|  dg S dgdgt|  dg dg dgt|  dg 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   Nr   r   )rs   get_special_tokens_maskrR   )r>   r   r   r   r   r   r   r   u  s    z%CLIPTokenizer.get_special_tokens_maskc                 C   sP   | j g}| jg}|du r.t|| | dg S t|| | | | | dg S )a  
        Create a mask from the two sequences passed. CLIP 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.
        Nr   )r   r   rR   r   r   r   r   $create_token_type_ids_from_sequences  s
    z2CLIPTokenizer.create_token_type_ids_from_sequencesc           
         s|  | j v r j | S t|d d |d d f }t|}|sF|d S t| fddd}| jvrhqd|\}}g }d}|t|k r:z|||}	W n* ty   |||d   Y q:Y n0 ||||	  |	}|| |kr"|t|d k r"||d  |kr"|	||  |d7 }qx|	||  |d7 }qxt|}|}t|dkrZqdqFt|}qFd	
|}| j |< |S )
NrP   </w>c                    s    j | tdS )Ninf)r   getfloat)pairr   r   r   <lambda>  r   z#CLIPTokenizer.bpe.<locals>.<lambda>keyr   r      r/   )r   ro   r.   minr   rR   index
ValueErrorrE   r"   rG   )
r>   rH   r+   r,   ZbigramfirstsecondZnew_wordrT   jr   r   r   bpe  sB    


2




zCLIPTokenizer.bpec                    s   g } j du r"d j|}nt  | }t j|D ]B}d fdd|	dD }|
dd  |dD  qB|S )zTokenize a string.Nr/   rJ   c                 3   s   | ]} j | V  qd S r9   )r   )r   r'   r   r   r   	<genexpr>  s   z*CLIPTokenizer._tokenize.<locals>.<genexpr>rg   c                 s   s   | ]
}|V  qd S r9   r   )r   Z	bpe_tokenr   r   r   r     r   )ru   rG   ry   rI   r4   rC   r0   findallr   encoderE   r   r5   )r>   r3   
bpe_tokensrH   r   r   r   	_tokenize  s    
"zCLIPTokenizer._tokenizec                 C   s   | j || j | jS )z0Converts a token (str) in an id using the vocab.)r}   r   rc   )r>   rH   r   r   r   _convert_token_to_id  s    z"CLIPTokenizer._convert_token_to_idc                 C   s   | j |S )z=Converts an index (integer) in a token (str) using the vocab.)r   r   )r>   r   r   r   r   _convert_id_to_token  s    z"CLIPTokenizer._convert_id_to_tokenc                    s@   d |}t fdd|D }|jd jddd }|S )z:Converts a sequence of tokens (string) in a single string.rJ   c                    s   g | ]} j | qS r   )r   )r   cr   r   r   r     r   z:CLIPTokenizer.convert_tokens_to_string.<locals>.<listcomp>rg   )rb   r   r/   )rG   	bytearraydecoderb   r]   r2   )r>   r6   r3   Z
byte_arrayr   r   r   convert_tokens_to_string  s    
z&CLIPTokenizer.convert_tokens_to_string)save_directoryfilename_prefixr   c           
   	   C   sP  t j|s td| d S t j||r4|d ndtd  }t j||rV|d ndtd  }t|ddd.}|	t
j| jd	d
ddd  W d    n1 s0    Y  d}t|dddt}|	d t| j dd dD ]B\}}	||	kr
td| |	}|	d|d  |d7 }qW d    n1 s>0    Y  ||fS )Nz*Vocabulary path ({}) should be a directory-rJ   r   r   wrg   rh   r   TF)indent	sort_keysensure_asciirn   r   z#version: 0.2
c                 S   s   | d S )Nr   r   )kvr   r   r   r     r   z/CLIPTokenizer.save_vocabulary.<locals>.<lambda>r   zqSaving vocabulary to {}: BPE merge indices are not consecutive. Please check that the tokenizer is not corrupted!r/   r   )ospathisdirrw   errorformatrG   VOCAB_FILES_NAMESrz   writer{   dumpsr}   sortedr   r~   warning)
r>   r   r   r   Z
merge_filefr   writerr   Ztoken_indexr   r   r   save_vocabulary  s2    <

*zCLIPTokenizer.save_vocabulary)r]   r^   r_   r^   r^   )N)NF)N)N)rX   rY   rZ   r[   r   Zvocab_files_namesPRETRAINED_VOCAB_FILES_MAPZpretrained_vocab_files_map&PRETRAINED_POSITIONAL_EMBEDDINGS_SIZESZmax_model_input_sizesZmodel_input_namesr?   propertyr   r   r   intr   r   boolr   r   r   r   r   r   r   rr   r   r   __classcell__r   r   r   r   r\     sF        2
   *r\   )"r[   r{   r   rK   	functoolsr   typingr   r   r   regexr0   Ztokenization_utilsr   r   r	   r
   r   utilsr   Z
get_loggerrX   rw   r   r   r   ZPRETRAINED_INIT_CONFIGURATIONr(   r.   r4   r7   objectr8   r\   r   r   r   r   <module>   s:   



 