
    9i                     "   S SK r S SKrS SKrS SKrS SKJr  S SKJrJr  S SK	J
r
JrJrJrJr  S SKrS SKJrJrJrJrJrJrJrJrJrJr  SSKJr  \(       a  S SKJr  S S	KJ r   S S
K!J"r"J#r#  / SQr$\RJ                  " \&5      r' " S S\5      r(\ RR                   " S S5      5       r* " S S5      r+\+r, " S S\+5      r- " S S\+5      r.SSS.S\/S\\0   S\\0   S\1\+S4   4S jjr2\ RR                   " S S5      5       r3\ RR                   " S S \35      5       r4\ RR                   " S! S"5      5       r5\ RR                   " S# S$\35      5       r6\ RR                   " S% S&\35      5       r7\\4\6\74   r8\ RR                   " S' S(5      5       r9S)\8S*\\0\0/\:S+   4   S,S-S\;\/\1\0\04   4   S.\:\1S/      S0\:\1S+\S1   \4      S2\;\/S34   S4\<S+   4S5 jr=SS6.S7\S\
4   S8\
S9\
S:\\/   S\
4
S; jjr>S\;\/\
4   4S< jr? " S= S>5      r@ " S? S@5      rASA rBSB\;\/\
4   S9\\;\/\
4   \1\
   \:\
   S4   4SC jrCSB\;\/\
4   S9\\;\/\
4   \1\
   \:\
   S4   S\:\8   4SD jrDS9\\;\/\
4   \1\
   \:\
   S4   4SE jrESF\/S9\\;\/\
4   \1\
   \:\
   4   S\\;\/\
4   \1\
   \:\
   4   4SG jrFg)H    N)defaultdict)autoEnum)AnyCallableOptionalTYPE_CHECKINGUnion)
_get_node_typeBUILTIN_TYPESkeystrLeafSpec
MappingKeySequenceKeySUPPORTED_NODEStree_flattentree_maptree_map_with_path   )ExportedProgram)Symbol)Source)ShapeEnvStrictMinMaxConstraint)
ConstraintDimdims*refine_dynamic_shapes_from_suggested_fixesAdditionalInputsc                   B    \ rS rSrSr\" 5       r\" 5       r\" 5       rSr	g)_DimHintType-   z
Enum for dynamic shape hints.
- AUTO means automatic inference of shape (static or dynamic).
- STATIC means static shape (always specialized).
- DYNAMIC means dynamic, will error out if specialized.
 N)
__name__
__module____qualname____firstlineno____doc__r   AUTOSTATICDYNAMIC__static_attributes__r#       [/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/export/dynamic_shapes.pyr!   r!   -   s     6DVFfGr-   r!   c                       \ rS rSr% \\S'   Sr\\   \S'   Sr	\\   \S'   Sr
\\   \S'   \S 5       r\S	 5       r\S
 5       rSSS jjrSrg)_DimHint:   typeNminmaxT_factoryc                  4    [        [        R                  5      $ N)r0   r!   r)   r#   r-   r.   r)   _DimHint.AUTOA   s    ))**r-   c                  4    [        [        R                  5      $ r7   )r0   r!   r+   r#   r-   r.   r+   _DimHint.DYNAMICE   s    ,,--r-   c                  4    [        [        R                  5      $ r7   )r0   r!   r*   r#   r-   r.   r*   _DimHint.STATICI   s    ++,,r-   c                     U R                   (       d  [        S[        U 5       S35      eUb  US:  d   S5       eUb  US:  d   S5       eUb  Ub  X::  d   S5       e[        U R                  XSS9$ )	N'z' object is not callabler   zmin must be non-negativezmax must be non-negativezmin must be <= maxF)r3   r4   r5   )r5   	TypeErrorr2   r0   )selfr3   r4   s      r.   __call___DimHint.__call__M   s|    }}aT
