
    קg                       U d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZ ddlmZ ddlmZmZ ddl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$ ddl%m&Z& ddl'Z'ddl(Z'ddl)m*c mZ+ dd	l,m-Z. dd
l/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6m7Z7 ddl8m9Z9m:Z: ddl'm;Z;m<Z<m=Z= ddl>m?Z?m@Z@mAZA ddlBmCZC ddlDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZL ddlMmNZN ddlOmPZP ddlQmRZRmSZSmTZTmUZU ddlVmWZW ddlXmYZYmZZZ ddl[m\Z\ ddl]m^Z^ ddl_m`c maZb ddlcmdZdmeZemfZf ddl8mgZg e$rddlhmiZi eZjeZk ejl        em          ZnddloZoddlpmqZq ddlrmsZsmtZt  G d deu          Zv G d d eu          Zwe'jx        jy        jz        Zzg d!Z{d"Z|d#Z}d$ Z~d% Z ed          d&e e         fd'            Z G d( d)eu          Zd&efd*Ze#e'j=        ef         Zd+ee         d&ee         fd,Zdd-e#e'j=        ef         d.ee         d&efd/Ze#e'j=        e'j<        e'j;        eeef         Zd-ed&efd0Zd-e#ee=f         d&efd1Zd2Ze&ed3<   d4e#e'j;        ef         d&efd5Zdd6Zd7 Zd8e'j*        j        fd9Zd:e'j*        j        d&efd;Zd4ed&efd<Zd4ed&efd=Zd4eoj        d&eoj        fd>Zd-e#ee;f         d&efd?Zd@ ZdAe#e=e'j        f         d&eeoj                 fdBZdAe#e=eoj        e'j        f         d&e eoj                 fdCZdAe#e=e'j        f         d&efdDZdE Zd&eeoj                 fdFZdG Z edHI           G dJ dK                      Z edHI           G dL dM                      Z edHI           G dN dO                      Z edHI           G dP dQ                      ZddSZdT ZdU ZdVe#ee;f         d&efdWZdX ZdY ZdZ Zd[ Zd\eoj        d]ed^efd_Zd` Zddaee         dbee         fdcZddddaee         dbee         fdeZd-e'j=        dfe'j=        d&dfdgZddhefdiZdj Zdk Zdl Zdm Zdn ZdHdodpZdq Z G dr dse          Z edHI           G dt du                      Z edHI           G dv dwe                      Z edHI           G dx dye                      Ze#eedf         Z edHI           G dz d{e                      Zd| Zd} Zd&efd~Z edHI           G d d                      Z edHI           G d deƦ                      Z edHI           G d deǦ                      Z edHI           G d deȦ                      ZdAe#ee=ee<ee;f         d&efdZeIfZ ed          d             Zd Zd Zd ZdVeoj        d&eoj        fdZde'j;        d&e'j=        fdZi dej        dej        dej        dej        dej        dej        dej        deٓdeړdej        dej        dej        dej        dej        deΓdej        dej        ej        ej        ej        eej        ej        ej        ej        ej        ej        d
ZddZ edHI           G d d                      Z G d deq          Z G d de          Z G d de          Z G d deq          Z G d d          Z ej                    Z edHI           G d d                      Z G d d          Zd Zd Z G d de'j*        j                  Zd Zd Z G d deoj        j        jq                  Zd Zd ZdS )a  
``torch.fx.experimental.symbolic_shapes`` provides interfaces for interacting with
our symbolic shapes reasoning system that is used heavily in torch.compile.  Although
this is not generally considered public API, when writing framework code in PyTorch
as well as extensions to PyTorch (e.g., in custom operator implementations), you may
need to make use of these APIs to setup dynamic shapes support appropriately.
    N)defaultdict)contextmanager)	dataclassfield)Enum)AnycastCallableDictIterableListOptionalSequenceSetTupleTypeUnionTYPE_CHECKING)	TypeAlias)_config)FakeTensorMetaShapeEnvEventrecord_shapeenv_eventreplay_shape_env_eventsshape_env_check_state_equal)SymNodeSymTypes)trace_structured
structured)SymBoolSymFloatSymInt)
ShapeGuardSourceTracingContext)is_traceable_wrapper_subclass)ApplicationFloorDivMod	PythonMod!IsNonOverlappingAndDenseIndicatorCleanDiv
FloorToInt	CeilToInt)	try_solve)int_oo)bound_sympySymPyValueRangeAnalysisValueRangesValueRangeErrorSingletonInt)format_frameCapturedTraceback)signpost_event)is_sparse_any)SymTmake_symbolsymbol_is_type)
LazyString)TensorPropertySource)
StrPrinter)
precedence
PRECEDENCEc                   4     e Zd ZU ej        ed<    fdZ xZS )GuardOnDataDependentSymNodecondc                 B     t                      j        |  || _        d S N)super__init__rE   )selfrE   args	__class__s      a/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/fx/experimental/symbolic_shapes.pyrI   z$GuardOnDataDependentSymNode.__init___   s"    $			    )__name__
__module____qualname__sympyExpr__annotations__rI   __classcell__rL   s   @rM   rD   rD   \   sH         
*        rN   rD   c                       e Zd ZdS )PendingUnbackedSymbolNotFoundNrO   rP   rQ    rN   rM   rX   rX   c           DrN   rX   )has_symbolic_sizes_stridescreate_contiguousShapeEnvis_concrete_int	guard_intguard_floatguard_scalarcanonicalize_bool_exprhint_intSYMPY_INTERPfree_symbolsis_symbol_binding_fx_nodeis_concrete_boolis_nested_intSHAPEENV_EVENT_KEYCURRENT_NODE_KEYhas_free_symbolssym_eqSymbolicContextStatelessSymbolicContextStatefulSymbolicContextSubclassSymbolicContextstatically_known_trueguard_size_obliviouscheck_consistentcompute_unbacked_bindingsConvertIntKeyrebind_unbackedresolve_unbacked_bindingsis_accessor_nodeshapeenv_eventcurrent_nodec                 l    t                               d| j        |                                            d S )Nzlru_cache_stats %s: %s)logdebugrO   cumulative_cache_info)	wrapped_fs    rM   log_lru_cache_statsr   y   s.    II&	(:I<[<[<]<]^^^^^rN   c                       fd}|S )Nc                     t          j                  |           j        ddfd}fd}|_        |_        t                              t          j                  rt          j	        t                     S )Nr   c                                                       } t          j        | j        z   | j        z   | j        | j                  S rG   )
cache_info	functools
_CacheInfohitsmissesmaxsizecurrsize)cur	prev_hitsprev_missesr   s    rM   r   z7lru_cache.<locals>.inner.<locals>.cumulative_cache_info   sG    &&((C'CH$cj(	  rN   c                  l                                     } | j        z  | j        z                d S rG   )r   r   r   )r   old_cache_clearr   r   r   s    rM   new_cache_clearz1lru_cache.<locals>.inner.<locals>.new_cache_clear   s@    &&((C!I3:%KOrN   )r   	lru_cachecache_clearr   r}   isEnabledForloggingDEBUGatexitregisterr   )fr   r   r   r   r   r   r   s      @@@@rM   innerzlru_cache.<locals>.inner   s    0I'0033	#/		 	 	 	 	 	 		 	 	 	 	 	 	 	 !0	*?	'GM** 	<O/;;;rN   rZ   )r   r   s   ` rM   r   r   ~   s$        @ LrN   returnc            	         dd l } dd l} dd l} dd l} t          j        t                   | j        j        j	        | j        j        j
        | j        j        | | j        j        | j        j        | j        j        | j        j        g	}d |D             S )Nr   c                 6    h | ]}t          j        |          S rZ   )inspectgetfile).0ms     rM   	<setcomp>z&uninteresting_files.<locals>.<setcomp>   s"    ---1GOA---rN   )torch._inductor.sizevarstorch._library.fake_impltorch._subclasses.meta_utilstorch._subclasses.fake_tensorsysmodulesrO   fxexperimental	recordingsym_nodeinterpreter	_inductorsizevars_library	fake_impl_subclasses
meta_utilsfake_tensor)torchmodss     rM   uninteresting_filesr      s    ########''''((((H'&  $%
D .-----rN   c                       e Zd ZdS )ConstraintViolationErrorNrY   rZ   rN   rM   r   r      r[   rN   r   c                     | j         S rG   )_has_symbolic_sizes_strides)elems    rM   r\   r\      s    ++rN   shapec                     dg}t          | d d                   D ] }|                    ||d         z             !t          t          |                    S )N   )reversedappendlist)r   stridesdims      rM   r]   r]      s\    Gcrc
## * *sWR[())))!!"""rN   afallbackc                     t          | t          j                  r| j                            |          S t          |           t          u s
J |             | S )z
    Retrieve the hint for an int (based on the underlying real values as observed
    at runtime).  If no hint is available (e.g., because data dependent shapes),
    if fallback is not None, use that instead (otherwise raise an error).
    )
isinstancer   r"   noderequire_hinttypeint)r   r   s     rM   rd   rd      sL     !U\"" -v""8,,,77c>>>1>>>HrN   c                 b    t          | t                    r| j                                        S dS NT)r   r   r   has_hintr   s    rM   r   r      s+    !X !v   4rN   c                     t          | t          t          f          sJ t          | t                    rdS t          | j        j        t
          j        j        j                  rdS dS )z Utility to check if underlying object
    in SymInt is concrete value. Also returns
    true if integer is passed in.

    Args:
        a (SymInt or int): Object to test if it int
    TF)	r   r"   r   r   exprrR   corenumbersIntegerr   s    rM   r_   r_      s]     a&#'''''!S t!&+uz19:: t5rN   zsympy.logic.boolalg.BooleanSympyBooleanr   c                     t          | t          j                  r| j                            dd          S t          | t
                    s
J |             | S )a  
    Perform a guard on a symbolic boolean expression in a size oblivious way.
    This is typically used when a non-oblivious test would result in a guard
    on a data dependent value of which we don't know the value of at compile time.
    When a guard is tested this way, we may diverge in behavior from how regular
    PyTorch semantics would treat it.  For more information, see
    https://github.com/pytorch/pytorch/pull/118579
     r   )r   r   r    r   rs   boolr   s    rM   rs   rs      sS     $&& y--b!444$%%++t+++rN   c                     t           j        t           j        t          t          f}t           t           j                  rt          t           j                  sJ t          j                                                                         k     fd           t          j
         j
                  D ]"\  }}t          j        ||k     fd           #dS t           |          rht           t                    sUt          |          rt          t                    rJ  d              t          j         k     fd           dS dS dS )z
    Test that two "meta" values (typically either Tensor or SymInt) have
    the same values, e.g., after retracing.  If we don't understand the
    quantities in question, we'll just skip the consistency check.
    c                  (    j          d j          dS N != z (old != new)r   newolds   rM   <lambda>z"check_consistent.<locals>.<lambda>  s    	5_5_sy5_5_5_ rN   c                  (    j          d j          dS r   r   r   s   rM   r   z"check_consistent.<locals>.<lambda>  s    CI)S)S39)S)S)S rN   r   c                       d  dS r   rZ   r   s   rM   r   z"check_consistent.<locals>.<lambda>  s    C)G)GS)G)G)G rN   N)r   r"   r!   r   floatr   Tensor_checkr   zipr   r   )r   r   scalar_typesijs   ``   rM   rt   rt     st    L%.#u=L#u|$$ I#u|,,,,,SWWYY#''))+-_-_-_-_-_```
 	39-- 	U 	UDAqLa!S!S!S!S!STTTT	U 	U 
C	&	& Iz#t/D/D I#|,,]ZT5J5J]]sL]L]X[L]L]]]]SCZ!G!G!G!G!GHHHHHI I I IrN   c                 L     |d S  fd|                                 D             S )Nc                 N    i | ]!\  }}j                             ||          |"S rZ   )unbacked_renamingsget)r   kv	shape_envs      rM   
<dictcomp>z-resolve_unbacked_bindings.<locals>.<dictcomp>  sB       Aq 	$((A..  rN   )items)r   bindingss   ` rM   rx   rx     sC    t   NN$$   rN   nc                 8   ddl m} |j        dk    rdS t          | |j                            d                    x}r|                                D ]\  }}t          j        ||          }t          |t                    r'|j        |u rt                              d|||           W|j        j        }t          |t           j                  r t%          |j                  dk    r|j        d         d         dk    rt          |j        d         d         x}	t           j                  rt          |	j        x}
t           j                  r| j        |
                             t3          dd                    rU|	j        dk    rJ|j        d         d	k    r9t7          t!          j        |
d                    }||k    sJ | d
|             |
}t          |t           j                  sJ ||k    sJ | d            |                     ||           dS dS )a  
    Suppose we are retracing a pre-existing FX graph that previously had
    fake tensor propagation (and therefore unbacked SymInts).  When we retrace,
    we re-propagate fake tensors, which results in new unbacked SymInts.
    When this happens, we need to tell the shape environment about the equivalence
    of the old and new unbacked SymInts.  Pass us the old torch.fx.Node (which
    has the old binding information) and the new result (which we can extract the
    new unbacked SymInts out from).
    r   )_tensor_versionplaceholderNunbacked_bindingsz4rebind_unbacked: discard _tensor_version %s %s -> %s   r   r   Tr   z possible memo disaster)torch._dynamo.tensor_version_opr   oprx   metar   r   pytreekey_getr   r   targetr}   infor   r   rR   	PiecewiselenrK   EqlhsSymbolvar_to_rangeissubsetr3   rhs'_sympy_cast_symbool_to_symint_guardless_rename_unbacked_to)r   r   resultr   r   raw_u0pathu1raw_u1eq
new_raw_u1repackeds               rM   rw   rw   "  sD    @????? 	t},Y

CV8W8WXXXx  :$NN,, 	: 	:LFD--B "c"" qx?'B'BOQWY]_abbbW\F 65?33$FK  A%%Aq!Q&&Q!222EH== '/:>> ' &z2;;K1<M<MNN ' !A)++ C58JXYCZCZ[[6)))h+D+DF+D+D))) $fel33333 V###%G%G%G###))&&9999A :  :	: 	:rN   r   c           
         | j         dk    rHt          | j        d         j                            d          t
          j                  r| j        dv rdS | j         dk    r| j        t
          j        j	        j
        t
          j        j	        j
        j        t
          j        j	        j
        j        t
          j        j	        j        t
          j        j	        j        j        t
          j        j	        j        j        t
          j        j	        j        t
          j        j	        j        j        t
          j        j	        j        j        f	v rdS dS )Ncall_methodr   example_value)sizestridestorage_offsetitemTcall_functionF)r   r   rK   r  r   r   r   r  opsatensym_sizedefaultr   
sym_stridesym_storage_offset	sym_numelr   s    rM   ry   ry   X  s     	=  ty|(,,_==u|LL 	!KGGGtw/!!dk		'	#	!	!)	!%	)	)1	 (
6 
' 
' t5rN   c           	      r   t          | t          j        t          j        t          j        t          j        t          j        t          j        f          s| S t          | t          j        t          j        t          j        f          r$t          j        j	        
                    |           } t          |           S )a   Canonicalize a boolean expression by transforming it into a lt / le
    inequality and moving all the non-constant terms to the rhs.
    We canonicalize And / Ors / Not via cnf and then canonicalize their subexpr
    recursively
    nb. sympy.Rel.canonical is not good enough https://github.com/sympy/sympy/issues/25924

    Args:
        expr (sympy.Expr): Expression to canonicalize
    )r   rR   RelAndOrNotr  Nelogicboolalgto_cnf_canonicalize_bool_expr_implr   s    rM   rc   rc   n  s{      dUY	58UYRWRZ[\\ $EHei899 0{"))$//'---rN   c                    t          | t          j        t          j        f          r* t	          |           t          t          | j                   S t          j        t          j	        t          j
        t          j        i}t          | t          |                                                    r%| j        | j        z
  }|t	          |                    }n\t          | t          j	        t          j        t          j        t          j        f          sJ | j        | j        z
  }t	          |           }d }d}t%          |          }t          |t          j                  rbg }g }|j        D ]9} ||          r|                    |            $|                    |           :t          j        | }t          j        | }n ||          r| d}} |||          S )z
    After canonicalization, we are guaranteed to have eliminated Ge/Gt relations
    (rewriting them to Le/Lt, respectively).
    c                 h    | j         p+t          | t          j                  o| j        d         j         S Nr   )is_negativer   rR   MulrK   ts    rM   is_negz,_canonicalize_bool_expr_impl.<locals>.is_neg  s)    }TAuy!9!9!SafQi>STrN   r   )r   rR   r*  r+  r   maprc   rK   GtLtGeLetuplekeysr	  r  r  r-  _reduce_to_lowest_termsAddr   )	r   oppositer  r8  r9  r	  posnegterms	            rM   r1  r1    s   
 $EH-.. CtDzz35tyAABB%(EHeh7H$hmmoo..// h!T$ZZ $58UXux HIIIIIh!JJU U U C
!#
&
&C#uy!! H 	! 	!Dvd|| !

D5!!!!

4    ioio	 4S1S#;;rN   c                     d }t          | t          j                  rP| j        }t	          j        t          j        t          ||                    fd|D             }t          j        | S |  ||           z  S )z
    Eliminates any integer factor from a given expression.
    E.g., 6x + 4y reduces to 3x + 2y.

    Useful when an expression is == or != to 0.
    c                     t          | t          j                  rt          t	          |                     S t          | t          j                  r#t          j        d | j        D                       S dS )Nc                 z    g | ]8}t          |t          j                  t          t	          |                    9S rZ   )r   rR   r   absr   r   args     rM   
<listcomp>zH_reduce_to_lowest_terms.<locals>.integer_coefficient.<locals>.<listcomp>  s7    ```CQVQ^A_A_`c#c((mm```rN   r   )	r   rR   r   rJ  r   r6  mathprodrK   xs    rM   integer_coefficientz4_reduce_to_lowest_terms.<locals>.integer_coefficient  sb    a'' 	s1vv;;59%% 	9``qv```aaa1rN   c                     g | ]}|z  S rZ   rZ   )r   rQ  factors     rM   rM  z+_reduce_to_lowest_terms.<locals>.<listcomp>  s    +++V+++rN   )	r   rR   rB  rK   r   reducerN  gcdr:  )r   rR  atomsrT  s      @rM   rA  rA    s       $	"" 0	!$(C0CU,K,KLL++++U+++y%  ))$////rN   c                     t          | t          t          f          sJ t          | t                    rdS t          | j        j        t
          j        j        j        t
          j        j        j	        f          rdS dS )z Utility to check if underlying object
    in SymBool is concrete value. Also returns
    true if integer is passed in.
    Args:
        a (SymBool or bool): Object to test if it bool
    TF)
r   r    r   r   r   rR   r.  r/  BooleanTrueBooleanFalser   s    rM   rh   rh     sj     a'4)))))!T t!&+ 3 ?ATAabcc t5rN   c                 h    t          | t          j                  o| j                                        S rG   )r   r   r"   r   ri   ss    rM   ri   ri     s'    a&&A16+?+?+A+AArN   valc              #   H  K   t          | t                    r!t          |           r| j        j        V  d S d S t          | t
          j                  r| V  d S t          | t          t          t          f          rd S t          | t          t          f          r| D ]}t          |          E d {V  d S t          |           r)t          |                                           E d {V  d S t          | t          j                  rwt          |                                           E d {V  t          |                                           E d {V  t          |                                           E d {V  d S | d S t'          d|  dt)          |                      )Nz&cannot extract sympy expressions from  )r   r   is_symbolicr   r   rR   Basicr   r   r   r?  r   _iterate_exprsr:   r  r   r   r  r  AssertionErrorr   )r^  r]  s     rM   rc  rc    s     #x   Y s 	 (-	  	 	C	%	% Y						C#ud+	,	, Y	C%	'	' Y 	) 	)A%a((((((((((	) 	)	s		 	Y!#((**-----------	C	&	& Y!#((**---------!#**,,/////////!#"4"4"6"677777777777	WcWWDQTIIWWXXXrN   c                     | t                      S t          |           }	 t          |          }n# t          $ r t                      cY S w xY w |j        j        d |D              S )Nc              3   $   K   | ]}|j         V  d S rG   rf   r   es     rM   	<genexpr>zfree_symbols.<locals>.<genexpr>  s$      *G*Ga1>*G*G*G*G*G*GrN   )setrc  nextStopIterationrf   union)r^  itr
first_exprs      rM   rf   rf     s{    
{uu


C#YY

   uu ):"(*G*G3*G*G*GHHs   1 AAc                 P    t          d t          |           D                        S )z)Faster version of bool(free_symbols(val))c              3   $   K   | ]}|j         V  d S rG   )	is_numberrh  s     rM   rj  z#has_free_symbols.<locals>.<genexpr>  s$      <<11;<<<<<<rN   )allrc  r^  s    rM   rl   rl     s*    <<s(;(;<<<<<<<rN   c                 4    d t          |           D             S )Nc                 ^    h | ]*}t          |t          j        t          j        f          (|+S rZ   )r=   r;   UNBACKED_INTUNBACKED_FLOATr   r]  s     rM   r   z(free_unbacked_symbols.<locals>.<setcomp>  s3    fff!.T=NPTPc<d*e*efAfffrN   rg  rP  s    rM   free_unbacked_symbolsr{     s    ff|AffffrN   c                 L   d| j         v rt          | j         d         t          j                  rut          | j         d         j        j        t          j                  rF| j        dk    s$t          | j         d         j        j                  r| j         d         j        j        S d S )Nr^  r   )
r  r   r   r"   r   r   rR   r
  r   r{  r'  s    rM   rg   rg     s    49U#U\22 	49U#(-u|<< 	 
M	!	!%:49U;K;P;U%V%V	!y$))4rN   c                     i }| j         D ]D}t          |          r3|j        d         j        j        |vr|||j        d         j        j        <   E|S )Nr^  )nodesrg   r  r   r   )graphrr   s      rM   find_symbol_binding_fx_nodesr    s]    
A 1 1$T** 	1ty/?/D/IQR/R/R,0Adi#()HrN   Tfrozenc                   *    e Zd ZdefdZdedefdZdS )rv   r   c                     dS )Nz#.cast_symbool_to_symint_guardless()rZ   rJ   s    rM   __str__zConvertIntKey.__str__  s    44rN   bc                      t          |          S )zGet the int value from bool) cast_symbool_to_symint_guardless)rJ   r  s     rM   r   zConvertIntKey.get  s    /222rN   N)rO   rP   rQ   strr  r   r   r   rZ   rN   rM   rv   rv     sQ        5 5 5 5 53T 3c 3 3 3 3 3 3rN   rv   c                   6    e Zd ZU eed<   defdZdedefdZdS )CallMethodKeynamer   c                     d| j          dS )N.z()r  r  s    rM   r  zCallMethodKey.__str__(  s     49    rN   oc                 <     t          || j                              S )zCall the method on object)getattrr  rJ   r  s     rM   r   zCallMethodKey.get+  s    $wq$)$$&&&rN   NrO   rP   rQ   r  rT   r  r   r   rZ   rN   rM   r  r  $  s\         
III! ! ! ! !'S 'S ' ' ' ' ' 'rN   r  c                   6    e Zd ZU eed<   defdZdedefdZdS )InnerTensorKey
inner_namer   c                     d| j          S Nr  )r  r  s    rM   r  zInnerTensorKey.__str__4  s    $4?$$$rN   r  c                 ,    t          || j                  S )zGet the inner tensor attribute)r  r  r  s     rM   r   zInnerTensorKey.get7  s    q$/***rN   Nr  rZ   rN   rM   r  r  0  s\         OOO% % % % %+S +S + + + + + +rN   r  c                   6    e Zd ZU eed<   defdZdedefdZdS )DivideByKeydivisorr   c                     d| j          dS )Nz.__floordiv__()r  r  s    rM   r  zDivideByKey.__str__@  s    /////rN   r  c                     || j         z  S )zDivide object by divisorr  r  s     rM   r   zDivideByKey.getC  s    DL  rN   N)rO   rP   rQ   r   rT   r  r  r   rZ   rN   rM   r  r  <  s\         LLL0 0 0 0 0!S !S ! ! ! ! ! !rN   r  Fc           	      T     dS  j         }t          |          r|s/t                              d|           |                                 	 ddt
          t          j        t          j	        f         f fd |d          }|slrjt          |t          j                  r5t          |                                |                                f          nd}t!          d d| d	| d
          |*|                                D ]}t          j        ||          }t          j        ||          }	t          |	t&                    rt          |	j        j        x}
t          j                  rt          |t&                    rZ|j        j        x}|
k    rHt          |t          j                  r                     |
|                                |
|           t          |t&                    s(                     |
t          j        |                     |S dS )a  
    After having run fake tensor propagation and producing example_value
    result, traverse example_value looking for freshly bound unbacked
    symbols and record their paths for later.  It is an error if
    we have allocated an unbacked SymInt but it cannot be found in
    example_value.  (NB: this means if you have a multi-output
    function, you must call this on the tuple of tensor output, you
    cannot wait!)

    The peek parameter lets you check out what the bindings are without
    changing the affected list.  This is primarily useful for ensuring
    unbacked_var_to_val is promptly populated when propagate_real_tensors is on.
    Nzcompute_unbacked_bindings %sr   c           
      6	   i }t          | t          t          f          rht          t	          |                     D ]I}|                     | |         |t          j        |          fz   |||         nd                      Jnt          |           r^| 	                                \  }}|D ]B}t          | |          }|                     ||t          |          fz                        Cnt          | t          j                  r.|                     |                                 |t          d          fz   | j        | j                                        nd                      |                     |                                 |t          d          fz   | j        | j                                        nd                      |                     |                                 |t          d          fz   | j        | j                                        nd                      n[t          | t          j        t          j        f          r^t          | j        j        x}	t.          j                  r8|	v r4|||	<   |                    |	|                               |	           nt          | t          j                  rt          | j        j        x}	t.          j                  rt	          |	j                  dk    rt          |	j        d         x}
t.          j                  rt          |	j        d         x}t.          j                  re|v ra|t=          t?          |
                    fz   ||	<   |&                    |	|t?          |
          z                                 |           nt          | t          j                   rt          | j        j        x}	t.          j!                  rt          |	j"        t.          j                  rm|	j#        dk    rb|	j"        v rY|tI                      fz   ||	j"        <   |#                    |	t?          |                                         |	j"                   |S )N)realr  r  r  r   r   r   )%r   r?  r   ranger  updater  SequenceKeyr&   __tensor_flatten__r  r  r   r   r  r  real_tensorr  r  r"   r!   r   _exprrR   r
  set_unbacked_var_to_valremover6  rK   r   r  r   r    r  r	  r  rv   )r   r  r  r  r   attrs_attrsubr]  r	  r  free_unbacked_symbols_with_pathpendingr   s               rM   r  zBcompute_unbacked_bindings.<locals>.free_unbacked_symbols_with_path_  s    A!eT]++ O&s1vv  AHH77aD$&*<Q*?*?)A"A,0,<a$      /q11 G&//11q!  D!!T**CHH77T^TXEYEYD[=[\\   
 Au|,, ?&33$-*?*?)A"A56]5NQ]//111TX     33

DM(,C,C+E$E78}7PQ]11333VZ     33((**DMBR4S4S3U,U?@}?XQ]99;;;^b      1u|U^<==)&AFL0q%,??)& LL!#55a>>>q!!!! 1el++&AFL0q%)<<& KK1$$afQi/s?? %afQi/s>> % 7NN {3s884466!#55aS9IJJJs####
 1em,,&AFL0q%(;;& quel33	&
 EQJJEW$$=??"44!%#55aTCCCqu%%%HrN   rZ   r   zPending unbacked symbols z not in returned outputs r`  z.
