a
    h}                 	   @  s  U d dl mZ d dlZd dlmZ d dlmZmZm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Zd dlZd dlZd dl
mZmZ d dlmZmZmZmZ d dlm Z m!Z! d dl"m#Z#m$Z$m%Z% d d	l&m'Z' d d
l(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 d dl3m4Z4m5Z5m6Z6m7Z7 d dl8Z8d dl9Z8d dl:m;  mZ< d dl=m>  m?Z@ d dl8mAZAmBZBmCZC d dlDmEZEmFZFmGZGmHZH d dlImJZJmKZKmLZLmMZM d dlNmOZO d dlPmQZQ d dlRmSZT d dlUmVZVmWZWmXZXmYZYmZZZ d dl[m\Z\m]Z] d dl^m_Z_ d dl`maZa d dlbmcZc d dldmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZp d dlqmrZr d dlsmtZtmuZu d dlvmwZw d dlxmyZy d dlzm{Z{m|Z|m}Z} d dl~mZmZmZmZ d dlmZmZ e0r0d dlZd dl8mZ d d lmZ d d!lmZ d d"l^mZmZmZ eZeZeeZG d#d$ d$eZG d%d& d&eZe8jjjZg d'Zd(Zd)Zd*d+d,d-d.Zd/Zd0ed1< e1d2Ze1d3ejeejZG d4d5 d5Zd6d7d8d9d:Zd;d<d=d>d?Zedd@dAdBdCZG dDdE dEeZdFdGdHdIdJZe2e8jCef Zd0edK< dLdMdNdOdPZddQd;dRdSdTdUZe2e8jCe8jBe8jAeeef Zd0edV< dVdGdWdXdYZdZdGdWd[d\Zd]dGdWd^d_Zd`dGdWdadbZdcdGdWdddeZdfdGdgdhdiZd2d2d+djdkdlZdmdndndodpdqZe2e8jee8jdrf f Zd0eds< dmdtdsd+dudvdwZdtdGdxdydzZd2d2dgd{d|Zdd~ddGdddddZd1d1dgddZdddgddZdZddddZe2eCeBeAeeeeje8jf Zd0ed< e2eee f Zd0ed< dddddZdddddZddGdddZddGdddZdddddZdtddxddZdddddZe$d}dG dd dZe$d}dG dd dZe$d}dG dd dZe$d}dG dd dZe$d}dG dd dZАdddddmddGddddZѐddmdddGdndddZd`dGdGdddZd`dGdWddZd`dGdWddZdddddńZd`dGdddǄZd`dGdddɄZd`d`d`dʜdd̄Zd2d2d`d͜ddτZd`d`d`dʜddфZdddWddՄZdd+dWdd؄ZddZd+dٜddۄZސddd;d;d+dܜddބZddߜdd;d;d+dܜddZddd+dddZdd`dRdGdddZd`dGdWddZdZdRdWddZd]ddWddZdddddZdddddZd}ddddGdGdddZdddddd ZG dd de'Ze$d}dG dd dZe$d}dG dd deZe$d}dG dd deZe2eedf Ze$d}dG d	d
 d
eZdddddZddGdgddZddGdgddZe$d}dG dd dZe$d}dG dd deZe5dZe1dZe$d}dG dd de,eef eZe$d}dG dd deZe$d}dG dd deZe$G d d! d!Zd"d#dd$d%ZekfZdd&d'd(d)Zd3d3dgd*d+Zed,d3d3d-d.d/ZG d0d1 d1e-Z edd3d2dGdGd3d4d5d6Zd7dAd8d9Zd:d:dRd;d<d=Zd:d:dGd;d>d?Zd1ddd@dAZdBdCdDdEdFZeeee8dGZddHd;dIdJdKdLZe$d}dG dMdN dNZ	G dOdP dPeuZ
G dQdR dRejZG dSdT dTeeuZe4dUedVG dWdX dXeZG dYdZ dZeetZe$d}dG d[d\ d\Ze$d}dG d]d^ d^eZG d_d` d`eZG dadb dbeuZG dcdd ddZe Ze$d}dG dedf dfZe$G dgdh dhZe!didjdkdldmZe$G dndo doZG dpdi diZddGdgdqdrZdFdRdGdsdtduZG dvdw dwe8j;jZ dxdAdydzZ!d{d|d+d}d~dZ"G dd deuZ#dddddZ$d$dd+dddZ%d$d+dddZ&e!dtddxddZ'dddddZ(dS (      )annotationsN)S)BoolLike	FloatLikeIntLike)Counterdefaultdict)	GeneratorIteratorMappingSequence)_GeneratorContextManagercontextmanager)asdict	dataclassfield)Enum)
AnyCallablecastGeneric
NamedTupleNoReturnOptionalTYPE_CHECKINGTypeVarUnion)
deprecated	ParamSpec	TypeAlias	TypeGuard)SymBoolSymFloatSymInt)
ShapeGuardSLocSourceTracingContext)dtrace_structured
LazyString
structuredtrace_structured)is_sparse_any)signpost_event)_config)FakeTensorMetarecord_shapeenv_eventreplay_shape_env_eventsshape_env_check_state_equalShapeEnvEvent)SymNodeSymTypes)py_sym_types)
OrderedSet)is_traceable_wrapper_subclass)Application	CeilToIntCleanDivFloorDiv
FloorToInt
IntTrueDiv!IsNonOverlappingAndDenseIndicatorMaxMinMod	PythonMod
TruncToInt)int_oo)
CppPrinterPythonPrinterSingletonInt)	try_solve)make_symbolsymbol_is_typeSymT)bound_sympySymPyValueRangeAnalysisValueRangeErrorValueRanges)CapturedTracebackformat_frame)TensorTensorPropertySource
FakeTensor)BoolLikeTypeFloatLikeTypeIntLikeTypec                      s0   e Zd ZU ded< dddd fddZ  ZS )GuardOnDataDependentSymNodesympy.Basiccondr   None)r^   argsreturnc                   s   t  j|  || _d S N)super__init__r^   )selfr^   r`   	__class__ S/var/www/auris/lib/python3.9/site-packages/torch/fx/experimental/symbolic_shapes.pyrd   y   s    z$GuardOnDataDependentSymNode.__init__)__name__
__module____qualname____annotations__rd   __classcell__rh   rh   rf   ri   r\   v   s   
r\   c                   @  s   e Zd ZdS )PendingUnbackedSymbolNotFoundNrj   rk   rl   rh   rh   rh   ri   ro   ~   s   ro   )+guard_or_falseguard_or_truehas_symbolic_sizes_stridescreate_contiguousShapeEnvis_concrete_intis_concrete_floatis_concrete_boolhas_static_value	guard_intguard_floatguard_scalarcanonicalize_bool_exprhint_intSYMPY_INTERPfree_symbolsis_symbol_binding_fx_nodeis_nested_intSHAPEENV_EVENT_KEYCURRENT_NODE_KEYhas_free_symbolshas_free_unbacked_symbolssym_andsym_eqsym_orSymbolicContextStatelessSymbolicContextStatefulSymbolicContextSubclassSymbolicContextSymIntSymbolicContextTrackedFakestatically_known_truestatically_known_falseguard_size_obliviouscheck_consistentcompute_unbacked_bindingsConvertIntKeyrebind_unbackedresolve_unbacked_bindingsis_accessor_nodeValueRangesSLocSymIntEqByExprSpecializationZshapeenv_eventZcurrent_nodez$functools._lru_cache_wrapper[object]r_   )	wrapped_fra   c                 C  s   t d| j|   d S )Nzlru_cache_stats %s: %s)logdebugrj   cumulative_cache_info)r   rh   rh   ri   log_lru_cache_stats   s
    r   zsympy.logic.boolalg.Booleanr   SympyBoolean_T_SympyTc                   @  sd   e Zd ZU dZded< dddddZdd	d
dZdd	ddZdddddZdd	ddZ	dS )r   a  
    This is a wrapper around SymInt which has alternative semantics for
    equality.  Specifically, instead of erroring or guarding, we
    instead will hash/compare equality based on the underlying sympy
    expression; e.g., s0 and s1 will always compare as False.

    NB: This does NOT do fancy analysis that maybe_evaluate_static does;
    we can only reason through equalities that occur because to expressions
    canonicalize to the same expression via regular simplification.
    Union[torch.SymInt, int]valr_   r   ra   c                 C  s
   || _ d S rb   r   )re   r   rh   rh   ri   rd      s    zSymIntEqByExpr.__init__strra   c                 C  s
   t | jS rb   )reprr   re   rh   rh   ri   __repr__   s    zSymIntEqByExpr.__repr__
sympy.Exprc                 C  s(   t | jtjr| jjjS t| jS d S rb   )
isinstancer   torchr#   nodeexprsympyIntegerr   rh   rh   ri   _extract   s    
zSymIntEqByExpr._extractobjectboolotherra   c                 C  sF   t |tsJ t| jtu r6t|jtu r6| j|jkS |  | kS rb   )r   r   typer   intr   re   r   rh   rh   ri   __eq__   s    zSymIntEqByExpr.__eq__r   c                 C  s   t |  S rb   )hashr   r   rh   rh   ri   __hash__   s    zSymIntEqByExpr.__hash__N)
rj   rk   rl   __doc__rm   rd   r   r   r   r   rh   rh   rh   ri   r      s   
	r   ztuple[IntLikeType, int]ztuple[int, IntLikeType, int])tupra   c                 C  s0   t | d r$d| d j | d fS dg| R S Nr      )r   r   nested_int_coeff)r   rh   rh   ri   _nested_int_aware_sort   s    

r   Optional[int]z?Callable[[Callable[..., _T]], functools._lru_cache_wrapper[_T]])maxsizera   c                   s   ddd fdd}|S )NCallable[..., _T] functools._lru_cache_wrapper[_T])fra   c                   sr   t | j ddddfdd}dd fdd}|_|_ttjrnt	t
 S )	Nr   zfunctools._CacheInfor   c                    s(     } t | j | j | j| jS rb   )
cache_info	functools
_CacheInfohitsmissesr   currsizecur)	prev_hitsprev_missesr   rh   ri   r     s    z7lru_cache.<locals>.inner.<locals>.cumulative_cache_infor_   c                    s&     } | j7 | j7    d S rb   )r   r   r   r   Zold_cache_clearr   r   r   rh   ri   new_cache_clear  s    

z1lru_cache.<locals>.inner.<locals>.new_cache_clear)r   	lru_cachecache_clearr   r   isEnabledForloggingDEBUGatexitregisterr   )r   r   r   r   r   ri   inner  s    	zlru_cache.<locals>.innerrh   )r   r   rh   r   ri   r     s     r   zset[str]r   c                  C  s   dd l } dd l} dd l} dd l} 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j| jj| jj| jj| jjg}dd l} dd |D | jj  B dhB S )Nr   c                 S  s   h | ]}t |qS rh   )inspectgetfile).0mrh   rh   ri   	<setcomp>N      z&uninteresting_files.<locals>.<setcomp><string>)!Ztorch._compileZtorch._dynamo.eval_frameZtorch._inductor.sizevarsZtorch._library.custom_opsZtorch._library.fake_impltorch._loggingtorch._subclasses.fake_tensortorch._subclasses.meta_utilssysmodulesrj   fxexperimentalZ	recordingsym_nodeinterpreter_compileZ_dynamoZ
eval_frameZ	_inductorZsizevarsZ_libraryZ
custom_opsZ	fake_implZ_subclassesZ
meta_utilsZfake_tensorZ_logging	_internalr*   Ztorch._dynamo.guardsguardsuninteresting_files)r   Zmodsrh   rh   ri   r   0  s<    
r   c                   @  s   e Zd ZdS )ConstraintViolationErrorNrp   rh   rh   rh   ri   r   T  s   r   torch.Tensorr   )elemra   c                 C  s   | j S rb   )Z_has_symbolic_sizes_strides)r   rh   rh   ri   rs   X  s    rs   IntzSequence[Int]z	list[Int])shapera   c                 C  s:   dg}t | d d D ]}|||d   qtt |S )Nr   )reversedappendlist)r   stridesdimrh   rh   ri   rt   _  s    rt   r   r   )afallbackra   c                 C  s0   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).
    )r   r   r#   r   require_hintr   r   )r   r   rh   rh   ri   r~   f  s    r~   Scalarr   ra   c                 C  s   t | tr| j S dS )NT)r   r5   r   has_hintr   rh   rh   ri   r  u  s    

r  r[   c                 C  s<   t | ttfsJ t | tr dS t | jjtjjjr8d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   r   r   corenumbersr   r  rh   rh   ri   rv   {  s    	
rv   rZ   c                 C  s<   t | ttfsJ t | tr dS t | jjtjjjr8dS dS )zUtility to check if underlying object
    in SymInt is concrete value. Also returns
    true if integer is passed in.

    Args:
        a (SymInt or float): Object to test if it float
    TF)	r   r"   floatr   r   r   r  r  Floatr  rh   rh   ri   rw     s    
rw   rY   c                 C  sF   t | ttfsJ t | tr dS t | jjtjjjtjjj	frBd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   r   logicboolalgBooleanTrueBooleanFalser  rh   rh   ri   rx     s    	
rx   z2Union[SymBool, SymFloat, SymInt, bool, float, int]c                 C  st   t | tt t sJ t | tr(t| sLt | tr:t| sLt | trPt| rPdS t | ts^J | jj	
| jj S )z
    User-code friendly utility to check if a value is static or dynamic.
    Returns true if given a constant, or a symbolic expression with a fixed value.

    Args:
        a (Union[SymBool, SymFloat, SymInt, bool, float, int]): Object to test
    T)r   r   r   r   rx   rw   rv   r6   r   	shape_envrN   r   is_singletonr  rh   rh   ri   ry     s     ry   zUnion[torch.SymBool, bool]r   ra   c                 C  s4   t | tjr| jddS t | ts,J | | S d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   N)r   r   r!   r   r   r   r   rh   rh   ri   r     s    	r   )newoldra   c                   s   t jt jttf}t t jrtt js,J t    k fdd t	j
 j
D ]"\}}t ||k fdd q\nTt |rt tst|rttrJ  d  t  k fdd 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                     s   j  d j  dS N != z (old != new)r   rh   r  r  rh   ri   <lambda>  r   z"check_consistent.<locals>.<lambda>c                     s   j  d j  dS r  r  rh   r  rh   ri   r    r   r  c                     s    d  dS r  rh   rh   r  rh   ri   r    r   N)r   r#   r"   r   r  r   rT   _checkr   zipr   r   )r  r  Zscalar_typesijrh   r  ri   r     s    r   zOptional[ShapeEnv]z,Optional[dict[sympy.Symbol, pytree.KeyPath]])r  bindingsra   c                   s.   |du rdS  dusJ  fdd|  D S )a  
    When we do fake tensor prop, we oftentimes will allocate new unbacked symints.
    We then run proxy tensor mode, which populates node.meta["unbacked_bindings"]
    with these new symints. To ensure consistency we use PropagateUnbackedSymInts
    to rename unbacked bindings to their old ones. But all of the node metas are
    still using the old bindings from before the renaming. This function helps to
    post facto apply any renamings discovered in the PropogateUnbackedSymInts pass.
    Nc                   s    i | ]\}} j |||qS rh   )unbacked_renamingsgetr   kvr  rh   ri   
<dictcomp>  r   z-resolve_unbacked_bindings.<locals>.<dictcomp>)items)r  r  rh   r#  ri   r     s    r   .Resulttorch.fx.Node)r  nresultra   c                 C  s  |j dkrdS t| |jd }r| dus2J | D ]\}}t||}t|trpt	
d|j||| q:|jjdur~q:|jj}t|tjrtt|jdkrttttjtjf |jd  }rt|d dkrtt|d  }	tjrtt|	j }
tjrt| j|
 tddrt|	jdkrttttjtjf |jd dkrttt|
d}||kspJ | d	| |
}t|tjs|jr:J d
| q:||ksJ | d| || q: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).
    placeholderNunbacked_bindingsz'rebind_unbacked: discard %s %s %s -> %s   r   r   r   Tr  z#should have been constant, but got z possible memo disaster) opr   metar  r%  pytreekey_getr   r   r   infotargetr   hintr   r   	Piecewiselenr`   r   tupleBasicEqlhsSymbolvar_to_rangeissubsetrQ   rhs'_sympy_cast_symbool_to_symint_guardlessr   _rename_unbacked_to)r  r(  r)  r  Zraw_u0pathu1Zraw_u1Zraw_u1_args0eqZ
new_raw_u1Zrepackedrh   rh   ri   r     sh    

*


	

r   r   ra   c              
   C  s   | j dkrFt| jd tjjrFt| jd jdtjrF| j	dv rFd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 rdS dS )z
    Helper function to determine if a node is trying to access
    a symbolic integer such as size, stride, offset or item. Currently
    primarily only used in a DCE pass to figure out purity.
    Zcall_methodr   example_value)sizestridestorage_offsetitemTcall_functionF)r.  r   r`   r   r   Noder/  r  rT   r3  opsatenZsym_sizedefaultr   
sym_stridesym_storage_offsetZ	sym_numelr   rh   rh   ri   r     s,    	





r   c                 C  sR   t | tjtjtjtjtjtjfs&| S t | tjtjtjfrJt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   r   RelAndOrNotr9  Ner	  r
  Zto_cnf_canonicalize_bool_expr_implr  rh   rh   ri   r}     s    r}   Tz!type[Union[sympy.Add, sympy.Mul]]list[sympy.Expr]Optional[bool]r   )clsr`   sortis_commutativera   c                 C  s   |s
| j S |r| tju r$tjjj}n$| tju r:tjjj}nt	d|  |du sTJ |d j
r|dd }|| | j|d g| |dS | }|| | j||dS n| j||dS dS )a  
    Create a sympy expression from a list of arguments, optimizing for performance.

    This function creates a sympy Add or Mul expression from a list of arguments
    while avoiding expensive operations like flattening. It handles sorting the
    arguments appropriately based on the expression type.

    Args:
        cls: The sympy class to create (Add or Mul)
        args: List of sympy expressions to combine
        sort: Whether to sort the arguments (default: True)
        is_commutative: Whether the operation is commutative (default: None)

    Returns:
        A sympy expression of type cls combining all arguments

    Raises:
        ValueError: If cls is not sympy.Add or sympy.Mul
    zUnknown cls: Tr   r   Nr\  )identityr   Addr  addZ_addsortMulmulZ_mulsort
ValueError	is_NumberZ
_from_argscopy)rZ  r`   r[  r\  Zsort_fnrestrh   rh   ri   _sympy_from_args  s"    


rg  c           	      C  sD  t | tjtjfr&t| tt| j S tjtj	tj
tji}t | t| rf| j| j }|t|  }n2t | tj	tjtjtjfsJ | j| j }t| }ddddd}tj}t|}t |tjrg }g }|jD ]$}||r||  q|| qttj|ddd}ttj|ddd}n||r6| tj }}|||dd	S )
z
    After canonicalization, we are guaranteed to have eliminated Ge/Gt relations
    (rewriting them to Le/Lt, respectively).
    r   r   tra   c                 S  s0   | j r| jp.t| tjo.| jd j o.| jd jS Nr   )rd  Zis_negativer   r   ra  r`   ri  rh   rh   ri   is_neg  s    "z,_canonicalize_bool_expr_impl.<locals>.is_negFTr[  r\  evaluate)r   r   rS  rT  r   mapr}   r`   GtLtGeLer7  keysr:  r>  r9  rV  r   Zero_reduce_to_lowest_termsr_  r   rg  )	r   oppositer>  ri  rl  r:  posnegZtermrh   rh   ri   rW    s0    

rW  c                   s   ddddd}dddddd | j rtttj | j}ttj	t
||d	krZ| S  fd
d|D }ttj|d| jdS | jrtjS | jr | || S | 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.
    r   r   xra   c                 S  sB   | j rtt| S | jr:| jd j r6tt| jd S dS dS d S r   )
