
    ڧgf                     ,   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	 d dl
mZmZmZmZmZmZmZ d dlZd dlZd dlmZmZ d dlmZ dd	gZ G d
 dej                  Z G d de          Zd ZdedefdZdee         fdZdeee ef                  dee ef         fdZ!	 	 	 d"dej"        deee ef                  de#deee ef                  deee          ee          f         f
dZ$ G d dej%                  Z&	 	 	 	 	 	 d#dej"        deeee          ee e f         f                  deeee          ee e f         f                  d eeee          ee e f         f                  deee ef                  de#deee ef                  dej%        fd!Z'dS )$    N)OrderedDict)deepcopy)chain)AnyCallableDictListOptionalTupleUnion)fxnn)
_copy_attrcreate_feature_extractorget_graph_node_namesc                   D     e Zd ZdZ fdZdej        dedef fdZ	 xZ
S )LeafModuleAwareTracerz
    An fx.Tracer that allows the user to specify a set of leaf modules, i.e.
    modules that are not to be traced through. The resulting graph ends up
    having single nodes referencing calls to the leaf modules' forward methods.
    c                     i | _         d|v r|                    d          }|| _          t                      j        |i | d S )Nleaf_modules)r   popsuper__init__)selfargskwargsr   	__class__s       a/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/models/feature_extraction.pyr   zLeafModuleAwareTracer.__init__   sQ    V##!::n55L ,D$)&)))))    mmodule_qualnamereturnc                     t          |t          | j                            rdS t                                          ||          S )NT)
isinstancetupler   r   is_leaf_module)r   r   r    r   s      r   r%   z$LeafModuleAwareTracer.is_leaf_module!   s?    at01122 	4ww%%a999r   )__name__
__module____qualname____doc__r   r   Modulestrboolr%   __classcell__r   s   @r   r   r      sw         * * * * *:	 :C :D : : : : : : : : : :r   r   c                        e Zd ZdZ fdZdej        j        defdZ		 dde
dej        j        d	ej        j        f fd
Zde
dej        j        d	e
fdZ xZS )NodePathTracerah  
    NodePathTracer is an FX tracer that, for each operation, also records the
    name of the Node from which the operation originated. A node name here is
    a `.` separated path walking the hierarchy from top level module down to
    leaf operation or leaf module. The name of the top level module is not
    included as part of the node name. For example, if we trace a module whose
    forward method applies a ReLU module, the name for that node will simply
    be 'relu'.

    Some notes on the specifics:
        - Nodes are recorded to `self.node_to_qualname` which is a dictionary
          mapping a given Node object to its node name.
        - Nodes are recorded in the order which they are executed during
          tracing.
        - When a duplicate node name is encountered, a suffix of the form
          _{int} is added. The counter starts from 1.
    c                 n     t                      j        |i | d| _        t                      | _        d S )N )r   r   current_module_qualnamer   node_to_qualname)r   r   r   r   s      r   r   zNodePathTracer.__init__:   s;    $)&)))')$
 !,r   r   forwardc                     | j         }	 |                     |          }|| _         |                     ||          s ||i |}||| _         S |                     d|||          || _         S # || _         w xY w)a  
        Override of `fx.Tracer.call_module`
        This override:
        1) Stores away the qualified name of the caller for restoration later
        2) Adds the qualified name of the caller to
           `current_module_qualname` for retrieval by `create_proxy`
        3) Once a leaf module is reached, calls `create_proxy`
        4) Restores the caller's qualified name into current_module_qualname
        call_module)r3   path_of_moduler%   create_proxy)r   r   r5   r   r   old_qualnamer    outs           r   r7   zNodePathTracer.call_moduleD   s     3	8"11!44O+:D(&&q/:: gt.v.. ,8D(( $$]OT6RR+7D((<D(7777s   ;A+ A+ +	A4Nkindtargetr!   c                     t                                          ||||||          }|                     | j        |j                  | j        |j        <   |S )z
        Override of `Tracer.create_proxy`. This override intercepts the recording
        of every operation and stores away the current traced module's qualified
        name in `node_to_qualname`
        )r   r9   _get_node_qualnamer3   noder4   )
