
    Vhqj                     .   d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
 d dlZd dlmc mZ d dlmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
l m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z, d dl-m.Z. d dl/m0Z1 d dl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< d dlm=Z=m>Z>m?Z?m@Z@mAZA e	rd dlBmCZC  ej                  eE      ZFde>defdZGd ZHde#de>dedeIeJeIeJe%f   f   deIeKeJeJf   eLe   f   f
dZM	 	 d7dZNdeIeOef   de
eIeOef   eKe   eLe   f   deLe   fdZPd ej                  ddfd!ZR	 d8d"e#d#ej                  j                  de
eIeOef   eKe   eLe   df   d$e7d%ej                  f
d&ZVd"e#d#ej                  j                  deIeOef   de
eIeOef   eKe   eLe   df   d'eJf
d(ZWd)ej                  j                  defd*ZZd+eeKeKe   eIeef   f      de[e\ej                  j                        fd,Z]d-e[e\ej                  j                        ddfd.Z^d-e[e\ej                  j                        ddfd/Z_e j                  d+eeKeKe   eIeef   f      fd0       Zae j                  d+eKe   d1eIeef   d"ej                  j                  jF                  fd2       Zde j                  d3ej                  j                  d+eKe   d1eIeef   d"ej                  j                  jF                  fd4       Ze G d5 d6ej                  j                        Zhy)9    N)defaultdict)AnyCallableOptionalTYPE_CHECKINGUnion)
AttrSourceGetItemSourceLocalSourceTensorPropertyTensorPropertySource)TrackedFake)InputDim)ConstantAttrMap)_fakify_params_buffers)Source)FakeScriptObject)FakeTensorMode)
Constraint)_check_dynamic_shapes_combine_args_DimHint_process_dynamic_shapes_RelaxedConstraint_tree_map_with_path)CustomObjArgument)_config)
_find_user_code_frame2_suggest_fixes_for_data_dependent_error_non_strictConstraintViolationError
DimDynamicEqualityConstraintGuardOnDataDependentSymNodeRelaxedUnspecConstraintShapeEnvStatelessSymbolicContextValueRanges)
GetAttrKeyKeyPath
MappingKeySequenceKeytree_map_with_path)Symbolkpreturnc                 .   t        d      }| D ]  }t        |t              rt        ||j                        }*t        |t
              rt        ||j                        }Qt        |t              rt        ||j                        }xt        d|        |S )z?
    Given a key path, return the source for the key path.
    argszUnknown KeyEntry )r   
isinstancer+   r
   idxr*   keyr(   r	   name
ValueError)r.   sourceks      N/home/dcms/DCMS/lib/python3.12/site-packages/torch/_export/non_strict_utils.pykey_path_to_sourcer:   ?   s     !(F 6a%"61551F:&"61551F:&/F04556 M    c                 N    | d u xs  t        | t        t        t        t        f      S N)r2   intfloatboolstr)ts    r9   _is_constant_argumentrC   Q   s!    9>
1sE4&=>>r;   moderB   t_constraintssourcesc                    t        |      }t        |      s4t        |t        j                  t        j
                  j                  f      r|S t        |t        j                        st        dt        |       d      t        |j                        }g }d g|z  }t        |      D ]  }	|	t        |di       v r |j                  t        j                          2|	t        |di       v r/|j                  t        j                          t#        d      ||	<   p|j                  t        j$                          t'        ||      }
t)        |      }| j*                  J ||v r||   j-                         D ]  \  }	}t/        |t0        j2                  |	      }|||	f   j                  |       t        |t4              rI|j6                  |
j8                  |	<   |j:                  | j*                  j<                  |j;                         <    | j?                  |||
	      }| j*                  j@                  j                  tC        |||
             |S )