is_Integerabsr   is_Mulr`   r|  rh   rh   ri   integer_coefficient0  s
    "z4_reduce_to_lowest_terms.<locals>.integer_coefficient)r|  factorra   c                 S  s~   | j r| | S | jrl| jd |krH| jd t| g| jdd  }nt| jdd  }ttj|| jdS t	d|  d S )Nr   r   r]  zillegal arg to div_by_factor: )
r}  r  r`   r   r   r   rg  ra  r\  AssertionError)r|  r  r`   rh   rh   ri   div_by_factor:  s    &z._reduce_to_lowest_terms.<locals>.div_by_factorr   c                   s   g | ]} |qS rh   rh   r   r|  r  r  rh   ri   
<listcomp>M  r   z+_reduce_to_lowest_terms.<locals>.<listcomp>Trm  )is_Addr   r   r   Exprr`   r   reducemathgcdrp  rg  r_  r\  r}  r   Oner  )r   r  atomsrh   r  ri   rw  (  s     
rw  zTypeGuard[SymInt]sra   c                 C  s   t | tjo| j S rb   )r   r   r#   r   r   r  rh   rh   ri   r   X  s    r   IterateExprsAtomIterateExprszIterator[sympy.Basic]r   c                 c  s   t | trt| r| jjV  nt | tjr2| V  nt | ttt	frDnt | t
tfrl| D ]}t|E dH  qVnt| rt|  E dH  ntt | tjrt|  E dH  t|  E dH  t|  E dH  n0| du rn&t | tjrntd|  dt|  dS )a  
    Recursively iterate through a value and yield all sympy expressions contained within it.

    This function traverses various data structures (tensors, lists, tuples, etc.) and extracts
    any symbolic expressions they contain. It's used for operations like finding free symbols
    in complex nested structures.

    Args:
        val: The value to extract sympy expressions from. Can be a symbolic type (SymInt, SymFloat, SymBool),
             a sympy expression, a primitive type (int, float, bool), a container (tuple, list),
             a sparse tensor, a regular tensor, None, or a torch.Generator.

    Yields:
        sympy.Basic: Each sympy expression found in the value.

    Raises:
        AssertionError: If the value is of an unsupported type.
    Nz&cannot extract sympy expressions from  )r   r5   is_symbolicr   r   r   r8  r   r  r   r7  r   _iterate_exprsr,   rF  r   rT   rG  rH  r	   r  r   )r   r  rh   rh   ri   r  b  s*    
r  zOrderedSet[sympy.Symbol]c                 C  sR   | du rt  S t| }zt|}W n ty:   t   Y S 0 |jjdd |D  S )a:  
    Recursively collect all free symbols from a value.

    This function traverses various data structures (tensors, lists, tuples, etc.) and extracts
    all sympy symbols contained within them. It's useful for finding all symbolic variables
    that a complex nested structure depends on.

    Args:
        val: The value to extract symbols from. Can be a symbolic type (SymInt, SymFloat, SymBool),
             a container (tuple, list), a tensor, or None.

    Returns:
        OrderedSet[sympy.Symbol]: An ordered set of all free symbols found in the value.
    Nc                 s  s   | ]}|j V  qd S rb   )r   r   erh   rh   ri   	<genexpr>  r   zfree_symbols.<locals>.<genexpr>)r7   r  nextStopIterationr   union)r   itrZ
first_exprrh   rh   ri   r     s    r   c                 C  s   t dd t| D  S )z)Faster version of bool(free_symbols(val))c                 s  s   | ]}|j p|jV  qd S rb   )	is_number
is_Booleanr  rh   rh   ri   r    r   z#has_free_symbols.<locals>.<genexpr>)allr  r   rh   rh   ri   r     s    r   r{  c                 C  sL   ddl m} t| D ]2}||D ]$}|jr t|tjtjfr   dS q qdS )z2Faster version of bool(free_unbacked_symbols(val))r   )iterargsTF)Zsympy.core.traversalr  r  	is_SymbolrL   rM   UNBACKED_INTUNBACKED_FLOAT)r|  r  r  argrh   rh   ri   r     s    r   c                 C  s   t dd t| D S )z?Like free_symbols, but filtered to only report unbacked symbolsc                 s  s$   | ]}t |tjtjfr|V  qd S rb   )rL   rM   r  r  r   r  rh   rh   ri   r    s   z(free_unbacked_symbols.<locals>.<genexpr>)r7   r   r  rh   rh   ri   free_unbacked_symbols  s    r  Optional[sympy.Symbol]c                 C  s`   d| j v r\t| j d tjr\t| j d jjtjr\| jdksNt	| j d jjr\| j d jjS dS )a  
    Check if a given FX node is a symbol binding node.

    A symbol binding node is one that has a SymInt value in its meta that contains
    a sympy Symbol expression, and is either a placeholder node or contains unbacked symbols.

    Args:
        node (torch.fx.Node): The FX node to check

    Returns:
        Optional[sympy.Symbol]: The sympy Symbol if the node is a symbol binding node, None otherwise
    r   r*  N)
r/  r   r   r#   r   r   r   r;  r.  r  rQ  rh   rh   ri   r     s    	r   ztorch.fx.Graphz!dict[sympy.Symbol, torch.fx.Node])graphra   c                 C  s4   i }| j D ]$}t| }dur
||vr
|||< q
|S )a  
    Find all nodes in an FX graph that bind sympy Symbols.

    This function scans through all nodes in the given FX graph and identifies
    nodes that bind sympy Symbols (typically placeholder nodes with SymInt values).
    When multiple nodes bind the same symbol, only the first occurrence is kept.

    Args:
        graph: The FX graph to search for symbol binding nodes

    Returns:
        A dictionary mapping from sympy Symbols to their binding FX nodes
    N)nodesr   )r  rr   r  rh   rh   ri   find_symbol_binding_fx_nodes  s
    

r  frozenc                   @  s"   e Zd ZU dZded< ded< dS )r   ar  
    This class is used in multi-graph compilation contexts where we generate
    multiple specialized graphs and dispatch to the appropriate one at runtime.
    This allows us to optimize the trade-off between performance and generality
    by creating specialized versions for common patterns (e.g., x.shape[0] % 16 == 0)
    while maintaining a general fallback.
    rV   sourcer   check_fnNrj   rk   rl   r   rm   rh   rh   rh   ri   r      s   
r   c                   @  s*   e Zd ZddddZddddd	Zd
S )r   r   r   c                 C  s   dS )Nz#.cast_symbool_to_symint_guardless()rh   r   rh   rh   ri   __str__  s    zConvertIntKey.__str__r   r[   bra   c                 C  s   t |S )zGet the int value from bool) cast_symbool_to_symint_guardless)re   r  rh   rh   ri   r    s    zConvertIntKey.getN)rj   rk   rl   r  r  rh   rh   rh   ri   r     s   r   c                   @  s4   e Zd ZU ded< ddddZddddd	Zd
S )CallMethodKeyr   namer   c                 C  s   d| j  dS )N.z()r  r   rh   rh   ri   r    s    zCallMethodKey.__str__r   ora   c                 C  s   t || j S )zCall the method on object)getattrr  re   r  rh   rh   ri   r     s    zCallMethodKey.getNrj   rk   rl   rm   r  r  rh   rh   rh   ri   r    s   
r  c                   @  s4   e Zd ZU ded< ddddZddddd	Zd
S )InnerTensorKeyr   
inner_namer   c                 C  s   d| j  S Nr  )r  r   rh   rh   ri   r  )  s    zInnerTensorKey.__str__r   r  c                 C  s   t || jS )zGet the inner tensor attribute)r  r  r  rh   rh   ri   r  ,  s    zInnerTensorKey.getNr  rh   rh   rh   ri   r  %  s   
r  c                   @  s4   e Zd ZU ded< ddddZdddd	d
ZdS )DivideByKeyr[   divisorr   r   c                 C  s   d| j  dS )Nz.__floordiv__()r  r   rh   rh   ri   r  5  s    zDivideByKey.__str__r   r  c                 C  s
   || j  S )zDivide object by divisorr  r  rh   rh   ri   r  8  s    zDivideByKey.getNr  rh   rh   rh   ri   r  1  s   
r  Fr   zpytree.KeyPathzOptional[object]zOptional[set[sympy.Symbol]]z"dict[sympy.Symbol, pytree.KeyPath])r   rA  realr  pendingsimplifyra   c              	     sF  t jt |d}dddfdd}|du r4t }i }t| ttfrtt| D ]8}	|	|| |	 |t
|	f |dur||	 ndd qRnt| r|  \}
}|
D ](}t| |}|	|||t|f  qnlt| tjrd	d
lm} t| |sJ |	||  |tdf | jdur,| j ndd | jtjtjtjtjfvr|	||  |tdf | jdur| j ndd |	||  |tdf | jdur| j ndd n|t| tjtjfr@t||  }t j!r@||v r@|||<  r2|dur2t|t"t#fs&J  $|| |%| nt| tjrt||  }t j&rt|j'dkrt|j'd	  }t j(t j!frt|j'd  }t j!r||v ||v A rt||vr|n| }t j(s r| j)v rddd fdd}||v r|n|} r0t|t j(r0t"|n||}|t*|f ||< |durt|t"sdJ t|t j(r~|t"| nt+||} r $|| |%| nt| tj,rBt||  }t j-rBt|j.t j!rB|j/dkrB|j.|v rB|t0 f ||j.< |dur6t1|t2u s J  r6 $|t"| |%|j. |S )a2  
    Recursively traverses a structure to find unbacked symbols and their access paths.

    This function walks through tensors, lists, tuples, and symbolic values to locate
    unbacked symbols that are in the pending set, and returns a mapping from those
    symbols to their access paths in the structure.

    Args:
        a: The object to traverse (tensor, list, tuple, SymInt, etc.)
        path: The current path in the object tree
        real: Optional real tensor corresponding to the fake tensor being traversed
        shape_env: Optional ShapeEnv to register unbacked values with
        pending: Set of unbacked symbols to look for (will be modified in-place)
        simplify: Whether to use simplified expressions

    Returns:
        A dictionary mapping unbacked symbols to their access paths
    r  r  r   Union[SymInt, SymFloat, SymBool]r   r  c                   s    r| j jS | j jS rb   )r   r   _exprr  )r  rh   ri   r   ^  s    z._free_unbacked_symbols_with_path.<locals>.exprN)r  r   rW   rF  rG  rH  r,  r   sympy.Symbolr#   c                   s*    j | t j|   j| d gd dS )Nr   r4  r  )create_symintnoder   
var_to_valvar_to_sourcesr  r  r#  rh   ri   _symint_wrap  s
    z6_free_unbacked_symbols_with_path.<locals>._symint_wrap)3r   partial _free_unbacked_symbols_with_pathsetr   r7  r   ranger6  updater0  ZSequenceKeyr8   __tensor_flatten__r  r  r   rT   r   rX   rF  r  Zreal_tensorZlayoutZ
sparse_csrZ
sparse_cscZ
sparse_bsrZ
sparse_bscrG  rH  r#   r"   r   r;  r   r  set_unbacked_var_to_valremovera  r`   r   r  r  r;   r!   r9  r:  r>  r   r   r   )r   rA  r  r  r  r  gor   r  r  attrs_attrsubrX   r  r:  r>  coeffr  Zunbackedr  r   rh   )r  r  ri   r  =  s    

 






r  )r  rE  old_example_valuepeekra   c                 C  sJ  | du rdS | j }t|}|s"dS |s:td| |  t|d| |dd}|s|rt|tjrtt	|
 | fnd}td| d| d	| d
|durF| D ]}t||}	t||}
t|
trt|
jj }tjrt|	tr(|	jj }|kr(t|tjr| || n| || qt|	ts| |t|	 q|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 %srh   Fr  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/edit)pending_fresh_unbacked_symbolsr  r   r2  clearr  r   r   rT   r   rG  rH  ro   valuesr0  r1  r5   r   r   r   r;  r@  _eliminate_unbackedsympify)r  rE  r  r  fsr  Zsymbol_to_pathextraZkeypathZold_symZnew_symnew_sZold_srh   rh   ri   r     sJ    



r   )r   rN  ra   c                 C  s~   t | tst | tsJ | S tjjjjr@t| }|dur<|S |S t	| j
dd}|du r^t| S | j
}|jj|d|d}t|S )zR
    Try to guard a, if data dependent error encountered just return default.
    Nr  F)size_obliviousfallback_value)r   r!   r   r   r   r   r.   backed_size_oblivious_static_eval_sym_boolr  r   
guard_boolr  evaluate_sym_node)r   rN  r)  r  r   r  rh   rh   ri   	_guard_orQ  s    
r  c                 C  s
   t | dS )zP
    Try to guard a, if data dependent error encountered just return false.
    Fr  r  rh   rh   ri   rq   k  s    rq   c                 C  s
   t | dS )zO
    Try to guard a, if data dependent error encountered just return true.
    Tr  r  rh   rh   ri   rr   r  s    rr   r!   c                 C  sj   t | tsJ | jj}z.| jj}||}|d ur<t|W S W d S W n  tyd   t	d| Y d S 0 d S )NzCould not simplify %s)
r   r!   r   r   r  _maybe_evaluate_staticr   	Exceptionr   r   )r|  r   r  Z
simplifiedrh   rh   ri   r  y  s    


r  c                 C  s8   t | tst | tsJ |  S t| }|du r2dS | S )a  
    Returns True if x can be simplified to a constant and is False.
    If x cannot be evaluated from static, we return False

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

    Args:
        x (bool, SymBool): The expression to try statically evaluating
    NFr   r!   r   r  r|  r)  rh   rh   ri   r     s    
r   c                 C  s4   t | tst | tsJ | S t| }|du r0dS |S )aE  
    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
    NFr  r  rh   rh   ri   r     s    
r   )r|  othersra   c                 G  s*   t |dkr| S |D ]}t| |} q| S )zB
    and, but for symbolic expressions, without bool casting.
    r   )r6  operatorand_r|  r  yrh   rh   ri   r     s
    r   )r|  r  ra   c                 C  s   t | ttfrHt |ttfrHt| t|kr0dS ttjtt	| |dS t | t
tjfrpt |t
tjfrp| |kS tdt|  dt| dS )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  N)r   r7  r   r6  r   r  r  r  rp  r   r   r   r#   r  r   )r|  r  rh   rh   ri   r     s     r   c                 G  s*   t |dkr| S |D ]}t| |} q| S )zA
    or, but for symbolic expressions, without bool casting.
    r   )r6  r  or_r  rh   rh   ri   r     s
    r   z2Union[SymBool, SymInt, SymFloat, int, bool, float]zUnion[bool, int, float]c                 C  sT   t | ttfrt| S t | ttfr,t| S t | ttfrBt	| S t
d|  dS )a  
    Guard a scalar value, which can be a symbolic or concrete boolean, integer, or float.

    This function dispatches to the appropriate guard function based on the type of the input.

    Args:
        a: A symbolic or concrete scalar value (bool, int, or float)

    Returns:
        The concrete value after guarding

    Raises:
        AssertionError: If the input is not a recognized scalar type
    zunrecognized scalar N)r   r!   r   r  r#   r   rz   r"   r  r{   r  r  rh   rh   ri   r|     s    r|   r#   c                 C  sD   t | tr@t | jtr@t | jjtjr@| jj| jjr@t	|  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   r4   r   r   r;  r  is_unbacked_symint_constrain_range_for_sizer  rh   rh   ri   _advise_is_size  s    "
r  r   upper_boundra   c                 C  sZ   t | trVt | jtrVt | jjtjrV| jj| jjrVt |t	rV| jj
| jj| d S rb   )r   r#   r   r4   r   r   r;  r  r  r   _constrain_is_bounded)r   r   rh   rh   ri   _advise_is_bounded"  s    
r  r   minmaxra   c                 C  s`   t | ttfrtdt | ts(J dt | jjtjsFJ 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 SymIntzconstraining non-Symbols NYI: N)r   r"   r!   rc  r#   r   r   r   r;  r  r  r   r  r  rh   rh   ri   r  -  s
    r  r  c                C  s   |du rt  }|du rt }||k r*tdt| trj||   krH|ksfn 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 [:])rE   rc  r   r   r   r  _constrain_ranger   r  rh   rh   ri   constrain_range>  s    
r  ztorch.SymIntr   r  ra   c                 C  sF   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  rh   rh   ri   constrain_unifyl  s    


r  )r   skipra   c                 C  sr   t | trZt }t|d D ]}|d u r. q6|j}q| j|rH|jj	nd|rT|j
ndS t| tu snJ | | S )Nr   r  r   )r   r!   r   currentframer  f_backr   expect_truef_codeco_filenamef_linenor   r   )r   r  framer  rh   rh   ri   r    s    
r  c                 C  s0   t | tr| jddS t| tu s,J | | S Nr  r   )r   r!   r   r  r   r   r  rh   rh   ri   r    s    
r  c                 C  s0   t | tr| jddS t| tu s,J | | S r  )r   r#   r   rz   r   r   r  rh   rh   ri   rz     s    
rz   r  c                 C  s.   t | tr| jddS t | ts*J | | S r  )r   r"   r   r{   r  r  rh   rh   ri   r{     s    
r{   ztorch.fx.GraphModulezlist[object])gmra   c                 C  s   dd | j jD S )Nc                 S  s    g | ]}|j d kr|jd qS )r*  r   )r.  r/  r   r(  rh   rh   ri   r    r   z'fx_placeholder_vals.<locals>.<listcomp>r  r  r  rh   rh   ri   fx_placeholder_vals  s    r  	list[str]c                 C  s   dd | j jD S )Nc                 S  s   g | ]}|j d kr|jqS )r*  )r.  r3  r  rh   rh   ri   r    r   z*fx_placeholder_targets.<locals>.<listcomp>r  r  rh   rh   ri   fx_placeholder_targets  s    r!  ignore_staticrT   )r  r`   r#  ra   c                G  s   | j jt| ||dS )Nr"  )r  evaluate_guards_for_argsr  )r  r#  r`   rh   rh   ri   eval_guards  s    
r%  dict[sympy.Symbol, int])r  r`   ra   c                 G  s   | j t| |S rb   )r  bind_symbolsr  )r  r`   rh   rh   ri   r'    s    r'  c                   @  s(   e Zd 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)
rj   rk   rl   r   DYNAMICDUCKSTATICSIZE_LIKE_UNBACKEDINFER_STRIDEOBLIVIOUS_SIZErh   rh   rh   ri   r(    s   r(  c                   @  s   e Zd ZU ded< dS )
Constraintr   	warn_onlyNrj   rk   rl   rm   rh   rh   rh   ri   r2    s   
r2  c                   @  s*   e Zd ZU dZded< dddd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.
    rQ   vrr&   r   r  ra   c                 C  s    | j j d|  d| j j S )zFormat the constrain equation <= )r6  lowerr  upperre   r  rh   rh   ri   render+  s    zStrictMinMaxConstraint.renderN)rj   rk   rl   r   rm   r<  rh   rh   rh   ri   r5    s   
r5  c                   @  s    e Zd ZdZdddd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&   r   r7  c                 C  s   d|   dS )NzRelaxedUnspecConstraint(r  r  r;  rh   rh   ri   r<  F  s    zRelaxedUnspecConstraint.renderN)rj   rk   rl   r   r<  rh   rh   rh   ri   r=  1  s   r=  c                   @  s   e Zd ZU dZded< ded< ded< ded	< ed
dZded< ed
dZded< ddddZdddddZ	ddddddZ
dddddZdddd d!d"Zddd#dd$d%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.)
    zlist[tuple[Source, Source]]source_pairszTlist[tuple[Source, Union[Source, sympy.Symbol], Callable[[sympy.Expr], sympy.Expr]]]derived_equalitieszlist[sympy.Symbol]phantom_symbolszset[Source]relaxed_sourcesF)initzdict[Source, Source]_parentszdict[Source, sympy.Expr]_defsr_   r   c                 C  s   i }t | d| i }t | d| | jD ] \}}| | || | q*| jD ]F\}}}t|tjr~||| j	| |< qR|| 
|| j	| |< qRd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}
        rD  rE  N)r   __setattr__r?  _union_findr@  r   r   r;  rE  _rewrite)re   rD  rE  source1source2r  rootfnrh   rh   ri   __post_init__n  s    z EqualityConstraint.__post_init__r&   r7  c                 C  s"   || j v r| | j | S |S d S rb   )rD  rH  r;  rh   rh   ri   rH    s    
zEqualityConstraint._find)root1root2ra   c                 C  s   ||kr|| j |< d S rb   )rD  )re   rO  rP  rh   rh   ri   rG    s    zEqualityConstraint._unionr   srcra   c                 C  s0   |  |}|| jv r| j| S t| S d S rb   )rH  rE  r   r;  r  )re   rR  rh   rh   ri   rI    s    


zEqualityConstraint._rewriter   )rJ  rK  ra   c                 C  sB   |  | }| jv p@|  | }| jv p@||kp@| ||dd S )Nc                 S  s   | S rb   rh   r  rh   rh   ri   r    r   z-EqualityConstraint.is_equal.<locals>.<lambda>)rH  rB  
is_derived)re   rJ  rK  src1src2rh   rh   ri   is_equal  s    zEqualityConstraint.is_equalz"Callable[[sympy.Expr], sympy.Expr])rR  
symbol_srcrM  ra   c                 C  s   |  |||  |kS rb   )rI  )re   rR  rW  rM  rh   rh   ri   rS    s    zEqualityConstraint.is_derivedN)rj   rk   rl   r   rm   r   rD  rE  rN  rH  rG  rI  rV  rS  rh   rh   rh   ri   r>  P  s   
#r>  zTypeGuard[SymbolicContext])symbolic_contextra   c                 C  s*   t | tsJ dt| tus&J ddS )NzInvalid symbolic_context objectz%Illegal usage of symbolic_context ABCT)r   r   r   rX  rh   rh   ri   _assert_symbol_context  s    rZ  c                 C  sb   t | tjtjfrVt| jdkr$dS | j\}}t|rBt |tjpTt |tjoTt|S t | tjS )Nr,  F)	r   r   r_  ra  r6  r`   _is_supported_equivalencer   r;  )r   r:  r>  rh   rh   ri   r[    s    
r[  r]   c                 C  s&   |  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	functionsZToFloatrD   r:   r  rh   rh   ri   #_has_uninterpretable_sympy_function  s
    


