
    Vh%                        d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	Z	d dl	m
Z
 d dlZd dlmZmZmZ ddl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 d	dlm Z   e jB                  e"      Z# G d d      Z$ G d dee      Z%y)    N)partial)AnyCallableOptionalUnion)Expr)bound_sympySymPyValueRangeAnalysisValueRanges   PowByNatural)int_oo   )InterpreterShimLoopBodyLoopBodyBlock)DefaultHandlerReductionType	StoreMode)cache_on_selfdominated_nodes)Vc                   `   e Zd ZdZdeddfdZdefdZede	e
j                  j                  ee   f   fd       Zde	eed	ef   f   de	eed	ee   f   f   fd
Zdede	e
j                  j                  ee   f   dedede	eed	ef   f   dee   fdZdedee   dee   fdZdedee   fdZy)	BoundVarsa  
    Performs Value Range Analysis on LoopBody's fx graph by calling BoundVars.run()
    It exposes the ranges of the nodes in the `bounds` variable

    Note. A current limitation of this analysis is that it just works on a per-loop basis.
    We should be able to propagate the bounds between across the whole graph. This may benefit
    the case a bounded variable is returned by a kernel and fed into another.
    	loop_bodyreturnNc                 ^   dt         t        t        f   dt        fd}|| _        |j                  j                         D ci c]"  \  }}|t        t           d ||      dz
        $ c}}| _        t        d | j                  j                         D              | _
        i | _        y c c}}w )Nvr   c                 P    t        | t              rt        |       j                  S | S N)
isinstancer   r	   upper)r   s    F/home/dcms/DCMS/lib/python3.12/site-packages/torch/_inductor/bounds.pyupper_boundz'BoundVars.__init__.<locals>.upper_bound&   s     +5a+>;q>''EAE    r   r   c              3   z   K   | ]3  }|j                   d dt        j                  fv sd|j                   v r| 5 yw)load	reductionmasked_subblockN)targetoperatorgetitem).0nodes     r$   	<genexpr>z%BoundVars.__init__.<locals>.<genexpr>/   s=      .
{{v{H4D4DEE DKK/ .
s   9;)r   r   intr   
var_rangesitemsr   replacement_valsr   	get_nodesunbounded_vars_bounds)selfr   r%   kr   s        r$   __init__zBoundVars.__init__%   s    	F5s+ 	F 	F # ",,224!
1 {4 KNQ$677!

 . .
002.
 
 @B!
s   'B)c           
          | j                   j                   d| j                   d| j                   d| j                   d| j
                   d
S )Nz(loop_body=z,
 replacement_vals=z, 
unbounded_vars=z, 
_bounds=))	__class____name__r   r4   r6   r7   )r8   s    r$   __repr__zBoundVars.__repr__8   s]    ~~&&' (( )  $ 5 56 7"112 3||nA	'	
r&   c                    | j                  | j                  j                        }| j                  D ]b  }t	        |j
                  t              rd|j
                  vs,d|j
                  vs;t        t           j                         | j                  |<   d t        j                  t                     5  t        | j                  j                  j                   |      }t"        j%                  d| j                  j                  j                          |j'                  t        j(                         | j                         d d d        | j                  S # 1 sw Y   | j                  S xY w)Nr*   set_indirectzget_bounds:
