
    PhM                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZ d dlmZ d dlmZmZmZmZmZmZmZ d dlmZ d dlZd dlZd dlZd dlZd dlmc m Z! d dl"m#c m Z$ d dl%m&Z&m'Z' d dl(m)Z) d dl*m+Z+m,Z, d d	l-m.Z. d d
l/m0Z0 d dl1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z;m<Z< d dl=m>Z>m?Z?m@Z@ d dlAmBZBmCZCmDZDmEZE d dlFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZP d dlmQZR d dlSmTZT d dlUmVZVmWZW d dlXmYZYmZZZm[Z[m\Z\ d dl]m^Z^m_Z_ d dl`maZambZb ddlcmdZdmeZemfZf ddlgmhZh ddlimjZj ddlkmlZl ddlmmnZn ddlompZp dd lqmrZr 	 	 d[d!ed"eed#f   d$eeesef      d%eeeesef   ee   f      d&eee@      f
d'Zt	 d\dd(d)d*d!ed"eed#f   d$eeesef      d%eeeesef   ee   f      d+eud,eesd#f   d&eBfd-Zvd\d.ej                  d/exd0ees   fd1Zyej                   G d2 d3             Z{ e{       Z| e&       a}ed4        Z~ eTd56      	 	 d[d!ed"ee   d$eeesef      d7eee@      d&ej                  j                   f
d8       Zd9 Zd: Zd; Z	 	 d[d)d5d<d!ed"eed#f   d$eeesef      d7eee@      d,eesd#f   d=eud&ej>                  j
                  fd>Z	 	 d[d(d)d?d!ed"eed#f   d$eeesef      d7eee@      d+eud,eesd#f   d&eBfd@ZdAej>                  j
                  d&ees   fdBZdCej>                  j
                  dDe3dEees   d&eesesf   fdFZdG dHdIZdJej                  j                   d&eesej                  f   fdKZeE	 	 d[d(d)d?d!ed"eed#f   d$eeesef      d7eee@      d+eud,eesd#f   d&eBfdL       ZdMees   d"ee   d$eesef   fdNZdddOdPeBd!eesej                  ej                  f   dQeeesef      dReeesexf      d&df
dSZdddTd!eesej                  ej                  f   dQeeesef      dUeeesexf      d&eBfdVZ	 d\dddd5d5dWd!ed"ee   d$eeesef      d7eee@      d%eeesef      dXeeesef      dYeud=eud&esfdZZy)]    N)OrderedDict)contextmanager)AnyCallableDictListOptionalTupleUnion)patch)core_aten_decompositionsget_decompositions)enable_python_dispatcher)	UserErrorUserErrorType)ConstantSource)CollectTracepointsPass)aot_export_moduleGraphSignature)functionalize)detect_fake_mode)
OpOverload)
FakeTensorFakeTensorMode)_create_constraint_Dim
Constraint)ExportedProgramModuleCallEntryModuleCallSignature_disable_prexisiting_fake_mode)
_sig_to_specsArgumentSpecConstantArgumentExportGraphSignature	InputKind	InputSpec
OutputKind
OutputSpecSymIntArgumentTensorArgument)	traceback)compatibility)make_fxmaybe_disable_fake_tensor_mode)ConstraintViolationErrorGuardOnDataDependentSymNodeShapeEnvStrictMinMaxConstraint)_PyTreeCodeGen_PyTreeInfo)ValueRangeErrorValueRanges   )_create_stateful_graph_module_process_constraintsCallSpec)-_AddRuntimeAssertionsForInlineConstraintsPass)lift_constant_tensor_pass)_RemoveRuntimeAssertionsPass)_replace_sym_size_ops_pass)!ReplaceViewOpsWithViewCopyOpsPass)_wrap_submodulesfargs.kwargsdynamic_shapesreturnc                    |t        |      dk(  ry ||ni }ddlmm fdd ddlm}  |t              i fdfd}dd l}t        | t              r| j                         } t        | t        j                  j                        r|j                  | j                        n|j                  |       } |j                   |i |j"                  }t        |      r|nt        |j%                               } ||      D ]  \  }	}
 ||	|
        g }j%                         D ]5  }|^}}|r|D ]  }|j'                  ||k(          %|j'                  |       7 |S )Nr   )MappingSequencec           
   3     K   t        | t        t        f      rt        |      s*t        t        j
                  dt        |        d| d      t        |       t        |      k7  r*t        t        j
                  d| dt        |        d      t        |      D ]  \  }} | |   |      E d {     y t        | t              rt        |      s*t        t        j
                  dt        |        d| d      t        |       t        |      k7  r*t        t        j
                  d| dt        |        d      |j                         D ]  \  }} | |   |      E d {     y t        j                  |       rt        |      t        |       k(  s6t        t        j
                  dt        |        dt        |        d	| d      t        j                  |       D ];  } t        | |j                        t        ||j                              E d {    = y t        | t         j"                        r| |f y |*t        t        j
                  dt        |        d
| d      y 7 7 7 [w)NzExpected dynamic_shapes of a z to be a Sequence, got z insteadz	Expected z	 to have z itemsz to be a Mapping, got z	 to be a z, got z to be None, got )
isinstancetuplelistr   r   INVALID_INPUTtypelen	enumeratedictitemsdataclassesis_dataclassfieldsgetattrnametorchTensor)	combined_argsrE   ishapekrB   rH   rI   tree_zips	         aC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_export/__init__.pyr_   z)_process_dynamic_shapes.<locals>.tree_zipc   s    meT]3nh7!//3D4G3H I)*(4 
 =!S%88!///y]9K8LFS  &n55#M!$4e<<< 6t,ng6!//3D4G3H I)*(4 
 =!S%88!///y]9K8LFS  +0025#M!$4e<<< 3%%m4'4+>>!//3D4G3H	RVWdReQf g)*(4 
 !''6#GM166$BGN\]\b\bDcddd 7u||4 .11)!//3D4G3H I)*(4  *7 = = es9   B.I91I12B3I9%I4&B5I9I7AI94I97I9c                     t        ||| j                        }| j                  dk7  r|| j                  k\  }| j                  t        j
                  dz
  k7  r|| j                  k  }|S )N
debug_name   r8   )dynamic_dim__name__minmaxsysmaxsize)dimtensorr\   
constraints       r`   to_constraintz._process_dynamic_shapes.<locals>.to_constraint   sU     s||D
77a<#sww.J77ckkAo%#sww.J    )defaultdictc           
         | j                   v r| j                      \  }}| j                  |k7  s| j                  |k7  rzt        j                  | j                   ||      }t        j                  | j                   | j                  | j                        }t        t        j                  d| d| d|  d      y | j                  | j                  f| j                   <   y )NzFound different definitions z and z! for the same symbolic dimension !)rf   rg   rh   r   readabler   r   rN   )rk   min_max_this_that_boundssymbolss        r`   check_same_boundsz2_process_dynamic_shapes.<locals>.check_same_bounds   s    <<7"-JD$ww$#''T/cllD$?cllCGGSWWE!//25'ug F77:e1>  #2 %(GGSWW#5F3<< ro   c                    t        |t              r~|j                         D ]j  \  }}t        |t              r/ |       |j                     j                   || |             E|Ht        t        j                  d| d| d| d       y t        |t        t        f      ryt        |      D ]j  \  }}t        |t              r/ |       |j                     j                   || |             E|Ht        t        j                  d| d| d| d       y |t        t        j                  d| d      y )NzUnexpected item #z (z) in dynamic_shape z of Tensor, try None insteadzUnexpected dynamic_shape )rK   rR   rS   r   rf   appendr   r   rN   rL   rM   rQ   )rl   r]   r\   rk   rz   ry   rn   s       r`   update_symbolsz/_process_dynamic_shapes.<locals>.update_symbols   sP   eT"++-3c4(%c*CLL)00sFA1NO')77/s"SE9LUG T/ /  ( t}-#E*3c4(%c*CLL)00sFA1NO')77/s"SE9LUG T/ /  +  !///w 7' '  !ro   )rP   collections.abcrH   rI   collectionsrp   rM   inspectrK   r   modulerY   nnModule	signatureforwardbind	argumentsvaluesr|   )rB   rC   rD   rE   rp   r}   r   r   r[   rl   r]   constraintsdynamic_dimsprimaryothersotherrH   rI   rx   rz   ry   rn   r_   s                   @@@@@@@r`   _process_dynamic_shapesr   V   sW    ^!4!9)VrF1.` ($G)+F6B !_%HHJ0:1ehhoo0N!!!)),T[TeTefgThI"INND3F3==M &0%HMdS`SgSgSiNjM!-@vu% A K('&""7e#34   w' ) ro   T )rE   strictpreserve_module_call_signaturer   r   c                @    t        | |||      }t        | |||||      S )a  
    API for exporting with dynamic shape specifications instead of constraints.
    It should be considered "release candidate" (RC), meant to replace `export`.

    Here, `dynamic_shapes` is expected to be a dict from
    argument names of `f` to dynamic shape specifications OR a tuple where each element
    corresponds to the original order of the arguments defined in the function signature
    ,as follows:
    - The dynamic shape of a tensor argument can be specified as:
      - Either a dict from dynamic dimension indices to Dim types. It is not
        required to include static dimension indices in this dict, but when
        they are, they should be mapped to None.
      - Or a tuple of Dim types or None. The Dim types correspond to dynamic
        dimensions, whereas static dimensions are denoted by None.
    - Arguments that are dicts or tuples of tensors are recursively specified
      by using mappings or sequences of contained specifications.

    See `export` for documentation of `f`, `args`, `kwargs` and return.
    )r   r   r   )r   _export)rB   rC   rD   rE   r   r   r   s          r`   export__RC__r      s3    8 *!T6>JK	'E ro   tindexrc   c                    t        | t        j                        s&t        t        j
                  dt        |              | j                         dk  rt        t        j
                  d      || j                         k\  r2t        t        j
                  d| j                         dz
   d| d      t        t        j                  |       t        |       |t        t        dt        j                        d	
      |      S )Nz0Expected tensor as input to dynamic_dim but got r8   z-Cannot mark 0-dimension tensors to be dynamiczCExpected the dimension passed to dynamic_dim to be in the range [0:z
] but got z., which is out of bounds for the given tensor.rd   )lowerupperF)vr	warn_onlyrb   )rK   rY   rZ   r   r   DYNAMIC_DIMrO   rk   r   weakrefrefidr3   r7   sympyoo)r   r   rc   s      r`   re   re     s    a&%%>tAwiH
 	

 	uuw{%%;
 	

 %%QRSRWRWRYZ[R[Q\ ]wLN
 	
 A
1%((3u	
  ro   c                        e Zd ZU dZdZeed<   y)ExportDynamoConfigz:
    Manage Export-specific configurations of Dynamo.
    T	allow_rnnN)rf   
__module____qualname____doc__r   bool__annotations__r   ro   r`   r   r   ,  s     Itro   r   c               #   :   K   t         i c} a 	 d  | a y # | a w xY wwN)DECOMP_TABLE)prevs    r`   _disable_decomp_tabler   :  s&      &rD,ts   
 F)is_backward_compatibler   c                 B   t         j                  j                  j                  j                  t         j                  j                  j                  j                  j
                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  j
                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  j
                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  j
                  i}|i }t         j                  j                  j                  t        j                  t                    5  t        j                  j                  | |dd|dd      |i |d   }ddt         fd}ddt         fd}t#        |||      \  }}}}	|	j$                  j&                  j)                         D 
