
    Phq                     >   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j                   ej"                  ej"                  ej$                        ej&                  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)get_node_targetCALLABLE_NODE_OPS)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      kC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\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__;   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_supportedA   s5    77++ T2 +++ f%-%)%7%7%?"] #;/IAv499~" ~ diilEHHMM:&tyy|4I& 0" ',,.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	   %   se    $  150jj-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"#))C,@"A	7IN	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>   &      
 .   )allrP   r   rI   s   ``r   _chainzchain.<locals>._chain        

 
 	
r   r(   r
   )rI   rV   s   ` r   r   r      s    
 

 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    
T 

 Z((r   c                   r    e Zd ZdZedej                  defd       Zede	j                  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 ]$  }|j                  dk(  rt        |      }|k(  s$ y y)Nget_attrFT)all_input_nodesr0   r5   )r   r   argr=   r_   s       r   _decline_if_input_dtypezBOpSupports.decline_if_input_dtype.<locals>._decline_if_input_dtype   s>     ++66Z'*3/	%  , r   
r    r!   r"   r#   r$   r%   r&   r'   r(   r
   )clsr_   re   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 ryy)NFT)name)r   r   ri   s     r   _decline_if_node_in_nameszFOpSupports.decline_if_node_in_names.<locals>._decline_if_node_in_names   s     yyL(r   rf   )rg   ri   rm   s    ` r   decline_if_node_in_namesz#OpSupports.decline_if_node_in_names   sN    
			#uxx"67	((--	 	 !!:;;r   N)r   r   r   r   classmethodr#   r_   r   rh   r    Setr"   rn   r)   r   r   r   r      sY     :5;; :;N : :" <AEE#J <CV < <r   r   rd   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_   )rd   rr   r_   s      r   r5   r5      s]    c588==)))((,,}-K!+K!HKEL ORhhW]N^E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 & < n  **GG	

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