r`  c                   @  s   e Zd ZdZdS )r   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)rj   rk   rl   r   rh   rh   rh   ri   r     s   r   c                   @  s   e Zd ZU dZded< dS )r   zE
    Data structure specifying any constraints on a SymInt input
    DimConstraint
constraintNr  rh   rh   rh   ri   r     s   
r   _P1_T1c                   @  sd   e Zd ZU dZded< dZded< dZded< dZded< dZd	ed
< dZ	ded< ddddZ
dS )r   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
    zDimList[DimDynamic]dynamic_sizesNdynamic_strideszDimList[DimConstraint]constraint_sizesconstraint_stridesz(Optional[list[list[Callable[_P1, _T1]]]]specialize_onOptional[SymbolicContext]view_base_contextr_   r   c                 C  s   | j d u r$t| dg gt| j  | jd u rJt| dtjgt| j  | jd u rnt| dd gt| j  | j	d u rt| dd gt| j  t
dd | jD sJ d S )Nri  rf  rg  rh  c                 s  s"   | ]}|t jt jt jfv V  qd S rb   )r(  r0  r,  r-  )r   rG  rh   rh   ri   r     s   z9StatelessSymbolicContext.__post_init__.<locals>.<genexpr>)ri  r   rF  r6  re  rf  r(  r0  rg  rh  r  r   rh   rh   ri   rN    s.    



z&StatelessSymbolicContext.__post_init__)rj   rk   rl   r   rm   rf  rg  rh  ri  rk  rN  rh   rh   rh   ri   r     s   
r   c                      s@   e Zd ZU dZdZded< dZded< dd fd	d
Z  ZS )r   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 owner's responsibility to maintain the lifecycle of the cache
    with respect to different shape_envs, clearing, etc.
    Nr&   tensor_sourcez dict[int, dict[str, sympy.Expr]]#shape_env_to_source_to_symbol_cacher_   r   c                   s0   t    | jd usJ | js,t| di  d S )Nrm  )rc   rN  rl  rm  r   rF  r   rf   rh   ri   rN  W  s    
z%StatefulSymbolicContext.__post_init__)	rj   rk   rl   r   rl  rm   rm  rN  rn   rh   rh   rf   ri   r   <  s   

r   c                      s4   e Zd ZU dZdZded< dd fddZ  ZS )	r   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.
    Nzdict[str, SymbolicContext]inner_contextsr_   r   c                   s   t    | jd u ri | _d S rb   )rc   rN  rn  r   rf   rh   ri   rN  i  s    

z%SubclassSymbolicContext.__post_init__)rj   rk   rl   r   rn  rm   rN  rn   rh   rh   rf   ri   r   _  s   
r   c                   @  sH   e Zd ZU dZded< ded< ded< dd	d
dZdddddZdS )r   zh
    Tracks the sources of all fake tensors we wrap in Dynamo.
    Used by shape guard computation.
    zUnion[FakeTensor, SymInt]faker&   r  rj  rX  r   r   c                 C  s   t | j| j fS rb   )r   ro  r  r  r   rh   rh   ri   r   z  s    zTrackedFake.__hash__r   r   r   c                 C  s.   t |tr*| j|ju o(| j |j kS dS NF)r   r   ro  r  r  r   rh   rh   ri   r   }  s    
 zTrackedFake.__eq__N)rj   rk   rl   r   rm   r   r   rh   rh   rh   ri   r   o  s   
r   z2Union[int, SymInt, float, SymFloat, bool, SymBool]z+TypeGuard[Union[SymInt, SymFloat, SymBool]]c                 C  s   t | tttfrdS | j S rp  )r   r   r  r   r   r  r   rh   rh   ri   r    s    r  ztuple[sympy.Expr, bool]r`   ra   c                 C  s   g g  }}| D ] }|j r$|| q|| qtj| g}|D ]}dd t||jD }q@tj| }|t|dkpt|dkot|dkfS )a  
    Expand products of sums into sums of products.

    This function takes a list of sympy expressions and separates them into
    additive expressions (those with is_Add=True) and other expressions.
    It then computes the distributive product, expanding (a+b)*(c+d) into a*c + a*d + b*c + b*d.

    Args:
        args: A list of sympy expressions to expand

    Returns:
        A tuple containing:
        - The expanded expression as a sympy.Expr
        - A boolean indicating whether expansion occurred (True if multiple additive
          expressions were present or if there was at least one additive and one other expression)
    c                 S  s   g | ]\}}|| qS rh   rh   )r   r   r  rh   rh   ri   r    r   z_expandsums.<locals>.<listcomp>r   r   )	r  r   r   ra  	itertoolsproductr`   r_  r6  )r`   Zaddsr   r  r)  r`  rh   rh   ri   _expandsums  s    

rt  c           	      C  s  dd | j D }tdd t| j |D r8t| j| S | jr| j \}}|jr|jr|dkrjtj	| ddS |dk rt
jtj	t
j|  dd S nv| jrg }g }| j D ]4}|jr|j d d	kr|t
j|  q|| qt|\}}t|\}}|s|r|| S | S )
a  
    A faster implementation of sympy's expand function for common cases.

    This function expands expressions like (a+b)^n or (a+b)*(c+d) into sums of products,
    but avoids the expensive checks and features of sympy's full expand implementation.
    It only recreates objects when necessary to avoid expensive operations.

    Args:
        expr: A sympy expression to expand

    Returns:
        The expanded expression
    c                 S  s   g | ]}t |qS rh   )_fast_expandr   r  rh   rh   ri   r    r   z _fast_expand.<locals>.<listcomp>c                 s  s   | ]\}}||uV  qd S rb   rh   )r   r  Znew_argrh   rh   ri   r    r   z_fast_expand.<locals>.<genexpr>r   F)deepr   r   )r`   anyr  ru  funcZis_Powr}  r  r   Zexpand_multinomialr   r  r  r   rt  )	r   new_argsbaseexpnumZdenr  Znum_changedZden_changedrh   rh   ri   ru    s,    


ru     )r  ra   c                 C  sB   t | dr:z
t| W S  ty6   td|  |  Y S 0 n| S dS )a  
    Expand the given symbolic expression by recursively rewriting product of
    sums into sum of products (with the product being either a multiplication or
    exponentiation).

    NOTE: using this on an intermediate expression may prevent simplification
    down the line, e.g., if we eagerly expand `(a + b)^2` into `a^2 + 2ab + b^2`,
    we won't be able to simplify `(a^2 + 2ab + b^2) / (a + b)` as easily.
    expandz"RecursionError in _fast_expand(%s)N)hasattrru  RecursionErrorr   warningr  rh   rh   ri   safe_expand  s    

r  c                   @  s.   e Zd ZU ded< ded< ded< ded< d	S )
_SymbolInfor  r!  zOptional[ValueRanges]r6  zOptional[sympy.Integer]r   r   is_size_likeNr4  rh   rh   rh   ri   r    s   
r  ztuple[_SymbolInfo, ...]zOptional[_SympyT])r   symbol_infounbacked_onlyr  ra   c                 C  sb  i }i }t |D ]\}}|\}}	}
}t|
tr0q|	dus<J |r|rtd|	j}td|	j}||krl|d }||krt||}	n|	j}|t u s|r|
dus|	j	s|	||< qt
jd| ddd}t|d }|| ||< t|	| ||< qz| |}W n$ ty    td| | Y dS 0 tt|}|jr:|S t||}| rT|jS |r^|S dS )	a  
    This variant of ShapeEnv._maybe_evaluate_static has no dependence on
    ShapeEnv and thus can be cached indefinitely.  It does the "heavy" lifting
    for static evaluation, including nontrivial reliance on Sympy simplification
    that occurs when we reallocate the symbols
    Nr,  l          r   Zevaluate_static_shape_Tpositiveintegerz(RecursionError in sympy.xreplace(%s, %s))	enumerater   rI   r  r9  r  r:  rQ   rE   is_intr   r;  r   rO   r`  xreplacer  r   r  r}   r  r  rN   r  )r   r  r  r  Znew_shape_envZnew_range_envidxsinfor!  r6  r   r  r9  r:  r  offsetnew_exproutrh   rh   ri   _maybe_evaluate_static_worker  sD    



r  r   c                   C  s   t dd S )Nzshouldn't be hit)r  rh   rh   rh   ri   error`	  s    r  zSequence[int])sizesr   ra   c                 C  s   t tt| |S rb   )r   r  "_eval_is_non_overlapping_and_dense)r  r   rh   rh   ri   !eval_is_non_overlapping_and_densee	  s    r  c                 C  sv   t | }|dkr(|d dkp&| d dk S tt| |tdd}d}|D ](\}}|dkrZqH||krh dS ||9 }qHdS )a  
    Evaluates whether a tensor with the given sizes and strides is non-overlapping and dense.

    A tensor is non-overlapping if there's no memory location that belongs to more than one element.
    A tensor is dense if all elements are stored in memory without gaps.

    Args:
        sizes: Sequence of dimension sizes for the tensor
        strides: Sequence of strides for the tensor

    Returns:
        True if the tensor is non-overlapping and dense, False otherwise
    r   r   r,  keyFT)r6  sortedr  r  
itemgetter)r  r   r   Zlengths_and_stridesZexpected_stridelengthrG  rh   rh   ri   r  k	  s    
r  c                 C  s   t d| fdS )Nr   r-  )r   r5  r  rh   rh   ri   r?  	  s    r?  zUnion[bool, torch.SymBool]zUnion[int, torch.SymInt])symboolra   c                 C  sJ   t | tr| rdS dS t| jj}| jjj|t| rBt| j	 nddS )a   
    Converts a SymBool or bool to a SymInt or int without introducing guards.

    This function maps True to 1 and False to 0, preserving the symbolic nature
    of the input when it's a SymBool. Unlike regular casting which might introduce
    guards, this function performs the conversion without adding any guards.

    Args:
        symbool: A boolean value, either a concrete bool or symbolic SymBool

    Returns:
        The corresponding integer value (1 for True, 0 for False) as either
        a concrete int or symbolic SymInt
    r   r   Nr4  )
r   r   r?  r   r   r  r  r  r   r   )r  Zint_symrh   rh   ri   r  	  s    
r  )r?   r  r  r   r   r   )rM  r   ra   c                   sz   t ||  dtjrBdt| ddddd fdd}n$t| ddddd fd	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   Nru   r   r   )re   r`   kwargsra   c                   s\   d u r|   | jkr2   | j|   n|   ksFJ d | g|R i |S )Nz9ShapeEnv cache key changed without version being updated!)_get_key_version_counterr   re   r`   r  fn_cacheZ	prior_keyprior_versionrh   ri   wrapper	  s    

z_lru_cache.<locals>.wrapperc                   s.   | j kr   | j  | g|R i |S rb   )r  r   r  )r  r  rh   ri   r  	  s    
)r   configZvalidate_shape_env_version_keyr   wrapsr   r   )rM  r   r  rh   r  ri   
_lru_cache	  s     r  c                   @  s>   e Zd ZU dZded< eddZded< eddZded	< d
S )RuntimeAsserta  
    This is pretty similar to ShapeGuard but it also comes with a message,
    and is exclusively used for things that MUST be true (unlike guards,
    which can evaluate False, in which case you just choose not to use
    a particular specialization)
    r   r   F)r   r   msgrR   stackN)rj   rk   rl   r   rm   r   r  r  rh   rh   rh   ri   r  	  s   
r  c                   @  s   e Zd ZdddddZdS )SymExprPrintersympy.Floatr   r  c                 C  s   t t|S rb   r   r  re   r   rh   rh   ri   _print_Float
  s    zSymExprPrinter._print_FloatN)rj   rk   rl   r  rh   rh   rh   ri   r  
  s   r  c                      sx   e Zd ZdZddddd fddZdd	d
ddZdd	d
ddZejdd	dddZ	ejdd	d
ddZ
  ZS )_ShapeGuardPrintera]  
    Abstract base class for printers that convert symbolic expressions to string representations.

    This class provides common functionality for printing symbolic expressions with
    special handling for symbols that represent tensor shapes, strides, etc.
    Subclasses implement specific formatting for different output languages.

    Args:
        symbol_to_source: Mapping from sympy symbols to their source objects
        source_ref: Function to convert a source to its string representation
        var_to_sources: Mapping from sympy symbols to their source objects (for error reporting)
    #Mapping[sympy.Symbol, list[Source]]Callable[[Source], str]r_   )symbol_to_source
source_refr  ra   c                   s    || _ || _|| _t   d S rb   )r  r  r  rc   rd   )re   r  r  r  rf   rh   ri   rd   
  s    z_ShapeGuardPrinter.__init__r  r   r  c                 C  s   t t|S )z>Convert a sympy Float to a Python float string representation.r  r  rh   rh   ri   r  
  s    z_ShapeGuardPrinter._print_Floatr  c                   sz   t |tjsJ tt|dd fdd} j|sfJ | ddd  j| D  d|  d	  j| d
 S )a  
        Convert a sympy Symbol to its source representation.

        This method looks up the symbol in symbol_to_source mapping and returns
        the string representation of its first source.

        Args:
            expr: The sympy Symbol to convert

        Returns:
            String representation of the symbol's source

        Raises:
            AssertionError: If the symbol is not found in symbol_to_source
        r   r   c                     s   t dd  j D S )Nc                 S  s    i | ]\}}|d d |D qS )c                 S  s   g | ]}|  qS rh   r  r  rh   rh   ri   r  8
  r   z^_ShapeGuardPrinter._print_Symbol.<locals>.repr_symbol_to_source.<locals>.<dictcomp>.<listcomp>rh   )r   symbolsourcesrh   rh   ri   r$  7
  s   zS_ShapeGuardPrinter._print_Symbol.<locals>.repr_symbol_to_source.<locals>.<dictcomp>)r   r  r%  rh   r   rh   ri   repr_symbol_to_source5
  s
    z?_ShapeGuardPrinter._print_Symbol.<locals>.repr_symbol_to_sourcez (could be from c                 S  s   g | ]}|  qS rh   r  r  rh   rh   ri   r  >
  r   z4_ShapeGuardPrinter._print_Symbol.<locals>.<listcomp>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   r   r;  r   r   r  r  r  print_source)re   r   r  rh   r   ri   _print_Symbol#
  s    z _ShapeGuardPrinter._print_Symbolr&   r7  c                 C  s   dS )z
        Convert a source object to its string representation.

        Args:
            source: The source object to convert

        Returns:
            String representation of the source
        Nrh   r;  rh   rh   ri   r  D
  s    z_ShapeGuardPrinter.print_sourcer   c                 C  s   dS )z
        Convert a sympy expression to its string representation.

        Args:
            expr: The sympy expression to convert

        Returns:
            String representation of the expression
        Nrh   r  rh   rh   ri   doprintQ
  s    z_ShapeGuardPrinter.doprint)rj   rk   rl   r   rd   r  r  abcabstractmethodr  r  rn   rh   rh   rf   ri   r  
  s   !r  c                      sH   e Zd ZdZddd fddZddd	d
dZdddddZ  ZS )ShapeGuardPythonPrintera  
    Python printer for shape guards that extends the base ShapeGuardPrinter.

    This class provides functionality to print symbolic expressions as Python code,
    with caching to improve performance when printing the same expressions multiple times.
    It handles printing of sources and expressions according to Python syntax.

    Args:
        *args: Arguments passed to the parent classes.
    r   r_   rq  c                   s   t  j|  i | _d S rb   )rc   rd   _print_cachere   r`   rf   rh   ri   rd   k
  s    z ShapeGuardPythonPrinter.__init__r&   r   r7  c                 C  s
   |  |S )z
        Convert a source object to its string representation using the source_ref function.

        Args:
            source: The source object to convert

        Returns:
            String representation of the source
        )r  r;  rh   rh   ri   r  o
  s    
z$ShapeGuardPythonPrinter.print_sourcer   r  c                 C  s8   | j |d}|dur|S t| |}|| j |< |S dS )a  
        Convert a sympy expression to its Python string representation with caching.

        This method first checks if the expression is already in the cache.
        If found, it returns the cached result; otherwise, it delegates to
        PythonPrinter's doprint method and caches the result.

        Args:
            expr: The sympy expression to convert

        Returns:
            String representation of the expression in Python syntax
        N)r  r  rG   r  )re   r   r   resrh   rh   ri   r  {
  s    
zShapeGuardPythonPrinter.doprint)rj   rk   rl   r   rd   r  r  rn   rh   rh   rf   ri   r  _
  s   r  z`torch.fx.experimental.symbolic_shapes.ShapeGuardPrinter` is deprecated, please use `torch.fx.experimental.symbolic_shapes.ShapeGuardPythonPrinter` instead.)categoryc                   @  s   e Zd ZdS )ShapeGuardPrinterNrp   rh   rh   rh   ri   r  
  s   r  c                      sD   e Zd Zddd fddZdddd	d
ZdddddZ  ZS )_ShapeGuardCppPrinterr   r_   rq  c                   s   t  | _i | _t j|  d S rb   )r  all_symbolssource_to_symbolrc   rd   r  rf   rh   ri   rd   
  s    z_ShapeGuardCppPrinter.__init__r&   r   r7  c                 C  sv   || j v r| j | jS | }tdd|}|}d}|| jv rV| d| }|d7 }q4t|| j |< | j| |S )Nz[^0-9a-zA-Z_]+r  r   r   )r  r  rer  r  r   r;  r`  )re   r  source_namemangled_nameZold_mangled_namecountrh   rh   ri   r  
  s    


z"_ShapeGuardCppPrinter.print_sourcer   r  c                 C  s   t | |S rb   )rF   r  r  rh   rh   ri   r  
  s    z_ShapeGuardCppPrinter.doprint)rj   rk   rl   rd   r  r  rn   rh   rh   rf   ri   r  
  s   r  c                   @  s   e Zd ZU ded< dS )_ShapeGuardsHelperr   exprsNr4  rh   rh   rh   ri   r  
  s   
r  c                   @  s   e Zd ZU ded< dS )_CppShapeGuardsHelperzdict[Source, sympy.Symbol]r  Nr4  rh   rh   rh   ri   r  
  s   
r  c                      s"   e Zd Zdd fddZ  ZS )LoggingShapeGuardPrinterr  )r  c                   s   t  |dd | d S )Nc                 S  s   |   S rb   r  r(  rh   rh   ri   r  
  r   z3LoggingShapeGuardPrinter.__init__.<locals>.<lambda>)rc   rd   )re   r  rf   rh   ri   rd   
  s    z!LoggingShapeGuardPrinter.__init__)rj   rk   rl   rd   rn   rh   rh   rf   ri   r  
  s   r  c                      s8   e Zd ZdZddd fddZddd	d
dZ  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.
     dict[sympy.Symbol, list[Source]]Mapping[str, str])r  source_name_to_debug_namec                   s   t    || _|| _d S rb   )rc   rd   r  r  )re   r  r  rf   rh   ri   rd   
  s    
z$DynamicDimConstraintPrinter.__init__r  r   r  c                 C  sJ   t |tjsJ tt|| j|s8J d| d| j| d  S )NzUnknown symbol z created by constraints solverr   )r   r   r;  r   r   r  r  r  r  rh   rh   ri   r  
  s
    
z)DynamicDimConstraintPrinter._print_Symbolrj   rk   rl   r   rd   r  rn   rh   rh   rf   ri   r  
  s   	r  c                   @  s  e Zd ZdZdddddddd	Zd
dddddZddddZdddddZdddddZddddddZ	ddd d!Z
ddd"d#Zddd$d%Zeddd&d'd(Zd)dd*d+Zd,d-d.d/d0Zd,d1d.d2d3Zd4d5dd6d7d8Zd9d:d,d;d<d=d>d?Zd@S )ADimConstraintsz
    Custom solver for a system of constraints on symbolic dimensions.
    Solutions are "static" values or simplified "dynamic" constraints.
    r  z$Mapping[sympy.Symbol, sympy.Integer]zset[sympy.Symbol]r  r_   )r  r  marked_dynamicr  ra   c                 C  sx   t t| _t | _i | _|| _t t| _t | _g | _t | _	t | _
t||| _g | _|| _tttth| _|   d S rb   )r   r  _univariate_inequalities_symbols_with_equalities_substitutions_var_to_val_congruences_multivariate_inequalities_symbolic_equivalences_static_results_dynamic_resultsr  _dcp_inconsistencies_marked_dynamicr9   rB   rC   r<   _supported_sympy_functions_enumerate_sympy_functions)re   r  r  r  r  rh   rh   ri   rd   
  s*    	