Did you accidentally call new_dynamic_size() or item() more times than you needed to in your fake implementation?
For more help, see https://docs.google.com/document/d/1RWrH-3wLEpzR9kCS6gGBNen_-Fs-8PVbWWFE5AcgeWE/editrG   )pending_fresh_unbacked_symbolsrk  r}   r  clearr   rR   r
  r  KeyPathr   r   r   reprr  r  rX   valuesr  r   r   r   r  _eliminate_unbackedsympify)r   r  old_example_valuepeekfssymbol_to_pathextrakeypathold_symnew_symnew_sold_sr  r  s   `           @@rM   ru   ru   H  s    		1B"ggG G 	HH3R888HHJJJ U	 U	%,./U	 U	 U	 U	 U	 U	 U	 U	n 98KK 	 	 mU\::m**,,m.J.J.L.LMNNN 
 0zG z zm z z^c z z z  ( ()0022 U U .):GDD .@@w11
U(99u5<HH
U "'844 U7<CT:T%Y^9^9^%eU\:: H%99%GGGG%99%GGGG':: U!55eU]7=S=STTTOG GrN   c                     t          | t                    r*| j                                        rt	          |           S dS t          |           S )a+  
    Returns True only if we can tell that a is True, possibly introducing
    a guard in the process.  If a depends on some unbacked SymInt, we may
    return False even though there may exist a possible value of the SymInt
    that would cause the expression to return True.

    When is it appropriate to use definitely_true?  First, if you can use
    a higher level combinator like parallel_or/parallel_and, prefer using
    those instead, they are definitely safe (modulo short-circuiting).
    Second, it can be used if the program would behave equivalently if
    definitely_true always returned False (parallel_or/parallel_and are
    examples of this pattern, modulo short-circuiting).  Finally, it even
    be OK if the program wouldn't behave equivalently, so long as the
    change is semantics preserving.  It can be semantics preserving if
    the program errors in more cases than it did previously (but otherwise
    behaves identically), or if it changes some quantity in a way that
    doesn't matter (e.g., strides often fall in this bucket.)
    Fr   r    r   r   
guard_boolr   r   s    rM   definitely_truer    sF    & !W 6?? 	a== 577NrN   c                     t          | t                    r+| j                                        rt	          |            S dS t          |            S )aK  
    Returns True only if we can tell that a is False, possibly introducing
    a guard in the process.  If a depends on some unbacked SymInt, we may
    return False even though there may exist a possible value of the SymInt
    that would cause the expression a to be False.  See definitely_true
    for more usage guidance.
    Fr  r   s    rM   definitely_falser    sL     !W 6?? 	!!}}$$5Aww;rN   rQ  c                 8   t          | t                    rm| j        j        }| j        j        }	 |                    |          }|t          |          S n+# t          $ r t          	                    d|           Y nw xY wdS t          | t                    sJ | S )aA  Returns True if x can be simplified to a constant and is true.

    .. note::
        This function doesn't introduce new guards, so the expression may end
        up evaluating to true at runtime even if this function returns False.

    Args:
        x (bool, SymBool): The expression to try statically evaluating

    NzCould not simplify %sF)
r   r    r   r   r   _maybe_evaluate_staticr   	Exceptionr}   r~   )rQ  r   r   
simplifieds       rM   rr   rr     s     !W 	v{F$		5"99$??J%J''' & 	5 	5 	5II-t44444	5uaHs   %A %A>=A>c                      t          d | D                       rdS t          d | D                       rdS t          |           S )z
    Evaluate the logical OR of several arguments, avoiding guarding on
    unbacked SymInts if another argument is definitely True.
    c              3   4   K   | ]}t          |          V  d S rG   )rr   r   r   s     rM   rj  zparallel_or.<locals>.<genexpr>*  s+      
2
2 ##
2
2
2
2
2
2rN   Tc              3   4   K   | ]}t          |          V  d S rG   )r  r  s     rM   rj  zparallel_or.<locals>.<genexpr>,  s*      
,
,!?1
,
,
,
,
,
,rN   )anyrK   s    rM   parallel_orr  %  sX    
 
2
2T
2
2
222 t

,
,t
,
,
,,, tt99rN   c                      t          d | D                       rdS t          d | D                       rdS t          |           S )z
    Evaluate the logical FALSE of several arguments, avoiding guarding on
    unbacked SymInts if another argument is definitely False.
    c              3   X   K   | ]%}t          t          j        |                    V  &d S rG   )rr   r   sym_notr  s     rM   rj  zparallel_and.<locals>.<genexpr>5  s5      
A
Aq q!1!122
A
A
A
A
A
ArN   Fc              3   4   K   | ]}t          |          V  d S rG   )r  r  s     rM   rj  zparallel_and.<locals>.<genexpr>7  s+      
-
-1A
-
-
-
-
-
-rN   )r  rt  r  s    rM   parallel_andr  0  sX    
 
A
AD
A
A
AAA u

-
-
-
-
--- ut99rN   c                 D   t          | t                    rt          |t                    s*t          | t                    rkt          |t                    rVt          |           t          |          k    rdS t	          j        t          j        t          t          | |          d          S t          | t          t          j        f          r't          |t          t          j        f          r| |k    S t          dt          |            dt          |                     )z
    Like ==, but when run on list/tuple, it will recursively test equality
    and use sym_and to join the results together, without guarding.
    FTzunexpected sym_eq between r`  )r   r?  r   r  r   rU  operatorand_r:  rm   r   r   r"   rd  r   )rQ  ys     rM   rm   rm   ;  s    
 	1e OAu!5!5 O:a;N;N OS]^_aeSfSf Oq66SVV5s61a/@/@$GGG	AU\*	+	+ O
1sEL>Q0R0R OAvM$q''MMDGGMMNNNrN   c                 (   t          | t          t          f          rt          |           S t          | t          t
          f          rt          |           S t          | t          t          f          rt          |           S t          d|            )Nzunrecognized scalar )r   r    r   r  r"   r   r`   r!   r   ra   rd  r   s    rM   rb   rb   I  s    !gt_%% 9!}}	A}	%	% 9||	A%(	)	) 91~~7A77888rN   r]  compiler_mincompiler_maxc                 4    |                      |||           d S rG   )constrain_symbol_range)r   r]  r  r  s       rM   _constrain_symbol_ranger  T  s     $$QlCCCCCrN   c                 ,   t          | t                    rxt          | j        t                    r`t          | j        j        t
          j                  r>| j        j                            | j        j                  rt          |            dS dS dS dS dS )ax  
    Don't use this directly; use torch._check_is_size instead.

    This is a softer version of _constrain_range_for_size (with min=0,
    max=Inf).  Instead of forcibly constraining a variable (and erroring if we
    failed to constrain it), it will simply advise us that a size is
    constrained in some way.  We will always defer a runtime assert for this
    constraint if we cannot prove it at compile-time, but we we only
    *sometimes* learn useful extra information at compile-time with this
    information.  This is in contrast to constrain_range_for_size, where if
    you don't call that on a fresh unbacked symint, chances are we will choke.

    TODO: Make Dynamo handle this appropriately if this is seen in Dynamo-ed
    code.  Right now this is only really used in code with AOTAutograd trace
    through, so it is not a big problem that this isn't supported, but in
    principle all of this code should be Dynamo'able too.

    TODO: I didn't support min/max because I didn't have a use case where this
    actually helped.  In principle we can support it, it just makes the
    implementation below more complicated.
    N)
r   r"   r   r   r   rR   r
  r   is_unbacked_symint_constrain_range_for_sizer   s    rM   _advise_is_sizer  X  s    D 	1f%qvw''% qv{EL11% F//<<	% 	"!$$$$$% % % % % % % %rN   minmaxc                 L   t          | t          t          f          rt          d          t          | t                    s
J d            t          | j        j        t          j                  s
J d            | j        j	        
                    | j        j        ||           dS )z=
    This function is NOT INTENDED to be used by itself.
    z$Constraining SymFloat/SymBool is nyiz#can only constrain range for SymIntconstraining non-Symbols NYIN)r   r!   r    
ValueErrorr"   r   r   rR   r
  r   r  r   r  r  s      rM   r  r    s    
 !h()) A?@@@a  GG"GGGGafk5<00PP2PPPPF..qv{CEEEEErN   )r  c          	      *   |t            }|t           }||k     rt          d          t          | t                    r*|| cxk    r|k    sn t          d|  d| d| d          dS | j        j                            | j        j        ||           dS )a  
    Applies a constraint that the passed in SymInt must lie between min-max
    inclusive-inclusive, WITHOUT introducing a guard on the SymInt (meaning
    that it can be used on unbacked SymInts).  If min/max are None, we assume
    that the dimension is unbounded in that direction.  Repeated application
    of constrain_range intersects the ranges.  This is a fairly low level API
    that doesn't have a lot of safety guarantees (TODO: provide higher level
    APIs).

    Currently, we use this API in the following circumstance: when we allocate
    an unbacked SymInt, denoting an integer quantity which is data dependent,
    we ordinarily do not know anything about what values it may take.  This
    means that any sort of guard on it will immediately fail.  However, in
    many cases, we know something about the unbacked SymInt: for example, we
    know that nonzero(x).size(0) must be >= 0.  We use constrain_range to
    narrow the possible range, declaring that negative symbols are impossible.
    This permits to definitely answer True to queries like 'nnz >= 0', even if
    we don't know what the actual (hinted) value of 'nnz' is.  In fact, we
    actually use constrain_range to unsoundly discharge common guards: for an
    unbacked SymInt produced by nonzero, we will also assume that it is not
    equal to 0/1 (even though these are perfectly possible values at runtime),
    because we generally expect graphs that are valid for N=2 to also be valid
    for N=1.
    NoMaximum value to constrain_as_size can't be less than the specified min value, received min={min} and max={max}Invalid value  for range [:])r0   r  r   r   r   r   _constrain_ranger   r  s      rM   constrain_ranger    s    2 {g
{
Syy/
 
 	

 !S qCIaIISII3IIIJJJF%%afk3<<<<<rN   r  c                     t          | t                    s,t          |t                    s
| |k    sJ dS |j        j        }n| j        j        }|                    | |           dS )z
    Given two SymInts, constrain them so that they must be equal.  NB:
    this will not work with SymInts that represent nontrivial expressions
    (yet!)
    N)r   r"   r   r   _constrain_unify)r   r  r   s      rM   constrain_unifyr    sl     a   %!V$$ 	)6666F(IIF$	q!$$$$$rN   skipc                 "   t          | t                    rYt          j                    }t	          |dz             D ]	}|j        }
| j                            |j        j	        |j
                  S t          |           t          u s
J |             | S Nr   )r   r    r   currentframer  f_backr   expect_truef_codeco_filenamef_linenor   r   )r   r  framer  s       rM   r   r     s    !W L$&&tax 	! 	!ALEEv!!%,":ENKKK77d???A???HrN   c                     t          | t                    r| j                            dd          S t	          |           t
          u s
J |             | S Nr   r   )r   r    r   r  r   r   r   s    rM   r  r    sJ    !W (v  Q'''77d???A???HrN   c                     t          | t                    r| j                            dd          S t	          |           t
          u s
J |             | S r  )r   r"   r   r`   r   r   r   s    rM   r`   r`     sJ    !V 'vA&&&77c>>>1>>>HrN   c                     t          | t                    r| j                            dd          S t          | t                    s
J |             | S r  )r   r!   r   ra   r   r   s    rM   ra   ra     sO    !X )v!!"a(((a"""""HrN   c                 .    d | j         j        D             S )Nc                 >    g | ]}|j         d k    |j        d         S )r   r^  )r   r  r   r   s     rM   rM  z'fx_placeholder_vals.<locals>.<listcomp>
  s*    KKKaQT]5J5JAF5M5J5J5JrN   r  r~  gms    rM   fx_placeholder_valsr  	  s    KK28>KKKKrN   c                 .    d | j         j        D             S )Nc                 2    g | ]}|j         d k    |j        S )r   )r   r  r  s     rM   rM  z*fx_placeholder_targets.<locals>.<listcomp>  s&    FFF0E0EAH0E0E0ErN   r  r  s    rM   fx_placeholder_targetsr    s    FFbhnFFFFrN   ignore_staticc                V    | j                             t          |           ||          S )Nr  )r   evaluate_guards_for_argsr  )r  r  rK   s      rM   eval_guardsr    s)    <001DR1H1H$^k0lllrN   c                 R    | j                             t          |           |          S rG   )r   bind_symbolsr  )r  rK   s     rM   r  r    s#    <$$%8%<%<dCCCrN   c                   &    e Zd ZdZdZdZdZdZdZdS )
DimDynamicai  
    Controls how to perform symbol allocation for a dimension.  It is always
    sound to default this to DYNAMIC, but the policies DUCK and STATIC can
    result in better trace-time and compile-time performance, as they reduce
    the number of allocated symbols and generally make your graph more static.

    NB: If we notice you've applied a constraint to the dimension, we will
    force it to DYNAMIC for simplicity.

    DimDynamic is controlled by a variety of higher level UX features.
    Currently:

    - In eager mode, the default policy is DUCK.
        - The default is changed to STATIC with assume_static_by_default.
        - An individual dim is marked DYNAMIC if you mark_dynamic_dim.
    - In export mode, the default policy is STATIC.
        - An individual dim is marked DYNAMIC if you specify it in
          dynamic_shapes passed to export.
    r   r   r         N)	rO   rP   rQ   __doc__DYNAMICDUCKSTATICSIZE_LIKE_UNBACKEDINFER_STRIDErZ   rN   rM   r  r    s6         ( G DFLLLrN   r  c                       e Zd ZU eed<   dS )
Constraint	warn_onlyN)rO   rP   rQ   r   rT   rZ   rN   rM   r%  r%  I  s         OOOOOrN   r%  c                   *    e Zd ZU dZeed<   defdZdS )StrictMinMaxConstrainta-  
    For clients: the size at this dimension must be within 'vr' (which
    specifies a lower and upper bound, inclusive-inclusive) AND it
    must be non-negative and should not be 0 or 1 (but see NB below).

    For backends: there must not be any guards on this dimension which
    are not implied by the given lower and upper bound.  Regardless of
    the lower bound, the backend can assume the size is non-negative
    and that it is not 0 or 1.

    An unbounded StrictMinMaxConstraint can be thought of as a strict version
    of "RelaxedUnspecConstraint".

    NB: Export will often unsoundly assume that a graph works for 0/1, even
    though at trace time we assumed size is not 0 or 1.  The idea is that
    if we produce a graph that works for a range of values, it will be OK
    for N=0/1 too.
    vrsourcec                 b    | j         j         d|                                 d| j         j         S )zFormat the constrain equation <= )r)  lowerr  upperrJ   r*  s     rM   renderzStrictMinMaxConstraint.renderc  s0     '-GGV[[]]GGGGGrN   N)rO   rP   rQ   r  r3   rT   r$   r0  rZ   rN   rM   r(  r(  M  sM          $ 	OOOHV H H H H H HrN   r(  c                       e Zd ZdZdefdZdS )RelaxedUnspecConstrainta  
    For clients: no explicit constraint; constraint is whatever is implicitly
    inferred by guards from tracing.

    For backends: there must exist at least TWO possible values for the
    size at this dimension which satisfy the guards for this dimension.

    In other words, this constraint helps us distinguish between "we don't
    care if this dimension specializes or not" versus "this dimension must be
    unspecialized."  However, this constraint doesn't say very much about what
    specialization is permitted; for example, if we guard on a size being
    even, this would still be acceptable under an unspec constraint.  This
    makes RelaxedUnspecConstraint useful for eager mode, where your backend compiler
    may add constraints to otherwise dynamic dimensions; we can't assert that
    there are NO guards as this is brittle because compilers should be able to
    add extra constraints.  If you want to assert that there are no guards,
    use StrictMinMaxConstraint with an unbounded ValueRanges.
    r*  c                 2    d|                                  dS )NzRelaxedUnspecConstraint(r  r  r/  s     rM   r0  zRelaxedUnspecConstraint.render|  s    :&++--::::rN   N)rO   rP   rQ   r  r$   r0  rZ   rN   rM   r2  r2  h  s9         $;V ; ; ; ; ; ;rN   r2  c                       e Zd ZU dZeeeef                  ed<   eeeeee	j
        f         ee	j        ge	j        f         f                  ed<   ee	j
                 ed<   d Zd Zd Zd Zd	 Zd
 ZdS )EqualityConstrainta  
    Represent and decide various kinds of equality constraints between input sources.

    A "source pair" is a pair of input sources for dynamic dimensions that
    are specified equal. We represent `source_pairs` in a union-find forest
    so that we can efficiently check whether two such sources are transitively equal.

    A "derived equality" relates an input source to an expression over a root.
    The root can be another input source, corresponding to some dynamic dimension,
    or a phantom symbol that does not directly represent any dynamic dimension. We
    represent `derived_equalities` involving input sources in a transitively-closed map
    so that we can efficiently check whether an input source is transitively equal to
    a given expression over another input source.
    (NOTE: In contrast, it is easy to decide whether an input source is transitively equal
    to a given expression over a phantom symbol; such expressions are already in canonical
    form and so the problem reduces to symbolic expression equality.)
    source_pairsderived_equalitiesphantom_symbolsc                     i }t                               | d|           i }t                               | d|           | j        D ]A\  }}|                     |                     |          |                     |                     B| j        D ]\  }}}t          |t          j                  r' ||          | j	        |                     |          <   G || 
                    |                    | j	        |                     |          <   dS )a  Pre-processing to answer queries `is_equal` and `is_derived` below.

        Example: Suppose we are given:
          source_pairs [a = b, b = c]
          derived_equalities [d = c + 1, e = d - 1]
        We first construct a union find with source_pairs:
          _parents = {a: a, b: a, c: a}
        Then we compute canonical symbolic expressions, recursively applying derived_equalities
        until we bottom out:
          _defs = {d: c + 1, e: (c + 1) - 1 aka c}
        _parents_defsN)object__setattr__r6  _union_findr7  r   rR   r
  r;  _rewrite)rJ   r:  r;  source1source2r*  rootfns           rM   __post_init__z EqualityConstraint.__post_init__  s    *,4X666 +-4%000 $ 1 	B 	BGWKK

7++TZZ-@-@AAAA $ 7 	I 	IFD" $-- I13D
4::f--..13DMM$4G4G1H1H
4::f--..	I 	IrN   c                 X    || j         v r |                     | j         |                   S |S rG   )r:  r?  r/  s     rM   r?  zEqualityConstraint._find  s-    T]""::dmF3444MrN   c                 *    ||k    r|| j         |<   d S d S rG   )r:  )rJ   root1root2s      rM   r>  zEqualityConstraint._union  s$    E>>#(DM%    >rN   c                     |                      |          }|| j        v r| j        |         S t          j        |                                          S rG   )r?  r;  rR   r
  r  )rJ   srcs     rM   r@  zEqualityConstraint._rewrite  sF    jjoo$* :c?" <

+++rN   c                     |                      |          |                      |          k    p|                     ||d           S )Nc                     | S rG   rZ   rP  s    rM   r   z-EqualityConstraint.is_equal.<locals>.<lambda>  s     rN   )r?  
is_derived)rJ   rA  rB  s      rM   is_equalzEqualityConstraint.is_equal  sC     JJw4::g#6#66 ;OOGWkk::		
rN   c                 l    |                      |           ||                      |                    k    S rG   )r@  )rJ   rK  
symbol_srcrD  s       rM   rN  zEqualityConstraint.is_derived  s/    }}S!!RRj(A(A%B%BBBrN   N)rO   rP   rQ   r  r   r   r$   rT   r   rR   r
  r
   rS   rE  r?  r>  r@  rO  rN  rZ   rN   rM   r5  r5    s          " uVV^,----U651E+FRWR\Q]_d_iQiHj#jkllll%,'''' I  I  ID  ) ) )

, 
, 
,
 
 
C C C C CrN   r5  c                     t          | t                    s
J d            t          |           t          us
J d            d S )NzInvalid symbolic_context objectz%Illegal usage of symbolic_context ABC)r   rn   r   symbolic_contexts    rM   _assert_symbol_contextrU    sK    &88[[:[[[[ !!888:a88888rN   c                 n   t          | t          j        t          j        f          rvt	          | j                  dk    rdS | j        \  }}t          |          rt          |t          j                  p(t          |t          j                  ot          |          S t          | t          j                  S )Nr   F)	r   rR   rB  r6  r  rK   _is_supported_equivalencer   r
  )r   r	  r  s      rM   rW  rW    s     $EI.// 
ty>>A59S&s++N
30N0N PU]++N0I#0N0N	
 dEL)))rN   c                     |                      t          j        j        j        j        t          j        j        j        j        t          j        j        j        j                  S )zL
    Add functions that our sympy interpreter can't reify into FX nodes
    )hasr   utils_sympy	functionsToFloat
TruncToIntr.   r   s    rM   #_has_uninterpretable_sympy_functionr_    sD     88$,$/$.  rN   c                       e Zd ZdZdS )rn   aS  
    Data structure specifying how we should create symbols in
    ``create_symbolic_sizes_strides_storage_offset``; e.g., should
    they be static or dynamic.

    This is an abstract base class because we are probably going to add
    another version of this that says "use exactly these SymInts, don't
    allocate fresh symbols."
    N)rO   rP   rQ   r  rZ   rN   rM   rn   rn     s           rN   rn   c                       e Zd ZU dZee         ed<   dZee         ed<   dZee	         ed<   dZ
ee	         ed<   dZee         ed<   d ZdS )	ro   z
    Create symbols in ``create_symbolic_sizes_strides_storage_offset`` via
    a symbolic_context determination as given by ``DimDynamic`` and ``DimConstraint``.
    This will cause fresh symbols to be allocated
    dynamic_sizesNdynamic_stridesconstraint_sizesconstraint_stridesview_base_contextc                    | j         <t                              | dt          j        gt          | j                  z             | j        2t                              | dd gt          | j                  z             | j        2t                              | dd gt          | j                  z             t          d | j         D                       sJ d S )Nrc  rd  re  c              3   `   K   | ])}|t           j        t           j        t           j        fv V  *d S rG   )r  r#  r  r   )r   r  s     rM   rj  z9StatelessSymbolicContext.__post_init__.<locals>.<genexpr>#  s7      `f6j5z7I:?[[rN   )
rc  r<  r=  r  r#  r  rb  rd  re  rt  r  s    rM   rE  z&StatelessSymbolicContext.__post_init__  s    't%69P8QTWX\XjTkTk8klll (t%7$#dFXBYBY9YZZZ"*t%9D6CHZD[D[;[\\\jnj~rN   )rO   rP   rQ   r  DimListr  rT   rc  rd  DimConstraintre  rf  r   rn   rE  rZ   rN   rM   ro   ro     s          
 :&&&&+/OWZ(////3gm,33315.555 48x0777@ @ @ @ @rN   ro   c                   \     e Zd ZU dZdZeed<   dZee	ed         f         ed<    fdZ
 xZS )rp   a   
    Create symbols in ``create_symbolic_sizes_strides_storage_offset`` via
    a symbolic_context determination as given by a cache of Source:Symbol. A cache hit
    will reuse a stored symbol, and a cache miss will write to this cache.

    This behaves like StatelessSymbolicContext, except the cache supersedes the
    other values - dynamic_sizes and constraint_sizes will not be read if we cache
    hit.

    It is the cache owners responsibility to maintain the lifecycle of the cache
    w/r/t different shape_envs, clearing, etc.
    Ntensor_source)r?   
