L irFddlZddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z mZddlmZmZmZddlZddlmcmZddlmZmZddlmZdd lmZdd l m!Z!m"Z"m#Z#dd l$m%Z%m&Z&dd l'm(Z(m)Z)m*Z*m+Z+dd l,m-Z-gdZ.ej^e0Z1edGddZ2edGddZ3gdZ4edGddZ5edGdde5Z6eddZ7edGdde8Z9edGddZ:edGd d!e:Z;edGd"d#e:Z<edGd$d%e:Z=e"D] Z>d&Z?e?e>d'Z@e#D] ZAe@eA d(ZBd)ZCge)eDdeDd*ejjejjDcic]}|d+c}ZHd,eHe:<d-eHeI<d.eHeJ<eCeHeK<eHeJeHe&<eHeKeHe%<ycc}w)/N) OrderedDict)Iterator)fields is_dataclass)AnyCallableOptional)_fx_map_aggregate _fx_map_arg)CapturedTraceback) compatibility)Graph magic_methodsreflectable_magic_methods)immutable_dictimmutable_list)Argument base_typesNodeTarget)check_for_mutable_operation) TracerBaseGraphAppendingTracer TraceErrorProxy MetaProxy AttributeParameterProxyScopeScopeContextManagerFis_backward_compatiblec,eZdZdZdedeffd ZxZS)r a7Scope object that records the module path and the module type of a module. Scope is used to track the information of the module that contains a Node in a Graph of GraphModule. For example:: class Sub(torch.nn.Module): def forward(self, x): # This will be a call_method Node in GraphModule, # scope for this would be (module_path="sub", module_type=Sub) return x.transpose(1, 2) class M(torch.nn.Module): def __init__(self) -> None: self.sub = Sub() def forward(self, x): # This will be a call_method Node as well, # scope for this would be (module_path="", None) x = x.transpose(1, 2) x = self.sub(x) return x module_path module_typec>t|||_||_yN)super__init__r%r&)selfr%r& __class__s T/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/fx/proxy.pyr*zScope.__init__Gs &&)__name__ __module__ __qualname____doc__strrr* __classcell__r,s@r-r r -s 0'C'c''r.r c8eZdZdZdedeffd ZdZdZxZS)r!zA context manager to track the Scope of Node during symbolic tracing. When entering a forward function of a Module, we'll update the scope information of the current module, and when we exit, we'll restore the previous scope information. scope current_scopect|tj||_|j|_|j |_||_yr()r)r*copy _prev_scoper%r&_scope)r+r7r8r,s r-r*zScopeContextManager.__init__TsD 99U+)55)55 r.c|jSr()r<r+s r- __enter__zScopeContextManager.__enter__bs {{r.c|jj|j_|jj|j_yr()r;r%r<r&)r+argss r-__exit__zScopeContextManager.__exit__es5"&"2"2">"> "&"2"2">"> r.) r/r0r1r2r r*r?rBr4r5s@r-r!r!Ms)   r.r!) nn_module_stacktorch_fnsource_fn_stack original_aten recompute ac_graph_idhas_backward_hook from_nodequantization_tag_numeric_debug_handlecustompartitioner_tagTceZdZUeed<dZeed<dZeed<dZeed<dZ eed<dZ eed<dZ e ed <e ed <ee ee effed <ee ee effed <ed  d'de dedeedfdee efdee deedefdZdej4dej4fdZed deddfdZed  d(de dedeedfdee efdee deedeegdffdZd Zed d!edefd"Z ed d#ddefd$Z!ed d#dde"fd%Z#ed d#ddefd&Z$y))rgraphFrecord_stack_traces!_record_forward_stack_traces_onlycheck_mutable_operations trace_assertsproxy_buffer_attributesforwardtraced_func_namer7 module_stacknode_name_to_scopeTr"NkindtargetrA.kwargsname type_exprreturnc0|dk(r|jr t||||jj||||||}|jj |jj f|j|j<tjrtj}|jd} | r| |_ tD],} | |vstj|| |j | <.t"j$j'dz } |jdddkDr|dd} | |j d<n8|j(r,tj|j(|j d <|j*ru|jsit-j.j1} | rE|j3| } | r2d j5| j7j9|_ t:j=d ||S) a' Inserts a graph node given target, args, kwargs, and name. This method can be overridden to do extra checking, validation, or modification of values used in node creation. For example, one might want to disallow in-place operations from being recorded. call_function stack_tracer in_grad_fnrgrad_fn_seq_nrseq_nrrCzcreate_node %s)rSrrP create_noder7r%r&rYr] fx_tracebackhas_preserved_node_metaget_current_metagetrb_COPY_META_FIELDSr:metatorchautograd_get_sequence_nrrXrQr extractsummary_filter_traceback_framesjoinformatstriplogdebug) r+rZr[rAr\r]r^node current_metarbfield new_seq_nruser_stack_summarys r-rhzTracerBase.create_nodes$ ? "t'D'D 'f =zz%%dFD&$ R JJ " " JJ " ".  *  / / 1+7+H+H+JL&**=9K#. + FL('+yye1D'EDIIe$ F 88:Q>J a014)*:;B? ",DIIh    +/99T5F5F+GDII' (  # #D,<,z.TracerBase._find_user_frame..Cs.@DELL,,55d;;s03N)inspect currentframef_backall)r+pt_filesrs @r-_find_user_framezTracerBase._find_user_frame+sV$$&  LLEHP  r.ac tjt|}| |||St|tr |j St |dr|j|St|trft |dr,|Dcgc]}|j|}}t||St||Dcgc]}|j|c}St|tr|Dcgc]}|j|c}St|tr t||St|trXt|j|j|j|j|j|j St|t"rXt#|j|j|j|j|j|j St|t$j&j(t$j&j*fr|St-|rdt/|Dcic]2}|j0|jt3||j04}}|j5d|j6d|St|gt8t:j<s||dur|St?dt|cc}wcc}wcc}wcc}w)z A method that lowers the objects seen as arguments during symbolic evaluation into Argument types that can be stored in IR. Can be override to support more trace-specific types. __fx_create_arg___fieldsra.zargument of type: ) _create_arg_bypassrltyperrrzhasattrrrrlistr_create_arg_dictslicestartstopsteprangero_ops OpOverloadHigherOrderOperatorrrr]getattrrhr,renumEnumNotImplementedError)r+rhandlerelemrAr|r\s r-rzTracerBase.create_argMsl&%((a1  4# # a 66M Q+ ,&&t, , 5 !q)$;<<$-<<tAw~%47a@dDOOD1@A A 4 678dDOOD)8 8 4 #D!, , 5 !(''  5 !(''  EJJ115::3Q3QR SH !_$AY DOOGAuzz,BCCF##OQ[["fM M 3Z33 4 Q#XH!$6tAwi"@AAE=@8*sK%:K*-K/ 7K4objctd)aFCalled when a proxy object is being converted to a boolean, such as when used in control flow. Normally we don't know what to do because we don't know the value of the proxy, but a custom tracer can attach more information to the graph node using create_node and can choose to return a value. zFsymbolically traced variables cannot be used as inputs to control flowrr+rs r-to_boolzTracerBase.to_bools T  r.ctd)aACalled when a proxy object is being iterated over, such as when used in control flow. Normally we don't know what to do because we don't know the value of the proxy, but a custom tracer can attach more information to the graph node using create_node and can choose to return an iterator. aHProxy object cannot be iterated. This can be attempted when the Proxy is used in a loop or as a *args or **kwargs function argument. See the torch.fx docs on pytorch.org for a more detailed explanation of what types of control flow can be traced, and check out the Proxy docstring for help troubleshooting Proxy iteration errorsrrs r-iterzTracerBase.iters %  r.c$t|dS)zCalled when a proxy object is has the keys() method called. This is what happens when ** is called on a proxy. This should return an iterator it ** is suppose to work in your custom tracer. keysrrs r-rzTracerBase.keyss &yf%''r.NN)NNN)%r/r0r1r__annotations__rQboolrRrSrTrUrWr3r rrrrrrrrr rrhrrrtrrrrrrrrrrr.r-rr{s L %%/4%t3&+d*M4$)T)&c% Lc5c?233S%T "2233$/##'BBBHcM" B S(]# B sm BC=B B0BH$="+"8"8$=   $=L$/!$!7!0!$/##'6:!!!CHo ! S#X ! sm !C=!#D67?3!0!F D$/EBCEBHEB0EBN$/ 7 t 0 $/  H 0 "$/((C(0(r.rc$eZdZdeffd ZxZS)rrPct|||_tdd|_t j |_i|_y)Nrg) r)r*rPr r7 collectionsrrXrY)r+rPr,s r-r*zGraphAppendingTracer.__init__s;  2t_ '335"$r.)r/r0r1rr*r4r5s@r-rrs%e%%r.rc |sJyr(rxs r- assert_fnrs H1r.c eZdZy)rN)r/r0r1rr.r-rrsr.rceZdZdZedddeddfdZd efd Zdd Z d e fd Z d e fd Z dZ ddZd edfdZdZd efdZeddZdZeddZy)ra ``Proxy`` objects are ``Node`` wrappers that flow through the program during symbolic tracing and record all the operations (``torch`` function calls, method calls, operators) that they touch into the growing FX Graph. If you're doing graph transforms, you can wrap your own ``Proxy`` method around a raw ``Node`` so that you can use the overloaded operators to add additional things to a ``Graph``. ``Proxy`` objects cannot be iterated. In other words, the symbolic tracer will throw an error if a ``Proxy`` is used in a loop or as an ``*args``/``**kwargs`` function argument. There are two main ways around this: 1. Factor out the untraceable logic into a top-level function and use ``fx.wrap`` on it. 2. If the control flow is static (i.e. the loop trip count is based on some hyperparameter), the code can be kept in its original position and refactored into something like:: for i in range(self.some_hyperparameter): indexed_item = proxied_value[i] For a more detailed description into the Proxy internals, check out the "Proxy" section in `torch/fx/README.md` Tr"NrztracerOptional[TracerBase]cN|t|j}||_||_yr()rrPrrz)r+rzrs r-r*zProxy.__init__s# >)$**5F  r.r_c6d|jjdS)NzProxy()rzr]r>s r-__repr__zProxy.__repr__s 'q))r.ct||Sr(r)r+ks r- __getattr__zProxy.__getattr__sq!!r.c|jSr(__dict__r>s r- __getstate__zProxy.__getstate__s }}r.cddl}i}|jjD]\}} |j||}|||< d|vsJd|vsJt|d|d}|jD]\}}||j|<|S#t$r@t j d||jj|j|}YwxYw)NrzVShallow copy %s of Proxy because it cannot be deepcopied. Proxy is created for node %srzr) r:ritemsdeepcopy Exceptionrxwarningrzr]r)r+memor:new_dictrvnew_obj new_proxys r- __deepcopy__zProxy.__deepcopy__s MM'') "DAq ''$--40"HQK "!!!8###(6*HX,>? NN$ &DAq$%I  q ! & ' 3IINN  $$))A, 'sB  ACCc||_yr(r)r+ds r- __setstate__zProxy.__setstate__s  r.cF|jjdd|f|z|S)N call_method__call__)rrr+rAr\s r-rzProxy.__call__s({{'' :w~v  r.ctj}|J|j}|Jtt j |j }tjdk\rddl m }|||jd}n|jdz}||}|jdk(rfdt|jDSjj!S) N r bisect_leftc|jSr(offsetrs r-z Proxy.__iter__...s r.keyUNPACK_SEQUENCEc3(K|] }| ywr(r)rrr+s r-rz!Proxy.__iter__..4s8DG8s)rrrrdisget_instructionsrsys version_infobisectrf_lastiopnamerargvalrr)r+r calling_frame inst_listrinst_idxinsts` r-__iter__zProxy.__iter__$s$$&    (((--m.B.BCD   w & *"=006HH%,,1H" ;;+ +8U4;;%78 8{{%%r.c\|jjdtj|fiSNra)rroperatorabsr>s r-__abs__z Proxy.__abs__8s${{''wPRSSr.c|jjr4tj}|J|j}|Jt t j|j}tjdk\rddl m }|||jd}n|jdz}||}|jdk(r||dz}|jJ||jdzdz }|jd k(xr|j d k(xs|jd k(} | r3|jd k(r$|jj#d t$|fiy|jj'|S)Nrrrc|jSr(rrs r-rz Proxy.__bool__..Gs ahhr.rrPOP_JUMP_IF_TRUEr LOAD_GLOBALAssertionErrorLOAD_ASSERTION_ERROR RAISE_VARARGSraT)rrTrrrrrrrrrrrrrargrrrr) r+rr instsrcurr firstlaststarts_with_asserts r-__bool__zProxy.__bool__;sS ;; $ $((*E$ $$!LLM , ,,--m.B.BCDE7*.!%)>)>DVW#++q0:D{{00cAgxx+++TXX]Q./LLM19 (88>||'==# &$++*HKK,,_i$RTU{{""4((r.c8|jj|Sr()rrr>s r-rz Proxy.keys[s{{%%r.ctd)Nz'len' is not supported in symbolic tracing by default. If you want this call to be recorded, please call torch.fx.wrap('len') at module scope RuntimeErrorr>s r-__len__z Proxy.__len___s   r.c |r|nd}|r|ni}ifd}t||t||tdkDr(tdtj d|t t j }t|tjjr.|jf|z}|jd|j||Stjj|r|jd|j ||St|tj"j$r td|jd||||j&j)|j  S) Nrc>t|rd|j<yyr()rr)rclstracerss r- find_tracerz-Proxy.__torch_function__..find_tracerms !S!$(!"r.r z!Found multiple different tracers z" while trying to trace operations rz1Unable to symbolically trace HigherOrderOperatorsrar]) map_aggregatelenr$rrnextrrro_C ScriptMethodownerrr] overridesis_tensor_method_or_propertyr/rrrP_target_to_str)r( orig_methodtypesrAr\r*rr)s` @r-__torch_function__zProxy.__torch_function__fs^t!r#% ) dK(fk* w*+ k588#8#8 9%%'$.D&&}k6F6FfU U ?? 7 7 D&&{33T6 +uzz'E'EF"#VWW&&\\001E1EF ' r.r()r_r)r_rr)r/r0r1r2rrr*r3rrrrrrrrr rrr rr% classmethodr7rr.r-rrs8$/T+A0*#*" dD4 &(7+&(T)$)@$/&0& %%r.rcReZdZdZ d deddffd ZdefdZed fd Z xZ S) rzW A Proxy subclass that propagates metadata (meta['val']) during graph tracing. rzrrc4t|||||_yr()r)r* fake_mode)r+rzrr;r,s r-r*zMetaProxy.__init__s v&"r.r_c6d|jjdS)Nz MetaProxy(rrr>s r-rzMetaProxy.__repr__sDIINN+1--r.c |r|nd}|r|ni}d}|D]}t|ts|}n|Jdt | ||||}|j5||Dcgc]-}t|t r|j jdn|/c}i||j jd<dddt|j |j|jScc}w#1swY9xYw)Nrz5No MetaProxy found in arguments, but one is expected.val) rrr)r7r;rrzrnr) r(r5r6rAr\ meta_proxyrrrr,s r-r7zMetaProxy.__torch_function__st!r  C#y)   % C %*;tVL  ! ! %0MQR 1e(s r-rzzAttribute.nodesN :: 11499dii*@"d Jzzr.cn|jjd|j|jf|z|S)Nr)rrrBrArs r-rzAttribute.__call__s1{{'' 499tyylT&96  r.N) r/r0r1rrr3r*propertyrzrrr.r-rrs@$/*U*#*0*  r.rcpeZdZdZdedeffd ZdefdZe dZ dZ d Z e d Z d Zd ZxZS) rz A special proxy which lets "shape", "size", "dim", and a few other attribute accesses pass through to the underlying module parameter object, so that conditional tests on these attributes will not throw exception during tracing rrzct|||t|tjj sJ||_||_yr()r)r*rronn Parameterparamr])r+rrzr]rLr,s r-r*zParameterProxy.__init__s: v&%!3!3444  r.r_c"d|jdS)NzParameterProxy(rr+r>s r-rzParameterProxy.__repr__s  1--r.c.|jjSr()rLshaper>s r-rOzParameterProxy.shapeszzr.c6|jjSr()rLsizer>s r-rQzParameterProxy.sizeszz  r.c6|jjSr()rLdimr>s r-rSzParameterProxy.dimszz~~r.c.|jjSr()rLndimr>s r-rUzParameterProxy.ndimszzr.c6|jjSr()rLnumelr>s r-rWzParameterProxy.numelszz!!r.c6|jjSr()rLnelementr>s r-rYzParameterProxy.nelementszz""$$r.)r/r0r1r2rrr*r3rrGrOrQrSrUrWrYr4r5s@r-rrsc z .#.  ! "%r.rcjfd}|_djdd}tt||y)Ncj|dj}tt}|jd|||S)Nrra)rrrr)rAr\rr[methods r-implz_scope..impls3!W^^FXv.F&&fM Mr.___)r/rwsetattrr)r\r]as_magics` r-r<r<s6 N   S)*"-x&r.cxdjdd}fd}||_||_tt||y)N__rr_r^cdtt}|jjd|||fiSr)rrrr)r+rhsr[orig_method_names r-r]z!_define_reflectable..impls/#34{{''#tbQQr.)rwr/r1r`r)rf method_namer]s` r-_define_reflectablerhsA(..s34B7KR DM#D E;%r.ctd|)NzJKeys for dictionaries used as an argument cannot contain a Node. Got key: r#)rs r-_no_nodes_errorrjs    r.ci}|jD]J\}}t|ts!|j|}t |t |j|||<L|Sr()rrr3rmap_argrj)r+rrrrs r-rrs] A "1!S!"A A 'q!!" Hr..c|Sr(rr+rs r-rr)sqr.c|jSr()rzros r-rr2s AFFr.c\t|Dcgc]}|j|c}Scc}wr()rrr+rrs r-rr3s#EUV2WT4??43H2W,X2Ws)cJ|Dcgc]}|j|c}Scc}wr()rrrs r-rr4sa+PdDOOD,A+P+Ps )Lrr:rrrloggingrrrrcollections.abcr dataclassesrrtypingrrr rotorch.fx.tracebackfxritorch._Cr r,r rltorch.utils._tracebackr _compatibilityrrPrrrimmutable_collectionsrrrzrrrroperator_schemasr__all__ getLoggerr/rxr r!rmrrr ValueErrorrrrrrr\r<rhrfrjrrrrrrrrr)ts0r-rs  #$,** ))O4)BBA449 g!e,''-'>e,-:  d+|(|(,|(@ d+%:%,%e, - d+  , d+{{,{|e,#I#I-#ILd+  , 0e,"%U"%-"%J F' 6N  &2*()*    T  S      &&    35X5P4+4%7%=>"%7%=>" s( H