NzUnsupported input type a;  . Export only supports pytree containers of basic types (Tensor, int, float, ...) as input. To register a custom dataclass, use torch.export.register_dataclass. To register a custom container type, use torch.utils._pytree.register_pytree_node. To register a constant input, use torch.utils._pytree.register_constant_dynamo_weak_dynamic_indices_dynamo_dynamic_indicesF)	warn_only)dynamic_sizesconstraint_sizes)basepropr3   )r7   symbolic_context)"r:   rC   r2   torchScriptObjectnnModuleTensorr6   typelenshaperangegetattrappendr!   DYNAMICr$   STATICr&   id	shape_envitemsr   r   SIZEr   constraint_rangerL   r5   source_name_to_debug_namefrom_tensortracked_fakesr   )rD   r.   rB   rE   rF   r7   n_dimsrK   rL   irO   t_id
constraintsrcfakes                  r9   fakifyrk   U   s     #FQ:a%2D2Dehhoo1V#Wa&%d1gY /V V
 	
 \FMv6] 
492>>  !3!34'!6;;   !3!34"9E"JQ  !2!23
4 0#) a5D>>%%%}*40668 	SMAz&F9L9LRSTCT1I%%c**&893=3N3N--a0CM??DNN44SXXZ@	S Af?OPDNN  ''D&BR(STKr;   c                 ,   t        | ||      }t        ||       t        ||      }t        t              |D ]  }||j
                     |j                  <     t        j                  j                  j                         }	|	|	j                  nq|sV| j                  j                  }
|
j                  |
j                  |
j                   d}t#        t%        g |d|      dd      nt#        t%        g d|      d      j&                  j&                  j(                  t+        d      5  |s t-        j.                  | j                        }nd}t        t0              t3        fd	||f      \  }}i }g }g }i }t5               }|D ]?  }t        j6                  j8                  j;                  |fd
j&                  |||||       A t=        ||t1        |j?                               |d      }|||||fcddd       S # 1 sw Y   yxY w)a  
    Given an nn module, example inputs, and constraints, return a new fake mode,
    fake inputs created in that mode whose dynamic shape dimensions are constrained
    by the given ranges, and sources for pairs of dynamic shape dimensions that are
    constrained to be equal.
    N)co_nameco_filenameco_firstlinenoT)rd   	co_fields+prefer_deferred_runtime_asserts_over_guards'allow_complex_guards_as_runtime_asserts)r^   allow_non_fake_inputsexport)rd   rq   rr   )r^   rs   zDetected fake_mode does not have a shape_env with tracked fakes. If you constructed the module under a FakeTensorMode, please initialize it like: FakeTensorMode(shape_env=ShapeEnv(tracked_fakes=[]))c                 "    t        | |      S r=   )rk   )r.   val	fake_moderF   rE   s     r9   <lambda>z"make_fake_inputs.<locals>.<lambda>   s    F9b#}gN r;   c                     | |f   S r=    )rg   dimrF   s     r9   rx   z"make_fake_inputs.<locals>.<lambda>   s    '4+"6 r;   F)source_pairsderived_equalitiesphantom_symbolsrelaxed_sourcesrJ   ) r   r   r   r   dictrg   r{   rP   _guardsTracingContexttry_getrw   forward__code__rm   rn   ro   r   r%   r^   rd   r6   inspect	signaturelistr,   setrt   dynamic_shapes_process_equalitiesr"   values)	nn_moduler1   kwargsr   _is_torch_jit_tracerr   combined_argsconstraintsrh   contextcoderp   original_signature	fake_argsfake_kwargsnamesr|   r}   r~   r   equalities_inputsrw   rF   rE   s                        @@@r9   make_fake_inputsr      s<   . ")T6:M-8)-HK6A$6GM! D
9Cjoo&z~~6D mm**224G %%	   ))||++"11
	
 # #<@8_	 #'	
	 # <@8_
 #'
	 "i&9&9&G&G&O^
 	
 
 +
"!(!2!293D3D!E!%7B47H!3N6N"
	;
 -/46SU-/'*u% 
	JLL'';;6##"	
	 /%1 !7!7!9:+
 
I+
 +
 +
s   2CH