sympy.Expr#shape_env_to_source_to_symbol_cachec                     t                                                       | j        J | j        st                              | di            d S d S )Nrn  )rH   rE  rl  rn  r<  r=  rJ   rL   s    rM   rE  z%StatefulSymbolicContext.__post_init__V  s_    !---7 	Pt%JBOOOOO	P 	PrN   )rO   rP   rQ   r  rl  r$   rT   rn  r   r   rE  rU   rV   s   @rM   rp   rp   <  s           !M6    cg'$sD9]4^/^*_fffP P P P P P P P PrN   rp   c                   B     e Zd ZU dZdZeeef         ed<    fdZ	 xZ
S )rq   a  
    The correct symbolic context for a given inner tensor of a traceable tensor subclass
    may differ from that of the outer symbolic context. This structure allows for this
    flexibility, with inner symbolic contexts mapped via attr -> symbolic context.
    Ninner_contextsc                 h    t                                                       | j        	i | _        d S d S rG   )rH   rE  rr  rp  s    rM   rE  z%SubclassSymbolicContext.__post_init__g  s8    &"$D '&rN   )rO   rP   rQ   r  rr  r   r  rn   rT   rE  rU   rV   s   @rM   rq   rq   ^  s`          
 26NDo-.555% % % % % % % % %rN   rq   c                 |    t          | t          t          t          f          rdS | j                                        S NF)r   r   r   r   r   ra  ru  s    rM   ra  ra  m  s4    #UD)** u8!!!rN      c                     t          | d          rB	 t          j        |           S # t          $ r  t                              d|            | cY S w xY w| S )Nexpandz"RecursionError in sympy.expand(%s))hasattrrR   rx  RecursionErrorr}   warning)r  s    rM   safe_expandr|  t  sg    q( 	<??" 	 	 	KK<a@@@HHH	 s   & 'AAc                       t          d          )Nzshouldn't be hit)rd  rZ   rN   rM   errorr~    s    
+
,
,,rN   c                 V    t          t          t          | |                              S rG   )r   r  "_eval_is_non_overlapping_and_dense)sizesr   s     rM   !eval_is_non_overlapping_and_denser    s#    z<UGLLMMNNNrN   c                     t          |           }|dk    r|d         dk    p| d         dk     S t          t          | |          t          j        d                    }d}|D ]\  }}|dk    r||k    r dS ||z  }dS )Nr   r   r   keyFT)r  sortedr   r  
itemgetter)r  r   r   lengths_and_stridesexpected_stridelengthr  s          rM   r  r    s    
e**C
 axxqzQ.%(Q,. !E7!4Q!7!7   O- " "Q;;_$$556!4rN   c                 0    t          j        d| fd          S )Nr   r   )rR   r  rP  s    rM   r  r    s    ?Aq69---rN   symboolc                    t          | t                    r| rdndS t          | j        j                  }| j        j                            |t          |           r&t          | j        	                                          nd           S )Nr   r   hint)
r   r   r  r   r   r   create_symintnoder   r   r   )r  int_syms     rM   r  r    s    '4   #"qq"5gl6GHHG<!33Gfnovfwfw  CB#glF_F_FaFaBbBbBb  ~B3  C  C  CrN   Absr  r-  r;  r<  r>  r=  MinMaxr)   r*   r(   TrueDivPowByNaturalr+   floorceiling)
r-   FloatPowr.   r  
RoundToIntRoundDecimalr^  
IntTrueDivFloatTrueDivr]  c                     t          |          |           dt          j        r$dt          j        |           fd            }n t          j        |           fd            }j        |_        j        |_        |S )a  
    Wrapper around lru_cache that clears when new info about shapes has been
    updated.

    Use lru_cache if the output is always the same, regardless of the
    constraints we know now (i.e. evaluate_expr)

    Use _lru_cache otherwise.

    Also note that this depends on _update_version_counter being called on the
    shape environment whenever the constraints are updated, otherwise the cache
    will not be cleared.
    r   Nc                    |                                  | j        k    r0                                 | j        |                                  n"|                                  k    s
J d             | g|R i |S )Nz9ShapeEnv cache key changed without version being updated!)_get_key_version_counterr   )rJ   rK   kwargsfn_cache	prior_keyprior_versions      rM   wrapperz_lru_cache.<locals>.wrapper  s       MMOO	 555$$&&& $ 5 MMOO		 DMMOO333O 433 8D242226222rN   c                 h    | j         k    r                                 | j          | g|R i |S rG   )r  r   )rJ   rK   r  r  r  s      rM   r  z_lru_cache.<locals>.wrapper  sM      555$$&&& $ 58D242226222rN   )r   configvalidate_shape_env_version_keyr   wrapsr   r   )rD  r   r  r  r  r  s      @@@rM   
_lru_cacher    s     "y!!"%%HM, 3					3 	3 	3 	3 	3 	3 
		3 	3" 
			3 	3 	3 	3 	3 
		3 #.G!,GNrN   c                   h    e Zd ZU ej        ed<    ed          Zeed<    ed          Z	eed<   dS )RuntimeAssertr   F)r  msgstackN)
rO   rP   rQ   rR   rS   rT   r   r  r  r  rZ   rN   rM   r  r    sZ         
*u%   C   E"""E3"""""rN   r  c                       e Zd Zd ZdS )SymExprPrinterc                 :    t          t          |                    S rG   )r  r   rJ   r   s     rM   _print_FloatzSymExprPrinter._print_Float  s    5;;rN   N)rO   rP   rQ   r  rZ   rN   rM   r  r    s#                 rN   r  c                   <     e Zd Z fdZd Zd Zd ZdefdZ xZ	S )ShapeGuardPrinterc                 r    t                                                       || _        || _        || _        d S rG   )rH   rI   symbol_to_source
source_refvar_to_sources)rJ   r  r  r  rL   s       rM   rI   zShapeGuardPrinter.__init__  s8     	 0$,rN   c                     d                     |                     |j        d         t          d                             S )Nznot {}r   r,  )formatparenthesizerK   rB   r  s     rM   
_print_NotzShapeGuardPrinter._print_Not   s/    t001z%?PQQRRRrN   c                 P    |                      |j        dt          d                   S )N and r*  	stringifyrK   rB   r  s     rM   
_print_AndzShapeGuardPrinter._print_And#  s    ~~di*U2CDDDrN   c                 P    |                      |j        dt          d                   S )N or r+  r  r  s     rM   	_print_OrzShapeGuardPrinter._print_Or&  s    ~~diD1ABBBrN   r   c                 j    t          |t          j                  s$J t          t	          |                                 fd} j                            |          s0J | dd  j        |         D              d |             d                                  j        |         d                   S )Nc                  d    t          d  j                                        D                       S )Nc                 .    i | ]\  }}|d  |D             S )c                 6    g | ]}|                                 S rZ   r  rz  s     rM   rM  z]ShapeGuardPrinter._print_Symbol.<locals>.repr_symbol_to_source.<locals>.<dictcomp>.<listcomp>.  s     333a333rN   rZ   )r   symbolsourcess      rM   r   zRShapeGuardPrinter._print_Symbol.<locals>.repr_symbol_to_source.<locals>.<dictcomp>-  s=       #FG 337333  rN   )r  r  r   r  s   rM   repr_symbol_to_sourcez>ShapeGuardPrinter._print_Symbol.<locals>.repr_symbol_to_source,  s@      '+'<'B'B'D'D     rN   z (could be from c                 6    g | ]}|                                 S rZ   r  rz  s     rM   rM  z3ShapeGuardPrinter._print_Symbol.<locals>.<listcomp>3  s     %R%R%R1affhh%R%R%RrN   z	) not in zu.  If this assert is failing, it could be due to the issue described in https://github.com/pytorch/pytorch/pull/90665r   )	r   rR   r
  r  r   r  r   r  r  )rJ   r   r  s   `  rM   _print_SymbolzShapeGuardPrinter._print_Symbol)  s    $-->>s4::>>>	 	 	 	 	 $((.. 	
 	
 Z Z%R%R8KD8Q%R%R%R Z Z++--Z Z Z	
 	
 	

 t4T:1=>>>rN   )
rO   rP   rQ   rI   r  r  r  r  r  rU   rV   s   @rM   r  r    s        	- 	- 	- 	- 	-S S SE E EC C C?S ? ? ? ? ? ? ? ?rN   r  c                        e Zd Z fdZ xZS )LoggingShapeGuardPrinterc                 P    t                                          |d |           d S )Nc                 *    |                                  S rG   r  r   s    rM   r   z3LoggingShapeGuardPrinter.__init__.<locals>.<lambda><  s    16688 rN   )rH   rI   )rJ   r  rL   s     rM   rI   z!LoggingShapeGuardPrinter.__init__;  s)    ););^LLLLLrN   )rO   rP   rQ   rI   rU   rV   s   @rM   r  r  :  sA        M M M M M M M M MrN   r  c                   4     e Zd ZdZ fdZdefdZd Z xZS )DynamicDimConstraintPrinterz
    Printer for dynamic dim constraints.
    - Instead of symbol s_k it prints its source t.size()[i]
    - Instead of Eq(_, _), Mod(_, _), etc. it prints _ == _, _ % _, etc.

    We use this to suggest code for specifying dynamic dim constraints.
    c                 d    t                                                       || _        || _        d S rG   )rH   rI   r  source_name_to_debug_name)rJ   r  r  rL   s      rM   rI   z$DynamicDimConstraintPrinter.__init__G  s/     0)B&&&rN   r   c                    t          |t          j                  s$J t          t	          |                                | j                            |          sJ d| d            | j        |         d                                         S )NzUnknown symbol z created by constraints solverr   )r   rR   r
  r  r   r  r   r  r  s     rM   r  z)DynamicDimConstraintPrinter._print_SymbolL  s    $-->>s4::>>>$((.. 	
 	
BdBBB	
 	
 	
 $T*1-22444rN   c           
          |                      |j        t          |                     d|j         d|                      |j        t          |                     S Nr`  )r  r	  rA   rel_opr  r  s     rM   _print_Relationalz-DynamicDimConstraintPrinter._print_RelationalS  sY    ##DHj.>.>??$+PTPaPabfbjlvw{l|l|P}P}rN   )	rO   rP   rQ   r  rI   r  r  r  rU   rV   s   @rM   r  r  ?  sx         C C C C C
5S 5 5 5 5@ @ @ @ @ @ @rN   r  c            
       (   e Zd ZdZd Zd Zd ZdefdZdefdZ	d Z
d	 Zd
 Zd Zed             Zd Zd Zd Zdeeeeef         f         deeef         ddfdZ	 	 	 ddej        deeeeef         ee         ee         f                  fdZdS )DimConstraintsz
    Custom solver for a system of constraints on symbolic dimensions.
    Solutions are "static" values or simplified "dynamic" constraints.
    c                    t          t                    | _        t                      | _        i | _        || _        t          t                    | _        t                      | _        g | _        t                      | _	        t                      | _
        t          ||          | _        g | _        || _        t          t           t"          t$          h| _        |                                  d S rG   )r   rk  _univariate_inequalities_symbols_with_equalities_substitutions_var_to_val_congruences_multivariate_inequalities_symbolic_equivalences_static_results_dynamic_resultsr  _dcp_inconsistencies_marked_dynamicr'   r)   r*   r(   _supported_sympy_functions_enumerate_sympy_functions)rJ   r  
var_to_valmarked_dynamicr  s        rM   rI   zDimConstraints.__init__]  s     NYY\M]M]% <?55% BD ?I-8-=-= <?55' HJ#
 *-*-%% 00@B[\\	 ,.  . 	@
' 	'')))))rN   c                 `     fd} fd}|                     t                    r|                    t          |          }|                     t                    r|                    t          |          }|                     t                    r|                    t          |          }|S )aA  
        Eliminate expressions of the form b // d and b % d while adding congruences of the form b % d == k.
        This leaves rational operators (in particular of the form b / d) that our inequality solver can handle.
        We solve the added congruences separately (using our congruence solver, see below).
        c                  0   | \  }}                     |                               |          }}|                    j                  |                    j                  z  }||z
  |z  }|dk    r j                                     |           |S r4  rewrite_with_congruencesxreplacer  r  addrK   baser  mod_reduced
congruencer]  rJ   s        rM   mod_handlerz<DimConstraints.rewrite_with_congruences.<locals>.mod_handler  s    0 !MD' 99!TBBDDaDabcelDmDm'D--(899G<L<LTM]<^<^^K,7JQ!!$((444rN   c                  <   | \  }}                     |                               |          }}|                    j                  |                    j                  z  }||z
  |z  }|dk    r j                                     |           ||z
  |z  S r4  r  r  s        rM   floor_div_handlerzBDimConstraints.rewrite_with_congruences.<locals>.floor_div_handler  s     !MD' 99!TBBDDaDabcelDmDm'D--(899G<L<LTM]<^<^^K,7JQ!!$((444 ;&'11rN   )rY  r)   replacer*   r(   )rJ   r]  r   r  r  s   ``   rM   r  z'DimConstraints.rewrite_with_congruences  s    	 	 	 	 	 	@	2 	2 	2 	2 	2 	2$ 88C== 	2<<[11D 88I 	8<<	;77D88H 	=<<*;<<DrN   c                 .   t           j        j        j        }t	                      }t          |          D ]A}t          t          ||          x}t          j	                  r|
                    |           B|                    | j                  | _        d S rG   )r   rZ  r[  r\  rk  dirr   r  rR   FunctionClassr  
differencer  _unsupported_sympy_functions)rJ   moduleall_functionsr  funcs        rM   r  z)DimConstraints._enumerate_sympy_functions  s    #-KK 	( 	(D'&$"7"77$9LMM (!!$''',9,D,DTEd,e,e)))rN   r   c                       |j         | j         S )z^
        Tracks list of sympy.Functions the export solver doesn't know how to handle.
        )rY  r  r  s     rM   _has_unsupported_sympy_functionz.DimConstraints._has_unsupported_sympy_function  s     tx:;;rN   c                    |t           j        k    rdS |}|                    | j                  }|t           j        k    r| j                            | d           t          |t           j                  s| 	                    |          rdS |j
        }|sJ d|             t          |          dk    r| j                            |           nt          t          |                    }t          | j        |                   }|                     ||          }t          | j        |                   }|t           j        k    r||k    S |                    | j                  }|t           j        k    r | j                            | d| d           t          |t           j                  r| j                            |           | j        |                             |           dS )zAdd an expression to the set of constraints.

        Return whether the expression is a trivial constraint (i.e., an obvious tautology).
        Tz is inconsistent!Fz2Did not expect constraint with no free variables: r   z, obtained by rewriting z# with congruences, is inconsistent!)rR   truer  r  falser  r   r   r-  r  rf   r  r  r  rl  iterr  r  r  r  r  )	rJ   r   	orig_exprorig_reducedrf   r]  old_n_congruencesnew_n_congruencesreduceds	            rM   r  zDimConstraints.add  s   
 5:4	 ))$*:;; 5;&&!((I)H)H)HIIIdEH%% 	)M)Md)S)S 	5(XXXRVXXXXX|q  +//5555 T,''((A #D$5a$8 9 900D99D #D$5a$8 9 9uz!!(,===mmD$455G%+%%%,, ' 'Y ' ' '   $)) 5-11!444)!,00666urN   c                     |j         r3| j                            |                                 d|            dS | j                            ||f           dS )zAdd an equality constraint == N)rs  r  r  r  r  r   )rJ   r*  r   s      rM   add_equalityzDimConstraints.add_equality  sb    > 	? $$%A%A4%A%ABBBBB '..~>>>>>rN   c                 
   i }| j                                         D ]d\  }}g }t                      }|D ]}|j        \  }}t	          j        dd          }	t	          j        |||	z  z
  |g          \  }
}||
k    ryt          j        j        	                    ||	          \  }}t          |t          j                  r7t          |t          j                  r||z  }|                    ||f           |                    |           |rnt	          j        j        j        | \  }}||z
  |z  h||<   ||t	          j        dd          z  |z   i||                             fd|D                        _|||<   f|S )Nreduce_congruences_tmpTinteger)symbolstmpc              3   F   K   | ]}t          j        |          |V  d S rG   )rR   checksol)r   r  substitutions     rM   rj  z5DimConstraints._reduce_congruences.<locals>.<genexpr><  sI       . .#- >*lCC.. . . . . .rN   )r  r   rk  rK   rR   r
  solve_linearpolys	polytoolsdivr   r   r   r  ntheorymodularsolve_congruencer  )rJ   reduced_congruencesr]  congruencesremainder_modulus_pairscongruences_to_checkr  r  r  r  r  solutionmodulus	remainderr  s                 @rM   _reduce_congruencesz"DimConstraints._reduce_congruences  s    "/5577 #	> #	>NA{&(##&55 ) 5 5
 *g l#;TJJJ#(#5dWs]6JUVTW#X#X#X  ;;).)>)B)B8S)Q)Q&GY!'5=99 !jTYTa>b>b !$-$7	/66	77KLLL  %((4444 ' 	>%*]%:%KMd%e"	7+,y=G*C)D#A& !7U\%-N-N-N#NQZ#Z[#A&-- . . . .1E. . .    
 *>#A&&""rN   c                     | j         rEd                    | j                   }| j                                          t          d|           d S )N
z*The following inconsistencies were found:
)r  joinr  r  )rJ   r  s     rM   _raise_inconsistenciesz%DimConstraints._raise_inconsistenciesE  sZ      	R))D122C!'')))P3PPQQQ	R 	RrN   c                 
                                        j        r j                                        } j                            |          }t          j        j                            ||          }t          |t          j	                  rt          d |j        D             |          }t          |t          j                  sJ d| d|             |j        \  }}||k    sJ d| d|              j                             j        j        |         d                                          d|            | j        |<    j        }t)                       _        |D ]7}                     |                    | j        |         i                     8                                    j                                         }|                                D ];\  }}	|	D ]1}
| j        vs#t	          j        |
| j        |         i          s                     |
          r|
j        \  }}d j        j         j        j        |         d                                                   }t	          j        |d	
          }ddlm}  ||          g j        j        |<   t=          t	          j        |||z            |          } j                             j                             t	          j        ||d                                        3= j                                        D ]l\  }}	 t          j        j                            ||          }t          |t          j!                  r-t          tE           fd|j        D                                 }t          |t          j	                  r=|j        D ]4} j                             j                             |                     5n2 j                             j                             |                     # tF          tH          f$ r]}tJ          &                    d|           |D ]4} j                             j                             |                     5Y d}~fd}~ww xY w j'        }g  _'        |D ]3\  }} (                    ||                     j                             4 j'        D ]N\  }} j                            |                                 d j                             |                      OdS )zPSolve the system of constraint equations to find simplified constraints
        c              3   N   K   | ] }t          |t          j                  |V  !d S rG   )r   rR   r  rK  s     rM   rj  z'DimConstraints.solve.<locals>.<genexpr>V  s5       [ [CQVQYAZAZ [ [ [ [ [ [ [rN   z$Expected an equality constraint for z, got zExpected a constraint on z instead of on r   r  r  Tr  )ConstantSourcer   c              3   P   K   | ] }|                     j                  |V  !d S rG   )r  r  )r   rL  rJ   s     rM   rj  z'DimConstraints.solve.<locals>.<genexpr>|  s8      (h(hVZVfIgIg(h(h(h(h(h(h(hrN   z!Failed to reduce inequalities: %sN))r0  r  popr  rR   solversinequalitiesreduce_inequalitiesr   r*  rl  rK   r  r  r  r  r  r  r  r  rk  r  r,  r   r  _is_supported_congruencer  r
  torch._dynamo.sourcer3  r/   r  doprintr+  r  NotImplementedErrorrd  r}   r{  r  r  )rJ   r]  exprsr)  r  r^  multivariate_inequalitiesr   r%  r&  r  r  r  tmp_namer  r3  r  rL  ri  symbolic_equivalencesr*  s   `                    rM   solvezDimConstraints.solveK  s    	##%%% + 	*-1133A155a88E}1EEeQOOH(EI.. g [ [ [ [ []effh11mm3mZ[3m3mck3m3mmmm"-KFCQ;;; VA V Vf V V;;; $$	(B1(Ea(H(M(M(O(O%Z%ZUX%Z%Z[[[%(D" )-(G%.1eeD+1 E E4+>q+A'BCCDDDD'')))' + 	*. #668817799 	X 	XNA{) 
X 
X
D///u~jSTVZVijkVlRm7n7n/44Z@@ X(2g#uty'J49KefgKhijKkKpKpKrKr's#u#u#l8TBBBGGGGGG;I>(;S;S:T	237%ehtWs]&C&CQGG-11$)2C2CEHQPQRSPTDUDU2V2VWWW
X 5;;== 	G 	GHAuG =5II%QRSSh11 j#D(h(h(h(h(h(h(h$h$hiiHh	22 K'} J J-11$)2C2CC2H2HIIIIJ )--di.?.?.I.IJJJ'8 G G G?CCC! G GD)--di.?.?.E.EFFFFG G G G GG !% ;&(#1 	J 	JLFDfdmmD4G&H&HIIII !7 	W 	WLFD!%%&U&UDI<M<Md<S<S&U&UVVVV	W 	Ws   ?C5P55R#ARR#c                    |j         \  }}t          |t          j                  rs|j         \  }}t          |t          j                  rt          |t          j                  p3t          |t          j                  ot          |t          j                  }nt          |t          j                  }|ot          |t          j                  }|S rG   )rK   r   rR   rB  r
  r   )clsr  r  r  r	  r  rE   s          rM   r9  z'DimConstraints._is_supported_congruence  s    "g
 dEI&& 	2yHCC..Q:c5=3Q3Q SC//QJsEL4Q4Q D
 dEL11D:
7EM::rN   c                 \      fd fd j                                         D             S )zPReturns a dictionary of the names of symbols to their specialized value
        c                 v    |                                  }j        j        rj        j        |          d| S |S )N = )r  r  r  )rK  r  rJ   s     rM   
debug_namez9DimConstraints.forced_specializations.<locals>.debug_name  s@    88::Dy2 )=dCNNNNNrN   c                 l    i | ]0\  }}|j         v  j        j        |         d                    |1S r   )r  r  r  )r   r]  r^  rG  rJ   s      rM   r   z9DimConstraints.forced_specializations.<locals>.<dictcomp>  sP     
 
 
3D((( Jty1!4Q788#(((rN   )r  r   )rJ   rG  s   `@rM   forced_specializationsz%DimConstraints.forced_specializations  s\    	 	 	 	 	
 
 
 
 
-3355
 
 
 	
rN   c                 J    t          |t          j        j        j                  S rG   )r   r   exportdynamic_shapes_DerivedDimrJ   r   s     rM   _is_derived_dimzDimConstraints._is_derived_dim  s    #u|:FGGGrN   c                     t          |t          j        j        j                  o$t          |t          j        j        j                   S rG   )r   r   rL  rM  _DimrN  rO  s     rM   _is_dimzDimConstraints._is_dim  s:    sEL7<== MsEL$?$KLLL	
rN   resultsname_to_dimNc           
      @
    ddl m}  fd}i }t          |                                          D ]v\  }}d|v rkt	          |d         t
          j                  rJt          t          |d         j	                            }t          |          |vr||t          |          <   t
          j        j                            |d         |          \  }	}
|                    dd          }t          j        ||
z
  |	z            }|                    dt"                    }t          j        ||
