
    -`i:                        d dl Z d dlZd dlmZ d dlmZ  ee          Zde j        d<   de j        d<   dej	        j
        _        d Zd	ee         fd
ZddedefdZddZd Z e              ed          rKd dlmZ d dlmZ d dlmZmZ  e eg                     ej	        j
        j        d<   ee_        ee_        dS dS )    N)init_loggeris_torch_equal1PYTORCH_NVML_BASED_CUDA_CHECKTORCHINDUCTOR_COMPILE_THREADS   c                    dd l mc m ddlm}m}m}m}m} ddl	m
 dt          t                   ffd}t          j        j        |          r1j        j        j          |j        j        j        j                  }nj                                        }| j        rt          | j        d         |          rm| j        d         j        j        |vrT| j                                         | j        r4t          | j        d         |          r| j        d         j        j        |vT |            g}g }	t/          t1          | j                            D ]}
| j        |
         }t          ||          r$|                    |d                   | j        |
<   Ct          ||          r|                     |                       qt          ||          r'|	                    |                                           |	                    |                                           t1          |          dk    sJ d S )Nr   )EnterSubgraphLineExitSubgraphLineMemoryPlanningLineMemoryPlanningStateSubgraphPythonWrapperCodegenVreturnc                    dd l }g }|                    d          }|                    d          }| D ]}t          |j                  r2|                    j        j         dt          |                      It          |j                  r2|                    j        j         dt          |                      |                    |	                                           |S )Nr   _none_shape)
	itertoolscount
isinstanceNoneAsConstantBufferappendgraphnamenextShapeAsConstantBufferget_name)graph_outputsr   namesshape_counternone_counternoder   irs         e/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/env_override.pyget_output_namesz3memory_plan_reuse_patched.<locals>.get_output_names-   s    !** q))! 	. 	.D$ 788 .GG43E3EGGHHHHD"":;; .IID4G4GIIJJJJT]]__----    )torch._inductor.ir	_inductorr%   torch._inductor.codegen.wrapperr   r   r   r   r   torch._inductor.virtualizedr   liststrr   r   wrapper_codepartition_signaturesoutput_nodesr'   linesr$   r   poprangelenplanr   )selfr   r   r   r   r   r'   	out_namesplanning_statespast_planning_statesiliner   r%   s               @@r&   memory_plan_reuse_patchedr>   "   s   #########              .-----49         	17')EFF/G 5A$$G 5B
 
		 G,,..	 	
tz"~'9:: JrN$I55 	
 	
tz"~'9:: JrN$I55 +*,,-O3tz??## ? ?z!}d.// 	? IIob&9::DJqMM/00 	?""#6#6#8#89999.// 	? ''(;(;(=(=>>> 3 3 5 56661$$$$$$r(   skip_cudagraphsc           	          ddl m} ddlm}mm ddlm ddlm	} g } |j
                                                  }                                 dt          dt          f fdt          t!          |          t!          |                    D ]\  }}	 |            }
|D ].}|
                    |j                                                   /|
                    |          }|j                            d	 |D                       } |fd
|j        |j        z  D                       |
z
  } | fd|D                       } |            |D ]}                    |j                   fd|
z
  D             }|                    |           fd|D             }fd|D             }fd|D             }|                    |            | fd|D                       }fd|D             }fd|D             }                     ||          } ||||||	|          }|                    |           |                    ||z
            }|ddd         S )z
    Gets signature for each graph partition, including input nodes, output nodes, and
    whether deallocating an input within graph partition.
    r   )dependencies)GraphPartitionSignatureMutationOutput
NoneLayoutr   )
OrderedSetbuf_namer   c                     j                             | d          }|dS t          |j        j                  r?t          |j                  r(j                            | d          x}r |          S dS dS )z
        Checks if buf_name is NoneLayout. Buffers with NoneLayout is not allocated
        so graph partition should not take it as inputs or outputs.
        NFT)name_to_bufgetr   r$   layoutmutation_real_name)rF   buf	real_namerC   rD   is_none_layoutr8   s      r&   rN   z=get_graph_partition_signature_patched.<locals>.is_none_layoutw   s    
 ""8T22;5choz22 	#(N33 1!4884HHH	1 &~i0004ur(   c                     g | ]	}|j         
S  )read_writes).0r$   s     r&   
<listcomp>z9get_graph_partition_signature_patched.<locals>.<listcomp>   s    444$T444r(   c                 >    g | ]} |j                   |j         S rP   )r   )rR   xrN   s     r&   rS   z9get_graph_partition_signature_patched.<locals>.<listcomp>   s=       )>!&11F  r(   c              3   N   K   | ]}j                             ||          V   d S NrK   rI   rR   r   r8   s     r&   	<genexpr>z8get_graph_partition_signature_patched.<locals>.<genexpr>   G       +
 +
8<D#''d33+
 +
 +
 +
 +
 +
r(   c                     g | ]}|v |	S rP   rP   rR   r   name_to_nodes     r&   rS   z9get_graph_partition_signature_patched.<locals>.<listcomp>   s.     
 
 
|## ###r(   c                 *    i | ]}|v ||         S rP   rP   r]   s     r&   
<dictcomp>z9get_graph_partition_signature_patched.<locals>.<dictcomp>   s5     
 
 
|## ,t$###r(   c                 "    i | ]}|v ||v S rP   rP   rR   r   buffer_names_to_freer^   s     r&   r`   z9get_graph_partition_signature_patched.<locals>.<dictcomp>   s6     
 
 
|## $..###r(   c                 $    g | ]}|v |v
|S rP   rP   rb   s     r&   rS   z9get_graph_partition_signature_patched.<locals>.<listcomp>   s9     
 
 
|##4H(H(H (H(H(Hr(   c              3   N   K   | ]}j                             ||          V   d S rW   rX   rY   s     r&   rZ   z8get_graph_partition_signature_patched.<locals>.<genexpr>   r[   r(   c                 6    g | ]} |          |         S rP   rP   )rR   r   rN   r^   s     r&   rS   z9get_graph_partition_signature_patched.<locals>.<listcomp>   s>     
 
 
!>$''

 
 