|+CDEE{cQhB(BB&{cQhB(BB&{ckSZM9MM7		seDDr-   r#   NN)returnr0   )r$   r%   r&   r'   r!   __annotations__r3   r   intr4   r5   boolstaticmethodr)   r+   r*   rA   r,   r#   r-   r.   r0   r0   :   sx    
C#C##Hhtn#+ + . . - -E Er-   r0   c            	       "   \ rS rSrSr\R                  5       r\R                  5       r\R                  5       rSSS.S\	S\
\   S\
\   4S jjrSS
 jrSS jrSS jrSS jrSS jrSS jrS	\	4S jrSS jr\S\	S\S\S	\	4S j5       rS rSrg)r   V   a	  
The ``Dim`` class allows users to specify dynamism in their exported
programs. By marking a dimension with a ``Dim``, the compiler associates the
dimension with a symbolic integer containing a dynamic range.

The API can be used in 2 ways: Dim hints (i.e. automatic dynamic shapes:
``Dim.AUTO``, ``Dim.DYNAMIC``, ``Dim.STATIC``), or named Dims (i.e.
``Dim("name", min=1, max=2)``).

Dim hints provide the lowest barrier to exportability, with the user only
needing to specify if a dimension if dynamic, static, or left for the
compiler to decide (``Dim.AUTO``). The export process will automatically
infer the remaining constraints on min/max ranges and relationships between
dimensions.

Example::

    class Foo(nn.Module):
        def forward(self, x, y):
            assert x.shape[0] == 4
            assert y.shape[0] >= 16
            return x @ y


    x = torch.randn(4, 8)
    y = torch.randn(8, 16)
    dynamic_shapes = {
        "x": {0: Dim.AUTO, 1: Dim.AUTO},
        "y": {0: Dim.AUTO, 1: Dim.AUTO},
    }
    ep = torch.export(Foo(), (x, y), dynamic_shapes=dynamic_shapes)

Here, export would raise an exception if we replaced all uses of ``Dim.AUTO`` with ``Dim.DYNAMIC``,
as ``x.shape[0]`` is constrained to be static by the model.

More complex relations between dimensions may also be codegened as runtime assertion nodes by the compiler,
e.g. ``(x.shape[0] + y.shape[1]) % 4 == 0``, to be raised if runtime inputs do not satisfy such constraints.

You may also specify min-max bounds for Dim hints, e.g. ``Dim.AUTO(min=16, max=32)``, ``Dim.DYNAMIC(max=64)``,
with the compiler inferring the remaining constraints within the ranges. An exception will be raised if
the valid range is entirely outside the user-specified range.

Named Dims provide a stricter way of specifying dynamism, where exceptions are raised if the compiler
infers constraints that do not match the user specification. For example, exporting the previous
model, the user would need the following ``dynamic_shapes`` argument::

    s0 = Dim("s0")
    s1 = Dim("s1", min=16)
    dynamic_shapes = {
        "x": {0: 4, 1: s0},
        "y": {0: s0, 1: s1},
    }
    ep = torch.export(Foo(), (x, y), dynamic_shapes=dynamic_shapes)

Named Dims also allow specification of relationships between dimensions, up
to univariate linear relations.  For example, the following indicates one
dimension is a multiple of another plus 4::

    s0 = Dim("s0")
    s1 = 3 * s0 + 4

Nr3   r4   namer3   r4   c                    SSK Jn  Uc  SOUnUc  UOUnXe:  d   SU SU 35       eUR                  5       (       d
   SU 35       eXl        XPl        X`l        g )Nr   int_ooz(Cannot create Dim with inconsistent min=, max=z)Dim name must be a valid identifier, got )torch.utils._sympy.numbersrO   isidentifierr$   r3   r4   )r@   rL   r3   r4   rO   _min_maxs          r.   __init__Dim.__init__   sn     	6KqSv#{WFse6RUQVWW{  ""V&OPTv$VV"r-   rD   c                    ^ [        T5      [        La  [        ST SU R                   S35      eU R	                  U4S j5      $ )NzAttempted to add z to m, where an integer was expected. (Only increasing linear operations with integer coefficients are supported.)c                    > U T-   $ r7   r#   xothers    r.   <lambda>Dim.__add__.<locals>.<lambda>   	    a%ir-   r2   rF   NotImplementedErrorr$   _deriver@   r\   s    `r.   __add__Dim.__add__   sK    ;c!%#E7$t}}o >_ _  ||/00r-   c                 
    X-   $ r7   r#   rc   s     r.   __radd__Dim.__radd__   
    |r-   c                    ^ [        T5      [        La  [        ST SU R                   S35      eU R	                  U4S j5      $ )NzAttempted to subtract z from rX   c                    > U T-
  $ r7   r#   rZ   s    r.   r]   Dim.__sub__.<locals>.<lambda>   r_   r-   r`   rc   s    `r.   __sub__Dim.__sub__   sL    ;c!%(vdmm_ E_ _  ||/00r-   c                 4    [        SU R                   S35      e)NzAttempted to negate zN. (Only increasing linear operations with integer coefficients are supported.))ra   r$   rc   s     r.   __rsub__Dim.__rsub__   s&    !"4==/ 2[ [
 	
r-   c                    ^ [        T5      [        Ld  TS::  a  [        ST SU R                   S35      eU R	                  U4S j5      $ )Nr   zAttempted to multiply z with zu, where a positive integer was expected. (Only increasing linear operations with integer coefficients are supported.)c                    > U T-  $ r7   r#   rZ   s    r.   r]   Dim.__mul__.<locals>.<lambda>   r_   r-   r`   rc   s    `r.   __mul__Dim.__mul__   sR    ;c!UaZ%(vdmm_ E_ _  ||/00r-   c                 
    X-  $ r7   r#   rc   s     r.   __rmul__Dim.__rmul__   ri   r-   c                 P    SSK Jn  [        U" U" U R                  5      5      5      $ )Nr   )sympify)sympyr{   strr$   )r@   fnr{   s      r.   _derived_nameDim._derived_name   s    !2gdmm,-..r-   c                 8    [        U R                  U5      X5      $ r7   )_DerivedDimr   r@   r~   s     r.   rb   Dim._derive   s    4--b14<<r-   min_max_c                     SSK Jn  US:X  a  S nX#:X  a  S nUc	  Uc  SU  S3$ Uc	  SU  SU S3$ Uc	  SU  SU S3$ SU  SU S	U S3$ )
Nr   rN      zDim('z')z', max=)z', min=rP   )rQ   rO   )rL   r   r   rO   s       r.   	_readableDim._readable   s    519D>D<DL4&##<4&vQ//<4&vQ//tfGD6vQ77r-   c                 l    [         R                  U R                  U R                  U R                  5      $ r7   )r   r   r$   r3   r4   r@   s    r.   __repr__Dim.__repr__   s!    }}T]]DHHdhh??r-   )r$   r4   r3   )rD   r   )r$   r%   r&   r'   r(   r0   r)   r+   r*   r}   r   rF   rU   rd   rg   rm   rp   ru   rx   r   rb   rH   r   r   r,   r#   r-   r.   r   r   V   s    =~ ==?D G__F 26D!)#<DSM11
1/3 /
= 8 83 8c 8c 8 8@r-   r   c                   F    \ rS rSrSrS\4S jr\S 5       r\S 5       r	Sr
g)	
_StaticDim   z
Class for static :func:`Dim` types.

This class is only for setting and checking static dim constraints,
and the user should never interact with it.
valuec                 0    [        U5      U l        Xl        g r7   )r}   r$   r   )r@   r   s     r.   rU   _StaticDim.__init__   s    E

r-   c                     U R                   $ r7   r   r   s    r.   r3   _StaticDim.min       zzr-   c                     U R                   $ r7   r   r   s    r.   r4   _StaticDim.max   r   r-   )r$   r   N)r$   r%   r&   r'   r(   rF   rU   propertyr3   r4   r,   r#   r-   r.   r   r      s;    c     r-   r   c                   Z    \ rS rSrSrS\S\S\4S jr\	S 5       r
\	S 5       rS	 rS
 rSrg)r   i  a  
Class for derived :func:`Dim` types.

Currently we only support increasing linear expressions with integer coefficients.
In other words, a derived Dim can always be written in the form Ax + B, where
x is a regular Dim (i.e., non-derived Dim), A and B are integers, and A is positive.
(In particular, the latter ensures that x < y => Ax + B < Ay + B.)
These restrictions on the form of derived Dims makes the metatheory simpler: e.g.,
it simplifies computing ranges for derived Dims, solving for underlying regular Dims,
deciding equalities between derived Dims, and so on.

The function lambda x: Ax + B is expressed by `fn`, where x is a normal Dim, `root`.
The range of a derived Dim is computed by mapping `fn` over the range of its `root`.
rL   rootr~   c                 (    Xl         X l        X0l        g r7   )r$   r   r~   )r@   rL   r   r~   s       r.   rU   _DerivedDim.__init__  s    	r-   c                 B   SSK Jn  SSKJn  U R                  R
                  U* L a  U* $ U R                  U" U R                  R
                  5      5      nU R                  nUS:  d/   SU R                   SUR                   SUR
                   S35       e[        U5      $ )Nr   IntegerrN   zExpected derived min value of z9 to be >= 0. Please specify an appropriate min value for  (currently ).)	r|   r   rQ   rO   r   r3   r~   r$   rF   )r@   r   rO   _min_symintr   s        r.   r3   _DerivedDim.min  s     	"599==VG#7Ngggdiimm45yya 	
