
    IЦi                         S SK r S SKJrJrJr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)AnyCallableDictOptionalTuple)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      ^/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/fx/experimental/normalize.pyr   NormalizeArgs.__init__%   s     	 +-,H)    nreturnc                 n  >^ U R                  T5      u  p#U4S jn[        TR                  U5      n[        U[        5      (       d   e[	        U Vs/ s H  n[        U5      PM     sn5      nUR                  5        VVs0 s H  u  pxXt" U5      _M     n	nnTR                  S:X  a  U R                  TR                  X#XY5      n
O[        TU ]-  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f s  snnf )Nc                    > [        U [        R                  5      (       a!  STR                  ;   a  TR                  S   $ S $ [	        U 5      $ )Ntype)
isinstancefxr   metar&   )argr"   s    r   get_type(NormalizeArgs.run_node.<locals>.get_type/   s<    #rww'')/166)9qvvf~CtC9r!   call_functionoutput)fetch_args_kwargs_from_envr   argsr'   tupler   itemsopr-   targetr   run_noder   r)   noder&   )r   r"   r0   kwargsr+   	arg_typesikvkwarg_typesoutr   s    `         r   r5   NormalizeArgs.run_node,   s    66q9	
 "!&&(3	)U++++	B	1+A.	BC	28,,.A.$!q(1+~.A44?"$$QXXtYTC'"1%C448!"DMM#FFCHHMFFCHHM
 CAs   D,D1r4   r0   .r7   r8   r<   c                    > [        U5      (       d   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      $ )Nr-   )callabler   r   tracercreate_proxyr   r-   )
r   r4   r0   r7   r8   r<   new_args_and_kwargsnew_args
new_kwargsr   s
            r   r-   NormalizeArgs.call_functionB   sx     0--
 #6 H;;++:  7(v>>r!   c                    > [        U[        5      (       d   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      $ r   )r'   strr   r   r   r   call_module)r   r4   r0   r7   rC   rD   rE   r   s          r   rI   NormalizeArgs.call_module[   sm     &#&&&&.KK--
 #6 H7&vDD7&vV<<r!   )r   r   )T)NN)__name__
__module____qualname____firstlineno____doc__torchr(   GraphModuleboolr   r   r   r5   r   r   r
   r   rH   r   r-   rI   __static_attributes____classcell__r   s   @r   r   r      s    $ RVIhh**IJNI I$ 3 6 0404?? HcM"? S#X	?
 E#s(O,? d38n-? ?2==$)(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!$ )
NormalizeOperatorsm   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_remapr4   r0   .r7   c                    > [        U5      (       d   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      $ )N   )r4   r0   r7   )r@   rY   lenr   r-   )r   r4   r0   r7   lhsrhsr   s         r   r-    NormalizeOperators.call_function   s     3334yA~w,V6BBHC7(55f=Z )   w$V6::r!    )"rK   rL   rM   rN   rO   rP   addoperatormulsubdivtruedivfloor_dividefloordiv	remaindermodeqneltlegtgerY   r   r   r   __annotations__r   r   r
   rH   r-   rS   rT   rU   s   @r   rW   rW   m   s   ( 			8<<		8<<		8<<		8##H--(++(++(++(++(++(++	 t#sS!8S#JO#<<  ";;$)(C-$8;BFsCx.; ;r!   rW   )rb   typingr   r   r   r   r   rP   torch.fxr(   r   r	   torch.fx.noder
   r   r   r   torch.fx.operator_schemasr   r   r   schema_type_annotationr   r   rW   r`   r!   r   <module>rw      sK     7 7    ' ? ?  <W=K W=t6;0 6;r!   