
    a,j]                        U d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZmZmZ d dlZd dlmZ d dlmZmZ d dlmZ ddlmZ erdd	lmZ g d
Z ed           G d de                      Zi Zeedef         eej                 f         e d<   deej                 fdZ! e!            eej"        <    G d d          Z#ej$        ej%        ej&        ej'        ej(        j)        e j*        ej+         e#             e,d          ej-         ej.        d          edZ/ e0e          D ]Z1 e2ee1          e/e1<   dddefdZ3dej4        j5        dej        fdZ6i Z7ee8e9e9f         ej        f         e d<   dej4        j5        dej        fdZ: ed          dedef         de8d          d!ee9d"f         ddfd#            Z;ed$edef         d%e
d&         de8eej                 dz  eej4        j5                 dz  f         fd'            Z<e	 d8d$edef         d%e
d         deej                 dz  fd(            Z< ed          	 d9d$edef         d%e=deej                 e8eej                 dz  eej4        j5                 dz  f         z  dz  fd)            Z< ed          d*e>de>fd+            Z? ed          d,ed-ede=fd.            Z@ ed          	 	 	 	 d:dedef         de8edf         d!ee9ef         dz  d/e8e         dz  d0ee9ef         dz  d1e=defd2            ZA ed          	 	 	 	 d:dedef         de8edf         d!ee9ef         dz  d/e8e         dz  d0ee9ef         dz  d1e=dedz  fd3            ZB ed          	 	 d;d4ejC        jD        de9de8e         d!ee9ef         dz  d1e=dedz  fd5            ZEd6ej        de8edf         d!ee9ef         d1e=dedz  f
d7ZFdS )<    N)Callable)AnycastLiteral
NamedTupleoverloadTYPE_CHECKING)boolean_dispatched)
OpOverloadOpOverloadPacket)
_fast_bind   )compatibility)Argument)ArgsKwargsPaircheck_for_mutable_operationget_signature_for_torch_opcreate_type_hinttype_matchesnormalize_functionnormalize_moduleF)is_backward_compatiblec                   H    e Zd ZU dZeedf         ed<   eeef         ed<   dS )r   z<
    Simple named tuple for wrapping args/kwargs pairs.
    .argskwargsN)	__name__
__module____qualname____doc__tupler   __annotations__dictstr     Y/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/torch/fx/operator_schemas.pyr   r       sF           S/cNr%   r   ._manual_overridesreturnc                      g } dt           j        dd fd}|                     t          j        |                     dt           j        dt
          dd fd}|                     t          j        |                     | S )Nselfr(   c                     d S Nr$   )r*   s    r&   nonzeroz!_nonzero_schemas.<locals>.nonzero0       r%   as_tuplec                    d S r,   r$   )r*   r/   s     r&   r-   z!_nonzero_schemas.<locals>.nonzero5   r.   r%   )torchTensorappendinspect	signaturebool)
signaturesr-   s     r&   _nonzero_schemasr8   -   s    Jel t     g'00111el  $     g'00111r%   c                   (    e Zd Zdedej        fdZdS )_FakeGlobalNamespacenamer(   c                 :    |dk    rt           S t          d          )Nr1   z!Expected a torch namespace lookup)r1   RuntimeError)r*   r;   s     r&   __getattr__z _FakeGlobalNamespace.__getattr__A   s    7??L>???r%   N)r   r   r   r#   types
ModuleTyper>   r$   r%   r&   r:   r:   @   sA        @ @(8 @ @ @ @ @ @r%   r:   t)r2   DeviceLayoutnumberFutureAnyEnumTypeQScheme	__torch__NoneTypeStoragerA   PyObjectts_typeztorch._C.JitTypec                 6    t          | j        t                    S )z
    Convert a TorchScript type to a Python type (including subtypes) via
    eval'ing the annotation_str. _type_eval_globals sets up expressions
    like "List" and "Future" to map to actual types (typing.List and jit.Future)
    )evalannotation_str_type_eval_globals)rL   s    r&    _torchscript_type_to_python_typerQ   Y   s     &(:;;;r%   	ts_schemac           	      $   ddl m} g }| j        D ]}t          |j                  }|                                r|j        n|j        }|j        dk    r|j        nd}|j	        r|j
        n|j        }|dk    r||j        k    rt          d|           |j        }t          |          D ]Z\  }}	|	j        |j        k    rt          d|	j         d|	j                    ||	j        |j        |	j        |	j        	          ||<   [|                     |||||	                     d
 | j        D             }