r   r<   r=   r   r   name	type_expr_proxyr   s
            r   r9   zNodePathTracer.create_proxyY   sQ     $$T64yQQ,0,C,CDD`bgbl,m,mej)r   r    r@   c                    |}|j         dk    r*t          |          dk    r|dz  }|t          |          z  }t          j        d|          |                    dd          d         }t          | j                                                  D ]f}t          j        | d|          L|	                    |d          }t          |          rt          |dd                    dz   }nd}|d| z  } ng|S )	Nr7   r   .z
.+_[0-9]+$rC      z(_[0-9]+)?$r2   )oplenr+   rematchrsplitreversedr4   valuesreplaceint)r   r    r@   node_qualnameexisting_qualnamepostfix
next_indexs          r   r?   z!NodePathTracer._get_node_qualnamee   s$   '7m## =!!A%%$SYY&M 8M=11=)00a88;M "*$*?*F*F*H*H!I!I 	 	 xM6668IJJV+33M2FFw<< #!$WQRR[!1!1A!5JJ "#J!1Z!1!11 W r   )NN)r&   r'   r(   r)   r   torchr   r*   r   r7   r+   r   r@   TargetrD   Proxyr9   Noder?   r-   r.   s   @r   r0   r0   '   s         $. . . . .8UX_ 8x 8 8 8 8, UY
 

!#
	
 
 
 
 
 
"# "RW\ "c " " " " " " " "r   r0   c                 X    t          |           t          fd|D                       S )zVCheck if y is a subsequence of x
    https://stackoverflow.com/a/24017747/4391249
    c              3   P   K   | ]t          fd D                       V   dS )c              3   $   K   | ]
}|k    V  d S N ).0x_itemy_items     r   	<genexpr>z'_is_subseq.<locals>.<genexpr>.<genexpr>   s(      996V#999999r   N)any)r^   r`   iter_xs    @r   ra   z_is_subseq.<locals>.<genexpr>   sA      JJfs9999&99999JJJJJJr   )iterall)xyrc   s     @r   
_is_subseqrh      s3     !WWFJJJJJJJJJJr   train_tracereval_tracerc                    t          | j                                                  }t          |j                                                  }t          |          t          |          k    r)t	          d t          ||          D                       rdS d}t          ||          rd}nt          ||          rd}nd}t          j        ||z              dS )z
    Utility function for warning the user if there are differences between
    the train graph nodes and the eval graph nodes.
    c              3   (   K   | ]\  }}||k    V  d S r\   r]   )r^   tes      r   ra   z*_warn_graph_differences.<locals>.<genexpr>   s*      2c2cda162c2c2c2c2c2cr   NztWhen choosing nodes for feature extraction, you may need to specify output nodes for train and eval mode separately.znNOTE: The nodes obtained by tracing the model in eval mode are a subsequence of those obtained in train mode. znNOTE: The nodes obtained by tracing the model in train mode are a subsequence of those obtained in eval mode. zdThe nodes obtained by tracing the model in train mode are different to those obtained in eval mode. )	listr4   rN   rI   re   ziprh   warningswarn)ri   rj   train_nodes
eval_nodessuggestion_msgmsgs         r   _warn_graph_differencesrw      s    
 |4;;==>>Kk299;;<<J
;3z??**s2c2cc+WaFbFb2c2c2c/c/c*	; 
 +z** uB 	 
J	,	, uA 	
 uM#&'''''r   r!   c                      t          j        t          j                  } g }| D ]M\  }}t          j        |          r4t          |t          j        j                  r|	                    |           N|S r\   )