z
  |	z            }||d|t          |          <    |t          |          ||          |t          |          <   |                    dd	           |                    dd	           x|                                D ]}t          |                                          D ]\  }}d|v rt	          |d         t
          j                  rwt          t          t          |d         j	                            x}          |k    r=|t          |                   d         }|d                             ||i          }||d<   t-                      }|                                D ]\  }}||vr
                     ||                   rd|v sd|v r|                    |           Cd|v rmt	          |d         t
          j                  rMt          t          |d         j	                            }|J |                    t          |                     |                    |          }i }|D ]K}d
}||v rD||         }d|v sd|v st	          |d         t4                    r ||||                   s|||<   d}|r|                                D ]\  }}||vr
||         }|j        j        dk    r|j        j        |k    rd|v sd|v rt          j        |          }t          t          |j	                            }t?          t          j         ||d                   |          d         t?          t          j         ||d                   |          d         d} ||||                   s|||<    nMt          |!                                          D ])}||vr "                    ||                   s||v r||= *|#                    |           d	S )a
  
        Here we resolve 2 concerns with derived dims suggested fixes: 1) newly introduced roots,
        and 2) root swapping.

        1) Newly introduced roots appear with modulo guards, e.g. Mod(dx, 2) = 0 suggests
        dx is a derived dim equal to 2 * _dx, introducing a new root _dx. Currently the final
        suggested fixes handle this correctly, but we can get intermediate results that look like
        {"dy": {"eq": "dx + 1"}, "dx": {"eq": "2 * _dx + 1, "min": 3, "max": 15}}
        and this routine prettifies this by unifying to a single root, and making each suggestion
        either a derived dim or min/max range, not both.

        2) With suggested fixes for derived dims, roots can be swapped,
        e.g. dx, dx - 1 -> dy + 1, dy. Here we don't want to print out the attached name,
        since this leads to messages like "dx - 1 = Dim("dx - 1", ...)".
        Instead we evaluate the new root value, and remove results for its derivations.

        First we find all the original roots (specified in dynamic_shapes), that are found in the
        values of results (i.e. used for computing suggesting fix values). These original roots
        (suppose `dx`) are either specialized, unchanged, refined, or swapped
        (expressed as a derived dim). If any of the first 3 cases happen, we suggest `dx`'s value
        in results, and remove suggestions for derivations of `dx`, assuming the derived relation
        is valid. If swapped, we find the new root, and use the fix to evaluate `dx`'s new value,
        and then do the same with `dx`'s derivations.

        Assuming the originally specified derived relations are correct is valid, because:
            1) if the relations are plain wrong (e.g. input shape = (6, 4) with spec (dx, dx - 1))
               produce_guards() will catch this and crash before hand.
            2) if the relations are numerically correct but do not match the emitted guard,
               for example:

                    def forward(self, x, y):
                        return x.reshape([-1]) + y  # guard: s0 * 2 = s1
                    inputs = (torch.randn(6, 2), torch.randn(12))
                    dx = Dim("dx", min=2, max=32)
                    dynamic_shapes={"x": (dx, 2), "y": (dx + 6, )}  # this matches values but not op

               then this leads to 2 linear equations, and a) produce_guards() is able to solve for
               the unique solution of dx = 6 and specialize, and b) the export constraint solver will
               raise an issue due to range constraints (a unique solution means not all values in a
               range satisfy a guard) and also force specializations.
        r   )Dimc                                         |          ood| v pd| v og|j        dk     r|                     dd          dk    p|j        |                     dd          k    o#|j        |                     dt                    k    S )Nr  r  r   )rS  r  r   r  r0   )cr   rJ   s     rM   _check_same_rangezDDimConstraints._process_derived_dim_roots.<locals>._check_same_range  s     S!! 4aZ-5A:4 Wq[9QUU5!__%9 2w!%%q//1	4 GquuUF333rN   r  r  r   r  )r  r  NTFrN  r   )$torch.export.dynamic_shapesrW  r   r   r   rR   rS   rl  r  rf   r  r  r   r!  r   rN  ceilr0   r  r5  r  subsrk  rS  r  r  r   rL   rO   rC  r  r/   r  r@  rP  r  )rJ   rT  rU  rW  rZ  introduced_rootsr   rY  rC  r*  r+  c_minmin_c_maxmax_old_rootr  new_root_exprnew_exprmodified_rootsmodified_root_valuesswapped_rootr   r   r]  r  s   `                         rM   _process_derived_dim_rootsz)DimConstraints._process_derived_dim_roots  sX   \ 	433333
	 
	 
	 
	 
	, ,.)) 	' 	'DAqqyyZ$<<yD4!56677t99K//23$SYY/).)>)B)B1T7D)Q)Q&GYEE%OOE9ei&77%BCCDEE%00E:uy'8G&CDDD15d)C)CGCII&-0ST$-O-O-OKD		*EE%&&&EE%&&& )//11 		' 		'HW]]__-- ' '1AII"1T7EJ77 d4$0D+E+E&F&FFFGG8SS$+CMM$:4$@M w||V],CDDH&AdG' $'55MMOO 	. 	.DAq##||KN++ .!uzz""1%%%%z!D'5:>>D4!56677'''""3t99--- (223CDD
 ;=" !	& !	&DLwDMaZZ5A::!!D'3// $.
 -,QD0ABB 756,T2#(L & $MMOO & &DAq++ %a.C}->>38CTX\C\C\ A::!#(=#3#3D $T$*;%<%< = =A'0$%1I1I1'M'Ma'P'0$%1I1I1'M'Ma'P& &F $5#4V[=N#O#O &=C 4T : % gllnn%% 	 	A####KN33 qN7J7JAJ 	+,,,,,rN   original_signaturerM  c                     ddl m}  j        j        sdS d# fd	t	          t
                    |i }d }fd} ||          } j                             j                  D ]}	 |	          }
|
|	k    rt          j
        d	|
          \  }}}|                                }|d
k    r||k    rN|                                r |||t          |                     }|                                r$ || ||          t          |                     |d
k    s
J |
            t          j        |          |         d<   fdt!                                          d           D             d}rt%                      }D ]r}||
                    d          d                  }                     |          r |                    |j        j                   X|                    |j                   s|dd                    t!          |                     dz  }                                D ]\  }	}|d|	 d| dz  }                     |           g }g }fdt!                                          fd          D                                             D ]$\  }}d|v rb|d         }t5          |t                    r|                    | d|            Bt9          |          r|                    | d|            l|                    dd          }|dk    rd}|                    dd          }|$|"|                    | d| d| d| d           ||                    | d| d| d           | |                    | d| d| d           	|                    | d| d            &|s|r |d!z  }|d"                    ||z             z  }|S )$z/Format a message for constraint violation errosr   )_get_dim_name_mappingr   Fc                     j         j                                        D ]3\  }}|s|                     ||          n|                     ||          } 4| S rG   )r  r  r   r  )r]  inverser   r   rJ   s       rM   	transformz2DimConstraints.prettify_results.<locals>.transformy  sV    	;AACC H H1+2GAIIaOOO		!QHrN   Nc                 V    | dk    rdS | dk    rdS | dk    rdS | dk    rdS | dk    sJ | S )N<=>=<>==rZ   )r   s    rM   flipz-DimConstraints.prettify_results.<locals>.flip  sL    TzztTzztSyysSyys::::IrN   c                     |dk    r||          d<   d S |dk    r|dz
  |          d<   d S |dk    r||          d<   d S |dk    r|dz   |          d<   d S |dk    sJ ||          d	<   d S )
Nrq  r  rs  r   rr  r  rt  ru  r  rZ   )r   r   digitrT  s      rM   relation_with_digitz<DimConstraints.prettify_results.<locals>.relation_with_digit  s    Tzz',e$$$s',qye$$$t',e$$$s',qye$$$Tzzzz&+d###rN   z( == | <= | >= | < | > )ru  r  c                 "    i | ]}||         S rZ   rZ   )r   r   rJ  s     rM   r   z3DimConstraints.prettify_results.<locals>.<dictcomp>  s1     "
 "
 "
 %a("
 "
 "
rN   c                 8    |                      d          d         S )NrF  r   )splitrP  s    rM   r   z1DimConstraints.prettify_results.<locals>.<lambda>  s    aggennQ/ rN   r  rF  z'Specializations unexpectedly required (, 9)! For more information, run with TORCH_LOGS="+dynamic".
z%  - solving the guards generated for z$ resulted in a specialized value of z.
c                 "    i | ]}||         S rZ   rZ   )r   r   rT  s     rM   r   z3DimConstraints.prettify_results.<locals>.<dictcomp>  s,     
 
 
Awqz
 
 
rN   c                      | d          S )NT)rn  rZ   )rQ  ro  s    rM   r   z1DimConstraints.prettify_results.<locals>.<lambda>  s    ii4888 rN   r  r   r  z = Dim('z', min=z, max=r  z', max=z')z
Suggested fixes:
  
  F)r[  rl  r  r  r   dictr  rn  r  rer|  stripisdigitr   rR   r  r  r@  rk  rP  r  rC  rO   r/  r   ri  r   r   rW  r   )rJ   rj  rM  constraint_violation_errorrJ  rl  rv  ry  rU  r]  r8  leftr   rightbufdebug_namesr   r   r^  dimsothersrY  otherr`  rb  rT  ro  s   `   `                    @@rM   prettify_resultszDimConstraints.prettify_resultsl  s8    	FEEEEEy2 	2	 	 	 	 	 	
 d##!N
	 
	 
		, 	, 	, 	, 	, ,+N;;%++D,ABB 	; 	;A	!AAvv h'BAFFOD"eBTzzdemm}} ;##D"c%jj9999 ;##E4488SYY????Tzzz1zzz&+mE&:&:d##"
 "
 "
 "
&++--//  "
 "
 "
 ! 	o%%K+ 2 2!!''%.."34'',, 2OOCH$56666OOCL1111J$))F;DWDW:X:X J J JC 16688 o o3nqnnfinnnn''===
 
 
 
#)8888$ $ $
 
 
 MMOO 	5 	5DAqqyy$eS)) 4MMQ"2"25"2"23333.u55 4MMQ"2"25"2"2333uuUD))199DuuUD))#(8KK1 K Ka K K K KD K K KLLLL%KK1 ? ?a ? ? ? ? ?@@@@%KK1 ? ?a ? ? ? ? ?@@@@KK1 3 3a 3 3 34444
  	.6 	.++C6;;tf}---C
rN   )NNN)rO   rP   rQ   r  rI   r  r  r   r  r  r  r,  r0  rA  classmethodr9  rJ  rP  rS  r   r  r   ri  r   	Signaturer   r   r   r   r  rZ   rN   rM   r  r  W  s        
7* 7* 7*r@ @ @Df f f<t < < < <+4 + + + +Z? ? ?'# '# '#RR R RDW DW DWL   ["
 
 
 H H H
 
 
n-c4S>)*n- #s(^n- 
	n- n- n- n-f RV#'#D D#-D !tCH~uSz49'L!MND D D D D DrN   r  c                   Z    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   eed<   eed<   d	S )
ShapeEnvSettingsz
    Encapsulates all shape env settings that could potentially affect
    FakeTensor dispatch. Used when creating dispatch cache keys.
    allow_scalar_outputsallow_dynamic_output_shape_opsassume_static_by_defaultspecialize_zero_one
duck_shape+prefer_deferred_runtime_asserts_over_guards'allow_complex_guards_as_runtime_assertsN)rO   rP   rQ   r  r   rT   rZ   rN   rM   r  r    sr          
 $((((""""15555-111111rN   r  c                      e Zd Zddddee         deee                  ddfdZddddddddd	d
ee         fdZe	d             Z
e	d             Ze	d             Ze	d             Ze	d             Ze	d             Ze	d             ZddZdeee                  fdZdefdZed             Z e            dej        dej        fd            Z e            dej        deddfd            Z e            dej        dej        fd            Z e            ddej        d ee         d!ee         fd"            Z e            dej        d ed!efd#            Z e            d$             Z d% Z! e            d&             Z" e            d'             Z#ed(             Z$ e            d)             Z% e            d*             Z&d+e'deej                 fd,Z(d-ej        d.e)ddfd/Z*dd0Z+dd1Z,dd2Z- e            d3e.d4e/de/ee0j1        j2                 ef         fd5            Z3d-ej        d.e)dee0j1        j2                 fd6Z4d7ee0j1        j2                 ddfd8Z5d7e0j1        j2        ddfd9Z6d: Z7 e            d;             Z8 e            d<             Z9ed=             Z:d> Z;d? Z<d@e=e         d+e'dAe>deej                 fdBZ?dCe/e         d+e'dAe>deej                 fdDZ@ddEdFe0jA        d+e'dAee>         fdGZBdefdHZC e            ddEd@e=e         dIe=e         dJedKe=e         d+e'dAee>         fdL            ZD e            ddMdNdOdPee         d+ee'         fdQ            ZE e            ddMdNdOdPee         d+ee'         fdR            ZF e            dS             ZGddTZHdUeIdVeJfdWZK e            dX             ZL e            dY             ZMd-ej        defdZZN e            d[             ZO e            ePjQ        dfd\eReeSeTeUf         d+e'd]ePd^eVddOf
d_            ZW e            ePjQ        ddddfd\ed+e'd]ePd^eVd`ee         daeddOfdb            ZXdcej        d\efddZYde ZZdf Z[dg fddddddhdiee\         djee]e>                  dkee^         deeI         fdlZ_dddmdieee\                  fdnZ`do Zadp ZbddqdrZcds Zddt Zedu ZfddvZgddcej        dwedeJfdxZheiddyee/dz                  d{ede/dO         fd|            Zj ekd          d}dOde/e/d~                  fd            ZleidddddddcdOded{edwedee/ej                          dee/e/ej        eJf                           ddfd            Zmeidd            Zneid             Zoeidd            Zp ekd          dddd            Zq ekd          dd            Zrdddee         fdZsd ZtddzddOdeIddfdZuddZvei e            dd                        Zw ekd          dd            ZxdeJfdZydeJfdZzeid             Z{d Z|ddefdZ}dUeIdefdZ~ ekd           ed          	 	 dddddOdwedefd                        Z	 	 dddddOdwedefdZd Z ed          ddd            Zdcej        ddfdZ ekd           e            dej        dedefd                        ZdS )r^   N)should_record_eventstracked_fakesr  r  r   c                >    | j         di | d|d<   ddlm}  |            | _        ||n| j        ot          j         | _        | j        ot          j        | _        | j         | _	        || _
        | j        rt          t          |          gng | _        i | _        d S )NFr  r   translation_validation_enabled)r  rZ   )_inittorch.fx.experimental.validatorr  _translation_validation_enabledr   translation_validation_no_bisectr  check_shape_env_recorded_eventscheck_recorded_eventsis_recordingr  r   r^   eventsfake_tensor_cache)rJ   r  r  r  r  s        rM   rI   zShapeEnv.__init__	  s     	
V */%&RRRRRR/M/M/O/O, $/ !  4 @?? 	! %P&*P 	"
 !% 99* 9=8QY]8F33344WY 	 [] 	rN   TF)r  r  r  r  r  	co_fieldsr  r  r  c          	         |t           j        }t          |||||||          | _        g | _        i | _        i | _        i | _        i | _        i | _	        i | _
        i | _        i | _        t                      | _        t                      | _        i | _        |r.t#          j        d          t#          j        d          d| _        t'          j                    | _        t'          j                    | _        i | _        d| _        t2          | _        | j                            d           d| _        d| _        d | _        t=          j                    | _         t=          j                    | _!        |r|ni | _"        g | _#        | $                                | _%        d| _&        i | _'        i | _(        i | _)        ddl*m+}	  |	            | _,        | j,        rsddl*m-}
  |
            | _.        t^          j0        1                                | _2        | j2        3                    | j2        4                    d                      i | _5        d S d S )	N)r  r  r  r  r  r  r  r   r   r   r   
create_envFr  )TranslationValidator)6r  use_duck_shaper  settingsguardsr  unbacked_var_to_valr  r  r  var_to_stackreplacementsr   rk  	divisible	size_like
val_to_varrR   r   	itertoolscountunbacked_symfloat_counterunbacked_symint_counterdeferred_runtime_assertsnum_deferred_runtime_assertsr}   r~   r  runtime_asserts_frozendim_constraintscollectionsCountercountersymbol_guard_counterr  r  r  _prev_cache_keyr  fx_node_cachesource_to_symbolunbacked_alloc_orderr  r  r  r  	validatorr   r   Graphr  inserting_beforeoutputname_to_node)rJ   r  r  r  r  r  r  r  r  r  r  s              rM   r  zShapeEnv._initJ	  sf   P .J(!5+I%= 3!8c4[

 

 

 )+ >@ GI 
 >@9;&@BCE =?DF*-%% -0EE 24 	I"'-"2"2u}Q7G7GHHDO)2):):&'0'8'8$: RT% -.)|$$$&+#9="*,, %0$7$9$9! '07R( CE+  $}} ! UW9;6 >@!RRRRRR/M/M/O/O,/ 	=LLLLLL1133DN))DJ J''
(9(9$(?(?@@@ ;=D#	= 	=rN   c                     | j         j        S rG   )r  r  r  s    rM   r  zShapeEnv.allow_scalar_outputs 
  s    }11rN   c                     | j         j        S rG   )r  r  r  s    rM   r  z'ShapeEnv.allow_dynamic_output_shape_ops$
  s    };;rN   c                     | j         j        S rG   )r  r  r  s    rM   r  z!ShapeEnv.assume_static_by_default(
  s    }55rN   c                     | j         j        S rG   )r  r  r  s    rM   r  zShapeEnv.specialize_zero_one,
  s    }00rN   c                     | j         j        S rG   )r  r  r  s    rM   r  zShapeEnv.duck_shape0
  s    }''rN   c                     | j         j        S rG   )r  r  r  s    rM   r  z4ShapeEnv.prefer_deferred_runtime_asserts_over_guards4
  s    }HHrN   c                     | j         j        S rG   )r  r  r  s    rM   r  z0ShapeEnv.allow_complex_guards_as_runtime_asserts8
  s    }DDrN   r  c                 `    d}dt           dt          dt          fd}t          | |||           dS )z1Compare another ShapeEnv for equivalence
        )r  r}   r  r  r  r  r  r  r  r  r  r  r  r  r  r  valuer   c                    | dv rddl m } t           ||                    S | dk    rd |D             S | dk    rd |                                D             S | dk    r!t          |                                          S | d	v rd S |S )
N)r  r  r   )copyr  c                     g | ]	}|j         
S rZ   r   r   gs     rM   rM  z;ShapeEnv.check_equal.<locals>.map_value.<locals>.<listcomp>g
  s    ...1...rN   r  c                 .    i | ]\  }}|d  |D             S )c                     g | ]	}|j         
S rZ   r   )r   ras     rM   rM  zFShapeEnv.check_equal.<locals>.map_value.<locals>.<dictcomp>.<listcomp>j
  s    222BG222rN   rZ   )r   r]  rass      rM   r   z;ShapeEnv.check_equal.<locals>.map_value.<locals>.<dictcomp>j
  s-    OOOvq#22c222OOOrN   r  )r  r  r  )r  rl  r   rk  r@  )r  r  r  s      rM   	map_valuez'ShapeEnv.check_equal.<locals>.map_value]
  s    NNN%%%%%%
 DDKK(((......222OOOOOO&&5::<<(((gggtLrN   N)r  r   r   )rJ   r  non_state_variable_namesr  s       rM   check_equalzShapeEnv.check_equal<
  sP    $
 2	3 	s 	s 	 	 	 	, 	$D%1I9UUUUUrN   c                 Z    | j         d S ddlm dffdfd| j         D             S )Nr   )TrackedFakefakec                     t          | j        t          j        t          j        f          r| j        nt          j        | j                  } || j        | j                  S rG   )	r   r  r   r"   r!   r   	from_faker*  rT  )r  
inner_faker  s     rM   maybe_transform_fakez>ShapeEnv._snapshot_tracked_fakes.<locals>.maybe_transform_fake{
  sV    di%,)GHH9#-di88  ;z4;8MNNNrN   c                 &    g | ]} |          S rZ   rZ   )r   r  r  s     rM   rM  z4ShapeEnv._snapshot_tracked_fakes.<locals>.<listcomp>
  s%    JJJt$$T**JJJrN   )r  torch._dynamo.variables.builderr  )rJ   r  r  s    @@rM   _snapshot_tracked_fakesz ShapeEnv._snapshot_tracked_fakesu
  sp    %4??????	O{ 	O 	O 	O 	O 	O 	O KJJJt7IJJJJrN   c                 0    t          | j                  dz
  S r  )r  r  r  s    rM   _last_event_indexzShapeEnv._last_event_index
  s    4;!##rN   c              #   H   K   d| _         	 d V  d| _         d S # d| _         w xY wNTF)r  r  s    rM   
_recordingzShapeEnv._recording
  s>       	&EEE %DD%%%%s    	!orig_sr  c                 4    |                      ||d           d S )Neliminate_unbacked)_set_replacement)rJ   r  r  s      rM   r  zShapeEnv._eliminate_unbacked
  s!    fe-ABBBBBrN   r   r   c                 >    t          j        |          | j        |<   dS )zUsed only when propagate_real_tensors; registers a value for an
        unbacked symbol, which can be used last resort to resolve hints.N)rR   r  r  )rJ   r   r   s      rM   r  z ShapeEnv.set_unbacked_var_to_val
  s!     ',mA&6&6 ###rN   c                    t          |t          j                  s
J |            t          |t          j                  s
J |            t          |          s
J |            t          |          s
J |            | j                            |          }t          |          rJ | d|             |                     ||d           || j        |<   ||                     ||d           d S d S )Nz -> rename_unbacked_torename_unbacked_to_dest)r   rR   r
  r{  r  r   r  r   )rJ   r  r  dests       rM   r  zShapeEnv._rename_unbacked_to
  s   &%,//77777%..55555$U++22U222$V,,44f444 $$V,,(..EE60E0Et0E0EEEEfe-ABBB*/'!!%/HIIIII rN   r   r  r  c                     |d}|t           }||k     rt          d          |                     |||           | j                            |           d S )Nr   r  r  r  )r0   r  r  r  r  rJ   r   r  r  s       rM   r  z"ShapeEnv._constrain_range_for_size
  s|    ;C;C993  
 	## 	$ 	
 	
 	

 	1rN   c           	      *   t          |t          j                  rD|t          |          cxk    r|k    s(n t	          dt          |           d| d| d          d S t          |t          j                  r|                     |||           d S d S )Nr  r  r  r  r  )r   rR   r   r   r4   r
  r  r  s       rM   r  zShapeEnv._constrain_range
  s    a'' 	3q66((((S((((%&Ws1vv&W&W3&W&WQT&W&W&WXXXF a&& 	''   (     	 	rN   c                 &   t          |t                    st          |t                    s
||k    sJ dS t          |j        j        t          j                  s
J d            |j        j        | u sJ t	          j        |          | j        |j        j        <   dS t          |j        j        t          j                  s
J d            |j        j        | u sJ t          |t                    s(t	          j        |          | j        |j        j        <   dS |j        j        |j        j        u sJ t          |j        j        t          j                  s
J d            | 	                    |j        j                  }|| j        |j        j        <   dS )z
        Given two SymInts, constrain them so that they must be equal.  NB:
        this will not work with SymInts that represent nontrivial expressions
        (yet!)
        r  N)
r   r"   r   r   rR   r
  r   r   r  r?  )rJ   r   r  new_vars       rM   r  zShapeEnv._constrain_unify
  sr    !V$$ 	9a(( BAvvvvvv!!&+u|<<\\>\\\\v'4////16q1A1A!!&+...
 afk5<88XX:XXXX6#t++++a(( 916q1A1A!!&+...v'16+;;;;;!!&+u|<<\\>\\\\**QV[1118!!&+...rN   c                 .    t          t          dd          S )Nignore_fresh_unbacked_symbolsFr  TLSr  s    rM   "_ignore_fresh_unbacked_symbols_tlsz+ShapeEnv._ignore_fresh_unbacked_symbols_tls
  s    s;UCCCrN   c                     dt           _        d S r   r  r  r  s    rM   $_ignore_fresh_unbacked_symbols_enterz-ShapeEnv._ignore_fresh_unbacked_symbols_enter
  s    ,0)))rN   c                     dt           _        d S ru  r  r  s    rM   #_ignore_fresh_unbacked_symbols_exitz,ShapeEnv._ignore_fresh_unbacked_symbols_exit
  s    ,1)))rN   c              #      K   |                                   	 dV  |                                  dS # |                                  w xY w)za
        Indicates that the newly allocated unbacked SymInts are being
        discarded
        N)r  r	  r  s    rM   r  z&ShapeEnv.ignore_fresh_unbacked_symbols
  sX       	11333	7EEE4466666D446666	   2 Ac                     d| _         dS )zFreeze this ShapeEnv to stop accumulating guards

        A frozen ShapeEnv will ignore any further guards generated on it and
        only emit a warning which may lead to accuracy problems.
        TNr  r  s    rM   freezezShapeEnv.freeze	  s     rN   c                     d| _         dS )a_  Freeze this ShapeEnv to stop adding deferred runtime asserts.

        We will error if you try to install a new runtime assert when it is
        frozen.  This would indicate a lowering violation, or perhaps something
        we know statically is already True but we are checking it again in a way
        that is not clearly dischargeable.
        TN)r  r  s    rM   freeze_runtime_assertszShapeEnv.freeze_runtime_asserts  s     '+###rN   r*  c                     | j         sd S |                                }|| j        vrt          j        |d          | j        |<   | j        |         S )NTr  )r  r  r  rR   r
  )rJ   r*  srcnames      rM   _create_symbol_for_sourcez"ShapeEnv._create_symbol_for_source  sW    3 	4++--...-2\'4-P-P-PD!'*$W--rN   r  r   c                 N    | j         r| j                            ||           d S d S rG   )r  r  add_var)rJ   r  r   s      rM   
