U
    9%e                     @   s  d dl Z d dlZd dl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	m
Z
 d dlmZmZmZmZmZ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mZ d dlmZmZmZmZmZ d dl m!Z! d	d
l"m#Z#m$Z$m%Z% d	dl&m'Z'm(Z(m)Z) d	dl*m+Z+m,Z,m-Z- d	dl.m/Z/m0Z0m1Z1 d	dl$m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 d	dl8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ d	dlAmBZB d	dlCmDZDmEZEmFZF d	dlGmHZH eIeJZKejLMeJdZNejLMeJdZOdd ZPdd ZQdd ZRG dd dejSjTZUdS )    N)defaultdict)contextmanager)DefaultDictDictListOptionalSetTuple)get_decompositions)dynamo_timed)free_symbolsmagic_methodsmethod_to_operatorShapeEnvSymTypes)no_dispatch   )configirmetrics)get_scheduling_for_deviceget_wrapper_codegen_for_deviceregister_backend_for_device)CppWrapperCodeGenCudaWrapperCodeGenWrapperCodeGen)LoweringExceptionMissingOperatorWithDecompMissingOperatorWithoutDecomp)ConstantFixedLayoutInputBuffer	Pointwise	Reduction	TensorBox)FALLBACK_ALLOW_LISTfallback_handler%fallback_node_due_to_unsupported_typelayout_constraints	loweringsmake_fallbackneeds_realized_inputsunsupported_output_tensor)SizeVarAllocator)convert_shape_to_inductorgather_originsget_sympy_Expr_dtype)VZ
perf_hintsoutput_codec              
   C   sD   t jt jt jt jt jt jt jt jt j	t j
h
}|r<|t j | |kS N)torchfloat32Zfloat64int64Zint32Zint16Zint8Zuint8boolZbfloat16Z	complex64addZfloat16)dtypecudaZsupported_dtype r;   T/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/_inductor/graph.pysupported_dtype_of_cpp_wrapper<   s    r=   c                 C   sl   t | tjtjtjjjfs"tdt | tjjjr8tj	S t | tjrLt
| S | jrXtj	S | jrdtjS d S d S )Nzgget_constant_buffer_dtype only supports input of sympy.Symbol, sympy.Expr or sympy.core.numbers.Integer)
isinstancesympySymbolExprcorenumbersIntegerAssertionErrorr4   r6   r0   
is_integerZis_floatr5   )Zconstant_bufferr;   r;   r<   may_get_constant_buffer_dtypeP   s     rG   c                 C   s   dd t D }| |kS )Nc                 S   s   h | ]}t |qS r;   )r   ).0mr;   r;   r<   	<setcomp>c   s     z"is_magic_method.<locals>.<setcomp>)r   )opZ	magic_opsr;   r;   r<   is_magic_methodb   s    rL   c                       s  e Zd ZejdddZejdddZdd Zdddd	d	e dfej	j
d
 fddZeedddZdd Zdd Zee dddZedd ZedddZedddZedddZe fd d!Zd"d# Zejd$d%d&Ze e d'd(d)Z!d*d+ Z"ed,d-d.Z#d/d0 Z$eej%d1d2d3Z&ed4 fd5d6Z' fd7d8Z(d9d: Z)d;d< Z*d=d> Z+ fd?d@Z,dAdB Z-ej	j.dC fdDdEZ/dFdG Z0dHdI Z1dJdK Z2e3ej	j.dLdMdNZ4dOdP Z5dQdR Z6dSdT Z7dUdV Z8edWdX Z9dYdZ Z:d[d\ Z;ed,d]d^Z<  Z=S )_GraphLowering)exc                 C   sx   | j rt| t| fS ddlm} |dt| jj }| j	||\}}}dd |D }dd |D }||fS )z
        Support dynamic shapes and dynamic strides by assigning variables
        to each dimension.  We duck-shape tensors, so if two tensors
        have the same size they get assigned the same symbolic variable.
        r   )ConstantSourceZ__inductor_unknown_tensor_c                 S   s$   g | ]}t |tjr|jjn|qS r;   r>   r4   SymIntnodeexprrH   ir;   r;   r<   