ci c]'  \  }
}t+        j,                  d	t/        |
            r|
|) c}}
|j0                  d
<   t3        j4                  ||xs i f      \  }}t7        |d|      \  }}t9        |||      }t;        j<                  ||      |_        t;        j<                  ||      |_         |cddd       S c c}}
w # 1 sw Y   yxY w)a]  
    A helper function that is intended to trace a module before any pre-autograd
    decomposition is run. The produced module will be "non-functional" and
    composed of aten operators. Later this API will be deleted in favor of more general
    torch.export API.

    Args:
      f: A callable to be traced

      args: example positional inputs.

      kwargs: optional example keyword inputs.

      constraints: A optional list of constraints on the dynamic arguments specifying
            their possible range of their shapes

    Returns:
        An nn.Module containing the traced method.

    NTsymbolic)r   assume_static_by_defaulttracing_modedecomposition_tablepre_dispatch
aten_graphr   modec                     t        d      )Nz%Calling train() is not supported yet.NotImplementedErrorselfr   s     r`   _trainz*capture_pre_autograd_graph.<locals>._trainx  s    %&MNNro   c                     t        d      )Nz$Calling eval() is not supported yet.r   r   s     r`   _evalz)capture_pre_autograd_graph.<locals>._eval{  s    %&LMMro   	^[if]\d+$inline_constraints)range_constraintsequality_constraints)T)!rY   opsatendropoutdefault	decompose
batch_norm_batch_norm_impl_indexnative_batch_norm_dynamoconfigr   rT   asdictDEFAULT_EXPORT_DYNAMO_CONFIGexportr   _convert_input_to_fake	shape_envruntime_var_to_rangerS   rematchstrmetapytreetree_flattenr:   r9   types
MethodTypetraineval)rB   rC   rD   r   decomp_tablemr   r   _	fake_moder^   v	flat_argsr   r   
unlifted_ms                   r`   capture_pre_autograd_graphr   D  sn   : 			&&		(>(>(F(F(P(P		!!))599>>+D+D+L+L+V+V		--55uyy~~7\7\7d7d7n7n		((00%))..2R2R2Z2Z2d2d	L ~				#	#K$6$67S$T	UMM  #%)# , ! 
 
 
 	Ot 	O	Nd 	N 4AtVD1a "++@@FFH(
H1xxc!f- qDH(
#$ **D&,B+?@	12Fq!Y2W//2/!5


 !++FA6
**5!4
K 
V	U,(
- 
V	Us    A>L,LBLLLc           
         t        |      dk(  r^t        |      dk(  rPt        t        | j                                     dk(  r+t        t        | j                                     dk(  rg i i d fS g d }| j                  j
                  D ]_  }|j                  dk(  sd|j                  v s"|j                  d   }|4t        |t        j                        sOj                  |       a t              x}r|}|J d       dfd}t        j                  t        j                  ||      }t        j                  t        j                  |j                  |      }	t        j                  t        j                  t!        j"                  |j                  d      i t        | j                  d	            t        | j                  d	                  }
||	|
|fS )
Nr   placeholdervalz<Cannot find fake_mode attatched to the graph's placeholders.c                        }dz  |S )Nr8   r   )xr   count	fake_inpss     r`   convert_to_fakez/_convert_input_to_fake.<locals>.convert_to_fake  s    

