
    IЦi                         S SK r S SKrS SKJr  S SKJrJrJrJr  S SK	J
r
  S SKrS SKJrJrJr  SSKJrJrJr  SSKJrJr  SS	KJr  \ R2                  " \5      r " S
 S5      rg)    N)partial)AnyCallableDictUnion)Expr)bound_sympyValueRangeAnalysisValueRanges   )InterpreterShimLoopBodyLoopBodyBlock)cache_on_selfdominated_nodes)Vc                   v   \ rS rSrSrS\SS4S jrS\4S jr\	S\
\R                  R                  \\   4   4S j5       rS	\
\\S
\4   4   S\
\\S
\\   4   4   4S jrS\S\
\R                  R                  \\   4   S\S\S	\
\\S
\4   4   S\\   4S jrS\S\\   S\\   4S jrS\S\\   4S jrSrg)	BoundVars   a  
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   S[         [        [        4   S[        4S jnXl        UR                  R                  5        VVs0 s H#  u  p4U[        [           " SU" U5      S-
  5      _M%     snnU l        [        S U R                  R                  5        5       5      U l
        0 U l        g s  snnf )Nvr   c                 Z    [        U [        5      (       a  [        U 5      R                  $ U $ N)
isinstancer   r	   upper)r   s    U/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/_inductor/bounds.pyupper_bound'BoundVars.__init__.<locals>.upper_bound   s#    +5a+>+>;q>''EAE    r   r   c              3      #    U  H:  nUR                   S S[        R                  4;   d  SUR                   ;   d  M6  Uv   M<     g7f)load	reductionmasked_subblockN)targetoperatorgetitem).0nodes     r   	<genexpr>%BoundVars.__init__.<locals>.<genexpr>'   s>      .
2{{v{H4D4DEE DKK/ D2s
   5A	A)r   r   intr   
var_rangesitemsr   replacement_valsr   	get_nodesunbounded_vars_bounds)selfr   r   kr   s        r   __init__BoundVars.__init__   s    	F5s+ 	F 	F # ",,224!
4 {4 KNQ$6774!

 . .
002.
 
 @B!
s   *B,c           
          U R                   R                   SU R                   SU R                   SU R                   SU R
                   S3
$ )Nz(loop_body=z,
 replacement_vals=z, 
unbounded_vars=z, 
_bounds=))	__class____name__r   r0   r2   r3   )r4   s    r   __repr__BoundVars.__repr__0   s]    ~~&&' (( )  $ 5 56 7"112 3||nA	'	
r!   c                    U R                  U R                  R                  5      nU R                   Hn  n[	        UR
                  [        5      (       a$  SUR
                  ;  d  M4  SUR
                  ;  d  MF  [        [           R                  5       U R                  U'   Mp     [        R                  " [        5       5         [        U R                  R                  R                   U5      n["        R%                  SU R                  R                  R                   5        UR'                  [        R(                  " 5       U R                  S9  S S S 5        U R                  $ ! , (       d  f       U R                  $ = f)Nr%   set_indirectzget_bounds:
%sinitial_env)swap_submodulesr   
submodulesr2   r   r&   strr   r   unknownr3   r   set_ops_handlerr
   r   
root_blockgraphlogdebugrunget_ops_handler)r4   rC   r*   interpreters       r   
get_boundsBoundVars.get_bounds9   s   ))$..*C*CD
 ''Ddkk3//!4"$++5%0%6%>%>%@T" ( 134)$..*C*C*I*I:VKII')B)B)H)HIOOA--/T\\OJ 5 ||	 54 ||s   BE""
E;rC   .c                   ^ ^ 0 mUR                  5        H  nUS:X  a  T R                  TU'   M  SU;   aU  T R                  R                  U   nS[        S[
        [        [        /[        [           4   4UU 4S jjnU" U5      TU'   Mu  SU;   aM  [        U[        S5      S  5      nT R                  R                  U   n[        T R                  U5      nUTU'   M  SU;   d   eX   TU'   M     T$ )N	get_indexr%   subblockr   c                    >^  UUU 4S j$ )Nc                 @   > TR                  TTR                  XT5      $ r   )r%   r3   )maskvalueresultr4   rR   s     r   <lambda><BoundVars.swap_submodules.<locals>.make_fn.<locals>.<lambda>_   s    t/C/C $,,V0r!    )rR   rW   r4   s   `r   make_fn*BoundVars.swap_submodules.<locals>.make_fn\   s     r!   r?   scan)keysrQ   r   	subblocksr   r   r   r   r   r-   lenindirect_varsr   r?   )	r4   rC   keyrR   r[   idxvarindirectrW   s	   `       @r   rB   BoundVars.swap_submodulesL   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!   rR   envrU   rV   c                 2   [        UR                  U5      nUR                  [        R                  " 5       US9  UR                  R
                   Vs/ s H  owR                  S:X  d  M  UPM     nn[        U5      S:X  d   eUR                  US      $ s  snf )Nr@   outputr   r   )	r   rH   rK   r   rL   nodesr&   r`   rg   )	r4   rR   rg   rU   rV   rC   interpr*   ri   s	            r   r%   BoundVars.masked_subblocko   s     !<

1$$&C
8#+>>#7#7S#74;;(;R$#7S6{a zz&)$$	 Ts   B)Boldnewc                 P    [        U[        5      (       d   eX R                  U'   U$ r   )r   r   r0   )r4   rm   rn   s      r   r?   BoundVars.set_indirect   s(    #{++++%(c"
r!   namec                     U R                   R                  U   nU R                  R                  U5      nUc  [	        X R                  5      nX0R                  U'   U$ r   )r   indexing_exprsr0   getr	   )r4   rq   exprbounds       r   rQ   BoundVars.get_index   sV    ~~,,T2%%))$/=&;&;<E ',d#r!   )r3   r   r0   r2   )r;   
__module____qualname____firstlineno____doc__r   r6   rD   r<   r   r   torchfxNoder   r   rN   r   r   rB   r   r%   r?   rQ   __static_attributes__rZ   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   )loggingr'   	functoolsr   typingr   r   r   r   sympyr   r|   torch.utils._sympy.value_rangesr	   r
   r   r   r   r   r   utilsr   r   virtualizedr   	getLoggerr;   rI   r   rZ   r!   r   <module>r      sJ       - -   X X ? ? 1  !z zr!   