<listcomp>   s     z8GraphLowering.symbolic_sizes_strides.<locals>.<listcomp>c                 S   s$   g | ]}t |tjr|jjn|qS r;   rP   rT   r;   r;   r<   rV      s     )
reuse_shape_envr.   sizestrideZtorch._dynamo.sourcerO   len
_shape_envZ
var_to_valZ,create_symbolic_sizes_strides_storage_offset)selfrN   rO   sourcerX   rY   _r;   r;   r<   symbolic_sizes_stridesh   s&    z$GraphLowering.symbolic_sizes_stridesc                 C   s,   dd |  D }dd | D }||fS )z+
        Primarily used to weights
        c                 S   s   g | ]}t |qS r;   r?   rD   rT   r;   r;   r<   rV      s     z6GraphLowering.static_sizes_strides.<locals>.<listcomp>c                 S   s   g | ]}t |qS r;   r`   rT   r;   r;   r<   rV      s     )rX   rY   )r\   rN   rX   rY   r;   r;   r<   static_sizes_strides   s    z"GraphLowering.static_sizes_stridesc                 C   sL   t dd kr$ddlm} td|t t dd krHddlm} td|t d S )Ncpur   )CppSchedulingr:   )TritonScheduling)r   Zcodegen.cpprc   r   r   Zcodegen.tritonrd   )r\   rc   rd   r;   r;   r<   init_backend_registration   s    z'GraphLowering.init_backend_registrationNF)gmc	           	         sl  t  | |d k	r|n| || _d| _d| _|d krFt }d| _n|| _d| _|| _t	|| _
i | _i | _d | _t | _t | _d| _g | _i | _i | _t | _t | _t | _t | _d | _d | _|| _i | _t | _g | _t | _i | _ t!t"| _#t$$ | _%d| _&|| _'|| _(|| _)d | _*| jr2| + nt | _,dh| _-|| _.d| _/d| _0g | _1d| _2| 3  d S )Nr   FTrM   zaten.convolution_backward )4super__init__decide_layout_opt
layout_optnum_channels_last_convZextra_tracebackr   rW   r[   r-   sizevarsgraph_inputsgraph_inputs_originalgraph_outputssetdevice_typesdevice_idxsr:   buffers	constantsconstant_reprsZremoved_buffersZremoved_inplace_buffersmutated_buffersZinplaced_to_removewrapper_codecurrent_nodenum_static_inputslistsZmutated_inputsZmutated_input_idxsZunaligned_buffersname_to_bufferr   listname_to_userstimeZcreation_timenamecpp_wrapperaot_modegraph_id	schedulerfind_nodes_prefer_channels_lastnodes_prefer_channels_last_warned_fallbackuser_visible_outputs	cache_key
cache_pathcache_linemapZdisable_cudagraphsre   )	r\   rf   Z	shape_envrz   r   r   r   r   rk   	__class__r;   r<   ri      sd    


zGraphLowering.__init__)returnc                 C   sx  t js
dS dd | jjD }t|}|dkr0dS tjjr^tj	 r^tj
d}td|s^dS tdd |D rtjjjrtjj	 rdS tt| jjd	| krtd
 dS tdd |D rtd dS tdd |D rtd dS tdd |D rtd dS tdd |D r4td dS | jjD ]6}|jtjjjjtjjjjfkr<td  dS q<dS )zl
        Decide if we should enable layout optimization for this graph based on
        heuristics.
        Fc                 S   s"   g | ]}|j tjjjjkr|qS r;   )targetr4   opsatenconvolutiondefaultrH   nr;   r;   r<   rV      s     z3GraphLowering.decide_layout_opt.<locals>.<listcomp>r   zMI2\d\dc                 s   s4   | ],}d D ]"}|j | jd jtdkV  q
qdS )r   r   valrb   N)argsmetadevicer4   rH   r   idxr;   r;   r<   	<genexpr>  s    z2GraphLowering.decide_layout_opt.<locals>.<genexpr>Ti,  z)Only a few conv, skip layout optimizationc                 s   s,   | ]$}d D ]}t |j| jd V  q
qdS )r   r   N)r   r   r   r   r;   r;   r<   r     s      zeSee perf regression with dynamic shape. Follow up in https://github.com/pytorch/pytorch/issues/102670c                 s   s6   | ].}|j d  dko,|j d jd ddkV  qdS )r   r   Nr   r   rX   r   r;   r;   r<   r   +  s    z.Found grouped convolution with >1 in_channels!c                 s   sX   | ]P}|j d  jd dd |j d  jd d koN|j d  jd dd kV  qdS )r   r   r      Nr   r   r;   r;   r<   r   8  s   0zKSkip layout optimization because some convolutions have smaller out_channelc                 s   sB   | ]:}|j d  jd ddko8|j d  jd d dkV  qdS )r   r   r   @   Nr   r   r;   r;   r<   r   D  s   z>Skip layout opt because all convolution channels are too smallzMSkip layout optimization because sdpa (scaled dot product attention) is found)r   Zlayout_optimizationgraphnodesrZ   r4   versionZhipr:   Zis_availableZget_device_nameresearchallbackendsmkldnnenabledr}   logdebuganyr   r   r   Z#_scaled_dot_product_flash_attentionr   Z'_scaled_dot_product_efficient_attention)rf   Z
conv_nodesZnconvZgpu_namer   r;   r;   r<   rj      st    
	

	





zGraphLowering.decide_layout_optc                 C   s   t  }t| jjjD ]D}|jtjjj	j
kr6|| q|jD ]}||kr<||  qq<q| jjjD ]"}||krd|jD ]}|| qvqd|S )aC  
        The rule to decide if an node prefer channels last is simple.
        1. if it's input/output of a convolution
        2. if one of its user prefers channels last

        We have rule 1 because cudnn runs a faster convolution kernel for channels last inputs;
        Rule 2 is also important. It makes sure that indirect inputs to convolution also prefers
        channels last.

        Consider the scenario: conv -> batch-norm -> relu -> conv
        Without rule 2, batch-norm output may use a contiguous layout. That will cause 2 extra copies:
        1. the output of batch-norm should be channels last initially since its input is a conv's output.
           Forcing the batch-norm's output to be contiguous results in the first copy
        2. The second conv's input is initially contiguous. This layout is propagated from the batch-norm's output.
           We need convert it to channels last layout which results in the second copy.
        With rule 2, we makes sure all the tensors in the chain uses channels last layout. So both copies
        can be saved.
        )rq   reversedmoduler   r   r   r4   r   r   r   r   r8   users)r\   Z
output_setr   userchildr;   r;   r<   r   e  s    



z-GraphLowering.find_nodes_prefer_channels_lastc                 C   s&   || j kr"| j | td| d S )NzUsing FallbackKernel: %s)r   r8   perf_hint_loginfor\   r   r;   r;   r<   warn_fallback  s    
zGraphLowering.warn_fallback)r   c                 C   s   |d k	r| j | d S r3   )rs   r8   )r\   r   r;   r;   r<   add_device_idx  s    zGraphLowering.add_device_idxc                 C   s   t jS r3   )r1   	fake_moder\   r;   r;   r<   r     s    zGraphLowering.fake_mode)buffer_namec                 C   s,   || j kr| j | S || jkr(| j| S d S r3   )r|   rn   )r\   r   r;   r;   r<   
get_buffer  s
    



zGraphLowering.get_bufferc                 C   sx   || j kr| j | jS || jkr.| j|  S || jkrF| j|  S td|}|rf| |dS td| d S )Nz1(as_strided|reinterpret_tensor)\(([a-zA-Z0-9_]+),r   could not find )	ru   r9   r|   	get_dtypern   r   matchgroupKeyError)r\   r   rI   r;   r;   r<   r     s    


zGraphLowering.get_dtypec                 C   s   ddl m} || jkr$| j|  S || jkrV| j| }tt|dd |rNdS | S || jkrn| j|  S t	d| d S )Nr   )MultiOutputLayoutZlayoutr   )
r   r   ru   Znumelr|   r>   getattr	get_numelrn   r   )r\   r   r   bufr;   r;   r<   r     s    



zGraphLowering.get_numelc                    s   t  j| S r3   )rh   run)r\   r   r   r;   r<   r     s    zGraphLowering.runc                 C   s$   t  jd7  _d| _td| d S )Nr   Fz"Set cpp_wrapper to False due to %s)r   disable_cpp_wrapperr   r   r   )r\   Zcondr;   r;   r<   r     s    z!GraphLowering.disable_cpp_wrapper)bufferc                 C   s*   dt | j }| j| || j|< |S )Nr   )rZ   rt   appendr|   )r\   r   r   r;   r;   r<   register_buffer  s    
zGraphLowering.register_buffer)buffer_namesc                 C   s   dd | }|| j|< |S )Nlist_r^   )joinr{   )r\   r   r   r;   r;   r<   register_list  s    
zGraphLowering.register_listc                    s    fdd  | d S )Nc                    sz   t | ttfr | D ]} | qt | tjrvt| drTt | jtjrTt | jjtjsXd S |  D ]}j| 	|  q`d S )Ndata)
r>   r}   tupler   IRNodehasattrr   Zget_read_namesr~   r   )valuexZ	read_nameregisterr\   r;   r<   r     s    
z1GraphLowering.register_users_of.<locals>.registerr;   )r\   Znode_outputr;   r   r<   register_users_of  s    zGraphLowering.register_users_of)r   c                 C   sD   t |tst| j| || jkr(dS | j| D ]}|  q2dS )z
        When a buffer is mutated we need to make sure all the reads to
        the old version are realized before the mutation happens.
        N)r>   strrE   rw   r8   r~   realize)r\   r   r   r;   r;   r<   mark_buffer_mutated  s    