Hr   r   c                 0    g fd}t        || |       S )Nc                 (    j                  |       y r=   )rZ   )pathrB   rW   flat_shapess      r9   _tree_map_helperz1_flatten_dynamic_shapes.<locals>._tree_map_helper  s    5!r;   )r   )r   r   r   r   s      @r9   _flatten_dynamic_shapesr      s$     K" (-Hr;   tensorc                 D    dD ]  }t        | |      st        | |        y )N)rH   rI   _dynamo_dynamic_range_dynamo_static_indices_dynamo_unbacked_indices)hasattrdelattr)r   attrs     r9   _clean_dynamic_markersr   
  s)     " 64 FD!"r;   rw   gmr   r   c                    | j                   }|J |j                  J |j                  D cg c]  }|j                   }}|j                  D cg c]  }|j                   }	}|j                  D cg c]  }|j                   }
}d}	 |j                  ||	|
|d       d|_        |j                  }||sJ ||j                          |j                         }|s|j                  ||||      }nd}|r|j                  d   |z   f|_        n|rt        |      }|r|yc c}w c c}w c c}w # t        $ r}|}Y d}~d}~ww xY w)a  
    Given a fake mode, sources pairs corresponding to equal dynamic shape dimensions,
    and a graph module, produce guards on the fake mode's shape env (raising constraint
    violations if any), solve (to suggest simplifications or fixes).
    Dynamo already performs this, so this is for non-strict mode.

    Additional inputs:
        equalities_inputs: the equality constraints to use for guards
        original_signature: the signature of the forward method
    NF)input_contextsr   ignore_staticTz"dummy constraint violation messager   )r^   rd   rj   r7   rO   produce_guardsr    frozendim_constraintssolveforced_specializationsprettify_resultsr1   )rw   r   r   r   r   r   r^   tfplaceholdersrF   r   constraint_violation_errorer   r   msgs                   r9   $produce_guards_and_solve_constraintsr     s   $ ##I   ""...&/&=&=>BGG>L>#,#:#:;Rryy;G;4=4K4KLbb))LNL!%	'  )/ 	! 	
 I//O *))((,CCE..&"	
 3!+E+J+J1+MPS+S*U"'	%=c%B"!(( "O ?;L $ '%&"'s)   D"D'3D,D1 1	E:EEnum_lifted_inputsc                    | j                   }|J |j                  j                  dg       }|D ci c]  }|||   
 }}|s|S t        j                  |      d   D ](  }	t        |	t        j                        st        |	       * t        |t              s7t        |t        t        f      sJ  t        |      |j                               }t        ||      }
|j                  j                   D cg c]  }|j"                  dk(   c}j%                  d      }t'        |
      ||z
  k(  sJ t)        t              }t+               }t-        |j                  j                         D ]  \  }}||k  s|j"                  dk7  rt/        |j                  d         st        |j                  d   t0              rR|
||z
     }t-        |j                  d   j2                        D ]<  \  }}t        |t        j4                        s"|j6                  j8                  j:                  rC|r||   nd}|t        |t<              r;|j>                  |j6                  j@                     ||j6                  j8                  <   n8tC        |jD                  |jF                        ||j6                  j8                  <   ||j6                  j8                     jI                  tK        |jL                  |             |jO                  |j6                  j8                  jP                         ?  |D ]  }||vs|j>                  |   ||<    |S c c}w c c}w )	a2  
    Given a fake mode's shape env and user-specified dynamic shapes,
    return the resulting range constraints and equality constraints.

    Additional args:
        num_lifted_inputs: the number of non-user-input placeholder nodes in the graph
        (used only to enumerate the user-input nodes)
    Ninline_constraintsr   placeholderTrv   )lowerupper)