ro   Tstatic_shapesFremove_duplicate)rP   rR   named_parametersnamed_buffersgraphnodesopr   rK   rY   rZ   r|   r   r   tree_map_onlyfrom_tensor	functoolspartial)gmrC   rD   r   nodefake_valdetected_fake_moder   	fake_argsfake_kwargsfake_params_buffersr   r   s              @@r`   r   r     s   
4yA~#f+*s48K8K8M3N/OST/TY\]abdbrbrbt]uYvz{Y{2r4$&II77m#(:yy'H#
8U\\(J  *	  .i888&	 `"`` E $$U\\?DII&&u||Y5J5JFSK ..u||/8/@/@AVAVfj/k0b$r7J7J\a7J7b2c 0b26r7G7GY^7G7_2`0bc k#6	AAro   c                     |j                   D ]-  }| j                  |j                  |j                        |_        / |j                  D ]-  }| j                  |j                  |j                        |_        / y r   )input_specsgettargetoutput_specs)param_buffer_tablesigspecs      r`   _replace_param_buffer_namesr
    sX    (,,T[[$++F    (,,T[[$++F !ro   c                    d}t        j                  dd|      }| j                         D ]  }t        |t        j
                  j                        s)|j                  j                  D ]  }|j                  dv rd}|j                  j                  di       x}rVt        t        |j                                     \  }}	t        |	t        j                   j"                        sJ ||k(  r|	|u rd}|sd }