z!GraphLowering.mark_buffer_mutatedc              
      s8    fdd}t t| t j jf  S )Nc                     s   j  D ]^\} } js
  | kr
  | kr
 j|jkr
 j|jkr
t |	 r
|   S q
dt
j  }  j | < tt d j| < | S )NZconstantzutf-8)ru   itemsZ	is_mkldnnrX   rY   r9   r   r4   eqr   rZ   hashlibsha256reprencode	hexdigestrv   )r   r   r   r\   r;   r<   allocate  s(    



z3GraphLowering.add_tensor_constant.<locals>.allocate)r$   creater   ConstantBufferr    r   r9   ra   )r\   r   r   r;   r   r<   add_tensor_constant  s    z!GraphLowering.add_tensor_constant)r   device_overridec                 C   sZ   | j | j|ks|dkr|S | d|j |jp0d }|| j krV| j | || j |< |S )z
        We AOT copy constants to the devices they are needed on.
        If device_override doesn't match the constant's device, then
        copy it and return a different name.
        Nr^   r   )ru   r   typeindexto)r\   r   r   Zalt_namer;   r;   r<   constant_name  s    
zGraphLowering.constant_namer   c           	   	      s   t  |||}t|tr0|jj}|| j|< |S t|ttt	frXt
|}|| j|< |S t|tjslt||js| |\}}n| |\}}tt|t|j|j||}|| j|< |jj| j|< | j|jj | |jj |S r3   )rh   placeholderr>   r   rR   rS   rn   intr7   floatr?   Zsympifyr4   TensorrE   Z_has_symbolic_sizes_stridesra   r_   r$   r   r!   r    r   r9   r   ro   rr   r8   r   r   r   )	r\   r   r   kwargsZexamplerS   sizesstridestensorr   r;   r<   r   !  s0    




