
    a,j                         d dl Z d dlmZ d dl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dS )    N)Callable)Any)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
df         d	z  deee
f         d	z  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()
    Tmodulenormalize_to_only_use_kwargsc                 f    t                                          |           i | _        || _        d S N)super__init__node_mapr   )selfr   r   	__class__s      _/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/torch/fx/experimental/normalize.pyr   zNormalizeArgs.__init__&   s2     	   +-,H)))    nreturnc                 N   |                                \  }}fdt          j                  }t          |t                    st          dt          |                     t	          d |D                       }fd|                                D             }j        dk    r| 	                    j
        ||||          }n!t                                                    }j        dk    r,| j        |<   j        |j        _        j        |j        _        |S )Nc                     t          | t          j                  rj                            d          S t          |           S )Ntype)
isinstancefxr	   metagetr!   )argr   s    r   get_typez(NormalizeArgs.run_node.<locals>.get_type0   s7    #rw'' *vzz&)))99r   zExpected tuple, got c              3   4   K   | ]}t          |          V  d S r   )r   ).0is     r   	<genexpr>z)NormalizeArgs.run_node.<locals>.<genexpr>8   s+      AA!*1--AAAAAAr   c                 .    i | ]\  }}| |          S  r-   )r)   kvr'   s      r   
<dictcomp>z*NormalizeArgs.run_node.<locals>.<dictcomp>9   s'    AAA$!Qq((1++AAAr   call_functionoutput)fetch_args_kwargs_from_envr   argsr"   tupleAssertionErrorr!   itemsopr1   targetr   run_noder   r$   node)	r   r   r4   kwargs	arg_typeskwarg_typesoutr'   r   s	    `     @r   r:   zNormalizeArgs.run_node-   s,   66q99f	 	 	 	 	
 "!&(33	)U++ 	K !IY!I!IJJJAAyAAAAA	AAAA&,,..AAA4?""$$QXtVYTTCC''""1%%C48!"DM#FCHMFCHM
r   Nr9   r4   .r<   r=   r>   c                     t          |          st          dt          |                     t          |||||| j                  }|r"|\  }}| j                            d|||          S t                                          |||          S )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   zNormalizeArgs.call_functionD   s      	R !P$v,,!P!PQQQ0-
 
  	?#6 Hj;++:   77((v>>>r   c                 @   t          |t                    st          dt          |                     t	          | j        |||| j                  }|r(|\  }}t                                          |||          S t                                          |||          S )NzExpected str target, got )	r"   strr6   r!   r   r   r   r   call_module)r   r9   r4   r<   rE   rF   rG   r   s          r   rJ   zNormalizeArgs.call_module^   s     &#&& 	M !KT&\\!K!KLLL.K-
 
  	=#6 Hj77&&vxDDD77&&vtV<<<r   )T)NN)__name__
__module____qualname____doc__torchr#   GraphModuleboolr   r	   r   r:   r
   r5   r   dictrI   r1   rJ   __classcell__r   s   @r   r   r      sj        $ RVI Ih*IJNI I I I I I$ 3      8 -1-1? ?? HcM"? S#X	?
 c?T)? #s(^d*? ? ? ? ? ?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_remapr9   r4   .r<   c                    t          |          st          dt          |                     || j        v rlt	          |          dk    r#t                                          |||          S |\  }}t                                          | j        |         ||fi           S t                                          |||          S )NrA      )r9   r4   r<   )rB   r6   r!   rW   lenr   r1   )r   r9   r4   r<   lhsrhsr   s         r   r1   z NormalizeOperators.call_function   s      	R !P$v,,!P!PQQQT3334yyA~~ww,,VT6BBBHC77((5f=3Z )    ww$$VT6:::r   ) rK   rL   rM   rN   rO   addoperatormulsubdivtruedivfloor_dividefloordiv	remaindermodeqneltlegtgerW   rR   r   r   __annotations__r
   r5   r   rI   r1   rS   rT   s   @r   rV   rV   q   s%         ( 		8<	8<	8<	8#H-(+(+(+(+(+(+	 t#sS!8S#JO#<<    ";;$)(C-$8;BFsCx.; ; ; ; ; ; ; ; ; ;r   rV   )r^   collections.abcr   typingr   rO   torch.fxr#   r   r   torch.fx.noder   r   r	   r
   torch.fx.operator_schemasr   r   r   schema_type_annotationr   r   rV   r-   r   r   <module>rt      s@    $ $ $ $ $ $               ' ' ' ' ' ' ' ' ? ? ? ? ? ? ? ? ? ? ? ?          < ; ; ; ; ;Z= Z= Z= Z= Z=K Z= Z= Z=z7; 7; 7; 7; 7;0 7; 7; 7; 7; 7;r   