inspect
getmemberstorchvisionopsisclass
issubclassrU   r   r*   append)membersresultrC   objs       r   _get_leaf_modules_for_opsr      sk     11GF  3?3 	JsEHO$D$D 	MM#Mr   original_tr_kwargsc                    t           t          j        f}t                      }| i n| }d|v r%t	          t          |d         |z                       n||d<   d|v r%t          t          |d         |z                       n||d<   |S )Nautowrap_modulesr   )mathr{   r|   r   r$   setro   )r   default_autowrap_modulesdefault_leaf_modulesresult_tracer_kwargss       r   _set_default_tracer_kwargsr      s     $ko6466!3!;22AS !555 	c&'9:=UUVVWWW% +, 111 	S%n58LLMMNNN! (
  r   Fmodeltracer_kwargssuppress_diff_warningconcrete_argsc                    t          |          }| j        }t          di |}|                    |                                 |           t          di |}|                    |                                 |           t          |j                                                  }t          |j                                                  }|st          ||           |                     |           ||fS )a	  
    Dev utility to return node names in order of execution. See note on node
    names under :func:`create_feature_extractor`. Useful for seeing which node
    names are available for feature extraction. There are two reasons that
    node names can't easily be read directly from the code for a model:

        1. Not all submodules are traced through. Modules from ``torch.nn`` all
           fall within this category.
        2. Nodes representing the repeated application of the same operation
           or leaf module get a ``_{counter}`` postfix.

    The model is traced twice: once in train mode, and once in eval mode. Both
    sets of node names are returned.

    For more details on the node naming conventions used here, please see the
    :ref:`relevant subheading <about-node-names>` in the
    `documentation <https://pytorch.org/vision/stable/feature_extraction.html>`_.

    Args:
        model (nn.Module): model for which we'd like to print node names
        tracer_kwargs (dict, optional): a dictionary of keyword arguments for
            ``NodePathTracer`` (they are eventually passed onto
            `torch.fx.Tracer <https://pytorch.org/docs/stable/fx.html#torch.fx.Tracer>`_).
            By default, it will be set to wrap and make leaf nodes all torchvision ops:
            {"autowrap_modules": (math, torchvision.ops,),"leaf_modules": _get_leaf_modules_for_ops(),}
            WARNING: In case the user provides tracer_kwargs, above default arguments will be appended to the user
            provided dictionary.
        suppress_diff_warning (bool, optional): whether to suppress a warning
            when there are discrepancies between the train and eval version of
            the graph. Defaults to False.
        concrete_args (Optional[Dict[str, any]]): Concrete arguments that should
            not be treated as Proxies. According to the `Pytorch docs
            <https://pytorch.org/docs/stable/fx.html#torch.fx.Tracer.trace>`_,
            this parameter's API may not be guaranteed.

    Returns:
        tuple(list, list): a list of node names from tracing the model in
        train mode, and another from tracing the model in eval mode.

    Examples::

        >>> model = torchvision.models.resnet18()
        >>> train_nodes, eval_nodes = get_graph_node_names(model)
    r   r]   )
r   trainingr0   tracetrainevalro   r4   rN   rw   )	r   r   r   r   is_trainingri   rj   rs   rt   s	            r   r   r      s    d /}==M.K!22M22Lu{{}}MBBB 11=11Kejjll-@@@|4;;==>>Kk299;;<<J  ;k:::	KK
""r   c            	       l     e Zd ZdZ	 d
dej        j        dej        dej        de	f fdZ
d fd		Z xZS )DualGraphModulea  
    A derivative of `fx.GraphModule`. Differs in the following ways:
    - Requires a train and eval version of the underlying graph
    - Copies submodules according to the nodes of both train and eval graphs.
    - Calling train(mode) switches between train graph and eval graph.
    GraphModuleroottrain_graph
eval_graph
class_namec                    t          t          j        |                                            || j        _        || _        || _        t          t          |j
                  t          |j
                            D ]_}|j        dv rTt          |j        t                    s$t          dt!          |j                             t#          || |j                   `|                                  || _        | j        j        | j        j        k    r*t          d| j        j         d| j        j         d          d| _        | j        j        r&d| j        j        j        vr| j        j        | _        dS dS dS )a  
        Args:
            root (nn.Module): module from which the copied module hierarchy is
                built
            train_graph (fx.Graph): the graph that should be used in train mode
            eval_graph (fx.Graph): the graph that should be used in eval mode
        )get_attrr7   z-node.target should be of type str instead of zGTrain mode and eval mode should use the same tracer class. Instead got z for eval vs z
 for trainNz<locals>)r   r   r   r   r   r&   r   r   r   rd   nodesrH   r#   r=   r+   	TypeErrortyper   r   graph_tracer_clsr(   )r   r   r   r   r   r@   r   s         r   r   zDualGraphModule.__init__  s    	bnd##,,...",&$ ${0114