,T]]O <;;?==/ J((2'	

 ;r-   c           
         SSK Jn  SSKJn  U R                  R
                  UL a  U$ U R                  U" U R                  R
                  5      5      nU R                  nU[        R                  S-
  ::  dC   SU R                   S[        R                  S-
   SUR                   SUR
                   S	3	5       e[        U5      $ )
Nr   r   rN   r   zExpected derived max value of z
 to be <= z.. Please specify an appropriate max value for r   r   )r|   r   rQ   rO   r   r4   r~   sysmaxsizer$   rF   )r@   r   rO   _max_symintr   s        r.   r4   _DerivedDim.max+  s     	"599==F"Mgggdiimm45yyckkAo- 	
,T]]O:ckkTUoEV W;;?==/ J((2'	
-
 ;r-   c                 \   ^ ^ [        T R                  T5      T R                  UU 4S j5      $ )Nc                 2   > T" TR                  U 5      5      $ r7   )r~   )r[   r~   r@   s    r.   r]   %_DerivedDim._derive.<locals>.<lambda>F  s    bnr-   )r   r   r   r   s   ``r.   rb   _DerivedDim._derive?  s*     r"II$
 	
r-   c                     U R                   $ r7   )r$   r   s    r.   r   _DerivedDim.__repr__I  s    }}r-   )r$   r~   r   N)r$   r%   r&   r'   r(   r}   r   r   rU   r   r3   r4   rb   r   r,   r#   r-   r.   r   r     sS    S   
    &    &
r-   r   rK   namesr3   r4   rD   .c                 2   ^ ^ [        UU 4S jU 5       5      $ )zX
Util to create multiple :func:`Dim` types.

Returns:
    A tuple of :func:`Dim` types.
c              3   :   >#    U  H  n[        UTTS 9v   M     g7f)rK   N)r   ).0rL   r4   r3   s     r.   	<genexpr>dims.<locals>.<genexpr>V  s     ?Ts,s   )tuple)r3   r4   r   s   `` r.   r   r   M  s     ????r-   c                   .    \ rS rSr% Sr\\S'   \\S'   Srg)_ConstraintTargetiY  z*
This represents input tensor dimensions.
t_iddimr#   N)r$   r%   r&   r'   r(   rF   rE   r,   r#   r-   r.   r   r   Y  s     I	Hr-   r   c                   f    \ rS rSr% Sr\\S'   S\S'   SS jrS rS	 r	S
 r
S rS r\S 5       rSrg)_Constraintic  z
This represents a Dim describing a constraint target.

`name` is the name of the Dim.
`constraint_range` contains the min/max bounds of the Dim.
rL   r   constraint_rangeNc                     SSK Jn  SSKJn  SSKJn  Uc  UnU" U R                  R                  U" XS9-  SS9n[        U R                  U R                  U R                  U5      $ )Nr   r   rN   ValueRangeslowerupperFvr	warn_only)%torch.fx.experimental.symbolic_shapesr   rQ   rO   torch.utils._sympy.value_rangesr   r   r   r   r   r   rL   )r@   r   r   r   rO   r   r   s          r.   _clone_with_range_Constraint._clone_with_rangeo  sc    P5?=E1$$''+E*OO
 IIHHII	
 	
r-   c                      U R                  US9$ )Nr   r   r@   r   s     r.   __ge___Constraint.__ge__      %%E%22r-   c                 &    U R                  US-   S9$ )Nr   r   r   r   s     r.   __gt___Constraint.__gt__      %%EAI%66r-   c                      U R                  US9$ )Nr   r   r@   r   s     r.   __le___Constraint.__le__  r   r-   c                 &    U R                  US-
  S9$ )Nr   r   r   r   s     r.   __lt___Constraint.__lt__  r   r-   c                     [        S5      e)NzCannot determine truth value of _Constraint. If you are trying to combine _Constraint's with logical connectives, you can specify them separately instead.)r?   r   s    r.   __bool___Constraint.__bool__  s    
 7
 	