input_namer{   ))r^   metagetpytreetree_flattenr2   rP   rT   r   r   tupler   rU   r   r   graphnodesopcountrV   r   r   	enumeraterC   r   rW   SymIntnodeexpr	is_numberr   var_to_range_exprr'   minmaxrZ   r   r5   updatefree_symbols)rw   r   r   r   r   r^   r   symbolrange_constraintsargflat_dynamic_shapesr   num_placeholders
input_dimsr   input_index
shape_specrf   dr{   s                       r9   make_constraintsr   W  s     ##I   %92>9K/5"6**     ""=1!4 (c5<<("3'(
 nd+.5$-888,^,]-A-A-CD1-P >@XX^^LT=0LRRSWX"#'7:K'KKKKT"J5L&rxx~~6 >T**dgg.F 5!12jIIe/7
 (7H)HI
dii.445 	>DAq!U\\*166;;3H3H (2jmt;*S(";5>5K5K6%affkk2 6A!ggSWW6%affkk2 166;;'..x499RS/TU##AFFKK$<$<=#	>>6  G**
 )2(>(>v(Ff%G u$ Ms   M2M	mc                     t               }t        | j                               j                  | j	                                dt
        j                  j                  dt        t           ffd | g |       |S )zSearch the module hierarchy, gathering up all tensor and ScriptObject constants.

    Returns a dictionary mapping hash(value) to the name of the constant. We
    have to abuse `hash` here unfortunately, see: [ScriptObject hash].
    r   prefix_atomsc                 N   | j                   j                         D ]a  \  }}t        |t        j                  t        j
                  t        f      s6|v r;dj                  ||gz         }|j                  ||       c | j                         D ]  \  }} |||gz   |        y N.)
__dict__r_   r2   rP   rT   rQ   r   joinaddnamed_children)r   r   	constantsr8   vfqnbuffers_parametersinners         r9   r   z%_gather_constant_attrs.<locals>.inner  s    JJ$$& 	&DAqLL&&$ **hh|qc12a%	& $$& 	4DAq!\QC'3	4r;   )
r   r   buffersr   
parametersrP   rR   rS   r   rA   )r   r   r   r   s     @@r9   _gather_constant_attrsr     se      !IQYY[)alln-4 4S	 4& 
!Rr;   r1   c                     | 
t               S t               }t        j                  |       D ]A  }t        |t        j
                  j                        s(|j                  t        |             C |S r=   )	r   r   tree_leavesr2   rP   rR   rS   r   rU   )r1   module_typesr   s      r9   #_get_graph_inputs_of_type_nn_moduler     s]     |u5L!!$' (c588??+T#Y'( r;   r   c                 d    | D ]+  }t         j                  j                  j                  |       - y r=   )rP   _exportutils$register_module_as_pytree_input_noder   rB   s     r9   ,_enter_enable_graph_inputs_of_type_nn_moduler     s-      D@@CDr;   c                 d    | D ]+  }t         j                  j                  j                  |       - y r=   )rP   r   r   &deregister_module_as_pytree_input_noder   s     r9   +_exit_enable_graph_inputs_of_type_nn_moduler     s-      FBB1EFr;   c              #      K   | d  y t        |       }t        |       	 d  t        |       y # t        |       w xY wwr=   )r   r   r   )r1   r   s     r9   &_enable_graph_inputs_of_type_nn_moduler     s?      |6t<L0>B3LA3LAs   A1 A>Ar   c           
   #     K   t        | |f      g}t        j                  | |f      D ]z  }t        |t        j
                  j                        s(t        ||      }|j                  t        j
                  j                  j                  j                  ||ddd             | t        j                         5 }|D ]  }|j                  |        d  d d d        y # 1 sw Y   y xY ww)NT)tie_weightsstrictstack_weights)r   r   r   r2   rP   rR   rS   r   rZ   r   	stateless_reparametrize_module