zGraphLowering.placeholderc              
      s  |t jkr,t|d ttfr,t |||S t|dr@|||S |tkr|	 
dd }|tkrlt| n\tjrt|grtnt}td|||| t| n$t|grt|||nt|||zt| ||}|W S  tk
r } zt|||||jd W 5 d }~X Y nX d S )Nr   Z_inductor_lowering_function.z"Creating implicit fallback for:
%s)operatorgetitemr>   r}   r   rh   call_functionr   r)   r   splitr%   r*   r   Zimplicit_fallbacksr
   r   r   r   r   Zoperator_str	Exceptionr   with_traceback__traceback__)r\   r   r   r   	base_nameerrorouter   r;   r<   r   B  s<    




zGraphLowering.call_functionc              
   C   s   t | j|}t|r| |S t z |jdkrPt| |j|j	W  5 Q R  S t
|jdkr|jd dkrddlm} || |j|j	dW  5 Q R  S W 5 Q R X | |S )Nr;   r   r      )r   )r9   r   )r   r   r,   r   r   shaper   itemr9   r   rZ   loweringr   tolist)r\   r   r   r   r   r   r;   r;   r<   get_attri  s    

 ,zGraphLowering.get_attrc                 C   s
   t  d S r3   rE   r\   r   r   r   r;   r;   r<   call_module{  s    zGraphLowering.call_modulec                 C   s
   t  d S r3   r  r  r;   r;   r<   call_method~  s    zGraphLowering.call_methodc           	   	      s>  t  |||}t|ttfs*tt|tdd |D sDt|dd |D | _| j	
 D ]\}}t|ttjfsztt|tsq^|  t|tst|j}t|tjst|}|j}t|tr| |kr^tj|| j|  z | j|}| j| | j|< W q^ tk
