o
    h!                     @   sX  d dl Z d dlmZ d dl mZ d dlmZ d dl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 ZG dd dZG dd dZ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"G dd dZ#G d d! d!Z$e$ Z%G d"d# d#Z&e& Z'dS )$    N)contextmanager)chain)local)Any)patch)IndentedBuffer)inplace_methodsmagic_methods   )reduction_num_outputs	sympy_strsympy_symbolc                   @   s0   e Zd ZdZ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.
    c                 C   s   d| | _ || _d S )N__torchinductor_)_key_default)selfvnamedefault r   Q/var/www/html/ai/venv/lib/python3.10/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   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   +   s
   zVirtualized._get_handlerc                 C   s   t |  |S r   )r!   r   )r   namer   r   r   __getattr__1      zVirtualized.__getattr__N)__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+   5   s    r+   c                 C   s   t | tjr
t| S t| S r   )
isinstancesympyExprr   str)ar   r   r   _arg_str9   s   r1   c                   @   s:   e Zd Zdd Zedd ZedddZedd	 Zd
S )MockHandlerc                    s    dkrdS  fdd}|S )Nr$   r2   c                     s>   dd | D }| dd | D  d  dd| dS )	Nc                 S   s   g | ]}t |qS r   )r1   .0r0   r   r   r   
<listcomp>E   s    z:MockHandler.__getattr__.<locals>.inner.<locals>.<listcomp>c                 s   s"    | ]\}}| d | V  qdS )=Nr   r4   kvr   r   r   	<genexpr>F   s     z9MockHandler.__getattr__.<locals>.inner.<locals>.<genexpr>zops.(, ))extenditemsjoin)argskwargsfargsr$   r   r   innerD   s   z&MockHandler.__getattr__.<locals>.innerr   r   r$   rE   r   rD   r   r%   @   s   zMockHandler.__getattr__c                 C   s   d|  d|  d| dS )Nzops.masked(r<   r=   r   )maskbodyotherr   r   r   maskedK   s   zMockHandler.maskedTc                 C   s   t dt|  dS )Nr;   r=   )r   r/   )	index_varsizecheckr   r   r   indirect_indexingO   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)rA   format_stringr   r   rE   V   s   
z:MockHandler._init_cls.<locals>.make_handler.<locals>.inner)staticmethod)rQ   rE   r   rP   r   make_handlerU   s   z+MockHandler._init_cls.<locals>.make_handler)r   r	   r?   r   r   )clsrS   r$   rQ   r   r   r   	_init_clsS   s   zMockHandler._init_clsNT)	r'   r(   r)   r%   rR   rJ   rN   classmethodrU   r   r   r   r   r2   ?   s    
r2   c                   @   s:   e Zd Zdd ZedddZdd Z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   rY   r   r   r   r   c   s   
zKernelFormatterHandler.__init__Nc              	   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(r<   z):c                 S   s(   g | ]}t t|ttjfrd n|qS )_)r/   r,   intr-   Integerr4   r9   r   r   r   r5   u   s    z7KernelFormatterHandler.ir_to_string.<locals>.<listcomp> = allow_indexingT)irr^   rX   r2   rZ   indent	writeliner@   zipVset_ops_handlerr   objectgetvalue)ir_fnr_   r`   r^   rA   names	formatterr$   arglhsresultr   r   r   ir_to_stringh   s,   
Rz#KernelFormatterHandler.ir_to_stringc                    s    fdd}|S )Nc                     sL   t j | i |} dkr|S dtj }j| d|  |S )NrN   tmprf   )r!   rY   nextr]   rZ   rj   )rA   rB   linevarnamer$   r   r   r   rE      s   z1KernelFormatterHandler.__getattr__.<locals>.innerr   rF   r   r{   r   r%      s   	z"KernelFormatterHandler.__getattr__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 )rw   )rx   r]   )r4   rb   r#   r   r   r5          z4KernelFormatterHandler.reduction.<locals>.<listcomp>,rf   r
   r   )rY   	reductionr   rangerZ   rj   r@   tuple)r   dtype	src_dtypereduction_typer    ry   
num_valuesvarnamesr   r#   r   r~      s
   z KernelFormatterHandler.reductionc                 C   s   | j d|  | j  S )Nzreturn )rZ   rj   ro   )r   ru   r   r   r   ro      s   
zKernelFormatterHandler.getvaluer   )	r'   r(   r)   r   rR   rv   r%   r~   ro   r   r   r   r   rX   b   s    rX   c                   @   s   e Zd Zdd Zdd ZdS )WrapperHandlerc                 C   
   || _ d S r   )_inner)r   rE   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interpreterc                   @   sr   e Zd ZU dZe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    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(r=   r   r#   r   r   r   __repr__   r&   zOpsValue.__repr__c                 C      t | |S r   )r   addr   rI   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*   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   _unwrapr3   r   r   r   r5      s    z9OpsWrapper.__getattr__.<locals>.inner.<locals>.<listcomp>c                 S   s   i | ]
\}}|t |qS r   r   r7   r   r   r   
<dictcomp>   r|   z9OpsWrapper.__getattr__.<locals>.inner.<locals>.<dictcomp>)r?   r   _wrapr!   _ops)rA   rB   new_args
new_kwargsrD   r   r   rE      s   z%OpsWrapper.__getattr__.<locals>.innerr   rF   r   rD   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   re   r   r   r   r:      s    z%OpsWrapper._unwrap.<locals>.<genexpr>)r,   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   re   r   r   r   r:      s    z#OpsWrapper._wrap.<locals>.<genexpr>)r,   r   r   r   r   r   r   r   r      s   zOpsWrapper._wrapTc                 C   s   t | } t| ||S r   )r   r   r   rN   )r_   rL   rM   r   r   r   rN      s   
zOpsWrapper.indirect_indexingNrV   )	r'   r(   r)   r*   r%   rR   r   r   rN   r   r   r   r   r      s    

r   c                   @   s   e Zd ZeZeZeZejZej	Z
ejZejZej	ZejZej	ZejZejZejZedef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 )_Vreturnc                 C      t  S )z9The operator handler specific to the current codegen task)r   r   r#   r   r   r   r        z_V.opsc                 C   r   z#The graph currently being generated)_graphr   r#   r   r   r   r     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   #  r   z_V.fake_modec                 C   r   )z$The kernel currently being generated)_kernelr   r#   r   r   r   r   (  r   z	_V.kernelc                 C   r   r   )_debugr   r#   r   r   r   r   -     z_V.debugc                 C   r   r   )_interpreterr   r#   r   r   r   r   1  r   z_V.interpreterN)!r'   r(   r)   r2   rX   r   r   r   rm   r   get_ops_handlerr   set_graph_handlerr   set_real_inputsget_real_inputsr   set_fake_modeget_fake_moder   set_kernel_handlerr   set_debug_handlerr   set_interpreter_handlerpropertyr   r   r   r   r   r   r   r   r   r   r   r     s8    




r   )(r[   
contextlibr   r   	threadingr   typingr   unittest.mockr   r-   torch._inductor.utilsr   torch.fx.graphr   r	   utilsr   r   r   r   r   r+   r1   r2   rX   r   rU   r   r   r   r   r   r   r   r   r   r   r   rl   r   r   r   r   <module>   s<    "#8






2"
2