L i|pUddlZddlZddlZddlZddlZddlmZmZmZddl m Z m Z m Z m Z mZddlmZmZmZddlZddlmZmZmZddlmZmZmZddlmZdd lmZd d l m!Z!e rd d l"m#Z#gd Z$ejJe&Z'ee(e)e*e+e,ejZej\ej^ej`ejbej<jdejfejhejjfZ6e6jnZ8ee de fe(fZ9ee:d<e ee;dedee(dfeede>Z?edZ@edZAeBjgdZDejjejjejjgZJeKe de fe:d<ejejejjjejjjejjjejjjejjjejjejjejjejjjejh eJZ\e]e de fe:d<e^ejdr/e\jejjje!dde e@eAfde e@eAffdZad e de fde(fd!Zbd"ecde(fd#Zdd$e de fde(fd%Zee*d&fd'ecd(e*de(fd)Zfe!d*Gd+deZge!d*d,e?de egge>fde?fd-Zhe!d*d,e?de e>ge>fde?fd.Ziy)/N)IterableMappingSequence)AnyCallableOptional TYPE_CHECKINGUnion) ParamSpec TypeAliasTypeVar)_fx_map_aggregate _fx_map_arg _NodeBase)ArgsKwargsPairnormalize_functionnormalize_module) dtype_abbrs)ops) compatibility)Graph)Nodemap_arg map_aggregatehas_side_effect.TargetArgument.r r ArgumentT)bound_P_R) placeholder call_method call_module call_functionget_attroutputroot1_side_effectful_need_to_be_preserved_pre_dispatch_side_effectful_functionsresize_storage_bytes_Fis_backward_compatiblefnreturnc0tj||SN)r-add)r1s S/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/fx/node.pyrrms!!"% I orig_methodc|j}|j}||SttjjfD]}t ||d|us|jcSt d|)Nzcannot find module for )__name__ __module__torchnn functionalgetattr RuntimeError)r8namemoduleguesss r6_find_module_of_methodrDtsq   D  # #F  ,,-" 5$ % 4>> !" 0 > ??r7objc.t|trPt|tjs6|jdk(r |j S|jd|j S|duryt|tj r |jSt|S)a:Return the repr() of an object, special-casing types (internal helper). If obj is a type, we return a shorter version than the default type.__repr__, based on the module and qualified name, which is typically enough to uniquely identify a type. For everything else, we fall back on repr(obj). builtins..z...) isinstancetypetypes GenericAliasr; __qualname__ FunctionTyper:repr)rEs r6 _type_reprrPs#tZU5G5G%H >>Z '## #..!3#3#3"455 cz#u))*|| 9r7funcctt|jd|ur |jSt|tj tj fr'|ttj|jdusC|jtjjk(r+|jd|jk(rd|jS|j}|dk(r6 tj|jddj}t%|}|j'dd}|d k(r |d k(rd |z}|d |S#t $r}t#d|d}~wwxYw) NzTensor.z torch.Tensor.z=rzUnable to represent lambdaz torch._opsz torch.opsr<segment_reduce_rH)r?rGr:rIrKMethodDescriptorTypeWrapperDescriptorTyper<Tensorr;_tensorrMinspect getsourcesplitstrip Exceptionr@rDreplace)rQrAerBs r6_get_qualified_namerasDx-5}} 4%44e6Q6QRS GELL$--> > 5==111   74==/!: :t}}o.. ==D z D$$T*005a8>>@D$D )F ^^kFT%55TzXQtf  D;z_format_arg..% %sA3;MKN  !!rz, ...[total_len=][c3FK|]\}}|ks t|ywr4rirks r6roz_format_arg..rprq,()c3BK|]\}}|dt|yw): Nri)rlkvs r6roz_format_arg..s$O41a2k!n%56Os{}%) hasattrrfrIlistjoin enumeratelentupledictitemsrstr)rcrdr maybe_len maybe_comma items_strs ` r6rjrjsVs()%%'' C   )23  c(\A--B5Ec#hZq3Q 5')A&& C   )23  c(\A--B5Ec#hZq3Q !X]c 5'+yk33 C IIO399;OO I;b!!#tSX~3xr7TcleZdZUdZeded<eedfed<ded<eed<eed <d ed <ed ed <ed ed<ee ed<e ed<ee dgefed<eee fed<e d dSddded ed d deddeedfdee ddffd Z deee ffdZ deee fddfdZedTdZedTdZe ddUd Zd!ddefd"Zd!ddefd#Zd!ddefd$Zd!ddefd%Ze ddUd&ZdVd'Zedeed(ffd)Zej8d*eed(fddfd+Zedeeeffd,Zej8d-eeefddfd.Zededfd/Ze dd0e d1eddfd2Z!e dd0e d1eddfd3Z"e dd4ed1eddfd5Z#edeefd6Z$e$j8d7eeddfd8Z$defd9Z%e&d e'defd:Z(e d dWd;d<d=eeed>eeed?edeefd@Z)e ddAfd;dBdCddDe dgefdEededfdFZ*e d;dXdGedefdHZ+e d; dYdIe,jZj\dJeee dKeeee fdLedee/f dMZ0e ddZdNZ1dOeddfdPZ2dedQe ddfdRZ3xZ4S)[rao ``Node`` is the data structure that represents individual operations within a ``Graph``. For the most part, Nodes represent callsites to various entities, such as operators, methods, and Modules (some exceptions include nodes that specify function inputs and outputs). Each ``Node`` has a function specified by its ``op`` property. The ``Node`` semantics for each value of ``op`` are as follows: - ``placeholder`` represents a function input. The ``name`` attribute specifies the name this value will take on. ``target`` is similarly the name of the argument. ``args`` holds either: 1) nothing, or 2) a single argument denoting the default parameter of the function input. ``kwargs`` is don't-care. Placeholders correspond to the function parameters (e.g. ``x``) in the graph printout. - ``get_attr`` retrieves a parameter from the module hierarchy. ``name`` is similarly the name the result of the fetch is assigned to. ``target`` is the fully-qualified name of the parameter's position in the module hierarchy. ``args`` and ``kwargs`` are don't-care - ``call_function`` applies a free function to some values. ``name`` is similarly the name of the value to assign to. ``target`` is the function to be applied. ``args`` and ``kwargs`` represent the arguments to the function, following the Python calling convention - ``call_module`` applies a module in the module hierarchy's ``forward()`` method to given arguments. ``name`` is as previous. ``target`` is the fully-qualified name of the module in the module hierarchy to call. ``args`` and ``kwargs`` represent the arguments to invoke the module on, *excluding the self argument*. - ``call_method`` calls a method on a value. ``name`` is as similar. ``target`` is the string name of the method to apply to the ``self`` argument. ``args`` and ``kwargs`` represent the arguments to invoke the module on, *including the self argument* - ``output`` contains the output of the traced function in its ``args[0]`` attribute. This corresponds to the "return" statement in the Graph printout. r_argsr _kwargsrgraphrAoprtarget)rN _input_nodesusersrJ _sort_key_repr_fnmetaTr/Nargskwargs return_typer2c P|dk(r6t|sptd|d|d|dtj|d |tvsJt |t s+td|d|d|dtj|d t|!||||||j||y) a} Instantiate an instance of ``Node``. Note: most often, you want to use the Graph APIs, i.e. ``Graph.call_module``, ``Graph.call_method``, etc. rather than instantiating a ``Node`` directly. Args: graph (Graph): The ``Graph`` to which this ``Node`` should belong. name (str): The name to which the output of this ``Node`` should be assigned op (str): The opcode for this ``Node``. Can be one of 'placeholder', 'call_method', 'call_module', 'call_function', 'get_attr', 'output' target ('Target'): The target this op should call. See the broader ``Node`` docstring for more details. args (Tuple['Argument']): The args to be passed to ``target`` kwargs (Dict[str, 'Argument']): The kwargs to be passed to ``target`` return_type (Optional[Any]): The python type expression representing the type of the output of this node. This field can be used for annotation of values in the generated code or for other types of analyses. r(zNode [graph = z , name = 'z '] target z has type z but a Callable is expectedz but a str is expectedN) callable ValueErrorr<typename _legal_opsrIrsuper__init___update_args_kwargs) selfrrArrrrr __class__s r6rz Node.__init__sJ  F# $UG:dV:fXZX]XfXfgmXnWop11 # ##fc* $UG:dV:fXZX]XfXfgmXnWop,, b&+>   v.r7cni|j|j|j|j|j|j|j |j |j|j|j|j|j|j|j|jdS)N)rrArrrJrrr_erased_prev_nextrrrr)__dict__rrArrrrrrrrrrrrrs r6 __getstate__zNode.__getstate__Hs mm ZZII''kkKKZZ||||ZZZZ --ZZ II!  r7statecN|jD]\}}t|||yr4)rsetattr)rrr{r|s r6 __setstate__zNode.__setstate__\s'KKM DAq D!Q  r7c|jS)z Returns the next ``Node`` in the linked list of Nodes. Returns: The next ``Node`` in the linked list of Nodes. )rrs r6nextz Node.next`zzr7c|jS)z Returns the previous ``Node`` in the linked list of Nodes. Returns: The previous ``Node`` in the linked list of Nodes. )rrs r6prevz Node.prevkrr7cJ|j|jk(sJd||k(rtjdy|j|j}||c|_|_||c|_|_|jj }|j j }t|t|kDr$|dt|dz^}}g||dz|_yt|t|kr$|dt|dz^}}g||dz |_yg|d|_y)aD Insert x before this node in the list of nodes in the graph. Example:: Before: p -> self bx -> x -> ax After: p -> x -> self bx -> ax Args: x (Node): The node to put before this node. Must be a member of the same graph. z0Attempting to move a Node into a different GraphzMTrying to prepend a node to itself. This behavior has no effect on the graph.Nrr)rlogdebug_remove_from_listrrrr)rxppsknskprefixrms r6prependz Node.prependvszzQWW$X&XX$ 19 II_    JJa"Agggg s8c#h ~SA.LVS,F,C!G,AK XC ~SA.LVS,F,C!G,AK#C))AKr7otherc4|j|jkDSr4rrrs r6__gt__z Node.__gt__~~//r7c4|j|jkSr4rrs r6__lt__z Node.__lt__rr7c||kDxs||k(Sr4rs r6__ge__z Node.__ge__e|,tu},r7c||kxs||k(Sr4rrs r6__le__z Node.__le__rr7c:|jj|y)z Insert ``x`` after this node in the list of nodes in the graph. Equivalent to ``self.next.prepend(x)`` Args: x (Node): The node to put after this node. Must be a member of the same graph. N)rr)rrs r6appendz Node.appends 1r7cR|j|j}}||c|_|_yr4)rr)rrns r6rzNode._remove_from_lists#zz4::1ar7.c|jS)a@ The tuple of arguments to this ``Node``. The interpretation of arguments depends on the node's opcode. See the :class:`Node` docstring for more information. Assignment to this property is allowed. All accounting of uses and users is updated automatically on assignment. )rrs r6rz Node.argsszzr7rnc<|j||jy)z Set the tuple of arguments to this Node. The interpretation of arguments depends on the node's opcode. See the ``fx.Graph`` docstring for more information. N)rr)rrns r6rz Node.argss   DLL1r7c|jS)aG The dict of keyword arguments to this ``Node``. The interpretation of arguments depends on the node's opcode. See the :class:`Node` docstring for more information. Assignment to this property is allowed. All accounting of uses and users is updated automatically on assignment. )rrs r6rz Node.kwargss||r7r{c<|j|j|y)z Set the dict of kwargs to this Node. The interpretation of arguments depends on the node's opcode. See the ``fx.Graph`` docstring for more information. N)rr)rr{s r6rz Node.kwargss   Q/r7cHt|jjS)aE Return all Nodes that are inputs to this Node. This is equivalent to iterating over ``args`` and ``kwargs`` and only collecting the values that are Nodes. Returns: List of ``Nodes`` that appear in the ``args`` and ``kwargs`` of this ``Node``, in that order. )rrkeysrs r6all_input_nodeszNode.all_input_nodessD%%**,--r7rmrccXt|j}|||<t||_y)a3 Update an existing positional argument to contain the new value ``arg``. After calling, ``self.args[idx] == arg``. Args: idx (int): The index into ``self.args`` of the element to update arg (Argument): The new argument value to write into ``args`` N)rrr)rrmrcrs r6 update_argzNode.update_args%DIIS $K r7cd|cxkrt|jksJdJd|jd|}|j|d}||fz|z|_i}t||j|j D]G}||j vs|j j ||jj |Iy)a  Insert an positional argument to the argument list with given index. Args: idx (int): The index of the element in ``self.args`` to be inserted before. arg (Argument): The new argument value to insert into ``args`` rz6insert_args index must be between 0 and len(self.args)N)rrrr setdefaultrrr)rrmrc args_left args_right_new_input_nodesnew_uses r6 insert_argzNode.insert_argsC)3tyy>) D ) D )IIdsO YYst_ #'*4 -/C)445',,. /Gd///!!,,W5 ((. /r7keyc2i|j||i|_y)a2 Update an existing keyword argument to contain the new value ``arg``. After calling, ``self.kwargs[key] == arg``. Args: key (str): The key in ``self.kwargs`` of the element to update arg (Argument): The new argument value to write into ``kwargs`` N)r)rrrcs r6 update_kwargzNode.update_kwargs0/c3/ r7c:|jjddS)a Return the Python stack trace that was recorded during tracing, if any. When traced with fx.Tracer, this property is usually populated by `Tracer.create_proxy`. To record stack traces during tracing for debug purposes, set `record_stack_traces = True` on the `Tracer` instance. When traced with dynamo, this property will be populated by default by `OutputGraph.create_proxy`. stack_trace would have the innermost frame at the end of the string. stack_traceN)rgetrs r6rzNode.stack_trace(syy}}]D11r7tracec"||jd<y)Nr)r)rrs r6rzNode.stack_trace6s#( - r7cT|jr|j|S|jSr4)rrArs r6__repr__z Node.__repr__:s" ====& &yyr7ct|tr|St|drBt|dd}| t |S|j dk(rd|S|j dk(rd|St |S)z Make target printouts more user-friendly. 1) builtins will be printed as `builtins.xyz` 2) operators will be printed as `operator.xyz` 3) other callables will be printed with qualified name, e.g. torch.add r;r:NrGz builtins. _operatorz operator.)rIrrr?rar;)rrAs r6_pretty_print_targetzNode._pretty_print_target?s~ fc "M 6< (6:t4D| +622  J."4&))""k1"4&))"6**r7F)include_tensor_metadataplaceholder_namesmaybe_return_typenamerc|jdk(rt|jtsJ|j}||jr|dt |jzndz }|r|j |y|jrt |jdnd}|jrdt|jdzdznd}d |jd |d t|jd |jd |jd| S|jdk(rx|jt |jdnd}d |jd |d t|jd |jd |j|jd S|jdk(r;|jr|rdt |j|d<d|jdSdtdtfd}|jjd|jjd|jjdd}d} |rt|tj r~|j"tj$tj&fvrR||j)} |j*} dt,|j.||j0| | d} n&|jt |jdnd} d |jd | d t|jd |jd |j|jdt3|jdt3|j4dS)aO Return a descriptive string representation of ``self``. This method can be used with no arguments as a debugging utility. This function is also used internally in the ``__str__`` method of ``Graph``. Together, the strings in ``placeholder_names`` and ``maybe_return_typename`` make up the signature of the autogenerated ``forward`` function in this Graph's surrounding GraphModule. ``placeholder_names`` and ``maybe_return_typename`` should not be used otherwise. Args: placeholder_names: A list that will store formatted strings representing the placeholders in the generated ``forward`` function. Internal use only. maybe_return_typename: A single-element list that will store a formatted string representing the output of the generated ``forward`` function. Internal use only. include_tensor_metadata: Whether to include tensor metadata Returns: str: If 1) we're using ``format_node`` as an internal helper in the ``__str__`` method of ``Graph``, and 2) ``self`` is a placeholder Node, return ``None``. Otherwise, return a descriptive string representation of the current Node. r%rzrrN z (default=rrxrz : z [num_users=z] = z[target=rsr)r*z -> zreturn shaper2c dddj|Dcgc] }t|c}dScc}w)Nrtrgrs)rr)rrs r6stringify_shapez)Node.format_node..stringify_shapes-499e%<c!f%<=>a@@%>:;OHNN<[;\()*;)z Node.sr7)propagate_meta replace_withdelete_user_cbr c|rPtjdk(sJdjjD]\}}|j|<tj}g}j j }|D]} || s|j| dtdtffd } t|ddr&|jD]} | j| t| j| } t| j| } t| t sJt| t"sJ| j%| | tjt|z dk(sJ|Dcgc] }||vs| c}Scc}w) a Replace all uses of ``self`` in the Graph with the Node ``replace_with``. Args: replace_with (Node): The node to replace all uses of ``self`` with. delete_user_cb (Callable): Callback that is called to determine whether a given user of the self node should be removed. propagate_meta (bool): Whether or not to copy all properties on the .meta field of the original node onto the replacement node. For safety, this is only valid to do if the replacement node doesn't already have an existing .meta field. Returns: The list of Nodes on which this change was made. rzmCalled node.replace_all_uses_with(replace_with, propagate_meta=True), but replace_with already has .meta keysrr2c|k(rS|Sr4r)rr rs r6maybe_replace_nodez6Node.replace_all_uses_with..maybe_replace_nodes9''Hr7_replace_hooksNoldnewr)rrrrrr owning_modulerrr?rrArrrrIrrr)rr r r r{r| to_processskippedmuse_noder replace_hooknew_args new_kwargsrs`` r6replace_all_uses_withzNode.replace_all_uses_withs2 |(()Q. : . ) )1'( !!!$ )$**%  JJ $ $" ?H!(+x( d t  q*D1$%$4$4QL T|/@/@xPQ#8==2DEH$X__6HIJh. ..j$/ //  ( (: >' ?*4::W-222%:a')9:::s 5 F?F impure_randomc N|jdvry|jdk(rt|jdd}| |jry|rt|jddrytj tj tjtjtjtjtjtjtjtjtjh }|j|vry|jt vS|jdk(ro|j"j$Jd |j"j$j'|j}|Jd |jt|d dSy) a3 Returns whether this op is impure, i.e. if its op is a placeholder or output, or if a call_function or call_module which is impure. Args: impure_random (bool): Whether to treat rand op as impure. Returns: bool: If the op is impure or not. >r*r%Tr(_schemaN_nondeterministic_seededFr'z1self.graph.owning_module not set for purity checkz'Did not find expected submodule target _is_impure)rr?r is_mutabler<randrandnrandintrandperm rand_like randn_like randint_likenormalpoisson bernoulli multinomialr-rr get_submodule)rrschema_random_functions target_mods r6 is_impurezNode.is_impures^ 77/ / 77o %T[[)T:F!f&7&74;;(BEJ     ""  !! ! {{//;;";; ; 77m #::++7 C 711?? LJ) 9$++G ):|U; ;r7r+ arg_types kwarg_typesnormalize_to_only_use_kwargsc`|jdk(rFt|jsJt|j|j|j |||S|jdk(rJt |jtsJt||j|j|j |Sy)a Returns normalized arguments to Python targets. This means that `args/kwargs` will be matched up to the module/functional's signature and return exclusively kwargs in positional order if `normalize_to_only_use_kwargs` is true. Also populates default values. Does not support positional-only parameters or varargs parameters. Supports module calls. May require `arg_types` and `kwarg_types` in order to disambiguate overloads. Args: root (torch.nn.Module): Module upon which to resolve module targets. arg_types (Optional[Tuple[Any]]): Tuple of arg types for the args kwarg_types (Optional[Dict[str, Any]]): Dict of arg types for the kwargs normalize_to_only_use_kwargs (bool): Whether to normalize to only use kwargs. Returns: Returns NamedTuple ArgsKwargsPair, or `None` if not successful. r()r5r'N) rrrrrrrIrr)rr+r3r4r5s r6normalized_argumentszNode.normalized_arguments2s< 77o %DKK( ((%   -I  WW %dkk3/ //#   -I  r7cdtdtffd }|jj}t|ddr&|jD]}|j |t |j|}t |j|}t|tsJt|tsJ|j||y)a Loop through input nodes of ``self``, and replace all instances of ``old_input`` with ``new_input``. Args: old_input (Node): The old input node to be replaced. new_input (Node): The new input node to replace ``old_input``. rr2c|k(rS|Sr4r)r new_input old_inputs r6rz3Node.replace_input_with..maybe_replace_noders !Y9 5A 5r7rNr) rrrr?rrArrrrIrrr)rr;r:rrrrrs `` r6replace_input_withzNode.replace_input_withfs 6$ 64 6 JJ $ $ 1& - ! 0 0 K  TJ Ktyy*<= .@A (E****d+++   :6r7 candidatec||jk(ry|jjj|d}||_|jjj ||yr4)rAr_graph_namespace create_name_rename_object)rr=rAs r6_renamez Node._renamesN  ! zz**66y$G  ##224>r7valuec.|dk(rnt|drb|jj}t|ddr?t |t sJ|j D]}|jD] }|||| d}t||rUt|jdr?||jjvr'd}|jjj|tj||||r&|jjj|yy)NrArrF_find_nodes_lookup_tableT) rrrr?rIrrrrEremoveobject __setattr__insert)rrArCrrrupdates r6rHzNode.__setattr__s 6>gdF3 ((Aq*D1!%--- JJED()(8(8E $5tDEE D$  $>? ;;;F JJ / / 6 6t <4u-  JJ / / 6 6t < r7r4)r2r)rrr2N)r2N)NN)T)NNF)r;rr:rr2N)5r:r;rM__doc__r__annotations__rrrrrrrrrpropertyrrrboolrrrrrrr rsetterrrrintrrrrr staticmethodrGrrrr2r<r=Modulerr7r<rBrH __classcell__)rs@r6rrs`6  !! #z/ "" N I G |$$   3-Nx# .// sCx.$/&*2/2/2/  2/  2/ O$ 2/S*_%2/c]2/ 2/02/h d38n ( $sCx. T $/"$0"$H0F0t00F0t0-F-t--F-t-$/0  eHcM*   [[2eHcM*2t22 S(]+   ]]0S(]+000 .f . .$/  c    T  0  $//c//T/0/2$/ 0 0( 0t 00 0 2Xc] 2 2)#)4))# +V+++0$/2659[ ). [#DI.[ (S 2[ "& [ # [0[z$/4E8; % 8;8;!&408;  8; f 8;08;t%0>t>t>1>@%0+/04-2 1hhoo1E#J'1d38n- 1 '+ 1 . ! 111f$/7072???==C=D=r7rnc>t|sJdt||S)z Apply fn recursively to each Node appearing in arg. arg may be a list, tuple, slice, or dict with string keys: the return value will have the same type and structure. z.torch.fx.map_arg(a, fn): fn must be a callable)rrrnr1s r6rrs# B<III< q" r7ct||S)z Apply fn recursively to each object appearing in arg. arg may be a list, tuple, slice, or dict with string keys: the return value will have the same type and structure. )rrUs r6rrs Q ##r7)jrGrZloggingoperatorrKcollections.abcrrrtypingrrrr r typing_extensionsr r r r<torch._Crrrtorch.fx.operator_schemasrrrtorch.utils._dtype_abbrsr_opsr_compatibilityrrr__all__ getLoggerr:rrrPfloatrNcomplexrrXr memory_formatr OpOverloadSymIntSymBoolSymFloatBaseArgumentTypes__args__ base_typesrrLrsliceranger r!r#r$rfromkeysr_C_set_grad_enabledamp_enter_autocast_exit_autocastr,r_assert _assert_asyncatenmsg_assert_scalardefault_assert_tensor_metadatasym_constrain_rangesym_constrain_range_for_sizeprofiler_record_function_enter_record_function_enter_new_record_function_exitinductoraccumulate_grad_setitemr-setrr5r.rrDrGrPrarjrrrrr7r6rs 77@@;; >> 1) Ag!   KK LL LL  LL JJ LL MM NN   ' ' (38,c12 2   oZ       Kx 0 t_ T] ]]   HH II IIO14c8J3K MM IIII$$II%%--II!!))II**22MM((MM,,MM''MM""** 6763xS12  4==12!!$--"E"E"M"MNe,R(Xb"f-=- @c(:@s@Fs(hsCx0S<49<V5C<d+G =9G =,G =Td+yhvx'78Y,d+$Y$HhZ-A$B$y$,$r7