zDimConstraints.__init__r  r   )r  r   ra   c                   sr   ddd fdd}ddd fdd}| trB|t|}| trX|t|}| trn|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).
        r   rq  c                    sb   | \}}  |  | }}|j|j }|| | }|dkr^j  | |S rj  rewrite_with_congruencesr  r  r  r`  r`   r{  r  Zmod_reduced
congruencer  re   rh   ri   mod_handler)  s    

z<DimConstraints.rewrite_with_congruences.<locals>.mod_handlerc                    sj   | \}}  |  | }}|j|j }|| | }|dkr^j  | || | S rj  r  r  r  rh   ri   floor_div_handlerN  s    

zBDimConstraints.rewrite_with_congruences.<locals>.floor_div_handler)r\  rB   replacerC   r<   )re   r  r   r  r  rh   r  ri   r  "  s    %


z'DimConstraints.rewrite_with_congruencesr   c                 C  sP   t jjj}t }t|D ]$}tt|| }tj	r|
| q|| j| _d S rb   )r   r]  r^  r_  r  dirr   r  r   ZFunctionClassr`  
differencer  _unsupported_sympy_functions)re   moduleZall_functionsr  ry  rh   rh   ri   r  o  s    
z)DimConstraints._enumerate_sympy_functionsr]   r   r  c                 C  s   |j | j S )z^
        Tracks list of sympy.Functions the export solver doesn't know how to handle.
        )r\  r  r  rh   rh   ri   _has_unsupported_sympy_functiony  s    z.DimConstraints._has_unsupported_sympy_functionr   c           	      C  s4  |t jkrdS |}|| j}|t jkr:| j| d t|t jt j	t j
fsZ| |r^dS |j}|svJ d| t|dkr| j| nt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!)r   truer  r  falser  r   r   rV  rT  rS  r  r   r6  r  r`  r  iterr  r  r9  r  r  )	re   r   	orig_exprZorig_reducedr   r  Zold_n_congruencesZnew_n_congruencesZreducedrh   rh   ri   r`    s>    


zDimConstraints.addr&   r   )r  r   ra   c                 C  s6   |j r"| j|  d|  n| j||f dS )zAdd an equality constraint == N)r  r  r`  r  r  r   )re   r  r   rh   rh   ri   add_equality  s    zDimConstraints.add_equalityz#dict[sympy.Symbol, set[sympy.Expr]]c                   s&  i }| j  D ]\}}g }t }|D ]}|j\}}tjddd}	tj|||	  |gd\}
}||
krtjj	||	\}}t
|tjrt
|tjr|| }|||f q&|| q&|rtjjj| \}}|| | h||< ||tjddd | i ||  fdd|D  q|||< q|S )NZreduce_congruences_tmpTr  )symbolstmpc                 3  s   | ]}t | s|V  qd S rb   )r   checksol)r   r  Zsubstitutionrh   ri   r    s   z5DimConstraints._reduce_congruences.<locals>.<genexpr>)r  r%  r  r`   r   r;  Zsolve_linearpolys	polytoolsdivr   r   r   r`  ZntheoryZmodularZsolve_congruencer  )re   reduced_congruencesr  congruencesZremainder_modulus_pairsZcongruences_to_checkr  r{  r  r  r  solutionmodulus	remainderrh   r  ri   _reduce_congruences  s:    


z"DimConstraints._reduce_congruencesc                 C  s.   | j r*d| j }| j   td| d S )N
z*The following inconsistencies were found:
)r  joinr  rc  )re   r  rh   rh   ri   _raise_inconsistencies  s    
z%DimConstraints._raise_inconsistenciesc                   sn       jr
 j } j|}tjj||}t|tj	rXt
dd |jD |}t|tjsxJ d| d| |j\}}||ksJ d| d|  j jj| d   d|  | j|<  j}t  _|D ]} || j| i q    q  }| D ]\}}	|	D ]}
| jvsNt|
| j| is& |
r&|
j\}}d	t jj jj| d   jj| d   }tj|d
d}ddlm} ||g jj|< t t||| |}|dusJ  j! j"t||d  q&q j D ]\}}ztjj||}t|tj#rbt
t$ fdd|jD }t|tj	r|jD ]} j! j"| qvn j! j"| W nV t%t&fy  } z8t'(d| |D ]} j! j"| qW Y d}~n
d}~0 0 q j)}g  _)|D ]\}} *|| j q j)D ],\}} j!|  d j"|  q<dS )zGSolve the system of constraint equations to find simplified constraintsc                 s  s   | ]}t |tjr|V  qd S rb   )r   r   r9  rv  rh   rh   ri   r    r   z'DimConstraints.solve.<locals>.<genexpr>z$Expected an equality constraint for z, got zExpected a constraint on z instead of on r   r  r  Tr  )ConstantSourceNr   c                 3  s   | ]}|  jr|V  qd S rb   )r  r  rv  r   rh   ri   r  .  s   z!Failed to reduce inequalities: %s)+r
  r  popr  r   ZsolversZinequalitiesZreduce_inequalitiesr   rS  r  r`   r9  r  r`  r  r  r  r  r  r  r  r  r%  r  _is_supported_congruencer   r  r  r;  torch._dynamo.sourcer  rJ   r  r  rT  r  NotImplementedErrorr  r   r  r  r  )re   r  r  r  r  r   Zmultivariate_inequalitiesr   r  r  r  r{  r  Ztmp_namer  r  r  r  r  expr2Zsymbolic_equivalencesr  Zexpr3rh   r   ri   solve  s    




(
2zDimConstraints.solve)r  ra   c                 C  sr   |j \}}t|tjrR|j \}}t|tjr8t|tjpNt|tjoNt|tj}nt|tj}|olt|tj}|S rb   )r`   r   r   r_  r;  r   )rZ  r  r{  r  r:  r>  r^   rh   rh   ri   r  H  s    

z'DimConstraints._is_supported_congruencezdict[str, sympy.Expr]c                   s.   dddfdd  fddj  D S )zGReturns a dictionary of the names of symbols to their specialized valuer&   r   rQ  c                   s.   |   } jjr& jj|  d| S |S d S )N = )r  r  r  )rR  r  r   rh   ri   
debug_name\  s    z9DimConstraints.forced_specializations.<locals>.debug_namec                   s0   i | ](\}}|j v r jj| d  |qS )r   )r  r  r  )r   r  r   r  re   rh   ri   r$  c  s   
z9DimConstraints.forced_specializations.<locals>.<dictcomp>)r  r%  r   rh   r  ri   forced_specializationsY  s    z%DimConstraints.forced_specializationsr   z2TypeGuard[torch.export.dynamic_shapes._DerivedDim])r   ra   c                 C  s   t |tjjjS rb   )r   r   exportdynamic_shapes_DerivedDimre   r   rh   rh   ri   _is_derived_dimi  s    zDimConstraints._is_derived_dimz*TypeGuard[torch.export.dynamic_shapes.Dim]c                 C  s"   t |tjjjo t |tjjj S rb   )r   r   r  r  Dimr  r  rh   rh   ri   _is_dimn  s    
zDimConstraints._is_dimzdict[str, dict[str, Any]]dict[str, Any])resultsname_to_dimra   c              	     s  ddl m} dddd fdd}i }t| D ]\}}d	|v r2t|d	 tjr2tt|d	 j	}t
||vr2||t
|< tjj|d	 |\}	}
|d
d}t||
 |	 }|dt}t||
 |	 }||d|t
|< |t
|||d|t
|< |d
d |dd q2| D ]}t| D ]p\}}d	|v r.t|d	 tjr.t
tt|d	 j	 }|kr.|t
| d	 }|d	 ||i}||d	< q.qt }| D ]\}}||vrȐq || rd
|v sd|v r|| nJd	|v rt|d	 tjrtt|d	 j	}|dus4J |t
| q||}i }|D ](}d}||v r|| }d
|v sd|v st|d	 tr|||| s|||< d}|rX| D ]\}}||vrؐq|| }|jjdkr|jj|krd
|v sd|v rt|}tt|j	}tt ||d
 |d tt ||d |d d}|||| s|||<  qXqqXt|! D ]4}||vrq "|| s||v r||= q|#| 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   )r  zMapping[str, int]r   r   )cr   ra   c                   sX     |oVd| v sd| v oV|jdk r4| dddksF|j| ddkoV|j| dtkS )Nr  r  r,  )r  r  r  r  rE   )r   r   r   rh   ri   _check_same_range  s    
*zDDimConstraints._process_derived_dim_roots.<locals>._check_same_rangerC  r  r,  r  )r  r  NTFr  r   )$torch.export.dynamic_shapesr  r   r%  r   r   r  r  r  r   r   r  r   r  r  r  ceilrE   floorr  r  subsr  r  r`  r  r   rg   rj   rL  r  rJ   r9  ru  r  r  )re   r  r  r  r!  Zintroduced_rootsr!  r   rL  r  r  Zc_minmin_Zc_maxmax_Zold_rootr  Znew_root_exprr  Zmodified_rootsZmodified_root_valuesZmrootZswapped_rootr   r   r  r)  rh   r   ri   _process_derived_dim_rootss  s    .
$








z)DimConstraints._process_derived_dim_rootszinspect.Signaturez,Union[dict[str, Any], tuple[Any], list[Any]]dict[str, str]r   )original_signaturer  constraint_violation_errorr  ra   c                   s  ddl m} jjsdS d/ddddfdd	tt|d
u rDi }ddddd}dddddfdd}||}jjD ]}	|	}
|
|	krqt	
d|
\}}}| }|dkr||krq| r|||t| q| r||||t| q|dksJ |
zt|| d< W q ty8   Y q0 q fddt  dd dD  d} rt } D ]@}||
dd  }|r||jj n||j qr|ddt| d7 }  D ] \}	}|d|	 d | d!7 }qֈ| g }g }fd"dt fd#ddD }| D ]"\}}d|v r|d }t|trx|| d|  nt|r\|| d|  n|d$d
}|d%krd
}|d&d
}|d
ur|d
ur|| d'| d(| d)| d* nf|d
ur|| d'| d(| d* n>|d
urF|| d'| d+| d* n|| d'| d, q:|sl|r|d-7 }|d.|| 7 }|S )0z/Format a message for constraint violation errosr   )_get_dim_name_mappingr  Fr   r   )r  inversera   c                   s6    j j D ]$\}}|s$| ||n
| ||} q| S rb   )r  r  r%  r  )r  r-  r!  r"  r   rh   ri   	transform7  s    z2DimConstraints.prettify_results.<locals>.transformN)r.  ra   c                 S  s@   | dkrdS | dkrdS | dkr$dS | dkr0dS | dks<J | S )N<=>=<>==rh   )r.  rh   rh   ri   flip@  s    z-DimConstraints.prettify_results.<locals>.flipr   r_   )r   r.  digitra   c                   s|   |dkr| |  d< nb|dkr0|d  |  d< nH|dkrF| |  d< n2|dkr`|d  |  d< n|dkslJ | |  d	< d S )
Nr/  r  r1  r   r0  r  r2  r3  rC  rh   )r   r.  r5  r  rh   ri   relation_with_digitL  s    z<DimConstraints.prettify_results.<locals>.relation_with_digitz( == | <= | >= | < | > )r3  rC  c                   s   i | ]}| | qS rh   rh   r   r!  )r  rh   ri   r$  p  s   z3DimConstraints.prettify_results.<locals>.<dictcomp>c                 S  s   |  dd S )Nr  r   )splitr  rh   rh   ri   r  t  r   z1DimConstraints.prettify_results.<locals>.<lambda>r  r  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                   s   i | ]}| | qS rh   rh   r8  r6  rh   ri   r$    s   c                   s    | ddS )NT)r-  rh   r  )r.  rh   ri   r    r   r  r,  r  z = Dim('z', min=z, max=r  z', max=z')z
Suggested fixes:
  
  )F)r"  r,  r  r  r   dictr  r  r  r  r9  stripisdigitr   r   r  	TypeErrorr  ru  r  r  r`  rL  rj   r	  r%  r(  r   r   r[  r  )re   r*  r  r+  r  r,  r4  r7  r  r  ri  leftr.  rightbufdebug_namesr!  r   r   dimsr  Zresults2r   r   r&  r'  rh   )r  r  re   r.  ri   prettify_results)  s    





$

zDimConstraints.prettify_resultsN)rj   rk   rl   r   rd   r  r  r  r`  r  r  r
  r  classmethodr  r  r  r  r(  rF  rh   rh   rh   ri   r  
  s$   =M
/	0[ 7r  c                   @  sR   e Zd ZU dZded< ded< ded< ded< ded< ded< ded	< ded
< dS )ShapeEnvSettingsz
    Encapsulates all shape env settings that could potentially affect
    FakeTensor dispatch. Used when creating dispatch cache keys.
    r   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_assertstrace_assertsNr  rh   rh   rh   ri   rH    s   
rH  c                   @  s"   e Zd ZU dZded< ded< dS )r   zG
    Locations of the guards that triggered lower and upper bound.
    r%   r9  r:  Nr  rh   rh   rh   ri   r     s   
r   ru   Iterator[None])r  ra   c                 c  s*   |    zd V  W |   n
|   0 d S rb   )_suppress_guards_enter_suppress_guards_exitr#  rh   rh   ri   _suppress_guards  s    rT  c                   @  s>   e Zd ZU dZded< eedZded< eedZded< dS )	_FrameLocalResultNOptional[str]loc)default_factoryr  localsr)  r  )	rj   rk   rl   rW  rm   r   r=  rY  r  rh   rh   rh   ri   rU    s   
rU  c                   @  s
  e Zd ZU dddddddddd	Zd
d
dd
dddddd	ddddddddddd
ddZeddddZeddddZeddddZeddddZ	eddddZ
eddddZedddd Zed!d"d#d$d%d&Zd dd'd(d)Zddd*d+Zd,dd-d.Zed#dd/d0Ze d1d2dd3d4d5Ze d1d,dd6d7d8Ze d1d1dd3d9d:Ze d1d,dd;d<d=Ze dd1d>d>dd?d@dAZe d2d,d,dd?dBdCZe dDdDddEdFdGZdddHdIZe dddJdKdLZed#ddMdNZe dddOdPZe dddQdRZd!dSdTdUdVZ d1dWddXdYdZZ!d[dd\d]d^Z"d[dd\d_d`Z#dddadbZ$e dcdddedfdgdhZ%d1dWdidXdjdkZ&diddldmdnZ'doddldpdqZ(e)dddrdsZ*e dddtduZ+e dddvdwZ,dxddydzZ-d{dd|d}Z.ddd~dZ/dd!dddddZ0dd!dddddZ1dddd!dddddZ2dddddZ3e ddddddd!dddddZ4d!ddddddddd	ddZ5e ddd2d>dddddZ6e ddd2d>dddddZ7e d,d!dddddZ8d2ddddZ9ddd1dddddddZ:e ddddZ;e dddDdTddZ<d1ddddZ=e ddddZ>e e?j@ddfdd!dddd2dddÄZAe e?j@dd
ddfd,d!dddddd2dĜddƄZBd1d,ddǜddɄZCd!ddTdd˄ZDd!ddd͜ddτZEddddќddӄZFddՄ fddddd
ddלddddddddddd
ddZGdd
ddddddddZHdddddZIdddddZJddddddZKd
ddddddddZLdddddZMddddd dZNddddZOddddddZPdd2dddd	d
ZQeRdddddddZSeTdd[ddddZUeRddddddddddddddddZVeRddd\ddZWeRdddd ZXeRdddddd!d"ZYeTd#dd$dddd%d&d'ZZeTd#d2dd\d(d)Z[ddd*dddd>d+d,d-d.Z\ddd/d1dd0ddd1d2d3Z]d1d2ddd4d5d6Z^d2dd\d7d8Z_eRe d1d2d9d:d;Z`eTd#d2dd\d<d=Zadddd>d?d@ZbdddAdBZceRd2d2d\dCdDZdddddEdFdGZedHddIdJZfddddKdLdMdNZgdddOdPdQdRZhdd,dSdTdUZidVddWdXZjdd[dddYdZd[ZkdZld>emd\< dd]dddd^d_d`ZndddadbZoddddcdddeZpdddfddgdidddddhdidjZqeTd#ed
dkdldddgdiddddddmdndoZrddddpdqdrZsdddfddsdidddddhdtduZtdddvdwZueTd#ed
dxdd[ddiddydzd{Zvd[dd\d|d}ZweTdd~e d1d,d,ddddZxdS (  ru   N)should_record_eventstracked_fakesrY  zOptional[list[Any]]r   r_   )rZ  r[  r  ra   c                K  s   | j f i | d|d< ddlm} | | _|d ur8|n| joDtj | _| joRtj| _d| _	|| _
| jrvtt|dgng | _i | _d S )NFrZ  r   translation_validation_enabled)r  )_inittorch.fx.experimental.validatorr]  _translation_validation_enabledr  Z translation_validation_no_bisectrZ  Zcheck_shape_env_recorded_eventscheck_recorded_eventsis_recordingr[  r3   ru   eventsfake_tensor_cache)re   rZ  r[  r  r]  rh   rh   ri   rd     s&    
zShapeEnv.__init__TF)	rI  rJ  rK  rL  rM  	co_fieldsrN  rO  rP  r   zOptional[dict[str, str]])
rI  rJ  rK  rL  rM  re  rN  rO  rP  ra   c       	      
   C  s  |d u rt j}t||||||||	d| _g | _i | _t | _i | _i | _	i | _
i | _i | _i | _i | _i | _i | _i | _i | _i | _t | _t | _i | _t | _t | _i | _d| _t| _| jd d| _d| _ d | _!t"# | _$t"# | _%|r|ni | _&g | _'| ( | _)d| _*d| _+i | _,i | _-i | _.i | _/i | _0|	| _1t2 | _3ddl4m5}
 |
 | _6| j6rddl4m7} | | _8t9j:; | _<| j<=| j<>d  i | _?d S )N)rI  rJ  rK  rL  rM  rN  rO  rP  r   Z