%sinitial_env)swap_submodulesr   
submodulesr6   r"   r+   strr   r   unknownr7   r   set_ops_handlerValueRangeAnalysisr   
root_blockgraphlogdebugrunget_ops_handler)r8   rE   r/   interpreters       r$   
get_boundszBoundVars.get_boundsA   s   ))$..*C*CD
 '' 	ADdkk3/!4"$++5%0%6%>%>%@T"	A 134 	K)$..*C*C*I*I:VKII')B)B)H)HIOOA--/T\\OJ	K ||		K ||s   4BEE*rE   .c                     i |j                         D ]  }|dk(  r j                  |<   d|v rR j                  j                  |   }dt        dt
        t        t        gt        t           f   f fd} ||      |<   nd|v rLt        |t        d      d        } j                  j                  |   }t         j                  |      }||<   d|v sJ ||   |<    S )N	get_indexr*   subblockr   c                       fdS )Nc                 B    j                  j                  | |      S r!   )r*   r7   )maskvalueresultr8   rT   s     r$   <lambda>z<BoundVars.swap_submodules.<locals>.make_fn.<locals>.<lambda>g   s!    t/C/C $,,eV0 r&    )rT   rY   r8   s   `r$   make_fnz*BoundVars.swap_submodules.<locals>.make_fnd   s     r&   rA   scan)keysrS   r   	subblocksr   r   r   r   r   r1   lenindirect_varsr   rA   )	r8   rE   keyrT   r\   idxvarindirectrY   s	   `       @r$   rD   zBoundVars.swap_submodulesT   s     ?A??$ 	.Ck!"nns"c)>>33C8+sCj+d*;;< &h/s3&#c.1345nn2237"4#4#4c:&s}$}(os7	.: r&   rT   envrW   rX   c                 ,   t        |j                  |      }|j                  t        j                         |       |j                  j
                  D cg c]  }|j                  dk(  s| }}t        |      dk(  sJ |j                  |d      S c c}w )NrB   outputr   r   )	r   rK   rN   r   rO   nodesr+   r`   rf   )	r8   rT   rf   rW   rX   rE   interpr/   rh   s	            r$   r*   zBoundVars.masked_subblockw   s     !<

1$$&C
8#+>>#7#7S44;;(;R$SS6{a zz&)$$	 Ts   B)Boldnewc                 H    t        |t              sJ || j                  |<   |S r!   )r"   r   r4   )r8   rk   rl   s      r$   rA   zBoundVars.set_indirect   s'    #{+++%(c"
r&   namec                     | j                   j                  |   }| j                  j                  |      }|t	        || j                        }|| j                  |<   |S r!   )r   indexing_exprsr4   getr	   )r8   rn   exprbounds       r$   rS   zBoundVars.get_index   sZ    ~~,,T2%%))$/=d&;&;<E ',d#r&   )r>   
__module____qualname____doc__r   r:   rF   r?   r   dicttorchfxNoder   r   rQ   r   r   rD   r   r*   rA   rS   r[   r&   r$   r   r      sG   B( Bt B&
# 
 DD0A!AB  $!sHS#X$667!	c8CT!2233	4!F%% %((--T!223% 	%
 % hsCx001% 
T	%  ;t+< TAR 
	c 	k$&7 	r&   r   c                   0   e Zd ZddZedededee   fd       Zdede	edf   de
eef   defd	Zded
ej                  dee   fdZ	 dded
ej                  dededdf
dZdej$                  dej$                  dededee   f
dZed
edej$                  dee   fd       Ze	 	 ddedej$                  deej$                     dedee   f
d       Zededee   fd       Zededee   fd       Zedededee   fd       Zedededee   fd       Zy) rI   r   Nc                 R    d| _         d}|D ]  }t        | || j                          y )NrI   )xorlogical_and
logical_orlogical_not)rn   setattrbool_handler)r8   boolean_operatorsops      r$   r:   zValueRangeAnalysis.__init__   s4    (	
 $ 	1BD"d//0	1r&   argskwargsc                  R    t        t        j                  t        j                        S r!   )r   sympyfalsetrue)r   r   s     r$   r   zValueRangeAnalysis.bool_handler   s     5;;

33r&   rn   .c                 *    t        j                         S r!   r   rG   )r8   rn   r   r   s       r$   _defaultzValueRangeAnalysis._default   s     ""$$r&   indexc                 *    t        j                         S r!   r   )r8   rn   r   s      r$   r(   zValueRangeAnalysis.load   s    ""$$r&   rX   modec                      y r!   r[   )r8   rn   r   rX   r   s        r$   storezValueRangeAnalysis.store   s     	r&   dtype	src_dtypereduction_typec                 *    t        j                         S r!   r   )r8   r   r   r   rX   s        r$   r)   zValueRangeAnalysis.reduction   s     ""$$r&   c                 J    t        |t              sJ | j                  ||      S r!   )r"   r   to_dtype)clsr   r   s      r$   
index_exprzValueRangeAnalysis.index_expr   s#    %---||E5))r&   xuse_compute_typesc                    t        j                  |       } |t        j                  k(  r| j	                         r"t        j                  | j
                  dk7        S | j                  r| S d| vr#t        j                  t        j                        S t        t        j                  t        j                        S dt        dt        j                  dt        j                  fd}| j                  rV| j	                         r,| j
                  rdnd}t        j                   |||            S t         |d|       |d|            S t         || j
                  |       || j                  |            S )Nr   r   r   r   c                     |j                   rt        j                  |       S | t        t         fv r| S 	 t        j                  |       S # t
        $ r | cY S w xY wr!   )is_floating_pointr   Floatr   Integer	TypeError)r   r   s     r$   castz)ValueRangeAnalysis.to_dtype.<locals>.cast   sU    &&{{1~%&))H ==++  Hs   A	 	AAr   )r   wraprx   boolis_singletonloweris_boolr   r   r   r   r   r   r#   )r   r   r   r   r   vals         r$   r   zValueRangeAnalysis.to_dtype   s    QEJJ~~"''155!"''

33"5;;

;;	C 	 	

 	 99~~77a"''S%(899"45>45>BB tAGGU3T!''55IJJr&   c                 0    t        j                  | d       S )Nc                     t        | d      S )Nr   r   )ys    r$   rZ   z+ValueRangeAnalysis.square.<locals>.<lambda>   s    LA<N r&   )r   convex_min_zero_mapr   s    r$   squarezValueRangeAnalysis.square   s    ..q2NOOr&   c                 J    t        j                  | t        j                        S r!   )r   decreasing_mapr,   negr   s    r$   r   zValueRangeAnalysis.neg   s    ))!X\\::r&   abc                 z    | j                  ||      }|t        j                         k(  r|S | j                  |      S r!   )truedivr   rG   trunc)r   r   r   r   s       r$   truncdivzValueRangeAnalysis.truncdiv   s6    KK1##%%Hyy|r&   c                 D    | j                  || j                  |            S r!   )addr   )r   r   r   s      r$   subzValueRangeAnalysis.sub  s    wwq#''!*%%r&   )r   Nr!   )NT)r>   rt   ru   r:   staticmethodr   r   r   rF   tuplerw   r   r   r   r(   r   r   rx   r   r   r)   classmethodr   r   r   r   r   r   r   r   r[   r&   r$   rI   rI      sH   	1 4C 43 4;s3C 4 4%S %c3h %c3h %TW %
% %UZZ %K4D % KO %

36>G	
%{{% ;;% &	%
 % 
S	% *s *5;; *;s;K * *  ,0"&	'K'K{{'K EKK('K  	'K
 
S	'K 'KR P# P+c* P P ;s ;{3' ; ;   S)9   &C &C &K$4 & &r&   rI   )&loggingr,   	functoolsr   typingr   r   r   r   r   r   rx   torch.utils._sympy.value_rangesr	   r
   r   utils._sympy.functionsr   utils._sympy.numbersr   r   r   r   r   ops_handlerr   r   r   utilsr   r   virtualizedr   	getLoggerr>   rL   r   rI   r[   r&   r$   <module>r      ss       1 1     2 ) ? ? A A 1  g!z zzk&0. k&r&   