
    -`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Zd dlmZ d dlmZmZ d dlmZ d dlmZ d	d
lmZ  ee          Ze G d d                      Z G d de          Z G d de          Z G d de          ZdS )    N)Callable)	dataclass)ClassVar)lazy_format_graph_code)PatternMatcherPassPatternPrettyPrinter)
VllmConfig)init_logger   )InductorPassc                   >    e Zd ZU dZee         dz  ed<   dZeed<   dS )InductorCompilationConfigNsplitting_opsFuse_inductor_graph_partition)	__name__
__module____qualname__r   liststr__annotations__r   bool     w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/compilation/vllm_inductor_pass.pyr   r      s<         &*M49t#***). $.....r   r   c                       e Zd ZU dZdZeedz           ed<   	 defdZ	e
ded ej        j        gdf         ded ej        j        gdf         fd            Zd	ej        j        d
eddfdZddZddZdS )VllmInductorPasszr
    An inductor pass with access to vLLM PassConfig.
    It provides timing, logging, and dumping utilities.
    Ndump_prefixconfigc                    t          |j        j        |j        j                  | _        |j        j        | _        |j        r|j        j        nd | _        |j        r|j        j	        nd | _	        | j
        j        | _        d S )N)r   r   )r   compilation_configr   r   pass_configmodel_configdtypemodel_dtypedevice_configdevice	__class__r   	pass_name)selfr   s     r   __init__zVllmInductorPass.__init__&   s     #< 3A)/)B)_#
 #
 #
 "4@8>8KU6.44QU+1+?IF ''T 	 0r   call_fnreturnc                 |     t          j                   dt          dt          j        j        dd f fd            }|S )Nr)   graphr,   c                     |                                   |                     |d            | |           |                     |d           |                                  d S )Nbeforeafter)begin
dump_graphend_and_log)r)   r.   r+   s     r   wrappedz.VllmInductorPass.time_and_log.<locals>.wrapped8   sb    JJLLLOOE8,,,GD%   OOE7+++r   )	functoolswrapsr   torchfxGraph)r+   r5   s   ` r   time_and_logzVllmInductorPass.time_and_log4   sY     
	!	!	* 	58> 	d 	 	 	 	 	 
"	!	 r   r.   stagec                 v    t           j        }|dnd| }t          d| d| j         d| |j                   d S )N .	post_grad)r   r   r   r(   owning_module)r)   r.   r<   ii_strs        r   r3   zVllmInductorPass.dump_graphB   s[    (iWWW8888888%:M	
 	
 	
 	
 	
r   c                 6    t          j                    | _        d S N)timeperf_counter_ns_start_time)r)   s    r   r2   zVllmInductorPass.beginI   s    /11r   c                     t          j                    | _        t          | j        | j        z
            dz  }t
                              d| j        |           d S )Ng    .Az%s completed in %.1f ms)rF   rG   	_end_timefloatrH   loggerdebugr(   )r)   duration_mss     r   r4   zVllmInductorPass.end_and_logL   sM    -//DNT-==>>F.LLLLLr   )r,   N)r   r   r   __doc__r   r   intr   r	   r*   staticmethodr   r8   r9   r:   r;   r   r3   r2   r4   r   r   r   r   r      s         
 )-K#*%,,,;1z 1 1 1 1 -ux~>DE	%ux~6<	=   \
 
s 
t 
 
 
 
2 2 2 2M M M M M Mr   r   c                       e Zd ZU dZdZeed<   	  ej        d          Z	e
ej                 ed<   dedefdZd	ed
eddfdZdS )VllmPatternMatcherPassa  
    A VllmInductorPass that uses the Inductor pattern matcher.
    Its main use is providing the dump_patterns utility that dumps the
    Inductor pattern matcher patterns into a file, which greatly aids debugging.

    TODO(luka) move more utilities to this pass.
    r   matched_countz0<OpOverload\(op='([^']*)', overload='([^']*)'\)>_OP_OVERLOAD_PATTERNstringr,   c                 T    t          | j                            d |                    S )z6Replace <OpOverload(..., ...)> with nicer formulationsc                 ^    d|                      d           d|                      d           S )Nz
torch.ops.r   r?      )group)ms    r   <lambda>z>VllmPatternMatcherPass._replace_op_overloads.<locals>.<lambda>f   s+    @qwwqzz@@AGGAJJ@@ r   )r   rU   sub)r)   rV   s     r   _replace_op_overloadsz,VllmPatternMatcherPass._replace_op_overloadsb   s3    %))@@ 
 
 	
r   r   pm_passNc                     |                                 sdS                     dd           ddlm}  | fd          }|                    d          5 }t          d j         d	|
           |j                                        D ]8\  }}|d         t          j
        k    rdt          |d                    d}nt          |          }                     |          }t          d| |
           t          |          D ]\  }	}