contextlib	ExitStackenter_context)r1   r   rw   ctxsr   fake_params_buffersstackctxs           r9   _fakify_module_inputsr    s      3D&>BCD!!4.1 c588??+"8C"HKK((>>' $"& ?  
			 5 	%C$	%  s%   AC+A)C+9C	C+C($C+modc              #     K   t        |       }t        d |j                         D              rJ d       t        j                  d ||f      rJ d       i }t               }i fd}dt        j                  j                  dt        dt        t        j                  j                  t        f   fd	}	 |j                         D ]  \  }	}
t        j                  j                  j                  |	      rP ||	      }|
D ]B  } || |      \  }}|	t        ||      u sJ t!        |||       |j#                  ||       |	||<   D |
D ]  }|j#                  |	|         t        j$                  t        j&                  |||f      \  }}| |||f |j                         D ]  \  }} || |      \  }}t!        |||         y # |j                         D ]  \  }} || |      \  }}t!        |||         w xY ww)
Nc              3   <   K   | ]  }t        |t                y wr=   r2   r   ).0objs     r9   	<genexpr>z)_fakify_script_objects.<locals>.<genexpr>%  s      .1
3()s   z+Mod shouldn't contain any FakeScriptObject.c                 "    t        | t              S r=   r  )r  s    r9   rx   z(_fakify_script_objects.<locals>.<lambda>)  s    Js$45 r;   z7args and kwargs shouldn't contain any FakeScriptObject.c                 f    t         j                  j                  j                  |       }| |<   |S r=   )rP   _libraryfake_class_registrymaybe_to_fake_obj)r  fake_objrw   fake_to_reals     r9   _maybe_fakify_objz1_fakify_script_objects.<locals>._maybe_fakify_obj0  s/    >>55GG	SVW!$Xr;   r  attr_fqnr/   c                 \    |j                  d      ^ }}| }|D ]  }t        ||      } ||fS r   )splitrY   )r  r  prefix_attr	last_attrcur_modr   s         r9   _leaf_mod_and_attrz2_fakify_script_objects.<locals>._leaf_mod_and_attr5  sB     #+.."5i 	-Dgt,G	-	!!r;   )r   anyr   r   tree_anyr   rP   rR   rS   rA   r   r_   r  r  _is_script_objectrY   setattrr   tree_map_onlyrQ   )r  r1   r   rw   constant_attrspatched_attrfake_constant_attrsr  r"  r  fqnsfake_script_objr   r!  r   r   r   orig_objr  s      `              @r9   _fakify_script_objectsr.    s*    ( '=S&AN 5C5J5J5L  545  5f~ A@A  L)+L
"XX__"(+"	uxx#	$"-'--/ 	6IC~~11CCCH"3C"8 ,C$6sC$@MGT''4"8888GT?;'++OSA(+L%,   6C'++C56	6 "(!5!5 1D&>"
	; I{,?NN)//1 	-MC.sC8MGTGT8,	-\//1 	-MC.sC8MGTGT8,	-s   B2G<7CG 2G<3G99G<c                       e Zd ZdZd ZddZy)_NonStrictTorchFunctionHandlera  
    1. Handles data-dependent errors raised by torch function calls in non-strict.

    Any data-dependent error is due to some condition on unbacked symints
    that cannot be resolved. A mechanical way of fixing the error is to use
    a torch._check() call to assert either that condition or its negation.
    The handler suggests these options as code and points to the location
    of the torch function call that raised the error as part of the error
    message shown to the user, who can then simply select and copy-paste
    a suggested fix at that location.

    NOTE: Not all data-dependent errors are raised by torch function calls.
    In particular, conditions on unbacked symints can appear outside such
    calls, and as such are not handled here.

    2. Overrides torch functions that are known to cause problems in non-strict.

    Certain Python features, such as indexing/slicing, cannot be intercepted
    in non-strict. Likewise, certain legacy ops, such as distributed collectives,
    may need to be mapped to other ops. When there is special handling in Dynamo
    for such things, tracing can fail in non-strict (while succeeding in strict).
    Fortunately, redirecting to other torch functions can often fix such issues.

    3. Handles line-of-code logging for each torch function call in non-strict.

    Usage: TORCHEXPORT_EXTENDED_DEBUG_CURRENT_LOC=1 TORCH_LOGS="+export" ...
    c                 &   t         j                  j                         rddlm}m} ||v r||   }t        j                  |      }t         |j                  |i |j                        }d}|t         j                  j                  t         j                  j                  t         j                  j                  fv rd|v r||d      |d<   |||fS |t         j                  u rHt        d t!        j"                  |d         d   D              rt         j$                  j                  ||fS |j&                  dk(  rVt)        |d   t         j*                        r9t)        |d   t         j,                        rt         j.                  |d   d|d   gi fS |||fS )Nr   )REDUCE_OP_TO_STRtraceable_collective_remapsrz   r   c              3   P   K   | ]  }t        |t        j                           y wr=   )r2   rP   r   )r  as     r9   r  z;_NonStrictTorchFunctionHandler._override.<locals>.<genexpr>  s       01