create_envFTr\  )TranslationValidator)@r  Zuse_duck_shaperH  settingsr   axiomsr  
unique_idsr  unbacked_var_to_valoblivious_var_to_valr<  var_to_range_slocr  r  var_to_stacksource_to_varreplacementsreplacements_slocsr  	divisible	size_like
val_to_varrr  r  unbacked_symfloat_counterunbacked_symint_counterdeferred_runtime_assertsnum_deferred_runtime_assertsr   r2  r  runtime_asserts_frozendim_constraintscollectionsr   countersymbol_guard_counterre  r  r  _prev_cache_keyr  _resimplify_floor_div_axiomsfx_node_cacher  Zunbacked_alloc_orderuser_specialization_stacksframework_specialization_stacksrP  r7   specializationsr_  r]  r`  rf  	validatorr   r   ZGraphr  Zinserting_beforeoutputname_to_node)re   rI  rJ  rK  rL  rM  re  rN  rO  rP  r]  rf  rh   rh   ri   r^  -  s|    *





zShapeEnv._initr   c                 C  s   | j jS rb   )rg  rI  r   rh   rh   ri   rI     s    zShapeEnv.allow_scalar_outputsc                 C  s   | j jS rb   )rg  rJ  r   rh   rh   ri   rJ  $  s    z'ShapeEnv.allow_dynamic_output_shape_opsc                 C  s   | j jS rb   )rg  rK  r   rh   rh   ri   rK  (  s    z!ShapeEnv.assume_static_by_defaultc                 C  s   | j jS rb   )rg  rL  r   rh   rh   ri   rL  ,  s    zShapeEnv.specialize_zero_onec                 C  s   | j jS rb   )rg  rM  r   rh   rh   ri   rM  0  s    zShapeEnv.duck_shapec                 C  s   | j jS rb   )rg  rN  r   rh   rh   ri   rN  4  s    z4ShapeEnv.prefer_deferred_runtime_asserts_over_guardsc                 C  s   | j jS rb   )rg  rO  r   rh   rh   ri   rO  8  s    z0ShapeEnv.allow_complex_guards_as_runtime_assertsr&   z$Callable[[sympy.Symbol], sympy.Expr]rQ  )r  r  ra   c           
      c  s.  |  }| j| }|tt|| tdjj}t| | 	|}i }|D ]R}t
|tjrHt
|jtjrHt
|jtjrH|j| jvrH|j| j|j< |j||j< qH| j| d| _z@dV  W |D ]}	| j|	d q|D ]}	| j|	d qd| _n:|D ]}	| j|	d q|D ]}	| j|	d qd| _0 dS )a  
        Temporarily add symbol-level axioms to the ShapeEnv. This is useful when you want to "fork"
        and have parallel universes of ShapeEnvs. For example, we use this when doing multi-graph
        compile so we can support various graphs with varying levels of specializations.

        This context manager allows for temporarily adding constraints to the shape environment
        based on a specialization function applied to a symbol associated with a source.

        Args:
            source: The source of the symbol to specialize
            check_fn: A function that takes a sympy Symbol and returns a sympy expression
                     representing a constraint/specialization to be applied
        NTF)r  rn  r#   r4   r   r   r  r=  get_implicationsr  r   r   r9  r:  r;  r>  r   ro  rh  r  r  r  )
re   r  r  r  symr   Z
new_axiomsZadded_replacementsZaxiomr!  rh   rh   ri   patch_source_specialization<  s<    


z$ShapeEnv.patch_source_specializationr   c                 C  s(   d}dddddd}t | ||| dS )z(Compare another ShapeEnv for equivalence)r{  r   rm  r  r  r  ra  rZ  rb  r[  rc  r  r}  r  ry  rl  rp  r~  _expr_sym_node_idr  r  r   r   )r  valuera   c                 S  st   | dv r ddl m } t||S | dkr6dd |D S | dkrPdd	 | D S | d
krdt| S | dv rpd S |S )N)rt  ru  r   )re  r   c                 S  s   g | ]
}|j qS rh   r  r   grh   rh   ri   r    r   z;ShapeEnv.check_equal.<locals>.map_value.<locals>.<listcomp>rv  c                 S  s    i | ]\}}|d d |D qS )c                 S  s   g | ]
}|j qS rh   r  )r   rarh   rh   ri   r    r   zFShapeEnv.check_equal.<locals>.map_value.<locals>.<dictcomp>.<listcomp>rh   )r   r  rasrh   rh   ri   r$    r   z;ShapeEnv.check_equal.<locals>.map_value.<locals>.<dictcomp>r  )r|  r  rd  )re  r  r%  r  ru  )r  r  re  rh   rh   ri   	map_value  s    z'ShapeEnv.check_equal.<locals>.map_valueN)r2   )re   r   Znon_state_variable_namesr  rh   rh   ri   check_equalj  s     zShapeEnv.check_equalc                   sB   | j d u rd S ddlm  ddd fddfdd| j D S )	Nr   r   r   )ro  ra   c                   s6   t | jtjtjfr| jn
t| j} || j| jS rb   )	r   ro  r   r#   r"   r/   Z	from_faker  rX  )ro  Z
inner_faker  rh   ri   maybe_transform_fake  s
    
	z>ShapeEnv._snapshot_tracked_fakes.<locals>.maybe_transform_fakec                   s   g | ]} |qS rh   rh   )r   ro  )r  rh   ri   r    r   z4ShapeEnv._snapshot_tracked_fakes.<locals>.<listcomp>)r[  Ztorch._dynamo.variables.builderr   r   rh   )r   r  ri   _snapshot_tracked_fakes  s
    
z ShapeEnv._snapshot_tracked_fakesr   c                 C  s   t | jd S Nr   )r6  rc  r   rh   rh   ri   _last_event_index  s    zShapeEnv._last_event_indexc                 c  s$   d| _ zd V  W d| _ nd| _ 0 d S NTF)rb  r   rh   rh   ri   
_recording  s    zShapeEnv._recordingr  r   )orig_sr  ra   c                 C  s   |  ||d d S )NZeliminate_unbacked)_set_replacement)re   r  r  rh   rh   ri   r    s    zShapeEnv._eliminate_unbacked)r!  r"  ra   c                 C  s"   t d|| t|| 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.zset_unbacked_var_to_val %s = %sN)r   r2  r   r  rj  )re   r!  r"  rh   rh   ri   r    s    z ShapeEnv.set_unbacked_var_to_valc                 C  s   t |tjsJ |t |tjs(J |t|s8J |t|sHJ || j|}|d urvt|rvJ | d| | ||d || j|< |d ur| ||d d S )Nz -> Zrename_unbacked_toZrename_unbacked_to_dest)r   r   r;  r  ro  r  r  r  )re   r  r  destrh   rh   ri   r@    s    
zShapeEnv._rename_unbacked_tor  c                 C  s   d S rb   rh   )re   r   r   rh   rh   ri   r    s    zShapeEnv._constrain_is_boundedr   r  c                 C  sH   |d u rd}|d u rt }||k r(td| j|||d | j| d S )Nr   r  compiler_mincompiler_max)rE   rc  constrain_symbol_rangerr  r`  re   r   r  r  rh   rh   ri   r    s    z"ShapeEnv._constrain_range_for_sizec                 C  sj   t |tjrJ|t|  kr$|ksFn tdt| d| d| dd S t |tjrf| j|||d d S )Nr	  r
  r  r  r  )r   r   r   r   rP   r;  r  r  rh   rh   ri   r    s     zShapeEnv._constrain_ranger#   r  c                 C  s   t |ts`t |ts"||ks^J qt |jjtjs:J d|jj| u sJJ t|| j|jj< nt |jjtjsxJ d|jj| u sJ t |tst|| j|jj< nH|jj|jju sJ t |jjtjsJ 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!)
        zconstraining non-Symbols NYIN)
r   r#   r   r   r   r;  r  r   ro  rH  )re   r   r  new_varrh   rh   ri   r    s$    


zShapeEnv._constrain_unifyc                 C  s   t tddS )Nignore_fresh_unbacked_symbolsFr  TLSr   rh   rh   ri   "_ignore_fresh_unbacked_symbols_tls9  s    z+ShapeEnv._ignore_fresh_unbacked_symbols_tlsr  c                 C  s   |   }|t_|S rb   )r  r  r  )re   r  prevrh   rh   ri   "_ignore_fresh_unbacked_symbols_set<  s    z+ShapeEnv._ignore_fresh_unbacked_symbols_setc              	   c  s0   |  d}zdV  W |  | n|  | 0 dS )za
        Indicates that the newly allocated unbacked SymInts are being
        discarded
        TN)r  )re   r  rh   rh   ri   r  B  s    
z&ShapeEnv.ignore_fresh_unbacked_symbolsc                 C  s
   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   rh   rh   ri   freezeN  s    zShapeEnv.freezec                 C  s
   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)rx  r   rh   rh   ri   freeze_runtime_assertsW  s    
zShapeEnv.freeze_runtime_assertsr  r7  c                 C  s:   | j s
d S | }|| jvr0tj|dd| j|< | j| S )NTr  )r`  r  r  r   r;  )re   r  srcnamerh   rh   ri   _create_symbol_for_sourcec  s    
z"ShapeEnv._create_symbol_for_sourcer   )r  r   ra   c                 C  s   | j r| j|| d S rb   )r`  r  Zadd_var)re   r  r   rh   rh   ri   
_add_z3vark  s    zShapeEnv._add_z3varr   r  c                 C  s   | j r| j| d S rb   )r`  r  Zadd_target_exprr  rh   rh   ri   _add_target_expro  s    zShapeEnv._add_target_exprc                 C  s   | j r| j| d S rb   )r`  r  Zadd_assertionr  rh   rh   ri   _add_assertions  s    zShapeEnv._add_assertionc                 C  s   | j r| j  d S rb   )r`  r  validater   rh   rh   ri   _check_translation_validatew  s    z$ShapeEnv._check_translation_validater   r7  z$tuple[Optional[torch.fx.Node], bool])r.  r`   ra   c                 C  s   ||f}d}| j r|| jvrtdd |D rLtdd |D sDJ d |fS d}tdd |D sxJ d|j d| | j|| }| j|< || j|j< | j	|d |fS )	NFc                 s  s   | ]}|d u V  qd S rb   rh   r   r   rh   rh   ri   r    r   z4ShapeEnv._create_fx_call_function.<locals>.<genexpr>c                 s  s   | ]}t |tjj V  qd S rb   )r   r   r   rK  r  rh   rh   ri   r    r   Tc                 s  s   | ]}|d uV  qd S rb   rh   r  rh   rh   ri   r    r   zmissing arg in FX graph (z): )
r`  r  rx  r  rj   r  rJ  r  r  r  )re   r.  r`   node_keyfreshr   rh   rh   ri   _create_fx_call_function{  s    z!ShapeEnv._create_fx_call_functionzOptional[torch.fx.Node]c              	   C  s~   | j s
d S | jj|ff}|| jvrt| || tddtdd|j}| j| }| 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/  )re   r  r   r  r  r   rh   rh   ri    _create_fx_placeholder_and_z3var  s    

z)ShapeEnv._create_fx_placeholder_and_z3varrD  c                 C  s,   | j r(|d ur(| j|j | j| d S rb   )r`  r  r  r  r  Z
erase_node)re   r   rh   rh   ri   _remove_fx_node  s    zShapeEnv._remove_fx_noder'  c                 C  s0   ddl m} | jr,|  |jt< | |jt< d S )Nr   )get_current_node)Ztorch._dynamo.utilsr  rZ  r  r/  r   r   )re   r   r  rh   rh   ri   _add_fx_node_metadata  s    zShapeEnv._add_fx_node_metadatac                   C  s   t tddS )Nsuppress_guardsFr  rh   rh   rh   ri   _suppress_guards_tls  s    zShapeEnv._suppress_guards_tlsc                 C  s.   t tdsg t_|  }tj| dt_d S )Nsuppress_guards_stackT)r  r  r  r  r   r  re   r  rh   rh   ri   rR    s
    
zShapeEnv._suppress_guards_enterc                 C  s&   t tjdkrtj nd}|t_d S )Nr   F)r6  r  r  r  r  r  rh   rh   ri   rS    s
    zShapeEnv._suppress_guards_exitz_GeneratorContextManager[None]c                 C  s   t | S )z5Context manager to ignore all guards generated inside)rT  r   rh   rh   ri   r    s    zShapeEnv.suppress_guardsztuple[int, int, int, int]c                 C  s    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
        )r6  ro  rq  rw  rj  r   rh   rh   ri   r    s
    zShapeEnv._get_keyc                 C  sD   t | j| jd krd| _|  }| j|kr@|| _|  jd7  _d S )Nr   T)r6  rq  r}  r~  r  r  )re   Zcur_keyrh   rh   ri   _update_version_counter  s    
z ShapeEnv._update_version_counterzSequence[IntLikeType]r   rX  )ex_sizer  rX  ra   c                 C  s   |  t|||S rb   )!_produce_dyn_sizes_from_int_tupler7  )re   r  r  rX  rh   rh   ri   _produce_dyn_sizes  s    
zShapeEnv._produce_dyn_sizes)tensor_sizer  rX  ra   c              	   C  s   t dd |D s J d| ddlm}m} t| |j}|j}g }t|D ]\}	}
| j|
|||j	|	||	 ||	 t
j|d}t|tr|jr|j|	 D ] }| jt|||j	|	| qt
jrt|tjrt|tj	r| j| || qP|S )Nc                 s  s   | ]}t | V  qd S rb   )r  )r   r   rh   rh   ri   r  
  r   z=ShapeEnv._produce_dyn_sizes_from_int_tuple.<locals>.<genexpr>z0Expect size to be a plain tuple of ints but got r   TensorPropertyrV   )do_not_specialize_zero_onerX  )r  r  r  rV   rZ  re  rg  r  create_symbolSIZEr  r  r   r   ri  r  r`  r   r   r;  rL   rM   rr  r   )re   r  r  rX  r  rV   dynamic_dimsZconstraint_dimsrF  r  r   r  Zspecializationrh   rh   ri   r    sJ    	

z*ShapeEnv._produce_dyn_sizes_from_int_tuplerY  r   rj  zDtuple[tuple[IntLikeType, ...], tuple[IntLikeType, ...], IntLikeType])exr  rX  ra   c                  sn   t fdd  D }t fdd  D }  }j||| fddt  D ||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  s   | ]}  |V  qd S rb   #_maybe_specialize_sym_int_with_hint)r   szr   rh   ri   r  A  s   zHShapeEnv.create_symbolic_sizes_strides_storage_offset.<locals>.<genexpr>c                 3  s   | ]}  |V  qd S rb   r  )r   sdr   rh   ri   r  D  s   c                   s   g | ]}t  |qS rh   )_is_dim_dynamicr   r  )r  rh   ri   r  O  r   zIShapeEnv.create_symbolic_sizes_strides_storage_offset.<locals>.<listcomp>rY  )r7  rF  rG  r  rH  -_create_symbolic_sizes_strides_storage_offsetr  r   )re   r  r  rX  r  	ex_strideex_storage_offsetrh   )r  re   ri   ,create_symbolic_sizes_strides_storage_offset0  s"    z5ShapeEnv.create_symbolic_sizes_strides_storage_offsetr[   )	maybe_symra   c                 C  s>   t |ttjfsJ t|r:|jj| us0J d|j S |S )NzFexpect the symbol is created from an shape env other than current one.)r   r   r   r#   r  r   r  r   )re   r  rh   rh   ri   r  v  s    
z,ShapeEnv._maybe_specialize_sym_int_with_hintzSequence[bool])r  r  r  is_dim_dynamicr  rX  ra   c             
     s:  t |}|d u rd g| }d g| }	g }
g }t|D ]<}|| rHtj}njrVtj}ntj}|
| || q4tjg| }
tjg| }t	|
|||	d}t
| |j}|j}	|j}|j}tdd |D rtjntj}tdd |D }t ||ksJ t | d| t ||ks2J t | d| t ||ksDJ t |	|ksVJ ddlm m ||}|||||	||} fdd	tt||D }g }t|D ]<\}}|d usJ |j|||  j|d
 qjj| j|d |d| jd
}t|t||fS )Nre  rf  rg  rh  c                 s  s   | ]}|t jkV  qd S rb   r(  r.  r   r  rh   rh   ri   r    r   zIShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>.<genexpr>c                 s  s   | ]}|t jkV  qd S rb   r  r  rh   rh   ri   r    r   r  r   r  c              	     s.   g | ]&\}\}}j || j|d qS )r  )r  r  )r   r  r  r4  r  rV   re   r  rh   ri   r    s   
zJShapeEnv._create_symbolic_sizes_strides_storage_offset.<locals>.<listcomp>r  dynamic_dimconstraint_dimrX  )r6  r  r(  r,  rK  r.  r-  r   r0  r   rZ  rg  rh  re  rf  r  r  r  rV   r  _compute_symbolic_strider  r  r  STRIDEr  STORAGE_OFFSETr7  )re   r  r  r  r  r  rX  r   rg  rh  r  rf  r  r  re  Zdynamic_offsetare_sizes_staticrF  rG  Z	sym_sizesrO  Zstride_exprrP  rh   r  ri   r    s    



$$

z6ShapeEnv._create_symbolic_sizes_strides_storage_offsetzSequence[sympy.Expr]zSequence[DimDynamic]zJSequence[Optional[Union[StrictMinMaxConstraint, RelaxedUnspecConstraint]]])	r  rF  r  r  rf  rh  r  rX  ra   c	                 C  s<  ddl m}	m}
 d gt| }i }dd t|D }|jtd |D ]\}}| }|t|d ko|| ||d  ||d   k}|dv r|st|}nd|| }|t	j
kr||v r|| }n@|}|t	j
kr|rt	jnt	j}| j||
||	j|||| |d}|||< || | ||| | < qDtd	d
 |D s8J |S )Nr   r  c                 S  s   g | ]\}}|| fqS rh   rh   )r   r  r   rh   rh   ri   r    r   z5ShapeEnv._compute_symbolic_stride.<locals>.<listcomp>r  r   r   r   r  c                 s  s   | ]}|d uV  qd S rb   rh   r  rh   rh   ri   r  /  r   z4ShapeEnv._compute_symbolic_stride.<locals>.<genexpr>)r  r  rV   r6  r  r[  r   r   r   r(  r0  r.  r-  r  r  r  )re   r  rF  r  r  rf  rh  r  rX  r  rV   rG  
candidatesval_listr   Zneg_ir  Zcontiguous_strideZ
out_strideZdynamic_strideZ
dyn_striderh   rh   ri   r    s<    

z!ShapeEnv._compute_symbolic_stride)r  zOptional[Source])r  r4  r  ra   c                C  s   | j rD|durD| |}|dus$J | |t}| t|| nd}t|tjrv|durlt||kslJ t|}n"t	|rd}t
t|| t||d}|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   r9  r   r   r  r#   r4   re   r  r4  r  r  r  r  rh   rh   ri   r  2  s    

