
    N j                         S SK r S SKJr  S SKJrJr  S SKrS SKrS SKJr  S SKJ	r	J
r
  S SKJrJrJrJr  S SKJrJrJr  SSKJr   " S	 S
\
5      r " S S\5      rg)    N)Callable)AnyOptional)ProxyTransformer)Argumentmap_aggregateNodeTarget)create_type_hintnormalize_functionnormalize_module   )AnnotateTypesWithSchemac                     ^  \ rS rSrSr SS\R                  R                  S\4U 4S jjjr	S\
S\4U 4S jjr  SS	\S
\\S4   S\\\4   S\\\S4      S\\\\4      4
U 4S jjjrS	\S
\\S4   S\\\4   4U 4S jjrSrU =r$ )NormalizeArgs   aS  
Normalize arguments to Python targets. This means that
`args/kwargs` will be matched up to the module/functional's
signature and rewritten to 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 (*args, **kwargs).

If the nodes have 'type' metadata, it will use it to disambiguate
overloads. Otherwise, it will throw an error.

Example usage:
    m = torchvision.models.resnet18()
    traced = torch.fx.symbolic_trace(m)
    traced = NormalizeArgs(traced).transform()
modulenormalize_to_only_use_kwargsc                 >   > [         TU ]  U5        0 U l        X l        g N)super__init__node_mapr   )selfr   r   	__class__s      p/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/torch/fx/experimental/normalize.pyr   NormalizeArgs.__init__&   s     	 +-,H)    nreturnc                 h  >^ U R                  T5      u  p#U4S jn[        TR                  U5      n[        U[        5      (       d  [        S[        U5       35      e[	        S U 5       5      nUR                  5        VVs0 s H  u  pgXd" U5      _M     nnnTR                  S:X  a  U R                  TR                  X#XX5      n	O[        T
U ]1  T5      n	TR                  S:w  aE  TU R                  U	'   TR                  U	R                  l        TR                  U	R                  l        U	$ s  snnf )Nc                    > [        U [        R                  5      (       a  TR                  R	                  S5      $ [        U 5      $ )Ntype)
isinstancefxr
   metagetr$   )argr    s    r   get_type(NormalizeArgs.run_node.<locals>.get_type0   s1    #rww''vvzz&))9r   zExpected tuple, got c              3   8   #    U  H  n[        U5      v   M     g 7fr   )r   ).0is     r   	<genexpr>)NormalizeArgs.run_node.<locals>.<genexpr>8   s     Ay!*1--ys   call_functionoutput)fetch_args_kwargs_from_envr	   argsr%   tupleAssertionErrorr$   itemsopr1   targetr   run_noder   r'   node)r   r    r4   kwargsr*   	arg_typeskvkwarg_typesoutr   s    `        r   r:   NormalizeArgs.run_node-   s    66q9	
 "!&&(3	)U++ #7Y7H!IJJAyAA	28,,.A.$!q(1+~.A44?"$$QXXtYTC'"1%C448!"DMM#FFCHHMFFCHHM
 Bs   D.r9   r4   .r<   r=   r@   c                    > [        U5      (       d  [        S[        U5       35      e[        UUUUUU R                  5      nU(       a!  Uu  pxU R
                  R                  SXU5      $ [        T	U ]!  XU5      $ )NExpected callable target, got r1   )	callabler6   r$   r   r   tracercreate_proxyr   r1   )
r   r9   r4   r<   r=   r@   new_args_and_kwargsnew_args
new_kwargsr   s
            r   r1   NormalizeArgs.call_functionD   s      #A$v,!PQQ0--
 #6 H;;++:  7(v>>r   c                    > [        U[        5      (       d  [        S[        U5       35      e[	        U R
                  UUUU R                  5      nU(       a  Uu  pV[        TU ]!  XU5      $ [        TU ]!  XU5      $ )NzExpected str target, got )	r%   strr6   r$   r   r   r   r   call_module)r   r9   r4   r<   rH   rI   rJ   r   s          r   rN   NormalizeArgs.call_module^   s~     &#&& #<T&\N!KLL.KK--
 #6 H7&vDD7&vV<<r   )r   r   )T)NN)__name__
__module____qualname____firstlineno____doc__torchr&   GraphModuleboolr   r
   r   r:   r   r5   r   dictrM   r   r1   rN   __static_attributes____classcell__r   s   @r   r   r      s    $ RVIhh**IJNI I$ 3 8 0404?? HcM"? S#X	?
 E#s(O,? d38n-? ?4==$)(C-$8=BFsCx.= =r   r   c                     ^  \ rS rSr% Sr\R                  \R                  \R                  \R                  \R                  \R                  \R                  \R                  \R                  \R                  \R                  \R                  \R                   \R                   \R"                  \R"                  \R$                  \R$                  \R&                  \R&                  \R(                  \R(                  \R*                  \R*                  0r\\\\/\4   \\\/\4   4   \S'   S\S\\S4   S\\\4   4U 4S jjrS	r U =r!$ )
NormalizeOperatorsq   a  
Normalize callsites that are different ways of "spelling" the same
invocation into a single, canonical call. Currently supports:

1. Normalize operators (e.g. operator.add) to the `torch` ops they
   ultimately invoke (e.g. torch.add) when it is possible to statically
   reason that

Example usage:

    m = torchvision.models.resnet18()

    traced = torch.fx.symbolic_trace(m)

    traced = NormalizeOperators(traced).transform()
binary_magic_method_remapr9   r4   .r<   c                   > [        U5      (       d  [        S[        U5       35      eXR                  ;   a@  [	        U5      S:w  a  [
        TU ]  XU5      $ Uu  pE[
        TU ]  U R                  U   XE40 S9$ [
        TU ]  XU5      $ )NrD      )r9   r4   r<   )rE   r6   r$   r_   lenr   r1   )r   r9   r4   r<   lhsrhsr   s         r   r1    NormalizeOperators.call_function   s      #A$v,!PQQ3334yA~w,V6BBHC7(55f=Z )   w$V6::r    )"rP   rQ   rR   rS   rT   rU   addoperatormulsubdivtruedivfloor_dividefloordiv	remaindermodeqneltlegtger_   rX   r   r   __annotations__r   r5   r   rM   r1   rY   rZ   r[   s   @r   r]   r]   q   s   ( 			8<<		8<<		8<<		8##H--(++(++(++(++(++(++	 t#sS!8S#JO#<<  ";;$)(C-$8;BFsCx.; ;r   r]   )rh   collections.abcr   typingr   r   rU   torch.fxr&   r   r   torch.fx.noder   r	   r
   r   torch.fx.operator_schemasr   r   r   schema_type_annotationr   r   r]   rf   r   r   <module>r~      sK     $      ' ? ?  <Z=K Z=z7;0 7;r   