_add_z3varzShapeEnv._add_z3var&  s5    / 	1N""6400000	1 	1rN   c                 L    | j         r| j                            |           d S d S rG   )r  r  add_target_exprr  s     rM   _add_target_exprzShapeEnv._add_target_expr*  s3    / 	1N**400000	1 	1rN   c                 L    | j         r| j                            |           d S d S rG   )r  r  add_assertionr  s     rM   _add_assertionzShapeEnv._add_assertion.  s3    / 	/N((.....	/ 	/rN   c                 J    | j         r| j                                         d S d S rG   )r  r  validater  s    rM   _check_translation_validatez$ShapeEnv._check_translation_validate2  s1    / 	&N##%%%%%	& 	&rN   r   rK   c                    ||f}d}| j         r|| j        vrt          d |D                       rt          d |D                       sJ d |fS d}t          d |D                       sJ d|j         d|             | j                            ||          x}| j        |<   || j        |j        <   | j        	                    |d           |fS )NFc              3      K   | ]}|d u V  	d S rG   rZ   r  s     rM   rj  z4ShapeEnv._create_fx_call_function.<locals>.<genexpr>D  s&      ++19++++++rN   c              3   V   K   | ]$}t          |t          j        j                   V  %d S rG   )r   r   r   Noder  s     rM   rj  z4ShapeEnv._create_fx_call_function.<locals>.<genexpr>G  s3      JJz!UX];;;JJJJJJrN   Tc              3      K   | ]}|d uV  	d S rG   rZ   r  s     rM   rj  z4ShapeEnv._create_fx_call_function.<locals>.<genexpr>N  s&      33q}333333rN   zmissing arg in FX graph (z): )
r  r  r  rt  rO   r  r  r  r  r   )rJ   r   rK   node_keyfreshr   s         rM   _create_fx_call_functionz!ShapeEnv._create_fx_call_function6  s    :/ 	0HDDV4V4V ++d+++++ # JJTJJJJJJJJU{"E 33d33333gg5gQSQ\5g5gae5g5gggg26*2J2J2t2T2TTD4%h/+/Ddi(!%%h55u<<rN   c           	      b   | j         sd S | j        j        |ff}|| j        vr|                     ||           t          j        ddt          j        dd|j                            }| j                            |          x}| j        |<   || j        |j        <   ||j	        d<   | j        |         S )Nz[^a-zA-Z0-9]r  z[()]r   r  )
r  r  r   r  r  r  r  r  r  r  )rJ   r  r   r$  mangled_namer   s         rM    _create_fx_placeholder_and_z3varz)ShapeEnv._create_fx_placeholder_and_z3varT  s    
 3 	4J*VI6
 4---OOFD)))6/3wFK8X8XYYL26*2H2H2V2VVD4%h/+/Ddi( #)DIh!(++rN   r   c                     | j         r=|=| j                            |j                   | j                            |           d S d S d S rG   )r  r  r5  r  r  
erase_node)rJ   r   s     rM   _remove_fx_nodezShapeEnv._remove_fx_noden  sW    / 	(D4D!!$),,,J!!$'''''	( 	(4D4DrN   c                     ddl m} | j        r:|                                 |j        t
          <    |            |j        t          <   d S d S )Nr   )get_current_node)torch._dynamo.utilsr.  r  r  r  rj   rk   )rJ   r   r.  s      rM   _add_fx_node_metadatazShapeEnv._add_fx_node_metadatas  s^    888888$ 	=,0,B,B,D,DDI()*:*:*<*<DI&'''	= 	=rN   c                 .    t          t          dd          S )Nsuppress_guardsFr  r  s    rM   _suppress_guards_tlszShapeEnv._suppress_guards_tlsz  s    s-u555rN   c                     dt           _        d S r   r  r2  r  s    rM   _suppress_guards_enterzShapeEnv._suppress_guards_enter}  s    "rN   c                     dt           _        d S ru  r5  r  s    rM   _suppress_guards_exitzShapeEnv._suppress_guards_exit  s    #rN   c              #      K   |                                   	 dV  |                                  dS # |                                  w xY w)z5Context manager to ignore all guards generated insideN)r6  r8  r  s    rM   r2  zShapeEnv.suppress_guards  sX       	##%%%	)EEE&&(((((D&&((((r  c                     t          | j                  t          | j                  | j        t          | j                  fS )z
        Defines the current "state" of the guards we've accumulated in this ShapeEnv.
        Determines when we need to invalidate our cache
        )r  r  r  r  r  r  s    rM   r  zShapeEnv._get_key  s8    
 D%&&DN(;(;T=^`cdhd|`}`}~~rN   c                 v    |                                  }| j        |k    r|| _        | xj        dz  c_        d S d S r  )r  r  r  )rJ   cur_keys     rM   _update_version_counterz ShapeEnv._update_version_counter  sI     --//7**#*D !!Q&!!!! +*rN   ex_sizerT  c                 J    |                      t          |          ||          S rG   )!_produce_dyn_sizes_from_int_tupler?  )rJ   r>  r*  rT  s       rM   _produce_dyn_sizeszShapeEnv._produce_dyn_sizes  s#    
 55eGnnfN^___rN   tensor_sizec                 \   t          d |D                       sJ d|             ddlm}m} t	          |           |j        }|j        }g }t          |          D ]N\  }	}
|                    | 	                    |
 |||j
        |	          ||	         ||	         |                     O|S )Nc              3   6   K   | ]}t          |           V  d S rG   )ra  )r   r^  s     rM   rj  z=ShapeEnv._produce_dyn_sizes_from_int_tuple.<locals>.<genexpr>  s-      ??C{3'''??????rN   z0Expect size to be a plain tuple of ints but got r   r?   TensorPropertyrS  )rt  r:  r?   rF  rU  rb  rd  	enumerater   create_symbolSIZE)rJ   rB  r*  rT  r?   rF  dynamic_dimsconstraint_dimsr  r   r^  s              rM   r@  z*ShapeEnv._produce_dyn_sizes_from_int_tuple  s   
 ??;?????  	B  	B  BBt  BB  BB  	B  	B  	BMMMMMMMM/000'5*;,, 	 	FAsKK**$$V^-@!DDQ"!1 +       rN   rS  exc          
          t           fd                                D                       }t           fd                                D                       }                                                               }                     |||fdt                                                    D             ||          S )z
        Returns a list of symbolic sizes and strides for the given tensor.
        We try our best to express stride in terms of the sizes, so as to not
        introduce new symbolic variables.
        c              3   B   K   | ]}                     |          V  d S rG   #_maybe_specialize_sym_int_with_hint)r   szrJ   s     rM   rj  zHShapeEnv.create_symbolic_sizes_strides_storage_offset.<locals>.<genexpr>  s1      YY@@DDYYYYYYrN   c              3   B   K   | ]}                     |          V  d S rG   rO  )r   sdrJ   s     rM   rj  zHShapeEnv.create_symbolic_sizes_strides_storage_offset.<locals>.<genexpr>  s1      ]]2$BB2FF]]]]]]rN   c                 0    g | ]}t          |          S rZ   )_is_dim_dynamic)r   r   rL  s     rM   rM  zIShapeEnv.create_symbolic_sizes_strides_storage_offset.<locals>.<listcomp>  s#    ===_R##===rN   rS  )r?  r  r  rP  r  -_create_symbolic_sizes_strides_storage_offsetr  r   )rJ   rL  r*  rT  r>  	ex_strideex_storage_offsets   ``     rM   ,create_symbolic_sizes_strides_storage_offsetz5ShapeEnv.create_symbolic_sizes_strides_storage_offset  s     YYYYrwwyyYYYYY]]]]QSQZQZQ\Q\]]]]]	 DDREVEVEXEXYYAA====U26688__===- B 
 
 	
rN   c                     t          |t          t          j        f          sJ t	          |          r1|j        j        | us
J d            |j                                        S |S )NzFexpect the symbol is created from an shape env other than current one.)r   r   r   r"   ra  r   r   r   )rJ   	maybe_syms     rM   rP  z,ShapeEnv._maybe_specialize_sym_int_with_hint  sj    )c5<%899999y!! 	1>+4777X 877>..000rN   rW  rX  is_dim_dynamicc                	    t                    }|d g|z  }d g|z  }	g }
g }t          |          D ]a}||         rt          j        }n  j        rt          j        }nt          j        }|
                    |           |                    |           bt          j        g|z  }
t          j        g|z  }t          |
|||	          }t          |           |j        }|j        }	|j        }|j        }t          d |D                       rt          j        nt          j        }t          d |D                       }t          |          |k    sJ t          |           d|             t          |          |k    sJ t          |           d|             t          |          |k    sJ t          |	          |k    sJ ddlmm                      |          d gt                    z  t)                    D ] \  }}|dv rt+          j        |          |<   !t/          d D                       rfd	t          t                              D             }d
 }t1          fdt          t                              D             |          }|D ]a\  }}|         T||         t          j        k    r>|         |v r4||                  |<   |         |         z  ||         |         z  <   bt/          d D                       rt3          fdt          t                              D             |          \  }}||         }||         t          j        k    r|rt          j        nt          j        }                     | j        |          ||	|         |          |<   t/          d D                       t          d D                       sJ  fdt)          t9                              D             }g }t)                    D ]J\  }}|J |                                         ||          j        |                               K                                          | j                  |d |          | j                            }t?          |          t?          |          |fS )Nrb  rc  rd  re  c              3   6   K   | ]}|t           j        k    V  d S rG   r  r!  r   r  s     rM   rj  zIShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>.<genexpr>4  s*      1`1`Q!z7H2H1`1`1`1`1`1`rN   c              3   6   K   | ]}|t           j        k    V  d S rG   r`  ra  s     rM   rj  zIShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>.<genexpr>5  s*      MM!qJ$55MMMMMMrN   r   r   rE  r  c              3      K   | ]}|d u V  	d S rG   rZ   r   rQ  s     rM   rj  zIShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>.<genexpr>B  s&      ,,!t),,,,,,rN   c                 z    i | ]7}|         
|         dk    |         |         z  |         |         z  8S r4  rZ   )r   r   r>  rW  r  r  s     rM   r   zJShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>.<dictcomp>C  sW       !9(Yq\Q->-> 
Yq\)47VAY+>->->->rN   c                     t          | d                   r(d| d         j                                        | d         fndg| R S )Nr   r   )ri   r   nested_int_coeff)tups    rM   _nested_int_aware_sortzVShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>._nested_int_aware_sortJ  sO     DQQTUVQWCXCX #QA4466A??c	rN   c                 4    g | ]}|         
|         |fS rG   rZ   r   r   rW  r  s     rM   rM  zJShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>.<listcomp>R  s+    TTTq&)BS)A,"BSBSBSrN   r  c              3      K   | ]}|d u V  	d S rG   rZ   rd  s     rM   rj  zIShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>.<genexpr>[  s&      --19------rN   c                 4    g | ]}|         
|         |fS rG   rZ   rk  s     rM   rM  zJShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>.<listcomp>^  s6       !!9, #1q),,,rN   )dynamic_dimconstraint_dimrT  c              3      K   | ]}|d uV  	d S rG   rZ   rd  s     rM   rj  zIShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>.<genexpr>o  s&      11Q1D=111111rN   c                 l    g | ]0\  }\  }}                     || j        |                     1S )r  r*  )r  rI  )r   r   symr  rF  r?   rJ   r*  s       rM   rM  zJShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>.<listcomp>q  sc     
 
 
 ;C ""++FN4GKK #  
 
 
rN   rr  ) r  r  r  r  r  r!  r   r   r#  ro   rU  rd  re  rb  rc  rt  r:  r?   rF  r@  rG  rR   r   r  r  r  rH  STRIDEr   r  STORAGE_OFFSETr?  )rJ   r>  rW  rX  r\  r*  rT  r   rd  re  rJ  rc  r   r  rb  dynamic_offsetare_sizes_staticr^  
candidatesri  val_listr  
dyn_stride	sym_sizesr$  stride_exprr%  rF  r?   r  r  s   ```  `                     @@@@rM   rV  z6ShapeEnv._create_symbolic_sizes_strides_storage_offset   sZ    'll # $v|"&#L O3ZZ 
* 
* "!$ ("*AA2 (")AA"A##A&&&&&q))))&O,s2L)67#=O7* /!1#5	      	/000+<-@(6*: /21`1`R_1`1`1`.`.`u**fpfuMM}MMMMM=!!S(((S-?-?*J*JS*J*J(((?##s***s=/A/A,L,Ls,L,L***#$$++++%&&#----MMMMMMMM!%!G!GQWYi!j!j.2Vc$ii-?	** 	/ 	/FAsf}}!M#..q	,,V,,,,, ,	      s4yy))  J   TTTTTE#f++,>,>TTT*  H ! P P1!9$);z?V)V)V[def[gku[u[u *9Q< 8F1I<@GfQi<OJwqzIaL89--f-----     !&s6{{!3!3   2  Q -Q/
"1%)@@@6F![!2!2JOJ ..((1FJJ *#5a#8%5 /  q	M ,,V,,,,, ,	Z 11&11111111
 
 
 
 
 
 
 #,Cg,>,>"?"?
 
 
	 
'// 	p 	pNA{ ***d44)A,7K7KFTbTikl7m7m 5 o o p p p p!33!$$V^-JKK*#!1    #''0MNN 4 	P 	P Yz!2!24FFFrN   )r*  rs  rm  r  c          	         |r|                                 nd}| j        r_|]|                     |          }|J |                     |t                    }|                     t          j        ||                     nd}t          |t          j	                  r'|t	          |          |k    sJ t	          |          }n7t          |          rd}t          t          || t          ||                    }|S )zCreate a SymInt value from a symbolic expression

        If you know what the current hint value of the SymInt to be created
        is, pass it into hint.  Otherwise, pass None and we will make our best
        guess

        Nfx_node)r  r  r  r)  r   r  rR   r  r   r   r{  r"   r   rJ   rs  r  r*  source_namer  r  outs           rM   r  zShapeEnv.create_symintnode  s    (.7fkkmmm4/ 	F4F33F;;F%%% ;;FCHHG  5 56666Gc5=)) 	I3xx4''''c((CC %S)) dCwGGGHHC
rN   c          	      
   |r|                                 nd}| j        r_|]|                     |          }|J |                     |t                    }|                     t          j        ||                     nd}t          |t          j	                  r'|t	          |          |k    sJ t	          |          }nAt          |          r|
J |            t          t          || t          ||                    }|S )z2Create a SymFloat value from a symbolic expressionNr~  )r  r  r  r)  r   r  rR   r  r   Floatr{  r!   r   r  s           rM   create_symfloatnodezShapeEnv.create_symfloatnode  s    (.7fkkmmm4/ 	F4F33F;;F%%% ;;FEJJG  5 56666Gc5;'' 	MSzzT))))**CC %S)) )||S|||73eT7KKKLLC
rN   c                 ^    |                      |                     |||          ||          S )z1Create a SymInt wrapping a new unspecified symbol)r*  rn  rr  )r  create_unspecified_symbol)rJ   r  r*  rn  s       rM   $create_unspecified_symint_and_symbolz-ShapeEnv.create_unspecified_symint_and_symbol  sJ     %%**' +  
  & 
 
 	
rN   c                 J    t          t          || t          d                    S )z7Create a SymBool object from a sympy boolean expressionN)r    r   r   rJ   rs  s     rM   create_symboolnodezShapeEnv.create_symboolnode  s      wsD$55666rN   prefixr)  c                 $   t           j        d uo-t          |          t           j                            d          v }|                     |          \  }}}t
                              d|||j        |j        |t          |          ||	  	         d S )N,z%s %s [%s, %s]%s (%s)%s
stack_info)
r  extended_debug_create_symbolr  r|  _get_stack_summaryr}   r  r-  r.  r7   )rJ   r  r  r)  is_debugfsummarymaybe_user_locmaybe_extra_debugs           rM   _log_create_unbacked_symbolz$ShapeEnv._log_create_unbacked_symbol  s    6dB  Es6{{V\VyVV  AD  WE  WE  HE6:6M6Mh6W6W3."3%FBHbhX@V@VXiv~ 	 	
 	
 	
 	
 	
rN   c           	         t          t          j        t          | j                            }| j        dxx         dz  cc<   |                                 s| j                            |           t          j
        d          | j        |<   t          j                    x}| j        |<   |j        sJ |                     |t"                    }|                     d||           t'          t)          || t"          d|                    S )z5Create a symbolic float without a hint value
        create_unbacked_symbolr   r  create_unbacked_symfloatNr~  )r<   r;   ry  rl  r  r  r  r  r   r8   extractr  r3   unknownr  is_floatr)  r   r  r!   r   rJ   r  r)  r  s       rM   r  z!ShapeEnv.create_unbacked_symfloat  s      +4+>TEc@d@dee-...!3...6688 	?/66v>>>$5$=1$E$E$E&!)4)<)>)>>Tv&{ 77FF(()CVRPPPeT7KKKLLLrN   c           	         t          t          j        t          | j                  d          }|                                 s| j                            |           | j        dxx         dz  cc<   t          j
        d          | j        |<   |                                 x}| j        |<   |j        sJ |                     |t                     }|                     d||           t%          t'          || t           d|                    S )	z7Create a symbolic integer without a hint value
        Tr  r  r   r  create_unbacked_symintNr~  )r<   r;   rx  rl  r  r  r  r   r  r8   r  r   _default_unspecified_value_ranger  is_intr)  r   r  r"   r   r  s       rM   r  zShapeEnv.create_unbacked_symint  s	     +4+<d4C_>`>`jnooo6688 	?/66v>>>-...!3...$5$=1$E$E$E&!)-)N)N)P)PPTv&y 77DD(()A62NNNgfdCwGGGHHHrN   c                 6    t          |t          j                  S )zSCheck if a sympy symbol matches the naming convention for unbacked symbols
        r=   r;   rx  )rJ   r  s     rM   r  zShapeEnv.is_unbacked_symint   s     fd&7888rN   c           	      :   t          t          j        t          | j                  d          }|                                 s| j                            |           | j        dxx         dz  cc<   t          j
        d          | j        |<   t          dd          x}| j        |<   |j        sJ |                     |t                     }|                     d||           t%          t'          t)          j        |d          | t           d|	                    S )
z7Create a symbolic boolean without a hint value
        Tr  r  r   r  r   create_unbacked_symboolNr~  )r<   r;   rx  rl  r  r  r  r   r  r8   r  r  r3   r  r  r)  r   r  r    r   rR   r  r  s       rM   r  z ShapeEnv.create_unbacked_symbool%  s     +4+<d4C_>`>`jnooo6688 	?/66v>>>-...!3...$5$=1$E$E$E&!)4Q):)::Tv&y 77EE(()BFBOOOwux22D$gVVVWWWrN   r^  rn  ro  c           	      :    |                      ||||ddd          S )zCreate a symbol with an unspecified value

        Compared to standard symbols we do not assume the value is positive,
        nor do we specialze on zero or one values.
        NT)positivedo_not_specialize_zero_onerT  )rH  )rJ   r^  r*  rn  ro  s        rM   r  z"ShapeEnv.create_unspecified_symbol8  s6    $ !!'+! " # # 	#rN   r  r  c                 r   t          |t                    r|j        j        |j        j        k    rrt
          j        }|j        j        |k    r4t          d|j        j         d| d|                                           |r||j	        |j
        <   d|j        |j
        <   d}|                                }t          |t                    r-t          |           |j        vri |j        t          |           <   t          |t                    r>|r<||j        t          |                    v r |j        t          |                    |         S |t
          j        u ri|                                 j        j        }	|                     |	           t          |t                    r|r|	|j        t          |                    |<   |	S |rd}
n| j        }
t          |t*                    sJ t-          |           d|             |r|dk     rJ d|             |t
          j        }|t
          j        u rJt1          j        |          }	t          |t                    r|r|	|j        t          |                    |<   |	S |t
          j        u r| j        }n#|t
          j        u rd}nt9          d	|           |d