t)          |
          dk    rd }n+t)          |
          dk    r	|
d         }nt+          |
          }t          j        ||          S )Nr   )	Parameterr*   inputfromz$Expected POSITIONAL_OR_KEYWORD, got z)Expected POSITIONAL_OR_KEYWORD for param z, got )r;   kinddefault
annotationc                 6    g | ]}t          |j                  S r$   )rQ   type).0rets     r&   
<listcomp>z9_torchscript_schema_to_signature_impl.<locals>.<listcomp>   s0       7:(22  r%   r   )return_annotation)r4   rT   	argumentsrQ   r[   has_default_valuedefault_valueemptyr;   
kwarg_onlyKEYWORD_ONLYPOSITIONAL_OR_KEYWORDAssertionErrorPOSITIONAL_ONLY	enumeraterW   rX   rY   r3   returnslenr    	Signature)rR   rT   
parametersargarg_typerX   r;   rW   idxpreturn_typesreturn_types               r&   %_torchscript_schema_to_signature_implrt   b   s    "!!!!!"$J" %
 %
3CH=='*'<'<'>'>S###IO 8v--sxx7 ~1I""0 	 6>>y666$%RD%R%RSSS ,D $J// 
 
Q6Y<<<(ZAFZZRSRXZZ   #,)"2I |	# # #
3 	I4dGQQQ	
 	
 	
 	
 >G>O  L <A	\		a		"1oL))Z;GGGGr%   _SCHEMA_TO_SIGNATURE_CACHEc                     | j         | j        f}t                              |          }||S t	          |           }|t          |<   |S r,   )r;   overload_nameru   getrt   )rR   	cache_key	cache_valress       r&    _torchscript_schema_to_signaturer|      sO     	 77I*..y99I
/	
:
:C,/y)Jr%   targetr   )r   .r   r   c                    t          | d          \  }}|r|rg }t          ||          D ]=\  }}	 t          |g|R i | |                    ||f           .# t          $ r Y :w xY wdt
          j        j        dd fd}t          |          dk    rd S t          |          dk    r|d         \  }	}
 ||
           d S d S d S d S )NT)return_schemasschemar(   c                 :    | j         rt          d|  d          d S )Nz!Tried to trace mutable operation z|. FX only supports functional code, so operations that mutate operands in-place (e.g. via `out` arguments) are not supported)
is_mutabler=   )r   s    r&   throw_if_mutablez5check_for_mutable_operation.<locals>.throw_if_mutable   s?      ") ) ) )   r%   r   r   )	r   zipr   r3   	TypeErrorr1   _CFunctionSchemark   )r}   r   r   r7   schemasmatched_schemascandidate_signaturer   r   _schema_to_checks              r&   r   r      sF    5VDQQQJ g SU
 ,/z7+C+C 	 	'.@@@@@@@&&(;V'DEEEE   	UX%< 	 	 	 	 	 1$$D!!Q&&!0!3A_----- D?   s   'A
A&%A&opr   Tc                     d S r,   r$   r   r   s     r&   r   r      s     SVRUr%   c                     d S r,   r$   r   s     r&   r   r      s	     &)Sr%   c                     t           t                    r	 j        g}nt           t                    r! fd                                 D             }not
                                         }|r|r|dfndS t          j        j	        
                               }||rdndS t          j                            |          }d |D             }|r||fn|S )a  
    Given an operator on the `torch` namespace, return a list of `inspect.Signature`
    objects corresponding to the overloads of that op.. May return `None` if a signature
    could not be retrieved.

    Args:
        op (Callable): An operator on the `torch` namespace to look up a signature for

    Returns:
        Optional[List[inspect.Signature]]: A list of signatures for the overloads of this
            operator, or None if the operator signatures could not be retrieved. If
            return_schemas=True, returns a tuple containing the optional Python signatures
            and the optional TorchScript Function signature
    c                 :    g | ]}t          |          j        S r$   )getattr_schema)r\   r   r   s     r&   r^   z.get_signature_for_torch_op.<locals>.<listcomp>   s&    PPPX72x((0PPPr%   N)NNc                 ,    g | ]}t          |          S r$   )r|   r\   r   s     r&   r^   z.get_signature_for_torch_op.<locals>.<listcomp>  s!    QQQv26::QQQr%   )