r-   c                     U R                   U R                  U R                  R                  R                  U R                  R                  R
                  S.$ N)r   r   r3   r4   r   r   r   r   r   r   r   s    r.   serializable_spec_Constraint.serializable_spec  sF     II88((++11((++11	
 	
r-   r#   )r   N)r$   r%   r&   r'   r(   r}   rE   r   r   r   r   r   r   r   r   r,   r#   r-   r.   r   r   c  sF     I..
(3737	
 
 
r-   r   c                   8    \ rS rSr% Sr\\S'   S\S'   \\S'   Srg)	_PhantomRooti  a  
This represents the root of a derived Dim where the root does not directly
specify the shape of any input dimension, but the derived Dim does.

e.g., the input shapes 2*dim and dim + 1 are related via a "phantom" dim.

The fields `name`, `constraint_range`, and `val` carried by a phantom root
help create a symbol for it. Any derived dims with this phantom root are
backed by expressions over this symbol.
rL   r   r   valr#   N)	r$   r%   r&   r'   r(   r}   rE   rF   r,   r#   r-   r.   r   r     s    	 I..	Hr-   r   c                   \    \ rS rSr% Sr\\S'   S\S'   \\\	4   \S'   \
\S'   \S 5       rS	rg
)_DerivedConstrainti  a+  
This represents a derived Dim, whose root is either a regular constraint target
(which directly specifies the shape of some input dimension) or a phantom root
(which does so indirectly).

It can be thought of as a subclass of `_Constraint`, except that it does not
support <, <=, >, >= operations.
rL   r   r   r   r~   c                     U R                   U R                  U R                  R                  R                  U R                  R                  R
                  S.$ r   r   r   s    r.   r   $_DerivedConstraint.serializable_spec  sF     II88((++11((++11	
 	
r-   r#   N)r$   r%   r&   r'   r(   r}   rE   r
   r   r   r   r   r   r,   r#   r-   r.   r   r     s<     I..
!</
00L
 
r-   r   c                   (    \ rS rSrSr\S 5       rSrg)_RelaxedConstrainti  as  
This represents a dim marked with Dim.AUTO/DYNAMIC (i.e. mark_dynamic() or maybe_mark_dynamic()),
which leaves relations & min/max ranges for inference, instead of requiring explicit specification.
The intention is for constraint violations to not be raised if produce_guards() finds equalities or
relations between a _RelaxedConstraint and another type of _Constraint.
c                 4    U R                   U R                  S.$ )Nr   r   r   r   s    r.   r   $_RelaxedConstraint.serializable_spec  s     II88
 	
r-   r#   N)r$   r%   r&   r'   r(   r   r   r,   r#   r-   r.   r   r     s     
 
r-   r   c                   `    \ rS rSr% Sr\\S'   \R                  " SSS9r	\
\\\4      \S'   Srg)	_IntWrapperi  z
Dummy wrapper class to wrap around integer inputs so that when we parse the
dynamic_shapes structure, we can mark if any of the integers were marked as
dynamic.
r   FN)initdefaultdynamismr#   )r$   r%   r&   r'   r(   rF   rE   dataclassesfieldr   r   r
   r0   r,   r#   r-   r.   r   r     s8     
H/:/@/@D0HhuXs]+, r-   r   
constraintget_sourcesr   	shape_envr   source_pairs)r   r   derived_equalities)r   r   phantom_symbolsr   relaxed_sourcesc                   ^ U" U R                   U R                  5      nU(       d  gUtmn	UR                  U4S jU	 5       5        [        U [        5      (       ai  U R
                  U;   a4  X0R
                     u  pU" X5      n	UR                  U4S jU	 5       5        gU R                   U R                  4X0R
                  '   g[        U [        5      (       Gaj  [        U R                  [        5      (       d5  U" U R                  R                   U R                  R                  5      S   nOU R                  R
                  U;   a  X`R                  R
                     nOUR                  U R                  R                  [        R                  R                  R                  U R                  R
                  5      [        R                  R                   R"                  R$                  R&                  U R                  R(                  S9nXU R                  R
                  '   U R*                  nUR-                  TX45        g[        U [.        5      (       a  UR1                  T5        gg)z
Updates `source_pairs`, `derived_equalities`, and `phantom_symbols` (which become
fields of `EqualityConstraint`) based on a given input `constraint`.
Nc              3   ,   >#    U  H	  nTU4v   M     g 7fr7   r#   r   other_sourcesources     r.   r   &_process_equalities.<locals>.<genexpr>  s     Q=<.=   c              3   ,   >#    U  H	  nTU4v   M     g 7fr7   r#   r  s     r.   r   r	    s       ;H<&=r
  r   )r   r  dynamic_dimconstraint_dim)r   r   extend
isinstancer   rL   r   r   r   create_symbolr   torch_dynamor  ConstantSourcefxexperimentalsymbolic_shapes
DimDynamicr+   r   r~   appendr   add)r   r   r   r   r   r  r  r  sourcesother_sourcesshared_t_id
shared_dimr   r~   r  s                 @r.   _process_equalitiesr    s    *//:>>:G$F] Q=QQ*k**??e#&+OO&<#K'@M  ;H   '1ooz~~%FE//"	J 2	3	3*//<88z33Z__5H5HI!LD ##6&';';< !.."++ ==//>>z?S?ST % 5 5 E E P P X X#-??#C#C	 /  9=
 4 45]] 	!!64"45	J 2	3	3F# 
4r-   	tree_namefunctreedynamic_shapesr   c                H  ^ ^^	^
^^^ S mUU U4S jm [        TU/UQ7ST06$ ! [         as  nSUR                  S   ;   aZ  U(       d   S5       eT(       d   S5       eS m
U4S	 jmU	U
UU4S
 jm	[        UTS9u  pVU H  n[        UT5      u  pXT	" Xh/ 5        M     e SnAff = f)a  
Customized tree_map for mapping pytrees to dynamic_shapes.

For built-in types (e.g., standard collections) this behaves exactly like tree_map.

OTOH for a user-defined class C registered with pytree, we cannot assume that a C
containing tensors can be mapped to a C containing dynamic shapes (i.e., C may not
be a polymorphic container). In that case we use the flattened form of C instead.
Thus a C(**tensors) that flattens to (**tensors) will map to (**dynamic_shapes).

Args:
    func: function to apply to each (int, float, str, bool, None, torch.Tensor)
    tree: input pytree
    dynamic_shapes: zero or more (typically one) dynamic_shapes to match

Returns:
    output pytree mapping func to each (int, float, str, bool, None, torch.Tensor)
c                 &    [        U 5      [        ;  $ r7   )r   r   )ts    r.   is_leaf$_tree_map_with_path.<locals>.is_leafO  s    
 a 55r-   c                    > [        U5      nU[        ;   a)  [        T[        U   R                  U5      S   /UQ7ST06$ T" X/UQ76 $ )Nr   r'  )r   r   r   
flatten_fn)pathr&  r#  typfr!  r'  s       r.   r-  _tree_map_with_path.<locals>.fV  se    Q/! &$//215    	  1.11r-   r'  mismatchr   z2Cannot be a mismatch if there is no dynamic_shapesz7Must provide a tree_name when there might be a mismatchc                     U [         L a  [        X   5      $ U [        [        4;   a  Ub   e[	        U5      $ [        SU  35      e)NzDid not expect type )dictr   listr   r   AssertionError)type_contextis      r.   _key!_tree_map_with_path.<locals>._keyn  sI    D=%gj11T5M)"?*?&q>)$';E7%CDDr-   c                 D   > SSK JnJn  U" UR                  ST SU  3SS9e)Nr   	UserErrorUserErrorTypez,Detected mismatch between the structure of `z` and `dynamic_shapes`: dynamic_shapes_validation	case_name)torch._dynamo.excr;  r<  INVALID_INPUT)msgr;  r<  r   s      r.   raise_mismatch_error1_tree_map_with_path.<locals>.raise_mismatch_errorw  s2    F!//B9+Mefiejk9 r-   c                 6  > [        U5      n[        U [        5      (       a  g [        U[        5      (       a  T" ST U SU R                   SU S35        U R                  UR                  :w  a*  T" ST U SU R                   SU SUR                   3	5        [	        U R
                  5      [	        UR
                  5      :w  a=  T" ST U S[	        U R
                  5       SU S[	        UR
                  5       S3
5        U R                  [        L a  [        U R                  5      [        UR                  5      :w  a*  T" ST U SU R                   SU SUR                   3	5        [        [        UR                  UR
                  5      5      nU R                   Vs/ s H  oTU   PM	     nnOUR
                  n[        [        U R
                  U5      5       H2  u  nu  pT
" UU	UT" U R                  U R                  U5      /-   5        M4     g s  snf )	N`z` is a z, but `dynamic_shapesz` is notz` has z elements, but `dynamic_shapesz	 elementsz` has keys )r   r  r   r2   lenchildren_specsr1  sortedr5  zip	enumerate)r"  r#  r+  rendered_path_remapkdynamic_shapes_children_specsr6  tree_dynamic_shapes__comparer7  rC  r   s             r.   rR  %_tree_map_with_path.<locals>._compare  s$    !'tdH--nh77(I;}oWTYYK H..;_HF 99 3 33(I;}oWTYYK H..;_GNDWDWCXZ t**+s>3P3P/QQ(I;}oVC@S@S<T;U V..;_F3~GdGdCeBffoq 99$dll+vn6L6L/MM,	{=/T\\N S22?NLbLbKce "N22N4Q4QRF IM4U1AY14U14B4Q4Q13<++-JK4/A/ 'TYYa @AA4 5Vs   #Hr'  N)r   
ValueErrorargsr   )r!  r"  r   r#  e_	tree_spec
other_treeother_tree_specrR  r7  r-  r'  rC  s   ` `      @@@@@r.   _tree_map_with_pathr\  6  s    262J!!TLNLGLL H" "W#WW>WWW9E) )V (g>LA,
%1*g%F"R8 - 	QHs   $ 
B!A.BB!c                 P   [        U [        5      (       a  U R                  5       n [        U [        R                  R
                  5      (       a   [        R                  " U R                  5      O[        R                  " U 5      nUb  UO0 nUR                  " U0 UD6R                  $ r7   )r  r   moduler  nnModuleinspect	signatureforwardbind	arguments)r-  rV  kwargsrb  s       r.   _combine_argsrg    s     !_%%HHJ a)) 	!))$q! 
 )VrF>>4*6*444r-   c                   :    \ rS rSrSrS rS rS rS rS
S jr	S	r
g)ShapesCollectioni  a  
Builder for dynamic_shapes.
Used to assign dynamic shape specifications to tensors that appear in inputs.

This is useful particularly when :func:`args` is a nested input structure, and it's
easier to index the input tensors, than to replicate the structure of :func:`args` in
the :func:`dynamic_shapes` specification.

Example::

    args = {"x": tensor_x, "others": [tensor_y, tensor_z]}

    dim = torch.export.Dim(...)
    dynamic_shapes = torch.export.ShapesCollection()
    dynamic_shapes[tensor_x] = (dim, dim + 1, 8)
    dynamic_shapes[tensor_y] = {0: dim * 2}
    # This is equivalent to the following (now auto-generated):
    # dynamic_shapes = {"x": (dim, dim + 1, 8), "others": [{0: dim * 2}, None]}

    torch.export(..., args, dynamic_shapes=dynamic_shapes)

To specify dynamism for integers, we need to first wrap the integers using
_IntWrapper so that we have a "unique identification tag" for each integer.

Example::

    args = {"x": tensor_x, "others": [int_x, int_y]}
    # Wrap all ints with _IntWrapper
    mapped_args = pytree.tree_map_only(int, lambda a: _IntWrapper(a), args)

    dynamic_shapes = torch.export.ShapesCollection()
    dynamic_shapes[tensor_x] = (dim, dim + 1, 8)
    dynamic_shapes[mapped_args["others"][0]] = Dim.DYNAMIC

    # This is equivalent to the following (now auto-generated):
    # dynamic_shapes = {"x": (dim, dim + 1, 8), "others": [Dim.DYNAMIC, None]}

    torch.export(..., args, dynamic_shapes=dynamic_shapes)
c                     0 U l         g r7   _shapesr   s    r.   rU   ShapesCollection.__init__  s	    r-   c                    [        U[        R                  [        45      (       d   S[	        U5       35       e[        U5      nX0R                  ;   a"  U R                  U   nX$:X  d   SU SU 35       eg X R                  [        U5      '   g )Nz:Cannot assign shape to non-tensor or non-_IntWrapper type z0Shapes assigned to input do not match: expected z, got )r  r  Tensorr   r2   idrl  )r@   r&  shaper   _shapes        r.   __setitem__ShapesCollection.__setitem__  s    !ellK899 	
Ha	R	
9 !u<<\\$'F? B6(&QVPWX? #(LLAr-   c                 r    [        U5      nX R                  ;  a  0 U R                  U'   U R                  U   $ r7   )rp  rl  )r@   r&  r   s      r.   __getitem__ShapesCollection.__getitem__  s2    !u||#!#DLL||D!!r-   c                 ,    [        U R                  5      $ r7   )rG  rl  r   s    r.   __len__ShapesCollection.__len__  s    4<<  r-   Nc                    ^ ^ [        5       mU U4S jn[        XU5      n[        XE5      n[        U4S jT R                   5       5      (       a  [        S5      eU$ )ze
Generates the :func:`dynamic_shapes` pytree structure according to :func:`args` and :func:`kwargs`.
c                 |   > [        U5      nUTR                  ;   a   TR                  U5        TR                  U   $ g r7   )rp  rl  r  )r+  r&  r   r@   t_idss      r.   
find_shape3ShapesCollection.dynamic_shapes.<locals>.find_shape  s5    a5Dt||#		$||D))r-   c              3   ,   >#    U  H	  oT;  v   M     g 7fr7   r#   )r   r   r}  s     r.   r   2ShapesCollection.dynamic_shapes.<locals>.<genexpr>  s     :\T5 \r
  zSome tensors that were assigned shapes were not found in args. Maybe such tensors were copied when passing them as args? Maybe such tensors are contained in classes that were not registered with pytree?)setrg  r\  anyrl  rU  )r@   mrV  rf  r~  combined_argsr#  r}  s   `      @r.   r#  ShapesCollection.dynamic_shapes  sY    
 	 &av6,ZG:T\\:::d 
 r-   rk  r7   )r$   r%   r&   r'   r(   rU   rs  rv  ry  r#  r,   r#   r-   r.   ri  ri    s!    &P( "!r-   ri  c                   8    \ rS rSrSrS rS	S jrS	S jrS rSr	g)
r   i   a	  
Infers dynamic_shapes based on additional inputs.

This is useful particularly for deployment engineers who, on the one hand, may
have access to ample testing or profiling data that can provide a fair sense of
representative inputs for a model, but on the other hand, may not know enough
about the model to guess which input shapes should be dynamic.

Input shapes that are different than the original are considered dynamic; conversely,
those that are the same as the original are considered static. Moreover, we verify
that the additional inputs are valid for the exported program. This guarantees that
tracing with them instead of the original would have generated the same graph.

Example::

    args0, kwargs0 = ...  # example inputs for export

    # other representative inputs that the exported program will run on
    dynamic_shapes = torch.export.AdditionalInputs()
    dynamic_shapes.add(args1, kwargs1)
    ...
    dynamic_shapes.add(argsN, kwargsN)

    torch.export(..., args0, kwargs0, dynamic_shapes=dynamic_shapes)
c                     / U l         g r7   	_examplesr   s    r.   rU   AdditionalInputs.__init__;  s	    r-   Nc                     [        U5      [        L d   SU S35       eUb  [        U5      [        L d   SU S35       eU R                  R	                  X45        g)z3
Additional input :func:`args` and :func:`kwargs`.
zRepresentative args z must be a tupleNzRepresentative kwargs z must be None or a dict)r2   r   r1  r  r  )r@   rV  rf  s      r.   r  AdditionalInputs.add>  sf    
 DzU"Q&:4&@P$QQ"~f!5 	
$VH,CD	
5 	tn-r-   c                     X#4/U R                   Q VVs/ s H  u  p#[        S [        XU5      5      PM     snntpES n[        UU/UQ7SS 06$ s  snnf )z
Infers a :func:`dynamic_shapes` pytree structure by merging shapes of the
original input :func:`args` and :func:`kwargs` and of each additional input
args and kwargs.
c                 n    [        U[        R                  5      (       a  [        UR                  5      $ U$ r7   )r  r  ro  r   rq  )r+  r&  s     r.   r]   1AdditionalInputs.dynamic_shapes.<locals>.<lambda>R  s%    *Q2M2Magg TST Tr-   c                 d  ^  [        U 4S jU 5       5      (       d  [        ST 4U-    S35      e[        T [        5      (       a@  [        T [        5      (       d+  [        U 4S jU 5       5      (       a  g [
        R                  $ [        U 4S jU 5       5      (       d  [        ST 4U-    S35      eg )Nc              3   R   >#    U  H  n[        T5      [        U5      :H  v   M     g 7fr7   )r2   )r   r\   vs     r.   r   JAdditionalInputs.dynamic_shapes.<locals>._mark_dynamism.<locals>.<genexpr>Y  s     D8%tAw$u+-8s   $'z^The following inputs were found to have differing types, so they cannot be marked as dynamic: .c              3   ,   >#    U  H	  oT:H  v   M     g 7fr7   r#   r   other_vr  s     r.   r   r  `  s     <8!|8r
  c              3   ,   >#    U  H	  oT:H  v   M     g 7fr7   r#   r  s     r.   r   r  e  s     @xGa<xr
  z`The following inputs were found to have differing values, but they cannot be marked as dynamic: )allrU  r  rF   rG   r   r+   )r  other_vss   ` r.   _mark_dynamism7AdditionalInputs.dynamic_shapes.<locals>._mark_dynamismX  s    D8DDD <=>D8O;LAO 
 !S!!*Q*=*=<8<<<;;&@x@@@$ABC@QQRT  r-   r'  c                 $    [        U 5      [        L $ r7   )r2   rF   )r6  s    r.   r]   r  p  s    d1gnr-   )r  r\  rg  r   )r@   r  rV  rf  r#  other_dynamic_shapesr  s          r.   r#  AdditionalInputs.dynamic_shapesI  s     #' A$.. A1

 !B	  Tav. !B1
-	( 
 "
 -	
 	
91
s   #Ac                     UR                  5       nU R                   H8  u  p4[        R                  R                  R                  X#U=(       d    0 5        M:     g)zG
Verifies that an exported program is valid for each additional input.
N)r^  r  r  export_unlift#_check_input_constraints_for_module)r@   epepmrV  rf  s        r.   verifyAdditionalInputs.verifys  s@    
 iik NNLDLL  DD6<R +r-   r  r7   )
r$   r%   r&   r'   r(   rU   r  r#  r  r,   r#   r-   r.   r   r      s    4	.(
T	r-   r   c                  2    Sn [         R                  U 5        g )NzTUsing None as a dynamic shape dimension is deprecated. Please use Dim.STATIC instead)logwarning)rB  s    r.   %_warn_on_None_dynamic_shape_dimensionr    s    	( 
 KKr-   r  c                   ^^^^	^
 SSK JmJm  Ub  [        U5      S:X  a  g[	        U[
        [        45      (       a  [        U5      " U R                  5       5      n 0 mUUU4S jm	UUU	4S jm
[	        U[        [
        [        45      (       d   e[	        U[        5      (       a  [        UR                  5       5      n[        U R                  5       5      n[        U5      [        U5      :w  aX  SU SU S3n[        U 5      S	:X  a)  US   U;  a   [	        XS      [        5      (       a  US
-  nOUS-  nT" TR                  USS9eUUU
4S jn[        XPUSS9  g)zz
Checks the dynamic_shapes specification for correctness,
using combined args + kwargs as reference for inputs structure.
r   r:  Nc           
        > U R                   T;   a  TU R                      u  pU R                  U:w  d  U R                  U:w  ar  [        R	                  U R                   X5      n[        R	                  U R                   U R                  U R                  5      nT" TR
                  SU SU SU  S35      eg U R                  U R                  4TU R                   '   g )NzFound different definitions z and z! for the same symbolic dimension !)r$   r3   r4   r   r   rA  )r   r   r   this_that_r;  r<  boundss        r.   check_same_bounds0_check_dynamic_shapes.<locals>.check_same_bounds  s    <<6!-JDww$#''T/cllD?cllCGGSWWE!//25'ug F77:e1>  #2 %(GGSWW#5F3<< r-   c                   > [        U[        5      (       a  UR                  5        Hw  u  p4[        U[        5      (       a
  T" U5        M$  Uc  [	        5         M3  [        U[
        [        45      (       a  MP  T" TR                  SU SU S[        U 5       SU< S3	SS9e   g [        U[        [        45      (       a  [        U5      [        UR                  5      :w  aQ  T" TR                  SU S[        U 5       S	UR                   S
[        UR                  5       S[        U5       S3SS9e[        U5       Hw  u  p4[        U[        5      (       a
  T" U5        M$  Uc  [	        5         M3  [        U[
        [        45      (       a  MP  T" TR                  SU SU S[        U 5       SU< S3	SS9e   g Ub"  T" TR                  SU S[        U 5       S3SS9eg )Nz%Unexpected dimension mapped to index z in input tensor shape  specified at `dynamic_shapeszP` (expected None, an int, a Dim, Dim.AUTO, Dim.STATIC, or Dim.DYNAMIC,  but got z	 instead)r=  r>  zExpected dynamic shape spec z5` to have the same length as the actual tensor shape z (expected z
, but got zUnexpected dimension #zO` (expected None, an int, a Dim, Dim.AUTO, Dim.STATIC, or Dim.DYNAMIC, but got zUnexpected input tensor shape z` (expected either a list/tuple of dimensions, or a dict mapping indices to dimensions, where each dimension is an int, a Dim, Dim.AUTO, Dim.STATIC, or Dim.DYNAMIC))r  r1  itemsr   r  rF   r0   rA  r   r   r2  rG  rq  rK  )r+  tensorrq  r6  r   r;  r<  r  s        r.   check_symbols,_check_dynamic_shapes.<locals>.check_symbols  s   eT""++-c3''%c*[9;$S3/::#%33?sBYZ_Y` a77=d|n E$$'7)5 #>  ( t}--5zS..!//25'9VW]^bWcVd eJJP,, X!!$V\\!2 3:c%j\T :  $E*c3''%c*[9;$S3/::#%3303J5' R77=d|n E##&'4 #>  + ++07TU[\`UaTb c` a 6  r-   zWWhen `dynamic_shapes` is specified as a dict, its top-level keys must be the arg names z  of `inputs`, but here they are z. r   zSince here `inputs` is a list/tuple enclosing a single dict, maybe you just forgot to enclose `dynamic_shapes` in a list/tuple?zwAlternatively, you could also ignore arg names entirely and specify `dynamic_shapes` as a list/tuple matching `inputs`.r=  r>  c                 r  > [        U[        R                  5      (       a
  T" XU5        g [        U[        5      (       aB  [        U[        5      (       a  [        S5      eUb  [        U[        [        45      (       d   eg g Ub3  [        U 5      nT" TR                  SU SU S[        U5       SU S3	SS9eg )	NzdUnable to specify input integers as dynamic through named Dims. Please use Dim.AUTO/DYNAMIC instead.zCannot associate shape r  z` to non-tensor type z at `inputsz` (expected None)r=  r>  )r  r  ro  r   _DimrU  rF   r0   r   rA  r2   )r+  r&  dynamic_shaperL  r;  r<  r  s       r.   check_shape*_check_dynamic_shapes.<locals>.check_shape  s    a&&$=1;''-.. A  !(J}sHo,V,VVV,V(( &t!//-m_<YZgYh i**.q'+m_L]_9	  )r-   inputsr  )r@  r;  r<  rG  r  r   r2  r2   valuesr1  keysrI  rA  r\  )r  r#  got_keysexpected_arg_namesrB  r  r;  r<  r  r  r  s         @@@@@r.   _check_dynamic_shapesr    s]    ;^!4!9.5$-00^,]-A-A-CD)+F6.` ntUD&9::::.$''++-.!-"4"4"67(v&899));(< =!!)
".  M"a'&q)9}-BCTJJY
 V ++S<W ( NhWr-   c                   ^^^^	^
^^^^ SSK JmJm  Ub  [        U5      S:X  a  / $ [	        U[
        [        45      (       a  [        U5      " U R                  5       5      n [        [        5      m0 m/ m
/ m	UUU
UU4S jmS	U	UU4S jjmU4S jmU4S jn[        X USS9  T
 H.  nUR                  R                  nUT;   d  M!  TU   S   Ul        M0     TR                  5        H  nT	R                  U5        M     T	$ )
zL
Reads the dynamic_shapes specification and produces a list of constraints.
r   r:  c                 ,  >^ ^^^
^ SS K m
SSKJn  SSKJm  SSKJn  UUU UU
UU4S jn[        T [        5      (       Ga)  T R                  nUR                  T;   a3  TUR                     S   n[        UR                  UR                  5      nOgUR                  T;  aH  [        UR                  U" U" UR                  UR                   S9SS9U" 5       S	9nUTUR                  '   OTUR                     n[#        [%        T5      TT R                  U" U" T R                  T R                   S9SS9UT R&                  5      n	[        U[        5      (       a  TR)                  U	5        U	$ [        T [*        5      (       aA  [-        [%        T5      TT R                  U" U" T R.                  T R.                  S9SS95      n	U	$ [        T [0        5      (       d   e[-        [%        T5      TT R                  U" U" T R                  T R                   S9SS95      n	U	$ )
Nr   r   )	try_solver   c                  6  > TR                  TR                  R                  SS9n TR                  U 5      nT	" TR	                  UTR
                  T   5      U 5      nUb  [        US   5      $ T" TR                  ST STR
                  T    SU SU  S3	5      e)	NT)integerr   zExpected shape[z] = z# of input Tensor to be of the form z, where z is an integer)r   r   r$   r~   Eqrq  rF   CONSTRAINT_VIOLATION)
symbolexprsolutionr;  r<  r   r6  r|   r  r  s
      r.   
root_valueB_process_dynamic_shapes.<locals>.to_constraint.<locals>.root_value(  s     \\#(("3"3T\BF66&>D $Q!@&IH#8A;''!66%aSV\\!_,= >##'&H r-   r   Fr   )rL   r   r   )r|   r   r   torch.utils._sympy.solver  r   r   r  r   r   r$   r   r   r   r   r3   r4   r   rp  r~   r  r   r   r   r   )r   r  r6  r   r   r  dim_rootroot_constraintr   r   r|   r  r;  r<  %derived_constraints_with_phantom_rootphantom_rootssymbolss   ```       @@r.   to_constraint._process_dynamic_shapes.<locals>.to_constraint!  s   P6?	 	 c;'' xxH  G+")(*;*;"<Q"?(#((#'' ""-7#!**%;&X\\N"'& # 48h//0$X%6%67+6
&"@# 
J $-- 6<<ZH, + Z(($6
&"#))D#	J(  c3''''$6
&"@#	J r-   c                   > S n[        U[        [        45      (       aF  [        U[        5      (       a	  U" XU5      nT" X U5      nTUR                     R	                  U5        g [        U[
        5      (       a  UR                  [        R                  :X  a   [        R                  R                  X5        O{UR                  [        R                  :X  a   [        R                  R                  X5        O=UR                  [        R                  :X  a  [        R                  R                  X5        TR	                  [!        [#        U 5      U5      5        g Uc   [        R                  R                  X5        g g )Nc                     [        U5      $ r7   )r   )r  r6  r   s      r.   _create_static_dimN_process_dynamic_shapes.<locals>._parse_tensor_dim.<locals>._create_static_dimw  s    e$$r-   )r  rF   r   r$   r  r0   r2   r!   r)   r  r  maybe_mark_dynamicr*   mark_staticr+   mark_dynamicr   rp  )r  idxr   r  r   constraintsr  r  s        r.   _parse_tensor_dim2_process_dynamic_shapes.<locals>._parse_tensor_dimv  s   	% cC:&&#s##(c:&sC8JCLL!((4X&&xx<,,,00=\000))&6\111**671"V*cBC[MM%%f2 r-   c                   > [        5       Ul        [        5       Ul        [        5       Ul        [        5       Ul        [        5       Ul        [        U[        5      (       a#  UR                  5        H  u  p4T" XU5        M     g [        U[        [        45      (       a  [        U5       H  u  p4T" XU5        M     g Uc*  [        UR                  5       5       H  nT" XS 5        M     g g r7   )r  _dynamo_weak_dynamic_indices_dynamo_dynamic_indices_dynamo_dynamic_range_dynamo_static_indices_dynamo_unbacked_indicesr  r1  r  r   r2  rK  ranger   )r+  r  rq  r6  r   r  s        r.   update_symbols/_process_dynamic_shapes.<locals>.update_symbols  s     /2e+),&'*u$(+%*-%'eT""++-!&S1 (t}--#E*!&S1 +]6::<(!&T2 ) r-   c                    > [        U[        R                  5      (       a
  T" XU5        g [        U[        5      (       a  X!l        g g r7   )r  r  ro  r   r   )r+  r&  r  r  s      r.   assoc_shape,_process_dynamic_shapes.<locals>.assoc_shape  s7    a&&4M2;'' 'J (r-   r  r  )rD   N)r@  r;  r<  rG  r  r   r2  r2   r  r   r\  r   rL   r  )r  r#  r  $derived_constraint_with_phantom_rootphantom_root_namedynamic_dimsr;  r<  r  r  r  r  r  r  r  s         @@@@@@@@@r.   _process_dynamic_shapesr  
  s     ;^!4!9	.5$-00^,]-A-A-CD ,7t+<G-/MFH)$&KS Sj3 3(3&	' NhW0U,@EEJJ' 9@@Q8RST8U05 1V  (<( ) r-   c                 V   0 n[        U S S9S    H  nUc  M  [        U[        5      (       a  M  [        U[        5      (       aI  X!UR                  '   [        U[
        5      (       a$  UR                  XR                  R                  '   M{  M}  [        U[        5      (       a  M   e   U$ )Nc                 "    [        U [        5      $ r7   )r  r   )r[   s    r.   r]   '_get_dim_name_mapping.<locals>.<lambda>  s    *Q,r-   rT  r   )r   r  rF   r   r$   r   r   r0   )r#  name_to_dimr   s      r.   _get_dim_name_mappingr    s     K, 	 ;c3S!!(+%#{++14HH--. , c8,,,,	 r-   rB  c           
        ^^^^ SSK nSSKmSSKJnJn  SSKJn   U R                  S5      S   R                  5       n0 mUR                  S5       GH  nUR                  5       nUR                  S	U5      =n	(       a  U	R                  S5      n
S
u  pUR                  SU5      =n(       a  [        UR                  S5      5      nUR                  SU5      =n(       a  [        UR                  S5      5      n[        XUS9TU
'   M  UR                  S5      u  pTR                  U5      n[!        UTR"                  5      (       a  [        U5      TU
'   GM  UTU
'   GM     [%        U5      m['        5       nTR)                  5        H  u  nn[!        U[        [        [*        TR,                  45      (       d   e[!        UTR,                  5      (       aJ  U" U5      (       d   eUTU'   UR/                  [1        [3        [5        UR6                  5      5      5      5        [!        U[*        5      (       d  M  UR/                  UR8                  R:                  5        M     TR)                  5        H  u  nnUT;   a  M  UU;   a  M   e   0 mUUUU4S jn[=        UX5      $ ! [         a  nU" UR                  S5      UeSnAff = f)a]  
When exporting with :func:`dynamic_shapes`, export may fail with a ConstraintViolation error if the specification
doesn't match the constraints inferred from tracing the model. The error message may provide suggested fixes -
changes that can be made to :func:`dynamic_shapes` to export successfully.

Example ConstraintViolation error message::

    Suggested fixes:

        dim = Dim('dim', min=3, max=6)  # this just refines the dim's range
        dim = 4  # this specializes to a constant
        dy = dx + 1  # dy was specified as an independent dim, but is actually tied to dx with this relation

This is a helper function that takes the ConstraintViolation error message and the original :func:`dynamic_shapes` spec,
and returns a new :func:`dynamic_shapes` spec that incorporates the suggested fixes.

Example usage::

    try:
        ep = export(mod, args, dynamic_shapes=dynamic_shapes)
    except torch._dynamo.exc.UserError as exc:
        new_shapes = refine_dynamic_shapes_from_suggested_fixes(
            exc.msg, dynamic_shapes
        )
        ep = export(mod, args, dynamic_shapes=new_shapes)

r   Nr:  )_is_supported_equivalencezSuggested fixes:r   z`Suggested fixes not found in error message given to refine_dynamic_shapes_from_suggested_fixes()
z(.*) = Dim\('(.*)'.*\)rC   z!.* = Dim\('.*', min\=([0-9]+).*\)z.* = Dim\('.*'.*max\=([0-9]+)\)rK   z = c                 ~  > Ub  [        U[        5      (       a  U$ UR                  T;   Ga	  TUR                     n[        UTR                  5      (       a  [	        U5      T	;   a  T	[	        U5         $ [        [        UR                  5      5      nUR                  T;   a  TUR                     nO!UR                  T
;   d   eT
UR                     nTR                  R                  R                  X45      u  pgUnUS:w  a  [        U5      U-  nUS:w  a  U[        U5      -   nUT	[	        U5      '   U$ U$ [        U[        5      (       ar  UR                  R                  T;   aX  UR                  T	;   a  T	UR                     $ UR                  TUR                  R                     5      nUT	UR                  '   U$ U$ )Nr   r   )r  rF   r$   Exprr}   nextiterfree_symbolsrL   polys	polytoolsdivr   r   r~   )r+  r   dummyfixr  r   modulus	remainder_dimderived_dim_cacher  shape_fixesr|   s            r.   apply_fixes?refine_dynamic_shapes_from_suggested_fixes.<locals>.apply_fixes+  s   ;*S#..J\\[(cll+C#uzz**s800,SX66!$s'7'7"89F{{k1*6;;7%{{k999*6;;7).)>)>)B)B3)O&GC!|!'lS0 A~!C	N225%c#h/J
[))chh.?.?;.N||00(66vvk#((*;*;<=26!#,,/
r-   )rer|   r@  r;  r<  r   r  splitstrip	ExceptionrA  matchgrouprF   r   r{   r  Numberr  r  r  r   r  r  r}   r  r   r  r   r$   r\  )rB  r#  r  r;  r<  r  shape_fixes_msgexcr  r  rL   rS   rT   	match_min	match_maxr  rootsrN  cr  r
  r  r  r|   s                       @@@@r.   r   r     sk   @ :O))$67:@@B K$$T*iikHH6<<5<;;q>D#JDHH%I3OOyO9??1-.HH%GMMyM9??1-. #D =K5)JD==&D$--$'ID! %)D!% +( (7K eE!!#1!c3UZZ@AAAAa$$,Q////KNIIc$tANN3456a%%IIaffoo& $ !!#1K1:-- $ 13" "H {NKKi  ''n
 	s   "J   
K*J>>K)Gr   ra  loggingr   collectionsr   enumr   r   typingr   r   r   r	   r
   r  torch.utils._pytreer   r   r   r   r   r   r   r   r   r   exported_programr   r|   r   torch._guardsr   r   r   r   __all__	getLoggerr$   r  r!   	dataclassr0   r   r  r   r   r}   rF   r   r   r   r   r   r   r   r   r   r2  r1  r  r  r\  rg  ri  r   r  r  r  r  r   r#   r-   r.   <module>r%     s      
 #  @ @    . $V !
4 
 E E E6Q@ Q@h  *H# HX '+	@	@sm	@19#	@
38_	@    D
# D
 D
N   " 
* 
 
4 
* 
 
  ; 24FFG
   8$8$3*d8n458$ 8$ U38_$%	8$
 u/018$ U8U3E-F#PQR8$ #x-(8$ ]8$~  $	y
38
y
y y }	y
 	yx5d38n 5\ \~\ \~XS>X$sCx.%*d3iEFXDoS>o$sCx.%*d3iEFo 
*od$sCx.%*d3iEF,}L	}L$sCx.%*d3i?@}L 4S>5:tCy01}Lr-   