r(   c                 0    g | ]}|j         j        v |S rP   )r   	constants)rR   r   r   s     r&   rS   z9get_graph_partition_signature_patched.<locals>.<listcomp>   s/     
 
 
dag>O6O6OD6O6O6Or(   Nr)   )torch._inductorrA   r*   rB   rC   rD   r-   r   torch.utils._ordered_setrE   r   r'   get_name_to_nodesr/   boolzipreversedupdateoutputs_by_namekeysintersection
ReadWrites
merge_listreadswrites
last_usage!get_graph_partition_symbol_inputsr   union)r8   
partitionsr?   rA   rB   rE   
signaturesunmet_output_names	partitionskip_cudagraphoutput_namesr$   returned_output_namesrQ   partition_input_namesextra_input_namesinput_nodesinput_deallocationextra_output_namesr2   constant_namessymbol_inputspartition_signaturerC   rD   r   rc   rN   r^   s   `                      @@@@@@r&   %get_graph_partition_signature_patchedr   f   s=    -,,,,,VVVVVVVVVV------333333J#AG$<$<$>$>??))++L          ( &)h77& & c
 c
!	> )3
 	= 	=D 4 9 9 ; ;<<<< , 9 9:L M M #-8844)444
 
 J   (.1CC     	 !+
 +
 +
 +
 +
@U+
 +
 +
 !
 !
 1;
 	9 	9D ''8888

 
 
 
-<
 
 

 	$$%6777
 
 
 
-
 
 


 
 
 
 
-
 
 

 
 
 
 
-
 
 
 	$$%7888 *
 +
 +
 +
 +
@U+
 +
 +
 !
 !

 
 
 
 
-
 
 

 
 
 
2
 
 
 >>y+VV55
 
 	-...288!66
 
 dddr(   F
should_logr   c                 *    ddl mc m} ddlm}m} ddlm}m}m	} |j
        }	t          |	t          j        j        j                  r|	j        x}
r|
                                }t          |
t          j        j                  r| d|
j         n|}|t          j        j        j        v s|t          j        j        j        v r#t          |
t          j        j                  sJ dS t          j        j        j        j        s	|j        dS dt0          d|dz  d	dfd
}|r|n|}t          ||          r t3           fd|j        D                       S |j
        J |                                s |d|           dS t          |j
        |j                  r |d|           dS t          |j
        |j                  r |d|           dS t=          |j
        dd          r |d|           dS  ||j
                  r |d|           dS dS )zBReturn True if we should partition the inductor graph on this noder   N)BaseSchedulerNodeFusedSchedulerNode)&_unstable_customized_partition_wrapperis_cudagraph_unsafe_opmaybe_log_cudagraph_partition.Tmsgr$   r   c                     d S rW   rP   )r   r$   s     r&   noop_logz*should_partition_patched.<locals>.noop_log9  s    r(   c              3   B   K   | ]}                     |          V  d S rW   )should_partition)rR   snoder8   s     r&   rZ   z+should_partition_patched.<locals>.<genexpr>?  s1      IIE4((//IIIIIIr(   znon gpu ops)r$   zDeviceCopy opszConditional opsunbacked_bindingszunbacked binding opszCUDAGraph-unsafe custom opsF)r*   r+   r%   torch._inductor.schedulerr   r   torch._inductor.utilsr   r   r   r$   r   torchFallbackKernelop_overloadr   _ops
OpOverload_overloadnameconfigcustom_should_partition_opstriton
cudagraphswrapperr/   anysnodesis_gpu
DeviceCopyConditionalgetattr)r8   r$   r   r%   r   r   r   r   r   ir_nodeopop_overload_packet_nameop_overload_namer   log_partition_reasons   `              r&   should_partition_patchedr     s    $########                 iG'5?-<== !! #%'')) "ej344)&;;)9;;;( 	 $%AB B5?#9#UUUb%*"7888884 O")42:Btc !2T!9 d     =GT88H$*++ JIIIIT[IIIIII9   ;;== ]6666t$)R]++ -D9999t$)R^,, .T::::tty-t44 3$????tdi(( :FFFFt5r(   c                     ddl mc m} ddlm} t
          |_        t          |_        |	                    dd          5   || j
                  | _        ddd           dS # 1 swxY w Y   dS )z
    (Re)initializes the scheduler member.  When initializing the scheduler, no CUBIN
    files should be generated (to avoid biasing any benchmarks and pessimizing
    fusion decisions).
    r   N)	Schedulerztriton.store_cubinF)torch._inductor.configr+   r   r   r   r   r   r   get_graph_partition_signaturepatch