isinstancer   r   r   	overloadsr'   rx   r1   jit	_builtins_find_builtinr   _jit_get_schemas_for_operator)r   r   r   overrideaten_fnr7   s   `     r&   r   r      s    , "j!! B:,	B(	)	) BPPPPPPP$((,, 	@'5?Hd##4?)%33B77?#1;<<t;(88AAQQQQQJ$2BJ  
Br%   xc                    	 t          | t          t          f          rt          | t                    rdt          dt          fd}ndt          dt          fd}t	          |           dk    r |t                    S | d         }| D ]7}t          ||          rt          ||          r|}& |t                    c S  ||          S n'# t          $ r t          j        d|             Y nw xY w| S )a  
    Produces a type hint for the given argument.

    The :func:`create_type_hint` looks for a type hint compatible with the input argument `x`.

    If `x` is a `list` or `tuple`, it looks for an object in the list whose type is a superclass
    of the rest, and uses that as `base_type` for the `List` or `Tuple` to be returned.
    If no such object is found, it defaults to `List[Any]`.

    If `x` is neither a `list` nor a `tuple`, it returns `x`.
    r   r(   c                     t           |          S r,   )listr   s    r&   ret_typez"create_type_hint.<locals>.ret_type  s    7Nr%   c                      t           | df         S )N.)r    r   s    r&   r   z"create_type_hint.<locals>.ret_type"  s     C=(r%   r   z@We were not able to successfully create type hint from the type )	r   r   r    r   rk   
issubclass	Exceptionwarningswarn)r   r   	base_typerA   s       r&   r   r     sS   
a$'' 	'!T"" )# # # # # # #
) ) ) ) ) ) 1vv{{x}}$!I ) )a++ )	1-- ) !II#8C==(((8I&&&-	'.  
 
 
RqRR	
 	
 	
 	
 	


 Hs   A8C	 ;AC	 =
