
    Vh                        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jJ                  e&      Z' ejP                  d      d        Z)h dZ*ejV                  fd	Z, e-       Z.ejV                  dd
de/ej`                  ef   deejb                  e	f   fdZ2y)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                  *   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         j0                  dt         j2                  dt         j4                  dt         j6                  dt4        dt6        dt8        dt         j:                  j<                  j>                  j@                  dt         jB                  dtD        dtF        dtH        dtJ        dtL        d tN        d!i} d"D ]  }|| tQ        t         |      <    | 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   Absr5   r6   r   r   r   	functions
elementaryr;   ExprCondPair	Piecewiser   r   r   r   r   r	   getattr)HANDLERSnames     I/home/dcms/DCMS/lib/python3.12/site-packages/torch/utils/_sympy/interp.pyhandlersra   6   si   2%2		62 	$2 	$	2
 	$2 	$2 	$2 	$2 			62 	M2 	i2 	*2 	*2 	g2 	w2  			5!2" 			5#2$ 	%%2& 	&'24 			#526 	U728 	592@ 			5A2B 			5C2D 			5E2F 			5G2H 			9I2J 			9K2L 	YM2N 	YO2P 	+Q2R 	"",,99;KS2T 	U2V 	*W2X 	*+SoF}lc2Hj V .)-%&. O    >   r/   r0   r!   r"   r8   r7   c                    t        |t        j                        rOt        |j                  d   t        j                  j
                  j                        r| j                  |d         S t        |t              r#| j                  |d   t        j                        S t        dt        j                  dt        j                  dt        dt         dt"        di}|j%                  |j&                        x} t)        | |      g || S |j&                  t        j*                  u rB|j,                  r6t/        | d      r*| j1                  |      }t2        j5                  d||       |S t/        |j&                  d	      r|j&                  j6                  }nt9               |j&                     }t)        | |      }	 |t:        v r_t=        |      dkD  sJ  ||d   |d         }t?        d
t=        |            D ]  }	 ||||	         } t2        j5                  d|||       |S  || }t2        j5                  d|||       |S # t@        $ r  tB        $ r t2        jE                  d||        w xY 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))#
isinstancerK   rW   argscorenumbersHalfsqrtr   to_dtypetorchfloat64r   floorceilingr   r
   r   getfuncr]   rU   
is_integerhasattrrh   r5   debugri   ra   ASSOCIATIVE_OPSlenrangeNotImplementedError	Exceptionwarning)
analysisrl   exprindex_dtypeINDEX_DTYPE_HANDLERShandler_namerhandleraccis
             r`   _run_sympy_handlerr   {   s   $		"z		!ejj((--( }}T!W%%$   a%--88 	N^}N=N -00;;H.wx.BBkBB yyEII$//gh	6RT"		%tQ/tyy/0yy44z$)),h-G?*t9q= =$q'47+C1c$i( ,c47+,IInlD#>JAIInlD!<H  3\4Hs   7A&H= H= =+I(r   missing_handlerenvr   c                ,   d }t        |t              rt        j                  }nUt        |t        j
                        rt        j                  }n*t        |t        j                        rt        j                  }|| j                  ||      S t        |t        j                        r5|j                  |t              x}t        ur|S |r ||      S t        |      t        | |j                  D cg c]  }t!        | ||||       c}||      S c c}w )Nr   )r   )rk   r   rr   boolrK   Integerint64NumberdoubleconstantSymbolrv   _nilKeyErrorr   rl   sympy_interp)r   r   r   r   r   dtyper   args           r`   r   r      s     E$$

	D%--	(	D%,,	'  u--	D%,,	't$$AT1H"4((4.   yy		
  ' /		
 	 		
s   0D
)3__doc__	functoolsloggingtypingr   r   rK   sympy.logic.boolalgr   SympyBooleanr   rr   rY   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__r5   	lru_cachera   r{   r   r   objectr   dictr   Exprr    rb   r`   <module>r      s        D       8 g! T> >B F :? 6r x *	ellC	 * 

L(
)*rb   