operations	scheduler)r8   r   r   s      r&   _update_scheduler_patchedr   [  s     ,++++++++333333!9I.SI+	*E	2	2 4 4"4?334 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4s   A!!A%(A%c                      ddl m}   | d          s | d          r/ddl}t          t          j        d          rddlm} ||_        dS dS dS )z;Workaround for TorchInductor autotune get_raw_stream() bug.r   r   2.9.0z2.9.1N_cuda_getCurrentRawStream)r   )	vllm.utils.torch_utilsr   builtinshasattrr   _Ctorch._Cr   get_raw_stream)r   r   _get_raw_streams      r&   _patch_get_raw_stream_if_neededr   t  s    555555 ~g 6.."9"9 6 58899 	6MMMMMM&5H###	6 	66 6r(   r   )PythonWrapperCodegen)GraphLowering)_Config_ConfigEntry)defaultr   )F)r   N)osr   vllm.loggerr   r   r   __name__loggerenvironr+   r   compile_threadsr>   r.   rl   r   r   r   r   r,   r   torch._inductor.graphr   torch.utils._config_moduler   r   _configmemory_plan_reuse_update_schedulerrP   r(   r&   <module>r      s   
			  # # # # # # 1 1 1 1 1 1	X		 /2
* + /2
* +)*  &8% 8% 8%HJ'+DzJ J J JDP PT Pd P P P Pf4 4 4 426 6 6     ! ! !>' @DDDDDD333333@@@@@@@@ EQLE EEO"#@A .G*&?M###@ @r(   