|||fi|}|j%                         D 	ci c]  \  }\  }}	| |
|      |	f c}	}}|j                  d<     y c c}	}}w )	Nz	L['self']z[^a-zA-Z0-9]r   )r   outputTnn_module_stackFc                     	 g  G fdd      }t        | dd |       ii       dj                        S # t        $ r | cY S w xY w)Nc                   "    e Zd Z fdZ fdZy)@_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Pathc                 *    j                  |       | S r   )r|   )r   rX   partss     r`   __getattr__zL_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getattr__  s     %T 2'+ro   c                 <    j                  t        |             | S r   )r|   r   )r   idxr  s     r`   __getitem__zL_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getitem__  s     %SX 6'+ro   N)rf   r   r   r  r  )r  s   r`   Pathr    s    ,,ro   r  Lr   .)r   join	Exception)pathr  r  s     @r`   normalize_pathz2_normalize_nn_module_stack.<locals>.normalize_path  sQ    $ ", , TC&$&)9#:;"xx.$ $#$s   37 AA)r   submodulesrK   rY   fxGraphModuler   r   r   r   r  nextiterr   
issubclassr   r   rS   )gm_torch_levelroot_clsrootroot_keyr   r   add_rootr  r  tyr  keys               r`   _normalize_nn_module_stackr,    s8   DvvosD1H$$&"ehh223HHNNDww33H"&))--0A2"FFF_%;%;%= >?b!"ehhoo6664<B(N$H$$ $,dH-="Q"Q ,;+@+@+B0+BZdB ..33+B0		+,= # 'B0s   E)r   disable_constraint_solverr-  c          
         |xs g }|xs i }t        |t              s&t        t        j                  dt        |             t        | t              r| j                         } t        j                  j                  j                  t        j                  t                    5  	 i }t        | ||      5  t        j                  j!                  | |dd|      |i |\  }}ddd       ddd       j.                  d	<   |S # 1 sw Y   "xY w# t"        t$        f$ r(}	t        t        j&                  t)        |	            d}	~	wt*        $ r-}	t        t        j,                  dt)        |	       d      d}	~	ww xY w# 1 sw Y   xY w)
z
    Traces either an nn.Module's forward function or just a callable with PyTorch
    operations inside and produce a torch.fx.GraphModule in torch IR.
    zAExpecting `args` to be a tuple of example positional inputs, got Tr   )r   r   r   r-  Nz=Consider annotating your code using torch._constrain_as_*(). constrain_as_size_example)	case_namemodule_call_specs)rK   rL   r   r   rN   rO   r   r   rY   r   r   r   rT   r   r   rA   r   r0   r6   CONSTRAINT_VIOLATIONr   r1   ANTI_PATTERNr   )
rB   rC   rD   r   r   r-  r1  r%  r   es
             r`   _export_to_torch_irr5    sz    #K\rFdE"33[\`ae\f[ghj 	j
 !_%HHJ				#	#K$6$67S$T	U	GI!!%CEVW$)MM$8$8 +-1!+.G %9 % 	% 	%! X 
V. 0AN+,+ XW )/: 	HM>>AGG* 	**OPSTUPVxX5 	! 
V	UsN   #E<%D4-D!DD	DE9#E  E9(E44E99E<<Fr   r   c                `    |t        j                  dt        d       t        | |||||      S )NzUsing `constraints` to specify dynamic shapes for export is DEPRECATED and will not be supported in the future. Please use `dynamic_shapes` instead (see docs on `torch.export.export`).rd   )
stacklevelr6  )warningswarnDeprecationWarningr   )rB   rC   rD   r   r   r   s         r`   r   r     sE     W 	
 	'E ro   r%  c                    t        j                  |      }g }| j                  j                         5  t	        t        || j                  j                              D ]  \  }\  }}|j                  dk(  sJ |j                  |j                         t        |t        j                        sPt        | |j                        rJ | j                  |j                  |       | j                  j                  |j                        }|j!                  |       t#        j"                  |j$                        |_         	 d d d        t'        | j                  j                        D ]G  }|j                  dk(  st)        |j*                        dk(  sJ | j                  j-                  |       I | j/                          |S # 1 sw Y   xY w)Nr   r   )r   tree_leavesr   inserting_beforerQ   zipr   r   r|   rX   rK   rY   rZ   hasattrregister_bufferget_attrreplace_all_uses_withcopyr   rM   rP   users
erase_node	recompile)r%  aot_export_argsr   user_input_namesr\   argr   rB  s           r`   _unlift_user_inputs_to_buffersrK  7  sb    ""?3I				.	.	0'I~7K7K7Q7Q(RSNA{T77m+++##DII.#u||,">499===..tyy#>)//88C**84 $		$)) 4 T 
1 ^))//077m#tzz?a'''  ++D1 1 ! 
1	0s   A9G ,BG  G	r   graph_signaturerI  c                    t        |j                        dk(  sJ |j                  J t        |      }| j                  j
                  D cg c]  }|j                  dk(  s| }}t        |j                        }|t        |j                        z   }||| }t        |      dkD  r|d   nd }	i }