zShapeEnv.create_symintnoderZ   c                C  s   | j rD|durD| |}|dus$J | |t}| t|| nd}t|tjrv|durlt||kslJ t|}n.t	|r|du sJ |t
t|| t||d}|S )z2Create a SymFloat value from a symbolic expressionNr  )r`  r  r  r  r  r   r9  r   r  r  r"   r4   r  rh   rh   ri   create_symfloatnode^  s    	

zShapeEnv.create_symfloatnoder(  )r  r  r  ra   c                 C  s   | j | j|||d||dS )z1Create a SymInt wrapping a new unspecified symbol)r  r  r  )r  create_unspecified_symbol)re   r  r  r  rh   rh   ri   $create_unspecified_symint_and_symbol  s    z-ShapeEnv.create_unspecified_symint_and_symbolr!   r  ra   c                 C  s   t t|| tdS )z7Create a SymBool object from a sympy boolean expressionN)r!   r4   r   re   r  rh   rh   ri   create_symboolnode  s    zShapeEnv.create_symboolnoder   rQ   zOptional[SymNode])prefixr  r6  r  r   ra   c           	   
     s~   t jd uott jdv }|d u r6| |\}}n| d }}tjd|jj	|||d t
d fddd d S )	N,r  z%s %s [%s, %s] %s%s
stack_infocreate_unbacked_symbolc                     s4   t t dj dj dtdt dS )N[r:  r  r)  )r  Znode_idr6  
user_stackr  )r   idr9  r:  r*   get_user_stackget_framework_stackrh   r   r  r6  rh   ri   r    s    z6ShapeEnv._log_create_unbacked_symbol.<locals>.<lambda>Zmetadata_fn)r  extended_debug_create_symbolr   r9  _get_stack_summaryr  r   r2  r9  r:  r+   )	re   r  r  r6  r  r   is_debugslocmaybe_extra_debugrh   r  ri   _log_create_unbacked_symbol  s,    

z$ShapeEnv._log_create_unbacked_symbolr"   c                 C  s   t tjt| j}| jd  d7  < |  s8| j| t	j
dd| j|< t  }| j|< |jsfJ |  }t||| j|< | |t}t|| td|d}| jd|||d t|S )z,Create a symbolic float without a hint valuer  r   r  Nr  create_unbacked_symfloatr   )rK   rM   r  r  rt  r{  r  r  r   rR   extractrm  rQ   unknownr<  is_float	_get_slocr   rl  r  r  r4   r  r"   re   r  r6  r  r  r   rh   rh   ri   r    s"    
z!ShapeEnv.create_unbacked_symfloatc                 C  s   t tjt| jdd}|  s*| j| | jd  d7  < t	j
dd| j|< |   }| j|< |jsjJ |  }t||| j|< | |t}t|| td|d}| jd||||d	 t|S )
z.Create a symbolic integer without a hint valueTr  r  r   r  Nr  create_unbacked_symintr  )rK   rM   r  r  ru  r  r  r   r{  rR   r   rm   _default_unspecified_value_ranger<  r  r  r   rl  r  r   r4   r  r#   )re   r  r  r6  r  r  r   rh   rh   ri   r    s"    

zShapeEnv.create_unbacked_symint)r  ra   c                 C  s   t |tjS )zJCheck if a sympy symbol matches the naming convention for unbacked symbolsrL   rM   r  )re   r  rh   rh   ri   r    s    zShapeEnv.is_unbacked_symintc                 C  s   t tjt| jdd}|  s*| j| | jd  d7  < t	j
dd| j|< tdd }| j|< |jslJ | d}t||| j|< | |t}tt|d| td|d	}| jd
|||d t|S )z.Create a symbolic boolean without a hint valueTr  r  r   r  r   z(default value range for unbacked SymBoolNr  create_unbacked_symboolr  )rK   rM   r  r  ru  r  r  r   r{  rR   r   rm  rQ   r<  r  r  r   rl  r  r   r4   r   r9  r  r!   r  rh   rh   ri   r    s"    

z ShapeEnv.create_unbacked_symboolz#Union[int, SymInt, float, SymFloat]ra  z"Optional[StatelessSymbolicContext])r   r  r  r  rX  ra   c              	   C  s   | j ||||dd|dS )z
        Create 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)r  r  rX  )r  )re   r   r  r  r  rX  rh   rh   ri   r    s    z"ShapeEnv.create_unspecified_symbol)r   r  r  r  r  r  rX  ra   c                   s  t |tr|jj|jjkrtj}|jjkrNtd|jj d d  |rddl	m
} t |slJ jduszJ ||jj< d|jj< d} }	t |trt| |jvri |jt| < t |tr|	r|	|jt|  v r|jt|  |	 S |tjtjfv r^| jj}
| |
 t |trD|	rD|
|jt|  |	< |tju rZ| j|
< |
S |rjd}n| j}t tsJ t d |rdk rJ d	 |durtj}|tju rt}
t |tr|	r|
|jt|  |	< |
S |tju r| j}n |tju r$d}nt d
| | ! }dv rh|rhdkr\tj"j#S tj"j$S n|rz| j%vrH| & }tt'u st(rt)t*j+||ddnt)t*j,||dd| j-|	< t t'rt| j.< n<t t/rt0| j.< nt1j2 j3 d| j.< g | j4< | 5t' |rP| j%< t t'r@|r| 6dk | 7|| j8< t9| !| jrdnd|| j:< n| ; | j8< t9||| j:< t |tr|rJ | j<|jdd | j8 }|j=s J |vr(t d|j d|j dd|j d|j d n^t t/rt>tj? tj? | j8< }t9||| j:< d|j d|j d |j@sJ nd }tAjBduotCtAjBDdv }d}|stEFdddvrd d}| G|\}}| jHjId  ||||d	 tJd  fd!d"d# | jKd   d7  < n(| j% }|| j-|	< | jHLd$|  t |tjMr| j4| }|N O s|d O r|d% |d  |d< |d%< d| jP|< t |tr|	r||jt|  |	< |S )&z5Create a new symbol which is tracked by this ShapeEnvzStatic shape constraint of z does not match input size of z, for r   rU   NFr  z!positive set for negative value: zunhandled dynamic_dim r  Tr  )r  r  )r  r   zuser code shown is first use of this value--the guard itself is not due user code but due to 0/1 specialization in the framework; to avoid specialization try torch._dynamo.mark_unbacked(tensor, dim)is_constraintz not in range [r:  r  r  r  r  ZTORCHDYNAMO_EXTENDED_ADVICE1)0r  zC, for more info run with TORCHDYNAMO_EXTENDED_DEBUG_CREATE_SYMBOL="zF" or to suppress this message run with TORCHDYNAMO_EXTENDED_ADVICE="0"z&create_symbol %s = %s for %s %s %s%s%sr  r  c                
     s:   t t  tt ttjdd	 dS )Nr   r  )r  r   r6  r  r  r  )
r   r   r  r*   from_tracebackr'   extract_stackrR   r   summaryrh   Z	range_strr  Z
sympy_exprr   rh   ri   r    s    z(ShapeEnv.create_symbol.<locals>.<lambda>r  zcreate_symbol %s duck sized %sr   )Qr   r5  r6  r9  r:  r(  r.  r   r  r  rV   r  re  rg  r   r  rm  r/  r1  r  r   r   r  rk  rL  r&   r   r,  r   r   r-  rM  r  r  r   rv  r  rs  _generate_unique_idr   r   rK   rM   r  FLOATrn  r  r  r  rI   Z
nested_intr   r  r  r  _default_value_ranger<  r   rl  r  _update_var_to_ranger  rQ   oor  r  r  r   r9  osgetenvr  r   r2  r+   r{  r   r;  r   is_ephemeralr|  )re   r   r  r  r  r  r  rX  rV   r  r  rL  Zduckr  Z	symbol_idr6  r  r  maybe_more_infor  Z	r_sourcesrh   r  ri   r  *  sV   

"

















zShapeEnv.create_symbol)r   r   ra   c                 C  s>   t jd||dd || jvs*J | dt|| j|< dS )z.Adds a new symbol to the symbolic environment.zadd_var_to_val %s %sTr  z already existsN)r   r   r  r   r   )re   r   r   rh   rh   ri   add_var_to_val+  s    zShapeEnv.add_var_to_valc                 C  s   |  }| j||S rb   )r  r  r  )re   r  src_namerh   rh   ri   _debug_name1  s    zShapeEnv._debug_namez6Union[StrictMinMaxConstraint, RelaxedUnspecConstraint])r  r   ra   c                 C  s   t |tr|jj|jj }}|  }||jkr2d }||jkr@d }| | d|  d}|d ur|d ur|d| d| | d| 7 }nV|d u r|d ur|d| | d| 7 }n*|d ur|d u r|d| d| | 7 }|S ||S )Nr  z in the specified ranger  r8  )	r   r5  r6  r9  r:  r  r  r  r<  )re   r  r   r9  r:  rN  Zc_renderrh   rh   ri   &_render_range_for_constraint_violation5  s"    


"z/ShapeEnv._render_range_for_constraint_violationr   )r`   r  ra   c                 O  s   | j |i |ddid jS )z
        Like produce_guards_verbose, but only returns the non-verbose python guard expressions
        (no verbose guards produced.)
        langs)pythonr   )produce_guards_verboser  r  rh   rh   ri   produce_guardsK  s    zShapeEnv.produce_guardsc                 C  s   |   S rb   r  r  rh   rh   ri   r  V  r   zShapeEnv.<lambda>r  verbose_python)r   input_contextsequalities_inputs_simplifiedr#  r  zSequence[FakeTensor]zSequence[Source]r  zOptional[list[ShapeGuard]]z"Optional[DimList[SymbolicContext]]zOptional[EqualityConstraint]ztuple[str, ...]zlist[_ShapeGuardsHelper])
placeholdersr  r  r   r$  r%  r&  r#  r  ra   c          N        sH  j d jr&tj}
|
 tt|ksLJ d d| dtjt	fddddd	|d
u rfddD }ntt|tksJ t
t|D ]P\}\}}t|r|d
u r|||< qt|ttttfsJ t|trJ qddlm m g ttttg g t|j
D ]J}|dv rT n.|dkrtt|j ntd| q:dlddddddfddddd fdd|rfi t
|D ]\}}|| < qddd fd!d"}|jD ]\}}|||| }}t !||}|st"|  d#t|trL|n
|#j$ d$|  d#t|trt|n
|#j$ q|j%D ]\}}}||}t|t j&r|j| d  fn||'|f\}}||}t !||}|st"d%|  d&|t &| d'| d#|#j$ d(|#j$ 
q|j(D ]}| )j|  qJdmdd)d*dd+fd,d-}dd.dd/fd0d1}t||D ] \}}}t|t*rdd2lm+} ||}t|t,sJ |d
u rqt|ttfr4|d
u rd
n
t-|d3d
} ||||  qnt|ttfrR||| qt|sbJ t.|rdd4lm/}! t|t0sJ |||j1|j2fg}"|3 \}#}$|#D ]6}%t-||%}&|j4|% }'|"|!||%|&|'j1|'j2f qn|||j1|j2fg}"|"D ]\}}(})}*t5|(rHt
|(6 D ](\}}+| j7|},||,|+|)|  qnt
|(6 D ](\}}+| j7|},||,|+|)|  qTt
|(8 D ](\}}+| j9|},||,|+|*|  q|| j:|(;  qqd5d 
D t<j$t= j>_?|	sD ]\}| }-j@rD|-jAv rDBt !jA|-  tt j&rtCrt| d krtq|rt|rjDrj Ed6|| d7  q|rƈj?F| t
D ]\}.}/}|/G| d7|/H }0|d8krjIC|- }1d
urz|j|1 d krB|0 d9j$|1  d:}0n6jJC|1 }2d
urh|0 d;|2 }0n|0 d<|1 d=}0n|0 d>|- d=}0|.|0 qt|r|jK j7u r|rtjLd?krtMtNjLtt j&rZv rZ|O| d sZd@'| d#|  dA' d  d# d   dB	}3|jP'||3 tt j&sv r|Q| d fdCdDs d }d@'| d#|  dE'| d#|  dF'| d##t R'|i dG}3|jP'||3 qt 	dHddI	
f
dJdK}4|d
u	rL|njSD ],}5jT|5jUdL|5jVdMd
u	rt	qR|4|5 	qRjWCd
g D ]8}6jT|6jUdLdNd
u	r	qX|6jUj?Y 	qZ D ]\}j[C}7|7d
u 	r	qЈj\ }8|
sJ g }9||d }:dO};|7j]t j^ t_ fv
rt`fdPdQ|D 
r`j?Yt a|7j] |
rx|7j]b j]k
r|9t jc|7j]dRdS |7j] dT|: d;|8j] };|7jdt j^t_fvrHt`fdUdQ|D 
rj?Yt c|7jd |9t jc|7jddRdS |;r0|7j] dT|: dT|7jd d;|8j] dA|8jd 	};n|: dT|7jd d;|8jd };|9r6t je|9dVdRi}<t
D ]2\}.}/}|d8kr|.|; n|.|/H|< qj }=|=D ]}>t|>tfr|>jgb @ h|7s|d }t et c|7j]t c|7jdH}?i||>}@dW|@ dX|? }3|>jP'||3 qtjtkjl	rdYG|d  d}0t
D ]t\}.}/}|d8kr|.|0 dZ nL|d[kr|.|0 n6|dkr|.d\|/G|d  d ntd]| qf	qЈrg }Ag }Bt }CD ]X\}D}}E|Dr.d^t|Ad?  d_|E  }F|A|F n d`|E  }F|B|F |CY| qt|Bdkrdamtn|C}Gdbm|B}Ht"dc|G dd|H nt|Adkrt Edet|A todfdi jpjqtd trdgdQ s D tnjts dhdidj j@rddklumv}I jWs D ]}J|JD ]}6B|6jU qqj[Z D ]Z\}K}L|Lj]t j^ t_ fvrrBt c|Lj]|K |Ljdt j^t_fvr@Bt c|K|Ljd q@twx " |Ijyjz{  W d
   n1 s0    Y  |d
u r|  g }Mt
D ]H\}.}/}|dkr2t|/tsJ |Mt}|.|/jA n|Mt~|. q|MS )na  
        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

        Returns guards in python and python with verbose comments (verbose) by
        default.
        r!  zlen(z	) != len(r  rT   r   rh  c                 S  s<   t tjg|   tjg|   d g|   d g|   dS )Nr  )r   r(  r,  r   r0  rk  rh   rh   ri   _create_no_constraints_context  s    zGShapeEnv.produce_guards_verbose.<locals>._create_no_constraints_contextNc                   s"   g | ]}t | r|nd qS rb   )r   r   ri  )
Tensorliker(  rh   ri   r    s   z3ShapeEnv.produce_guards_verbose.<locals>.<listcomp>r   r  r"  cppzUnknown lang: r   r   zOptional[Callable[[], str]]r_   )r3  r  r  r4  ra   c                   s    | | fddf d S )Nc                     s    r    S S rb   rh   rh   r4  r  rh   ri   r    r   zVShapeEnv.produce_guards_verbose.<locals>.record_constraint_violation.<locals>.<lambda>)r   )r3  r  r  r4  )constraint_violationsr,  ri   record_constraint_violation  s    zDShapeEnv.produce_guards_verbose.<locals>.record_constraint_violationr   zTypeGuard[TensorPropertySource]rQ  c                   s   t | o| j ju S rb   )r   propr  )rR  r  rh   ri   is_dim  s    

z/ShapeEnv.produce_guards_verbose.<locals>.is_dimr&   r   )tensor_dim_srcra   c                   sl    | j    }| jd us J |j| j }t|tjr@|jjS t	|t
u s^J dt	| t|S d S )NzExpected int, got )r{  r  r  r   r   r   r#   r   r   r   r   r   r   )r1  ro  Zsymint)r'  source_indexrh   ri   get_expression  s    z7ShapeEnv.produce_guards_verbose.<locals>.get_expressionr  z is not equal to zExpected input z to be equal to z, where z
, but got r[   ra  )r  r   rb  ra   c                   s  t dt| j|| t|tr,t|s,J t|trN|j d urN|j }t|tr|jj	}t|t
jr| |  |d urt|ts| | nd}t|tr܇fdd|jD }tdd | D rd}n*t|tr|jrt|}|dvrd}|rr|d usJ d	d
dfdd}| |}d| d|  d}	|j| |	t||d  | |f n
t
|} | |f d}t|tr||jj  kr|jjksn d}nt|tr|dvrd}|r|d usJ | |}j| d }
j | d }d|  d| d|rXdd!|"  nd |
rtdd!|
"  nd }	|j| |	 d S )Nztrack_symint %s %s %sFc                   s   i | ]}| j |d qS rb   r<  r  r  r   rh   ri   r$  d  s   zIShapeEnv.produce_guards_verbose.<locals>.track_symint.<locals>.<dictcomp>c                 s  s   | ]}|d u V  qd S rb   rh   )r   r6  rh   rh   ri   r  g  r   zHShapeEnv.produce_guards_verbose.<locals>.track_symint.<locals>.<genexpr>Tr  r   r   r  c                   s     | }| dS r  )r  )r  Zsexpr)
py_printerrh   ri   r4  t  s    
zCShapeEnv.produce_guards_verbose.<locals>.track_symint.<locals>.hintNot all values of z are valid because z was inferred to be equal to r  zYou marked z; as dynamic but your code specialized it to be a constant (z). If you're using mark_dynamic, either remove it or use maybe_mark_dynamic. If you're using Dim.DYNAMIC, replace it with either Dim.STATIC or Dim.AUTO.z

Framework stack:
r  z

User stack:
)#r   r   r)   r  r   r#   r  r   Zmaybe_as_intr   r   r;  r   r=  r`  r5  r   rx  r  r  r   r  r  r3  r   r  r   r6  r9  r:  r  r  r  r	  format)r  r   rb  r  Zconstraint_violatedZsym_vrsr  r4  var_with_ranger  r  Zframework_stack)input_guardsr5  r.  re   symbol_to_constraintsr  rh   ri   track_symintO  s    









z5ShapeEnv.produce_guards_verbose.<locals>.track_symintrZ   )r  r   ra   c                   s   t dt| j| t|tr*t|s*J t|trL|j d urL|j }t|tr|jj	}t|t
jrx| |   | |f nt
|} | |f d S )Nztrack_symfloat %s %s)r   r   r)   r  r   r"   r  r   Zmaybe_as_floatr   r   r;  r   r  )r  r   r  )r9  r  rh   ri   track_symfloat  s    


z7ShapeEnv.produce_guards_verbose.<locals>.track_symfloatLocalSourcerb  )
AttrSourcec                 S  s   g | ]}g qS rh   rh   )r   r  rh   rh   ri   r    r   zSkipping guard %sr  r#  zN  # duck sizing added this equality because these variables had the same size zY (to avoid this specialization, set torch.fx.experimental._config.use_duck_shape = False)  # z  # (unknown var z, please file a bug)z  # (unknown source r   zThe values of  and z must always be equal.c                   s     | iS rb   )r  r  )r   r  rh   ri   r  g  r   z1ShapeEnv.produce_guards_verbose.<locals>.<lambda>z) must always be related to the values of z by r  r$   )guardra   c                   s~   | j}|v rd S | z.d}t	fdd|jD r^jd usRJ j|}t D ]6\}}}||}|dkr| d| j }|	| qj
| |sPt|jdkrPtt|j}	| d }| }	|	D ]f}
t|
tr2||
}d| d	| d
}|
j|| qt|
tr@qtd|
 qW n& tyx   jd| j  Y n0 d S )NFc                 3  s$   | ]}| D ]} |V  qqd S rb   rh   )r   r  r  )r0  r  rh   ri   r    s   zGShapeEnv.produce_guards_verbose.<locals>.issue_guard.<locals>.<genexpr>r#  r@  r   r   r6   satisfy the generated guard r  zunrecognized constraint zFailing guard allocated at %s)r  r   r`  rx  r   ry  r  r  r  r   r  r6  r  r  r   r5  r  r3  r  r=  r  r  r   r  )rB  r   Z
is_trivialr  printerlang
guard_exprr  r  constraintsr   r8  r  )
	all_exprsr0  issuedr  printersr5  r.  re   r:  r  rh   ri   issue_guard~  sL    



z4ShapeEnv.produce_guards_verbose.<locals>.issue_guardrh   rh  r  )rh  r  c                 3  s   | ]} |V  qd S rb   rh   r   r  r0  rh   ri   r    r   z2ShapeEnv.produce_guards_verbose.<locals>.<genexpr>Frn  r8  c                 3  s   | ]} |V  qd S rb   rh   rM  rN  rh   ri   r    r   ro  r6  rC  znot math.isnan(zM  # implicit guard for float input due to NaN specialization in the frameworkr  z~std::isnan(zUnimplemented for lang: z  . z  - r:  r  zConstraints violated (r;  z$%s Warning only constraints violateddynamicc                 s  s   | ]}|rd V  qdS r   Nrh   )r   r"  rh   rh   ri   r  ;  r   T)reverse)Z
num_guardsr   Zsymbol_guard_counts)PopulateValidator)N)N)r   r2  ra  r1   rc  r  r6  r   rT   r/   r  r  r   r#   r   r"   r  r   r  r  rV   rz  r   r  r  r  r   r  r  r  r?  evaluate_exprr   r9  r   r  r  r@  r;  r  rA  extendr   r>  r&   r  r8   r?  r   rg  rh  r  rn  r,   rF  r  rG  r  r  rH  r  ru  r  ry  r`  r  r  r  r  r   r  r  r  rn  rp  r/  r   r  r  rV  r3  rS  r  r   r  r   r  rv  r  r`  r%  r<  rl  r9  r  rE   rx  rs  r  rt  r:  rS  r5  r6  r=  r  rL   rM   r  r	  r  r-   re  r{  sumr  r|  r_  rS  fx_tracebackZpreserve_node_metar  r  runr  r  r  )Nre   r'  r  r  r   r$  r%  r&  r#  r  r  r  ri  contextrE  rR  r3  rT  rU  expr1r  concrete_valZsrcEqrL  rM  r  Zexpr2_Zphantom_symbolr;  r<  r  r>  rb  r?  Zsources_tensors_constraintsr  r  r  Zinner_tZinner_contextZcurr_tZconstraint_sizeZconstraint_stridessZproperty_sourcer  r  rD  r  s0r  r  rK  rB  r  r  vr_slocZboundsrfZverbose_exprboundrG  r   rF  r8  Z	warn_msgsZ
error_msgsrD  r3  Zmsg_cbZstr_msgZdebug_names_strerrrS  r  r  r6  Zhelpersrh   )r  rV   r*  r(  rH  r-  r   r9  r0  rI  r  r'  rJ  r5  r.  re   r2  r  r:  r  ri   r   R  s   $




=

 

(



 $c






	

	







	&;



*


	







2

zShapeEnv.produce_guards_verboser   r#  z#Sequence[Union[SymInt, FakeTensor]]rV  )r'  r   r#  ra   c                  sT   ddl m  dd tt|D }| j| fdd|D ||d}|rPd|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                 S  s   g | ]}d | qS rk  rh   r  rh   rh   ri   r  w  r   z6ShapeEnv.produce_guards_expression.<locals>.<listcomp>c                   s   g | ]} |qS rh   rh   r  r=  rh   ri   r  z  r   rb  rA  N)r  r>  r  r6  r!  r	  )re   r'  r   r#  	arg_namesZproduced_guardsrh   r=  ri   produce_guards_expressioni  s    
z"ShapeEnv.produce_guards_expressionzUnion[int, float, bool])codera   c                 C  s    dd | j  D }t|t|S )z?
        To be used by compile_fx to evaluate symexprs
        c                 S  s   i | ]\}}t ||qS rh   )r   r   r  r   rh   rh   ri   r$    r   z-ShapeEnv.evaluate_symexpr.<locals>.<dictcomp>r  r%  evalr   re   re  r`   rh   rh   ri   evaluate_symexpr  s    zShapeEnv.evaluate_symexprr  c                   s$    fdd j  D }t|t|S )zB
        To be used by compile_fx to deserialize symexprs
        c                   s0   i | ](\}}t |tt| tt|d dqS )Nr  )r   r#   r4   r   rf  r   rh   ri   r$    s   z0ShapeEnv.deserialize_symexpr.<locals>.<dictcomp>rg  ri  rh   r   ri   deserialize_symexpr  s    
zShapeEnv.deserialize_symexprzSequence[object])re  r`   ra   c                 C  s0   d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                 S  s   g | ]}d | qS rk  rh   r  rh   rh   ri   r    r   z7ShapeEnv.evaluate_guards_expression.<locals>.<listcomp>L)r  r6  rh  r   r=  r  )re   re  r`   rc  rh   rh   ri   evaluate_guards_expression  s    z#ShapeEnv.evaluate_guards_expressionr"  zSequence[Tensor])r'  r`   r#  ra   c                C  s"   | j ||d}|r| ||S dS )zRGenerate guards for a graph's placeholder values and evaluate the guards with argsr"  T)rd  rm  )re   r'  r`   r#  re  rh   rh   ri   r$    s    z!ShapeEnv.evaluate_guards_for_argszSequence[torch.SymInt]zlist[ShapeGuard])symintsra   c                   s&   dd  D   fdd| j D }|S )z
        Get a list of guards, but pruned so it only provides guards that
        reference symints from the passed in input
        c                 S  s$   h | ]}t |jjtjr|jjqS rh   )r   r   r   r   r;  r  rh   rh   ri   r     s   z-ShapeEnv.get_pruned_guards.<locals>.<setcomp>c                   s*   g | ]"}t  fd d|jjD r|qS )c                 3  s   | ]}| v V  qd S rb   rh   r  rn  rh   ri   r    r   z8ShapeEnv.get_pruned_guards.<locals>.<listcomp>.<genexpr>)r  r   r   r  ro  rh   ri   r    s   z.ShapeEnv.get_pruned_guards.<locals>.<listcomp>r   )re   rn  r   rh   ro  ri   get_pruned_guards  s    