C	 	!C-,C-signature_typeargument_typec                    t          | d|           }| u rdS |t          j        u r(| k    r"| j        }t	          fd|D                       S t          | dd           t
          u r| j        d         t          u rt          u rdS t          j                  st          j
        d|  d           dS t          dd           t
          u rt          j        d                   S dt          d	t          ffd
} |          S | t          u rt          j        u rdS | t           j        u rt          t$          hv rdS t          j                  r$t          j        |           rt          |           S dS )N
__origin__Tc              3   8   K   | ]}t          |          V  d S r,   )r   )r\   cr   s     r&   	<genexpr>ztype_matches.<locals>.<genexpr>C  s-      IIa<=11IIIIIIr%   r   z.Does not support nested parametric types, got z. Please file a bug.FrA   r(   c                     t          j        |           t          urdS t          j        |           }|dk    rdS t	          fd|D                       S )NF)r$   Tc              3   J   K   | ]}|t           u pt          |          V  d S r,   )Ellipsisr   )r\   r   sig_el_types     r&   r   z=type_matches.<locals>.is_homogeneous_tuple.<locals>.<genexpr>Z  s6      XXXD*Q*D*DXXXXXXr%   )typing
get_originr    get_argsall)rA   	containedr   s     r&   is_homogeneous_tuplez*type_matches.<locals>.is_homogeneous_tupleT  s`     ##500u**IE!!tXXXXiXXXXXXr%   )r   r   Union__args__anyr   intr4   isclassr   r   r   objectr6   r1   dtypenumbersNumberfloat)r   r   sig_origin_typesig_containedr   r   s    `   @r&   r   r   8  s   nlNKKO&&t &,&&>]+J+J&/IIII=IIIIII~|T22d::$-a0 CK3$6$64{++ 	Meeee   5=,55==m4Q7EEE	YF 	Yt 	Y 	Y 	Y 	Y 	Y 	Y $#M222 %+!=!=t''Mc5\,I,It}%% 9'/.*I*I 9-8885r%   	arg_typeskwarg_typesnormalize_to_only_use_kwargsc                 b    t          | |||||          }|t          d|  d| d|           |S )zb
    Wrapper around normalize_function that never returns None, but
    loudly errors instead
    NzFailed to normalize function z with args z and kwargs )r   r=   )r}   r   r   r   r   r   r{   s          r&   _normalize_function_or_errorr   k  s]     fi6R C {YFYYtYYQWYY
 
 	
 
r%   c                 `   |i }d}t          | t          j                  s3t          | t          t          f          st          | d          r| j        } t          | t          j                  st          | t          t          f          s| }| t          v ryt          | t                    rt          d          t          |          }|d         |d         }
}	t          j        |	          j        t          j        |
          j        k    rdS |	}t          |          st          dt          |                     t          j        t          j        |                    }t!          ||||          }nt          |           st          dt          |                      t#          |           }g }|ru|D ]8}	 t%          |g|R i | |                    |           )# t(          $ r Y 5w xY wt+          |          dk    rn%t+          |          d	k    rt!          |d         |||          }n|||r|nt-          t.          t0                   d
          }|r|ni }|D ]}d}	 t%          |g|R i |}|j                                        D ])\  }}|j        |         }|ot7          |j        |          }*n# t(          $ r d}Y nw xY w|rt!          ||||          } nnFd                    d |D                       }t=          dt?          j         |            d|           |S )a  
    Returns normalized arguments to PyTorch functions. This means that
    `args/kwargs` will be matched up to the 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 (*args, **kwargs). Does not support modules.

    May require `arg_types` and `kwarg_types` in order to disambiguate overloads.

    Args:
        target (Callable): Function that we are normalizing
        args (Tuple[Any]): Tuple of args to the function
        kwargs (Optional[Dict[str, Any]]): Dict of kwargs to the function
        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 normalized_args_and_kwargs, or `None` if not successful.
    N_opz"target should not be a string hereif_trueif_falsez*target_for_analysis must be callable, got ztarget must be callable, got r   r   r$   TF
c              3   4   K   | ]}t          |          V  d S r,   )r#   r   s     r&   r   z%normalize_function.<locals>.<genexpr>  s9       1 1(.F1 1 1 1 1 1r%   z Tried to normalize arguments to zy but the schema match was ambiguous! Please provide argument types to the normalize_arguments() call. Available schemas:
)!r   r?   BuiltinFunctionTyper   r   hasattrr   r
   r#   rg   r4   r5   rm   callabler[   unwrap&_args_kwargs_to_normalized_args_kwargsr   r   r3   r   rk   r   r    r   r`   itemsr   rY   joinr=   r1   typename)r}   r   r   r   r   r   new_args_and_kwargstarget_for_analysis
