
    -`i-                        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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 d d
lmZ  ee          Ze j         G d d                      Z G d d          ZdS )    N)Callable)PicklerAny)CachingAutotuner)VllmBackend)end_monitoring_torch_compile)
VllmConfig)Range)init_loggerc                   F    e Zd ZU eed<   dZeed<   dZede	f         ed<   dS )
RangeEntrycompile_rangeFcompiledN.runnable)
__name__
__module____qualname__r   __annotations__r   boolr   r   r        v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/compilation/piecewise_backend.pyr   r      sG         Hd#'HhsCx '''''r   r   c                   F   e Zd Z	 ddej        dz  dedededee         dede	d	e
eed
ef         f         dz  fdZded
ef         ded
ef         fdZddZde
eef         fdZdeed
f         dee         fdZdedeed
f         defdZdededz  fdZdedefdZdS )PiecewiseBackendNgraphvllm_configpiecewise_compile_indextotal_piecewise_compilessym_shape_indicesvllm_backendreturns_tuplecompiled_runnables.c	                    t          |du          t          |du          z  s
J d            || _        || _        |j        | _        || _        || _        || _        || _        |dk    | _        ||dz
  k    | _	        |dk    | _
        |j        | _        | j                                        | _        | j        rDd}	| j        d         }
|
j        |j        j        k    sJ t%          |
j        |	          | j        d<   d| j         }t(                              |           | j        j        | _        d	| j         }t(                              |           || _        || _        i | _        t5          | j                  | _        | j        | j        D ]}t9          |t:                    r|d
k    sJ t=          d          t9          |t>                    sJ t%          ||          }|| j        vr2tA          |          | j        |<   | j        !                    |           | j        D ]}tA          |          | j        |<   ddl"m#} |$                                | _%        dS )ad  
        The backend for piecewise compilation.
        It mainly handles the compilation of static shapes and
        dispatching based on runtime shape.

        We will compile `self.graph` once for the general shape,
        and then compile for different shapes specified in
        `compilation_config.compile_sizes`.

        This class supports two mutually exclusive modes:
        1. Compilation (graph is set, compiled_runnables is None):
           Used during initial compilation when we have the FX graph
           and need to compile it for each shape range.
        2. Precompilation (graph is None, compiled_runnables is set):
           Used when loading from cache/AOT artifacts where we already
           have pre-compiled callables and don't need the original graph.

        Exactly one of graph or compiled_runnables must be provided.
        Nz:exactly one of graph and compiled_runnables should be set.r      istartendz"PiecewiseBackend: compile_ranges: z!PiecewiseBackend: compile_sizes: cudagraph_capture_sizeszmcudagraph_capture_sizes not supported in compile_sizes.This should be handled in `post_init_cudagraph_sizes`.)r   )!_on_compilation_complete_callback)&r   r   r   compilation_configr   r   r!   r#   is_first_graphis_last_graphis_full_graph
is_encoderis_encoder_compilationget_compile_rangescompile_rangesr)   scheduler_configmax_num_batched_tokensr   r(   logger
debug_oncecompile_sizesr    r"   range_entriessetto_be_compiled_ranges
isinstancestrNotImplementedErrorintr   addvllm.compilation.backendsr+   geton_compilation_complete)selfr   r   r   r   r    r!   r"   r#   	max_int32last_compile_range
log_stringsizeranger+   s                  r   __init__zPiecewiseBackend.__init__    s   < E%&&.@.L)M)MM 	
 	
H	
 	
M 
&"-"@'>$(@%("45:48PST8TT5:&2&=#"5HHJJ& 	 "I!%!4R!8"&/FG G G G ',(.I' ' 'D# P$:MOO
*%%%!4BM9KMM
*%%%!2* 79 25T5H1I1I" )* > >dC(( >#<<<<<-Q  
 &dC00000!$777ED$7774>*/5 5 5*51 266u===( 	 	E(2#) ) )Du%% 	POOOOO'H'L'L'N'N$$$r   compiled_graphreturnc                 4     dt           dt           f fd}|S )NargsrL   c                  h     |  }j         st          |t          t          f          s|S |d         S )Nr   )r"   r<   tuplelist)rN   graph_outputrK   rD   s     r   compiled_graph_wrapperzKPiecewiseBackend.get_compiled_graph_wrapper.<locals>.compiled_graph_wrapper   sA    )>40L ! 'L5$-)P)P '###A&r   r   )rD   rK   rS   s   `` r   get_compiled_graph_wrapperz+PiecewiseBackend.get_compiled_graph_wrapper   s=    	'# 	'# 	' 	' 	' 	' 	' 	' 	' &%r   c                     | j         rV| j        sQ| j        j                                         t          | j                   | j        |                                  d S d S d S d S N)r.   r;   r!   compiler_managersave_to_filer	   r   rC   )rD   s    r   check_for_ending_compilationz-PiecewiseBackend.check_for_ending_compilation   s     	/d&@ 	/ .;;===()9:::+7,,.....	/ 	/ 	/ 	/ 87r   c                 b    G d dt                     dt          dt          f         dt          ffd}i }| j                                        D ]]\  }}|j        st                              d|           (t          |j
        d          r  ||j
                  |t          |          <   ^|S )	Nc                       e Zd ZdedefdZdS )EPiecewiseBackend.to_bytes.<locals>.StandaloneCompiledArtifactsPicklerobjrL   c                     t          |t                    r5|                                 t          j        t          j        |          ffS t          S rV   )r<   r   prepare_for_picklepickleloadsdumpsNotImplemented)rD   r]   s     r   reducer_overridezVPiecewiseBackend.to_bytes.<locals>.StandaloneCompiledArtifactsPickler.reducer_override   sU    c#344 **,,,!< *  
 &%r   N)r   r   r   objectr   rd   r   r   r   "StandaloneCompiledArtifactsPicklerr\      s6        &F &s & & & & & &r   rf   fn.rL   c                 h   t          | d          s
J d            t          j        j                            dd          5  |                                 }t          j                    } |                              |           |	                                }d d d            n# 1 swxY w Y   |S )N	serializezfn must have serialize methodbundled_autograd_cacheT)
hasattrtorch
_functorchconfigpatchri   ioBytesIOdumpgetvalue)rg   entryfresultrf   s       r   ri   z,PiecewiseBackend.to_bytes.<locals>.serialize   s    2{++LL-LLL+!(../GNN & &JLL22155::5AAA& & & & & & & & & & & & & & & Ms   AB''B+.B+z9entry with range %s not compiled, so cannot get its bytesri   )r   r   r   bytesr9   itemsr   r6   debugrk   r   r=   )rD   ri   out	range_keyrt   rf   s        @r   to_byteszPiecewiseBackend.to_bytes   s    		& 		& 		& 		& 		& 		& 		& 		&	(38, 	 	 	 	 	 	 	  $ 2 8 8 : : 	@ 	@Iu> O   u~{33 @&/i&?&?C	NN#
r   rN   c                     g }| j         J | j         j         j        D ].}|j        dk    r!|                    |j        d                    . t          |          t          |          k    sJ |S )Nplaceholderexample_value)r   nodesopappendmetalen)rD   rN   fake_example_inputsnodes       r   _fakify_argszPiecewiseBackend._fakify_args   s     !z%%%J$* 	 	Dw-''#**49_+EFFFF&''3t994444""r   range_entryc           
         |j         sC| j        8|                     | j        t          |j                                     |_        n|j                                        s|                     |          nt          |          }t          j
        j                            dd          5  | j        j                            | j        || j        j        | j        |j        | j        | j                  |_        d d d            n# 1 swxY w Y   d|_         | j                            |j                   |                                  d S d S )Nrj   T)r   graph_index
num_graphs)r   r#   rT   r=   r   r   is_single_sizer   rQ   rl   rm   rn   ro   r!   rW   compiler   inductor_configr,   r   r   r;   removerY   )rD   r   rN   	args_lists       r   _maybe_compile_for_range_entryz/PiecewiseBackend._maybe_compile_for_range_entry   s    #  	0&2'+'F'F+C0I,J,JK( ($$ '4CCEE$D%%d+++d  $+112JDQQ  ,0+<+M+U+U
!)9/&1&?$($@#'#@ ,V , ,K(               $(K &--k.GHHH--/////A 	0  	0s   *ADD	D	runtime_shapec                     | j         d S || j         v r| j        t          ||                   S | j        D ]}||v r| j        |         c S d S )Nr'   )r8   r9   r   r3   )rD   r   rI   s      r   _find_range_for_shapez&PiecewiseBackend._find_range_for_shape  sv     %4D...%e-]&S&S&STT, 5 5 E))-e4444 *tr   c                     || j         d                  }|                     |          }|J d| d| j                     |                     ||            |j        | S )Nr   zShape: z out of considered ranges: )r    r   r3   r   r   )rD   rN   r   r   s       r   __call__zPiecewiseBackend.__call__  sy    T3A6700??&&UmUU@SUU '&& 	++K>>>#{#T**r   rV   )rL   N)r   r   r   fxGraphModuler
   r?   rQ   r   r   dictr=   r   r   rJ   rT   rY   rw   r|   rP   r   r   r   r   r   r   r   r   r   r      s        DHkO kO~$kO  kO "%	kO
 #&kO  9kO "kO kO !hsCx&8!89D@kO kO kO kOZ&&sCx0&	#s(	& & & &/ / / /"$sEz* " " " "H#sCx #T#Y # # # #,#0%#0-238_#0	#0 #0 #0 #0J3 :;L    	+c 	+c 	+ 	+ 	+ 	+ 	+ 	+r   r   )dataclassesrp   r`   collections.abcr   r   typingr   torch._functorch.configrl   torch.fxr   )torch._inductor.runtime.triton_heuristicsr   rA   r   vllm.compilation.monitorr	   vllm.configr
   vllm.config.utilsr   vllm.loggerr   r   r6   	dataclassr   r   r   r   r   <module>r      sh       				  $ $ $ $ $ $                       F F F F F F 1 1 1 1 1 1 A A A A A A " " " " " " # # # # # # # # # # # #	X		 ( ( ( ( ( ( ( (~+ ~+ ~+ ~+ ~+ ~+ ~+ ~+ ~+ ~+r   