
    -`iz4                        d dl Z d dlZd dlmZ d dlmZmZ d dlmZm	Z	 d dl
mZ d dlmZmZm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mZ d d	lmZ d d
lmZ  ee          Z ed          Z  ed          Z!dej"        j#        j$        j%        dededdfdZ&dej"        j#        j$        j%        dededdfdZ'eded         fd            Z( G d d          Z)dS )    N)abstractmethod)Callable	Generator)contextmanagernullcontext)CodeType)Any	ParamSpecTypeVar)CompilationModeCUDAGraphModeget_current_vllm_config)DynamicShapesType)init_logger)layerwise_nvtx_marker_contextRPselfargskwargsreturnc                     dS )z-No-op to skip the GLOBAL_STATE guard entirelyN r   r   r   s      l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/compilation/wrapper.py_noop_add_global_state_guardr      	     	D    c                     dS )z:No-op to skip the TORCH_FUNCTION_MODE_STACK guard entirelyNr   r   s      r   )_noop_add_torch_function_mode_stack_guardr    "   r   r   NNNc               #     K   t           j        j        j        j        j        } t           j        j        j        j        j        }t           j        j        j        }t           j        j        j	        }	 dt           j        j        _        dt           j        j        _	        t          t           j        j        j        j        _        t          t           j        j        j        j        _        dV  | t           j        j        j        j        _        |t           j        j        j        j        _        |t           j        j        _        |t           j        j        _	        dS # | t           j        j        j        j        _        |t           j        j        j        j        _        |t           j        j        _        |t           j        j        _	        w xY w)a  Context manager for compilation settings and patches.

    This manager:
    1. Sets higher dynamo cache limits for compilation. (Needed for
        qwen2_5_vl see test_qwen2_5_vl_evs_functionality).
        Generally a recompilation can happen whenever we use a new
        backend instance in torch.compile.
    2. Patches out add_global_state_guard to skip GLOBAL_STATE guards
    3. Patches out add_torch_function_mode_stack_guard to skip
        TORCH_FUNCTION_MODE_STACK guards.
    4. Restores everything when compilation completes
    i   i    N)torch_C_dynamoguardsGuardManageradd_global_state_guard#add_torch_function_mode_stack_guardconfigcache_size_limitaccumulated_cache_size_limitr   r    )original_global_state_guard(original_torch_function_mode_stack_guardoriginal_cache_sizeoriginal_accumulated_caches       r   _compilation_contextr1   )   s\       	,C   	,P -  -.?!&!5!RW04-<@9 ) 	,C 6 	,P 	 ( 	,C 5 	,P 1D-<V999 ( 	,C 5 	,P 1D-<V9VVVVs   0A:E A.Gc                       e Zd ZdZdededefdZdeeef         dej	        dej
        defdZdd	Zdededefd
ZdededefdZedededefd            ZdefdZdededdfdZeded         fd            ZdS )TorchCompileWithNoGuardsWrapperaM  
    A wrapper class for torch.compile, it ensures that all guards are dropped
    when CompilationMode is not CompilationMode.STOCK_TORCH_COMPILE.
    When guards are dropped, the first time __call__ is invoked, a single
    compilation is triggered. Dynamo should never be traced again after that
    since we drop all guards.
    r   r   r   c                 Z    t          | d          sJ  | j        |i |  | j        |i |S )N_check_shape_invariants)hasattrr5   forwardr   s      r   check_invariants_and_forwardz<TorchCompileWithNoGuardsWrapper.check_invariants_and_forwardc   sH    t677777$$d5f555t|T,V,,,r   callable_fnc                    | j         rzt          |          }t          |          }t          d                    | j        j                  | ||          5 } ||i ||_        d d d            n# 1 swxY w Y   |j        S  ||i |S )Nz Torch Compiled Module (input):{})	in_tensorr   )layerwise_nvtx_tracing_enabledlistdictr   format	__class____name__result)r   r9   r   r   	args_listkwargs_dictctxs          r   _call_with_optional_nvtx_rangez>TorchCompileWithNoGuardsWrapper._call_with_optional_nvtx_rangei   s     . 
	T

Iv,,K.299$.:QRR#"	   :
 ([$9&99
: : : : : : : : : : : : : : : :{D+F+++s   A00A47A4Nc                    d| _         t                      }|| _        |j        j        }|j        j        | _        |t          d          |j        	                    |          }i }t          |t                    r|dk    r|j        j        }d| _        |j        j        j        | _        |j        j        j        }|t"          j        k    rP| j        rCt&          j        r
J d            t&          j        r|t,          j        k    s
J d            d |d<   nd	 |d<   | j        }|t,          j        k    r.t&          j        r
J d
            | j        r
J d            | j        }t7                      }t&          j        rkt9          t:          j        j        d          r&t:          j        j                             d          }n&d}|dz  }|dz  }tB          "                    |           |5  t;          j#        |dd||          | _$        d d d            n# 1 swxY w Y   t&          j        rB|t"          j        k    r4t:          j        j%        &                    | j'                   d | _(        d S d S d S )NFz)Compilation mode cannot be NO_COMPILATIONinductorTz\compilation_config.dynamic_shapes_config.evaluate_guards requires VLLM_USE_BYTECODE_HOOK=0. zGevaluate_guards for backed shapes requires VLLM_USE_AOT_COMPILE=False. c                     d | D             S )Nc                 $    g | ]}|j         d k    S )	SHAPE_ENV)
guard_type).0entrys     r   
<listcomp>zNTorchCompileWithNoGuardsWrapper.__init__.<locals>.<lambda>.<locals>.<listcomp>   s-     8 8 88=E$38 8 8r   r   xs    r   <lambda>z:TorchCompileWithNoGuardsWrapper.__init__.<locals>.<lambda>   s      8 8AB8 8 8 r   guard_filter_fnc                     d | D             S )Nc                     g | ]}d S )Fr   )rM   _s     r   rO   zNTorchCompileWithNoGuardsWrapper.__init__.<locals>.<lambda>.<locals>.<listcomp>   s    7I7I7I!7I7I7Ir   r   rP   s    r   rR   z:TorchCompileWithNoGuardsWrapper.__init__.<locals>.<lambda>   s    7I7Iq7I7I7I r   z;UNBACKED dynamic shapes requires VLLM_USE_BYTECODE_HOOK=0. z)UNBACKED dynamic shapes do not add guardsenable_aot_compile)rW   z/torch._dynamo.config.enable_aot_compile is not z.available. AOT compile is disabled and please z+upgrade PyTorch version to use AOT compile.)	fullgraphdynamicbackendoptions))compiledr   vllm_configcompilation_configmodeobservability_configenable_layerwise_nvtx_tracingr<   RuntimeErrorinit_backend
isinstancestrinductor_compile_configfirst_compiledynamic_shapes_configevaluate_guardstyper   STOCK_TORCH_COMPILEenvsVLLM_USE_BYTECODE_HOOKVLLM_USE_AOT_COMPILEr   BACKEDr7   UNBACKEDr8   r   r6   r#   r%   r*   patchloggerwarningcompile_compiled_callableconvert_frameregister_bytecode_hookbytecode_hook_compiled_bytecode)	r   r]   r_   rZ   r[   ds_typecompiled_ptraot_contextmsgs	            r   __init__z(TorchCompileWithNoGuardsWrapper.__init__y   s   -//&-2,J 	+ <JKKK0==kJJgs## 	M:(=(=!4LG!*@P 	 0FK?666# J6  : 6
 ,  #&7&>>>>7 ?>>
. .)** .J-I)* L '000 2  M 2 +XX-XXX+<L!mm$ 	$u}+-ABB $#m288D8QQGGGDDs### 	 	&+m' ' 'D#	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 & 	<4?3V+V+VM'>>t?QRRR7;D###	< 	<+V+Vs   (HHHc                     t          | j        d          s t          ddt          j         dz             | j                            ||f          S )Naot_compilezuaot_compile is not supported by the current configuration. Please make sure torch.compile is enabled with the latest z)version of PyTorch (current using torch: ))r6   ru   rb   r#   __version__r   r   s      r   r   z+TorchCompileWithNoGuardsWrapper.aot_compile   sc    t.>> 	ORe>ORRRS  
 &22D&>BBBr   c                    t           j        r| j        j        j        t
          j        k    r | j        |i |S | j        sLt          j
        j                            |                                             | j        | j        g|R i |S |                                 5   | j        | j        g|R i |cd d d            S # 1 swxY w Y   d S | j        s| j        st'                      nt          j                            d          }d| _        t-                      5  |5   | j        | j        g|R i |cd d d            cd d d            S # 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nfail_on_recompileF)rl   rm   r]   r^   r_   r   rk   ru   ry   r#   r%   
eval_frameremove_from_cacheoriginal_code_objectrF   _dispatch_to_compiled_coder7   rg   ri   r   compiler
set_stancer1   )r   r   r   rE   s       r   __call__z(TorchCompileWithNoGuardsWrapper.__call__   s   & 	 38"67 7 /t.????*  (::4;T;T;V;VWWW:t:+.2  6<   4466  >4>'+  /5                   %D-1-AD^../BCC 
 "'D%''    :t:+.2  6<                                         sH    CC
C E*#E9E*E	E*E	E**E.1E.c                     d S )Nr   r   s      r   r7   z'TorchCompileWithNoGuardsWrapper.forward   s    9<r   c                 $    | j         j        j        S )z6Return the original code object of the forward method.)r@   r7   __code__)r   s    r   r   z4TorchCompileWithNoGuardsWrapper.original_code_object   s    ~%..r   old_codenew_codec                    ||                                  urdS t          j                    }|rd|j        r]|j        }|j        j        }|j        j                            t          j	        j
                  d         }|dk    r|dk    rn	|r|j        ]|j        d         }|j        |k    sJ |j        d         | urdS || _        | j                                        }|r|dz  }|                                s	 ddl}|                    |          }	t%          |d	          5 }
|
                    |	           ddd           n# 1 swxY w Y   t(                              d
|           n# t,          $ r Y nw xY w| j        j        j        t2          j        k    r6d|j        v r/ddl}|                    |          }	d|	 }t9          |          dS dS )z8Hook to save the compiled bytecode for direct execution.N_compilezconvert_frame.pyframer   ztransformed_code.pyr   wz#Dynamo transformed code saved to %supdateaB  Assigning / modifying buffers of nn.Module during forward pass is not allowed when using cudagraph inside the compiler because it will cause silent errors. Please use eager mode or fix the code. The following code contains clues about which buffer is being modified (please search for the usage of the function `update`):
)r   sys	_getframef_backf_codeco_nameco_filenamesplitospathsepf_localsry   r]   compile_debug_dump_pathexistsdepyf	decompileopenwriterr   debug	Exceptionr^   cudagraph_moder   NONEco_namesrb   )r   r   r   r   	code_name	file_namer   decompiled_filer   srcfr}   s               r   rx   z-TorchCompileWithNoGuardsWrapper.bytecode_hook   s]   4446666F 	 	LE,I066rw{CCBGIJ&&98J+J+J  	 	 w'|x''''>&!--F"*7799 	"%::O"))++ 
 !LLL//(33Cos33 %q% % % % % % % % % % % % % % % LL!FXXXX    D />-BTTTH---LLL//(++CR MP	R R  s### UT--s6   9)E+ "E8E+ EE+ EE+ +
E87E8r!   c              #      K   |                                  }| j        J | j        | j        j        _        	 dV  || j        j        _        dS # || j        j        _        w xY w)a+  
        Context manager to dispatch to internally compiled code for torch<2.8.
        Why does this work? Because Dynamo guarantees that the compiled
        bytecode has exactly the same arguments, cell variables, and free
        variables as the original code. Therefore we can directly switch
        the code object in the function and call it.

        See https://dev-discuss.pytorch.org/t/what-is-the-relationship-requirement-among-original-bytecode-transformed-bytecode-and-bytecode-returned-by-hooks-in-dynamo/1693/7 for more details.
        N)r   ry   r@   r7   r   )r   originals     r   r   z:TorchCompileWithNoGuardsWrapper._dispatch_to_compiled_code7  sr       ,,..&222*.*A'	7EEE.6DN"+++hDN"+6666s   A A!)r   N)rA   
__module____qualname____doc__r	   r8   r   r   r   r   r   rF   r~   r   r   r   r7   r   r   rx   r   r   r   r   r   r   r3   r3   Z   s        -# - - - - - -,#AqD>,23&,DEH,	, , , , T< T< T< T<lC C C C C C Cc S S    @ <S<C<C<<< ^</h / / / /6$h 6$( 6$t 6$ 6$ 6$ 6$p 7I6F,G 7 7 7 ^7 7 7r   r3   )*r   r   abcr   collections.abcr   r   
contextlibr   r   typesr   typingr	   r
   r   r#   torch._C._dynamo.guards	vllm.envsrl   vllm.configr   r   r   vllm.config.compilationr   vllm.loggerr   vllm.utils.nvtx_pytorch_hooksr   rA   rr   r   r   r$   r%   r&   r'   r   r    r1   r3   r   r   r   <module>r      s   
			 



       / / / / / / / / 2 2 2 2 2 2 2 2       * * * * * * * * * *            O O O O O O O O O O 5 5 5 5 5 5 # # # # # # G G G G G G	X		GCLLIcNN	
(

!
.	7:	FI			 	 	 		
(

!
.	7:	FI			 	 	 	 -Wi(89 -W -W -W -W`o7 o7 o7 o7 o7 o7 o7 o7 o7 o7r   