dispatchedr   r   sigtorch_op_schemasr   r   sig_matchesbound_typesarg_namero   paramschema_printoutss                        r&   r   r     s=   > ~vu899	F%5z$BCC	 FE""	  fe788 Y6,j9::Y %'''
 &#&& K$%IJJJ+F3J *9 5z*7MXG!'**5$X..9: : t")+,, 	 XTBU=V=VXX   /B C CDDDv;
 
  	Q !Of!O!OPPP5f==35 8	 (8  #2DTDDDVDDD#**+>????    H ?##q((_%%**&L#A&f6R' '## (K,C-6 P		DsR<P<PI1<"D++"K/? " "+&*
0*4 3+6?+ + +CN+ +K 7B6K6Q6Q6S6S " " 2((;(Fx(P.9 /"l$)$4h?" ?""
  ) 0 0 0*/KKK0& 	" F$7$($*$@	!" !" 0 "E	" (,yy 1 12A1 1 1 ( ($ 'b5>&;Q;Q b bO_b b   s%   >%G$$
G10G1*AJ>>KKrootc                    	 |                      |          }n&# t          $ r}t          d| d          |d}~ww xY wt          |j        d          rs|j        j        }t          t          j        |d          |j        k    rCt          j
        t          j        |j                            }|i }t          ||||          }	|	S dS )a(  
    Returns normalized arguments to PyTorch modules. This means that
    `args/kwargs` will be matched up to the 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 (*args, **kwargs).

    Args:
        root (nn.Module): root module upon which we query modules
        target (Callable): Function that we are normalizing
        args (Tuple[Any]): Tuple of args to the function
        kwargs (Optional[Dict[str, Any]]): Dict of kwargs to the function
        normalize_to_only_use_kwargs (bool): Whether to normalize to only use kwargs.

    Returns:

        Returns normalized_args_and_kwargs, or `None` if not successful.
    z$Tried to normalize node with target z# but root did not have that target!Nr   )get_submoduleAttributeErrorr=   r   	__class__r   r   r1   nnr4   r5   r   forwardr   )
r   r}   r   r   r   submode	classnamer   r   s
             r&   r   r     s    6##F++   !6 ! ! !
 
 	
 v,, 	'$-	58Y--1AAA#GN6>$B$BCCC~"HT6#?# # '&4s    
;6;r   c                 R  	 t           j        j        t           j        j        h	t	          	fd| j                                        D                       r.t          | j                                                  g dk    rdS t          | g|R i |}|
                                 i }g }t          | j                  D ]K\  }}|s4|t          |          k     r!|                    |j        |                    ;|j        |         ||<   Lt          t!          |          |          S )a  
    Given a call target, args, and kwargs, return the arguments normalized into
    an ArgsKwargsPair, or None if the type signature is not supported by
    this normalization.

    Args:

        sig (inspect.Signature): Signature object for the target
        args (Tuple): Arguments that appear at the callsite for `target`
        kwargs (Dict): Keyword arguments that appear at the callsite for `target`
        normalize_to_only_use_kwargs (bool): Whether to normalize to only use kwargs.

    Returns:

        Optional[ArgsKwargsPair]: Normalized args and kwargs for `target`, or `None` if
            this target is not supported.
    c              3   *   K   | ]}|j         vV  d S r,   )rW   )r\   rq   supported_parameter_typess     r&   r   z9_args_kwargs_to_normalized_args_kwargs.<locals>.<genexpr>\  s+      
T
Tq1622
T
T
T
T
T
Tr%   )rU   rV   to	generatorN)r4   rT   rf   re   r   rm   valuesr   keysr   apply_defaultsri   rk   r3   r`   r   r    )
r   r   r   r   
bound_args
new_kwargsnew_argsir   r   s
            @r&   r   r   >  s7   6 	/&! 
T
T
T
TCN<Q<Q<S<S
T
T
TTT 
 ##%%&&*N*N*NNN4C1$111&11J!#JHcn-- < <5+ 	<CIIOOJ078888 * 4U ;Ju%//:666r%   ).)F)NNNF)NF)Genumr4   r   r?   r   r   collections.abcr   r   r   r   r   r   r	   r1   torch._jit_internalr
   
torch._opsr   r   torch.utils._inspectr   _compatibilityr   noder   __all__r   r'   r"   r   rl   r!   r8   r-   r:   r2   devicelayoutr   r   rE   Enumqschemer[   UntypedStorageTypeVarrP   dirkr   rQ   r   r   rt   ru   r    r#   r|   r   r   r6   r   r   r   r   r   r   Moduler   r   r$   r%   r&   <module>r     s         $ $ $ $ $ $ J J J J J J J J J J J J J J J J  2 2 2 2 2 2 3 3 3 3 3 3 3 3 + + + + + + ) ) ) ) ) )     e,,,    Z   -, HJ 4c*D1B,CCD I I I$w01      $4#3#5#5 %-  @ @ @ @ @ @ @ @ lllni9}%%''T

#			   
V / /A#GFA..q<.@ <S < < < <6Hx&6H6H 6H 6H 6Hr HJ DsCx'2C!CD I I Ix&    e,,,&S#X&

 & j!& 
	& & & -,&R 
VcV,3DMV
4!"T)40G+H4+OOPV V V 
V
 
=@) )c),3EN)	'
t#) ) ) 
)
 e,,,38%C %Cc%C,0%C 		D"#d*D1H,ID,PPQR
%C %C %C -,%CP e,,,) )6 ) ) ) -,)X e,,,/ /S /T / / / -,/d e,,, %)#')-). S#X
S/ cNT! SzD 	
 c3h$& #'    -,. e,,, %)#')-).H HS#XH
S/H cNT!H SzD 	H
 c3h$&H #'H dH H H -,HV e,,,
 %)).+ +
(/++ *+ cNT!	+
 #'+ d+ + + -,+\17		17
S/17 cN17 #'	17
 d17 17 17 17 17 17r%   