o
    I&i+)                     @  s  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	m
Z
mZmZ d dlmZ d dlZd dlmZ d d	lmZmZ d
dlmZmZmZ erRd dlmZ e ZG dd dZG dd dZG dd deZd,ddZG dd dZ G dd dZ!G dd dZ"e #  ede Z$edeZ%edeZ&ed eZ'ed!eZ(ed"eZ)ed#eZ*ed$eZ+ed%eZ,G d&d' d'Z-G d(d) d)Z.e. Z/e Z0G d*d+ d+Z1e1 Z2dS )-    )annotationsN)contextmanager)chain)local)AnyCallableTYPE_CHECKINGUnion)patch)IndentedBuffer)inplace_methodsmagic_methods   )reduction_num_outputs	sympy_strsympy_symbol)GraphLoweringc                   @  s2   e Zd ZdZdddZdd Zdd	 Zd
d ZdS )Virtualizedz
    A global variable that redirects via thread local variable

    This allows us to swap in different op implementations in codegen.
    vnamestrc                 C  s   d| | _ || _d S )NZ__torchinductor_)_key_default)selfr   default r   FC:\wamp64\www\opt\env\Lib\site-packages\torch/_inductor/virtualized.py__init__   s   
zVirtualized.__init__c                   s.      ttj| t fdd}| S )Nc                	   3  s&    zd V  W    d S    w N)_set_handlerr   Zpriorr   r   r   ctx'   s   z%Virtualized._set_handler.<locals>.ctx)_get_handlersetattrthreadlocalr   r   )r   valuer    r   r   r   r   #   s
   zVirtualized._set_handlerc                 C  s*   zt t| jW S  ty   |   Y S w r   )getattrr#   r   AttributeErrorr   r   r   r   r   r!   0   s
   zVirtualized._get_handlerc                 C  s   t |  |S r   )r%   r!   )r   namer   r   r   __getattr__6      zVirtualized.__getattr__N)r   r   )__name__
__module____qualname____doc__r   r   r!   r)   r   r   r   r   r      s    
r   c                   @  s   e Zd ZdS )NullHandlerN)r+   r,   r-   r   r   r   r   r/   :   s    r/   c                      s    e Zd ZdZ fddZ  ZS )NullKernelHandleraO  
    We need access `V.kernel.removed_buffers` in DeferredLine class when there
    is no kernel in the context. This happens when codegening the wrapper.
    Initialize `removed_buffers` and `inplaced_to_remove` explicitly so we don't
    need call 'getattr' with default value which is error prone to typo in
    attribute name.
    c                   s   t    t | _t | _d S r   )superr   setZremoved_buffersZinplaced_to_remover'   	__class__r   r   r   G   s   
zNullKernelHandler.__init__)r+   r,   r-   r.   r   __classcell__r   r   r3   r   r0   >   s    r0   returnr   c                 C  s   t | tjr
t| S t| S r   )
isinstancesympyExprr   r   )ar   r   r   _arg_strM   s   r;   c                   @  s>   e Zd Zdd ZedddZeddd	d
Zedd ZdS )MockHandlerc                   s    dkrdS  fdd}|S )Nr(   r<   c                    s>   dd | D }| dd | D  d  dd| dS )	Nc                 S  s   g | ]}t |qS r   )r;   .0r:   r   r   r   
<listcomp>Y   s    z:MockHandler.__getattr__.<locals>.inner.<locals>.<listcomp>c                 s  s"    | ]\}}| d | V  qdS )=Nr   r>   kvr   r   r   	<genexpr>Z   s     z9MockHandler.__getattr__.<locals>.inner.<locals>.<genexpr>zops.(, ))extenditemsjoin)argskwargsZfargsr(   r   r   innerX   s   z&MockHandler.__getattr__.<locals>.innerr   r   r(   rN   r   rM   r   r)   T   s   zMockHandler.__getattr__r6   r   c                 C  s   d|  d|  d| dS )Nzops.masked(rF   rG   r   )maskbodyotherr   r   r   masked_   s   zMockHandler.maskedTsympy.Symbolc                 C  s   t dt|  dS )NrE   rG   )r   r   )Z	index_varsizecheckr   r   r   indirect_indexingc   s   zMockHandler.indirect_indexingc                 C  s8   dd }t t t D ]\}}t| ||| qd S )Nc                   s   t  fdd}|S )Nc                    s
    j |  S r   )format)rK   format_stringr   r   rN   j   s   
