
    ɯwg                         d dl Z d dlZd dlmZ d dlmZmZmZ d dlm	Z	 d dl
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 j0                  e      Z G d
 d      Zy)    N)partial)AnyCallableDict)Expr)bound_sympyValueRangeAnalysisValueRanges   )InterpreterShimLoopBodyLoopBodyBlock)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 }|| _         |j                  j                         D ci c]"  \  }}|t        t           d ||      dz
        $ c}}| _        t        d | j                   j                         D              | _        i | _	        y c c}}w )Nc                 P    t        | t              rt        |       j                  S | S N)
isinstancer   r   upper)vs    [/home/mcse/projects/flask/flask-venv/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   
var_rangesitemsr
   r   replacement_valsr   	get_nodesunbounded_vars_bounds)selfr   r   kr   s        r   __init__zBoundVars.__init__   s    	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   r+   r-   r.   )r/   s    r   __repr__zBoundVars.__repr__1   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   
submodulesr-   r   r#   strr
   r   unknownr.   r   set_ops_handlerr	   r   
root_blockgraphlogdebugrunget_ops_handler)r/   r<   r'   interpreters       r   
get_boundszBoundVars.get_bounds:   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*r<   .c                 |    i |j                         D ]  }|dk(  r j                  |<   d|v r+ j                  j                  |   } fd} ||      |<   Gd|v rLt	        |t        d      d        } j                  j                  |   }t         j                  |      }||<   d|v sJ ||   |<    S )N	get_indexr"   c                       fdS )Nc                 B    j                  j                  | |      S r   )r"   r.   )maskvalueresultr/   subblocks     r   <lambda>z<BoundVars.swap_submodules.<locals>.make_fn.<locals>.<lambda>^   s!    t/C/C $,,eV0 r    )rO   rN   r/   s   `r   make_fnz*BoundVars.swap_submodules.<locals>.make_fn]   s     r   r8   scan)	keysrI   r   	subblocksintlenindirect_varsr   r8   )	r/   r<   keyrO   rR   idxvarindirectrN   s	   `       @r   r;   zBoundVars.swap_submodulesM   s     ?A??$ 	.Ck!"nns"c)>>33C8
 &h/s3&#c.1345nn2237"4#4#4c:&s}$}(os3	.6 r   rO   envrL   rM   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 )Nr9   outputr   r   )	r   rA   rD   r   rE   nodesr#   rW   r]   )	r/   rO   r]   rL   rM   r<   interpr'   r_   s	            r   r"   zBoundVars.masked_subblockn   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
   r+   )r/   rb   rc   s      r   r8   zBoundVars.set_indirect~   s'    #{+++%(c"
r   namec                     | j                   j                  |   }| j                  j                  |      }|t	        || j                        }|| j                  |<   |S r   )r   indexing_exprsr+   getr   )r/   re   exprbounds       r   rI   zBoundVars.get_index   sZ    ~~,,T2%%))$/=d&;&;<E ',d#r   )r5   
__module____qualname____doc__r   r1   r=   r6   r   r   torchfxNoder
   r   rG   r   r   r;   r   r"   r8   rI   rQ   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B%% %((--T!223% 	%
 % hsCx001% 
T	%  ;t+< TAR 
	d 	{4'8 	r   r   )loggingr$   	functoolsr   typingr   r   r   sympyr   rn   torch.utils._sympy.value_rangesr   r	   r
   r   r   r   r   utilsr   r   virtualizedr   	getLoggerr5   rB   r   rQ   r   r   <module>ry      sL       & &   X X ? ? 1  g!x xr   