v r|
r| j        |         }n|r
|| j        vrit-          |          t<          u st?          |          r0tA          tB          j"        tG          | j$                  |d          }n/tA          tB          j%        tG          | j$                  |d          }t          |t<                    rt1          j        |          | j$        |<   nyt          |tL                    rt1          j'        |          | j$        |<   nGtQ          |j        )                                |j        *                                          | j$        |<   g | j+        |<   | ,                    |t<                     |r
|| j        |<   t          |t<                    r|r6| -                    |dk               | .                                | j/        |<   n| 0                                | j/        |<   t          |t                    r|rJ | j/        |xx         |j        z  cc<   | j/        |         }|j1        sJ ||vr"t          | d|j         d|j         d          d|j         d|j         d}nct          |tL                    rLte          t0          j3         t0          j3                  x| j/        |<   }d|j         d|j         d}|j4        sJ nd}|}tj          j6        duo-to          |          tj          j6        8                    d          v }d}|sd| d}| 9                    |          \  }}}| j:        ;                    d|||                                ||ty          |          |||
  
         | j=        dxx         dz  cc<   n;| j        |         }| j:        >                    d||                                           t          |t0          j?                  rq| j+        |         }|@                    |           |A                                s1|d         A                                r|d         |d         c|d<   |d<   d| jB        |<   t          |t                    r|r||j        t          |                    |<   |S )z>Create a new symbol which is tracked by this ShapeEnv
        zStatic shape constraint of z does not match input size of z, for NFr`  r   z!positive set for negative value: zunhandled dynamic_dim r  Tr  r  )r  r  )coeffr   z not in range [r}  r  [r   r  zC, for more info run with TORCHDYNAMO_EXTENDED_DEBUG_CREATE_SYMBOL=""z*create_symbol %s = %s for %s %s%s (%s)%s%sr  rH  zcreate_symbol %s duck sized %sr   )Cr   r(  r)  r-  r.  r  r!  r   r  rb  idxrd  rp   idrn  r"  r  r   r   r  r  r$   r   r  rR   r   r   r  rd  r  r   ri   r<   r;   rI  r  r  FLOATr   r  r6   
nested_intrg  r  r  r  _default_value_ranger  r  r  r3   oor  r  r  r  r|  r  r}   r  r7   r  r~   r
  r   is_ephemeralr  )rJ   r^  r*  rn  ro  r  r  rT  r  r  r  duckr  
sympy_exprr)  	range_strr  maybe_more_infor  r  r  	r_sourcess                         rM   rH  zShapeEnv.create_symbolS  s    n&<== 
	".BSBY]k]n]tBtBt$+K &#--.+.2C2I + +il + +!;;==+ +     E=H .vz:@D 1&*=!N kkmm')@AA 	PtHH$4$XXXMO@DJ')@AA 	_	_ $4$XY[\`YaYa$bbb#G4QR]^^*777--//49C**3///*,CDD b b^a DRXXN{[J% 	;"'"&":&&))EEd6ll+E+EV+E+EEEETq+Ts+T+T %$,K*+++-$$C*,CDD b b^a DRXXN{[JJO++ ?DDJ...DD !G+!G!GHHH&==0=$AA R	ODO33 CyyC=#5#5(C4H4HS[eijjj

(S5I5IT\cghhh
#s## u.3mC.@.@
++C'' u.3k#.>.>
++ /;38;N;N;P;PX[X`XqXqXsXs.t.t.t
+ /1D
+OOJ,,, 2'1$#s##  \''
Q777 594M4M4O4OD%j11484Y4Y4[4[D%j1 n.DEE G#OOO%j111^5FF111&z2y   b==2c3a3a"(3a3aVXV^3a3a3abbb777BH777		C'' 5@%(EH5U5UU!*-777BH777	{"""" 	A 34? RJ6#F#L#LS#Q#QQ  !O OAKO O O   ;?:Q:QRZ:[:[7Hn&7HMM<C	X 6 6IZgo     L)))Q.)))) $AHNN;QNNNa&& 
	-+A.IV$$$&&(( JYq\-F-F-H-H J.7mYq\+	!im
 ,-D%a(&(?@@ 	\[ 	\Z[@DJ;WrN   r   c                     t                               d||d           || j        vsJ | d            t          j        |          | j        |<   dS )z0 Adds a new symbol to the symbolic environment. zadd_var_to_val %s %sTr  z already existsN)r}   r~   r  rR   r   )rJ   r   r^  s      rM   add_var_to_valzShapeEnv.add_var_to_val  s]    		($	EEE4?***t,D,D,D*** %c 2 2rN   c                 `    |                                 }| j                            ||          S rG   )r  r  r   )rJ   r*  src_names      rM   _debug_namezShapeEnv._debug_name  s(    ;;==-11(HEEErN   c                    t          |t                    r|j        j        |j        j        }}|                                 }||j        k    rd }||j        k    rd }|                     |           d|                                 d}|$|"|d| d|                     |           d| z  }nE|!||d|                     |           d| z  }n"| ||d| d|                     |           z  }|S |                    |          S )NrF  z in the specified ranger`  r,  )	r   r(  r)  r-  r.  r  r  r  r0  )rJ   r*  rY  r-  r.  r#  c_renders          rM   &_render_range_for_constraint_violationz/ShapeEnv._render_range_for_constraint_violation  sD   a/00 	4:qtz5E//11G%%%%**622]]v{{}}]]]H U%6PPP4+;+;F+C+CPPPPP5#4E 0 0 8 8EEeEEE"u}EEE4+;+;F+C+CEEEOxxrN   c                 *    |                                  S rG   r  r  s    rM   r   zShapeEnv.<lambda>"  s    QVVXX rN   )r  input_contextsequalities_inputs_simplifiedr  r  r  r  c                #   >?@ABCDEFGHIJKL  j                             d            j        r)t           j                  }	                     |	           t                    t          |          k    sJ d d| d            t          j        t          f@d A|@AfdD             }nt          |          t                    k    sJ t          t          |                    D ]j\  }
\  }}t          |@          r| A|          ||
<   )t          |t          t          t          t           f          sJ t          |t"                    rJ kdd	lm?m> g Et+          j        t"                    Lt+          j        t.                    Kg Bd:Bfd
	H>?fdF|r|i It          |          D ]\  }
}|
I|                                <   Ifd}|j        D ]\  }} ||           ||          }}                     t7          j        ||                    }|st;          |                                 dt          |t                    r|n|                     j                   d|                                 dt          |t                    r|n|                     j                             |j         D ]&\  }}} ||          }t          |t6          j!                  r'| j"        |         d                                         fn ||           #                    |          f\  }} ||          }                     t7          j        ||                    }|s{t;          d|                                 d |t7          j!        |                     d| d|                     j                   d|                     j                   
          (|j$        D ](}L|         %                     j"        |                    )d:EH KLfd	}ELfd}t          ||          D ]\  }}}t          |tL                    rddlm'}  ||          }t          |tP                    sJ |Gt          |t          t          f          r |||           pt          |t          t           f          r |||           t          |@          sJ tS          |          rddlm*} t          |tV                    sJ |||j,        |j-        fg}|.                                \  }} |D ]L}!t_          ||!          }"|j0        |!         }#|1                     |||!          |"|#j,        |#j-        f           Mn|||j,        |j-        fg}|D ])\  }}$}%}&te          |$          rMt          |$3                                          D ]*\  }
}' ?|>j4        |
          }( ||(|'|%|
                    +dt          |$3                                          D ]*\  }
}' ?|>j4        |
          }( ||(|'|%|
                    +t          |$5                                          D ]*\  }
}' ?|>j6        |
          }( ||(|'|&|
                    + | ?|>j7                  |$8                                           +g Dts          L j        t/          K:                                           j;                   _<        |sED ]\  }C j=        rP|                                })|) j>        v r3 ?                    t7          j         j>        |)         C                     t          Ct6          j!                  r(L@                    C          r|LC         d         k    r|rAt          |?          r1CjA        r* j         B                    d |           dC             F|          r j<        C                    |C           t          L j"                  E                    C          }*D1                     |           d|*            t          |?          rH|jF        >j4        u r9|r6t          CjG                  dk    rt          t          CjG                            Jt          Ct6          j!                  rCKv r|J                    |LC         d                   sd #                    |           d|                                 d #                    LC         d                    dLC         d                                          d	}+ H|jK         #                    |          |+           t          Ct6          j!                  sJKv r|L                    |LJ         d         CJfd          s߉LJ         d         }d #                    |           d|                                 d #                    |           d|                                 d #                    |           dC                    Jt7          jM         #                    |                    i           d }+ H|jK         #                    |          |+           t/                      Gd!t          d"dfDFGH KLfd#},||n jO        D ]*}- P                    |-jQ        d$%           |,|-           + jR        @                    dg           D ]S}. P                    |.jQ        d$%           S                    |.jQ                  C j<        T                    C           TLU                                D ]/\  J} jV        @                    J          }/|/J jV        vr, jV        J         }/|sJ g }0|/jW        t6          jX         t           fvrt          Ffd&|D                       r2 j<        T                    t7          j[        J|/jW                             |r"|/jW         \                                jW        k    r'|01                    tM          |/jW                             |01                     |d                              |/j]        t6          jX        t          fvrtt          Ffd'|D                       r2 j<        T                    t7          j^        J|/j]                             |01                    tM          |/j]                             t          |0          dk    r4D1                    d(_                    |0                     KJ         }1|1D ] }2t          |2t                    r|2ja         \                                z  b                    |/          s|d         }t7          jc        t7          j^        |/jW        J          t7          j^        J|/j]                            Ct          L j"                  E                    C          }3 d                    ||2          }4d)|4 d*|3 }+ H|2jK         #                    |          |+           t          Jt          jg                  r(D1                    d+ |d                    d           1Br$g }5g }6t/                      }7BD ]u\  }8}}+|8r6d,t          |5          dz    d- |+             }+|51                    |+           >d. |+             }+|61                    |+           |7T                    |           vt          |6          dk    rLd/_                    t          |7                    }7d0_                    |6          }9t;          d1|7 d2|9           t          |5          dk    r(t           B                    d3t          |5                     t          d4di  jj         jk        t          D          t          d5 Lm                                D                       t           jn        m                                d67          d8            j=        rCdd9lomp}:  jR        m                                D ]!};|;D ]}. ?                    |.jQ                   " jV        U                                D ]\  }<}=|=jW        t6          jX         t           fvr- ?                    t7          j^        |=jW        |<                     |=j]        t6          jX        t          fvr- ?                    t7          j^        |<|=j]                             t          jr                    5   |: js         jt                  u                                 ddd           n# 1 swxY w Y   | v                                 DS );a}  
        Generates a list of guards strings which, when evaluated in a context that
        defines tensors for all the sources, returns True or False depending
        on if the guards in the list evaluated to True or not.  Primarily used by Dynamo,
        but this is also helpful for manual testing of guards (see
        evaluate_guards_for_args)

        For convenience in testing, a source is allowed to be a str,
        in which case we will assume it is a LocalSource

        simplified lets you omit duck sizing, equality and 0/1 guards.
        This is useful for testing when you don't care about the boilerplate
        guards, and it may be helpful for user output too (be careful though;
        some equality guards are nontrivial!  It would be nice to get simplified
        output to print them too).  It's private because it's not
        intended for normal use
        produce_guardszlen(z	) != len(r  c                     t          t          j        g|                                 z  t          j        g|                                 z  d g|                                 z  d g|                                 z            S )Nr^  )ro   r  r  r   r#  r7  s    rM   _create_no_constraints_contextz?ShapeEnv.produce_guards.<locals>._create_no_constraints_contextQ  si    +)12QUUWW<!+!8 9AEEGG C"&!%%''!1$(6AEEGG#3   rN   Nc                 J    g | ]}t          |          r |          nd  S rG   )r   )r   r8  
Tensorliker  s     rM   rM  z+ShapeEnv.produce_guards.<locals>.<listcomp>\  sM        6@:5N5N ..q111  rN   r   rE  c                 D                         | |fdf           d S )Nc                  (     r                nS rG   rZ   )r  r  s   rM   r   zNShapeEnv.produce_guards.<locals>.record_constraint_violation.<locals>.<lambda>  s"    D0Q30@0@0@0@c rN   )r   )r&  rG  r  r  constraint_violationss     ``rM   record_constraint_violationz<ShapeEnv.produce_guards.<locals>.record_constraint_violation  s>    !((J(Q(Q(Q(Q(QR    rN   c                 @    t          |           o| j        j        u S rG   )r   proprI  )rK  rF  r?   s    rM   is_dimz'ShapeEnv.produce_guards.<locals>.is_dim  s#    c#788\SXI\=\\rN   c                 "   | j                                                           }|j        | j                 }t	          |t
          j                  r|j        j        S t          |          t          u sJ dt          |                       |S )NzExpected int, got )r  r  r   r  r   r   r"   r   r   r   r   )tensor_dim_srcr  symintplaceholderssource_indexs      rM   get_expressionz/ShapeEnv.produce_guards.<locals>.get_expression  s    #L1D1I1I1K1K$LMN$67fel33 "!;++<<3...0ST&\\0S0S...!MrN   rF  z is not equal to zExpected input z to be equal to z, where z
, but got c           	      D   t                               dt          | j                  ||           t	          |t
                    rt          |          sJ t	          |t
                    r2|j                                        |j                                        }t	          |t
                    r|j        j	        }t	          |t          j                  rN|                             |            |0t	          |t                    s|                             |           nd}t	          |t                    rAfd|j        D             }t#          d |                                D                       rd}n1t	          |t                    r|j        rt)          |          }|dvrd}|rsfd}                    | |          }d| d	                    |            d
}	 |j                            |           |	t1          j        ||                     
                    | |f           d S t          j        |          }
                    | |f           d}t	          |t                    r&||j        j        cxk    r|j        j        k    sn d}nt	          |t                    r|dvrd}|r\                    | |          }d| d	                    |            d| d}	 |j                            |           |	           d S d S )Nztrack_symint %s %s %sFc                 H    i | ]}|j                             |d           S rG   r  r   r   rQ  rJ   s     rM   r   zAShapeEnv.produce_guards.<locals>.track_symint.<locals>.<dictcomp>	  s.    "]"]"]1d&7&;&;At&D&D"]"]"]rN   c              3      K   | ]}|d u V  	d S rG   rZ   )r   r)  s     rM   rj  z@ShapeEnv.produce_guards.<locals>.track_symint.<locals>.<genexpr>
  s&      EEbrTzEEEEEErN   Tr  c                 `    t          j                                      |           }| dS r  )r  r  r;  )r]  sexprrJ   r  r  s     rM   r  z;ShapeEnv.produce_guards.<locals>.track_symint.<locals>.hint  s4    $56F
TXTg$h$h$p$pqr$s$sE&+;;;.rN   Not all values of z are valid because z was inferred to be equal to r  z  was inferred to be a constant (z).)r}   r~   r>   r  r   r"   ra  r   maybe_as_intr   rR   r
  r   r2  r  r(  rf   r  r  rs  r   r  r  r&  r   partialr   r)  r-  r.  )r*  r^  
constraintr]  constraint_violatedsym_vrsr   r  var_with_ranger  input_guardsr  rJ   r  symbol_to_constraintsr  s             rM   track_symintz-ShapeEnv.produce_guards.<locals>.track_symint  s   II-z&+/F/FZXXX!#v..B+c2B2BBBB#v&& .38+@+@+B+B+Nh++--#v&& <eHMa.. %$Q'..v666". *:7N O O / .a044Z@@@*/'!*.DEE ;"]"]"]"]an"]"]"]EEGNN4D4DEEEEE 726/#J0GHH ;; ; #AA  !6: 3* / / / / / / / *.)T)TU[]g)h)hW W W#//77W W W  43&0 ,,V44!*!24!;!;	    ##VQK00000M#&&##VQK000&+#j*@AA 3!4KKKK
8KKKKK.2+
,CDD 3 &((.2+& e%)%P%PQWYc%d%dN]^ ] ]++F33] ]UX] ] ]  0/
0DdFVFVW]F^F^`cddddde erN   c                 r   t                               dt          | j                  |           t	          |t
                    rt          |          sJ t	          |t
                    r2|j                                        |j                                        }t	          |t
                    rZ|j        j	        }t	          |t          j                  r|                             |                                | |f           d S t          j        |          }                    | |f           d S )Nztrack_symfloat %s %s)r}   r~   r>   r  r   r!   ra  r   maybe_as_floatr   rR   r
  r   r  )r*  r^  r]  r  r  s      rM   track_symfloatz/ShapeEnv.produce_guards.<locals>.track_symfloat:  s   II,j.E.EsKKK!#x00DK4D4DDDD#x(( 0SX-D-D-F-F-Rh--//#x(( 1HMa.. 7$Q'..v666##VQK00000K$$##VQK00000rN   LocalSource)
AttrSourcezSkipping guard %sr  r   zThe values of r  z must always be equal.c                 2                         | i          S rG   )r  )rQ  r   r  s    rM   r   z)ShapeEnv.produce_guards.<locals>.<lambda>  s-    hlhuhuw}  @A  wB  iC  iC rN   z) must always be related to the values of z by r  guardr   c                                         | j                  }|v rd S                     |           	 d}t          fd|j        D                       rj                            |          }t          j                                      |          }
	                    |           
                    |           |st          |j                  dk    rt          t          |j                            }|         d         }|         }|D ]}t          |t                    rE                    ||          }d| d| d}	 |j                            |          |	           \t          |t&                    rrt)          d|           d S d S d S # t*          $ rG j                            d	d
                    | j                                                              w xY w)NFc              3   D   K   | ]}|         D ]} |          V  d S rG   rZ   )r   r]  r*  r  r  s      rM   rj  z?ShapeEnv.produce_guards.<locals>.issue_guard.<locals>.<genexpr>  s?      cc!O_`aObccVvvf~~cccccccrN   r   r   r   satisfy the generated guard r  zunrecognized constraint zFailing guard allocated at: 
%sr   )simplifyr   r  r  rf   r  r  r  r;  r   r  r  rl  r  r   r(  r  r&  r  r2  rd  r  r}   r{  r/  r  r  )r  r   
is_trivial
guard_exprr  r*  constraintsrY  r  r  r=  r  issuedr  rJ   r  r  r  s             rM   issue_guardz,ShapeEnv.produce_guards.<locals>.issue_guard  sV   ==,,D v~~JJt"
ccccct/@ccccc @!%!5!9!9$!?!?J./?TM`aaiijnoo
Z(((%%d+++ " Qc$*;&<&<&A&A!$t'8"9"9::F-f5a8F"7"?K( Q Q%a)?@@ Q-1-X-XY_ab-c-cN!M^ !M !M?I!M !M !M   87TEUEUV\E]E]_bcccc'+BCC Q
 !"01OA1O1O"P"PP'Q Q&A&AQ Q       !CRWWU[M_M_MaMaEbEbcccs   E'F% %AG6rZ   axiomsc              3   .   K   | ]} |          V  d S rG   rZ   r   r*  r  s     rM   rj  z*ShapeEnv.produce_guards.<locals>.<genexpr>  +      <<&vvf~~<<<<<<rN   c              3   .   K   | ]} |          V  d S rG   rZ   r   s     rM   rj  z*ShapeEnv.produce_guards.<locals>.<genexpr>!  r  rN   r,  r  r  znot __math_isnan(z  . z  - r}  r.  zConstraints violated (r~  z$%s Warning only constraints violateddynamicc              3      K   | ]}|d V  	dS r   NrZ   )r   r   s     rM   rj  z*ShapeEnv.produce_guards.<locals>.<genexpr>a  s'      #N#N!A#NA#N#N#N#N#N#NrN   T)reverse)
num_guardsrf   symbol_guard_counts)PopulateValidatorrG   )wr}   r  r  r   r  r  r  r   r   r   rG  r   r   r"   r   r!   r   r   r:  r?   rF  r  r   rk  r  r6  evaluate_exprrR   r  r   r  r  r7  r
  r  r  r8  extendr  r  r$   r&   r  rq   rd  re  r  r  rr  r   r:   r  rI  r  rt  ru  r  r  r@  r  r  r  r  r  r   rs  r~   r  r  r;  r  rf   rl  r  rO  r&  rN  r  r#   r  r  r   r  r  r  r   r  r-  r  r0   r  r=  r  r.  r>  r/  r(  r)  r  r*  r  r=   r;   r  r  r9   r  r  sumr  r  r  r
  fx_tracebackpreserve_node_metar  r  runr  )MrJ   r  r  r  r  r  r  r  r  r   r   r8  contextrK  r  src1src2expr1expr2concrete_valrC  rD  rG  expr2_phantom_symbolr  r  r*  r  r  sources_tensors_constraintsr  r  r  inner_tinner_contextcurr_tconstraint_sizeconstraint_stridessproperty_sourcer  r  r  r  r  r  r  boundsr  rY  r  r  	warn_msgs
error_msgsr  r&  errr
  r  rs  r)  rF  r?   r  r  r  r   r=  r  r  r  r  r  r  r  r  sM   `` `                                                          @@@@@@@@@@@@@@@rM   r  zShapeEnv.produce_guards  so   @ 	&''' % 	(/<<IY'''<  CLL0002Z2Z2ZPW2Z2Z2Z000lN3
	 	 	 !    #/  NN
 ~&&#l*;*;;;;;#,S~-N-N#O#O 9 9<Awa,, 9,J,J1,M,Mq)%a&#x)GHHHHH)'4888888z 	NMMMMMMM &2488 + 7 < <LN	 	 	 	 	 	
	] 	] 	] 	] 	] 	]  1	]L#G,, - -3+,SXXZZ((" " " " " " 0<  
d-~d33^^D5I5Iu  $11%(5%2H2HII# 299;; r rJuc4J4J+o55PUP^P^_c_nPoPo r r99;;r r4>uc4J4J+o55PUP^P^_c_nPoPor r   "3!E  T2&s++ DNdTYT`CaCa HT4.t4Q7<<>>??(...0@0@0F0FG "z E  $11%(5&2I2IJJ# 2E#((** E E2el:6677E E!+E E05t0O0OE E $)>>$/#B#BE E   #4"C ] ] 0778KN8[\\\\C	e C	e C	e C	e C	e C	e C	e C	e C	e C	e C	eJ	1 	1 	1 	1 	1 	1  #&lG^"L"L 1	t 1	tAvw&#&& -<<<<<<$V,,ff-----y!fc]++ VQ'''A%011 vq)))a,,,,,,Q// r;;;;;;!'+BCCCCC
 Q 8':TU/+ //11q!  D%a..G$+$:4$@M/66"
6400%6%8	8     177;SU\Uo/p.q+C^ t t?V_.? (( t!*6;;==!9!9 N N2*>*>sNDWYZ*[*[$_b/!:LMMMMN "+6;;==!9!9 N N2*>*>sNDWYZ*[*[$_b/!:LMMMM!*6==??!;!; P P2*>*>sNDY[\*]*]$_b:KA:NOOOO L!5!5c>;X!Y!Y[a[p[p[r[rsssst$ -O%**,,--*	 
  
  ;	p , :p :p7 ^$kkmmG$"777--eht7LW7UW[.\.\]]] tU\22$((.. .t4Q777
 ! !Z8L%M%M !~ !':zz&?Q?Q<]<]W[<]<]^^^ 6&>> D(55fdCCC)*:JH[\\ddeijj

6 2 2????@@@v';<<p~':::) ;D-..!33!$t'8"9"9::F"466
p 555-66v?OPT?UVW?XYY 64T-=-=f-E-E 4 4&++-- 4 4#//0@0Fq0IJJ4 4O_`dOefgOhOmOmOoOo4 4 4 
 434E4OQUQaQabhQiQiknooo 'tU\::p"777-88AQRXAYZ[A\  _C  _C  _C  _C  _C  D  D 8 /v6q9}T-=-=f-E-E } }&++-- } }-1-=-=c-B-B} }GJxxzz} }#//77} }<@MM6SXS`aeaqaqruavavSwSwJx<y<y} } } 
 434E4OQUQaQabhQiQiknooo (	z (	d (	 (	 (	 (	 (	 (	 (	 (	 (	 (	 (	 (	 (	Z !' 2ff 	 	E**5:b*AAMK /33D"== 	+ 	+B**272*>>J==))D $$T****  05577 2	L 2	LOFG!%%f--Ay!222%f-NNNFwy6'222<<<<G<<<<< H(,,UXfag-F-FGGG # 0ag1J1J1L1L1R&R&RMM#ag,,///MM**WQZ00111wux000<<<<G<<<<< H(,,UXfag-F-FGGGc!'ll+++6{{QV[[00111 4F;$  A!!%;<<  !"t'@'@'B'B BLLQOO %,QZF#(9UXagv-F-FQWYZY`HaHa#b#bD):;KZY]Yl)m)m)u)uvz){){J-1-X-XY_ab-c-cN n^ n nbl n n   87 ! $ 0 0 8 8 #   fdj11 LJGAJ1G1GJJJKKK  	RIJ%%K.C 0 0*	:s 0<s9~~1<<SSUU<<C$$S))))(..C%%c***OOJ////:"""ii{(;(;<<ii
++.[     
 Y!##		@#i..QQQ., "%jj ##N#N/?/F/F/H/H#N#N#N N N (.d.G.N.N.P.PZ^'_'_'_  	
 	
 	
 / 	DIIIIII
 4;;== 3 3 3 3B))"'22223  ,2244 C CR8UXIw#777))%(28S*A*ABBB8EHf#555))%(3*A*ABBB 022 D D!!$*dn==AACCCD D D D D D D D D D D D D D D >,,...s   F)AGGAG
GAG
r  r  c                    ddl m d t          t          |                    D             }|                     |fd|D             ||          }|rd                    |          S dS )a  
        Expected to be used with evaluate_guards_expression(). Produces the guards
        for the given placeholders and returns a string expression to be evaluated
        by evaluate_guards_expression given concrete values for the placeholders.
        r   r  c                     g | ]}d | S r7  rZ   r   r   s     rM   rM  z6ShapeEnv.produce_guards_expression.<locals>.<listcomp>  s    ???WWW???rN   c                 &    g | ]} |          S rZ   rZ   )r   r   r  s     rM   rM  z6ShapeEnv.produce_guards_expression.<locals>.<listcomp>  s!    ///[[^^///rN   r%  r  N)r:  r  r  r  r  r/  )rJ   r  r  r  	arg_namesproduced_guardsr  s         @rM   produce_guards_expressionz"ShapeEnv.produce_guards_expression  s     	544444??eC,=,=&>&>???	--////Y///'	 . 
 
  	1<<000trN   c                 t    d | j                                         D             }t          |t          |          S )z?
        To be used by compile_fx to evaluate symexprs
        c                 4    i | ]\  }}t          |          |S rZ   )r  )r   ri  r^  s      rM   r   z-ShapeEnv.evaluate_symexpr.<locals>.<dictcomp>  s$    BBB3ABBBrN   )r  r   evalre   )rJ   coderK   s      rM   evaluate_symexprzShapeEnv.evaluate_symexpr  s8     CB$/*?*?*A*ABBBD,---rN   c                     d t          t          |                    D             }t          |t          dt	          t          ||                    i          S )z
        Expected to be used with produce_guards_expression(). Evaluates an expression
        generated by produce_guards_expression for the given concrete args.
        c                     g | ]}d | S r7  rZ   r(  s     rM   rM  z7ShapeEnv.evaluate_guards_expression.<locals>.<listcomp>  s    777WWW777rN   L)r  r  r/  re   r  r   )rJ   r0  rK   r*  s       rM   evaluate_guards_expressionz#ShapeEnv.evaluate_guards_expression  sN    
 87eCII&6&6777	D,d3y$3G3G.H.H(IJJJrN   r  c                d    |                      ||          }|r|                     ||          S dS )z[Generate guards for a graph's placeholder values and evaluate the guards with args
        r  T)r,  r5  )rJ   r  rK   r  r0  s        rM   r  z!ShapeEnv.evaluate_guards_for_args  s?     --l--XX 	?224>>>trN   c                     d D             g }| j         D ]<}t          fd|j        j        D                       r|                    |           =|S )z
        Get a list of guards, but pruned so it only provides guards that
        reference symints from the passed in input
        c                 n    h | ]2}t          |j        j        t          j                  &|j        j        3S rZ   )r   r   r   rR   r
  rz  s     rM   r   z-ShapeEnv.get_pruned_guards.<locals>.<setcomp>  s1    YYY1:afk5<3X3XY16;YYYrN   c              3       K   | ]}|v V  	d S rG   rZ   )r   r]  symintss     rM   rj  z-ShapeEnv.get_pruned_guards.<locals>.<genexpr>  s'      ==A1<======rN   )r  rt  r   rf   r   )rJ   r:  r  r  s    `  rM   get_pruned_guardszShapeEnv.get_pruned_guards  sq    
 ZYYYY 	! 	!A====)<===== !a   rN   c                 @   i fd}t          ||          D ]\  }}|	t          |t                    r |||           +t          |t          j                  sJ t          |                                          D ]$\  }} ||                    |          |           %t          |                                          D ]$\  }} ||                    |          |           % ||                                |                                           S )a5  
        Given a paired list of placeholders (fake tensors with
        symbolic sizes) and concrete arguments (regular tensors
        with real sizes), returns a dictionary mapping each
        symbol to its real value.  So for example, if you
        have a placeholder with size (s0, s1), binding
        (2, 4) to it will give you {s0: 2, s1: 4}.  This is
        not guaranteed to bind ALL symbols in the ShapeEnv;
        we can't bind a symbol if it doesn't occur in any placeholder,
        and symbols that already have replacements won't get bindings.

        This is a little duplicative with evaluate_guards but
        it's different enough that it seemed cleanest to make
        another copy.  This assumes the guards are already checked,
        though if it's cheap we'll check for shenanigans
        c                 ~   t          |t                    r|j        j        }t          |t          j                  r.|v r#|         | k    sJ |          d|              d S | |<   d S t          | t          j                  r7| v r'|          |  k    sJ |           d|               d S |  | <   d S d S d S )Nr   )r   r"   r   r   rR   r
  )rL  r^  r]  r   s      rM   bind_symintz*ShapeEnv.bind_symbols.<locals>.bind_symint  s    #v&& ,HMa.. 	,H}}'{c111hqk3L3Ls3L3L11111&)EL11 ,rX~~'|t333!5P5P3$5P5P33333(+t!, ,, ,rN   )	r   r   r"   r   r   rG  r  r  r  )	rJ   r  rK   r>  r8  rL  r   r]  r   s	           @rM   r  zShapeEnv.bind_symbols  sA   " -/	, 	, 	, 	, 	, ,-- 	B 	BFAsy!V$$ C###a.....!!&&((++ , ,1CHHQKK++++!!((**-- . .1CJJqMM1----K**,,a.>.>.@.@AAAArN   c                 *      fd j         D             S )zSReturns a list of guard expressions that aren't statically known (i.e. not trivial)c                 |    g | ]8}                     |j        d                               |j                  9S )rZ   r  )r  r   r  )r   r  rJ   s     rM   rM  z2ShapeEnv.get_nontrivial_guards.<locals>.<listcomp>  s]      B  B  BedFaFabgbluwFaFxFx  GAej))  GA  GA  GArN   )r  r  s   `rM   get_nontrivial_guardszShapeEnv.get_nontrivial_guards  s7     B  B  B  Bt{  B  B  B  	BrN   c                 \    fdd                     fd| j        D                       S )zQFormat this shape env's guard expressions with optional traceback info if verbosec                 t    sdS dd                     d |                                 D                        S )Nr   z
   Guarded at:
c              3       K   | ]	}d |z   V  
dS )z   NrZ   )r   ls     rM   rj  z<ShapeEnv.format_guards.<locals>.format_tb.<locals>.<genexpr>  s&      /O/Oa	/O/O/O/O/O/OrN   )r/  r  )tbverboses    rM   	format_tbz)ShapeEnv.format_guards.<locals>.format_tb  sA     rQ/O/O299;;/O/O/O(O(OQQQrN   r.  c              3   L   K   | ]}d |j           |j                   V  dS )z - N)r   r  )r   r  rH  s     rM   rj  z)ShapeEnv.format_guards.<locals>.<genexpr>  s?      ]]CuzC99U[+A+ACC]]]]]]rN   )r/  r  )rJ   rG  rH  s    `@rM   format_guardszShapeEnv.format_guards  sO    	R 	R 	R 	R 	R
 yy]]]]QUQ\]]]]]]rN   size_obliviousc                       fd|j         D             }|rN j        |                                z  D ]1}||         't          dt                    ||<   ||         j        sJ 2t          ||          S )zPGiven a sympy expression, computes a ValueRanges bound for what values it can bec                 H    i | ]}|j                             |d           S rG   r  r  s     rM   r   z(ShapeEnv.bound_sympy.<locals>.<dictcomp>   s.    UUUa4,00D99UUUrN   Nr   )rf   r  r@  r3   r0   r  r1   )rJ   r   rK  r  rQ  s   `    rM   r1   zShapeEnv.bound_sympy  s    UUUU4CTUUU 
	2 ^l&7&7&9&99 2 2?. '2!V&<&<LO'?11114...rN   r  sympy.Symbolcompute_hintc                 J    |$d  j                                         D             }n fd|D             }d  j        D             }t          j        ||          }|r fd|D             }t          t                              |                                                    S )z
        Given the symbols in an expression, it returns all the runtime asserts that have those symbols
        concatenated with all the guards.
        If symbols is None, it returns all the runtime asserts (and all the guards)
        Nc              3   .   K   | ]}|D ]}|j         V  d S rG   r   )r   rsr  s      rM   rj  z&ShapeEnv.get_axioms.<locals>.<genexpr>  sM       , ,#%(*, ,#$  !v , , , , , , ,rN   c              3   t   K   | ]2}|j         vj                            |d           D ]}|j        V  3dS )rZ   N)r  r  r   r   )r   r]  r  rJ   s      rM   rj  z&ShapeEnv.get_axioms.<locals>.<genexpr>  sf       R R#$1DO3K3K(,(E(I(I!R(P(P 4L3K#$  !v3K3K3K3K3KR RrN   c              3   $   K   | ]}|j         V  d S rG   r   r  s     rM   rj  z&ShapeEnv.get_axioms.<locals>.<genexpr>  s$      ..Q!&......rN   c              3   f   K   | ]+}t          |                    j                            V  ,d S rG   )rc   r  r  )r   r   rJ   s     rM   rj  z&ShapeEnv.get_axioms.<locals>.<genexpr>   s:      ZZa,QZZ-H-HIIZZZZZZrN   )	r  r  r  r  chainr?  r  fromkeysr@  )rJ   r  rO  runtime_assertsr  r  s   `     rM   
get_axiomszShapeEnv.get_axioms  s     ?, ,)-)F)M)M)O)O, , ,OOR R R R(/R R RO /.$+...99 	[ZZZZSYZZZFT]]6**//11222rN   ri  )rm  zsympy.logic.boolalg.BooleanAtomc                 &   i fd} ||           t          |t          j                  rQ |t          j        |j        |j                              |t          j        |j        |j                             nt          |t          j                  r |t          j        |j        |j                              |t          j        |j        |j                             |j        j	        r7|j        j	        r+ |t          j        |j        |j        dz
                       nEt          |t          j                  r+ |t          j        |j        |j        dz                        t                                                    S )zI Given a expression, it returns a list of predicates that follow from it c                    t          |           } t          | t          j        t          j        f          rt          | t          j                  rt          j        nt          j        }t          j         t          |           | j        | j                  <   t          j         t          |           | j        | j                  <   t          j	         || j        | j                  <   t          j	         || j        | j                  <   d S t          j        | <   t          j	        t          t          j
        |                     <   d S rG   )rc   r   rR   r  r-  r
  r   r	  r  r  r,  )r   rC  equivs     rM   add_exprz+ShapeEnv.get_implications.<locals>.add_expr)  s    )$//D$58 455 M (2$'A'AO588ux8=