r   Y q^X q^|   td| j| j d S )Nc              
   s   s6   | ].}t |ttjtd tjtjtjj	j
tfV  qd S r3   )r>   r$   r   r   r   r   r?   rA   ZlogicZboolalgBooleanr   rH   r   r;   r;   r<   r     s   z'GraphLowering.output.<locals>.<genexpr>c                 S   s   g | ]}t j|qS r;   )r   ExternKernelZrealize_inputr  r;   r;   r<   rV     s     z(GraphLowering.output.<locals>.<listcomp>zGForce channels last inputs for %d conv for the current graph with id %d)rh   outputr>   r   r}   rE   r   r   rp   rn   r   r$   r?   rA   r   r   r   
StorageBoxr!   get_nameZMutationLayoutZrealize_intoro   r   
ValueErrorfinalizer   r   rl   r   )	r\   r   r   r   resultr   r   Zvalue_storage_boxindr   r;   r<   r    s>    

zGraphLowering.outputc                 C   s   | j D ]}|  qd S r3   )rt   Zdecide_layout)r\   r   r;   r;   r<   r    s    
zGraphLowering.finalize)r   c                    s  |h}|j dkr,| |\}}|t||O }tj| | | |j dkr~|jtj	k	r~t
|r~t|jdd||}n|j dkr|jtkrt|j |f||\}}| |j||}n|jtjjjkrt|jd tjr|jd jj}nt |}nJt|jr<t|jd tjr.|jd jj}nt |}nt |}tjjjjtjjjjtjjjjg tdd |jD }t fdd|jD }|s|r6t|jd tj r6|jd ! }tj"#|jd }	|	r6t$|r6t%|}
t$|& dkr(|| j'kr(|j(| j)kr(|s(tj*}
tj+,||
}t$t-|j}|d	krt|t.r|jD ]:}|jt/krr|0  tjjj1jtjjj2jtjjj3jg}| j4s|5tjjj6j tj7j8rH|tjj9j:jtjj9j:j;tjj9j<j;tjj9j=jtjj9j>jtjj9j>j;tjjj?jtjj@jAjtjj@jAj;tjj@jBjg
7 }tj7jCrH|tjjDjEjg7 }|j|krrtj+,|t%|jd ! }|j d
kr`t|jFjFtGtHfr`|I  q`|Jt$|j t|t.r|K r|0  W 5 Q R X W 5 Q R X t|t.rt|jFtjLrt|jFjFtjMr||jFjF_Nnt|jFjFtjOr||jFjF_Nt|jFjFtjPrjt|jFjFjFtjMrj||jFjFjF_NnFt|jFjFtjQr|jFjFjRst|jFjFjSd tjOr||jFjFjSd _N| T| |S )Nr   F)Zadd_to_fallback_setr   c                 s   s   | ]}|j d kV  qdS )r  N)rK   rH   r   r;   r;   r<   r     s     z)GraphLowering.run_node.<locals>.<genexpr>c                 3   s   | ]}|j  kV  qd S r3   r   r  Zas_strided_opsr;   r<   r     s       r   r  r   )UrK   Zfetch_args_kwargs_from_envr/   r   r   Zcurrent_originsset_current_noder   r   r   r'   r&   r(   r   r4   r   r   Z
sym_strider>   r   rQ   rR   rS   rh   run_noderL   Z
as_stridedr   Zas_strided_Zas_strided_scatterr   r   r   rY   Z_prims_commonZis_non_overlapping_and_denserZ   Zget_stride_orderget_sizer   r   r   ZNHWC_STRIDE_ORDERr  Zrequire_stride_orderrq   r$   r+   Zrealize_hintZconvolution_backwardmmZ_int_mmrk   r   r   Z_CZ_has_mkldnnr   Z_convolution_pointwisebinaryZ_convolution_pointwise_Z _convolution_transpose_pointwiseZ_linear_pointwiseZmkldnn_rnn_layerZonednnZqconv2d_pointwiseZqlinear_pointwiseZhas_mklZmklZ_mkl_linearr   r"   r#   r   Z
mark_reuseZhas_exceeded_max_readsr  ZLoopsZorigin_nodeBufferComputedBufferZMultiOutputindicesinputsr   )r\   r   Zoriginsr   r   r  Z	is_outputZis_input_for_as_stridedr   ZdenseZstride_orderZ	num_usersr   Zneed_fixed_layoutr   r  r<   r    s    

 



 
















 

 
zGraphLowering.run_nodec                 C   s   t jr| d d S )Nzcpp codegen disabled)r   Zdisable_cpp_codegenr   r   r;   r;   r<   check_cpp_codegen_disabledL  s    z(GraphLowering.check_cpp_codegen_disabledc                 C   s   t jdkr| d d S )Nlinuxzplatform not linux)sysplatformr   r   r;   r;   r<   check_platformP  s    
zGraphLowering.check_platformc                 C   sd   | j  D ]T}d }t|tr&| }n"t|tjtjtjj	j
frHt|}t|| js
| d q
d S )Nzunsupported inputs dtype)rn   valuesr>   r$   r   r?   r@   rA   rB   rC   rD   rG   r=   r:   r   )r\   r   r9   r;   r;   r<   check_input_for_cpp_bufferT  s    

 z(GraphLowering.check_input_for_cpp_buffer)rR   c                 c   s$   | j }z|| _ d V  W 5 || _ X d S r3   )ry   )r\   rR   oldr;   r;   r<   r  a  s
    
zGraphLowering.set_current_nodec                 C   s   |    |   |   d S r3   )r   r$  r&  r   r;   r;   r<   check_cpp_wrapperj  s    zGraphLowering.check_cpp_wrapperc                 C   s   d| j k| _| jrL|   | jr.| js.td| jrL| jr@t nt | _d S | j  }| j	D ]}|
| j q\|d t|dkstdd|t|dk}|rdn| }t|}| | _d S )Nr:   z'CudaWrapperCodeGen hit unsupported caserb   r   zDoes not support mixing {}+r   )rr   r:   r   r(  rE   r   r   rx   copyrt   r8   
get_devicer   discardrZ   formatr   popr   )r\   rr   r   Zonly_cpuZdevice_typeZwrapper_code_gen_clsr;   r;   r<   init_wrapper_codeo  s(    


zGraphLowering.init_wrapper_codec                 C   sP   ddl m} |   || j| _ | j d k	s.t| j   | jd k	sFt| j S )Nr   	Scheduler)r   r1  r/  rt   rE   codegenrx   generate)r\   r1  r;   r;   r<   r2    s    
zGraphLowering.codegenc                 C   sl   ddl m} || j}d}g }g }|jD ]8}| }||7 }|||d f ||| f q(|||fS )Nr   r0  r   r  )r   r1  rt   r   Zget_read_write_buffers_sizesr   Zget_estimated_runtime)r\   r1  r   total_bytesZnode_countsZnode_runtimesrR   	num_bytesr;   r;   r<   count_bytes  s    

zGraphLowering.count_bytesc           	      C   s   ddl m} |  \}}dd |D }||\}}|j|||d}|| _|| _|| _| j	 D ]\}}t
||| q`td|j td|j td| tjrtd|j tjd	 tj|j tjtj|jd
 d  |S )Nr   )PyCodeCachec                 S   s   g | ]\}}||j fqS r;   )Zstack_trace)rH   line_norR   r;   r;   r<   rV     s     z3GraphLowering.compile_to_module.<locals>.<listcomp>)linemapzOutput code written to: %sOutput code: 
