
    Vh                        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 ddl	m
Z
mZ g dZeZej                  eej"                  ej"                  ej$                        eeej"                  ej$                     f   f      Zej*                  eef   Z ed       G d	 d
e j.                               Z ed       G d de             Zej4                  ej*                  eej6                  j8                  f   ej:                  j<                  gef   Z  ed      de defd       Z! ed      dedefd       Z" ed      dedefd       Z# ed       G d d             Z$dej:                  j<                  dejJ                  fdZ&y)    N)compatibility   )TensorMetadata)CALLABLE_NODE_OPSget_node_target)OperatorSupportBaseOperatorSupportcreate_op_supportchain
OpSupports	any_chainF)is_backward_compatiblec                       e Zd ZdZej
                  dej                  ee	j                  j                  f   de	j                  j                  defd       Zy)r   z@Interface for determining if a fx.Node is supported by a backend
submodulesnodereturnc                     t         N)NotImplementedError)selfr   r   s      P/home/dcms/DCMS/lib/python3.12/site-packages/torch/fx/passes/operator_support.pyis_node_supportedz%OperatorSupportBase.is_node_supported(   s
     "!    N)__name__
__module____qualname____doc__abcabstractmethodtMappingstrtorchnnModulefxNodeboolr    r   r   r   r   $   sS    J"))C$89"AF"	" "r   r   c                       e Zd ZU dZeed<   d
dej                  e   fdZdej                  e
ej                  j                  f   dej                  j                  defd	Zy)r	   a  
    `_support_dict` maps node.target typename to supported inputs dtypes.

    node.target typename is retrieved using helper function `get_node_target()`

    If supported inputs dtypes is None, it means any dtype is supported, else
    we should see a tuple like (([dtypes], ...), {"name":[dtypes], ...}).

    The first tuple ([dtypes], ...) indicates what dtypes are supported for
    inputs in node.args and the second dict {"name": [dtypes], ...} indicates
    what dtypes are supported for inputs in node.kwargs.

    For inputs in args, if we don't want to check it, we can put None there,
    e.g. (None, [torch.float]) indicates that we don't care about the type of
    the first input in args. And for inputs in kwargs, if not listed, will not
    be checked.
    _support_dictNsupport_dictc                     |xs i | _         y r   )r+   )r   r,   s     r   __init__zOperatorSupport.__init__E   s    )/Rr   r   r   r   c                    |j                   t        vryt        ||      }|| j                  vry| j                  |   y| j                  |   \  }}t	        |      D ]r  \  }}t        |j                        |k  r nU|#t        |j                  |   t        j                  j                        sUt        |j                  |         }||vsr y |j                         D ]d  \  }	}|	|j                  vrt        |j                  |	   t        j                  j                        sGt        |j                  |	         }
|
|vsd y y)aO  
        Args:
            `submodules`: mapping from module name to the module. This can be
                          retrieved by calling model.named_modules().

            `node`: a Fx node that we want to determine whether it's supported.

        Returns:
            `is_supported`: whether the arg `node` is supported.
        TF)opr   r   r+   	enumeratelenargs
