
    .`ibA                    B   d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dl	Z
d dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZ er!d dlZd dlmZ d dlm Z  d dl!Z"d d	l#m$Z$ d d
l%m&Z& nT ed e'            d          Z" ed e'            d          Z ed e'            d          Z  ed e'            d          Z ee(          Z)dZ*d4dZ+ G d d          Z,d5d!Z-d" Z. ej/        d#          Z0 ej/        d$          Z1d6d*Z2d7d,Z3d8d/Z4d9d0Z5d:d3Z6dS );    )annotationsN)TYPE_CHECKING)LRUCache)Cache)init_logger)
LazyLoader)GrammarOutputSchedulerOutput)TokenizerLike)
InputBatchxgrxgrammarocoutlines_core
file_utilsztransformers.file_utilsconvert_slow_tokenizerz#transformers.convert_slow_tokenizerscheduler_outputr
   grammar_outputr	   input_batchr   logitstorch.TensorreturnNonec                   |j         }i }d}| j        }t          |j                  }t	          |j                  D ]9\  }	}
|	|z   }|t          |                    |
d                    z  }|
|v r|||
<   :g }t          j	        |j
        d         |j
        d         fd|j                  }d}|j        D ]}
t          |                    |
d                    }|                    |
          x}=t          d|z             D ]*}||z   }|||z            ||<   |                    |           +|d|z   z  }t          j        |                              |j        d          }t          |          |j
        d         k    }d}|s>t          j        |t          j        d	d
          }|                    |j        d          }t)          j        |||           dS )a-  
    Apply grammar bitmask to output logits of the model with xgrammar function.

    Args:
        scheduler_output (SchedulerOutput): The result of engine scheduling.
        input_batch (InputBatch): The input of model runner.
        logits (torch.Tensor): The output logits of model forward.
    r       )shape
fill_valuedtypeNT)non_blockingcpu)r    device
pin_memory)indices)grammar_bitmaskscheduled_spec_decode_tokenssetstructured_output_request_ids	enumeratereq_idslengetnpfullr   r    rangeappendtorch
from_numpytor#   tensorint32r   apply_token_bitmask_inplace)r   r   r   r   r&   struct_out_req_batch_indicescumulative_offsetspec_tokensstruct_out_req_idsbatch_indexreq_idlogit_indexout_indicessorted_bitmaskcumulative_indexnum_spec_tokens	logit_idxibitmask_indexskip_out_indicesindex_tensors                        s/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/v1/structured_output/utils.pyapply_grammar_bitmaskrI   ,   s6     %4O 46 "?K^IJJ()<== ? ?V!$55S!<!<==='''3>(0K W|A 5a 89#  N
  > 0 0koofb99::599&AAAIN1.// 2 2 )A0?@PST@T0U}-""=1111A// &~6699D :  O ;''6<?:L I |u{5T
 
 
 $v}4HH#FO\RRRRRR    c                      e Zd ZdZddZdS )	OutlinesVocabularyzo
    Wrapper class for `outlines_core.Vocabulary`,
    which allows us to store a hash with the vocabulary
    
vocabularyoc.Vocabularyr   r   c                    || _         t          j        |                                                    d                                                    }t          |d          }|| _        d S )Nutf-8   )innerhashlibsha256__repr__encode	hexdigestint_hash)selfrM   hex_strhash_ints       rH   __init__zOutlinesVocabulary.__init__   sZ    
 .!4!4!6!6!=!=g!F!FGGQQSSw##


rJ   N)rM   rN   r   r   )__name__
__module____qualname____doc__r]   r   rJ   rH   rL   rL   z   s2         
     rJ   rL   strc                    t          j        d          } t          j        d          }t           j                            d          }| r| S |r!t           j                            |dd          S t           j                            |          r'|dk    r!t           j                            |dd          S t          j                    }t           j                            |dd          S )zFGet the context object that contains previously-computed return valuesOUTLINES_CACHE_DIRXDG_CACHE_HOME~z.cacheoutlines/)osgetenvpath
expanduserjoinisdirtempfile
gettempdir)outlines_cache_dirxdg_cache_homehome_dirtempdirs       rH   get_outlines_cache_pathru      s    #788Y/00Nw!!#&&H "!! Bw||NHjAAA
 
w}}X <8s??w||Hh
;;; !##G7<<:666rJ   c                 |   t                      } t          j        rt                              d           t          | dd          }t          j                            d          }|	                    dd          }||k    r|
                                 |                    d|           |S t          d	          S )
z3Get the Cache instance to be used for index cachingzEnabling outlines cache. This is an unbounded on-disk cache. It may consume a lot of disk space and should not be used with untrusted clients.noner   )eviction_policy
cull_limitr   __version__N   )maxsize)ru   envsVLLM_V1_USE_OUTLINES_CACHEloggerwarningr   	importlibmetadataversionr-   clearr(   r   )	cache_dircacheoutlines_versioncached_versions       rH   get_outlines_cacher      s     ())I& 2	
 	
 	

 iAFFF$-55oFF=$77---KKMMM		-!1222C    rJ   z^<0x[0-9A-F]{2}>$u   ^.{0,6}�+.{0,6}$	tokenizerr   eos_token_idrX   dict[bytes, list[int]]c           	     `   
 d t          j                                                    D             