zShapeEnv.get_pruned_guardsr&  )r'  r`   ra   c                   s   i  dddd fdd}t ||D ]\}}|du r6q$t|trL||| q$t|tjs\J t| D ]\}}|||| qht| D ]\}}|||| q|| |  q$ 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
        r   r_   )r  r   ra   c                   s   t |trt | tsJ |jj}t |tjrb| v rX | | ks`J  |  d|  q|  |< nPt | tjr|  v r |  |  ksJ  |   d|   n|   | < d S )Nr  )r   r#   r   r   r   r   r;  )r  r   r  r  rh   ri   bind_symint  s    
$

,z*ShapeEnv.bind_symbols.<locals>.bind_symintN)	r  r   r#   r   rT   r  rF  rG  rH  )re   r'  r`   rs  ri  r  r  r  rh   rr  ri   r'    s    

zShapeEnv.bind_symbolszlist[SympyBoolean]c                   s    fdd j D S )zSReturns a list of guard expressions that aren't statically known (i.e. not trivial)c                   s0   g | ](} j |jd |jddu r |jqS )rh   rL  N)r  r   r  r  r   rB  r   rh   ri   r    s   
z2ShapeEnv.get_nontrivial_guards.<locals>.<listcomp>rp  r   rh   r   ri   get_nontrivial_guards  s    
zShapeEnv.get_nontrivial_guards)verbosera   c                   s   d  fdd| jD S )zQFormat this shape env's guard expressions with optional traceback info if verboser  c                 3  s0   | ](}d |j   r dt|j nd V  qdS )z - r  r  N)r   r   r  rt  rv  rh   ri   r    s   z)ShapeEnv.format_guards.<locals>.<genexpr>)r	  r   )re   rv  rh   rw  ri   format_guards  s    zShapeEnv.format_guards)r   r  ra   c                   sP    fdd|j D }|rF j| @ D ]}|| dur&tdt||< q&t||S )zPGiven a sympy expression, computes a ValueRanges bound for what values it can bec                   s   i | ]}| j |d qS rb   r4  r  r   rh   ri   r$    r   z(ShapeEnv.bound_sympy.<locals>.<dictcomp>Nr,  )r   rr  ru  rQ   rE   rN   )re   r   r  r<  r|  rh   r   ri   rN     s    zShapeEnv.bound_sympyzOptional[tuple[sympy.Symbol]]ztuple[SympyBoolean, ...])r  compute_hintra   c                   st   |du rdd  j  D }n fdd|D }dd  jD }t||}|rb fd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                 s  s   | ]}|D ]}|j V  q
qd S rb   r  )r   rsr  rh   rh   ri   r    s   z&ShapeEnv.get_axioms.<locals>.<genexpr>c                 3  s2   | ]*}| j vr j|d D ]}|jV  qqdS )rh   N)r  rv  r  r   )r   r  r  r   rh   ri   r    s   
c                 s  s   | ]}|j V  qd S rb   r  r  rh   rh   ri   r  $  r   c                 3  s   | ]}t | jV  qd S rb   )r}   r  r  r  r   rh   ri   r  '  s   )	rv  r  r   rr  chainr7  r=  fromkeysru  )re   r  ry  Zruntime_assertsr   rh  rh   r   ri   
get_axioms  s    

zShapeEnv.get_axiomsz@tuple[tuple[SympyBoolean, sympy.logic.boolalg.BooleanAtom], ...]r  ra   c                   s   i  ddd fdd}|| t |tjr^|tj|j|jdd |tj|j|jdd nt |tjr|tj|j|jdd |tj|j|jdd |jj	r|jj	r|tj|j|jd dd n(t |tjr|tj|j|jd dd t
  S )	zGGiven a expression, it returns a list of predicates that follow from itr   r_   r  c                   s   t | } t| tjtjfrt| tjr,tjntj}tj t| | j| jdd< tj t| | j| jdd< tj	 || j| jdd< tj	 || j| jdd< ntj | < tj	 t t
| < d S )NFrn  )r}   r   r   r9  rV  r  r   r:  r>  r  rU  )r   rx  equivrh   ri   add_expr3  s    
z+ShapeEnv.get_implications.<locals>.add_exprFrn  r   )r   r   r9  rt  r:  r>  rs  rr  rV  
is_integerr7  r%  )re   r  r  rh   r  ri   r  ,  s    zShapeEnv.get_implications)r  ry  r  rh  r<  r]   zOptional[tuple[SympyBoolean]]z1Optional[tuple[tuple[sympy.Symbol, ValueRanges]]]zOptional[sympy.Basic])r   r  ry  r  rh  r<  ra   c             	     s  |r|rJ   ||}|r0| j j}t|}ddd fdd}|du rf| j  j}n>i }|D ],}	|	j|jrn|t	 
  |	 qn|| ||}|j}
|
s|js|jr|S |du r؈ jnt	|t fddt|
td	D }t||||}|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 and unbacked SymInts,
        e.g., if s0 happens to be 3 this run, compute_hint will subsitute s0 with 3.
        zdict[sympy.Expr, sympy.Expr]r_   )rh  ra   c                   sR    j s
d S d _ i }|  D ]&\}}|tr| ||i q| | d S rp  )r~  r%  r\  r<   r  r  )rh  Z	new_itemsr!  r"  r   rh   ri   resimplify_floor_divv  s    
z=ShapeEnv._maybe_evaluate_static.<locals>.resimplify_floor_divNc                 3  s0   | ](}t || j|| jv V  qd S rb   )r  r  r  rr  r  re   Z
var_rangesrh   ri   r    s   
z2ShapeEnv._maybe_evaluate_static.<locals>.<genexpr>r  )r  r  r  rj  r}   rh  r   r=  r  r=  r  r  r  r<  r7  r  r   r  )re   r   r  ry  r  rh  r<  r  substr  r  r  r  rh   r  ri   r  U  s8    



zShapeEnv._maybe_evaluate_staticr   c                 C  sJ   i }|j D ]$}| |}|jr&||kr
|||< q
|rBt||S |S dS )zS
        Apply symbol replacements to any symbols in the given expression.
        N)r   rH  r  r  r  )re   r   ro  r  r  rh   rh   ri   r    s    


zShapeEnv.replacec                 C  s>   t  }| jD ]}| |}|js|| q|| _|   d S rb   )r  rq  r  r  r`  r  )re   Znew_divisibler!  r  rh   rh   ri   _update_divisible  s    

zShapeEnv._update_divisiblec                 C  s  t |}| |}|r|ts*|tri }g |t|tR D ]}t|jdkr\qH|j\}}|dksv|dkr|| }}|dks|dkrH| j|dd}|j	|kr|j
tu r|n|||< qH|j|krH|j
tu r|n|||< qH|r||}|tr^i }|tD ]B}t|jd tr
|jd j\}	}
|	|
 dkr
|	|
 ||< q
|r^||}|tr|   i }|tD ]h}|j\}	}
t|
tr|
j\}}| t|	|
| jv r|	|kr| t||| jv r|||< q|r||}t |}|tri }|tj}|tj|tj}|tD ]6}|j\}	}
| t|	|
| jv rBt|	|
||< qB|r||}t |}|tj}|tj|tj}||r||r|}|S )zAUse known constraints and replacements to simplify the given exprr,  r   r   Tr  )r  r  r\  r@   rA   r  r6  r`   rN   r9  ry  r:  r  rD   r   r>   r<   r  rB   rq  r   PowRationalr  r   r;   r=  )re   r   r  Zmin_max_replacementsZatomr   r  r6  Ztrunc_replacementsr{  r  Zdiv_replacementsZbase1Zdivisor1ZpowsZ	rationalsfdr  Znew_powsZnew_rationalsrh   rh   ri   r    s|    












zShapeEnv.simplifyr~  
allow_none)r   r  ra   c                  s<  t  | j}|js8ddlm} t||r2dS | j|dd}|durL|S |rTdS | jr|| j}|dd | j	 D }|j
s|j
s||krtd | |S td	 || ntd
 || | jr,|| jj
s,td  td fddd | t|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   rH   NT)ry  c                 S  s   i | ]\}}|t |d qS r,  r  r   rh   rh   ri   r$  0  r   z&ShapeEnv.size_hint.<locals>.<dictcomp>zoblivious_size hit %s -> %sz3oblivious_size counterfactual failed %s -> %s != %sz1oblivious_size miss %s -> %s (counterfactual: %s)z*propagate_real_tensors size_hint(%s) -> %spropagate_real_tensorsc                     s&   t  t ttjdd dS Nr   r  )r   r)  r  r   r*   r  rR   r   r  rh   r   Zunsound_exprrh   ri   r  P  s    z$ShapeEnv.size_hint.<locals>.<lambda>r  propagate_real_tensors: r  )r  r  r  r   torch.utils._sympy.singleton_intrI   r   r  rk  r%  r   r   r2  rj  r  r+   guard_or_defer_runtime_assertr   r9  _make_data_dependent_error)re   r   r  result_exprrI   r  correct_hintcounterfactual_hintrh   r  ri   	size_hint  sf    	


zShapeEnv.size_hintc                 C  s$   t || j}|jp"| |d uS rb   )r  r  r  r  r  )re   r   r  rh   rh   ri   r  b  s    zShapeEnv.has_hintsize_oblivious_resultexpr_sym_node_idr\   )r   unhinted_exprr  r  ra   c                  s   g } j D ]<}dj|  }jd|| |jv r
|| q
d}|d ur`d| d}d\}	}
 j	rzd}nd}| d  d	 d
dt
t|pd d| d|	 ddt
t j  d|
 }td fddd 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)

zCaused by: 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
Zguard_on_data_dependent_errorc                     s*   t  t jttjdd dS )Nr   r  )r   r  Zexpr_idr  )r   r  r*   r  rR   r   r  rh   r   re   r  rh   ri   r    s    z5ShapeEnv._make_data_dependent_error.<locals>.<lambda>r  )r   r	  rm  r7  r   r   rr  r   r  r  rp  r   r(   r\   )re   r   r  r  r  Zsize_like_symbolsr  Z
stacktraceZsize_oblivious_result_msgr  r  descr  rh   r  ri   r  j  sD    




z#ShapeEnv._make_data_dependent_errorr	  zOptional[ValueRangesSLoc])r  r6  r^  r
  ra   c                C  sF  |j |j }}|dk r*|| jv r*t|d}|| jvrt| jd|| || j|< |d u rh|  }t||}|| j	|< n| j| }||@ }	|	|kr|d u r|  }t||}|	j |j kr|j | j	| _ |	j|jkr|j| j	| _|	| j|< | jd||	 | j
| }
d urB| j| }|
|vrB|sB|
|v sBJ |
 d| d S )Nr,  z"_update_var_to_range %s = %s (new)z%_update_var_to_range %s = %s (update)z not in )r9  r:  rr  rQ   r<  r   r   r  r   rl  r  r  )re   r  r6  r^  r
  r9  r:  r  r  r  r"  r  rh   rh   ri   r    s6    	








zShapeEnv._update_var_to_range)r   tgtr  ra   c                   s  j  dkrdS  jv r$dS t tjs4J jrFtsFdS d} jv rj  }	}
 | ||s6tjdkr6ttj}tt |dd}|dur6jd| | 	|d }tt|jt|j}	
||	j   	}||s6J d|d| jv r^ttjr^j n&ttjrjv rj  ||sjd || dS  jv rj	d	d
}
j	 d	d
}|
|sjd |
| dS ttjtjfrt td fddd j  g D ](}rRj!|< t"j#d	dj$|< q>t%j&rj'dj   d (  jjdd	d t)d | j  <  j*vrԈ+ j* < ,  -tj dd dS )zw
        Adds or updates a replacement for a symbol.
        Use this instead of `self.replacements[a] = tgt`.
        Nr   FZfloordiv_inequalityz2set_replacement: solve for %s in %s == %s gives %sz
tgt_bound=z not a subset of src_bound=z:skipped set_replacement %s = %s (%s) [%s not subset of %s]Tr  zVskipped set_replacement %s = %s (%s) [%s not subset of %s (size-oblivious conditions)]Zsymbolic_shape_specializationc                	     sN   t  dd j g D t ttjdd rFtnd dS )Nc                 S  s   g | ]}|  qS rh   r  r  rh   rh   ri   r  `  r   z?ShapeEnv._set_replacement.<locals>.<lambda>.<locals>.<listcomp>r   r  )r  r  r  reasonr  r  )r   r  r  r*   r  rR   r   r  rh   r   r  re   r  user_tbrh   ri   r  ^  s    z+ShapeEnv._set_replacement.<locals>.<lambda>r  r+  zSpecializing %s to %sr   ZSPECIALIZATIONr  zset_replacement %s = %s (%s) %srn  ).ro  r  r   r   r   r;  rO  r[  r<  rN   r  r=  r6  r  r  rJ   r9  r   r   rQ   r:   r9  r=   r:  rl  rr  r`  r   r  r'   r  r+   r  r  rR   r   r  r  Zprint_specializationsr  r  r2  rp  r  r  r  )re   r   r  r  Z	tgt_boundZ	src_boundr  r  Zrat_b_boundZb_boundZtgt_bound_soZsrc_bound_sor  rh   r  ri   r    s    




	


zShapeEnv._set_replacementc                 C  s   | j | |   d S rb   )rq  r`  r  r  rh   rh   ri   _add_divisible  s    zShapeEnv._add_divisibler  c                   s\   | j vr|S  j | } fdd|jD } j | |\}}|rR ||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                   s   i | ]}|  |qS rh   )rH  r  r   rh   ri   r$    r   z"ShapeEnv._find.<locals>.<dictcomp>find)ro  r   Z	_xreplacer  )re   r   r  Zcur_replaceZreplacedchangedrh   r   ri   rH    s    


zShapeEnv._findc              	     sV  t |tjr&|jD ]} | qdS t |tjsBtd| dS t |tjrRdS t	|j
}t|dksvJ d| t|dkrdS ddd fd	d
}t||dd}|j}|j} | t |tjsdS |tsz|t|t}t|dkrtdd |D rtdddddd}|||rN | |d n|||rp | |d npt||d dd}	|	durtdd t|	d D r |	d }
tt|
dk}|r |d |
d W n ty   Y n0 |trRtt|t}z$t||dd}	|	dur:|	d dkr:  | |j\}}t |tj!r:t |tj"r:t|jdkr:|j\}}t |tj!r:t |tj#r: $|r:|t%|| } & j'j(} )|t*+ j,| t-.| | j/v r 0| j/| |  | j1v r( j12|  ||| d W n tyP   Y n0 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)
        Nz;_maybe_guard_rel() was called on non-relation expression %sr   z3The expression should not be static by this point: r+  r  ztuple[int, int, str]r{  c                   s   |  j v o tdd  j |  D } j| dd}|d u r@tj}n,t| tjrft|t	j
s\J t|}ntj}| j}|rzdnd||fS )Nc                 s  s   | ]}|  V  qd S rb   )r  r  rh   rh   ri   r    s   zHShapeEnv._maybe_guard_rel.<locals>._smart_symbol_sort.<locals>.<genexpr>Tr  r   r   )r  r  r  r   r   rL   rM   r  r   r   r  r   r  )r|  Zhas_only_ephemeral_sourcesZ	hint_sizerF  r  r   rh   ri   _smart_symbol_sort  s    
z5ShapeEnv._maybe_guard_rel.<locals>._smart_symbol_sortT)r  rR  c                 s  s   | ]}|j d kV  qdS rQ  r  r  rh   rh   ri   r    s   z,ShapeEnv._maybe_guard_rel.<locals>.<genexpr>r   r   )r:  r>  ra   c                 S  s4   t | tjr0t| r t|s dS t| tjr0dS dS r  )r   r   r;  r  rL   rM   r  )r:  r>  rh   rh   ri   trivial_solve  s    
z0ShapeEnv._maybe_guard_rel.<locals>.trivial_solveZtrivial_lhsZtrivial_rhsFr  c                 s  s   | ]}|j V  qd S rb   )r  r)  rh   rh   ri   r    s   r   r  r,  Zdivisibility)3r   r   rS  r`   _maybe_guard_relrR  r   r  rV  r   r   r6  r  r:  r>  _refine_rangesr9  r\  rB   r  r<   r  rx  r  r  rH  rJ   r  Zpreorder_traversalr  r  r  r  Numberra  r;  r  r  r  r   r   r  rO   floordivr<  rQ   wraprj  r  rr  r`  )re   r   r  freer  r:  r>  Zfloor_div_atomsr  r  r  okmod_exprpqr   Zi0di1rh   r   ri   r    s    











zShapeEnv._maybe_guard_rel)r  ra   c                 C  s   |s
| j sdnd}t|tS )Nr   r,  )rL  rQ   rE   )re   r  r9  rh   rh   ri   r  =  s    zShapeEnv._default_value_rangec                 C  s   t  S rb   )rQ   Zunknown_intr   rh   rh   ri   r  C  s    z)ShapeEnv._default_unspecified_value_rangec                 C  sP   t |t}t|D ].}|j\}}t||}t|d}| | q| 	|S rj  )
r7  r  r<   r   r`   rB   r   r9  rT  r  )re   r   Z
floor_divsr  r{  r  r  Zeq_exprrh   rh   ri   _simplify_floor_divF  s    

zShapeEnv._simplify_floor_div)r   r[  ra   c                 C  sf   | j rb| jd  d7  < tddi | j| d| dd tjd||t tjk rZd	nd
d d S )NZignored_backward_guardr   rP  Zevaluate_expr_frozenr  r,  )Zignored_guardversionz>Ignored guard %s == %s, this could result in accuracy problemsTFr  )	r  r{  r-   re  r   r2  getEffectiveLevelr   WARNING)re   r   r[  rh   rh   ri   _check_frozenV  s$    zShapeEnv._check_frozenOptional[types.FrameType]c                 C  s.   t  }|d ur*|jjt vr"|S |j}q|S rb   )r   r  r  r  r   r  )re   r  rh   rh   ri   _get_user_framel  s    zShapeEnv._get_user_frameztuple[SLoc, str])r  framework_locra   c           
      C  s   |}|d u r@|   }z&|d ur6t|jj|j|jj}W ~n~0 d }t }|rt	|d }|dkr~|| j
t v r~|d8 }q\t|| dd}d}|r|rddt| }|rtjrtjdd}	|dd|	  7 }n|r|d	7 }t|||fS )
Nr   r   T)liner  zM
User Stack (most recent call last):
  (snipped, see stack below for prefix)
r  z
C++ stack trace:
z?
For C++ stack trace, run with TORCHDYNAMO_EXTENDED_DEBUG_CPP=1)r  	tracebackFrameSummaryr  r  r  co_namer'   r  r6  filenamer   rS   r	  format_listr  Zextended_debug_cpprR   r   r7  r%   )
re   r  r  Zflocr  Zmaybe_user_locr  r  r  Z	cpp_stackrh   rh   ri   r  t  s@    

zShapeEnv._get_stack_summaryr%   )r  ra   c                 C  s   | j |d\}}|S )N)r  )r  )re   r  r  r  rh   rh   ri   r    s    zShapeEnv._get_sloc)r  ra   c                 C  s@   t t|  dd }|| jv r0|d7 }q| j| |S )N   d   r   )r   hashlibsha256encode	hexdigestri  r`  )re   r  attemptrh   rh   ri   r    s
    

zShapeEnv._generate_unique_idrU  c                   s  i }i  t   }du s"|jjdkr(t S tt|j}t|j\}}d\}}}t	|D ]:\}	}
|
j
durr|
j
}||jkr~qZ|du r|	 }}qZ|	}qZ|du s|du rt S ddd fdd|j}|||d	  D ]b}
|
j
 }durt||}t|
jtr|
j|jv rt|j|
j d
 }fdd|D ||
j< q||j| |d	 |  }|sdt S t|d
 t|d
   dfdd|D  }t|| dS )z
        Given the current user code frame, finds the relevant lines of code,
        values of symbolic locals, and free symbols involved.
        Nr   )NNNr   rV  r{  c                   s   t | tjrh|  D ]}| q|  D ]}| q*|   d|   d|   d|   dS t | tttfr| j	j
jD ]6}t| v rq|jv rj| d   t|< qt| S d S )NzTensor(shape: z
, stride: z, storage_offset: r  r   )r   r   rT   rF  rG  rH  r!   r#   r"   r   r   r   r   r  r  )r|  r  r  )frame_symbolsr  re   rh   ri   r    s(    