z:MockHandler._init_cls.<locals>.make_handler.<locals>.inner)staticmethod)rZ   rN   r   rY   r   make_handleri   s   z+MockHandler._init_cls.<locals>.make_handler)r   r   rI   r   r"   )clsr\   r(   rZ   r   r   r   	_init_clsg   s   zMockHandler._init_clsNr6   r   T)r6   rT   )	r+   r,   r-   r)   r[   rS   rW   classmethodr^   r   r   r   r   r<   S   s    r<   c                   @  s@   e Zd Zdd ZeddddZdd	d
ZdddZdd ZdS )KernelFormatterHandlerc                 C  s   || _ td| _t | _d S )Nr   )parent_handlerr   output	itertoolscountvar_counter)r   rc   r   r   r   r   w   s   
zKernelFormatterHandler.__init__Nr6   r   c              	   C  sH  ddl m} |d ur||gn|g}|d urddgndg}tt }|jd |jdd| d W d    n1 s?w   Y  t||D ]\}}|rddd	d
 |D }	|j|	 d|  qIt	
|0 t|dd | | }
||
W  d    W  d    S 1 sw   Y  W d    d S 1 sw   Y  d S )Nr   )FlexibleLayoutindexrindexzdef inner_fn(rF   z):c                 S  s(   g | ]}t t|ttjfrd n|qS )_)r   r7   intr8   Integerr>   rC   r   r   r   r?      s    z7KernelFormatterHandler.ir_to_string.<locals>.<listcomp> = Zallow_indexingT)Zirrh   rb   r<   rd   indent	writelinerJ   zipVset_ops_handlerr
   objectgetvalue)Zir_fnri   rj   rh   rK   names	formatterr(   arglhsresultr   r   r   ir_to_string|   s,   
Rz#KernelFormatterHandler.ir_to_stringCallable[..., str]c                   s    fdd}|S )Nc                    sL   t j | i |} dkr|S dtj }j| d|  |S )NrW   tmprp   )r%   rc   nextrg   rd   rr   )rK   rL   linevarnamer(   r   r   r   rN      s   z1KernelFormatterHandler.__getattr__.<locals>.innerr   rO   r   r   r   r)      s   	z"KernelFormatterHandler.__getattr__Union[tuple[str, ...], str]c                   sd    j ||||}t|} fddt|D } jd| d|  |dkr.t|S |d S )Nc                   s   g | ]
}d t  j qS )r   )r   rg   )r>   rl   r'   r   r   r?          z4KernelFormatterHandler.reduction.<locals>.<listcomp>,rp   r   r   )rc   	reductionr   rangerd   rr   rJ   tuple)r   ZdtypeZ	src_dtypeZreduction_typer$   r   Z
num_valuesvarnamesr   r'   r   r      s
   z KernelFormatterHandler.reductionc                 C  s   | j d|  | j  S )Nzreturn )rd   rr   rw   )r   r|   r   r   r   rw      s   
zKernelFormatterHandler.getvaluer   r_   )r6   r~   )r6   r   )	r+   r,   r-   r   r[   r}   r)   r   rw   r   r   r   r   rb   v   s    

	rb   c                   @  s   e Zd Zdd Zdd ZdS )WrapperHandlerc                 C  
   || _ d S r   )_inner)r   rN   r   r   r   r         
zWrapperHandler.__init__c                 C  s   t | j|S r   )r%   r   )r   itemr   r   r   r)         zWrapperHandler.__getattr__N)r+   r,   r-   r   r)   r   r   r   r   r      s    r   opsgraphreal_inputs	fake_modekerneldebuginterpreteraot_compilationcurrent_nodec                   @  sr   e Zd ZU dZded< dd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )OpsValuea,  The return type of most ops calls.

    This exists so we can overload magic methods, and write mathematical
    expressions much more fluently. So instead of

        ops.add(ops.mul(ops.mul(ops.sub(ops.mul(_Ap2, x), _Ap3), x), x), _1)

    we can write

        (_Ap2 * x - _Ap3) * x * x + _1

    r   r$   c                 C  r   r   r$   )r   r$   r   r   r   r      r   zOpsValue.__init__c                 C  s
   t | jS r   )r   r$   r'   r   r   r   __str__   r   zOpsValue.__str__c                 C  s   d| j dS )Nz	OpsValue(rG   r   r'   r   r   r   __repr__   r*   zOpsValue.__repr__c                 C     t | |S r   )r   addr   rR   r   r   r   __add__   r   zOpsValue.__add__c                 C  r   r   )r   mulr   r   r   r   __mul__   r   zOpsValue.__mul__c                 C  r   r   )r   subr   r   r   r   __sub__   r   zOpsValue.__sub__c                 C  s
   t | S r   )r   negr'   r   r   r   __neg__   r   zOpsValue.__neg__c                 C  r   r   )r   truedivr   r   r   r   __truediv__   r   zOpsValue.__truediv__c                 C  r   r   )r   floordivr   r   r   r   __floordiv__   r   zOpsValue.__floordiv__c                 C  r   r   )r   modr   r   r   r   __mod__   r   zOpsValue.__mod__c                 C  r   r   )r   powr   r   r   r   __pow__   r   zOpsValue.__pow__N)r+   r,   r-   r.   __annotations__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s   
 r   c                   @  s>   e Zd ZdZdd Zedd Zedd Zedd	d