|D ]%  }|j                  |j                     }||vr!||
|<   ' i }i }| j                  j                  |	      5  t        |      D ]  }| j                  j                  |      }|j                  |_        |j                  ||<   ||
v sC|
|   }t        j                  |j                         |_        |j#                  |       |j                  ||j                  <    	 d d d        t%        t        |j'                                     }|
j)                         D ]  }| j                  j+                  |        | j-                          |j                  D cg c]	  }||vs| c}|_        |j                  j'                         D ci c]  \  }}||vs|| c}}|_	        |j.                  j'                         D ci c]  \  }}||v s|| }}}|j.                  j'                         D ci c]  \  }}||vs|| c}}|_        |j                  j1                  |j)                                |j2                  D cg c]  }||v r||   n| c}|_        |S c c}w # 1 sw Y   xY wc c}w c c}}w c c}}w c c}}w c c}w )Nr   r   )rP   user_inputsbackward_signaturesetr   r   r   
parametersbuffersinputs_to_buffersrX   inserting_afterreversedr   r  rD  r   rC  rR   rS   r   rF  rG  buffers_to_mutateextenduser_outputs)r   rL  rI  namesr   placeholdersstartendbuffer_nodeslast_placeholder_node	old_nodesbuffer_namereplacesnew_node_namesrX   new_nodeold_nodebr\   ouser_inputs_to_mutates                        r`   _lift_buffers_to_user_inputsri  P  s,   
 **+q000--555 !E%'XX^^P^Ttww-7OD^LP**+E
#o--.
.Cc*L03L0AA0EL,4*,I%77		Be#!%	+	 
 H%'N		!	!"7	8-.Dxx++D1H&mmHO#+==N4 y $T? $		(-- 8..x8*2--' / 
9 (>#7#7#9:;N$$&
H% ' LLN*9*A*AT*AQQe^q*ATO(::@@B)BAaun1B)O% )::@@BBAa5j1B   )::@@B)BAaun1B)O% &&~'<'<'>?5D5Q5Q$5QqH}!+5Q$O  ! Y Q 
9	8  U))$sV   L!!L!9AL&AL&	L3 L3	L8L8L>L>3M MM
&L0c                     | S r   r   r   s    r`   <lambda>rl    s    ro   	transformc                   t         j                  j                  j                  j	                  | |      5    |t
              | g ||j                         d      \  }}d d d        t        j                  ||f      }d}t        j                        t        |j                        z   }	j                  j                  D ]M  }
|
j                  dk(  s||	k\  r1|||	z
     }t        |t         j                         s||
j"                  d<   |dz  }O |j$                  d u}dt&        fd}t)        t+        |j,                        |j.                  |j0                  t+        |j2                        |j4                  |j"                  j7                  d	i       |r|j$                  j8                  ni |r|j$                  j:                  ni |r|j$                  j<                  nd |j                  j                  D 
cg c]  }
|
j                  dk(  s ||
       c}
t        j                  t?        tA        tC        |j                  j                                    jD                        D 
cg c]
  }
 ||
       c}

      \  }}tG        ||      }tI        ||      }tJ        jL                   G d d             } ||||      S # 1 sw Y   cxY wc c}