z'ShapeEnv._find_frame_locals.<locals>.gor   r   c                   s   g | ]} |qS rh   rh   )r   Z
flat_local)r  rh   ri   r    s   z/ShapeEnv._find_frame_locals.<locals>.<listcomp>r  c                   s   g | ]}| d  qS rb   rh   )r   rW  )indentrh   ri   r    r   )rW  rY  r  )_find_user_code_framer  r  rU  r   disBytecoder   getsourcelinesr  starts_liner  r  r   argvalr   f_localsr0  Ztree_flattenr6  lstripr	  r>  )re   frame_localsr  ZinstructionsZco_linesr  startendr   r  instrZlast_linenolinenoZflat_localsZlocsZ	frame_locrh   )r  r  r  re   ri   _find_frame_locals  sN    






zShapeEnv._find_frame_locals)r  r  forcing_specra   c           	   	     s   t d fddd td fddd jtjrt }tjd uoT|tjk}	|\}}d}|sxd| d	}jj
d
|sn d|||||d d S )NZguard_addedc                	     sB   t  jtdtd fddj D t dS )Nr)  c                   s$   i | ]\}}| j v rt||qS rh   r   r   r   r  rh   ri   r$    s   
z9ShapeEnv._log_guard.<locals>.<lambda>.<locals>.<dictcomp>)r   r  expr_node_idr  r  symbol_to_sourcesr  )	r   r  r*   r  r  rn  r%  r   r  rh   r  r  re   rh   ri   r    s    

z%ShapeEnv._log_guard.<locals>.<lambda>r  Zguard_added_fastc                     s,   t  tt ttjdd dS )Nr   r  )r   r  r  )r   r*   r  r'   r  rR   r   r  rh   r  rh   ri   r    s    r  zA, for more info run with TORCHDYNAMO_EXTENDED_DEBUG_GUARD_ADDED=""z%s %s [guard added] %s%s%sz (forcing_spec)r  )r(   r+   r   r   r   INFOr   r  Zextended_debug_guard_addedr  r2  )	re   r  r  r  Zstr_gr  r  r  r  rh   r  ri   
_log_guard  s:    


zShapeEnv._log_guardr  r4   )r   r  r  ra   c                 C  s$   t || _| j|j|j|j||dS )zY
        Given a a SymNode, evaluates sym_node.expr, adding guards if necessary.
        )r  )r  r  rT  r   r4  r  )re   r   r  r  rh   rh   ri   r  ,  s    

zShapeEnv.evaluate_sym_nodec                 C  s   |   }|d usJ tt|j}tjdkrPddlm} |||j	dd d}n
|j	d }tjdkrz|| j
d	v rz|d
7 }|| }|j
dkr|jd ur||d
  }|j
dkr|jdkp|j
dk}|r||d  j
dkrdS dS )N)r)     r   )bisect_leftc                 S  s   | j S rb   )r  r  rh   rh   ri   r  P  r   z,ShapeEnv._is_python_assert.<locals>.<lambda>r  r,  )r)     )ZTO_BOOL
COMPARE_OPr   POP_JUMP_IF_TRUELOAD_GLOBALr  LOAD_ASSERTION_ERRORRAISE_VARARGSTF)r  r   r  get_instructionsr  r   version_infobisectr  f_lastiopnamer  r  )re   r  Zinstsr  r   instfirstZstarts_with_assertrh   rh   ri   _is_python_assert?  s*    	



zShapeEnv._is_python_assert)r  unsound_resultra   c                   s@   t d  td fddd td fddd d S )Nz.propagate_real_tensors evaluate_expr(%s) -> %sr  c                     s&   t  t ttjdd dS r  r  rh   )r  r  rh   ri   r  q  s    z7ShapeEnv._log_real_tensor_propagation.<locals>.<lambda>r  Z!propagate_real_tensors_provenancec                	     sF   t  t jtdtd fddj D t dS )Nr)  c                   s$   i | ]\}}| j v rt||qS rh   r  r   )r  rh   ri   r$    s   
zKShapeEnv._log_real_tensor_propagation.<locals>.<lambda>.<locals>.<dictcomp>)r   r)  r  r  r  r  r  )	r   r  r*   r  r  rn  r%  r   r  rh   r  re   r  rh   ri   r  {  s    

)r   r  r+   r(   )re   r  r  rh   r  ri   _log_real_tensor_propagationg  s    
z%ShapeEnv._log_real_tensor_propagationr  z!Optional[Union[int, bool, float]])r  r4  r  r  r  r  ra   c             	   C  s   t  }| |||||||S )z
        Given an expression, evaluates it, adding guards if necessary
        When fallback_value is not None the function return fallback_value instead of failing with data dependent error.
        )ru   r  _inner_evaluate_expr)re   r  r4  r  r  r  r  Zsuppress_guards_tlsrh   rh   ri   rT    s    zShapeEnv.evaluate_exprrT  )save_tracked_fakesr  )r  r4  r  r  r  r  r  ra   c           	   
   C  sd   z| j ||||||dW S  ty^ } z.t|tr4n| jd||||  W Y d }~n
d }~0 0 d S )Nr  zKfailed during evaluate_expr(%s, hint=%s, size_oblivious=%s, forcing_spec=%s)_evaluate_exprr  r   r\   r   r  )	re   r  r4  r  r  r  r  r  r  rh   rh   ri   r    s(    
zShapeEnv._inner_evaluate_expr)r   assumed_valuera   c                 C  s$   |  d\}}td|||| d S )NTzfcould not evaluate %s due to data dependency, it was assumed to be %s with no runtime assertions %s %s)r  r   r2  )re   r   r  r  r  rh   rh   ri   _log_suppressed_dde  s    zShapeEnv._log_suppressed_ddez!Optional[Union[bool, int, float]]c                  sj  t tjjjtjjjfrS tjdd fdd}d }d}	jr&|d ur&	 s&|s&t
dd jD s&|d u r&| }
|
tju rtj|f\}}	n^|
tju rވtj|f\}}tj|f\}}	n*tj||
f\}}tj|f\}}	|d usJ |	r&| d }zjrjd  d urt  trx ksJ  d	  n t sJ  d	  W S }j||d
}|d urjd|rd dn|| |stjr d ur| ksJ | d	  |W S d}d }
|jj ksj|dd}|d usLJ |jj ksd}|s|d ur| |W S j r!j  }js!dd j " D  }js||krt#d| |}
d}|s$j$r$!j$!j }js$%| d}|}
d}|sJj&rJ' rJtj}
d}d}|sd }|shj|dd
}j(|!j||j)dn|}|
d u r| }
*||
 tj+rt  trt |tjtj,frt-|}|
tju rt.t/|}n$|
tju rt-|}nt||
}|r80|d d|
  |
W S 	 sj1d||d 2| j3st4|5 |d
}j67| j89t:;<| n0|d  nj1d||d W n& t=y   |	r>|  Y n0 	 sf|d urf|jD ]\}j?|  d7  < |stj@d urj?| tj@krj#dtj@| jA|dd q|
S )Nr]   r   c                    s0    d u r" } | d usJ | S t S d S rb   )r  r   r  r  r4  r  re   rh   ri   compute_concrete_val  s
    
z5ShapeEnv._evaluate_expr.<locals>.compute_concrete_valFc                 s  s   | ]}t |tjV  qd S rb   )rL   rM   r  r  rh   rh   ri   r    r   z*ShapeEnv._evaluate_expr.<locals>.<genexpr>zeval %s [trivial]r  r  z eval %s == %s [statically known]zsize_oblivious(r  Tr  c                 S  s   i | ]\}}|t d |qS r  r  r   rh   rh   ri   r$  a  s   z+ShapeEnv._evaluate_expr.<locals>.<dictcomp>z/oblivious_size %s -> %s (passed counterfactual)r  r  r  rh  r  zevaluate_expr: zeval [guard suppressed]r   z6symbol_guard_limit_before_specialize=%s exceeded on %s)Br   r   r	  r
  r  r  r   cacher`  r  rx  r   r  r  r   _assertr  r  not_rC  r  r  r   r   r   r9  r  r  r  r  ru  r  rk  r  r%  r2  rj  r  rP  r  r  r  r  Z/inject_EVALUATE_EXPR_flip_equality_TESTING_ONLYrV  rU  r   r   r  r  r  rO  r$   r  r   r   rh  r  r=  r  r  r  r  r|  Z$symbol_guard_limit_before_specializerT  )re   r  r4  r  r  r  r  r   r   r  r[  rz  r  ZeqlrB  r   static_exprZtransmute_into_runtime_assertr  r  r  r  r  r  r  r  rh   r  ri   r    sl   	



	
 

	



	





zShapeEnv._evaluate_exprc                 C  s@   | j  D ]}|  q
| j D ]}|D ]}|j  q*q"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)rm  r  cleanuprv  r  )re   r  r  r  rh   rh   ri   r    s
    
zShapeEnv.cleanup)r  )r  r  r  ra   c                 C  s  |}|  |}|dur.| jd|| t|S | j |dd}|dusHJ | jsl|j| j krl| j||dS | j	r|dur| 
 s| tj|f\}}|dusJ |r| | | 
 s| jd|dd | jrtd	| | |tj | | |}t|}tjd
d}	t|||	}
tdd |jD dd d}|rB|d nd}| j|g |
 | jt|  | !| |  j"d
7  _"| #  n| jd|dd dS )a  
        Adds a guard that orig_expr is True if we can or fall back to adding 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  Zruntime_assertFr  z&runtime_asserts_frozen but then got %sr   r  c                 s  s   | ]}t |tjr|V  qd S rb   r  r  rh   rh   ri   r  ;  r   z9ShapeEnv.guard_or_defer_runtime_assert.<locals>.<genexpr>c                 S  s   t | jdd  S r  )r   r  r  rh   rh   ri   r  <  r   z8ShapeEnv.guard_or_defer_runtime_assert.<locals>.<lambda>r  r   z!runtime_assert [guard suppressed])$r  r   r   r   rN  r   r  ru  rT  r`  r  r  r   r  r  r  rx  r  r   r  r  r}   rR   r   r  r  rv  
setdefaultr   rh  r  r=  r  r  rw  r  )re   r  r  r  r   r  r  r   r  r  r  candsixrh   rh   ri   r    s^    




z&ShapeEnv.guard_or_defer_runtime_assertc           
      C  sR  |  |}|jD ]:}t|tjs&J t| j|d tr<qt||}|d u s|j	r|d j	s`q|\}}| j
| }|j|j }}t|| j
}	||	jk rt|tjtjtjfr|	jtt|tj }||	jkrt|tjtjtjfr|	jtt|tj }|t||kr
q| |t|| | j
|  rB| || j
| jd | j  qd S )Nr   Zrange_refined_to_singleton)r  r   r   r   r;  r  r  rI   rJ   r  r<  r9  r:  rN   r9  rs  rq  r   rt  rr  rQ   r  r  r  r  r   )
re   r   r  r  Zr_exprr>  r6  r9  r:  Zrhs_vrrh   rh   ri   r  V  s<    



zShapeEnv._refine_rangesr   )r  r  r  ra   c                 C  sR   t ||}| j|t  }| || | j|  }|krNtd||j|j d S )Nz"constrain_symbol_range %s [%s, %s])	rQ   r<  r  r  r  r   r2  r9  r:  )re   r  r  r  Zupd_vrZold_vrZnew_vrrh   rh   ri   r    s    
zShapeEnv.constrain_symbol_range)NN)NN)N)F)F)NF)F)N)F)FN)N)FN)NNFN)N)NNFN)N)yrj   rk   rl   rd   r^  propertyrI  rJ  rK  rL  rM  rN  rO  r   r  r  r  r  r  r0   r  r  r@  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  staticmethodr  rR  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   rd  rj  rk  rm  r$  rq  r'  ru  rx  rN   r  r}  r   r  r  r  r  r  r  r  r  r  r  r  rH  r  r  r  r  r  r  r  r  r  r  r  r  rm   r  r  r  rT  r  r  r  r  r  r  r  rh   rh   rh   ri   ru     s  
B$ t-C%
1F v:+$ %   $      !
4	  (0W
"K$J*@$3 - "	,J5  (&   ,	.    ,  
 [>
c                 C  s   t | to| jjjS rb   )r   r#   r   r   r  r  rh   rh   ri   _is_int  s    r  )ri  r  ra   c                 C  s   t | do|| jv S )N_dynamo_dynamic_indices)r  r  )ri  r  rh   rh   ri   r    s    r  c                      s$   e Zd Zddd fddZ  ZS )PropagateUnbackedSymIntsr'  r&  )r(  ra   c                   s,   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  rc   run_noder   r  )re   r(  r  r)  rf   rh   ri   r    s    z!PropagateUnbackedSymInts.run_node)rj   rk   rl   r  rn   rh   rh   rf   ri   r    s   r  r  c                  C  sB   t  } | d ur>| jjtjt t	tjj
 s6q>| j} q| S rb   )r   r  r  r  
startswithr  rA  dirnamer   r   sepr  )r  rh   rh   ri   r    s    r  r  ztypes.FrameType)r  r  ra   c                 C  sN   t |jj|j|jj}| jd }|ddt j	|g
  7 }|f| _d S )Nr   z(

The following call raised this error:
r  )r  r  r  r  r  r  r`   r	  StackSummary	from_listr7  )r  r  frame_summaryr  rh   rh   ri   _blame_user_code  s    
r  c                      s8   e Zd ZdZddd fddZddd	d
dZ  ZS )_PythonMsgPrinterz
    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 ==, !=, >, <).
    zdict[str, list[str]]r_   )src_mapra   c                   s   t    || _d S rb   )rc   rd   r  )re   r  rf   rh   ri   rd     s    
z_PythonMsgPrinter.__init__r  r   r  c                 C  s   | j |j d S rj  )r  r  r  rh   rh   ri   r    s    z_PythonMsgPrinter._print_Symbolr  rh   rh   rf   ri   r    s   r  rV  )r^   ra   c                 C  s.   t | tjr*| jdkr*| jdkr*t| jS dS )z
    Check if a condition (SymPy expression) is checking for non-negative values (>= 0).
    Returns the variable name if it's a non-negative check (>= 0), None otherwise.
    r0  r   N)r   r   rR  Zrel_opr>  r   r:  )r^   rh   rh   ri   _is_non_negative_check  s    
r  zdefaultdict[str, list[str]])r  r  ra   c                   s2  | j }d fdd|jD }|r4td| dS t }| jd }|d7 }|t	|}t
|}g }|rd| d	| d
| dd d d d| dg}nd|| dd| dg}t|D ] \}	}
|d|	d  d|
 7 }qd fddtdd |jD D }|d| d| d7 }|f| _dS )av  
    Enhances a GuardOnDataDependentSymNode error with suggested fixes using torch._check.

    This function analyzes the condition that caused the data-dependent error and generates
    user-friendly suggestions for fixing it by adding appropriate torch._check calls.
    It handles special cases like non-negative checks with specific recommendations.

    Args:
        e: The GuardOnDataDependentSymNode error to enhance with suggestions
        src_map: A mapping from symbol names to their corresponding source-level variable names

    Returns:
        None. Modifies the error message in-place by updating e.args[0].
    r:  c                 3  s   | ]}|j  vr|j V  qd S rb   r  r  r  rh   ri   r    r   z(_suggest_torch_checks.<locals>.<genexpr>z.Unable to find user code corresponding to {%s}Nr   zG
To fix the error, insert one of the following checks before this call:z)You can add either: torch._check_is_size(z) or torch._check(z >=0) Note: torch._check_is_size(z*) could prevent data dependent errors thatz\ happen in a guard_size_oblivious(..) context by opting into guard_size_oblivious reasoning.z< See documentation on guard_size_oblivious for more details:zj https://pytorch.org/docs/stable/generated/torch.fx.experimental.symbolic_shapes.guard_size_oblivious.htmlztorch._check(r  r<  r   rO  c                 3  s(   | ] }d | dd  |  V  qdS )`z` with z or N)r	  r  r  rh   ri   r    s   c                 s  s   | ]}|j V  qd S rb   r  r  rh   rh   ri   r    r   z3

(These suggested fixes were derived by replacing z in z and its negation.))r^   r	  r   r   r  r  r`   r  r   rU  r  r  r  )r  r  r^   diffrD  r  Znot_cond_strvar_nameZsuggested_fixesr  ZfixZ
src_mappedrh   r  ri   _suggest_torch_checks  sB    



r   r~  c              	   C  s  t  }|durt| | tt}|j D ]\}}zt|}W n( tyj   t	
dt|| Y q,Y n0 |D ]\}}|t| }t|tjr|t|jj | qpt|tjrpt|jD ]6\}	}
t|
tjr|t|
jj | d|	 d qqpq,t| jtjjjrt| | 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.
    NzQpytree.tree_leaves_with_path failed for value of type {%s} in local variable {%s}z.shape[r  )r  r  r   r   r  r%  r0  tree_leaves_with_pathrc  r   r  r   Zkeystrr   r   r#   r   r   r   r   rT   r  r   r^   r   r	  r
  Booleanr   )r  r  r  varr   r!  rA  Zleafr  r  r   rh   rh   ri   2_suggest_fixes_for_data_dependent_error_non_strict$  s0    



(r$  zGenerator[None, None, None]c              	   c  sP   | j di }dd | D }|| j d< zdV  W || j d< n|| j d< 0 dS )a  
    Temporarily modifies unbacked_bindings in a node's metadata by removing the first element
    of each path, which corresponds to an effect token.

    This is used when processing nodes that have effect tokens as the first element in their
    unbacked_bindings paths. The context manager ensures that the original bindings are
    restored after the operation is complete.

    Args:
        node: The FX node whose unbacked_bindings will be temporarily modified

    Yields:
        None
    r+  c                 S  s&   i | ]\}}||r|d d n|qS rQ  rh   )r   r!  rA  rh   rh   ri   r$  d  r   z:_remove_effect_token_unbacked_bindings.<locals>.<dictcomp>N)r/  r  r%  )r   Zold_bindingsZnew_bindingsrh   rh   ri   &_remove_effect_token_unbacked_bindingsO  s    
r%  r4   )r   ra   c                 C  s$   | j }| j}||jv r |j| S |S rb   )r  r  r  )r   r  r)  rh   rh   ri   _get_placeholder_exprr  s
    

r&  )N)TN)NNNF)NF)NN)r   )N()  
__future__r   r   r   Ztorch._prims_commonr   r   r   r  r   rz  r  r   r  r   rr  r   r  r  r  r  r   	threadingr  r   r   collections.abcr	   r
   r   r   
contextlibr   r   Zdataclassesr   r   r   enumr   typingr   r   r   r   r   r   r   r   r   r   Ztyping_extensionsr   r   r   r    r   Ztorch.fxZtorch.fx.tracebackr   rW  Ztorch.utils._pytreer]  Z_pytreer0  r!   r"   r#   r  r$   r%   r&   r'   r   r(   r)   r*   r+   r   r,   Ztorch._utils_internalr-   Ztorch.fx.experimentalr.   r  Ztorch.fx.experimental.recordingr/   r0   r1   r2   r3   Ztorch.fx.experimental.sym_noder4   r5   Ztorch.typesr6   Ztorch.utils._ordered_setr7   Ztorch.utils._python_dispatchr8   Ztorch.utils._sympy.functionsr9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   Ztorch.utils._sympy.numbersrE   Ztorch.utils._sympy.printersrF   rG   r  rI   Ztorch.utils._sympy.solverJ   Ztorch.utils._sympy.symbolrK   rL   rM   Ztorch.utils._sympy.value_rangesrN   rO   rP   rQ   Ztorch.utils._tracebackrR   rS   typesrT   r  rV   r   rX   rY   rZ   r[   r   Z	InputListZDimList	getLoggerrj   r   RuntimeErrorr\   ro   Z_opsrL  rM  __all__r   r   r   r   rm   r   r  r8  r   r   r   r   r   r   rs   r   r   rt   r~   r  r   r  r  rv   rw   rx   ry   r   r   r   r7  r&  r   r   r}   rg  rW  rw  r   r  r  r  r   r   r   r  r   r  r   r   r  r  r  r  r   r  rq   rr   r  r   r   r   r   r   r|   r  r  r  r  r  r  r  rz   r{   r  r!  r%  r'  r(  r2  r5  r=  ra  r>  rZ  r[  r`  r   r   rc  rd  r   r   r   r   r  ZIndicatorTypesrt  ru  r  r  r  r  r  r  r?  r  r   r  r  r  ABCr  r  FutureWarningr  r  r  r  r  r  r  localr  rH  r   rT  rU  ru   r  r  ZInterpreterr  r  r  r  r  r   r$  r%  r&  rh   rh   rh   ri   <module>   s.  08

/'(#" y  6-0.	    2 f*.25j


"C" 3$b+
	 7Y3     [	                                 `;+"