t                      j                            dd                               |
j                  }d                    d|	 dgfdj        D             z   d| gz                                 dd          }                     |          }t          | d|
           :	 ddd           dS # 1 swxY w Y   dS )a  
        If debug dumping is enabled, dump the Inductor pattern-matcher patterns
        into the debug_dump_path folder next to the dumped fx graphs.

        This method does its best to print something that looks like Python code
        for easier debugging and potentially navigation. If any errors appear in
        the output, please add to this method.

        TODO(luka): use pattern object to manually produce pattern graph
        NT)parentsexist_okr   )unique_filepathc                 &    dj          d|  dz  S )Nz	patterns.r?   z.py)r(   )rB   debug_dump_pathr)   s    r   r\   z6VllmPatternMatcherPass.dump_patterns.<locals>.<lambda>   s"    o(KDN(K(KQ(K(K(KK r   wzE# This file was produced by VllmPatternMatcherPass.dump_patterns for a  .
# It does its best to produce valid-Python-looking code but please add to dump_patterns if there are any errors.

from torch._higher_order_ops.auto_functionalize import auto_functionalized as auto_functionalized
from torch._inductor.pattern_matcher import *
vllm = torch.ops.vllm)filer   (z, operator.getitem)z

# Patterns for op: auto_functionalized
zdef pattern_z():c                 J    g | ]}j         |          d j        |           S )z = )memoized_objs_namesmemoized_objs_pp).0keypps     r   
<listcomp>z8VllmPatternMatcherPass.dump_patterns.<locals>.<listcomp>   sR        !$  "5c: : :!237: :  r   zreturn z
    )compile_debug_dump_pathmkdirvllm.utils.system_utilsrc   openprintr(   patternsitemsoperatorgetitemreprr^   	enumerater   	namespacecreate_namepretty_printpatternjoinrl   replace)r)   r   r_   rc   	file_pathfnoderw   	node_reprrB   r   out_nodepattern_reprre   rp   s   `            @@r   dump_patternsz$VllmPatternMatcherPass.dump_patternsk   s    !88:: 	FdT:::;;;;;;#OKKKKK
 
	 ^^C   )	7A)%)^) ) ) 
 
 
 
 #*"2"8"8":": 7 7h7h... FDaMM F F FII $T

I 66yAA	;	;;!DDDD"+H"5"5 7 7JAw-//BL,,-BDIII  "w??H#'99..../    (*'=   0X//01$ $ gdH-- ! $(#=#=l#K#KL\---A66666%77)	7 )	7 )	7 )	7 )	7 )	7 )	7 )	7 )	7 )	7 )	7 )	7 )	7 )	7 )	7 )	7 )	7 )	7s   E.GG G)r   r   r   rO   rT   rP   r   recompilerU   r   Patternr   r^   r	   r   r   r   r   r   rS   rS   R   s           M351;;2 2(2:.   
C 
C 
 
 
 
@7J @79K @7PT @7 @7 @7 @7 @7 @7r   rS   c                   P     e Zd Zdededdf fdZdej        j        ddfdZ	 xZ
S )PrinterInductorPassnamer   r,   Nc                 X    t                                          |           || _        d S rE   )superr*   r   )r)   r   r   r'   s      r   r*   zPrinterInductorPass.__init__   s&       			r   r.   c                 <    |                      || j                   d S rE   )r3   r   )r)   r.   s     r   __call__zPrinterInductorPass.__call__   s    ty)))))r   )r   r   r   r   r	   r*   r8   r9   r:   r   __classcell__)r'   s   @r   r   r      sx        S *       *ehn * * * * * * * * *r   r   )r6   ry   rF   collections.abcr   dataclassesr   typingr   regexr   r8   torch._dynamo.utilsr   torch._inductor.pattern_matcherr   r   vllm.configr	   vllm.loggerr
   inductor_passr   r   rL   r   r   rS   r   r   r   r   <module>r      s         $ $ $ $ $ $ ! ! ! ! ! !            6 6 6 6 6 6 T T T T T T T T " " " " " " # # # # # # ' ' ' ' ' '	X		 / / / / / / / /
2M 2M 2M 2M 2M| 2M 2M 2MjY7 Y7 Y7 Y7 Y7- Y7 Y7 Y7x* * * * ** * * * * *r   