jd4jj484458=
jd4jj484456;khhtx2236;khhtx22333 $jdAF,UYt__==>>>rN   r   )r   rR   r  r>  r	  r  r=  r<  r-  
is_integerr?  r   )rJ   ri  r]  r\  s      @rM   get_implicationszShapeEnv.get_implications#  sn    	M 	M 	M 	M 	M" 	a"" 		1HUXaeQU++,,,HUXaeQU++,,,,58$$ 	1HUXaeQU++,,,HUXaeQU++,,,u 5AE$4 5!%3344458$$ 	1HUXaeQUQY//000U[[]]###rN   )unbacked_onlyrO  rK  r  r  r`  r  r  zOptional[sympy.Expr]c                   |r|rJ || j         }nt          |          }|                     |          }|r|                    | j                  }t          |          }t          |j                  }||                     ||          }i }	|D ]V}
|
j        	                    |j                  r5|	
                    t          |                     |
                               W|                    |	          }t          |j                  }i }i }t          |          D ]\  }}t          | j                            |d          t                    r4||         }|rJ|| j        v rAt#          d|j                  }t'          d|j                  }||k    rt+          ||          }n|j        }|t,           u s|r	|| j        v s|j        s|||<   t1          j        d| dd          }t5          |dz
            }||z   ||<   t7          j        ||           ||<   	 |                    |          }nB# t:          $ r5 t<                              d	||           | j         d
xx         dz  cc<   Y dS w xY wt          tC          |                    }|j"        r|S 	 tG          ||          }|$                                r|j        S |r|ndS )a-  
        Tries to evaluate expr without introducing guards

        If unbacked_only == True, then we only do substitutions on
        unbacked SymInts (leaving regular hinted integers alone).  This could
        result in an expression that still contains backed SymInts, which you
        could then potentially guard on.

        Use compute_hint == True if you are trying to compute a non-binding
        hint for the particular hint values of backed SymInts, e.g., if
        s0 happens to be 3 this run, compute_hint will subsitute s0 with 3.
        NrO  r   l          evaluate_static_shape_Tr  r   z(RecursionError in sympy.xreplace(%s, %s)sympy_recursion_error)%r  r  r  r  r  rc   r?  rf   rY  r  r  r_  rG  r   r   r6   r  r  r-  r  r.  r3   r0   r  rR   r
  r   r2   r  rz  r}   r{  r  r|  rs  r1   is_singleton)rJ   r   r`  rO  rK  r  r  
var_rangesr  substri  new_shape_envnew_range_envr  r   r)  r-  r.  r]  offsetre  r  s                         rM   r  zShapeEnv._maybe_evaluate_staticH  s7   (  -v---*JJl++J}}T"" 	2==11D%d++ )**>__W<_HHF 	= 	=A~&&t'899 =T$"7"7":":;;<<<}}U##)** (( 9	H 9	HFC$/--a66EE  AB !!t~"5"5Arx(( GRX.. E>>$UE22B
 &   !#$#7#7I $8 $&a  ;c;;dTXYYYA ^^F 6zM!6:2wGGM!	}}]33HH 	 	 	KKBD-XXXL0111Q611144	 *+h*?*?@@ 	O
		 (M22 	9(2xxd2s   $H: :;I98I9c                 n      fd|j         D             }t          |                    |                    S )zIApply symbol replacements to any symbols in the given expression
        c           	      l    i | ]0}|                     t          t          j        |                    1S rZ   )r?  r	   rR   r
  r   r]  rJ   s     rM   r   z$ShapeEnv.replace.<locals>.<dictcomp>  s3    XXX4::d5<&;&;<<XXXrN   )rf   r|  r  )rJ   r   r  s   `  rM   r  zShapeEnv.replace  s<     YXXXdFWXXX4==66777rN   c                     t                      }| j        D ]3}|                     |          }|j        s|                    |           4|| _        |                                  d S rG   )rk  r  r  rs  r  r=  )rJ   new_divisibler   ress       rM   _update_divisiblezShapeEnv._update_divisible  si     	% 	%A,,q//C= %!!!$$$&$$&&&&&rN   c                 |   |                      |          }|                    t                    r|                                  i }|                    t                    D ]}|j        \  }}t          |t                    ri|j        \  }}|                      t          ||                    | j        v r5||k    r/|                      t          ||                    | j        v r|||<   |	                    |          }t          |          }|                    t                    ri }|                    t          j                  }|                    t          j                                      |                    t          j                            }	|                    t                    D ]I}
|
j        \  }}|                      t          ||                    | j        v rt!          ||          ||
<   J|	                    |          }t          |          }|                    t          j                  }|                    t          j                                      |                    t          j                            }|                    |          r|                    |	          r|}|S )zJUse known constraints and replacements to simplify the given expr
        )r  rY  r(   rq  rW  rK   r   r)   r  r  r|  rR   PowRationalr  r   r,   r  )rJ   r   div_replacementsatomr  r  base1divisor1pows	rationalsfdre  new_powsnew_rationalss                 rM   r  zShapeEnv.simplify  sV    ||D!! 88H 	%""$$$!

8,, : : $	ggx00 :&-lOE8||Cg$6$6774>II EMMdll3uh;O;O.P.PTXTb.b.b19(.==!122Dt$$D88H 	 !::ei((D

5>22==djj>W>WXXIjj** C C "g<<D' 2 233t~EE+3D'+B+B$R(}}%566H"8,,H~~ei00H$NN5>::EEhnnUZUbFcFcddM  &&  =+A+A)+L+L  rN   rv  
allow_nonec                   t                                        | j                  }|j        sddlm} t          ||          rdS |                     |d          }||S |rdS | j        r|                    | j                  j	        sbt                              d           t          dfd	           |                     t          j        |          d
| d            S |                     |          |S )a  
        Gets a size hint for a given expression from the underlying shapes we had.
        Does not introduce a guard, so only use this when you can guarantee that
        your code is still valid for arbitrary shapes (such as optimization decisions)
        r   r5   NTrb  z*propagate_real_tensors size_hint(%s) -> %spropagate_real_tensorsc                      t                     t                    t          j        t          j        d                                                    dS Nr   r  )r   r  r  r  r   from_tracebackr8   r  summary)r   unsound_exprs   rM   r   z$ShapeEnv.size_hint.<locals>.<lambda>,  sM    $(JJ&*<&8&8%/%>?P?X^_?`?`?`?h?h?j?j%k%k- - rN   metadata_fnpropagate_real_tensors: r  )r|  r  r  rs   torch.utils._sympy.singleton_intr6   r   r  r  rf   r}   r{  r   defer_runtime_assertrR   r  _make_data_dependent_error)rJ   r   r  result_exprr6   r  r  s    `    @rM   	size_hintzShapeEnv.size_hint  s^    "$''00AA$ 	EEEEEEE+|44 t++Kd+KKA} t' (*33D4LMM#0 (KK LdT`aaa$0% % % % %    --l;;R;RRLRR   ('11+tDDDrN   c                     t          |                              | j                  }|j        p|                     |          d uS rG   )r|  r  r  rs  r  )rJ   r   r  s      rM   r   zShapeEnv.has_hint<  sA    !$''00AA$\(C(CK(P(PX\(\\rN   size_oblivious_resultr  c                   g }|j         D ]n}d                    | j        |                                                   }| j                            d||           || j        v r|                    |           od}|d| d}|                     d          \  }}	}
|j	        rd}nd}| d| d	| d
d                    t          t          |                    pd d| dd                    t          j                            |g                                                     dd                    t          t          |j                              d|
z   }t          ||          S )Nr   z-Data dependent variable '%s' allocated at:
%szNATTENTION: guard_size_oblivious would fix the error, evaluating expression to zb.
Maybe you need to add guard_size_oblivious to framework code, see doc below for more guidance.

TzDCould not extract specialized integer from data-dependent expressionz,Could not guard on data-dependent expressionr`  z (unhinted: z).  (Size-like symbols: r}  nonez)

zAPotential framework code culprit (scroll up for full backtrace):
z
For more information, run with TORCH_LOGS="dynamic"
For extended logs when we create symbols, also add TORCHDYNAMO_EXTENDED_DEBUG_CREATE_SYMBOL="r  z"
If you suspect the guard was triggered from C++, add TORCHDYNAMO_EXTENDED_DEBUG_CPP=1
For more debugging help, see https://docs.google.com/document/d/1HSuTTVvYH1pTew89Rtpeu84Ht3nQEFTYhAX3Ypa_xJs/edit?usp=sharing
)rf   r/  r  r  r}   r~   r  r   r  r^  r:  r  	tracebackStackSummary	from_listrD   )rJ   r   unhinted_exprr  size_like_symbolsr]  
stacktracesize_oblivious_result_msgr  r  r  descr  s                rM   r  z#ShapeEnv._make_data_dependent_errorA  s    " 	, 	,A!21!5!<!<!>!>??JHNNKQPZ[[[DN""!((+++$&! ,uav u u u & 7;6M6Md6S6S3."3? 	BYDDAD 
q 
qd 
q 
q 
q 
q#'99S6G-H-H#I#I#SV
q 
q(
q 
q wwy-77
CCJJLLMM	
q 
q ;>((3sDL]C^C^:_:_
q 
q 
q  	  +4555rN   c                    |j         |j        }}|dk     r|| j        v rd}|| j        vr7t	          ||          }| j                            d||           || j        |<   nL| j        |         }|t	          ||          z  }||k    r&|| j        |<   | j                            d||           | j                            |          x} | j        |         }||v sJ | d|             d S d S )Nr   z"_update_var_to_range %s = %s (new)z%_update_var_to_range %s = %s (update)z not in )	r-  r.  r  r  r3   r}   r~   r  r   )	rJ   r  r)  r-  r.  r  r   r   r   s	            rM   _update_var_to_rangezShapeEnv._update_var_to_rangeg  s   xu 1994>11E ***E5))AHNN?KKK()Df%%#F+CE5111Cczz,/!&)FPSTTT$$V,,,A9!&)A666a,,,,666 :96rN   tgtr  c                       j                             d          k    rdS t          t          j                  sJ  j        rt                    sdS d} j        v r{ j                 }                               } 	                    |           |
                    |          st          j                  dk    rt          t          j                            }t          t          j                  |d          }| j                            d||                                |d                   }t'          t)          |j                  t-          |j                            }	 	                    ||	                                          }|
                    |          sJ  j        v r5t          t          j                  r j                                       n=t          t          j                  r# j        v r j                                       |
                    |          s! j                            d||           dS  j        v rd                     d          }
                     d          }|

                    |          s! j                            d	|
|           dS t          t          j        t          j        f          rt9          j                    t=          d
 fd           t>          j         r[ j        !                    d j"                 d         #                                            j                            dd           t"          $                    d|            j         <    %                                  &                    t          j                             dS )zw
        Adds or updates a replacement for a symbol.
        Use this instead of `self.replacements[a] = tgt`.
        Nr   Ffloordiv_inequalityz2set_replacement: solve for %s in %s == %s gives %sz:skipped set_replacement %s = %s (%s) [%s not subset of %s]TrK  zVskipped set_replacement %s = %s (%s) [%s not subset of %s (size-oblivious conditions)]symbolic_shape_specializationc            	      *   t                     d j                             g           D             t                    t          j        t          j        d                                                    rt          j                  nd dS )Nc                 6    g | ]}|                                 S rZ   r  rz  s     rM   rM  z?ShapeEnv._set_replacement.<locals>.<lambda>.<locals>.<listcomp>  s     QQQQQQQrN   r   r  )r  r  r  reasonr  
user_stack)r  r  r   r   r  r8   r  r  )r   r  rJ   r  user_tbs   rM   r   z+ShapeEnv._set_replacement.<locals>.<lambda>  s    "1ggQQ$2E2I2I!R2P2PQQQ!#YY!'67H7PVW7X7X7X7`7`7b7bccHO"Y*";G"D"D"DUY% % rN   r  zSpecializing %s to %sr   SPECIALIZATIONr  zset_replacement %s = %s (%s) %s)'r  r   r   rR   r
  r  rW  r  r1   r  r  r  rf   rl  r  r/   r  r}   r~   r3   r.   r-  r-   r.  r  r  r   r  r%   extract_stackr   r  print_specializationsr{  r  r  r  r=  r  )rJ   r   r  r  	tgt_bound	src_boundr  r  rat_b_boundb_boundtgt_bound_sosrc_bound_sor  s   ````        @rM   r  zShapeEnv._set_replacement  s&    $#''40000F !U\*****7 	@YZ]@^@^ 	F 	!!!)!,I ((--I%%a333 %%i00 9S9I5J5Ja5O5Oc.//00ehq#..uMMM=HNN#WYZ\]_bdefff #'"2"21Q4"8"8K))K4E*F*F
S^SdHeHeffG--a999 $ 0 0 5 5I$--i88888( DN""z#u|'D'D"""3''''C.. &3$.3H3H""1%%% %%i00 	[]^`cehjsu~dn$$#//D/II#//$/GG#,,\:: HNN $WXY[^`ceqsA A AFcEM5;788 	B %244G/       
 
 
 
 + B  !8$:Ma:PQR:S:X:X:Z:Z\_```/DAAA2AsCKKK"!$$&&& 	ehq#../////rN   c                 b    | j                             |           |                                  d S rG   )r  r  r=  r  s     rM   _add_divisiblezShapeEnv._add_divisible  s0    4   $$&&&&&rN   c                      | j         vr|S  j         |         } fd|j        D             } j         |                             |          \  }}|r                     ||d            j         |         S )z
        Implements a DSU-like algorithm to find the variable that represents a
        Also handles transitive non-identity replacements.

        a: b + c
        c: d
        c                 <    i | ]}|                     |          S rZ   )r?  rm  s     rM   r   z"ShapeEnv._find.<locals>.<dictcomp>  s%    BBBAq$**Q--BBBrN   find)r  rf   	_xreplacer  )rJ   r   rp  cur_replacereplacedchangeds   `     rM   r?  zShapeEnv._find  s     D%%%H"BBBB1ABBB -a0::;GG' 	7!!!Xv666 ##rN   	sympy.Relc           	      V	    t          |t          j                  sJ t          |t          j                  rdS t	          |j                  }t          |          dk    sJ d|             t          |          dk    rdS  fd}t          ||d          }|j        }|j	        } 
                    |           t          |t          j                  sdS |                    t                    s	 |                    t                                        |                    t                              }t          |          dk    r t#          d |D                       rt$          d	 } |||          r+                     |                     |          d
           n |||          r+                     |                     |          d           nt+          ||d         d          }|t-          d t          j        |d                   D                       rr                     |d                   }	t          t1          |	                    dk    }
