
    Ph                         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  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edeej                  j                  ef   fd       Zdeeedef   f   deeedef   f   fd	Zd
edeej                  j                  ef   dededeeedef   f   defdZdededefdZded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           
      X   || _         |j                  j                         D ci c]<  \  }}|t        |t              s|j
                  rt        d|dz
        n
t        |      > c}}| _        t        d | j                   j                         D              | _        i | _        y c c}}w )Nr   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     aC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_inductor/bounds.py	<genexpr>z%BoundVars.__init__.<locals>.<genexpr>!   s=      .
2{{v{H4D4DEE DKK/ 2s   9;)r   
var_rangesitems
isinstanceint	is_numberr
   r   replacement_valsr   	get_nodesunbounded_vars_bounds)selfr   kvs       r    __init__zBoundVars.__init__   s    "
 ",,224	!
 51 1c"akk 1a!e$Q  5	!
 . .
002.
 
 :<!
s   AB&c                 d   | j                  | j                  j                        }| j                  D ][  }t	        |j
                  t              rd|j
                  vs,d|j
                  vs;t        j                         | j                  |<   ] t        j                  t                     5  t        | j                  j                  j                  |      }|j!                  t        j"                         | j                         d d d        | j                  S # 1 sw Y   | j                  S xY w)Nr   set_indirectinitial_env)swap_submodulesr   
submodulesr)   r$   r   strr
   unknownr*   r   set_ops_handlerr	   r   
root_blockgraphrunget_ops_handler)r+   r4   r   interpreters       r    
get_boundszBoundVars.get_bounds*   s    ))$..*C*CD
 ''Ddkk3/!4"$++5%0%8%8%:T" ( 134)$..*C*C*I*I:VKOOA--/T\\OJ 5 || 5 ||s   -ADD/r4   .c                 b    i |j                         D ]  }|dk(  r j                  |<   d|v r+ j                  j                  |   } fd} ||      |<   Gd|v sJ t	        |t        d      d        } j                  j                  |   }t         j                  |      }||<    S )N	get_indexr   c                       fdS )Nc                 B    j                  j                  | |      S N)r   r*   )maskvalueresultr+   subblocks     r    <lambda>z<BoundVars.swap_submodules.<locals>.make_fn.<locals>.<lambda>M   s    t/C/C $,,eV0     )rF   rE   r+   s   `r    make_fnz*BoundVars.swap_submodules.<locals>.make_fnL   s     rH   r0   )	keysr?   r   	subblocksr%   lenindirect_varsr   r0   )	r+   r4   keyrF   rJ   idxvarindirectrE   s	   `       @r    r3   zBoundVars.swap_submodules<   s     9;??$Ck!"nns"c)>>33C8
 &h/s &,,,#c.1345nn2237"4#4#4c:&s1 %4 rH   rF   envrC   rD   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 )Nr1   outputr   r   )	r   r9   r:   r   r;   nodesr   rM   rS   )	r+   rF   rS   rC   rD   r4   interpr   rU   s	            r    r   zBoundVars.masked_subblock\   s     !<

1$$&C
8#+>>#7#7S#744;;(;R$#7S6{a zz&)$$	 Ts   B)Boldnewc                 H    t        |t              sJ || j                  |<   |S rB   )r$   r
   r'   )r+   rX   rY   s      r    r0   zBoundVars.set_indirectl   s'    #{+++%(c"
rH   namec                     | j                   j                  |   }| j                  j                  |      }|t	        || j                        }|| j                  |<   |S rB   )r   indexing_exprsr'   getr   )r+   r[   exprbounds       r    r?   zBoundVars.get_indexq   sZ    ~~,,T2%%))$/=d&;&;<E ',d#rH   )__name__
__module____qualname____doc__r   r.   r   r   torchfxNoder
   r=   r5   r   r   r3   r   r   r   r0   r?   rI   rH   r    r   r      s   <( <t <$ D!;<  "sHS#X$667	c8C,--	.@%% %((--,-% 	%
 % hsCx001% 
%  ; ; 
	d 	{ 	rH   r   )r   	functoolsr   typingr   r   r   sympyr   re   torch.utils._sympy.value_rangesr   r	   r
   irr   r   r   utilsr   r   virtualizedr   r   rI   rH   r    <module>ro      s5      & &   X X 8 8 1 l lrH   