
    	i                         d dl Z d dlmZ d dlmZmZ d dlZd dlZd dlmZ d dlm	Z	m
Z
 d dlmZmZmZmZ d dlmZmZmZ ddlmZ  G d	 d
e
      Z G d de      Zy)    N)Callable)AnyOptional)ProxyTransformer)Argumentmap_aggregateNodeTarget)create_type_hintnormalize_functionnormalize_module   )AnnotateTypesWithSchemac                        e Zd ZdZ	 ddej
                  j                  def fdZde	de
f fdZ	 	 dded	eed
f   deee
f   deee
d
f      deeee
f      f
 fdZded	eed
f   deee
f   f fdZ xZS )NormalizeArgsa  
    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         |   |       i | _        || _        y N)super__init__node_mapr   )selfr   r   	__class__s      p/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/torch/fx/experimental/normalize.pyr   zNormalizeArgs.__init__&   s!     	 +-,H)    nreturnc                 \   | j                        \  }}fd}t        j                  |      }t        |t              st        dt        |             t	        d |D              }|j                         D ci c]  \  }}| ||       }}}j                  dk(  r | j                  j                  ||||      }	nt        
| 1        }	j                  dk7  rE| j                  |	<   j                  |	j                  _        j                  |	j                  _        |	S c c}}w )Nc                     t        | t        j                        rj                  j	                  d      S t        |       S )Ntype)
isinstancefxr
   metagetr"   )argr   s    r   get_typez(NormalizeArgs.run_node.<locals>.get_type0   s.    #rww'vvzz&))9r   zExpected tuple, got c              3   2   K   | ]  }t        |        y wr   )r   ).0is     r   	<genexpr>z)NormalizeArgs.run_node.<locals>.<genexpr>8   s     A!*1-As   call_functionoutput)fetch_args_kwargs_from_envr	   argsr#   tupleAssertionErrorr"   itemsopr-   targetr   run_noder   r%   node)r   r   r0   kwargsr(   	arg_typeskvkwarg_typesoutr   s    `        r   r6   zNormalizeArgs.run_node-   s    66q9f	
 "!&&(3	)U+ #7Y7H!IJJAyAA	28,,.A$!Qq(1+~AA44?"$$QXXtVYTC'"1%C448!"DMM#FFCHHMFFCHHM
 Bs   >D(r5   r0   .r8   r9   r<   c                     t        |      st        dt        |             t        |||||| j                        }|r#|\  }}| j
                  j                  d|||      S t        	| !  |||      S )NExpected callable target, got r-   )	callabler2   r"   r   r   tracercreate_proxyr   r-   )
r   r5   r0   r8   r9   r<   new_args_and_kwargsnew_args
new_kwargsr   s
            r   r-   zNormalizeArgs.call_functionD   s      #A$v,!PQQ0--
 #6 Hj;;++:  7(v>>r   c                     t        |t              st        dt        |             t	        | j
                  |||| j                        }|r|\  }}t        | !  |||      S t        | !  |||      S )NzExpected str target, got )	r#   strr2   r"   r   r   r   r   call_module)r   r5   r0   r8   rC   rD   rE   r   s          r   rH   zNormalizeArgs.call_module^   s     &#& #<T&\N!KLL.KK--
 #6 Hj7&vxDD7&vtV<<r   )T)NN)__name__
__module____qualname____doc__torchr$   GraphModuleboolr   r
   r   r6   r   r1   r   dictrG   r   r-   rH   __classcell__r   s   @r   r   r      s    $ RVIhh**IJNI$ 3 8 0404?? HcM"? S#X	?
 E#s(O,? d38n-?4==$)(C-$8=BFsCx.= =r   r   c                       e Zd ZU dZej
                  ej
                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                   ej                   ej"                  ej"                  ej$                  ej$                  ej&                  ej&                  ej(                  ej(                  iZeeeegef   eeegef   f   ed<   dedeedf   deeef   f fdZ xZS )NormalizeOperatorsa  
    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_remapr5   r0   .r8   c                    t        |      st        dt        |             || j                  v rEt	        |      dk7  rt
        |   |||      S |\  }}t
        |   | j                  |   ||fi       S t
        |   |||      S )Nr?      )r5   r0   r8   )r@   r2   r"   rU   lenr   r-   )r   r5   r0   r8   lhsrhsr   s         r   r-   z NormalizeOperators.call_function   s      #A$v,!PQQT3334yA~w,VT6BBHC7(55f=3Z )   w$VT6::r   ) rI   rJ   rK   rL   rM   addoperatormulsubdivtruedivfloor_dividefloordiv	remaindermodeqneltlegtgerU   rP   r   r   __annotations__r   r1   r   rG   r-   rQ   rR   s   @r   rT   rT   q   s   ( 			8<<		8<<		8<<		8##H--(++(++(++(++(++(++	 t#sS!8S#JO#<<  ";;$)(C-$8;BFsCx.; ;r   rT   )r\   collections.abcr   typingr   r   rM   torch.fxr$   r   r   torch.fx.noder   r	   r
   r   torch.fx.operator_schemasr   r   r   schema_type_annotationr   r   rT    r   r   <module>rs      sK     $      ' ? ?  <Z=K Z=z7;0 7;r   