1ell+s   $&__getitem__   )rP   distributedis_available)torch.distributed._functional_collectivesr2  r3  r   r   r   bind	arguments
all_reducereduce_scatter_tensor_reduce_scatter_baser   r#  r   r   _refs__name__r2   rT   r   select)selffuncr1   r   r2  r3  mapped_funcr   s           r9   	_overridez(_NonStrictTorchFunctionHandler._overrides  st   ))+
 22 :$?#--d3	ninnd=f=GGH%%00%%;;%%:: 
 v~'7t'Et"D&005<<
  5;5H5Ha5QRS5T  {{))477==M)ja%,,.O$q'5<<0||d1gq$q'%:B>>T6!!r;   Nc                    |xs i }t         j                  j                         r ||i |S t        j	                  t
        j                        rqt        j                  rat               }|Ut        j                  d|j                  |j                  j                  |j                  |j                  j                         | j!                  |||      \  }}}	  ||i |S # t"        $ r}t%        |        d }~ww xY w)Nz%s called at %s:%s in %s)rP   compileris_dynamo_compilinglogisEnabledForloggingDEBUGconfigextended_debug_current_locr   debug__qualname__f_codern   f_linenorm   rF  r#   r   )rC  rD  typesr1   r   framer   s          r9   __torch_function__z1_NonStrictTorchFunctionHandler.__torch_function__  s    2>>--/(((GMM*v/P/P)+E 		.%%LL,,NNLL(( "^^D$?dF	(((* 	>qA	s   C! !	C;*C66C;)rz   N)rA  
__module__rQ  __doc__rF  rV  rz   r;   r9   r0  r0  V  s    8$"Lr;   r0  )FF)F)ir  r   rL  collectionsr   typingr   r   r   r   r   rP   torch.utils._pytreer   _pytreer   torch._dynamo.sourcer	   r
   r   r   r   torch._dynamo.variables.builderr   @torch._export.passes.add_runtime_assertions_for_constraints_passr   (torch._export.passes.lift_constants_passr   torch._export.utilsr   torch._guardsr   "torch._library.fake_class_registryr   torch._subclasses.fake_tensorr   torch.exportr   torch.export.dynamic_shapesr   r   r   r   r   r   torch.export.graph_signaturer   torch.fx.experimentalr   rN  %torch.fx.experimental.symbolic_shapesr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   sympyr-   	getLoggerrA  rJ  r:   rC   r   r>   r   r   rk   r   rA   r   rT   r   fxGraphModule	Signaturer   r   rR   rS   r   r   rU   r   r   r   contextmanagerr   _subclassesfake_tensorr  r.  	overridesTorchFunctionModer0  rz   r;   r9   <module>rt     s      # @ @  $ $  8 U D 6   ? 8 #  ; 3     g!7 v $?1
11 1 T#z/223	1
 %S/4</01r ,1p
fS>$sCx.%*d3i?@ 
#Y	"5<< 	"D 	"$ >)>)>) $sCx.%*d3iEF>) *	>)
  ))>)BMMM S>M $sCx.%*d3iEF	M
 M`ehhoo / B	
5sT#s(^34
5	ehhoo		Dd588??+,D	DFd588??+,F	F B
5sT#s(^34
5B B 
*cN   ,,;; : B-	B-
*B- cNB-   ,,;;	B- B-JXU__%F%F Xr;   