w c c}
w )NF)trace_jointr   r   r   r8   rF   c                    d| j                   v s
J |  d       | j                   d   }t        |t              rt        | j                        S t        |t
        j                        rt        | j                        S t        |      S )Nr   z has no 'val' metadata field)rX   )value)	r   rK   r   r+   rX   rY   SymIntr*   r$   )r   r   s     r`   make_argument_specz._export_non_strict.<locals>.make_argument_spec  sp    		!HdV+G#HH!iic:&!tyy11U\\*!tyy11##..ro   rh  )rO  inputs_to_parametersrT  rY  buffer_mutationsuser_input_mutationsgrad_paramsgrad_user_inputsloss_outputinputsoutputs)r  r  c                   r    e Zd ZU ej                  j
                  ed<   eed<   ee	ej                  f   ed<   y)5_export_non_strict.<locals>._ExportedProgramNonStrictr   r  tensor_constantsN)rf   r   r   rY   r   r!  r   r%   r   r   rZ   r   ro   r`   _ExportedProgramNonStrictr~    s,    HH   !!sELL011ro   r  )'rY   r   utils	stateless_reparametrize_moduler   r   r   r=  rP   rR  rS  r   r   r   rK   rZ   r   rP  r#   r"   rQ  rO  ru  rT  rY  rW  r  gradients_to_parametersgradients_to_user_inputsrz  r"  r#  rV  rC   r%   r=   rT   	dataclass)modr   r   r  rn  r   rL  r   r   total_param_buffersr   user_argis_jointrt  r  r  export_graph_signaturer  r  s                      r`   _export_non_strictr    s    
	!	!	7	7=P	Q:i(9:/i/+,,./
O 
R ""I{#;<IEo889C@W@W<XX77m#++$U-@%@A!(ELL9'/DIIe$QJE  11=H/L / !.334,AA);;556(::WW[[)@"ERZO66NN`bX`;;TTfhFNO66BBTX57XX^^`^TtwwR_G_"4(^`6<6H6HdS[\^\d\d\j\jSkNlImIrIr6st6sd#D)6st!K 2kXde05KL2 2 2
 %
 o 
R	QT ats   ,K7K
K(KKr  c                     i }| j                  d      D ]
  \  }}|||<    | j                  d      D ]
  \  }}|||<    |S )NFr   )r   r   )r  params_buffersrX   parambuffers        r`   _get_params_buffersr    s]    .0N++U+Ce$t D ))5)Af%t Bro   c                b  6 |xs g }|xs i }|s	t        | t        j                  j                        sJ t	        |      dk(  sJ t	        |      dk(  sJ d       t	        |      dk(  sJ d       d66fd}t        | |i | j                         |      }6J t        |j                  |j                  j                  |j                  t        |       i g t        dt        g g t        j                  |i f      d   6            g||f|j                   		      S t#        | ||||
      }t        |      }	t%        |||      \  }
}}}|j                  j&                  D ]  }|j(                  dk(  sd|j*                  vs"t-        ||j.                        }t        |t        j                  j                        r]|J d       |j1                  |d      |j*                  d<    i }|j                  j&                  D ]  }|j.                  }|j*                  }|j(                  dk(  r|t-        ||      }t        |t        j                  j                        rL|j3                  dd      D ]  \  }}|||dz   |z   <    |j5                  dd      D ]  \  }}|||dz   |z   <    |j(                  dk(  r5t-        ||      }t        |t        j6                  j8                        s|||<   |j(                  dk(  st        |j.                  t        j:                  j<                        r+|j>                  D ]U  }|j(                  dk(  st        j6                  j@                  jB                  D ]  }||v s||   ||j.                     |<    W  |jD                  x6}6J 6jF                  tH        tJ        fvrt        jL                  tJ        d6g      6|j                  jN                  jP                  jR                  }tU        tW        ||jX                  6            |j                  _'        |j[                          i }t        | t        j                  j                        r&i }i }| j3                  d      D ]/  \  }}|j]                  t_        |      g       ja                  |       1 | j5                  d      D ]/  \  }}|j]                  t_        |      g       ja                  |       1 |j3                  d      D ]8  \  }} ||vsJ t_        |       |v s|t_        |          jc                         ||<   : |j5                  d      D ]8  \  }}!||vsJ t_        |!      |v s|t_        |!         jc                         ||<   : t        | t        j                  j                        rte        |tG        |              d }"t        ||
tg        ||
|      ||"      }|j                  }#|j                  }$|j                   }%|#j                  j&                  D ]  }|j(                  dk(  s|j.                  |$jh                  v rG|$jh                  |j.                     }&|&|v r*||&   jk                         D ]  \  }'}(|(|j*                  |'<    |j.                  |$jl                  v s|$jl                  |j.                     })|)|v s||)   jk                         D ]  \  }'}(|(|j*                  |'<     |rd|jn                  jp                  jk                         D '(ci c]'  \  }'}(ts        jt                  dtw        |'            r|'|() c}(}'|#j*                  d<   ty        d t{        |$j|                        D        d      }*t        j                  ||f      \  }+},t        |#|*|+      \  }-}.t        | t        j                  j                        r?t        ||$       |	jk                         D /ci c]  \  }}/|j                  ||      |/ }	}}/|j*                  d   jk                         D 01ci c]  \  }0}1|0t        dg g d|1 }2}0}1t	        |      dkD  r" t        |2|$      |#      }3|3J |3j                  }#|J t        |#|#j                  |$|	|-|.t        dt        g g |,|            g|2jk                         D 04cg c]  \  }0}4t        |0|4       c}4}0z   ||f|%		      }5t	        |-      dkD  st	        |.      dkD  r|5j                  t        |-|.            }5|5S c c}(}'w c c}/}w c c}1}0w c c}4}0w )a  
    Traces either an nn.Module's forward function or just a callable with PyTorch
    operations inside and produce a ExportedProgram.

    Args:
        m: the `nn.Module` or callable to trace.

        args: example positional inputs.

        kwargs: optional example keyword inputs.

        constraints: A optional list of constraints on the dynamic arguments specifying
            their possible range of their shapes

        preserve_module_call_signature: A list of submodule paths for which the original
            calling conventions are preserved as metadata.

    Returns:
        An ExportedProgram containing the traced method.
    r   zdynamic shape NYIzkeyword arguments NYINc                       fd}|S )Nc                     G fddt         j                  j                        }  ||       |fi |\  }}d }t        j                  ||j
                        |_        t        j                  ||j                        |_        t        j                  ||j                        |_        t        j                  ||j                        |_        t        j                  ||j                        |_	        ||fS )Nc                   *     e Zd Z fdZfdZ xZS )R_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapperc                 0    t         |           || _        y r   )super__init___export_root)r   r  	__class__s     r`   r  z[_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.__init__  s    (*,/)ro   c                 j    t        j                   | j                  |i |      \  }t        |      S r   )r   r   r  rL   )r   rC   rD   	flat_outsout_specs       r`   r   zZ_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.forward
  s8    .4.A.ABS$BSBSUYBd]cBd.e+	8$Y//ro   )rf   r   r   r  r   __classcell__)r  r  s   @r`   Wrapperr    s    00 0ro   r  c                 D    | j                  d      r| t        d      d  S | S )Nz_export_root.)
startswithrP   rk  s    r`   
strip_rootzU_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.strip_root  s&    78||O7T1S123[Z[[ro   )
rY   r   r   r   tree_maprR  rS  rT  ru  rW  )	r  rC   rD   r  r   r  r  
aot_exportr  s	          r`   _aot_export_non_strictzA_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict  s    0ehhoo 0 %WS\4B6BC\ "(S^^!L$ooj#++F(.
CDYDY(Z%+1??:sG_G_+`((.
CDYDY(Z%3wro   r   )r  r  r  s   ` r`   _tuplify_outputsz!_export.<locals>._tuplify_outputs  s    , *)ro   rm   r8   )r  )r   rB  r   zbCannot find dynamo_fake_mode. This could be due to the exported graph module have no placeholders.Tr   call_moduleF)recurser   r  call_functionr   c                 H     dt         j                  j                  f fd}|S )Nr%  c                     t        | |      } | dfi |\  }}t        |||      }|j                  j                  | j                         d|j                  vsJ ||j                  d<   ||fS )Nr   rh  )rK  ri  r   update)r%  rC   rD   rI  r   rL  rh  r  s          r`   _aot_export_strictzA_export.<locals>._process_user_inputs.<locals>._aot_export_strict  sz    =ndS",^R"J6"JB$@_Vf$g! GGNN>../*"''999/DBGG+,&&ro   )rY   r   r!  )r  r  s   ` r`   _process_user_inputsz%_export.<locals>._process_user_inputs  s!    
	'uxx/C/C 
	' "!ro   r   r   r   c              3   b   K   | ]'  \  }}|j                   t        j                  k(  s$| ) y wr   )kindr&   
