L i 4ddlZddlZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z ddlmZmZmZmZmZddlZddlmcmZddlmcmZddlmZdd lmZdd l m!Z!dd l"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*dd l+m,Z,dd l-m.Z.m/Z/m0Z0ddlm1Z1m2Z2ddl3m4Z4ejje6Z7gdZ8Gdde Z9e dGddZ:da;edZ< d[deejzej|ej~jfdej~jdeAde9deBf dZCGdd ZDGd!d"eDej~jZEGd#d$eDej~jZFGd%d&ejZHGd'd(ej~jZI d\d)ed*eeHd+eIfd,ZJd-ej,jd.e$d+dfd/ZLd0ZMd1ZNd2eAd3eAd+eAfd4ZOd5ej~jd6ej~jfd7ZPd8ej~jd+eAfd9ZQd8ej,jd+ej,jfd:ZTd8eej,jeEeIfd+ej,jfd;ZUd8eej,jeEeIfd+ej,jfd<ZVd=ej~jdeAfd>ZW d\d=ej~jdeAd?ej~jd@eeeAgej~jffdAZXdBeAdCeYd+eAfdDZZdEeAdBeAd+eBfdFZ[GdGdHZ\e GdIdJZ]dKej,jdLeIfdMZ^ d]dNej~jdOe_eAeYfdPeAfdQZ`GdRdSZad8ej,jdLeIdTe_eAebeAffdUZcdVZd d\d)ej~jdWe_eAeeeAfdXeeeAdYee_eYebeAffdZZfy)^N) defaultdict)contextmanager)deepcopy) dataclass)Enum)AnyCallablecastOptionalUnion)FakeScriptObject)ExportedProgram)reorder_kwargs)ConstantArgumentExportGraphSignature InputKindModuleCallSignatureSymBoolArgumentSymFloatArgumentSymIntArgumentTensorArgument)is_fx_symbolic_tracing) _get_attr_get_attr_via_attr_list_print_readable) GetAttrKey SequenceKey)_remove_effect_tokens)FlatArgsAdapterInterpreterModuleInterpreterModuleDispatcherUnflattenedModule unflattenceZdZdZdZdZdZy) _AttrKind parameterbufferconstantmoduleN)__name__ __module__ __qualname__ PARAMETERBUFFERCONSTANTMODULE\/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/export/unflatten.pyr&r&1sI FH Fr3r&T)frozencNeZdZUdZej ed<eed<eed<eed<y) _TensorIDzJCustom tensor identifier containing storage, stride, and size information.untyped_storagestridesizestorage_offsetN) r+r,r-__doc__torchUntypedStorage__annotations__tupleintr2r3r4r7r78s!T))) M Kr3r7c#8Kt}da d|ay#|awxYwwNF)RUN_WITH_INTERPRETER)old_flags r4_disable_interpreterrFEs'$H ( 'xs from_obj to_moduletarget attr_kind persistentc |jd^}}|h}|D]| t}|D]i}t| s)t| tj j |j fd|jjDk|}~|D]?}|tjk(r9t|tj jsJ|j||P|tjk(r1t|tj sJ|j#||||tj$k(rQt|t&rJdt|tj tj(fsJt||||tj*k(s t|tj j sJt|||By)N.c3@K|]\}}t|r|ywN) _is_call_name).0kt_callitems r4 z_assign_attr..fs'Av D)s)rKz2FakeScriptObject should only exist during tracing.)splitsethasattrsetattrr=nnModuleupdate_modulesitemsr&r. isinstance Parameterregister_parameterr/Tensorregister_bufferr0r ScriptObjectr1) rGrHrIrJrKprefixfield to_modulestsrTs @r4 _assign_attrriRs\\#&NVUJ #&5# I9d+ 4):; II!*!3!3!9!9!;     0  ++ +h(:(:; ;;  ( ( 9 )** *h 5 55  % %eX* % M ),, ,!(,<= D =LL&&  Iuh / )** *h8 88 Iuh /+0r3c0eZdZUeeed<deefdZy)_SubmoduleBase_tyreturnc|jS)z Subclass of this class - InterpreterModule, InterpreterModuleDispatcher, represents corresponding model in eager model. To get this type information for those modules in eager model we need to use this method. )rlselfs r4 type_namez_SubmoduleBase.type_names xxr3N)r+r,r-r strr?rqr2r3r4rkrks #8C=r3rkceZdZUdZeej jed< d dej jdee ffd Z dZ dZ d dZxZS) r!zA module that uses torch.fx.Interpreter to execute instead of the usual codegen that GraphModule uses. This provides better stack trace information and makes it easier to debug execution. graph_modulegraphtycvt|||_||_||j_t |_yrO)super__init__rurl owning_modulerD_run_with_interpreter)rprurv __class__s r4ryzInterpreterModule.__init__s2  #'  %9"r3cd|jJdtsStjj s |j s)t |jj|g|iSrt|}|jt|d}|jfd|Dt|tk(sJt|t|jk(sJt|}tjj||jj |ddiS)Nz&Didn't finalize this InterpreterModulec30K|] }|vr|ywrOr2)rQ kwarg_namekwargss r4rUz,InterpreterModule.forward..s& "!V+:& sruenable_io_processingF)rtrr=compileris_dynamo_compilingr{typeforwardlist arg_nameslenextendr@fx Interpreterrurun)rpargsrarg_list kwarg_namess ` r4rzInterpreterModule.forwards  ,V.VV,%' NN . . 08R8R 34))*224I$I&I I  :"nnS]_=  &1 ;'3v;6668}DNN(;;;;X88''DJJ'?CC,1 r3cTtjj||j|jd<|jj g|_|jjD]7}|jdk(s|j j|j9y)Nrt placeholder) r=r GraphModuleru__dict__lintrnodesopappendrIrpnodes r4finalizezInterpreterModule.finalizesy).(<(Returns a list of paths that are used to access the flat args.r2ros r4get_flat_arg_pathsz"FlatArgsAdapter.get_flat_arg_paths*s r3)NN)r+r,r-r<abcabstractmethodpytreeTreeSpecrrr dictrrrrr2r3r4r r s  .2!  __  OO  I  4S>*  c]   c    DIr3r cfeZdZ d dedeeffd ZdZdZdZ dZ dZ d Z d d Z xZS) r# export_moduleflat_args_adapterc,t-||jj t dd}|j Dcgc]}|j }}|ddk(sJt|j}t|j|_tjj|_||j_ t|j |_||_ |jj|_||jd<d|_t"|_t'||jt)|t+|_t/||\}}|j,j1|j3t5|j6|||j8|_g|_|j<} t?} i} |jj@D]} | | } || | vrBtjBjE| jG| jH| || <tK| || || tLjN| jQ| t?|jjR}t?}i}|jjTD]} | |vrd}|jV| }nd }| | }|||vr|jG|f|||<tK|||d|| tLjX| |jQ| | j[D]\} }| | vs| |vrd}|||vs$t]|tjBjDsd }|rB|||vr |d f|||<tK|||d|| tLjXd ||| vr || ||<tK| |||| tLjNi}|jVj[D]l\}}|||vr5t]|tj^r|jG}||||<|||}tK|||tLj`ntctd,t?},fd }t?}|jjfD]}|jhtjjNk(s)|jhtjjXk(r|jlrto|jpd sJt]|jrttsJ|||j<|jr|jpjv|jr|jQ|jr|jQ|jr|jhtjjXk(r |jlr<|jhtjjxk(s|jhtjjzk(s_to|jpd sJt]|jrttsJ|||jV|jr|jpjv|jr|jQ|jr|jVj[D]H\}}||vs ||}|,vsJ,|d\} }!||| ||jQjrJ|j<j[D],\}}||vs ||}"|",vr,|"d\} }!||"| |.i}#,j3D]#}$|$D%cgc]}%|%d  }&}%|$D] \}'}!|&|#|'< %t}||#gt|j3}(|Dcgc] }||(vs| }}|j|(||Dcgc] }d|vs| }}|jjD)cgc]})|)jdk(s|)c})|_Cd |_Dt|D*cic]\}*}||* }+}*}|jdD]\} }!| |+vs t|+|+| <t||+|jj|jycc}wcc}%wcc}wcc}wcc})wcc}}*w)Nz%Unflattening on JointExportModule NYIct|tjrGt|j |j |j |jSt|S)z8Returns _TensorID dataclass for tensors, otherwise id().)r8r9r:r;) r_r=rbr7r8r9r:r;id)rs r4_idz'UnflattenedModule.__init__.._id9sR#u||, $'$7$7$9::<##5#7  c7Nr3runflattened_moduleF) requires_grad)rJT)rJrKc6|}|j||fyrO)r)obj_id node_name target_name name_list consts_maps r4add_to_consts_mapz5UnflattenedModule.__init__..add_to_consts_maps "6*I   i5 6r3namer@rremove_duplicate)Krxrygraph_signaturebackward_signature ValueErrormodule_call_graphfqnrrur=rrrzrrtmetaadaptedrDr{#_inplace_buffer_and_input_mutations_fix_nn_module_stacks_IValsivals_outline_submodulesr\values_copy_graph_attrs _graph_modulerange_constraintsequality_constraints state_dictrW parametersrZr`clonerrir&r.addnon_persistent_buffersbuffers constantsr/r^r_rbr0rr input_specskindrrKrXargrIrrrCONSTANT_TENSOR CUSTOM_OBJ _sink_params_deduplicate_modules_dispatch_modulesrrinput_placeholderscheck_input_constraints enumerate named_modulesr_reorder_submodulesrr).rprrrentryfqn_list export_graph seen_modules seen_attrsrassigned_params id_to_paramrparamrassigned_buffers id_to_bufferrKr(tensor is_buffer id_to_constrr) _constantconsts_targetsradded_params_bufferss const_nameconstconst_idph_name_ tensor_idinputs_to_state node_targetttargetsnredirected_call_indicesri fqn_orderrr|s. @r4ryzUnflattenedModule.__init__0sm   ( ( ; ; GDE E ,9+J+JK%EIIKK{b    3 34 ' (E(EFXX^^% #'  !)-*I*I!J!2!..33 *. &' %9"+L$:N:NOl+X #6|T#J j ,--/0 -55tZH!.!@!@*,!#-- $'E  ((33 &Dt$E5z,*/((*<*<KKM1D1D+=+ CJ' CJ'#--      % &"%T%9%9%P%P!Q%(UUW ((00 'D--" &006! #D)6{,.-3\\^Z,H S[) S[)!,#**%     &% ',',,.! LD&&$2B*BI6{l****3! K|31LV- V-a0'..# v;k1/5KF ,F ,'11 9! L  +44::< MC8}K/h 5'~~/H-5 CM*#CM2I #,,   JU J $'5 7 *-%%11 -Avv,,,)***q||quuf---!!((C000! 00:;EEJJHH ""188,$((2)*** 66Y66666Y111quuf---!!((C000! //9:EEJJHH ""188,5 -:"/!8!8!>!>!@ 3 J/u::---'1!4 !(GZ@$((2  3)3399; ;KC..K J.' 215 !)Wc: ;13%,,. -K%01qt1G1# -1%," - - T?B/"6|7J7J7L"M#+RCs:Q/QCRR 6G#+>Cs#~C>> "ZZ--# M1ID# (,$*3H*=>3S!V> >))5)A 1GD!9$"%i. $ 1 D),  QLZ2S? # ?s6c c c"c> cc&c$;c$ c)c|jD]d\}}t|dzt|ds!t|jt j jsPt|jfy)N:ru)rprintrXr_rur=rr)rprrs r4 _print_graphzUnflattenedModule._print_graph.sU**, !HC #) sG$CIIuxx~~)Ncii  !r3c|jdj}||jk(r|S|j t d|jj |j|||j |}t||jjk7r.t dt|d|jj|S)NrzeThere is no flat args adapter specified. Are you sure you are calling this with the right arguments? )rrrrrz)r)rr(rr)rtree_flatten_with_pathrrr%r.rtorch._export.utilsr0rrrrrrrr)rprrr(reordered_kwargsflat_args_with_pathr)xr,r0flat_arg_pathsr1rnew_flat_args_with_paths r4process_forward_inputsz(UnflattenedModule.process_forward_inputsOs**1-77 ! -fi6G6GH '-'D'D # $( $W$77aQqT7 7 ! #  i'' '<<''.ir301B1B0CD MN--i$GIDL  ' ' O||t#--**==? *S-@C N-RRR%.i$8 +!S(C0&4C%8%9  +' ++>' .'')@$BXBX c88 +s  F :+Fc|j|i|}|jdj}tr_t j j ||jj|ddi}t|trt|dk(r|dS|Stjjs |js't|j j"|g|}n:t j j ||jj|ddi}t%j&||j(S)NrrrFr)r9rr(rr=rrrurr_r@rrrr{rrtrrtree_unflattenout_spec)rprrr,r( return_valtree_outs r4rzUnflattenedModule.forwards/D//@@ **1-77 ! #--d$**-EII16J*e,ZA1E!!}$  >> - - /t7Q7Q6tD--.66tHiHHxx++D +CGG16H$$Xy/A/ABBr3ctjj||j|jd<|jj y)Nrt)r=rrrurrros r4rzUnflattenedModule.finalizes3(-(<(.elide_call_indicess^  *77m+++++C03C06fXQse,CD~-&)  *r3FrrurN)rrrrr(rgetrVrrArWrsplitrr^sortedrrange _call_namerr]pop set_submoduler"rrXrur)rpr rrrrbaser1 attrs_maprIorig_fqnrindexed_call_modulesrrr!rerEmod_rDs @r4rz#UnflattenedModule._dispatch_moduless8%T*++ =EyyU__ii&=&A&A#s&KL.1SjCIIcNsCj ct$++SXsO< = $ $ *Ff}!'sA!6$(#''-" !!&)  */=.B.B.D  *H*.45I.JKFAsCKLK< 1$s<01QA$Xq1u5C"99(+ # /f=FFJJ4P Q ""/ (0C\R  ***E*B 8HCsG$"3 2o.--8D"4111&sDJJ78  8-Ls H?c"t|d||||S)Nr#rrs r4rz UnflattenedModule.print_readablerr3rOr)r+r,r-rr r ryr&r.r9rrrrrrs@r4r#r#/s\8<|&|$O4||! 6;zC*88x  r3r#r*rrmct|}t||}tjj |j dd|_|S)aUnflatten an ExportedProgram, producing a module with the same module hierarchy as the original eager module. This can be useful if you are trying to use :mod:`torch.export` with another system that expects a module hierarchy instead of the flat graph that :mod:`torch.export` usually produces. .. note:: The args/kwargs of unflattened modules will not necessarily match the eager module, so doing a module swap (e.g. :code:`self.submod = new_mod`) will not necessarily work. If you need to swap a module out, you need to set the :code:`preserve_module_call_signature` parameter of :func:`torch.export.export`. Args: module (ExportedProgram): The ExportedProgram to unflatten. flat_args_adapter (Optional[FlatArgsAdapter]): Adapt flat args if input TreeSpec does not match with exported module's. Returns: An instance of :class:`UnflattenedModule`, which has the same module hierarchy as the original eager module pre-export. z*do not trace into preprocessing the inputsT)reason recursive)rr#r=_dynamodisabler9)r*rms r4r$r$sO,#6 *F&"34A %}}44   ; 5 A Hr3rurcttt|j}|jdk(rt |j dk(sJ|j d}|jDcic]}|jdk(s|j|!}}i}|jjDcic]\}}|| } }}|jjDcic] \}}|| | }}}|j|jt |} |d| D]} || j} || } |j| 5|jdtj j"j$j&| | f| j(jD]\}}|j(|< ddd| j+fdt-|| d}|f|_ycc}wcc}}wcc}}w#1swYHxYw)a^Transform buffer and input mutations from their functionalized form into copy_ nodes in the graph. Functionalization represents a buffer mutation by passing the buffer as an input and output. For example, consider the eager code: def forward(self, x): self.buffer += x return x * x This corresponds to a graph that looks like: def forward(self, buffer, x): mutated_buffer = aten.add(buffer, x) mul = aten.mul(x, x) return (mutated_buffer, mul) We want to inplace this into something that looks like the original eager code: def forward(self, buffer, x): mutated_buffer = aten.add(buffer, x) buffer.copy_(mutated_buffer) mul = aten.mul(x, x) return (mul,) Input mutations are handled similarly. outputrrrN call_functionc |uSrOr2)r6new_nodes r4z5_inplace_buffer_and_input_mutations..Ts 1H;Lr3)nextiterreversedrrrrrinputs_to_buffersr^buffers_to_mutater\user_inputs_to_mutateinserting_after create_noder=opsatencopy_defaultrreplace_all_uses_withr@)rur output_node return_argsrinput_name_to_nodemutation_name_to_input_namerR buffer_fqnbuffer_fqn_to_input_name num_mutationsmutation input_name input_nodev user_outputsr]s @r4rrs:tHU[[123K >>X %#k.>.>*?1*DD D""1%K%*KK 477m3K 4#%,;+L+L+R+R+T '!Z A    ->>DDF# Az #J //##  &&'L'LM34M/ N0? ' 3  " "8 , %((!5!5!=!= H?UH! ++- %1#$ a  %  % &&x1LM N"]^45L%(KI  # % %s%/G1G14 G6!G<A3HH c |jD]}d|jvr|jd}|jDcgc]\}}d|vr|jddn|!}}}|^}}d}|D]} t || r|dz }| }n|t |kst jd||t ||z tt|jd||jd<ycc}}w)Nnn_module_stackrrrzPnn_module_stack fqns %s at node %s do not form a stack! dropping last %d entries) rrrrV _is_prefixrlogwarningrrr^) rurryr_tfqnsprev_fqn next_fqnsnum_valid_indicescurr_fqns r4rr\s   DII - ))$56+113 R"%CIIcN1  4  $9! H(H-!Q&!#  s?3 3 KKbO$'88   ,0_**,-.@/@A,DII' (=   s$C2cVt|t|kxr|dt||k(S)z2Check whether `candidate` is a prefix of `target`.N)r) candidaterIs r4rzrzs+ y>CK ' QF3CS^,D ,QQr3 parent_fqn child_fqnc|dk(r|S|jd}|jd}|dt||k7rtd|d|ddj|t|dS)NrrMzChild module 'z(' is not a descendant of parent module 'za'.This is currently unsupported.Please try to make child module attach to parent module directly.)rVr RuntimeErrorr)rr parent_split child_splits r4_compute_accessorrsR##C(L//#&K&S&'<7YK'OPZ|\P P  88KL 1 34 55r3r6ycTdtjjdtfd}t |j tjjsJt |j tjjsJ||j ||j k(S)Nrurmc T g}i dtf fd }t|jD]\}}tj||j Dcgc] }t|}}|tj||j jDcgc] \}}|d|c}}z }|jdvr |jnd} |j|d|jd| dd j|d | t|<d j|Scc}wcc}}w) Nrmct|tjjrdt t |zSt |S)N%)r_r=rNoderrr)r nodes_idxs r4arg_dumpz>_check_graph_equivalence..graph_dump..arg_dumps7#uxx}}-S2c7!3444s8Or3=)r[get_attrrz: [z](z, )r) rrrrrtree_maprrr^rrIrrr) ruretrr!rr args_dumpkeyvaluerIrs @r4 graph_dumpz,_check_graph_equivalence..graph_dumps$&  S  !- $GAt-3__Xtyy-QRcSRIR "(//(DKK"H"N"N"PC%q  I%)GG/L$LT[[RTF JJ!Btwwiq499Y3G2HJ K"#Ibh  $yy~Ss DD$ )r=rrrrr_ru)r6rrs r4_check_graph_equivalencerst%((..S( agguxx~~ .. . agguxx~~ .. . agg *QWW"5 55r3gmcvd}t|d|r|dz }t|d|rd|}t||||S)Nr_spec_r)rXrY)rspecr!rs r4 _add_specrsN A "qcl # Q "qcl # A3&%((//2  3t $$r3 module_to_add create_modulec z|jd^}}t|D]\}}t||d}|K||dj|d|dz}ntj j }t|||t|tj j sy|}|j||y)NrMrF) rVrrrr=rZr[rYr_ add_module) rrIrrrerfr!rTrs r4_add_submodulers \\#&NVUV$ 4dD) >(&sxxwQ'@A* Cv &&%((//2 NN5-(r3rMrc$|dk(r|S|d|dz S)Nrrr2)rMrs r4rJrJs$640$qQ00r3 call_namec^tjtj|dz|duS)Nz(@\d+)?$)rematchescape)rrMs r4rPrPs% 88BIIdOk19 =T IIr3ceZdZ ddejj deejjdfdeee e e e fde e e fde eejjeff dZd Zd Zd Zd Zd ZdZdZdZy) _ModuleFrameN flat_graphr. module_stackrr*c !|_|_|_|_|_|__|_| _| _ d_ jd\_ !} tj| dz_ | '| _t| dr | j ni_ncj j#jt%t&j(j+!_j _jj,_i_i_d_Ct5jj} !fd}t7j| j|j,j9| _jjrAjjd}|jj:j_jjj=t?j jj jj2j| dzj| j#j}|j |j@jBd k(sJ|j@jDd}|j@jDd}|jFJ|jFJj,jId5tK|jBDcgc] }j,jMd |"}}i}|jFD] }j,jM|||<"tOjtQ||f|j@}tS|jTD]\}}j,jWd tXjZ||ft]|t^s |j`nd | }t]|t^re|j`jvs~tcjbj|j`jd|_2|j0j|j`< dddj j,jgj25g}|jTD]}t]|t^r|j=|jh/|j`jvr|j=dYt]|tjtltntpfsJ|j=j jsj|j`tuj j||j@}j j,jwtXjZ|df}j j,jwtXjZ|df}tK|jBDcgc]8}j j,jwtXjZ||f:}}|jFD cic]9} | j j,jwtXjZ|| f;}} dddj2JtQj2_<j2_=yyycc}w#1swYxYwcc}wcc} w#1swYaxYw)NFrrrvcjrjd|n|}|jvrj|Sttjj }|j|<|S)NrMr)rcreated_modulesr!r=rr)rrCrparentrprvs r4rz,_ModuleFrame.__init__..create_module>sj06 &**Qse,4/////55*588>>+;C-3$$T* r3r)r parent_moduleparent_call_modulercall_idxr*_positional_arg_r[ _constant_)rrIrr)>rr seen_nodesrrrrr module_idrverboserrJrr*rXrrFr!r=rrrunode_mapnode_to_placeholderrrrrr]r_SubmoduleEntryr) num_childrenchildren_specscontextrerIrrr@rinputsrfrrr_rrcopyrinserting_beforerrrrr remap_inputrr[rr)"rprrrrrrrrrrr* num_callsrrbase_module_framer( args_spec kwargs_specr1 arg_nodes kwarg_nodesrr,r flat_arg_node input_nodesr- inputs_node args_node kwargs_noder!rRrvs"` ` @r4ryz_ModuleFrame.__init__ su% $($. ("!2 "&"3"3B"7"i#DHHi!m<   DK)0)ArDJ..22!%((.."2r:DK DJ[[&& =? #% ;?  (T^^DH  6==(DKK O&,ll&>&>x&HD #  0$($5$5dnn$Ea$H!'8'?'?'H'H $   dnn - 4 4#{{"&++"4"4'+'>'>&];;  &))$..9  T[[%<$$11Q6 66!))88;I#++::1=K$$, ,,&&2 22++D1  %Y%;%;<JJ**-=cU+CD ! '//ED(, (>(>t(DK%E2KK9%{3%% !*)*:*: ;HC$(JJ$:$:*'//'-$.c3C#D HH#-cU!3%; %M"#'78 xx4??2-1YYtsxx7P7U7U-V *)001JK! B""33D4K4KL* =? &--E!%)9:#**5;;74??:#**40)! . . / 0   $** KK33DOOEJJ4OP&2KK&&%% !KK--;;$${A&6 #kk//==$${A&6 #9#9#9:KK%%33H4D4DyRSnU )00 t{{((66 ((;*: K* V**6 66+0+;D # # (-8D # # *k&=   DK* * sQ:]%]7C1])A#]E;],?=]"<], >]' ],]]" ],,]5c|jdk7s Jd|d|j|jusJ|jjd5|jj |j |j }dddtj|j_||j|<y#1swY=xYw)NrzCannot add placeholder z to root module) type_expr) rrurrrrrrrr)rpr6placeholder_nodes r4add_placeholderz_ModuleFrame.add_placeholdersxx2~K!8?KK~ww$//))) ZZ ( ( . P#zz55aff5O  P!% !&& 1&6  #  P Ps 2B>>Cctjtjj|j |j }tjtjj|j |j}|jj|j||}tj|j|_ ||j|<|SrO)r tree_map_onlyr=rrrrrrur[rIrrr)rpr6rrrs r4copy_sym_call_functionz#_ModuleFrame.copy_sym_call_functions##EHHMM43C3CQVVL%%ehhmmT5E5EqxxPzz''$?IIaff%  a r3c6|j|jusJ||jvr|j|S|jd|d||jvr|j|S|j dk(s%|j j|j|j||jm|jjj|j5|jjd|jj|ddd|j|S|j dk(rk|jt j"j$j&j(t j"j$j*j,t j"j$j.j(t j"j$j0j2t j"j$j4j,t j"j$j6j,fvs/t9|jdr9|jj:dk(r |j=||j|S|j j|j|j>jA||StCd|j d |#1swYxYw) Nz remap_input(rrrr[r, _operatorz(Could not run remap_input() on op type: z for node )"rurrr%rrrrFrrrrr insert_argrrIr=rgrhsym_sizerArTrjunbindsum dim_IntListviewdiffrXr,rrreadr)rpr6s r4rz_ModuleFrame.remap_inputsSww$//)))  ==# # \!A&' (( (++A. . DDM !T%;%;%?%?%I%Q   #&&2[[&&778O8OPV++66q$++:Q:QRS:TUV++A. . TT_ $ HH ''++ ##++ %%)) "".. ##++ ##++  ,/AHH4G4G;4V  ' ' *==# #  # # ' ' 1 =::??4+ +:144& 1#N 1VVs *6LLc  jjjdg}jj j }|j |jD]}t|tttttfrS|jjvr)|j!j|ji|j!d{t#d|fd t%j&t) fd|D|j*}t-j j&j.|j*}|}ng}j0j3D]\}|j4D]K}|jjvs|j!||j!j0|\^j.}t7|dk(r|d}t|t8t:j<j>fsJj@jC||yt|t:j<j>r|jDj dn(|D cgc]} | jDj dc} |jDd<t7|dk(r||j j0|d<ytG|D]u\} } |  t:j<jI|| jJ} | jDj d| jDd<| j j0| <wycc} w)Nz'Unsupported data type for output node: c|yj|j}|jvrj|S|jvrj|St d|dj )NzCould not find output node z . Graph: )rrrrrru)rZ seen_noderps r4get_actual_output_nodez=_ModuleFrame.finalize_outputs..get_actual_output_node sx> OOFKK8  -==33$":"::33I>>&5fXYtzzlSr3c3.K|] }|ywrOr2)rQrZrs r4rUz0_ModuleFrame.finalize_outputs..sP,V4Psrrval)&rrKrrrFrrrr_rrrrrrrrrrr*r@r<rrrkeysusersrrr=rrrurZrrProxyr)rp orig_outputsr(rZ tree_out_node parent_out graph_outputs orig_node user_nodeor! orig_output proxy_outrs` @r4finalize_outputsz_ModuleFrame.finalize_outputss   40 **..t~~>  T[[%<#++ &&'((  {{doo5$++DOOFKK,HI$++D1&A&J! ( 0 P<PP""M 3I ""D$;$;Y=O=O3JHUMM!]]//1  !*I ~~T__<$++I6%,,T]]9-EF  00J=!Q& -a 0 -$ )>??? -(   -7    " "5 )-:;!&&**U#;  |  !i&74>DKK a 1"+L"9 >;&!HHNN:6q9>> (3(8(8(<( ?06  9;G#'tyy1B'C'H'H'J"KE"R'OOJJOO%%OO((%% 5#))#.q1** (8$a %(9(99 99ww*$/33DKK@ NN4 MHqTZZ(D%s7MrO)r+r,r-r=rrr@rrrrr rArrr rr#ryrrrr rrr%rr2r3r4rr sLPb9HHNNb9UXX]]C'(b95hsmS!89:b9 %8 89b9uxx335FFGHb9H 7 )VX>t* %&#[r3rceZdZUeed<ej jed<ejjed<eed<e ed<ej jed<y)rrrrrrr*N) r+r,r-rrr?r=rZr[rrrAr2r3r4rrs;O88??" % HM HHOOr3r orig_graph root_modulec:i}tt}tt}i}t|t |j ||||ddgd|j Dcic]%}|jr|j|j'c}| j||fScc}w)N)rNrr)r*) rrrWrr@rrr(rr)r!r"rrrrrs r4rrs+-J5@5FL&1#&6J24O j  %66  IIu &  ik  ## s*Brr"rec6|dk(rWt|jddD]8}t||t||tj j :g}t|jjD]S\}}| ||z}t|||jdddzt|||j||||fU|jtjd|D]\}}}|j!||y)NrrrrrM)re)r)rrrrr=rZr[r]r^rrVdelattrrsortr itemgetterregister_module)rr"rerchildrenrchildrs r4rrs | ()!"- ?Cfc*2vsEHHOO,=> ?HFOO11347 e = tmE9SYYs^A5F5LM3u56 7 MMh))!,M-",4tU+,r3c(eZdZdZdZdZdZdZy)raY Collect the intermediate values of mutations in a graph. Example: in the following graph, suppose that buf_in and buf_out are the input and output values of a buffer. buf_in = placeholder() ... ival1 = f0(buf_in, ...) # inside self.n0(...) ... ival2 = f1(ival1, ...) # inside self.n1(...) ... buf_out = f2(ival2, ...) # inside self.n2(...) return buf_out, ... Here ival1 and ival2 are intermediate values created inside calls to n0 and n1 respectively, and used inside calls to n1 and n2 respectively. c,tt|_yrO)rrWnode_names_by_fqnros r4ryz_IVals.__init__s!,S!1r3cxt|tjjr|jj SyrC)r_r=_ops OpOverload_schema is_mutable)rprIs r4 _is_mutablez_IVals._is_mutables) fejj33 4>>,, ,r3c||jdk(sJ|j|j}td|j||sZt t |j dj\}}|j|j|j|j|jdS)zI Read state corresponding to a given intermediate value. r[zChecking mutabilityryr) rr3rIr%r_rarrr-rrrr)rpmfrbrrs r4rz _IVals.reads ww/)))   T[[ ) #T[[!4(499->#?#F#F#HIJFC  " "3 ' + +DII 6~~diil++r3c |D]}|D]}|jj}|j|j}|jDcgc]}|j |vs|}}|D]}|j |5|jdtjjjj|jd|f} tj|j| _dddycc}w#1swYxYw)zT Update states corresponding to intermediate values that were read. r[rN)r*rur-rrrrerfr=rgrhrirjrrr) rp partitionsshared_submodulesrru node_namesrrrr]s r4r\z _IVals.update,s", = * = **!33EII> $)KKHq166Z3GHH! =D..t4=#(#4#4+!IINN0088!YYq\40$ )- $))(< == = = =I==sC:C:8A3C? ?D N)r+r,r-r<ryr3rr\r2r3r4rrs(2 ,=r3rrc|jD]5\}}|r t||n|}|D]}t||}t|||7yrO)r^rrrY)rr"rrnamesr*rrs r4rrAsX ',,.' 56?; 2[ 'D"d#C FD# & ''r3ci}|D]I}t|D]7\}}t|j|j}t |j |}d}|d|D]}t |j|js$|j} |j|jk(r|jd^} } t| | jj| t|j|j} t |j | } | |j_| ||<|r| j||jd}:L|S)NFrMT)rrJrrrrrr*rrVrr]rKrrIrL)r8r r9r!rrrI deduplicatedseenrrerseen_child_fqn seen_targets r4rrMs? '$,!"34# ,HAu"599enn=I&u'7'7CF L)"1- ,+DKKF"00Fxx599, )/ S(9 /?HHLLTR)3DHHdmm)L&7!,,n' ;F007=K/ :),,VT[[A'+ = , # ,$,L #"r3rscopemodule_id_to_inputs_removedc ttt|vrt|t|iS|jj D]M\}t t d|||gz}|j D]\}}|j|Ot|dd}|t|jdk(rSt|tjjsJtt!d|j} t| dk(rdn| d} t!d|j} | D]Q} t#|| j$tvs*t't!fd| j(| _Si} | D]u} | j*|vrd}|| j*D]H}|j-d }|dt||Dcgc]}|j-d dc}k(sF|}n|q|| | <wt}| j D]\} }t| j.dkDr|t|d}t1||}t|tj2tj4fsJ|j7| 5|j9d d j;|}ddd| j=d |j?| |jA| j*t|tBr|jEt||iScc}w#1swY|xYw)aSink params, buffers, and constants from graph inputs into get_attr nodes. Exported modules are purely functional, so they pass their parameters and buffers in as inputs to the graph. To replicate eager's semantics, we need to get them from the module state via get_attr instead. module: GraphModule, potentially containing nested submodules. inputs_to_state: mapping graph input names to the corresponding key in the state_dict. scope: tracks where we are in the module hierarchy, so that we can emit the right `getattr(self, "foo.bar")` calls, etc. module_id_to_inputs_removed: records inputs removed by child modules, mapping the module object id to the list of placeholder node names in the child module that were removed. Nztorch.nn.Modulerurc |jdk(S)Nrrrs r4r^z_sink_params..s144=#8r3rc |jdk(S)NrrFrGs r4r^z_sink_params..s)>r3c8|jtvSrO)rr)rrC submodules r4r^z_sink_params..saff,G9 ,VVr3rMrrT)propagate_meta)#rrWrr]r^rr r\rrrr_r=rrrfilterrrIr@rrrVrrrbrdrerfrrk erase_noderr!r)r*rrBrCrsubmod_id_to_inputs_removedrRrvrurthe_last_inputcall_module_nodesrinputs_to_state_of_scope state_namesnsn_splitr6inputs_removed attr_path state_attrr]rJs ` @r4rrxs9,#*&1#&6# &z006 76 CDD"??0025i&2 "I .  TFN ' ' # 0557 5DAq ' * 1 1! 4 55 FGT *E }EKK(A-** eUXX^^ ,, , &8%++F GF [A-T6":N> L! fdkk2   R ]6Q%QVIIDI @B4 99O +  !$)), Bxx}H #e*%5)Ia!''#,q/)II%      )3 &/44 #uN4::< &j tzz?Q "3u:<0I0CJj5<<9K9K*LM MM&&~6 N ,,Z)9LM N  & &x & E 499% &&+, vJ ''O*J8 N Ns!L8 "L==M )TrO)r)grrloggingrr collectionsr contextlibrr dataclassesrenumrtypingrr r r r r=torch.fx._pytreer_pytreertorch.utils._pytreeutilsr"torch._library.fake_class_registryr torch.exportrtorch.export._tree_utilsrtorch.export.exported_programrrrrrrrrtorch.fx._symbolic_tracertorch.fx.graph_modulerrrrr_remove_effect_tokens_passr getLoggerr+r{__all__r&r7rDrFrbrdrZr[rrboolrirkr!r"ABCr r#r$rrrrzrrrrrrrrrrrArJrPrrrrrrrWrrrrr2r3r4rms  #%!77 $$$$?(3   <UU7=g!  $((" 00ELL%"4"4ehhooEF00xx00 00 00  00f  U U p-".%((//-"`cgg,~ ~ DMQ! ! 080I! ! HE) 88>>E))E) E)P$NR 6#6#6#6$66EHHOO64%((//C%((.. R ehh""$57HHIR XX]]RM ehh""$57HHIM XX]]M%%%*AE ) ) )88??)HcUEHHOO%;<= )41S1S1S1 JSJJJ zzz   $EHHNN$AR$4GI, HHOO,(,S#X,@C,,?=?=D '  '" 'S#c(]# '(#^BF p( HHOOp(#tCy.)p( 9p("*$sCH}*=!> p(r3