d fd}i }g }                                                                 D ]J\  }}| j        v r ||          }|rt          |t          t          f          rt          |          }n|dk    r|dk    sd|v rt          	                    |          s{t          	                    |          r't          t          |dd         d	          g          }nO
fd
|D             }	d|	v rt          d| d| d|           t          |	          }n|                    d          }||k    r)|                    |g                               |           5|                    |           L|S )zCreate a map from vocabulary tokens to lists of equivalent token ids.

    Returns:
        A Dict of token string -> equivalent token ids
    c                    i | ]\  }}||	S r   r   ).0kvs      rH   
<dictcomp>z'_reduced_vocabulary.<locals>.<dictcomp>   s+       A1  rJ   tokenrb   r   c                                         | g          }t          |           t          u r|                     t          j                  s| dk    rd|z   S |S )Nz<0x20> )convert_tokens_to_stringtyperb   
startswithr   SPIECE_UNDERLINE)r   stringr   s     rH   convert_token_to_stringz4_reduced_vocabulary.<locals>.convert_token_to_string   s^    33UG<< KK3  !<==   <rJ   u   �      rQ   c                :    g | ]}                     |          S r   )r-   )r   cunicode_to_bytess     rH   
<listcomp>z'_reduced_vocabulary.<locals>.<listcomp>   s(     H H HQ!1!5!5a!8!8 H H HrJ   NzCannot convert token `z` (z) to bytes: rP   )r   rb   r   rb   )r   bytes_to_unicodeitems	get_vocaball_special_tokens
isinstancebytes	bytearrayre_replacement_seqmatchre_llama_byte_tokenrX   RuntimeErrorrV   
setdefaultr1   )r   r   r   rM   empty_token_idsr   	token_idx	token_strtoken_bytes	byte_valsr   s   `         @rH   _reduced_vocabularyr      s+    /@BBHHJJ        *,J!#O%//117799 %. %.yI000++E22	  	.%%!344 8
 $I..x''EX,=,=I%%.@.F.Fy.Q.Q% ',,U33 3"'U1Q3Z)<)<(=">">KK !I H H H% H H HIy((*DU D D!*D D8AD D   #(	"2"2KK'..w77L((%%k266==iHHH""9----rJ   rN   c                   t          | d          r| j        S 	 t          | d          r| j        | j        }n t          dt	          |            d          t          | |          }t          t          j        ||                    }|| _        |S # t          $ r&}t          dt	          |            d          |d}~ww xY w)z2Get the `Vocabulary` object for a given tokenizer._outlines_vocabularyr   Nz?Error during structured outputs setup for outlines: Tokenizer (zi) has no `eos_token_id` property, but `eos_token_id` is required for structured outputs to work properly.z,Cannot get the vocabulary of the tokenizer (z0). The tokenizer should have a get_vocab method.)
hasattrr   r   
ValueErrorr   r   rL   r   
VocabularyAttributeError)r   r   reduced_vocabrM   es        rH   get_outlines_vocabularyr     s   y011 .--9n-- 	)2H2T$1LLVOOV V V   ,
 
 (lM(R(RSS
)3	&    Y     
 
 		s   A9B 
C!B>>Cgrammar_strboolc                    | rt          | t                    sdS |                     d          D ]4}t          j        dd|                                          }|s-d|v r dS 5dS )aV  
    Check if grammar appears to use Lark syntax.

    Args:
        grammar_str: Input grammar string

    Returns:
        bool: True if grammar appears to be in Lark format, False otherwise

    Examples:
        >>> grammar_is_likely_lark("rule: 'abc'")
        True
        >>> grammar_is_likely_lark("rule ::= 'abc'")
        False
    F
	(#|//).*$ z::=T)r   rb   splitresubstrip)r   lines     rH   grammar_is_likely_larkr   !  s       jc:: u!!$''  vlB--3355 	 D==55  4rJ   c           
        t          | t                    st          dt          |                      |                                 st          d          t                      }t                      }g }d*dd+d}d,d}fd|                     d          D             }d}t          |d          D ]\  }}	|	r|	                    d          rd|	v r	 |	                    dd          d                                                             d          }
|	                    |
           ||
}|
dk    rd}# t          $ r}t          d| d          |d}~ww xY w|st          d          |                    d|            d}g }t          |d          D ]\  }}	|	s		 d|	v r|	                    d          s|r-|                    | dd                    |                      |	                    dd          \  }
}|
                                                    d          } ||d| d|           t          j        d d!|          }|                     ||                     |                                g}n|	                    d          r|st          d"| d#          |	dd                                         } ||d$| d|           t          j        d d!|          }|                     ||                     |                    |           # t          $ r(}t          d%| d&t          |                     |d}~ww xY w|r-|                    | dd                    |                      ||z
  d'hz
  }|r2t          d(d)                    t!          |                               d                    |          S )-a  
    Convert a Lark grammar string to EBNF format.

    EBNF reference:
    https://github.com/ggerganov/llama.cpp/blob/master/grammars/README.md
    Lark grammar reference:
    https://lark-parser.readthedocs.io/en/latest/grammar.html

    Args:
        grammar_str: Input grammar in Lark format

    Returns:
        str: Converted grammar in EBNF format

    Examples:
        >>> print(convert_lark_to_ebnf("rule: 'hello'"))
        root ::= rule
        rule ::= "hello"
    zGrammar must be a string, got zGrammar string cannot be emptyr   rb   r   c                R    t          j        dd|                                           S )z)Remove comments and whitespace from line.r   r   )r   r   r   )r   s    rH   
clean_linez(convert_lark_to_ebnf.<locals>.clean_line^  s"    vlB--33555rJ   text	rule_nameline_numrX   r   c                    |                      d          dz  dk    s|                      d          dz  dk    rt          d| d|           dS )z Validate quote matching in text.'   r   "zMismatched quotes in z	 on line N)countr   )r   r   r   s      rH   check_quotesz*convert_lark_to_ebnf.<locals>.check_quotesb  s]    ::c??Q!##tzz#':a'?'?SYSSSSTTT (@'?rJ   set[str]c                    t          j        dd|           } t          j        dd|           } t          t          j        d|                     S )z"Extract rule references from text.z"[^"]*"r   z[+*?()|\[\]{}]r   z\b[a-zA-Z_][a-zA-Z0-9_]*\b)r   r   r(   findall)r   s    rH   extract_referencesz0convert_lark_to_ebnf.<locals>.extract_referencesg  sF     vj"d++v'd332:;TBBCCCrJ   c                &    g | ]} |          S r   r   )r   r   r   s     rH   r   z(convert_lark_to_ebnf.<locals>.<listcomp>o  s#    BBB$ZZBBBrJ   r   Nr   |:r   ?startzInvalid rule format on line z". Expected 'rule_name: definition'zNo valid rules found in grammar	root ::= z ::=  | zrule 'r   z	'([^']*)'z"\1"zAlternative '|' on line z$ without a preceding rule definitionzalternative for rule 'zError on line z: rootz"Referenced rules are not defined: z, )r   rb   r   rb   )r   rb   r   rb   r   rX   r   r   )r   rb   r   r   )r   rb   r   r   r   r(   r   r*   r   add
IndexErrorr1   rm   r   r   updatesorted)r   defined_rulesreferenced_rulesoutput_linesr   r   lines
first_ruler   r   namer   current_rulecurrent_definition
definitionalt_defundefined_rulesr   s                    @rH   convert_lark_to_ebnfr   A  s   ( k3'' OM${:K:KMMNNN ;9:::EEMuuL6 6 6 6U U U U
D D D D CBBB+*;*;D*A*ABBBEJ#E1--  $ 	ts++ 	$;;zz#q))!,2244::3??!!$'''%!%J7??!(J    78 7 7 7     <:;;; 0J00111 L#E1-- %K %K$ 	!	Kd{{4??3#7#7{  '''NNejj9K.L.LNN  
 $(::c1#5#5 j#zz||11#66Z)A,)A)A)A8LLLVL':FF
 ''(:(::(F(FGGG&0&6&6&8&8%9""%% 3# $>8 > > >  
 qrr(..**ElEEEx   &w@@ ''(:(:7(C(CDDD"))'222 	K 	K 	KBhBB#a&&BBCCJ	K  T|RR%**=O2P2PRRSSS '6&AO 
U6/;R;R1S1SUU
 
 	
 99\"""s2   A"D;;
EEE)FL..
M 8#MM choice	list[str]c                p    d	dfd| D             }dd                     d |D                       z   }|S )
Nsrb   r   c                .    t          j        dd|           S )z+Escape special characters in a EBNF string.z(["\\])z\\\1)r   r   )r   s    rH   escape_ebnf_stringz-choice_as_grammar.<locals>.escape_ebnf_string  s     vj'1---rJ   c              3  .   K   | ]} |          V  d S )Nr   )r   r   r   s     rH   	<genexpr>z$choice_as_grammar.<locals>.<genexpr>  s/      ==))!,,======rJ   r   r   c              3  "   K   | ]
}d | d V  dS )r   Nr   )r   r   s     rH   r   z$choice_as_grammar.<locals>.<genexpr>  s*      &I&IAx1xxx&I&I&I&I&I&IrJ   )r   rb   r   rb   )rm   )r   escaped_choicesgrammarr   s      @rH   choice_as_grammarr     s\    . . . .
 >===f===OEJJ&I&I&I&I&IIIIGNrJ   )
r   r
   r   r	   r   r   r   r   r   r   )r   rb   )r   r   r   rX   r   r   )r   r   r   rN   )r   rb   r   r   )r   rb   r   rb   )r   r   r   rb   )7
__future__r   rS   importlib.metadatar   ri   ro   typingr   numpyr.   regexr   r2   
cachetoolsr   	diskcacher   	vllm.envsr}   vllm.loggerr   vllm.utils.import_utilsr   vllm.v1.core.sched.outputr	   r
   r   r   #transformers.convert_slow_tokenizerr   transformers.file_utilsr   r   r   vllm.tokenizersr   vllm.v1.worker.gpu_input_batchr   globalsr^   r   CACHErI   rL   ru   r   compiler   r   r   r   r   r   r   r   rJ   rH   <module>r     s   # " " " " "      				                                         # # # # # # . . . . . . D D D D D D D D HHHHHH000000------9999999
*UGGIIz
2
2C	D''))_	5	5BL''))5NOOJ'Z ''))-R 
 
X		KS KS KS KS\        7 7 7 70! ! !, !bj!566 RZ 566 C C C CL   >   @# # # #D     rJ   