|
r5                     t3          t          j        |d                   |	d           n# t$          $ r Y nw xY w|                    t                    rt7          t9          |                    t                                        }	 t+          ||d          }||d         dk    rz                     |           |j        \  }}t          |t          j                  r@t          |t          j                   r%t          |j                  dk    r|j        \  }}t          |t          j                  rt          |t          j                  rΉ !                    |          r|t          j"        ||          z  } #                                j$        j%        } &                    |tO          j(         j)        |         tU          j+        |                               | j,        v r j,        -                    |                                |||z  d           n# t$          $ r Y nw xY wdS )z
        The relational guard is guarded to be true.  Use this information to
        simplify shapes (i.e. a == b or a % 5 == 0)
        Nr   z3The expression should not be static by this point:    c                     | j         v o#t          d j         |          D                       }                    | d          pt          j        }| j        }|rdnd||fS )Nc              3   >   K   | ]}|                                 V  d S rG   )r  rz  s     rM   rj  zHShapeEnv._maybe_guard_rel.<locals>._smart_symbol_sort.<locals>.<genexpr>3  s,      0b0ba1A1A0b0b0b0b0b0brN   Tr~  r   r   )r  rt  r  r   r   r  )rQ  has_only_ephemeral_sourcesr  r  rJ   s       rM   _smart_symbol_sortz5ShapeEnv._maybe_guard_rel.<locals>._smart_symbol_sort1  ss    T((bS0b0b4K^_`Ka0b0b0b-b-b ' >>!>55DD6D3:AAD$GGrN   T)r  r  c              3   ,   K   | ]}|j         d k    V  dS r  r  r  s     rM   rj  z,ShapeEnv._maybe_guard_rel.<locals>.<genexpr>H  s(      3\3\qAIN3\3\3\3\3\3\rN   c                     t          | t          j                  r<t          |           rt          |          sdS t	          | t
          j                  rdS dS r  )r   rR   r
  r{  r=   r;   r  )r	  r  s     rM   trivial_solvez0ShapeEnv._maybe_guard_rel.<locals>.trivial_solveY  s\    !#u|44 (055 (>STW>X>X (#'4)#tz:: (#'4 !5rN   trivial_lhstrivial_rhsFr  c              3   $   K   | ]}|j         V  d S rG   )r^  )r   r8  s     rM   rj  z,ShapeEnv._maybe_guard_rel.<locals>.<genexpr>j  s$      ,b,baQ\,b,b,b,b,b,brN   r   rA  r   divisibility).r   rR   r)  r-  r   rf   r  r  r	  r  _refine_rangesr  rY  r)   rW  r(   rn  r  r<  r  r?  r/   rt  preorder_traversalr{  r	   r
  rl  r  r  rK   Numberr6  r  rV  r  r   r   r  r2   floordivr  r3   wrapr  r  )rJ   r   freer  r	  r  floor_div_atomsr  r  r  okmod_exprpqrY  i0di1s   `                 rM   _maybe_guard_relzShapeEnv._maybe_guard_rel  s    $	*****
 dEH%% 	FD%&&4yy1}}}ZTXZZ}}}t99q==F	H 	H 	H 	H 	H d 2DAAAhhD!!! $)) 	Fxx}} +	*"%))H"5"5";";CIIh<O<O"P"P''!++3\3\O3\3\3\0\0\+-- ! ! ! !=c** 
a))#tzz#NNNN"]3,, a))#tzz#NNNN!$QUKKKA},b,b5C[\]^_\`CaCa,b,b,b)b)b}"&**QqT"2"2 !6w!?!?@@AE a 11$u|T!W2M2MwX_```&   88C== !	DC1122HdH%HHH=QqTQYY''111 $=DAq!!U\22 Nz!UY7O7O NTWXYX^T_T_cdTdTd !2 'q%,77N&r5<88N !33B77N !"EIaOO 3A!%!<!<!>!>!C!HB !55b:Q:Z $ 1" 5{7G7J7J; ;     "T^33 $ 2 22 6 6 6 11"a"fnMMM&   s&   5F&J 
J)(J)<FR 
R&%R&c                 B    | j         rdnd}t          |t                    S )Nr   r   )r  r3   r0   )rJ   r-  s     rM   r  zShapeEnv._default_value_range  s#    -415&)))rN   c                 8    t          t           t                    S rG   )r3   r0   r  s    rM   r  z)ShapeEnv._default_unspecified_value_range  s    F7F+++rN   c                 &   t          |                    t                              }t          |          D ]F}|j        \  }}t          ||          }t          j        |d          }|                     |           G| 	                    |          S r4  )
r?  rW  r(   r   rK   r)   rR   r  r  r  )rJ   r   
floor_divsr{  r  r  r  eq_exprs           rM   _simplify_floor_divzShapeEnv._simplify_floor_div  s    4::h//00
 :&& 	( 	(BGMD'4))Hhx++Gw''''}}T"""rN   c                     | j         rW| j        dxx         dz  cc<   t          ddi | j        | d| dd           t                              d||d	
           d S d S )Nignored_backward_guardr   r  evaluate_expr_frozenr  r   )ignored_guardversionz>Ignored guard %s == %s, this could result in accuracy problemsTr  )r  r  r9   r  r}   r{  )rJ   r   r  s      rM   _check_frozenzShapeEnv._check_frozen  s    ; 	L1222a7222&n(,%@%@,%@%@  !  
 
 
 KKXZ^`ly}K~~~~~	 	rN   r  c                 `   d }t          j                    }	 |S|j        j        t	                      vr0t          j        |j        j        |j        |j        j                  }n	|j	        }|S~n# ~w xY wd}t          j                    }|rdt          |d                   z   }d}|r,|r*dd                    t          j        |                    z   }|rOt          j        rCt#          j        d          }|dd                    |                                          z   z  }n|r|dz  }|||fS )	Nr   z at r   zM
User Stack (most recent call last):
  (snipped, see stack below for prefix)
T)cppz
C++ stack trace:
z?
For C++ stack trace, run with TORCHDYNAMO_EXTENDED_DEBUG_CPP=1)r   r  r  r  r   r  FrameSummaryr  co_namer  r%   r  r7   r/  format_listr  extended_debug_cppr8   r  r  )rJ   r  r  r  r  r  r  	cpp_stacks           rM   r  zShapeEnv._get_stack_summary  sq   $&&	#<+3F3H3HHH(50,   H
  # IIII  .00 	@#l72;&?&??N 	 	<	-g66778 
  	1 	)1d;;;I!7"'')BRBRBTBT:U:U!UU 	3
 ):::s   AA. .A1forcing_specc           
      ^   | j                             t          j                  rt	          |          }t
          j        d uo|t
          j        k    }|                     |          \  }}}d}	|sd| d}	| j                             d|s|n| d||t          |          |	||           d S d S )Nr   zA, for more info run with TORCHDYNAMO_EXTENDED_DEBUG_GUARD_ADDED="r  z%s %s [guard added]%s (%s)%s%sz (forcing_spec)r  )
r}   r   r   INFOr  r  extended_debug_guard_addedr  r  r7   )
rJ   r  r  r  str_gr  r  r  r  r  s
             rM   
_log_guardzShapeEnv._log_guard  s    8  .. 	FFE8DsRXRsIsH:>:Q:QRZ:[:[7Hn&7 O H?DH H H   HMM0*J60J0J0JX&&!#  	 	 	 	 		 	rN   )save_tracked_fakesr  r  c                    	 |                      |||||          S # t          $ r  | j                            d||||            w xY w)Nr  zKfailed during evaluate_expr(%s, hint=%s, size_oblivious=%s, forcing_spec=%s)_evaluate_exprr  r}   r{  )rJ   r  r  r  rK  r  s         rM   r  zShapeEnv.evaluate_expr  sm    	&&y$^j&kkk 	 	 	H]4   	s	    *Ac                
    t          j        d           fd            }d}d} j        r|                                 s|s |            }	|	t          j        u r%                     t          j        |f          \  }}n|	t          j	        u rI                     t          j        |f          \  }
}                     t          j        |
f          \  }}nI                     t          j        ||	f          \  }}                     t          j        |f          \  }}|J |r                     |           d}d}	 j        r4 j                            d           k    sJ  d             S }                     ||          }|5 j                            d|           |k    sJ | d             |S d}d}	|j         j                                        k    s                     |d	          }|j         j                                        k    sd}|s                     |d          } j        rW                     j                  xj        s6t                              d
           t1          dfd           d}}	n2                     |                     j                  ||          |}|	
 |            }	                     ||	           t6          j        rOt;          t<                    r:t;          |t          j        t          j         f          rt	          j!        |          }|	t          j        u r|}n8|	t          j	        u rt	          j!        |          }nt	          j        ||	          }|r "                    |d d            |	S                                  st;          |t          j#                  r $                    |            j%        s@tM          j'        d          }tQ          ||          } j)        *                    |           n "                    |d                                             s| +                    d||           |j        D ]}} j,        |xx         dz  cc<   |sdt6          j-        X j,        |         t6          j-        k    r= j        .                    dt6          j-        |            /                    |d           ~n? +                    d||           n&# t`          $ r |r 1                    |            w xY w|	S )zO
        Given an expression, evaluates it, adding guards if necessary
        Nc                  Z                                    S t          j                   S rG   )r  rR   r  )r  r  rJ   s   rM   compute_concrete_valz5ShapeEnv._evaluate_expr.<locals>.compute_concrete_val  s*    |~~i000}T***rN   Fzeval %s [trivial]r   r  z eval %s == %s [statically known]Tr`  z.propagate_real_tensors evaluate_expr(%s) -> %sr  c                      t                     t                    t          j        t          j        d                                                    dS r  r  )r  unsound_results   rM   r   z)ShapeEnv._evaluate_expr.<locals>.<lambda>m  sM    (,Y*.~*>*>)3)BCTC\bcCdCdCdClClCnCn)o)o1 1 rN   r  r  r  r  r   r  zevaluate_expr: r/  r  z6symbol_guard_limit_before_specialize=%s exceeded on %szeval [guard suppressed])2r   r   r  r3  rR   r
  r&  r   _assertr  r  not_r  r0  rs  r}   r~   r  rf   r  r@  r  r  r{  r   r  r  r  /inject_EVALUATE_EXPR_flip_equality_TESTING_ONLYr   r   r  r-  r,  r  r)  r  r  r8   r  r#   r  r   r  r  $symbol_guard_limit_before_specializer  r  r  r,  )rJ   r  r  r  rK  r  r  r   r%  r  rE  r  eqlr  rF  r   static_exprtransmute_into_runtime_assertre  r  r  r  r]  r  s   ```                    @rM   r  zShapeEnv._evaluate_expr
  sl    
	T	"	"	+ 	+ 	+ 	+ 	+ 	+ 
#	"	+ 4	1'1133 (& ( 0/11Luz))";;EMG:VVee,,66x}wjQQQ";;EMC6RRee66x{WlD[\\Q";;EMC6RRe###
  1**4000 E	Y" !2I>>>#$,,,.F.F.F.F,,,  D55dES 6 U UK&A9kZZZ#&$...;0J0JD0J0J..."",1)L%)=)=)?)???  66t46PP -1E1E1G1GGG,0)) 040K0K +/ 1L 1 1- 0/8/A/A$BZ/[/[[^i $TV_aoppp(4) ) ) ) )    9=5'5"== MM$/:: 2G >    $D#3355t\222 J'"4..' #4%(EH)=>>'
 y uz)),,IdOOHT<00, $))NyNNnNN   $#,,.. Pa++ - ))!,,,C P .51===E&q%00EK&&u----
 --a1N91N1NOOO ,,.. Y$OOFALOIII^ E E1!4449444 !-E"GS 5a86;fff !HMM X & K !  
 !..qt.DDD 91<XXXX3  	 	 	 +$$T***	6 s!   :T3 AT3 G/T3 B#T3 3#Uc                     | j         D ]}|j                                         | j                                        D ]}|                                 | j                                        D ] }|D ]}|j                                         !dS )z
        Break reference cycles.

        This destroys the stacks. If you really want to keep them, we
        just need some way to break references on code objects.
        N)r  r  cleanupr  r  r  )rJ   r  r]  r  r  s        rM   r  zShapeEnv.cleanup  s      	 	AGOO"))++ 	 	AIIKKKK07799 	# 	#C # #  """"#	# 	#rN   c                    |}|                      |          }|| j                            d||           |S |                      |d          }| j        s9|j        | j                                        k    r|                     ||          S | j        rU|S| 	                                s?| 
                    t          j        |f          \  }}|J |r|                     |           | 	                                s/| j        r
J |            |                     |t           j                   t%          |t           j                  r|                     |           |}t+          |          }t-          j        d          }	t1          |||	          }
t3          d |j        D             d	 
          }|r|d         nd}| j                            |g                               |
           | xj        dz  c_        |                                  |                     d|d           n|                     d|d           dS )aQ  Create an assert that is checked at runtime

        Args:
            orig_expr (sympy.Expr): Boolean expression to assert is true
            msg (str): Message to display on assertion failure
            fx_node (Optional, torch.fx.Node): node in ``self.graph`` corresponding
                to the expression, if applicable

        Nz*runtime_assert %s == %s [statically known]Tr  r~  r   r  c              3   N   K   | ] }t          |t          j                  |V  !d S rG   r  rz  s     rM   rj  z0ShapeEnv.defer_runtime_assert.<locals>.<genexpr>  s4      aa!N1dN_<`<`aAaaaaaarN   c                 :    t          | j        dd                    S r  )r   r  r\  s    rM   r   z/ShapeEnv.defer_runtime_assert.<locals>.<lambda>  s'    qtuvu{|}|~|~u  rA  rA rN   r  r   runtime_assertFr  z!runtime_assert [guard suppressed]) r  r}   r~   r  rf   r  r@  r  r  r3  r&  r   r  r0  r  r  rR   r
  r   r)  r  rc   r8   r  r  r  r  
setdefaultr   r  r=  r  )rJ   r  r  r  r   r  re  r   r%  r  r  candsixs                rM   r  zShapeEnv.defer_runtime_assert  s     11$77"HNNGT_``` ..t4.HH? 	AHDY]a]l]q]q]s]sDsDs%%h%@@@ 0	1#--// $ 77zRRKD%### 1**4000((** 	` 288D888tUZ000 $	** ,%%d+++ I)$//D%-1555EtS%00Baat'8aaa  hA  hA  B  B  BE $-rB)44R<<CCBGGG--2--((***OO,ieOLLLLOO?Y^O___trN   c                 t   |                      |          }|j        D ]}t          |t          j                  sJ t          | j                            |d           t                    rNt          ||          }||j	        r|d         j	        su|\  }}| j
        |         }|j        |j        }}t          || j
                  }	||	j        k     r`t          |t          j        t          j        t          j        f          r/|	j        t#          t          |t          j                            z   }||	j        k    r`t          |t          j        t          j        t          j        f          r/|	j        t#          t          |t          j                            z
  }|t)          ||          k    r|                     |t)          ||                     | j
        |                                         r'|                     || j
        |         j        d           | j                                         d S )Nr   range_refined_to_singleton)r  rf   r   rR   r
  r  r   r6   r/   r^  r  r-  r.  r1   r  r=  r;  r   r>  r<  r3   r  re  r  r  r   )
rJ   r   r  r  r_exprr  r)  r-  r.  rhs_vrs
             rM   r  zShapeEnv._refine_ranges,  s   }}T""' 1	6 1	6Ffel33333$/--fd;;\JJ  $''Ay!2yqty KFC"6*B8RX5E d&788F v|##
6EHehPUPX;Y(Z(Z# s:feh+G+G'H'HHv|##
6EHehPUPX;Y(Z(Z#s:feh+G+G'H'HH [.... %%fk%.G.GHHH (5577 m%%fd.?.G.MOklll '335555c1	6 1	6rN   )r   r]  r  r  c                 "   t          ||          }| j                            |t          j                              }|                     ||           | j        |         x}|k    r)t
                              d||j        |j                   d S d S )Nz"constrain_symbol_range %s [%s, %s])	r3   r  r   r  r  r}   r  r-  r.  )rJ   r]  r  r  upd_vrold_vrnew_vrs          rM   r  zShapeEnv.constrain_symbol_rangeb  s     \<88"&&q+*=*?*?@@!!!V,,,'**Fv55HH91flFLYYYYY 65rN   )r  r^   r   NNNr   N)rs  rm  r  ru  )r   rm  r   rm  )r   rm  )r   rN  r   rm  )r   r  r   N)NNFrG   )r  rm  )rO   rP   rQ   r   r   r   r   rI   r  propertyr  r  r  r  r  r  r  r  r  r   r  r   r  r   rR   r
  rS   r  r  r  r  r  r  r  r  r	  r  r  r  r$   r  r   r  r  r  r  r
   r   r   r   r"  r&  r)  r,  r0  r3  r6  r8  r2  r  r=  r   rn   rA  r@  r   rY  rP  rV  r  r  r  r  r  r3   r  r  r  r  r  r  r   r   r"   r   r!   rj  r  rH  r  r  r  r#   ri  r5  r  r,  r1  r5  r  r;  r  rA  rJ  r1   r  rY  r   r_  r  r  rq  r  r  r   r  r  r  r  r?  r  r  r  r  r  r  r  r  r  r  r  r  r  rZ   rN   rM   r^   r^   	  s{        04-12] 2] 2]&tn2]  S	*2]
 
2] 2] 2] 2]v "'+
 "' ! &* 5: 16IT= T= T=, TN-T= T= T= T=l 2 2 X2 < < X< 6 6 X6 1 1 X1 ( ( X( I I XI E E XE7V 7V 7V 7VrK$s))< K K K K$$3 $ $ $ $ & & ^& C%, Cuz C C C C 7 7# 7$ 7 7 7 7 
J%, 
Ju| 
J 
J 
J 
J  5< hsm YabeYf    & %* 3 S    " 9 9 9BD D D 1 1 1 2 2 2 	7 	7 ^	7    	+ 	+ 	+. .8EL;Q . . . .1 1T 1d 1 1 1 11 1 1 1/ / / /& & & & == = 
x&,	-	= = = =:,L, , 
%(-	 	, , , ,4(HUX]$; ( ( ( ( (
=%(- =D = = = =6 6 6 # # # $ $ $ ) ) ^)  	' 	' 	'`$,SM`#)` .=` !%UZ 0	` ` ` `7<Sz28 =L 04EJ/?	   4 7;
 
 
L
 

 #?3
 
 
 
x      7;IG IG IG#IG C=IG 	IG
 !IG IG #?3IG IG IG IGV  (,* * ** 3-	*
 V$* * * *X  (,# # ## 3-	#
 V$# # # #J 

 

 

7 7 7 7
# 
; 
 
 
 
 M M M$ I I I$9 9$ 9 9 9 9
 X X X$ 
 #-/(,# #3x/0# #  	#
 &# 
# # # #4 
 #-/(,#'+0l ll l  	l
 &l 4.l %)l 
l l l l\35< 3c 3 3 3 3F F F     , &%	e	 $(=A ;?e	 e	 e	 Z e	 !!9:e	 $$67e	 
ce	 e	 e	 e	V .2   j)*	   0. . .K K K MQ     
 
 
/ / /bB B B^ ^ ^ ^/ /
 /D /[ / / / /  3 3(5+@"A 3X\ 3ino{i| 3 3 3 Z3( Yt__"$("$-259h3i-j"$ "$ "$ _"$H ;@W\$DJNM3 M3 M3 M348M3PTM3M3.6uUZ7H.IM3 uU5<+D%EFGM3 
 	M3 M3 M3 ZM3^ 8 8 8 Z8 ' ' Z' % % % Z%N Ys^^:? & & & & & ^&R Ys^^] ] ] ^] jn $6 $6 $6X`aeXf $6 $6 $6 $6L- - -:{0. {0| {0# {0RV {0 {0 {0 {0z' ' ' ' $ $ $  Z$" Ys^^z z z ^zz*k * * * *,+ , , , , # # Z#  "'; ';4 '; '; '; ';R t    , Ys^^d333HL-2	LQ	 	 	| 	&*	EI	 	 	 43 ^	 JN.3BMRB B B B'+BFJB B B BH# # # d333B B B B 43B\465: 46$ 46 46 46 46l YtZ ZC ZWZ Z Z Z  Z Z ZrN   r^   c                 N    t          | t                    o| j        j        j        S rG   )r   r"   r   r   rs  r   s    rM   _is_intr  l  s    dF##@	(@@rN   c                 4    t          | d          o|| j        v S )N_dynamo_dynamic_indices)ry  r  )r8  r  s     rM   rU  rU  p  s     1/00SQ!:S5SSrN   c                   8     e Zd Zdej        j        f fdZ xZS )PropagateUnbackedSymIntsr   c                     ddl m} t                                          |          }t	           |            j        ||           |S )z]
        Run an FX node, propagating unbacked Symbol bindings to the new fake tensor
        r   )detect_fake_mode)torch._guardsr  rH   run_noderw   r   )rJ   r   r  r  rL   s       rM   r   z!PropagateUnbackedSymInts.run_nodet  sR     	322222!!!$$((**4a@@@rN   )rO   rP   rQ   r   r   r"  r   rU   rV   s   @rM   r  r  s  sH        %(-          rN   r  c                     t          j                    } | o| j        j                            t
          j                            t          j        t                              t
          j        j
        z             sn	| j        } | o| S rG   )r   r  r  r  
startswithosr  dirnamer   r   sepr  )r  s    rM   _find_user_code_framer&    sr     ""E

|'22GOOGOE2233bgkA
 
 	  
 LrN   c                 $   t          j        |j        j        |j        |j        j                  }| j        d         }|dd                    t           j        	                    |g          
                                          z   z  }|f| _        d S )Nr   z(

The following call raised this error:
r   )r  r  r  r  r  r  rK   r/  r  r  r  )ri  r  frame_summaryr  s       rM   _blame_user_coder)    s    *  M
 &)C5
	&00-AAHHJJKK	LC VAFFFrN   c                   .     e Zd ZdZ fdZd Zd Z xZS )_PythonPrinterz
    Util printer that replaces sympy symbols with their source-level names
    and renders sympy relational operators (e.g., Eq, Ne, Ge, Le) inline
    (i.e., as ==, !=, >, <).
    c                 V    t                                                       || _        d S rG   )rH   rI   src_map)rJ   r-  rL   s     rM   rI   z_PythonPrinter.__init__  s$    rN   c                 2    | j         |j                 d         S r4  )r-  r  r  s     rM   r  z_PythonPrinter._print_Symbol  s    |CH%a((rN   c                    |                      |j        t          j        j                            |                    }|j        }|                      |j        t          j        j                            |                    }| d| d| S r  )r  r	  rR   printingrA   r  r  )rJ   r   r	  r  r  s        rM   r  z _PythonPrinter._print_Relational  sy    %.*C*N*Nt*T*TUU%.*C*N*Nt*T*TUU&&&&&&&rN   )rO   rP   rQ   r  rI   r  r  rU   rV   s   @rM   r+  r+    s`             ) ) )' ' ' ' ' ' 'rN   r+  c                 R   | j         }d                    fd|j        D                       }|rt                              d|           d S t                    }| j        d         }|dz  }d|                    |           dd|                    t          j	        |                     dg}t          |          D ]\  }}|d|d	z    d
| z  }d                    fdt          d |j        D                       D                       }	|d|	 d| dz  }|f| _        d S )Nr}  c              3   8   K   | ]}|j         v|j         V  d S rG   r  r   r]  r-  s     rM   rj  z(_suggest_torch_checks.<locals>.<genexpr>  s1      PP!&:O:OQV:O:O:O:OPPrN   z.Unable to find user code corresponding to {%s}r   zG
To fix the error, insert one of the following checks before this call:ztorch._check(r  r  r   r  c              3   Z   K   | ]%}d | dd                     |                    V  &dS )`z` with r  N)r/  r3  s     rM   rj  z(_suggest_torch_checks.<locals>.<genexpr>  sW         	0A//fkk'!*--//     rN   c              3   $   K   | ]}|j         V  d S rG   r  rz  s     rM   rj  z(_suggest_torch_checks.<locals>.<genexpr>  s$      ::1::::::rN   z3

(These suggested fixes were derived by replacing z in z and its negation.))rE   r/  rf   r}   r{  r+  rK   r;  rR   r,  rG  r  )
ri  r-  rE   diffprinterr  suggested_fixesr   fix
src_mappeds
    `        rM   _suggest_torch_checksr<    s|   6D99PPPPT%6PPPPPD DdKKKW%%G
&)CUUC 	1--000;	$88;;;O O,, # #3"ac""S"""    ::(9:::::    J l:llSWllllCVAFFFrN   c           	         t                      }|[t          | |           t          t                    }|j                                        D ]
\  }}t          j        |          D ]\  }}|t          j        |          z   }t          |t          j                  r3|t          |j        j                                               |           it          |t          j                  rlt#          |j                  D ]W\  }}	t          |	t          j                  r8|t          |	j        j                                               | d| d           Xt'          | |           dS dS )z
    Given a raised data-dependent error, add the following to the error message:
    1. the closest user code location that raised the error;
    2. suggested fixes for the error in terms of live variables at that location.
    Nz.shape[r  )r&  r)  r   r   f_localsr   r  tree_leaves_with_pathkeystrr   r   r"   r  r   r   r   r   rG  r   r<  )
ri  r  r-  varr^  r  leafr  r   r   s
             rM   2_suggest_fixes_for_data_dependent_error_non_strictrC    so    "##EE""" d##,,.. 		U 		UHC$:3?? U U
dV]4000dEL11 UC	//077====el33 U"+DJ"7"7 U U3%c5<88 U#C$6$67>>$?S?Sq?S?S?STTTU 	a)))))' rN   rG   r  ru  r  rI  )r  builtinsr  r   r   r  r   rN  r  r#  r  r   	threadingr  r   
contextlibr   dataclassesr   r   enumr   r   typingr   r	   r
   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   torch.fxtorch.fx.tracebackr   r  torch.fx.experimentalr   r  torch.fx.experimental.recordingr   r   r   r   r   torch.fx.experimental.sym_noder   r   torch._loggingr   r   r    r!   r"   r  r#   r$   r%   torch.utils._python_dispatchr&   torch.utils._sympy.functionsr'   r(   r)   r*   r+   r,   r-   r.   torch.utils._sympy.solver/   torch.utils._sympy.numbersr0   torch.utils._sympy.value_rangesr1   r2   r3   r4   r  r6   torch.utils._tracebackr7   r8   torch._utils_internalr9   r   r:   torch.utils._pytreerZ  _pytreer  torch.utils._sympy.symbolr;   r<   r=   r>   r:  r?   	InputListri  	getLoggerrO   r}   rR   sympy.printing.strr@   sympy.printing.precedencerA   rB   RuntimeErrorrD   rX   _opsr   r!  __all__rj   rk   r   r   r  r   r   r   r\   r   Intr]   rd   r   Scalarr   r_   r   rT   rs   rt   rx   r"  rw   ry   rc   r1  rS   rA  rh   ri   r   rb  rc  r
  rf   rl   r{  rg   r  rv   r  r  r  ru   r  r  rr   r  r  rm   rb   r  r  r  r  r  r   r  r`   ra   r  r  r  r  r  r%  r(  r2  rj  r5  rU  rW  r_  rn   ro   rp   rq   ra  IndicatorTypesr|  r~  r  r  r  r  rJ  r  negtltleger  r  modr  truedivpowr  r\  roundtruncre   r  r  r  r  r  r  r  localr  r  r^   r  rU  Interpreterr  r&  r)  r0  r+  r<  rC  rZ   rN   rM   <module>rq     s5                      				 				 



         # # # # # # % % % % % % ( ( ( ( ( ( ( (                                     ( ' ' ' ' '   ) ) ) ) ) ) ) ) ) 3 3 3 3 3 3              = < < < < < < < 7 7 7 7 7 7 7 7 , + + + + + + + + + < < < < < < < < < < F F F F F F                    / . . . . . - - - - - - n n n n n n n n n n n n 9 9 9 9 9 9 B B B B B B B B 0 0 0 0 0 0 6 6 6 6 6 6 $ $ $ $ $ $ $ $ $ G G G G G G G G G G % % % % % % :999999	
g!!  ) ) ) ) ) ) < < < < < < < <    ,   	 	 	 	 	L 	 	 	 z~
 
 
 & ! _ _ _
! ! !J 4.SX . . . .0	 	 	 	 	| 	 	 	, , , , , EL##Xc] #tCy # # # #	 	elC'( 	HSM 	S 	 	 	 	 
u|U^U]CL	M 4    
uS&[) d    * 8i 7 7 7uU]D%89 d    I I I I0  0:%(- 0: 0: 0: 0:l58= T    ,. ., . . . ..#| # # # # #L0%* 0 0 0 0 00dGm,     "B B BYfel23 Y8M Y Y Y Y0
IeFEJ<= 
I#elBS 
I 
I 
I 
I=% 45 =$ = = = =
g g gx'=       $3 3 3 3 3 3 3 3 $' ' ' ' ' ' ' ' $+ + + + + + + + $! ! ! ! ! ! ! !Y Y Y Yv  4  U4=1 d    2	 	 		 	 	O O O9 9 9D%, Dc DY\ D D D D'% '% '%RF Fhsm F# F F F F DH )= )= )=x} )=8C= )= )= )= )=V%u| % % % % % %b           L L LG G G *. m m m m mD D D       b $        $H H H H HZ H H H4 $; ; ; ; ;j ; ; ;2 ,.EtKL
$ZC ZC ZC ZC ZC ZC ZC ZCzb b b* * *     $	 	 	 	 	 	 	 	 $@ @ @ @ @ @ @ @` $P P P P P6 P P PB $% % % % %5 % % %"U3xwFG "D " " " "
 45
3  - - -
O O O  >.uz .ej . . . .Cem C C C C C	8<(+ 	(+ 	(+	
 	(+ 	(+ 	(+ 
3 
3 
8<  ! x HL ()J  TZ!" ty#$ *(H.N*"$~7  >1 1 1 1p $# # # # # # # #         Z      
#? #? #? #? #? #? #? #?LM M M M M0 M M M
@ @ @ @ @* @ @ @0Y
 Y
 Y
 Y
 Y
 Y
 Y
 Y
x io $2 2 2 2 2 2 2 2c1Z c1Z c1Z c1Z c1Z c1Z c1Z c1ZLcA A AT T T	 	 	 	 	ux3 	 	 	    ' ' ' ' 'U^'2 ' ' '*  4* * * * *rN   