%szCompiled module path: )filer   z.debug)	codecacher7  r2  writeZload_by_key_pathr   r   r   ru   r   setattroutput_code_logr   __file__r   r   r   Zbenchmark_kernelprintr"  stderrr1   r2   r*  ospathsplitext)	r\   r7  coder9  keyrD  modr   r   r;   r;   r<   compile_to_module  s$    zGraphLowering.compile_to_modulec                 C   sP   | j rB| jrBddlm} |  \}}td| |j| || jdS | 	 j
S d S )Nr   )AotCodeCacher:  )r:   )r   r   r<  rJ  r2  r?  r   compiler:   rI  call)r\   rJ  rF  r9  r;   r;   r<   compile_to_fn  s    zGraphLowering.compile_to_fnc                 C   s   | j d k	stdd | j D S )Nc                 S   s,   g | ]$}t |tjst |tjs| qS r;   )r>   r   ZNoneAsConstantBufferZShapeAsConstantBufferr  )rH   rR   r;   r;   r<   rV     s   z2GraphLowering.get_output_names.<locals>.<listcomp>)rp   rE   r   r;   r;   r<   get_output_names  s    zGraphLowering.get_output_namesc                 C   s4   || j  ko2| j |  dko2| j |  jdkS )Nr   rb   )rn   keysr   r+  r   r   r;   r;   r<   is_unspec_arg  s
    zGraphLowering.is_unspec_arg)>__name__
__module____qualname__r4   r   r_   ra   re   	frozensetfxZGraphModuleri   staticmethodr7   rj   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r   r  r   r   r   r   r   r   r   r   r   r   r  r  r	  r  r  Noder  r   r$  r&  r   r  r(  r/  r2  r6  rI  rM  rN  rP  __classcell__r;   r;   r   r<   rM   g   sn   #G3
!'/ 
	rM   )Vr   loggingr   rC  r   r"  r   collectionsr   
contextlibr   typingr   r   r   r   r   r	   r?   r4   Ztorch._loggingZtorch.fxZtorch._decompr
   Ztorch._dynamo.utilsr   Z%torch.fx.experimental.symbolic_shapesr   r   r   r   r   Ztorch.utils._mode_utilsr   rg   r   r   r   Zcodegen.commonr   r   r   Zcodegen.wrapperr   r   r   excr   r   r   r   r    r!   r"   r#   r$   r  r%   r&   r'   r(   r)   r*   r+   r,   rm   r-   utilsr.   r/   r0   Zvirtualizedr1   	getLoggerrQ  r   Z_loggingZgetArtifactLoggerr   r?  r=   rG   rL   rU  ZInterpreterrM   r;   r;   r;   r<   <module>   sB     (