USER_INPUT).0r\   ss      r`   	<genexpr>z_export.<locals>.<genexpr>  s)     gDtq!R[RfRfHfDs   %//r1  )r{  r|  )r{  r|  in_specr  r   )FrK   rY   r   r   rP   r  
state_dictr   r   r   r  r  r   r    r   r   r  r5  r   r   r   r   rW   r  r   r   r   r   r!  _opsHigherOrderOperator_input_nodesproxy_COPY_META_FIELDS	_out_specrO   rM   rL   TreeSpec_codegenpytree_info	orig_argsr4   r5   _in_specrG  
setdefaultr   r|   popr,  _reorder_kwargs_by_namesru  rS   rT  r   r   r   r   r   r"  rQ   r  r:   r
  r  r   graph_module
_transformr<   )7rB   rC   rD   r   r   r   r  ep_non_strictr%  r  r   r   r  dynamo_fake_moder   attrparams_buffers_to_node_metar  r   	submodulerX   r   rJ  entryorig_out_specr  r  param_lookupbuffer_lookupr  r  dynamo_namedynamo_paramdynamo_bufferr  r   r  r  
param_namer^   r   ra  
num_liftedr   orig_in_specr   r   rl   fqnspecsmodule_call_signaturesresr  exported_programr  s7                                                         @r`   r   r     s	   < #K\rF!UXX__---12a777;1$9&99$6{a8!88	*0 +1dBRbc###"""R!4RV=P=PRVXZQ[=\]^=_ai!jkl6N*;;

 
	
 )	'EN )8N E[[ikoqwDxAI{/1A $$**77j U$))%;>4;;7DdEHHOO4'3 x3 $4#?#?TX#?#Y		%  + #%$$**yy77m#7I)UXX__5(99$Y^9_GD!GK/t0CD  `  )66tV[6\GD!GK/t0CD  ] 77j 7Ii)=)=>6:+F3
 77o%jejjFdFd.e((66Z'!&!A!A D=MQRW[7

CEJ "B )+ +8  .777H}}}T5M)??5$
;$$--99CCI$2##	
%N! )+!UXX__%-/.0--u-EKD%##BuIr299$? FOOUOCLD&$$RZ4;;DA D)7)H)HZ_)H)`%K&8888,</2>r,?O2P2T2T2V";/ *a
 +9*F*FX]*F*^&K&8888- M12?=@Q2R2V2V2X";/ +_
 !UXX__%">47;"" ' I{C&M 
		B*..$55
 77m#{{4III3HHU
!<< ;J G M M O1'(		! !P{{4FFF4FFt{{S"== ;K H N N P1'(		! !Q "  )22GGMMO)
O1xxc!f- qDO)
$% gy!7!C!CDgijJ %114.AI|.B
/++ !UXX__%#$68NOYgYmYmYopYov,00t<fDYop )--.ABHHJJJC 	 @B@%@@J  
 )*Q.T$%;=STUWX$$$&

	0BP\gtu	vw3I3O3O3QR3QxsCc	"3QR	S	v) !S)=%>%B+669:KMab
 e)
$ q( 	Ss   ,d*d*d%;d+	arg_namesc           	          t        |       t        |      t        |      z   k(  s,J dt        |        dt        |       dt        |       d       | t        |      d  D ci c]  }|||   
 c}S c c}w )Nz,Total number of arg names is expected to be z	 but got z positional args, z kwargs.)rP   )r  rC   rD   kw_names       r`   r  r    s    y>SYV44 
6s9~6F Gt9+/F}H	F4 5>c$ij4IJ4IGVG_$4IJJJs    A0)extra_filesopset_versionepr  r  c                $   ddl m}m} ddlm}  || |      }t        |t        t        j                  f      rt        |      }t        j                  |d      5 }t        j                  |      D ]0  }	|	j                  }
t        ||
      }|j                  d|
 d|       2 |j                  dt        |             |r>|j!                         D ]+  \  }}|j#                  d      }|j                  d	| |       - d d d        y # 1 sw Y   y xY w)
Nr8   )	serializeSerializedArtifactSCHEMA_VERSIONwserialized_.jsonversionutf-8extra_files/)serde.serializer  r  serde.schemar  rK   r   pathlibr  zipfileZipFilerT   rV   rX   rW   writestrrS   encode)r  rB   r  r  r  r  r  artifactzipffield
field_nameserialized_fieldextra_file_namecontentencoded_contents                  r`   saver    s     ?,#,R#?H!c7<<()F	C	 D ''1EJ&x<MMK
|59;KL 2
 	i^!45 ,7,=,=,?(").."9_,=>P -@ 
!	 	 s   B$DD)r  expected_opset_versionr  c                h   t        | t        t        j                  f      rt        |       } t	        j
                  | d      5 }t        |j                  d            }ddlm	} ||k7  rt        d| d| d      ddlm}m}  |di t        j                  |      D ci c],  }|j                   |j                  d	|j                    d
      . c}}	 ||	      }