ZdS )
OpsWrapperzThis wraps any returned IR values into an `OpsValue` instance, so that we
    can overload the magic methods for writing mathematical expressions fluently.
    c                   s    fdd}|S )Nc                    s:   dd | D }dd |  D }ttt |i |S )Nc                 S  s   g | ]}t |qS r   r   _unwrapr=   r   r   r   r?      s    z9OpsWrapper.__getattr__.<locals>.inner.<locals>.<listcomp>c                 S  s   i | ]
\}}|t |qS r   r   rA   r   r   r   
<dictcomp>   r   z9OpsWrapper.__getattr__.<locals>.inner.<locals>.<dictcomp>)rI   r   _wrapr%   _ops)rK   rL   new_argsZ
new_kwargsrM   r   r   rN      s   z%OpsWrapper.__getattr__.<locals>.innerr   rO   r   rM   r   r)      s   zOpsWrapper.__getattr__c                 C  s4   t | ttfrtdd | D S t | tr| jS | S )Nc                 s  s    | ]}t |V  qd S r   r   ro   r   r   r   rD     s    z%OpsWrapper._unwrap.<locals>.<genexpr>)r7   listr   r   r$   xr   r   r   r     s
   
zOpsWrapper._unwrapc                 C  s(   t | ttfrtdd | D S t| S )Nc                 s  s    | ]}t |V  qd S r   )r   ro   r   r   r   rD     s    z#OpsWrapper._wrap.<locals>.<genexpr>)r7   r   r   r   r   r   r   r   r     s   zOpsWrapper._wrapTc                 C  s   t | } t| ||S r   )r   r   r   rW   )ri   rU   rV   r   r   r   rW     s   
zOpsWrapper.indirect_indexingNr`   )	r+   r,   r-   r.   r)   r[   r   r   rW   r   r   r   r   r      s    

r   c                   @  sN  e Zd ZU eZeZeZejZde	d< ej
Zde	d< ejZde	d< ejZde	d< ej
Zde	d< ejZde	d	< ej
Zde	d
< ejZde	d< ejZde	d< ejZde	d< ejZde	d< ej
Zde	d< ejZde	d< ej
Zde	d< e d(ddZ!e d)ddZ"e dd Z#e dd Z$e dd Z%e dd  Z&e d!d" Z'e d#d$ Z(e d%d& Z)d'S )*_VzCallable[[Any], Any]ru   zCallable[[], Any]get_ops_handlerzCallable[[GraphLowering], Any]set_graph_handlerset_real_inputsget_real_inputsset_fake_modeget_fake_modeset_kernel_handlerset_debug_handlerset_interpreter_handlerset_aot_compilationget_aot_compilationset_current_nodeget_current_noder6   _MockHandlerc                 C     t  S )z9The operator handler specific to the current codegen task)r   r!   r'   r   r   r   r   2     z_V.opsr   c                 C  r   z#The graph currently being generated)_graphr!   r'   r   r   r   r   7  r   z_V.graphc                 C  r   )znon-fake example inputs)_real_inputsr!   r'   r   r   r   r   <  r   z_V.real_inputsc                 C  r   r   )
_fake_moder!   r'   r   r   r   r   A  r   z_V.fake_modec                 C  r   )z$The kernel currently being generated)_kernelr!   r'   r   r   r   r   F  r   z	_V.kernelc                 C  r   r   )_debugr!   r'   r   r   r   r   K     z_V.debugc                 C  r   r   )_interpreterr!   r'   r   r   r   r   O  r   z_V.interpreterc                 C  r   r   )_aot_compilationr!   r'   r   r   r   r   S  r   z_V.aot_compilationc                 C  r   r   )_current_noder!   r'   r   r   r   r   W  r   z_V.current_nodeN)r6   r   )r6   r   )*r+   r,   r-   r<   rb   r   r   r   ru   r   r!   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r     sH   
 





r   r_   )3
__future__r   re   
contextlibr   r   	threadingr   typingr   r   r   r	   Zunittest.mockr
   r8   Ztorch._inductor.utilsr   Ztorch.fx.graphr   r   utilsr   r   r   Ztorch._inductor.graphr   r#   r   r/   r0   r;   r<   rb   r   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rt   r   r   r   r   <module>   sJ    "
#:








2"
>