o
    0h                     @   s  d Z ddlZddlZddlmZmZ ddlZddlmZ	m
Z
 ddlZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ e%e&Z'e(ddd Z)h d	Z*ej+fd
dZ,e- Z.ej+ddde/ej0ef deej1e	f fddZ2dS )a  
This is a simple interpreter for Sympy expressions that dispatches to
classes following the torch._inductor.virtualized calling convention.
For directness, the interpreter takes the handler directly rather than
consulting the TLS.  It does not use most of the methods on the full
handler; only those with corresponding Sympy expressions.  To see an example
of a full handler, see torch.utils._sympy.value_ranges.ValueRangeAnalysis.
    N)AnyUnion)BooleanBooleanAtom   )BitwiseFn_bitwise_andBitwiseFn_bitwise_or	CeilToIntCleanDivFloatPowFloatTrueDivFloorDiv
FloorToIntIdentity
IntTrueDiv!IsNonOverlappingAndDenseIndicatorMaxMinModModularIndexingOpaqueUnaryFn_log2PowByNatural	PythonModRoundDecimal
RoundToIntToFloatTruncToFloat
TruncToIntWherec                  C   s6  i t jdt jdt jdt jdt jdt jdt jdt jdt j	d	t
d
tdtdtdtdtdt jdt jdi tdtdt jdtdtdt jdt jdt jdt jdt jdt jdtdtdtdt jjjj dt j!dt"dt#dt$dt%dt&d t'd!i} d"D ]	}|| t(t |< q| S )#Nor_and_eqneltgtlegenot_int_truedivtruedivfloordivtruncwhereaddmulpowpow_by_naturalmodabslogexpminimummaximummodular_indexingexpr_cond_pair	piecewiseidentity&is_non_overlapping_and_dense_indicatorround_decimallog2bitwise_and
bitwise_or)	cossintansinhcoshtanhasinacosatan))sympyOrAndEqNeLtGtLeGeNotr   r   r   r
   r   r   AddMulr   r   Powr   r   Absr3   r4   r   r   r   	functions
elementaryr9   ExprCondPair	Piecewiser   r   r   r   r   r   getattr)HANDLERSname r^   M/var/www/vscode/kcb/lib/python3.10/site-packages/torch/utils/_sympy/interp.pyhandlers6   s   	
 !"#$%&'()*+,5r`   >   r-   r.   r   r    r6   r5   c           
      C   s  t |tjrt |jd tjjjr| |d S t |tr&| 	|d t
jS tdtjdtjdtdtdtdi}||j }d urLt| |g ||R  S |jtju rh|jrht| drh| |}td|| |S t|jd	rs|jj}nt |j }t| |}z@|tv rt|dksJ ||d |d }td
t|D ]	}	||||	 }qtd||| |W S || }td||| |W S  t y     t!y   t"d||  w )Nr   r   trunc_to_intfloor_to_intceil_to_intround_to_intsym_sumzsym_sum(%s) -> %s_torch_handler_name   z%s(%s) -> %szfailed while executing %s(%s))#
isinstancerI   rU   argscorenumbersHalfsqrtr   to_dtypetorchfloat64r   floorceilingr   r	   r   getfuncr[   rS   
is_integerhasattrre   r3   debugrf   r`   ASSOCIATIVE_OPSlenrangeNotImplementedError	Exceptionwarning)
analysisri   exprindex_dtypeINDEX_DTYPE_HANDLERShandler_namerhandleraccir^   r^   r_   _run_sympy_handler{   sP   




r   r   missing_handlerenvr   c                   s   d }t |trtj}nt |tjrtj}n	t |tjrtj}|d ur( 	||S t |tj
rD|t }tur:|S r@|S t|t  fdd|jD |dS )Nc              	      s   g | ]}t  |d qS )r   )sympy_interp).0argr~   r   r   r   r^   r_   
<listcomp>   s    z sympy_interp.<locals>.<listcomp>)r   )rh   r   ro   boolrI   Integerint64NumberdoubleconstantSymbolrs   _nilKeyErrorr   ri   )r~   r   r   r   r   dtyper   r^   r   r_   r      s.   	

r   )3__doc__	functoolsloggingtypingr   r   rI   sympy.logic.boolalgr   SympyBooleanr   ro   rW   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	getLogger__name__r3   	lru_cacher`   rx   r   r   objectr   dictr   Exprr   r^   r^   r^   r_   <module>   s*   	h

A9