|;|j#                         D ](  }|j                  d|       j%                  d      ||<   * |
cd d d        S c c}w # 1 sw Y   y xY w)Nrr  r8   r  zSerialized version z+ does not match our current schema version r  )deserializer  r  r  r  r  r   )rK   r   r  r  r  r  intreadr  r  RuntimeErrorr  r  r  rT   rV   rX   keysdecode)rB   r  r  r  r  r  r  r  r  r  r  filenames               r`   loadr    s;    !c7<<()F	C	 Ddii	*+0n$%gY /""0!14 
 	E (: (
 )//0BCCE 

DIIEJJ<u&EFFC(
 " "',,.(,		L
2K(L(S(ST[(\H% / ; 
!	  
!	 s    AD(1D#AD(#D((D1)r   rE   optionsremove_runtime_assertionsr-  r	  r
  c                H   |t        j                  d       ddlm} |t	        | |||      }t        | ||||      }	t        j                  |i |xs i }
t        j                         5  t        j                  j                  |	|
|      }ddd       |S # 1 sw Y   S xY w)az  
    Note: this function is not stable yet

    Traces either an nn.Module's forward function or just a callable with PyTorch
    operations inside, generates executable cpp code from the program, and returns
    the path to the generated shared library

    Args:
        f: the `nn.Module` or callable to trace.

        args: example positional inputs.

        kwargs: optional example keyword inputs.

        constraints: A optional list of constraints on the dynamic arguments specifying
            their possible range of their shapes

        dynamic_shapes: An experimental new feature designed to subsume ``constraints``.
            A dict mapping argument names of ``f`` to their dynamic shape
            specifications, as follows. Dynamic shape specifications can be a
            dict from dynamic dimensions to ``Dim`` types, or a tuple/list of
            ``Optional[Dim]`` corresponding to each input dimension.

        options: A dictionary of options to control inductor

        disable_constraint_solver: Whether the dim constraint solver must be disabled.

    Returns:
        Path to the generated shared library
    NzGThe constraints field is deprecated. Please use dynamic_shapes instead.r   )select_decomp_table)r-  )r9  r:  torch._inductor.decompositionr  r   r5  r   arg_tree_leavesrY   no_grad	_inductoraot_compile)rB   rC   rD   r   rE   r	  r
  r-  r  r   flat_example_inputsso_paths               r`   r  r  A  s    R 1	

 B-av~N 
	";
B !00$I6<RI	//--b2EwO 
 N 
 Ns   +"BB!)NNr   )rD  rT   r   iojsonr  r   ri   r   r9  r   r  r   r   
contextlibr   typingr   r   r   r   r	   r
   r   unittest.mockr   r   rY   torch._dynamotorch.fxtorch.fx._pytreer   _pytree	fx_pytreetorch.utils._pytreer  r   torch._decompr   r   torch._dispatch.pythonr   torch._dynamo.excr   r   torch._dynamo.sourcer   -torch._export.passes.collect_tracepoints_passr   torch._functorch.aot_autogradr   r   !torch._functorch.eager_transformsr   torch._guardsr   
torch._opsr   torch._subclasses.fake_tensorr   r   torch.exportr   r   r   torch.export.exported_programr   r   r    r!   torch.export.graph_signaturer"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   fx_tracebacktorch.fx._compatibilityr-   "torch.fx.experimental.proxy_tensorr.   r/   %torch.fx.experimental.symbolic_shapesr0   r1   r2   r3   torch.fx.graphr4   r5   torch.utils._sympy.value_rangesr6   r7   r  r9   r:   r;   2passes.add_runtime_assertions_for_constraints_passr<    passes.lift_constant_tensor_passr=    passes.remove_runtime_assertionsr>    passes.replace_sym_size_ops_passr?   /passes.replace_view_ops_with_view_copy_ops_passr@   wrappersrA   r   r   r   r   rZ   r  re   r  r   r   r   r   r   r   r   r   r
  r,  r!  r5  r   rK  ri  r  r  r   r  r  BytesIOr  r  r  r   ro   r`   <module>r9     s      	   	 
     # % D D D      $ $ $ $ F ; 6 / P K ; * ! D = =    / 1 V  7 H 
 H J H ' (,BF	MM
S/M T#s(^$M U4S>5:#=>?	M
 d:Mf (,$
 CG68$$
S/$ T#s(^$$
 U4S>5:#=>?$ $ %*#s(O$ $N5<<  # >     23  ()   e, (,.2	JJ
*J T#s(^$J $z*+	J
 XX__J -JZ BFG(Z (,.2	2 79&+22
S/2 T#s(^$2 $z*+	2 %*#s(O2  $2 XX2p (,.2	 68
S/ T#s(^$ $z*+	  %*#s(O 8HH(( 
#Y25!5!#5! 3i5! 
#s(^	5!| FRUXX__ c5<<6G1H    (,.2	S 68SS
S/S T#s(^$S $z*+	S S %*#s(OS S  SlKS	 Ks KTRUWZRZ^ K -1.2QQS',,

*+Q $sCx.)	Q
 DcN+Q 
QB -17;	&S',,

*+& $sCx.)& %T#s(^4	&
 &X (,B
 /3/3(,&+&+BB
*B T#s(^$B
 $z*+B T#s(^,B d38n%B  $B  $B 	Bro   