8H3I3IJJ 	4 	4Dw555!$+s33 i#$gTXY]YdTeTe$g$ghhh4t{333 	

 
 ?&$*:*FFF mZ^ZiZu  m  m  EI  EU  Ea  m  m  m    :! 	6j
8N8[&[&[#z5D	6 	6&[&[r   Tc                     |r| j         s| j        | _        n|s| j         r| j        | _        t	                                          |          S )z
        Swap out the graph depending on the selected training mode.
        NOTE this should be safe when calling model.eval() because that just
        calls this with mode == False.
        )mode)r   r   r   r   r   r   )r   r   r   s     r   r   zDualGraphModule.train?  sT      	) 	))DJJ 	)$- 	)DJww}}$}'''r   )r   )T)r&   r'   r(   r)   rU   r   r*   r   Graphr+   r   r   r-   r.   s   @r   r   r     s          er)6 )6HO)624()6HJ)6^a)6 )6 )6 )6 )6 )6V( ( ( ( ( ( ( ( ( (r   r   return_nodestrain_return_nodeseval_return_nodesc           	      D	   t          |          }| j        }t          d |||fD                       rt          d          |du |du z  rt          d          |du |du z  st          d          dt          t
          t
          f         fd}|* ||          }t          |          }t          |          }n ||          } ||          }i }	i }
||d}dD ]Z}|d	k    r|                                  n|d
k    r|                                  t          di |}|
                    | |          }t          | t          j                  r| j        j        n| j        }t!          j        |j        ||          }t'          |j                                                  }t-          t/          |                    t-          |          k    rt          d          ||                                         D ]0t3          fd|D                       st          d d          1g }t5          |j        j                  D ]"}|j        dk    r|                    |           #|st          d          |D ]}|j                            |           d |j        j        D             }tA                      }t5          |          D ]}|j        !                    |          }|||         D ]|"                    d          }d#                    |$                    d          d|dz                      k    r.||||                  <   ||         %                                n}tA          t5          t'          |&                                                              }|j        '                    |d                   5  |j        (                    |           ddd           n# 1 swxY w Y   |j        )                                 |*                                 ||	|<   ||
|<   \|stW          |	d	         |	d
                    tY          | |
d	         |
d
         |          }|                     |           |                    |           |S )a2  
    Creates a new graph module that returns intermediate nodes from a given
    model as dictionary with user specified keys as strings, and the requested
    outputs as values. This is achieved by re-writing the computation graph of
    the model via FX to return the desired nodes as outputs. All unused nodes
    are removed, together with their corresponding parameters.

    Desired output nodes must be specified as a ``.`` separated
    path walking the module hierarchy from top level module down to leaf
    operation or leaf module. For more details on the node naming conventions
    used here, please see the :ref:`relevant subheading <about-node-names>`
    in the `documentation <https://pytorch.org/vision/stable/feature_extraction.html>`_.

    Not all models will be FX traceable, although with some massaging they can
    be made to cooperate. Here's a (not exhaustive) list of tips:

        - If you don't need to trace through a particular, problematic
          sub-module, turn it into a "leaf module" by passing a list of
          ``leaf_modules`` as one of the ``tracer_kwargs`` (see example below).
          It will not be traced through, but rather, the resulting graph will
          hold a reference to that module's forward method.
        - Likewise, you may turn functions into leaf functions by passing a
          list of ``autowrap_functions`` as one of the ``tracer_kwargs`` (see
          example below).
        - Some inbuilt Python functions can be problematic. For instance,
          ``int`` will raise an error during tracing. You may wrap them in your
          own function and then pass that in ``autowrap_functions`` as one of
          the ``tracer_kwargs``.

    For further information on FX see the
    `torch.fx documentation <https://pytorch.org/docs/stable/fx.html>`_.

    Args:
        model (nn.Module): model on which we will extract the features
        return_nodes (list or dict, optional): either a ``List`` or a ``Dict``
            containing the names (or partial names - see note above)
            of the nodes for which the activations will be returned. If it is
            a ``Dict``, the keys are the node names, and the values
            are the user-specified keys for the graph module's returned
            dictionary. If it is a ``List``, it is treated as a ``Dict`` mapping
            node specification strings directly to output names. In the case
            that ``train_return_nodes`` and ``eval_return_nodes`` are specified,
            this should not be specified.
        train_return_nodes (list or dict, optional): similar to
            ``return_nodes``. This can be used if the return nodes
            for train mode are different than those from eval mode.
            If this is specified, ``eval_return_nodes`` must also be specified,
            and ``return_nodes`` should not be specified.
        eval_return_nodes (list or dict, optional): similar to
            ``return_nodes``. This can be used if the return nodes
            for train mode are different than those from eval mode.
            If this is specified, ``train_return_nodes`` must also be specified,
            and `return_nodes` should not be specified.
        tracer_kwargs (dict, optional): a dictionary of keyword arguments for
            ``NodePathTracer`` (which passes them onto it's parent class
            `torch.fx.Tracer <https://pytorch.org/docs/stable/fx.html#torch.fx.Tracer>`_).
            By default, it will be set to wrap and make leaf nodes all torchvision ops:
            {"autowrap_modules": (math, torchvision.ops,),"leaf_modules": _get_leaf_modules_for_ops(),}
            WARNING: In case the user provides tracer_kwargs, above default arguments will be appended to the user
            provided dictionary.
        suppress_diff_warning (bool, optional): whether to suppress a warning
            when there are discrepancies between the train and eval version of
            the graph. Defaults to False.
        concrete_args (Optional[Dict[str, any]]): Concrete arguments that should
            not be treated as Proxies. According to the `Pytorch docs
            <https://pytorch.org/docs/stable/fx.html#torch.fx.Tracer.trace>`_,
            this parameter's API may not be guaranteed.

    Examples::

        >>> # Feature extraction with resnet
        >>> model = torchvision.models.resnet18()
        >>> # extract layer1 and layer3, giving as names `feat1` and feat2`
        >>> model = create_feature_extractor(
        >>>     model, {'layer1': 'feat1', 'layer3': 'feat2'})
        >>> out = model(torch.rand(1, 3, 224, 224))
        >>> print([(k, v.shape) for k, v in out.items()])
        >>>     [('feat1', torch.Size([1, 64, 56, 56])),
        >>>      ('feat2', torch.Size([1, 256, 14, 14]))]

        >>> # Specifying leaf modules and leaf functions
        >>> def leaf_function(x):
        >>>     # This would raise a TypeError if traced through
        >>>     return int(x)
        >>>
        >>> class LeafModule(torch.nn.Module):
        >>>     def forward(self, x):
        >>>         # This would raise a TypeError if traced through
        >>>         int(x.shape[0])
        >>>         return torch.nn.functional.relu(x + 4)
        >>>
        >>> class MyModule(torch.nn.Module):
        >>>     def __init__(self):
        >>>         super().__init__()
        >>>         self.conv = torch.nn.Conv2d(3, 1, 3)
        >>>         self.leaf_module = LeafModule()
        >>>
        >>>     def forward(self, x):
        >>>         leaf_function(x.shape[0])
        >>>         x = self.conv(x)
        >>>         return self.leaf_module(x)
        >>>
        >>> model = create_feature_extractor(
        >>>     MyModule(), return_nodes=['leaf_module'],
        >>>     tracer_kwargs={'leaf_modules': [LeafModule],
        >>>                    'autowrap_functions': [leaf_function]})

    c              3      K   | ]}|d u V  	d S r\   r]   )r^   args     r   ra   z+create_feature_extractor.<locals>.<genexpr>  s&      
X
X33$;
X
X
X
X
X
Xr   zcEither `return_nodes` or `train_return_nodes` and `eval_return_nodes` together, should be specifiedNzcIf any of `train_return_nodes` and `eval_return_nodes` are specified, then both should be specifiedz\If `train_return_nodes` and `eval_return_nodes` are specified, then both should be specifiedr!   c                     t          | t                    rd | D             S d |                                 D             S )Nc                 H    i | ]}t          |          t          |           S r]   r+   )r^   is     r   
<dictcomp>z@create_feature_extractor.<locals>.to_strdict.<locals>.<dictcomp>  s&    ...qCFFCFF...r   c                 N    i | ]"\  }}t          |          t          |          #S r]   r   )r^   kvs      r   r   z@create_feature_extractor.<locals>.to_strdict.<locals>.<dictcomp>  s*    55541aAA555r   )r#   ro   items)ns    r   
to_strdictz,create_feature_extractor.<locals>.to_strdict  sD    a 	/..A....55177995555r   )r   r   r   r   r   zYThere are duplicate nodes! Please raise an issue https://github.com/pytorch/vision/issuesc                 F    g | ]}t          j        d  d|          duS )^z(\.|$)N)rJ   rK   )r^   r   querys     r   
<listcomp>z,create_feature_extractor.<locals>.<listcomp>  s6    ]]]!3e!3!3!3Q77tC]]]r   znode: 'z' is not present in model. Hint: use `get_graph_node_names` to make sure the `return_nodes` you specified are present. It may even be that you need to specify `train_return_nodes` and `eval_return_nodes` separately.outputz1No output nodes found in graph_module.graph.nodesc                     g | ]}|S r]   r]   )r^   r   s     r   r   z,create_feature_extractor.<locals>.<listcomp>  s    555q555r   rF   rG   )r   r]   )-r   r   re   
ValueErrorr   r+   r   r   r   r0   r   r#   r   r*   r   r&   r   r   r   ro   r4   rN   rI   r   keysrb   rM   r   r   rH   r   
erase_noder   getcountjoinsplitr   r   inserting_afterr   eliminate_dead_code	recompilerw   r   )r   r   r   r   r   r   r   r   r   tracersgraphsmode_return_nodesr   tracerr   rA   graph_moduleavailable_nodesorig_output_nodesr   r   output_nodesr    depthr   s                           @r   r   r   N  s   j /}==M.K

X
X<1CEV"W
X
X
XXX 
q
 
 	
 	d"'8D'@A 
q
 
 	
 T!&8D&@A ywxxx6c3h 6 6 6 6
 !!z,//%l33$\22'Z(:;;&J'899 GF=OYj3k3k! H H7??KKMMMMV^^JJLLL  00-00U-@@+5eRY+G+G[u''U^~fk5$??v6==??@@s?##$$O(<(<<<k   't,1133 
	 
	E ]]]]_]]]^^  6e 6 6 6   ,,233 	, 	,Atx!((+++  	RPQQQ" 	- 	-A))!,,,, 65L.4555"}}% 	 	A$599!<<O&
 *40  C((88O11#66{{CDDMMCDL!24!8!?@%d+//666E N #8D1C1C1E1E,F,F#G#GHH //b	:: 	4 	4%%l333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	..000    t ! C 0'&/BBB #5&/6&>VZ[[[L 
KK{###s   O55O9	<O9	)NFN)NNNNFN)(ry   r   rJ   rq   collectionsr   copyr   	itertoolsr   typingr   r   r   r	   r
   r   r   rU   r{   r   r   torch.fx.graph_moduler   __all__Tracerr   r0   rh   rw   r   r   r+   r   r*   r,   r   r   r   r   r]   r   r   <module>r      s     				  # # # # # #             D D D D D D D D D D D D D D D D D D              , , , , , , &'=
>: : : : :BI : : :(` ` ` ` `* ` ` `FK K K(. (~ ( ( ( (>4:     8DcN3K  PTUXZ]U]P^        & /3"'.2	># >#9>#DcN+>#  ># DcN+	>#
 49d3i ># ># ># >#B?( ?( ?( ?( ?(bn ?( ?( ?(H @DEIDH.2"'.2n n9n5cDcN!:;<n !tCy$sCx.'@!ABn  d3ic3h&? @A	n
 DcN+n  n DcN+n ^n n n n n nr   