isinstancer#   r&   r'   _get_arg_dtypeitemskwargs)r   r   r   targetargs_dtypeskwargs_dtypesidtypes	arg_dtypekkwarg_dtypes              r   r   z!OperatorSupport.is_node_supportedH   s?    77++ T2 +++ f%-%)%7%7%?"] #;/ 	IAv499~" ~ diilEHHMM:&tyy|4I&	" ',,. 
	IAv# dkk!nehhmm<(Q8K&(
	 r   r   )r   r   r   r   SupportDict__annotations__r    Optionalr.   r!   r"   r#   r$   r%   r&   r'   r(   r   r)   r   r   r	   r	   /   s_    $ 0QZZ%< 0:))C$89:AF:	:r   r	   r   r   c                 4      G  fddt               } |       S )zWraps a `IsNodeSupported` function into an `OperatorSupportBase` instance

    `IsNodeSupported` has the same call signature as
    `OperatorSupportBase.is_node_supported`
    c                       e Zd Zdej                  eej                  j                  f   dej                  j                  def fdZy)4create_op_support.<locals>.FunctionalOperatorSupportr   r   r   c                      ||      S r   r)   )r   r   r   r   s      r   r   zFcreate_op_support.<locals>.FunctionalOperatorSupport.is_node_supported   s     %Z66r   N)r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r   r   s   r   FunctionalOperatorSupportrE      s>    	7iiUXX__(<=	7EJXX]]	7	7r   rH   )r   )r   rH   s   ` r   r
   r
      s    7$7 7 %&&r   
op_supportc                  2     dt         f fd}t        |      S )zCombines a sequence of `OperatorSupportBase` instances to form a single `OperatorSupportBase`
    instance by evaluating each input `OperatorSupportBase` instance, and returns False if
    any of it reports False.
    r   c                 2     t         fdD              S )Nc              3   B   K   | ]  }|j                          y wr   rG   .0xr   submodss     r   	<genexpr>z(chain.<locals>._chain.<locals>.<genexpr>        J!1&&w5J   )allrP   r   rI   s   ``r   _chainzchain.<locals>._chain       JzJJJr   r(   r
   )rI   rV   s   ` r   r   r      s    K K V$$r   c                  2     dt         f fd}t        |      S )zCombines a sequence of `OperatorSupportBase` instances to form a single `OperatorSupportBase`
    instance by evaluating each input `OperatorSupportBase` instance, and returns True if
    any of it reports True.
    r   c                 2     t         fdD              S )Nc              3   B   K   | ]  }|j                          y wr   rG   rM   s     r   rQ   z0any_chain.<locals>._any_chain.<locals>.<genexpr>   rR   rS   )anyrU   s   ``r   
_any_chainzany_chain.<locals>._any_chain   rW   r   rX   )rI   r]   s   ` r   r   r      s    KT K Z((r   c                   ^    e Zd ZdZedej                  defd       Zede	e
   defd       Zy)r   zA set of atomic `OperatorSupportBase` instances that can be combined together
    to form more complex operator support logic.
    dtyper   c                     dt         j                  t        t        j                  j
                  f   dt        j                  j                  dt        ffd}t        |      S )zCReport a node as non-supported, if any of its arguments is of dtyper   r   r   c                 L    |j                   D ]  }t        |      }|k(  s y y)NFT)all_input_nodesr5   )r   r   argr=   r_   s       r   _decline_if_input_dtypezBOpSupports.decline_if_input_dtype.<locals>._decline_if_input_dtype   s4     ++ !*3/	% ! r   
r    r!   r"   r#   r$   r%   r&   r'   r(   r
   )clsr_   rd   s    ` r   decline_if_input_dtypez!OpSupports.decline_if_input_dtype   sN    			#uxx"67	((--	 	 !!899r   disallow_setc                     dt         j                  t        t        j                  j
                  f   dt        j                  j                  dt        ffd}t        |      S )za
        If a node has a name that is in the disallow set, reported it as non-supported.
        r   r   r   c                      |j                   vS r   )name)r   r   rh   s     r   _decline_if_node_in_nameszFOpSupports.decline_if_node_in_names.<locals>._decline_if_node_in_names   s     99L00r   re   )rf   rh   rl   s    ` r   decline_if_node_in_namesz#OpSupports.decline_if_node_in_names   sN    	1		#uxx"67	1((--	1 	1 !!:;;r   N)r   r   r   r   classmethodr#   r_   r   rg   setr"   rm   r)   r   r   r   r      sU     :5;; :;N : : <CH <AT < <r   r   rc   c                     t        | t        j                  j                        sJ | j                  j                  d      }t        |t              r|j                  }|S | j                  d   }|S )Ntensor_metatype)r4   r#   r&   r'   metagetr   r_   )rc   rq   r_   s      r   r5   r5      si    c588==)))((,,}-K k>2 	 

 L XXf 

 Lr   )'r   typingr    r#   torch.fxtorch.fx._compatibilityr   
shape_propr   tools_commonr   r   __all__r"   TargetTypeNamerB   tupleSequencer_   dictSupportedArgumentDTypesr!   r@   ABCr   r	   Callabler$   r%   r&   r'   r(   IsNodeSupportedr
   r   r   r   Anyr5   r)   r   r   <module>r      s   
    1 & <  **		

1::ekk*+S!**U[[))*	,  ii(??@ e,"#'' " -" e,R) R -Rt **aiiUXX__(<=uxx}}MtST e,' '=P ' -'  e,	%* 	%/B 	% -	% e,	). 	)3F 	) -	) e,!< !< -!<H !%% r   